mirror of
https://github.com/octoleo/restic.git
synced 2024-11-27 07:16:40 +00:00
cache: code cleanups
This commit is contained in:
parent
503c8140b1
commit
e9390352a7
11
internal/cache/backend.go
vendored
11
internal/cache/backend.go
vendored
@ -79,10 +79,9 @@ func (b *Backend) Save(ctx context.Context, h backend.Handle, rd backend.RewindR
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = b.Cache.Save(h, rd)
|
err = b.Cache.save(h, rd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
debug.Log("unable to save %v to cache: %v", h, err)
|
debug.Log("unable to save %v to cache: %v", h, err)
|
||||||
_ = b.Cache.remove(h)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +119,7 @@ func (b *Backend) cacheFile(ctx context.Context, h backend.Handle) error {
|
|||||||
if !b.Cache.Has(h) {
|
if !b.Cache.Has(h) {
|
||||||
// nope, it's still not in the cache, pull it from the repo and save it
|
// nope, it's still not in the cache, pull it from the repo and save it
|
||||||
err := b.Backend.Load(ctx, h, 0, 0, func(rd io.Reader) error {
|
err := b.Backend.Load(ctx, h, 0, 0, func(rd io.Reader) error {
|
||||||
return b.Cache.Save(h, rd)
|
return b.Cache.save(h, rd)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// try to remove from the cache, ignore errors
|
// try to remove from the cache, ignore errors
|
||||||
@ -198,8 +197,7 @@ func (b *Backend) Stat(ctx context.Context, h backend.Handle) (backend.FileInfo,
|
|||||||
debug.Log("cache Stat(%v)", h)
|
debug.Log("cache Stat(%v)", h)
|
||||||
|
|
||||||
fi, err := b.Backend.Stat(ctx, h)
|
fi, err := b.Backend.Stat(ctx, h)
|
||||||
if err != nil {
|
if err != nil && b.Backend.IsNotExist(err) {
|
||||||
if b.Backend.IsNotExist(err) {
|
|
||||||
// try to remove from the cache, ignore errors
|
// try to remove from the cache, ignore errors
|
||||||
_ = b.Cache.remove(h)
|
_ = b.Cache.remove(h)
|
||||||
}
|
}
|
||||||
@ -207,9 +205,6 @@ func (b *Backend) Stat(ctx context.Context, h backend.Handle) (backend.FileInfo,
|
|||||||
return fi, err
|
return fi, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return fi, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsNotExist returns true if the error is caused by a non-existing file.
|
// IsNotExist returns true if the error is caused by a non-existing file.
|
||||||
func (b *Backend) IsNotExist(err error) bool {
|
func (b *Backend) IsNotExist(err error) bool {
|
||||||
return b.Backend.IsNotExist(err)
|
return b.Backend.IsNotExist(err)
|
||||||
|
16
internal/cache/file.go
vendored
16
internal/cache/file.go
vendored
@ -32,7 +32,7 @@ func (c *Cache) canBeCached(t backend.FileType) bool {
|
|||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load returns a reader that yields the contents of the file with the
|
// 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
|
// given handle. rd must be closed after use. If an error is returned, the
|
||||||
// ReadCloser is nil.
|
// ReadCloser is nil.
|
||||||
func (c *Cache) load(h backend.Handle, length int, offset int64) (io.ReadCloser, error) {
|
func (c *Cache) load(h backend.Handle, length int, offset int64) (io.ReadCloser, error) {
|
||||||
@ -78,8 +78,8 @@ func (c *Cache) load(h backend.Handle, length int, offset int64) (io.ReadCloser,
|
|||||||
return util.LimitReadCloser(f, int64(length)), nil
|
return util.LimitReadCloser(f, int64(length)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save saves a file in the cache.
|
// save saves a file in the cache.
|
||||||
func (c *Cache) Save(h backend.Handle, rd io.Reader) error {
|
func (c *Cache) save(h backend.Handle, rd io.Reader) error {
|
||||||
debug.Log("Save to cache: %v", h)
|
debug.Log("Save to cache: %v", h)
|
||||||
if rd == nil {
|
if rd == nil {
|
||||||
return errors.New("Save() called with nil reader")
|
return errors.New("Save() called with nil reader")
|
||||||
@ -139,13 +139,17 @@ func (c *Cache) Save(h backend.Handle, rd io.Reader) error {
|
|||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove deletes a file. When the file is not cache, no error is returned.
|
// remove deletes a file. When the file is not cached, no error is returned.
|
||||||
func (c *Cache) remove(h backend.Handle) error {
|
func (c *Cache) remove(h backend.Handle) error {
|
||||||
if !c.Has(h) {
|
if !c.canBeCached(h.Type) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return fs.Remove(c.filename(h))
|
err := fs.Remove(c.filename(h))
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear removes all files of type t from the cache that are not contained in
|
// Clear removes all files of type t from the cache that are not contained in
|
||||||
|
8
internal/cache/file_test.go
vendored
8
internal/cache/file_test.go
vendored
@ -30,7 +30,7 @@ func generateRandomFiles(t testing.TB, tpe backend.FileType, c *Cache) restic.ID
|
|||||||
t.Errorf("index %v present before save", id)
|
t.Errorf("index %v present before save", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := c.Save(h, bytes.NewReader(buf))
|
err := c.save(h, bytes.NewReader(buf))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -151,7 +151,7 @@ func TestFileLoad(t *testing.T) {
|
|||||||
Type: restic.PackFile,
|
Type: restic.PackFile,
|
||||||
Name: id.String(),
|
Name: id.String(),
|
||||||
}
|
}
|
||||||
if err := c.Save(h, bytes.NewReader(data)); err != nil {
|
if err := c.save(h, bytes.NewReader(data)); err != nil {
|
||||||
t.Fatalf("Save() returned error: %v", err)
|
t.Fatalf("Save() returned error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,7 +237,7 @@ func TestFileSaveConcurrent(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < nproc/2; i++ {
|
for i := 0; i < nproc/2; i++ {
|
||||||
g.Go(func() error { return c.Save(h, bytes.NewReader(data)) })
|
g.Go(func() error { return c.save(h, bytes.NewReader(data)) })
|
||||||
|
|
||||||
// Can't use load because only the main goroutine may call t.Fatal.
|
// Can't use load because only the main goroutine may call t.Fatal.
|
||||||
g.Go(func() error {
|
g.Go(func() error {
|
||||||
@ -280,7 +280,7 @@ func TestFileSaveAfterDamage(t *testing.T) {
|
|||||||
Type: restic.PackFile,
|
Type: restic.PackFile,
|
||||||
Name: id.String(),
|
Name: id.String(),
|
||||||
}
|
}
|
||||||
if err := c.Save(h, bytes.NewReader(data)); err == nil {
|
if err := c.save(h, bytes.NewReader(data)); err == nil {
|
||||||
t.Fatal("Missing error when saving to deleted cache directory")
|
t.Fatal("Missing error when saving to deleted cache directory")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user