code.gitea.io/gitea@v1.19.3/modules/lfs/content_store.go (about) 1 // Copyright 2020 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package lfs 5 6 import ( 7 "crypto/sha256" 8 "encoding/hex" 9 "errors" 10 "hash" 11 "io" 12 "os" 13 14 "code.gitea.io/gitea/modules/log" 15 "code.gitea.io/gitea/modules/storage" 16 ) 17 18 var ( 19 // ErrHashMismatch occurs if the content has does not match OID 20 ErrHashMismatch = errors.New("Content hash does not match OID") 21 // ErrSizeMismatch occurs if the content size does not match 22 ErrSizeMismatch = errors.New("Content size does not match") 23 ) 24 25 // ContentStore provides a simple file system based storage. 26 type ContentStore struct { 27 storage.ObjectStorage 28 } 29 30 // NewContentStore creates the default ContentStore 31 func NewContentStore() *ContentStore { 32 contentStore := &ContentStore{ObjectStorage: storage.LFS} 33 return contentStore 34 } 35 36 // Get takes a Meta object and retrieves the content from the store, returning 37 // it as an io.ReadSeekCloser. 38 func (s *ContentStore) Get(pointer Pointer) (storage.Object, error) { 39 f, err := s.Open(pointer.RelativePath()) 40 if err != nil { 41 log.Error("Whilst trying to read LFS OID[%s]: Unable to open Error: %v", pointer.Oid, err) 42 return nil, err 43 } 44 return f, err 45 } 46 47 // Put takes a Meta object and an io.Reader and writes the content to the store. 48 func (s *ContentStore) Put(pointer Pointer, r io.Reader) error { 49 p := pointer.RelativePath() 50 51 // Wrap the provided reader with an inline hashing and size checker 52 wrappedRd := newHashingReader(pointer.Size, pointer.Oid, r) 53 54 // now pass the wrapped reader to Save - if there is a size mismatch or hash mismatch then 55 // the errors returned by the newHashingReader should percolate up to here 56 written, err := s.Save(p, wrappedRd, pointer.Size) 57 if err != nil { 58 log.Error("Whilst putting LFS OID[%s]: Failed to copy to tmpPath: %s Error: %v", pointer.Oid, p, err) 59 return err 60 } 61 62 // check again whether there is any error during the Save operation 63 // because some errors might be ignored by the Reader's caller 64 if wrappedRd.lastError != nil && !errors.Is(wrappedRd.lastError, io.EOF) { 65 err = wrappedRd.lastError 66 } else if written != pointer.Size { 67 err = ErrSizeMismatch 68 } 69 70 // if the upload failed, try to delete the file 71 if err != nil { 72 if errDel := s.Delete(p); errDel != nil { 73 log.Error("Cleaning the LFS OID[%s] failed: %v", pointer.Oid, errDel) 74 } 75 } 76 77 return err 78 } 79 80 // Exists returns true if the object exists in the content store. 81 func (s *ContentStore) Exists(pointer Pointer) (bool, error) { 82 _, err := s.ObjectStorage.Stat(pointer.RelativePath()) 83 if err != nil { 84 if os.IsNotExist(err) { 85 return false, nil 86 } 87 return false, err 88 } 89 return true, nil 90 } 91 92 // Verify returns true if the object exists in the content store and size is correct. 93 func (s *ContentStore) Verify(pointer Pointer) (bool, error) { 94 p := pointer.RelativePath() 95 fi, err := s.ObjectStorage.Stat(p) 96 if os.IsNotExist(err) || (err == nil && fi.Size() != pointer.Size) { 97 return false, nil 98 } else if err != nil { 99 log.Error("Unable stat file: %s for LFS OID[%s] Error: %v", p, pointer.Oid, err) 100 return false, err 101 } 102 103 return true, nil 104 } 105 106 // ReadMetaObject will read a git_model.LFSMetaObject and return a reader 107 func ReadMetaObject(pointer Pointer) (io.ReadCloser, error) { 108 contentStore := NewContentStore() 109 return contentStore.Get(pointer) 110 } 111 112 type hashingReader struct { 113 internal io.Reader 114 currentSize int64 115 expectedSize int64 116 hash hash.Hash 117 expectedHash string 118 lastError error 119 } 120 121 // recordError records the last error during the Save operation 122 // Some callers of the Reader doesn't respect the returned "err" 123 // For example, MinIO's Put will ignore errors if the written size could equal to expected size 124 // So we must remember the error by ourselves, 125 // and later check again whether ErrSizeMismatch or ErrHashMismatch occurs during the Save operation 126 func (r *hashingReader) recordError(err error) error { 127 r.lastError = err 128 return err 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, r.recordError(werr) 139 } 140 } 141 142 if errors.Is(err, io.EOF) || r.currentSize >= r.expectedSize { 143 if r.currentSize != r.expectedSize { 144 return n, r.recordError(ErrSizeMismatch) 145 } 146 147 shaStr := hex.EncodeToString(r.hash.Sum(nil)) 148 if shaStr != r.expectedHash { 149 return n, r.recordError(ErrHashMismatch) 150 } 151 } 152 153 return n, r.recordError(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 }