From cb43afa2200f1e5b5377128a1e2c962ca638b1b1 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 1 Dec 2024 12:28:47 +0100 Subject: [PATCH] repository: expose cache via method --- cmd/restic/cmd_backup.go | 2 +- cmd/restic/cmd_prune.go | 2 +- cmd/restic/exclude.go | 4 ++-- internal/repository/check.go | 4 ++-- internal/repository/raw.go | 4 ++-- internal/repository/repository.go | 22 +++++++++++++--------- 6 files changed, 21 insertions(+), 17 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index b7eed1318..aa225338a 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -302,7 +302,7 @@ func (opts BackupOptions) Check(gopts GlobalOptions, args []string) error { // from being saved in a snapshot based on path only func collectRejectByNameFuncs(opts BackupOptions, repo *repository.Repository) (fs []archiver.RejectByNameFunc, err error) { // exclude restic cache - if repo.Cache != nil { + if repo.Cache() != nil { f, err := rejectResticCache(repo) if err != nil { return nil, err diff --git a/cmd/restic/cmd_prune.go b/cmd/restic/cmd_prune.go index 213714799..fce109bdd 100644 --- a/cmd/restic/cmd_prune.go +++ b/cmd/restic/cmd_prune.go @@ -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 { - if repo.Cache == nil { + if repo.Cache() == nil { Print("warning: running prune without a cache, this may be very slow!\n") } diff --git a/cmd/restic/exclude.go b/cmd/restic/exclude.go index 99d1128a9..1c05f4abb 100644 --- a/cmd/restic/exclude.go +++ b/cmd/restic/exclude.go @@ -11,12 +11,12 @@ import ( // rejectResticCache returns a RejectByNameFunc that rejects the restic cache // directory (if set). func rejectResticCache(repo *repository.Repository) (archiver.RejectByNameFunc, error) { - if repo.Cache == nil { + if repo.Cache() == nil { return func(string) bool { return false }, nil } - cacheBase := repo.Cache.BaseDir() + cacheBase := repo.Cache().BaseDir() if cacheBase == "" { return nil, errors.New("cacheBase is empty string") diff --git a/internal/repository/check.go b/internal/repository/check.go index 4e57a7c1c..2bf2ac8f3 100644 --- a/internal/repository/check.go +++ b/internal/repository/check.go @@ -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 { err := checkPackInner(ctx, r, id, blobs, size, bufRd, dec) if err != nil { - if r.Cache != nil { + if r.cache != nil { // 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 diff --git a/internal/repository/raw.go b/internal/repository/raw.go index 31443b010..c5a4a72b7 100644 --- a/internal/repository/raw.go +++ b/internal/repository/raw.go @@ -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 // the second time, then it is likely corrupted at the backend. 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. // 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) diff --git a/internal/repository/repository.go b/internal/repository/repository.go index b6a8cab11..09cc1977a 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -38,7 +38,7 @@ type Repository struct { key *crypto.Key keyID restic.ID idx *index.MasterIndex - Cache *cache.Cache + cache *cache.Cache opts Options @@ -154,10 +154,14 @@ func (r *Repository) UseCache(c *cache.Cache) { return } debug.Log("using cache") - r.Cache = c + r.cache = c r.be = c.Wrap(r.be) } +func (r *Repository) Cache() *cache.Cache { + return r.cache +} + // SetDryRun sets the repo backend into dry-run mode. func (r *Repository) SetDryRun() { 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 - sortCachedPacksFirst(r.Cache, blobs) + sortCachedPacksFirst(r.cache, blobs) buf, err := r.loadBlob(ctx, blobs, buf) if err != nil { - if r.Cache != nil { + if r.cache != nil { for _, blob := range blobs { 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 - _ = 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 // index files still present in the repo. func (r *Repository) prepareCache() error { - if r.Cache == nil { + if r.cache == nil { return nil } packs := r.idx.Packs(restic.NewIDSet()) // clear old packs - err := r.Cache.Clear(restic.PackFile, packs) + err := r.cache.Clear(restic.PackFile, packs) if err != nil { 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) if err != nil { - if r.Cache != nil { + if r.cache != nil { // ignore error as there is not much we can do here - _ = r.Cache.Forget(h) + _ = r.cache.Forget(h) } // retry on error