| | | 1 | | using Microsoft.Data.Sqlite; |
| | | 2 | | using System.Runtime.InteropServices; |
| | | 3 | | |
| | | 4 | | namespace LOCKnet.Data; |
| | | 5 | | |
| | | 6 | | internal static class StorageRewriteArtifacts |
| | | 7 | | { |
| | | 8 | | internal const string RewriteTempSuffix = ".rewrite.tmp"; |
| | | 9 | | internal const string RewriteBackupSuffix = ".rewrite.bak"; |
| | | 10 | | |
| | | 11 | | internal static string? TryResolveDatabasePath(string connectionString) |
| | 206 | 12 | | { |
| | 206 | 13 | | if (string.IsNullOrWhiteSpace(connectionString)) |
| | 1 | 14 | | return null; |
| | | 15 | | |
| | 205 | 16 | | var builder = new SqliteConnectionStringBuilder(connectionString); |
| | 205 | 17 | | if (builder.Mode == SqliteOpenMode.Memory) |
| | 146 | 18 | | return null; |
| | | 19 | | |
| | 59 | 20 | | if (string.IsNullOrWhiteSpace(builder.DataSource) || builder.DataSource == ":memory:") |
| | 2 | 21 | | return null; |
| | | 22 | | |
| | 57 | 23 | | return Path.GetFullPath(builder.DataSource); |
| | 206 | 24 | | } |
| | | 25 | | |
| | 123 | 26 | | internal static string GetTempPath(string databasePath) => databasePath + RewriteTempSuffix; |
| | | 27 | | |
| | 122 | 28 | | internal static string GetBackupPath(string databasePath) => databasePath + RewriteBackupSuffix; |
| | | 29 | | |
| | | 30 | | internal static bool HasPendingArtifacts(string? databasePath) |
| | 2 | 31 | | => databasePath is not null && |
| | 2 | 32 | | (File.Exists(GetTempPath(databasePath)) || File.Exists(GetBackupPath(databasePath))); |
| | | 33 | | |
| | | 34 | | internal static StartupRecoveryOutcome Recover(string? databasePath) |
| | 163 | 35 | | { |
| | 163 | 36 | | if (string.IsNullOrWhiteSpace(databasePath)) |
| | 66 | 37 | | return default; |
| | | 38 | | |
| | 97 | 39 | | var primaryPath = Path.GetFullPath(databasePath); |
| | 97 | 40 | | var tempPath = GetTempPath(primaryPath); |
| | 97 | 41 | | var backupPath = GetBackupPath(primaryPath); |
| | 97 | 42 | | var shouldClearPendingState = false; |
| | | 43 | | |
| | 97 | 44 | | var mainExists = File.Exists(primaryPath); |
| | 97 | 45 | | var backupExists = File.Exists(backupPath); |
| | 97 | 46 | | var tempExists = File.Exists(tempPath); |
| | 97 | 47 | | var mainValid = mainExists && IsUsableSqliteDatabase(primaryPath); |
| | 97 | 48 | | var backupValid = backupExists && IsUsableSqliteDatabase(backupPath); |
| | 97 | 49 | | var tempValid = tempExists && IsUsableSqliteDatabase(tempPath); |
| | | 50 | | |
| | 97 | 51 | | if ((!mainExists || !mainValid) && backupValid) |
| | 1 | 52 | | { |
| | 1 | 53 | | RestoreFile(backupPath, primaryPath); |
| | 1 | 54 | | mainExists = true; |
| | 1 | 55 | | mainValid = true; |
| | 1 | 56 | | backupExists = false; |
| | 1 | 57 | | backupValid = false; |
| | 1 | 58 | | } |
| | 96 | 59 | | else if ((!mainExists || !mainValid) && !backupExists && tempValid) |
| | 1 | 60 | | { |
| | 1 | 61 | | PromoteFile(tempPath, primaryPath); |
| | 1 | 62 | | mainExists = true; |
| | 1 | 63 | | mainValid = true; |
| | 1 | 64 | | tempExists = false; |
| | 1 | 65 | | tempValid = false; |
| | 1 | 66 | | } |
| | | 67 | | |
| | 97 | 68 | | if (!mainExists && (backupExists || tempExists)) |
| | 1 | 69 | | throw new InvalidOperationException("Vault-Rewrite-Artefakte gefunden, aber keine verwendbare Hauptdatenbank konnt |
| | | 70 | | |
| | 96 | 71 | | if (mainExists && !mainValid && (backupExists || tempExists)) |
| | 1 | 72 | | throw new InvalidOperationException("Vault-Datei ist nach einer unterbrochenen Rewrite-Bereinigung ungueltig. Back |
| | | 73 | | |
| | 95 | 74 | | if (mainValid && backupExists) |
| | 1 | 75 | | { |
| | 1 | 76 | | if (TryDeleteFile(backupPath)) |
| | 1 | 77 | | shouldClearPendingState = true; |
| | 1 | 78 | | } |
| | | 79 | | |
| | 95 | 80 | | if (mainValid && tempExists) |
| | 2 | 81 | | TryDeleteFile(tempPath); |
| | | 82 | | |
| | 95 | 83 | | return new StartupRecoveryOutcome(shouldClearPendingState); |
| | 161 | 84 | | } |
| | | 85 | | |
| | | 86 | | internal static void ClearPendingState(SqliteConnection connection) |
| | 2 | 87 | | { |
| | 2 | 88 | | ArgumentNullException.ThrowIfNull(connection); |
| | | 89 | | |
| | 2 | 90 | | using var command = connection.CreateCommand(); |
| | 2 | 91 | | command.CommandText = @" |
| | 2 | 92 | | UPDATE MasterKey |
| | 2 | 93 | | SET RequiresStorageCompaction = 0, |
| | 2 | 94 | | LastStorageCompactionAttemptUtc = NULL, |
| | 2 | 95 | | LastStorageCompactionFailureKind = 0, |
| | 2 | 96 | | LastStorageCompactionError = NULL, |
| | 2 | 97 | | UpdatedAt = CURRENT_TIMESTAMP |
| | 2 | 98 | | WHERE Id = 1;"; |
| | 2 | 99 | | command.ExecuteNonQuery(); |
| | 4 | 100 | | } |
| | | 101 | | |
| | | 102 | | internal static bool IsUsableSqliteDatabase(string databasePath) |
| | 38 | 103 | | { |
| | 38 | 104 | | if (!File.Exists(databasePath)) |
| | 1 | 105 | | return false; |
| | | 106 | | |
| | | 107 | | try |
| | 37 | 108 | | { |
| | 37 | 109 | | var builder = new SqliteConnectionStringBuilder |
| | 37 | 110 | | { |
| | 37 | 111 | | DataSource = databasePath, |
| | 37 | 112 | | Mode = SqliteOpenMode.ReadOnly, |
| | 37 | 113 | | }; |
| | | 114 | | |
| | 37 | 115 | | using var connection = new SqliteConnection(builder.ToString()); |
| | 37 | 116 | | connection.Open(); |
| | | 117 | | |
| | 37 | 118 | | using var quickCheck = connection.CreateCommand(); |
| | 37 | 119 | | quickCheck.CommandText = "PRAGMA quick_check(1);"; |
| | 37 | 120 | | var quickCheckResult = Convert.ToString(quickCheck.ExecuteScalar()) ?? string.Empty; |
| | 28 | 121 | | if (!string.Equals(quickCheckResult, "ok", StringComparison.OrdinalIgnoreCase)) |
| | 0 | 122 | | return false; |
| | | 123 | | |
| | 28 | 124 | | using var tableCheck = connection.CreateCommand(); |
| | 28 | 125 | | tableCheck.CommandText = "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='MasterKey';"; |
| | 28 | 126 | | return Convert.ToInt64(tableCheck.ExecuteScalar() ?? 0L) > 0; |
| | | 127 | | } |
| | 9 | 128 | | catch (SqliteException) |
| | 9 | 129 | | { |
| | 9 | 130 | | return false; |
| | | 131 | | } |
| | 0 | 132 | | catch (IOException) |
| | 0 | 133 | | { |
| | 0 | 134 | | return false; |
| | | 135 | | } |
| | 38 | 136 | | } |
| | | 137 | | |
| | | 138 | | internal static void ReplacePrimaryDatabase(string tempPath, string primaryPath, string backupPath) |
| | 8 | 139 | | { |
| | 16 | 140 | | for (var attempt = 0; attempt < 120; attempt++) |
| | 8 | 141 | | { |
| | | 142 | | try |
| | 8 | 143 | | { |
| | 8 | 144 | | SqliteConnection.ClearAllPools(); |
| | 8 | 145 | | Thread.Sleep(25); |
| | | 146 | | |
| | 8 | 147 | | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && File.Exists(primaryPath)) |
| | 0 | 148 | | { |
| | 0 | 149 | | File.Replace(tempPath, primaryPath, backupPath, ignoreMetadataErrors: true); |
| | 0 | 150 | | } |
| | | 151 | | else |
| | 8 | 152 | | { |
| | 8 | 153 | | File.Move(tempPath, primaryPath, overwrite: true); |
| | 8 | 154 | | } |
| | | 155 | | |
| | 8 | 156 | | return; |
| | | 157 | | } |
| | 0 | 158 | | catch (IOException) when (attempt < 119) |
| | 0 | 159 | | { |
| | 0 | 160 | | Thread.Sleep(25); |
| | 0 | 161 | | } |
| | 0 | 162 | | catch (UnauthorizedAccessException) when (attempt < 119) |
| | 0 | 163 | | { |
| | 0 | 164 | | Thread.Sleep(25); |
| | 0 | 165 | | } |
| | 0 | 166 | | } |
| | | 167 | | |
| | 0 | 168 | | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && File.Exists(primaryPath)) |
| | 0 | 169 | | { |
| | 0 | 170 | | SqliteConnection.ClearAllPools(); |
| | 0 | 171 | | File.Replace(tempPath, primaryPath, backupPath, ignoreMetadataErrors: true); |
| | 0 | 172 | | return; |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | SqliteConnection.ClearAllPools(); |
| | 0 | 176 | | File.Move(tempPath, primaryPath, overwrite: true); |
| | 8 | 177 | | } |
| | | 178 | | |
| | | 179 | | private static void RestoreFile(string sourcePath, string destinationPath) |
| | 1 | 180 | | { |
| | 1 | 181 | | MoveFileWithRetry(sourcePath, destinationPath, deleteDestinationIfPresent: true); |
| | 1 | 182 | | } |
| | | 183 | | |
| | | 184 | | private static void PromoteFile(string sourcePath, string destinationPath) |
| | 1 | 185 | | { |
| | 1 | 186 | | MoveFileWithRetry(sourcePath, destinationPath, deleteDestinationIfPresent: true); |
| | 1 | 187 | | } |
| | | 188 | | |
| | | 189 | | internal static bool TryDeleteFile(string path) |
| | 46 | 190 | | { |
| | 92 | 191 | | for (var attempt = 0; attempt < 120; attempt++) |
| | 46 | 192 | | { |
| | | 193 | | try |
| | 46 | 194 | | { |
| | 46 | 195 | | SqliteConnection.ClearAllPools(); |
| | 46 | 196 | | Thread.Sleep(25); |
| | | 197 | | |
| | 46 | 198 | | if (File.Exists(path)) |
| | 24 | 199 | | File.Delete(path); |
| | | 200 | | |
| | 46 | 201 | | if (!File.Exists(path)) |
| | 46 | 202 | | return true; |
| | 0 | 203 | | } |
| | 0 | 204 | | catch (IOException) |
| | 0 | 205 | | { |
| | 0 | 206 | | } |
| | 0 | 207 | | catch (UnauthorizedAccessException) |
| | 0 | 208 | | { |
| | 0 | 209 | | } |
| | | 210 | | |
| | 0 | 211 | | Thread.Sleep(25); |
| | 0 | 212 | | } |
| | | 213 | | |
| | 0 | 214 | | return !File.Exists(path); |
| | 46 | 215 | | } |
| | | 216 | | |
| | | 217 | | private static void MoveFileWithRetry(string sourcePath, string destinationPath, bool deleteDestinationIfPresent) |
| | 2 | 218 | | { |
| | 4 | 219 | | for (var attempt = 0; attempt < 120; attempt++) |
| | 2 | 220 | | { |
| | | 221 | | try |
| | 2 | 222 | | { |
| | 2 | 223 | | SqliteConnection.ClearAllPools(); |
| | 2 | 224 | | Thread.Sleep(25); |
| | | 225 | | |
| | 2 | 226 | | if (deleteDestinationIfPresent && File.Exists(destinationPath) && !TryDeleteFile(destinationPath)) |
| | 0 | 227 | | throw new IOException($"Zieldatei konnte nicht entfernt werden: {destinationPath}"); |
| | | 228 | | |
| | 2 | 229 | | File.Move(sourcePath, destinationPath); |
| | 2 | 230 | | return; |
| | | 231 | | } |
| | 0 | 232 | | catch (IOException) when (attempt < 119) |
| | 0 | 233 | | { |
| | 0 | 234 | | Thread.Sleep(25); |
| | 0 | 235 | | } |
| | 0 | 236 | | catch (UnauthorizedAccessException) when (attempt < 119) |
| | 0 | 237 | | { |
| | 0 | 238 | | Thread.Sleep(25); |
| | 0 | 239 | | } |
| | 0 | 240 | | } |
| | | 241 | | |
| | 0 | 242 | | if (deleteDestinationIfPresent && File.Exists(destinationPath) && !TryDeleteFile(destinationPath)) |
| | 0 | 243 | | throw new IOException($"Zieldatei konnte nicht entfernt werden: {destinationPath}"); |
| | | 244 | | |
| | 0 | 245 | | SqliteConnection.ClearAllPools(); |
| | 0 | 246 | | File.Move(sourcePath, destinationPath); |
| | 2 | 247 | | } |
| | | 248 | | |
| | 160 | 249 | | internal readonly record struct StartupRecoveryOutcome(bool ShouldClearPendingState); |
| | | 250 | | } |