aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/jshipit/SysUtils.java
diff options
context:
space:
mode:
authoraxtloss <axtlos@getcryst.al>2023-05-26 00:03:58 +0200
committeraxtloss <axtlos@getcryst.al>2023-05-26 00:03:58 +0200
commit87cc052844e2cacca591edd68884f1e7f6c49a3a (patch)
tree2e39577998921746253a7ed1ea08ee99e6d4ecc9 /src/main/java/io/github/jshipit/SysUtils.java
parent3fdb1012609b71a41e551ea0aad3b8a27b134e21 (diff)
downloadjshipit-87cc052844e2cacca591edd68884f1e7f6c49a3a.tar.gz
jshipit-87cc052844e2cacca591edd68884f1e7f6c49a3a.tar.bz2
Add comments to functions
Diffstat (limited to '')
-rw-r--r--src/main/java/io/github/jshipit/SysUtils.java38
1 files changed, 35 insertions, 3 deletions
diff --git a/src/main/java/io/github/jshipit/SysUtils.java b/src/main/java/io/github/jshipit/SysUtils.java
index 59450ad..ed14612 100644
--- a/src/main/java/io/github/jshipit/SysUtils.java
+++ b/src/main/java/io/github/jshipit/SysUtils.java
@@ -4,10 +4,19 @@ import com.sun.jna.Platform;
import java.io.File;
+/*
+* A class for running system commands
+ */
public class SysUtils {
-
+ /*
+ * Untar a tarball to a directory
+ *
+ * @param in The tarball to untar
+ * @param out The directory to untar to
+ */
public void untar(String in, String out) {
+ // We do not use the Java tar library because it does not care about unix permissions. Common Java L
new File(out).mkdirs();
ProcessBuilder pb = new ProcessBuilder("tar", "-xf", in, "-C", out);
pb.inheritIO();
@@ -19,10 +28,20 @@ public class SysUtils {
}
}
+ /*
+ * Execute a command in a bwrap sandbox
+ *
+ * @param args The arguments to pass to bwrap
+ * @param execute Whether to execute the command or just return the command
+ *
+ * @return The command that was executed. Empty string if execute is true
+ */
public String execInBwrap(String[] args, boolean execute) {
if (!execute) {
return "bwrap "+String.join(" ", args);
}
+ // ProcessBuilder wraps the args in quotes, since all args are passed at once, they are all quoted together
+ // This breaks bwrap (and many other commands), so we use bash -c to execute the command with the entire command as the arg
ProcessBuilder pb = new ProcessBuilder("bash", "-c", "bwrap "+String.join(" ", args));
pb.inheritIO();
try {
@@ -34,12 +53,24 @@ public class SysUtils {
return "";
}
+ /*
+ * Creates an overlay mount
+ *
+ * @param lower The lower directories to use
+ * @param upper The upper directory to use
+ * @param target The target directory to mount to
+ * @param work The work directory to use
+ * @param execute Whether to execute the command or just return the command
+ *
+ * @return The command that was executed. Empty string if execute is true
+ */
public String overlayMount(String[] lower, String upper, String target, String work, boolean execute) {
if (!execute) {
return "mount -t overlay overlay -o lowerdir="+String.join(":", lower)+",upperdir="+upper+",workdir="+work+" "+target;
}
if (Platform.isLinux()) {
-
+ // unshare creates a new user namespace, so we can mount without root
+ // This only works on Linux version 5.11 and above, which everyone should have by now
ProcessBuilder pb = new ProcessBuilder("unshare", "--user", "--map-root-user", "--mount", "mount", "-t", "overlay", "overlay", "-o", "lowerdir="+String.join(":", lower)+",upperdir="+upper+",workdir="+work, target);
pb.inheritIO();
try {
@@ -50,8 +81,9 @@ public class SysUtils {
}
} else {
+ // Since other *nix systems do not have overlayfs or unshare, we just print an error
+ // I would be surprised if you even got this far on Windows
System.out.println("Platform not supported.");
- System.out.println("mount -t overlay overlay -o lowerdir="+String.join(":", lower)+",upperdir="+upper+",workdir="+work+" "+target);
}
return "";
}