repository: expose cache via method

This commit is contained in:
Michael Eischer 2024-12-01 12:28:47 +01:00
parent 99e105eeb6
commit b7ff8ea9cd
6 changed files with 21 additions and 17 deletions

View File

@ -304,7 +304,7 @@ func (opts BackupOptions) Check(gopts GlobalOptions, args []string) error {
// from being saved in a snapshot based on path only // from being saved in a snapshot based on path only
func collectRejectByNameFuncs(opts BackupOptions, repo *repository.Repository) (fs []archiver.RejectByNameFunc, err error) { func collectRejectByNameFuncs(opts BackupOptions, repo *repository.Repository) (fs []archiver.RejectByNameFunc, err error) {
// exclude restic cache // exclude restic cache
if repo.Cache != nil { if repo.Cache() != nil {
f, err := rejectResticCache(repo) f, err := rejectResticCache(repo)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -171,7 +171,7 @@ func runPrune(ctx context.Context, opts PruneOptions, gopts GlobalOptions, term
} }
func runPruneWithRepo(ctx context.Context, opts PruneOptions, gopts GlobalOptions, repo *repository.Repository, ignoreSnapshots restic.IDSet, term *termstatus.Terminal) error { func runPruneWithRepo(ctx context.Context, opts PruneOptions, gopts GlobalOptions, repo *repository.Repository, ignoreSnapshots restic.IDSet, term *termstatus.Terminal) error {
if repo.Cache == nil { if repo.Cache() == nil {
Print("warning: running prune without a cache, this may be very slow!\n") Print("warning: running prune without a cache, this may be very slow!\n")
} }

View File

@ -11,12 +11,12 @@ import (
// rejectResticCache returns a RejectByNameFunc that rejects the restic cache // rejectResticCache returns a RejectByNameFunc that rejects the restic cache
// directory (if set). // directory (if set).
func rejectResticCache(repo *repository.Repository) (archiver.RejectByNameFunc, error) { func rejectResticCache(repo *repository.Repository) (archiver.RejectByNameFunc, error) {
if repo.Cache == nil { if repo.Cache() == nil {
return func(string) bool { return func(string) bool {
return false return false
}, nil }, nil
} }
cacheBase := repo.Cache.BaseDir() cacheBase := repo.Cache().BaseDir()
if cacheBase == "" { if cacheBase == "" {
return nil, errors.New("cacheBase is empty string") return nil, errors.New("cacheBase is empty string")

View File

@ -40,9 +40,9 @@ func (e *partialReadError) Error() string {
func CheckPack(ctx context.Context, r *Repository, id restic.ID, blobs []restic.Blob, size int64, bufRd *bufio.Reader, dec *zstd.Decoder) error { func CheckPack(ctx context.Context, r *Repository, id restic.ID, blobs []restic.Blob, size int64, bufRd *bufio.Reader, dec *zstd.Decoder) error {
err := checkPackInner(ctx, r, id, blobs, size, bufRd, dec) err := checkPackInner(ctx, r, id, blobs, size, bufRd, dec)
if err != nil { if err != nil {
if r.Cache != nil { if r.cache != nil {
// ignore error as there's not much we can do here // ignore error as there's not much we can do here
_ = r.Cache.Forget(backend.Handle{Type: restic.PackFile, Name: id.String()}) _ = r.cache.Forget(backend.Handle{Type: restic.PackFile, Name: id.String()})
} }
// retry pack verification to detect transient errors // retry pack verification to detect transient errors

View File

@ -21,10 +21,10 @@ func (r *Repository) LoadRaw(ctx context.Context, t restic.FileType, id restic.I
// retry loading damaged data only once. If a file fails to download correctly // retry loading damaged data only once. If a file fails to download correctly
// the second time, then it is likely corrupted at the backend. // the second time, then it is likely corrupted at the backend.
if h.Type != backend.ConfigFile && id != restic.Hash(buf) { if h.Type != backend.ConfigFile && id != restic.Hash(buf) {
if r.Cache != nil { if r.cache != nil {
// Cleanup cache to make sure it's not the cached copy that is broken. // Cleanup cache to make sure it's not the cached copy that is broken.
// Ignore error as there's not much we can do in that case. // Ignore error as there's not much we can do in that case.
_ = r.Cache.Forget(h) _ = r.cache.Forget(h)
} }
buf, err = loadRaw(ctx, r.be, h) buf, err = loadRaw(ctx, r.be, h)

View File

@ -38,7 +38,7 @@ type Repository struct {
key *crypto.Key key *crypto.Key
keyID restic.ID keyID restic.ID
idx *index.MasterIndex idx *index.MasterIndex
Cache *cache.Cache cache *cache.Cache
opts Options opts Options
@ -154,10 +154,14 @@ func (r *Repository) UseCache(c *cache.Cache) {
return return
} }
debug.Log("using cache") debug.Log("using cache")
r.Cache = c r.cache = c
r.be = c.Wrap(r.be) r.be = c.Wrap(r.be)
} }
func (r *Repository) Cache() *cache.Cache {
return r.cache
}
// SetDryRun sets the repo backend into dry-run mode. // SetDryRun sets the repo backend into dry-run mode.
func (r *Repository) SetDryRun() { func (r *Repository) SetDryRun() {
r.be = dryrun.New(r.be) r.be = dryrun.New(r.be)
@ -230,15 +234,15 @@ func (r *Repository) LoadBlob(ctx context.Context, t restic.BlobType, id restic.
} }
// try cached pack files first // try cached pack files first
sortCachedPacksFirst(r.Cache, blobs) sortCachedPacksFirst(r.cache, blobs)
buf, err := r.loadBlob(ctx, blobs, buf) buf, err := r.loadBlob(ctx, blobs, buf)
if err != nil { if err != nil {
if r.Cache != nil { if r.cache != nil {
for _, blob := range blobs { for _, blob := range blobs {
h := backend.Handle{Type: restic.PackFile, Name: blob.PackID.String(), IsMetadata: blob.Type.IsMetadata()} h := backend.Handle{Type: restic.PackFile, Name: blob.PackID.String(), IsMetadata: blob.Type.IsMetadata()}
// ignore errors as there's not much we can do here // ignore errors as there's not much we can do here
_ = r.Cache.Forget(h) _ = r.cache.Forget(h)
} }
} }
@ -722,14 +726,14 @@ func (r *Repository) createIndexFromPacks(ctx context.Context, packsize map[rest
// prepareCache initializes the local cache. indexIDs is the list of IDs of // prepareCache initializes the local cache. indexIDs is the list of IDs of
// index files still present in the repo. // index files still present in the repo.
func (r *Repository) prepareCache() error { func (r *Repository) prepareCache() error {
if r.Cache == nil { if r.cache == nil {
return nil return nil
} }
packs := r.idx.Packs(restic.NewIDSet()) packs := r.idx.Packs(restic.NewIDSet())
// clear old packs // clear old packs
err := r.Cache.Clear(restic.PackFile, packs) err := r.cache.Clear(restic.PackFile, packs)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error clearing pack files in cache: %v\n", err) fmt.Fprintf(os.Stderr, "error clearing pack files in cache: %v\n", err)
} }
@ -855,9 +859,9 @@ func (r *Repository) ListPack(ctx context.Context, id restic.ID, size int64) ([]
entries, hdrSize, err := pack.List(r.Key(), backend.ReaderAt(ctx, r.be, h), size) entries, hdrSize, err := pack.List(r.Key(), backend.ReaderAt(ctx, r.be, h), size)
if err != nil { if err != nil {
if r.Cache != nil { if r.cache != nil {
// ignore error as there is not much we can do here // ignore error as there is not much we can do here
_ = r.Cache.Forget(h) _ = r.cache.Forget(h)
} }
// retry on error // retry on error