pr mentioned changes

This commit is contained in:
Jan-Peter Klein
2025-06-05 13:01:47 +02:00
parent 8c84720e24
commit 225332bd4f

View File

@@ -1,9 +1,10 @@
package org.cryptomator.common.recovery;
import static org.cryptomator.common.vaults.VaultState.Value.*;
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 org.cryptomator.common.vaults.VaultState.Value;
@@ -13,19 +14,23 @@ public final class BackupRestorer {
private BackupRestorer() {}
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)) {
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))
.orElse(false);
} catch (IOException e) {