Make backend.LoadAll() similar to ioutil.ReadAll()
This commit is contained in:
parent
05afedd950
commit
212936eb52
7 changed files with 26 additions and 31 deletions
|
@ -99,7 +99,7 @@ func runCat(gopts GlobalOptions, args []string) error {
|
||||||
return nil
|
return nil
|
||||||
case "key":
|
case "key":
|
||||||
h := restic.Handle{Type: restic.KeyFile, Name: id.String()}
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -150,7 +150,7 @@ func runCat(gopts GlobalOptions, args []string) error {
|
||||||
switch tpe {
|
switch tpe {
|
||||||
case "pack":
|
case "pack":
|
||||||
h := restic.Handle{Type: restic.DataFile, Name: id.String()}
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -154,7 +154,7 @@ func TestConfig(t testing.TB) {
|
||||||
var testString = "Config"
|
var testString = "Config"
|
||||||
|
|
||||||
// create config and read it back
|
// 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 {
|
if err == nil {
|
||||||
t.Fatalf("did not get expected error for non-existing config")
|
t.Fatalf("did not get expected error for non-existing config")
|
||||||
}
|
}
|
||||||
|
@ -168,7 +168,7 @@ func TestConfig(t testing.TB) {
|
||||||
// same config
|
// same config
|
||||||
for _, name := range []string{"", "foo", "bar", "0000000000000000000000000000000000000000000000000000000000000000"} {
|
for _, name := range []string{"", "foo", "bar", "0000000000000000000000000000000000000000000000000000000000000000"} {
|
||||||
h := restic.Handle{Type: restic.ConfigFile, Name: name}
|
h := restic.Handle{Type: restic.ConfigFile, Name: name}
|
||||||
buf, err := backend.LoadAll(b, h, nil)
|
buf, err := backend.LoadAll(b, h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to read config with name %q: %v", name, err)
|
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))
|
err := b.Save(h, bytes.NewReader(data))
|
||||||
test.OK(t, err)
|
test.OK(t, err)
|
||||||
|
|
||||||
buf, err := backend.LoadAll(b, h, nil)
|
buf, err := backend.LoadAll(b, h)
|
||||||
test.OK(t, err)
|
test.OK(t, err)
|
||||||
if len(buf) != len(data) {
|
if len(buf) != len(data) {
|
||||||
t.Fatalf("number of bytes does not match, want %v, got %v", len(data), len(buf))
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, err := backend.LoadAll(b, h, nil)
|
buf, err := backend.LoadAll(b, h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("test %d failed: Load() returned %v", i, err)
|
t.Errorf("test %d failed: Load() returned %v", i, err)
|
||||||
continue
|
continue
|
||||||
|
@ -605,7 +605,7 @@ func TestBackend(t testing.TB) {
|
||||||
|
|
||||||
// test Load()
|
// test Load()
|
||||||
h := restic.Handle{Type: tpe, Name: ts.id}
|
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.OK(t, err)
|
||||||
test.Equals(t, ts.data, string(buf))
|
test.Equals(t, ts.data, string(buf))
|
||||||
|
|
||||||
|
|
|
@ -2,31 +2,26 @@ package backend
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"restic"
|
"restic"
|
||||||
|
|
||||||
"restic/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoadAll reads all data stored in the backend for the handle. The buffer buf
|
// LoadAll reads all data stored in the backend for the handle.
|
||||||
// is resized to accomodate all data in the blob. Errors returned by be.Load()
|
func LoadAll(be restic.Backend, h restic.Handle) (buf []byte, err error) {
|
||||||
// are passed on, except io.ErrUnexpectedEOF is silenced and nil returned
|
rd, err := be.Get(h, 0, 0)
|
||||||
// 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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "Stat")
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if fi.Size > int64(len(buf)) {
|
defer func() {
|
||||||
buf = make([]byte, int(fi.Size))
|
io.Copy(ioutil.Discard, rd)
|
||||||
}
|
e := rd.Close()
|
||||||
|
if err == nil {
|
||||||
|
err = e
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
n, err := be.Load(h, buf, 0)
|
return ioutil.ReadAll(rd)
|
||||||
if errors.Cause(err) == io.ErrUnexpectedEOF {
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
buf = buf[:n]
|
|
||||||
return buf, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Closer wraps an io.Reader and adds a Close() method that does nothing.
|
// Closer wraps an io.Reader and adds a Close() method that does nothing.
|
||||||
|
|
|
@ -24,7 +24,7 @@ func TestLoadAll(t *testing.T) {
|
||||||
err := b.Save(restic.Handle{Name: id.String(), Type: restic.DataFile}, bytes.NewReader(data))
|
err := b.Save(restic.Handle{Name: id.String(), Type: restic.DataFile}, bytes.NewReader(data))
|
||||||
OK(t, err)
|
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)
|
OK(t, err)
|
||||||
|
|
||||||
if len(buf) != len(data) {
|
if len(buf) != len(data) {
|
||||||
|
@ -50,7 +50,7 @@ func TestLoadSmallBuffer(t *testing.T) {
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
buf := make([]byte, len(data)-23)
|
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)
|
OK(t, err)
|
||||||
|
|
||||||
if len(buf) != len(data) {
|
if len(buf) != len(data) {
|
||||||
|
@ -76,7 +76,7 @@ func TestLoadLargeBuffer(t *testing.T) {
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
buf := make([]byte, len(data)+100)
|
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)
|
OK(t, err)
|
||||||
|
|
||||||
if len(buf) != len(data) {
|
if len(buf) != len(data) {
|
||||||
|
|
|
@ -658,7 +658,7 @@ func (c *Checker) CountPacks() uint64 {
|
||||||
func checkPack(r restic.Repository, id restic.ID) error {
|
func checkPack(r restic.Repository, id restic.ID) error {
|
||||||
debug.Log("checking pack %v", id.Str())
|
debug.Log("checking pack %v", id.Str())
|
||||||
h := restic.Handle{Type: restic.DataFile, Name: id.String()}
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -147,7 +147,7 @@ func SearchKey(s *Repository, password string, maxKeys int) (*Key, error) {
|
||||||
// LoadKey loads a key from the backend.
|
// LoadKey loads a key from the backend.
|
||||||
func LoadKey(s *Repository, name string) (k *Key, err error) {
|
func LoadKey(s *Repository, name string) (k *Key, err error) {
|
||||||
h := restic.Handle{Type: restic.KeyFile, Name: name}
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -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())
|
debug.Log("load %v with id %v", t, id.Str())
|
||||||
|
|
||||||
h := restic.Handle{Type: t, Name: id.String()}
|
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 {
|
if err != nil {
|
||||||
debug.Log("error loading %v: %v", id.Str(), err)
|
debug.Log("error loading %v: %v", id.Str(), err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in a new issue