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

import (
4
	"errors"
5 6 7 8
	"fmt"
	"io"

	cmds "github.com/jbenet/go-ipfs/commands"
9
	core "github.com/jbenet/go-ipfs/core"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
10
	internal "github.com/jbenet/go-ipfs/core/commands2/internal"
11
	importer "github.com/jbenet/go-ipfs/importer"
12 13 14 15 16 17
	dag "github.com/jbenet/go-ipfs/merkledag"
)

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

18 19 20 21
type AddOutput struct {
	Added []Object
}

22 23
var addCmd = &cmds.Command{
	Options: []cmds.Option{
24
		cmds.Option{[]string{"recursive", "r"}, cmds.Bool, "Must be specified when adding directories"},
25 26
	},
	Arguments: []cmds.Argument{
27
		cmds.Argument{"file", cmds.ArgFile, false, true, "The path to a file to be added to IPFS"},
28
	},
29 30
	Description: "Add an object to ipfs.",
	Help: `Adds contents of <path> to ipfs. Use -r to add directories.
Brian Tiger Chow's avatar
Brian Tiger Chow committed
31 32 33 34
    Note that directories are added recursively, to form the ipfs
    MerkleDAG. A smarter partial add with a staging area (like git)
    remains to be implemented.
`,
35 36 37 38
	Run: func(res cmds.Response, req cmds.Request) {
		n := req.Context().Node

		// if recursive, set depth to reflect so
Brian Tiger Chow's avatar
fmt  
Brian Tiger Chow committed
39 40 41
		// opt, found := req.Option("r")
		// if r, _ := opt.(bool); found && r {
		// }
42

Brian Tiger Chow's avatar
Brian Tiger Chow committed
43 44 45 46
		readers, err := internal.ToReaders(req.Arguments())
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
47
		}
48

49 50 51 52 53 54 55 56 57 58
		dagnodes, err := add(n, readers)
		if err != nil {
			res.SetError(errors.New("cast error"), cmds.ErrNormal)
			return
		}

		added := make([]Object, len(req.Arguments()))
		for _, dagnode := range dagnodes {

			k, err := dagnode.Key()
59 60 61
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
62
			}
63
			added = append(added, Object{Hash: k.String(), Links: nil})
64 65
		}

66
		res.SetOutput(&AddOutput{added})
67
	},
68 69
	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
		cmds.Text: func(res cmds.Response) ([]byte, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
70 71 72 73 74 75 76
			val, ok := res.Output().(*AddOutput)
			if !ok {
				return nil, errors.New("cast err")
			}
			added := val.Added
			if len(added) == 1 {
				s := fmt.Sprintf("Added object: %s\n", added[0].Hash)
77 78
				return []byte(s), nil
			}
79

Brian Tiger Chow's avatar
Brian Tiger Chow committed
80 81
			s := fmt.Sprintf("Added %v objects:\n", len(added))
			for _, obj := range added {
82 83 84 85
				s += fmt.Sprintf("- %s\n", obj.Hash)
			}
			return []byte(s), nil
		},
86
	},
87
	Type: &AddOutput{},
88 89
}

90
func add(n *core.IpfsNode, readers []io.Reader) ([]*dag.Node, error) {
91

92
	dagnodes := make([]*dag.Node, 0)
93

94 95 96 97 98 99
	for _, reader := range readers {
		node, err := importer.NewDagFromReader(reader)
		if err != nil {
			return nil, err
		}

100
		err = addNode(n, node)
101 102 103 104 105 106 107
		if err != nil {
			return nil, err
		}

		dagnodes = append(dagnodes, node)
	}
	return dagnodes, nil
108
}
109 110 111 112 113 114 115 116 117 118 119 120 121 122

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

	err = n.Pinning.Pin(node, true) // ensure we keep it
	if err != nil {
		return err
	}

	return nil
}