Download this file
| 1 | package release |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "os" |
| 6 | ) |
| 7 | |
| 8 | // RecordCreation is called only inside root's first-registration transaction, |
| 9 | // after root proves the installation is new. Existing history is preserved. |
| 10 | func (h *RootHistory) RecordCreation(b HistoryBinding, installation string) error { |
| 11 | if !b.valid() || installation == "" || len(installation) > 4096 { |
| 12 | return os.ErrPermission |
| 13 | } |
| 14 | _, err := h.read(b) |
| 15 | if err == nil { |
| 16 | return nil |
| 17 | } |
| 18 | if !errors.Is(err, os.ErrNotExist) { |
| 19 | return err |
| 20 | } |
| 21 | return h.write(historyRecord{Schema: 1, Binding: b, Revision: 1, Creation: installation, Fresh: true, |
| 22 | Programs: historyPrograms{Revision: 1, Complete: true}, Refusals: historyRefusals{Revision: 1, Complete: true}}) |
| 23 | } |
| 24 | |
| 25 | func (h *RootHistory) FreshInstallation(b HistoryBinding, installation string) (bool, error) { |
| 26 | r, err := h.read(b) |
| 27 | if err != nil { |
| 28 | return false, err |
| 29 | } |
| 30 | return installation != "" && r.Creation == installation && r.Fresh, nil |
| 31 | } |
| 32 | |
| 33 | func (h *RootHistory) ConsumeCreation(b HistoryBinding) error { |
| 34 | r, err := h.read(b) |
| 35 | if errors.Is(err, os.ErrNotExist) { |
| 36 | return nil |
| 37 | } |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | if !r.Fresh { |
| 42 | return nil |
| 43 | } |
| 44 | if r.Revision == ^uint64(0) { |
| 45 | return os.ErrPermission |
| 46 | } |
| 47 | r.Revision++ |
| 48 | r.Fresh = false |
| 49 | return h.write(r) |
| 50 | } |
| 51 | |