| | | 1 | | using LOCKnet.Core.DataAbstractions; |
| | | 2 | | using Microsoft.Data.Sqlite; |
| | | 3 | | using System.Runtime.InteropServices; |
| | | 4 | | |
| | | 5 | | namespace LOCKnet.Data.Repositories; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// SQLite-Implementierung von <see cref="IVaultMigrationRepository"/> fuer atomare Header- und Credential-Migrationen. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class VaultMigrationRepository : RepositoryBase, IVaultMigrationRepository |
| | | 11 | | { |
| | | 12 | | private readonly StorageRewriteHooks? _rewriteHooks; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Initialisiert eine neue Instanz von <see cref="VaultMigrationRepository"/>. |
| | | 16 | | /// </summary> |
| | 27 | 17 | | public VaultMigrationRepository(string connectionString) : this(connectionString, null) |
| | 27 | 18 | | { |
| | 27 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Initialisiert eine neue Instanz von <see cref="VaultMigrationRepository"/>. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="connectionFactory">Factory fuer Storage-spezifische SQLite-Verbindungen.</param> |
| | 21 | 25 | | public VaultMigrationRepository(ISqliteConnectionFactory connectionFactory) : this(connectionFactory, null) |
| | 21 | 26 | | { |
| | 21 | 27 | | } |
| | | 28 | | |
| | 31 | 29 | | internal VaultMigrationRepository(string connectionString, StorageRewriteHooks? rewriteHooks) : base(connectionString) |
| | 31 | 30 | | { |
| | 31 | 31 | | _rewriteHooks = rewriteHooks; |
| | 31 | 32 | | } |
| | | 33 | | |
| | 24 | 34 | | internal VaultMigrationRepository(ISqliteConnectionFactory connectionFactory, StorageRewriteHooks? rewriteHooks) : bas |
| | 24 | 35 | | { |
| | 24 | 36 | | _rewriteHooks = rewriteHooks; |
| | 24 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc/> |
| | | 40 | | public IReadOnlyList<CredentialRecord> GetAllCredentials() |
| | 19 | 41 | | { |
| | 19 | 42 | | var list = new List<CredentialRecord>(); |
| | 19 | 43 | | using var conn = GetConnection(); |
| | 19 | 44 | | using var cmd = conn.CreateCommand(); |
| | 19 | 45 | | cmd.CommandText = "SELECT Id, Title, Username, EncryptedPassword, EncryptedMetadata, CredentialUuid, SecretFormatVer |
| | | 46 | | |
| | 19 | 47 | | using var reader = cmd.ExecuteReader(); |
| | 24 | 48 | | while (reader.Read()) |
| | 5 | 49 | | list.Add(MapCredential(reader)); |
| | | 50 | | |
| | 19 | 51 | | return list; |
| | 19 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc/> |
| | | 55 | | public void ApplyMigration(VaultHeader header, IReadOnlyList<CredentialRecord> credentials) |
| | 4 | 56 | | { |
| | 4 | 57 | | ArgumentNullException.ThrowIfNull(header); |
| | 4 | 58 | | ArgumentNullException.ThrowIfNull(credentials); |
| | | 59 | | |
| | 19 | 60 | | foreach (var credential in credentials) |
| | 4 | 61 | | StoredCredentialGuard.ValidateForPersistence(credential); |
| | | 62 | | |
| | 3 | 63 | | using var conn = GetConnection(); |
| | 3 | 64 | | ConfigureMigrationConnection(conn); |
| | | 65 | | |
| | 3 | 66 | | var began = false; |
| | | 67 | | try |
| | 3 | 68 | | { |
| | 3 | 69 | | using (var begin = conn.CreateCommand()) |
| | 3 | 70 | | { |
| | 3 | 71 | | begin.CommandText = "BEGIN EXCLUSIVE;"; |
| | 3 | 72 | | begin.ExecuteNonQuery(); |
| | 3 | 73 | | began = true; |
| | 3 | 74 | | } |
| | | 75 | | |
| | 14 | 76 | | foreach (var credential in credentials) |
| | 3 | 77 | | { |
| | 3 | 78 | | using var updateCredential = conn.CreateCommand(); |
| | 3 | 79 | | updateCredential.CommandText = @" |
| | 3 | 80 | | UPDATE Credentials |
| | 3 | 81 | | SET Title = $title, |
| | 3 | 82 | | Username = $username, |
| | 3 | 83 | | EncryptedPassword = $password, |
| | 3 | 84 | | EncryptedMetadata = $encryptedMetadata, |
| | 3 | 85 | | CredentialUuid = $credentialUuid, |
| | 3 | 86 | | SecretFormatVersion = $secretFormatVersion, |
| | 3 | 87 | | MetadataFormatVersion = $metadataFormatVersion, |
| | 3 | 88 | | URL = $url, |
| | 3 | 89 | | Notes = $notes, |
| | 3 | 90 | | IconKey = $iconKey, |
| | 3 | 91 | | CredentialType = $credentialType, |
| | 3 | 92 | | UpdatedAt = CURRENT_TIMESTAMP |
| | 3 | 93 | | WHERE Id = $id;"; |
| | 3 | 94 | | updateCredential.Parameters.AddWithValue("$id", credential.Id); |
| | 3 | 95 | | updateCredential.Parameters.AddWithValue("$title", credential.Title); |
| | 3 | 96 | | updateCredential.Parameters.AddWithValue("$username", (object?)credential.Username ?? DBNull.Value); |
| | 3 | 97 | | updateCredential.Parameters.AddWithValue("$password", credential.EncryptedPassword); |
| | 3 | 98 | | updateCredential.Parameters.AddWithValue("$encryptedMetadata", (object?)credential.EncryptedMetadata ?? DBNull.V |
| | 3 | 99 | | updateCredential.Parameters.AddWithValue("$credentialUuid", credential.CredentialUuid); |
| | 3 | 100 | | updateCredential.Parameters.AddWithValue("$secretFormatVersion", credential.SecretFormatVersion); |
| | 3 | 101 | | updateCredential.Parameters.AddWithValue("$metadataFormatVersion", credential.MetadataFormatVersion); |
| | 3 | 102 | | updateCredential.Parameters.AddWithValue("$url", (object?)credential.Url ?? DBNull.Value); |
| | 3 | 103 | | updateCredential.Parameters.AddWithValue("$notes", (object?)credential.Notes ?? DBNull.Value); |
| | 3 | 104 | | updateCredential.Parameters.AddWithValue("$iconKey", (object?)credential.IconKey ?? DBNull.Value); |
| | 3 | 105 | | updateCredential.Parameters.AddWithValue("$credentialType", (int)credential.CredentialType); |
| | 3 | 106 | | updateCredential.ExecuteNonQuery(); |
| | 2 | 107 | | } |
| | | 108 | | |
| | 2 | 109 | | using (var updateHeader = conn.CreateCommand()) |
| | 2 | 110 | | { |
| | 2 | 111 | | updateHeader.CommandText = @" |
| | 2 | 112 | | UPDATE MasterKey |
| | 2 | 113 | | SET PasswordHash = $hash, |
| | 2 | 114 | | FormatVersion = $formatVersion, |
| | 2 | 115 | | KdfIdentifier = $kdfIdentifier, |
| | 2 | 116 | | KdfParameters = $kdfParameters, |
| | 2 | 117 | | Salt = $salt, |
| | 2 | 118 | | WrappedVaultKey = $wrappedVaultKey, |
| | 2 | 119 | | UsesLegacyKeyMaterial = $usesLegacyKeyMaterial, |
| | 2 | 120 | | RequiresStorageCompaction = $requiresStorageCompaction, |
| | 2 | 121 | | LastStorageCompactionAttemptUtc = $lastStorageCompactionAttemptUtc, |
| | 2 | 122 | | LastStorageCompactionFailureKind = $lastStorageCompactionFailureKind, |
| | 2 | 123 | | LastStorageCompactionError = $lastStorageCompactionError, |
| | 2 | 124 | | UpdatedAt = CURRENT_TIMESTAMP |
| | 2 | 125 | | WHERE Id = 1;"; |
| | 2 | 126 | | updateHeader.Parameters.AddWithValue("$hash", header.LegacyPasswordHash); |
| | 2 | 127 | | updateHeader.Parameters.AddWithValue("$formatVersion", header.FormatVersion); |
| | 2 | 128 | | updateHeader.Parameters.AddWithValue("$kdfIdentifier", header.KdfIdentifier); |
| | 2 | 129 | | updateHeader.Parameters.AddWithValue("$kdfParameters", header.KdfParameters.Serialize()); |
| | 2 | 130 | | updateHeader.Parameters.AddWithValue("$salt", header.Salt); |
| | 2 | 131 | | updateHeader.Parameters.AddWithValue("$wrappedVaultKey", header.WrappedVaultKey); |
| | 2 | 132 | | updateHeader.Parameters.AddWithValue("$usesLegacyKeyMaterial", header.UsesLegacyKeyMaterial ? 1 : 0); |
| | 2 | 133 | | updateHeader.Parameters.AddWithValue("$requiresStorageCompaction", header.RequiresStorageCompaction ? 1 : 0); |
| | 2 | 134 | | updateHeader.Parameters.AddWithValue("$lastStorageCompactionAttemptUtc", (object?)header.LastStorageCompactionAt |
| | 2 | 135 | | updateHeader.Parameters.AddWithValue("$lastStorageCompactionFailureKind", (int)header.LastStorageCompactionFailu |
| | 2 | 136 | | updateHeader.Parameters.AddWithValue("$lastStorageCompactionError", (object?)header.LastStorageCompactionError ? |
| | 2 | 137 | | updateHeader.ExecuteNonQuery(); |
| | 2 | 138 | | } |
| | | 139 | | |
| | 2 | 140 | | using var commit = conn.CreateCommand(); |
| | 2 | 141 | | commit.CommandText = "COMMIT;"; |
| | 2 | 142 | | commit.ExecuteNonQuery(); |
| | 2 | 143 | | began = false; |
| | 2 | 144 | | } |
| | 1 | 145 | | catch |
| | 1 | 146 | | { |
| | 1 | 147 | | if (began) |
| | 1 | 148 | | { |
| | | 149 | | try |
| | 1 | 150 | | { |
| | 1 | 151 | | using var rollback = conn.CreateCommand(); |
| | 1 | 152 | | rollback.CommandText = "ROLLBACK;"; |
| | 1 | 153 | | rollback.ExecuteNonQuery(); |
| | 1 | 154 | | } |
| | 0 | 155 | | catch (SqliteException) |
| | 0 | 156 | | { |
| | 0 | 157 | | } |
| | 1 | 158 | | } |
| | | 159 | | |
| | 1 | 160 | | throw; |
| | | 161 | | } |
| | 4 | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <inheritdoc/> |
| | 2 | 165 | | public bool HasPendingStorageArtifacts() => StorageRewriteArtifacts.HasPendingArtifacts(_databasePath); |
| | | 166 | | |
| | | 167 | | /// <inheritdoc/> |
| | | 168 | | public StorageCompactionInfo CompactStorage() |
| | 14 | 169 | | { |
| | | 170 | | try |
| | 14 | 171 | | { |
| | 14 | 172 | | if (_databasePath is null) |
| | 1 | 173 | | { |
| | 1 | 174 | | return new StorageCompactionInfo |
| | 1 | 175 | | { |
| | 1 | 176 | | IsPending = true, |
| | 1 | 177 | | FailureKind = StorageCompactionFailureKind.Unknown, |
| | 1 | 178 | | UserMessage = "Speicherbereinigung noch offen: Fuer diese SQLite-Verbindung steht kein dateibasierter Rewrite |
| | 1 | 179 | | LastError = "Dateibasierter Rewrite ist fuer nicht-dateibasierte SQLite-Verbindungen nicht verfuegbar." |
| | 1 | 180 | | }; |
| | | 181 | | } |
| | | 182 | | |
| | 13 | 183 | | var primaryPath = _databasePath; |
| | 13 | 184 | | var tempPath = StorageRewriteArtifacts.GetTempPath(primaryPath); |
| | 13 | 185 | | var backupPath = StorageRewriteArtifacts.GetBackupPath(primaryPath); |
| | | 186 | | |
| | 13 | 187 | | var artifactFinalization = TryFinalizeExistingArtifacts(primaryPath, tempPath, backupPath); |
| | 13 | 188 | | if (artifactFinalization is not null) |
| | 3 | 189 | | return artifactFinalization; |
| | | 190 | | |
| | 10 | 191 | | if (File.Exists(tempPath) && !StorageRewriteArtifacts.TryDeleteFile(tempPath)) |
| | 0 | 192 | | { |
| | 0 | 193 | | return new StorageCompactionInfo |
| | 0 | 194 | | { |
| | 0 | 195 | | IsPending = true, |
| | 0 | 196 | | FailureKind = StorageCompactionFailureKind.BusyOrLocked, |
| | 0 | 197 | | UserMessage = "Speicherbereinigung noch offen: Ein altes Rewrite-Artefakt konnte nicht entfernt werden.", |
| | 0 | 198 | | LastError = $"Rewrite-Tempdatei konnte nicht entfernt werden: {tempPath}" |
| | 0 | 199 | | }; |
| | | 200 | | } |
| | | 201 | | |
| | 10 | 202 | | if (File.Exists(backupPath) && !StorageRewriteArtifacts.TryDeleteFile(backupPath)) |
| | 0 | 203 | | { |
| | 0 | 204 | | return new StorageCompactionInfo |
| | 0 | 205 | | { |
| | 0 | 206 | | IsPending = true, |
| | 0 | 207 | | FailureKind = StorageCompactionFailureKind.BusyOrLocked, |
| | 0 | 208 | | UserMessage = "Speicherbereinigung noch offen: Eine alte Rewrite-Sicherung blockiert einen neuen Bereinigungsv |
| | 0 | 209 | | LastError = $"Rewrite-Sicherung konnte nicht entfernt werden: {backupPath}" |
| | 0 | 210 | | }; |
| | | 211 | | } |
| | | 212 | | |
| | 10 | 213 | | _rewriteHooks?.BeforeVacuumInto?.Invoke(tempPath); |
| | 6 | 214 | | BuildRewriteCandidate(tempPath); |
| | 5 | 215 | | VerifyRewriteCandidate(tempPath); |
| | 4 | 216 | | _rewriteHooks?.AfterVacuumInto?.Invoke(tempPath); |
| | | 217 | | |
| | 3 | 218 | | StorageRewriteArtifacts.ReplacePrimaryDatabase(tempPath, primaryPath, backupPath); |
| | 3 | 219 | | _rewriteHooks?.AfterReplace?.Invoke(primaryPath, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? backupPath |
| | | 220 | | |
| | 3 | 221 | | if (File.Exists(backupPath) && !StorageRewriteArtifacts.TryDeleteFile(backupPath)) |
| | 0 | 222 | | { |
| | 0 | 223 | | return new StorageCompactionInfo |
| | 0 | 224 | | { |
| | 0 | 225 | | IsPending = true, |
| | 0 | 226 | | FailureKind = StorageCompactionFailureKind.BusyOrLocked, |
| | 0 | 227 | | UserMessage = "Speicherbereinigung noch offen: Die alte Vault-Datei konnte nach dem Rewrite noch nicht entfern |
| | 0 | 228 | | LastError = $"Rewrite-Sicherung konnte nach dem Austausch nicht entfernt werden: {backupPath}" |
| | 0 | 229 | | }; |
| | | 230 | | } |
| | | 231 | | |
| | 3 | 232 | | if (File.Exists(tempPath) && !StorageRewriteArtifacts.TryDeleteFile(tempPath)) |
| | 0 | 233 | | { |
| | 0 | 234 | | return new StorageCompactionInfo |
| | 0 | 235 | | { |
| | 0 | 236 | | IsPending = true, |
| | 0 | 237 | | FailureKind = StorageCompactionFailureKind.BusyOrLocked, |
| | 0 | 238 | | UserMessage = "Speicherbereinigung noch offen: Das temporare Rewrite-Artefakt konnte nach dem Austausch nicht |
| | 0 | 239 | | LastError = $"Rewrite-Tempdatei konnte nach dem Austausch nicht entfernt werden: {tempPath}" |
| | 0 | 240 | | }; |
| | | 241 | | } |
| | | 242 | | |
| | 3 | 243 | | return new StorageCompactionInfo |
| | 3 | 244 | | { |
| | 3 | 245 | | IsPending = false, |
| | 3 | 246 | | FailureKind = StorageCompactionFailureKind.None, |
| | 3 | 247 | | UserMessage = "Speicherbereinigung durch Rewrite abgeschlossen.", |
| | 3 | 248 | | }; |
| | | 249 | | } |
| | 2 | 250 | | catch (SqliteException ex) |
| | 2 | 251 | | { |
| | 2 | 252 | | var (failureKind, userMessage) = MapCompactionFailure(ex); |
| | 2 | 253 | | return new StorageCompactionInfo |
| | 2 | 254 | | { |
| | 2 | 255 | | IsPending = true, |
| | 2 | 256 | | FailureKind = failureKind, |
| | 2 | 257 | | UserMessage = userMessage, |
| | 2 | 258 | | LastError = ex.Message, |
| | 2 | 259 | | }; |
| | | 260 | | } |
| | 2 | 261 | | catch (InvalidOperationException ex) |
| | 2 | 262 | | { |
| | 2 | 263 | | return new StorageCompactionInfo |
| | 2 | 264 | | { |
| | 2 | 265 | | IsPending = true, |
| | 2 | 266 | | FailureKind = StorageCompactionFailureKind.Corruption, |
| | 2 | 267 | | UserMessage = "Speicherbereinigung noch offen: Die neu geschriebene Vault-Datei ist inkonsistent. Backup pruefen |
| | 2 | 268 | | LastError = ex.Message, |
| | 2 | 269 | | }; |
| | | 270 | | } |
| | 2 | 271 | | catch (IOException ex) |
| | 2 | 272 | | { |
| | 2 | 273 | | return new StorageCompactionInfo |
| | 2 | 274 | | { |
| | 2 | 275 | | IsPending = true, |
| | 2 | 276 | | FailureKind = StorageCompactionFailureKind.Io, |
| | 2 | 277 | | UserMessage = "Speicherbereinigung noch offen: Die Vault-Datei konnte nicht sicher neu geschrieben oder ersetzt |
| | 2 | 278 | | LastError = ex.Message, |
| | 2 | 279 | | }; |
| | | 280 | | } |
| | 1 | 281 | | catch (UnauthorizedAccessException ex) |
| | 1 | 282 | | { |
| | 1 | 283 | | return new StorageCompactionInfo |
| | 1 | 284 | | { |
| | 1 | 285 | | IsPending = true, |
| | 1 | 286 | | FailureKind = StorageCompactionFailureKind.BusyOrLocked, |
| | 1 | 287 | | UserMessage = "Speicherbereinigung noch offen: Die Vault-Datei ist noch gesperrt oder nicht schreibbar.", |
| | 1 | 288 | | LastError = ex.Message, |
| | 1 | 289 | | }; |
| | | 290 | | } |
| | 14 | 291 | | } |
| | | 292 | | |
| | | 293 | | private static (StorageCompactionFailureKind failureKind, string userMessage) MapCompactionFailure(SqliteException ex) |
| | 2 | 294 | | => ex.SqliteErrorCode switch |
| | 2 | 295 | | { |
| | 0 | 296 | | 5 or 6 => (StorageCompactionFailureKind.BusyOrLocked, "Speicherbereinigung noch offen: Die Vault-Datei ist gerade |
| | 0 | 297 | | 10 => (StorageCompactionFailureKind.Io, "Speicherbereinigung noch offen: Beim Rewrite der Vault-Datei ist ein I/O- |
| | 1 | 298 | | 11 or 26 => (StorageCompactionFailureKind.Corruption, "Speicherbereinigung noch offen: Die Datenbank meldet Integr |
| | 0 | 299 | | 13 => (StorageCompactionFailureKind.InsufficientSpace, "Speicherbereinigung noch offen: Fuer den Rewrite ist nicht |
| | 1 | 300 | | _ => (StorageCompactionFailureKind.Unknown, "Speicherbereinigung noch offen: SQLite konnte den Rewrite nicht absch |
| | 2 | 301 | | }; |
| | | 302 | | |
| | | 303 | | private StorageCompactionInfo? TryFinalizeExistingArtifacts(string primaryPath, string tempPath, string backupPath) |
| | 13 | 304 | | { |
| | 13 | 305 | | var mainValid = StorageRewriteArtifacts.IsUsableSqliteDatabase(primaryPath); |
| | | 306 | | |
| | 13 | 307 | | if (File.Exists(backupPath)) |
| | 3 | 308 | | { |
| | 3 | 309 | | if (!mainValid) |
| | 1 | 310 | | { |
| | 1 | 311 | | return new StorageCompactionInfo |
| | 1 | 312 | | { |
| | 1 | 313 | | IsPending = true, |
| | 1 | 314 | | FailureKind = StorageCompactionFailureKind.Corruption, |
| | 1 | 315 | | UserMessage = "Speicherbereinigung noch offen: Vorhandene Rewrite-Artefakte muessen beim Neustart wiederherges |
| | 1 | 316 | | LastError = "Rewrite-Sicherung vorhanden, aber die Hauptdatenbank ist momentan nicht gueltig." |
| | 1 | 317 | | }; |
| | | 318 | | } |
| | | 319 | | |
| | 2 | 320 | | if (!StorageRewriteArtifacts.TryDeleteFile(backupPath)) |
| | 0 | 321 | | { |
| | 0 | 322 | | return new StorageCompactionInfo |
| | 0 | 323 | | { |
| | 0 | 324 | | IsPending = true, |
| | 0 | 325 | | FailureKind = StorageCompactionFailureKind.BusyOrLocked, |
| | 0 | 326 | | UserMessage = "Speicherbereinigung noch offen: Die alte Rewrite-Sicherung konnte noch nicht entfernt werden.", |
| | 0 | 327 | | LastError = $"Rewrite-Sicherung konnte nicht entfernt werden: {backupPath}" |
| | 0 | 328 | | }; |
| | | 329 | | } |
| | | 330 | | |
| | 2 | 331 | | if (File.Exists(tempPath)) |
| | 1 | 332 | | StorageRewriteArtifacts.TryDeleteFile(tempPath); |
| | | 333 | | |
| | 2 | 334 | | return new StorageCompactionInfo |
| | 2 | 335 | | { |
| | 2 | 336 | | IsPending = false, |
| | 2 | 337 | | FailureKind = StorageCompactionFailureKind.None, |
| | 2 | 338 | | UserMessage = "Speicherbereinigung abgeschlossen.", |
| | 2 | 339 | | }; |
| | | 340 | | } |
| | | 341 | | |
| | 10 | 342 | | if (File.Exists(tempPath) && mainValid) |
| | 0 | 343 | | { |
| | 0 | 344 | | if (!StorageRewriteArtifacts.TryDeleteFile(tempPath)) |
| | 0 | 345 | | { |
| | 0 | 346 | | return new StorageCompactionInfo |
| | 0 | 347 | | { |
| | 0 | 348 | | IsPending = true, |
| | 0 | 349 | | FailureKind = StorageCompactionFailureKind.BusyOrLocked, |
| | 0 | 350 | | UserMessage = "Speicherbereinigung noch offen: Ein unvollstaendiges Rewrite-Artefakt konnte noch nicht entfern |
| | 0 | 351 | | LastError = $"Rewrite-Tempdatei konnte nicht entfernt werden: {tempPath}" |
| | 0 | 352 | | }; |
| | | 353 | | } |
| | 0 | 354 | | } |
| | | 355 | | |
| | 10 | 356 | | return null; |
| | 13 | 357 | | } |
| | | 358 | | |
| | | 359 | | private void BuildRewriteCandidate(string tempPath) |
| | 6 | 360 | | { |
| | 6 | 361 | | using var conn = GetConnection(); |
| | 5 | 362 | | ConfigureMigrationConnection(conn); |
| | | 363 | | |
| | 5 | 364 | | using (var checkpoint = conn.CreateCommand()) |
| | 5 | 365 | | { |
| | 5 | 366 | | checkpoint.CommandText = "PRAGMA wal_checkpoint(TRUNCATE);"; |
| | 5 | 367 | | checkpoint.ExecuteNonQuery(); |
| | 5 | 368 | | } |
| | | 369 | | |
| | 5 | 370 | | using var cmd = conn.CreateCommand(); |
| | 5 | 371 | | cmd.CommandText = $"VACUUM INTO {ToSqliteStringLiteral(tempPath)};"; |
| | 5 | 372 | | cmd.ExecuteNonQuery(); |
| | 10 | 373 | | } |
| | | 374 | | |
| | | 375 | | private static void VerifyRewriteCandidate(string tempPath) |
| | 5 | 376 | | { |
| | 5 | 377 | | if (!StorageRewriteArtifacts.IsUsableSqliteDatabase(tempPath)) |
| | 0 | 378 | | throw new InvalidOperationException("Rewrite-Zieldatei ist keine verwendbare SQLite-Datenbank."); |
| | | 379 | | |
| | 5 | 380 | | var builder = new SqliteConnectionStringBuilder |
| | 5 | 381 | | { |
| | 5 | 382 | | DataSource = tempPath, |
| | 5 | 383 | | Mode = SqliteOpenMode.ReadOnly, |
| | 5 | 384 | | }; |
| | | 385 | | |
| | 5 | 386 | | using var connection = new SqliteConnection(builder.ToString()); |
| | 5 | 387 | | connection.Open(); |
| | | 388 | | |
| | 5 | 389 | | using var masterKeyCount = connection.CreateCommand(); |
| | 5 | 390 | | masterKeyCount.CommandText = "SELECT COUNT(*) FROM MasterKey;"; |
| | 5 | 391 | | if (Convert.ToInt64(masterKeyCount.ExecuteScalar() ?? 0L) != 1) |
| | 1 | 392 | | throw new InvalidOperationException("Rewrite-Zieldatei enthaelt keinen konsistenten MasterKey-Header."); |
| | 8 | 393 | | } |
| | | 394 | | |
| | | 395 | | private static void ConfigureMigrationConnection(SqliteConnection conn) |
| | 8 | 396 | | { |
| | 8 | 397 | | using var cmd = conn.CreateCommand(); |
| | 8 | 398 | | cmd.CommandText = @" |
| | 8 | 399 | | PRAGMA journal_mode = DELETE; |
| | 8 | 400 | | PRAGMA synchronous = FULL; |
| | 8 | 401 | | PRAGMA locking_mode = EXCLUSIVE; |
| | 8 | 402 | | PRAGMA busy_timeout = 5000;"; |
| | 8 | 403 | | cmd.ExecuteNonQuery(); |
| | 16 | 404 | | } |
| | | 405 | | |
| | 5 | 406 | | private static string ToSqliteStringLiteral(string value) => $"'{value.Replace("'", "''")}'"; |
| | | 407 | | |
| | 5 | 408 | | private static CredentialRecord MapCredential(SqliteDataReader reader) => new() |
| | 5 | 409 | | { |
| | 5 | 410 | | Id = reader.GetInt32(0), |
| | 5 | 411 | | Title = reader.GetString(1), |
| | 5 | 412 | | Username = reader.IsDBNull(2) ? null : reader.GetString(2), |
| | 5 | 413 | | EncryptedPassword = (byte[])reader[3], |
| | 5 | 414 | | EncryptedMetadata = reader.IsDBNull(4) ? [] : (byte[])reader[4], |
| | 5 | 415 | | CredentialUuid = reader.IsDBNull(5) ? string.Empty : reader.GetString(5), |
| | 5 | 416 | | SecretFormatVersion = reader.IsDBNull(6) ? CredentialSecretFormatVersion.Legacy : reader.GetInt32(6), |
| | 5 | 417 | | MetadataFormatVersion = reader.IsDBNull(7) ? CredentialMetadataFormatVersion.Legacy : reader.GetInt32(7), |
| | 5 | 418 | | Url = reader.IsDBNull(8) ? null : reader.GetString(8), |
| | 5 | 419 | | Notes = reader.IsDBNull(9) ? null : reader.GetString(9), |
| | 5 | 420 | | CreatedAt = reader.GetDateTime(10), |
| | 5 | 421 | | UpdatedAt = reader.GetDateTime(11), |
| | 5 | 422 | | IconKey = reader.IsDBNull(12) ? null : reader.GetString(12), |
| | 5 | 423 | | CredentialType = reader.IsDBNull(13) ? CredentialType.Password : (CredentialType)reader.GetInt32(13), |
| | 5 | 424 | | }; |
| | | 425 | | } |
| | | 426 | | |
| | | 427 | | internal sealed class StorageRewriteHooks |
| | | 428 | | { |
| | | 429 | | public Action<string>? BeforeVacuumInto { get; init; } |
| | | 430 | | public Action<string>? AfterVacuumInto { get; init; } |
| | | 431 | | public Action<string, string?>? AfterReplace { get; init; } |
| | | 432 | | } |