From a457355a25f9913ffc993a52d3c423b4a487ff7d Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Tue, 31 Aug 2021 10:04:42 +0200 Subject: [PATCH] allow drive letters A and B as mount targets --- .../AvailableDriveLetterChooser.java | 2 +- .../common/vaults/WebDavVolume.java | 2 +- .../common/vaults/WindowsDriveLetters.java | 28 ++++++++++++++++--- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/cryptomator/common/mountpoint/AvailableDriveLetterChooser.java b/src/main/java/org/cryptomator/common/mountpoint/AvailableDriveLetterChooser.java index d0fc04f3c..23ce8466a 100644 --- a/src/main/java/org/cryptomator/common/mountpoint/AvailableDriveLetterChooser.java +++ b/src/main/java/org/cryptomator/common/mountpoint/AvailableDriveLetterChooser.java @@ -24,6 +24,6 @@ class AvailableDriveLetterChooser implements MountPointChooser { @Override public Optional chooseMountPoint(Volume caller) { - return this.windowsDriveLetters.getAvailableDriveLetterPath(); + return this.windowsDriveLetters.getDesiredAvailableDriveLetterPath(); } } diff --git a/src/main/java/org/cryptomator/common/vaults/WebDavVolume.java b/src/main/java/org/cryptomator/common/vaults/WebDavVolume.java index b1850bc1e..3ac1820d4 100644 --- a/src/main/java/org/cryptomator/common/vaults/WebDavVolume.java +++ b/src/main/java/org/cryptomator/common/vaults/WebDavVolume.java @@ -70,7 +70,7 @@ public class WebDavVolume implements Volume { //on windows, prevent an automatic drive letter selection in the upstream library. Either we choose already a specific one or there is no free. Supplier driveLetterSupplier; if (System.getProperty("os.name").toLowerCase().contains("windows") && vaultSettings.winDriveLetter().isEmpty().get()) { - driveLetterSupplier = () -> windowsDriveLetters.getAvailableDriveLetter().orElse(null); + driveLetterSupplier = () -> windowsDriveLetters.getDesiredAvailableDriveLetter().orElse(null); } else { driveLetterSupplier = () -> vaultSettings.winDriveLetter().get(); } diff --git a/src/main/java/org/cryptomator/common/vaults/WindowsDriveLetters.java b/src/main/java/org/cryptomator/common/vaults/WindowsDriveLetters.java index f3ef2c7dc..9ca56b462 100644 --- a/src/main/java/org/cryptomator/common/vaults/WindowsDriveLetters.java +++ b/src/main/java/org/cryptomator/common/vaults/WindowsDriveLetters.java @@ -22,11 +22,11 @@ import java.util.stream.StreamSupport; @Singleton public final class WindowsDriveLetters { - private static final Set C_TO_Z; + private static final Set A_TO_Z; static { - try (IntStream stream = IntStream.rangeClosed('C', 'Z')) { - C_TO_Z = stream.mapToObj(i -> String.valueOf((char) i)).collect(ImmutableSet.toImmutableSet()); + try (IntStream stream = IntStream.rangeClosed('A', 'Z')) { + A_TO_Z = stream.mapToObj(i -> String.valueOf((char) i)).collect(ImmutableSet.toImmutableSet()); } } @@ -35,7 +35,7 @@ public final class WindowsDriveLetters { } public Set getAllDriveLetters() { - return C_TO_Z; + return A_TO_Z; } public Set getOccupiedDriveLetters() { @@ -59,6 +59,26 @@ public final class WindowsDriveLetters { return getAvailableDriveLetter().map(this::toPath); } + /** + * Skips A and B and only returns them if all other are occupied. + * + * @return an Optional containing either the letter of a free drive letter or empty, if none is available + */ + public Optional getDesiredAvailableDriveLetter() { + var availableDriveLetters = getAvailableDriveLetters(); + var optString = availableDriveLetters.stream().filter(s -> !(s.equals("A") || s.equals("B"))).findFirst(); + return optString.or(() -> availableDriveLetters.stream().findFirst()); + } + + /** + * Skips A and B and only returns them if all other are occupied. + * + * @return an Optional containing either the path to a free drive letter or empty, if none is available + */ + public Optional getDesiredAvailableDriveLetterPath() { + return getDesiredAvailableDriveLetter().map(this::toPath); + } + public Path toPath(String driveLetter) { return Path.of(driveLetter + ":\\"); }