aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/jshipit/SysUtils.java
blob: 144c401c2f421fc0d2b9f337b18669b9ea2e2f90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package io.github.jshipit;

import com.sun.jna.Platform;

import java.io.File;

public class SysUtils {

    public void chmod(String path, int mode) {
        ProcessBuilder pb = new ProcessBuilder("chmod", Integer.toString(mode), path);
        pb.inheritIO();
        try {
            Process p = pb.start();
            p.waitFor();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void untar(String in, String out) {
        new File(out).mkdirs();
        ProcessBuilder pb = new ProcessBuilder("tar", "-xf", in, "-C", out);
        pb.inheritIO();
        try {
            Process p = pb.start();
            p.waitFor();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public String execInBwrap(String[] args, boolean execute) {
        //System.out.println("bwrap "+String.join(" ", args));
        if (!execute) {
            return "bwrap "+String.join(" ", args);
        }
        ProcessBuilder pb = new ProcessBuilder("bash", "-c", "bwrap "+String.join(" ", args));
        pb.inheritIO();
        try {
            Process p = pb.start();
            p.waitFor();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "";
    }

    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()) {

            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 {
                Process p = pb.start();
                p.waitFor();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        } else {
            System.out.println("Platform not supported.");
            System.out.println("mount -t overlay overlay -o lowerdir="+String.join(":", lower)+",upperdir="+upper+",workdir="+work+" "+target);
        }
        return "";
    }
}