diff --git a/main/commons/src/main/java/org/cryptomator/common/vaults/Vault.java b/main/commons/src/main/java/org/cryptomator/common/vaults/Vault.java index 39cf6d2fd..72c1c7774 100644 --- a/main/commons/src/main/java/org/cryptomator/common/vaults/Vault.java +++ b/main/commons/src/main/java/org/cryptomator/common/vaults/Vault.java @@ -42,7 +42,11 @@ import java.util.EnumSet; import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import static org.cryptomator.common.Constants.MASTERKEY_FILENAME; @@ -70,6 +74,8 @@ public class Vault { private final StringBinding accessPoint; private final BooleanBinding accessPointPresent; private final BooleanProperty showingStats; + private final Lock lock = new ReentrantLock(); //FIXME: naming + private final Condition isLocked = lock.newCondition(); //FIXME: improve naming private volatile Volume volume; @@ -146,6 +152,13 @@ public class Vault { if (throwable != null) { LOG.warn("Unexpected unmount and lock of vault " + getDisplayName(), throwable); } + lock.lock(); + try { + isLocked.signal(); + } finally { + lock.unlock(); + } + }); } catch (Exception e) { destroyCryptoFileSystem(); @@ -163,6 +176,20 @@ public class Vault { volume.unmount(); } destroyCryptoFileSystem(); + lock.lock(); + try { + while (state.get() != VaultState.Value.LOCKED) { + if (!isLocked.await(3000, TimeUnit.MILLISECONDS)) { + throw new VolumeException("Locking failed"); //FIXME: other exception + } + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new VolumeException("Lock failed."); //FIXME: other/new exception + } finally { + lock.unlock(); + } + } public void reveal(Volume.Revealer vaultRevealer) throws VolumeException { diff --git a/main/commons/src/main/java/org/cryptomator/common/vaults/WebDavVolume.java b/main/commons/src/main/java/org/cryptomator/common/vaults/WebDavVolume.java index 352cfb489..03c83377c 100644 --- a/main/commons/src/main/java/org/cryptomator/common/vaults/WebDavVolume.java +++ b/main/commons/src/main/java/org/cryptomator/common/vaults/WebDavVolume.java @@ -17,8 +17,6 @@ import java.net.InetAddress; import java.net.UnknownHostException; import java.nio.file.Path; import java.util.Optional; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionStage; import java.util.function.Consumer; import java.util.function.Supplier; @@ -34,6 +32,7 @@ public class WebDavVolume implements Volume { private WebDavServer server; private WebDavServletController servlet; private Mounter.Mount mount; + private Consumer onExitAction; @Inject public WebDavVolume(Provider serverProvider, VaultSettings vaultSettings, Settings settings, WindowsDriveLetters windowsDriveLetters) { @@ -47,9 +46,10 @@ public class WebDavVolume implements Volume { public void mount(CryptoFileSystem fs, String mountFlags, Consumer onExitAction) throws VolumeException { startServlet(fs); mountServlet(); + this.onExitAction = onExitAction; } - private void startServlet(CryptoFileSystem fs){ + private void startServlet(CryptoFileSystem fs) { if (server == null) { server = serverProvider.get(); } @@ -69,7 +69,7 @@ public class WebDavVolume implements Volume { //on windows, prevent an automatic drive letter selection in the upstream library. Either we choose already a specifc one or there is no free. Supplier driveLetterSupplier; - if(System.getProperty("os.name").toLowerCase().contains("windows") && vaultSettings.winDriveLetter().isEmpty().get()) { + if (System.getProperty("os.name").toLowerCase().contains("windows") && vaultSettings.winDriveLetter().isEmpty().get()) { driveLetterSupplier = () -> windowsDriveLetters.getAvailableDriveLetter().orElse(null); } else { driveLetterSupplier = () -> vaultSettings.winDriveLetter().get(); @@ -104,6 +104,7 @@ public class WebDavVolume implements Volume { throw new VolumeException(e); } cleanup(); + onExitAction.accept(null); } @Override @@ -114,6 +115,7 @@ public class WebDavVolume implements Volume { throw new VolumeException(e); } cleanup(); + onExitAction.accept(null); } @Override diff --git a/main/ui/src/main/java/org/cryptomator/ui/lock/LockWorkflow.java b/main/ui/src/main/java/org/cryptomator/ui/lock/LockWorkflow.java index fecbe42c4..5566caeb0 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/lock/LockWorkflow.java +++ b/main/ui/src/main/java/org/cryptomator/ui/lock/LockWorkflow.java @@ -83,7 +83,7 @@ public class LockWorkflow extends Task { @Override protected void succeeded() { LOG.info("Lock of {} succeeded.", vault.getDisplayName()); - //DO NOT SET VAULT STATE HERE, this is done by the vault internally + vault.stateProperty().transition(VaultState.Value.PROCESSING, VaultState.Value.LOCKED); } @Override