2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-23 11:28:54 +00:00

backend: Ensure Reader is closed on error

This commit is contained in:
Alexander Neumann 2017-01-23 16:20:07 +01:00
parent 4a354befe5
commit 82d9163955
3 changed files with 7 additions and 2 deletions

View File

@ -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.

View File

@ -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")
}

View File

@ -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
}
}