Set a file readable/writable by owner only
Weijun Wang
weijun.wang at oracle.com
Fri Sep 22 05:48:00 UTC 2017
I need to make a file rw only by the owner. How do you think of my code at the end?
Especially, for the aclView case, I tried on Windows, and the result is like
C:\>icacls x
x NT AUTHORITY\SYSTEM:(I)(F)
BUILTIN\Administrators:(I)(F)
BUILTIN\Users:(I)(RX)
Successfully processed 1 files; Failed processing 0 files
C:\>java A9 x
C:\>icacls x
x BUILTIN\Administrators:(DE,RD,WD)
Successfully processed 1 files; Failed processing 0 files
Can I simply set it to (DE,RD,WD)? Is the original (F) better? Do I need to retain the permissions of other non-USERS?
Thanks
Max
-----
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.*;
import java.util.Collections;
public class A9 {
public static void main(String[] args) throws Exception {
Path file = Paths.get(args[0]);
PosixFileAttributeView posixView = Files.getFileAttributeView(
file, PosixFileAttributeView.class);
if (posixView != null) {
posixView.setPermissions(PosixFilePermissions.fromString("rw-------"));
return;
}
AclFileAttributeView aclView = Files.getFileAttributeView(
file, AclFileAttributeView.class);
if (aclView != null) {
AclEntry.Builder builder = AclEntry.newBuilder();
builder.setPermissions(
AclEntryPermission.WRITE_DATA,
AclEntryPermission.READ_DATA,
AclEntryPermission.DELETE);
builder.setPrincipal(aclView.getOwner());
builder.setType(AclEntryType.ALLOW);
aclView.setAcl(Collections.singletonList(builder.build()));
return;
}
System.out.println("No idea how");
}
}
More information about the core-libs-dev
mailing list