Skip to content

Commit dc7fc8b

Browse files
committed
fix Save bug caused by MinIO
1 parent b6dd567 commit dc7fc8b

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

modules/lfs/content_store.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ func (s *ContentStore) Put(pointer Pointer, r io.Reader) error {
6060
return err
6161
}
6262

63+
// check again whether there is any error during the Save operation
64+
// because some errors might be ignored by the Reader's caller
65+
if wrappedRd.lastError != nil {
66+
return wrappedRd.lastError
67+
}
68+
6369
// This shouldn't happen but it is sensible to test
6470
if written != pointer.Size {
6571
if err := s.Delete(p); err != nil {
@@ -109,6 +115,17 @@ type hashingReader struct {
109115
expectedSize int64
110116
hash hash.Hash
111117
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(n int, err error) (int, error) {
127+
r.lastError = err
128+
return n, err
112129
}
113130

114131
func (r *hashingReader) Read(b []byte) (int, error) {
@@ -118,22 +135,22 @@ func (r *hashingReader) Read(b []byte) (int, error) {
118135
r.currentSize += int64(n)
119136
wn, werr := r.hash.Write(b[:n])
120137
if wn != n || werr != nil {
121-
return n, werr
138+
return r.recordError(n, werr)
122139
}
123140
}
124141

125-
if err != nil && err == io.EOF {
142+
if errors.Is(err, io.EOF) || r.currentSize >= r.expectedSize {
126143
if r.currentSize != r.expectedSize {
127-
return n, ErrSizeMismatch
144+
return r.recordError(n, ErrSizeMismatch)
128145
}
129146

130147
shaStr := hex.EncodeToString(r.hash.Sum(nil))
131148
if shaStr != r.expectedHash {
132-
return n, ErrHashMismatch
149+
return r.recordError(n, ErrHashMismatch)
133150
}
134151
}
135152

136-
return n, err
153+
return r.recordError(n, err)
137154
}
138155

139156
func newHashingReader(expectedSize int64, expectedHash string, reader io.Reader) *hashingReader {

0 commit comments

Comments
 (0)