cache: Auto-remove invalid files

This commit is contained in:
Alexander Neumann 2017-09-24 23:11:01 +02:00
parent 176bfa6529
commit e262f35d0a

View file

@ -6,6 +6,7 @@ import (
"path/filepath" "path/filepath"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/restic/restic/internal/crypto"
"github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/fs" "github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
@ -50,6 +51,18 @@ func (c *Cache) Load(h restic.Handle, length int, offset int64) (io.ReadCloser,
return nil, errors.Wrap(err, "Open") return nil, errors.Wrap(err, "Open")
} }
fi, err := f.Stat()
if err != nil {
_ = f.Close()
return nil, errors.Wrap(err, "Stat")
}
if fi.Size() <= crypto.Extension {
_ = f.Close()
_ = c.Remove(h)
return nil, errors.New("cached file is truncated, removing")
}
if offset > 0 { if offset > 0 {
if _, err = f.Seek(offset, io.SeekStart); err != nil { if _, err = f.Seek(offset, io.SeekStart); err != nil {
f.Close() f.Close()