/** * 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()); }
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()); } } } }
@Override public void stop() { if (!properties.shouldKeepReport()) { deleteQuietly(reportDir); } }
@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; }
/** * Deletes the temporary files remaining from previous downloads */ @Override public void start() { try { forceMkdir(downloadDir); for (File tempFile : listTempFile(this.downloadDir)) { deleteQuietly(tempFile); } } catch (IOException e) { throw new IllegalStateException("Fail to create the directory: " + downloadDir, e); } }
private static void deleteAllRecursivelyExceptLockFile(Path dirToDelete) { try (DirectoryStream<Path> stream = list(dirToDelete)) { Iterator<Path> it = stream.iterator(); while (it.hasNext()) { FileUtils.deleteQuietly(it.next().toFile()); } } catch (IOException e) { throw new IllegalStateException("Failed to clean working directory: " + dirToDelete.toString(), e); } }
@Test public void deleteQuietly_does_not_fail_if_argument_is_null() { FileUtils.deleteQuietly((File) null); FileUtils.deleteQuietly((Path) null); }
@Override public void download(URI uri, File toFile) { try { copyInputStreamToFile(downloader.newInputSupplier(uri, this.connectTimeout, this.readTimeout).getInput(), toFile); } catch (IOException e) { deleteQuietly(toFile); throw failToDownload(uri, e); } }
private File unzipFile(File cachedFile) throws IOException { String filename = cachedFile.getName(); File destDir = new File(cachedFile.getParentFile(), filename + "_unzip"); File lockFile = new File(cachedFile.getParentFile(), filename + "_unzip.lock"); if (!destDir.exists()) { FileOutputStream out = new FileOutputStream(lockFile); try { java.nio.channels.FileLock lock = out.getChannel().lock(); try { // Recheck in case of concurrent processes if (!destDir.exists()) { File tempDir = pluginFiles.createTempDir(); ZipUtils.unzip(cachedFile, tempDir, newLibFilter()); FileUtils.moveDirectory(tempDir, destDir); } } finally { lock.release(); } } finally { out.close(); deleteQuietly(lockFile); } } return destDir; } }
private void downloadRelease(Release release) throws URISyntaxException, IOException { String url = release.getDownloadUrl(); URI uri = new URI(url); if (url.startsWith("file:")) { // used for tests File file = toFile(uri.toURL()); copyFileToDirectory(file, downloadDir); } else { String filename = substringAfterLast(uri.getPath(), "/"); if (!filename.endsWith("." + PLUGIN_EXTENSION)) { filename = release.getKey() + "-" + release.getVersion() + "." + PLUGIN_EXTENSION; } File targetFile = new File(downloadDir, filename); File tempFile = new File(downloadDir, filename + "." + TMP_SUFFIX); downloader.download(uri, tempFile); copyFile(tempFile, targetFile); deleteQuietly(tempFile); } }
private void registerPluginInfo(PluginInfo info) { String pluginKey = info.getKey(); if (blacklistedPluginKeys.contains(pluginKey)) { LOG.warn("Plugin {} [{}] is blacklisted and is being uninstalled", info.getName(), pluginKey); deleteQuietly(info.getNonNullJarFile()); return; } if (FORBIDDEN_COMPATIBLE_PLUGINS.contains(pluginKey)) { throw MessageException.of(String.format("Plugin '%s' is no longer compatible with this version of SonarQube", pluginKey)); } PluginInfo existing = pluginInfosByKeys.put(pluginKey, info); if (existing != null) { throw MessageException.of(format("Found two versions of the plugin %s [%s] in the directory extensions/plugins. Please remove one of %s or %s.", info.getName(), pluginKey, info.getNonNullJarFile().getName(), existing.getNonNullJarFile().getName())); } }
/** * Move or copy plugin to directory extensions/plugins. If a version of this plugin * already exists then it's deleted. */ private void overrideAndRegisterPlugin(File sourceFile) { File destDir = fs.getInstalledPluginsDir(); File destFile = new File(destDir, sourceFile.getName()); if (destFile.exists()) { // plugin with same filename already installed deleteQuietly(destFile); } try { moveFile(sourceFile, destFile); } catch (IOException e) { throw new IllegalStateException(format("Fail to move plugin: %s to %s", sourceFile.getAbsolutePath(), destFile.getAbsolutePath()), e); } PluginInfo info = PluginInfo.create(destFile); PluginInfo existing = pluginInfosByKeys.put(info.getKey(), info); if (existing != null) { if (!existing.getNonNullJarFile().getName().equals(destFile.getName())) { deleteQuietly(existing.getNonNullJarFile()); } LOG.info("Plugin {} [{}] updated to version {}", info.getName(), info.getKey(), info.getVersion()); } else { LOG.info("Plugin {} [{}] installed", info.getName(), info.getKey()); } }
public QProfileDto copyToName(DbSession dbSession, QProfileDto sourceProfile, String toName) { OrganizationDto organization = db.organizationDao().selectByUuid(dbSession, sourceProfile.getOrganizationUuid()) .orElseThrow(() -> new IllegalStateException("Organization with UUID [" + sourceProfile.getOrganizationUuid() + "] does not exist")); QProfileDto to = prepareTarget(dbSession, organization, sourceProfile, toName); File backupFile = temp.newFile(); try { backup(dbSession, sourceProfile, backupFile); restore(dbSession, backupFile, to); return to; } finally { org.sonar.core.util.FileUtils.deleteQuietly(backupFile); } }
@Test public void deleteQuietly_does_not_fail_if_file_does_not_exist() throws IOException { File file = new File(temporaryFolder.newFolder(), "blablabl"); assertThat(file).doesNotExist(); FileUtils.deleteQuietly(file); }
@Test public void deleteQuietly_deletes_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(); FileUtils.deleteQuietly(symLink.toFile()); assertThat(symLink).doesNotExist(); assertThat(file1).isRegularFile(); }
@Test public void deleteQuietly_deletes_directory_and_content() throws IOException { Path target = temporaryFolder.newFolder().toPath(); Path childFile1 = Files.createFile(target.resolve("file1.txt")); Path childDir1 = Files.createDirectory(target.resolve("subDir1")); Path childFile2 = Files.createFile(childDir1.resolve("file2.txt")); Path childDir2 = Files.createDirectory(childDir1.resolve("subDir2")); assertThat(target).isDirectory(); assertThat(childFile1).isRegularFile(); assertThat(childDir1).isDirectory(); assertThat(childFile2).isRegularFile(); assertThat(childDir2).isDirectory(); FileUtils.deleteQuietly(target.toFile()); assertThat(target).doesNotExist(); assertThat(childFile1).doesNotExist(); assertThat(childDir1).doesNotExist(); assertThat(childFile2).doesNotExist(); assertThat(childDir2).doesNotExist(); }
@Test public void deleteDirectory_deletes_directory_and_content() throws IOException { Path target = temporaryFolder.newFolder().toPath(); Path childFile1 = Files.createFile(target.resolve("file1.txt")); Path childDir1 = Files.createDirectory(target.resolve("subDir1")); Path childFile2 = Files.createFile(childDir1.resolve("file2.txt")); Path childDir2 = Files.createDirectory(childDir1.resolve("subDir2")); assertThat(target).isDirectory(); assertThat(childFile1).isRegularFile(); assertThat(childDir1).isDirectory(); assertThat(childFile2).isRegularFile(); assertThat(childDir2).isDirectory(); FileUtils.deleteQuietly(target.toFile()); assertThat(target).doesNotExist(); assertThat(childFile1).doesNotExist(); assertThat(childDir1).doesNotExist(); assertThat(childFile2).doesNotExist(); assertThat(childDir2).doesNotExist(); }
void terminate() { if (tomcat.getServer().getState().isAvailable()) { try { tomcat.stop(); tomcat.destroy(); } catch (Exception e) { Loggers.get(EmbeddedTomcat.class).error("Fail to stop web server", e); } } deleteQuietly(tomcatBasedir()); }
private static void cleanTempFolders(Path path) throws IOException { if (Files.exists(path)) { try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, new CleanFilter())) { for (Path p : stream) { deleteQuietly(p.toFile()); } } } }
@Override public void delete() { cache.clear(); deleteQuietly(getStatusFilePath().toFile()); }