|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io/fs" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | +) |
| 11 | + |
| 12 | +// ActionResult records the outputs of a completed action, mirroring Bazel's |
| 13 | +// ActionResult message. Outputs are not stored inline: each named output is a |
| 14 | +// digest pointing into the CAS. |
| 15 | +type ActionResult struct { |
| 16 | + // Outputs maps an output name (e.g. "analysis.pb", "plugin.wasm") to the |
| 17 | + // CAS digest of its contents. |
| 18 | + Outputs map[string]Digest `json:"outputs"` |
| 19 | +} |
| 20 | + |
| 21 | +// ActionCache maps action digests to ActionResults, stored as JSON files at |
| 22 | +// ac/<xx>/<hash> under the cache root. Unlike CAS entries, action cache |
| 23 | +// entries are not self-validating — the value is not derivable from the key — |
| 24 | +// so Get additionally checks that every referenced output still exists in the |
| 25 | +// CAS before reporting a hit, exactly like Bazel's disk cache does. |
| 26 | +// |
| 27 | +// Entry I/O goes through the same os.Root as the CAS, confining every read |
| 28 | +// and write to the cache directory. |
| 29 | +type ActionCache struct { |
| 30 | + root *os.Root |
| 31 | + cas *CAS |
| 32 | +} |
| 33 | + |
| 34 | +func newActionCache(root *os.Root, cas *CAS) *ActionCache { |
| 35 | + return &ActionCache{root: root, cas: cas} |
| 36 | +} |
| 37 | + |
| 38 | +// path returns an entry's path relative to the cache root. |
| 39 | +func (a *ActionCache) path(d Digest) string { |
| 40 | + return filepath.Join("ac", d.Hash[:2], d.Hash) |
| 41 | +} |
| 42 | + |
| 43 | +// Get returns the cached result for an action, or ErrNotFound on a miss. An |
| 44 | +// entry whose outputs are missing or corrupt in the CAS is treated as a miss |
| 45 | +// and evicted. |
| 46 | +func (a *ActionCache) Get(action Digest) (*ActionResult, error) { |
| 47 | + if !action.valid() { |
| 48 | + return nil, ErrNotFound |
| 49 | + } |
| 50 | + path := a.path(action) |
| 51 | + data, err := a.root.ReadFile(path) |
| 52 | + if err != nil { |
| 53 | + if errors.Is(err, fs.ErrNotExist) { |
| 54 | + return nil, ErrNotFound |
| 55 | + } |
| 56 | + return nil, fmt.Errorf("cache: %w", err) |
| 57 | + } |
| 58 | + var result ActionResult |
| 59 | + if err := json.Unmarshal(data, &result); err != nil { |
| 60 | + a.root.Remove(path) |
| 61 | + return nil, ErrNotFound |
| 62 | + } |
| 63 | + for _, d := range result.Outputs { |
| 64 | + if !a.cas.Contains(d) { |
| 65 | + a.root.Remove(path) |
| 66 | + return nil, ErrNotFound |
| 67 | + } |
| 68 | + } |
| 69 | + return &result, nil |
| 70 | +} |
| 71 | + |
| 72 | +// PutTree stores every file under dir in the CAS and records them as the |
| 73 | +// action's outputs, named by their paths relative to dir. Use this for |
| 74 | +// actions whose tool writes an output directory, like WASM compilation. |
| 75 | +func (a *ActionCache) PutTree(action Digest, dir string) error { |
| 76 | + outputs := map[string]Digest{} |
| 77 | + err := filepath.WalkDir(dir, func(path string, entry fs.DirEntry, err error) error { |
| 78 | + if err != nil || entry.IsDir() { |
| 79 | + return err |
| 80 | + } |
| 81 | + rel, err := filepath.Rel(dir, path) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + data, err := os.ReadFile(path) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + d, err := a.cas.Put(data) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + outputs[filepath.ToSlash(rel)] = d |
| 94 | + return nil |
| 95 | + }) |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("cache: %w", err) |
| 98 | + } |
| 99 | + if len(outputs) == 0 { |
| 100 | + return fmt.Errorf("cache: no outputs found under %s", dir) |
| 101 | + } |
| 102 | + return a.Put(action, &ActionResult{Outputs: outputs}) |
| 103 | +} |
| 104 | + |
| 105 | +// GetTree materializes a cached action's outputs as files under dir, or |
| 106 | +// returns ErrNotFound on a miss. A file already present is reused only if its |
| 107 | +// contents still hash to the expected digest; otherwise it is rewritten from |
| 108 | +// the CAS. Writes are staged, fsynced, and renamed, so a crash cannot leave a |
| 109 | +// right-sized but torn file that later reads would trust. |
| 110 | +func (a *ActionCache) GetTree(action Digest, dir string) error { |
| 111 | + result, err := a.Get(action) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + for rel, d := range result.Outputs { |
| 116 | + // Output names come from the action cache entry, which — unlike a CAS |
| 117 | + // blob — is not self-validating, so a tampered entry could carry a |
| 118 | + // name like "../../etc/x". Reject anything that isn't a relative path |
| 119 | + // confined to dir; this is the confinement the os.Root gives the rest |
| 120 | + // of the cache, which GetTree can't use because dir is a caller-owned |
| 121 | + // path a tool must read by absolute name. |
| 122 | + if !filepath.IsLocal(rel) { |
| 123 | + return fmt.Errorf("cache: unsafe output path %q in action %s", rel, action) |
| 124 | + } |
| 125 | + path := filepath.Join(dir, filepath.FromSlash(rel)) |
| 126 | + // Trust an existing file only if it still hashes to the digest; |
| 127 | + // size alone can mask in-place corruption that would make the |
| 128 | + // consuming tool hard-fail with no way to repair (see wazero). |
| 129 | + if existing, err := os.ReadFile(path); err == nil && DigestOf(existing) == d { |
| 130 | + continue |
| 131 | + } |
| 132 | + data, err := a.cas.Get(d) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil { |
| 137 | + return fmt.Errorf("cache: %w", err) |
| 138 | + } |
| 139 | + if err := writeFileAtomic(path, data); err != nil { |
| 140 | + return fmt.Errorf("cache: %w", err) |
| 141 | + } |
| 142 | + } |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +// writeFileAtomic writes data to path via a staged temp file in the same |
| 147 | +// directory, fsynced before an atomic rename, so a reader never observes a |
| 148 | +// partial or torn file even across a crash. |
| 149 | +func writeFileAtomic(path string, data []byte) error { |
| 150 | + f, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+"-*") |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + tmp := f.Name() |
| 155 | + if _, err := f.Write(data); err != nil { |
| 156 | + f.Close() |
| 157 | + os.Remove(tmp) |
| 158 | + return err |
| 159 | + } |
| 160 | + if err := f.Sync(); err != nil { |
| 161 | + f.Close() |
| 162 | + os.Remove(tmp) |
| 163 | + return err |
| 164 | + } |
| 165 | + if err := f.Close(); err != nil { |
| 166 | + os.Remove(tmp) |
| 167 | + return err |
| 168 | + } |
| 169 | + if err := os.Rename(tmp, path); err != nil { |
| 170 | + os.Remove(tmp) |
| 171 | + return err |
| 172 | + } |
| 173 | + return nil |
| 174 | +} |
| 175 | + |
| 176 | +// Put records the result of an action. All outputs must already be in the |
| 177 | +// CAS; writes are staged and renamed so concurrent processes never observe a |
| 178 | +// partial entry. |
| 179 | +func (a *ActionCache) Put(action Digest, result *ActionResult) error { |
| 180 | + for name, d := range result.Outputs { |
| 181 | + if !a.cas.Contains(d) { |
| 182 | + return fmt.Errorf("cache: output %q (%s) missing from CAS", name, d) |
| 183 | + } |
| 184 | + } |
| 185 | + data, err := json.Marshal(result) |
| 186 | + if err != nil { |
| 187 | + return fmt.Errorf("cache: %w", err) |
| 188 | + } |
| 189 | + path := a.path(action) |
| 190 | + if err := a.root.MkdirAll(filepath.Dir(path), 0755); err != nil { |
| 191 | + return fmt.Errorf("cache: %w", err) |
| 192 | + } |
| 193 | + f, name, err := a.cas.createTemp(action.Hash[:8] + "-") |
| 194 | + if err != nil { |
| 195 | + return fmt.Errorf("cache: %w", err) |
| 196 | + } |
| 197 | + defer a.root.Remove(name) |
| 198 | + if _, err := f.Write(data); err != nil { |
| 199 | + f.Close() |
| 200 | + return fmt.Errorf("cache: %w", err) |
| 201 | + } |
| 202 | + if err := f.Close(); err != nil { |
| 203 | + return fmt.Errorf("cache: %w", err) |
| 204 | + } |
| 205 | + if err := a.root.Rename(name, path); err != nil { |
| 206 | + return fmt.Errorf("cache: %w", err) |
| 207 | + } |
| 208 | + return nil |
| 209 | +} |
0 commit comments