backends: pass error logger to backends

This commit is contained in:
Michael Eischer
2025-09-14 16:13:21 +02:00
parent 13f743e26b
commit 4dc71f24c5
31 changed files with 96 additions and 96 deletions
+4 -4
View File
@@ -2,9 +2,7 @@ package cache
import (
"context"
"fmt"
"io"
"os"
"sync"
"github.com/restic/restic/internal/backend"
@@ -22,16 +20,18 @@ type Backend struct {
// is finished.
inProgressMutex sync.Mutex
inProgress map[backend.Handle]chan struct{}
errorLog func(string, ...interface{})
}
// ensure Backend implements backend.Backend
var _ backend.Backend = &Backend{}
func newBackend(be backend.Backend, c *Cache) *Backend {
func newBackend(be backend.Backend, c *Cache, errorLog func(string, ...interface{})) *Backend {
return &Backend{
Backend: be,
Cache: c,
inProgress: make(map[backend.Handle]chan struct{}),
errorLog: errorLog,
}
}
@@ -253,7 +253,7 @@ func (b *Backend) List(ctx context.Context, t backend.FileType, fn func(f backen
// clear the cache for files that are not in the repo anymore, ignore errors
err = b.Cache.Clear(t, ids)
if err != nil {
fmt.Fprintf(os.Stderr, "error clearing %s files in cache: %v\n", t.String(), err)
b.errorLog("error clearing %s files in cache: %v\n", t.String(), err)
}
return nil
+6 -6
View File
@@ -67,7 +67,7 @@ func list(t testing.TB, be backend.Backend, fn func(backend.FileInfo) error) {
func TestBackend(t *testing.T) {
be := mem.New()
c := TestNewCache(t)
wbe := c.Wrap(be)
wbe := c.Wrap(be, t.Logf)
h, data := randomData(5234142)
@@ -135,7 +135,7 @@ func (l *loadCountingBackend) Load(ctx context.Context, h backend.Handle, length
func TestOutOfBoundsAccess(t *testing.T) {
be := &loadCountingBackend{Backend: mem.New()}
c := TestNewCache(t)
wbe := c.Wrap(be)
wbe := c.Wrap(be, t.Logf)
h, data := randomData(50)
save(t, be, h, data)
@@ -164,7 +164,7 @@ func TestOutOfBoundsAccess(t *testing.T) {
func TestForget(t *testing.T) {
be := &loadCountingBackend{Backend: mem.New()}
c := TestNewCache(t)
wbe := c.Wrap(be)
wbe := c.Wrap(be, t.Logf)
h, data := randomData(50)
save(t, be, h, data)
@@ -236,7 +236,7 @@ func TestErrorBackend(t *testing.T) {
time.Sleep(time.Millisecond)
}
wrappedBE := c.Wrap(errBackend)
wrappedBE := c.Wrap(errBackend, t.Logf)
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
@@ -249,7 +249,7 @@ func TestErrorBackend(t *testing.T) {
func TestAutomaticCacheClear(t *testing.T) {
be := mem.New()
c := TestNewCache(t)
wbe := c.Wrap(be)
wbe := c.Wrap(be, t.Logf)
// add two handles h1 and h2
h1, data := randomData(2000)
@@ -308,7 +308,7 @@ func TestAutomaticCacheClearInvalidFilename(t *testing.T) {
}
save(t, be, h, data)
wbe := c.Wrap(be)
wbe := c.Wrap(be, t.Logf)
// list all files in the backend
list(t, wbe, func(_ backend.FileInfo) error { return nil })
+2 -2
View File
@@ -237,8 +237,8 @@ func IsOld(t time.Time, maxAge time.Duration) bool {
}
// Wrap returns a backend with a cache.
func (c *Cache) Wrap(be backend.Backend) backend.Backend {
return newBackend(be, c)
func (c *Cache) Wrap(be backend.Backend, errorLog func(string, ...interface{})) backend.Backend {
return newBackend(be, c, errorLog)
}
// BaseDir returns the base directory.