简体   繁体   English

在UNIX上使用NIO时未设置写权限

[英]Write permissions not getting set when using NIO on unix

I am using NIO to set permissions on all directories in a path as below. 我正在使用NIO在以下路径中的所有目录上设置权限。 i am trying to give 777 permissions , however the "w" part is not getting applied...Whats wrong here? 我正在尝试授予777权限,但是“ w”部分没有得到应用...这里怎么了?

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;

public class permissionTest {

    public static void main(String [] args) throws IOException{

        Path dirPath = Paths.get("./part1/part2/part3");


        Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rwxrwxrwx");
        System.out.print(permissions.toString() + ' ');

        FileAttribute<Set<PosixFilePermission>> fileAttributes = 

                PosixFilePermissions.asFileAttribute(permissions);

        Files.createDirectories(dirPath, fileAttributes);

    }

}

$ ls -ld part1/
drwxr-xr-x 3 * *4096 Oct 30 02:48 part1/

This is because the umask is applied. 这是因为应用了umask。

Try and type this at the shell: 尝试在外壳上键入:

umask 0

and then rerun your program (or launch your IDE from the command line and run the code). 然后重新运行您的程序(或从命令行启动IDE并运行代码)。 It also means that if you do umask 027 , the permissions for your created directories will be 750. 这也意味着,如果您执行umask 027 ,则所创建目录的权限将为750。

Unfortunately, you cannot change your process' umask (since this is per process, and inherited) in Java itself... 不幸的是,您无法在Java本身中更改进程的umask(因为这是每个进程并继承的)...


Note that there is this method to set "absolute" file permissions. 请注意,有一种方法可以设置“绝对”文件权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM