From 115adc172269e823b562e102c283d31961b9e914 Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Wed, 11 Sep 2024 20:03:21 +0000 Subject: [PATCH] wallets: Remove dependency to Bisq 2 FileUtils Class We can't depend on Bisq 2's FileUtils class to be able to reuse the wallets library in Bisq 1. --- .../wallets/regtest/AbstractRegtestSetup.java | 43 ++++++++++++++++++- .../regtest/process/DaemonProcess.java | 10 ++++- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/wallets/bitcoind/regtest/src/main/java/bisq/wallets/regtest/AbstractRegtestSetup.java b/wallets/bitcoind/regtest/src/main/java/bisq/wallets/regtest/AbstractRegtestSetup.java index 1bd0e42fa6..4ae0250b2e 100644 --- a/wallets/bitcoind/regtest/src/main/java/bisq/wallets/regtest/AbstractRegtestSetup.java +++ b/wallets/bitcoind/regtest/src/main/java/bisq/wallets/regtest/AbstractRegtestSetup.java @@ -17,12 +17,15 @@ package bisq.wallets.regtest; -import bisq.common.file.FileUtils; import bisq.wallets.json_rpc.RpcConfig; import bisq.wallets.regtest.process.BisqProcess; import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.List; public abstract class AbstractRegtestSetup implements BisqProcess { @@ -32,7 +35,7 @@ public abstract class AbstractRegtestSetup implements Bis protected final Path tmpDirPath; public AbstractRegtestSetup() throws IOException { - this.tmpDirPath = FileUtils.createTempDir(); + this.tmpDirPath = createTempDir(); } protected abstract T createProcess(); @@ -49,4 +52,40 @@ public void shutdown() { public abstract List mineOneBlock() throws InterruptedException; public abstract RpcConfig getRpcConfig(); + + public static Path createTempDir() throws IOException { + Path tempDirPath = Files.createTempDirectory(null); + recursiveDeleteOnShutdownHook(tempDirPath); + return tempDirPath; + } + + public static void recursiveDeleteOnShutdownHook(Path path) { + Runtime.getRuntime().addShutdownHook(new Thread( + () -> { + try { + Files.walkFileTree(path, new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path file, + @SuppressWarnings("unused") BasicFileAttributes attrs) + throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException e) + throws IOException { + if (e == null) { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } + // directory iteration failed + throw e; + } + }); + } catch (IOException e) { + throw new RuntimeException("Failed to delete " + path, e); + } + })); + } } diff --git a/wallets/bitcoind/regtest/src/main/java/bisq/wallets/regtest/process/DaemonProcess.java b/wallets/bitcoind/regtest/src/main/java/bisq/wallets/regtest/process/DaemonProcess.java index 79d3dc366f..41e7a043fe 100644 --- a/wallets/bitcoind/regtest/src/main/java/bisq/wallets/regtest/process/DaemonProcess.java +++ b/wallets/bitcoind/regtest/src/main/java/bisq/wallets/regtest/process/DaemonProcess.java @@ -17,12 +17,12 @@ package bisq.wallets.regtest.process; -import bisq.common.file.FileUtils; import bisq.common.file.LogScanner; import bisq.common.threading.ThreadName; import lombok.Getter; import lombok.extern.slf4j.Slf4j; +import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.util.Collections; @@ -48,7 +48,7 @@ public DaemonProcess(Path dataDir) { @Override public void start() { try { - FileUtils.makeDirs(dataDir.toFile()); + makeDirs(dataDir.toFile()); process = createAndStartProcess(); waitUntilReady(); } catch (IOException e) { @@ -120,4 +120,10 @@ protected boolean waitUntilLogContainsLines() { throw new CannotStartProcessException(processName, e); } } + + private static void makeDirs(File dir) throws IOException { + if (!dir.exists() && !dir.mkdirs()) { + throw new IOException("Could not make dir " + dir); + } + } }