From e966df3fed8f6300eda72d6632214249098eaaa3 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 23 Jan 2016 17:19:55 +0100 Subject: [PATCH] Add Load() to MemBackend --- backend/mem_backend.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/backend/mem_backend.go b/backend/mem_backend.go index e689584c2..e4782e40c 100644 --- a/backend/mem_backend.go +++ b/backend/mem_backend.go @@ -45,6 +45,10 @@ func NewMemoryBackend() *MemoryBackend { return memGetReader(be, t, name, offset, length) } + be.MockBackend.LoadFn = func(h Handle, p []byte, off int64) (int, error) { + return memLoad(be, h, p, off) + } + be.MockBackend.RemoveFn = func(t Type, name string) error { return memRemove(be, t, name) } @@ -61,6 +65,10 @@ func NewMemoryBackend() *MemoryBackend { return nil } + be.MockBackend.LocationFn = func() string { + return "Memory Backend" + } + debug.Log("MemoryBackend.New", "created new memory backend") return be @@ -171,6 +179,40 @@ func memGetReader(be *MemoryBackend, t Type, name string, offset, length uint) ( return readCloser{bytes.NewReader(buf)}, nil } +func memLoad(be *MemoryBackend, h Handle, p []byte, off int64) (int, error) { + be.m.Lock() + defer be.m.Unlock() + + if err := h.Valid(); err != nil { + return 0, err + } + + if h.Type == Config { + h.Name = "" + } + + debug.Log("MemoryBackend.Load", "get %v offset %v len %v", h, off, len(p)) + + if _, ok := be.data[entry{h.Type, h.Name}]; !ok { + return 0, errors.New("no such data") + } + + buf := be.data[entry{h.Type, h.Name}] + if off > int64(len(buf)) { + return 0, errors.New("offset beyond end of file") + } + + buf = buf[off:] + + n := copy(p, buf) + + if len(p) > len(buf) { + return n, io.ErrUnexpectedEOF + } + + return n, nil +} + func memRemove(be *MemoryBackend, t Type, name string) error { be.m.Lock() defer be.m.Unlock()