From 81f209ba9e9cd0bcaf7d5dfa654ca6f0a6778a72 Mon Sep 17 00:00:00 2001 From: Skylot Date: Thu, 26 May 2022 16:46:35 +0100 Subject: [PATCH] fix: check if directory exists before delete (#1493) --- .../java/jadx/core/utils/files/FileUtils.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/jadx-core/src/main/java/jadx/core/utils/files/FileUtils.java b/jadx-core/src/main/java/jadx/core/utils/files/FileUtils.java index 3c1979369..e25fbc31c 100644 --- a/jadx-core/src/main/java/jadx/core/utils/files/FileUtils.java +++ b/jadx-core/src/main/java/jadx/core/utils/files/FileUtils.java @@ -115,17 +115,22 @@ public class FileUtils { 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 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) { @@ -152,11 +157,11 @@ public class FileUtils { } 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); }