2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 23:06:32 +00:00

Revert "repository: removed redundant prepareCache method from Repository"

This reverts commit 720609f8ba.
This commit is contained in:
Srigovind Nayak 2024-08-17 00:18:13 +05:30 committed by Michael Eischer
parent 0ca9355bc0
commit 8a7ae17d4d
4 changed files with 45 additions and 6 deletions

View File

@ -146,7 +146,11 @@ func (c *Checker) LoadIndex(ctx context.Context, p *progress.Counter) (hints []e
return hints, append(errs, err) return hints, append(errs, err)
} }
c.repo.SetIndex(c.masterIndex) err = c.repo.SetIndex(c.masterIndex)
if err != nil {
debug.Log("SetIndex returned error: %v", err)
errs = append(errs, err)
}
// compute pack size using index entries // compute pack size using index entries
c.packs, err = pack.Size(ctx, c.repo, false) c.packs, err = pack.Size(ctx, c.repo, false)

View File

@ -52,8 +52,10 @@ func RepairIndex(ctx context.Context, repo *Repository, opts RepairIndexOptions,
return err return err
} }
repo.SetIndex(mi) err = repo.SetIndex(mi)
if err != nil {
return err
}
packSizeFromIndex, err = pack.Size(ctx, repo, false) packSizeFromIndex, err = pack.Size(ctx, repo, false)
if err != nil { if err != nil {
return err return err

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"math" "math"
"os"
"runtime" "runtime"
"sort" "sort"
"sync" "sync"
@ -585,8 +586,9 @@ func (r *Repository) ListPacksFromIndex(ctx context.Context, packs restic.IDSet)
} }
// SetIndex instructs the repository to use the given index. // SetIndex instructs the repository to use the given index.
func (r *Repository) SetIndex(i restic.MasterIndex) { func (r *Repository) SetIndex(i restic.MasterIndex) error {
r.idx = i.(*index.MasterIndex) r.idx = i.(*index.MasterIndex)
return r.prepareCache()
} }
func (r *Repository) clearIndex() { func (r *Repository) clearIndex() {
@ -626,8 +628,12 @@ func (r *Repository) LoadIndex(ctx context.Context, p *progress.Counter) error {
return errors.New("index uses feature not supported by repository version 1") return errors.New("index uses feature not supported by repository version 1")
} }
} }
if ctx.Err() != nil {
return ctx.Err()
}
return ctx.Err() // remove index files from the cache which have been removed in the repo
return r.prepareCache()
} }
// createIndexFromPacks creates a new index by reading all given pack files (with sizes). // createIndexFromPacks creates a new index by reading all given pack files (with sizes).
@ -693,6 +699,33 @@ func (r *Repository) createIndexFromPacks(ctx context.Context, packsize map[rest
return invalid, nil return invalid, nil
} }
// 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 {
return nil
}
indexIDs := r.idx.IDs()
debug.Log("prepare cache with %d index files", len(indexIDs))
// clear old index files
err := r.Cache.Clear(restic.IndexFile, indexIDs)
if err != nil {
fmt.Fprintf(os.Stderr, "error clearing index files in cache: %v\n", err)
}
packs := r.idx.Packs(restic.NewIDSet())
// clear old packs
err = r.Cache.Clear(restic.PackFile, packs)
if err != nil {
fmt.Fprintf(os.Stderr, "error clearing pack files in cache: %v\n", err)
}
return nil
}
// SearchKey finds a key with the supplied password, afterwards the config is // SearchKey finds a key with the supplied password, afterwards the config is
// read and parsed. It tries at most maxKeys key files in the repo. // read and parsed. It tries at most maxKeys key files in the repo.
func (r *Repository) SearchKey(ctx context.Context, password string, maxKeys int, keyHint string) error { func (r *Repository) SearchKey(ctx context.Context, password string, maxKeys int, keyHint string) error {

View File

@ -22,7 +22,7 @@ type Repository interface {
Key() *crypto.Key Key() *crypto.Key
LoadIndex(ctx context.Context, p *progress.Counter) error LoadIndex(ctx context.Context, p *progress.Counter) error
SetIndex(mi MasterIndex) SetIndex(mi MasterIndex) error
LookupBlob(t BlobType, id ID) []PackedBlob LookupBlob(t BlobType, id ID) []PackedBlob
LookupBlobSize(t BlobType, id ID) (size uint, exists bool) LookupBlobSize(t BlobType, id ID) (size uint, exists bool)