private void processDirectory(File file, final Trie.Builder builder) { new DirectoryFileTree(file).visit(new FileVisitor() { @Override public void visitDir(FileVisitDetails dirDetails) { } @Override public void visitFile(FileVisitDetails fileDetails) { try { ZipEntry zipEntry = new ZipEntry(fileDetails.getPath()); InputStream inputStream = fileDetails.open(); try { processEntry(zipEntry, builder); } finally { inputStream.close(); } } catch (IOException e) { throw new UncheckedIOException(e); } } }); }
JarSnapshot createSnapshot(HashCode hash, FileTree classes, final ClassFilesAnalyzer analyzer) { final Map<String, HashCode> hashes = Maps.newHashMap(); classes.visit(new FileVisitor() { public void visitDir(FileVisitDetails dirDetails) { } public void visitFile(FileVisitDetails fileDetails) { analyzer.visitFile(fileDetails); String className = fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", ""); HashCode classHash = hasher.hash(fileDetails.getFile()); hashes.put(className, classHash); } }); return new JarSnapshot(new JarSnapshotData(hash, hashes, analyzer.getAnalysis())); }
@Override public void visitTreeOrBackingFile(final FileVisitor visitor) { File backingFile = getBackingFile(); if (backingFile!=null) { new SingletonFileTree(backingFile).visit(visitor); } else { // We need to wrap the visitor so that the file seen by the visitor has already // been extracted from the archive and we do not try to extract it again. // It's unsafe to keep the FileVisitDetails provided by TarFileTree directly // because we do not expect to visit the same paths again (after extracting everything). visit(new FileVisitor() { @Override public void visitDir(FileVisitDetails dirDetails) { visitor.visitDir(new DefaultFileVisitDetails(dirDetails.getFile(), chmod, stat)); } @Override public void visitFile(FileVisitDetails fileDetails) { visitor.visitFile(new DefaultFileVisitDetails(fileDetails.getFile(), chmod, stat)); } }); } }
private void doVisit(FileVisitor visitor, File file, LinkedList<String> relativePath, int segmentIndex, AtomicBoolean stopFlag) { if (stopFlag.get()) { return; } String segment = patternSegments.get(segmentIndex); if (segment.contains("**")) { PatternSet patternSet = new PatternSet(); patternSet.include(includePattern); patternSet.exclude(excludeSpec); DirectoryFileTree fileTree = new DirectoryFileTree(baseDir, patternSet); fileTree.visitFrom(visitor, file, new RelativePath(file.isFile(), relativePath.toArray(new String[relativePath.size()]))); } else if (segment.contains("*") || segment.contains("?")) { PatternStep step = PatternStepFactory.getStep(segment, false); File[] children = file.listFiles(); if (children == null) { if (!file.canRead()) { throw new GradleException(String.format("Could not list contents of directory '%s' as it is not readable.", file)); } // else, might be a link which points to nothing, or has been removed while we're visiting, or ... throw new GradleException(String.format("Could not list contents of '%s'.", file)); } for (File child : children) { if (stopFlag.get()) { break; } String childName = child.getName(); if (step.matches(childName)) { relativePath.addLast(childName); doVisitDirOrFile(visitor, child, relativePath, segmentIndex + 1, stopFlag); relativePath.removeLast(); } } } else { relativePath.addLast(segment); doVisitDirOrFile(visitor, new File(file, segment), relativePath, segmentIndex + 1, stopFlag); relativePath.removeLast(); } }
JarSnapshot createSnapshot(byte[] hash, FileTree classes, final ClassFilesAnalyzer analyzer) { final Map<String, byte[]> hashes = new HashMap<String, byte[]>(); classes.visit(new FileVisitor() { public void visitDir(FileVisitDetails dirDetails) { } public void visitFile(FileVisitDetails fileDetails) { analyzer.visitFile(fileDetails); String className = fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", ""); byte[] classHash = hasher.hash(fileDetails.getFile()); hashes.put(className, classHash); } }); return new JarSnapshot(new JarSnapshotData(hash, hashes, analyzer.getAnalysis())); }
private Collection<String> accumulatePackages() { final Collection<String> result = new HashSet<>(); compileSourceRoots.getAsFileTree().visit(new FileVisitor() { @Override public void visitDir(FileVisitDetails dirDetails) { final String packageName = dirDetails.getRelativePath().getPathString().replace(File.separatorChar, '.'); if (logger.isDebugEnabled()) { logger.debug("Packages: " + packageName); } result.add(packageName); } @Override public void visitFile(FileVisitDetails fileDetails) { // ignore } }); return result; }
public static void toRelativeFiles(final FileCollection source, final List<RelativeFile> targets) { FileTree fileTree = source.getAsFileTree(); fileTree.visit(new FileVisitor() { public void visitDir(FileVisitDetails dirDetails) {} public void visitFile(FileVisitDetails fileDetails) { targets.add(new RelativeFile(fileDetails.getFile(), fileDetails.getRelativePath())); } }); }
private void visitImpl(FileVisitor visitor, InputStream inputStream) throws IOException { AtomicBoolean stopFlag = new AtomicBoolean(); NoCloseTarInputStream tar = new NoCloseTarInputStream(inputStream); TarEntry entry; while (!stopFlag.get() && (entry = tar.getNextEntry()) != null) { if (entry.isDirectory()) { visitor.visitDir(new DetailsImpl(entry, tar, stopFlag, chmod)); } else { visitor.visitFile(new DetailsImpl(entry, tar, stopFlag, chmod)); } } }
@Override public void visitTreeOrBackingFile(FileVisitor visitor) { File backingFile = getBackingFile(); if (backingFile!=null) { new SingletonFileTree(backingFile).visit(visitor); } else { visit(visitor); } }
private void visitDirs(RelativePath path, FileVisitor visitor) { if (path == null || path.getParent() == null || !visitedDirs.add(path)) { return; } visitDirs(path.getParent(), visitor); visitor.visitDir(new FileVisitDetailsImpl(path, null, stopFlag, chmod)); }
@Override public void walkDir(File file, RelativePath path, FileVisitor visitor, Spec<? super FileTreeElement> spec, AtomicBoolean stopFlag, boolean postfix) { File[] children = file.listFiles(); if (children == null) { if (file.isDirectory() && !file.canRead()) { throw new GradleException(String.format("Could not list contents of directory '%s' as it is not readable.", file)); } // else, might be a link which points to nothing, or has been removed while we're visiting, or ... throw new GradleException(String.format("Could not list contents of '%s'.", file)); } List<FileVisitDetails> dirs = new ArrayList<FileVisitDetails>(); for (int i = 0; !stopFlag.get() && i < children.length; i++) { File child = children[i]; boolean isFile = child.isFile(); RelativePath childPath = path.append(isFile, child.getName()); FileVisitDetails details = new DefaultFileVisitDetails(child, childPath, stopFlag, fileSystem, fileSystem, !isFile); if (DirectoryFileTree.isAllowed(details, spec)) { if (isFile) { visitor.visitFile(details); } else { dirs.add(details); } } } // now handle dirs for (int i = 0; !stopFlag.get() && i < dirs.size(); i++) { FileVisitDetails dir = dirs.get(i); if (postfix) { walkDir(dir.getFile(), dir.getRelativePath(), visitor, spec, stopFlag, postfix); visitor.visitDir(dir); } else { visitor.visitDir(dir); walkDir(dir.getFile(), dir.getRelativePath(), visitor, spec, stopFlag, postfix); } } }
/** * Process the specified file or directory. If it is a directory, then its contents * (but not the directory itself) will be checked with {@link #isAllowed(FileTreeElement, Spec)} and notified to * the listener. If it is a file, the file will be checked and notified. */ public void visitFrom(FileVisitor visitor, File fileOrDirectory, RelativePath path) { AtomicBoolean stopFlag = new AtomicBoolean(); Spec<FileTreeElement> spec = patternSet.getAsSpec(); if (fileOrDirectory.exists()) { if (fileOrDirectory.isFile()) { processSingleFile(fileOrDirectory, visitor, spec, stopFlag); } else { walkDir(fileOrDirectory, path, visitor, spec, stopFlag); } } else { LOGGER.info("file or directory '{}', not found", fileOrDirectory); } }
private void processSingleFile(File file, FileVisitor visitor, Spec<FileTreeElement> spec, AtomicBoolean stopFlag) { RelativePath path = new RelativePath(true, file.getName()); FileVisitDetails details = new DefaultFileVisitDetails(file, path, stopFlag, fileSystem, fileSystem, false); if (isAllowed(details, spec)) { visitor.visitFile(details); } }
private void doVisit(FileVisitor visitor, File file, LinkedList<String> relativePath, int segmentIndex, AtomicBoolean stopFlag) { if (stopFlag.get()) { return; } String segment = patternSegments.get(segmentIndex); if (segment.contains("**")) { PatternSet patternSet = new PatternSet(); patternSet.include(includePattern); patternSet.exclude(excludeSpec); DirectoryFileTree fileTree = new DirectoryFileTree(baseDir, patternSet); fileTree.visitFrom(visitor, file, new RelativePath(file.isFile(), relativePath.toArray(new String[relativePath.size()]))); } else if (segment.contains("*") || segment.contains("?")) { PatternStep step = PatternStepFactory.getStep(segment, false); File[] children = file.listFiles(); if (children == null) { if (!file.canRead()) { throw new GradleException(String.format("Could not list contents of directory '%s' as it is not readable.", file)); } // else, might be a link which points to nothing, or has been removed while we're visiting, or ... throw new GradleException(String.format("Could not list contents of '%s'.", file)); } for (File child : children) { if (stopFlag.get()) { break; } if (step.matches(child.getName())) { relativePath.addLast(child.getName()); doVisitDirOrFile(visitor, child, relativePath, segmentIndex + 1, stopFlag); relativePath.removeLast(); } } } else { relativePath.addLast(segment); doVisitDirOrFile(visitor, new File(file, segment), relativePath, segmentIndex + 1, stopFlag); relativePath.removeLast(); } }
public void visit(FileVisitor visitor) { InputStream inputStream; try { inputStream = resource.read(); assert inputStream != null; } catch (MissingResourceException e) { DeprecationLogger.nagUserOfDeprecatedBehaviour( String.format("The specified tar file %s does not exist and will be silently ignored", getDisplayName()) ); return; } catch (ResourceException e) { throw new InvalidUserDataException(String.format("Cannot expand %s.", getDisplayName()), e); } try { try { visitImpl(visitor, inputStream); } finally { inputStream.close(); } } catch (Exception e) { String message = "Unable to expand " + getDisplayName() + "\n" + " The tar might be corrupted or it is compressed in an unexpected way.\n" + " By default the tar tree tries to guess the compression based on the file extension.\n" + " If you need to specify the compression explicitly please refer to the DSL reference."; throw new GradleException(message, e); } }
JarSnapshot createSnapshot(FileTree archivedClasses) { final Map<String, byte[]> hashes = new HashMap<String, byte[]>(); archivedClasses.visit(new FileVisitor() { public void visitDir(FileVisitDetails dirDetails) { } public void visitFile(FileVisitDetails fileDetails) { hashes.put(fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", ""), hasher.hash(fileDetails.getFile())); } }); return new JarSnapshot(hashes); }
private static Set<String> classesInJar(FileTree jarContents) { final Set<String> out = new HashSet<String>(); jarContents.visit(new FileVisitor() { public void visitDir(FileVisitDetails dirDetails) {} public void visitFile(FileVisitDetails fileDetails) { out.add(fileDetails.getPath().replaceAll("/", ".").replaceAll("\\.class$", "")); } }); return out; }
public FileTree visit(FileVisitor visitor) { for (FileTree tree : getSourceCollections()) { tree.visit(visitor); } return this; }
@Override public void visitTreeOrBackingFile(FileVisitor visitor) { visit(visitor); }
public FileTree visit(FileVisitor visitor) { tree.visit(visitor); return this; }
public void visitTreeOrBackingFile(FileVisitor visitor) { tree.visitTreeOrBackingFile(visitor); }
public Visit(FileVisitor visitor, AtomicBoolean stopFlag) { this.visitor = visitor; this.stopFlag = stopFlag; }
public void visit(FileVisitor visitor) { doVisit(visitor, baseDir, new LinkedList<String>(), 0, new AtomicBoolean()); }
public void visit(FileVisitor visitor) { visitor.visitFile(new SingletonFileVisitDetails(file, fileSystem, false)); }
public void visit(FileVisitor visitor) { visitFrom(visitor, dir, RelativePath.EMPTY_ROOT); }
private void walkDir(File file, RelativePath path, FileVisitor visitor, Spec<FileTreeElement> spec, AtomicBoolean stopFlag) { directoryWalkerFactory.create().walkDir(file, path, visitor, spec, stopFlag, postfix); }
public void visit(FileVisitor visitor) { }