I'm using
Files.isWritable(file)
in my Java application to check if I have permissions to modify file before modifying it.
This usually works okay but I had a customer complaining that although he had give full permission to his files it still wasn't working. The customers setup consisted of a remote linux drive mounted with Samba to his Windows machine, and my software was running on the Windows machine.
It turned out he had given full permissions to the folder and files for a particular user, but not the users group or anybody else on linux.
If he changed permissions on linux from 700 to 777 then it worked but I'm not sure that he should have to do that?
Is there a problem with Java when checking permissions on a samba mount
It depends.
The access control mechanisms on UNIX and Windows have traditionally been different. Previously, as with samba3, you had a rather crude mapping between those permissions, which worked in the simpler cases, but had problems in the more difficult ones. You can find numerous tutorials, forum posts and mailing lists dealing with those special cases.
Nowadays, things have gotten better, as there are Access Control Lists in NFSv4 style. The advantages are several:
You normally use ACLFileAttributeView as stated in the short example:
// lookup "joe"
UserPrincipal joe = file.getFileSystem().getUserPrincipalLookupService()
.lookupPrincipalByName("joe");
// get view
AclFileAttributeView view = Files.getFileAttributeView(file, AclFileAttributeView.class);
// create ACE to give "joe" read access
AclEntry entry = AclEntry.newBuilder()
.setType(AclEntryType.ALLOW)
.setPrincipal(joe)
.setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.READ_ATTRIBUTES)
.build();
// read ACL, insert ACE, re-write ACL
List<AclEntry> acl = view.getAcl();
acl.add(0, entry); // insert before any DENY entries
view.setAcl(acl);
In your case it would be sufficient to query the view from the second step for the permissions you like to examine. For a detailed overview, I like to use this documentation from Oracle - while the examples are from chmod
, the permissions themselves are the same in Java (but there also exists the shorter JavaDoc for them, AclEntryPermission Enums).