public static void main(String[] args) throws IOException { Path file = Paths.get("c:/touch.txt"); AclFileAttributeView aclAttr = Files.getFileAttributeView(file, AclFileAttributeView.class); System.out.println(aclAttr.getOwner()); for(AclEntry aclEntry : aclAttr.getAcl()){ System.out.println(aclEntry); } System.out.println(); UserPrincipalLookupService upls = file.getFileSystem().getUserPrincipalLookupService(); UserPrincipal user = upls.lookupPrincipalByName(System.getProperty("user.name")); AclEntry.Builder builder = AclEntry.newBuilder(); builder.setPermissions( EnumSet.of(AclEntryPermission.READ_DATA, AclEntryPermission.EXECUTE, AclEntryPermission.READ_ACL, AclEntryPermission.READ_ATTRIBUTES, AclEntryPermission.READ_NAMED_ATTRS, AclEntryPermission.WRITE_ACL, AclEntryPermission.DELETE )); builder.setPrincipal(user); builder.setType(AclEntryType.ALLOW); aclAttr.setAcl(Collections.singletonList(builder.build())); }
String getWindowsUserName(AclFileAttributeView aclFileAttributeView) { if (launcher.isUnix()) return ""; try { return aclFileAttributeView.getOwner().getName(); } catch (IOException ignored) { String username = System.getenv("USERNAME"); if (StringUtils.isBlank(username)) return ""; String domain = System.getenv("USERDOMAIN"); if (StringUtils.isNotBlank(domain) && !username.endsWith("$")) { username = domain + "\\" + username; } else if (username.endsWith("$")) { username = "BUILTIN\\Administrators"; } return username; } }
if (view != null) sb.append("owner:"+view.getOwner().getName()+"\n"); for (AclEntry acl : view.getAcl())
private static void setWindowsPermissions(Path path) throws IOException { AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class); UserPrincipal owner = view.getOwner(); List<AclEntry> acl = view.getAcl(); ListIterator<AclEntry> it = acl.listIterator(); while (it.hasNext()) { AclEntry entry = it.next(); if ("BUILTIN\\Administrators".equals(entry.principal().getName()) || "NT AUTHORITY\\SYSTEM".equals(entry.principal().getName())) { continue; } it.remove(); } AclEntry entry = AclEntry.newBuilder() .setType(AclEntryType.ALLOW) .setPrincipal(owner) .setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.WRITE_DATA, AclEntryPermission.APPEND_DATA, AclEntryPermission.READ_NAMED_ATTRS, AclEntryPermission.WRITE_NAMED_ATTRS, AclEntryPermission.EXECUTE, AclEntryPermission.READ_ATTRIBUTES, AclEntryPermission.WRITE_ATTRIBUTES, AclEntryPermission.DELETE, AclEntryPermission.READ_ACL, AclEntryPermission.SYNCHRONIZE) .build(); acl.add(entry); view.setAcl(acl); }
private static void setWindowsPermissions(Path path) throws IOException { AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class); UserPrincipal owner = view.getOwner(); List<AclEntry> acl = view.getAcl(); ListIterator<AclEntry> it = acl.listIterator(); while (it.hasNext()) { AclEntry entry = it.next(); if ("BUILTIN\\Administrators".equals(entry.principal().getName()) || "NT AUTHORITY\\SYSTEM".equals(entry.principal().getName())) { continue; } it.remove(); } AclEntry entry = AclEntry.newBuilder() .setType(AclEntryType.ALLOW) .setPrincipal(owner) .setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.WRITE_DATA, AclEntryPermission.APPEND_DATA, AclEntryPermission.READ_NAMED_ATTRS, AclEntryPermission.WRITE_NAMED_ATTRS, AclEntryPermission.EXECUTE, AclEntryPermission.READ_ATTRIBUTES, AclEntryPermission.WRITE_ATTRIBUTES, AclEntryPermission.DELETE, AclEntryPermission.READ_ACL, AclEntryPermission.SYNCHRONIZE) .build(); acl.add(entry); view.setAcl(acl); }
private static void setWindowsPermissions(Path path) throws IOException { AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class); UserPrincipal owner = view.getOwner(); List<AclEntry> acl = view.getAcl(); ListIterator<AclEntry> it = acl.listIterator(); while (it.hasNext()) { AclEntry entry = it.next(); if ("BUILTIN\\Administrators".equals(entry.principal().getName()) || "NT AUTHORITY\\SYSTEM".equals(entry.principal().getName())) { continue; } it.remove(); } AclEntry entry = AclEntry.newBuilder() .setType(AclEntryType.ALLOW) .setPrincipal(owner) .setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.WRITE_DATA, AclEntryPermission.APPEND_DATA, AclEntryPermission.READ_NAMED_ATTRS, AclEntryPermission.WRITE_NAMED_ATTRS, AclEntryPermission.EXECUTE, AclEntryPermission.READ_ATTRIBUTES, AclEntryPermission.WRITE_ATTRIBUTES, AclEntryPermission.DELETE, AclEntryPermission.READ_ACL, AclEntryPermission.SYNCHRONIZE) .build(); acl.add(entry); view.setAcl(acl); }