|
@@ -11,9 +11,11 @@ import javax.inject.Inject;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.nio.file.Files;
|
|
|
+import java.nio.file.LinkOption;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
import java.util.Optional;
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
class TemporaryMountPointChooser implements MountPointChooser {
|
|
|
|
|
@@ -22,6 +24,7 @@ class TemporaryMountPointChooser implements MountPointChooser {
|
|
|
|
|
|
private final VaultSettings vaultSettings;
|
|
|
private final Environment environment;
|
|
|
+ private volatile boolean clearedDebris;
|
|
|
|
|
|
@Inject
|
|
|
public TemporaryMountPointChooser(VaultSettings vaultSettings, Environment environment) {
|
|
@@ -40,9 +43,24 @@ class TemporaryMountPointChooser implements MountPointChooser {
|
|
|
|
|
|
@Override
|
|
|
public Optional<Path> chooseMountPoint(Volume caller) {
|
|
|
+ clearDebrisIfNeeded();
|
|
|
return this.environment.getMountPointsDir().map(this::choose);
|
|
|
}
|
|
|
|
|
|
+ //clean leftovers of not-regularly unmounted vaults
|
|
|
+ //see https://github.com/cryptomator/cryptomator/issues/1013 and https://github.com/cryptomator/cryptomator/issues/1061
|
|
|
+ private synchronized void clearDebrisIfNeeded() {
|
|
|
+ assert environment.getMountPointsDir().isPresent();
|
|
|
+ if (clearedDebris) {
|
|
|
+ return; // already cleared
|
|
|
+ }
|
|
|
+ Path mountPointDir = environment.getMountPointsDir().get();
|
|
|
+ if (Files.exists(mountPointDir, LinkOption.NOFOLLOW_LINKS)) {
|
|
|
+ IrregularUnmountCleaner.removeIrregularUnmountDebris(mountPointDir);
|
|
|
+ }
|
|
|
+ clearedDebris = true;
|
|
|
+ }
|
|
|
+
|
|
|
private Path choose(Path parent) {
|
|
|
String basename = this.vaultSettings.mountName().get();
|
|
|
//regular
|
|
@@ -51,13 +69,13 @@ class TemporaryMountPointChooser implements MountPointChooser {
|
|
|
return mountPoint;
|
|
|
}
|
|
|
//with id
|
|
|
- mountPoint = parent.resolve(basename + " (" +vaultSettings.getId() + ")");
|
|
|
+ mountPoint = parent.resolve(basename + " (" + vaultSettings.getId() + ")");
|
|
|
if (Files.notExists(mountPoint)) {
|
|
|
return mountPoint;
|
|
|
}
|
|
|
//with id and count
|
|
|
for (int i = 1; i < MAX_TMPMOUNTPOINT_CREATION_RETRIES; i++) {
|
|
|
- mountPoint = parent.resolve(basename + "_(" +vaultSettings.getId() + ")_"+i);
|
|
|
+ mountPoint = parent.resolve(basename + "_(" + vaultSettings.getId() + ")_" + i);
|
|
|
if (Files.notExists(mountPoint)) {
|
|
|
return mountPoint;
|
|
|
}
|