resolve.go 1.02 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/Qmb3Hm9QDFmfYuET4pu7Kyg8JV78jFa1nvZx5vnCZsK4ck/go-ipld-format"
11 12 13
)

func ResolveUnixfsOnce(ctx context.Context, ds dag.DAGService, nd node.Node, name string) (*node.Link, error) {
14 15 16 17 18 19 20 21 22
	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
			return nd.GetNodeLink(name)
		}

		switch upb.GetType() {
23 24 25 26 27 28 29 30 31 32 33 34
		case ft.THAMTShard:
			s, err := hamt.NewHamtFromDag(ds, nd)
			if err != nil {
				return nil, err
			}

			// TODO: optimized routine on HAMT for returning a dag.Link to avoid extra disk hits
			out, err := s.Find(ctx, name)
			if err != nil {
				return nil, err
			}

35 36 37 38
			return node.MakeLink(out)
		default:
			return nd.GetNodeLink(name)
		}
39 40
	default:
		lnk, _, err := nd.ResolveLink([]string{name})
41 42 43 44
		if err != nil {
			return nil, err
		}
		return lnk, nil
45 46
	}
}