restic/internal/mock/backend.go

119 lines
2.8 KiB
Go
Raw Normal View History

package mock
import (
2017-06-04 09:16:55 +00:00
"context"
"io"
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"
)
// Backend implements a mock backend.
type Backend struct {
2017-06-15 11:40:27 +00:00
CloseFn func() error
IsNotExistFn func(err error) bool
SaveFn func(ctx context.Context, h restic.Handle, rd io.Reader) error
LoadFn func(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error)
StatFn func(ctx context.Context, h restic.Handle) (restic.FileInfo, error)
ListFn func(ctx context.Context, t restic.FileType) <-chan string
RemoveFn func(ctx context.Context, h restic.Handle) error
TestFn func(ctx context.Context, h restic.Handle) (bool, error)
DeleteFn func(ctx context.Context) error
LocationFn func() string
}
// Close the backend.
func (m *Backend) Close() error {
if m.CloseFn == nil {
return nil
}
return m.CloseFn()
}
// Location returns a location string.
func (m *Backend) Location() string {
if m.LocationFn == nil {
return ""
}
return m.LocationFn()
}
2017-06-15 11:40:27 +00:00
// IsNotExist returns true if the error is caused by a missing file.
func (m *Backend) IsNotExist(err error) bool {
if m.IsNotExistFn == nil {
return false
}
return m.IsNotExistFn(err)
}
// Save data in the backend.
2017-06-04 09:16:55 +00:00
func (m *Backend) Save(ctx context.Context, h restic.Handle, rd io.Reader) error {
if m.SaveFn == nil {
return errors.New("not implemented")
}
2017-06-04 09:16:55 +00:00
return m.SaveFn(ctx, h, rd)
}
2017-01-23 17:11:10 +00:00
// Load loads data from the backend.
2017-06-04 09:16:55 +00:00
func (m *Backend) Load(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
2017-01-23 17:11:10 +00:00
if m.LoadFn == nil {
2017-01-22 21:01:12 +00:00
return nil, errors.New("not implemented")
}
2017-06-04 09:16:55 +00:00
return m.LoadFn(ctx, h, length, offset)
2017-01-22 21:01:12 +00:00
}
// Stat an object in the backend.
2017-06-04 09:16:55 +00:00
func (m *Backend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, error) {
if m.StatFn == nil {
2016-08-31 20:39:36 +00:00
return restic.FileInfo{}, errors.New("not implemented")
}
2017-06-04 09:16:55 +00:00
return m.StatFn(ctx, h)
}
// List items of type t.
2017-06-04 09:16:55 +00:00
func (m *Backend) List(ctx context.Context, t restic.FileType) <-chan string {
if m.ListFn == nil {
ch := make(chan string)
close(ch)
return ch
}
2017-06-04 09:16:55 +00:00
return m.ListFn(ctx, t)
}
// Remove data from the backend.
2017-06-04 09:16:55 +00:00
func (m *Backend) Remove(ctx context.Context, h restic.Handle) error {
if m.RemoveFn == nil {
return errors.New("not implemented")
}
2017-06-04 09:16:55 +00:00
return m.RemoveFn(ctx, h)
}
// Test for the existence of a specific item.
2017-06-04 09:16:55 +00:00
func (m *Backend) Test(ctx context.Context, h restic.Handle) (bool, error) {
if m.TestFn == nil {
return false, errors.New("not implemented")
}
2017-06-04 09:16:55 +00:00
return m.TestFn(ctx, h)
}
// Delete all data.
2017-06-04 09:16:55 +00:00
func (m *Backend) Delete(ctx context.Context) error {
if m.DeleteFn == nil {
return errors.New("not implemented")
}
2017-06-04 09:16:55 +00:00
return m.DeleteFn(ctx)
}
// Make sure that Backend implements the backend interface.
var _ restic.Backend = &Backend{}