mirror of
https://github.com/octoleo/restic.git
synced 2024-11-30 08:44:02 +00:00
restic: simplify nodeCreateFileAt
The code to write the file content is never used.
This commit is contained in:
parent
5644079707
commit
a2e54eac64
@ -1,7 +1,6 @@
|
|||||||
package restic
|
package restic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -188,7 +187,7 @@ func (node Node) GetExtendedAttribute(a string) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NodeCreateAt creates the node at the given path but does NOT restore node meta data.
|
// NodeCreateAt creates the node at the given path but does NOT restore node meta data.
|
||||||
func NodeCreateAt(ctx context.Context, node *Node, path string, repo BlobLoader) error {
|
func NodeCreateAt(node *Node, path string) error {
|
||||||
debug.Log("create node %v at %v", node.Name, path)
|
debug.Log("create node %v at %v", node.Name, path)
|
||||||
|
|
||||||
switch node.Type {
|
switch node.Type {
|
||||||
@ -197,7 +196,7 @@ func NodeCreateAt(ctx context.Context, node *Node, path string, repo BlobLoader)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case "file":
|
case "file":
|
||||||
if err := nodeCreateFileAt(ctx, node, path, repo); err != nil {
|
if err := nodeCreateFileAt(path); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case "symlink":
|
case "symlink":
|
||||||
@ -310,38 +309,14 @@ func nodeCreateDirAt(node *Node, path string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func nodeCreateFileAt(ctx context.Context, node *Node, path string, repo BlobLoader) error {
|
func nodeCreateFileAt(path string) error {
|
||||||
f, err := fs.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
|
f, err := fs.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = nodeWriteNodeContent(ctx, node, repo, f)
|
if err := f.Close(); err != nil {
|
||||||
closeErr := f.Close()
|
return errors.WithStack(err)
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if closeErr != nil {
|
|
||||||
return errors.WithStack(closeErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func nodeWriteNodeContent(ctx context.Context, node *Node, repo BlobLoader, f *os.File) error {
|
|
||||||
var buf []byte
|
|
||||||
for _, id := range node.Content {
|
|
||||||
buf, err := repo.LoadBlob(ctx, DataBlob, id, buf)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = f.Write(buf)
|
|
||||||
if err != nil {
|
|
||||||
return errors.WithStack(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package restic
|
package restic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -245,7 +244,7 @@ func TestNodeRestoreAt(t *testing.T) {
|
|||||||
} else {
|
} else {
|
||||||
nodePath = filepath.Join(tempdir, test.Name)
|
nodePath = filepath.Join(tempdir, test.Name)
|
||||||
}
|
}
|
||||||
rtest.OK(t, NodeCreateAt(context.TODO(), &test, nodePath, nil))
|
rtest.OK(t, NodeCreateAt(&test, nodePath))
|
||||||
rtest.OK(t, NodeRestoreMetadata(&test, nodePath, func(msg string) { rtest.OK(t, fmt.Errorf("Warning triggered for path: %s: %s", nodePath, msg)) }))
|
rtest.OK(t, NodeRestoreMetadata(&test, nodePath, func(msg string) { rtest.OK(t, fmt.Errorf("Warning triggered for path: %s: %s", nodePath, msg)) }))
|
||||||
|
|
||||||
fi, err := os.Lstat(nodePath)
|
fi, err := os.Lstat(nodePath)
|
||||||
|
@ -265,14 +265,14 @@ func (res *Restorer) traverseTreeInner(ctx context.Context, target, location str
|
|||||||
return filenames, hasRestored, nil
|
return filenames, hasRestored, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (res *Restorer) restoreNodeTo(ctx context.Context, node *restic.Node, target, location string) error {
|
func (res *Restorer) restoreNodeTo(node *restic.Node, target, location string) error {
|
||||||
if !res.opts.DryRun {
|
if !res.opts.DryRun {
|
||||||
debug.Log("restoreNode %v %v %v", node.Name, target, location)
|
debug.Log("restoreNode %v %v %v", node.Name, target, location)
|
||||||
if err := fs.Remove(target); err != nil && !errors.Is(err, os.ErrNotExist) {
|
if err := fs.Remove(target); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||||
return errors.Wrap(err, "RemoveNode")
|
return errors.Wrap(err, "RemoveNode")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := restic.NodeCreateAt(ctx, node, target, res.repo)
|
err := restic.NodeCreateAt(node, target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
debug.Log("node.CreateAt(%s) error %v", target, err)
|
debug.Log("node.CreateAt(%s) error %v", target, err)
|
||||||
return err
|
return err
|
||||||
@ -435,7 +435,7 @@ func (res *Restorer) RestoreTo(ctx context.Context, dst string) (uint64, error)
|
|||||||
debug.Log("second pass, visitNode: restore node %q", location)
|
debug.Log("second pass, visitNode: restore node %q", location)
|
||||||
if node.Type != "file" {
|
if node.Type != "file" {
|
||||||
_, err := res.withOverwriteCheck(ctx, node, target, location, false, nil, func(_ bool, _ *fileState) error {
|
_, err := res.withOverwriteCheck(ctx, node, target, location, false, nil, func(_ bool, _ *fileState) error {
|
||||||
return res.restoreNodeTo(ctx, node, target, location)
|
return res.restoreNodeTo(node, target, location)
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user