persist writes the in-memory entries to disk atomically. Best-effort: a write failure only means the next cold start re-probes, which is correct.
()
| 99 | // persist writes the in-memory entries to disk atomically. Best-effort: a |
| 100 | // write failure only means the next cold start re-probes, which is correct. |
| 101 | func (c *detectionCache) persist() { |
| 102 | c.mu.RLock() |
| 103 | file := detectionCacheFile{Tools: make(map[string]detectionEntry, len(c.entries))} |
| 104 | for name, entry := range c.entries { |
| 105 | file.Tools[name] = entry |
| 106 | } |
| 107 | path := c.path |
| 108 | c.mu.RUnlock() |
| 109 | |
| 110 | if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { |
| 111 | return |
| 112 | } |
| 113 | payload, err := json.MarshalIndent(file, "", " ") |
| 114 | if err != nil { |
| 115 | return |
| 116 | } |
| 117 | tmp := fmt.Sprintf("%s.tmp.%d", path, os.Getpid()) |
| 118 | if err := os.WriteFile(tmp, payload, 0o600); err != nil { |
| 119 | return |
| 120 | } |
| 121 | _ = os.Rename(tmp, path) |
| 122 | } |