From 97cdd5386122ddc00f416bcc164e8e69e013082c Mon Sep 17 00:00:00 2001 From: CalDescent Date: Wed, 1 Dec 2021 11:59:14 +0000 Subject: [PATCH] Fixed bugs in safeDeleteEmptyParentDirectories() --- .../java/org/qortal/utils/FilesystemUtils.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/qortal/utils/FilesystemUtils.java b/src/main/java/org/qortal/utils/FilesystemUtils.java index 6424e5e8..5c3e8b38 100644 --- a/src/main/java/org/qortal/utils/FilesystemUtils.java +++ b/src/main/java/org/qortal/utils/FilesystemUtils.java @@ -169,16 +169,22 @@ public class FilesystemUtils { } public static void safeDeleteEmptyParentDirectories(Path path) throws IOException { - final Path absolutePath = path.toAbsolutePath(); - if (!absolutePath.toFile().isDirectory()) { + final Path parentPath = path.toAbsolutePath().getParent(); + if (!parentPath.toFile().isDirectory()) { return; } - if (!FilesystemUtils.pathInsideDataOrTempPath(absolutePath)) { + if (!FilesystemUtils.pathInsideDataOrTempPath(parentPath)) { return; } - Files.deleteIfExists(absolutePath); + try { + Files.deleteIfExists(parentPath); - FilesystemUtils.safeDeleteEmptyParentDirectories(absolutePath.getParent()); + } catch (DirectoryNotEmptyException e) { + // We've reached the limits of what we can delete + return; + } + + FilesystemUtils.safeDeleteEmptyParentDirectories(parentPath); } public static boolean pathInsideDataOrTempPath(Path path) {