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); } }
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; } };
try { UserPrincipalLookupService lookupService = dotCopyFile.getFileSystem().getUserPrincipalLookupService(); Files.setOwner(dotCopyFile, lookupService.lookupPrincipalByName(owner)); } catch (Exception e) { logger.warn("Could not set file owner to {} because {}", new Object[]{owner, e});
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; } };
/** * 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); }
return Files.setOwner( path, owner ); } catch( IOException e ) { throw u( e );
/** This method changes the ownership of folder/file to the user provided * @param destinationLocation : the location of the path to be owned * @param user : the user which needs to own the folder/file */ private void changeOwnership(String destinationLocation, String user){ Path path = Paths.get(destinationLocation); FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class); UserPrincipalLookupService lookupService = FileSystems.getDefault() .getUserPrincipalLookupService(); UserPrincipal userPrincipal; try { userPrincipal = lookupService.lookupPrincipalByName(user); Files.setOwner(path, userPrincipal); } catch (IOException e) { LOGGER.error("Error while changing ownership of destination location[" + destinationLocation +"]" + "to user[" + user + "]" , e); } }
static void verifyTmpDirSetup(PolicyUpdaterConfiguration configuration) throws IOException { // ensure tmp dir exists String policyTmpDir = configuration.getPolicyFileTmpDir(); Path tmpDir = Paths.get(policyTmpDir); if (java.nio.file.Files.exists(tmpDir)) { return; } LOG.warn("The temp dir doesnt exist so will create it: " + tmpDir); java.nio.file.Files.createDirectory(tmpDir); // get the user from config file to perform chown aginst the tmp dir // chown -R $zpu_user $ROOT/tmp/zpe String user = configuration.getZpuDirOwner(); if (user == null) { LOG.warn("Cannot chown of the temp dir: " + tmpDir + " : no configured user"); return; } try { java.nio.file.attribute.UserPrincipalLookupService lookupSvc = java.nio.file.FileSystems.getDefault().getUserPrincipalLookupService(); java.nio.file.attribute.UserPrincipal uprinc = lookupSvc.lookupPrincipalByName(user); Files.setOwner(tmpDir, uprinc); } catch (Exception exc) { LOG.warn("Failed to chown of the temp dir: " + tmpDir + ", user: " + user + ", exc: " + exc.getMessage()); } }
@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 ); }
import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.FileOwnerAttributeView; import java.nio.file.attribute.UserPrincipal; import java.nio.file.attribute.UserPrincipalLookupService; public class Test { public static void main(String[] args) throws Exception { Path path = Paths.get("C:/home/docs/users.txt"); FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class); UserPrincipalLookupService lookupService = FileSystems.getDefault() .getUserPrincipalLookupService(); UserPrincipal userPrincipal = lookupService.lookupPrincipalByName("mary"); Files.setOwner(path, userPrincipal); System.out.println("Owner: " + view.getOwner().getName()); } }
/** * Обновляет владельца файла. <p> * * Параметр path связан с файловой системой, которая поддерживает FileOwnerAttributeView. <p> * * Это представление атрибута файла * предоставляет доступ к атрибуту файла, который является владельцем файла. <p> * * Пример использования: предположим, что мы хотим сделать «joe» владельцем файла: <p> * Путь пути = ... * UserPrincipalLookupService lookupService = * поставщик (путь) .getUserPrincipalLookupService (); * UserPrincipal joe = lookupService.lookupPrincipalByName ("joe"); * Files.setOwner (путь, joe); * @param user Новый владелец файла * @return файл */ public File setOwner(UserPrincipal user){ try { return new File( Files.setOwner(path, user) ); } catch (IOException ex) { //Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex); throw new IOError(ex); } }
@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); } }
Files.setOwner(path, user);
try { UserPrincipalLookupService lookupService = dotCopyFile.getFileSystem().getUserPrincipalLookupService(); Files.setOwner(dotCopyFile, lookupService.lookupPrincipalByName(owner)); } catch (Exception e) { logger.warn("Could not set file owner to {} because {}", new Object[]{owner, e});