2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-16 18:15:20 +00:00
restic/internal/backend/mem/mem_backend.go

251 lines
5.4 KiB
Go
Raw Normal View History

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"
"encoding/base64"
"hash"
2015-11-22 15:12:00 +00:00
"io"
"net/http"
2015-11-22 15:12:00 +00:00
"sync"
2015-11-22 15:30:13 +00:00
2022-10-15 21:14:33 +00:00
"github.com/cespare/xxhash/v2"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/backend/location"
"github.com/restic/restic/internal/backend/util"
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"
2015-11-22 15:12:00 +00:00
)
type memMap map[backend.Handle][]byte
2015-11-22 15:12:00 +00:00
// make sure that MemoryBackend implements backend.Backend
var _ backend.Backend = &MemoryBackend{}
// NewFactory creates a persistent mem backend
func NewFactory() location.Factory {
be := New()
return location.NewHTTPBackendFactory[struct{}, *MemoryBackend](
"mem",
func(s string) (*struct{}, error) {
return &struct{}{}, nil
},
location.NoPassword,
func(_ context.Context, _ struct{}, _ http.RoundTripper) (*MemoryBackend, error) {
return be, nil
},
func(_ context.Context, _ struct{}, _ http.RoundTripper) (*MemoryBackend, error) {
return be, nil
},
)
}
2017-06-15 11:40:27 +00:00
var errNotFound = errors.New("not found")
const connectionCount = 2
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
}
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.Is(err, errNotFound)
2017-06-15 11:40:27 +00:00
}
// Save adds new Data to the backend.
func (be *MemoryBackend) Save(ctx context.Context, h backend.Handle, rd backend.RewindReader) error {
2016-01-24 00:15:35 +00:00
be.m.Lock()
defer be.m.Unlock()
2023-10-01 08:52:57 +00:00
h.IsMetadata = false
if h.Type == backend.ConfigFile {
2016-01-24 00:15:35 +00:00
h.Name = ""
}
if _, ok := be.data[h]; ok {
2016-01-24 19:23:50 +00: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
2020-11-07 23:05:53 +00:00
return ctx.Err()
2016-01-24 00:15:35 +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 backend.Handle, length int, offset int64, fn func(rd io.Reader) error) error {
return util.DefaultLoad(ctx, h, length, offset, be.openReader, fn)
}
func (be *MemoryBackend) openReader(ctx context.Context, h backend.Handle, length int, offset int64) (io.ReadCloser, error) {
2017-01-22 21:01:12 +00:00
be.m.Lock()
defer be.m.Unlock()
2023-10-01 08:52:57 +00:00
h.IsMetadata = false
if h.Type == backend.ConfigFile {
2017-01-22 21:01:12 +00:00
h.Name = ""
}
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
}
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]
}
return io.NopCloser(bytes.NewReader(buf)), ctx.Err()
2017-01-22 21:01:12 +00:00
}
// Stat returns information about a file in the backend.
func (be *MemoryBackend) Stat(ctx context.Context, h backend.Handle) (backend.FileInfo, error) {
be.m.Lock()
defer be.m.Unlock()
2023-10-01 08:52:57 +00:00
h.IsMetadata = false
if h.Type == backend.ConfigFile {
2016-01-23 22:27:58 +00:00
h.Name = ""
}
e, ok := be.data[h]
2016-01-23 22:27:58 +00:00
if !ok {
return backend.FileInfo{}, errNotFound
2016-01-23 22:27:58 +00:00
}
return backend.FileInfo{Size: int64(len(e)), Name: h.Name}, ctx.Err()
2016-01-23 22:27:58 +00:00
}
// Remove deletes a file from the backend.
func (be *MemoryBackend) Remove(ctx context.Context, h backend.Handle) error {
2015-11-22 15:12:00 +00:00
be.m.Lock()
defer be.m.Unlock()
2023-10-01 08:52:57 +00:00
h.IsMetadata = false
if _, ok := be.data[h]; !ok {
2017-06-15 11:40:27 +00:00
return errNotFound
2015-11-22 15:12:00 +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
}
// List returns a channel which yields entries from the backend.
func (be *MemoryBackend) List(ctx context.Context, t backend.FileType, fn func(backend.FileInfo) error) error {
entries := make(map[string]int64)
2015-11-22 15:12:00 +00:00
be.m.Lock()
for entry, buf := range be.data {
2015-11-22 15:12:00 +00:00
if entry.Type != t {
continue
}
entries[entry.Name] = int64(len(buf))
}
be.m.Unlock()
for name, size := range entries {
fi := backend.FileInfo{
Name: name,
Size: size,
}
if ctx.Err() != nil {
return ctx.Err()
}
err := fn(fi)
if err != nil {
return err
}
2015-11-22 15:30:13 +00:00
if ctx.Err() != nil {
return ctx.Err()
2015-11-22 15:12:00 +00:00
}
}
2015-11-22 15:12:00 +00:00
return ctx.Err()
2015-11-22 15:12:00 +00:00
}
2021-08-07 20:20:49 +00:00
func (be *MemoryBackend) Connections() uint {
return connectionCount
2021-08-07 20:20:49 +00: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 21:14:33 +00: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 15:39:57 +00:00
func (be *MemoryBackend) Delete(ctx context.Context) error {
be.m.Lock()
defer be.m.Unlock()
2020-11-07 23:05:53 +00: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
}