add.go 2.93 KB
Newer Older
1 2 3
package commands

import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4
	"errors"
5
	"fmt"
6
	"io"
7 8 9 10 11 12 13
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/jbenet/go-ipfs/core"
	"github.com/jbenet/go-ipfs/importer"
	dag "github.com/jbenet/go-ipfs/merkledag"
Jeromy's avatar
Jeromy committed
14
	"github.com/jbenet/go-ipfs/pin"
15
	ft "github.com/jbenet/go-ipfs/unixfs"
16 17 18 19 20
)

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

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

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

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

		// Add the file
34
		_, err := AddPath(n, path, depth, out)
35
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36
			if err == ErrDepthLimitExceeded && depth == 1 {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37
				err = errors.New("use -r to recursively add directories")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38
			}
39 40 41 42 43 44 45
			return fmt.Errorf("addFile error: %v", err)
		}

	}
	return nil
}

46
// AddPath adds a particular path to ipfs.
47
func AddPath(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
48 49 50 51 52 53 54 55 56 57
	if depth == 0 {
		return nil, ErrDepthLimitExceeded
	}

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

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

61
	return addFile(n, fpath, depth, out)
62 63
}

64
func addDir(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
65
	tree := &dag.Node{Data: ft.FolderPBData()}
66 67 68 69 70 71 72 73 74

	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())
75
		nd, err := AddPath(n, fp, depth-1, out)
76 77 78 79 80 81 82 83 84
		if err != nil {
			return nil, err
		}

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

Jeromy's avatar
Jeromy committed
85
	log.Infof("adding dir: %s", fpath)
86 87

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

90
func addFile(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
Jeromy's avatar
Jeromy committed
91 92 93 94 95
	mp, ok := n.Pinning.(pin.ManualPinner)
	if !ok {
		return nil, errors.New("invalid pinner type! expected manual pinner")
	}

96
	root, err := importer.BuildDagFromFile(fpath, n.DAG, mp)
97 98 99 100
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
101
	log.Infof("adding file: %s", fpath)
Jeromy's avatar
Jeromy committed
102 103

	for _, l := range root.Links {
Jeromy's avatar
Jeromy committed
104
		log.Infof("adding subblock: '%s' %s", l.Name, l.Hash.B58String())
Jeromy's avatar
Jeromy committed
105 106
	}

107 108 109 110 111 112 113 114
	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)

115
	return root, nil
116 117 118
}

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

126 127 128 129 130 131 132 133
	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)

Jeromy's avatar
Jeromy committed
134 135
	// ensure we keep it
	return n.Pinning.Pin(nd, true)
136
}