|
@@ -1,9 +1,10 @@
|
|
package org.cryptomator.common.recovery;
|
|
package org.cryptomator.common.recovery;
|
|
|
|
|
|
-import static org.cryptomator.common.vaults.VaultState.Value.*;
|
|
|
|
-
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
-import java.nio.file.*;
|
|
|
|
|
|
+import java.nio.file.Path;
|
|
|
|
+import java.nio.file.Files;
|
|
|
|
+import java.nio.file.StandardCopyOption;
|
|
|
|
+import java.nio.file.attribute.FileTime;
|
|
import java.util.stream.Stream;
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
import org.cryptomator.common.vaults.VaultState.Value;
|
|
import org.cryptomator.common.vaults.VaultState.Value;
|
|
@@ -13,19 +14,23 @@ public final class BackupRestorer {
|
|
private BackupRestorer() {}
|
|
private BackupRestorer() {}
|
|
|
|
|
|
public static boolean restoreIfPresent(Path vaultPath, Value vaultState) {
|
|
public static boolean restoreIfPresent(Path vaultPath, Value vaultState) {
|
|
- Path targetFile;
|
|
|
|
- switch (vaultState) {
|
|
|
|
- case VAULT_CONFIG_MISSING -> targetFile = vaultPath.resolve("vault.cryptomator");
|
|
|
|
- case MASTERKEY_MISSING -> targetFile = vaultPath.resolve("masterkey.cryptomator");
|
|
|
|
- default -> {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ Path targetFile = switch (vaultState) {
|
|
|
|
+ case VAULT_CONFIG_MISSING -> vaultPath.resolve("vault.cryptomator");
|
|
|
|
+ case MASTERKEY_MISSING -> vaultPath.resolve("masterkey.cryptomator");
|
|
|
|
+ default -> throw new IllegalArgumentException("Unexpected vault state: " + vaultState);
|
|
|
|
+ };
|
|
|
|
|
|
try (Stream<Path> files = Files.list(vaultPath)) {
|
|
try (Stream<Path> files = Files.list(vaultPath)) {
|
|
- return files
|
|
|
|
- .filter(file -> isValidBackupFileForState(file.getFileName().toString(), vaultState))
|
|
|
|
- .findFirst()
|
|
|
|
|
|
+ return files.filter(file -> isValidBackupFileForState(file.getFileName().toString(), vaultState))
|
|
|
|
+ .max((f1, f2) -> {
|
|
|
|
+ try {
|
|
|
|
+ FileTime time1 = Files.getLastModifiedTime(f1);
|
|
|
|
+ FileTime time2 = Files.getLastModifiedTime(f2);
|
|
|
|
+ return time1.compareTo(time2);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+ })
|
|
.map(backupFile -> copyBackupFile(backupFile, targetFile))
|
|
.map(backupFile -> copyBackupFile(backupFile, targetFile))
|
|
.orElse(false);
|
|
.orElse(false);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|