/** * 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(); }
@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 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 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; }
/** {@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; }
@Override public UserPrincipal getOwner() throws IOException { return read().owner(); }
@Override public UserPrincipal getOwner() throws IOException { PosixFileAttributes v = provider.readAttributes(path, PosixFileAttributes.class, options); return v.owner(); }
@Override public UserPrincipal getOwner() throws IOException { return readAttributes().owner(); }
/** * 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(); }
@Override public UserPrincipal getOwner() throws IOException { return readAttributes().owner(); }
@Override public UserPrincipal getOwner() throws IOException { return readAttributes().owner(); }
@Override public UserPrincipal getOwner() throws IOException { return readAttributes().owner(); }
@Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { setPosixFileAttributes(dir, parentDirAttributes.owner(), parentDirAttributes.group(), parentDirAttributes.permissions()); return FileVisitResult.CONTINUE; } });
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (attrs.isRegularFile()) { setPosixFileAttributes(file, parentDirAttributes.owner(), parentDirAttributes.group(), filePermissions); } return FileVisitResult.CONTINUE; }
@Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { setPosixFileAttributes(dir, parentDirAttributes.owner(), parentDirAttributes.group(), parentDirAttributes.permissions()); return FileVisitResult.CONTINUE; } });
@Test @Category( { Attributes.class, Posix.class, OwnerView.class } ) public void testOwnerByTwoMethods() throws IOException { assertThat( Files.getOwner( pathDefault() ) ).isEqualTo( Files.readAttributes( pathDefault(), PosixFileAttributes.class ).owner() ); }
private void addDetails() throws IOException { if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) { Path path = Paths.get(this.path); PosixFileAttributes attrs = Files.getFileAttributeView(path, PosixFileAttributeView.class).readAttributes(); permissions = PosixFilePermissions.toString(attrs.permissions()); UserPrincipal ownerPrincipal = attrs.owner(); owner = ownerPrincipal.toString(); UserPrincipal groupPrincipal = attrs.group(); group = groupPrincipal.toString(); } }
private static void copyOwner(Path source, Path target, boolean sameFileSystem, PosixFileAttributes posixAttributes, PosixFileAttributeView posixView, LinkOption[] linkOptions) throws IOException { UserPrincipal owner = posixAttributes.owner(); if (!sameFileSystem) { UserPrincipalLookupService userPrincipalLookupService = source.getFileSystem().getUserPrincipalLookupService(); try { owner = userPrincipalLookupService.lookupPrincipalByName(owner.getName()); } catch (UserPrincipalNotFoundException e) { // user doesn't exist in target file system, ignore return; } } posixView.setOwner(owner); }