aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/jshipit/SysUtils.java
blob: e6de8865257e22e05533f824ea859c39ba51d71a (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
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 void execInBwrap(String[] args) {
        //System.out.println("bwrap "+String.join(" ", args));
        ProcessBuilder pb = new ProcessBuilder("bwrap", args.toString());
        pb.inheritIO();
        try {
            Process p = pb.start();
            p.waitFor();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void overlayMount(String[] lower, String upper, String target, String work) {
        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);
        }
    }
}