Perform cleanup of tmp mount points dir only once.

This commit is contained in:
Armin Schrenk
2020-11-18 14:07:34 +01:00
parent df2b4ac086
commit b0ad86f16b
2 changed files with 36 additions and 23 deletions

View File

@@ -1,20 +1,44 @@
package org.cryptomator.common.mountpoint;
import org.cryptomator.common.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Optional;
@Singleton
class IrregularUnmountCleaner {
public static Logger LOG = LoggerFactory.getLogger(IrregularUnmountCleaner.class);
public static void removeIrregularUnmountDebris(Path dirContainingMountPoints) {
private final Optional<Path> tmpMountPointDir;
private volatile boolean alreadyChecked = false;
@Inject
public IrregularUnmountCleaner(Environment env) {
this.tmpMountPointDir = env.getMountPointsDir();
}
public void clearIrregularUnmountDebrisIfNeeded() {
if (alreadyChecked || tmpMountPointDir.isEmpty()) {
return; //nuthin to do
}
if (Files.exists(tmpMountPointDir.get(), LinkOption.NOFOLLOW_LINKS)) {
clearIrregularUnmountDebris(tmpMountPointDir.get());
}
alreadyChecked = true;
}
private void clearIrregularUnmountDebris(Path dirContainingMountPoints) {
IOException cleanupFailed = new IOException("Cleanup failed");
try {
@@ -41,11 +65,12 @@ class IrregularUnmountCleaner {
}
} catch (IOException e) {
LOG.warn("Unable to perform cleanup of mountpoint dir {}.", dirContainingMountPoints, e);
} finally {
alreadyChecked = true;
}
}
private static void deleteEmptyDir(Path dir) throws IOException {
private void deleteEmptyDir(Path dir) throws IOException {
assert Files.isDirectory(dir, LinkOption.NOFOLLOW_LINKS);
try {
Files.delete(dir); // attempt to delete dir non-recursively (will fail, if there are contents)
@@ -54,7 +79,7 @@ class IrregularUnmountCleaner {
}
}
private static void deleteDeadLink(Path symlink) throws IOException {
private void deleteDeadLink(Path symlink) throws IOException {
assert Files.isSymbolicLink(symlink);
if (Files.notExists(symlink)) { // following link: target does not exist
Files.delete(symlink);

View File

@@ -1,6 +1,5 @@
package org.cryptomator.common.mountpoint;
import org.apache.commons.lang3.SystemUtils;
import org.cryptomator.common.Environment;
import org.cryptomator.common.settings.VaultSettings;
import org.cryptomator.common.vaults.Volume;
@@ -11,11 +10,8 @@ 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 {
@@ -24,12 +20,14 @@ class TemporaryMountPointChooser implements MountPointChooser {
private final VaultSettings vaultSettings;
private final Environment environment;
private final IrregularUnmountCleaner cleaner;
private volatile boolean clearedDebris;
@Inject
public TemporaryMountPointChooser(VaultSettings vaultSettings, Environment environment) {
public TemporaryMountPointChooser(VaultSettings vaultSettings, Environment environment, IrregularUnmountCleaner cleaner) {
this.vaultSettings = vaultSettings;
this.environment = environment;
this.cleaner = cleaner;
}
@Override
@@ -43,23 +41,13 @@ class TemporaryMountPointChooser implements MountPointChooser {
@Override
public Optional<Path> chooseMountPoint(Volume caller) {
clearDebrisIfNeeded();
assert environment.getMountPointsDir().isPresent();
//clean leftovers of not-regularly unmounted vaults
//see https://github.com/cryptomator/cryptomator/issues/1013 and https://github.com/cryptomator/cryptomator/issues/1061
cleaner.clearIrregularUnmountDebrisIfNeeded();
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();