Skip to content

Commit

Permalink
Use Files.delete as it provides more information about the reason in …
Browse files Browse the repository at this point in the history
…case the delete fails.

Rename deleteDirectory to deleteFileOrDirectory as it handles both cases.
  • Loading branch information
HenrikJannsen committed Dec 30, 2022
1 parent 1a24bd2 commit 68429cc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
29 changes: 16 additions & 13 deletions common/src/main/java/bisq/common/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
8 changes: 7 additions & 1 deletion network/tor/src/test/java/bisq/TorIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(() -> {
Expand Down
7 changes: 6 additions & 1 deletion network/tor/src/test/java/bisq/tor/AbstractTorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
6 changes: 5 additions & 1 deletion persistence/src/main/java/bisq/persistence/Persistence.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 68429cc

Please sign in to comment.