add.go 3 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
	"github.com/jbenet/go-ipfs/importer/chunk"
13
	dag "github.com/jbenet/go-ipfs/merkledag"
14
	pinning "github.com/jbenet/go-ipfs/pin"
15 16 17 18 19
)

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

20
type AddOutput struct {
21
	Added []*Object
22 23
}

24 25
var addCmd = &cmds.Command{
	Options: []cmds.Option{
26
		cmds.BoolOption("recursive", "r", "Must be specified when adding directories"),
27 28
	},
	Arguments: []cmds.Argument{
29
		cmds.FileArg("file", true, true, "The path to a file to be added to IPFS"),
30
	},
31 32
	Description: "Add an object to ipfs.",
	Help: `Adds contents of <path> to ipfs. Use -r to add directories.
Matt Bell's avatar
Matt Bell committed
33 34 35
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.
Brian Tiger Chow's avatar
Brian Tiger Chow committed
36
`,
37
	Run: func(req cmds.Request) (interface{}, error) {
38 39
		n := req.Context().Node

40
		readers, err := internal.CastToReaders(req.Arguments())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
41
		if err != nil {
42
			return nil, err
43
		}
44

45 46
		dagnodes, err := add(n, readers)
		if err != nil {
47
			return nil, err
48 49
		}

50
		// TODO: include fs paths in output (will need a way to specify paths in underlying filearg system)
51
		added := make([]*Object, 0, len(req.Arguments()))
52
		for _, dagnode := range dagnodes {
53
			object, err := getOutput(dagnode)
54
			if err != nil {
55
				return nil, err
56
			}
57 58

			added = append(added, object)
59 60
		}

61
		return &AddOutput{added}, nil
62
	},
63 64
	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
		cmds.Text: func(res cmds.Response) ([]byte, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
65 66 67 68 69 70 71
			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)
72 73
				return []byte(s), nil
			}
74

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

85
func add(n *core.IpfsNode, readers []io.Reader) ([]*dag.Node, error) {
86 87 88 89 90
	mp, ok := n.Pinning.(pinning.ManualPinner)
	if !ok {
		return nil, errors.New("invalid pinner type! expected manual pinner")
	}

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

93 94
	// TODO: allow adding directories (will need support for multiple files in filearg system)

95
	for _, reader := range readers {
96
		node, err := importer.BuildDagFromReader(reader, n.DAG, mp, chunk.DefaultSplitter)
97 98 99 100
		if err != nil {
			return nil, err
		}

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

		dagnodes = append(dagnodes, node)
	}
108

109
	return dagnodes, nil
110
}
111 112 113 114 115 116 117 118 119 120 121 122 123 124

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
}