-
Notifications
You must be signed in to change notification settings - Fork 1
/
pack.go
419 lines (362 loc) · 9.2 KB
/
pack.go
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package git
import (
"compress/zlib"
"io"
"reflect"
"sort"
"unsafe"
"encoding/binary"
"errors"
"fmt"
"os"
)
type PackFile struct {
repo *Git
Pack string
Index string
firstLevelFanout [256][4]byte // from the index
indexContents []Ptr
indexPtrs []IndexPtr
indexCRCs []uint32
crossRef map[uint32]int
data *os.File
cache map[int64]*PackedObject
}
func (p *PackFile) GetNamed(RefType, string) *NamedRef {
return nil
}
func (p *PackFile) open() (*os.File, error) {
if p.data == nil {
f, err := os.Open(p.Pack)
if err != nil {
return nil, err
}
p.data = f
}
return p.data, nil
}
type PackedObject struct {
name Ptr
container *PackFile
offset int64
size int64
typecode ObjType
headerlen uint8
dedeltatype ObjType
dedelta []byte
}
func (po *PackedObject) Load() (GitObject, error) {
buf, t, err := po.deDeltaifiedBytes(0)
if err != nil {
return nil, err
}
return po.container.repo.Interpret(&po.name, t, buf)
}
var ErrUnknownObjectType = errors.New("unknown object type")
func (p *PackFile) newPackedObject(obj *Ptr, at int64) (*PackedObject, error) {
if p.cache[at] != nil {
return p.cache[at], nil
}
data, err := p.open()
if err != nil {
return nil, err
}
data.Seek(at, 0)
var header [10]byte
n, err := data.Read(header[:])
if err != nil {
return nil, err
}
if n != 10 {
return nil, fmt.Errorf("only read %d of header", n)
}
var size uint64
var typeCode byte
typeCode = (header[0] >> 4) & 7
size = uint64(header[0] & 0xf)
i := 0
shift := uint(4)
//fmt.Printf("at %d, size=%d\n", i, size)
for (header[i] & 0x80) != 0 {
i++
size += uint64(header[i]&0x7f) << shift
shift += 7
//fmt.Printf("at %d, size=%d\n", i, size)
}
po := &PackedObject{
name: *obj,
container: p,
offset: at,
size: int64(size),
typecode: ObjType(typeCode),
headerlen: uint8(i + 1),
dedeltatype: ObjNone,
}
p.cache[at] = po
return po, nil
}
func (po *PackedObject) Name() *Ptr {
return &po.name
}
func (po *PackedObject) Type() ObjType {
return po.typecode
}
func decodeOffsetDelta(chunk []byte) (int64, []byte) {
delta_rel_offset := int64(chunk[0] & 0x7f)
i := 0
//fmt.Printf(" offset chunk[0] 0x%02x\n", chunk[0])
for (chunk[i] & 0x80) != 0 {
i++
//fmt.Printf(" offset chunk[%d] 0x%02x\n", i, chunk[i])
delta_rel_offset = ((1 + delta_rel_offset) << 7) + int64(chunk[i]&0x7f)
}
return delta_rel_offset, chunk[i+1:]
}
func (po *PackedObject) Payload() ([]byte, error) {
//log.Info("Want Payload(%s)", &po.name)
buf, t, err := po.deDeltaifiedBytes(0)
if err != nil {
return nil, err
}
// a bit inefficient in the common case; we are copying the payload bytes
// around... it'd be better to let read() insert the preamble, at least
// in the no-delta case
preamble := fmt.Sprintf("%s %d\x00", t, len(buf))
//log.Info("%s preamble => %s", &po.name, preamble)
out := make([]byte, len(preamble)+len(buf))
copy(out[0:len(preamble)], []byte(preamble))
copy(out[len(preamble):], buf)
return out, err
}
func (po *PackedObject) deDeltaifiedBytes(depth int) ([]byte, ObjType, error) {
if po.dedeltatype != ObjNone {
return po.dedelta, po.dedeltatype, nil
}
//log.Info("deDelatify[%d](%s)", depth, &po.name)
buf, base, err := po.read()
if base == nil {
//log.Info(" leaf %s : %d bytes", po.typecode, len(buf))
po.dedeltatype = po.typecode
po.dedelta = buf
return buf, po.typecode, err
}
if base.offset == 0 {
panic("we only handle offset deltas so far")
}
p := po.container
i, ok := p.crossRef[uint32(base.offset)]
if !ok {
panic("not a real offset")
}
baseObj, err := p.newPackedObject(&(p.indexContents[i]), base.offset)
if err != nil {
return nil, ObjNone, err
}
baseData, t, err := baseObj.deDeltaifiedBytes(depth + 1)
if err != nil {
//fmt.Printf("Could not read base object %s!\n", &baseObj.name)
return nil, ObjNone, err
}
data, ptr, err := patchDelta(t, baseData, buf)
if err != nil {
return nil, ObjNone, err
}
//log.Info("patched into %s (%d bytes) we want %s", ptr, len(data), &po.name)
// make sure the hash of the result of applying the delta
// is what we expect
if !ptr.Equals(&po.name) {
return nil, ObjNone, ErrDeltaMismatch
}
po.dedeltatype = t
po.dedelta = data
return data, t, err
}
var ErrDeltaMismatch = errors.New("expanded delta name mismatch")
type BaseSpec struct {
name *Ptr
offset int64
}
func (po *PackedObject) read() ([]byte, *BaseSpec, error) {
var base *BaseSpec
data := po.container.data
data.Seek(po.offset+int64(po.headerlen), 0)
/*log.Info("<%s>\noffset = %d headerlen = %d size = %d type=%s",
&po.name,
po.offset,
po.headerlen,
po.size,
po.typecode)*/
var chunk [10]byte
n, err := data.Read(chunk[:])
h := chunk[:n]
if po.typecode == ObjOffsetDelta {
delta_rel_offset, h2 := decodeOffsetDelta(h)
/*fmt.Printf("offset delta %d ; implies offset @%d\n",
delta_rel_offset,
po.offset-delta_rel_offset)*/
h = h2
base = &BaseSpec{
offset: po.offset - delta_rel_offset,
}
}
data.Seek(po.offset+int64(po.headerlen)+10-int64(len(h)), 0)
rc, err := zlib.NewReader(data)
if err != nil {
panic(err)
}
defer rc.Close()
//fmt.Printf("Trying to read %d bytes\n", po.size)
buf := make([]byte, po.size)
num, err := io.ReadFull(rc, buf)
//fmt.Printf("Read %d with error: %s\n", num, err)
if err != nil {
if err != io.EOF || num != len(buf) {
return nil, base, err
}
}
return buf[:num], base, nil
}
func (p *PackFile) Get(obj *Ptr) GitObject {
at := p.find(obj)
if at == 0 {
return nil
}
item, err := p.newPackedObject(obj, at)
if err != nil {
return nil
}
return item
}
// returns the offset of the object in this packfile, or 0 if not present
func (p *PackFile) find(obj *Ptr) int64 {
i := obj.hash[0]
var a, b int
if i > 0 {
a = int(binary.BigEndian.Uint32(p.firstLevelFanout[i-1][:]))
}
b = int(binary.BigEndian.Uint32(p.firstLevelFanout[i][:]))
if a > b {
panic("bad pack index")
}
if a == b {
// empty region
return 0
}
if a+1 == b {
// only one thing in region
if obj.Equals(&p.indexContents[a]) {
return (&p.indexPtrs[a]).asOffset()
}
}
k := sort.Search(b-a, func(i int) bool {
return !obj.Less(&p.indexContents[a+i])
})
if obj.Equals(&p.indexContents[a+k]) {
return (&p.indexPtrs[a+k]).asOffset()
}
return 0
}
type IndexPtr [4]byte
func (ip *IndexPtr) asOffset() int64 {
u := binary.BigEndian.Uint32((*ip)[:])
return int64(u)
}
type memBlock interface{}
func readRaw(dest memBlock, nbytes int, src *os.File) (int, error) {
var raw []byte
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&raw))
dv := reflect.ValueOf(dest)
if dv.Kind() == reflect.Ptr {
hdr.Data = dv.Elem().UnsafeAddr()
} else if dv.Kind() == reflect.Slice {
hdr.Data = dv.Pointer()
}
hdr.Len = nbytes
hdr.Cap = nbytes
/*at, _ := src.Seek(0, 1)
fmt.Printf("Reading %d bytes at +%d...\n", nbytes, at)*/
return src.Read(raw)
}
func (p *PackFile) loadIndex() error {
rdr, err := os.Open(p.Index)
if err != nil {
return err
}
defer rdr.Close()
var version, signature uint32
binary.Read(rdr, binary.BigEndian, &signature)
binary.Read(rdr, binary.BigEndian, &version)
readRaw(&p.firstLevelFanout, 256*4, rdr)
/*
for i, f := range p.firstLevelFanout[:] {
fmt.Printf(" [0x%02x] %#x %d\n", i, f[:], binary.BigEndian.Uint32(f[:]))
}*/
count := int(binary.BigEndian.Uint32(p.firstLevelFanout[255][:]))
entries := make([]Ptr, count)
crctable := make([]uint32, count)
ptrs := make([]IndexPtr, count)
crossRef := make(map[uint32]int, count)
readRaw(entries, count*20, rdr)
readRaw(crctable, count*4, rdr)
readRaw(ptrs, count*4, rdr)
for i := 0; i < count; i++ {
offset := binary.BigEndian.Uint32(ptrs[i][:])
crossRef[offset] = i
}
/* for i := 0; i < count; i++ {
offset := binary.BigEndian.Uint32(ptrs[i][:])
fmt.Printf(" [%d] %s @%d\n", i, &entries[i], offset)
}*/
p.indexContents = entries
p.indexCRCs = crctable
p.indexPtrs = ptrs
p.crossRef = crossRef
return nil
}
func IncludePackFile(g *Git, pack string) (*PackFile, error) {
p := &PackFile{
repo: g,
Pack: pack,
Index: pack[:len(pack)-5] + ".idx",
cache: make(map[int64]*PackedObject),
}
err := p.loadIndex()
if err != nil {
return nil, err
}
log.Info("Including %s with %d items", pack, len(p.indexContents))
g.AddStore(p)
return p, nil
}
/*
rdr, err := os.Open(f)
if err != nil {
return err
}
var signature, version, count uint32
binary.Read(rdr, binary.BigEndian, &signature)
binary.Read(rdr, binary.BigEndian, &version)
binary.Read(rdr, binary.BigEndian, &count)
if signature != GitPackSignature {
return ErrNotAPack
}
fmt.Printf("signature = %#x\n", signature)
fmt.Printf("version = %d\n", version)
fmt.Printf("count = %d\n", count)
for i :=uint32(0); i<count; i++ {
var p Ptr
rdr.Read(p.hash[:])
fmt.Printf(" [%d] %s\n", i, &p)
}
return nil
}
*/
/*
* The per-object header is a pretty dense thing, which is
* - first byte: low four bits are "size", then three bits of "type",
* and the high bit is "size continues".
* - each byte afterwards: low seven bits are size continuation,
* with the high bit being "size continues"
*/
var ErrNotAPack = errors.New("not a pack file")
const GitPackSignature = 0x5041434b