From 5cc94ce00995b03b98ce369c9f1787c098ccc60c Mon Sep 17 00:00:00 2001 From: Niki Date: Mon, 15 Apr 2019 10:03:15 +0200 Subject: [PATCH] Fix TemporaryFiles.close() is not indempotent --- src/be/nikiroo/utils/TempFiles.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) 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 -- 2.27.0