More shell fixes

This commit is contained in:
Christopher Schnick
2022-12-11 11:19:12 +01:00
parent ec62a12a20
commit 5ce83ddef2
6 changed files with 27 additions and 16 deletions
@@ -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;
@@ -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) {
@@ -76,7 +76,7 @@ public interface ShellType {
List<String> createMkdirsCommand(String dirs);
List<String> createFileReadCommand(String file);
String createFileReadCommand(String file);
String createFileWriteCommand(String file);
@@ -154,8 +154,8 @@ public class ShellTypes {
}
@Override
public List<String> 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<String> 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<String> createFileReadCommand(String file) {
return List.of("cat", file);
public String createFileReadCommand(String file) {
return "cat \"" + file + "\"";
}
@Override
@@ -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();
}
@@ -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;
}