@Override public AbstractFile getCanonicalFile() { return file.getCanonicalFile(); }
@Override public AbstractFile getCanonicalFile() { if(!getCanonicalFileSet) { getCanonicalFile = file.getCanonicalFile(); // Create a CachedFile instance around the file if recursion is enabled if(recurseInstances) { // AbstractFile#getCanonicalFile() may return 'this' if the file is not a symlink. In that case, // no need to create a new CachedFile, simply use this one. if(getCanonicalFile==file) getCanonicalFile = this; else getCanonicalFile = new CachedFile(getCanonicalFile, true); } getCanonicalFileSet = true; } return getCanonicalFile; }
private AbstractFile resolveSymlink(AbstractFile file, Set<AbstractFile> visitedFiles) { if (visitedFiles.contains(file)) return null; else { visitedFiles.add(file); return file.isSymlink() ? resolveSymlink(file.getCanonicalFile(), visitedFiles) : file; } }
/** * Pre-fetch the attributes that are used by the table renderer and some actions from the given CachedFile. * By doing so, the attributes will be available when the associated getters are called and thus the methods won't * be I/O bound and will not lock. * * @param cachedFile a CachedFile instance from which to pre-fetch attributes */ private static void prefetchCachedFileAttributes(AbstractFile cachedFile) { cachedFile.isDirectory(); cachedFile.isBrowsable(); cachedFile.isHidden(); // Pre-fetch isSymlink attribute and if the file is a symlink, pre-fetch the canonical file and its attributes if(cachedFile.isSymlink()) { AbstractFile canonicalFile = cachedFile.getCanonicalFile(); if(canonicalFile!=cachedFile) // Cheap test to prevent infinite recursion on bogus file implementations prefetchCachedFileAttributes(canonicalFile); } }
/** * Tests {@link AbstractFile#getCanonicalPath()} by asserting that it returns a non-null value, that the file can * be resolved again using this path, and that the resolved file is the same as the orginal file. * The tests are performed on a regular file and a directory file. * * @throws IOException should not happen * @throws NoSuchAlgorithmException should not happen */ @Test public void testCanonicalPath() throws IOException, NoSuchAlgorithmException { // Regular file createFile(tempFile, 1); testPathResolution(tempFile.getCanonicalFile(), tempFile.getCanonicalPath()); // Directory file tempFile.delete(); tempFile.mkdir(); testPathResolution(tempFile.getCanonicalFile(), tempFile.getCanonicalPath()); // Test getCanonicalPath(boolean) on the directory file assert tempFile.getCanonicalPath(true).endsWith(tempFile.getSeparator()); assert !tempFile.getCanonicalPath(false).endsWith(tempFile.getSeparator()); }
file = file.getCanonicalFile();