Frage

I'm trying to set the next set of posix permissions rwxrwsr-x to a folder that I'm creating in java, but haven't had success on this. I tried the next code:

Set<PosixFilePermission> posixPermissions = PosixFilePermissions.fromString("rwxrwsr-x");
Files.setPosixFilePermissions(someDir, posixPermissions);

but I'm getting the next Exception:

java.lang.IllegalArgumentException: Invalid mode

I spent some time looking for a way to do this, but I couldn't find anything. I know I can use ProcessBuilder class to run a command in the OS to set those permissions, but I wanted to do this purely in Java. I need to set those permissions so any file created inside that directory "inherits" the group of the directory.

War es hilfreich?

Lösung

According to the documentation you are only allowed to use the characters 'r','w','x' and '-'. But in your code snippet you are using 's', which is illegal of course. It should be like this to work:

Set<PosixFilePermission> posixPermissions = PosixFilePermissions.fromString("rwxrwxr-x");
Files.setPosixFilePermissions(someDir, posixPermissions);

Setuid (+s) ist not possible in pure Java, because it's a OS-specific feature.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top