Skip to content

Commit

Permalink
fix: check if directory exists before delete (#1493)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed May 26, 2022
1 parent 34a31aa commit 81f209b
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions jadx-core/src/main/java/jadx/core/utils/files/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,22 @@ public static boolean deleteDir(File dir) {

public static void deleteDirIfExists(Path dir) {
if (Files.exists(dir)) {
deleteDir(dir);
try {
deleteDir(dir);
} catch (Exception e) {
LOG.error("Failed to delete dir: " + dir.toAbsolutePath(), e);
}
}
}

public static void deleteDir(Path dir) {
private static void deleteDir(Path dir) {
try (Stream<Path> pathStream = Files.walk(dir)) {
pathStream.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(file -> {
if (!file.delete()) {
LOG.warn("Failed to remove file: {}", file.getAbsolutePath());
.forEach(path -> {
try {
Files.delete(path);
} catch (Exception e) {
throw new JadxRuntimeException("Failed to delete path: " + path.toAbsolutePath(), e);
}
});
} catch (Exception e) {
Expand All @@ -152,11 +157,11 @@ private static Path createTempRootDir() {
}

public static void deleteTempRootDir() {
deleteDir(TEMP_ROOT_DIR);
deleteDirIfExists(TEMP_ROOT_DIR);
}

public static void clearTempRootDir() {
deleteDir(TEMP_ROOT_DIR);
deleteDirIfExists(TEMP_ROOT_DIR);
makeDirs(TEMP_ROOT_DIR);
}

Expand Down

0 comments on commit 81f209b

Please sign in to comment.