]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Worked around Windows FS problems in IndexedRelationsSearcherBase 23/1823/1 release/1.32.0.1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 8 Jun 2018 22:24:40 +0000 (01:24 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 8 Jun 2018 22:24:40 +0000 (01:24 +0300)
See https://blogs.msdn.microsoft.com/oldnewthing/20120907-00/?p=6663/

gitlab #19

Change-Id: I0bdcdc4945bde80446da20f18f7e471af0a97c07

bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/DatabaseIndexing.java
bundles/org.simantics.db.indexing/src/org/simantics/db/indexing/IndexedRelationsSearcherBase.java
bundles/org.simantics.utils/src/org/simantics/utils/FileUtils.java

index abd6e1ba54ef1203f7508ee285b8e26a4ed442fd..39ff02199fa5ec25ae0631932e6a6c43429f92d4 100644 (file)
@@ -14,6 +14,8 @@ package org.simantics.db.indexing;
 import java.io.File;
 import java.io.FileFilter;
 import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.ArrayList;
 
 import org.simantics.db.Resource;
@@ -194,8 +196,9 @@ public final class DatabaseIndexing {
             // Make sure that index-base is a valid directory
             if (DEBUG)
                 System.out.println(indexBase + " is not a directory! Removing it.");
-            FileUtils.deleteAll(indexBase);
-            indexBase.mkdirs();
+            Path base = indexBase.toPath();
+            FileUtils.emptyDirectory(base);
+            Files.createDirectories(base);
             return;
         }
         File allDirtyFile = getAllDirtyFile();
index 056a05e8ae06b1d7d4558207a05c8b135017ab11..387b2b509bc930ef15965726cc053e20c786a8ca 100644 (file)
@@ -612,7 +612,7 @@ abstract public class IndexedRelationsSearcherBase {
 
         if (overwrite) {
             mon.subTask("Erasing previous index");
-            FileUtils.deleteAll(indexPath);
+            FileUtils.emptyDirectory(indexPath.toPath());
         }
 
         final AtomicReference<FSDirectory> directory = new AtomicReference<FSDirectory>();
index 798fd02b8742c302958c495759fc2ef7950c5b0c..71fb9ac2c1fab6278405e7ac31bbf2861aaaea1e 100644 (file)
@@ -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<Path> {
         
         @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<Path> {
+        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<Path> {
         
         private final Path fromPath;