Search in sources :

Example 1 with AclEntryFlag

use of java.nio.file.attribute.AclEntryFlag in project java-chassis by ServiceComb.

the class FortifyUtils method getDefaultFileAttributes.

public static FileAttribute<?> getDefaultFileAttributes(String filePath) {
    Path file = new File(filePath).toPath();
    if (isPosix()) {
        return PosixFilePermissions.asFileAttribute(FilePerm.getDefaultPosixPerm());
    } else {
        // for not posix must support ACL, or failed.
        String userName = System.getProperty("user.name");
        UserPrincipal user = null;
        try {
            user = file.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName(userName);
        } catch (IOException e) {
            throw new RuntimeException("Unknown user error.");
        }
        final AclEntry entry = AclEntry.newBuilder().setType(AclEntryType.ALLOW).setPrincipal(user).setPermissions(FilePerm.getDefaultAclPerm()).setFlags(new AclEntryFlag[] { AclEntryFlag.FILE_INHERIT, AclEntryFlag.DIRECTORY_INHERIT }).build();
        return new FileAttribute<List<AclEntry>>() {

            public String name() {
                return "acl:acl";
            }

            /* Windows ACL */
            //public Object value() { ArrayList l = new ArrayList(); l.add(entry); return l; }
            public List<AclEntry> value() {
                ArrayList<AclEntry> l = new ArrayList<AclEntry>();
                l.add(entry);
                return l;
            }
        };
    }
}
Also used : Path(java.nio.file.Path) AclEntryFlag(java.nio.file.attribute.AclEntryFlag) AclEntry(java.nio.file.attribute.AclEntry) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) UserPrincipal(java.nio.file.attribute.UserPrincipal) FileAttribute(java.nio.file.attribute.FileAttribute)

Example 2 with AclEntryFlag

use of java.nio.file.attribute.AclEntryFlag in project j2objc by google.

the class AclEntryTest method testGetters.

@Test
public void testGetters() throws Exception {
    UserPrincipal user = Files.getOwner(Paths.get("."));
    AclEntry aclEntry = AclEntry.newBuilder().setType(AclEntryType.ALLOW).setPrincipal(user).setFlags(AclEntryFlag.INHERIT_ONLY).setPermissions(AclEntryPermission.READ_DATA, AclEntryPermission.READ_ATTRIBUTES).build();
    assertEquals(AclEntryType.ALLOW, aclEntry.type());
    assertEquals(user, aclEntry.principal());
    Set<AclEntryPermission> permissions = aclEntry.permissions();
    assertEquals(2, permissions.size());
    assertTrue(permissions.contains(AclEntryPermission.READ_DATA));
    assertTrue(permissions.contains(AclEntryPermission.READ_ATTRIBUTES));
    Set<AclEntryFlag> flags = aclEntry.flags();
    assertEquals(1, flags.size());
    assertTrue(flags.contains(AclEntryFlag.INHERIT_ONLY));
}
Also used : AclEntryFlag(java.nio.file.attribute.AclEntryFlag) AclEntry(java.nio.file.attribute.AclEntry) AclEntryPermission(java.nio.file.attribute.AclEntryPermission) UserPrincipal(java.nio.file.attribute.UserPrincipal) Test(org.junit.Test)

Aggregations

AclEntry (java.nio.file.attribute.AclEntry)2 AclEntryFlag (java.nio.file.attribute.AclEntryFlag)2 UserPrincipal (java.nio.file.attribute.UserPrincipal)2 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 AclEntryPermission (java.nio.file.attribute.AclEntryPermission)1 FileAttribute (java.nio.file.attribute.FileAttribute)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1