From 68429cc581ce4bc5fc37ee8b77db6712aae18550 Mon Sep 17 00:00:00 2001 From: HenrikJannsen Date: Fri, 30 Dec 2022 13:45:29 -0500 Subject: [PATCH] Use Files.delete as it provides more information about the reason in case the delete fails. Rename deleteDirectory to deleteFileOrDirectory as it handles both cases. --- .../main/java/bisq/common/util/FileUtils.java | 29 ++++++++++--------- .../test/java/bisq/TorIntegrationTest.java | 8 ++++- .../test/java/bisq/tor/AbstractTorTest.java | 7 ++++- .../java/bisq/persistence/Persistence.java | 6 +++- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/common/src/main/java/bisq/common/util/FileUtils.java b/common/src/main/java/bisq/common/util/FileUtils.java index 6ba008c918..9c8d664dc2 100644 --- a/common/src/main/java/bisq/common/util/FileUtils.java +++ b/common/src/main/java/bisq/common/util/FileUtils.java @@ -70,33 +70,36 @@ public static void deleteOnExit(File file) { /** * @param file The file to delete and to get removed from the `DeleteOnExitHook`. */ - public static void releaseTempFile(File file) { + public static void releaseTempFile(File file) throws IOException { if (!DeleteOnExitHook.isShutdownInProgress()) { DeleteOnExitHook.remove(file.getPath()); } deleteFile(file); } - public static void deleteDirectory(String dirPath) { - deleteDirectory(new File(dirPath)); + public static void deleteFileOrDirectory(String dirPath) throws IOException { + deleteFileOrDirectory(new File(dirPath)); } - public static void deleteDirectory(File dir) { + public static void deleteFileOrDirectory(Path path) throws IOException { + deleteFileOrDirectory(path.toFile()); + } + + public static void deleteFileOrDirectory(File dir) throws IOException { File[] files = dir.listFiles(); if (files != null) { - for (final File file : files) { - deleteDirectory(file); + for (File file : files) { + deleteFileOrDirectory(file); } } - //noinspection ResultOfMethodCallIgnored - dir.delete(); + if (dir.exists()) { + Files.delete(dir.toPath()); + } } - public static void deleteFile(File file) { - if (file != null && file.exists()) { - if (!file.delete()) { - log.error("Cannot delete file {}", file); - } + public static void deleteFile(File file) throws IOException { + if (file.exists()) { + Files.delete(file.toPath()); } } diff --git a/network/tor/src/test/java/bisq/TorIntegrationTest.java b/network/tor/src/test/java/bisq/TorIntegrationTest.java index 229ba4f51d..f7bf54fa46 100644 --- a/network/tor/src/test/java/bisq/TorIntegrationTest.java +++ b/network/tor/src/test/java/bisq/TorIntegrationTest.java @@ -26,6 +26,7 @@ import org.slf4j.LoggerFactory; import java.io.File; +import java.io.IOException; import java.util.concurrent.Executors; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -39,7 +40,12 @@ public class TorIntegrationTest { public void testShutdownDuringStartup() { String torDirPath = OsUtils.getUserDataDir() + "/TorifyIntegrationTest"; File versionFile = new File(torDirPath + "/" + Constants.VERSION); - FileUtils.deleteDirectory(new File(torDirPath)); + try { + FileUtils.deleteFileOrDirectory(torDirPath); + } catch (IOException e) { + log.error("Could not delete " + torDirPath, e); + throw new RuntimeException(e); + } assertFalse(versionFile.exists()); Tor tor = Tor.getTor(torDirPath); new Thread(() -> { diff --git a/network/tor/src/test/java/bisq/tor/AbstractTorTest.java b/network/tor/src/test/java/bisq/tor/AbstractTorTest.java index a068af43b9..b6eea7d8cb 100644 --- a/network/tor/src/test/java/bisq/tor/AbstractTorTest.java +++ b/network/tor/src/test/java/bisq/tor/AbstractTorTest.java @@ -51,7 +51,12 @@ public static void cleanTorInstallDir(String torDirPathSpec) { File torDir = new File(torDirPathSpec); if (torDir.exists()) { log.info("Cleaning tor install dir {}", torDirPathSpec); - FileUtils.deleteDirectory(torDir); + try { + FileUtils.deleteFileOrDirectory(torDir); + } catch (IOException e) { + log.error("Could not delete " + torDir, e); + throw new RuntimeException(e); + } } File versionFile = new File(torDir, VERSION); assertFalse(versionFile.exists()); diff --git a/persistence/src/main/java/bisq/persistence/Persistence.java b/persistence/src/main/java/bisq/persistence/Persistence.java index 526b07bf98..4341621493 100644 --- a/persistence/src/main/java/bisq/persistence/Persistence.java +++ b/persistence/src/main/java/bisq/persistence/Persistence.java @@ -151,7 +151,11 @@ public boolean persist(T persistableStore) { log.error("Error closing stream " + ioe.getMessage(), ioe); // swallow } if (tempFile != null) { - FileUtils.releaseTempFile(tempFile); + try { + FileUtils.releaseTempFile(tempFile); + } catch (IOException e) { + log.error("Could not delete " + tempFile, e); + } } } return success;