在使用JAVA启动Process之前设置umask

时间:2018-03-31 08:38:31

标签: java process file-permissions umask

我会在使用java启动进程之前添加一个umask,但java进程api(我使用java 9)不提供设置umask的方法。

我尝试通过执行umask来运行一个进程,该值的值允许我创建但不能读取(0644)。它不起作用......

Optional<String> umask = ProcessUtil.getUmask();
if (!umask.isPresent()) {
    this.restartOnFail();
    return;
}
ProcessUtil.setUmask(parent.config.umask);
this.timeAtLaunch = System.currentTimeMillis();
this.status = ProcessStatus.LAUNCHING;
this.process = pb.start();
this.onExitProcess();
ProcessUtil.setUmask(umask.get());

setUmask代码是:

public static void setUmask(String mask) {
    try {
        String[] commands = {"umask", mask};
        Process exec = Runtime.getRuntime().exec(commands);
        exec.waitFor();
    } catch (Exception e) {
        Server.global.logErr("Impossible to set umask for %s.", mask);
    }
}

我试图这样做但似乎无法工作(我启动了一个创建文件并读取它的进程,它应该只创建它并在读取时发出错误。)因为进程创建了一个文件,没有umask设置的权限。他能够阅读该文件,这不应该发生......

我创建了一个ready runnable类:

import java.io.*;

public class StackOverFlowTest {

    public static void setUmask(String mask) {
        try {
            String[] commands = {"umask", mask};
            Process exec = Runtime.getRuntime().exec(commands);
            exec.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void runCommand(String [] command) throws IOException, InterruptedException {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(command);
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
        String s = null;
        while ((s = stdInput.readLine()) != null) {
            System.out.println("stdout: " + s);
        }
        while ((s = stdError.readLine()) != null) {
            System.out.println("stderr: " + s);
        }
        proc.waitFor();
    }

    public static void main(String[] args) {
        setUmask("0644"); // set this permission -----w--w-

        String[] create = {"touch", "file.stackoverflow"};
        String[] read = {"cat", "file.stackoverflow"};

        try {
            runCommand(create);
            runCommand(read);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

当cat启动时,这段代码实际上没有显示任何错误,这是一个问题!

(对不起我的英文)

所以有人有提示启动流程以及使用java的一些权限吗?

0 个答案:

没有答案