Browse Source

`deletePlugin(id)` which is called by `UpdateManager.uninstallPlugin(id)` failed since the `PluginRepository.deletePluginPath()` call used `Files.deleteIfExists(path)` which is not able to delete recursively. (#135)

pull/136/head
Jan Høydahl 8 years ago committed by Decebal Suiu
parent
commit
a77245a0da
  1. 4
      pf4j/src/main/java/ro/fortsoft/pf4j/AbstractPluginManager.java
  2. 9
      pf4j/src/main/java/ro/fortsoft/pf4j/BasePluginRepository.java
  3. 2
      pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java
  4. 37
      pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java
  5. 6
      pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginRepositoryTest.java

4
pf4j/src/main/java/ro/fortsoft/pf4j/AbstractPluginManager.java

@ -304,9 +304,7 @@ public abstract class AbstractPluginManager implements PluginManager {
Path pluginPath = pluginWrapper.getPluginPath(); Path pluginPath = pluginWrapper.getPluginPath();
pluginRepository.deletePluginPath(pluginPath); return pluginRepository.deletePluginPath(pluginPath);
return true;
} }
/** /**

9
pf4j/src/main/java/ro/fortsoft/pf4j/BasePluginRepository.java

@ -15,10 +15,12 @@
*/ */
package ro.fortsoft.pf4j; package ro.fortsoft.pf4j;
import ro.fortsoft.pf4j.util.FileUtils;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.NoSuchFileException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -65,7 +67,10 @@ public class BasePluginRepository implements PluginRepository {
@Override @Override
public boolean deletePluginPath(Path pluginPath) { public boolean deletePluginPath(Path pluginPath) {
try { try {
return Files.deleteIfExists(pluginPath); FileUtils.delete(pluginPath);
return true;
} catch (NoSuchFileException nsf) {
return false; // Return false on not found to be compatible with previous API
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

2
pf4j/src/main/java/ro/fortsoft/pf4j/DefaultPluginRepository.java

@ -103,7 +103,7 @@ public class DefaultPluginRepository extends BasePluginRepository {
// do not overwrite an old version, remove it // do not overwrite an old version, remove it
if (pluginDirectory.exists()) { if (pluginDirectory.exists()) {
FileUtils.delete(pluginDirectory); FileUtils.delete(pluginDirectory.toPath());
} }
// create root for plugin // create root for plugin

37
pf4j/src/main/java/ro/fortsoft/pf4j/util/FileUtils.java

@ -16,8 +16,11 @@
package ro.fortsoft.pf4j.util; package ro.fortsoft.pf4j.util;
import java.io.*; import java.io.*;
import java.nio.file.FileVisitResult;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -68,28 +71,27 @@ public class FileUtils {
} }
/** /**
* Delete a file or recursively delete a folder. * Delete a file or recursively delete a folder, do not follow symlinks.
* *
* @param fileOrFolder * @param path the file or folder to delete
* @return true, if successful * @throws IOException if something goes wrong
*/ */
public static boolean delete(File fileOrFolder) { public static void delete(Path path) throws IOException {
boolean success = false; Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
if (fileOrFolder.isDirectory()) { @Override
File [] files = fileOrFolder.listFiles(); public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
if (files != null) { if (!attrs.isSymbolicLink()) {
for (File file : files) { Files.delete(path);
if (file.isDirectory()) {
success |= delete(file);
} else {
success |= file.delete();
} }
return FileVisitResult.CONTINUE;
} }
}
}
success |= fileOrFolder.delete();
return success; @Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
} }
public static List<File> getJars(Path folder) { public static List<File> getJars(Path folder) {
@ -116,5 +118,4 @@ public class FileUtils {
} }
} }
} }
} }

6
pf4j/src/test/java/ro/fortsoft/pf4j/DefaultPluginRepositoryTest.java

@ -21,7 +21,9 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -40,6 +42,8 @@ public class DefaultPluginRepositoryTest {
@Before @Before
public void setUp() throws IOException { public void setUp() throws IOException {
testFolder.newFolder("plugin-1"); 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"));
testFolder.newFolder("plugin-2"); testFolder.newFolder("plugin-2");
testFolder.newFolder("plugin-3"); testFolder.newFolder("plugin-3");
} }
@ -81,7 +85,7 @@ public class DefaultPluginRepositoryTest {
} }
private void assertPathExists(List<Path> paths, Path path) { private void assertPathExists(List<Path> paths, Path path) {
assertTrue("The directory must contains the file " + path, paths.contains(path)); assertTrue("The directory must contain the file " + path, paths.contains(path));
} }
private Path getPluginsRoot() { private Path getPluginsRoot() {

Loading…
Cancel
Save