| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "github.com/carlosframework/platform/internal/hostbox" |
| 6 | "github.com/carlosframework/platform/internal/registry" |
| 7 | "os" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | func (h ClaimRootHTTP) verifyInventoryTimer(host string, owner registry.ClaimOwnership, e hostbox.StopInventoryEntry) error { |
| 12 | if h.Reconcile.Registry == nil || e.Kind != "timer" || e.ManagedRoot != "registry" || !strings.HasPrefix(e.RelativePath, host+"/") || e.RowIdentity == "" || e.Ownership != owner.ClaimOwner || e.Device != 0 || e.Inode != 0 || e.Type != 0 { |
| 13 | return hostbox.ErrTypedInventory |
| 14 | } |
| 15 | name := strings.TrimPrefix(e.RelativePath, host+"/") |
| 16 | if name == "" || strings.ContainsAny(name, "/\\") || name == "." || name == ".." { |
| 17 | return hostbox.ErrTypedInventory |
| 18 | } |
| 19 | timer, err := h.Reconcile.Registry.GetTimer(host, name) |
| 20 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 21 | return err |
| 22 | } |
| 23 | if timer == nil { |
| 24 | if e.Materialization != "intended" && !e.TimerRetired { |
| 25 | return hostbox.ErrTypedInventory |
| 26 | } |
| 27 | return nil |
| 28 | } |
| 29 | if e.TimerRetired || (e.Materialization != "intended" && e.Materialization != "materialized") || timer.Host != host || timer.Name != name || timer.ClaimIncarnation != owner.ClaimIncarnation || timer.ClaimStamp != owner.ClaimStamp || timer.ClaimOwner != e.Ownership || timer.ClaimRowID != e.RowIdentity || timer.Registration != e.RowIdentity { |
| 30 | return hostbox.ErrTypedInventory |
| 31 | } |
| 32 | return nil |
| 33 | } |
| 34 | |
| 35 | func (h ClaimRootHTTP) verifyNewTimerIntentions(prior, next hostbox.StopInventory) error { |
| 36 | for _, e := range next.Entries { |
| 37 | if e.TimerRetired && e.Kind != "timer" { |
| 38 | return hostbox.ErrTypedInventory |
| 39 | } |
| 40 | if e.Kind != "timer" { |
| 41 | continue |
| 42 | } |
| 43 | known := false |
| 44 | for _, old := range prior.Entries { |
| 45 | if old.Kind == "timer" && old.ArtifactID == e.ArtifactID { |
| 46 | known = true |
| 47 | break |
| 48 | } |
| 49 | } |
| 50 | if known { |
| 51 | continue |
| 52 | } |
| 53 | if e.Materialization != "intended" || e.TimerRetired { |
| 54 | return hostbox.ErrTypedInventory |
| 55 | } |
| 56 | row, err := h.Reconcile.Registry.GetTimer(next.Host, strings.TrimPrefix(e.RelativePath, next.Host+"/")) |
| 57 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 58 | return err |
| 59 | } |
| 60 | if row != nil { |
| 61 | return hostbox.ErrTypedInventory |
| 62 | } |
| 63 | } |
| 64 | return nil |
| 65 | } |
| 66 | |