mirror of
https://github.com/octoleo/restic.git
synced 2025-01-02 22:50:19 +00:00
Rename variables
This commit is contained in:
parent
7ec674f3e8
commit
d9b5832034
28
archiver.go
28
archiver.go
@ -30,7 +30,7 @@ var archiverAllowAllFiles = func(string, os.FileInfo) bool { return true }
|
|||||||
|
|
||||||
// Archiver is used to backup a set of directories.
|
// Archiver is used to backup a set of directories.
|
||||||
type Archiver struct {
|
type Archiver struct {
|
||||||
s *repo.Repository
|
repo *repo.Repository
|
||||||
|
|
||||||
blobToken chan struct{}
|
blobToken chan struct{}
|
||||||
|
|
||||||
@ -39,9 +39,9 @@ type Archiver struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewArchiver returns a new archiver.
|
// NewArchiver returns a new archiver.
|
||||||
func NewArchiver(s *repo.Repository) *Archiver {
|
func NewArchiver(repo *repo.Repository) *Archiver {
|
||||||
arch := &Archiver{
|
arch := &Archiver{
|
||||||
s: s,
|
repo: repo,
|
||||||
blobToken: make(chan struct{}, maxConcurrentBlobs),
|
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())
|
debug.Log("Archiver.Save", "Save(%v, %v)\n", t, id.Str())
|
||||||
|
|
||||||
// test if this blob is already known
|
// 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())
|
debug.Log("Archiver.Save", "(%v, %v) already saved\n", t, id.Str())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise save blob
|
// otherwise save blob
|
||||||
err := arch.s.SaveFrom(t, id, length, rd)
|
err := arch.repo.SaveFrom(t, id, length, rd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
debug.Log("Archiver.Save", "Save(%v, %v): error %v\n", t, id.Str(), err)
|
debug.Log("Archiver.Save", "Save(%v, %v): error %v\n", t, id.Str(), err)
|
||||||
return err
|
return err
|
||||||
@ -86,11 +86,11 @@ func (arch *Archiver) SaveTreeJSON(item interface{}) (backend.ID, error) {
|
|||||||
|
|
||||||
// check if tree has been saved before
|
// check if tree has been saved before
|
||||||
id := backend.Hash(data)
|
id := backend.Hash(data)
|
||||||
if arch.s.Index().Has(id) {
|
if arch.repo.Index().Has(id) {
|
||||||
return id, nil
|
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) {
|
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
|
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){}
|
resultChannels := [](<-chan saveResult){}
|
||||||
|
|
||||||
for {
|
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
|
// check if all content is still available in the repository
|
||||||
contentMissing := false
|
contentMissing := false
|
||||||
for _, blob := range oldNode.blobs {
|
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())
|
debug.Log("Archiver.fileWorker", " %v not using old data, %v (%v) is missing", e.Path(), blob.ID.Str(), blob.Storage.Str())
|
||||||
contentMissing = true
|
contentMissing = true
|
||||||
break
|
break
|
||||||
@ -557,14 +557,14 @@ func (arch *Archiver) Snapshot(p *Progress, paths []string, parentID backend.ID)
|
|||||||
sn.Parent = parentID
|
sn.Parent = parentID
|
||||||
|
|
||||||
// load parent snapshot
|
// load parent snapshot
|
||||||
parent, err := LoadSnapshot(arch.s, parentID)
|
parent, err := LoadSnapshot(arch.repo, parentID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// start walker on old tree
|
// start walker on old tree
|
||||||
ch := make(chan WalkTreeJob)
|
ch := make(chan WalkTreeJob)
|
||||||
go WalkTree(arch.s, parent.Tree, done, ch)
|
go WalkTree(arch.repo, parent.Tree, done, ch)
|
||||||
jobs.Old = ch
|
jobs.Old = ch
|
||||||
} else {
|
} else {
|
||||||
// use closed channel
|
// use closed channel
|
||||||
@ -622,7 +622,7 @@ func (arch *Archiver) Snapshot(p *Progress, paths []string, parentID backend.ID)
|
|||||||
sn.Tree = root.Subtree
|
sn.Tree = root.Subtree
|
||||||
|
|
||||||
// save snapshot
|
// save snapshot
|
||||||
id, err := arch.s.SaveJSONUnpacked(backend.Snapshot, sn)
|
id, err := arch.repo.SaveJSONUnpacked(backend.Snapshot, sn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
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())
|
debug.Log("Archiver.Snapshot", "saved snapshot %v", id.Str())
|
||||||
|
|
||||||
// flush server
|
// flush server
|
||||||
err = arch.s.Flush()
|
err = arch.repo.Flush()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// save index
|
// save index
|
||||||
indexID, err := arch.s.SaveIndex()
|
indexID, err := arch.repo.SaveIndex()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
debug.Log("Archiver.Snapshot", "error saving index: %v", err)
|
debug.Log("Archiver.Snapshot", "error saving index: %v", err)
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
@ -51,8 +51,8 @@ func BenchmarkChunkEncrypt(b *testing.B) {
|
|||||||
data := Random(23, 10<<20) // 10MiB
|
data := Random(23, 10<<20) // 10MiB
|
||||||
rd := bytes.NewReader(data)
|
rd := bytes.NewReader(data)
|
||||||
|
|
||||||
s := SetupBackend(b)
|
s := SetupRepo(b)
|
||||||
defer TeardownBackend(b, s)
|
defer TeardownRepo(b, s)
|
||||||
|
|
||||||
buf := make([]byte, chunker.MaxSize)
|
buf := make([]byte, chunker.MaxSize)
|
||||||
buf2 := 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) {
|
func BenchmarkChunkEncryptParallel(b *testing.B) {
|
||||||
s := SetupBackend(b)
|
s := SetupRepo(b)
|
||||||
defer TeardownBackend(b, s)
|
defer TeardownRepo(b, s)
|
||||||
|
|
||||||
data := Random(23, 10<<20) // 10MiB
|
data := Random(23, 10<<20) // 10MiB
|
||||||
|
|
||||||
@ -101,10 +101,10 @@ func BenchmarkChunkEncryptParallel(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func archiveDirectory(b testing.TB) {
|
func archiveDirectory(b testing.TB) {
|
||||||
server := SetupBackend(b)
|
repo := SetupRepo(b)
|
||||||
defer TeardownBackend(b, server)
|
defer TeardownRepo(b, repo)
|
||||||
|
|
||||||
arch := restic.NewArchiver(server)
|
arch := restic.NewArchiver(repo)
|
||||||
|
|
||||||
_, id, err := arch.Snapshot(nil, []string{*benchArchiveDirectory}, nil)
|
_, id, err := arch.Snapshot(nil, []string{*benchArchiveDirectory}, nil)
|
||||||
OK(b, err)
|
OK(b, err)
|
||||||
@ -135,8 +135,8 @@ func archiveWithDedup(t testing.TB) {
|
|||||||
t.Skip("benchdir not set, skipping TestArchiverDedup")
|
t.Skip("benchdir not set, skipping TestArchiverDedup")
|
||||||
}
|
}
|
||||||
|
|
||||||
server := SetupBackend(t)
|
repo := SetupRepo(t)
|
||||||
defer TeardownBackend(t, server)
|
defer TeardownRepo(t, repo)
|
||||||
|
|
||||||
var cnt struct {
|
var cnt struct {
|
||||||
before, after, after2 struct {
|
before, after, after2 struct {
|
||||||
@ -145,24 +145,24 @@ func archiveWithDedup(t testing.TB) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// archive a few files
|
// archive a few files
|
||||||
sn := SnapshotDir(t, server, *benchArchiveDirectory, nil)
|
sn := SnapshotDir(t, repo, *benchArchiveDirectory, nil)
|
||||||
t.Logf("archived snapshot %v", sn.ID().Str())
|
t.Logf("archived snapshot %v", sn.ID().Str())
|
||||||
|
|
||||||
// get archive stats
|
// get archive stats
|
||||||
cnt.before.packs = server.Count(backend.Data)
|
cnt.before.packs = repo.Count(backend.Data)
|
||||||
cnt.before.dataBlobs = server.Index().Count(pack.Data)
|
cnt.before.dataBlobs = repo.Index().Count(pack.Data)
|
||||||
cnt.before.treeBlobs = server.Index().Count(pack.Tree)
|
cnt.before.treeBlobs = repo.Index().Count(pack.Tree)
|
||||||
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
||||||
cnt.before.packs, cnt.before.dataBlobs, cnt.before.treeBlobs)
|
cnt.before.packs, cnt.before.dataBlobs, cnt.before.treeBlobs)
|
||||||
|
|
||||||
// archive the same files again, without parent snapshot
|
// 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())
|
t.Logf("archived snapshot %v", sn2.ID().Str())
|
||||||
|
|
||||||
// get archive stats again
|
// get archive stats again
|
||||||
cnt.after.packs = server.Count(backend.Data)
|
cnt.after.packs = repo.Count(backend.Data)
|
||||||
cnt.after.dataBlobs = server.Index().Count(pack.Data)
|
cnt.after.dataBlobs = repo.Index().Count(pack.Data)
|
||||||
cnt.after.treeBlobs = server.Index().Count(pack.Tree)
|
cnt.after.treeBlobs = repo.Index().Count(pack.Tree)
|
||||||
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
||||||
cnt.after.packs, cnt.after.dataBlobs, cnt.after.treeBlobs)
|
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
|
// 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())
|
t.Logf("archived snapshot %v, parent %v", sn3.ID().Str(), sn2.ID().Str())
|
||||||
|
|
||||||
// get archive stats again
|
// get archive stats again
|
||||||
cnt.after2.packs = server.Count(backend.Data)
|
cnt.after2.packs = repo.Count(backend.Data)
|
||||||
cnt.after2.dataBlobs = server.Index().Count(pack.Data)
|
cnt.after2.dataBlobs = repo.Index().Count(pack.Data)
|
||||||
cnt.after2.treeBlobs = server.Index().Count(pack.Tree)
|
cnt.after2.treeBlobs = repo.Index().Count(pack.Tree)
|
||||||
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
||||||
cnt.after2.packs, cnt.after2.dataBlobs, cnt.after2.treeBlobs)
|
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")
|
t.Skip("benchdir not set, skipping TestArchiverDedup")
|
||||||
}
|
}
|
||||||
|
|
||||||
s := SetupBackend(t)
|
s := SetupRepo(t)
|
||||||
defer TeardownBackend(t, s)
|
defer TeardownRepo(t, s)
|
||||||
|
|
||||||
// archive a few files
|
// archive a few files
|
||||||
arch := restic.NewArchiver(s)
|
arch := restic.NewArchiver(s)
|
||||||
|
8
cache.go
8
cache.go
@ -18,13 +18,13 @@ type Cache struct {
|
|||||||
base string
|
base string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCache(s *repo.Repository) (*Cache, error) {
|
func NewCache(repo *repo.Repository) (*Cache, error) {
|
||||||
cacheDir, err := getCacheDir()
|
cacheDir, err := getCacheDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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)
|
debug.Log("Cache.New", "opened cache at %v", basedir)
|
||||||
|
|
||||||
return &Cache{base: basedir}, nil
|
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.
|
// 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)
|
list, err := c.list(backend.Snapshot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -115,7 +115,7 @@ func (c *Cache) Clear(s *repo.Repository) error {
|
|||||||
for _, entry := range list {
|
for _, entry := range list {
|
||||||
debug.Log("Cache.Clear", "found entry %v", entry)
|
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)
|
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)
|
err = c.purge(backend.Snapshot, entry.Subtype, entry.ID)
|
||||||
|
@ -8,13 +8,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestCache(t *testing.T) {
|
func TestCache(t *testing.T) {
|
||||||
server := SetupBackend(t)
|
repo := SetupRepo(t)
|
||||||
defer TeardownBackend(t, server)
|
defer TeardownRepo(t, repo)
|
||||||
|
|
||||||
_, err := restic.NewCache(server)
|
_, err := restic.NewCache(repo)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
arch := restic.NewArchiver(server)
|
arch := restic.NewArchiver(repo)
|
||||||
|
|
||||||
// archive some files, this should automatically cache all blobs from the snapshot
|
// archive some files, this should automatically cache all blobs from the snapshot
|
||||||
_, _, err = arch.Snapshot(nil, []string{*benchArchiveDirectory}, nil)
|
_, _, err = arch.Snapshot(nil, []string{*benchArchiveDirectory}, nil)
|
||||||
|
@ -59,9 +59,9 @@ func parseTime(str string) (time.Time, error) {
|
|||||||
return time.Time{}, fmt.Errorf("unable to parse time: %q", str)
|
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)
|
debug.Log("restic.find", "checking tree %v\n", id)
|
||||||
tree, err := restic.LoadTree(s, id)
|
tree, err := restic.LoadTree(repo, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -93,7 +93,7 @@ func (c CmdFind) findInTree(s *repo.Repository, id backend.ID, path string) ([]f
|
|||||||
}
|
}
|
||||||
|
|
||||||
if node.Type == "dir" {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -105,7 +105,7 @@ func (c CmdFind) findInTree(s *repo.Repository, id backend.ID, path string) ([]f
|
|||||||
return results, nil
|
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)
|
debug.Log("restic.find", "searching in snapshot %s\n for entries within [%s %s]", name, c.oldest, c.newest)
|
||||||
|
|
||||||
id, err := backend.ParseID(name)
|
id, err := backend.ParseID(name)
|
||||||
@ -113,12 +113,12 @@ func (c CmdFind) findInSnapshot(s *repo.Repository, name string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
sn, err := restic.LoadSnapshot(s, id)
|
sn, err := restic.LoadSnapshot(repo, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
results, err := c.findInTree(s, sn.Tree, "")
|
results, err := c.findInTree(repo, sn.Tree, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -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)
|
debug.Log("restic.fsckFile", "checking file %v", IDs)
|
||||||
var bytes uint64
|
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)
|
debug.Log("restic.fsck", " checking data blob %v\n", id)
|
||||||
|
|
||||||
// test if blob is in the index
|
// 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 {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("storage for blob %v (%v) not found", id, tpe)
|
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 {
|
if opts.CheckData {
|
||||||
// load content
|
// load content
|
||||||
_, err := s.LoadBlob(pack.Data, id)
|
_, err := repo.LoadBlob(pack.Data, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// test if data blob is there
|
// 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 {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@ -77,10 +77,10 @@ func fsckFile(opts CmdFsck, s *repo.Repository, IDs []backend.ID) (uint64, error
|
|||||||
return bytes, nil
|
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())
|
debug.Log("restic.fsckTree", "checking tree %v", id.Str())
|
||||||
|
|
||||||
tree, err := restic.LoadTree(s, id)
|
tree, err := restic.LoadTree(repo, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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())
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -139,7 +139,7 @@ func fsckTree(opts CmdFsck, s *repo.Repository, id backend.ID) error {
|
|||||||
// record id
|
// record id
|
||||||
seenIDs.Insert(node.Subtree)
|
seenIDs.Insert(node.Subtree)
|
||||||
|
|
||||||
err = fsckTree(opts, s, node.Subtree)
|
err = fsckTree(opts, repo, node.Subtree)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
firstErr = err
|
firstErr = err
|
||||||
fmt.Fprintf(os.Stderr, "%v\n", 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
|
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)
|
debug.Log("restic.fsck", "checking snapshot %v\n", id)
|
||||||
|
|
||||||
sn, err := restic.LoadSnapshot(s, id)
|
sn, err := restic.LoadSnapshot(repo, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("loading snapshot %v failed: %v", id, err)
|
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 {
|
if err != nil {
|
||||||
debug.Log("restic.fsck", " checking tree %v for snapshot %v\n", sn.Tree, id)
|
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)
|
fmt.Fprintf(os.Stderr, "snapshot %v:\n error for tree %v:\n %v\n", id, sn.Tree, err)
|
||||||
|
@ -74,12 +74,12 @@ func addKey(s *repo.Repository) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteKey(s *repo.Repository, name string) error {
|
func deleteKey(repo *repo.Repository, name string) error {
|
||||||
if name == s.KeyName() {
|
if name == repo.KeyName() {
|
||||||
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 := s.Remove(backend.Key, name)
|
err := repo.Remove(backend.Key, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -38,8 +38,8 @@ func printNode(prefix string, n *restic.Node) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func printTree(prefix string, s *repo.Repository, id backend.ID) error {
|
func printTree(prefix string, repo *repo.Repository, id backend.ID) error {
|
||||||
tree, err := restic.LoadTree(s, id)
|
tree, err := restic.LoadTree(repo, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -48,7 +48,7 @@ func printTree(prefix string, s *repo.Repository, id backend.ID) error {
|
|||||||
fmt.Println(printNode(prefix, entry))
|
fmt.Println(printNode(prefix, entry))
|
||||||
|
|
||||||
if entry.Type == "dir" && entry.Subtree != nil {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
8
node.go
8
node.go
@ -103,14 +103,14 @@ func nodeTypeFromFileInfo(fi os.FileInfo) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateAt creates the node at the given path and restores all the meta data.
|
// 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 {
|
switch node.Type {
|
||||||
case "dir":
|
case "dir":
|
||||||
if err := node.createDirAt(path); err != nil {
|
if err := node.createDirAt(path); err != nil {
|
||||||
return errors.Annotate(err, "createDirAt")
|
return errors.Annotate(err, "createDirAt")
|
||||||
}
|
}
|
||||||
case "file":
|
case "file":
|
||||||
if err := node.createFileAt(path, s); err != nil {
|
if err := node.createFileAt(path, repo); err != nil {
|
||||||
return errors.Annotate(err, "createFileAt")
|
return errors.Annotate(err, "createFileAt")
|
||||||
}
|
}
|
||||||
case "symlink":
|
case "symlink":
|
||||||
@ -176,7 +176,7 @@ func (node Node) createDirAt(path string) error {
|
|||||||
return nil
|
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)
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600)
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
@ -185,7 +185,7 @@ func (node Node) createFileAt(path string, s *repo.Repository) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, id := range node.Content {
|
for _, id := range node.Content {
|
||||||
buf, err := s.LoadBlob(pack.Data, id)
|
buf, err := repo.LoadBlob(pack.Data, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Annotate(err, "Load")
|
return errors.Annotate(err, "Load")
|
||||||
}
|
}
|
||||||
|
@ -23,21 +23,21 @@ type testJSONStruct struct {
|
|||||||
Baz []byte
|
Baz []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
var serverTests = []testJSONStruct{
|
var repoTests = []testJSONStruct{
|
||||||
testJSONStruct{Foo: 23, Bar: "Teststring", Baz: []byte("xx")},
|
testJSONStruct{Foo: 23, Bar: "Teststring", Baz: []byte("xx")},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSaveJSON(t *testing.T) {
|
func TestSaveJSON(t *testing.T) {
|
||||||
server := SetupBackend(t)
|
repo := SetupRepo(t)
|
||||||
defer TeardownBackend(t, server)
|
defer TeardownRepo(t, repo)
|
||||||
|
|
||||||
for _, obj := range serverTests {
|
for _, obj := range repoTests {
|
||||||
data, err := json.Marshal(obj)
|
data, err := json.Marshal(obj)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
data = append(data, '\n')
|
data = append(data, '\n')
|
||||||
h := sha256.Sum256(data)
|
h := sha256.Sum256(data)
|
||||||
|
|
||||||
id, err := server.SaveJSON(pack.Tree, obj)
|
id, err := repo.SaveJSON(pack.Tree, obj)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
Assert(t, bytes.Equal(h[:], id),
|
Assert(t, bytes.Equal(h[:], id),
|
||||||
@ -47,10 +47,10 @@ func TestSaveJSON(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkSaveJSON(t *testing.B) {
|
func BenchmarkSaveJSON(t *testing.B) {
|
||||||
server := SetupBackend(t)
|
repo := SetupRepo(t)
|
||||||
defer TeardownBackend(t, server)
|
defer TeardownRepo(t, repo)
|
||||||
|
|
||||||
obj := serverTests[0]
|
obj := repoTests[0]
|
||||||
|
|
||||||
data, err := json.Marshal(obj)
|
data, err := json.Marshal(obj)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
@ -60,7 +60,7 @@ func BenchmarkSaveJSON(t *testing.B) {
|
|||||||
t.ResetTimer()
|
t.ResetTimer()
|
||||||
|
|
||||||
for i := 0; i < t.N; i++ {
|
for i := 0; i < t.N; i++ {
|
||||||
id, err := server.SaveJSON(pack.Tree, obj)
|
id, err := repo.SaveJSON(pack.Tree, obj)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
Assert(t, bytes.Equal(h[:], id),
|
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}
|
var testSizes = []int{5, 23, 2<<18 + 23, 1 << 20}
|
||||||
|
|
||||||
func TestSave(t *testing.T) {
|
func TestSave(t *testing.T) {
|
||||||
server := SetupBackend(t)
|
repo := SetupRepo(t)
|
||||||
defer TeardownBackend(t, server)
|
defer TeardownRepo(t, repo)
|
||||||
|
|
||||||
for _, size := range testSizes {
|
for _, size := range testSizes {
|
||||||
data := make([]byte, size)
|
data := make([]byte, size)
|
||||||
@ -83,15 +83,15 @@ func TestSave(t *testing.T) {
|
|||||||
id := backend.Hash(data)
|
id := backend.Hash(data)
|
||||||
|
|
||||||
// save
|
// save
|
||||||
sid, err := server.Save(pack.Data, data, nil)
|
sid, err := repo.Save(pack.Data, data, nil)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
Equals(t, id, sid)
|
Equals(t, id, sid)
|
||||||
|
|
||||||
OK(t, server.Flush())
|
OK(t, repo.Flush())
|
||||||
|
|
||||||
// read back
|
// read back
|
||||||
buf, err := server.LoadBlob(pack.Data, id)
|
buf, err := repo.LoadBlob(pack.Data, id)
|
||||||
|
|
||||||
Assert(t, len(buf) == len(data),
|
Assert(t, len(buf) == len(data),
|
||||||
"number of bytes read back does not match: expected %d, got %d",
|
"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) {
|
func TestSaveFrom(t *testing.T) {
|
||||||
server := SetupBackend(t)
|
repo := SetupRepo(t)
|
||||||
defer TeardownBackend(t, server)
|
defer TeardownRepo(t, repo)
|
||||||
|
|
||||||
for _, size := range testSizes {
|
for _, size := range testSizes {
|
||||||
data := make([]byte, size)
|
data := make([]byte, size)
|
||||||
@ -115,13 +115,13 @@ func TestSaveFrom(t *testing.T) {
|
|||||||
id := backend.Hash(data)
|
id := backend.Hash(data)
|
||||||
|
|
||||||
// save
|
// 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, err)
|
||||||
|
|
||||||
OK(t, server.Flush())
|
OK(t, repo.Flush())
|
||||||
|
|
||||||
// read back
|
// read back
|
||||||
buf, err := server.LoadBlob(pack.Data, id[:])
|
buf, err := repo.LoadBlob(pack.Data, id[:])
|
||||||
|
|
||||||
Assert(t, len(buf) == len(data),
|
Assert(t, len(buf) == len(data),
|
||||||
"number of bytes read back does not match: expected %d, got %d",
|
"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) {
|
func BenchmarkSaveFrom(t *testing.B) {
|
||||||
server := SetupBackend(t)
|
repo := SetupRepo(t)
|
||||||
defer TeardownBackend(t, server)
|
defer TeardownRepo(t, repo)
|
||||||
|
|
||||||
size := 4 << 20 // 4MiB
|
size := 4 << 20 // 4MiB
|
||||||
|
|
||||||
@ -150,48 +150,48 @@ func BenchmarkSaveFrom(t *testing.B) {
|
|||||||
|
|
||||||
for i := 0; i < t.N; i++ {
|
for i := 0; i < t.N; i++ {
|
||||||
// save
|
// 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, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadJSONPack(t *testing.T) {
|
func TestLoadJSONPack(t *testing.T) {
|
||||||
if *benchTestDir == "" {
|
if *benchTestDir == "" {
|
||||||
t.Skip("benchdir not set, skipping TestServerStats")
|
t.Skip("benchdir not set, skipping")
|
||||||
}
|
}
|
||||||
|
|
||||||
server := SetupBackend(t)
|
repo := SetupRepo(t)
|
||||||
defer TeardownBackend(t, server)
|
defer TeardownRepo(t, repo)
|
||||||
|
|
||||||
// archive a few files
|
// archive a few files
|
||||||
sn := SnapshotDir(t, server, *benchTestDir, nil)
|
sn := SnapshotDir(t, repo, *benchTestDir, nil)
|
||||||
OK(t, server.Flush())
|
OK(t, repo.Flush())
|
||||||
|
|
||||||
tree := restic.NewTree()
|
tree := restic.NewTree()
|
||||||
err := server.LoadJSONPack(pack.Tree, sn.Tree, &tree)
|
err := repo.LoadJSONPack(pack.Tree, sn.Tree, &tree)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadJSONUnpacked(t *testing.T) {
|
func TestLoadJSONUnpacked(t *testing.T) {
|
||||||
if *benchTestDir == "" {
|
if *benchTestDir == "" {
|
||||||
t.Skip("benchdir not set, skipping TestServerStats")
|
t.Skip("benchdir not set, skipping")
|
||||||
}
|
}
|
||||||
|
|
||||||
server := SetupBackend(t)
|
repo := SetupRepo(t)
|
||||||
defer TeardownBackend(t, server)
|
defer TeardownRepo(t, repo)
|
||||||
|
|
||||||
// archive a snapshot
|
// archive a snapshot
|
||||||
sn := restic.Snapshot{}
|
sn := restic.Snapshot{}
|
||||||
sn.Hostname = "foobar"
|
sn.Hostname = "foobar"
|
||||||
sn.Username = "test!"
|
sn.Username = "test!"
|
||||||
|
|
||||||
id, err := server.SaveJSONUnpacked(backend.Snapshot, &sn)
|
id, err := repo.SaveJSONUnpacked(backend.Snapshot, &sn)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
var sn2 restic.Snapshot
|
var sn2 restic.Snapshot
|
||||||
|
|
||||||
// restore
|
// restore
|
||||||
err = server.LoadJSONUnpacked(backend.Snapshot, id, &sn2)
|
err = repo.LoadJSONUnpacked(backend.Snapshot, id, &sn2)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
Equals(t, sn.Hostname, sn2.Hostname)
|
Equals(t, sn.Hostname, sn2.Hostname)
|
||||||
|
16
restorer.go
16
restorer.go
@ -14,8 +14,8 @@ import (
|
|||||||
|
|
||||||
// Restorer is used to restore a snapshot to a directory.
|
// Restorer is used to restore a snapshot to a directory.
|
||||||
type Restorer struct {
|
type Restorer struct {
|
||||||
s *repo.Repository
|
repo *repo.Repository
|
||||||
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, dstpath string, node *Node) bool
|
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 }
|
var restorerAbortOnAllErrors = func(str string, node *Node, err error) error { return err }
|
||||||
|
|
||||||
// NewRestorer creates a restorer preloaded with the content from the snapshot id.
|
// NewRestorer creates a restorer preloaded with the content from the snapshot id.
|
||||||
func NewRestorer(s *repo.Repository, id backend.ID) (*Restorer, error) {
|
func NewRestorer(repo *repo.Repository, id backend.ID) (*Restorer, error) {
|
||||||
r := &Restorer{s: s, Error: restorerAbortOnAllErrors}
|
r := &Restorer{repo: repo, Error: restorerAbortOnAllErrors}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
r.sn, err = LoadSnapshot(s, id)
|
r.sn, err = LoadSnapshot(repo, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Annotate(err, "load snapshot for restorer")
|
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 {
|
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 {
|
if err != nil {
|
||||||
return res.Error(dir, nil, errors.Annotate(err, "LoadTree"))
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err := node.CreateAt(dstPath, res.s)
|
err := node.CreateAt(dstPath, res.repo)
|
||||||
|
|
||||||
// Did it fail because of ENOENT?
|
// Did it fail because of ENOENT?
|
||||||
if pe, ok := errors.Cause(err).(*os.PathError); ok {
|
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
|
// Create parent directories and retry
|
||||||
err = os.MkdirAll(filepath.Dir(dstPath), 0700)
|
err = os.MkdirAll(filepath.Dir(dstPath), 0700)
|
||||||
if err == nil || err == os.ErrExist {
|
if err == nil || err == os.ErrExist {
|
||||||
err = node.CreateAt(dstPath, res.s)
|
err = node.CreateAt(dstPath, res.repo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,9 +50,9 @@ func NewSnapshot(paths []string) (*Snapshot, error) {
|
|||||||
return sn, nil
|
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}
|
sn := &Snapshot{id: id}
|
||||||
err := s.LoadJSONUnpacked(backend.Snapshot, id, sn)
|
err := repo.LoadJSONUnpacked(backend.Snapshot, id, sn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestNewSnapshot(t *testing.T) {
|
func TestNewSnapshot(t *testing.T) {
|
||||||
s := SetupBackend(t)
|
s := SetupRepo(t)
|
||||||
defer TeardownBackend(t, s)
|
defer TeardownRepo(t, s)
|
||||||
|
|
||||||
paths := []string{"/home/foobar"}
|
paths := []string{"/home/foobar"}
|
||||||
|
|
||||||
|
@ -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 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)")
|
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-")
|
tempdir, err := ioutil.TempDir(*TestTempDir, "restic-test-")
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
@ -29,23 +29,23 @@ func SetupBackend(t testing.TB) *repo.Repository {
|
|||||||
err = os.Setenv("RESTIC_CACHE", filepath.Join(tempdir, "cache"))
|
err = os.Setenv("RESTIC_CACHE", filepath.Join(tempdir, "cache"))
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
s := repo.New(b)
|
repo := repo.New(b)
|
||||||
OK(t, s.Init(*TestPassword))
|
OK(t, repo.Init(*TestPassword))
|
||||||
return s
|
return repo
|
||||||
}
|
}
|
||||||
|
|
||||||
func TeardownBackend(t testing.TB, s *repo.Repository) {
|
func TeardownRepo(t testing.TB, repo *repo.Repository) {
|
||||||
if !*TestCleanup {
|
if !*TestCleanup {
|
||||||
l := s.Backend().(*local.Local)
|
l := repo.Backend().(*local.Local)
|
||||||
t.Logf("leaving local backend at %s\n", l.Location())
|
t.Logf("leaving local backend at %s\n", l.Location())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
OK(t, s.Delete())
|
OK(t, repo.Delete())
|
||||||
}
|
}
|
||||||
|
|
||||||
func SnapshotDir(t testing.TB, server *repo.Repository, path string, parent backend.ID) *restic.Snapshot {
|
func SnapshotDir(t testing.TB, repo *repo.Repository, path string, parent backend.ID) *restic.Snapshot {
|
||||||
arch := restic.NewArchiver(server)
|
arch := restic.NewArchiver(repo)
|
||||||
sn, _, err := arch.Snapshot(nil, []string{path}, parent)
|
sn, _, err := arch.Snapshot(nil, []string{path}, parent)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
return sn
|
return sn
|
||||||
|
4
tree.go
4
tree.go
@ -30,9 +30,9 @@ func (t Tree) String() string {
|
|||||||
return fmt.Sprintf("Tree<%d nodes>", len(t.Nodes))
|
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{}
|
tree := &Tree{}
|
||||||
err := s.LoadJSONPack(pack.Tree, id, tree)
|
err := repo.LoadJSONPack(pack.Tree, id, tree)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
10
tree_test.go
10
tree_test.go
@ -93,19 +93,19 @@ func TestNodeComparison(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadTree(t *testing.T) {
|
func TestLoadTree(t *testing.T) {
|
||||||
server := SetupBackend(t)
|
repo := SetupRepo(t)
|
||||||
defer TeardownBackend(t, server)
|
defer TeardownRepo(t, repo)
|
||||||
|
|
||||||
// save tree
|
// save tree
|
||||||
tree := restic.NewTree()
|
tree := restic.NewTree()
|
||||||
id, err := server.SaveJSON(pack.Tree, tree)
|
id, err := repo.SaveJSON(pack.Tree, tree)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
// save packs
|
// save packs
|
||||||
OK(t, server.Flush())
|
OK(t, repo.Flush())
|
||||||
|
|
||||||
// load tree again
|
// load tree again
|
||||||
tree2, err := restic.LoadTree(server, id)
|
tree2, err := restic.LoadTree(repo, id)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
Assert(t, tree.Equals(tree2),
|
Assert(t, tree.Equals(tree2),
|
||||||
|
10
walk.go
10
walk.go
@ -16,10 +16,10 @@ type WalkTreeJob struct {
|
|||||||
Tree *Tree
|
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())
|
debug.Log("walkTree", "start on %q (%v)", path, treeID.Str())
|
||||||
|
|
||||||
t, err := LoadTree(s, treeID)
|
t, err := LoadTree(repo, treeID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jobCh <- WalkTreeJob{Path: path, Error: err}
|
jobCh <- WalkTreeJob{Path: path, Error: err}
|
||||||
return
|
return
|
||||||
@ -28,7 +28,7 @@ func walkTree(s *repo.Repository, path string, treeID backend.ID, done chan stru
|
|||||||
for _, node := range t.Nodes {
|
for _, node := range t.Nodes {
|
||||||
p := filepath.Join(path, node.Name)
|
p := filepath.Join(path, node.Name)
|
||||||
if node.Type == "dir" {
|
if node.Type == "dir" {
|
||||||
walkTree(s, p, node.Subtree, done, jobCh)
|
walkTree(repo, p, node.Subtree, done, jobCh)
|
||||||
} else {
|
} else {
|
||||||
jobCh <- WalkTreeJob{Path: p, Node: node}
|
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
|
// 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
|
// file and directory it finds. When the channel done is closed, processing
|
||||||
// stops.
|
// 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())
|
debug.Log("WalkTree", "start on %v", id.Str())
|
||||||
walkTree(server, "", id, done, jobCh)
|
walkTree(repo, "", id, done, jobCh)
|
||||||
close(jobCh)
|
close(jobCh)
|
||||||
debug.Log("WalkTree", "done")
|
debug.Log("WalkTree", "done")
|
||||||
}
|
}
|
||||||
|
12
walk_test.go
12
walk_test.go
@ -16,22 +16,22 @@ func TestWalkTree(t *testing.T) {
|
|||||||
dirs, err := filepath.Glob(*testWalkDirectory)
|
dirs, err := filepath.Glob(*testWalkDirectory)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
server := SetupBackend(t)
|
repo := SetupRepo(t)
|
||||||
defer TeardownBackend(t, server)
|
defer TeardownRepo(t, repo)
|
||||||
|
|
||||||
// archive a few files
|
// archive a few files
|
||||||
arch := restic.NewArchiver(server)
|
arch := restic.NewArchiver(repo)
|
||||||
sn, _, err := arch.Snapshot(nil, dirs, nil)
|
sn, _, err := arch.Snapshot(nil, dirs, nil)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
// flush server, write all packs
|
// flush repo, write all packs
|
||||||
OK(t, server.Flush())
|
OK(t, repo.Flush())
|
||||||
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
|
|
||||||
// start tree walker
|
// start tree walker
|
||||||
treeJobs := make(chan restic.WalkTreeJob)
|
treeJobs := make(chan restic.WalkTreeJob)
|
||||||
go restic.WalkTree(server, sn.Tree, done, treeJobs)
|
go restic.WalkTree(repo, sn.Tree, done, treeJobs)
|
||||||
|
|
||||||
// start filesystem walker
|
// start filesystem walker
|
||||||
fsJobs := make(chan pipe.Job)
|
fsJobs := make(chan pipe.Job)
|
||||||
|
Loading…
Reference in New Issue
Block a user