| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | |
| 8 | "reasonix/internal/session" |
| 9 | ) |
| 10 | |
| 11 | func historicalReconciliationCheckpoint(source historicalSource, migration desktopMigrationSource) (desktopMigrationCheckpoint, error) { |
| 12 | if source.format == "canonical" { |
| 13 | return newDesktopMigrationCheckpoint(migration, desktopCanonicalMigrationKey(filepath.Dir(source.path), filepath.Base(source.path)), canonicalMigrationSourceFiles(filepath.Dir(source.path), filepath.Base(source.path))) |
| 14 | } |
| 15 | key := desktopLegacyMigrationKey(source.path) |
| 16 | if source.head != "" { |
| 17 | key = desktopLegacyHeadKey(source.path, source.head) |
| 18 | } |
| 19 | return newDesktopMigrationCheckpoint(migration, key, desktopLegacyMigrationFiles(source.path, migration)) |
| 20 | } |
| 21 | |
| 22 | func historicalReconciliationReceipts(ctx context.Context, source historicalSource) ([]desktopMigrationReceipt, error) { |
| 23 | if source.format == "canonical" { |
| 24 | return canonicalConversionReceipts(ctx, source.path) |
| 25 | } |
| 26 | ledger, err := readDesktopMigrationLedger() |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | key := desktopLegacyMigrationKey(source.path) |
| 31 | if source.head != "" { |
| 32 | key = desktopLegacyHeadKey(source.path, source.head) |
| 33 | } |
| 34 | receipts := []desktopMigrationReceipt{} |
| 35 | for id, record := range ledger.Records { |
| 36 | if !historicalSourceKeyMatches(id, key) { |
| 37 | continue |
| 38 | } |
| 39 | if record.Status == "completed" && record.TargetSessionID != "" && record.ContentDigest != "" { |
| 40 | receipts = append(receipts, desktopMigrationReceipt{TargetSessionID: record.TargetSessionID, ContentDigest: record.ContentDigest}) |
| 41 | } else if record.PreviousCompletion != nil { |
| 42 | receipts = append(receipts, *record.PreviousCompletion) |
| 43 | } |
| 44 | } |
| 45 | return receipts, nil |
| 46 | } |
| 47 | |
| 48 | // Legacy normalization happens in disposable storage. Source files, selected |
| 49 | // DAG heads and old-format metadata are never rewritten during recovery. |
| 50 | func openHistoricalReconciliationSource(ctx context.Context, source historicalSource) (*session.Service, session.SessionRef, func(), error) { |
| 51 | root := filepath.Dir(source.path) |
| 52 | cleanup := func() {} |
| 53 | if source.format != "canonical" { |
| 54 | var err error |
| 55 | root, err = os.MkdirTemp("", "reasonix-history-proof-") |
| 56 | if err != nil { |
| 57 | return nil, session.SessionRef{}, cleanup, err |
| 58 | } |
| 59 | cleanup = func() { _ = os.RemoveAll(root) } |
| 60 | } |
| 61 | old, err := session.NewService("migration-source", session.NewFilesystemPersistence(root)) |
| 62 | if err != nil { |
| 63 | cleanup() |
| 64 | return nil, session.SessionRef{}, func() {}, err |
| 65 | } |
| 66 | finish := func() { shutdownHistoricalProofService(old); cleanup() } |
| 67 | ref := session.SessionRef{HostID: "migration-source", SessionID: filepath.Base(source.path)} |
| 68 | if source.format != "canonical" { |
| 69 | runtime, _, err := old.ContinueImported(ctx, source.path, source.head) |
| 70 | if err != nil { |
| 71 | finish() |
| 72 | return nil, ref, func() {}, err |
| 73 | } |
| 74 | ref = runtime.Ref() |
| 75 | if err := old.Close(ctx, ref); err != nil { |
| 76 | finish() |
| 77 | return nil, ref, func() {}, err |
| 78 | } |
| 79 | } |
| 80 | return old, ref, finish, nil |
| 81 | } |
| 82 |