@cryptotaxi247 / kubo / commits / 31ff95453

Move IPNS resolutions into the core library

Move IPNS resolutions into the core library via the pathresolver.go file. Fix the CLI commands to leverage this core component.

gatesvp committed Apr 20, 2015 at 00:37 UTC 31ff95453954d7ecd9bd49056158a85ec0428943
10 files changed +66 -26
core/commands/cat.go
+2 -2
@@ -18,7 +18,7 @@ var CatCmd = &cmds.Command{
18 Helptext: cmds.HelpText{
19 Tagline: "Show IPFS object data",
20 ShortDescription: `
21 -Retrieves the object named by <ipfs-path> and outputs the data
21 +Retrieves the object named by <ipfs-or-ipns-path> and outputs the data
22 it contains.
23 `,
24 },
@@ -62,7 +62,7 @@ func cat(ctx context.Context, node *core.IpfsNode, paths []string) ([]io.Reader,
62 readers := make([]io.Reader, 0, len(paths))
63 length := uint64(0)
64 for _, fpath := range paths {
65 - dagnode, err := node.Resolver.ResolvePath(path.Path(fpath))
65 + dagnode, err := core.Resolve(node, path.Path(fpath))
66 if err != nil {
67 return nil, 0, err
68 }
core/commands/get.go
+8 -2
@@ -24,7 +24,7 @@ var GetCmd = &cmds.Command{
24 Helptext: cmds.HelpText{
25 Tagline: "Download IPFS objects",
26 ShortDescription: `
27 -Retrieves the object named by <ipfs-path> and stores the data to disk.
27 +Retrieves the object named by <ipfs-or-ipns-path> and stores the data to disk.
28
29 By default, the output will be stored at ./<ipfs-path>, but an alternate path
30 can be specified with '--output=<path>' or '-o=<path>'.
@@ -166,5 +166,11 @@ func getCompressOptions(req cmds.Request) (int, error) {
166 }
167
168 func get(node *core.IpfsNode, p string, compression int) (io.Reader, error) {
169 - return utar.NewReader(path.Path(p), node.DAG, node.Resolver, compression)
169 + pathToResolve := path.Path(p)
170 + dagnode, err := core.Resolve(node, pathToResolve)
171 + if err != nil {
172 + return nil, err
173 + }
174 +
175 + return utar.NewReader(pathToResolve, node.DAG, dagnode, compression)
176 }
core/commands/ls.go
+4 -3
@@ -10,9 +10,10 @@ import (
10 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
11
12 cmds "github.com/ipfs/go-ipfs/commands"
13 + core "github.com/ipfs/go-ipfs/core"
14 merkledag "github.com/ipfs/go-ipfs/merkledag"
15 path "github.com/ipfs/go-ipfs/path"
15 - "github.com/ipfs/go-ipfs/unixfs"
16 + unixfs "github.com/ipfs/go-ipfs/unixfs"
17 unixfspb "github.com/ipfs/go-ipfs/unixfs/pb"
18 )
19
@@ -35,7 +36,7 @@ var LsCmd = &cmds.Command{
36 Helptext: cmds.HelpText{
37 Tagline: "List links from an object.",
38 ShortDescription: `
38 -Retrieves the object named by <ipfs-path> and displays the links
39 +Retrieves the object named by <ipfs-or-ipns-path> and displays the links
40 it contains, with the following format:
41
42 <link base58 hash> <link size in bytes> <link name>
@@ -65,7 +66,7 @@ it contains, with the following format:
66
67 dagnodes := make([]*merkledag.Node, 0)
68 for _, fpath := range paths {
68 - dagnode, err := node.Resolver.ResolvePath(path.Path(fpath))
69 + dagnode, err := core.Resolve(node, path.Path(fpath))
70 if err != nil {
71 res.SetError(err, cmds.ErrNormal)
72 return
core/commands/object.go
+3 -3
@@ -345,7 +345,7 @@ Data should be in the format specified by the --inputenc flag.
345
346 // objectData takes a key string and writes out the raw bytes of that node (if there is one)
347 func objectData(n *core.IpfsNode, fpath path.Path) (io.Reader, error) {
348 - dagnode, err := n.Resolver.ResolvePath(fpath)
348 + dagnode, err := core.Resolve(n, fpath)
349 if err != nil {
350 return nil, err
351 }
@@ -357,7 +357,7 @@ func objectData(n *core.IpfsNode, fpath path.Path) (io.Reader, error) {
357
358 // objectLinks takes a key string and lists the links it points to
359 func objectLinks(n *core.IpfsNode, fpath path.Path) (*Object, error) {
360 - dagnode, err := n.Resolver.ResolvePath(fpath)
360 + dagnode, err := core.Resolve(n, fpath)
361 if err != nil {
362 return nil, err
363 }
@@ -369,7 +369,7 @@ func objectLinks(n *core.IpfsNode, fpath path.Path) (*Object, error) {
369
370 // objectGet takes a key string from args and a format option and serializes the dagnode to that format
371 func objectGet(n *core.IpfsNode, fpath path.Path) (*dag.Node, error) {
372 - dagnode, err := n.Resolver.ResolvePath(fpath)
372 + dagnode, err := core.Resolve(n, fpath)
373 if err != nil {
374 return nil, err
375 }
core/commands/refs.go
+1 -1
@@ -164,7 +164,7 @@ Displays the hashes of all local objects.
164 func objectsForPaths(n *core.IpfsNode, paths []string) ([]*dag.Node, error) {
165 objects := make([]*dag.Node, len(paths))
166 for i, p := range paths {
167 - o, err := n.Resolver.ResolvePath(path.Path(p))
167 + o, err := core.Resolve(n, path.Path(p))
168 if err != nil {
169 return nil, err
170 }
core/core.go
-4
@@ -345,10 +345,6 @@ func (n *IpfsNode) OnlineMode() bool {
345 }
346 }
347
348 -func (n *IpfsNode) Resolve(fpath string) (*merkledag.Node, error) {
349 - return n.Resolver.ResolvePath(path.Path(fpath))
350 -}
351 -
348 func (n *IpfsNode) Bootstrap(cfg BootstrapConfig) error {
349
350 // TODO what should return value be when in offlineMode?
core/corerepo/pinning.go
+2 -2
@@ -16,7 +16,7 @@ func Pin(n *core.IpfsNode, paths []string, recursive bool) ([]u.Key, error) {
16
17 dagnodes := make([]*merkledag.Node, 0)
18 for _, fpath := range paths {
19 - dagnode, err := n.Resolver.ResolvePath(path.Path(fpath))
19 + dagnode, err := core.Resolve(n, path.Path(fpath))
20 if err != nil {
21 return nil, fmt.Errorf("pin: %s", err)
22 }
@@ -51,7 +51,7 @@ func Unpin(n *core.IpfsNode, paths []string, recursive bool) ([]u.Key, error) {
51
52 dagnodes := make([]*merkledag.Node, 0)
53 for _, fpath := range paths {
54 - dagnode, err := n.Resolver.ResolvePath(path.Path(fpath))
54 + dagnode, err := core.Resolve(n, path.Path(fpath))
55 if err != nil {
56 return nil, err
57 }
core/pathresolver.go new
+40
@@ -0,0 +1,40 @@
1 +package core
2 +
3 +import (
4 + merkledag "github.com/ipfs/go-ipfs/merkledag"
5 + path "github.com/ipfs/go-ipfs/path"
6 + "strings"
7 +)
8 +
9 +// Resolves the given path by parsing out /ipns/ entries and then going
10 +// through the /ipfs/ entries and returning the final merkledage node.
11 +// Effectively enables /ipns/ in CLI commands.
12 +func Resolve(n *IpfsNode, p path.Path) (*merkledag.Node, error) {
13 + strpath := string(p)
14 +
15 + // for now, we only try to resolve ipns paths if
16 + // they begin with "/ipns/". Otherwise, ambiguity
17 + // emerges when resolving just a <hash>. Is it meant
18 + // to be an ipfs or an ipns resolution?
19 +
20 + if strings.HasPrefix(strpath, "/ipns/") {
21 + // if it's an ipns path, try to resolve it.
22 + // if we can't, we can give that error back to the user.
23 + ipnsPath := p.Segments()[1]
24 + extensions := p.Segments()[2:]
25 + key, err := n.Namesys.Resolve(n.Context(), ipnsPath)
26 + if err != nil {
27 + return nil, err
28 + }
29 +
30 + pathHead := make([]string, 2)
31 + pathHead[0] = "ipfs"
32 + pathHead[1] = key.Pretty()
33 +
34 + p = path.FromSegments(append(pathHead, extensions...)...)
35 + //p = path.RebasePath(path.FromSegments(extensions...), basePath)
36 + }
37 +
38 + // ok, we have an ipfs path now (or what we'll treat as one)
39 + return n.Resolver.ResolvePath(p)
40 +}
path/path.go
+5 -2
@@ -1,10 +1,9 @@
1 package path
2
3 import (
4 + u "github.com/ipfs/go-ipfs/util"
5 "path"
6 "strings"
6 -
7 - u "github.com/ipfs/go-ipfs/util"
7 )
8
9 // TODO: debate making this a private struct wrapped in a public interface
@@ -36,3 +35,7 @@ func (p Path) Segments() []string {
35 func (p Path) String() string {
36 return string(p)
37 }
38 +
39 +func FromSegments(seg ...string) Path {
40 + return Path(strings.Join(seg, "/"))
41 +}
unixfs/tar/reader.go
+1 -7
@@ -28,12 +28,11 @@ type Reader struct {
28 err error
29 }
30
31 -func NewReader(path path.Path, dag mdag.DAGService, resolver *path.Resolver, compression int) (*Reader, error) {
31 +func NewReader(path path.Path, dag mdag.DAGService, dagnode *mdag.Node, compression int) (*Reader, error) {
32
33 reader := &Reader{
34 signalChan: make(chan struct{}),
35 dag: dag,
36 - resolver: resolver,
36 }
37
38 var err error
@@ -47,11 +46,6 @@ func NewReader(path path.Path, dag mdag.DAGService, resolver *path.Resolver, com
46 reader.writer = tar.NewWriter(&reader.buf)
47 }
48
50 - dagnode, err := resolver.ResolvePath(path)
51 - if err != nil {
52 - return nil, err
53 - }
54 -
49 // writeToBuf will write the data to the buffer, and will signal when there
50 // is new data to read
51 _, filename := gopath.Split(path.String())