void createOrCleanupDir(File dir) throws IOException { FileUtils.forceMkdir(dir); org.sonar.core.util.FileUtils.cleanDirectory(dir); } }
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 cleanDirectory_does_nothing_if_argument_does_not_exist() throws IOException { FileUtils.cleanDirectory(new File("/a/b/ToDoSSS")); }
@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); } } }
/** * JAR files of directory extensions/plugins can be moved when server is up and plugins are uninstalled. * For this reason these files must not be locked by classloaders. They are copied to the directory * web/deploy/plugins in order to be loaded by {@link org.sonar.core.platform.PluginLoader}. */ @Override public ExplodedPlugin explode(PluginInfo pluginInfo) { File toDir = new File(fs.getDeployedPluginsDir(), pluginInfo.getKey()); try { forceMkdir(toDir); 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()); ExplodedPlugin explodedPlugin = explodeFromUnzippedDir(pluginInfo.getKey(), jarTarget, toDir); pluginFileSystem.addInstalledPlugin(pluginInfo, jarTarget); return explodedPlugin; } 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 cleanDirectory_throws_NPE_if_file_is_null() throws IOException { expectDirectoryCanNotBeNullNPE(); FileUtils.cleanDirectory(null); }
@Test public void cleanDirectory_removes_directories_and_files_in_target_directory_but_not_target_directory() 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(); // on supporting FileSystem, target will change if directory is recreated Object targetKey = getFileKey(target); FileUtils.cleanDirectory(target.toFile()); assertThat(target).isDirectory(); assertThat(childFile1).doesNotExist(); assertThat(childDir1).doesNotExist(); assertThat(childFile2).doesNotExist(); assertThat(childDir2).doesNotExist(); assertThat(getFileKey(target)).isEqualTo(targetKey); }
@Test public void cleanDirectory_throws_IAE_if_argument_is_a_file() throws IOException { File file = temporaryFolder.newFile(); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("'" + file.getAbsolutePath() + "' is not a directory"); FileUtils.cleanDirectory(file); }
@Test public void cleanDirectory_follows_symlink_to_target_directory() throws IOException { assumeTrue(SystemUtils.IS_OS_UNIX); Path target = temporaryFolder.newFolder().toPath(); Path symToDir = Files.createSymbolicLink(temporaryFolder.newFolder().toPath().resolve("sym_to_dir"), target); 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(symToDir).isSymbolicLink(); assertThat(childFile1).isRegularFile(); assertThat(childDir1).isDirectory(); assertThat(childFile2).isRegularFile(); assertThat(childDir2).isDirectory(); // on supporting FileSystem, target will change if directory is recreated Object targetKey = getFileKey(target); Object symLinkKey = getFileKey(symToDir); FileUtils.cleanDirectory(symToDir.toFile()); assertThat(target).isDirectory(); assertThat(symToDir).isSymbolicLink(); assertThat(childFile1).doesNotExist(); assertThat(childDir1).doesNotExist(); assertThat(childFile2).doesNotExist(); assertThat(childDir2).doesNotExist(); assertThat(getFileKey(target)).isEqualTo(targetKey); assertThat(getFileKey(symToDir)).isEqualTo(symLinkKey); }
void createOrCleanupDir(File dir) throws IOException { FileUtils.forceMkdir(dir); org.sonar.core.util.FileUtils.cleanDirectory(dir); } }
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); } }
private static void cleanDirectory(File dir) { try { FileUtils.cleanDirectory(dir); } catch (IOException e) { throw new IllegalStateException("Failed to recreate working directory: " + dir.getAbsolutePath(), e); } } }
@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); } } }
/** * JAR files of directory extensions/plugins can be moved when server is up and plugins are uninstalled. * For this reason these files must not be locked by classloaders. They are copied to the directory * web/deploy/plugins in order to be loaded by {@link org.sonar.core.platform.PluginLoader}. */ @Override public ExplodedPlugin explode(PluginInfo pluginInfo) { File toDir = new File(fs.getDeployedPluginsDir(), pluginInfo.getKey()); try { forceMkdir(toDir); 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()); ExplodedPlugin explodedPlugin = explodeFromUnzippedDir(pluginInfo.getKey(), jarTarget, toDir); pluginFileSystem.addInstalledPlugin(pluginInfo, jarTarget); return explodedPlugin; } catch (Exception e) { throw new IllegalStateException(String.format( "Fail to unzip plugin [%s] %s to %s", pluginInfo.getKey(), pluginInfo.getNonNullJarFile().getAbsolutePath(), toDir.getAbsolutePath()), e); } } }