Skip to content

Commit

Permalink
wallets: Remove dependency to Bisq 2 FileUtils Class
Browse files Browse the repository at this point in the history
We can't depend on Bisq 2's FileUtils class to be able to reuse the
wallets library in Bisq 1.
  • Loading branch information
alvasw committed Sep 11, 2024
1 parent ad23554 commit 115adc1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends BisqProcess> implements BisqProcess {
Expand All @@ -32,7 +35,7 @@ public abstract class AbstractRegtestSetup<T extends BisqProcess> implements Bis
protected final Path tmpDirPath;

public AbstractRegtestSetup() throws IOException {
this.tmpDirPath = FileUtils.createTempDir();
this.tmpDirPath = createTempDir();
}

protected abstract T createProcess();
Expand All @@ -49,4 +52,40 @@ public void shutdown() {
public abstract List<String> 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);
}
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 115adc1

Please sign in to comment.