forked from TrueCloudLab/restic
backends: add sanity check for the uploaded file size
Bugs in the error handling while uploading a file to the backend could cause incomplete files, e.g. https://github.com/golang/go/issues/42400 which could affect the local backend. Proactively add sanity checks which will treat an upload as failed if the reported upload size does not match the actual file size.
This commit is contained in:
parent
4526d5d197
commit
c73316a111
7 changed files with 40 additions and 4 deletions
|
@ -118,11 +118,16 @@ func (b *Local) Save(ctx context.Context, h restic.Handle, rd restic.RewindReade
|
|||
}
|
||||
|
||||
// save data, then sync
|
||||
_, err = io.Copy(f, rd)
|
||||
wbytes, err := io.Copy(f, rd)
|
||||
if err != nil {
|
||||
_ = f.Close()
|
||||
return errors.Wrap(err, "Write")
|
||||
}
|
||||
// sanity check
|
||||
if wbytes != rd.Length() {
|
||||
_ = f.Close()
|
||||
return errors.Errorf("wrote %d bytes instead of the expected %d bytes", wbytes, rd.Length())
|
||||
}
|
||||
|
||||
if err = f.Sync(); err != nil {
|
||||
pathErr, ok := err.(*os.PathError)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue