Rename variables

This commit is contained in:
Alexander Neumann 2015-05-09 13:32:52 +02:00
parent 7ec674f3e8
commit d9b5832034
18 changed files with 144 additions and 144 deletions

View file

@ -30,7 +30,7 @@ var archiverAllowAllFiles = func(string, os.FileInfo) bool { return true }
// Archiver is used to backup a set of directories.
type Archiver struct {
s *repo.Repository
repo *repo.Repository
blobToken chan struct{}
@ -39,9 +39,9 @@ type Archiver struct {
}
// NewArchiver returns a new archiver.
func NewArchiver(s *repo.Repository) *Archiver {
func NewArchiver(repo *repo.Repository) *Archiver {
arch := &Archiver{
s: s,
repo: repo,
blobToken: make(chan struct{}, maxConcurrentBlobs),
}
@ -60,13 +60,13 @@ func (arch *Archiver) Save(t pack.BlobType, id backend.ID, length uint, rd io.Re
debug.Log("Archiver.Save", "Save(%v, %v)\n", t, id.Str())
// test if this blob is already known
if arch.s.Index().Has(id) {
if arch.repo.Index().Has(id) {
debug.Log("Archiver.Save", "(%v, %v) already saved\n", t, id.Str())
return nil
}
// otherwise save blob
err := arch.s.SaveFrom(t, id, length, rd)
err := arch.repo.SaveFrom(t, id, length, rd)
if err != nil {
debug.Log("Archiver.Save", "Save(%v, %v): error %v\n", t, id.Str(), err)
return err
@ -86,11 +86,11 @@ func (arch *Archiver) SaveTreeJSON(item interface{}) (backend.ID, error) {
// check if tree has been saved before
id := backend.Hash(data)
if arch.s.Index().Has(id) {
if arch.repo.Index().Has(id) {
return id, nil
}
return arch.s.SaveJSON(pack.Tree, item)
return arch.repo.SaveJSON(pack.Tree, item)
}
func (arch *Archiver) reloadFileIfChanged(node *Node, file *os.File) (*Node, error) {
@ -184,7 +184,7 @@ func (arch *Archiver) SaveFile(p *Progress, node *Node) error {
return err
}
chnker := chunker.New(file, arch.s.Config.ChunkerPolynomial, sha256.New())
chnker := chunker.New(file, arch.repo.Config.ChunkerPolynomial, sha256.New())
resultChannels := [](<-chan saveResult){}
for {
@ -254,7 +254,7 @@ func (arch *Archiver) fileWorker(wg *sync.WaitGroup, p *Progress, done <-chan st
// check if all content is still available in the repository
contentMissing := false
for _, blob := range oldNode.blobs {
if ok, err := arch.s.Test(backend.Data, blob.Storage.String()); !ok || err != nil {
if ok, err := arch.repo.Test(backend.Data, blob.Storage.String()); !ok || err != nil {
debug.Log("Archiver.fileWorker", " %v not using old data, %v (%v) is missing", e.Path(), blob.ID.Str(), blob.Storage.Str())
contentMissing = true
break
@ -557,14 +557,14 @@ func (arch *Archiver) Snapshot(p *Progress, paths []string, parentID backend.ID)
sn.Parent = parentID
// load parent snapshot
parent, err := LoadSnapshot(arch.s, parentID)
parent, err := LoadSnapshot(arch.repo, parentID)
if err != nil {
return nil, nil, err
}
// start walker on old tree
ch := make(chan WalkTreeJob)
go WalkTree(arch.s, parent.Tree, done, ch)
go WalkTree(arch.repo, parent.Tree, done, ch)
jobs.Old = ch
} else {
// use closed channel
@ -622,7 +622,7 @@ func (arch *Archiver) Snapshot(p *Progress, paths []string, parentID backend.ID)
sn.Tree = root.Subtree
// save snapshot
id, err := arch.s.SaveJSONUnpacked(backend.Snapshot, sn)
id, err := arch.repo.SaveJSONUnpacked(backend.Snapshot, sn)
if err != nil {
return nil, nil, err
}
@ -632,13 +632,13 @@ func (arch *Archiver) Snapshot(p *Progress, paths []string, parentID backend.ID)
debug.Log("Archiver.Snapshot", "saved snapshot %v", id.Str())
// flush server
err = arch.s.Flush()
err = arch.repo.Flush()
if err != nil {
return nil, nil, err
}
// save index
indexID, err := arch.s.SaveIndex()
indexID, err := arch.repo.SaveIndex()
if err != nil {
debug.Log("Archiver.Snapshot", "error saving index: %v", err)
return nil, nil, err

View file

@ -51,8 +51,8 @@ func BenchmarkChunkEncrypt(b *testing.B) {
data := Random(23, 10<<20) // 10MiB
rd := bytes.NewReader(data)
s := SetupBackend(b)
defer TeardownBackend(b, s)
s := SetupRepo(b)
defer TeardownRepo(b, s)
buf := make([]byte, chunker.MaxSize)
buf2 := make([]byte, chunker.MaxSize)
@ -82,8 +82,8 @@ func benchmarkChunkEncryptP(b *testing.PB, buf []byte, rd Rdr, key *crypto.Key)
}
func BenchmarkChunkEncryptParallel(b *testing.B) {
s := SetupBackend(b)
defer TeardownBackend(b, s)
s := SetupRepo(b)
defer TeardownRepo(b, s)
data := Random(23, 10<<20) // 10MiB
@ -101,10 +101,10 @@ func BenchmarkChunkEncryptParallel(b *testing.B) {
}
func archiveDirectory(b testing.TB) {
server := SetupBackend(b)
defer TeardownBackend(b, server)
repo := SetupRepo(b)
defer TeardownRepo(b, repo)
arch := restic.NewArchiver(server)
arch := restic.NewArchiver(repo)
_, id, err := arch.Snapshot(nil, []string{*benchArchiveDirectory}, nil)
OK(b, err)
@ -135,8 +135,8 @@ func archiveWithDedup(t testing.TB) {
t.Skip("benchdir not set, skipping TestArchiverDedup")
}
server := SetupBackend(t)
defer TeardownBackend(t, server)
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
var cnt struct {
before, after, after2 struct {
@ -145,24 +145,24 @@ func archiveWithDedup(t testing.TB) {
}
// archive a few files
sn := SnapshotDir(t, server, *benchArchiveDirectory, nil)
sn := SnapshotDir(t, repo, *benchArchiveDirectory, nil)
t.Logf("archived snapshot %v", sn.ID().Str())
// get archive stats
cnt.before.packs = server.Count(backend.Data)
cnt.before.dataBlobs = server.Index().Count(pack.Data)
cnt.before.treeBlobs = server.Index().Count(pack.Tree)
cnt.before.packs = repo.Count(backend.Data)
cnt.before.dataBlobs = repo.Index().Count(pack.Data)
cnt.before.treeBlobs = repo.Index().Count(pack.Tree)
t.Logf("packs %v, data blobs %v, tree blobs %v",
cnt.before.packs, cnt.before.dataBlobs, cnt.before.treeBlobs)
// archive the same files again, without parent snapshot
sn2 := SnapshotDir(t, server, *benchArchiveDirectory, nil)
sn2 := SnapshotDir(t, repo, *benchArchiveDirectory, nil)
t.Logf("archived snapshot %v", sn2.ID().Str())
// get archive stats again
cnt.after.packs = server.Count(backend.Data)
cnt.after.dataBlobs = server.Index().Count(pack.Data)
cnt.after.treeBlobs = server.Index().Count(pack.Tree)
cnt.after.packs = repo.Count(backend.Data)
cnt.after.dataBlobs = repo.Index().Count(pack.Data)
cnt.after.treeBlobs = repo.Index().Count(pack.Tree)
t.Logf("packs %v, data blobs %v, tree blobs %v",
cnt.after.packs, cnt.after.dataBlobs, cnt.after.treeBlobs)
@ -173,13 +173,13 @@ func archiveWithDedup(t testing.TB) {
}
// archive the same files again, with a parent snapshot
sn3 := SnapshotDir(t, server, *benchArchiveDirectory, sn2.ID())
sn3 := SnapshotDir(t, repo, *benchArchiveDirectory, sn2.ID())
t.Logf("archived snapshot %v, parent %v", sn3.ID().Str(), sn2.ID().Str())
// get archive stats again
cnt.after2.packs = server.Count(backend.Data)
cnt.after2.dataBlobs = server.Index().Count(pack.Data)
cnt.after2.treeBlobs = server.Index().Count(pack.Tree)
cnt.after2.packs = repo.Count(backend.Data)
cnt.after2.dataBlobs = repo.Index().Count(pack.Data)
cnt.after2.treeBlobs = repo.Index().Count(pack.Tree)
t.Logf("packs %v, data blobs %v, tree blobs %v",
cnt.after2.packs, cnt.after2.dataBlobs, cnt.after2.treeBlobs)
@ -199,8 +199,8 @@ func BenchmarkLoadTree(t *testing.B) {
t.Skip("benchdir not set, skipping TestArchiverDedup")
}
s := SetupBackend(t)
defer TeardownBackend(t, s)
s := SetupRepo(t)
defer TeardownRepo(t, s)
// archive a few files
arch := restic.NewArchiver(s)

View file

@ -18,13 +18,13 @@ type Cache struct {
base string
}
func NewCache(s *repo.Repository) (*Cache, error) {
func NewCache(repo *repo.Repository) (*Cache, error) {
cacheDir, err := getCacheDir()
if err != nil {
return nil, err
}
basedir := filepath.Join(cacheDir, s.Config.ID)
basedir := filepath.Join(cacheDir, repo.Config.ID)
debug.Log("Cache.New", "opened cache at %v", basedir)
return &Cache{base: basedir}, nil
@ -106,7 +106,7 @@ func (c *Cache) purge(t backend.Type, subtype string, id backend.ID) error {
}
// Clear removes information from the cache that isn't present in the server any more.
func (c *Cache) Clear(s *repo.Repository) error {
func (c *Cache) Clear(repo *repo.Repository) error {
list, err := c.list(backend.Snapshot)
if err != nil {
return err
@ -115,7 +115,7 @@ func (c *Cache) Clear(s *repo.Repository) error {
for _, entry := range list {
debug.Log("Cache.Clear", "found entry %v", entry)
if ok, err := s.Test(backend.Snapshot, entry.ID.String()); !ok || err != nil {
if ok, err := repo.Test(backend.Snapshot, entry.ID.String()); !ok || err != nil {
debug.Log("Cache.Clear", "snapshot %v doesn't exist any more, removing %v", entry.ID, entry)
err = c.purge(backend.Snapshot, entry.Subtype, entry.ID)

View file

@ -8,13 +8,13 @@ import (
)
func TestCache(t *testing.T) {
server := SetupBackend(t)
defer TeardownBackend(t, server)
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
_, err := restic.NewCache(server)
_, err := restic.NewCache(repo)
OK(t, err)
arch := restic.NewArchiver(server)
arch := restic.NewArchiver(repo)
// archive some files, this should automatically cache all blobs from the snapshot
_, _, err = arch.Snapshot(nil, []string{*benchArchiveDirectory}, nil)

View file

@ -59,9 +59,9 @@ func parseTime(str string) (time.Time, error) {
return time.Time{}, fmt.Errorf("unable to parse time: %q", str)
}
func (c CmdFind) findInTree(s *repo.Repository, id backend.ID, path string) ([]findResult, error) {
func (c CmdFind) findInTree(repo *repo.Repository, id backend.ID, path string) ([]findResult, error) {
debug.Log("restic.find", "checking tree %v\n", id)
tree, err := restic.LoadTree(s, id)
tree, err := restic.LoadTree(repo, id)
if err != nil {
return nil, err
}
@ -93,7 +93,7 @@ func (c CmdFind) findInTree(s *repo.Repository, id backend.ID, path string) ([]f
}
if node.Type == "dir" {
subdirResults, err := c.findInTree(s, id, filepath.Join(path, node.Name))
subdirResults, err := c.findInTree(repo, id, filepath.Join(path, node.Name))
if err != nil {
return nil, err
}
@ -105,7 +105,7 @@ func (c CmdFind) findInTree(s *repo.Repository, id backend.ID, path string) ([]f
return results, nil
}
func (c CmdFind) findInSnapshot(s *repo.Repository, name string) error {
func (c CmdFind) findInSnapshot(repo *repo.Repository, name string) error {
debug.Log("restic.find", "searching in snapshot %s\n for entries within [%s %s]", name, c.oldest, c.newest)
id, err := backend.ParseID(name)
@ -113,12 +113,12 @@ func (c CmdFind) findInSnapshot(s *repo.Repository, name string) error {
return err
}
sn, err := restic.LoadSnapshot(s, id)
sn, err := restic.LoadSnapshot(repo, id)
if err != nil {
return err
}
results, err := c.findInTree(s, sn.Tree, "")
results, err := c.findInTree(repo, sn.Tree, "")
if err != nil {
return err
}

View file

@ -34,7 +34,7 @@ func init() {
}
}
func fsckFile(opts CmdFsck, s *repo.Repository, IDs []backend.ID) (uint64, error) {
func fsckFile(opts CmdFsck, repo *repo.Repository, IDs []backend.ID) (uint64, error) {
debug.Log("restic.fsckFile", "checking file %v", IDs)
var bytes uint64
@ -42,7 +42,7 @@ func fsckFile(opts CmdFsck, s *repo.Repository, IDs []backend.ID) (uint64, error
debug.Log("restic.fsck", " checking data blob %v\n", id)
// test if blob is in the index
packID, tpe, _, length, err := s.Index().Lookup(id)
packID, tpe, _, length, err := repo.Index().Lookup(id)
if err != nil {
return 0, fmt.Errorf("storage for blob %v (%v) not found", id, tpe)
}
@ -52,13 +52,13 @@ func fsckFile(opts CmdFsck, s *repo.Repository, IDs []backend.ID) (uint64, error
if opts.CheckData {
// load content
_, err := s.LoadBlob(pack.Data, id)
_, err := repo.LoadBlob(pack.Data, id)
if err != nil {
return 0, err
}
} else {
// test if data blob is there
ok, err := s.Test(backend.Data, packID.String())
ok, err := repo.Test(backend.Data, packID.String())
if err != nil {
return 0, err
}
@ -77,10 +77,10 @@ func fsckFile(opts CmdFsck, s *repo.Repository, IDs []backend.ID) (uint64, error
return bytes, nil
}
func fsckTree(opts CmdFsck, s *repo.Repository, id backend.ID) error {
func fsckTree(opts CmdFsck, repo *repo.Repository, id backend.ID) error {
debug.Log("restic.fsckTree", "checking tree %v", id.Str())
tree, err := restic.LoadTree(s, id)
tree, err := restic.LoadTree(repo, id)
if err != nil {
return err
}
@ -122,7 +122,7 @@ func fsckTree(opts CmdFsck, s *repo.Repository, id backend.ID) error {
}
debug.Log("restic.fsckTree", "check file %v (%v)", node.Name, id.Str())
bytes, err := fsckFile(opts, s, node.Content)
bytes, err := fsckFile(opts, repo, node.Content)
if err != nil {
return err
}
@ -139,7 +139,7 @@ func fsckTree(opts CmdFsck, s *repo.Repository, id backend.ID) error {
// record id
seenIDs.Insert(node.Subtree)
err = fsckTree(opts, s, node.Subtree)
err = fsckTree(opts, repo, node.Subtree)
if err != nil {
firstErr = err
fmt.Fprintf(os.Stderr, "%v\n", err)
@ -157,15 +157,15 @@ func fsckTree(opts CmdFsck, s *repo.Repository, id backend.ID) error {
return firstErr
}
func fsckSnapshot(opts CmdFsck, s *repo.Repository, id backend.ID) error {
func fsckSnapshot(opts CmdFsck, repo *repo.Repository, id backend.ID) error {
debug.Log("restic.fsck", "checking snapshot %v\n", id)
sn, err := restic.LoadSnapshot(s, id)
sn, err := restic.LoadSnapshot(repo, id)
if err != nil {
return fmt.Errorf("loading snapshot %v failed: %v", id, err)
}
err = fsckTree(opts, s, sn.Tree)
err = fsckTree(opts, repo, sn.Tree)
if err != nil {
debug.Log("restic.fsck", " checking tree %v for snapshot %v\n", sn.Tree, id)
fmt.Fprintf(os.Stderr, "snapshot %v:\n error for tree %v:\n %v\n", id, sn.Tree, err)

View file

@ -74,12 +74,12 @@ func addKey(s *repo.Repository) error {
return nil
}
func deleteKey(s *repo.Repository, name string) error {
if name == s.KeyName() {
func deleteKey(repo *repo.Repository, name string) error {
if name == repo.KeyName() {
return errors.New("refusing to remove key currently used to access repository")
}
err := s.Remove(backend.Key, name)
err := repo.Remove(backend.Key, name)
if err != nil {
return err
}

View file

@ -38,8 +38,8 @@ func printNode(prefix string, n *restic.Node) string {
}
}
func printTree(prefix string, s *repo.Repository, id backend.ID) error {
tree, err := restic.LoadTree(s, id)
func printTree(prefix string, repo *repo.Repository, id backend.ID) error {
tree, err := restic.LoadTree(repo, id)
if err != nil {
return err
}
@ -48,7 +48,7 @@ func printTree(prefix string, s *repo.Repository, id backend.ID) error {
fmt.Println(printNode(prefix, entry))
if entry.Type == "dir" && entry.Subtree != nil {
err = printTree(filepath.Join(prefix, entry.Name), s, entry.Subtree)
err = printTree(filepath.Join(prefix, entry.Name), repo, entry.Subtree)
if err != nil {
return err
}

View file

@ -103,14 +103,14 @@ func nodeTypeFromFileInfo(fi os.FileInfo) string {
}
// CreateAt creates the node at the given path and restores all the meta data.
func (node *Node) CreateAt(path string, s *repo.Repository) error {
func (node *Node) CreateAt(path string, repo *repo.Repository) error {
switch node.Type {
case "dir":
if err := node.createDirAt(path); err != nil {
return errors.Annotate(err, "createDirAt")
}
case "file":
if err := node.createFileAt(path, s); err != nil {
if err := node.createFileAt(path, repo); err != nil {
return errors.Annotate(err, "createFileAt")
}
case "symlink":
@ -176,7 +176,7 @@ func (node Node) createDirAt(path string) error {
return nil
}
func (node Node) createFileAt(path string, s *repo.Repository) error {
func (node Node) createFileAt(path string, repo *repo.Repository) error {
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600)
defer f.Close()
@ -185,7 +185,7 @@ func (node Node) createFileAt(path string, s *repo.Repository) error {
}
for _, id := range node.Content {
buf, err := s.LoadBlob(pack.Data, id)
buf, err := repo.LoadBlob(pack.Data, id)
if err != nil {
return errors.Annotate(err, "Load")
}

View file

@ -23,21 +23,21 @@ type testJSONStruct struct {
Baz []byte
}
var serverTests = []testJSONStruct{
var repoTests = []testJSONStruct{
testJSONStruct{Foo: 23, Bar: "Teststring", Baz: []byte("xx")},
}
func TestSaveJSON(t *testing.T) {
server := SetupBackend(t)
defer TeardownBackend(t, server)
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
for _, obj := range serverTests {
for _, obj := range repoTests {
data, err := json.Marshal(obj)
OK(t, err)
data = append(data, '\n')
h := sha256.Sum256(data)
id, err := server.SaveJSON(pack.Tree, obj)
id, err := repo.SaveJSON(pack.Tree, obj)
OK(t, err)
Assert(t, bytes.Equal(h[:], id),
@ -47,10 +47,10 @@ func TestSaveJSON(t *testing.T) {
}
func BenchmarkSaveJSON(t *testing.B) {
server := SetupBackend(t)
defer TeardownBackend(t, server)
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
obj := serverTests[0]
obj := repoTests[0]
data, err := json.Marshal(obj)
OK(t, err)
@ -60,7 +60,7 @@ func BenchmarkSaveJSON(t *testing.B) {
t.ResetTimer()
for i := 0; i < t.N; i++ {
id, err := server.SaveJSON(pack.Tree, obj)
id, err := repo.SaveJSON(pack.Tree, obj)
OK(t, err)
Assert(t, bytes.Equal(h[:], id),
@ -72,8 +72,8 @@ func BenchmarkSaveJSON(t *testing.B) {
var testSizes = []int{5, 23, 2<<18 + 23, 1 << 20}
func TestSave(t *testing.T) {
server := SetupBackend(t)
defer TeardownBackend(t, server)
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
for _, size := range testSizes {
data := make([]byte, size)
@ -83,15 +83,15 @@ func TestSave(t *testing.T) {
id := backend.Hash(data)
// save
sid, err := server.Save(pack.Data, data, nil)
sid, err := repo.Save(pack.Data, data, nil)
OK(t, err)
Equals(t, id, sid)
OK(t, server.Flush())
OK(t, repo.Flush())
// read back
buf, err := server.LoadBlob(pack.Data, id)
buf, err := repo.LoadBlob(pack.Data, id)
Assert(t, len(buf) == len(data),
"number of bytes read back does not match: expected %d, got %d",
@ -104,8 +104,8 @@ func TestSave(t *testing.T) {
}
func TestSaveFrom(t *testing.T) {
server := SetupBackend(t)
defer TeardownBackend(t, server)
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
for _, size := range testSizes {
data := make([]byte, size)
@ -115,13 +115,13 @@ func TestSaveFrom(t *testing.T) {
id := backend.Hash(data)
// save
err = server.SaveFrom(pack.Data, id[:], uint(size), bytes.NewReader(data))
err = repo.SaveFrom(pack.Data, id[:], uint(size), bytes.NewReader(data))
OK(t, err)
OK(t, server.Flush())
OK(t, repo.Flush())
// read back
buf, err := server.LoadBlob(pack.Data, id[:])
buf, err := repo.LoadBlob(pack.Data, id[:])
Assert(t, len(buf) == len(data),
"number of bytes read back does not match: expected %d, got %d",
@ -134,8 +134,8 @@ func TestSaveFrom(t *testing.T) {
}
func BenchmarkSaveFrom(t *testing.B) {
server := SetupBackend(t)
defer TeardownBackend(t, server)
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
size := 4 << 20 // 4MiB
@ -150,48 +150,48 @@ func BenchmarkSaveFrom(t *testing.B) {
for i := 0; i < t.N; i++ {
// save
err = server.SaveFrom(pack.Data, id[:], uint(size), bytes.NewReader(data))
err = repo.SaveFrom(pack.Data, id[:], uint(size), bytes.NewReader(data))
OK(t, err)
}
}
func TestLoadJSONPack(t *testing.T) {
if *benchTestDir == "" {
t.Skip("benchdir not set, skipping TestServerStats")
t.Skip("benchdir not set, skipping")
}
server := SetupBackend(t)
defer TeardownBackend(t, server)
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
// archive a few files
sn := SnapshotDir(t, server, *benchTestDir, nil)
OK(t, server.Flush())
sn := SnapshotDir(t, repo, *benchTestDir, nil)
OK(t, repo.Flush())
tree := restic.NewTree()
err := server.LoadJSONPack(pack.Tree, sn.Tree, &tree)
err := repo.LoadJSONPack(pack.Tree, sn.Tree, &tree)
OK(t, err)
}
func TestLoadJSONUnpacked(t *testing.T) {
if *benchTestDir == "" {
t.Skip("benchdir not set, skipping TestServerStats")
t.Skip("benchdir not set, skipping")
}
server := SetupBackend(t)
defer TeardownBackend(t, server)
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
// archive a snapshot
sn := restic.Snapshot{}
sn.Hostname = "foobar"
sn.Username = "test!"
id, err := server.SaveJSONUnpacked(backend.Snapshot, &sn)
id, err := repo.SaveJSONUnpacked(backend.Snapshot, &sn)
OK(t, err)
var sn2 restic.Snapshot
// restore
err = server.LoadJSONUnpacked(backend.Snapshot, id, &sn2)
err = repo.LoadJSONUnpacked(backend.Snapshot, id, &sn2)
OK(t, err)
Equals(t, sn.Hostname, sn2.Hostname)

View file

@ -14,8 +14,8 @@ import (
// Restorer is used to restore a snapshot to a directory.
type Restorer struct {
s *repo.Repository
sn *Snapshot
repo *repo.Repository
sn *Snapshot
Error func(dir string, node *Node, err error) error
Filter func(item string, dstpath string, node *Node) bool
@ -24,12 +24,12 @@ type Restorer struct {
var restorerAbortOnAllErrors = func(str string, node *Node, err error) error { return err }
// NewRestorer creates a restorer preloaded with the content from the snapshot id.
func NewRestorer(s *repo.Repository, id backend.ID) (*Restorer, error) {
r := &Restorer{s: s, Error: restorerAbortOnAllErrors}
func NewRestorer(repo *repo.Repository, id backend.ID) (*Restorer, error) {
r := &Restorer{repo: repo, Error: restorerAbortOnAllErrors}
var err error
r.sn, err = LoadSnapshot(s, id)
r.sn, err = LoadSnapshot(repo, id)
if err != nil {
return nil, errors.Annotate(err, "load snapshot for restorer")
}
@ -38,7 +38,7 @@ func NewRestorer(s *repo.Repository, id backend.ID) (*Restorer, error) {
}
func (res *Restorer) restoreTo(dst string, dir string, treeID backend.ID) error {
tree, err := LoadTree(res.s, treeID)
tree, err := LoadTree(res.repo, treeID)
if err != nil {
return res.Error(dir, nil, errors.Annotate(err, "LoadTree"))
}
@ -74,7 +74,7 @@ func (res *Restorer) restoreNodeTo(node *Node, dir string, dst string) error {
return nil
}
err := node.CreateAt(dstPath, res.s)
err := node.CreateAt(dstPath, res.repo)
// Did it fail because of ENOENT?
if pe, ok := errors.Cause(err).(*os.PathError); ok {
@ -83,7 +83,7 @@ func (res *Restorer) restoreNodeTo(node *Node, dir string, dst string) error {
// Create parent directories and retry
err = os.MkdirAll(filepath.Dir(dstPath), 0700)
if err == nil || err == os.ErrExist {
err = node.CreateAt(dstPath, res.s)
err = node.CreateAt(dstPath, res.repo)
}
}
}

View file

@ -50,9 +50,9 @@ func NewSnapshot(paths []string) (*Snapshot, error) {
return sn, nil
}
func LoadSnapshot(s *repo.Repository, id backend.ID) (*Snapshot, error) {
func LoadSnapshot(repo *repo.Repository, id backend.ID) (*Snapshot, error) {
sn := &Snapshot{id: id}
err := s.LoadJSONUnpacked(backend.Snapshot, id, sn)
err := repo.LoadJSONUnpacked(backend.Snapshot, id, sn)
if err != nil {
return nil, err
}

View file

@ -8,8 +8,8 @@ import (
)
func TestNewSnapshot(t *testing.T) {
s := SetupBackend(t)
defer TeardownBackend(t, s)
s := SetupRepo(t)
defer TeardownRepo(t, s)
paths := []string{"/home/foobar"}

View file

@ -17,7 +17,7 @@ var TestPassword = flag.String("test.password", "", `use this password for repos
var TestCleanup = flag.Bool("test.cleanup", true, "clean up after running tests (remove local backend directory with all content)")
var TestTempDir = flag.String("test.tempdir", "", "use this directory for temporary storage (default: system temp dir)")
func SetupBackend(t testing.TB) *repo.Repository {
func SetupRepo(t testing.TB) *repo.Repository {
tempdir, err := ioutil.TempDir(*TestTempDir, "restic-test-")
OK(t, err)
@ -29,23 +29,23 @@ func SetupBackend(t testing.TB) *repo.Repository {
err = os.Setenv("RESTIC_CACHE", filepath.Join(tempdir, "cache"))
OK(t, err)
s := repo.New(b)
OK(t, s.Init(*TestPassword))
return s
repo := repo.New(b)
OK(t, repo.Init(*TestPassword))
return repo
}
func TeardownBackend(t testing.TB, s *repo.Repository) {
func TeardownRepo(t testing.TB, repo *repo.Repository) {
if !*TestCleanup {
l := s.Backend().(*local.Local)
l := repo.Backend().(*local.Local)
t.Logf("leaving local backend at %s\n", l.Location())
return
}
OK(t, s.Delete())
OK(t, repo.Delete())
}
func SnapshotDir(t testing.TB, server *repo.Repository, path string, parent backend.ID) *restic.Snapshot {
arch := restic.NewArchiver(server)
func SnapshotDir(t testing.TB, repo *repo.Repository, path string, parent backend.ID) *restic.Snapshot {
arch := restic.NewArchiver(repo)
sn, _, err := arch.Snapshot(nil, []string{path}, parent)
OK(t, err)
return sn

View file

@ -30,9 +30,9 @@ func (t Tree) String() string {
return fmt.Sprintf("Tree<%d nodes>", len(t.Nodes))
}
func LoadTree(s *repo.Repository, id backend.ID) (*Tree, error) {
func LoadTree(repo *repo.Repository, id backend.ID) (*Tree, error) {
tree := &Tree{}
err := s.LoadJSONPack(pack.Tree, id, tree)
err := repo.LoadJSONPack(pack.Tree, id, tree)
if err != nil {
return nil, err
}

View file

@ -93,19 +93,19 @@ func TestNodeComparison(t *testing.T) {
}
func TestLoadTree(t *testing.T) {
server := SetupBackend(t)
defer TeardownBackend(t, server)
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
// save tree
tree := restic.NewTree()
id, err := server.SaveJSON(pack.Tree, tree)
id, err := repo.SaveJSON(pack.Tree, tree)
OK(t, err)
// save packs
OK(t, server.Flush())
OK(t, repo.Flush())
// load tree again
tree2, err := restic.LoadTree(server, id)
tree2, err := restic.LoadTree(repo, id)
OK(t, err)
Assert(t, tree.Equals(tree2),

10
walk.go
View file

@ -16,10 +16,10 @@ type WalkTreeJob struct {
Tree *Tree
}
func walkTree(s *repo.Repository, path string, treeID backend.ID, done chan struct{}, jobCh chan<- WalkTreeJob) {
func walkTree(repo *repo.Repository, path string, treeID backend.ID, done chan struct{}, jobCh chan<- WalkTreeJob) {
debug.Log("walkTree", "start on %q (%v)", path, treeID.Str())
t, err := LoadTree(s, treeID)
t, err := LoadTree(repo, treeID)
if err != nil {
jobCh <- WalkTreeJob{Path: path, Error: err}
return
@ -28,7 +28,7 @@ func walkTree(s *repo.Repository, path string, treeID backend.ID, done chan stru
for _, node := range t.Nodes {
p := filepath.Join(path, node.Name)
if node.Type == "dir" {
walkTree(s, p, node.Subtree, done, jobCh)
walkTree(repo, p, node.Subtree, done, jobCh)
} else {
jobCh <- WalkTreeJob{Path: p, Node: node}
}
@ -41,9 +41,9 @@ func walkTree(s *repo.Repository, path string, treeID backend.ID, done chan stru
// WalkTree walks the tree specified by id recursively and sends a job for each
// file and directory it finds. When the channel done is closed, processing
// stops.
func WalkTree(server *repo.Repository, id backend.ID, done chan struct{}, jobCh chan<- WalkTreeJob) {
func WalkTree(repo *repo.Repository, id backend.ID, done chan struct{}, jobCh chan<- WalkTreeJob) {
debug.Log("WalkTree", "start on %v", id.Str())
walkTree(server, "", id, done, jobCh)
walkTree(repo, "", id, done, jobCh)
close(jobCh)
debug.Log("WalkTree", "done")
}

View file

@ -16,22 +16,22 @@ func TestWalkTree(t *testing.T) {
dirs, err := filepath.Glob(*testWalkDirectory)
OK(t, err)
server := SetupBackend(t)
defer TeardownBackend(t, server)
repo := SetupRepo(t)
defer TeardownRepo(t, repo)
// archive a few files
arch := restic.NewArchiver(server)
arch := restic.NewArchiver(repo)
sn, _, err := arch.Snapshot(nil, dirs, nil)
OK(t, err)
// flush server, write all packs
OK(t, server.Flush())
// flush repo, write all packs
OK(t, repo.Flush())
done := make(chan struct{})
// start tree walker
treeJobs := make(chan restic.WalkTreeJob)
go restic.WalkTree(server, sn.Tree, done, treeJobs)
go restic.WalkTree(repo, sn.Tree, done, treeJobs)
// start filesystem walker
fsJobs := make(chan pipe.Job)