From 92b77baf40c241023a6d05075fe2e78afbcf5650 Mon Sep 17 00:00:00 2001 From: JaniruTEC <52893617+JaniruTEC@users.noreply.github.com> Date: Mon, 10 Jul 2023 15:42:49 +0200 Subject: [PATCH] Improved handling of existing hideaways --- .../common/mount/MountWithinParentUtil.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/cryptomator/common/mount/MountWithinParentUtil.java b/src/main/java/org/cryptomator/common/mount/MountWithinParentUtil.java index f203a359f..2ec5ad862 100644 --- a/src/main/java/org/cryptomator/common/mount/MountWithinParentUtil.java +++ b/src/main/java/org/cryptomator/common/mount/MountWithinParentUtil.java @@ -28,9 +28,7 @@ public final class MountWithinParentUtil { var hideExists = Files.exists(hideaway, LinkOption.NOFOLLOW_LINKS); //TODO: possible improvement by just deleting an _empty_ hideaway - if (mpExists && hideExists) { //both resources exist (whatever type) - throw new MountPointPreparationException(new FileAlreadyExistsException(hideaway.toString())); - } else if (!mpExists && !hideExists) { //neither mountpoint nor hideaway exist + if (!mpExists && !hideExists) { //neither mountpoint nor hideaway exist throw new MountPointPreparationException(new NoSuchFileException(mountPoint.toString())); } else if (!mpExists) { //only hideaway exists checkIsDirectory(hideaway); @@ -42,8 +40,13 @@ public final class MountWithinParentUtil { } catch (IOException e) { throw new MountPointPreparationException(e); } - } else { //only mountpoint exists + } else { //mountpoint exists... try { + if (hideExists) { //... with hideaway + removeResidualHideaway(hideaway); + } + + //... (now) without hideaway checkIsDirectory(mountPoint); checkIsEmpty(mountPoint); @@ -68,6 +71,16 @@ public final class MountWithinParentUtil { } } + private static void removeResidualHideaway(Path hideaway) throws IOException { + if (!Files.isDirectory(hideaway, LinkOption.NOFOLLOW_LINKS)) { + if (SystemUtils.IS_OS_WINDOWS) { + Files.setAttribute(hideaway, WIN_HIDDEN_ATTR, false); + } + throw new MountPointPreparationException(new NotDirectoryException(hideaway.toString())); + } + Files.delete(hideaway); + } + static void cleanup(Path mountPoint) { Path hideaway = getHideaway(mountPoint); try {