Repository navigation
Expand file tree
/
Copy pathcache.go
More file actions
368 lines (337 loc) · 8.29 KB
/
Copy pathcache.go
File metadata and controls
368 lines (337 loc) · 8.29 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package kgraph
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"syscall"
"unsafe"
)
// cacheVersion is the layout of the cache file and of what the parsers put
// in it, so that no build mixes commits read the old way with new ones.
// TestParserDigest fails when the parsers read anything else and it stays.
const cacheVersion = 1
var cacheMagic = []byte("kgraph-c")
// cacheAlign is the boundary every fixed-width column starts at, so that
// the mapped file is used in place; the padding is zero. The file is
// little-endian, as the host is taken to be.
const cacheAlign = 8
// save writes s to path through a temporary file, so that a reader never
// sees half of it.
func (s *store) save(path string) (err error) {
f, err := os.CreateTemp(filepath.Dir(path), filepath.Base(path)+".*")
if err != nil {
return err
}
defer func() {
if err != nil {
f.Close()
os.Remove(f.Name())
}
}()
w := bufio.NewWriterSize(f, 1<<20)
e := &encoder{w: w}
e.write(cacheMagic)
e.u32(cacheVersion)
e.hashes(s.tips)
e.hashes(s.hashes)
encodeSlice(e, s.order)
encodeSlice(e, s.keys)
encodeSlice(e, s.date)
encodeSlice(e, s.parents.start)
encodeSlice(e, s.parents.list)
for _, col := range s.facts.keyCols() {
encodeSlice(e, *col)
}
for _, col := range s.facts.namedCols() {
e.u32(uint32(len(*col)))
e.pad()
for _, n := range *col {
e.u32(n.commit)
e.write(n.id[:])
}
}
for _, col := range s.facts.refCols() {
e.refs(*col)
}
if err := w.Flush(); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return os.Rename(f.Name(), path)
}
// loadStore reads a store that save wrote. It maps the file and keeps the
// mapping, which the columns of the store and of the graph built from it
// refer into, until release.
func loadStore(path string) (*store, error) {
b, err := mapCache(path)
if err != nil {
return nil, err
}
s, err := decodeStore(path, b)
if err != nil {
unmapCache(b)
return nil, err
}
s.mapped = b
return s, nil
}
// release unmaps the cache file; nothing of s may be used after it.
func (s *store) release() {
unmapCache(s.mapped)
s.mapped = nil
}
// decodeStore reads the store in b, the bytes of the file at path.
func decodeStore(path string, b []byte) (*store, error) {
d := &decoder{b: b}
if !bytes.Equal(d.take(len(cacheMagic)), cacheMagic) {
return nil, fmt.Errorf("%s is not a commit graph cache", path)
}
if v := d.u32(); v != cacheVersion {
return nil, fmt.Errorf("%s has version %d, not %d", path, v, cacheVersion)
}
s := newStore()
s.tips = view[hash](d, d.count(20))
s.hashes = view[hash](d, d.count(20))
s.order = view[uint32](d, d.count(4))
s.keys = view[uint64](d, d.count(8))
s.date = view[int64](d, d.count(8))
s.parents.start = view[uint32](d, d.count(4))
s.parents.list = view[uint32](d, d.count(4))
for _, col := range s.facts.keyCols() {
*col = view[uint64](d, d.count(8))
}
for _, col := range s.facts.namedCols() {
*col = view[namedRef](d, d.count(24))
}
for _, col := range s.facts.refCols() {
*col = d.refs()
}
if d.err == nil && len(d.b) > 0 {
d.err = errors.New("trailing bytes")
}
if d.err == nil {
d.err = s.check()
}
if d.err == nil {
s.fanout = fanoutOf(s.keys)
}
if d.err != nil {
return nil, fmt.Errorf("read %s: %w", path, d.err)
}
return s, nil
}
// check tells whether the lengths and indexes of s fit together, so that
// a damaged cache cannot make a build index out of range.
func (s *store) check() error {
n := uint32(len(s.hashes))
p := &s.parents
short := len(s.order) != int(n) || len(s.keys) != int(n) || len(s.date) != int(n) || len(p.start) != int(n)+1 || p.start[n] != uint32(len(p.list))
for _, col := range s.facts.keyCols() {
short = short || len(*col) != int(n)
}
if short {
return errors.New("lengths do not match")
}
for i := range n {
if p.start[i] > p.start[i+1] {
return errors.New("parent offsets out of order")
}
}
for _, c := range p.list {
if c >= n {
return errors.New("parent out of range")
}
}
for _, c := range s.order {
if c >= n {
return errors.New("order out of range")
}
}
for _, col := range s.facts.namedCols() {
for _, r := range *col {
if r.commit >= n {
return errors.New("commit out of range")
}
}
}
for _, col := range s.facts.refCols() {
refs := *col
for i, fx := range refs {
if fx.commit >= n || i > 0 && fx.commit < refs[i-1].commit {
return errors.New("refs out of order")
}
if len(fx.id) > 40 {
return errors.New("id too long")
}
}
}
return nil
}
// encoder writes little-endian values; the writer keeps the first error
// for Flush to return.
type encoder struct {
w *bufio.Writer
n int // bytes written
buf [8]byte
}
func (e *encoder) write(b []byte) {
e.w.Write(b)
e.n += len(b)
}
// pad writes zeros up to the next column boundary.
func (e *encoder) pad() {
var zeros [cacheAlign]byte
if r := e.n % cacheAlign; r != 0 {
e.write(zeros[:cacheAlign-r])
}
}
func (e *encoder) u32(v uint32) {
binary.LittleEndian.PutUint32(e.buf[:], v)
e.write(e.buf[:4])
}
func (e *encoder) u64(v uint64) {
binary.LittleEndian.PutUint64(e.buf[:], v)
e.write(e.buf[:8])
}
func (e *encoder) bool(v bool) {
e.buf[0] = 0
if v {
e.buf[0] = 1
}
e.write(e.buf[:1])
}
func (e *encoder) bytes(b []byte) {
e.u32(uint32(len(b)))
e.write(b)
}
func (e *encoder) hashes(hs []hash) {
e.u32(uint32(len(hs)))
e.pad()
for _, h := range hs {
e.write(h[:])
}
}
// refs writes a column of ids the messages name, which are not
// fixed-width.
func (e *encoder) refs(refs []fixRef) {
e.u32(uint32(len(refs)))
for _, fx := range refs {
e.u32(fx.commit)
e.bytes(fx.id)
e.u64(fx.subject)
e.bool(fx.quoted)
}
}
func encodeSlice[T uint32 | uint64 | int64](e *encoder, s []T) {
e.u32(uint32(len(s)))
e.pad()
binary.Write(e.w, binary.LittleEndian, s)
e.n += len(s) * binary.Size(T(0))
}
// decoder reads what encoder wrote and keeps the first error; after one it
// returns zero values.
type decoder struct {
b []byte
off int // bytes taken
err error
}
func (d *decoder) take(n int) []byte {
if d.err != nil {
return nil
}
if n > len(d.b) {
d.err = io.ErrUnexpectedEOF
return nil
}
b := d.b[:n:n]
d.b = d.b[n:]
d.off += n
return b
}
// align skips the padding before a column.
func (d *decoder) align() {
if r := d.off % cacheAlign; r != 0 {
d.take(cacheAlign - r)
}
}
// count reads the length of a list whose entries take at least size bytes
// each, and checks that the rest of the file can hold them.
func (d *decoder) count(size int) int {
n := int(d.u32())
if n > len(d.b)/size {
d.err = io.ErrUnexpectedEOF
return 0
}
return n
}
func (d *decoder) u32() uint32 {
if b := d.take(4); b != nil {
return binary.LittleEndian.Uint32(b)
}
return 0
}
func (d *decoder) u64() uint64 {
if b := d.take(8); b != nil {
return binary.LittleEndian.Uint64(b)
}
return 0
}
func (d *decoder) bool() bool {
b := d.take(1)
return b != nil && b[0] == 1
}
func (d *decoder) bytes() []byte { return d.take(int(d.u32())) }
// refs reads what encoder.refs wrote; the ids are copied out of the file.
func (d *decoder) refs() []fixRef {
refs := make([]fixRef, d.count(17))
for i := range refs {
fx := &refs[i]
fx.commit, fx.id, fx.subject, fx.quoted = d.u32(), bytes.Clone(d.bytes()), d.u64(), d.bool()
}
return refs
}
// view returns the n values of T at the decoder's position, the file's
// bytes themselves.
func view[T uint32 | uint64 | int64 | hash | namedRef](d *decoder, n int) []T {
var zero T
d.align()
b := d.take(n * int(unsafe.Sizeof(zero)))
if b == nil || n == 0 {
return []T{}
}
return unsafe.Slice((*T)(unsafe.Pointer(&b[0])), n)
}
// mapCache maps the cache file read-only, so that the graph uses its
// columns in place and no copy of the file is held. save never changes a
// file in place but renames a new one over it, so a mapping stays valid.
func mapCache(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
st, err := f.Stat()
if err != nil {
return nil, err
}
if st.IsDir() {
return nil, fmt.Errorf("%s is a directory", path)
}
if st.Size() == 0 {
return nil, nil
}
return syscall.Mmap(int(f.Fd()), 0, int(st.Size()), syscall.PROT_READ, syscall.MAP_SHARED)
}
// unmapCache releases what mapCache returned.
func unmapCache(b []byte) {
if b != nil {
syscall.Munmap(b)
}
}