@Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { if (!attrs.isSymbolicLink()) { // Broken link that can't be followed entries.add(file.toUri().toURL()); } 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(); }
@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()); }
@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(); }
@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(); }
@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()); }
@Restricted(NoExternalUse.class) public static boolean isSymlink(@Nonnull Path path) { /* * Windows Directory Junctions are effectively the same as Linux symlinks to directories. * Unfortunately, the Java 7 NIO2 API function isSymbolicLink does not treat them as such. * It thinks of them as normal directories. To use the NIO2 API & treat it like a symlink, * you have to go through BasicFileAttributes and do the following check: * isSymbolicLink() || isOther() * The isOther() call will include Windows reparse points, of which a directory junction is. * It also includes includes devices, but reading the attributes of a device with NIO fails * or returns false for isOther(). (i.e. named pipes such as \\.\pipe\JenkinsTestPipe return * false for isOther(), and drives such as \\.\PhysicalDrive0 throw an exception when * calling readAttributes. */ try { BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); return attrs.isSymbolicLink() || (attrs instanceof DosFileAttributes && attrs.isOther()); } catch (IOException ignored) { return false; } }
/** * Delete directory recursively. * * @param f Directory. * @param deleteIfExists Ignore delete errors if the file doesn't exist. * @return {@code true} if successful. */ private boolean deleteRecursive(File f, boolean deleteIfExists) { BasicFileAttributes attrs; try { attrs = Files.readAttributes(f.toPath(), BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); } catch (IOException ignore) { return deleteIfExists && !f.exists(); } if (!attrs.isDirectory() || attrs.isSymbolicLink()) return f.delete() || (deleteIfExists && !f.exists()); File[] entries = f.listFiles(); if (entries != null) { for (File entry : entries) { boolean res = deleteRecursive(entry, true); if (!res) return false; } } return f.delete() || (deleteIfExists && !f.exists()); }
@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(); }
@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()); }
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(); }
/** * Determines the file type of a {@link Path}. * * @param p Path * @return FileType * @throws IOException if file attributes cannot be read */ public static FileType of(Path p) throws IOException { BasicFileAttributes attrs = Files.readAttributes(p, BasicFileAttributes.class); if (attrs.isRegularFile()) { return FileType.REGULAR_FILE; } else if (attrs.isDirectory()) { return FileType.DIR; } else if (attrs.isSymbolicLink()) { return FileType.SYMLINK; } return FileType.OTHER; }
public static Memory filetype(Environment env, TraceInfo trace, String path) { File file = new File(path); if (file.isFile()) return new StringMemory("file"); else if (file.isDirectory()) return new StringMemory("dir"); else { try { BasicFileAttributes attributes = Files.readAttributes(Paths.get(path), BasicFileAttributes.class); if (attributes.isSymbolicLink()) { return new StringMemory("link"); } return new StringMemory("unknown"); } catch (IOException e) { env.warning(trace, e.getMessage()); return Memory.FALSE; } } }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (attrs.isSymbolicLink()) { // symbolic link to folder return FileVisitResult.CONTINUE; } TarArchiveEntry tarEntry = new TarArchiveEntry(FilePathUtil.relativize(basePath, file)); if (file.toFile().canExecute()) { tarEntry.setMode(tarEntry.getMode() | 0755); } CompressArchiveUtil.putTarEntry(tarArchiveOutputStream, tarEntry, file); return FileVisitResult.CONTINUE; }
boolean isSymlinkDir = attrs.isDirectory() && attrs.isSymbolicLink();
@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()); }
if (fileAttributes.isSymbolicLink()) { fileProperties.type = FileType.SYMLINK; fileProperties.linkTarget = FileUtil.readSymlinkTarget(file);
@Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { if (!attrs.isSymbolicLink()) { Files.delete(path); } return FileVisitResult.CONTINUE; }
Path file = ...; BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class); System.out.println("creationTime: " + attr.creationTime()); System.out.println("lastAccessTime: " + attr.lastAccessTime()); System.out.println("lastModifiedTime: " + attr.lastModifiedTime()); System.out.println("isDirectory: " + attr.isDirectory()); System.out.println("isOther: " + attr.isOther()); System.out.println("isRegularFile: " + attr.isRegularFile()); System.out.println("isSymbolicLink: " + attr.isSymbolicLink()); System.out.println("size: " + attr.size());
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(); }