diff --git a/src/restic/backend.go b/src/restic/backend.go index 9e0a4b47c..af3e5b6dc 100644 --- a/src/restic/backend.go +++ b/src/restic/backend.go @@ -28,7 +28,8 @@ type Backend interface { // Get returns a reader that yields the contents of the file at h at the // given offset. If length is nonzero, only a portion of the file is - // returned. rd must be closed after use. + // returned. rd must be closed after use. If an error is returned, the + // ReadCloser must be nil. Get(h Handle, length int, offset int64) (io.ReadCloser, error) // Stat returns information about the File identified by h. diff --git a/src/restic/backend/s3/s3.go b/src/restic/backend/s3/s3.go index 793750009..0ad1cf91f 100644 --- a/src/restic/backend/s3/s3.go +++ b/src/restic/backend/s3/s3.go @@ -214,6 +214,7 @@ func (be *s3) Get(h restic.Handle, length int, offset int64) (io.ReadCloser, err debug.Log("Get %v: pass on object", h) _, err = obj.Seek(offset, 0) if err != nil { + _ = obj.Close() return nil, errors.Wrap(err, "obj.Seek") } @@ -223,10 +224,12 @@ func (be *s3) Get(h restic.Handle, length int, offset int64) (io.ReadCloser, err // otherwise use a buffer with ReadAt info, err := obj.Stat() if err != nil { + _ = obj.Close() return nil, errors.Wrap(err, "obj.Stat") } if offset > info.Size { + _ = obj.Close() return nil, errors.Errorf("offset larger than file size") } @@ -245,6 +248,7 @@ func (be *s3) Get(h restic.Handle, length int, offset int64) (io.ReadCloser, err } if err != nil { + _ = obj.Close() return nil, errors.Wrap(err, "obj.ReadAt") } diff --git a/src/restic/backend/sftp/sftp.go b/src/restic/backend/sftp/sftp.go index 4dee53b37..c49126b41 100644 --- a/src/restic/backend/sftp/sftp.go +++ b/src/restic/backend/sftp/sftp.go @@ -419,7 +419,7 @@ func (r *SFTP) Get(h restic.Handle, length int, offset int64) (io.ReadCloser, er if offset > 0 { _, err = f.Seek(offset, 0) if err != nil { - f.Close() + _ = f.Close() return nil, err } }