pinning + pathresolver: fix pinning/unpinning of sharded directories
* Change ResolveToCid to take a Resolver and a NameSystem instead of an ipfs Node. * Make the pin/unpin methods use a Unixfs path resolver. Closes: #3974 License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>
Steven Allen committed
Jun 12, 2017 at 18:02 UTC
e2cd36e88f9e9587404c874ce36e9ffe5578d103
3 files changed
+36
-13
core/commands/pin.go
+14
-3
@@ -12,6 +12,7 @@ import (
12
dag "github.com/ipfs/go-ipfs/merkledag"
13
path "github.com/ipfs/go-ipfs/path"
14
pin "github.com/ipfs/go-ipfs/pin"
15
+ uio "github.com/ipfs/go-ipfs/unixfs/io"
16
17
context "context"
18
u "gx/ipfs/QmWbjfz3u6HkAdPh34dgPchGbQjob6LXLhAeCGii2TX69n/go-ipfs-util"
@@ -377,13 +378,18 @@ new pin and removing the old one.
378
return
379
}
380
380
- fromc, err := core.ResolveToCid(req.Context(), n, from)
381
+ r := &path.Resolver{
382
+ DAG: n.DAG,
383
+ ResolveOnce: uio.ResolveUnixfsOnce,
384
+ }
385
+
386
+ fromc, err := core.ResolveToCid(req.Context(), n.Namesys, r, from)
387
if err != nil {
388
res.SetError(err, cmds.ErrNormal)
389
return
390
}
391
386
- toc, err := core.ResolveToCid(req.Context(), n, to)
392
+ toc, err := core.ResolveToCid(req.Context(), n.Namesys, r, to)
393
if err != nil {
394
res.SetError(err, cmds.ErrNormal)
395
return
@@ -486,13 +492,18 @@ func pinLsKeys(args []string, typeStr string, ctx context.Context, n *core.IpfsN
492
493
keys := make(map[string]RefKeyObject)
494
495
+ r := &path.Resolver{
496
+ DAG: n.DAG,
497
+ ResolveOnce: uio.ResolveUnixfsOnce,
498
+ }
499
+
500
for _, p := range args {
501
pth, err := path.ParsePath(p)
502
if err != nil {
503
return nil, err
504
}
505
495
- c, err := core.ResolveToCid(ctx, n, pth)
506
+ c, err := core.ResolveToCid(ctx, n.Namesys, r, pth)
507
if err != nil {
508
return nil, err
509
}
core/corerepo/pinning.go
+15
-3
@@ -19,6 +19,7 @@ import (
19
20
"github.com/ipfs/go-ipfs/core"
21
path "github.com/ipfs/go-ipfs/path"
22
+ uio "github.com/ipfs/go-ipfs/unixfs/io"
23
24
cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
25
node "gx/ipfs/Qmb3Hm9QDFmfYuET4pu7Kyg8JV78jFa1nvZx5vnCZsK4ck/go-ipld-format"
@@ -26,13 +27,19 @@ import (
27
28
func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ([]*cid.Cid, error) {
29
dagnodes := make([]node.Node, 0)
30
+
31
+ r := &path.Resolver{
32
+ DAG: n.DAG,
33
+ ResolveOnce: uio.ResolveUnixfsOnce,
34
+ }
35
+
36
for _, fpath := range paths {
37
p, err := path.ParsePath(fpath)
38
if err != nil {
39
return nil, err
40
}
41
35
- dagnode, err := core.Resolve(ctx, n.Namesys, n.Resolver, p)
42
+ dagnode, err := core.Resolve(ctx, n.Namesys, r, p)
43
if err != nil {
44
return nil, fmt.Errorf("pin: %s", err)
45
}
@@ -61,15 +68,20 @@ func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool)
68
}
69
70
func Unpin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ([]*cid.Cid, error) {
64
-
71
var unpinned []*cid.Cid
72
+
73
+ r := &path.Resolver{
74
+ DAG: n.DAG,
75
+ ResolveOnce: uio.ResolveUnixfsOnce,
76
+ }
77
+
78
for _, p := range paths {
79
p, err := path.ParsePath(p)
80
if err != nil {
81
return nil, err
82
}
83
72
- k, err := core.ResolveToCid(ctx, n, p)
84
+ k, err := core.ResolveToCid(ctx, n.Namesys, r, p)
85
if err != nil {
86
return nil, err
87
}
core/pathresolver.go
+7
-7
@@ -57,14 +57,14 @@ func Resolve(ctx context.Context, nsys namesys.NameSystem, r *path.Resolver, p p
57
return r.ResolvePath(ctx, p)
58
}
59
60
-// ResolveToKey resolves a path to a key.
60
+// ResolveToCid resolves a path to a cid.
61
//
62
-// It first checks if the path is already in the form of just a key (<key> or
63
-// /ipfs/<key>) and returns immediately if so. Otherwise, it falls back onto
62
+// It first checks if the path is already in the form of just a cid (<cid> or
63
+// /ipfs/<cid>) and returns immediately if so. Otherwise, it falls back onto
64
// Resolve to perform resolution of the dagnode being referenced.
65
-func ResolveToCid(ctx context.Context, n *IpfsNode, p path.Path) (*cid.Cid, error) {
65
+func ResolveToCid(ctx context.Context, nsys namesys.NameSystem, r *path.Resolver, p path.Path) (*cid.Cid, error) {
66
67
- // If the path is simply a key, parse and return it. Parsed paths are already
67
+ // If the path is simply a cid, parse and return it. Parsed paths are already
68
// normalized (read: prepended with /ipfs/ if needed), so segment[1] should
69
// always be the key.
70
if p.IsJustAKey() {
@@ -77,12 +77,12 @@ func ResolveToCid(ctx context.Context, n *IpfsNode, p path.Path) (*cid.Cid, erro
77
if err != nil {
78
return nil, err
79
}
80
- dagnode, err := Resolve(ctx, n.Namesys, n.Resolver, head)
80
+ dagnode, err := Resolve(ctx, nsys, r, head)
81
if err != nil {
82
return nil, err
83
}
84
85
- // Extract and return the key of the link to the target dag node.
85
+ // Extract and return the cid of the link to the target dag node.
86
link, _, err := dagnode.ResolveLink([]string{tail})
87
if err != nil {
88
return nil, err