2016-01-23 18:19:26 +00:00
|
|
|
package mem
|
2015-11-22 15:12:00 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-22 21:01:12 +00:00
|
|
|
"bytes"
|
2017-06-03 15:39:57 +00:00
|
|
|
"context"
|
2020-12-19 11:50:22 +00:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/base64"
|
2020-12-19 11:39:48 +00:00
|
|
|
"hash"
|
2015-11-22 15:12:00 +00:00
|
|
|
"io"
|
2017-01-22 11:32:20 +00:00
|
|
|
"io/ioutil"
|
2015-11-22 15:12:00 +00:00
|
|
|
"sync"
|
2015-11-22 15:30:13 +00:00
|
|
|
|
2018-01-17 04:59:16 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2020-12-17 11:47:53 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
2020-12-17 11:47:53 +00:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2015-11-22 15:12:00 +00:00
|
|
|
)
|
|
|
|
|
2017-01-25 16:48:35 +00:00
|
|
|
type memMap map[restic.Handle][]byte
|
2015-11-22 15:12:00 +00:00
|
|
|
|
2016-08-31 17:10:10 +00:00
|
|
|
// make sure that MemoryBackend implements backend.Backend
|
2016-08-31 20:39:36 +00:00
|
|
|
var _ restic.Backend = &MemoryBackend{}
|
2016-08-31 17:10:10 +00:00
|
|
|
|
2017-06-15 11:40:27 +00:00
|
|
|
var errNotFound = errors.New("not found")
|
|
|
|
|
2015-11-22 15:12:00 +00: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 18:19:26 +00:00
|
|
|
// New returns a new backend that saves all data in a map in memory.
|
|
|
|
func New() *MemoryBackend {
|
2015-11-22 15:12:00 +00:00
|
|
|
be := &MemoryBackend{
|
|
|
|
data: make(memMap),
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("created new memory backend")
|
2015-11-22 15:30:13 +00:00
|
|
|
|
2015-11-22 15:12:00 +00:00
|
|
|
return be
|
|
|
|
}
|
|
|
|
|
2016-08-31 17:10:10 +00:00
|
|
|
// Test returns whether a file exists.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (be *MemoryBackend) Test(ctx context.Context, h restic.Handle) (bool, error) {
|
2015-11-22 15:12:00 +00:00
|
|
|
be.m.Lock()
|
|
|
|
defer be.m.Unlock()
|
|
|
|
|
2017-01-25 16:48:35 +00:00
|
|
|
debug.Log("Test %v", h)
|
2015-11-22 15:30:13 +00:00
|
|
|
|
2017-01-25 16:48:35 +00:00
|
|
|
if _, ok := be.data[h]; ok {
|
2020-11-07 23:05:53 +00:00
|
|
|
return true, ctx.Err()
|
2015-11-22 15:12:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 23:05:53 +00:00
|
|
|
return false, ctx.Err()
|
2015-11-22 15:12:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:27 +00:00
|
|
|
// IsNotExist returns true if the file does not exist.
|
|
|
|
func (be *MemoryBackend) IsNotExist(err error) bool {
|
|
|
|
return errors.Cause(err) == errNotFound
|
|
|
|
}
|
|
|
|
|
2016-08-31 17:10:10 +00:00
|
|
|
// Save adds new Data to the backend.
|
2018-03-03 13:20:54 +00:00
|
|
|
func (be *MemoryBackend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
|
2016-01-24 00:15:35 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
2020-12-17 11:47:53 +00:00
|
|
|
return backoff.Permanent(err)
|
2016-01-24 00:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
be.m.Lock()
|
|
|
|
defer be.m.Unlock()
|
|
|
|
|
2016-09-01 19:19:30 +00:00
|
|
|
if h.Type == restic.ConfigFile {
|
2016-01-24 00:15:35 +00:00
|
|
|
h.Name = ""
|
|
|
|
}
|
|
|
|
|
2017-01-25 16:48:35 +00:00
|
|
|
if _, ok := be.data[h]; ok {
|
2016-01-24 19:23:50 +00:00
|
|
|
return errors.New("file already exists")
|
|
|
|
}
|
|
|
|
|
2017-01-22 11:32:20 +00:00
|
|
|
buf, err := ioutil.ReadAll(rd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-18 22:41:29 +00:00
|
|
|
// sanity check
|
|
|
|
if int64(len(buf)) != rd.Length() {
|
|
|
|
return errors.Errorf("wrote %d bytes instead of the expected %d bytes", len(buf), rd.Length())
|
|
|
|
}
|
|
|
|
|
2020-12-19 11:50:22 +00:00
|
|
|
beHash := be.Hasher()
|
|
|
|
// must never fail according to interface
|
2021-01-29 21:12:51 +00:00
|
|
|
_, err = beHash.Write(buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-12-19 11:50:22 +00:00
|
|
|
if !bytes.Equal(beHash.Sum(nil), rd.Hash()) {
|
|
|
|
return errors.Errorf("invalid file hash or content, got %s expected %s",
|
|
|
|
base64.RawStdEncoding.EncodeToString(beHash.Sum(nil)),
|
|
|
|
base64.RawStdEncoding.EncodeToString(rd.Hash()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-29 23:15:20 +00:00
|
|
|
be.data[h] = buf
|
|
|
|
debug.Log("saved %v bytes at %v", len(buf), h)
|
|
|
|
|
2020-11-07 23:05:53 +00:00
|
|
|
return ctx.Err()
|
2016-01-24 00:15:35 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 04:59:16 +00:00
|
|
|
// Load runs fn with a reader that yields the contents of the file at h at the
|
|
|
|
// given offset.
|
|
|
|
func (be *MemoryBackend) Load(ctx context.Context, h restic.Handle, length int, offset int64, fn func(rd io.Reader) error) error {
|
|
|
|
return backend.DefaultLoad(ctx, h, length, offset, be.openReader, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (be *MemoryBackend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
|
2017-01-22 21:01:12 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
2020-12-17 11:47:53 +00:00
|
|
|
return nil, backoff.Permanent(err)
|
2017-01-22 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
be.m.Lock()
|
|
|
|
defer be.m.Unlock()
|
|
|
|
|
|
|
|
if h.Type == restic.ConfigFile {
|
|
|
|
h.Name = ""
|
|
|
|
}
|
|
|
|
|
2017-01-23 17:11:10 +00:00
|
|
|
debug.Log("Load %v offset %v len %v", h, offset, length)
|
2017-01-22 21:01:12 +00:00
|
|
|
|
|
|
|
if offset < 0 {
|
|
|
|
return nil, errors.New("offset is negative")
|
|
|
|
}
|
|
|
|
|
2017-01-25 16:48:35 +00:00
|
|
|
if _, ok := be.data[h]; !ok {
|
2017-06-15 11:40:27 +00:00
|
|
|
return nil, errNotFound
|
2017-01-22 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 16:48:35 +00:00
|
|
|
buf := be.data[h]
|
2017-01-22 21:01:12 +00: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]
|
|
|
|
}
|
|
|
|
|
2020-11-07 23:05:53 +00:00
|
|
|
return ioutil.NopCloser(bytes.NewReader(buf)), ctx.Err()
|
2017-01-22 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 17:10:10 +00:00
|
|
|
// Stat returns information about a file in the backend.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (be *MemoryBackend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, error) {
|
2016-01-23 22:27:58 +00:00
|
|
|
be.m.Lock()
|
|
|
|
defer be.m.Unlock()
|
|
|
|
|
|
|
|
if err := h.Valid(); err != nil {
|
2020-12-17 11:47:53 +00:00
|
|
|
return restic.FileInfo{}, backoff.Permanent(err)
|
2016-01-23 22:27:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 19:19:30 +00:00
|
|
|
if h.Type == restic.ConfigFile {
|
2016-01-23 22:27:58 +00:00
|
|
|
h.Name = ""
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("stat %v", h)
|
2016-01-23 22:27:58 +00:00
|
|
|
|
2017-01-25 16:48:35 +00:00
|
|
|
e, ok := be.data[h]
|
2016-01-23 22:27:58 +00:00
|
|
|
if !ok {
|
2017-06-15 11:40:27 +00:00
|
|
|
return restic.FileInfo{}, errNotFound
|
2016-01-23 22:27:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 23:05:53 +00:00
|
|
|
return restic.FileInfo{Size: int64(len(e)), Name: h.Name}, ctx.Err()
|
2016-01-23 22:27:58 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 17:10:10 +00:00
|
|
|
// Remove deletes a file from the backend.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (be *MemoryBackend) Remove(ctx context.Context, h restic.Handle) error {
|
2015-11-22 15:12:00 +00:00
|
|
|
be.m.Lock()
|
|
|
|
defer be.m.Unlock()
|
|
|
|
|
2017-01-25 16:48:35 +00:00
|
|
|
debug.Log("Remove %v", h)
|
2015-11-22 15:30:13 +00:00
|
|
|
|
2017-01-25 16:48:35 +00:00
|
|
|
if _, ok := be.data[h]; !ok {
|
2017-06-15 11:40:27 +00:00
|
|
|
return errNotFound
|
2015-11-22 15:12:00 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 16:48:35 +00:00
|
|
|
delete(be.data, h)
|
2015-11-22 15:12:00 +00:00
|
|
|
|
2020-11-07 23:05:53 +00:00
|
|
|
return ctx.Err()
|
2015-11-22 15:12:00 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 17:10:10 +00:00
|
|
|
// List returns a channel which yields entries from the backend.
|
2018-01-20 12:43:07 +00:00
|
|
|
func (be *MemoryBackend) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
|
2018-01-21 16:25:36 +00:00
|
|
|
entries := make(map[string]int64)
|
2015-11-22 15:12:00 +00:00
|
|
|
|
2018-01-21 16:25:36 +00:00
|
|
|
be.m.Lock()
|
2018-01-20 12:43:07 +00:00
|
|
|
for entry, buf := range be.data {
|
2015-11-22 15:12:00 +00:00
|
|
|
if entry.Type != t {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-01-21 16:25:36 +00:00
|
|
|
entries[entry.Name] = int64(len(buf))
|
|
|
|
}
|
|
|
|
be.m.Unlock()
|
|
|
|
|
|
|
|
for name, size := range entries {
|
2018-01-20 12:43:07 +00:00
|
|
|
fi := restic.FileInfo{
|
2018-01-21 16:25:36 +00:00
|
|
|
Name: name,
|
|
|
|
Size: size,
|
2018-01-20 12:43:07 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2018-01-20 12:43:07 +00:00
|
|
|
err := fn(fi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-22 15:30:13 +00:00
|
|
|
|
2018-01-20 12:43:07 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
2015-11-22 15:12:00 +00:00
|
|
|
}
|
2018-01-20 12:43:07 +00:00
|
|
|
}
|
2015-11-22 15:12:00 +00:00
|
|
|
|
2018-01-20 12:43:07 +00:00
|
|
|
return ctx.Err()
|
2015-11-22 15:12:00 +00:00
|
|
|
}
|
2016-08-31 17:10:10 +00:00
|
|
|
|
|
|
|
// Location returns the location of the backend (RAM).
|
|
|
|
func (be *MemoryBackend) Location() string {
|
|
|
|
return "RAM"
|
|
|
|
}
|
|
|
|
|
2020-12-19 11:39:48 +00:00
|
|
|
// Hasher may return a hash function for calculating a content hash for the backend
|
|
|
|
func (be *MemoryBackend) Hasher() hash.Hash {
|
2020-12-19 11:50:22 +00:00
|
|
|
return md5.New()
|
2020-12-19 11:39:48 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 17:10:10 +00:00
|
|
|
// Delete removes all data in the backend.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (be *MemoryBackend) Delete(ctx context.Context) error {
|
2016-08-31 17:10:10 +00:00
|
|
|
be.m.Lock()
|
|
|
|
defer be.m.Unlock()
|
|
|
|
|
2020-11-07 23:05:53 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2016-08-31 17:10:10 +00:00
|
|
|
be.data = make(memMap)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the backend.
|
|
|
|
func (be *MemoryBackend) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|