static void resolveConfig(Environment env, final Settings.Builder settingsBuilder) { try { Files.walkFileTree(env.configFile(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String fileName = file.getFileName().toString(); if (fileName.startsWith("logging.")) { for (String allowedSuffix : ALLOWED_SUFFIXES) { if (fileName.endsWith(allowedSuffix)) { loadConfig(file, settingsBuilder); break; } } } return FileVisitResult.CONTINUE; } }); } catch (IOException ioe) { throw new ElasticsearchException("Failed to load logging configuration", ioe); } }
/** * * @param directory * @throws IOException */ public static void recursiveDelete(Path directory) throws IOException { Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile( Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory( Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); }
@Override public void close() { memory.clear(); try { Files.walkFileTree(tmpDir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.deleteIfExists(file); return super.visitFile(file, attrs); } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.deleteIfExists(dir); return super.postVisitDirectory(dir, exc); } }); } catch (IOException e) { throw new UncheckedIOException(e); } }
/** * Deletes a directory recursively * * @param directory * @throws IOException */ public static void deleteDirectory(Path directory) throws IOException { Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); }
/** * Deletes a directory recursively * * @param directory * @return true if deletion succeeds, false otherwise */ public static boolean deleteDirectory(Path directory) { if (directory != null) { try { Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); } catch (IOException ignored) { return false; } } return true; }
@Override public boolean execute() { File folder=CacheManager.getRootCacheInstance().getPath(); //System.gc(); try { Files.walkFileTree(folder.toPath(), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); } catch (IOException e) { logger.error(e.getMessage()); return false; } super.notifyEvent(new SumoActionEvent(SumoActionEvent.ENDACTION,"cache cleaned...", -1)); return true; }
private static void collectFiles(final Path dir, final String fileSuffix, final Map<String, Set<Path>> files) throws IOException { Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (file.toString().endsWith(fileSuffix)) { String groupName = dir.relativize(file.getParent()).toString(); Set<Path> filesSet = files.get(groupName); if (filesSet == null) { filesSet = new HashSet<>(); files.put(groupName, filesSet); } filesSet.add(file); } return FileVisitResult.CONTINUE; } }); }
private static void checkLoggerUsage(Consumer<WrongLoggerUsage> wrongUsageCallback, String... classDirectories) throws IOException { for (String classDirectory : classDirectories) { Path root = Paths.get(classDirectory); if (Files.isDirectory(root) == false) { throw new IllegalArgumentException(root + " should be an existing directory"); } Files.walkFileTree(root, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (Files.isRegularFile(file) && file.getFileName().toString().endsWith(".class")) { try (InputStream in = Files.newInputStream(file)) { ESLoggerUsageChecker.check(wrongUsageCallback, in); } } return super.visitFile(file, attrs); } }); } }
@Override public void deleteBlob(String blobName) throws IOException { Path blobPath = path.resolve(blobName); if (Files.isDirectory(blobPath)) { // delete directory recursively as long as it is empty (only contains empty directories), // which is the reason we aren't deleting any files, only the directories on the post-visit Files.walkFileTree(blobPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); } else { Files.delete(blobPath); } }
public static void removeDirectoryWithContent(Path directory) throws IOException { if (Files.isDirectory(directory)) { Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); } }
/** * Starts watching the subdirectories of the provided directory for changes. * @param root Root directory of the subdirectories to register. * @throws IOException If a subdirectory cannot be registered. * @checkstyle RequireThisCheck (20 lines) * @checkstyle NonStaticMethodCheck (20 lines) */ private void processSubevents(final Path root) throws IOException { try { Files.walkFileTree( root, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory( final Path subdir, final BasicFileAttributes attrs ) throws IOException { registerDirectory(subdir); return FileVisitResult.CONTINUE; } }); } catch (final IOException ex) { throw new IOException("Failed to register subdirectories", ex); } }
static void indexDocs(final IndexWriter writer, Path path) throws IOException { if (Files.isDirectory(path)) { Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { try { indexDoc(writer, file, attrs.lastModifiedTime().toMillis()); } catch (IOException ignore) { } return FileVisitResult.CONTINUE; } } ); } else { indexDoc(writer, path, Files.getLastModifiedTime(path).toMillis()); } }
private void registerRecursively(final Path directory) throws IOException { Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(final Path visitedDirectory, final BasicFileAttributes attrs) throws IOException { if (!FileSystemWatcher.this.watchedFiles.matchesDirectory(visitedDirectory)) { return FileVisitResult.SKIP_SUBTREE; } final WatchKey key = visitedDirectory.register(watcher, ENTRY_CREATE, ENTRY_MODIFY, ENTRY_DELETE); watchedPaths.put(key, visitedDirectory); return FileVisitResult.CONTINUE; } }); }
public void testWalkFileTreeMultiple() throws IOException { final int ITERATIONS = 1000; Path newDirectory = WALKFILETREE_FILE_DIR; for (int counter=0; counter< ITERATIONS; counter++) { HangNotifier.ping(); final List<Path> visitorFiles = new LinkedList<Path>(); // Check that we keep returning the same set of files! Files.walkFileTree(newDirectory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attributes) { if (path.toString().endsWith(".txt")) { visitorFiles.add(path); } return FileVisitResult.CONTINUE; } }); if(!ReiserSpotter.getIsReiser()){ assertEquals("Wrong number of files walked on iteration " + counter, NUMBER_OF_FILES, visitorFiles.size()); } } }
@Override protected void tearDown() throws Exception { if (tempDir != null) { // delete tempDir and its contents Files.walkFileTree(tempDir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.deleteIfExists(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { if (exc != null) { return FileVisitResult.TERMINATE; } Files.deleteIfExists(dir); return FileVisitResult.CONTINUE; } }); } }
public Set<Path> getMapFolders(final Logger logger) throws IOException { final Set<Path> mapFolders = new HashSet<>(); for(Path root : getRootPaths()) { int depth = "".equals(root.toString()) ? 0 : Iterables.size(root); Files.walkFileTree(getPath().resolve(root), ImmutableSet.of(FileVisitOption.FOLLOW_LINKS), maxDepth - depth, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if(!isExcluded(dir)) { if(MapFolder.isMapFolder(dir)) { mapFolders.add(dir); } return FileVisitResult.CONTINUE; } else { logger.fine("Skipping excluded path " + dir); return FileVisitResult.SKIP_SUBTREE; } } }); } return mapFolders; }
/** * Builds a class path with all the files selected by the given predicate * in the specified base directory and, recursively, in all of its * sub-directories. * @param dir the directory in which to find the (jar) files. * @param select tests whether or not to include a regular file. * @return the class path. * @throws NullPointerException if the argument is {@code null}. * @throws IOException if an I/O error occurs while trawling the directory. */ public static ClassPath fromDir(Path dir, Predicate<Path> select) throws IOException { requireNonNull(dir, "dir"); requireNonNull(select, "select"); ClassPath cp = new ClassPath(); Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (attrs.isRegularFile() && select.test(file)) { cp.add(file); } return FileVisitResult.CONTINUE; } }); return cp; }
private void deleteDirectory(Path directory) throws IOException { Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); }
public LocalTranslog(TranslogConfig config) throws IOException { super(config.getShardId(), config.getIndexSettings()); ReadWriteLock rwl = new ReentrantReadWriteLock(); readLock = new ReleasableLock(rwl.readLock()); writeLock = new ReleasableLock(rwl.writeLock()); this.translogPath = config.getTranslogPath(); // clean all files Files.createDirectories(this.translogPath); Files.walkFileTree(this.translogPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } }); // create a new directory writeChannel = FileChannel.open(this.translogPath.resolve(getFileNameFromId(tmpTranslogGeneration.get())), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); writtenOffset = 0; }
@After public void deleteTmpDir() throws IOException { Files.walkFileTree(this.tmpDir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult postVisitDirectory(final Path theDir, final IOException e) throws IOException { if (e != null) return TERMINATE; Files.delete(theDir); return CONTINUE; } @Override public FileVisitResult visitFile(final Path theFile, final BasicFileAttributes attrs) throws IOException { Files.delete(theFile); return CONTINUE; } }); }
/** * Deletes directory along with all inner files. * * @param path to directory. * @throws IOException if there are errors during deleting. */ public static void deleteDir(Path path) throws IOException { if (!Files.exists(path)) { return; } Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return super.postVisitDirectory(dir, exc); } }); }
/** */ @Test(expected = IgniteException.class) public void failIfChecksumDoesNotMatch() throws IOException { URI exportDirUri = folder.newFolder().toURI(); String destination = exportDirUri.toString(); backupInCompute(destination); Files.walkFileTree(Paths.get(exportDirUri), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (FileChecksumHelper.isChecksum(file)) { Files.write(file, "corrupted".getBytes()); } return FileVisitResult.CONTINUE; } }); restoreInCompute(destination); }
/** */ private void corruptDataFile(URI exportDirUri) throws IOException { Files.walkFileTree(Paths.get(exportDirUri), new SimpleFileVisitor<Path>() { private boolean firstFile; @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String fileName = file.getFileName().toString(); if (!fileName.equals(ExportNameConventionUtil.METADATA_RESOURCE_NAME)) { // skip first data file if (!firstFile) { firstFile = true; return FileVisitResult.CONTINUE; } Files.write(file, "corruption".getBytes()); return FileVisitResult.TERMINATE; } return FileVisitResult.CONTINUE; } }); }
public static void removeRecursive(final Path path) throws IOException { Files.walkFileTree( path, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { Files.deleteIfExists(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException { Files.deleteIfExists(dir); return FileVisitResult.CONTINUE; } }); }
static List<String> listClassNamesInPackage(String packageName) throws Exception { List<String> classes = new ArrayList<>(); Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(packageName.replace('.', File.separatorChar)); if (!resources.hasMoreElements()) { throw new IllegalStateException("No package found: " + packageName); } PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:*.class"); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); Files.walkFileTree(Paths.get(resource.toURI()), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { if (pathMatcher.matches(path.getFileName())) { try { String className = Paths.get(resource.toURI()).relativize(path).toString().replace(File.separatorChar, '.'); classes.add(packageName + '.' + className.substring(0, className.length() - 6)); } catch (URISyntaxException e) { throw new IllegalStateException(e); } } return FileVisitResult.CONTINUE; } }); } return classes; }
public ArchiveContainer(Path archivePath) throws IOException, ProviderNotFoundException, SecurityException { this.archivePath = archivePath; if (multiReleaseValue != null && archivePath.toString().endsWith(".jar")) { Map<String,String> env = Collections.singletonMap("multi-release", multiReleaseValue); FileSystemProvider jarFSProvider = fsInfo.getJarFSProvider(); Assert.checkNonNull(jarFSProvider, "should have been caught before!"); this.fileSystem = jarFSProvider.newFileSystem(archivePath, env); } else { this.fileSystem = FileSystems.newFileSystem(archivePath, null); } packages = new HashMap<>(); for (Path root : fileSystem.getRootDirectories()) { Files.walkFileTree(root, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { if (isValid(dir.getFileName())) { packages.put(new RelativeDirectory(root.relativize(dir).toString()), dir); return FileVisitResult.CONTINUE; } else { return FileVisitResult.SKIP_SUBTREE; } } }); } }
/** * This method is primarily used to give visual confirmation that a test case * generated files when the compilation succeeds and so generates no other output, * such as error messages. */ List<Path> showFiles(Path dir) throws IOException { List<Path> files = new ArrayList<>(); Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (Files.isRegularFile(file)) { out.println("Found " + file); files.add(file); } return FileVisitResult.CONTINUE; } }); return files; }
/** * Deletes all content of a directory (but not the directory itself). * @param root the directory to be cleaned * @throws IOException if an error occurs while cleaning the directory */ public void cleanDirectory(Path root) throws IOException { if (!Files.isDirectory(root)) { throw new IOException(root + " is not a directory"); } Files.walkFileTree(root, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes a) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e != null) { throw e; } if (!dir.equals(root)) { Files.delete(dir); } return FileVisitResult.CONTINUE; } }); }
/** * Find files matching the file extension, in one or more directories. * <p>Similar to the shell "find" command: {@code find paths -name \*.ext}. * @param fileExtension the extension to search for * @param paths the directories in which to search for files * @return the files matching the file extension * @throws IOException if an error occurred while searching for files */ public Path[] findFiles(String fileExtension, Path... paths) throws IOException { Set<Path> files = new TreeSet<>(); // use TreeSet to force a consistent order for (Path p : paths) { Files.walkFileTree(p, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (file.getFileName().toString().endsWith(fileExtension)) { files.add(file); } return FileVisitResult.CONTINUE; } }); } return files.toArray(new Path[files.size()]); }
static void delete(Path root) throws IOException { if (!Files.exists(root)) return; Files.walkFileTree(root, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path f, BasicFileAttributes a) throws IOException { Files.delete(f); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e != null) throw e; if (!dir.equals(root)) Files.delete(dir); return FileVisitResult.CONTINUE; } }); }
/** * Traverses the directory tree and collects all matching file names. * TODO: clean up exception handling. * * @param startDir start directory * @throws IOException */ private void traverse(Path startDir) throws IOException { Files.walkFileTree(startDir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String pathName = file.toString(); if (hasMatchingExtension(pathName)) { fileList.add(pathName); // System.out.println("added file " + file.toString()); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e == null) { //System.out.println("visiting " + dir.toString()); return FileVisitResult.CONTINUE; } else { // directory iteration failed throw e; } } }); }
public static void deleteFiles (String path){ try { Path directory = Paths.get(path); Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); } catch (IOException e) { e.printStackTrace(); } }
/** * Deletes the given path, and if it is a directory, all files and subdirectories within it. * * @param rootDir directory to delete * @throws IOException if any error occurs while deleting files or directories */ public static void deleteRecursively(Path rootDir) throws IOException { if (rootDir == null || !Files.exists(rootDir)) { return; } Files.walkFileTree(rootDir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); }
private static void deleteDirectory(Path dirToDelete) { try { Files.walkFileTree(dirToDelete, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); } catch (IOException e) { LOGGER.warn("Could not delete directory {}", dirToDelete); LogUtil.stacktrace(LOGGER, e); } }
/** * Register watchers recursively. Applicable for unix type operation systems. * * @param path to directory * @throws IOException during watchers registration */ private void registerWatchersRecursively(final Path path) throws IOException { Files.walkFileTree(path, new SimpleFileVisitor<Path>() { @Override public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException { LOG.trace("Registering in watcher server: {}", () -> dir); final WatchKey watchKey = dir .register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY); // , SensitivityWatchEventModifier.HIGH); if (watchKeyConsumer != null) { watchKeyConsumer.accept(watchKey); } return FileVisitResult.CONTINUE; } }); }
void processSection(ZipOutputStream zos, Section section, Path top) throws IOException { final String prefix = section.jmodDir(); Files.walkFileTree(top, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Path relPath = top.relativize(file); if (!relPath.toString().equals(MODULE_INFO) && !matches(relPath, excludes)) { try (InputStream in = Files.newInputStream(file)) { writeZipEntry(zos, in, prefix, relPath.toString()); } } return FileVisitResult.CONTINUE; } }); }