Various small fixes

This commit is contained in:
Christopher Schnick 2023-01-01 16:38:52 +01:00
parent 742dde4820
commit b14393134f
10 changed files with 75 additions and 36 deletions

View file

@ -2,8 +2,7 @@ package io.xpipe.beacon;
import io.xpipe.beacon.exchange.StopExchange;
import io.xpipe.core.impl.FileNames;
import io.xpipe.core.impl.LocalStore;
import io.xpipe.core.process.ShellProcessControl;
import io.xpipe.core.process.OsType;
import io.xpipe.core.process.ShellTypes;
import io.xpipe.core.util.XPipeInstallation;
@ -111,17 +110,16 @@ public class BeaconServer {
}
public static String getDaemonDebugExecutable(String installationBase) throws Exception {
try (ShellProcessControl pc = new LocalStore().create().start()) {
var debug = BeaconConfig.launchDaemonInDebugMode();
if (!debug) {
throw new IllegalStateException();
var osType = OsType.getLocal();
var debug = BeaconConfig.launchDaemonInDebugMode();
if (!debug) {
throw new IllegalStateException();
} else {
if (BeaconConfig.attachDebuggerToDaemon()) {
return FileNames.join(
installationBase, XPipeInstallation.getDaemonDebugAttachScriptPath(osType));
} else {
if (BeaconConfig.attachDebuggerToDaemon()) {
return FileNames.join(
installationBase, XPipeInstallation.getDaemonDebugAttachScriptPath(pc.getOsType()));
} 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.StreamDataStore;
import io.xpipe.core.util.JacksonizedValue;
import io.xpipe.core.util.ValidationException;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;
@ -59,13 +60,13 @@ public class FileStore extends JacksonizedValue implements FilenameStore, Stream
@Override
public void checkComplete() throws Exception {
if (fileSystem == null) {
throw new IllegalStateException("File system is missing");
throw new ValidationException("File system is missing");
}
if (file == null) {
throw new IllegalStateException("File is missing");
throw new ValidationException("File is missing");
}
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;
import io.xpipe.core.process.ShellProcessControl;
import io.xpipe.core.process.ShellTypes;
import java.util.ServiceLoader;
@ -9,6 +10,10 @@ public abstract class LocalProcessControlProvider {
private static LocalProcessControlProvider INSTANCE;
public static LocalProcessControlProvider get() {
if (INSTANCE == null) {
throw new IllegalStateException("Process control not initialized");
}
return INSTANCE;
}
@ -16,10 +21,10 @@ public abstract class LocalProcessControlProvider {
INSTANCE = layer != null
? ServiceLoader.load(layer, LocalProcessControlProvider.class)
.findFirst()
.orElse(null)
.orElseThrow()
: ServiceLoader.load(LocalProcessControlProvider.class)
.findFirst()
.orElse(null);
.orElseThrow();
}
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 {
try (var sub = subShell(type).start()) {
return sub.executeStringSimpleCommand(command);

View file

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

View file

@ -2,7 +2,7 @@ package io.xpipe.core.util;
import io.xpipe.core.charsetter.NewLine;
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.StringWriter;
@ -67,13 +67,13 @@ public class Deobfuscator {
var file = Files.createTempFile("xpipe_stracktrace", null);
Files.writeString(file, stackTrace);
var proc = new ProcessBuilder(
"retrace." + (OsType.getLocal().equals(OsType.WINDOWS) ? "bat" : "sh"),
System.getenv("XPIPE_MAPPING"),
file.toString()
)
"retrace." + (OsType.getLocal().equals(OsType.WINDOWS) ? "bat" : "sh"),
System.getenv("XPIPE_MAPPING"),
file.toString())
.redirectErrorStream(true);
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();
if (code == 0) {
return out;
@ -104,10 +104,7 @@ public class Deobfuscator {
return false;
}
if (OsType.getLocal().equals(OsType.LINUX)) {
return ShellStore.local().create().executeBooleanSimpleCommand("which retrace.sh");
}
return true;
var t = ShellTypes.getPlatformDefault();
return LocalProcess.executeSimpleBooleanCommand(t.createWhichCommand("retrace." + t.getScriptFileEnding()));
}
}

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,10 +31,14 @@ public class XPipeInstallation {
Path path = Path.of(ProcessHandle.current().info().command().orElseThrow());
var name = path.getFileName().toString();
if (name.endsWith("java") || name.endsWith("java.exe")) {
return Path.of(System.getProperty("user.dir"));
var isImage = ModuleHelper.isImage();
if (!isImage) {
return Path.of(System.getProperty("user.dir"));
}
return getLocalInstallationBasePathForJavaExecutable(path);
} else {
return getLocalInstallationBasePathForExecutable(path);
}
return getLocalInstallationBasePathForExecutable(path);
}
public static Path getLocalDynamicLibraryDirectory() {
@ -55,6 +59,16 @@ public class XPipeInstallation {
: 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) {
if (OsType.getLocal().equals(OsType.MAC)) {
return executable.getParent().getParent().getParent();

View file

@ -19,13 +19,13 @@ public class XPipeTempDirectory {
public static String get(ShellProcessControl proc) throws Exception {
var base = proc.getOsType().getTempDirectory(proc);
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.getOsType().equals(OsType.LINUX) || proc.getOsType().equals(OsType.MAC)) {
proc.executeSimpleCommand("(chmod -f 777 \"" + dir + "\" || true)");
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)) {
proc.executeSimpleCommand("(chmod -f 777 \"" + dir + "\"");
}
}
return dir;

View file

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