Refine search
@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()); }
/** * 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; }
protected Map<String, String> getAttributesFromFile(final Path file) { Map<String, String> attributes = new HashMap<>(); try { FileStore store = Files.getFileStore(file); if (store.supportsFileAttributeView("basic")) { try { final DateFormat formatter = new SimpleDateFormat(FILE_MODIFY_DATE_ATTR_FORMAT, Locale.US); BasicFileAttributeView view = Files.getFileAttributeView(file, BasicFileAttributeView.class); BasicFileAttributes attrs = view.readAttributes(); attributes.put(FILE_LAST_MODIFY_TIME_ATTRIBUTE, formatter.format(new Date(attrs.lastModifiedTime().toMillis()))); FileOwnerAttributeView view = Files.getFileAttributeView(file, FileOwnerAttributeView.class); attributes.put(FILE_OWNER_ATTRIBUTE, view.getOwner().getName()); } catch (Exception ignore) { try { PosixFileAttributeView view = Files.getFileAttributeView(file, PosixFileAttributeView.class); attributes.put(FILE_PERMISSIONS_ATTRIBUTE, PosixFilePermissions.toString(view.readAttributes().permissions())); attributes.put(FILE_GROUP_ATTRIBUTE, view.readAttributes().group().getName()); } catch (Exception ignore) {
/** * Gets local file's group. * * @param filePath the file path * @return the group of the local file */ public static String getLocalFileGroup(String filePath) throws IOException { PosixFileAttributes attr = Files.readAttributes(Paths.get(filePath), PosixFileAttributes.class); return attr.group().getName(); }
/** * Gets local file's permission mode. * * @param filePath the file path * @return the file mode in short, e.g. 0777 */ public static short getLocalFileMode(String filePath) throws IOException { Set<PosixFilePermission> permission = Files.readAttributes(Paths.get(filePath), PosixFileAttributes.class).permissions(); return translatePosixPermissionToMode(permission); }
@Override public FileInfo fetchFileInfo(String fileName) { Path path = Paths.get(fileName); FileInfo info = new FileInfo(); PosixFileAttributes attrs = Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS); if (attrs.isSymbolicLink()) { info.setAttribute(EFS.ATTRIBUTE_SYMLINK, true); info.setStringAttribute(EFS.ATTRIBUTE_LINK_TARGET, Files.readSymbolicLink(path).toString()); attrs = Files.readAttributes(path, PosixFileAttributes.class); info.setLastModified(attrs.lastModifiedTime().toMillis()); info.setLength(attrs.size()); info.setDirectory(attrs.isDirectory()); Set<PosixFilePermission> perms = attrs.permissions(); info.setAttribute(EFS.ATTRIBUTE_OWNER_READ, perms.contains(PosixFilePermission.OWNER_READ)); info.setAttribute(EFS.ATTRIBUTE_OWNER_WRITE, perms.contains(PosixFilePermission.OWNER_WRITE));
filePath = Paths.get(file.getAbsolutePath()); fileProperties.exists = Files.exists(filePath, LinkOption.NOFOLLOW_LINKS); DosFileAttributes dosAttrs = Files.readAttributes(filePath, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS); fileProperties.dosAttributes = FileUtil.dosAttrsToString(dosAttrs); PosixFileAttributes posixAttrs = Files.readAttributes(filePath, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS); fileProperties.posixPermissions = PosixFilePermissions.toString(posixAttrs.permissions());
/** * 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(); }
boolean deleted = false; try { Path p = Paths.get(path); boolean isDir = false; boolean isSymlink = false; if (!OSValidator.isWindows()) { isDir = Files.readAttributes(p, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS) .isDirectory(); isSymlink = Files.readAttributes(p, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS) .isSymbolicLink(); } else { isDir = new File(path).isDirectory(); if (!localOnly) eventBus.post(new MFileDeleted(mf, true)); deleted = Files.deleteIfExists(p); if (!localOnly && !deleted) eventBus.post(new MFileWritten(mf, true));
private SegmentProperties doGetStreamSegmentInfo(String streamSegmentName) throws IOException { long traceId = LoggerHelpers.traceEnter(log, "getStreamSegmentInfo", streamSegmentName); PosixFileAttributes attrs = Files.readAttributes(Paths.get(config.getRoot(), streamSegmentName), PosixFileAttributes.class); StreamSegmentInformation information = StreamSegmentInformation.builder() .name(streamSegmentName) .length(attrs.size()) .sealed(!(attrs.permissions().contains(OWNER_WRITE))) .lastModified(new ImmutableDate(attrs.creationTime().toMillis())) .build(); LoggerHelpers.traceLeave(log, "getStreamSegmentInfo", traceId, streamSegmentName); return information; }
private static List<FileAttribute<Set<PosixFilePermission>>> getPosixAttributes(Path file) throws IOException { if (Files.exists(file) && supportsFileOwnerAttributeView(file, PosixFileAttributeView.class)) { PosixFileAttributeView posixView = Files.getFileAttributeView(file, PosixFileAttributeView.class); if (posixView != null) { return Collections.singletonList(PosixFilePermissions.asFileAttribute(posixView.readAttributes().permissions())); } } return Collections.emptyList(); }
private static void walk(Path current, boolean inExecDir) throws IOException { if (Files.isDirectory(current)) { List<Path> list = Files.list(current).collect(Collectors.toList()); for (Path child : list) { boolean next = inExecDir || Optional.ofNullable(child.getFileName()) .map(Path::toString) .filter(TRIGGER_NAME::contains) .isPresent(); walk(child, next); } } else if (isExecutableTarget(current, inExecDir)) { PosixFileAttributeView attr = Files.getFileAttributeView(current, PosixFileAttributeView.class); Set<PosixFilePermission> perms = attr.readAttributes().permissions(); if (perms.containsAll(EXECUTABLES) == false) { System.out.printf("set executable: %s (%s)%n", current, PosixFilePermissions.toString(perms)); perms.addAll(EXECUTABLES); attr.setPermissions(perms); } } }
/** {@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; }
final PathAttributes attributes = new PathAttributes(); final Class<? extends BasicFileAttributes> provider = isPosix ? PosixFileAttributes.class : DosFileAttributes.class; final BasicFileAttributes a = Files.readAttributes(file, provider, LinkOption.NOFOLLOW_LINKS); if(a.isRegularFile()) { attributes.setSize(a.size()); attributes.setAccessedDate(a.lastAccessTime().toMillis()); if(isPosix) { attributes.setOwner(((PosixFileAttributes) a).owner().getName()); attributes.setGroup(((PosixFileAttributes) a).group().getName()); attributes.setPermission(new Permission(PosixFilePermissions.toString(((PosixFileAttributes) a).permissions()))); if(Files.isReadable(file)) { actions = actions.or(Permission.Action.read); if(Files.isWritable(file)) { actions = actions.or(Permission.Action.write);
boolean isSymlink = false; if (!OSValidator.isWindows()) { isDir = Files.readAttributes(f.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS) .isDirectory(); isSymlink = Files.readAttributes(f.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS) .isSymbolicLink(); } else { isDir = f.isDirectory(); metaData.put("encrypt", Boolean.toString(Main.chunkStoreEncryptionEnabled)); metaData.put("lastmodified", Long.toString(f.lastModified())); String slp = EncyptUtils.encString(Files.readSymbolicLink(f.toPath()).toFile().getPath(), Main.chunkStoreEncryptionEnabled); metaData.put("symlink", slp);
private static void makeWritable(@Nonnull Path path) throws IOException { if (!Functions.isWindows()) { try { PosixFileAttributes attrs = Files.readAttributes(path, PosixFileAttributes.class); Set<PosixFilePermission> newPermissions = attrs.permissions(); newPermissions.add(PosixFilePermission.OWNER_WRITE); Files.setPosixFilePermissions(path, newPermissions); } catch (NoSuchFileException ignored) { return; } catch (UnsupportedOperationException ignored) { // PosixFileAttributes not supported, fall back to old IO. } } /* * We intentionally do not check the return code of setWritable, because if it * is false we prefer to rethrow the exception thrown by Files.deleteIfExists, * which will have a more useful message than something we make up here. */ path.toFile().setWritable(true); }
@Override public boolean isSelected(File basedir, String filename, File file) { if (group == null) { throw new BuildException("the group attribute is required"); } try { GroupPrincipal actualGroup = followSymlinks ? Files.readAttributes(file.toPath(), PosixFileAttributes.class).group() : Files.readAttributes(file.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS).group(); return actualGroup != null && actualGroup.getName().equals(group); } catch (IOException e) { // => not the expected group } return false; } }
final Instant m_time; final PosixFileAttributeView posix_view = Files.getFileAttributeView( file, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); final PosixFileAttributes attribs = posix_view.readAttributes(); size = BigInteger.valueOf(attribs.size()); owner = attribs.owner().getName(); group = attribs.group().getName(); perms = attribs.permissions(); a_time = attribs.lastAccessTime().toInstant(); m_time = attribs.lastModifiedTime().toInstant(); c_time = attribs.creationTime().toInstant(); } else { final BasicFileAttributeView basic_view = Files.getFileAttributeView( file, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
boolean symlink = false; if (!OSValidator.isWindows() && !followSymlinks) symlink = Files.readAttributes(file.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS) .isSymbolicLink(); if (!symlink) { if (file.isDirectory()) {
assertEquals(filePermissions, PosixFilePermissions.toString(Files.getPosixFilePermissions(path))); assertEquals(user, Files.getOwner(path).getName()); assertEquals(group, Files.readAttributes(path, PosixFileAttributes.class).group().getName()); } finally { appender.stop();