path.go 2.43 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4
package path

import (
	"fmt"
5 6 7
	"path"
	"strings"

8
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9 10
	merkledag "github.com/jbenet/go-ipfs/merkledag"
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/op/go-logging"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13
)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14
var log = u.Logger("path", logging.ERROR)
15

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
16 17
// Resolver provides path resolution to IPFS
// It has a pointer to a DAGService, which is uses to resolve nodes.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18 19 20 21
type Resolver struct {
	DAG *merkledag.DAGService
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
22 23 24
// ResolvePath fetches the node for given path. It uses the first
// path component as a hash (key) of the first node, then resolves
// all other components walking the links, with ResolveLinks.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25
func (s *Resolver) ResolvePath(fpath string) (*merkledag.Node, error) {
26
	log.Debug("Resolve: '%s'", fpath)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27 28 29 30 31 32 33 34 35 36 37
	fpath = path.Clean(fpath)

	parts := strings.Split(fpath, "/")

	// skip over empty first elem
	if len(parts[0]) == 0 {
		parts = parts[1:]
	}

	// if nothing, bail.
	if len(parts) == 0 {
Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
38
		return nil, fmt.Errorf("ipfs path must contain at least one component")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39 40 41 42 43
	}

	// first element in the path is a b58 hash (for now)
	h, err := mh.FromB58String(parts[0])
	if err != nil {
44
		u.DOut("given path element is not a base58 string.\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45 46 47
		return nil, err
	}

48
	u.DOut("Resolve dag get.\n")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49 50 51 52 53 54 55 56
	nd, err := s.DAG.Get(u.Key(h))
	if err != nil {
		return nil, err
	}

	return s.ResolveLinks(nd, parts[1:])
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
57 58 59 60 61 62
// ResolveLinks iteratively resolves names by walking the link hierarchy.
// Every node is fetched from the DAGService, resolving the next name.
// Returns the last node found.
//
// ResolveLinks(nd, []string{"foo", "bar", "baz"})
// would retrieve "baz" in ("bar" in ("foo" in nd.Links).Links).Links
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
63 64 65 66 67 68 69 70 71
func (s *Resolver) ResolveLinks(ndd *merkledag.Node, names []string) (
	nd *merkledag.Node, err error) {

	nd = ndd // dup arg workaround

	// for each of the path components
	for _, name := range names {

		var next u.Key
72
		var nlink *merkledag.Link
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73 74 75 76
		// for each of the links in nd, the current object
		for _, link := range nd.Links {
			if link.Name == name {
				next = u.Key(link.Hash)
77
				nlink = link
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
78 79 80 81 82 83 84
				break
			}
		}

		if next == "" {
			h1, _ := nd.Multihash()
			h2 := h1.B58String()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85
			return nil, fmt.Errorf("no link named %q under %s", name, h2)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
86 87
		}

88 89 90 91 92 93 94 95 96
		if nlink.Node == nil {
			// fetch object for link and assign to nd
			nd, err = s.DAG.Get(next)
			if err != nil {
				return nd, err
			}
			nlink.Node = nd
		} else {
			nd = nlink.Node
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
97 98 99 100
		}
	}
	return
}