public static String getFileOwner(String path) throws IOException { return Files.getOwner(FileSystems.getDefault().getPath(path)).getName(); }
@Override public boolean isSelected(File basedir, String filename, File file) { if (owner == null) { throw new BuildException("the owner attribute is required"); } if (file != null) { try { UserPrincipal user = followSymlinks ? Files.getOwner(file.toPath()) : Files.getOwner(file.toPath(), LinkOption.NOFOLLOW_LINKS); return user != null && owner.equals(user.getName()); } catch (UnsupportedOperationException | IOException ex) { // => not the expected owner } } return false; }
@Override public void deleteIfExists(File path, String user, String logPrefix) throws IOException { String absolutePath = path.getAbsolutePath(); if (Utils.checkFileExists(absolutePath)) { LOG.info("Deleting path (runAsUser) {}", absolutePath); if (user == null) { user = Files.getOwner(path.toPath()).getName(); } List<String> commands = new ArrayList<>(); commands.add("rmr"); commands.add(absolutePath); ClientSupervisorUtils.processLauncherAndWait(_conf, user, commands, null, logPrefix); if (Utils.checkFileExists(absolutePath)) { // It's possible that permissions were not set properly on the directory, and // the user who is *supposed* to own the dir does not. In this case, try the // delete as the supervisor user. Utils.forceDelete(absolutePath); if (Utils.checkFileExists(absolutePath)) { throw new RuntimeException(path + " was not deleted."); } } } }
/** * @return the user that some operations should be done as. * * @throws IOException on any error */ protected String getWorkerUser() throws IOException { LOG.info("GET worker-user for {}", _workerId); File file = new File(ConfigUtils.workerUserFile(_conf, _workerId)); if (_ops.fileExists(file)) { return _ops.slurpString(file).trim(); } else if (_assignment != null && _assignment.is_set_owner()) { return _assignment.get_owner(); } if (ConfigUtils.isLocalMode(_conf)) { return System.getProperty("user.name"); } else { File f = new File(ConfigUtils.workerArtifactsRoot(_conf)); if (f.exists()) { return Files.getOwner(f.toPath()).getName(); } throw new IllegalStateException("Could not recover the user for " + _workerId); } }
@Test public void testChownToOwnUser() throws Exception { String file1 = "some-file.dat"; createFileWithJunk(file1, 100); String fullPath = testDir + pathSep + file1; Path path = Paths.get(fullPath); UserPrincipal owner = Files.getOwner(path); String user = owner.getName(); vertx.fileSystem().chown(fullPath, user, null, ar -> { deleteFile(file1); assertTrue(ar.succeeded()); testComplete(); }); await(); }
assertEquals(user, Files.getOwner(path).getName()); assertEquals(group, Files.readAttributes(path, PosixFileAttributes.class).group().getName()); } finally {
@Test public void testChownToOwnUser() throws Exception { String file1 = "some-file.dat"; createFileWithJunk(file1, 100); String fullPath = testDir + pathSep + file1; Path path = Paths.get(fullPath); UserPrincipal owner = Files.getOwner(path); String user = owner.getName(); vertx.fileSystem().chown(fullPath, user, null, ar -> { deleteFile(file1); assertTrue(ar.succeeded()); testComplete(); }); await(); }
/** * Get the name of teh principal representing the file owner * @param path path to a file * @return the name of the principal representing the file owner * @throws IOException in case of failure in underlying layer */ public static final String getOwner(String path) throws IOException { return Files.getOwner(Paths.get(path)).getName(); }
@Override public String getOwner() throws IOException { return Files.getOwner(file.toPath()).getName(); }
public static String getFileOwner(String path) throws IOException { return Files.getOwner(FileSystems.getDefault().getPath(path)).getName(); }
@Override public String getOwner() throws IOException { return Files.getOwner(file.toPath()).getName(); }
private String ownerFor( File file ) { Path filePath = Paths.get(file.toURI()); try { return Files.getOwner(filePath).getName(); } catch (IOException e) { log().debug(e, "Unable to read the owner of '{0}'", filePath); return null; } }
private String ownerFor( File file ) { Path filePath = Paths.get(file.toURI()); try { return Files.getOwner(filePath).getName(); } catch (IOException e) { log().debug(e, "Unable to read the owner of '{0}'", filePath); return null; } }
@Test @Category( { Posix.class, Unix.class, Principals.class } ) public void testFilesHaveOwners() throws IOException { assertThat( Files.getOwner( getFile() ) ).isNotNull(); }
@Override protected String getOwner(File file, LsArguments args) { try { final String owner = Files.getOwner(file.toPath()).getName(); return StringUtil.fixSizeString(7, true, owner); } catch (IOException e) { return super.getOwner(file, args); } }
@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 ); }
@Override protected String getOwner(File file, LsArguments args) { try { final String owner = Files.getOwner(file.toPath()).getName(); return StringUtil.fixSizeString(7, true, owner); } catch (IOException e) { return super.getOwner(file, args); } }
@Test @Category( { Attributes.class, Posix.class, OwnerView.class } ) public void testOwnerByTwoMethods() throws IOException { assertThat( Files.getOwner( pathDefault() ) ).isEqualTo( Files.readAttributes( pathDefault(), PosixFileAttributes.class ).owner() ); }
@Test(expected = UnsupportedOperationException.class) public void getOwner() throws IOException { Files.getOwner(fs.getPath("path"), (LinkOption) null); }
@Override public void checkAccess( EightyPath path, AccessMode... modes ) { checkContract( path ); // can not be moved to Eighty because Files.exists calls checkAccess first -> loop if( !isDirectory( path ) && !isFile( path ) ) { throw u( new NoSuchFileException( "files does not exist " + path ) ); } // todo generalize if( Arrays.asList( modes ).contains( AccessMode.WRITE ) ) { UserPrincipal fileOwner = Unchecked.u( () -> Files.getOwner( path ) ); if( !principals.getCurrentUser().equals( fileOwner ) ) { throw u( new AccessDeniedException( "file is not writable " ) ); } } }