public FilePropsImpl(BasicFileAttributes attrs) { creationTime = attrs.creationTime().toMillis(); lastModifiedTime = attrs.lastModifiedTime().toMillis(); lastAccessTime = attrs.lastAccessTime().toMillis(); isDirectory = attrs.isDirectory(); isOther = attrs.isOther(); isRegularFile = attrs.isRegularFile(); isSymbolicLink = attrs.isSymbolicLink(); size = attrs.size(); }
/** Returns whether or not the file with the given name in the given dir is a directory. */ private static boolean isDirectory( SecureDirectoryStream<Path> dir, Path name, LinkOption... options) throws IOException { return dir.getFileAttributeView(name, BasicFileAttributeView.class, options) .readAttributes() .isDirectory(); }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { // size is specified on regular files only // https://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/BasicFileAttributes.html#size-- if (attrs.isRegularFile()) { size += attrs.size(); } return FileVisitResult.CONTINUE; } }
@Override public long size() throws IOException { BasicFileAttributes attrs = readAttributes(); // Don't return a size for directories or symbolic links; their sizes are implementation // specific and they can't be read as bytes using the read methods anyway. if (attrs.isDirectory()) { throw new IOException("can't read: is a directory"); } else if (attrs.isSymbolicLink()) { throw new IOException("can't read: is a symbolic link"); } return attrs.size(); }
/** * This implementation returns the underlying File's timestamp. * @see java.nio.file.Files#getLastModifiedTime(Path, java.nio.file.LinkOption...) */ @Override public long lastModified() throws IOException { // We can not use the superclass method since it uses conversion to a File and // only a Path on the default file system can be converted to a File... return Files.getLastModifiedTime(this.path).toMillis(); }
/** {@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; }
protected BlockingAction<String> createTempDirectoryInternal(String parentDir, String prefix, String perms, Handler<AsyncResult<String>> handler) { FileAttribute<?> attrs = perms == null ? null : PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(perms)); return new BlockingAction<String>(handler) { public String perform() { try { Path tmpDir; if (parentDir != null) { Path dir = vertx.resolveFile(parentDir).toPath(); if (attrs != null) { tmpDir = Files.createTempDirectory(dir, prefix, attrs); } else { tmpDir = Files.createTempDirectory(dir, prefix); } } else { if (attrs != null) { tmpDir = Files.createTempDirectory(prefix, attrs); } else { tmpDir = Files.createTempDirectory(prefix); } } return tmpDir.toFile().getAbsolutePath(); } catch (IOException e) { throw new FileSystemException(e); } } }; }
/** {@inheritDoc} */ @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { size += attrs.size(); return FileVisitResult.CONTINUE; }
private void setFileCreationDate(File f, long time) throws IOException { BasicFileAttributeView attributes = Files.getFileAttributeView(f.toPath(), BasicFileAttributeView.class); FileTime creationTime = FileTime.fromMillis(time); attributes.setTimes(creationTime, creationTime, creationTime); } }
/** * 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(); }
/** * 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(); }
@Override public void setTimes(FileTime lastModifiedTime, FileTime lastAccessTime, FileTime createTime) throws IOException { basicView.setTimes(lastModifiedTime, lastAccessTime, createTime); }
@Override public void setOwner(UserPrincipal owner) throws IOException { ownerView.setOwner(owner); } }
@Restricted(NoExternalUse.class) public static int permissionsToMode(Set<PosixFilePermission> permissions) { PosixFilePermission[] allPermissions = PosixFilePermission.values(); int result = 0; for (int i = 0; i < allPermissions.length; i++) { result <<= 1; result |= permissions.contains(allPermissions[i]) ? 1 : 0; } return result; }
@Override public Optional<Long> sizeIfKnown() { BasicFileAttributes attrs; try { attrs = readAttributes(); } catch (IOException e) { // Failed to get attributes; we don't know the size. return Optional.absent(); } // Don't return a size for directories or symbolic links; their sizes are implementation // specific and they can't be read as bytes using the read methods anyway. if (attrs.isDirectory() || attrs.isSymbolicLink()) { return Optional.absent(); } return Optional.of(attrs.size()); }
/** Returns whether or not the file with the given name in the given dir is a directory. */ private static boolean isDirectory( SecureDirectoryStream<Path> dir, Path name, LinkOption... options) throws IOException { return dir.getFileAttributeView(name, BasicFileAttributeView.class, options) .readAttributes() .isDirectory(); }
@Override public void setTimes(FileTime lastModifiedTime, FileTime lastAccessTime, FileTime createTime) throws IOException { basicView.setTimes(lastModifiedTime, lastAccessTime, createTime); }
@Override public void setOwner(UserPrincipal owner) throws IOException { ownerView.setOwner(owner); } }
/** Returns whether or not the file with the given name in the given dir is a directory. */ private static boolean isDirectory( SecureDirectoryStream<Path> dir, Path name, LinkOption... options) throws IOException { return dir.getFileAttributeView(name, BasicFileAttributeView.class, options) .readAttributes() .isDirectory(); }
/** Returns whether or not the file with the given name in the given dir is a directory. */ private static boolean isDirectory( SecureDirectoryStream<Path> dir, Path name, LinkOption... options) throws IOException { return dir.getFileAttributeView(name, BasicFileAttributeView.class, options) .readAttributes() .isDirectory(); }