mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-09-18 22:14:26 +00:00
Lazily call IrregularUnmountCleaner from (and only from!) TemporaryMountPointChooser
This commit is contained in:
+1
-8
@@ -10,7 +10,7 @@ import java.nio.file.LinkOption;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
|
||||
public class IrregularUnmountCleaner {
|
||||
class IrregularUnmountCleaner {
|
||||
|
||||
public static Logger LOG = LoggerFactory.getLogger(IrregularUnmountCleaner.class);
|
||||
|
||||
@@ -48,7 +48,6 @@ public class IrregularUnmountCleaner {
|
||||
private static void deleteEmptyDir(Path dir) throws IOException {
|
||||
assert Files.isDirectory(dir, LinkOption.NOFOLLOW_LINKS);
|
||||
try {
|
||||
checkEmpty(dir);
|
||||
Files.delete(dir); // attempt to delete dir non-recursively (will fail, if there are contents)
|
||||
} catch (DirectoryNotEmptyException e) {
|
||||
LOG.info("Found non-empty directory in mountpoint dir: {}", dir);
|
||||
@@ -62,10 +61,4 @@ public class IrregularUnmountCleaner {
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkEmpty(Path dir) throws IOException {
|
||||
if(Files.newDirectoryStream(dir).iterator().hasNext()) {
|
||||
throw new DirectoryNotEmptyException(dir.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+20
-2
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user