package main // An item put off for a restore (design 47 G §563, §302, §664): `backed-up` lists it with the restore's act and since when. The // night's process keeps it in the module's state directory while the item waits, and the tools' runtime reads it there. import ( "encoding/json" "os" "path/filepath" "time" ) // PutOff says an item gave way to a restore: the act of that restore and since when. type PutOff struct { Restore string `json:"restore"` Since time.Time `json:"since"` } type putOffItem struct { Module string `json:"module"` Item string `json:"item"` PutOff } func (b *Backups) putOffFile() string { return filepath.Join(b.Where.State, "put-off.json") } func (b *Backups) readPutOff() []putOffItem { raw, err := os.ReadFile(b.putOffFile()) if err != nil { return nil } var all []putOffItem _ = json.Unmarshal(raw, &all) return all } func (b *Backups) writePutOff(all []putOffItem) { if len(all) == 0 { if err := os.Remove(b.putOffFile()); err != nil && !os.IsNotExist(err) { b.Say("the items put off could not be cleared: %v", err) } return } raw, _ := json.MarshalIndent(all, "", " ") if err := os.WriteFile(b.putOffFile(), append(raw, '\n'), 0o600); err != nil { b.Say("the items put off could not be recorded: %v", err) } } // keepPutOff records a module's items as put off for a restore, since now. func (b *Backups) keepPutOff(items []ItemRef, act string) { all := b.readPutOff() for _, ref := range items { all = append(all, putOffItem{Module: ref.Module, Item: ref.Item, PutOff: PutOff{Restore: act, Since: b.Now().UTC()}}) } b.writePutOff(all) } // clearPutOff forgets a module's items put off: they started again. func (b *Backups) clearPutOff(module string) { var keep []putOffItem for _, p := range b.readPutOff() { if p.Module != module { keep = append(keep, p) } } b.writePutOff(keep) } // withPutOff says on a module's item reports which items are put off. func (b *Backups) withPutOff(module string, reports []ItemReport) []ItemReport { for _, p := range b.readPutOff() { if p.Module != module { continue } for i := range reports { if reports[i].ID == p.Item { put := p.PutOff reports[i].PutOff = &put } } } return reports }