X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.utils%2Fsrc%2Forg%2Fsimantics%2Futils%2FFileUtils.java;h=71fb9ac2c1fab6278405e7ac31bbf2861aaaea1e;hb=1e432ddab834037d213a0be5f42b8babfa52c811;hp=798fd02b8742c302958c495759fc2ef7950c5b0c;hpb=19f302bfe2446e8a0b1e4a7dd53219ac0400b75c;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/FileUtils.java b/bundles/org.simantics.utils/src/org/simantics/utils/FileUtils.java index 798fd02b8..71fb9ac2c 100644 --- a/bundles/org.simantics.utils/src/org/simantics/utils/FileUtils.java +++ b/bundles/org.simantics.utils/src/org/simantics/utils/FileUtils.java @@ -959,21 +959,34 @@ public class FileUtils { return read; } - public static void delete(Path databaseLocation) throws IOException { - Files.walkFileTree(databaseLocation, new DeleteDirectoriesVisitor()); + public static void delete(Path path) throws IOException { + if (Files.exists(path)) + Files.walkFileTree(path, new DeleteDirectoriesVisitor()); } - + + /** + * Empties the specified directory but does not delete the directory itself. + * If a non-directory path is given, it is simply deleted. + * + * @param path + * @throws IOException + */ + public static void emptyDirectory(Path path) throws IOException { + if (Files.isDirectory(path)) + Files.walkFileTree(path, new EmptyDirectoryVisitor()); + else + Files.deleteIfExists(path); + } + public static void copy(Path from, Path to) throws IOException { Files.walkFileTree(from, new CopyDirectoriesVisitor(from, to)); } - + public static class DeleteDirectoriesVisitor extends SimpleFileVisitor { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); - if (Files.exists(file)) - throw new IOException("Could not delete file " + file.toAbsolutePath().toString()); + Files.deleteIfExists(file); return FileVisitResult.CONTINUE; } @@ -981,13 +994,36 @@ public class FileUtils { public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { if (exc != null) throw exc; - Files.delete(dir); - if (Files.exists(dir)) - throw new IOException("Could not delete file " + dir.toAbsolutePath().toString()); + Files.deleteIfExists(dir); return FileVisitResult.CONTINUE; } } - + + private static class EmptyDirectoryVisitor extends SimpleFileVisitor { + int depth = 0; + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.deleteIfExists(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + ++depth; + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + if (exc != null) + throw exc; + if (--depth > 0) + Files.deleteIfExists(dir); + return FileVisitResult.CONTINUE; + } + } + public static class CopyDirectoriesVisitor extends SimpleFileVisitor { private final Path fromPath;