Merge pull request #5026 from MichaelEischer/fix-handling-invalid-filenames

cache: Fix handling of invalid filenames
This commit is contained in:
Michael Eischer 2024-08-31 16:42:13 +02:00 committed by GitHub
commit ddf35a60ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 30 additions and 8 deletions

View file

@ -2,6 +2,8 @@ package main
import ( import (
"context" "context"
"os"
"path/filepath"
"testing" "testing"
"github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/repository"
@ -16,6 +18,11 @@ func testRunInit(t testing.TB, opts GlobalOptions) {
rtest.OK(t, runInit(context.TODO(), InitOptions{}, opts, nil)) rtest.OK(t, runInit(context.TODO(), InitOptions{}, opts, nil))
t.Logf("repository initialized at %v", opts.Repo) t.Logf("repository initialized at %v", opts.Repo)
// create temporary junk files to verify that restic does not trip over them
for _, path := range []string{"index", "snapshots", "keys", "locks", filepath.Join("data", "00")} {
rtest.OK(t, os.WriteFile(filepath.Join(opts.Repo, path, "tmp12345"), []byte("junk file"), 0o600))
}
} }
func TestInitCopyChunkerParams(t *testing.T) { func TestInitCopyChunkerParams(t *testing.T) {

View file

@ -146,10 +146,9 @@ func TestPruneWithDamagedRepository(t *testing.T) {
env.gopts.backendTestHook = oldHook env.gopts.backendTestHook = oldHook
}() }()
// prune should fail // prune should fail
rtest.Assert(t, withTermStatus(env.gopts, func(ctx context.Context, term *termstatus.Terminal) error { rtest.Equals(t, repository.ErrPacksMissing, withTermStatus(env.gopts, func(ctx context.Context, term *termstatus.Terminal) error {
return runPrune(context.TODO(), pruneDefaultOptions, env.gopts, term) return runPrune(context.TODO(), pruneDefaultOptions, env.gopts, term)
}) == repository.ErrPacksMissing, }), "prune should have reported index not complete error")
"prune should have reported index not complete error")
} }
// Test repos for edge cases // Test repos for edge cases

View file

@ -80,7 +80,7 @@ func TestListOnce(t *testing.T) {
defer cleanup() defer cleanup()
env.gopts.backendTestHook = func(r backend.Backend) (backend.Backend, error) { env.gopts.backendTestHook = func(r backend.Backend) (backend.Backend, error) {
return newListOnceBackend(r), nil return newOrderedListOnceBackend(r), nil
} }
pruneOpts := PruneOptions{MaxUnused: "0"} pruneOpts := PruneOptions{MaxUnused: "0"}
checkOpts := CheckOptions{ReadData: true, CheckUnused: true} checkOpts := CheckOptions{ReadData: true, CheckUnused: true}
@ -148,7 +148,7 @@ func TestFindListOnce(t *testing.T) {
defer cleanup() defer cleanup()
env.gopts.backendTestHook = func(r backend.Backend) (backend.Backend, error) { env.gopts.backendTestHook = func(r backend.Backend) (backend.Backend, error) {
return newListOnceBackend(r), nil return newOrderedListOnceBackend(r), nil
} }
testSetupBackupData(t, env) testSetupBackupData(t, env)

View file

@ -231,9 +231,8 @@ func (b *Backend) List(ctx context.Context, t backend.FileType, fn func(f backen
wrapFn := func(f backend.FileInfo) error { wrapFn := func(f backend.FileInfo) error {
id, err := restic.ParseID(f.Name) id, err := restic.ParseID(f.Name)
if err != nil { if err != nil {
// returning error here since, if we cannot parse the ID, the file // ignore files with invalid name
// is invalid and the list must exit. return nil
return err
} }
ids.Insert(id) ids.Insert(id)

View file

@ -296,3 +296,20 @@ func TestAutomaticCacheClear(t *testing.T) {
t.Errorf("cache doesn't have file2 after list") t.Errorf("cache doesn't have file2 after list")
} }
} }
func TestAutomaticCacheClearInvalidFilename(t *testing.T) {
be := mem.New()
c := TestNewCache(t)
data := test.Random(rand.Int(), 42)
h := backend.Handle{
Type: backend.IndexFile,
Name: "tmp12345",
}
save(t, be, h, data)
wbe := c.Wrap(be)
// list all files in the backend
list(t, wbe, func(_ backend.FileInfo) error { return nil })
}