mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-05-14 08:41:28 +00:00
Lazily call IrregularUnmountCleaner from (and only from!) TemporaryMountPointChooser
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.cryptomator.ui.launcher;
|
||||
|
||||
import org.cryptomator.common.Environment;
|
||||
import org.cryptomator.common.mountpoint.IrregularUnmountCleaner;
|
||||
import org.cryptomator.common.settings.Settings;
|
||||
import org.cryptomator.common.vaults.Vault;
|
||||
import org.cryptomator.integrations.tray.TrayIntegrationProvider;
|
||||
@@ -16,8 +15,6 @@ import javafx.collections.ObservableList;
|
||||
import java.awt.Desktop;
|
||||
import java.awt.SystemTray;
|
||||
import java.awt.desktop.AppReopenedListener;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -65,10 +62,6 @@ public class UiLauncher {
|
||||
// register app reopen listener
|
||||
Desktop.getDesktop().addAppEventListener((AppReopenedListener) e -> showMainWindowAsync(hasTrayIcon));
|
||||
|
||||
//clean leftovers of not-regularly unmounted vaults
|
||||
//see https://github.com/cryptomator/cryptomator/issues/1013 and https://github.com/cryptomator/cryptomator/issues/1061
|
||||
env.getMountPointsDir().filter(path -> Files.exists(path, LinkOption.NOFOLLOW_LINKS)).ifPresent(IrregularUnmountCleaner::removeIrregularUnmountDebris);
|
||||
|
||||
// auto unlock
|
||||
Collection<Vault> vaultsToAutoUnlock = vaults.filtered(this::shouldAttemptAutoUnlock);
|
||||
if (!vaultsToAutoUnlock.isEmpty()) {
|
||||
|
||||
Reference in New Issue
Block a user