]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils/src/org/simantics/utils/FileUtils.java
Worked around Windows FS problems in IndexedRelationsSearcherBase
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / FileUtils.java
index 4e68f29c4ac95246da7d81d2b9e14c7b323dafbc..44a3dfcf5731b9ff2c9334959a66e3463416f283 100644 (file)
@@ -33,12 +33,14 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.SimpleFileVisitor;
 import java.nio.file.StandardCopyOption;
+import java.nio.file.StandardOpenOption;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Deque;
 import java.util.LinkedList;
 import java.util.Random;
+import java.util.function.Predicate;
 import java.util.zip.DataFormatException;
 import java.util.zip.Deflater;
 import java.util.zip.Inflater;
@@ -826,9 +828,9 @@ public class FileUtils {
        }
     }
 
-    private static void copy(File file, ZipOutputStream zout) throws IOException {
+    public static void copy(File file, OutputStream out) throws IOException {
        try (InputStream in = new FileInputStream(file)) {
-               copy(in, zout);
+               copy(in, out);
        }
     }
 
@@ -840,8 +842,8 @@ public class FileUtils {
      * @throws IOException
      */
     public static void extractZip(File zipFile, File dst) throws IOException {
-        if (LOGGER.isDebugEnabled())
-               LOGGER.debug("Extracting zip "+zipFile);
+        if (LOGGER.isTraceEnabled())
+            LOGGER.trace("Extracting zip "+zipFile);
         try (FileInputStream fis = new FileInputStream(zipFile)) {
             extractZip(fis, dst);
         }
@@ -863,8 +865,8 @@ public class FileUtils {
         while (entry != null) {
             // for each entry to be extracted
             String name = entry.getName();
-            if (LOGGER.isDebugEnabled())
-                LOGGER.debug("Extracting "+name);
+            if (LOGGER.isTraceEnabled())
+                LOGGER.trace("Extracting "+name);
             File file = new File(dst, name);
 
             if (entry.isDirectory())
@@ -959,21 +961,47 @@ public class FileUtils {
         return read;
     }
 
-    public static void delete(Path databaseLocation) throws IOException {
-        Files.walkFileTree(databaseLocation, new DeleteDirectoriesVisitor());
+    public static void deleteWithFilter(Path path, Predicate<Path> filter) throws IOException {
+        if (Files.exists(path))
+            Files.walkFileTree(path, new DeleteDirectoriesVisitor(filter));
     }
-    
+
+    public static void delete(Path path) throws IOException {
+        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));
     }
     
     public static class DeleteDirectoriesVisitor extends SimpleFileVisitor<Path> {
-        
+
+        private Predicate<Path> filter;
+
+        public DeleteDirectoriesVisitor(Predicate<Path> filter) {
+            this.filter = filter;
+        }
+
         @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());
+            if (filter != null && !filter.test(file)) {
+                return FileVisitResult.CONTINUE;
+            }
+            Files.deleteIfExists(file);
             return FileVisitResult.CONTINUE;
         }
         
@@ -981,13 +1009,39 @@ 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());
+            if (filter != null && !filter.test(dir)) {
+                return FileVisitResult.CONTINUE;
+            }
+            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;
@@ -1019,4 +1073,9 @@ public class FileUtils {
                raf.getFD().sync();
                }
     }
+    
+    public static void sync(Path path) throws IOException {
+        try (InputStream stream = Files.newInputStream(path, StandardOpenOption.SYNC)) {
+        }
+    }
 }