/** * Gets local file's owner. * * @param filePath the file path * @return the owner of the local file */ public static String getLocalFileOwner(String filePath) throws IOException { PosixFileAttributes attr = Files.readAttributes(Paths.get(filePath), PosixFileAttributes.class); return attr.owner().getName(); }
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 UfsFileStatus getFileStatus(String path) throws IOException { String tpath = stripPath(path); File file = new File(tpath); PosixFileAttributes attr = Files.readAttributes(Paths.get(file.getPath()), PosixFileAttributes.class); String contentHash = UnderFileSystemUtils.approximateContentHash(file.length(), file.lastModified()); return new UfsFileStatus(path, contentHash, file.length(), file.lastModified(), attr.owner().getName(), attr.group().getName(), FileUtils.translatePosixPermissionToMode(attr.permissions())); }
@Override public UfsDirectoryStatus getDirectoryStatus(String path) throws IOException { String tpath = stripPath(path); File file = new File(tpath); PosixFileAttributes attr = Files.readAttributes(Paths.get(file.getPath()), PosixFileAttributes.class); return new UfsDirectoryStatus(path, attr.owner().getName(), attr.group().getName(), FileUtils.translatePosixPermissionToMode(attr.permissions()), file.lastModified()); }
@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); } }
@Override public UfsStatus getStatus(String path) throws IOException { String tpath = stripPath(path); File file = new File(tpath); PosixFileAttributes attr = Files.readAttributes(Paths.get(file.getPath()), PosixFileAttributes.class); if (file.isFile()) { // Return file status. String contentHash = UnderFileSystemUtils.approximateContentHash(file.length(), file.lastModified()); return new UfsFileStatus(path, contentHash, file.length(), file.lastModified(), attr.owner().getName(), attr.group().getName(), FileUtils.translatePosixPermissionToMode(attr.permissions())); } // Return directory status. return new UfsDirectoryStatus(path, attr.owner().getName(), attr.group().getName(), FileUtils.translatePosixPermissionToMode(attr.permissions()), file.lastModified()); }
@Override public void set(File file, String view, String attribute, Object value, boolean create) { if (attribute.equals("owner")) { checkNotCreate(view, attribute, create); UserPrincipal user = checkType(view, attribute, value, UserPrincipal.class); // TODO(cgdecker): Do we really need to do this? Any reason not to allow any UserPrincipal? if (!(user instanceof UserLookupService.JimfsUserPrincipal)) { user = createUserPrincipal(user.getName()); } file.setAttribute("owner", "owner", user); } }
@Override public UfsStatus[] listStatus(String path) throws IOException { path = stripPath(path); File file = new File(path); File[] files = file.listFiles(); if (files != null) { UfsStatus[] rtn = new UfsStatus[files.length]; int i = 0; for (File f : files) { // TODO(adit): do we need extra call for attributes? PosixFileAttributes attr = Files.readAttributes(Paths.get(f.getPath()), PosixFileAttributes.class); short mode = FileUtils.translatePosixPermissionToMode(attr.permissions()); UfsStatus retStatus; if (f.isDirectory()) { retStatus = new UfsDirectoryStatus(f.getName(), attr.owner().getName(), attr.group().getName(), mode, f.lastModified()); } else { String contentHash = UnderFileSystemUtils.approximateContentHash(f.length(), f.lastModified()); retStatus = new UfsFileStatus(f.getName(), contentHash, f.length(), f.lastModified(), attr.owner().getName(), attr.group().getName(), mode); } rtn[i++] = retStatus; } return rtn; } else { return null; } }
/** * Convert POSIX attributes to property map. * * @param attrs Attributes view. * @return IGFS properties map. */ public static Map<String, String> posixAttributesToMap(PosixFileAttributes attrs) { if (attrs == null) return null; Map<String, String> props = U.newHashMap(3); props.put(IgfsUtils.PROP_USER_NAME, attrs.owner().getName()); props.put(IgfsUtils.PROP_GROUP_NAME, attrs.group().getName()); int perm = 0; for(PosixFilePermission p : attrs.permissions()) perm |= (1 << 8 - p.ordinal()); props.put(IgfsUtils.PROP_PERMISSION, '0' + Integer.toOctalString(perm)); return props; }
try { FileOwnerAttributeView view = Files.getFileAttributeView(file, FileOwnerAttributeView.class); attributes.put(FILE_OWNER_ATTRIBUTE, view.getOwner().getName()); } catch (Exception ignore) {
try { FileOwnerAttributeView view = Files.getFileAttributeView(filePath, FileOwnerAttributeView.class); attributes.put(FILE_OWNER_ATTRIBUTE, view.getOwner().getName()); } catch (Exception ignore) {
/** {@inheritDoc} */ @Override public Map<String, String> properties(final String path) throws IOException { Path p = path(path); PosixFileAttributes attrs = Files.getFileAttributeView(p, PosixFileAttributeView.class).readAttributes(); Map<String, String> props = new HashMap<>(); props.put(IgfsUtils.PROP_USER_NAME, attrs.owner().getName()); props.put(IgfsUtils.PROP_GROUP_NAME, attrs.group().getName()); props.put(IgfsUtils.PROP_PERMISSION, permissions(path)); return props; }
@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(); }
Path path = Paths.get("/path/to/file.ext"); UserPrincipal owner = Files.getOwner(path); String username = owner.getName();
assertEquals(user, Files.getOwner(path).getName()); assertEquals(group, Files.readAttributes(path, PosixFileAttributes.class).group().getName()); } finally {
/** * Determines the 'owner' of the file. */ public static String getOwnerName(final File file) { try { final Path path = Paths.get(file.getAbsolutePath()); final FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(path, FileOwnerAttributeView.class); return ownerAttributeView != null ? ownerAttributeView.getOwner().getName() : null; } catch(Exception e) { logger.warn("Failed to determine 'owner' of {}: {}", file, e.getMessage()); return null; } }
@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(); }
@Override public void set(File file, String view, String attribute, Object value, boolean create) { if (attribute.equals("owner")) { UserPrincipal user = checkType(view, attribute, value, UserPrincipal.class); // TODO(cgdecker): Do we really need to do this? Any reason not to allow any UserPrincipal? if (!(user instanceof UserLookupService.JimfsUserPrincipal)) { user = createUserPrincipal(user.getName()); } file.setAttribute("owner", "owner", user); } }