From f4ddddc61b8643bf4e34ce65b4de9bf2d2bd6831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Mon, 3 Apr 2017 15:25:07 +0200 Subject: [PATCH] Delete plugin zip on uninstall (#136) --- .../pf4j/DefaultPluginRepository.java | 2 +- .../java/ro/fortsoft/pf4j/util/FileUtils.java | 29 +++++++++++++++++++ .../pf4j/DefaultPluginRepositoryTest.java | 4 +++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java index 953bc4e..f662bc4 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java @@ -68,7 +68,7 @@ public class DefaultPluginRepository extends BasePluginRepository { @Override public boolean deletePluginPath(Path pluginPath) { - // TODO remove plugin zip ? + FileUtils.optimisticDelete(FileUtils.findWithEnding(pluginPath, ".zip", ".ZIP", ".Zip")); return super.deletePluginPath(pluginPath); } diff --git a/pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java b/pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java index 24f67fc..70b4e01 100644 --- a/pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java +++ b/pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java @@ -118,4 +118,33 @@ public class FileUtils { } } } + + /** + * Finds a path with various endings or null if not found + * @param basePath the base name + * @param endings a list of endings to search for + * @return new path or null if not found + */ + public static Path findWithEnding(Path basePath, String... endings) { + for (String ending : endings) { + Path newPath = basePath.resolveSibling(basePath.getFileName() + ending); + if (Files.exists(newPath)) { + return newPath; + } + } + return null; + } + + /** + * Delete a file (not recursively) and ignore any errors + * @param path the path to delete + */ + public static void optimisticDelete(Path path) { + if (path == null) { + return; + } + try { + Files.delete(path); + } catch (IOException ignored) { } + } } diff --git a/pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginRepositoryTest.java b/pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginRepositoryTest.java index 0e85bdc..95c153b 100644 --- a/pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginRepositoryTest.java +++ b/pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginRepositoryTest.java @@ -44,6 +44,8 @@ public class DefaultPluginRepositoryTest { testFolder.newFolder("plugin-1"); // Prove that we can delete a folder with a file inside Files.createFile(Paths.get(testFolder.getRoot().getAbsolutePath()).resolve("plugin-1").resolve("myfile")); + // Create a zip file for plugin-1 to test that it is deleted when plugin is deleted + Files.createFile(Paths.get(testFolder.getRoot().getAbsolutePath()).resolve("plugin-1.zip")); testFolder.newFolder("plugin-2"); testFolder.newFolder("plugin-3"); } @@ -74,7 +76,9 @@ public class DefaultPluginRepositoryTest { PluginRepository instance = new DefaultPluginRepository(pluginsRoot, false); + assertTrue(Files.exists(pluginsRoot.resolve("plugin-1.zip"))); assertTrue(instance.deletePluginPath(pluginsRoot.resolve("plugin-1"))); + assertFalse(Files.exists(pluginsRoot.resolve("plugin-1.zip"))); assertTrue(instance.deletePluginPath(pluginsRoot.resolve("plugin-3"))); assertFalse(instance.deletePluginPath(pluginsRoot.resolve("plugin-4")));