public void cleanDir() throws IOException { File destDirFile = new File(destDir); FileUtils.forceMkdir(destDirFile); FileUtils.cleanDirectory(destDirFile); }
public static void cleanRocksDbLocalDir(String rocksDbDir) throws IOException { File file = new File(rocksDbDir); if (file.exists()) { FileUtils.cleanDirectory(file); } else { FileUtils.forceMkdir(file); } } }
/** * Deletes a directory recursively. * * @param directory directory to delete * @throws IOException in case deletion is unsuccessful * @throws IllegalArgumentException if {@code directory} does not exist or is not a directory */ public static void deleteDirectory(final File directory) throws IOException { if (!directory.exists()) { return; } if (!isSymlink(directory)) { cleanDirectory(directory); } if (!directory.delete()) { final String message = "Unable to delete directory " + directory + "."; throw new IOException(message); } }
default void cleanDataDir(String path) { File dataDir = new File(path); try { logger.info("Deleting contents of data dir {}", path); if (dataDir.exists()) { FileUtils.cleanDirectory(dataDir); } } catch (IOException e) { logger.error(String.format("Failed to delete ES data dir: %s", dataDir.getAbsolutePath()), e); } }
private void createWorkingDirectoryIfNotExist(File buildWorkingDirectory) { if (assignment.shouldCleanWorkingDir() && buildWorkingDirectory.exists()) { try { FileUtils.cleanDirectory(buildWorkingDirectory); goPublisher.consumeLineWithPrefix("Cleaning working directory \"" + buildWorkingDirectory.getAbsolutePath() + "\" since stage is configured to clean working directory"); } catch (IOException e) { bomb("Clean working directory is set to true. Unable to clean working directory for agent: " + buildWorkingDirectory.getAbsolutePath() + ", with error: " + e.getMessage()); } } if (!buildWorkingDirectory.exists()) { if (!buildWorkingDirectory.mkdirs()) { bomb("Unable to create working directory for agent: " + buildWorkingDirectory.getAbsolutePath()); } } } }
@After public void tearDown() throws IOException { File inDir = new File("/tmp/in"); if (inDir.exists()) { FileUtils.cleanDirectory(inDir); } File outDir = new File("/tmp/out"); if (outDir.exists()) { FileUtils.cleanDirectory(outDir); } }
@Test public void testCleanEmpty() throws Exception { assertEquals(0, top.list().length); FileUtils.cleanDirectory(top); assertEquals(0, top.list().length); }
private void emptyTestDirectory() throws IOException { //Delete Virtual File System folder Path dir = Paths.get(sshTestServer.getVirtualFileSystemPath()); FileUtils.cleanDirectory(dir.toFile()); } }
@Override public void initialize() { cleanupOldPluginDirectories(); try { File bundledPluginsDirectory = new File(systemEnvironment.get(PLUGIN_GO_PROVIDED_PATH)); if (shouldReplaceBundledPlugins(bundledPluginsDirectory)) { FileUtils.cleanDirectory(bundledPluginsDirectory); zipUtil.unzip(getPluginsZipStream(), bundledPluginsDirectory); } pluginManager.startInfrastructure(true); } catch (Exception e) { LOG.error("Could not extract bundled plugins to default bundled directory", e); } }
@Test public void testDeletesNested() throws Exception { final File nested = new File(top, "nested"); assertTrue(nested.mkdirs()); FileUtils.touch(new File(nested, "file")); assertEquals(1, top.list().length); FileUtils.cleanDirectory(top); assertEquals(0, top.list().length); }
@Test public void testThrowsOnNullList() throws Exception { if (System.getProperty("os.name").startsWith("Win") || !chmod(top, 0, false)) { // test wont work if we can't restrict permissions on the // directory, so skip it. return; } try { FileUtils.cleanDirectory(top); fail("expected IOException"); } catch (final IOException e) { assertEquals("Failed to list contents of " + top.getAbsolutePath(), e.getMessage()); } }
@Before public void setup() throws Exception { this.createTestMetadata(); FileUtils.forceMkdir(new File(dstPath)); FileUtils.cleanDirectory(new File(dstPath)); }
@Test public void testDeletesRegular() throws Exception { FileUtils.touch(new File(top, "regular")); FileUtils.touch(new File(top, ".hidden")); assertEquals(2, top.list().length); FileUtils.cleanDirectory(top); assertEquals(0, top.list().length); }
@Test public void testStillClearsIfGivenDirectoryIsASymlink() throws Exception { if (System.getProperty("os.name").startsWith("Win")) { // cant create symlinks in windows. return; } final File randomDirectory = new File(top, "randomDir"); assertTrue(randomDirectory.mkdirs()); FileUtils.touch(new File(randomDirectory, "randomfile")); assertEquals(1, randomDirectory.list().length); final File symlinkDirectory = new File(top, "fakeDir"); setupSymlink(randomDirectory, symlinkDirectory); FileUtils.cleanDirectory(symlinkDirectory); assertEquals(0, symlinkDirectory.list().length); assertEquals(0, randomDirectory.list().length); }
@Test public void testThrowsOnCannotDeleteFile() throws Exception { final File file = new File(top, "restricted"); FileUtils.touch(file); if (System.getProperty("os.name").startsWith("Win") || !chmod(top, 500, false)) { // test wont work if we can't restrict permissions on the // directory, so skip it. return; } try { FileUtils.cleanDirectory(top); fail("expected IOException"); } catch (final IOException e) { assertEquals("Unable to delete file: " + file.getAbsolutePath(), e.getMessage()); } }
@Test public void testCleanDirWithASymlinkDir() throws Exception { if (System.getProperty("os.name").startsWith("Win")) { // cant create symlinks in windows. return; } final File realOuter = new File(top, "realouter"); assertTrue(realOuter.mkdirs()); final File realInner = new File(realOuter, "realinner"); assertTrue(realInner.mkdirs()); FileUtils.touch(new File(realInner, "file1")); assertEquals(1, realInner.list().length); final File randomDirectory = new File(top, "randomDir"); assertTrue(randomDirectory.mkdirs()); FileUtils.touch(new File(randomDirectory, "randomfile")); assertEquals(1, randomDirectory.list().length); final File symlinkDirectory = new File(realOuter, "fakeinner"); setupSymlink(randomDirectory, symlinkDirectory); assertEquals(1, symlinkDirectory.list().length); // assert contents of the real directory were removed including the symlink FileUtils.cleanDirectory(realOuter); assertEquals(0, realOuter.list().length); // ensure that the contents of the symlink were NOT removed. assertEquals("Contents of sym link should not have been removed", 1, randomDirectory.list().length); }
@Test public void testCleanDirWithSymlinkFile() throws Exception { if (System.getProperty("os.name").startsWith("Win")) { // cant create symlinks in windows. return; } final File realOuter = new File(top, "realouter"); assertTrue(realOuter.mkdirs()); final File realInner = new File(realOuter, "realinner"); assertTrue(realInner.mkdirs()); final File realFile = new File(realInner, "file1"); FileUtils.touch(realFile); assertEquals(1, realInner.list().length); final File randomFile = new File(top, "randomfile"); FileUtils.touch(randomFile); final File symlinkFile = new File(realInner, "fakeinner"); setupSymlink(randomFile, symlinkFile); assertEquals(2, realInner.list().length); // assert contents of the real directory were removed including the symlink FileUtils.cleanDirectory(realOuter); assertEquals(0, realOuter.list().length); // ensure that the contents of the symlink were NOT removed. assertTrue(randomFile.exists()); assertFalse(symlinkFile.exists()); }
@Test public void testCleanDirWithParentSymlinks() throws Exception { if (System.getProperty("os.name").startsWith("Win")) { // cant create symlinks in windows. return; } final File realParent = new File(top, "realparent"); assertTrue(realParent.mkdirs()); final File realInner = new File(realParent, "realinner"); assertTrue(realInner.mkdirs()); FileUtils.touch(new File(realInner, "file1")); assertEquals(1, realInner.list().length); final File randomDirectory = new File(top, "randomDir"); assertTrue(randomDirectory.mkdirs()); FileUtils.touch(new File(randomDirectory, "randomfile")); assertEquals(1, randomDirectory.list().length); final File symlinkDirectory = new File(realParent, "fakeinner"); setupSymlink(randomDirectory, symlinkDirectory); assertEquals(1, symlinkDirectory.list().length); final File symlinkParentDirectory = new File(top, "fakeouter"); setupSymlink(realParent, symlinkParentDirectory); // assert contents of the real directory were removed including the symlink FileUtils.cleanDirectory(symlinkParentDirectory);// should clean the contents of this but not recurse into other links assertEquals(0, symlinkParentDirectory.list().length); assertEquals(0, realParent.list().length); // ensure that the contents of the symlink were NOT removed. assertEquals("Contents of sym link should not have been removed", 1, randomDirectory.list().length); }
@Override public boolean execute(BuildCommand command, BuildSession buildSession) { File dir = buildSession.resolveRelativeDir(command.getWorkingDirectory(), command.getStringArg("path")); String[] allowed = command.getArrayArg("allowed"); if (allowed.length == 0) { try { FileUtils.cleanDirectory(dir); } catch (IOException e) { return false; } } else { DirectoryCleaner cleaner = new DirectoryCleaner(dir, buildSession.processOutputStreamConsumer()); cleaner.allowed(allowed); cleaner.clean(); } return true; } }