@Override public UserPrincipalLookupService getUserPrincipalLookupService() { return delegate.getUserPrincipalLookupService(); }
/** * 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); }
/** * 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); }
public static void setFilePermissions(Path filePath) throws IOException { if ("root".equalsIgnoreCase(ZaleniumConfiguration.getCurrentUser())) { UserPrincipal hostUid = filePath.getFileSystem() .getUserPrincipalLookupService().lookupPrincipalByName(ZaleniumConfiguration.getHostUid()); Files.setOwner(filePath, hostUid); GroupPrincipal hostGid = filePath.getFileSystem() .getUserPrincipalLookupService().lookupPrincipalByGroupName(ZaleniumConfiguration.getHostGid()); Files.getFileAttributeView(filePath, PosixFileAttributeView.class).setGroup(hostGid); } }
UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
public Void perform() { try { Path target = vertx.resolveFile(path).toPath(); UserPrincipalLookupService service = target.getFileSystem().getUserPrincipalLookupService(); UserPrincipal userPrincipal = user == null ? null : service.lookupPrincipalByName(user); GroupPrincipal groupPrincipal = group == null ? null : service.lookupPrincipalByGroupName(group); if (groupPrincipal != null) { PosixFileAttributeView view = Files.getFileAttributeView(target, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); if (view == null) { throw new FileSystemException("Change group of file not supported"); } view.setGroup(groupPrincipal); } if (userPrincipal != null) { Files.setOwner(target, userPrincipal); } } catch (SecurityException e) { throw new FileSystemException("Accessed denied for chown on " + path); } catch (IOException e) { throw new FileSystemException(e); } return null; } };
if (owner != null && !owner.trim().isEmpty()) { try { UserPrincipalLookupService lookupService = dotCopyFile.getFileSystem().getUserPrincipalLookupService(); Files.setOwner(dotCopyFile, lookupService.lookupPrincipalByName(owner)); } catch (Exception e) { if (group != null && !group.trim().isEmpty()) { try { UserPrincipalLookupService lookupService = dotCopyFile.getFileSystem().getUserPrincipalLookupService(); PosixFileAttributeView view = Files.getFileAttributeView(dotCopyFile, PosixFileAttributeView.class); view.setGroup(lookupService.lookupPrincipalByGroupName(group));
public Void perform() { try { Path target = vertx.resolveFile(path).toPath(); UserPrincipalLookupService service = target.getFileSystem().getUserPrincipalLookupService(); UserPrincipal userPrincipal = user == null ? null : service.lookupPrincipalByName(user); GroupPrincipal groupPrincipal = group == null ? null : service.lookupPrincipalByGroupName(group); if (groupPrincipal != null) { PosixFileAttributeView view = Files.getFileAttributeView(target, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); if (view == null) { throw new FileSystemException("Change group of file not supported"); } view.setGroup(groupPrincipal); } if (userPrincipal != null) { Files.setOwner(target, userPrincipal); } } catch (SecurityException e) { throw new FileSystemException("Accessed denied for chown on " + path); } catch (IOException e) { throw new FileSystemException(e); } return null; } };
@Override public UserPrincipalLookupService getUserPrincipalLookupService() { return rootFs.getUserPrincipalLookupService(); }
@Override public UserPrincipalLookupService getUserPrincipalLookupService() { return delegate.getUserPrincipalLookupService(); }
private GroupPrincipal getGroupPrincipalFrom(String groupName) throws IOException { try { if (_isCacheEnabled) { return _nameToGroupPrincipal.get(groupName); } UserPrincipalLookupService service = FileSystems.getDefault().getUserPrincipalLookupService(); return service.lookupPrincipalByGroupName(groupName); } catch (IOException | UnsupportedOperationException e) { return null; } }
/** * Set the file owner * @param path path to file * @param owner the owner * @throws IOException in case of failure in underlying layer */ public static final void setOwner(String path, String owner) throws IOException { UserPrincipal userPrincipal = Paths.get(path).getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName(owner); Files.setOwner(Paths.get(path), userPrincipal); }
@Test @Category( { Principals.class } ) public void testGetPrincipalsLookupServiceDoesNotThrow() throws IOException { assertThat( FS.getUserPrincipalLookupService() ).isNotNull(); }
@Override public void setGroup(String group) throws IOException { UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService(); GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(group); Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class).setGroup(groupPrincipal); }
@Override public void setGroup(String group) throws IOException { UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService(); GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(group); Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class).setGroup(groupPrincipal); }
@Test @Category( { Principals.class, Attributes.class, FileOwnerView.class } ) public void testFindOwner() throws IOException { UserPrincipalLookupService lookupService = FS.getUserPrincipalLookupService(); UserPrincipal owner = Files.getOwner( FS.getPath( "" ).toAbsolutePath() ); assertThat( lookupService.lookupPrincipalByName( owner.getName() ) ).isEqualTo( owner ); }
@Test(expected = UnsupportedOperationException.class) // We don't support security public void getUserPrincipalLookupService() { fileSystem.getUserPrincipalLookupService(); }
@Test @Category( { Principals.class, Posix.class, Unix.class, PermissionChecks.class } ) public void testDifferentOwnerCanNotWrite() throws IOException { UserPrincipal none = FS.getUserPrincipalLookupService().lookupPrincipalByName( "root" ); Files.setOwner( fileTA(), none ); //System.out.println( Files.getPosixFilePermissions( fileTA() ) ); assertThatThrownBy( () -> Files.write( absTA(), CONTENT_OTHER ) ).isInstanceOf( AccessDeniedException.class ); }
@Override public void setUnixOwner(final Path file, final String owner) throws BackgroundException { try { final UserPrincipal principal = session.getClient().getUserPrincipalLookupService().lookupPrincipalByName(owner); Files.setOwner(session.toPath(file), principal); } catch(IOException e) { throw new LocalExceptionMappingService().map("Failure to write attributes of {0}", e, file); } }
@Override public void setUnixGroup(final Path file, final String group) throws BackgroundException { try { final GroupPrincipal principal = session.getClient().getUserPrincipalLookupService().lookupPrincipalByGroupName(group); Files.getFileAttributeView(session.toPath(file), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS).setGroup(principal); } catch(IOException e) { throw new LocalExceptionMappingService().map("Failure to write attributes of {0}", e, file); } }