Debian Codegraph @

pack/packfile.go

raw
1 package pack
2
3 import (
4 "compress/zlib"
5 "fmt"
6 "hash"
7 "io"
8 "io/ioutil"
9 )
10
11 // Packfile encapsulates the behavior of accessing an unpacked representation of
12 // all of the objects encoded in a single packfile.
13 type Packfile struct {
14 // Version is the version of the packfile.
15 Version uint32
16 // Objects is the total number of objects in the packfile.
17 Objects uint32
18 // idx is the corresponding "pack-*.idx" file giving the positions of
19 // objects in this packfile.
20 idx *Index
21
22 // hash is the hash algorithm used in this pack.
23 hash hash.Hash
24
25 // r is an io.ReaderAt that allows read access to the packfile itself.
26 r io.ReaderAt
27 }
28
29 // Close closes the packfile if the underlying data stream is closeable. If so,
30 // it returns any error involved in closing.
31 func (p *Packfile) Close() error {
32 var iErr error
33 if p.idx != nil {
34 iErr = p.idx.Close()
35 }
36
37 if close, ok := p.r.(io.Closer); ok {
38 return close.Close()
39 }
40 return iErr
41 }
42
43 // Object returns a reference to an object packed in the receiving *Packfile. It
44 // does not attempt to unpack the packfile, rather, that is accomplished by
45 // calling Unpack() on the returned *Object.
46 //
47 // If there was an error loading or buffering the base, it will be returned
48 // without an object.
49 //
50 // If the object given by the SHA-1 name, "name", could not be found,
51 // (nil, errNotFound) will be returned.
52 //
53 // If the object was able to be loaded successfully, it will be returned without
54 // any error.
55 func (p *Packfile) Object(name []byte) (*Object, error) {
56 // First, try and determine the offset of the last entry in the
57 // delta-base chain by loading it from the corresponding pack index.
58 entry, err := p.idx.Entry(name)
59 if err != nil {
60 if !IsNotFound(err) {
61 // If the error was not an errNotFound, re-wrap it with
62 // additional context.
63 err = fmt.Errorf("gitobj/pack: could not load index: %s", err)
64 }
65 return nil, err
66 }
67
68 // If all goes well, then unpack the object at that given offset.
69 r, err := p.find(int64(entry.PackOffset))
70 if err != nil {
71 return nil, err
72 }
73
74 return &Object{
75 data: r,
76 typ: r.Type(),
77 }, nil
78 }
79
80 // find finds and returns a Chain element corresponding to the offset of its
81 // last element as given by the "offset" argument.
82 //
83 // If find returns a ChainBase, it loads that data into memory, but does not
84 // zlib-flate it. Otherwise, if find returns a ChainDelta, it loads all of the
85 // leading elements in the chain recursively, but does not apply one delta to
86 // another.
87 func (p *Packfile) find(offset int64) (Chain, error) {
88 // Read the first byte in the chain element.
89 buf := make([]byte, 1)
90 if _, err := p.r.ReadAt(buf, offset); err != nil {
91 return nil, err
92 }
93
94 // Store the original offset; this will be compared to when loading
95 // chain elements of type OBJ_OFS_DELTA.
96 objectOffset := offset
97
98 // Of the first byte, (0123 4567):
99 // - Bit 0 is the M.S.B., and indicates whether there is more data
100 // encoded in the length.
101 // - Bits 1-3 ((buf[0] >> 4) & 0x7) are the object type.
102 // - Bits 4-7 (buf[0] & 0xf) are the first 4 bits of the variable
103 // length size of the encoded delta or base.
104 typ := PackedObjectType((buf[0] >> 4) & 0x7)
105 size := uint64(buf[0] & 0xf)
106 shift := uint(4)
107 offset += 1
108
109 for buf[0]&0x80 != 0 {
110 // If there is more data to be read, read it.
111 if _, err := p.r.ReadAt(buf, offset); err != nil {
112 return nil, err
113 }
114
115 // And update the size, bitshift, and offset accordingly.
116 size |= (uint64(buf[0]&0x7f) << shift)
117 shift += 7
118 offset += 1
119 }
120
121 switch typ {
122 case TypeObjectOffsetDelta, TypeObjectReferenceDelta:
123 // If the type of delta-base element is a delta, (either
124 // OBJ_OFS_DELTA, or OBJ_REFS_DELTA), we must load the base,
125 // which itself could be either of the two above, or a
126 // OBJ_COMMIT, OBJ_BLOB, etc.
127 //
128 // Recursively load the base, and keep track of the updated
129 // offset.
130 base, offset, err := p.findBase(typ, offset, objectOffset)
131 if err != nil {
132 return nil, err
133 }
134
135 // Now load the delta to apply to the base, given at the offset
136 // "offset" and for length "size".
137 //
138 // NB: The delta instructions are zlib compressed, so ensure
139 // that we uncompress the instructions first.
140 zr, err := zlib.NewReader(&OffsetReaderAt{
141 o: offset,
142 r: p.r,
143 })
144 if err != nil {
145 return nil, err
146 }
147
148 delta, err := ioutil.ReadAll(zr)
149 if err != nil {
150 return nil, err
151 }
152
153 // Then compose the two and return it as a *ChainDelta.
154 return &ChainDelta{
155 base: base,
156 delta: delta,
157 }, nil
158 case TypeCommit, TypeTree, TypeBlob, TypeTag:
159 // Otherwise, the object's contents are given to be the
160 // following zlib-compressed data.
161 //
162 // The length of the compressed data itself is not known,
163 // rather, "size" determines the length of the data after
164 // inflation.
165 return &ChainBase{
166 offset: offset,
167 size: int64(size),
168 typ: typ,
169
170 r: p.r,
171 }, nil
172 }
173 // Otherwise, we received an invalid object type.
174 return nil, errUnrecognizedObjectType
175 }
176
177 // findBase finds the base (an object, or another delta) for a given
178 // OBJ_OFS_DELTA or OBJ_REFS_DELTA at the given offset.
179 //
180 // It returns the preceding Chain, as well as an updated read offset into the
181 // underlying packfile data.
182 //
183 // If any of the above could not be completed successfully, findBase returns an
184 // error.
185 func (p *Packfile) findBase(typ PackedObjectType, offset, objOffset int64) (Chain, int64, error) {
186 var baseOffset int64
187
188 hashlen := p.hash.Size()
189
190 // We assume that we have to read at least an object ID's worth (the
191 // hash length in the case of a OBJ_REF_DELTA, or greater than the
192 // length of the base offset encoded in an OBJ_OFS_DELTA).
193 var sha [MaxHashSize]byte
194 if _, err := p.r.ReadAt(sha[:hashlen], offset); err != nil {
195 return nil, baseOffset, err
196 }
197
198 switch typ {
199 case TypeObjectOffsetDelta:
200 // If the object is of type OBJ_OFS_DELTA, read a
201 // variable-length integer, and find the object at that
202 // location.
203 i := 0
204 c := int64(sha[i])
205 baseOffset = c & 0x7f
206
207 for c&0x80 != 0 {
208 i += 1
209 c = int64(sha[i])
210
211 baseOffset += 1
212 baseOffset <<= 7
213 baseOffset |= c & 0x7f
214 }
215
216 baseOffset = objOffset - baseOffset
217 offset += int64(i) + 1
218 case TypeObjectReferenceDelta:
219 // If the delta is an OBJ_REFS_DELTA, find the location of its
220 // base by reading the SHA-1 name and looking it up in the
221 // corresponding pack index file.
222 e, err := p.idx.Entry(sha[:hashlen])
223 if err != nil {
224 return nil, baseOffset, err
225 }
226
227 baseOffset = int64(e.PackOffset)
228 offset += int64(hashlen)
229 default:
230 // If we did not receive an OBJ_OFS_DELTA, or OBJ_REF_DELTA, the
231 // type given is not a delta-fied type. Return an error.
232 return nil, baseOffset, fmt.Errorf(
233 "gitobj/pack: type %s is not deltafied", typ)
234 }
235
236 // Once we have determined the base offset of the object's chain base,
237 // read the delta-base chain beginning at that offset.
238 r, err := p.find(baseOffset)
239 return r, offset, err
240 }