resolve.go 1.29 KB
Newer Older
1 2 3 4 5 6 7
package io

import (
	"context"

	dag "github.com/ipfs/go-ipfs/merkledag"
	ft "github.com/ipfs/go-ipfs/unixfs"
8
	hamt "github.com/ipfs/go-ipfs/unixfs/hamt"
9

10
	node "gx/ipfs/QmYNyRZJBUYPNrLszFmrBrPJbsBh2vMsefz5gnDpB5M1P6/go-ipld-format"
11 12
)

Jeromy's avatar
Jeromy committed
13 14 15
// ResolveUnixfsOnce resolves a single hop of a path through a graph in a
// unixfs context. This includes handling traversing sharded directories.
func ResolveUnixfsOnce(ctx context.Context, ds dag.DAGService, nd node.Node, names []string) (*node.Link, []string, error) {
16 17 18 19 20
	switch nd := nd.(type) {
	case *dag.ProtoNode:
		upb, err := ft.FromBytes(nd.Data())
		if err != nil {
			// Not a unixfs node, use standard object traversal code
Jeromy's avatar
Jeromy committed
21 22 23 24 25 26
			lnk, err := nd.GetNodeLink(names[0])
			if err != nil {
				return nil, nil, err
			}

			return lnk, names[1:], nil
27 28 29
		}

		switch upb.GetType() {
30 31 32
		case ft.THAMTShard:
			s, err := hamt.NewHamtFromDag(ds, nd)
			if err != nil {
Jeromy's avatar
Jeromy committed
33
				return nil, nil, err
34 35
			}

Jeromy's avatar
Jeromy committed
36
			out, err := s.Find(ctx, names[0])
37
			if err != nil {
Jeromy's avatar
Jeromy committed
38
				return nil, nil, err
39 40
			}

Jeromy's avatar
Jeromy committed
41
			return out, names[1:], nil
42
		default:
Jeromy's avatar
Jeromy committed
43 44 45 46 47 48
			lnk, err := nd.GetNodeLink(names[0])
			if err != nil {
				return nil, nil, err
			}

			return lnk, names[1:], nil
49
		}
50
	default:
Jeromy's avatar
Jeromy committed
51
		lnk, rest, err := nd.ResolveLink(names)
52
		if err != nil {
Jeromy's avatar
Jeromy committed
53
			return nil, nil, err
54
		}
Jeromy's avatar
Jeromy committed
55
		return lnk, rest, nil
56 57
	}
}