Various small fixes

This commit is contained in:
Christopher Schnick 2023-01-01 16:38:52 +01:00
parent 23d0fcee57
commit ac16967efd
11 changed files with 90 additions and 36 deletions

View file

@ -2,8 +2,7 @@ package io.xpipe.beacon;
import io.xpipe.beacon.exchange.StopExchange; import io.xpipe.beacon.exchange.StopExchange;
import io.xpipe.core.impl.FileNames; import io.xpipe.core.impl.FileNames;
import io.xpipe.core.impl.LocalStore; import io.xpipe.core.process.OsType;
import io.xpipe.core.process.ShellProcessControl;
import io.xpipe.core.process.ShellTypes; import io.xpipe.core.process.ShellTypes;
import io.xpipe.core.util.XPipeInstallation; import io.xpipe.core.util.XPipeInstallation;
@ -111,17 +110,16 @@ public class BeaconServer {
} }
public static String getDaemonDebugExecutable(String installationBase) throws Exception { public static String getDaemonDebugExecutable(String installationBase) throws Exception {
try (ShellProcessControl pc = new LocalStore().create().start()) { var osType = OsType.getLocal();
var debug = BeaconConfig.launchDaemonInDebugMode(); var debug = BeaconConfig.launchDaemonInDebugMode();
if (!debug) { if (!debug) {
throw new IllegalStateException(); throw new IllegalStateException();
} else { } else {
if (BeaconConfig.attachDebuggerToDaemon()) { if (BeaconConfig.attachDebuggerToDaemon()) {
return FileNames.join( return FileNames.join(
installationBase, XPipeInstallation.getDaemonDebugAttachScriptPath(pc.getOsType())); installationBase, XPipeInstallation.getDaemonDebugAttachScriptPath(osType));
} else { } else {
return FileNames.join(installationBase, XPipeInstallation.getDaemonDebugScriptPath(pc.getOsType())); return FileNames.join(installationBase, XPipeInstallation.getDaemonDebugScriptPath(osType));
}
} }
} }
} }

View file

@ -5,6 +5,7 @@ import io.xpipe.core.store.FileSystemStore;
import io.xpipe.core.store.FilenameStore; import io.xpipe.core.store.FilenameStore;
import io.xpipe.core.store.StreamDataStore; import io.xpipe.core.store.StreamDataStore;
import io.xpipe.core.util.JacksonizedValue; import io.xpipe.core.util.JacksonizedValue;
import io.xpipe.core.util.ValidationException;
import lombok.Getter; import lombok.Getter;
import lombok.experimental.SuperBuilder; import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized; import lombok.extern.jackson.Jacksonized;
@ -59,13 +60,13 @@ public class FileStore extends JacksonizedValue implements FilenameStore, Stream
@Override @Override
public void checkComplete() throws Exception { public void checkComplete() throws Exception {
if (fileSystem == null) { if (fileSystem == null) {
throw new IllegalStateException("File system is missing"); throw new ValidationException("File system is missing");
} }
if (file == null) { if (file == null) {
throw new IllegalStateException("File is missing"); throw new ValidationException("File is missing");
} }
if (!FileNames.isAbsolute(file)) { if (!FileNames.isAbsolute(file)) {
throw new IllegalStateException("File path is not absolute"); throw new ValidationException("File path is not absolute");
} }
} }

View file

