How can I set the umask from within java?

后端 未结 4 960
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 13:36

I\'m new to Java. Where is umask exposed in the api?

相关标签:
4条回答
  • 2020-11-30 14:20
    import java.nio.file.Files
    import java.nio.file.attribute.PosixFilePermission
    
    File file = new File("/some/path") 
    Files.setPosixFilePermissions(file.toPath(), [
                    PosixFilePermission.OWNER_READ,
                    PosixFilePermission.OWNER_WRITE
                ].toSet())
    
    0 讨论(0)
  • 2020-11-30 14:21

    Another approach is to use a 3rd-party Java library that exposes POSIX system calls; e.g.

    • Jtux
    • The "Posix for Java" library,
    • and so on (Google for "java posix library").

    The problem with this approach is that it is intrinsically non-portable (won't work on a non-POSIX compliant platform), and requires a platform-specific native library ... and all that that entails.

    0 讨论(0)
  • 2020-11-30 14:24

    java.nio.file.attribute.PosixFileAttributes in Java SE 7.

    0 讨论(0)
  • 2020-11-30 14:26

    You can't fiddle with the umask directly, since Java is an abstraction and the umask is POSIX-implementation specific. But you have the following API:

    File f;
    f.setExecutable(true);
    f.setReadable(false);
    f.setWritable(true);
    

    There are some more APIs available, check the docs.

    If you must have direct access to the umask, either do it via JNI and the chmod() syscall, or spawn a new process with exec("chmod").

    0 讨论(0)
提交回复
热议问题