github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/lfs/content_store.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package lfs 7 8 import ( 9 "crypto/sha256" 10 "encoding/hex" 11 "errors" 12 "fmt" 13 "hash" 14 "io" 15 "os" 16 17 "github.com/gitbundle/modules/log" 18 "github.com/gitbundle/modules/storage" 19 ) 20 21 var ( 22 // ErrHashMismatch occurs if the content has does not match OID 23 ErrHashMismatch = errors.New("Content hash does not match OID") 24 // ErrSizeMismatch occurs if the content size does not match 25 ErrSizeMismatch = errors.New("Content size does not match") 26 ) 27 28 // ErrRangeNotSatisfiable represents an error which request range is not satisfiable. 29 type ErrRangeNotSatisfiable struct { 30 FromByte int64 31 } 32 33 // IsErrRangeNotSatisfiable returns true if the error is an ErrRangeNotSatisfiable 34 func IsErrRangeNotSatisfiable(err error) bool { 35 _, ok := err.(ErrRangeNotSatisfiable) 36 return ok 37 } 38 39 func (err ErrRangeNotSatisfiable) Error() string { 40 return fmt.Sprintf("Requested range %d is not satisfiable", err.FromByte) 41 } 42 43 // ContentStore provides a simple file system based storage. 44 type ContentStore struct { 45 storage.ObjectStorage 46 } 47 48 // NewContentStore creates the default ContentStore 49 func NewContentStore() *ContentStore { 50 contentStore := &ContentStore{ObjectStorage: storage.LFS} 51 return contentStore 52 } 53 54 // Get takes a Meta object and retrieves the content from the store, returning 55 // it as an io.ReadSeekCloser. 56 func (s *ContentStore) Get(pointer Pointer) (storage.Object, error) { 57 f, err := s.Open(pointer.RelativePath()) 58 if err != nil { 59 log.Error("Whilst trying to read LFS OID[%s]: Unable to open Error: %v", pointer.Oid, err) 60 return nil, err 61 } 62 return f, err 63 } 64 65 // Put takes a Meta object and an io.Reader and writes the content to the store. 66 func (s *ContentStore) Put(pointer Pointer, r io.Reader) error { 67 p := pointer.RelativePath() 68 69 // Wrap the provided reader with an inline hashing and size checker 70 wrappedRd := newHashingReader(pointer.Size, pointer.Oid, r) 71 72 // now pass the wrapped reader to Save - if there is a size mismatch or hash mismatch then 73 // the errors returned by the newHashingReader should percolate up to here 74 written, err := s.Save(p, wrappedRd, pointer.Size) 75 if err != nil { 76 log.Error("Whilst putting LFS OID[%s]: Failed to copy to tmpPath: %s Error: %v", pointer.Oid, p, err) 77 return err 78 } 79 80 // This shouldn't happen but it is sensible to test 81 if written != pointer.Size { 82 if err := s.Delete(p); err != nil { 83 log.Error("Cleaning the LFS OID[%s] failed: %v", pointer.Oid, err) 84 } 85 return ErrSizeMismatch 86 } 87 88 return nil 89 } 90 91 // Exists returns true if the object exists in the content store. 92 func (s *ContentStore) Exists(pointer Pointer) (bool, error) { 93 _, err := s.ObjectStorage.Stat(pointer.RelativePath()) 94 if err != nil { 95 if os.IsNotExist(err) { 96 return false, nil 97 } 98 return false, err 99 } 100 return true, nil 101 } 102 103 // Verify returns true if the object exists in the content store and size is correct. 104 func (s *ContentStore) Verify(pointer Pointer) (bool, error) { 105 p := pointer.RelativePath() 106 fi, err := s.ObjectStorage.Stat(p) 107 if os.IsNotExist(err) || (err == nil && fi.Size() != pointer.Size) { 108 return false, nil 109 } else if err != nil { 110 log.Error("Unable stat file: %s for LFS OID[%s] Error: %v", p, pointer.Oid, err) 111 return false, err 112 } 113 114 return true, nil 115 } 116 117 // ReadMetaObject will read a git_model.LFSMetaObject and return a reader 118 func ReadMetaObject(pointer Pointer) (io.ReadCloser, error) { 119 contentStore := NewContentStore() 120 return contentStore.Get(pointer) 121 } 122 123 type hashingReader struct { 124 internal io.Reader 125 currentSize int64 126 expectedSize int64 127 hash hash.Hash 128 expectedHash string 129 } 130 131 func (r *hashingReader) Read(b []byte) (int, error) { 132 n, err := r.internal.Read(b) 133 134 if n > 0 { 135 r.currentSize += int64(n) 136 wn, werr := r.hash.Write(b[:n]) 137 if wn != n || werr != nil { 138 return n, werr 139 } 140 } 141 142 if err != nil && err == io.EOF { 143 if r.currentSize != r.expectedSize { 144 return n, ErrSizeMismatch 145 } 146 147 shaStr := hex.EncodeToString(r.hash.Sum(nil)) 148 if shaStr != r.expectedHash { 149 return n, ErrHashMismatch 150 } 151 } 152 153 return n, err 154 } 155 156 func newHashingReader(expectedSize int64, expectedHash string, reader io.Reader) *hashingReader { 157 return &hashingReader{ 158 internal: reader, 159 expectedSize: expectedSize, 160 expectedHash: expectedHash, 161 hash: sha256.New(), 162 } 163 }