diff --git a/changelog/unreleased/issue-1935 b/changelog/unreleased/issue-1935 new file mode 100644 index 000000000..4edc64a92 --- /dev/null +++ b/changelog/unreleased/issue-1935 @@ -0,0 +1,8 @@ +Bugfix: Remove truncated files from cache + +When a file in the local cache is truncated, and restic tries to access data +beyond the end of the (cached) file, it used to return an error "EOF". This is +now fixed, such truncated files are removed and the data is fetched directly +from the backend. + +https://github.com/restic/restic/issues/1935 diff --git a/internal/cache/file.go b/internal/cache/file.go index f232bc7c1..32ad237bf 100644 --- a/internal/cache/file.go +++ b/internal/cache/file.go @@ -63,6 +63,12 @@ func (c *Cache) Load(h restic.Handle, length int, offset int64) (io.ReadCloser, return nil, errors.Errorf("cached file %v is truncated, removing", h) } + if fi.Size() < offset+int64(length) { + _ = f.Close() + _ = c.Remove(h) + return nil, errors.Errorf("cached file %v is too small, removing", h) + } + if offset > 0 { if _, err = f.Seek(offset, io.SeekStart); err != nil { _ = f.Close() diff --git a/internal/cache/file_test.go b/internal/cache/file_test.go index 35ce762c0..cf275c84e 100644 --- a/internal/cache/file_test.go +++ b/internal/cache/file_test.go @@ -218,7 +218,7 @@ func TestFileLoad(t *testing.T) { {32*1024 + 5, 0}, {0, 123}, {0, 64*1024 + 234}, - {100, 5234142}, + {100, 5234142 - 100}, } for _, test := range tests {