1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-21 23:33:03 +00:00

repository: expose cache via method

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

View file

@ -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

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 {
if repo.Cache == nil {
if repo.Cache() == nil {
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
// 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")

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 {
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

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
// 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)

View file

@ -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