From 5ce83ddef28adb358dd4983503b07fcd847d19ec Mon Sep 17 00:00:00 2001 From: Christopher Schnick Date: Sun, 11 Dec 2022 11:19:12 +0100 Subject: [PATCH] More shell fixes --- .../io/xpipe/core/process/ProcessControl.java | 2 ++ .../core/process/ShellProcessControl.java | 2 -- .../java/io/xpipe/core/process/ShellType.java | 2 +- .../java/io/xpipe/core/process/ShellTypes.java | 18 +++++++++--------- .../java/io/xpipe/core/store/MachineStore.java | 13 +++++++++---- .../io/xpipe/core/util/XPipeTempDirectory.java | 6 ++++++ 6 files changed, 27 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/io/xpipe/core/process/ProcessControl.java b/core/src/main/java/io/xpipe/core/process/ProcessControl.java index c814645f7..6a401e1b5 100644 --- a/core/src/main/java/io/xpipe/core/process/ProcessControl.java +++ b/core/src/main/java/io/xpipe/core/process/ProcessControl.java @@ -20,6 +20,8 @@ public interface ProcessControl extends Closeable, AutoCloseable { void writeLine(String line) throws IOException; + void write(byte[] b) throws IOException; + @Override void close() throws IOException; void kill() throws Exception; diff --git a/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java b/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java index 03b2b04ef..38dcf50a2 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java +++ b/core/src/main/java/io/xpipe/core/process/ShellProcessControl.java @@ -53,8 +53,6 @@ public interface ShellProcessControl extends ProcessControl { ShellProcessControl elevation(SecretValue value); - ShellProcessControl startTimeout(Integer timeout); - SecretValue getElevationPassword(); default ShellProcessControl subShell(@NonNull ShellType type) { diff --git a/core/src/main/java/io/xpipe/core/process/ShellType.java b/core/src/main/java/io/xpipe/core/process/ShellType.java index c85c71d40..7d20e3bcc 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellType.java +++ b/core/src/main/java/io/xpipe/core/process/ShellType.java @@ -76,7 +76,7 @@ public interface ShellType { List createMkdirsCommand(String dirs); - List createFileReadCommand(String file); + String createFileReadCommand(String file); String createFileWriteCommand(String file); diff --git a/core/src/main/java/io/xpipe/core/process/ShellTypes.java b/core/src/main/java/io/xpipe/core/process/ShellTypes.java index d634a1578..c47d3ef3f 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellTypes.java +++ b/core/src/main/java/io/xpipe/core/process/ShellTypes.java @@ -154,8 +154,8 @@ public class ShellTypes { } @Override - public List createFileReadCommand(String file) { - return List.of("type", file); + public String createFileReadCommand(String file) { + return "type \"" + file + "\""; } @Override @@ -227,7 +227,7 @@ public class ShellTypes { @Override public String getExecutable() { - return "powershell.exe"; + return "powershell"; } @Override @@ -242,7 +242,7 @@ public class ShellTypes { @Override public String queryShellProcessId(ShellProcessControl control) throws IOException { - control.writeLine("powershell (Get-WmiObject Win32_Process -Filter ProcessId=$PID).ParentProcessId"); + control.writeLine("echo $PID"); var r = new BufferedReader(new InputStreamReader(control.getStdout(), StandardCharsets.US_ASCII)); // Read echo of command @@ -329,8 +329,8 @@ public class ShellTypes { } @Override - public List createFileReadCommand(String file) { - return List.of("cmd", "/c", "type", file); + public String createFileReadCommand(String file) { + return "cmd /c type \""+ file + "\""; } @Override @@ -368,7 +368,7 @@ public class ShellTypes { // Read actual output var line = r.readLine(); - if (line.equals("Not Windows")) { + if (line.contains("Not Windows")) { return StandardCharsets.UTF_8; } @@ -509,8 +509,8 @@ public class ShellTypes { } @Override - public List createFileReadCommand(String file) { - return List.of("cat", file); + public String createFileReadCommand(String file) { + return "cat \"" + file + "\""; } @Override diff --git a/core/src/main/java/io/xpipe/core/store/MachineStore.java b/core/src/main/java/io/xpipe/core/store/MachineStore.java index 9e4ae8c35..a387682cf 100644 --- a/core/src/main/java/io/xpipe/core/store/MachineStore.java +++ b/core/src/main/java/io/xpipe/core/store/MachineStore.java @@ -11,19 +11,22 @@ public interface MachineStore extends FileSystemStore, ShellStore { @Override public default InputStream openInput(String file) throws Exception { - return create().command(proc -> proc.getShellType().flatten(proc.getShellType().createFileReadCommand(proc.getOsType().normalizeFileName(file)))) + return create().command(proc -> proc.getShellType() + .createFileReadCommand(proc.getOsType().normalizeFileName(file))) .startExternalStdout(); } @Override public default OutputStream openOutput(String file) throws Exception { - return create().command(proc -> proc.getShellType().createFileWriteCommand(proc.getOsType().normalizeFileName(file))) + return create().command(proc -> proc.getShellType() + .createFileWriteCommand(proc.getOsType().normalizeFileName(file))) .startExternalStdin(); } @Override public default boolean exists(String file) throws Exception { - try (var pc = create().command(proc -> proc.getShellType().createFileExistsCommand(proc.getOsType().normalizeFileName(file))) + try (var pc = create().command(proc -> proc.getShellType() + .createFileExistsCommand(proc.getOsType().normalizeFileName(file))) .start()) { return pc.discardAndCheckExit(); } @@ -31,7 +34,9 @@ public interface MachineStore extends FileSystemStore, ShellStore { @Override public default boolean mkdirs(String file) throws Exception { - try (var pc = create().command(proc -> proc.getShellType().flatten(proc.getShellType().createMkdirsCommand(proc.getOsType().normalizeFileName(file)))) + try (var pc = create().command(proc -> proc.getShellType() + .flatten(proc.getShellType() + .createMkdirsCommand(proc.getOsType().normalizeFileName(file)))) .start()) { return pc.discardAndCheckExit(); } diff --git a/core/src/main/java/io/xpipe/core/util/XPipeTempDirectory.java b/core/src/main/java/io/xpipe/core/util/XPipeTempDirectory.java index 2ccd8ce78..5da37d2b1 100644 --- a/core/src/main/java/io/xpipe/core/util/XPipeTempDirectory.java +++ b/core/src/main/java/io/xpipe/core/util/XPipeTempDirectory.java @@ -1,6 +1,7 @@ package io.xpipe.core.util; import io.xpipe.core.impl.FileNames; +import io.xpipe.core.process.OsType; import io.xpipe.core.process.ShellProcessControl; import io.xpipe.core.store.ShellStore; @@ -22,6 +23,11 @@ public class XPipeTempDirectory { throw new IOException("Unable to access or create temporary directory " + dir); } + if (proc.getOsType().equals(OsType.LINUX)) { + proc.executeSimpleCommand("(chmod -f 777 \"" + dir + "\" || true)"); + + } + return dir; }