-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackfile.go
312 lines (285 loc) · 7.65 KB
/
packfile.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
package g
import (
"compress/zlib"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"path/filepath"
)
type (
PackObjectType uint8
)
const (
_ PackObjectType = iota
ObjCommit
ObjTree
ObjBlob
ObjTag
ObjOfsDelta
ObjRefDelta
)
// PackFileReadCloser is a Factory that creates a ReadCloser for reading Object
// content from a Pack File that is Not Deltified.
var PackFileReadCloser = func(path string, offset int64) func() (io.ReadCloser, error) {
return func() (io.ReadCloser, error) {
fh, err := os.Open(path)
if err != nil {
return nil, err
}
defer func() { _ = fh.Close() }()
_, err = fh.Seek(offset, io.SeekStart)
if err != nil {
return nil, err
}
return zlib.NewReader(fh)
}
}
var PackFileReadCloserRefDelta = func(path string, offset int64) func() (io.ReadCloser, error) {
return func() (io.ReadCloser, error) {
return nil, errors.New("not implemented")
}
}
var PackFileReadCloserOfsDelta = func(path string, offset int64) func() (io.ReadCloser, error) {
return func() (io.ReadCloser, error) {
return nil, errors.New("not implemented")
}
}
func lookupInPackfiles(sha Sha) (*Object, error) {
var packFiles []string
// find the available pack files
if err := filepath.Walk(
ObjectPackfileDirectory(),
func(path string, info os.FileInfo, err error) error {
if info == nil {
return nil
}
if info.IsDir() {
return nil
}
path = filepath.Base(path)
if filepath.Ext(info.Name()) == ".idx" {
packFiles = append(packFiles, path[5:len(path)-4])
}
return nil
},
); err != nil {
return nil, err
}
// check each pack file index for the sha
for _, v := range packFiles {
offset, found, err := findOffsetInIdx(sha, filepath.Join(ObjectPackfileDirectory(), fmt.Sprintf("pack-%s.idx", v)))
if err != nil {
return nil, err
}
if found {
return findObjectInPack(offset, filepath.Join(ObjectPackfileDirectory(), fmt.Sprintf("pack-%s.pack", v)), sha)
}
}
return nil, nil
}
func readIdxMagic(fh *os.File) error {
magic := make([]byte, 4)
if err := binary.Read(fh, binary.BigEndian, magic); err != nil {
return err
}
// check magic bytes
if magic[0] != 255 || magic[1] != 116 || magic[2] != 79 || magic[3] != 99 {
return errors.New("invalid packfile index magic bytes")
}
return nil
}
func readPackMagic(fh *os.File) error {
magic := make([]byte, 4)
if err := binary.Read(fh, binary.BigEndian, magic); err != nil {
return err
}
// check magic bytes
if magic[0] != 80 || magic[1] != 65 || magic[2] != 67 || magic[3] != 75 {
return errors.New("invalid packfile index magic bytes")
}
return nil
}
func readIdxFormat(fh *os.File) (uint32, error) {
var format uint32
if err := binary.Read(fh, binary.BigEndian, &format); err != nil {
return 0, err
}
return format, nil
}
func readPackFormat(fh *os.File) (uint32, error) {
var format uint32
if err := binary.Read(fh, binary.BigEndian, &format); err != nil {
return 0, err
}
return format, nil
}
func readFanout(fh *os.File) ([256]uint32, error) {
// fanout is an array off jump offsets for the first byte of a sha
// this allows us to search for a sha faster, by starting closer.
var fanout [256]uint32
err := binary.Read(fh, binary.BigEndian, &fanout)
return fanout, err
}
func findObjectName(items uint32, fh *os.File, sha Sha) (uint32, bool, error) {
var hash [20]byte
// should be an efficiently implemented binary search,
// for now a blunt force string trauma
for i := uint32(0); i < items; i++ {
if err := binary.Read(fh, binary.BigEndian, &hash); err != nil {
return i, false, err
}
h, err := NewSha(hash[:])
if err != nil {
return i, false, err
}
if h.AsHexString() == sha.AsHexString() {
return i, true, nil
}
}
return 0, false, nil
}
func readObjectOffset(size uint32, fh *os.File, i uint32) (uint32, error) {
// skip remaining sorted object names
// skip 4-byte CRC32 values (*size)
// skip to i offset in 4 byte offset values
// @todo if offset most significant bit is set, lookupInPackfiles in long offset table
if _, err := fh.Seek(int64(4+4+(256*4)+(20*size)+(4*size)+(4*i)), io.SeekStart); err != nil {
return 0, err
}
var offset uint32
if err := binary.Read(fh, binary.BigEndian, &offset); err != nil {
return 0, err
}
// we now have the offset to lookupInPackfiles in the pack
return offset, nil
}
func findOffsetInIdx(sha Sha, path string) (uint32, bool, error) {
fh, err := os.Open(path)
if err != nil {
return 0, false, err
}
defer func() { _ = fh.Close() }()
// read the magic bytes to check correct
if err := readIdxMagic(fh); err != nil {
return 0, false, err
}
// read the idx format and assert it is 2
if format, err := readIdxFormat(fh); err != nil || format != 2 {
if err != nil {
return 0, false, err
} else {
return 0, false, errors.New("invalid pack file idx format, expected 2")
}
}
// read fanout buckets
fanout, err := readFanout(fh)
if err != nil {
return 0, false, err
}
// lookupInPackfiles search bounds
var startOffset uint32
if sha.hash[0] == 0 {
startOffset = 0
} else {
startOffset = fanout[sha.hash[0]-1]
}
endOffset := fanout[sha.hash[0]]
size := fanout[255]
// to make the search more efficient, we can jump to the start
// address of this sha 1st byte bucket.
if _, err := fh.Seek(int64(startOffset*20), io.SeekCurrent); err != nil {
return 0, false, err
}
i, found, err := findObjectName(endOffset-startOffset, fh, sha)
if err != nil {
return 0, false, err
}
if !found {
return 0, false, nil
}
offset, err := readObjectOffset(size, fh, i+startOffset)
return offset, found, err
}
func findObjectInPack(offset uint32, path string, sha Sha) (*Object, error) {
fh, err := os.Open(path)
if err != nil {
return nil, err
}
defer func() { _ = fh.Close() }()
if err := readPackMagic(fh); err != nil {
return nil, err
}
// read the idx format and assert it is 2
if format, err := readPackFormat(fh); err != nil || format != 2 {
if err != nil {
return nil, err
} else {
return nil, errors.New("invalid pack file format, expected 2")
}
}
var size uint32
if err := binary.Read(fh, binary.BigEndian, &size); err != nil {
return nil, err
}
if _, err := fh.Seek(int64(offset), io.SeekStart); err != nil {
return nil, err
}
typ, length, err := readPackTypeLength(fh)
if err != nil {
return nil, err
}
obj := &Object{}
switch typ {
case ObjBlob, ObjOfsDelta, ObjRefDelta:
obj.Typ = ObjectTypeBlob
case ObjCommit, ObjTag:
obj.Typ = ObjectTypeCommit
case ObjTree:
obj.Typ = ObjectTypeTree
}
obj.Sha = sha
obj.Length = int(length)
// This HeaderLength was added before I knew about pack files,
// the purpose was to create a factory that allowed a reader to
// be initialized if required. The HeaderLength bytes are discarded
// before a ReadCloser implementation streams object content via zlib.
// For pack files this still makes sense but here we set it to 0 and
// include the seeking as part of the ReadCloser factory config.
p, err := fh.Seek(0, io.SeekCurrent)
if err != nil {
return nil, err
}
obj.HeaderLength = 0
switch typ {
case ObjOfsDelta:
obj.ReadCloser = PackFileReadCloserOfsDelta(path, p)
case ObjRefDelta:
obj.ReadCloser = PackFileReadCloserRefDelta(path, p)
default:
obj.ReadCloser = PackFileReadCloser(path, p)
}
return obj, nil
}
func readPackTypeLength(fh *os.File) (PackObjectType, uint64, error) {
var v uint8
var t PackObjectType
var l uint64
for i := 0; i < 9; i++ {
if err := binary.Read(fh, binary.BigEndian, &v); err != nil {
return 0, 0, err
}
if i == 0 {
t = PackObjectType(v & 0b01110000 >> 4)
l = uint64(v & 0b00001111)
} else {
l |= uint64(v&0b01111111) << (4 + ((i - 1) * 7))
}
if v&0b10000000 == 0 {
// no continue bit set
break
}
}
return t, l, nil
}