From: Niki Date: Mon, 15 Apr 2019 08:03:15 +0000 (+0200) Subject: Fix TemporaryFiles.close() is not indempotent X-Git-Tag: nikiroo-utils-4.7.1~2 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=5cc94ce00995b03b98ce369c9f1787c098ccc60c;p=nikiroo-utils.git Fix TemporaryFiles.close() is not indempotent --- diff --git a/src/be/nikiroo/utils/TempFiles.java b/src/be/nikiroo/utils/TempFiles.java index 865fbce..b54f0bc 100644 --- a/src/be/nikiroo/utils/TempFiles.java +++ b/src/be/nikiroo/utils/TempFiles.java @@ -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