aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/jshipit/Mount.java
blob: 63b60dd96ebc080b71b2b451326bf3b6c37eacd7 (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
package io.github.jshipit;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

public class Mount {
    public interface CLibrary extends Library {
        int mount(String source, String target,
                  String filesystemtype, int mountflags,
                  String data);

        int umount(String target);
    }

    public void mount(String source, String target,
                      String filesystemtype, int mountflags,
                      String data) {
        CLibrary libc;

        if (Platform.isLinux()) {
            libc = Native.load("c", CLibrary.class);
            int result = libc.mount(source, target, filesystemtype, mountflags, data);
            if (result == 0) {
                System.out.println("Device mounted successfully.");
            } else {
                System.out.println("Device mount failed.");
            }
        }
    }

    public void overlayMount(String[] lower, String upper, String target) {
        CLibrary libc;

        if (Platform.isLinux()) {
            libc = Native.load("c", CLibrary.class);
            int result = libc.mount("overlay", target, "overlay", 0, "lowerdir="+String.join(":", lower)+",upperdir="+upper+",workdir="+target+"/work");
            if (result == 0) {
                System.out.println("Device mounted successfully.");
            } else {
                System.out.println("Device mount failed.");
            }
        } else {
            System.out.println("Platform not supported.");
            System.out.println("mount -t overlay overlay -o lowerdir="+String.join(":", lower)+",upperdir="+upper+",workdir="+target+"/work "+target);
        }
    }
}