mirror of
https://github.com/restic/restic.git
synced 2025-12-11 18:47:50 +00:00
data: split node and snapshot code from restic package
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"path"
|
||||
|
||||
"github.com/restic/restic/internal/bloblru"
|
||||
"github.com/restic/restic/internal/data"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
"github.com/restic/restic/internal/walker"
|
||||
"golang.org/x/sync/errgroup"
|
||||
@@ -29,12 +30,12 @@ func New(format string, repo restic.Loader, w io.Writer) *Dumper {
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Dumper) DumpTree(ctx context.Context, tree *restic.Tree, rootPath string) error {
|
||||
func (d *Dumper) DumpTree(ctx context.Context, tree *data.Tree, rootPath string) error {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
// ch is buffered to deal with variable download/write speeds.
|
||||
ch := make(chan *restic.Node, 10)
|
||||
ch := make(chan *data.Node, 10)
|
||||
go sendTrees(ctx, d.repo, tree, rootPath, ch)
|
||||
|
||||
switch d.format {
|
||||
@@ -47,7 +48,7 @@ func (d *Dumper) DumpTree(ctx context.Context, tree *restic.Tree, rootPath strin
|
||||
}
|
||||
}
|
||||
|
||||
func sendTrees(ctx context.Context, repo restic.BlobLoader, tree *restic.Tree, rootPath string, ch chan *restic.Node) {
|
||||
func sendTrees(ctx context.Context, repo restic.BlobLoader, tree *data.Tree, rootPath string, ch chan *data.Node) {
|
||||
defer close(ch)
|
||||
|
||||
for _, root := range tree.Nodes {
|
||||
@@ -58,7 +59,7 @@ func sendTrees(ctx context.Context, repo restic.BlobLoader, tree *restic.Tree, r
|
||||
}
|
||||
}
|
||||
|
||||
func sendNodes(ctx context.Context, repo restic.BlobLoader, root *restic.Node, ch chan *restic.Node) error {
|
||||
func sendNodes(ctx context.Context, repo restic.BlobLoader, root *data.Node, ch chan *data.Node) error {
|
||||
select {
|
||||
case ch <- root:
|
||||
case <-ctx.Done():
|
||||
@@ -66,11 +67,11 @@ func sendNodes(ctx context.Context, repo restic.BlobLoader, root *restic.Node, c
|
||||
}
|
||||
|
||||
// If this is no directory we are finished
|
||||
if root.Type != restic.NodeTypeDir {
|
||||
if root.Type != data.NodeTypeDir {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := walker.Walk(ctx, repo, *root.Subtree, walker.WalkVisitor{ProcessNode: func(_ restic.ID, nodepath string, node *restic.Node, err error) error {
|
||||
err := walker.Walk(ctx, repo, *root.Subtree, walker.WalkVisitor{ProcessNode: func(_ restic.ID, nodepath string, node *data.Node, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -80,7 +81,7 @@ func sendNodes(ctx context.Context, repo restic.BlobLoader, root *restic.Node, c
|
||||
|
||||
node.Path = path.Join(root.Path, nodepath)
|
||||
|
||||
if node.Type != restic.NodeTypeFile && node.Type != restic.NodeTypeDir && node.Type != restic.NodeTypeSymlink {
|
||||
if node.Type != data.NodeTypeFile && node.Type != data.NodeTypeDir && node.Type != data.NodeTypeSymlink {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -98,11 +99,11 @@ func sendNodes(ctx context.Context, repo restic.BlobLoader, root *restic.Node, c
|
||||
|
||||
// WriteNode writes a file node's contents directly to d's Writer,
|
||||
// without caring about d's format.
|
||||
func (d *Dumper) WriteNode(ctx context.Context, node *restic.Node) error {
|
||||
func (d *Dumper) WriteNode(ctx context.Context, node *data.Node) error {
|
||||
return d.writeNode(ctx, d.w, node)
|
||||
}
|
||||
|
||||
func (d *Dumper) writeNode(ctx context.Context, w io.Writer, node *restic.Node) error {
|
||||
func (d *Dumper) writeNode(ctx context.Context, w io.Writer, node *data.Node) error {
|
||||
wg, ctx := errgroup.WithContext(ctx)
|
||||
limit := int(d.repo.Connections())
|
||||
wg.SetLimit(1 + limit) // +1 for the writer.
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/restic/restic/internal/archiver"
|
||||
"github.com/restic/restic/internal/data"
|
||||
"github.com/restic/restic/internal/fs"
|
||||
"github.com/restic/restic/internal/repository"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
@@ -81,7 +82,7 @@ func WriteTest(t *testing.T, format string, cd CheckDump) {
|
||||
sn, _, _, err := arch.Snapshot(ctx, []string{"."}, archiver.SnapshotOptions{})
|
||||
rtest.OK(t, err)
|
||||
|
||||
tree, err := restic.LoadTree(ctx, repo, *sn.Tree)
|
||||
tree, err := data.LoadTree(ctx, repo, *sn.Tree)
|
||||
rtest.OK(t, err)
|
||||
|
||||
dst := &bytes.Buffer{}
|
||||
|
||||
@@ -7,12 +7,12 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/restic/restic/internal/data"
|
||||
"github.com/restic/restic/internal/debug"
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
)
|
||||
|
||||
func (d *Dumper) dumpTar(ctx context.Context, ch <-chan *restic.Node) (err error) {
|
||||
func (d *Dumper) dumpTar(ctx context.Context, ch <-chan *data.Node) (err error) {
|
||||
w := tar.NewWriter(d.w)
|
||||
|
||||
defer func() {
|
||||
@@ -48,7 +48,7 @@ func tarIdentifier(id uint32) int {
|
||||
return int(id)
|
||||
}
|
||||
|
||||
func (d *Dumper) dumpNodeTar(ctx context.Context, node *restic.Node, w *tar.Writer) error {
|
||||
func (d *Dumper) dumpNodeTar(ctx context.Context, node *data.Node, w *tar.Writer) error {
|
||||
relPath, err := filepath.Rel("/", node.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -79,16 +79,16 @@ func (d *Dumper) dumpNodeTar(ctx context.Context, node *restic.Node, w *tar.Writ
|
||||
header.Mode |= cISVTX
|
||||
}
|
||||
|
||||
if node.Type == restic.NodeTypeFile {
|
||||
if node.Type == data.NodeTypeFile {
|
||||
header.Typeflag = tar.TypeReg
|
||||
}
|
||||
|
||||
if node.Type == restic.NodeTypeSymlink {
|
||||
if node.Type == data.NodeTypeSymlink {
|
||||
header.Typeflag = tar.TypeSymlink
|
||||
header.Linkname = node.LinkTarget
|
||||
}
|
||||
|
||||
if node.Type == restic.NodeTypeDir {
|
||||
if node.Type == data.NodeTypeDir {
|
||||
header.Typeflag = tar.TypeDir
|
||||
header.Name += "/"
|
||||
}
|
||||
@@ -100,7 +100,7 @@ func (d *Dumper) dumpNodeTar(ctx context.Context, node *restic.Node, w *tar.Writ
|
||||
return d.writeNode(ctx, w, node)
|
||||
}
|
||||
|
||||
func parseXattrs(xattrs []restic.ExtendedAttribute) map[string]string {
|
||||
func parseXattrs(xattrs []data.ExtendedAttribute) map[string]string {
|
||||
tmpMap := make(map[string]string)
|
||||
|
||||
for _, attr := range xattrs {
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/restic/restic/internal/restic"
|
||||
"github.com/restic/restic/internal/data"
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
)
|
||||
|
||||
@@ -120,12 +120,12 @@ func checkTar(t *testing.T, testDir string, srcTar *bytes.Buffer) error {
|
||||
func TestFieldTooLong(t *testing.T) {
|
||||
const maxSpecialFileSize = 1 << 20 // Unexported limit in archive/tar.
|
||||
|
||||
node := restic.Node{
|
||||
node := data.Node{
|
||||
Name: "file_with_xattr",
|
||||
Path: "/file_with_xattr",
|
||||
Type: restic.NodeTypeFile,
|
||||
Type: data.NodeTypeFile,
|
||||
Mode: 0644,
|
||||
ExtendedAttributes: []restic.ExtendedAttribute{
|
||||
ExtendedAttributes: []data.ExtendedAttribute{
|
||||
{
|
||||
Name: "user.way_too_large",
|
||||
Value: make([]byte, 2*maxSpecialFileSize),
|
||||
|
||||
@@ -5,11 +5,11 @@ import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/restic/restic/internal/data"
|
||||
"github.com/restic/restic/internal/errors"
|
||||
"github.com/restic/restic/internal/restic"
|
||||
)
|
||||
|
||||
func (d *Dumper) dumpZip(ctx context.Context, ch <-chan *restic.Node) (err error) {
|
||||
func (d *Dumper) dumpZip(ctx context.Context, ch <-chan *data.Node) (err error) {
|
||||
w := zip.NewWriter(d.w)
|
||||
|
||||
defer func() {
|
||||
@@ -27,7 +27,7 @@ func (d *Dumper) dumpZip(ctx context.Context, ch <-chan *restic.Node) (err error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Dumper) dumpNodeZip(ctx context.Context, node *restic.Node, zw *zip.Writer) error {
|
||||
func (d *Dumper) dumpNodeZip(ctx context.Context, node *data.Node, zw *zip.Writer) error {
|
||||
relPath, err := filepath.Rel("/", node.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -39,11 +39,11 @@ func (d *Dumper) dumpNodeZip(ctx context.Context, node *restic.Node, zw *zip.Wri
|
||||
Modified: node.ModTime,
|
||||
}
|
||||
header.SetMode(node.Mode)
|
||||
if node.Type == restic.NodeTypeFile {
|
||||
if node.Type == data.NodeTypeFile {
|
||||
header.Method = zip.Deflate
|
||||
}
|
||||
|
||||
if node.Type == restic.NodeTypeDir {
|
||||
if node.Type == data.NodeTypeDir {
|
||||
header.Name += "/"
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ func (d *Dumper) dumpNodeZip(ctx context.Context, node *restic.Node, zw *zip.Wri
|
||||
return errors.Wrap(err, "ZipHeader")
|
||||
}
|
||||
|
||||
if node.Type == restic.NodeTypeSymlink {
|
||||
if node.Type == data.NodeTypeSymlink {
|
||||
if _, err = w.Write([]byte(node.LinkTarget)); err != nil {
|
||||
return errors.Wrap(err, "Write")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user