package mirror import ( "encoding/json" "os" "path/filepath" "time" ) // Removals kept so mirrors in step agree on them: a version removed here // while a peer was down is removed there when it is back, rather than copied // back here from it. Kept in the mirror's own bookkeeping, a dot file the // file server never serves as a model. const tombstoneFile = ".removed.json" // Tombstones are the versions removed, model/version to when, in ms. func (m *Manager) Tombstones() map[string]int64 { m.mu.Lock() defer m.mu.Unlock() return m.readTombstones() } func (m *Manager) readTombstones() map[string]int64 { out := map[string]int64{} b, err := os.ReadFile(filepath.Join(m.root, tombstoneFile)) if err == nil { _ = json.Unmarshal(b, &out) } return out } func (m *Manager) writeTombstones(t map[string]int64) { b, _ := json.Marshal(t) tmp := filepath.Join(m.root, tombstoneFile+".tmp") if os.WriteFile(tmp, b, 0o644) == nil { _ = os.Rename(tmp, filepath.Join(m.root, tombstoneFile)) } } func (m *Manager) recordTombstone(model, version string, at time.Time) { m.mu.Lock() defer m.mu.Unlock() t := m.readTombstones() if at.UnixMilli() > t[model+"/"+version] { t[model+"/"+version] = at.UnixMilli() m.writeTombstones(t) } } func (m *Manager) clearTombstone(model, version string) { m.mu.Lock() defer m.mu.Unlock() t := m.readTombstones() if _, ok := t[model+"/"+version]; ok { delete(t, model+"/"+version) m.writeTombstones(t) } }