Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pinning + pathresolver: fix pinning/unpinning of sharded directories #3975

Merged
merged 2 commits into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions core/commands/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
dag "github.com/ipfs/go-ipfs/merkledag"
path "github.com/ipfs/go-ipfs/path"
pin "github.com/ipfs/go-ipfs/pin"
uio "github.com/ipfs/go-ipfs/unixfs/io"

context "context"
u "gx/ipfs/QmWbjfz3u6HkAdPh34dgPchGbQjob6LXLhAeCGii2TX69n/go-ipfs-util"
Expand Down Expand Up @@ -377,13 +378,18 @@ new pin and removing the old one.
return
}

fromc, err := core.ResolveToCid(req.Context(), n, from)
r := &path.Resolver{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This construction is more and more frequent around, it might be good to store it in ipfs node struct or have helper for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the end of the day, I think a better solution would be to avoid this resolver dance altogether. The root of the issue comes from the fact that the path doesn't dictate how it should be interpreted: as an IPLD path through IPLD objects (where the segments are keys) or an IPFS path through potentially sharded IPFS directories (where the segments are directory entries). To work around this:

  1. It's up to commands to chose the initial resolver.
  2. Resolvers do some additional magic under the covers to detect if a node should be treated as an IPLD or IPFS object by looking at it's Cid (they really shouldn't be doing this).

The solution I've proposed is to use separate prefixes: /ipld/... and /ipfs/... (ipfs/notes#248). I believe this is how this was intended to work (when designed) anyways.

If we did this, we could pass around the protocol along with the Cid/relative path so that core.Resolve and core.ResolveToCid could pick the right resolver for the given protocol.

DAG: n.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}

fromc, err := core.ResolveToCid(req.Context(), n.Namesys, r, from)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

toc, err := core.ResolveToCid(req.Context(), n, to)
toc, err := core.ResolveToCid(req.Context(), n.Namesys, r, to)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down Expand Up @@ -486,13 +492,18 @@ func pinLsKeys(args []string, typeStr string, ctx context.Context, n *core.IpfsN

keys := make(map[string]RefKeyObject)

r := &path.Resolver{
DAG: n.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}

for _, p := range args {
pth, err := path.ParsePath(p)
if err != nil {
return nil, err
}

c, err := core.ResolveToCid(ctx, n, pth)
c, err := core.ResolveToCid(ctx, n.Namesys, r, pth)
if err != nil {
return nil, err
}
Expand Down
18 changes: 15 additions & 3 deletions core/corerepo/pinning.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,27 @@ import (

"github.com/ipfs/go-ipfs/core"
path "github.com/ipfs/go-ipfs/path"
uio "github.com/ipfs/go-ipfs/unixfs/io"

cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
node "gx/ipfs/Qmb3Hm9QDFmfYuET4pu7Kyg8JV78jFa1nvZx5vnCZsK4ck/go-ipld-format"
)

func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ([]*cid.Cid, error) {
dagnodes := make([]node.Node, 0)

r := &path.Resolver{
DAG: n.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}

for _, fpath := range paths {
p, err := path.ParsePath(fpath)
if err != nil {
return nil, err
}

dagnode, err := core.Resolve(ctx, n.Namesys, n.Resolver, p)
dagnode, err := core.Resolve(ctx, n.Namesys, r, p)
if err != nil {
return nil, fmt.Errorf("pin: %s", err)
}
Expand Down Expand Up @@ -61,15 +68,20 @@ func Pin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool)
}

func Unpin(n *core.IpfsNode, ctx context.Context, paths []string, recursive bool) ([]*cid.Cid, error) {

var unpinned []*cid.Cid

r := &path.Resolver{
DAG: n.DAG,
ResolveOnce: uio.ResolveUnixfsOnce,
}

for _, p := range paths {
p, err := path.ParsePath(p)
if err != nil {
return nil, err
}

k, err := core.ResolveToCid(ctx, n, p)
k, err := core.ResolveToCid(ctx, n.Namesys, r, p)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions core/pathresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ func Resolve(ctx context.Context, nsys namesys.NameSystem, r *path.Resolver, p p
return r.ResolvePath(ctx, p)
}

// ResolveToKey resolves a path to a key.
// ResolveToCid resolves a path to a cid.
//
// It first checks if the path is already in the form of just a key (<key> or
// /ipfs/<key>) and returns immediately if so. Otherwise, it falls back onto
// It first checks if the path is already in the form of just a cid (<cid> or
// /ipfs/<cid>) and returns immediately if so. Otherwise, it falls back onto
// Resolve to perform resolution of the dagnode being referenced.
func ResolveToCid(ctx context.Context, n *IpfsNode, p path.Path) (*cid.Cid, error) {
func ResolveToCid(ctx context.Context, nsys namesys.NameSystem, r *path.Resolver, p path.Path) (*cid.Cid, error) {

// If the path is simply a key, parse and return it. Parsed paths are already
// If the path is simply a cid, parse and return it. Parsed paths are already
// normalized (read: prepended with /ipfs/ if needed), so segment[1] should
// always be the key.
if p.IsJustAKey() {
Expand All @@ -77,12 +77,12 @@ func ResolveToCid(ctx context.Context, n *IpfsNode, p path.Path) (*cid.Cid, erro
if err != nil {
return nil, err
}
dagnode, err := Resolve(ctx, n.Namesys, n.Resolver, head)
dagnode, err := Resolve(ctx, nsys, r, head)
if err != nil {
return nil, err
}

// Extract and return the key of the link to the target dag node.
// Extract and return the cid of the link to the target dag node.
link, _, err := dagnode.ResolveLink([]string{tail})
if err != nil {
return nil, err
Expand Down