Java Code Examples for java.nio.file.Files#getOwner()

The following examples show how to use java.nio.file.Files#getOwner() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilePermissionTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testDefaultOwnership() throws Exception {
    // This is unix test only
    if (!Util.isWindows()) {
        CommandContext ctx = CLITestUtil.getCommandContext(TestSuiteEnvironment.getServerAddress(),
                TestSuiteEnvironment.getServerPort(), System.in, System.out);

        String tmpFile = "tmpFile";
        Path path = Paths.get(tmpFile);

        try {
            ctx.handle("echo \"aaa\" >> " + tmpFile);

            UserPrincipal userPrincipal = Files.getOwner(path);
            assertThat("The test file has unexpected ownership: " + userPrincipal.toString(),
                    userPrincipal.toString(), CoreMatchers.is(CoreMatchers.equalTo(System.getProperty("user.name"))));
        } finally {
            ctx.terminateSession();
            Files.delete(path);
        }
    }
}
 
Example 2
Source File: TestUserPrincipalLookupService.java    From jsr203-hadoop with Apache License 2.0 6 votes vote down vote up
@Test
public void testUsersEquals() throws IOException {
  Path rootPath = Paths.get(clusterUri);

  UserPrincipal user = Files.getOwner(rootPath);
  assertNotNull(user);

  // Get the same user
  UserPrincipal user2 = Files.getOwner(rootPath);
  assertNotNull(user2);

  Assert.assertTrue(user.equals(user));
  Assert.assertTrue(user.equals(user2) && user2.equals(user));

  Assert.assertFalse(user.equals(null));
  Assert.assertFalse(user.equals(new Double(-1)));

  UserPrincipal userTest = rootPath.getFileSystem()
      .getUserPrincipalLookupService().lookupPrincipalByName("test");
  Assert.assertFalse(user.equals(userTest));
}
 
Example 3
Source File: TestVMOptionsFile.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void makeFileNonReadable(String file) throws IOException {
    Path filePath = Paths.get(file);
    Set<String> supportedAttr = filePath.getFileSystem().supportedFileAttributeViews();

    if (supportedAttr.contains("posix")) {
        Files.setPosixFilePermissions(filePath, PosixFilePermissions.fromString("-w--w----"));
    } else if (supportedAttr.contains("acl")) {
        UserPrincipal fileOwner = Files.getOwner(filePath);

        AclFileAttributeView view = Files.getFileAttributeView(filePath, AclFileAttributeView.class);

        AclEntry entry = AclEntry.newBuilder()
                .setType(AclEntryType.DENY)
                .setPrincipal(fileOwner)
                .setPermissions(AclEntryPermission.READ_DATA)
                .build();

        List<AclEntry> acl = view.getAcl();
        acl.add(0, entry);
        view.setAcl(acl);
    }
}
 
Example 4
Source File: CheckLockLocationTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static String getOwner(Path path) {
    UserPrincipal user = null;
    try {
        user = Files.getOwner(path);
    } catch (Exception x) {
        System.err.println("Failed to get owner of: " + path);
        System.err.println("\terror is: " + x);
    }
    return user == null ? "???" : user.getName();
}
 
Example 5
Source File: TestUserPrincipalLookupService.java    From jsr203-hadoop with Apache License 2.0 5 votes vote down vote up
@Test
public void testClone() throws IOException, CloneNotSupportedException {
  Path rootPath = Paths.get(clusterUri);

  UserPrincipal user = Files.getOwner(rootPath);
  assertNotNull(user);

  UserPrincipal user3 = (UserPrincipal) ((HadoopUserPrincipal) user).clone();
  Assert.assertTrue(user.equals(user3));
}
 
Example 6
Source File: TestUserPrincipalLookupService.java    From jsr203-hadoop with Apache License 2.0 5 votes vote down vote up
@Test
public void testHashcode() throws IOException {
  Path rootPath = Paths.get(clusterUri);

  UserPrincipal user = Files.getOwner(rootPath);
  assertNotNull(user);

  // Get the same user
  UserPrincipal user2 = Files.getOwner(rootPath);
  assertNotNull(user2);
  // Test hash code
  Assert.assertTrue(user.hashCode() == user2.hashCode());
}
 
Example 7
Source File: TestUserPrincipalLookupService.java    From jsr203-hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Try to get user and set the same user in the root.
 * 
 * @throws IOException
 */
@Test
public void testGetSetUGI() throws IOException {
  Path rootPath = Paths.get(clusterUri);

  UserPrincipal user = Files.getOwner(rootPath);
  assertNotNull(user);

  Files.setOwner(rootPath, user);
}
 
Example 8
Source File: CheckLockLocationTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static String getOwner(Path path) {
    UserPrincipal user = null;
    try {
        user = Files.getOwner(path);
    } catch (Exception x) {
        System.err.println("Failed to get owner of: " + path);
        System.err.println("\terror is: " + x);
    }
    return user == null ? "???" : user.getName();
}
 
Example 9
Source File: CheckLockLocationTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static String getOwner(Path path) {
    UserPrincipal user = null;
    try {
        user = Files.getOwner(path);
    } catch (Exception x) {
        System.err.println("Failed to get owner of: " + path);
        System.err.println("\terror is: " + x);
    }
    return user == null ? "???" : user.getName();
}
 
Example 10
Source File: CheckLockLocationTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static String getOwner(Path path) {
    UserPrincipal user = null;
    try {
        user = Files.getOwner(path);
    } catch (Exception x) {
        System.err.println("Failed to get owner of: " + path);
        System.err.println("\terror is: " + x);
    }
    return user == null ? "???" : user.getName();
}
 
