| 1 | package sessioncatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | ) |
| 7 | |
| 8 | // ProveRecoveryLineage reads session content for the directories that hold |
| 9 | // recovery copies and records which copies a parent already covers. Metadata |
| 10 | // discovery never reads content, so on its own it lists every copy as an |
| 11 | // ordinary conversation; this is the separate, caller-scheduled step that earns |
| 12 | // the right to hide one. A directory whose content cannot be read keeps its |
| 13 | // copies visible, and directories without recovery copies are not touched. |
| 14 | func (c *Catalog) ProveRecoveryLineage(ctx context.Context) error { |
| 15 | if c == nil || c.db == nil || !c.opts.MetadataOnly { |
| 16 | return nil |
| 17 | } |
| 18 | rows, err := c.readDB(ctx).QueryContext(ctx, `SELECT DISTINCT directory,scope,workspace_root |
| 19 | FROM catalog_sessions WHERE missing_since=0 AND recovered=1`) |
| 20 | if err != nil { |
| 21 | return fmt.Errorf("list directories with recovery copies: %w", err) |
| 22 | } |
| 23 | var targets []DirectoryTarget |
| 24 | for rows.Next() { |
| 25 | var target DirectoryTarget |
| 26 | if err := rows.Scan(&target.Path, &target.Scope, &target.WorkspaceRoot); err != nil { |
| 27 | _ = rows.Close() |
| 28 | return err |
| 29 | } |
| 30 | targets = append(targets, target) |
| 31 | } |
| 32 | if err := rows.Close(); err != nil { |
| 33 | return err |
| 34 | } |
| 35 | for _, target := range targets { |
| 36 | if err := ctx.Err(); err != nil { |
| 37 | return err |
| 38 | } |
| 39 | if err := c.proveDirectoryLineage(ctx, target); err != nil { |
| 40 | return err |
| 41 | } |
| 42 | } |
| 43 | return nil |
| 44 | } |
| 45 | |
| 46 | func (c *Catalog) proveDirectoryLineage(ctx context.Context, target DirectoryTarget) error { |
| 47 | if c.opts.Maintenance != nil { |
| 48 | ctx = c.opts.Maintenance.Context(ctx) |
| 49 | release, err := c.opts.Maintenance.BackgroundSlice(ctx, false) |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | defer release(0) |
| 54 | } |
| 55 | return c.reconcileDirectoryFull(ctx, target, c.mutationSeq.Add(1)) |
| 56 | } |
| 57 |