Quirin
Quirin

Reputation: 738

Why file writing permission is not set correctly in Java 7?

Could somebody explain me, why writing permission isn't set for others by the following code snippet on Ubuntu 13.04 and java 1.7 (OpenJDK 64bit). All other permissions are set correctly, except the writing for others, which I think is suspicious.

public void testCreateFileWithPermissions() throws IOException {
    Path target = Paths.get(TEST_FILE);
    cadf.createFileWithPermissions(target, "rwxrwxrwx");
    Set<PosixFilePermission> perms = Files.getPosixFilePermissions(target);
    for (PosixFilePermission perm : perms) {
      System.out.println(perm.toString());
    }
  }

public void createFileWithPermissions(Path target, String permissions)
      throws IOException {
    Set<PosixFilePermission> perms = PosixFilePermissions
        .fromString(permissions);
    FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions
        .asFileAttribute(perms);
    Files.createFile(target, attr);
  }

I got the following output:

OWNER_WRITE
OTHERS_EXECUTE
GROUP_READ
OWNER_READ
GROUP_EXECUTE
GROUP_WRITE
OWNER_EXECUTE
OTHERS_READ

Upvotes: 4

Views: 1098

Answers (2)

adler
adler

Reputation: 969

Since changing the umask was no option for me I use Files.setPosixFilePermissions(Path, Set<PosixFilePermission>) after creating the file.

Upvotes: 2

Quirin
Quirin

Reputation: 738

According to CPU 100 comment, I made a quick search about umask and it turned out I was on the default value, which is 0002. Thus, I couldn't create any files with writting permissions for other users. Here is a good explanation of umask.

Upvotes: 4

Related Questions