forked from TrueCloudLab/restic
cache: backend add List method and a cache clear functionality
* removes files which are no longer in the repository, including index files, snapshot files and pack files from the cache. cache: fix ids set initialisation with NewIDSet()
This commit is contained in:
parent
48e3832322
commit
fac1d9fea1
1 changed files with 40 additions and 0 deletions
40
internal/backend/cache/backend.go
vendored
40
internal/backend/cache/backend.go
vendored
|
@ -2,11 +2,14 @@ package cache
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/restic/restic/internal/backend"
|
||||
"github.com/restic/restic/internal/debug"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
)
|
||||
|
||||
// Backend wraps a restic.Backend and adds a cache.
|
||||
|
@ -215,3 +218,40 @@ func (b *Backend) IsNotExist(err error) bool {
|
|||
func (b *Backend) Unwrap() backend.Backend {
|
||||
return b.Backend
|
||||
}
|
||||
|
||||
func (b *Backend) List(ctx context.Context, t backend.FileType, fn func(f backend.FileInfo) error) error {
|
||||
if !b.Cache.canBeCached(t) {
|
||||
return b.Backend.List(ctx, t, fn)
|
||||
}
|
||||
|
||||
// will contain the IDs of the files that are in the repository
|
||||
ids := restic.NewIDSet()
|
||||
|
||||
// wrap the original function to also add the file to the ids set
|
||||
wrapFn := func(f backend.FileInfo) error {
|
||||
id, err := restic.ParseID(f.Name)
|
||||
if err != nil {
|
||||
// returning error here since, if we cannot parse the ID, the file
|
||||
// is invalid and the list must exit.
|
||||
return err
|
||||
}
|
||||
|
||||
ids.Insert(id)
|
||||
|
||||
// execute the original function
|
||||
return fn(f)
|
||||
}
|
||||
|
||||
err := b.Backend.List(ctx, t, wrapFn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue