| 1 |
package gitobj |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/sha1" |
| 6 |
"crypto/sha256" |
| 7 |
"fmt" |
| 8 |
"hash" |
| 9 |
"io" |
| 10 |
"io/ioutil" |
| 11 |
"os" |
| 12 |
"sync/atomic" |
| 13 |
|
| 14 |
"github.com/git-lfs/gitobj/v2/storage" |
| 15 |
) |
| 16 |
|
| 17 |
// ObjectDatabase enables the reading and writing of objects against a storage |
| 18 |
// backend. |
| 19 |
type ObjectDatabase struct { |
| 20 |
// members managed via sync/atomic must be aligned at the top of this |
| 21 |
// structure (see: https://github.com/git-lfs/git-lfs/pull/2880). |
| 22 |
|
| 23 |
// closed is a uint32 managed by sync/atomic's <X>Uint32 methods. It |
| 24 |
// yields a value of 0 if the *ObjectDatabase it is stored upon is open, |
| 25 |
// and a value of 1 if it is closed. |
| 26 |
closed uint32 |
| 27 |
|
| 28 |
// ro is the locations from which we can read objects. |
| 29 |
ro storage.Storage |
| 30 |
// rw is the location to which we write objects. |
| 31 |
rw storage.WritableStorage |
| 32 |
|
| 33 |
// temp directory, defaults to os.TempDir |
| 34 |
tmp string |
| 35 |
|
| 36 |
// objectFormat is the object format (hash algorithm) |
| 37 |
objectFormat ObjectFormatAlgorithm |
| 38 |
} |
| 39 |
|
| 40 |
type options struct { |
| 41 |
alternates string |
| 42 |
objectFormat ObjectFormatAlgorithm |
| 43 |
} |
| 44 |
|
| 45 |
type Option func(*options) |
| 46 |
|
| 47 |
type ObjectFormatAlgorithm string |
| 48 |
|
| 49 |
const ( |
| 50 |
ObjectFormatSHA1 = ObjectFormatAlgorithm("sha1") |
| 51 |
ObjectFormatSHA256 = ObjectFormatAlgorithm("sha256") |
| 52 |
) |
| 53 |
|
| 54 |
// Alternates is an Option to specify the string of alternate repositories that |
| 55 |
// are searched for objects. The format is the same as for |
| 56 |
// GIT_ALTERNATE_OBJECT_DIRECTORIES. |
| 57 |
func Alternates(alternates string) Option { |
| 58 |
return func(args *options) { |
| 59 |
args.alternates = alternates |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// ObjectFormat is an Option to specify the hash algorithm (object format) in |
| 64 |
// use in Git. If not specified, it defaults to ObjectFormatSHA1. |
| 65 |
func ObjectFormat(algo ObjectFormatAlgorithm) Option { |
| 66 |
return func(args *options) { |
| 67 |
args.objectFormat = algo |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// FromFilesystem constructs an *ObjectDatabase instance that is backed by a |
| 72 |
// directory on the filesystem. Specifically, this should point to: |
| 73 |
// |
| 74 |
// /absolute/repo/path/.git/objects |
| 75 |
func FromFilesystem(root, tmp string, setters ...Option) (*ObjectDatabase, error) { |
| 76 |
args := &options{objectFormat: ObjectFormatSHA1} |
| 77 |
|
| 78 |
for _, setter := range setters { |
| 79 |
setter(args) |
| 80 |
} |
| 81 |
|
| 82 |
b, err := NewFilesystemBackend(root, tmp, args.alternates, hasher(args.objectFormat)) |
| 83 |
if err != nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
|
| 87 |
odb, err := FromBackend(b, setters...) |
| 88 |
if err != nil { |
| 89 |
return nil, err |
| 90 |
} |
| 91 |
odb.tmp = tmp |
| 92 |
return odb, nil |
| 93 |
} |
| 94 |
|
| 95 |
func FromBackend(b storage.Backend, setters ...Option) (*ObjectDatabase, error) { |
| 96 |
args := &options{objectFormat: ObjectFormatSHA1} |
| 97 |
|
| 98 |
for _, setter := range setters { |
| 99 |
setter(args) |
| 100 |
} |
| 101 |
|
| 102 |
ro, rw := b.Storage() |
| 103 |
odb := &ObjectDatabase{ |
| 104 |
ro: ro, |
| 105 |
rw: rw, |
| 106 |
objectFormat: args.objectFormat, |
| 107 |
} |
| 108 |
return odb, nil |
| 109 |
} |
| 110 |
|
| 111 |
// Close closes the *ObjectDatabase, freeing any open resources (namely: the |
| 112 |
// `*git.ObjectScanner instance), and returning any errors encountered in |
| 113 |
// closing them. |
| 114 |
// |
| 115 |
// If Close() has already been called, this function will return an error. |
| 116 |
func (o *ObjectDatabase) Close() error { |
| 117 |
if !atomic.CompareAndSwapUint32(&o.closed, 0, 1) { |
| 118 |
return fmt.Errorf("gitobj: *ObjectDatabase already closed") |
| 119 |
} |
| 120 |
|
| 121 |
if err := o.ro.Close(); err != nil { |
| 122 |
return err |
| 123 |
} |
| 124 |
if err := o.rw.Close(); err != nil { |
| 125 |
return err |
| 126 |
} |
| 127 |
return nil |
| 128 |
} |
| 129 |
|
| 130 |
// Object returns an Object (of unknown implementation) satisfying the type |
| 131 |
// associated with the object named "sha". |
| 132 |
// |
| 133 |
// If the object could not be opened, is of unknown type, or could not be |
| 134 |
// decoded, than an appropriate error is returned instead. |
| 135 |
func (o *ObjectDatabase) Object(sha []byte) (Object, error) { |
| 136 |
r, err := o.open(sha) |
| 137 |
if err != nil { |
| 138 |
return nil, err |
| 139 |
} |
| 140 |
|
| 141 |
typ, _, err := r.Header() |
| 142 |
if err != nil { |
| 143 |
return nil, err |
| 144 |
} |
| 145 |
|
| 146 |
var into Object |
| 147 |
switch typ { |
| 148 |
case BlobObjectType: |
| 149 |
into = new(Blob) |
| 150 |
case TreeObjectType: |
| 151 |
into = new(Tree) |
| 152 |
case CommitObjectType: |
| 153 |
into = new(Commit) |
| 154 |
case TagObjectType: |
| 155 |
into = new(Tag) |
| 156 |
default: |
| 157 |
return nil, fmt.Errorf("gitobj: unknown object type: %s", typ) |
| 158 |
} |
| 159 |
return into, o.decode(r, into) |
| 160 |
} |
| 161 |
|
| 162 |
// Blob returns a *Blob as identified by the SHA given, or an error if one was |
| 163 |
// encountered. |
| 164 |
func (o *ObjectDatabase) Blob(sha []byte) (*Blob, error) { |
| 165 |
var b Blob |
| 166 |
|
| 167 |
if err := o.openDecode(sha, &b); err != nil { |
| 168 |
return nil, err |
| 169 |
} |
| 170 |
return &b, nil |
| 171 |
} |
| 172 |
|
| 173 |
// Tree returns a *Tree as identified by the SHA given, or an error if one was |
| 174 |
// encountered. |
| 175 |
func (o *ObjectDatabase) Tree(sha []byte) (*Tree, error) { |
| 176 |
var t Tree |
| 177 |
if err := o.openDecode(sha, &t); err != nil { |
| 178 |
return nil, err |
| 179 |
} |
| 180 |
return &t, nil |
| 181 |
} |
| 182 |
|
| 183 |
// Commit returns a *Commit as identified by the SHA given, or an error if one |
| 184 |
// was encountered. |
| 185 |
func (o *ObjectDatabase) Commit(sha []byte) (*Commit, error) { |
| 186 |
var c Commit |
| 187 |
|
| 188 |
if err := o.openDecode(sha, &c); err != nil { |
| 189 |
return nil, err |
| 190 |
} |
| 191 |
return &c, nil |
| 192 |
} |
| 193 |
|
| 194 |
// Tag returns a *Tag as identified by the SHA given, or an error if one was |
| 195 |
// encountered. |
| 196 |
func (o *ObjectDatabase) Tag(sha []byte) (*Tag, error) { |
| 197 |
var t Tag |
| 198 |
|
| 199 |
if err := o.openDecode(sha, &t); err != nil { |
| 200 |
return nil, err |
| 201 |
} |
| 202 |
return &t, nil |
| 203 |
} |
| 204 |
|
| 205 |
// WriteBlob stores a *Blob on disk and returns the SHA it is uniquely |
| 206 |
// identified by, or an error if one was encountered. |
| 207 |
func (o *ObjectDatabase) WriteBlob(b *Blob) ([]byte, error) { |
| 208 |
buf, err := ioutil.TempFile(o.tmp, "") |
| 209 |
if err != nil { |
| 210 |
return nil, err |
| 211 |
} |
| 212 |
defer o.cleanup(buf) |
| 213 |
|
| 214 |
sha, _, err := o.encodeBuffer(b, buf) |
| 215 |
if err != nil { |
| 216 |
return nil, err |
| 217 |
} |
| 218 |
|
| 219 |
if err = b.Close(); err != nil { |
| 220 |
return nil, err |
| 221 |
} |
| 222 |
|
| 223 |
return sha, nil |
| 224 |
} |
| 225 |
|
| 226 |
// WriteTree stores a *Tree on disk and returns the SHA it is uniquely |
| 227 |
// identified by, or an error if one was encountered. |
| 228 |
func (o *ObjectDatabase) WriteTree(t *Tree) ([]byte, error) { |
| 229 |
sha, _, err := o.encode(t) |
| 230 |
if err != nil { |
| 231 |
return nil, err |
| 232 |
} |
| 233 |
return sha, nil |
| 234 |
} |
| 235 |
|
| 236 |
// WriteCommit stores a *Commit on disk and returns the SHA it is uniquely |
| 237 |
// identified by, or an error if one was encountered. |
| 238 |
func (o *ObjectDatabase) WriteCommit(c *Commit) ([]byte, error) { |
| 239 |
sha, _, err := o.encode(c) |
| 240 |
if err != nil { |
| 241 |
return nil, err |
| 242 |
} |
| 243 |
return sha, nil |
| 244 |
} |
| 245 |
|
| 246 |
// WriteTag stores a *Tag on disk and returns the SHA it is uniquely identified |
| 247 |
// by, or an error if one was encountered. |
| 248 |
func (o *ObjectDatabase) WriteTag(t *Tag) ([]byte, error) { |
| 249 |
sha, _, err := o.encode(t) |
| 250 |
if err != nil { |
| 251 |
return nil, err |
| 252 |
} |
| 253 |
return sha, nil |
| 254 |
} |
| 255 |
|
| 256 |
// Root returns the filesystem root that this *ObjectDatabase works within, if |
| 257 |
// backed by a fileStorer (constructed by FromFilesystem). If so, it returns |
| 258 |
// the fully-qualified path on a disk and a value of true. |
| 259 |
// |
| 260 |
// Otherwise, it returns empty-string and a value of false. |
| 261 |
func (o *ObjectDatabase) Root() (string, bool) { |
| 262 |
type rooter interface { |
| 263 |
Root() string |
| 264 |
} |
| 265 |
|
| 266 |
if root, ok := o.rw.(rooter); ok { |
| 267 |
return root.Root(), true |
| 268 |
} |
| 269 |
return "", false |
| 270 |
} |
| 271 |
|
| 272 |
// Hasher returns a new hash instance suitable for this object database. |
| 273 |
func (o *ObjectDatabase) Hasher() hash.Hash { |
| 274 |
return hasher(o.objectFormat) |
| 275 |
} |
| 276 |
|
| 277 |
// encode encodes and saves an object to the storage backend and uses an |
| 278 |
// in-memory buffer to calculate the object's encoded body. |
| 279 |
func (d *ObjectDatabase) encode(object Object) (sha []byte, n int64, err error) { |
| 280 |
return d.encodeBuffer(object, bytes.NewBuffer(nil)) |
| 281 |
} |
| 282 |
|
| 283 |
// encodeBuffer encodes and saves an object to the storage backend by using the |
| 284 |
// given buffer to calculate and store the object's encoded body. |
| 285 |
func (d *ObjectDatabase) encodeBuffer(object Object, buf io.ReadWriter) (sha []byte, n int64, err error) { |
| 286 |
cn, err := object.Encode(buf) |
| 287 |
if err != nil { |
| 288 |
return nil, 0, err |
| 289 |
} |
| 290 |
|
| 291 |
tmp, err := ioutil.TempFile(d.tmp, "") |
| 292 |
if err != nil { |
| 293 |
return nil, 0, err |
| 294 |
} |
| 295 |
defer d.cleanup(tmp) |
| 296 |
|
| 297 |
to := NewObjectWriter(tmp, d.Hasher()) |
| 298 |
if _, err = to.WriteHeader(object.Type(), int64(cn)); err != nil { |
| 299 |
return nil, 0, err |
| 300 |
} |
| 301 |
|
| 302 |
if seek, ok := buf.(io.Seeker); ok { |
| 303 |
if _, err = seek.Seek(0, io.SeekStart); err != nil { |
| 304 |
return nil, 0, err |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
if _, err = io.Copy(to, buf); err != nil { |
| 309 |
return nil, 0, err |
| 310 |
} |
| 311 |
|
| 312 |
if err = to.Close(); err != nil { |
| 313 |
return nil, 0, err |
| 314 |
} |
| 315 |
|
| 316 |
if _, err := tmp.Seek(0, io.SeekStart); err != nil { |
| 317 |
return nil, 0, err |
| 318 |
} |
| 319 |
return d.save(to.Sha(), tmp) |
| 320 |
} |
| 321 |
|
| 322 |
// save writes the given buffer to the location given by the storer "o.s" as |
| 323 |
// identified by the sha []byte. |
| 324 |
func (o *ObjectDatabase) save(sha []byte, buf io.Reader) ([]byte, int64, error) { |
| 325 |
n, err := o.rw.Store(sha, buf) |
| 326 |
|
| 327 |
return sha, n, err |
| 328 |
} |
| 329 |
|
| 330 |
// open gives an `*ObjectReader` for the given loose object keyed by the given |
| 331 |
// "sha" []byte, or an error. |
| 332 |
func (o *ObjectDatabase) open(sha []byte) (*ObjectReader, error) { |
| 333 |
if atomic.LoadUint32(&o.closed) == 1 { |
| 334 |
return nil, fmt.Errorf("gitobj: cannot use closed *pack.Set") |
| 335 |
} |
| 336 |
|
| 337 |
f, err := o.ro.Open(sha) |
| 338 |
if err != nil { |
| 339 |
return nil, err |
| 340 |
} |
| 341 |
if o.ro.IsCompressed() { |
| 342 |
return NewObjectReadCloser(f) |
| 343 |
} |
| 344 |
return NewUncompressedObjectReadCloser(f) |
| 345 |
} |
| 346 |
|
| 347 |
// openDecode calls decode (see: below) on the object named "sha" after openin |
| 348 |
// it. |
| 349 |
func (o *ObjectDatabase) openDecode(sha []byte, into Object) error { |
| 350 |
r, err := o.open(sha) |
| 351 |
if err != nil { |
| 352 |
return err |
| 353 |
} |
| 354 |
return o.decode(r, into) |
| 355 |
} |
| 356 |
|
| 357 |
// decode decodes an object given by the sha "sha []byte" into the given object |
| 358 |
// "into", or returns an error if one was encountered. |
| 359 |
// |
| 360 |
// Ordinarily, it closes the object's underlying io.ReadCloser (if it implements |
| 361 |
// the `io.Closer` interface), but skips this if the "into" Object is of type |
| 362 |
// BlobObjectType. Blob's don't exhaust the buffer completely (they instead |
| 363 |
// maintain a handle on the blob's contents via an io.LimitedReader) and |
| 364 |
// therefore cannot be closed until signaled explicitly by gitobj.Blob.Close(). |
| 365 |
func (o *ObjectDatabase) decode(r *ObjectReader, into Object) error { |
| 366 |
typ, size, err := r.Header() |
| 367 |
if err != nil { |
| 368 |
return err |
| 369 |
} else if typ != into.Type() { |
| 370 |
return &UnexpectedObjectType{Got: typ, Wanted: into.Type()} |
| 371 |
} |
| 372 |
|
| 373 |
if _, err = into.Decode(o.Hasher(), r, size); err != nil { |
| 374 |
return err |
| 375 |
} |
| 376 |
|
| 377 |
if into.Type() == BlobObjectType { |
| 378 |
return nil |
| 379 |
} |
| 380 |
return r.Close() |
| 381 |
} |
| 382 |
|
| 383 |
func (o *ObjectDatabase) cleanup(f *os.File) { |
| 384 |
f.Close() |
| 385 |
os.Remove(f.Name()) |
| 386 |
} |
| 387 |
|
| 388 |
func hasher(algo ObjectFormatAlgorithm) hash.Hash { |
| 389 |
switch algo { |
| 390 |
case ObjectFormatSHA1: |
| 391 |
return sha1.New() |
| 392 |
case ObjectFormatSHA256: |
| 393 |
return sha256.New() |
| 394 |
default: |
| 395 |
return nil |
| 396 |
} |
| 397 |
} |