Java - failed to create directory with permission 777 (instead of 775)

I am trying to create a directory with public write rights. To simplify things, I want to have permissions 777. Here is my code:

private static FileAttribute<Set<PosixFilePermission>> DIR_PERMISSIONS;
static {
    Set<PosixFilePermission> perms = new HashSet<>();
    perms.add(PosixFilePermission.OWNER_WRITE);
    perms.add(PosixFilePermission.OWNER_READ);
    perms.add(PosixFilePermission.OWNER_EXECUTE);
    perms.add(PosixFilePermission.GROUP_WRITE);
    perms.add(PosixFilePermission.GROUP_READ);
    perms.add(PosixFilePermission.GROUP_EXECUTE);
    perms.add(PosixFilePermission.OTHERS_WRITE);
    perms.add(PosixFilePermission.OTHERS_READ);
    perms.add(PosixFilePermission.OTHERS_EXECUTE);
    DIR_PERMISSIONS = PosixFilePermissions.asFileAttribute(perms);
}

private Path ensurePath(LocalDate localDate) throws IOException {
    String year = String.valueOf(localDate.getYear());
    String month = String.format("%02d", localDate.getMonthValue());
    Path path = Paths.get(rootDirectory, year, month);
    return Files.createDirectories(path, DIR_PERMISSIONS);
}

With rootDirectory=/tmp/datathis should create folders of type /tmp/data/2016/01, each with permissions 777. Instead, the folders have permissions 775(drwxrwxr-x.), So they lack a public record. Why does it work like this? Perhaps the JVM needs a special parameter to be able to set such permissions? My system is Fedora 24, the application is a standard Spring boot application launched by the maven plugin.

+6
1

, , :

Google : fooobar.com/questions/1014467/... ("Java ." ). , @OlivierGrégoire. umask - .

Spring . . default umask - 0002. /tmp .

public class Test {
    public static void main(String[] args) {
        Set<PosixFilePermission> fullPermission = new HashSet<PosixFilePermission>();
        fullPermission.add(PosixFilePermission.OWNER_EXECUTE);
        fullPermission.add(PosixFilePermission.OWNER_READ);
        fullPermission.add(PosixFilePermission.OWNER_WRITE);

        fullPermission.add(PosixFilePermission.GROUP_EXECUTE);
        fullPermission.add(PosixFilePermission.GROUP_READ);
        fullPermission.add(PosixFilePermission.GROUP_WRITE);

        fullPermission.add(PosixFilePermission.OTHERS_EXECUTE);
        fullPermission.add(PosixFilePermission.OTHERS_READ);
        fullPermission.add(PosixFilePermission.OTHERS_WRITE);

        Path path = Paths.get("/tmp/data/", "01/26");
        try {
            Files.createDirectories(path, PosixFilePermissions.asFileAttribute(fullPermission));            
            outputLS(path);         
            Files.setPosixFilePermissions(path, fullPermission);            
            outputLS(path);         
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void outputLS(Path path) throws IOException {
        System.out.println(new java.util.Scanner(Runtime.getRuntime().exec("ls -alt " + path.toAbsolutePath() + "/..").getInputStream()).useDelimiter("\\A").next());
    }
}

:

total 12
drwxrwxr-x 3 jsampson jsampson 4096 Jan 26 21:53 .
drwxrwxr-x 2 jsampson jsampson 4096 Jan 26 21:53 26
drwxrwxr-x 3 jsampson jsampson 4096 Jan 26 21:53 ..

total 12
drwxrwxr-x 3 jsampson jsampson 4096 Jan 26 21:53 .
drwxrwxrwx 2 jsampson jsampson 4096 Jan 26 21:53 26
drwxrwxr-x 3 jsampson jsampson 4096 Jan 26 21:53 ..

umask / . .

0

Source: https://habr.com/ru/post/1014465/


All Articles