add.go 2.78 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"
14
	ft "github.com/jbenet/go-ipfs/unixfs"
15 16 17 18 19
)

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

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

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

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

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

41
		// get the key to print it
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42 43 44 45 46 47
		// k, err := nd.Key()
		// if err != nil {
		// 	return fmt.Errorf("addFile error: %v", err)
		// }
		//
		// Commenting out of here, because it's already in addNode below.
48
		// fmt.Fprintf(out, "added %s %s\n", k, path)
49 50 51 52
	}
	return nil
}

53
// AddPath adds a particular path to ipfs.
54
func AddPath(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
55 56 57 58 59 60 61 62 63 64
	if depth == 0 {
		return nil, ErrDepthLimitExceeded
	}

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

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

68
	return addFile(n, fpath, depth, out)
69 70
}

71
func addDir(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
72
	tree := &dag.Node{Data: ft.FolderPBData()}
73 74 75 76 77 78 79 80 81

	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())
82
		nd, err := AddPath(n, fp, depth-1, out)
83 84 85 86 87 88 89 90 91 92 93 94
		if err != nil {
			return nil, err
		}

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

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

95
func addFile(n *core.IpfsNode, fpath string, depth int, out io.Writer) (*dag.Node, error) {
96 97 98 99 100
	root, err := importer.NewDagFromFile(fpath)
	if err != nil {
		return nil, err
	}

Jeromy's avatar
Jeromy committed
101 102 103 104 105
	k, err := root.Key()
	if err != nil {
		return nil, err
	}

106
	fmt.Fprintf(out, "Adding file: %s = %s\n", fpath, k)
Jeromy's avatar
Jeromy committed
107
	for _, l := range root.Links {
108
		fmt.Fprintf(out, "SubBlock: %s\n", l.Hash.B58String())
Jeromy's avatar
Jeromy committed
109 110
	}

111 112 113 114 115 116 117 118 119 120 121 122
	return root, addNode(n, root, fpath)
}

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

	// ensure we keep it. atm no-op
123
	return n.PinDagNodeRecursively(nd, -1)
124
}