Просмотр исходного кода

Updated CustomMountPointChooser to use MPR instead of OS-Checks

Updated CustomMountPointChooser to use MountPointRequirements instead of OS-Checks.

This led to the discovery of the bug that was fixed in 724b20c826923433348d34fc399aec64e85faae1.
JaniruTEC 4 лет назад
Родитель
Сommit
708bcaa630

+ 28 - 20
main/commons/src/main/java/org/cryptomator/common/mountpoint/CustomMountPointChooser.java

@@ -1,6 +1,6 @@
 package org.cryptomator.common.mountpoint;
 
-import org.apache.commons.lang3.SystemUtils;
+import org.cryptomator.common.vaults.MountPointRequirement;
 import org.cryptomator.common.vaults.Vault;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -34,28 +34,37 @@ public class CustomMountPointChooser implements MountPointChooser {
 
 	@Override
 	public boolean prepare(Path mountPoint) throws InvalidMountPointException {
-		//On Windows the target folder MUST NOT exist...
-		//https://github.com/billziss-gh/winfsp/issues/320
-		if (SystemUtils.IS_OS_WINDOWS) {
+		MountPointRequirement requirement = this.vault.getMountPointRequirement();
+		//Requirement "NONE" doesn't make any sense here.
+		//No need to prepare/verify a Mountpoint without requiring one...
+		assert requirement != MountPointRequirement.NONE;
+
+		//Not implemented anywhere (yet)
+		assert requirement != MountPointRequirement.PARENT_OPT_MOUNT_POINT;
+
+		if(requirement == MountPointRequirement.PARENT_NO_MOUNT_POINT) {
+			//This the case on Windows when using FUSE
+			//See https://github.com/billziss-gh/winfsp/issues/320
+
 			//We must use #notExists() here because notExists =/= !exists (see docs)
-			if (Files.notExists(mountPoint, LinkOption.NOFOLLOW_LINKS)) {
-				//File really doesn't exist
-				return false;
+			if (!Files.notExists(mountPoint, LinkOption.NOFOLLOW_LINKS)) {
+				//File exists OR can't be determined
+				throw wrapAsIMPE(new FileAlreadyExistsException(mountPoint.toString()));
 			}
-			//File exists OR can't be determined
-			throw wrapAsIMPE(new FileAlreadyExistsException(mountPoint.toString()));
-		}
+		} else if(requirement == MountPointRequirement.EMPTY_MOUNT_POINT) {
+			//This is the case for Windows when using Dokany
+			//and for Linux and Mac
 
-		//... on Mac and Linux it's the opposite
-		if (!Files.isDirectory(mountPoint)) {
-			throw wrapAsIMPE(new NotDirectoryException(mountPoint.toString()));
-		}
-		try (DirectoryStream<Path> ds = Files.newDirectoryStream(mountPoint)) {
-			if (ds.iterator().hasNext()) {
-				throw wrapAsIMPE(new DirectoryNotEmptyException(mountPoint.toString()));
+			if (!Files.isDirectory(mountPoint)) {
+				throw wrapAsIMPE(new NotDirectoryException(mountPoint.toString()));
+			}
+			try (DirectoryStream<Path> ds = Files.newDirectoryStream(mountPoint)) {
+				if (ds.iterator().hasNext()) {
+					throw wrapAsIMPE(new DirectoryNotEmptyException(mountPoint.toString()));
+				}
+			} catch (IOException exception) {
+				throw wrapAsIMPE(exception);
 			}
-		} catch (IOException exception) {
-			throw wrapAsIMPE(exception);
 		}
 		LOG.debug("Successfully checked custom mount point: {}", mountPoint);
 		return false;
@@ -64,5 +73,4 @@ public class CustomMountPointChooser implements MountPointChooser {
 	private InvalidMountPointException wrapAsIMPE(Exception exception) {
 		return new InvalidMountPointException(exception);
 	}
-
 }