| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "github.com/carlosframework/platform/internal/hostbox" |
| 6 | "github.com/carlosframework/platform/internal/hostguard" |
| 7 | "github.com/carlosframework/platform/internal/rootstate" |
| 8 | "path/filepath" |
| 9 | ) |
| 10 | |
| 11 | func (h ClaimRootHTTP) bindInventoryPaths(ctx context.Context, in *hostbox.StopInventory) error { |
| 12 | if h.inventoryIdentity == nil { |
| 13 | return nil |
| 14 | } |
| 15 | prior, _, err := h.Journals.Inventory.Snapshot(ctx, in.Host, hostguard.Captured{Incarnation: in.Incarnation, Stamp: in.InstallationStamp}) |
| 16 | if err != nil { |
| 17 | return err |
| 18 | } |
| 19 | for i, e := range in.Entries { |
| 20 | // Caller-supplied persistent evidence never creates root authority. |
| 21 | claimedBoot := e.Identity.Boot |
| 22 | e.Identity = rootstate.FileIdentity{} |
| 23 | if e.Kind != "path" || e.Materialization != "materialized" { |
| 24 | in.Entries[i] = e |
| 25 | continue |
| 26 | } |
| 27 | if e.ManagedRoot != "data" || !filepath.IsLocal(e.RelativePath) { |
| 28 | return hostbox.ErrTypedInventory |
| 29 | } |
| 30 | oldFound := false |
| 31 | for _, old := range prior.Entries { |
| 32 | if old.ArtifactID == e.ArtifactID && old.Materialization == "materialized" { |
| 33 | if old.Identity.Boot == "" { |
| 34 | return hostbox.ErrTypedInventory |
| 35 | } |
| 36 | e.Identity = old.Identity |
| 37 | oldFound = true |
| 38 | } |
| 39 | } |
| 40 | path := filepath.Join(h.Roots[e.ManagedRoot], e.RelativePath) |
| 41 | f, err := hostbox.OpenInventoryFile(path) |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | if oldFound { |
| 46 | err = hostbox.VerifyInventoryIdentity(h.Roots[e.ManagedRoot], e) |
| 47 | } else { |
| 48 | e.Identity, err = h.inventoryIdentity(f) |
| 49 | if err == nil && (claimedBoot != e.Identity.Boot || e.Device != e.Identity.Device || e.Inode != e.Identity.Inode || e.Type != e.Identity.Mode&0170000) { |
| 50 | err = hostbox.ErrChanged |
| 51 | } |
| 52 | } |
| 53 | f.Close() |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | in.Entries[i] = e |
| 58 | } |
| 59 | return nil |
| 60 | } |
| 61 | |