2016-01-23 19:19:26 +01:00
|
|
|
package mem
|
2015-11-22 16:12:00 +01:00
|
|
|
|
|
|
|
import (
|
2017-01-22 22:01:12 +01:00
|
|
|
"bytes"
|
2015-11-22 16:12:00 +01:00
|
|
|
"io"
|
2017-01-22 12:32:20 +01:00
|
|
|
"io/ioutil"
|
2016-08-31 22:39:36 +02:00
|
|
|
"restic"
|
2015-11-22 16:12:00 +01:00
|
|
|
"sync"
|
2015-11-22 16:30:13 +01:00
|
|
|
|
2017-01-22 22:01:12 +01:00
|
|
|
"restic/backend"
|
2016-09-01 22:17:37 +02:00
|
|
|
"restic/errors"
|
2016-08-21 17:46:23 +02:00
|
|
|
|
2016-02-14 15:29:28 +01:00
|
|
|
"restic/debug"
|
2015-11-22 16:12:00 +01:00
|
|
|
)
|
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
type memMap map[restic.Handle][]byte
|
2015-11-22 16:12:00 +01:00
|
|
|
|
2016-08-31 19:10:10 +02:00
|
|
|
// make sure that MemoryBackend implements backend.Backend
|
2016-08-31 22:39:36 +02:00
|
|
|
var _ restic.Backend = &MemoryBackend{}
|
2016-08-31 19:10:10 +02:00
|
|
|
|
2015-11-22 16:12:00 +01:00
|
|
|
// MemoryBackend is a mock backend that uses a map for storing all data in
|
|
|
|
// memory. This should only be used for tests.
|
|
|
|
type MemoryBackend struct {
|
|
|
|
data memMap
|
|
|
|
m sync.Mutex
|
|
|
|
}
|
|
|
|
|
2016-01-23 19:19:26 +01:00
|
|
|
// New returns a new backend that saves all data in a map in memory.
|
|
|
|
func New() *MemoryBackend {
|
2015-11-22 16:12:00 +01:00
|
|
|
be := &MemoryBackend{
|
|
|
|
data: make(memMap),
|
|
|
|
}
|
|
|
|
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("created new memory backend")
|
2015-11-22 16:30:13 +01:00
|
|
|
|
2015-11-22 16:12:00 +01:00
|
|
|
return be
|
|
|
|
}
|
|
|
|
|
2016-08-31 19:10:10 +02:00
|
|
|
// Test returns whether a file exists.
|
2017-01-25 17:48:35 +01:00
|
|
|
func (be *MemoryBackend) Test(h restic.Handle) (bool, error) {
|
2015-11-22 16:12:00 +01:00
|
|
|
be.m.Lock()
|
|
|
|
defer be.m.Unlock()
|
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
debug.Log("Test %v", h)
|
2015-11-22 16:30:13 +01:00
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
if _, ok := be.data[h]; ok {
|
2015-11-22 16:12:00 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2016-08-31 19:10:10 +02:00
|
|
|
// Save adds new Data to the backend.
|
2017-01-22 12:32:20 +01:00
|
|
|
func (be *MemoryBackend) Save(h restic.Handle, rd io.Reader) error {
|
2016-01-24 01:15:35 +01:00
|
|
|
if err := h.Valid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
be.m.Lock()
|
|
|
|
defer be.m.Unlock()
|
|
|
|
|
2016-09-01 21:19:30 +02:00
|
|
|
if h.Type == restic.ConfigFile {
|
2016-01-24 01:15:35 +01:00
|
|
|
h.Name = ""
|
|
|
|
}
|
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
if _, ok := be.data[h]; ok {
|
2016-01-24 20:23:50 +01:00
|
|
|
return errors.New("file already exists")
|
|
|
|
}
|
|
|
|
|
2017-01-22 12:32:20 +01:00
|
|
|
buf, err := ioutil.ReadAll(rd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
be.data[h] = buf
|
2017-01-22 12:32:20 +01:00
|
|
|
debug.Log("saved %v bytes at %v", len(buf), h)
|
2016-01-24 01:15:35 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-23 18:11:10 +01:00
|
|
|
// Load returns a reader that yields the contents of the file at h at the
|
2017-01-22 22:01:12 +01:00
|
|
|
// given offset. If length is nonzero, only a portion of the file is
|
|
|
|
// returned. rd must be closed after use.
|
2017-01-23 18:11:10 +01:00
|
|
|
func (be *MemoryBackend) Load(h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
|
2017-01-22 22:01:12 +01:00
|
|
|
if err := h.Valid(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
be.m.Lock()
|
|
|
|
defer be.m.Unlock()
|
|
|
|
|
|
|
|
if h.Type == restic.ConfigFile {
|
|
|
|
h.Name = ""
|
|
|
|
}
|
|
|
|
|
2017-01-23 18:11:10 +01:00
|
|
|
debug.Log("Load %v offset %v len %v", h, offset, length)
|
2017-01-22 22:01:12 +01:00
|
|
|
|
|
|
|
if offset < 0 {
|
|
|
|
return nil, errors.New("offset is negative")
|
|
|
|
}
|
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
if _, ok := be.data[h]; !ok {
|
2017-01-22 22:01:12 +01:00
|
|
|
return nil, errors.New("no such data")
|
|
|
|
}
|
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
buf := be.data[h]
|
2017-01-22 22:01:12 +01:00
|
|
|
if offset > int64(len(buf)) {
|
|
|
|
return nil, errors.New("offset beyond end of file")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = buf[offset:]
|
|
|
|
if length > 0 && len(buf) > length {
|
|
|
|
buf = buf[:length]
|
|
|
|
}
|
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
return backend.Closer{Reader: bytes.NewReader(buf)}, nil
|
2017-01-22 22:01:12 +01:00
|
|
|
}
|
|
|
|
|
2016-08-31 19:10:10 +02:00
|
|
|
// Stat returns information about a file in the backend.
|
2016-08-31 22:39:36 +02:00
|
|
|
func (be *MemoryBackend) Stat(h restic.Handle) (restic.FileInfo, error) {
|
2016-01-23 23:27:58 +01:00
|
|
|
be.m.Lock()
|
|
|
|
defer be.m.Unlock()
|
|
|
|
|
|
|
|
if err := h.Valid(); err != nil {
|
2016-08-31 22:39:36 +02:00
|
|
|
return restic.FileInfo{}, err
|
2016-01-23 23:27:58 +01:00
|
|
|
}
|
|
|
|
|
2016-09-01 21:19:30 +02:00
|
|
|
if h.Type == restic.ConfigFile {
|
2016-01-23 23:27:58 +01:00
|
|
|
h.Name = ""
|
|
|
|
}
|
|
|
|
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("stat %v", h)
|
2016-01-23 23:27:58 +01:00
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
e, ok := be.data[h]
|
2016-01-23 23:27:58 +01:00
|
|
|
if !ok {
|
2016-08-31 22:39:36 +02:00
|
|
|
return restic.FileInfo{}, errors.New("no such data")
|
2016-01-23 23:27:58 +01:00
|
|
|
}
|
|
|
|
|
2016-08-31 22:39:36 +02:00
|
|
|
return restic.FileInfo{Size: int64(len(e))}, nil
|
2016-01-23 23:27:58 +01:00
|
|
|
}
|
|
|
|
|
2016-08-31 19:10:10 +02:00
|
|
|
// Remove deletes a file from the backend.
|
2017-01-25 17:48:35 +01:00
|
|
|
func (be *MemoryBackend) Remove(h restic.Handle) error {
|
2015-11-22 16:12:00 +01:00
|
|
|
be.m.Lock()
|
|
|
|
defer be.m.Unlock()
|
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
debug.Log("Remove %v", h)
|
2015-11-22 16:30:13 +01:00
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
if _, ok := be.data[h]; !ok {
|
2015-11-22 16:12:00 +01:00
|
|
|
return errors.New("no such data")
|
|
|
|
}
|
|
|
|
|
2017-01-25 17:48:35 +01:00
|
|
|
delete(be.data, h)
|
2015-11-22 16:12:00 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-31 19:10:10 +02:00
|
|
|
// List returns a channel which yields entries from the backend.
|
2016-08-31 22:39:36 +02:00
|
|
|
func (be *MemoryBackend) List(t restic.FileType, done <-chan struct{}) <-chan string {
|
2015-11-22 16:12:00 +01:00
|
|
|
be.m.Lock()
|
|
|
|
defer be.m.Unlock()
|
|
|
|
|
|
|
|
ch := make(chan string)
|
|
|
|
|
|
|
|
var ids []string
|
|
|
|
for entry := range be.data {
|
|
|
|
if entry.Type != t {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ids = append(ids, entry.Name)
|
|
|
|
}
|
|
|
|
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("list %v: %v", t, ids)
|
2015-11-22 16:30:13 +01:00
|
|
|
|
2015-11-22 16:12:00 +01:00
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
for _, id := range ids {
|
|
|
|
select {
|
|
|
|
case ch <- id:
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ch
|
|
|
|
}
|
2016-08-31 19:10:10 +02:00
|
|
|
|
|
|
|
// Location returns the location of the backend (RAM).
|
|
|
|
func (be *MemoryBackend) Location() string {
|
|
|
|
return "RAM"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes all data in the backend.
|
|
|
|
func (be *MemoryBackend) Delete() error {
|
|
|
|
be.m.Lock()
|
|
|
|
defer be.m.Unlock()
|
|
|
|
|
|
|
|
be.data = make(memMap)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the backend.
|
|
|
|
func (be *MemoryBackend) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|