Repository navigation
Expand file tree
/
Copy pathcache.go
More file actions
103 lines (96 loc) · 3.26 KB
/
Copy pathcache.go
File metadata and controls
103 lines (96 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Package cache implements sqlc's on-disk cache, designed after Bazel's
// local disk cache.
//
// It has two halves:
//
// - A content-addressable store (CAS) holding blobs keyed by the SHA-256
// hash of their contents, laid out as cas/<xx>/<hash>.
// - An action cache (AC) mapping the digest of an action — a description of
// cacheable work and all of its inputs — to the digests of the outputs
// that work produced, laid out as ac/<xx>/<hash>.
//
// Work whose output is not derivable from its inputs, like query analysis,
// uses both halves, just like Bazel: hash the action, look its digest up in
// the action cache, then fetch the referenced output blobs from the CAS.
//
// Remote fetches with a declared checksum, like WASM plugins, need no action
// cache entry at all: the declared sha256 is itself a content address, so the
// blob is stored and loaded directly from the CAS keyed by that checksum.
package cache
import (
"fmt"
"os"
"path/filepath"
)
// Cache bundles the CAS and the action cache that shares it. All storage
// I/O is confined to the cache directory through an os.Root; callers should
// Close the cache when finished with it to release the root.
type Cache struct {
root *os.Root
CAS *CAS
Actions *ActionCache
}
// Dir returns the cache root, defaulting to os.UserCacheDir(). The location
// can be overridden with the SQLCCACHE environment variable.
func Dir() (string, error) {
cache := os.Getenv("SQLCCACHE")
if cache != "" {
return cache, nil
}
cacheHome, err := os.UserCacheDir()
if err != nil {
return "", err
}
return filepath.Join(cacheHome, "sqlc"), nil
}
// Open returns the cache rooted at Dir().
func Open() (*Cache, error) {
root, err := Dir()
if err != nil {
return nil, err
}
return OpenAt(root)
}
// OpenAt returns the cache rooted at the given directory, creating it if
// necessary.
func OpenAt(dir string) (*Cache, error) {
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, fmt.Errorf("failed to create %s directory: %w", dir, err)
}
root, err := os.OpenRoot(dir)
if err != nil {
return nil, fmt.Errorf("cache: %w", err)
}
cas, err := newCAS(root)
if err != nil {
root.Close()
return nil, err
}
return &Cache{
root: root,
CAS: cas,
Actions: newActionCache(root, cas),
}, nil
}
// Close releases the cache's handle on its root directory.
func (c *Cache) Close() error {
return c.root.Close()
}
// ExecDir creates a fresh, private scratch directory for materializing the
// output tree of the given action, for tools that need their outputs on disk
// (like wazero's compilation cache). Each call returns a new directory under
// exec/, so two concurrent processes never share one — otherwise a tool
// staging files there (wazero writes <key>.tmp files in place) could be swept
// into the other's PutTree. The caller must remove it when done; its contents
// are always reproducible from the CAS, so losing it is harmless.
func (c *Cache) ExecDir(action Digest) (string, error) {
base := filepath.Join(c.root.Name(), "exec")
if err := os.MkdirAll(base, 0755); err != nil {
return "", fmt.Errorf("failed to create %s directory: %w", base, err)
}
dir, err := os.MkdirTemp(base, action.Hash+"-")
if err != nil {
return "", fmt.Errorf("cache: %w", err)
}
return dir, nil
}