Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / utils / TempFiles.java
index b4ac6d2aead595b9e2a9421b6be6ebd60a117b4e..b54f0bc3e7ae5642cb9e3c8a135ac656eed4dee9 100644 (file)
@@ -63,7 +63,9 @@ public class TempFiles implements Closeable {
 
                root = base;
 
-               IOUtils.deltree(root, true);
+               if (root.exists()) {
+                       IOUtils.deltree(root, true);
+               }
 
                root = new File(root.getParentFile(), ".temp");
                root.mkdir();
@@ -108,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();
@@ -162,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