| | | 1 | | using LOCKnet.Core.DataAbstractions; |
| | | 2 | | |
| | | 3 | | namespace LOCKnet.Data; |
| | | 4 | | |
| | | 5 | | internal static class PlainToEncryptedVaultMigrationArtifacts |
| | | 6 | | { |
| | | 7 | | internal const string EncryptedTempSuffix = ".migration.encrypted.tmp"; |
| | | 8 | | internal const string PlainBackupSuffix = ".migration.plain.bak"; |
| | | 9 | | |
| | | 10 | | internal static string GetEncryptedTempPath(string databasePath) => databasePath + EncryptedTempSuffix; |
| | | 11 | | |
| | | 12 | | internal static string GetPlainBackupPath(string databasePath) => databasePath + PlainBackupSuffix; |
| | | 13 | | |
| | | 14 | | internal static bool HasPendingArtifacts(string? databasePath) |
| | | 15 | | => databasePath is not null && |
| | | 16 | | (File.Exists(GetEncryptedTempPath(databasePath)) || File.Exists(GetPlainBackupPath(databasePath))); |
| | | 17 | | |
| | | 18 | | internal static PlainToEncryptedVaultMigrationRecoveryDecision Decide(string? databasePath, VaultHeader header) |
| | | 19 | | { |
| | | 20 | | if (databasePath is null) |
| | | 21 | | return PlainToEncryptedVaultMigrationRecoveryDecision.None(); |
| | | 22 | | |
| | | 23 | | var tempPath = GetEncryptedTempPath(databasePath); |
| | | 24 | | var backupPath = GetPlainBackupPath(databasePath); |
| | | 25 | | var tempExists = File.Exists(tempPath); |
| | | 26 | | var backupExists = File.Exists(backupPath); |
| | | 27 | | |
| | | 28 | | return header.StorageMigrationState switch |
| | | 29 | | { |
| | | 30 | | VaultStorageMigrationState.None => (tempExists || backupExists) |
| | | 31 | | ? PlainToEncryptedVaultMigrationRecoveryDecision.Fail("Persistierte Storage-Migrationsartefakte gefunden, aber k |
| | | 32 | | : PlainToEncryptedVaultMigrationRecoveryDecision.None(), |
| | | 33 | | VaultStorageMigrationState.InProgress => tempExists && !backupExists |
| | | 34 | | ? PlainToEncryptedVaultMigrationRecoveryDecision.ResumeExport(tempPath, backupPath) |
| | | 35 | | : PlainToEncryptedVaultMigrationRecoveryDecision.Fail("Storage-Migration als in Arbeit markiert, aber die erwart |
| | | 36 | | VaultStorageMigrationState.FinalizationPending => backupExists && !tempExists |
| | | 37 | | ? PlainToEncryptedVaultMigrationRecoveryDecision.FinalizeBackupCleanup(tempPath, backupPath) |
| | | 38 | | : PlainToEncryptedVaultMigrationRecoveryDecision.Fail("Storage-Migration wartet auf Finalisierung, aber die erwa |
| | | 39 | | VaultStorageMigrationState.Failed => (tempExists || backupExists) |
| | | 40 | | ? PlainToEncryptedVaultMigrationRecoveryDecision.Fail("Fehlgeschlagene Storage-Migration hat Artefakte hinterlas |
| | | 41 | | : PlainToEncryptedVaultMigrationRecoveryDecision.None(), |
| | | 42 | | _ => PlainToEncryptedVaultMigrationRecoveryDecision.Fail("Unbekannter Storage-Migrationszustand erkannt.", tempPat |
| | | 43 | | }; |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | internal enum PlainToEncryptedVaultMigrationRecoveryAction |
| | | 48 | | { |
| | | 49 | | None = 0, |
| | | 50 | | ResumeExport = 1, |
| | | 51 | | FinalizeBackupCleanup = 2, |
| | | 52 | | Fail = 3, |
| | | 53 | | } |
| | | 54 | | |
| | 5 | 55 | | internal sealed record PlainToEncryptedVaultMigrationRecoveryDecision( |
| | 5 | 56 | | PlainToEncryptedVaultMigrationRecoveryAction Action, |
| | 3 | 57 | | string? Message, |
| | 0 | 58 | | string? EncryptedTempPath, |
| | 5 | 59 | | string? PlainBackupPath) |
| | | 60 | | { |
| | 1 | 61 | | internal static PlainToEncryptedVaultMigrationRecoveryDecision None() => new(PlainToEncryptedVaultMigrationRecoveryAct |
| | | 62 | | |
| | | 63 | | internal static PlainToEncryptedVaultMigrationRecoveryDecision ResumeExport(string tempPath, string backupPath) |
| | 1 | 64 | | => new(PlainToEncryptedVaultMigrationRecoveryAction.ResumeExport, "Storage-Migration muss mit dem vorhandenen encryp |
| | | 65 | | |
| | | 66 | | internal static PlainToEncryptedVaultMigrationRecoveryDecision FinalizeBackupCleanup(string tempPath, string backupPat |
| | 1 | 67 | | => new(PlainToEncryptedVaultMigrationRecoveryAction.FinalizeBackupCleanup, "Storage-Migration hat die Ziel-Vault ers |
| | | 68 | | |
| | | 69 | | internal static PlainToEncryptedVaultMigrationRecoveryDecision Fail(string message, string tempPath, string backupPath |
| | 2 | 70 | | => new(PlainToEncryptedVaultMigrationRecoveryAction.Fail, message, tempPath, backupPath); |
| | | 71 | | } |