restic/internal/backend/mem/mem_backend.go

293 lines
6.1 KiB
Go
Raw Normal View History

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"
2017-06-03 17:39:57 +02:00
"context"
"encoding/base64"
"hash"
2015-11-22 16:12:00 +01:00
"io"
"sync"
2015-11-22 16:30:13 +01:00
2022-10-15 23:14:33 +02:00
"github.com/cespare/xxhash/v2"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/sema"
2020-12-17 12:47:53 +01:00
"github.com/restic/restic/internal/debug"
2017-07-23 14:21:03 +02:00
"github.com/restic/restic/internal/errors"
2017-07-24 17:42:25 +02:00
"github.com/restic/restic/internal/restic"
2017-07-23 14:21:03 +02:00
2020-12-17 12:47:53 +01:00
"github.com/cenkalti/backoff/v4"
2015-11-22 16:12:00 +01:00
)
type memMap map[restic.Handle][]byte
2015-11-22 16:12:00 +01:00
// make sure that MemoryBackend implements backend.Backend
2016-08-31 22:39:36 +02:00
var _ restic.Backend = &MemoryBackend{}
2017-06-15 13:40:27 +02:00
var errNotFound = errors.New("not found")
const connectionCount = 2
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
sem sema.Semaphore
2015-11-22 16:12:00 +01:00
}
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 {
sem, err := sema.New(connectionCount)
if err != nil {
panic(err)
}
2015-11-22 16:12:00 +01:00
be := &MemoryBackend{
data: make(memMap),
sem: sem,
2015-11-22 16:12:00 +01:00
}
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
}
// Test returns whether a file exists.
2017-06-03 17:39:57 +02:00
func (be *MemoryBackend) Test(ctx context.Context, h restic.Handle) (bool, error) {
be.sem.GetToken()
defer be.sem.ReleaseToken()
2015-11-22 16:12:00 +01:00
be.m.Lock()
defer be.m.Unlock()
debug.Log("Test %v", h)
2015-11-22 16:30:13 +01:00
if _, ok := be.data[h]; ok {
2020-11-08 00:05:53 +01:00
return true, ctx.Err()
2015-11-22 16:12:00 +01:00
}
2020-11-08 00:05:53 +01:00
return false, ctx.Err()
2015-11-22 16:12:00 +01:00
}
2017-06-15 13:40:27 +02:00
// IsNotExist returns true if the file does not exist.
func (be *MemoryBackend) IsNotExist(err error) bool {
return errors.Is(err, errNotFound)
2017-06-15 13:40:27 +02:00
}
// Save adds new Data to the backend.
func (be *MemoryBackend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
2016-01-24 01:15:35 +01:00
if err := h.Valid(); err != nil {
2020-12-17 12:47:53 +01:00
return backoff.Permanent(err)
2016-01-24 01:15:35 +01:00
}
be.sem.GetToken()
defer be.sem.ReleaseToken()
2016-01-24 01:15:35 +01:00
be.m.Lock()
defer be.m.Unlock()
2020-07-28 10:13:11 +02:00
h.ContainedBlobType = restic.InvalidBlob
2016-09-01 21:19:30 +02:00
if h.Type == restic.ConfigFile {
2016-01-24 01:15:35 +01:00
h.Name = ""
}
if _, ok := be.data[h]; ok {
2016-01-24 20:23:50 +01:00
return errors.New("file already exists")
}
buf, err := io.ReadAll(rd)
if err != nil {
return err
}
// sanity check
if int64(len(buf)) != rd.Length() {
return errors.Errorf("wrote %d bytes instead of the expected %d bytes", len(buf), rd.Length())
}
beHash := be.Hasher()
// must never fail according to interface
_, err = beHash.Write(buf)
if err != nil {
panic(err)
}
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()),
)
}
be.data[h] = buf
debug.Log("saved %v bytes at %v", len(buf), h)
2020-11-08 00:05:53 +01:00
return ctx.Err()
2016-01-24 01:15:35 +01: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 22:01:12 +01:00
if err := h.Valid(); err != nil {
2020-12-17 12:47:53 +01:00
return nil, backoff.Permanent(err)
2017-01-22 22:01:12 +01:00
}
be.sem.GetToken()
2017-01-22 22:01:12 +01:00
be.m.Lock()
defer be.m.Unlock()
2020-07-28 10:13:11 +02:00
h.ContainedBlobType = restic.InvalidBlob
2017-01-22 22:01:12 +01:00
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 {
be.sem.ReleaseToken()
2017-01-22 22:01:12 +01:00
return nil, errors.New("offset is negative")
}
if _, ok := be.data[h]; !ok {
be.sem.ReleaseToken()
2017-06-15 13:40:27 +02:00
return nil, errNotFound
2017-01-22 22:01:12 +01:00
}
buf := be.data[h]
2017-01-22 22:01:12 +01:00
if offset > int64(len(buf)) {
be.sem.ReleaseToken()
2017-01-22 22:01:12 +01:00
return nil, errors.New("offset beyond end of file")
}
buf = buf[offset:]
if length > 0 && len(buf) > length {
buf = buf[:length]
}
return be.sem.ReleaseTokenOnClose(io.NopCloser(bytes.NewReader(buf)), nil), ctx.Err()
2017-01-22 22:01:12 +01:00
}
// Stat returns information about a file in the backend.
2017-06-03 17:39:57 +02:00
func (be *MemoryBackend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, error) {
2016-01-23 23:27:58 +01:00
if err := h.Valid(); err != nil {
2020-12-17 12:47:53 +01:00
return restic.FileInfo{}, backoff.Permanent(err)
2016-01-23 23:27:58 +01:00
}
be.sem.GetToken()
defer be.sem.ReleaseToken()
be.m.Lock()
defer be.m.Unlock()
2020-07-28 10:13:11 +02:00
h.ContainedBlobType = restic.InvalidBlob
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
e, ok := be.data[h]
2016-01-23 23:27:58 +01:00
if !ok {
2017-06-15 13:40:27 +02:00
return restic.FileInfo{}, errNotFound
2016-01-23 23:27:58 +01:00
}
2020-11-08 00:05:53 +01:00
return restic.FileInfo{Size: int64(len(e)), Name: h.Name}, ctx.Err()
2016-01-23 23:27:58 +01:00
}
// Remove deletes a file from the backend.
2017-06-03 17:39:57 +02:00
func (be *MemoryBackend) Remove(ctx context.Context, h restic.Handle) error {
be.sem.GetToken()
defer be.sem.ReleaseToken()
2015-11-22 16:12:00 +01:00
be.m.Lock()
defer be.m.Unlock()
debug.Log("Remove %v", h)
2015-11-22 16:30:13 +01:00
2020-07-28 10:13:11 +02:00
h.ContainedBlobType = restic.InvalidBlob
if _, ok := be.data[h]; !ok {
2017-06-15 13:40:27 +02:00
return errNotFound
2015-11-22 16:12:00 +01:00
}
delete(be.data, h)
2015-11-22 16:12:00 +01:00
2020-11-08 00:05:53 +01:00
return ctx.Err()
2015-11-22 16:12:00 +01:00
}
// List returns a channel which yields entries from the backend.
func (be *MemoryBackend) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
entries := make(map[string]int64)
2015-11-22 16:12:00 +01:00
be.m.Lock()
for entry, buf := range be.data {
2015-11-22 16:12:00 +01:00
if entry.Type != t {
continue
}
entries[entry.Name] = int64(len(buf))
}
be.m.Unlock()
for name, size := range entries {
fi := restic.FileInfo{
Name: name,
Size: size,
}
if ctx.Err() != nil {
return ctx.Err()
}
err := fn(fi)
if err != nil {
return err
}
2015-11-22 16:30:13 +01:00
if ctx.Err() != nil {
return ctx.Err()
2015-11-22 16:12:00 +01:00
}
}
2015-11-22 16:12:00 +01:00
return ctx.Err()
2015-11-22 16:12:00 +01:00
}
2021-08-07 22:20:49 +02:00
func (be *MemoryBackend) Connections() uint {
return connectionCount
2021-08-07 22:20:49 +02:00
}
// Location returns the location of the backend (RAM).
func (be *MemoryBackend) Location() string {
return "RAM"
}
// Hasher may return a hash function for calculating a content hash for the backend
func (be *MemoryBackend) Hasher() hash.Hash {
2022-10-15 23:14:33 +02:00
return xxhash.New()
}
// HasAtomicReplace returns whether Save() can atomically replace files
func (be *MemoryBackend) HasAtomicReplace() bool {
return false
}
// Delete removes all data in the backend.
2017-06-03 17:39:57 +02:00
func (be *MemoryBackend) Delete(ctx context.Context) error {
be.m.Lock()
defer be.m.Unlock()
2020-11-08 00:05:53 +01:00
if ctx.Err() != nil {
return ctx.Err()
}
be.data = make(memMap)
return nil
}
// Close closes the backend.
func (be *MemoryBackend) Close() error {
return nil
}