-
Notifications
You must be signed in to change notification settings - Fork 10
/
object_db.go
397 lines (337 loc) · 9.94 KB
/
object_db.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
package gitobj
import (
"bytes"
"crypto/sha1"
"crypto/sha256"
"fmt"
"hash"
"io"
"io/ioutil"
"os"
"sync/atomic"
"github.com/git-lfs/gitobj/v2/storage"
)
// ObjectDatabase enables the reading and writing of objects against a storage
// backend.
type ObjectDatabase struct {
// members managed via sync/atomic must be aligned at the top of this
// structure (see: https://github.com/git-lfs/git-lfs/pull/2880).
// closed is a uint32 managed by sync/atomic's <X>Uint32 methods. It
// yields a value of 0 if the *ObjectDatabase it is stored upon is open,
// and a value of 1 if it is closed.
closed uint32
// ro is the locations from which we can read objects.
ro storage.Storage
// rw is the location to which we write objects.
rw storage.WritableStorage
// temp directory, defaults to os.TempDir
tmp string
// objectFormat is the object format (hash algorithm)
objectFormat ObjectFormatAlgorithm
}
type options struct {
alternates string
objectFormat ObjectFormatAlgorithm
}
type Option func(*options)
type ObjectFormatAlgorithm string
const (
ObjectFormatSHA1 = ObjectFormatAlgorithm("sha1")
ObjectFormatSHA256 = ObjectFormatAlgorithm("sha256")
)
// Alternates is an Option to specify the string of alternate repositories that
// are searched for objects. The format is the same as for
// GIT_ALTERNATE_OBJECT_DIRECTORIES.
func Alternates(alternates string) Option {
return func(args *options) {
args.alternates = alternates
}
}
// ObjectFormat is an Option to specify the hash algorithm (object format) in
// use in Git. If not specified, it defaults to ObjectFormatSHA1.
func ObjectFormat(algo ObjectFormatAlgorithm) Option {
return func(args *options) {
args.objectFormat = algo
}
}
// FromFilesystem constructs an *ObjectDatabase instance that is backed by a
// directory on the filesystem. Specifically, this should point to:
//
// /absolute/repo/path/.git/objects
func FromFilesystem(root, tmp string, setters ...Option) (*ObjectDatabase, error) {
args := &options{objectFormat: ObjectFormatSHA1}
for _, setter := range setters {
setter(args)
}
b, err := NewFilesystemBackend(root, tmp, args.alternates, hasher(args.objectFormat))
if err != nil {
return nil, err
}
odb, err := FromBackend(b, setters...)
if err != nil {
return nil, err
}
odb.tmp = tmp
return odb, nil
}
func FromBackend(b storage.Backend, setters ...Option) (*ObjectDatabase, error) {
args := &options{objectFormat: ObjectFormatSHA1}
for _, setter := range setters {
setter(args)
}
ro, rw := b.Storage()
odb := &ObjectDatabase{
ro: ro,
rw: rw,
objectFormat: args.objectFormat,
}
return odb, nil
}
// Close closes the *ObjectDatabase, freeing any open resources (namely: the
// `*git.ObjectScanner instance), and returning any errors encountered in
// closing them.
//
// If Close() has already been called, this function will return an error.
func (o *ObjectDatabase) Close() error {
if !atomic.CompareAndSwapUint32(&o.closed, 0, 1) {
return fmt.Errorf("gitobj: *ObjectDatabase already closed")
}
if err := o.ro.Close(); err != nil {
return err
}
if err := o.rw.Close(); err != nil {
return err
}
return nil
}
// Object returns an Object (of unknown implementation) satisfying the type
// associated with the object named "sha".
//
// If the object could not be opened, is of unknown type, or could not be
// decoded, than an appropriate error is returned instead.
func (o *ObjectDatabase) Object(sha []byte) (Object, error) {
r, err := o.open(sha)
if err != nil {
return nil, err
}
typ, _, err := r.Header()
if err != nil {
return nil, err
}
var into Object
switch typ {
case BlobObjectType:
into = new(Blob)
case TreeObjectType:
into = new(Tree)
case CommitObjectType:
into = new(Commit)
case TagObjectType:
into = new(Tag)
default:
return nil, fmt.Errorf("gitobj: unknown object type: %s", typ)
}
return into, o.decode(r, into)
}
// Blob returns a *Blob as identified by the SHA given, or an error if one was
// encountered.
func (o *ObjectDatabase) Blob(sha []byte) (*Blob, error) {
var b Blob
if err := o.openDecode(sha, &b); err != nil {
return nil, err
}
return &b, nil
}
// Tree returns a *Tree as identified by the SHA given, or an error if one was
// encountered.
func (o *ObjectDatabase) Tree(sha []byte) (*Tree, error) {
var t Tree
if err := o.openDecode(sha, &t); err != nil {
return nil, err
}
return &t, nil
}
// Commit returns a *Commit as identified by the SHA given, or an error if one
// was encountered.
func (o *ObjectDatabase) Commit(sha []byte) (*Commit, error) {
var c Commit
if err := o.openDecode(sha, &c); err != nil {
return nil, err
}
return &c, nil
}
// Tag returns a *Tag as identified by the SHA given, or an error if one was
// encountered.
func (o *ObjectDatabase) Tag(sha []byte) (*Tag, error) {
var t Tag
if err := o.openDecode(sha, &t); err != nil {
return nil, err
}
return &t, nil
}
// WriteBlob stores a *Blob on disk and returns the SHA it is uniquely
// identified by, or an error if one was encountered.
func (o *ObjectDatabase) WriteBlob(b *Blob) ([]byte, error) {
buf, err := ioutil.TempFile(o.tmp, "")
if err != nil {
return nil, err
}
defer o.cleanup(buf)
sha, _, err := o.encodeBuffer(b, buf)
if err != nil {
return nil, err
}
if err = b.Close(); err != nil {
return nil, err
}
return sha, nil
}
// WriteTree stores a *Tree on disk and returns the SHA it is uniquely
// identified by, or an error if one was encountered.
func (o *ObjectDatabase) WriteTree(t *Tree) ([]byte, error) {
sha, _, err := o.encode(t)
if err != nil {
return nil, err
}
return sha, nil
}
// WriteCommit stores a *Commit on disk and returns the SHA it is uniquely
// identified by, or an error if one was encountered.
func (o *ObjectDatabase) WriteCommit(c *Commit) ([]byte, error) {
sha, _, err := o.encode(c)
if err != nil {
return nil, err
}
return sha, nil
}
// WriteTag stores a *Tag on disk and returns the SHA it is uniquely identified
// by, or an error if one was encountered.
func (o *ObjectDatabase) WriteTag(t *Tag) ([]byte, error) {
sha, _, err := o.encode(t)
if err != nil {
return nil, err
}
return sha, nil
}
// Root returns the filesystem root that this *ObjectDatabase works within, if
// backed by a fileStorer (constructed by FromFilesystem). If so, it returns
// the fully-qualified path on a disk and a value of true.
//
// Otherwise, it returns empty-string and a value of false.
func (o *ObjectDatabase) Root() (string, bool) {
type rooter interface {
Root() string
}
if root, ok := o.rw.(rooter); ok {
return root.Root(), true
}
return "", false
}
// Hasher returns a new hash instance suitable for this object database.
func (o *ObjectDatabase) Hasher() hash.Hash {
return hasher(o.objectFormat)
}
// encode encodes and saves an object to the storage backend and uses an
// in-memory buffer to calculate the object's encoded body.
func (d *ObjectDatabase) encode(object Object) (sha []byte, n int64, err error) {
return d.encodeBuffer(object, bytes.NewBuffer(nil))
}
// encodeBuffer encodes and saves an object to the storage backend by using the
// given buffer to calculate and store the object's encoded body.
func (d *ObjectDatabase) encodeBuffer(object Object, buf io.ReadWriter) (sha []byte, n int64, err error) {
cn, err := object.Encode(buf)
if err != nil {
return nil, 0, err
}
tmp, err := ioutil.TempFile(d.tmp, "")
if err != nil {
return nil, 0, err
}
defer d.cleanup(tmp)
to := NewObjectWriter(tmp, d.Hasher())
if _, err = to.WriteHeader(object.Type(), int64(cn)); err != nil {
return nil, 0, err
}
if seek, ok := buf.(io.Seeker); ok {
if _, err = seek.Seek(0, io.SeekStart); err != nil {
return nil, 0, err
}
}
if _, err = io.Copy(to, buf); err != nil {
return nil, 0, err
}
if err = to.Close(); err != nil {
return nil, 0, err
}
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
return nil, 0, err
}
return d.save(to.Sha(), tmp)
}
// save writes the given buffer to the location given by the storer "o.s" as
// identified by the sha []byte.
func (o *ObjectDatabase) save(sha []byte, buf io.Reader) ([]byte, int64, error) {
n, err := o.rw.Store(sha, buf)
return sha, n, err
}
// open gives an `*ObjectReader` for the given loose object keyed by the given
// "sha" []byte, or an error.
func (o *ObjectDatabase) open(sha []byte) (*ObjectReader, error) {
if atomic.LoadUint32(&o.closed) == 1 {
return nil, fmt.Errorf("gitobj: cannot use closed *pack.Set")
}
f, err := o.ro.Open(sha)
if err != nil {
return nil, err
}
if o.ro.IsCompressed() {
return NewObjectReadCloser(f)
}
return NewUncompressedObjectReadCloser(f)
}
// openDecode calls decode (see: below) on the object named "sha" after openin
// it.
func (o *ObjectDatabase) openDecode(sha []byte, into Object) error {
r, err := o.open(sha)
if err != nil {
return err
}
return o.decode(r, into)
}
// decode decodes an object given by the sha "sha []byte" into the given object
// "into", or returns an error if one was encountered.
//
// Ordinarily, it closes the object's underlying io.ReadCloser (if it implements
// the `io.Closer` interface), but skips this if the "into" Object is of type
// BlobObjectType. Blob's don't exhaust the buffer completely (they instead
// maintain a handle on the blob's contents via an io.LimitedReader) and
// therefore cannot be closed until signaled explicitly by gitobj.Blob.Close().
func (o *ObjectDatabase) decode(r *ObjectReader, into Object) error {
typ, size, err := r.Header()
if err != nil {
return err
} else if typ != into.Type() {
return &UnexpectedObjectType{Got: typ, Wanted: into.Type()}
}
if _, err = into.Decode(o.Hasher(), r, size); err != nil {
return err
}
if into.Type() == BlobObjectType {
return nil
}
return r.Close()
}
func (o *ObjectDatabase) cleanup(f *os.File) {
f.Close()
os.Remove(f.Name())
}
func hasher(algo ObjectFormatAlgorithm) hash.Hash {
switch algo {
case ObjectFormatSHA1:
return sha1.New()
case ObjectFormatSHA256:
return sha256.New()
default:
return nil
}
}