From 212936eb529fc63c052721de20e379d9a81ec11f Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 23 Jan 2017 16:07:39 +0100 Subject: [PATCH] Make backend.LoadAll() similar to ioutil.ReadAll() --- src/cmds/restic/cmd_cat.go | 4 ++-- src/restic/backend/test/tests.go | 10 +++++----- src/restic/backend/utils.go | 31 ++++++++++++----------------- src/restic/backend/utils_test.go | 6 +++--- src/restic/checker/checker.go | 2 +- src/restic/repository/key.go | 2 +- src/restic/repository/repository.go | 2 +- 7 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/cmds/restic/cmd_cat.go b/src/cmds/restic/cmd_cat.go index d95e22740..47a7e06a2 100644 --- a/src/cmds/restic/cmd_cat.go +++ b/src/cmds/restic/cmd_cat.go @@ -99,7 +99,7 @@ func runCat(gopts GlobalOptions, args []string) error { return nil case "key": h := restic.Handle{Type: restic.KeyFile, Name: id.String()} - buf, err := backend.LoadAll(repo.Backend(), h, nil) + buf, err := backend.LoadAll(repo.Backend(), h) if err != nil { return err } @@ -150,7 +150,7 @@ func runCat(gopts GlobalOptions, args []string) error { switch tpe { case "pack": h := restic.Handle{Type: restic.DataFile, Name: id.String()} - buf, err := backend.LoadAll(repo.Backend(), h, nil) + buf, err := backend.LoadAll(repo.Backend(), h) if err != nil { return err } diff --git a/src/restic/backend/test/tests.go b/src/restic/backend/test/tests.go index 16147eeae..421c382f6 100644 --- a/src/restic/backend/test/tests.go +++ b/src/restic/backend/test/tests.go @@ -154,7 +154,7 @@ func TestConfig(t testing.TB) { var testString = "Config" // create config and read it back - _, err := backend.LoadAll(b, restic.Handle{Type: restic.ConfigFile}, nil) + _, err := backend.LoadAll(b, restic.Handle{Type: restic.ConfigFile}) if err == nil { t.Fatalf("did not get expected error for non-existing config") } @@ -168,7 +168,7 @@ func TestConfig(t testing.TB) { // same config for _, name := range []string{"", "foo", "bar", "0000000000000000000000000000000000000000000000000000000000000000"} { h := restic.Handle{Type: restic.ConfigFile, Name: name} - buf, err := backend.LoadAll(b, h, nil) + buf, err := backend.LoadAll(b, h) if err != nil { t.Fatalf("unable to read config with name %q: %v", name, err) } @@ -482,7 +482,7 @@ func TestSave(t testing.TB) { err := b.Save(h, bytes.NewReader(data)) test.OK(t, err) - buf, err := backend.LoadAll(b, h, nil) + buf, err := backend.LoadAll(b, h) test.OK(t, err) if len(buf) != len(data) { t.Fatalf("number of bytes does not match, want %v, got %v", len(data), len(buf)) @@ -531,7 +531,7 @@ func TestSaveFilenames(t testing.TB) { continue } - buf, err := backend.LoadAll(b, h, nil) + buf, err := backend.LoadAll(b, h) if err != nil { t.Errorf("test %d failed: Load() returned %v", i, err) continue @@ -605,7 +605,7 @@ func TestBackend(t testing.TB) { // test Load() h := restic.Handle{Type: tpe, Name: ts.id} - buf, err := backend.LoadAll(b, h, nil) + buf, err := backend.LoadAll(b, h) test.OK(t, err) test.Equals(t, ts.data, string(buf)) diff --git a/src/restic/backend/utils.go b/src/restic/backend/utils.go index 575e847ed..0af9486ba 100644 --- a/src/restic/backend/utils.go +++ b/src/restic/backend/utils.go @@ -2,31 +2,26 @@ package backend import ( "io" + "io/ioutil" "restic" - - "restic/errors" ) -// LoadAll reads all data stored in the backend for the handle. The buffer buf -// is resized to accomodate all data in the blob. Errors returned by be.Load() -// are passed on, except io.ErrUnexpectedEOF is silenced and nil returned -// instead, since it means this function is working properly. -func LoadAll(be restic.Backend, h restic.Handle, buf []byte) ([]byte, error) { - fi, err := be.Stat(h) +// LoadAll reads all data stored in the backend for the handle. +func LoadAll(be restic.Backend, h restic.Handle) (buf []byte, err error) { + rd, err := be.Get(h, 0, 0) if err != nil { - return nil, errors.Wrap(err, "Stat") + return nil, err } - if fi.Size > int64(len(buf)) { - buf = make([]byte, int(fi.Size)) - } + defer func() { + io.Copy(ioutil.Discard, rd) + e := rd.Close() + if err == nil { + err = e + } + }() - n, err := be.Load(h, buf, 0) - if errors.Cause(err) == io.ErrUnexpectedEOF { - err = nil - } - buf = buf[:n] - return buf, err + return ioutil.ReadAll(rd) } // Closer wraps an io.Reader and adds a Close() method that does nothing. diff --git a/src/restic/backend/utils_test.go b/src/restic/backend/utils_test.go index a90fe6371..2996cf494 100644 --- a/src/restic/backend/utils_test.go +++ b/src/restic/backend/utils_test.go @@ -24,7 +24,7 @@ func TestLoadAll(t *testing.T) { err := b.Save(restic.Handle{Name: id.String(), Type: restic.DataFile}, bytes.NewReader(data)) OK(t, err) - buf, err := backend.LoadAll(b, restic.Handle{Type: restic.DataFile, Name: id.String()}, nil) + buf, err := backend.LoadAll(b, restic.Handle{Type: restic.DataFile, Name: id.String()}) OK(t, err) if len(buf) != len(data) { @@ -50,7 +50,7 @@ func TestLoadSmallBuffer(t *testing.T) { OK(t, err) buf := make([]byte, len(data)-23) - buf, err = backend.LoadAll(b, restic.Handle{Type: restic.DataFile, Name: id.String()}, buf) + buf, err = backend.LoadAll(b, restic.Handle{Type: restic.DataFile, Name: id.String()}) OK(t, err) if len(buf) != len(data) { @@ -76,7 +76,7 @@ func TestLoadLargeBuffer(t *testing.T) { OK(t, err) buf := make([]byte, len(data)+100) - buf, err = backend.LoadAll(b, restic.Handle{Type: restic.DataFile, Name: id.String()}, buf) + buf, err = backend.LoadAll(b, restic.Handle{Type: restic.DataFile, Name: id.String()}) OK(t, err) if len(buf) != len(data) { diff --git a/src/restic/checker/checker.go b/src/restic/checker/checker.go index 600ec2e54..3b2f0787b 100644 --- a/src/restic/checker/checker.go +++ b/src/restic/checker/checker.go @@ -658,7 +658,7 @@ func (c *Checker) CountPacks() uint64 { func checkPack(r restic.Repository, id restic.ID) error { debug.Log("checking pack %v", id.Str()) h := restic.Handle{Type: restic.DataFile, Name: id.String()} - buf, err := backend.LoadAll(r.Backend(), h, nil) + buf, err := backend.LoadAll(r.Backend(), h) if err != nil { return err } diff --git a/src/restic/repository/key.go b/src/restic/repository/key.go index 6303ada72..7ce9757aa 100644 --- a/src/restic/repository/key.go +++ b/src/restic/repository/key.go @@ -147,7 +147,7 @@ func SearchKey(s *Repository, password string, maxKeys int) (*Key, error) { // LoadKey loads a key from the backend. func LoadKey(s *Repository, name string) (k *Key, err error) { h := restic.Handle{Type: restic.KeyFile, Name: name} - data, err := backend.LoadAll(s.be, h, nil) + data, err := backend.LoadAll(s.be, h) if err != nil { return nil, err } diff --git a/src/restic/repository/repository.go b/src/restic/repository/repository.go index 56229233f..ed36cad37 100644 --- a/src/restic/repository/repository.go +++ b/src/restic/repository/repository.go @@ -54,7 +54,7 @@ func (r *Repository) LoadAndDecrypt(t restic.FileType, id restic.ID) ([]byte, er debug.Log("load %v with id %v", t, id.Str()) h := restic.Handle{Type: t, Name: id.String()} - buf, err := backend.LoadAll(r.be, h, nil) + buf, err := backend.LoadAll(r.be, h) if err != nil { debug.Log("error loading %v: %v", id.Str(), err) return nil, err