2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 04:45:15 +00:00

Merge branch 'refactor-backend'

This commit is contained in:
Alexander Neumann 2014-12-21 18:34:04 +01:00
commit 5c9c1c9dbd
23 changed files with 376 additions and 295 deletions

View File

@ -22,9 +22,8 @@ const (
) )
type Archiver struct { type Archiver struct {
be backend.Server s Server
key *Key ch *ContentHandler
ch *ContentHandler
bl *BlobList // blobs used for the current snapshot bl *BlobList // blobs used for the current snapshot
parentBl *BlobList // blobs from the parent snapshot parentBl *BlobList // blobs from the parent snapshot
@ -58,11 +57,10 @@ func (s *Stats) Add(other Stats) {
s.Other += other.Other s.Other += other.Other
} }
func NewArchiver(be backend.Server, key *Key) (*Archiver, error) { func NewArchiver(s Server) (*Archiver, error) {
var err error var err error
arch := &Archiver{ arch := &Archiver{
be: be, s: s,
key: key,
fileToken: make(chan struct{}, maxConcurrentFiles), fileToken: make(chan struct{}, maxConcurrentFiles),
blobToken: make(chan struct{}, maxConcurrentBlobs), blobToken: make(chan struct{}, maxConcurrentBlobs),
} }
@ -82,7 +80,7 @@ func NewArchiver(be backend.Server, key *Key) (*Archiver, error) {
arch.Filter = func(string, os.FileInfo) bool { return true } arch.Filter = func(string, os.FileInfo) bool { return true }
arch.bl = NewBlobList() arch.bl = NewBlobList()
arch.ch, err = NewContentHandler(be, key) arch.ch, err = NewContentHandler(s)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -8,15 +8,12 @@ import (
"errors" "errors"
"io/ioutil" "io/ioutil"
"sort" "sort"
"sync"
) )
const ( const (
MinPrefixLength = 4 MinPrefixLength = 4
) )
var idPool = sync.Pool{New: func() interface{} { return ID(make([]byte, IDSize)) }}
var ( var (
ErrNoIDPrefixFound = errors.New("no ID found") ErrNoIDPrefixFound = errors.New("no ID found")
ErrMultipleIDMatches = errors.New("multiple IDs with prefix found") ErrMultipleIDMatches = errors.New("multiple IDs with prefix found")
@ -24,7 +21,10 @@ var (
// Each lists all entries of type t in the backend and calls function f() with // Each lists all entries of type t in the backend and calls function f() with
// the id and data. // the id and data.
func Each(be Server, t Type, f func(id ID, data []byte, err error)) error { func Each(be interface {
Lister
Getter
}, t Type, f func(id ID, data []byte, err error)) error {
ids, err := be.List(t) ids, err := be.List(t)
if err != nil { if err != nil {
return err return err
@ -45,7 +45,7 @@ func Each(be Server, t Type, f func(id ID, data []byte, err error)) error {
// Each lists all entries of type t in the backend and calls function f() with // Each lists all entries of type t in the backend and calls function f() with
// the id. // the id.
func EachID(be Server, t Type, f func(ID)) error { func EachID(be Lister, t Type, f func(ID)) error {
ids, err := be.List(t) ids, err := be.List(t)
if err != nil { if err != nil {
return err return err
@ -101,7 +101,7 @@ func Hash(data []byte) ID {
// Find loads the list of all blobs of type t and searches for IDs which start // Find loads the list of all blobs of type t and searches for IDs which start
// with prefix. If none is found, nil and ErrNoIDPrefixFound is returned. If // with prefix. If none is found, nil and ErrNoIDPrefixFound is returned. If
// more than one is found, nil and ErrMultipleIDMatches is returned. // more than one is found, nil and ErrMultipleIDMatches is returned.
func Find(be Server, t Type, prefix string) (ID, error) { func Find(be Lister, t Type, prefix string) (ID, error) {
p, err := hex.DecodeString(prefix) p, err := hex.DecodeString(prefix)
if err != nil { if err != nil {
return nil, err return nil, err
@ -134,7 +134,7 @@ func Find(be Server, t Type, prefix string) (ID, error) {
// FindSnapshot takes a string and tries to find a snapshot whose ID matches // FindSnapshot takes a string and tries to find a snapshot whose ID matches
// the string as closely as possible. // the string as closely as possible.
func FindSnapshot(be Server, s string) (ID, error) { func FindSnapshot(be Lister, s string) (ID, error) {
// parse ID directly // parse ID directly
if id, err := ParseID(s); err == nil { if id, err := ParseID(s); err == nil {
return id, nil return id, nil

View File

@ -46,9 +46,40 @@ func str2id(s string) backend.ID {
return id return id
} }
type IDList backend.IDs type mockBackend struct {
list func(backend.Type) (backend.IDs, error)
get func(backend.Type, backend.ID) ([]byte, error)
create func(backend.Type, []byte) (backend.ID, error)
test func(backend.Type, backend.ID) (bool, error)
remove func(backend.Type, backend.ID) error
close func() error
}
var samples = IDList{ func (m mockBackend) List(t backend.Type) (backend.IDs, error) {
return m.list(t)
}
func (m mockBackend) Get(t backend.Type, id backend.ID) ([]byte, error) {
return m.get(t, id)
}
func (m mockBackend) Create(t backend.Type, data []byte) (backend.ID, error) {
return m.create(t, data)
}
func (m mockBackend) Test(t backend.Type, id backend.ID) (bool, error) {
return m.test(t, id)
}
func (m mockBackend) Remove(t backend.Type, id backend.ID) error {
return m.remove(t, id)
}
func (m mockBackend) Close() error {
return m.close()
}
var samples = backend.IDs{
str2id("20bdc1402a6fc9b633aaffffffffffffffffffffffffffffffffffffffffffff"), str2id("20bdc1402a6fc9b633aaffffffffffffffffffffffffffffffffffffffffffff"),
str2id("20bdc1402a6fc9b633ccd578c4a92d0f4ef1a457fa2e16c596bc73fb409d6cc0"), str2id("20bdc1402a6fc9b633ccd578c4a92d0f4ef1a457fa2e16c596bc73fb409d6cc0"),
str2id("20bdc1402a6fc9b633ffffffffffffffffffffffffffffffffffffffffffffff"), str2id("20bdc1402a6fc9b633ffffffffffffffffffffffffffffffffffffffffffffff"),
@ -59,20 +90,25 @@ var samples = IDList{
str2id("fa31d65b87affcd167b119e9d3d2a27b8236ca4836cb077ed3e96fcbe209b792"), str2id("fa31d65b87affcd167b119e9d3d2a27b8236ca4836cb077ed3e96fcbe209b792"),
} }
func (l IDList) List(backend.Type) (backend.IDs, error) {
return backend.IDs(l), nil
}
func TestPrefixLength(t *testing.T) { func TestPrefixLength(t *testing.T) {
l, err := backend.PrefixLength(samples, backend.Snapshot) list := samples
m := mockBackend{}
m.list = func(t backend.Type) (backend.IDs, error) {
return list, nil
}
l, err := backend.PrefixLength(m, backend.Snapshot)
ok(t, err) ok(t, err)
equals(t, 10, l) equals(t, 10, l)
l, err = backend.PrefixLength(samples[:3], backend.Snapshot) list = samples[:3]
l, err = backend.PrefixLength(m, backend.Snapshot)
ok(t, err) ok(t, err)
equals(t, 10, l) equals(t, 10, l)
l, err = backend.PrefixLength(samples[3:], backend.Snapshot) list = samples[3:]
l, err = backend.PrefixLength(m, backend.Snapshot)
ok(t, err) ok(t, err)
equals(t, 4, l) equals(t, 4, l)
} }

View File

@ -6,6 +6,7 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
"sync"
) )
// IDSize contains the size of an ID, in bytes. // IDSize contains the size of an ID, in bytes.
@ -14,6 +15,8 @@ const IDSize = sha256.Size
// References content within a repository. // References content within a repository.
type ID []byte type ID []byte
var idPool = sync.Pool{New: func() interface{} { return ID(make([]byte, IDSize)) }}
// ParseID converts the given string to an ID. // ParseID converts the given string to an ID.
func ParseID(s string) (ID, error) { func ParseID(s string) (ID, error) {
b, err := hex.DecodeString(s) b, err := hex.DecodeString(s)

View File

@ -25,15 +25,39 @@ type Lister interface {
List(Type) (IDs, error) List(Type) (IDs, error)
} }
type Server interface { type Getter interface {
Create(Type, []byte) (ID, error)
Get(Type, ID) ([]byte, error) Get(Type, ID) ([]byte, error)
Lister }
type Creater interface {
Create(Type, []byte) (ID, error)
}
type Tester interface {
Test(Type, ID) (bool, error) Test(Type, ID) (bool, error)
}
type Remover interface {
Remove(Type, ID) error Remove(Type, ID) error
Version() uint }
type Closer interface {
Close() error Close() error
}
type Deleter interface {
Delete() error
}
type Locationer interface {
Location() string Location() string
} }
type Backend interface {
Lister
Getter
Creater
Tester
Remover
Closer
}

View File

@ -328,3 +328,8 @@ func (b *Local) Version() uint {
func (b *Local) Close() error { func (b *Local) Close() error {
return nil return nil
} }
// Delete removes the repository and all files.
func (b *Local) Delete() error {
return os.RemoveAll(b.p)
}

View File

@ -4,7 +4,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"sort" "sort"
"testing" "testing"
@ -41,10 +40,10 @@ func teardownBackend(t *testing.T, b *backend.Local) {
return return
} }
ok(t, os.RemoveAll(b.Location())) ok(t, b.Delete())
} }
func testBackend(b backend.Server, t *testing.T) { func testBackend(b *backend.Local, t *testing.T) {
for _, tpe := range []backend.Type{backend.Data, backend.Key, backend.Lock, backend.Snapshot, backend.Tree, backend.Map} { for _, tpe := range []backend.Type{backend.Data, backend.Key, backend.Lock, backend.Snapshot, backend.Tree, backend.Map} {
// detect non-existing files // detect non-existing files
for _, test := range TestStrings { for _, test := range TestStrings {
@ -126,10 +125,10 @@ func TestBackend(t *testing.T) {
assert(t, err != nil, "opening invalid repository at /invalid-restic-test should have failed, but err is nil") assert(t, err != nil, "opening invalid repository at /invalid-restic-test should have failed, but err is nil")
assert(t, b == nil, fmt.Sprintf("opening invalid repository at /invalid-restic-test should have failed, but b is not nil: %v", b)) assert(t, b == nil, fmt.Sprintf("opening invalid repository at /invalid-restic-test should have failed, but b is not nil: %v", b))
b = setupBackend(t) s := setupBackend(t)
defer teardownBackend(t, b) defer teardownBackend(t, s)
testBackend(b, t) testBackend(s, t)
} }
func TestLocalBackendCreationFailures(t *testing.T) { func TestLocalBackendCreationFailures(t *testing.T) {

View File

@ -72,7 +72,7 @@ func (cmd CmdBackup) Execute(args []string) error {
return fmt.Errorf("wrong number of parameters, Usage: %s", cmd.Usage()) return fmt.Errorf("wrong number of parameters, Usage: %s", cmd.Usage())
} }
be, key, err := OpenRepo() s, err := OpenRepo()
if err != nil { if err != nil {
return err return err
} }
@ -81,7 +81,7 @@ func (cmd CmdBackup) Execute(args []string) error {
target := args[0] target := args[0]
if len(args) > 1 { if len(args) > 1 {
parentSnapshotID, err = backend.FindSnapshot(be, args[1]) parentSnapshotID, err = s.FindSnapshot(args[1])
if err != nil { if err != nil {
return fmt.Errorf("invalid id %q: %v", args[1], err) return fmt.Errorf("invalid id %q: %v", args[1], err)
} }
@ -89,7 +89,7 @@ func (cmd CmdBackup) Execute(args []string) error {
fmt.Printf("found parent snapshot %v\n", parentSnapshotID) fmt.Printf("found parent snapshot %v\n", parentSnapshotID)
} }
arch, err := restic.NewArchiver(be, key) arch, err := restic.NewArchiver(s)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "err: %v\n", err) fmt.Fprintf(os.Stderr, "err: %v\n", err)
} }
@ -183,7 +183,7 @@ func (cmd CmdBackup) Execute(args []string) error {
close(arch.ScannerStats) close(arch.ScannerStats)
} }
plen, err := backend.PrefixLength(be, backend.Snapshot) plen, err := s.PrefixLength(backend.Snapshot)
if err != nil { if err != nil {
return err return err
} }

View File

@ -31,7 +31,7 @@ func (cmd CmdCat) Execute(args []string) error {
return fmt.Errorf("type or ID not specified, Usage: %s", cmd.Usage()) return fmt.Errorf("type or ID not specified, Usage: %s", cmd.Usage())
} }
be, key, err := OpenRepo() s, err := OpenRepo()
if err != nil { if err != nil {
return err return err
} }
@ -47,13 +47,13 @@ func (cmd CmdCat) Execute(args []string) error {
} }
// find snapshot id with prefix // find snapshot id with prefix
id, err = backend.Find(be, backend.Snapshot, args[1]) id, err = s.FindSnapshot(args[1])
if err != nil { if err != nil {
return err return err
} }
} }
ch, err := restic.NewContentHandler(be, key) ch, err := restic.NewContentHandler(s)
if err != nil { if err != nil {
return err return err
} }
@ -73,13 +73,13 @@ func (cmd CmdCat) Execute(args []string) error {
} }
// try storage id // try storage id
buf, err := be.Get(backend.Data, id) buf, err := s.Get(backend.Data, id)
if err != nil { if err != nil {
return err return err
} }
// decrypt // decrypt
buf, err = key.Decrypt(buf) buf, err = s.Decrypt(buf)
if err != nil { if err != nil {
return err return err
} }
@ -98,13 +98,13 @@ func (cmd CmdCat) Execute(args []string) error {
err := ch.LoadJSON(backend.Tree, id, &tree) err := ch.LoadJSON(backend.Tree, id, &tree)
if err != nil { if err != nil {
// try storage id // try storage id
buf, err := be.Get(backend.Tree, id) buf, err := s.Get(backend.Tree, id)
if err != nil { if err != nil {
return err return err
} }
// decrypt // decrypt
buf, err = key.Decrypt(buf) buf, err = s.Decrypt(buf)
if err != nil { if err != nil {
return err return err
} }
@ -156,7 +156,7 @@ func (cmd CmdCat) Execute(args []string) error {
return nil return nil
case "key": case "key":
data, err := be.Get(backend.Key, id) data, err := s.Get(backend.Key, id)
if err != nil { if err != nil {
return err return err
} }

View File

@ -110,10 +110,10 @@ func (c CmdFind) findInTree(ch *restic.ContentHandler, id backend.ID, path strin
return results, nil return results, nil
} }
func (c CmdFind) findInSnapshot(be backend.Server, key *restic.Key, id backend.ID) error { func (c CmdFind) findInSnapshot(s restic.Server, id backend.ID) error {
debug("searching in snapshot %s\n for entries within [%s %s]", id, c.oldest, c.newest) debug("searching in snapshot %s\n for entries within [%s %s]", id, c.oldest, c.newest)
ch, err := restic.NewContentHandler(be, key) ch, err := restic.NewContentHandler(s)
if err != nil { if err != nil {
return err return err
} }
@ -166,7 +166,7 @@ func (c CmdFind) Execute(args []string) error {
} }
} }
be, key, err := OpenRepo() s, err := OpenRepo()
if err != nil { if err != nil {
return err return err
} }
@ -174,21 +174,21 @@ func (c CmdFind) Execute(args []string) error {
c.pattern = args[0] c.pattern = args[0]
if c.Snapshot != "" { if c.Snapshot != "" {
snapshotID, err := backend.FindSnapshot(be, c.Snapshot) snapshotID, err := backend.FindSnapshot(s, c.Snapshot)
if err != nil { if err != nil {
return fmt.Errorf("invalid id %q: %v", args[1], err) return fmt.Errorf("invalid id %q: %v", args[1], err)
} }
return c.findInSnapshot(be, key, snapshotID) return c.findInSnapshot(s, snapshotID)
} }
list, err := be.List(backend.Snapshot) list, err := s.List(backend.Snapshot)
if err != nil { if err != nil {
return err return err
} }
for _, snapshotID := range list { for _, snapshotID := range list {
err := c.findInSnapshot(be, key, snapshotID) err := c.findInSnapshot(s, snapshotID)
if err != nil { if err != nil {
return err return err

View File

@ -75,10 +75,10 @@ func fsckTree(ch *restic.ContentHandler, id backend.ID) error {
return nil return nil
} }
func fsck_snapshot(be backend.Server, key *restic.Key, id backend.ID) error { func fsck_snapshot(s restic.Server, id backend.ID) error {
debug("checking snapshot %v\n", id) debug("checking snapshot %v\n", id)
ch, err := restic.NewContentHandler(be, key) ch, err := restic.NewContentHandler(s)
if err != nil { if err != nil {
return err return err
} }
@ -108,27 +108,27 @@ func (cmd CmdFsck) Execute(args []string) error {
return fmt.Errorf("type or ID not specified, Usage: %s", cmd.Usage()) return fmt.Errorf("type or ID not specified, Usage: %s", cmd.Usage())
} }
be, key, err := OpenRepo() s, err := OpenRepo()
if err != nil { if err != nil {
return err return err
} }
if len(args) == 1 && args[0] != "all" { if len(args) == 1 && args[0] != "all" {
snapshotID, err := backend.FindSnapshot(be, args[0]) snapshotID, err := s.FindSnapshot(args[0])
if err != nil { if err != nil {
return fmt.Errorf("invalid id %q: %v", args[0], err) return fmt.Errorf("invalid id %q: %v", args[0], err)
} }
return fsck_snapshot(be, key, snapshotID) return fsck_snapshot(s, snapshotID)
} }
list, err := be.List(backend.Snapshot) list, err := s.List(backend.Snapshot)
if err != nil { if err != nil {
return err return err
} }
for _, snapshotID := range list { for _, snapshotID := range list {
err := fsck_snapshot(be, key, snapshotID) err := fsck_snapshot(s, snapshotID)
if err != nil { if err != nil {
return err return err

View File

@ -22,17 +22,17 @@ func init() {
} }
} }
func list_keys(be backend.Server, key *restic.Key) error { func list_keys(s restic.Server) error {
tab := NewTable() tab := NewTable()
tab.Header = fmt.Sprintf(" %-10s %-10s %-10s %s", "ID", "User", "Host", "Created") tab.Header = fmt.Sprintf(" %-10s %-10s %-10s %s", "ID", "User", "Host", "Created")
tab.RowFormat = "%s%-10s %-10s %-10s %s" tab.RowFormat = "%s%-10s %-10s %-10s %s"
plen, err := backend.PrefixLength(be, backend.Key) plen, err := s.PrefixLength(backend.Key)
if err != nil { if err != nil {
return err return err
} }
backend.Each(be, backend.Key, func(id backend.ID, data []byte, err error) { s.Each(backend.Key, func(id backend.ID, data []byte, err error) {
k := restic.Key{} k := restic.Key{}
err = json.Unmarshal(data, &k) err = json.Unmarshal(data, &k)
if err != nil { if err != nil {
@ -40,7 +40,7 @@ func list_keys(be backend.Server, key *restic.Key) error {
} }
var current string var current string
if id.Equal(key.ID()) { if id.Equal(s.Key().ID()) {
current = "*" current = "*"
} else { } else {
current = " " current = " "
@ -54,7 +54,7 @@ func list_keys(be backend.Server, key *restic.Key) error {
return nil return nil
} }
func add_key(be backend.Server, key *restic.Key) error { func add_key(s restic.Server) error {
pw := readPassword("RESTIC_NEWPASSWORD", "enter password for new key: ") pw := readPassword("RESTIC_NEWPASSWORD", "enter password for new key: ")
pw2 := readPassword("RESTIC_NEWPASSWORD", "enter password again: ") pw2 := readPassword("RESTIC_NEWPASSWORD", "enter password again: ")
@ -62,7 +62,7 @@ func add_key(be backend.Server, key *restic.Key) error {
return errors.New("passwords do not match") return errors.New("passwords do not match")
} }
id, err := key.AddKey(be, pw) id, err := restic.AddKey(s, pw, s.Key())
if err != nil { if err != nil {
return fmt.Errorf("creating new key failed: %v\n", err) return fmt.Errorf("creating new key failed: %v\n", err)
} }
@ -72,12 +72,12 @@ func add_key(be backend.Server, key *restic.Key) error {
return nil return nil
} }
func delete_key(be backend.Server, key *restic.Key, id backend.ID) error { func delete_key(s restic.Server, id backend.ID) error {
if id.Equal(key.ID()) { if id.Equal(s.Key().ID()) {
return errors.New("refusing to remove key currently used to access repository") return errors.New("refusing to remove key currently used to access repository")
} }
err := be.Remove(backend.Key, id) err := s.Remove(backend.Key, id)
if err != nil { if err != nil {
return err return err
} }
@ -86,7 +86,7 @@ func delete_key(be backend.Server, key *restic.Key, id backend.ID) error {
return nil return nil
} }
func change_password(be backend.Server, key *restic.Key) error { func change_password(s restic.Server) error {
pw := readPassword("RESTIC_NEWPASSWORD", "enter password for new key: ") pw := readPassword("RESTIC_NEWPASSWORD", "enter password for new key: ")
pw2 := readPassword("RESTIC_NEWPASSWORD", "enter password again: ") pw2 := readPassword("RESTIC_NEWPASSWORD", "enter password again: ")
@ -95,13 +95,13 @@ func change_password(be backend.Server, key *restic.Key) error {
} }
// add new key // add new key
id, err := key.AddKey(be, pw) id, err := restic.AddKey(s, pw, s.Key())
if err != nil { if err != nil {
return fmt.Errorf("creating new key failed: %v\n", err) return fmt.Errorf("creating new key failed: %v\n", err)
} }
// remove old key // remove old key
err = be.Remove(backend.Key, key.ID()) err = s.Remove(backend.Key, s.Key().ID())
if err != nil { if err != nil {
return err return err
} }
@ -120,25 +120,25 @@ func (cmd CmdKey) Execute(args []string) error {
return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage()) return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage())
} }
be, key, err := OpenRepo() s, err := OpenRepo()
if err != nil { if err != nil {
return err return err
} }
switch args[0] { switch args[0] {
case "list": case "list":
return list_keys(be, key) return list_keys(s)
case "add": case "add":
return add_key(be, key) return add_key(s)
case "rm": case "rm":
id, err := backend.Find(be, backend.Key, args[1]) id, err := backend.Find(s, backend.Key, args[1])
if err != nil { if err != nil {
return err return err
} }
return delete_key(be, key, id) return delete_key(s, id)
case "change": case "change":
return change_password(be, key) return change_password(s)
} }
return nil return nil

View File

@ -3,6 +3,7 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/restic/restic/backend" "github.com/restic/restic/backend"
) )
@ -27,22 +28,22 @@ func (cmd CmdList) Execute(args []string) error {
return fmt.Errorf("type not specified, Usage: %s", cmd.Usage()) return fmt.Errorf("type not specified, Usage: %s", cmd.Usage())
} }
be, key, err := OpenRepo() s, err := OpenRepo()
if err != nil { if err != nil {
return err return err
} }
var ( var (
t backend.Type t backend.Type
each func(backend.Server, backend.Type, func(backend.ID, []byte, error)) error = backend.Each each func(backend.Type, func(backend.ID, []byte, error)) error = s.Each
) )
switch args[0] { switch args[0] {
case "data": case "data":
t = backend.Data t = backend.Data
each = key.Each each = s.EachDecrypted
case "trees": case "trees":
t = backend.Tree t = backend.Tree
each = key.Each each = s.EachDecrypted
case "snapshots": case "snapshots":
t = backend.Snapshot t = backend.Snapshot
case "maps": case "maps":
@ -55,7 +56,7 @@ func (cmd CmdList) Execute(args []string) error {
return errors.New("invalid type") return errors.New("invalid type")
} }
return each(be, t, func(id backend.ID, data []byte, err error) { return each(t, func(id backend.ID, data []byte, err error) {
if t == backend.Data || t == backend.Tree { if t == backend.Data || t == backend.Tree {
fmt.Printf("%s %s\n", id, backend.Hash(data)) fmt.Printf("%s %s\n", id, backend.Hash(data))
} else { } else {

View File

@ -63,22 +63,22 @@ func (cmd CmdLs) Usage() string {
return "ls snapshot-ID [DIR]" return "ls snapshot-ID [DIR]"
} }
func (cmd CmdLs) Execute(be backend.Server, key *restic.Key, args []string) error { func (cmd CmdLs) Execute(s restic.Server, key *restic.Key, args []string) error {
if len(args) < 1 || len(args) > 2 { if len(args) < 1 || len(args) > 2 {
return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage()) return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage())
} }
be, key, err := OpenRepo() s, err := OpenRepo()
if err != nil { if err != nil {
return err return err
} }
id, err := backend.FindSnapshot(be, args[0]) id, err := backend.FindSnapshot(s, args[0])
if err != nil { if err != nil {
return err return err
} }
ch, err := restic.NewContentHandler(be, key) ch, err := restic.NewContentHandler(s)
if err != nil { if err != nil {
return err return err
} }

View File

@ -29,12 +29,12 @@ func (cmd CmdRestore) Execute(args []string) error {
return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage()) return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage())
} }
be, key, err := OpenRepo() s, err := OpenRepo()
if err != nil { if err != nil {
return err return err
} }
id, err := backend.FindSnapshot(be, args[0]) id, err := backend.FindSnapshot(s, args[0])
if err != nil { if err != nil {
errx(1, "invalid id %q: %v", args[0], err) errx(1, "invalid id %q: %v", args[0], err)
} }
@ -42,7 +42,7 @@ func (cmd CmdRestore) Execute(args []string) error {
target := args[1] target := args[1]
// create restorer // create restorer
res, err := restic.NewRestorer(be, key, id) res, err := restic.NewRestorer(s, id)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "creating restorer failed: %v\n", err) fmt.Fprintf(os.Stderr, "creating restorer failed: %v\n", err)
os.Exit(2) os.Exit(2)

View File

@ -92,12 +92,12 @@ func (cmd CmdSnapshots) Execute(args []string) error {
return fmt.Errorf("wrong number of arguments, usage: %s", cmd.Usage()) return fmt.Errorf("wrong number of arguments, usage: %s", cmd.Usage())
} }
be, key, err := OpenRepo() s, err := OpenRepo()
if err != nil { if err != nil {
return err return err
} }
ch, err := restic.NewContentHandler(be, key) ch, err := restic.NewContentHandler(s)
if err != nil { if err != nil {
return err return err
} }
@ -107,7 +107,7 @@ func (cmd CmdSnapshots) Execute(args []string) error {
tab.RowFormat = "%-8s %-19s %-10s %s" tab.RowFormat = "%-8s %-19s %-10s %s"
list := []*restic.Snapshot{} list := []*restic.Snapshot{}
backend.EachID(be, backend.Snapshot, func(id backend.ID) { s.EachID(backend.Snapshot, func(id backend.ID) {
sn, err := restic.LoadSnapshot(ch, id) sn, err := restic.LoadSnapshot(ch, id)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error loading snapshot %s: %v\n", id, err) fmt.Fprintf(os.Stderr, "error loading snapshot %s: %v\n", id, err)
@ -127,7 +127,7 @@ func (cmd CmdSnapshots) Execute(args []string) error {
} }
}) })
plen, err := backend.PrefixLength(be, backend.Snapshot) plen, err := s.PrefixLength(backend.Snapshot)
if err != nil { if err != nil {
return err return err
} }

View File

@ -70,13 +70,15 @@ func (cmd CmdInit) Execute(args []string) error {
os.Exit(1) os.Exit(1)
} }
_, err = restic.CreateKey(be, pw) s := restic.NewServer(be)
_, err = restic.CreateKey(s, pw)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "creating key in backend at %s failed: %v\n", opts.Repo, err) fmt.Fprintf(os.Stderr, "creating key in backend at %s failed: %v\n", opts.Repo, err)
os.Exit(1) os.Exit(1)
} }
fmt.Printf("created restic backend at %s\n", be.Location()) fmt.Printf("created restic backend at %s\n", opts.Repo)
return nil return nil
} }
@ -86,7 +88,7 @@ func (cmd CmdInit) Execute(args []string) error {
// * /foo/bar -> local repository at /foo/bar // * /foo/bar -> local repository at /foo/bar
// * sftp://user@host/foo/bar -> remote sftp repository on host for user at path foo/bar // * sftp://user@host/foo/bar -> remote sftp repository on host for user at path foo/bar
// * sftp://host//tmp/backup -> remote sftp repository on host at path /tmp/backup // * sftp://host//tmp/backup -> remote sftp repository on host at path /tmp/backup
func open(u string) (backend.Server, error) { func open(u string) (backend.Backend, error) {
url, err := url.Parse(u) url, err := url.Parse(u)
if err != nil { if err != nil {
return nil, err return nil, err
@ -107,7 +109,7 @@ func open(u string) (backend.Server, error) {
} }
// Create the backend specified by URI. // Create the backend specified by URI.
func create(u string) (backend.Server, error) { func create(u string) (backend.Backend, error) {
url, err := url.Parse(u) url, err := url.Parse(u)
if err != nil { if err != nil {
return nil, err return nil, err
@ -127,18 +129,20 @@ func create(u string) (backend.Server, error) {
return backend.CreateSFTP(url.Path[1:], "ssh", args...) return backend.CreateSFTP(url.Path[1:], "ssh", args...)
} }
func OpenRepo() (backend.Server, *restic.Key, error) { func OpenRepo() (restic.Server, error) {
be, err := open(opts.Repo) be, err := open(opts.Repo)
if err != nil { if err != nil {
return nil, nil, err return restic.Server{}, err
} }
key, err := restic.SearchKey(be, readPassword("RESTIC_PASSWORD", "Enter Password for Repository: ")) s := restic.NewServer(be)
err = s.SearchKey(readPassword("RESTIC_PASSWORD", "Enter Password for Repository: "))
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("unable to open repo: %v", err) return restic.Server{}, fmt.Errorf("unable to open repo: %v", err)
} }
return be, key, nil return s, nil
} }
func init() { func init() {
@ -167,59 +171,5 @@ func main() {
os.Exit(1) os.Exit(1)
} }
// fmt.Printf("parser: %#v\n", parser)
// fmt.Printf("%#v\n", parser.Active.Name)
// if opts.Repo == "" {
// fmt.Fprintf(os.Stderr, "no repository specified, use -r or RESTIC_REPOSITORY variable\n")
// os.Exit(1)
// }
// if len(args) == 0 {
// cmds := []string{"init"}
// for k := range commands {
// cmds = append(cmds, k)
// }
// sort.Strings(cmds)
// fmt.Printf("nothing to do, available commands: [%v]\n", strings.Join(cmds, "|"))
// os.Exit(0)
// }
// cmd := args[0]
// switch cmd {
// case "init":
// err = commandInit(opts.Repo)
// if err != nil {
// errx(1, "error executing command %q: %v", cmd, err)
// }
// return
// case "version":
// fmt.Printf("%v\n", version)
// return
// }
// f, ok := commands[cmd]
// if !ok {
// errx(1, "unknown command: %q\n", cmd)
// }
// // read_password("enter password: ")
// repo, err := open(opts.Repo)
// if err != nil {
// errx(1, "unable to open repo: %v", err)
// }
// key, err := restic.SearchKey(repo, readPassword("RESTIC_PASSWORD", "Enter Password for Repository: "))
// if err != nil {
// errx(2, "unable to open repo: %v", err)
// }
// err = f(repo, key, args[1:])
// if err != nil {
// errx(1, "error executing command %q: %v", cmd, err)
// }
// restic.PoolAlloc() // restic.PoolAlloc()
} }

View File

@ -11,18 +11,16 @@ import (
var ErrWrongData = errors.New("wrong data decrypt, checksum does not match") var ErrWrongData = errors.New("wrong data decrypt, checksum does not match")
type ContentHandler struct { type ContentHandler struct {
be backend.Server s Server
key *Key
bl *BlobList bl *BlobList
} }
// NewContentHandler creates a new content handler. // NewContentHandler creates a new content handler.
func NewContentHandler(be backend.Server, key *Key) (*ContentHandler, error) { func NewContentHandler(s Server) (*ContentHandler, error) {
ch := &ContentHandler{ ch := &ContentHandler{
be: be, s: s,
key: key, bl: NewBlobList(),
bl: NewBlobList(),
} }
return ch, nil return ch, nil
@ -49,7 +47,7 @@ func (ch *ContentHandler) LoadSnapshot(id backend.ID) (*Snapshot, error) {
// into the content handler. // into the content handler.
func (ch *ContentHandler) LoadAllMaps() error { func (ch *ContentHandler) LoadAllMaps() error {
// add all maps from all snapshots that can be decrypted to the storage map // add all maps from all snapshots that can be decrypted to the storage map
err := backend.EachID(ch.be, backend.Map, func(id backend.ID) { err := backend.EachID(ch.s, backend.Map, func(id backend.ID) {
bl, err := LoadBlobList(ch, id) bl, err := LoadBlobList(ch, id)
if err != nil { if err != nil {
return return
@ -95,7 +93,7 @@ func (ch *ContentHandler) Save(t backend.Type, data []byte) (Blob, error) {
} }
// encrypt blob // encrypt blob
n, err := ch.key.Encrypt(ciphertext, data) n, err := ch.s.Encrypt(ciphertext, data)
if err != nil { if err != nil {
return Blob{}, err return Blob{}, err
} }
@ -103,7 +101,7 @@ func (ch *ContentHandler) Save(t backend.Type, data []byte) (Blob, error) {
ciphertext = ciphertext[:n] ciphertext = ciphertext[:n]
// save blob // save blob
sid, err := ch.be.Create(t, ciphertext) sid, err := ch.s.Create(t, ciphertext)
if err != nil { if err != nil {
return Blob{}, err return Blob{}, err
} }
@ -133,13 +131,13 @@ func (ch *ContentHandler) SaveJSON(t backend.Type, item interface{}) (Blob, erro
func (ch *ContentHandler) Load(t backend.Type, id backend.ID) ([]byte, error) { func (ch *ContentHandler) Load(t backend.Type, id backend.ID) ([]byte, error) {
if t == backend.Snapshot { if t == backend.Snapshot {
// load data // load data
buf, err := ch.be.Get(t, id) buf, err := ch.s.Get(t, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// decrypt // decrypt
buf, err = ch.key.Decrypt(buf) buf, err = ch.s.Decrypt(buf)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -154,7 +152,7 @@ func (ch *ContentHandler) Load(t backend.Type, id backend.ID) ([]byte, error) {
} }
// load data // load data
buf, err := ch.be.Get(t, blob.Storage) buf, err := ch.s.Get(t, blob.Storage)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -165,7 +163,7 @@ func (ch *ContentHandler) Load(t backend.Type, id backend.ID) ([]byte, error) {
} }
// decrypt // decrypt
buf, err = ch.key.Decrypt(buf) buf, err = ch.s.Decrypt(buf)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -201,13 +199,13 @@ func (ch *ContentHandler) LoadJSON(t backend.Type, id backend.ID, item interface
// decrypts it and calls json.Unmarshal on the item. // decrypts it and calls json.Unmarshal on the item.
func (ch *ContentHandler) LoadJSONRaw(t backend.Type, id backend.ID, item interface{}) error { func (ch *ContentHandler) LoadJSONRaw(t backend.Type, id backend.ID, item interface{}) error {
// load data // load data
buf, err := ch.be.Get(t, id) buf, err := ch.s.Get(t, id)
if err != nil { if err != nil {
return err return err
} }
// decrypt // decrypt
buf, err = ch.key.Decrypt(buf) buf, err = ch.s.Decrypt(buf)
if err != nil { if err != nil {
return err return err
} }

114
key.go
View File

@ -75,77 +75,14 @@ type keys struct {
// CreateKey initializes a master key in the given backend and encrypts it with // CreateKey initializes a master key in the given backend and encrypts it with
// the password. // the password.
func CreateKey(be backend.Server, password string) (*Key, error) { func CreateKey(s Server, password string) (*Key, error) {
// fill meta data about key return AddKey(s, password, nil)
k := &Key{
Created: time.Now(),
KDF: "scrypt",
N: scryptN,
R: scryptR,
P: scryptP,
}
hn, err := os.Hostname()
if err == nil {
k.Hostname = hn
}
usr, err := user.Current()
if err == nil {
k.Username = usr.Username
}
// generate random salt
k.Salt = make([]byte, scryptSaltsize)
n, err := rand.Read(k.Salt)
if n != scryptSaltsize || err != nil {
panic("unable to read enough random bytes for salt")
}
// call scrypt() to derive user key
k.user, err = k.scrypt(password)
if err != nil {
return nil, err
}
// generate new random master keys
k.master, err = k.newKeys()
if err != nil {
return nil, err
}
// encrypt master keys (as json) with user key
buf, err := json.Marshal(k.master)
if err != nil {
return nil, err
}
k.Data = GetChunkBuf("key")
n, err = k.EncryptUser(k.Data, buf)
k.Data = k.Data[:n]
// dump as json
buf, err = json.Marshal(k)
if err != nil {
return nil, err
}
// store in repository and return
id, err := be.Create(backend.Key, buf)
if err != nil {
return nil, err
}
k.id = id
FreeChunkBuf("key", k.Data)
return k, nil
} }
// OpenKey tries do decrypt the key specified by id with the given password. // OpenKey tries do decrypt the key specified by id with the given password.
func OpenKey(be backend.Server, id backend.ID, password string) (*Key, error) { func OpenKey(s Server, id backend.ID, password string) (*Key, error) {
// extract data from repo // extract data from repo
data, err := be.Get(backend.Key, id) data, err := s.Get(backend.Key, id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -187,9 +124,9 @@ func OpenKey(be backend.Server, id backend.ID, password string) (*Key, error) {
// SearchKey tries to decrypt all keys in the backend with the given password. // SearchKey tries to decrypt all keys in the backend with the given password.
// If none could be found, ErrNoKeyFound is returned. // If none could be found, ErrNoKeyFound is returned.
func SearchKey(be backend.Server, password string) (*Key, error) { func SearchKey(s Server, password string) (*Key, error) {
// list all keys // list all keys
ids, err := be.List(backend.Key) ids, err := s.List(backend.Key)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -197,7 +134,7 @@ func SearchKey(be backend.Server, password string) (*Key, error) {
// try all keys in repo // try all keys in repo
var key *Key var key *Key
for _, id := range ids { for _, id := range ids {
key, err = OpenKey(be, id, password) key, err = OpenKey(s, id, password)
if err != nil { if err != nil {
continue continue
} }
@ -209,7 +146,7 @@ func SearchKey(be backend.Server, password string) (*Key, error) {
} }
// AddKey adds a new key to an already existing repository. // AddKey adds a new key to an already existing repository.
func (oldkey *Key) AddKey(be backend.Server, password string) (backend.ID, error) { func AddKey(s Server, password string, template *Key) (*Key, error) {
// fill meta data about key // fill meta data about key
newkey := &Key{ newkey := &Key{
Created: time.Now(), Created: time.Now(),
@ -242,8 +179,16 @@ func (oldkey *Key) AddKey(be backend.Server, password string) (backend.ID, error
return nil, err return nil, err
} }
// copy master keys from oldkey if template == nil {
newkey.master = oldkey.master // generate new random master keys
newkey.master, err = newkey.newKeys()
if err != nil {
return nil, err
}
} else {
// copy master keys from old key
newkey.master = template.master
}
// encrypt master keys (as json) with user key // encrypt master keys (as json) with user key
buf, err := json.Marshal(newkey.master) buf, err := json.Marshal(newkey.master)
@ -262,7 +207,7 @@ func (oldkey *Key) AddKey(be backend.Server, password string) (backend.ID, error
} }
// store in repository and return // store in repository and return
id, err := be.Create(backend.Key, buf) id, err := s.Create(backend.Key, buf)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -270,7 +215,7 @@ func (oldkey *Key) AddKey(be backend.Server, password string) (backend.ID, error
FreeChunkBuf("key", newkey.Data) FreeChunkBuf("key", newkey.Data)
return id, nil return newkey, nil
} }
func (k *Key) scrypt(password string) (*keys, error) { func (k *Key) scrypt(password string) (*keys, error) {
@ -426,25 +371,6 @@ func (k *Key) DecryptUser(ciphertext []byte) ([]byte, error) {
return k.decrypt(k.user, ciphertext) return k.decrypt(k.user, ciphertext)
} }
// Each calls backend.Each() with the given parameters, Decrypt() on the
// ciphertext and, on successful decryption, f with the plaintext.
func (k *Key) Each(be backend.Server, t backend.Type, f func(backend.ID, []byte, error)) error {
return backend.Each(be, t, func(id backend.ID, data []byte, e error) {
if e != nil {
f(id, nil, e)
return
}
buf, err := k.Decrypt(data)
if err != nil {
f(id, nil, err)
return
}
f(id, buf, nil)
})
}
func (k *Key) String() string { func (k *Key) String() string {
if k == nil { if k == nil {
return "<Key nil>" return "<Key nil>"

View File

@ -15,42 +15,43 @@ import (
var testPassword = "foobar" var testPassword = "foobar"
var testCleanup = flag.Bool("test.cleanup", true, "clean up after running tests (remove local backend directory with all content)") var testCleanup = flag.Bool("test.cleanup", true, "clean up after running tests (remove local backend directory with all content)")
func setupBackend(t testing.TB) *backend.Local { func setupBackend(t testing.TB) restic.Server {
tempdir, err := ioutil.TempDir("", "restic-test-") tempdir, err := ioutil.TempDir("", "restic-test-")
ok(t, err) ok(t, err)
b, err := backend.CreateLocal(tempdir) b, err := backend.CreateLocal(tempdir)
ok(t, err) ok(t, err)
return b return restic.NewServer(b)
} }
func teardownBackend(t testing.TB, b *backend.Local) { func teardownBackend(t testing.TB, s restic.Server) {
if !*testCleanup { if !*testCleanup {
t.Logf("leaving local backend at %s\n", b.Location()) l := s.Backend().(*backend.Local)
t.Logf("leaving local backend at %s\n", l.Location())
return return
} }
ok(t, os.RemoveAll(b.Location())) ok(t, s.Delete())
} }
func setupKey(t testing.TB, be backend.Server, password string) *restic.Key { func setupKey(t testing.TB, s restic.Server, password string) *restic.Key {
k, err := restic.CreateKey(be, password) k, err := restic.CreateKey(s, password)
ok(t, err) ok(t, err)
return k return k
} }
func TestRepo(t *testing.T) { func TestRepo(t *testing.T) {
be := setupBackend(t) s := setupBackend(t)
defer teardownBackend(t, be) defer teardownBackend(t, s)
_ = setupKey(t, be, testPassword) _ = setupKey(t, s, testPassword)
} }
func TestEncryptDecrypt(t *testing.T) { func TestEncryptDecrypt(t *testing.T) {
be := setupBackend(t) s := setupBackend(t)
defer teardownBackend(t, be) defer teardownBackend(t, s)
k := setupKey(t, be, testPassword) k := setupKey(t, s, testPassword)
for _, size := range []int{5, 23, 1 << 20, 7<<20 + 123} { for _, size := range []int{5, 23, 1 << 20, 7<<20 + 123} {
data := make([]byte, size) data := make([]byte, size)
@ -74,9 +75,9 @@ func TestEncryptDecrypt(t *testing.T) {
} }
func TestLargeEncrypt(t *testing.T) { func TestLargeEncrypt(t *testing.T) {
be := setupBackend(t) s := setupBackend(t)
defer teardownBackend(t, be) defer teardownBackend(t, s)
k := setupKey(t, be, testPassword) k := setupKey(t, s, testPassword)
for _, size := range []int{chunker.MaxSize, chunker.MaxSize + 1} { for _, size := range []int{chunker.MaxSize, chunker.MaxSize + 1} {
data := make([]byte, size) data := make([]byte, size)
@ -120,9 +121,9 @@ func BenchmarkDecrypt(b *testing.B) {
size := 8 << 20 // 8MiB size := 8 << 20 // 8MiB
data := make([]byte, size) data := make([]byte, size)
be := setupBackend(b) s := setupBackend(b)
defer teardownBackend(b, be) defer teardownBackend(b, s)
k := setupKey(b, be, testPassword) k := setupKey(b, s, testPassword)
ciphertext := restic.GetChunkBuf("BenchmarkDecrypt") ciphertext := restic.GetChunkBuf("BenchmarkDecrypt")
n, err := k.Encrypt(ciphertext, data) n, err := k.Encrypt(ciphertext, data)

View File

@ -11,24 +11,20 @@ import (
) )
type Restorer struct { type Restorer struct {
be backend.Server s Server
key *Key ch *ContentHandler
ch *ContentHandler sn *Snapshot
sn *Snapshot
Error func(dir string, node *Node, err error) error Error func(dir string, node *Node, err error) error
Filter func(item string, node *Node) bool Filter func(item string, node *Node) bool
} }
// NewRestorer creates a restorer preloaded with the content from the snapshot snid. // NewRestorer creates a restorer preloaded with the content from the snapshot snid.
func NewRestorer(be backend.Server, key *Key, snid backend.ID) (*Restorer, error) { func NewRestorer(s Server, snid backend.ID) (*Restorer, error) {
r := &Restorer{ r := &Restorer{s: s}
be: be,
key: key,
}
var err error var err error
r.ch, err = NewContentHandler(be, key) r.ch, err = NewContentHandler(s)
if err != nil { if err != nil {
return nil, arrar.Annotate(err, "create contenthandler for restorer") return nil, arrar.Annotate(err, "create contenthandler for restorer")
} }

144
server.go Normal file
View File

@ -0,0 +1,144 @@
package restic
import (
"errors"
"github.com/restic/restic/backend"
)
type Server struct {
be backend.Backend
key *Key
}
func NewServer(be backend.Backend) Server {
return Server{be: be}
}
func NewServerWithKey(be backend.Backend, key *Key) Server {
return Server{be: be, key: key}
}
// Each lists all entries of type t in the backend and calls function f() with
// the id and data.
func (s Server) Each(t backend.Type, f func(id backend.ID, data []byte, err error)) error {
return backend.Each(s.be, t, f)
}
// Each lists all entries of type t in the backend and calls function f() with
// the id.
func (s Server) EachID(t backend.Type, f func(backend.ID)) error {
return backend.EachID(s.be, t, f)
}
// Find loads the list of all blobs of type t and searches for IDs which start
// with prefix. If none is found, nil and ErrNoIDPrefixFound is returned. If
// more than one is found, nil and ErrMultipleIDMatches is returned.
func (s Server) Find(t backend.Type, prefix string) (backend.ID, error) {
return backend.Find(s.be, t, prefix)
}
// FindSnapshot takes a string and tries to find a snapshot whose ID matches
// the string as closely as possible.
func (s Server) FindSnapshot(id string) (backend.ID, error) {
return backend.FindSnapshot(s.be, id)
}
// PrefixLength returns the number of bytes required so that all prefixes of
// all IDs of type t are unique.
func (s Server) PrefixLength(t backend.Type) (int, error) {
return backend.PrefixLength(s.be, t)
}
// Returns the backend used for this server.
func (s Server) Backend() backend.Backend {
return s.be
}
func (s *Server) SearchKey(password string) error {
key, err := SearchKey(*s, password)
if err != nil {
return err
}
s.key = key
return nil
}
func (s Server) Decrypt(ciphertext []byte) ([]byte, error) {
if s.key == nil {
return nil, errors.New("key for server not set")
}
return s.key.Decrypt(ciphertext)
}
func (s Server) Encrypt(ciphertext, plaintext []byte) (int, error) {
if s.key == nil {
return 0, errors.New("key for server not set")
}
return s.key.Encrypt(ciphertext, plaintext)
}
func (s Server) Key() *Key {
return s.key
}
// Each calls Each() with the given parameters, Decrypt() on the ciphertext
// and, on successful decryption, f with the plaintext.
func (s Server) EachDecrypted(t backend.Type, f func(backend.ID, []byte, error)) error {
if s.key == nil {
return errors.New("key for server not set")
}
return s.Each(t, func(id backend.ID, data []byte, e error) {
if e != nil {
f(id, nil, e)
return
}
buf, err := s.key.Decrypt(data)
if err != nil {
f(id, nil, err)
return
}
f(id, buf, nil)
})
}
// Proxy methods to backend
func (s Server) List(t backend.Type) (backend.IDs, error) {
return s.be.List(t)
}
func (s Server) Get(t backend.Type, id backend.ID) ([]byte, error) {
return s.be.Get(t, id)
}
func (s Server) Create(t backend.Type, data []byte) (backend.ID, error) {
return s.be.Create(t, data)
}
func (s Server) Test(t backend.Type, id backend.ID) (bool, error) {
return s.be.Test(t, id)
}
func (s Server) Remove(t backend.Type, id backend.ID) error {
return s.be.Remove(t, id)
}
func (s Server) Close() error {
return s.be.Close()
}
func (s Server) Delete() error {
if b, ok := s.be.(backend.Deleter); ok {
return b.Delete()
}
return errors.New("Delete() called for backend that does not implement this method")
}

View File

@ -8,7 +8,7 @@ import (
"github.com/restic/restic/backend" "github.com/restic/restic/backend"
) )
func testSnapshot(t *testing.T, be backend.Server) { func testSnapshot(t *testing.T, s restic.Server) {
var err error var err error
sn, err := restic.NewSnapshot("/home/foobar") sn, err := restic.NewSnapshot("/home/foobar")
ok(t, err) ok(t, err)