Moving the code for unregular unmount leftovers from mountpoint chooser to an own class and execute it at each application start.

This commit is contained in:
Armin Schrenk
2020-11-02 17:34:34 +01:00
parent d24734e16f
commit aa22f656e8
2 changed files with 69 additions and 1 deletions

View File

@@ -0,0 +1,60 @@
package org.cryptomator.common.mountpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collection;
public class UnregularUnmountCleaner {
public static Logger LOG = LoggerFactory.getLogger(UnregularUnmountCleaner.class);
public static void removeUnregularUnmountDebris(Path dirOfMountPoints) {
Collection<IOException> exceptionCatcher = new ArrayList<>();
try {
LOG.debug("Performing cleanup of mountpoint dir {}.", dirOfMountPoints);
Files.list(dirOfMountPoints).filter(p -> {
if (Files.isDirectory(p, LinkOption.NOFOLLOW_LINKS)) {
return true;
} else {
LOG.debug("Found non-directory element in mountpoint dir: {}", p);
return false;
}
}).forEach(dir -> {
try {
try {
Files.readAttributes(dir, BasicFileAttributes.class); //we follow the link and see if it exists
} catch (NoSuchFileException e) {
Files.delete(dir); //broken link file, we delete it
LOG.debug("Removed broken entry {} from mountpoint dir.");
return;
}
//check if dir is empty
if (Files.list(dir).findFirst().isEmpty()) {
Files.delete(dir);
LOG.debug("Removed empty dir {} from mountpoint dir.");
} else {
LOG.debug("Found non-empty directory in mountpoint dir: {}", dir);
}
} catch (IOException e) {
exceptionCatcher.add(e);
}
});
exceptionCatcher.forEach(exception -> {
LOG.debug("Unable to clean element from mountpoint dir due to", exception);
});
} catch (IOException e) {
LOG.debug("Unable to perform cleanup of mountpoint dir {}.", dirOfMountPoints, e);
}
}
}

View File

@@ -1,5 +1,7 @@
package org.cryptomator.ui.launcher;
import org.cryptomator.common.Environment;
import org.cryptomator.common.mountpoint.UnregularUnmountCleaner;
import org.cryptomator.common.settings.Settings;
import org.cryptomator.common.vaults.Vault;
import org.cryptomator.jni.JniException;
@@ -30,15 +32,17 @@ public class UiLauncher {
private final FxApplicationStarter fxApplicationStarter;
private final AppLaunchEventHandler launchEventHandler;
private final Optional<MacFunctions> macFunctions;
private final Environment env;
@Inject
public UiLauncher(Settings settings, ObservableList<Vault> vaults, TrayMenuComponent.Builder trayComponent, FxApplicationStarter fxApplicationStarter, AppLaunchEventHandler launchEventHandler, Optional<MacFunctions> macFunctions) {
public UiLauncher(Settings settings, ObservableList<Vault> vaults, TrayMenuComponent.Builder trayComponent, FxApplicationStarter fxApplicationStarter, AppLaunchEventHandler launchEventHandler, Optional<MacFunctions> macFunctions, Environment env) {
this.settings = settings;
this.vaults = vaults;
this.trayComponent = trayComponent;
this.fxApplicationStarter = fxApplicationStarter;
this.launchEventHandler = launchEventHandler;
this.macFunctions = macFunctions;
this.env = env;
}
public void launch() {
@@ -61,6 +65,10 @@ 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().ifPresent(UnregularUnmountCleaner::removeUnregularUnmountDebris);
// auto unlock
Collection<Vault> vaultsToAutoUnlock = vaults.filtered(this::shouldAttemptAutoUnlock);
if (!vaultsToAutoUnlock.isEmpty()) {