From ad8fc537d4cde0d8891cf1cd39862055ca7f03cb Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Sat, 9 Jun 2018 01:35:29 +0300 Subject: [PATCH] Worked around Windows FS problems in IndexedRelationsSearcherBase See https://blogs.msdn.microsoft.com/oldnewthing/20120907-00/?p=6663/ gitlab #19 Change-Id: Ia12c848aa75836597121eadec05061ea23bb1240 --- .../db/indexing/DatabaseIndexing.java | 2 +- .../IndexedRelationsSearcherBase.java | 2 +- .../src/org/simantics/utils/FileUtils.java | 54 ++++++++++++++----- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/DatabaseIndexing.java b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/DatabaseIndexing.java index b429eb49a..bf7293e50 100644 --- a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/DatabaseIndexing.java +++ b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/DatabaseIndexing.java @@ -199,7 +199,7 @@ public final class DatabaseIndexing { // Make sure that index-base is a valid directory if (LOGGER.isDebugEnabled()) LOGGER.debug(indexBase + " is not a directory! Removing it."); - FileUtils.delete(indexBase); + FileUtils.emptyDirectory(indexBase); Files.createDirectories(indexBase); return; } diff --git a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsSearcherBase.java b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsSearcherBase.java index 74d329901..1d5001c64 100644 --- a/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsSearcherBase.java +++ b/bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsSearcherBase.java @@ -624,7 +624,7 @@ abstract public class IndexedRelationsSearcherBase { mon.subTask("Erasing previous index"); if (getLogger().isDebugEnabled()) getLogger().debug("Erasing previous index {}", indexPath.toAbsolutePath()); - FileUtils.delete(indexPath); + FileUtils.emptyDirectory(indexPath); } } 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 6ae209c9a..44a3dfcf5 100644 --- a/bundles/org.simantics.utils/src/org/simantics/utils/FileUtils.java +++ b/bundles/org.simantics.utils/src/org/simantics/utils/FileUtils.java @@ -970,6 +970,20 @@ public class FileUtils { deleteWithFilter(path, null); } + /** + * 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)); } @@ -987,12 +1001,7 @@ public class FileUtils { if (filter != null && !filter.test(file)) { return FileVisitResult.CONTINUE; } - if (Files.exists(file)) { - Files.delete(file); - } - if (Files.exists(file)) { - throw new IOException("Could not delete file " + file.toAbsolutePath().toString()); - } + Files.deleteIfExists(file); return FileVisitResult.CONTINUE; } @@ -1003,15 +1012,36 @@ public class FileUtils { if (filter != null && !filter.test(dir)) { return FileVisitResult.CONTINUE; } - if (Files.exists(dir)) { - 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; -- 2.43.2