Fix TemporaryFiles.close() is not indempotent
authorNiki <david.roulet@uclouvain.be>
Mon, 15 Apr 2019 08:03:15 +0000 (10:03 +0200)
committerNiki <david.roulet@uclouvain.be>
Mon, 15 Apr 2019 08:03:15 +0000 (10:03 +0200)
src/be/nikiroo/utils/TempFiles.java

index 865fbce765c474b30e86362ae36a2343e745eb7e..b54f0bc3e7ae5642cb9e3c8a135ac656eed4dee9 100644 (file)
@@ -110,8 +110,8 @@ public class TempFiles implements Closeable {
                        if (!test.exists()) {
                                test.createNewFile();
                                if (!test.exists()) {
-                                       throw new IOException("Cannot create temporary file: "
-                                                       + test);
+                                       throw new IOException(
+                                                       "Cannot create temporary file: " + test);
                                }
 
                                test.deleteOnExit();
@@ -164,9 +164,16 @@ public class TempFiles implements Closeable {
 
        @Override
        public synchronized void close() throws IOException {
-               IOUtils.deltree(root); // NO exception here
-               root.getParentFile().delete(); // only if empty
-               root = null;
+               File root = this.root;
+               this.root = null;
+
+               if (root != null) {
+                       IOUtils.deltree(root);
+
+                       // Since we allocate temp directories from a base point,
+                       // try and remove that base point
+                       root.getParentFile().delete(); // (only works if empty)
+               }
        }
 
        @Override