Fix content copy to not ignore unexpected EOF

Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
Derek McGowan 2021-09-09 09:52:19 -07:00
parent ff75f7d489
commit 2458afeb13
No known key found for this signature in database
GPG Key ID: F58C5D0A4405ACDB
2 changed files with 17 additions and 4 deletions

View File

@ -144,9 +144,14 @@ func Copy(ctx context.Context, cw Writer, r io.Reader, size int64, expected dige
}
}
if _, err := copyWithBuffer(cw, r); err != nil {
copied, err := copyWithBuffer(cw, r)
if err != nil {
return errors.Wrap(err, "failed to copy")
}
if size != 0 && copied < size-ws.Offset {
// Short writes would return its own error, this indicates a read failure
return errors.Wrapf(io.ErrUnexpectedEOF, "failed to read expected number of bytes")
}
if err := cw.Commit(ctx, size, expected, opts...); err != nil {
if !errdefs.IsAlreadyExists(err) {
@ -165,8 +170,15 @@ func CopyReaderAt(cw Writer, ra ReaderAt, n int64) error {
return err
}
_, err = copyWithBuffer(cw, io.NewSectionReader(ra, ws.Offset, n))
return err
copied, err := copyWithBuffer(cw, io.NewSectionReader(ra, ws.Offset, n))
if err != nil {
return errors.Wrap(err, "failed to copy")
}
if copied < n {
// Short writes would return its own error, this indicates a read failure
return errors.Wrap(io.ErrUnexpectedEOF, "failed to read expected number of bytes")
}
return nil
}
// CopyReader copies to a writer from a given reader, returning

View File

@ -65,10 +65,11 @@ func TestCopy(t *testing.T) {
},
{
name: "commit already exists",
source: defaultSource,
source: newCopySource("this already exists"),
writer: fakeWriter{commitFunc: func() error {
return errdefs.ErrAlreadyExists
}},
expected: "this already exists",
},
}