diff --git a/core/src/main/java/io/xpipe/core/process/ElevationFunction.java b/core/src/main/java/io/xpipe/core/process/ElevationFunction.java index ff139840..25afe2c9 100644 --- a/core/src/main/java/io/xpipe/core/process/ElevationFunction.java +++ b/core/src/main/java/io/xpipe/core/process/ElevationFunction.java @@ -4,6 +4,34 @@ import io.xpipe.core.util.FailableFunction; public interface ElevationFunction { + static ElevationFunction ifNotRoot(ElevationFunction function) { + return new ElevationFunction() { + @Override + public String getPrefix() { + return function.getPrefix(); + } + + @Override + public boolean isSpecified() { + return true; + } + + @Override + public boolean apply(ShellControl shellControl) throws Exception { + if (shellControl.getOsType() == OsType.WINDOWS) { + return false; + } + + var isRoot = shellControl.executeSimpleBooleanCommand("test \"${EUID:-$(id -u)}\" -eq 0"); + if (isRoot) { + return false; + } + + return function.apply(shellControl); + } + }; + } + static ElevationFunction of(String prefix, FailableFunction f) { return new ElevationFunction() { @Override diff --git a/core/src/main/java/io/xpipe/core/process/ShellControl.java b/core/src/main/java/io/xpipe/core/process/ShellControl.java index 2b52f330..a50e1266 100644 --- a/core/src/main/java/io/xpipe/core/process/ShellControl.java +++ b/core/src/main/java/io/xpipe/core/process/ShellControl.java @@ -207,7 +207,10 @@ public interface ShellControl extends ProcessControl { return CommandBuilder.ofString(command); } }; - return singularSubShell(o); + var sc = singularSubShell(o); + sc.withSourceStore(getSourceStore().orElse(null)); + sc.setParentSystemAccess(ParentSystemAccess.identity()); + return sc; } default T enforceDialect(@NonNull ShellDialect type, FailableFunction sc)