gunk-dev/the-valley

cache.go

sourceca98fdc29fbfmain's published tip history
4.4 KiB
1// What a snapshot remembers between commands: facts about objects that
2// never change, kept under the user's cache directory, outside every
3// checkout.
4//
5// <cache>/trees/<tree ID> the valley-tree-v1 digest of a git tree
6// <cache>/landings/<SHA-256> a transfer statement that passed every
7// content check (transfer.go)
8//
9// A tree entry is the line "valley-tree-v1 <tree ID> <digest>", then a
10// line holding the SHA-256 of the first. A landing entry is the text that
11// names what was checked, under the SHA-256 of that text: it is found only
12// when the file holds exactly that text. An entry that does not read as
13// one, such as one cut short or damaged, or one that is not a regular
14// file, is not found, and the work it would have saved is done again.
15// Each entry is written to a file of its own and renamed into place, so
16// commands running at once never see one half written. Nothing here fails
17// a command or prints anything: a cache that cannot be read or written
18// leaves the work to be done each time.
19//
20// A landing entry is never a version's landing evidence. It records that
21// a statement's content checks passed for one head, landing commit,
22// request name and signing key; the snapshot still checks, on every read,
23// that the key is accepted, that the integrator's records bind the head to
24// the statement, and that the landing commit is on main.
25
26package rebase
27
28import (
29 "crypto/sha256"
30 "encoding/hex"
31 "io"
32 "os"
33 "path/filepath"
34 "strings"
35)
36
37// CacheDir is the directory a snapshot remembers verified transfer
38// statements in: valley/transfers under the user's cache directory
39// (os.UserCacheDir, which honours XDG_CACHE_HOME), beside the clones
40// valley keeps there, or "" when there is none.
41func CacheDir() string {
42 dir, err := os.UserCacheDir()
43 if err != nil || dir == "" {
44 return ""
45 }
46 return filepath.Join(dir, "valley", "transfers")
47}
48
49// cache is a snapshot's cache directory, or none when dir is "".
50type cache struct{ dir string }
51
52// entryLimit is the most an entry is read to: every entry is far smaller.
53const entryLimit = 4096
54
55// tree is the digest remembered for a tree, if one is.
56func (c cache) tree(oid string) (string, bool) {
57 data, ok := c.read("trees", oid)
58 if !ok {
59 return "", false
60 }
61 line, sum, ok := strings.Cut(string(data), "\n")
62 fields := strings.Fields(line)
63 if !ok || sum != hexSum(line)+"\n" || len(fields) != 3 || fields[0] != "valley-tree-v1" || fields[1] != oid || !isHex(fields[2], 64) {
64 return "", false
65 }
66 return fields[2], true
67}
68
69// putTree remembers a tree's digest.
70func (c cache) putTree(oid, digest string) {
71 line := "valley-tree-v1 " + oid + " " + digest
72 c.write("trees", oid, line+"\n"+hexSum(line)+"\n")
73}
74
75// landing reports whether a landing entry holding text is remembered.
76func (c cache) landing(text string) bool {
77 data, ok := c.read("landings", hexSum(text))
78 return ok && string(data) == text
79}
80
81// putLanding remembers a landing entry holding text.
82func (c cache) putLanding(text string) {
83 c.write("landings", hexSum(text), text)
84}
85
86func hexSum(s string) string {
87 sum := sha256.Sum256([]byte(s))
88 return hex.EncodeToString(sum[:])
89}
90
91// read is the content of an entry, if it reads. Only a regular file is
92// an entry: the name is opened without following a symlink or waiting
93// (entryFlags), so a FIFO, a device or a symlink in an entry's place is a
94// miss, and never a read that blocks.
95func (c cache) read(kind, name string) ([]byte, bool) {
96 if c.dir == "" || !isHex(name, len(name)) || name == "" {
97 return nil, false
98 }
99 f, err := os.OpenFile(filepath.Join(c.dir, kind, name), os.O_RDONLY|entryFlags, 0)
100 if err != nil {
101 return nil, false
102 }
103 defer f.Close()
104 if info, err := f.Stat(); err != nil || !info.Mode().IsRegular() {
105 return nil, false
106 }
107 data, err := io.ReadAll(io.LimitReader(f, entryLimit))
108 return data, err == nil
109}
110
111// write puts an entry in place whole: written to a file of its own in the
112// same directory, then renamed over its name.
113func (c cache) write(kind, name, content string) {
114 if c.dir == "" || !isHex(name, len(name)) || name == "" {
115 return
116 }
117 dir := filepath.Join(c.dir, kind)
118 if os.MkdirAll(dir, 0o700) != nil {
119 return
120 }
121 f, err := os.CreateTemp(dir, ".new-*")
122 if err != nil {
123 return
124 }
125 _, err = f.WriteString(content)
126 if cerr := f.Close(); err == nil {
127 err = cerr
128 }
129 if err == nil {
130 err = os.Rename(f.Name(), filepath.Join(dir, name))
131 }
132 if err != nil {
133 os.Remove(f.Name())
134 }
135}