/** * Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories. * <p> * The difference between File.delete() and this method are: * <ul> * <li>A directory to be deleted does not have to be empty.</li> * <li>No exceptions are thrown when a file or directory cannot be deleted.</li> * </ul> * * @param file file or directory to delete, can be {@code null} * @return {@code true} if the file or directory was deleted, otherwise {@code false} */ public static boolean deleteQuietly(@Nullable File file) { if (file == null) { return false; } return deleteQuietly(file.toPath()); }
void createOrCleanupDir(File dir) throws IOException { FileUtils.forceMkdir(dir); org.sonar.core.util.FileUtils.cleanDirectory(dir); } }
/** * Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories. * <p> * The difference between File.delete() and this method are: * <ul> * <li>A directory to be deleted does not have to be empty.</li> * <li>No exceptions are thrown when a file or directory cannot be deleted.</li> * </ul> * * @param path file or directory to delete, can be {@code null} * @return {@code true} if the file or directory was deleted, otherwise {@code false} */ public static boolean deleteQuietly(@Nullable Path path) { if (path == null) { return false; } try { if (path.toFile().isDirectory()) { deleteDirectory(path); } else { Files.delete(path); } return true; } catch (IOException | SecurityException ignored) { return false; } }
/** * Deletes a directory recursively. * * @param directory directory to delete * @throws IOException in case deletion is unsuccessful */ public static void deleteDirectory(Path directory) throws IOException { requireNonNull(directory, DIRECTORY_CAN_NOT_BE_NULL); deleteDirectoryImpl(directory); }
private static void deleteDirectoryImpl(Path path) throws IOException { requireNonNull(path, DIRECTORY_CAN_NOT_BE_NULL); File file = path.toFile(); if (!file.exists()) { return; } checkIO(!Files.isSymbolicLink(path), "Directory '%s' is a symbolic link", path); checkIO(!file.isFile(), "Directory '%s' is a file", path); Files.walkFileTree(path, DeleteRecursivelyFileVisitor.INSTANCE); checkIO(!file.exists(), "Unable to delete directory '%s'", path); }
/** * Cleans a directory recursively. * * @param directory directory to delete * @throws IOException in case deletion is unsuccessful */ public static void cleanDirectory(File directory) throws IOException { requireNonNull(directory, DIRECTORY_CAN_NOT_BE_NULL); Path path = directory.toPath(); if (!path.toFile().exists()) { return; } cleanDirectoryImpl(path); }
/** * Deletes a directory recursively. * * @param directory directory to delete * @throws IOException in case deletion is unsuccessful */ public static void deleteDirectory(File directory) throws IOException { requireNonNull(directory, DIRECTORY_CAN_NOT_BE_NULL); deleteDirectoryImpl(directory.toPath()); }
private static void deleteDirectoryImpl(Path path) throws IOException { requireNonNull(path, DIRECTORY_CAN_NOT_BE_NULL); File file = path.toFile(); if (!file.exists()) { return; } checkIO(!Files.isSymbolicLink(path), "Directory '%s' is a symbolic link", path); checkIO(!file.isFile(), "Directory '%s' is a file", path); Files.walkFileTree(path, DeleteRecursivelyFileVisitor.INSTANCE); checkIO(!file.exists(), "Unable to delete directory '%s'", path); }
/** * Cleans a directory recursively. * * @param directory directory to delete * @throws IOException in case deletion is unsuccessful */ public static void cleanDirectory(File directory) throws IOException { requireNonNull(directory, DIRECTORY_CAN_NOT_BE_NULL); Path path = directory.toPath(); if (!path.toFile().exists()) { return; } cleanDirectoryImpl(path); }
private static void cleanTempFolders(Path path) throws IOException { if (path.toFile().exists()) { try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, new CleanFilter())) { for (Path p : stream) { deleteQuietly(p.toFile()); } } } }
public void cancelDownloads() { try { if (downloadDir.exists()) { org.sonar.core.util.FileUtils.cleanDirectory(downloadDir); } } catch (IOException e) { throw new IllegalStateException("Fail to clean the plugin downloads directory: " + downloadDir, e); } }
@Test public void deleteDirectory_does_not_fail_if_file_does_not_exist() throws IOException { File file = new File(temporaryFolder.newFolder(), "foo.d"); FileUtils.deleteDirectory(file); }
/** * Deletes a directory recursively. * * @param directory directory to delete * @throws IOException in case deletion is unsuccessful */ public static void deleteDirectory(Path directory) throws IOException { requireNonNull(directory, DIRECTORY_CAN_NOT_BE_NULL); deleteDirectoryImpl(directory); }
@Override public void stop() { if (!properties.shouldKeepReport()) { deleteQuietly(reportDir); } }
@Override public ExplodedPlugin explode(PluginInfo pluginInfo) { File tempDir = new File(fs.getTempDir(), TEMP_RELATIVE_PATH); File toDir = new File(tempDir, pluginInfo.getKey()); try { org.sonar.core.util.FileUtils.cleanDirectory(toDir); File jarSource = pluginInfo.getNonNullJarFile(); File jarTarget = new File(toDir, jarSource.getName()); FileUtils.copyFile(jarSource, jarTarget); ZipUtils.unzip(jarSource, toDir, newLibFilter()); return explodeFromUnzippedDir(pluginInfo.getKey(), jarTarget, toDir); } catch (Exception e) { throw new IllegalStateException(String.format( "Fail to unzip plugin [%s] %s to %s", pluginInfo.getKey(), pluginInfo.getNonNullJarFile().getAbsolutePath(), toDir.getAbsolutePath()), e); } } }
@Test public void deleteDirectory_throws_NPE_if_argument_is_null() throws IOException { expectDirectoryCanNotBeNullNPE(); FileUtils.deleteDirectory((File) null); }
/** * Deletes a directory recursively. * * @param directory directory to delete * @throws IOException in case deletion is unsuccessful */ public static void deleteDirectory(File directory) throws IOException { requireNonNull(directory, DIRECTORY_CAN_NOT_BE_NULL); deleteDirectoryImpl(directory.toPath()); }
@Override public void stop() { if (persistit != null) { try { persistit.close(false); persistit = null; } catch (PersistitException e) { throw new IllegalStateException("Fail to close caches", e); } } deleteQuietly(tempDir); tempDir = null; }
@Test public void cleanDirectory_does_nothing_if_argument_does_not_exist() throws IOException { FileUtils.cleanDirectory(new File("/a/b/ToDoSSS")); }
@Test public void deleteDirectory_throws_IOE_if_file_is_symbolicLink() throws IOException { assumeTrue(SystemUtils.IS_OS_UNIX); Path folder = temporaryFolder.newFolder().toPath(); Path file1 = Files.createFile(folder.resolve("file1.txt")); Path symLink = Files.createSymbolicLink(folder.resolve("link1"), file1); assertThat(file1).isRegularFile(); assertThat(symLink).isSymbolicLink(); expectedException.expect(IOException.class); expectedException.expectMessage("Directory '" + symLink.toFile().getAbsolutePath() + "' is a symbolic link"); FileUtils.deleteDirectory(symLink.toFile()); }