Example 11
Source File: CheckLockLocationTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private static String getOwner(Path path) {
    UserPrincipal user = null;
    try {
        user = Files.getOwner(path);
    } catch (Exception x) {
        System.err.println("Failed to get owner of: " + path);
        System.err.println("\terror is: " + x);
    }
    return user == null ? "???" : user.getName();
}
 
Example 12
Source File: CheckLockLocationTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static String getOwner(Path path) {
    UserPrincipal user = null;
    try {
        user = Files.getOwner(path);
    } catch (Exception x) {
        System.err.println("Failed to get owner of: " + path);
        System.err.println("\terror is: " + x);
    }
    return user == null ? "???" : user.getName();
}
 
Example 13
Source File: CheckLockLocationTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static String getOwner(Path path) {
    UserPrincipal user = null;
    try {
        user = Files.getOwner(path);
    } catch (Exception x) {
        System.err.println("Failed to get owner of: " + path);
        System.err.println("\terror is: " + x);
    }
    return user == null ? "???" : user.getName();
}
 
Example 14
Source File: CheckLockLocationTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static String getOwner(Path path) {
    UserPrincipal user = null;
    try {
        user = Files.getOwner(path);
    } catch (Exception x) {
        System.err.println("Failed to get owner of: " + path);
        System.err.println("\terror is: " + x);
    }
    return user == null ? "???" : user.getName();
}
 
Example 15
Source File: CheckLockLocationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static String getOwner(Path path) {
    UserPrincipal user = null;
    try {
        user = Files.getOwner(path);
    } catch (Exception x) {
        System.err.println("Failed to get owner of: " + path);
        System.err.println("\terror is: " + x);
    }
    return user == null ? "???" : user.getName();
}
 
Example 16
Source File: CheckLockLocationTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static String getOwner(Path path) {
    UserPrincipal user = null;
    try {
        user = Files.getOwner(path);
    } catch (Exception x) {
        System.err.println("Failed to get owner of: " + path);
        System.err.println("\terror is: " + x);
    }
    return user == null ? "???" : user.getName();
}
 
Example 17
Source File: CheckLockLocationTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static String getOwner(Path path) {
    UserPrincipal user = null;
    try {
        user = Files.getOwner(path);
    } catch (Exception x) {
        System.err.println("Failed to get owner of: " + path);
        System.err.println("\terror is: " + x);
    }
    return user == null ? "???" : user.getName();
}
 
Example 18
Source File: CheckLockLocationTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static String getOwner(Path path) {
    UserPrincipal user = null;
    try {
        user = Files.getOwner(path);
    } catch (Exception x) {
        System.err.println("Failed to get owner of: " + path);
        System.err.println("\terror is: " + x);
    }
    return user == null ? "???" : user.getName();
}
 
Example 19
Source File: CheckLockLocationTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static String getOwner(Path path) {
    UserPrincipal user = null;
    try {
        user = Files.getOwner(path);
    } catch (Exception x) {
        System.err.println("Failed to get owner of: " + path);
        System.err.println("\terror is: " + x);
    }
    return user == null ? "???" : user.getName();
}
 
Example 20
Source File: AESKeyFileEncrypterFactory.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
private void checkFilePermissions(String fileLocation, File file) throws IOException
{
    if(isPosixFileSystem(file))
    {
        Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(file.toPath());

        if (permissions.contains(PosixFilePermission.GROUP_READ)
                || permissions.contains(PosixFilePermission.OTHERS_READ)
                || permissions.contains(PosixFilePermission.GROUP_WRITE)
                || permissions.contains(PosixFilePermission.OTHERS_WRITE)) {
            throw new IllegalArgumentException("Key file '"
                    + fileLocation
                    + "' has incorrect permissions.  Only the owner "
                    + "should be able to read or write this file.");
        }
    }
    else if(isAclFileSystem(file))
    {
        AclFileAttributeView attributeView = Files.getFileAttributeView(file.toPath(), AclFileAttributeView.class);
        ArrayList<AclEntry> acls = new ArrayList<>(attributeView.getAcl());
        ListIterator<AclEntry> iter = acls.listIterator();
        UserPrincipal owner = Files.getOwner(file.toPath());
        while(iter.hasNext())
        {
            AclEntry acl = iter.next();
            if(acl.type() == AclEntryType.ALLOW)
            {
                Set<AclEntryPermission> originalPermissions = acl.permissions();
                Set<AclEntryPermission> updatedPermissions = EnumSet.copyOf(originalPermissions);


                if (updatedPermissions.removeAll(EnumSet.of(AclEntryPermission.APPEND_DATA,
                        AclEntryPermission.EXECUTE,
                        AclEntryPermission.WRITE_ACL,
                        AclEntryPermission.WRITE_DATA,
                        AclEntryPermission.WRITE_OWNER))) {
                    throw new IllegalArgumentException("Key file '"
                            + fileLocation
                            + "' has incorrect permissions.  The file should not be modifiable by any user.");
                }
                if (!owner.equals(acl.principal()) && updatedPermissions.removeAll(EnumSet.of(AclEntryPermission.READ_DATA))) {
                    throw new IllegalArgumentException("Key file '"
                            + fileLocation
                            + "' has incorrect permissions.  Only the owner should be able to read from the file.");
                }
            }
        }
    }
    else
    {
        throw new IllegalArgumentException(ILLEGAL_ARG_EXCEPTION);
    }
}