dirbuilder.go 1.07 KB
Newer Older
1 2 3
package io

import (
4
	"gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
Jeromy's avatar
Jeromy committed
5

6
	key "github.com/ipfs/go-ipfs/blocks/key"
7 8
	mdag "github.com/ipfs/go-ipfs/merkledag"
	format "github.com/ipfs/go-ipfs/unixfs"
9 10 11 12 13 14 15
)

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

Henry's avatar
Henry committed
16 17 18 19 20 21
// NewEmptyDirectory returns an empty merkledag Node with a folder Data chunk
func NewEmptyDirectory() *mdag.Node {
	return &mdag.Node{Data: format.FolderPBData()}
}

// NewDirectory returns a directoryBuilder. It needs a DAGService to add the Children
22 23 24
func NewDirectory(dserv mdag.DAGService) *directoryBuilder {
	db := new(directoryBuilder)
	db.dserv = dserv
Henry's avatar
Henry committed
25
	db.dirnode = NewEmptyDirectory()
26 27 28
	return db
}

Henry's avatar
Henry committed
29
// AddChild adds a (name, key)-pair to the root node.
30
func (d *directoryBuilder) AddChild(ctx context.Context, name string, k key.Key) error {
Jeromy's avatar
Jeromy committed
31
	cnode, err := d.dserv.Get(ctx, k)
32 33 34 35
	if err != nil {
		return err
	}

36
	return d.dirnode.AddNodeLinkClean(name, cnode)
37 38
}

Henry's avatar
Henry committed
39
// GetNode returns the root of this directoryBuilder
40 41 42
func (d *directoryBuilder) GetNode() *mdag.Node {
	return d.dirnode
}