From a63bcfbaa269627887c553a8ddc1b77b033db7ae Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Sun, 4 Sep 2016 16:04:16 +0200 Subject: [PATCH] relaxed "vault not empty" check --- .../cryptomator/ui/controllers/InitializeController.java | 6 ++++-- main/ui/src/main/java/org/cryptomator/ui/model/Vault.java | 8 ++++++-- main/ui/src/main/resources/localization/en.txt | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/main/ui/src/main/java/org/cryptomator/ui/controllers/InitializeController.java b/main/ui/src/main/java/org/cryptomator/ui/controllers/InitializeController.java index 9a61f28c9..d45cce37a 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/controllers/InitializeController.java +++ b/main/ui/src/main/java/org/cryptomator/ui/controllers/InitializeController.java @@ -10,8 +10,8 @@ package org.cryptomator.ui.controllers; import java.io.IOException; -import java.io.UncheckedIOException; import java.net.URL; +import java.nio.file.DirectoryNotEmptyException; import java.nio.file.FileAlreadyExistsException; import java.util.Objects; import java.util.Optional; @@ -124,7 +124,9 @@ public class InitializeController extends LocalizedFXMLViewController { listener.ifPresent(this::invokeListenerLater); } catch (FileAlreadyExistsException ex) { messageLabel.setText(localization.getString("initialize.messageLabel.alreadyInitialized")); - } catch (UncheckedIOException | IOException ex) { + } catch (DirectoryNotEmptyException ex) { + messageLabel.setText(localization.getString("initialize.messageLabel.notEmpty")); + } catch (IOException ex) { LOG.error("I/O Exception", ex); messageLabel.setText(localization.getString("initialize.messageLabel.initializationFailed")); } finally { diff --git a/main/ui/src/main/java/org/cryptomator/ui/model/Vault.java b/main/ui/src/main/java/org/cryptomator/ui/model/Vault.java index 07c83530d..8c0c00fc3 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/model/Vault.java +++ b/main/ui/src/main/java/org/cryptomator/ui/model/Vault.java @@ -12,6 +12,7 @@ import static org.apache.commons.lang3.StringUtils.stripStart; import java.io.IOException; import java.io.UncheckedIOException; +import java.nio.file.DirectoryNotEmptyException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; @@ -30,6 +31,7 @@ import org.apache.commons.lang3.SystemUtils; import org.cryptomator.common.LazyInitializer; import org.cryptomator.common.Optionals; import org.cryptomator.crypto.engine.InvalidPassphraseException; +import org.cryptomator.filesystem.File; import org.cryptomator.filesystem.FileSystem; import org.cryptomator.filesystem.charsets.NormalizedNameFileSystem; import org.cryptomator.filesystem.crypto.CryptoFileSystemDelegate; @@ -109,8 +111,10 @@ public class Vault implements CryptoFileSystemDelegate { public void create(CharSequence passphrase) throws IOException { try { FileSystem fs = getNioFileSystem(); - if (fs.children().count() > 0) { - throw new FileAlreadyExistsException(null, null, "Vault location not empty."); + if (fs.files().map(File::name).filter(s -> s.endsWith(VAULT_FILE_EXTENSION)).count() > 0) { + throw new FileAlreadyExistsException("masterkey.cryptomator", null, "Vault location not empty."); + } else if (fs.folders().count() > 0) { + throw new DirectoryNotEmptyException(fs.toString()); } cryptoFileSystemFactory.initializeNew(fs, passphrase); } catch (UncheckedIOException e) { diff --git a/main/ui/src/main/resources/localization/en.txt b/main/ui/src/main/resources/localization/en.txt index 1e72c083e..984884574 100644 --- a/main/ui/src/main/resources/localization/en.txt +++ b/main/ui/src/main/resources/localization/en.txt @@ -26,6 +26,7 @@ initialize.label.password=Password initialize.label.retypePassword=Retype password initialize.button.ok=Create vault initialize.messageLabel.alreadyInitialized=Vault already initialized +initialize.messageLabel.notEmpty=Vault not empty initialize.messageLabel.initializationFailed=Could not initialize vault. See log file for details. initialize.messageLabel.passwordStrength.0=Very weak initialize.messageLabel.passwordStrength.1=Weak