forked from speedata/gogit
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathrepo_utils.go
403 lines (340 loc) · 9.51 KB
/
repo_utils.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
package git
import (
"bufio"
"bytes"
csha1 "crypto/sha1"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
)
func checkIdxVersion(r io.Reader, magic []byte, version uint32) error {
var buf [8]byte
n, err := io.ReadFull(r, buf[:])
if err != nil {
return err
}
if n < len(buf) {
return errors.New("Unexpected EOF")
}
if !bytes.Equal(magic, buf[:4]) {
return fmt.Errorf("Unknown magic byte %q, expected %q", buf[:4], magic)
}
if v := binary.BigEndian.Uint32(buf[4:]); v != version {
return fmt.Errorf("Not a version %d idx file %q", version, v)
}
return nil
}
func isIdxOffsetValue64(v uint32) (bool, uint32) {
// test wether MSB is set, return that and the other 31bits
return v&(1<<31) > 0, v &^ (1 << 31)
}
func readIdxFile(path string) (*idxFile, error) {
ifile := &idxFile{}
ifile.indexpath = path
ifile.packpath = path[0:len(path)-3] + "pack"
idxf, err := os.Open(path)
if err != nil {
return nil, err
}
defer idxf.Close()
// Simulataniously calculate the checksum
sha1sum := csha1.New()
idx := io.TeeReader(idxf, sha1sum)
// check magic byte and verion
// level 0
if err = checkIdxVersion(idx, []byte{255, 't', 'O', 'c'}, 2); err != nil {
return nil, err
}
// read the complete fanout table
// we only really use the last entry for the total number of entries
// since we are putting the hashes into a map
// level 1
fanout := make([]uint32, 256)
if err = binary.Read(idx, binary.BigEndian, fanout); err != nil {
return nil, err
}
// read in all hashes. hashes should be in sorted order
// level 2
numObjects := int64(fanout[255])
ids := make([]sha1, numObjects)
for i := range ids {
// read the next sha1 hash
n, err := io.ReadFull(idx, ids[i][:])
if n < len(ids[i]) {
err = fmt.Errorf("Too short for sha1: %q", ids[i])
}
if err != nil {
return nil, err
}
}
// skip crc32 for now, only necessary for verifying the compressed
// level 3
if _, err := io.CopyN(ioutil.Discard, idx, 4*numObjects); err != nil {
return nil, err
}
// the final offsets
ifile.offsetValues = make(map[sha1]uint64, numObjects)
// the short 31 bit offsets
// MSB signals whether to use the other 31 bit as offset into the
// packfile directly, or whether it's an index for the 64 bit
// offsets in large packfiles (level 5)
// level 4
offsetValues32 := make([]uint32, numObjects)
type link struct {
id sha1 // temporarily hold the hash, so we don't have to look it up again
offset uint32 // offset into the 64 bit offset table
}
// temporarily hold hash <-> 64bit table index
var idxOffsetValues64 []link
// read the offsets and split out the large offsets
for i := range offsetValues32 {
var ov uint32
if err != binary.Read(idx, binary.BigEndian, &ov) {
return nil, err
}
if ok, ov := isIdxOffsetValue64(ov); ok {
// MSB is set, This is an index into the 64 bit table.
idxOffsetValues64 = append(idxOffsetValues64, link{ids[i], ov})
} else {
// MSB not set. We can add this offset directly.
ifile.offsetValues[ids[i]] = uint64(ov)
}
}
// assume there are as many 64bit entries as there are large offsets
// level 5
offsetValues64 := make([]uint64, len(idxOffsetValues64))
// read in the complete table of large offsets
if err != binary.Read(idx, binary.BigEndian, offsetValues64) {
return nil, err
}
// look up the large offsets, put them into the map.
for _, iov := range idxOffsetValues64 {
if int(iov.offset) >= len(offsetValues64) {
return nil, errors.New("Unexpected large index to 64bit index table")
}
ifile.offsetValues[iov.id] = offsetValues64[iov.offset]
}
// This is the sha1 hash for the associated pack file
if _, err := io.CopyN(ioutil.Discard, idx, csha1.Size); err != nil {
return nil, err
}
// finalize the sha1 of the idx file
hashCalculated := sha1sum.Sum(nil)
// we don't need to write anymore
idx = nil
// read the hash from the end of the file
hashFile := make([]byte, csha1.Size)
if _, err = io.ReadFull(idxf, hashFile); err != nil {
return nil, err
}
if !bytes.Equal(hashFile, hashCalculated) {
return nil, fmt.Errorf(`Chacksum missmatch. Got "%x", expected "%x"`, hashCalculated, hashFile)
}
// Not sure whether this should be done here.
fi, err := os.Open(ifile.packpath)
if err != nil {
return nil, err
}
defer fi.Close()
if err = checkIdxVersion(fi, []byte("PACK"), 2); err != nil {
return nil, err
}
ifile.packversion = 2
return ifile, nil
}
// If the object is stored in its own file (i.e not in a pack file),
// this function returns the full path to the object file.
// It does not test if the file exists.
func filepathFromSHA1(rootdir, sha1 string) string {
return filepath.Join(rootdir, "objects", sha1[:2], sha1[2:])
}
// The object length in a packfile is a bit more difficult than
// just reading the bytes. The first byte has the length in its
// lowest four bits, and if bit 7 is set, it means 'more' bytes
// will follow. These are added to the »left side« of the length
func readLenInPackFile(buf []byte) (length int, advance int) {
advance = 0
shift := [...]byte{0, 4, 11, 18, 25, 32, 39, 46, 53, 60}
length = int(buf[advance] & 0x0F)
for buf[advance]&0x80 > 0 {
advance += 1
length += (int(buf[advance]&0x7F) << shift[advance])
}
advance++
return
}
// Read from a pack file (given by path) at position offset. If this is a
// non-delta object, the (inflated) bytes are just returned, if the object
// is a deltafied-object, we have to apply the delta to base objects
// before hand.
func readObjectBytes(path string, indexfiles *map[string]*idxFile, offset uint64, sizeonly bool) (ot ObjectType, length int64, dataRc io.ReadCloser, err error) {
offsetInt := int64(offset)
file, err := os.Open(path)
if err != nil {
return
}
defer func() {
if err != nil || sizeonly {
if file != nil {
file.Close()
}
}
}()
pos, err := file.Seek(offsetInt, io.SeekStart)
if err != nil {
return
}
if pos != offsetInt {
err = errors.New("Seek went wrong")
return
}
buf := make([]byte, 1024)
n, err := file.Read(buf)
if err != nil {
return
}
if n == 0 {
err = errors.New("Nothing read from pack file")
return
}
ot = ObjectType(buf[0] & 0x70)
l, p := readLenInPackFile(buf)
pos = int64(p)
length = int64(l)
var baseObjectOffset uint64
switch ot {
case ObjectCommit, ObjectTree, ObjectBlob, ObjectTag:
if sizeonly {
// if we are only interested in the size of the object,
// we don't need to do more expensive stuff
return
}
_, err = file.Seek(offsetInt+pos, io.SeekStart)
if err != nil {
return
}
dataRc, err = readerDecompressed(file)
if err != nil {
return
}
dataRc = wrapReadCloser(io.LimitReader(dataRc, length), dataRc)
return
// data, err = readCompressedDataFromFile(file, offsetInt+pos, length)
case 0x60:
// DELTA_ENCODED object w/ offset to base
// Read the offset first, then calculate the starting point
// of the base object
num := int64(buf[pos]) & 0x7f
for buf[pos]&0x80 > 0 {
pos = pos + 1
num = ((num + 1) << 7) | int64(buf[pos]&0x7f)
}
baseObjectOffset = uint64(offsetInt - num)
pos = pos + 1
case 0x70:
// DELTA_ENCODED object w/ base BINARY_OBJID
var id sha1
id, err = NewId(buf[pos : pos+20])
if err != nil {
return
}
pos = pos + 20
f := (*indexfiles)[path[0:len(path)-4]+"idx"]
var ok bool
if baseObjectOffset, ok = f.offsetValues[id]; !ok {
log.Fatal("not implemented yet")
err = errors.New("base object is not exist")
return
}
}
var (
base []byte
baseRc io.ReadCloser
)
ot, _, baseRc, err = readObjectBytes(path, indexfiles, baseObjectOffset, false)
if err != nil {
return
}
defer func() {
baseRc.Close()
}()
base, err = ioutil.ReadAll(baseRc)
if err != nil {
return
}
_, err = file.Seek(offsetInt+pos, io.SeekStart)
if err != nil {
return
}
rc, err := readerDecompressed(file)
if err != nil {
return
}
zpos := 0
// This is the length of the base object. Do we need to know it?
_, bytesRead := readerLittleEndianBase128Number(rc)
//log.Println(zpos, bytesRead)
zpos += bytesRead
resultObjectLength, bytesRead := readerLittleEndianBase128Number(rc)
zpos += bytesRead
if sizeonly {
// if we are only interested in the size of the object,
// we don't need to do more expensive stuff
length = resultObjectLength
return
}
br := &readAter{base}
data, err := readerApplyDelta(br, rc, resultObjectLength)
dataRc = newBufReadCloser(data)
return
}
// Read the contents of the object file at path.
// Return the content type, the contents of the file and error, if any
func readObjectFile(path string, sizeonly bool) (ot ObjectType, length int64, dataRc io.ReadCloser, err error) {
f, err := os.Open(path)
if err != nil {
return 0, 0, nil, err
}
dataRc, err = readerDecompressed(f)
if err != nil {
f.Close()
return 0, 0, nil, err
}
// we need to buffer, otherwise Fscan can read too far
dataRc = wrapReadCloser(bufio.NewReader(dataRc), dataRc)
var t string
_, err = fmt.Fscanf(dataRc, "%s %d\x00", &t, &length)
if err != nil {
dataRc.Close()
return 0, 0, nil, err
}
if length < 0 {
dataRc.Close()
return 0, 0, nil, errors.New(`Negitive length of object file`)
}
// now wrap in LimitedReader to not read over the end
dataRc = wrapReadCloser(io.LimitReader(dataRc, length), dataRc)
switch t {
case "blob":
ot = ObjectBlob
case "tree":
ot = ObjectTree
case "commit":
ot = ObjectCommit
case "tag":
ot = ObjectTag
default:
dataRc.Close()
return 0, 0, nil, fmt.Errorf(`Unknown object type: %q`, t)
}
if sizeonly {
dataRc.Close()
dataRc = nil
}
return ot, length, dataRc, nil
}