dirbuilder.go 879 Bytes
Newer Older
1 2 3
package io

import (
Jeromy's avatar
Jeromy committed
4 5 6 7
	"time"

	"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"

8 9 10
	mdag "github.com/ipfs/go-ipfs/merkledag"
	format "github.com/ipfs/go-ipfs/unixfs"
	u "github.com/ipfs/go-ipfs/util"
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
)

type directoryBuilder struct {
	dserv   mdag.DAGService
	dirnode *mdag.Node
}

func NewDirectory(dserv mdag.DAGService) *directoryBuilder {
	db := new(directoryBuilder)
	db.dserv = dserv
	db.dirnode = new(mdag.Node)
	db.dirnode.Data = format.FolderPBData()
	return db
}

func (d *directoryBuilder) AddChild(name string, k u.Key) error {
Jeromy's avatar
Jeromy committed
27 28 29 30
	ctx, cancel := context.WithTimeout(context.TODO(), time.Minute)
	defer cancel()

	cnode, err := d.dserv.Get(ctx, k)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
	if err != nil {
		return err
	}

	err = d.dirnode.AddNodeLinkClean(name, cnode)
	if err != nil {
		return err
	}

	return nil
}

func (d *directoryBuilder) GetNode() *mdag.Node {
	return d.dirnode
}