From 08fc5e9a77f20339e1cfb921f25d8bcac5ab57c0 Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Tue, 20 Sep 2022 10:55:23 +0200 Subject: [PATCH] Fixes #2454 --- .../mountpoint/CustomMountPointChooser.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cryptomator/common/mountpoint/CustomMountPointChooser.java b/src/main/java/org/cryptomator/common/mountpoint/CustomMountPointChooser.java index 60d7a3dc9..295e922c1 100644 --- a/src/main/java/org/cryptomator/common/mountpoint/CustomMountPointChooser.java +++ b/src/main/java/org/cryptomator/common/mountpoint/CustomMountPointChooser.java @@ -1,7 +1,6 @@ package org.cryptomator.common.mountpoint; import org.apache.commons.lang3.SystemUtils; -import org.cryptomator.common.Environment; import org.cryptomator.common.settings.VaultSettings; import org.cryptomator.common.settings.VolumeImpl; import org.cryptomator.common.vaults.MountPointRequirement; @@ -19,7 +18,6 @@ import java.nio.file.NoSuchFileException; import java.nio.file.NotDirectoryException; import java.nio.file.Path; import java.nio.file.Paths; -import java.nio.file.attribute.BasicFileAttributes; import java.util.Optional; class CustomMountPointChooser implements MountPointChooser { @@ -118,6 +116,7 @@ class CustomMountPointChooser implements MountPointChooser { if (caller.getMountPointRequirement() == MountPointRequirement.PARENT_NO_MOUNT_POINT) { Path hideaway = getHideaway(mountPoint); try { + waitForMountpointRestoration(mountPoint); Files.move(hideaway, mountPoint); if (SystemUtils.IS_OS_WINDOWS) { Files.setAttribute(mountPoint, WIN_HIDDEN, false); @@ -128,6 +127,24 @@ class CustomMountPointChooser implements MountPointChooser { } } + //on Windows removing the mountpoint takes some time, so we poll for at most 3 seconds + private void waitForMountpointRestoration(Path mountPoint) throws FileAlreadyExistsException { + int attempts = 0; + while (!Files.notExists(mountPoint, LinkOption.NOFOLLOW_LINKS)) { + attempts++; + if (attempts >= 10) { + throw new FileAlreadyExistsException("Timeout waiting for mountpoint cleanup for " + mountPoint + " ."); + } + + try { + Thread.sleep(300); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new FileAlreadyExistsException("Interrupted before mountpoint " + mountPoint + " was cleared"); + } + } + } + private void checkIsDirectory(Path toCheck) throws InvalidMountPointException { if (!Files.isDirectory(toCheck, LinkOption.NOFOLLOW_LINKS)) { throw new InvalidMountPointException(new NotDirectoryException(toCheck.toString()));