| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "github.com/carlosframework/platform/internal/hostbox" |
| 6 | "github.com/carlosframework/platform/internal/rootstate" |
| 7 | "path/filepath" |
| 8 | ) |
| 9 | |
| 10 | // Rebase only the journal's deny-only descriptor set. The retained inventory |
| 11 | // proves which filesystem its old device number named; the old file inode is |
| 12 | // never an ownership grant. Unmapped or ambiguous filesystems refuse recovery. |
| 13 | func (k LinuxStopKernel) rebootStopFiles(ctx context.Context, in StopInventory, files []hostbox.StopFileIdentity) ([]hostbox.StopFileIdentity, error) { |
| 14 | type filesystem struct { |
| 15 | id string |
| 16 | device uint64 |
| 17 | } |
| 18 | devices := map[uint64]filesystem{} |
| 19 | for _, e := range in.Entries { |
| 20 | if e.Kind != "path" || e.Materialization != "materialized" { |
| 21 | continue |
| 22 | } |
| 23 | if err := ctx.Err(); err != nil { |
| 24 | return nil, err |
| 25 | } |
| 26 | root := k.ManagedRoots[e.ManagedRoot] |
| 27 | if !filepath.IsAbs(root) || !filepath.IsLocal(e.RelativePath) { |
| 28 | return nil, ErrClaimStopInventory |
| 29 | } |
| 30 | f, err := hostbox.OpenInventoryFile(filepath.Join(root, e.RelativePath)) |
| 31 | if err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | err = hostbox.VerifyInventoryFile(f, e) |
| 35 | if err == nil { |
| 36 | err = hostbox.VerifyInventoryIdentity(root, e) |
| 37 | } |
| 38 | now, captureErr := rootstate.ObjectIdentity(f) |
| 39 | f.Close() |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | if captureErr != nil { |
| 44 | return nil, captureErr |
| 45 | } |
| 46 | next := filesystem{e.Identity.Filesystem, now.Device} |
| 47 | if prior, ok := devices[e.Device]; ok && prior != next { |
| 48 | return nil, ErrClaimStopInventory |
| 49 | } |
| 50 | devices[e.Device] = next |
| 51 | } |
| 52 | out := make([]hostbox.StopFileIdentity, 0, len(files)) |
| 53 | for _, f := range files { |
| 54 | current, ok := devices[f.Device] |
| 55 | if !ok || f.Inode == 0 { |
| 56 | return nil, ErrClaimStopInventory |
| 57 | } |
| 58 | out = append(out, hostbox.StopFileIdentity{Device: current.device, Inode: f.Inode}) |
| 59 | } |
| 60 | return out, nil |
| 61 | } |
| 62 | |