Search in sources :

Example 11 with UserPrincipalLookupService

use of java.nio.file.attribute.UserPrincipalLookupService in project alluxio by Alluxio.

the class FileUtils method changeLocalFileUser.

/**
 * Changes the local file's user.
 *
 * @param path that will change owner
 * @param user the new user
 */
public static void changeLocalFileUser(String path, String user) throws IOException {
    UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
    PosixFileAttributeView view = Files.getFileAttributeView(Paths.get(path), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
    UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(user);
    view.setOwner(userPrincipal);
}
Also used : UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) UserPrincipal(java.nio.file.attribute.UserPrincipal) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 12 with UserPrincipalLookupService

use of java.nio.file.attribute.UserPrincipalLookupService in project alluxio by Alluxio.

the class FileUtils method changeLocalFileGroup.

/**
 * Changes the local file's group.
 *
 * @param path that will change owner
 * @param group the new group
 */
public static void changeLocalFileGroup(String path, String group) throws IOException {
    UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
    PosixFileAttributeView view = Files.getFileAttributeView(Paths.get(path), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
    GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(group);
    view.setGroup(groupPrincipal);
}
Also used : UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 13 with UserPrincipalLookupService

use of java.nio.file.attribute.UserPrincipalLookupService in project alluxio by Alluxio.

the class StackFS method chownInternal.

private int chownInternal(String path, long uid, long gid) {
    path = transformPath(path);
    Path filePath = Paths.get(path);
    if (!Files.exists(filePath)) {
        return -ErrorCodes.ENOENT();
    }
    try {
        UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
        PosixFileAttributeView view = Files.getFileAttributeView(filePath, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
        String userName = "";
        if (uid != ID_NOT_SET_VALUE && uid != ID_NOT_SET_VALUE_UNSIGNED) {
            userName = AlluxioFuseUtils.getUserName(uid);
            if (userName.isEmpty()) {
                // This should never be reached
                LOG.error("Failed to get user name from uid {}", uid);
                return -ErrorCodes.EINVAL();
            }
            view.setOwner(lookupService.lookupPrincipalByName(userName));
        }
        String groupName = "";
        if (gid != ID_NOT_SET_VALUE && gid != ID_NOT_SET_VALUE_UNSIGNED) {
            groupName = AlluxioFuseUtils.getGroupName(gid);
            if (groupName.isEmpty()) {
                // This should never be reached
                LOG.error("Failed to get group name from gid {}", gid);
                return -ErrorCodes.EINVAL();
            }
            view.setGroup(lookupService.lookupPrincipalByGroupName(groupName));
        } else if (!userName.isEmpty()) {
            groupName = AlluxioFuseUtils.getGroupName(userName);
            view.setGroup(lookupService.lookupPrincipalByGroupName(groupName));
        }
        return 0;
    } catch (IOException e) {
        LOG.error("Failed to chown {}", path, e);
        return -ErrorCodes.EIO();
    }
}
Also used : Path(java.nio.file.Path) UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) IOException(java.io.IOException) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 14 with UserPrincipalLookupService

use of java.nio.file.attribute.UserPrincipalLookupService in project jimfs by google.

the class UserLookupServiceTest method testServiceNotSupportingGroups.

@Test
public void testServiceNotSupportingGroups() throws IOException {
    UserPrincipalLookupService service = new UserLookupService(false);
    try {
        service.lookupPrincipalByGroupName("group");
        fail();
    } catch (UserPrincipalNotFoundException expected) {
        assertThat(expected.getName()).isEqualTo("group");
    }
}
Also used : UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) UserPrincipalNotFoundException(java.nio.file.attribute.UserPrincipalNotFoundException) Test(org.junit.Test)

Example 15 with UserPrincipalLookupService

use of java.nio.file.attribute.UserPrincipalLookupService in project jimfs by google.

the class UserLookupServiceTest method testUserLookupService.

@Test
public void testUserLookupService() throws IOException {
    UserPrincipalLookupService service = new UserLookupService(true);
    UserPrincipal bob1 = service.lookupPrincipalByName("bob");
    UserPrincipal bob2 = service.lookupPrincipalByName("bob");
    UserPrincipal alice = service.lookupPrincipalByName("alice");
    assertThat(bob1).isEqualTo(bob2);
    assertThat(bob1).isNotEqualTo(alice);
    GroupPrincipal group1 = service.lookupPrincipalByGroupName("group");
    GroupPrincipal group2 = service.lookupPrincipalByGroupName("group");
    GroupPrincipal foo = service.lookupPrincipalByGroupName("foo");
    assertThat(group1).isEqualTo(group2);
    assertThat(group1).isNotEqualTo(foo);
}
Also used : UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) UserPrincipal(java.nio.file.attribute.UserPrincipal) Test(org.junit.Test)

Aggregations

UserPrincipalLookupService (java.nio.file.attribute.UserPrincipalLookupService)18 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)12 UserPrincipal (java.nio.file.attribute.UserPrincipal)12 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)11 IOException (java.io.IOException)7 Path (java.nio.file.Path)7 FileSystem (java.nio.file.FileSystem)3 FileSystemException (io.vertx.core.file.FileSystemException)2 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)2 Test (org.junit.Test)2 AlluxioException (alluxio.exception.AlluxioException)1 Mode (alluxio.security.authorization.Mode)1 File (java.io.File)1 InvalidPathException (java.nio.file.InvalidPathException)1 UserPrincipalNotFoundException (java.nio.file.attribute.UserPrincipalNotFoundException)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 IgfsException (org.apache.ignite.igfs.IgfsException)1