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);
}
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);
}
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();
}
}
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");
}
}
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);
}
Aggregations