Armin Schrenk 2 years ago
parent
commit
08fc5e9a77

+ 19 - 2
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()));