2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-15 17:47:21 +00:00

Merge pull request #425 from restic/fix-loadall

backend.LoadAll: return nil on expected error
This commit is contained in:
Alexander Neumann 2016-02-08 00:37:04 +01:00
commit eccbcb73a1
3 changed files with 71 additions and 1 deletions

View File

@ -245,6 +245,17 @@ func TestLoad(t testing.TB) {
} }
} }
// load with a too-large buffer, this should return io.ErrUnexpectedEOF
buf := make([]byte, length+100)
n, err := b.Load(handle, buf, 0)
if n != length {
t.Errorf("wrong length for larger buffer returned, want %d, got %d", length, n)
}
if err != io.ErrUnexpectedEOF {
t.Errorf("wrong error returned for larger buffer: want io.ErrUnexpectedEOF, got %#v", err)
}
OK(t, b.Remove(backend.Data, id.String())) OK(t, b.Remove(backend.Data, id.String()))
} }

View File

@ -1,7 +1,11 @@
package backend package backend
import "io"
// 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. The buffer buf
// is resized to accomodate all data in the blob. // 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 Backend, h Handle, buf []byte) ([]byte, error) { func LoadAll(be Backend, h Handle, buf []byte) ([]byte, error) {
fi, err := be.Stat(h) fi, err := be.Stat(h)
if err != nil { if err != nil {
@ -13,6 +17,9 @@ func LoadAll(be Backend, h Handle, buf []byte) ([]byte, error) {
} }
n, err := be.Load(h, buf, 0) n, err := be.Load(h, buf, 0)
if err == io.ErrUnexpectedEOF {
err = nil
}
buf = buf[:n] buf = buf[:n]
return buf, err return buf, err
} }

View File

@ -37,3 +37,55 @@ func TestLoadAll(t *testing.T) {
} }
} }
} }
func TestLoadSmallBuffer(t *testing.T) {
b := mem.New()
for i := 0; i < 20; i++ {
data := Random(23+i, rand.Intn(MiB)+500*KiB)
id := backend.Hash(data)
err := b.Save(backend.Handle{Name: id.String(), Type: backend.Data}, data)
OK(t, err)
buf := make([]byte, len(data)-23)
buf, err = backend.LoadAll(b, backend.Handle{Type: backend.Data, Name: id.String()}, buf)
OK(t, err)
if len(buf) != len(data) {
t.Errorf("length of returned buffer does not match, want %d, got %d", len(data), len(buf))
continue
}
if !bytes.Equal(buf, data) {
t.Errorf("wrong data returned")
continue
}
}
}
func TestLoadLargeBuffer(t *testing.T) {
b := mem.New()
for i := 0; i < 20; i++ {
data := Random(23+i, rand.Intn(MiB)+500*KiB)
id := backend.Hash(data)
err := b.Save(backend.Handle{Name: id.String(), Type: backend.Data}, data)
OK(t, err)
buf := make([]byte, len(data)+100)
buf, err = backend.LoadAll(b, backend.Handle{Type: backend.Data, Name: id.String()}, buf)
OK(t, err)
if len(buf) != len(data) {
t.Errorf("length of returned buffer does not match, want %d, got %d", len(data), len(buf))
continue
}
if !bytes.Equal(buf, data) {
t.Errorf("wrong data returned")
continue
}
}
}