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

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

	cmds "github.com/jbenet/go-ipfs/commands"
13
	core "github.com/jbenet/go-ipfs/core"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
14
	internal "github.com/jbenet/go-ipfs/core/commands2/internal"
15
	importer "github.com/jbenet/go-ipfs/importer"
16
	"github.com/jbenet/go-ipfs/importer/chunk"
17
	dag "github.com/jbenet/go-ipfs/merkledag"
18
	pinning "github.com/jbenet/go-ipfs/pin"
19
	ft "github.com/jbenet/go-ipfs/unixfs"
20 21 22 23 24
)

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

25
type AddOutput struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26 27
	Objects []*Object
	Names   []string
28 29
}

30 31
var addCmd = &cmds.Command{
	Options: []cmds.Option{
32
		cmds.BoolOption("recursive", "r", "Must be specified when adding directories"),
33 34
	},
	Arguments: []cmds.Argument{
35
		cmds.StringArg("file", true, true, "The path to a file to be added to IPFS"),
36
	},
37 38
	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
39 40 41
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
42
`,
43
	Run: func(req cmds.Request) (interface{}, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44
		var added AddOutput
45 46
		n := req.Context().Node

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47 48 49 50 51
		recursive, err := req.Option("r").Bool()
		if err != nil {
			return nil, err
		}

52 53 54 55
		// THIS IS A HORRIBLE HACK -- FIXME!!!
		// see https://github.com/jbenet/go-ipfs/issues/309

		// returns the last one
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56 57 58 59
		addDagnode := func(name string, dn *dag.Node) error {
			o, err := getOutput(dn)
			if err != nil {
				return err
60
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61 62 63

			added.Objects = append(added.Objects, o)
			added.Names = append(added.Names, name)
64
			return nil
65
		}
66

67 68 69 70 71 72 73 74 75 76 77 78
		addFile := func(name string) (*dag.Node, error) {
			f, err := os.Open(name)
			if err != nil {
				return nil, err
			}
			defer f.Close()

			dns, err := add(n, []io.Reader{f})
			if err != nil {
				return nil, err
			}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79
			log.Infof("adding file: %s", name)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80
			if err := addDagnode(name, dns[len(dns)-1]); err != nil {
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
				return nil, err
			}
			return dns[len(dns)-1], nil // last dag node is the file.
		}

		var addPath func(name string) (*dag.Node, error)
		addDir := func(name string) (*dag.Node, error) {
			tree := &dag.Node{Data: ft.FolderPBData()}

			entries, err := ioutil.ReadDir(name)
			if err != nil {
				return nil, err
			}

			// construct nodes for containing files.
			for _, e := range entries {
				fp := filepath.Join(name, e.Name())
				nd, err := addPath(fp)
				if err != nil {
					return nil, err
				}

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

			log.Infof("adding dir: %s", name)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
109
			if err := addDagnode(name, tree); err != nil {
110 111 112
				return nil, err
			}
			return tree, nil
113 114
		}

115 116
		addPath = func(fpath string) (*dag.Node, error) {
			fi, err := os.Stat(fpath)
117
			if err != nil {
118
				return nil, err
119
			}
120

121
			if fi.IsDir() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
122 123 124
				if !recursive {
					return nil, errors.New("use -r to recursively add directories")
				}
125 126 127 128 129 130 131
				return addDir(fpath)
			}
			return addFile(fpath)
		}

		paths, err := internal.CastToStrings(req.Arguments())
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
132
			panic(err)
133
			return nil, err
134 135
		}

136 137 138 139 140
		for _, f := range paths {
			if _, err := addPath(f); err != nil {
				return nil, err
			}
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
141
		return added, nil
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

		// readers, err := internal.CastToReaders(req.Arguments())
		// if err != nil {
		// 	return nil, err
		// }
		//
		// dagnodes, err := add(n, readers)
		// if err != nil {
		// 	return nil, err
		// }
		//
		// // TODO: include fs paths in output (will need a way to specify paths in underlying filearg system)
		// added := make([]*Object, 0, len(req.Arguments()))
		// for _, dagnode := range dagnodes {
		// 	object, err := getOutput(dagnode)
		// 	if err != nil {
		// 		return nil, err
		// 	}
		//
		// 	added = append(added, object)
		// }
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
163 164
		//
		// return &AddOutput{added}, nil
165
	},
166 167
	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
		cmds.Text: func(res cmds.Response) ([]byte, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
168 169
			val, ok := res.Output().(*AddOutput)
			if !ok {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
170
				return nil, errors.New("cast error")
171
			}
172

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
173 174 175
			var buf bytes.Buffer
			for i, obj := range val.Objects {
				buf.Write([]byte(fmt.Sprintf("added %s %s\n", obj.Hash, val.Names[i])))
176
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
177
			return buf.Bytes(), nil
178
		},
179
	},
180
	Type: &AddOutput{},
181 182
}

183
func add(n *core.IpfsNode, readers []io.Reader) ([]*dag.Node, error) {
184 185 186 187 188
	mp, ok := n.Pinning.(pinning.ManualPinner)
	if !ok {
		return nil, errors.New("invalid pinner type! expected manual pinner")
	}

189
	dagnodes := make([]*dag.Node, 0)
190

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

193
	for _, reader := range readers {
194
		node, err := importer.BuildDagFromReader(reader, n.DAG, mp, chunk.DefaultSplitter)
195 196 197 198
		if err != nil {
			return nil, err
		}

199
		err = addNode(n, node)
200 201 202 203 204 205
		if err != nil {
			return nil, err
		}

		dagnodes = append(dagnodes, node)
	}
206

207
	return dagnodes, nil
208
}
209 210 211 212 213 214 215 216 217 218 219 220 221 222

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
}