add.go 3.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
package commands

import (
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/jbenet/go-ipfs/core"
	"github.com/jbenet/go-ipfs/importer"
13
	"github.com/jbenet/go-ipfs/importer/chunk"
14
	dag "github.com/jbenet/go-ipfs/merkledag"
15
	"github.com/jbenet/go-ipfs/pin"
16
	ft "github.com/jbenet/go-ipfs/unixfs"
17
	uio "github.com/jbenet/go-ipfs/unixfs/io"
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
)

// Error indicating the max depth has been exceded.
var ErrDepthLimitExceeded = fmt.Errorf("depth limit exceeded")

// Add is a command that imports files and directories -- given as arguments -- into ipfs.
func Add(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error {
	depth := 1

	// if recursive, set depth to reflect so
	if r, ok := opts["r"].(bool); r && ok {
		depth = -1
	}

	// add every path in args
	for _, path := range args {

		// Add the file
		_, err := AddPath(n, path, depth, out)
		if err != nil {
			if err == ErrDepthLimitExceeded && depth == 1 {
				err = errors.New("use -r to recursively add directories")
			}
			return fmt.Errorf("addFile error: %v", err)
		}

	}
	return nil
}

// AddPath adds a particular path to ipfs.
func AddPath(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
	if depth == 0 {
		return nil, ErrDepthLimitExceeded
	}

	fi, err := os.Stat(fpath)
	if err != nil {
		return nil, err
	}

	if fi.IsDir() {
		return addDir(n, fpath, depth, out)
	}

	return addFile(n, fpath, depth, out)
}

func addDir(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
	tree := &dag.Node{Data: ft.FolderPBData()}

	files, err := ioutil.ReadDir(fpath)
	if err != nil {
		return nil, err
	}

	// construct nodes for containing files.
	for _, f := range files {
		fp := filepath.Join(fpath, f.Name())
		nd, err := AddPath(n, fp, depth-1, out)
		if err != nil {
			return nil, err
		}

		if err = tree.AddNodeLink(f.Name(), nd); err != nil {
			return nil, err
		}
	}

	log.Info("adding dir: %s", fpath)

	return tree, addNode(n, tree, fpath, out)
}

func addFile(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
93 94 95 96 97 98 99 100
	dw := uio.NewDagWriter(n.DAG, chunk.DefaultSplitter)
	mp, ok := n.Pinning.(pin.ManualPinner)
	if !ok {
		return nil, errors.New("invalid pinner type! expected manual pinner")
	}
	dw.Pinner = mp

	root, err := importer.ImportFileDag(fpath, dw)
101 102 103 104 105 106 107 108 109 110
	if err != nil {
		return nil, err
	}

	log.Info("adding file: %s", fpath)

	for _, l := range root.Links {
		log.Info("adding subblock: %s %s", l.Name, l.Hash.B58String())
	}

111 112 113 114 115 116 117 118 119
	k, err := root.Key()
	if err != nil {
		return nil, err
	}

	// output that we've added this node
	fmt.Fprintf(out, "added %s %s\n", k, fpath)

	return root, nil
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
}

// addNode adds the node to the graph + local storage
func addNode(n *core.IpfsNode, nd *dag.Node, fpath string, out io.Writer) error {
	// add the file to the graph + local storage
	err := n.DAG.AddRecursive(nd)
	if err != nil {
		return err
	}

	k, err := nd.Key()
	if err != nil {
		return err
	}

	// output that we've added this node
	fmt.Fprintf(out, "added %s %s\n", k, fpath)

	// ensure we keep it
	return n.Pinning.Pin(nd, true)
}