| 1 | namespace Cage.Windows; |
| 2 | |
| 3 | internal static class UpdateRecovery |
| 4 | { |
| 5 | public static string BackendPath(string backup) => Path.Combine(backup, "resources", "cage.exe"); |
| 6 | |
| 7 | |
| 8 | |
| 9 | public static async Task PrepareRollback(string backup, CancellationToken cancellation = default) |
| 10 | { |
| 11 | var (exit, result) = await Backend.JsonAt(BackendPath(backup), ["prepare-update", "--json", "--stop"], cancellation); |
| 12 | if (exit != 0 || result.Text("status") != "prepared") |
| 13 | throw new IOException("旧版后台无法确认环境已停止,原应用备份已保留"); |
| 14 | } |
| 15 | |
| 16 | public static void CleanupDirectory(string? path) |
| 17 | { |
| 18 | if (path == null) return; |
| 19 | try { Directory.Delete(path, true); } |
| 20 | catch (IOException) { } |
| 21 | catch (UnauthorizedAccessException) { } |
| 22 | } |
| 23 | |
| 24 | |
| 25 | |
| 26 | public static void CleanupAfterSuccess(string backup, string download, string marker) |
| 27 | { |
| 28 | CleanupDirectory(backup); |
| 29 | CleanupDirectory(download); |
| 30 | try { File.Delete(marker); } |
| 31 | catch (IOException) { } |
| 32 | catch (UnauthorizedAccessException) { } |
| 33 | } |
| 34 | } |