mirror of
https://github.com/restic/restic.git
synced 2025-08-12 17:47:41 +00:00
Remove restic.Cache interface
It was used in one code path, which then asserted its concrete type as *cache.Cache. Privatised some of the interface methods.
This commit is contained in:
12
internal/cache/backend.go
vendored
12
internal/cache/backend.go
vendored
@@ -40,7 +40,7 @@ func (b *Backend) Remove(ctx context.Context, h restic.Handle) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return b.Cache.Remove(h)
|
||||
return b.Cache.remove(h)
|
||||
}
|
||||
|
||||
var autoCacheTypes = map[restic.FileType]struct{}{
|
||||
@@ -77,7 +77,7 @@ func (b *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindRea
|
||||
err = b.Cache.Save(h, rd)
|
||||
if err != nil {
|
||||
debug.Log("unable to save %v to cache: %v", h, err)
|
||||
_ = b.Cache.Remove(h)
|
||||
_ = b.Cache.remove(h)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ func (b *Backend) cacheFile(ctx context.Context, h restic.Handle) error {
|
||||
})
|
||||
if err != nil {
|
||||
// try to remove from the cache, ignore errors
|
||||
_ = b.Cache.Remove(h)
|
||||
_ = b.Cache.remove(h)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ func (b *Backend) cacheFile(ctx context.Context, h restic.Handle) error {
|
||||
// loadFromCacheOrDelegate will try to load the file from the cache, and fall
|
||||
// back to the backend if that fails.
|
||||
func (b *Backend) loadFromCacheOrDelegate(ctx context.Context, h restic.Handle, length int, offset int64, consumer func(rd io.Reader) error) error {
|
||||
rd, err := b.Cache.Load(h, length, offset)
|
||||
rd, err := b.Cache.load(h, length, offset)
|
||||
if err != nil {
|
||||
debug.Log("error caching %v: %v, falling back to backend", h, err)
|
||||
return b.Backend.Load(ctx, h, length, offset, consumer)
|
||||
@@ -162,7 +162,7 @@ func (b *Backend) Load(ctx context.Context, h restic.Handle, length int, offset
|
||||
|
||||
if b.Cache.Has(h) {
|
||||
debug.Log("Load(%v, %v, %v) from cache", h, length, offset)
|
||||
rd, err := b.Cache.Load(h, length, offset)
|
||||
rd, err := b.Cache.load(h, length, offset)
|
||||
if err == nil {
|
||||
err = consumer(rd)
|
||||
if err != nil {
|
||||
@@ -216,7 +216,7 @@ func (b *Backend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, e
|
||||
if err != nil {
|
||||
if b.Backend.IsNotExist(err) {
|
||||
// try to remove from the cache, ignore errors
|
||||
_ = b.Cache.Remove(h)
|
||||
_ = b.Cache.remove(h)
|
||||
}
|
||||
|
||||
return fi, err
|
||||
|
3
internal/cache/cache.go
vendored
3
internal/cache/cache.go
vendored
@@ -46,9 +46,6 @@ func readVersion(dir string) (v uint, err error) {
|
||||
|
||||
const cacheVersion = 1
|
||||
|
||||
// ensure Cache implements restic.Cache
|
||||
var _ restic.Cache = &Cache{}
|
||||
|
||||
var cacheLayoutPaths = map[restic.FileType]string{
|
||||
restic.PackFile: "data",
|
||||
restic.SnapshotFile: "snapshots",
|
||||
|
18
internal/cache/file.go
vendored
18
internal/cache/file.go
vendored
@@ -40,7 +40,7 @@ type readCloser struct {
|
||||
// Load returns a reader that yields the contents of the file with the
|
||||
// given handle. rd must be closed after use. If an error is returned, the
|
||||
// ReadCloser is nil.
|
||||
func (c *Cache) Load(h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
|
||||
func (c *Cache) load(h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
|
||||
debug.Log("Load from cache: %v", h)
|
||||
if !c.canBeCached(h.Type) {
|
||||
return nil, errors.New("cannot be cached")
|
||||
@@ -59,13 +59,13 @@ func (c *Cache) Load(h restic.Handle, length int, offset int64) (io.ReadCloser,
|
||||
|
||||
if fi.Size() <= crypto.Extension {
|
||||
_ = f.Close()
|
||||
_ = c.Remove(h)
|
||||
_ = c.remove(h)
|
||||
return nil, errors.Errorf("cached file %v is truncated, removing", h)
|
||||
}
|
||||
|
||||
if fi.Size() < offset+int64(length) {
|
||||
_ = f.Close()
|
||||
_ = c.Remove(h)
|
||||
_ = c.remove(h)
|
||||
return nil, errors.Errorf("cached file %v is too small, removing", h)
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ func (c *Cache) Load(h restic.Handle, length int, offset int64) (io.ReadCloser,
|
||||
}
|
||||
|
||||
// SaveWriter returns a writer for the cache object h. It must be closed after writing is finished.
|
||||
func (c *Cache) SaveWriter(h restic.Handle) (io.WriteCloser, error) {
|
||||
func (c *Cache) saveWriter(h restic.Handle) (io.WriteCloser, error) {
|
||||
debug.Log("Save to cache: %v", h)
|
||||
if !c.canBeCached(h.Type) {
|
||||
return nil, errors.New("cannot be cached")
|
||||
@@ -112,7 +112,7 @@ func (c *Cache) Save(h restic.Handle, rd io.Reader) error {
|
||||
return errors.New("Save() called with nil reader")
|
||||
}
|
||||
|
||||
f, err := c.SaveWriter(h)
|
||||
f, err := c.saveWriter(h)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -120,19 +120,19 @@ func (c *Cache) Save(h restic.Handle, rd io.Reader) error {
|
||||
n, err := io.Copy(f, rd)
|
||||
if err != nil {
|
||||
_ = f.Close()
|
||||
_ = c.Remove(h)
|
||||
_ = c.remove(h)
|
||||
return errors.Wrap(err, "Copy")
|
||||
}
|
||||
|
||||
if n <= crypto.Extension {
|
||||
_ = f.Close()
|
||||
_ = c.Remove(h)
|
||||
_ = c.remove(h)
|
||||
debug.Log("trying to cache truncated file %v, removing", h)
|
||||
return nil
|
||||
}
|
||||
|
||||
if err = f.Close(); err != nil {
|
||||
_ = c.Remove(h)
|
||||
_ = c.remove(h)
|
||||
return errors.Wrap(err, "Close")
|
||||
}
|
||||
|
||||
@@ -140,7 +140,7 @@ func (c *Cache) Save(h restic.Handle, rd io.Reader) error {
|
||||
}
|
||||
|
||||
// Remove deletes a file. When the file is not cache, no error is returned.
|
||||
func (c *Cache) Remove(h restic.Handle) error {
|
||||
func (c *Cache) remove(h restic.Handle) error {
|
||||
if !c.Has(h) {
|
||||
return nil
|
||||
}
|
||||
|
10
internal/cache/file_test.go
vendored
10
internal/cache/file_test.go
vendored
@@ -42,13 +42,13 @@ func randomID(s restic.IDSet) restic.ID {
|
||||
}
|
||||
|
||||
func load(t testing.TB, c *Cache, h restic.Handle) []byte {
|
||||
rd, err := c.Load(h, 0, 0)
|
||||
rd, err := c.load(h, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if rd == nil {
|
||||
t.Fatalf("Load() returned nil reader")
|
||||
t.Fatalf("load() returned nil reader")
|
||||
}
|
||||
|
||||
buf, err := ioutil.ReadAll(rd)
|
||||
@@ -148,7 +148,7 @@ func TestFileSaveWriter(t *testing.T) {
|
||||
Name: id.String(),
|
||||
}
|
||||
|
||||
wr, err := c.SaveWriter(h)
|
||||
wr, err := c.saveWriter(h)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -166,7 +166,7 @@ func TestFileSaveWriter(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rd, err := c.Load(h, 0, 0)
|
||||
rd, err := c.load(h, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -223,7 +223,7 @@ func TestFileLoad(t *testing.T) {
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("%v/%v", test.length, test.offset), func(t *testing.T) {
|
||||
rd, err := c.Load(h, test.length, test.offset)
|
||||
rd, err := c.load(h, test.length, test.offset)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user