From 5e2e8c35d5f3c19f803196ea49e5dd1bb1d19ceb Mon Sep 17 00:00:00 2001 From: JaniruTEC <52893617+JaniruTEC@users.noreply.github.com> Date: Mon, 10 Jul 2023 17:38:30 +0200 Subject: [PATCH] Added cleanup of junctions on Win --- .../common/mount/MountWithinParentUtil.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/cryptomator/common/mount/MountWithinParentUtil.java b/src/main/java/org/cryptomator/common/mount/MountWithinParentUtil.java index 3ce6d1c39..04e84f64e 100644 --- a/src/main/java/org/cryptomator/common/mount/MountWithinParentUtil.java +++ b/src/main/java/org/cryptomator/common/mount/MountWithinParentUtil.java @@ -12,6 +12,7 @@ import java.nio.file.LinkOption; import java.nio.file.NoSuchFileException; import java.nio.file.NotDirectoryException; import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; public final class MountWithinParentUtil { @@ -24,7 +25,7 @@ public final class MountWithinParentUtil { static void prepareParentNoMountPoint(Path mountPoint) throws MountPointPreparationException { Path hideaway = getHideaway(mountPoint); - var mpExists = Files.exists(mountPoint, LinkOption.NOFOLLOW_LINKS); + var mpExists = removeResidualJunction(mountPoint); //Handle junction as not existing var hideExists = Files.exists(hideaway, LinkOption.NOFOLLOW_LINKS); //TODO: possible improvement by just deleting an _empty_ hideaway @@ -71,6 +72,25 @@ public final class MountWithinParentUtil { } } + private static boolean removeResidualJunction(Path path) throws MountPointPreparationException { + try { + if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) { + return false; + } + if (!SystemUtils.IS_OS_WINDOWS) { //So far this is only a problem on Windows + return true; + } + if (Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS).isOther()) { + LOG.info("Mountpoint \"{}\" is still a junction. Deleting it.", path); + Files.delete(path); //Throws if path is also a non-empty folder + return false; + } + return true; + } catch (IOException e) { + throw new MountPointPreparationException(e); + } + } + private static void removeResidualHideaway(Path hideaway) throws IOException { if (!Files.isDirectory(hideaway, LinkOption.NOFOLLOW_LINKS)) { throw new MountPointPreparationException(new NotDirectoryException(hideaway.toString()));