2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-23 19:38:57 +00:00
restic/internal/dump/common.go

118 lines
2.7 KiB
Go
Raw Normal View History

2020-11-09 22:22:27 +00:00
package dump
import (
"context"
"io"
"path"
2021-09-24 13:38:23 +00:00
"github.com/restic/restic/internal/bloblru"
2020-11-09 22:22:27 +00:00
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/walker"
)
// dumper implements saving node data.
type dumper interface {
2020-12-18 23:42:46 +00:00
io.Closer
2020-11-09 22:22:27 +00:00
dumpNode(ctx context.Context, node *restic.Node, repo restic.Repository) error
}
2021-09-24 13:38:23 +00:00
// WriteDump will write the contents of the given tree to the given destination.
// It will loop over all nodes in the tree and dump them recursively.
type WriteDump func(ctx context.Context, repo restic.Repository, tree *restic.Tree, rootPath string, dst io.Writer) error
func NewCache() *bloblru.Cache {
return bloblru.New(64 << 20)
}
func writeDump(ctx context.Context, repo restic.Repository, tree *restic.Tree, rootPath string, dmp dumper) error {
2020-11-09 22:22:27 +00:00
for _, rootNode := range tree.Nodes {
rootNode.Path = rootPath
err := dumpTree(ctx, repo, rootNode, rootPath, dmp)
if err != nil {
2021-01-30 18:35:46 +00:00
// ignore subsequent errors
_ = dmp.Close()
2020-12-18 23:42:46 +00:00
2020-11-09 22:22:27 +00:00
return err
}
}
2020-12-18 23:42:46 +00:00
return dmp.Close()
2020-11-09 22:22:27 +00:00
}
func dumpTree(ctx context.Context, repo restic.Repository, rootNode *restic.Node, rootPath string, dmp dumper) error {
rootNode.Path = path.Join(rootNode.Path, rootNode.Name)
rootPath = rootNode.Path
if err := dmp.dumpNode(ctx, rootNode, repo); err != nil {
return err
}
// If this is no directory we are finished
if !IsDir(rootNode) {
return nil
}
err := walker.Walk(ctx, repo, *rootNode.Subtree, nil, func(_ restic.ID, nodepath string, node *restic.Node, err error) (bool, error) {
if err != nil {
return false, err
}
if node == nil {
return false, nil
}
node.Path = path.Join(rootPath, nodepath)
if IsFile(node) || IsLink(node) || IsDir(node) {
err := dmp.dumpNode(ctx, node, repo)
if err != nil {
return false, err
}
}
return false, nil
})
return err
}
2021-09-24 13:38:23 +00:00
// WriteNodeData writes the contents of the node to the given Writer.
func WriteNodeData(ctx context.Context, w io.Writer, repo restic.Repository, node *restic.Node, cache *bloblru.Cache) error {
2020-11-09 22:22:27 +00:00
var (
buf []byte
err error
)
for _, id := range node.Content {
2021-09-24 13:38:23 +00:00
blob, ok := cache.Get(id)
if !ok {
blob, err = repo.LoadBlob(ctx, restic.DataBlob, id, buf)
if err != nil {
return err
}
buf = cache.Add(id, blob) // Reuse evicted buffer.
2020-11-09 22:22:27 +00:00
}
2021-09-24 13:38:23 +00:00
if _, err := w.Write(blob); err != nil {
2020-11-09 22:22:27 +00:00
return errors.Wrap(err, "Write")
}
}
return nil
}
// IsDir checks if the given node is a directory.
func IsDir(node *restic.Node) bool {
return node.Type == "dir"
}
// IsLink checks if the given node as a link.
func IsLink(node *restic.Node) bool {
return node.Type == "symlink"
}
// IsFile checks if the given node is a file.
func IsFile(node *restic.Node) bool {
return node.Type == "file"
}