@ -1,6 +1,7 @@
package io.xpipe.core.impl; package io.xpipe.core.impl;
import io.xpipe.core.process.ShellProcessControl; import io.xpipe.core.process.ShellProcessControl;
import io.xpipe.core.process.ShellTypes;
import java.util.ServiceLoader; import java.util.ServiceLoader;
@ -9,6 +10,10 @@ public abstract class LocalProcessControlProvider {
private static LocalProcessControlProvider INSTANCE; private static LocalProcessControlProvider INSTANCE;
public static LocalProcessControlProvider get() { public static LocalProcessControlProvider get() {
if (INSTANCE == null) {
throw new IllegalStateException("Process control not initialized");
}
return INSTANCE; return INSTANCE;
} }
@ -16,10 +21,10 @@ public abstract class LocalProcessControlProvider {
INSTANCE = layer != null INSTANCE = layer != null
? ServiceLoader.load(layer, LocalProcessControlProvider.class) ? ServiceLoader.load(layer, LocalProcessControlProvider.class)
.findFirst() .findFirst()
.orElse(null) .orElseThrow()
: ServiceLoader.load(LocalProcessControlProvider.class) : ServiceLoader.load(LocalProcessControlProvider.class)
.findFirst() .findFirst()
.orElse(null); .orElseThrow();
} }
public static ShellProcessControl create() { public static ShellProcessControl create() {

View file

@ -35,6 +35,15 @@ public interface ShellProcessControl extends ProcessControl {
} }
} }
default void executeSimpleCommand(String command,String failMessage) throws Exception {
try (CommandProcessControl c = command(command).start()) {
c.discardOrThrow();
} catch (ProcessOutputException out) {
var message = out.getMessage();
throw new ProcessOutputException(message != null ? failMessage + ": " + message : failMessage);
}
}
default String executeStringSimpleCommand(ShellType type, String command) throws Exception { default String executeStringSimpleCommand(ShellType type, String command) throws Exception {
try (var sub = subShell(type).start()) { try (var sub = subShell(type).start()) {
return sub.executeStringSimpleCommand(command); return sub.executeStringSimpleCommand(command);

View file

@ -89,6 +89,8 @@ public interface ShellType {
String createFileExistsCommand(String file); String createFileExistsCommand(String file);
String createFileTouchCommand(String file);
String createWhichCommand(String executable); String createWhichCommand(String executable);
Charset determineCharset(ShellProcessControl control) throws Exception; Charset determineCharset(ShellProcessControl control) throws Exception;

View file

@ -175,6 +175,11 @@ public class ShellTypes {
return String.format("dir /a \"%s\"", file); return String.format("dir /a \"%s\"", file);
} }
@Override
public String createFileTouchCommand(String file) {
return "COPY NUL \"" + file + "\"";
}
@Override @Override
public String createWhichCommand(String executable) { public String createWhichCommand(String executable) {
return "where \"" + executable + "\""; return "where \"" + executable + "\"";
@ -222,6 +227,11 @@ public class ShellTypes {
@Value @Value
public static class PowerShell implements ShellType { public static class PowerShell implements ShellType {
@Override
public String createFileTouchCommand(String file) {
return "$error_count=$error.Count; Out-File -FilePath \"" + file + "\"; $LASTEXITCODE=$error.Count - $error_count";
}
@Override @Override
public String getOrConcatenationOperator() { public String getOrConcatenationOperator() {
return ";"; return ";";
@ -404,6 +414,11 @@ public class ShellTypes {
public abstract static class PosixBase implements ShellType { public abstract static class PosixBase implements ShellType {
@Override
public String createFileTouchCommand(String file) {
return "touch \"" + file + "\"";
}
@Override @Override
public List<String> executeCommandListWithShell(String cmd) { public List<String> executeCommandListWithShell(String cmd) {
return List.of(getExecutable(), "-c", cmd); return List.of(getExecutable(), "-c", cmd);

View file

@ -2,7 +2,7 @@ package io.xpipe.core.util;
import io.xpipe.core.charsetter.NewLine; import io.xpipe.core.charsetter.NewLine;
import io.xpipe.core.process.OsType; import io.xpipe.core.process.OsType;
import io.xpipe.core.store.ShellStore; import io.xpipe.core.process.ShellTypes;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
@ -69,11 +69,11 @@ public class Deobfuscator {
var proc = new ProcessBuilder( var proc = new ProcessBuilder(
"retrace." + (OsType.getLocal().equals(OsType.WINDOWS) ? "bat" : "sh"), "retrace." + (OsType.getLocal().equals(OsType.WINDOWS) ? "bat" : "sh"),
System.getenv("XPIPE_MAPPING"), System.getenv("XPIPE_MAPPING"),
file.toString() file.toString())
)
.redirectErrorStream(true); .redirectErrorStream(true);
var active = proc.start(); var active = proc.start();
var out = new String(active.getInputStream().readAllBytes()).replaceAll("\r\n", NewLine.LF.getNewLineString()); var out = new String(active.getInputStream().readAllBytes())
.replaceAll("\r\n", NewLine.LF.getNewLineString());
var code = active.waitFor(); var code = active.waitFor();
if (code == 0) { if (code == 0) {
return out; return out;
@ -104,10 +104,7 @@ public class Deobfuscator {
return false; return false;
} }
if (OsType.getLocal().equals(OsType.LINUX)) { var t = ShellTypes.getPlatformDefault();
return ShellStore.local().create().executeBooleanSimpleCommand("which retrace.sh"); return LocalProcess.executeSimpleBooleanCommand(t.createWhichCommand("retrace." + t.getScriptFileEnding()));
}
return true;
} }
} }

View file

@ -0,0 +1,12 @@
package io.xpipe.core.util;
import io.xpipe.core.process.ShellTypes;
public class LocalProcess {
public static boolean executeSimpleBooleanCommand(String cmd) throws Exception {
var proc = new ProcessBuilder(ShellTypes.getPlatformDefault().executeCommandListWithShell(cmd)).redirectErrorStream(true).redirectOutput(
ProcessBuilder.Redirect.DISCARD).start();
return proc.waitFor() == 0;
}
}

View file

@ -31,11 +31,15 @@ public class XPipeInstallation {
Path path = Path.of(ProcessHandle.current().info().command().orElseThrow()); Path path = Path.of(ProcessHandle.current().info().command().orElseThrow());
var name = path.getFileName().toString(); var name = path.getFileName().toString();
if (name.endsWith("java") || name.endsWith("java.exe")) { if (name.endsWith("java") || name.endsWith("java.exe")) {
var isImage = ModuleHelper.isImage();
if (!isImage) {
return Path.of(System.getProperty("user.dir")); return Path.of(System.getProperty("user.dir"));
} }
return getLocalInstallationBasePathForJavaExecutable(path);
} else {
return getLocalInstallationBasePathForExecutable(path); return getLocalInstallationBasePathForExecutable(path);
} }
}
public static Path getLocalDynamicLibraryDirectory() { public static Path getLocalDynamicLibraryDirectory() {
Path path = getLocalInstallationBasePath(); Path path = getLocalInstallationBasePath();
@ -55,6 +59,16 @@ public class XPipeInstallation {
: path.resolve("extensions"); : path.resolve("extensions");
} }
private static Path getLocalInstallationBasePathForJavaExecutable(Path executable) {
if (OsType.getLocal().equals(OsType.MAC)) {
return executable.getParent().getParent().getParent().getParent().getParent();
} else if (OsType.getLocal().equals(OsType.LINUX)) {
return executable.getParent().getParent().getParent().getParent();
} else {
return executable.getParent().getParent().getParent();
}
}
private static Path getLocalInstallationBasePathForExecutable(Path executable) { private static Path getLocalInstallationBasePathForExecutable(Path executable) {
if (OsType.getLocal().equals(OsType.MAC)) { if (OsType.getLocal().equals(OsType.MAC)) {
return executable.getParent().getParent().getParent(); return executable.getParent().getParent().getParent();

View file

@ -19,13 +19,13 @@ public class XPipeTempDirectory {
public static String get(ShellProcessControl proc) throws Exception { public static String get(ShellProcessControl proc) throws Exception {
var base = proc.getOsType().getTempDirectory(proc); var base = proc.getOsType().getTempDirectory(proc);
var dir = FileNames.join(base, "xpipe"); var dir = FileNames.join(base, "xpipe");
if (!proc.executeBooleanSimpleCommand(proc.getShellType().flatten(proc.getShellType().createMkdirsCommand(dir)))) {
throw new IOException("Unable to access or create temporary directory " + dir); if (!proc.executeBooleanSimpleCommand(proc.getShellType().createFileExistsCommand(dir))) {
} proc.executeSimpleCommand(proc.getShellType().flatten(proc.getShellType().createMkdirsCommand(dir)), "Unable to access or create temporary directory " + dir);
if (proc.getOsType().equals(OsType.LINUX) || proc.getOsType().equals(OsType.MAC)) { if (proc.getOsType().equals(OsType.LINUX) || proc.getOsType().equals(OsType.MAC)) {
proc.executeSimpleCommand("(chmod -f 777 \"" + dir + "\" || true)"); proc.executeSimpleCommand("(chmod -f 777 \"" + dir + "\"");
}
} }
return dir; return dir;

View file

@ -22,6 +22,7 @@ javadoc{
addStringOption('link', 'https://docs.oracle.com/en/java/javase/19/docs/api/') addStringOption('link', 'https://docs.oracle.com/en/java/javase/19/docs/api/')
addBooleanOption('html5', true) addBooleanOption('html5', true)
addStringOption('Xdoclint:none', '-quiet') addStringOption('Xdoclint:none', '-quiet')
addBooleanOption('-enable-preview', true)
} }
} }