add.go 5.16 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
	u "github.com/jbenet/go-ipfs/util"
21 22 23 24 25
)

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

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

31
var addCmd = &cmds.Command{
32 33 34 35 36 37 38 39 40 41
	Helptext: cmds.HelpText{
		Tagline: "Add an object to ipfs.",
		ShortDescription: `
Adds contents of <path> to ipfs. Use -r to add directories.
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.
`,
	},

42
	Options: []cmds.Option{
43
		cmds.BoolOption("recursive", "r", "Must be specified when adding directories"),
44 45
	},
	Arguments: []cmds.Argument{
46
		cmds.StringArg("file", true, true, "The path to a file to be added to IPFS"),
47
	},
48
	Run: func(req cmds.Request) (interface{}, error) {
49
		added := &AddOutput{}
50 51 52 53
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}
54

55
		recursive, _, err := req.Option("r").Bool()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
56 57 58 59
		if err != nil {
			return nil, err
		}

60 61 62 63
		// 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
64 65 66 67
		addDagnode := func(name string, dn *dag.Node) error {
			o, err := getOutput(dn)
			if err != nil {
				return err
68
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69 70 71

			added.Objects = append(added.Objects, o)
			added.Names = append(added.Names, name)
72
			return nil
73
		}
74

75 76 77 78 79 80 81 82 83 84 85 86
		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
87
			log.Infof("adding file: %s", name)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
88
			if err := addDagnode(name, dns[len(dns)-1]); err != nil {
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
				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
117
			if err := addDagnode(name, tree); err != nil {
118 119 120
				return nil, err
			}
			return tree, nil
121 122
		}

123 124
		addPath = func(fpath string) (*dag.Node, error) {
			fi, err := os.Stat(fpath)
125
			if err != nil {
126
				return nil, err
127
			}
128

129
			if fi.IsDir() {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
130 131 132
				if !recursive {
					return nil, errors.New("use -r to recursively add directories")
				}
133 134 135 136 137 138 139
				return addDir(fpath)
			}
			return addFile(fpath)
		}

		paths, err := internal.CastToStrings(req.Arguments())
		if err != nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
140
			panic(err)
141
			return nil, err
142 143
		}

144 145 146 147 148
		for _, f := range paths {
			if _, err := addPath(f); err != nil {
				return nil, err
			}
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
149
		return added, nil
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

		// 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
171 172
		//
		// return &AddOutput{added}, nil
173
	},
174
	Marshalers: cmds.MarshalerMap{
175
		cmds.Text: func(res cmds.Response) ([]byte, error) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
176 177
			val, ok := res.Output().(*AddOutput)
			if !ok {
178
				return nil, u.ErrCast()
179
			}
180

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
181 182 183
			var buf bytes.Buffer
			for i, obj := range val.Objects {
				buf.Write([]byte(fmt.Sprintf("added %s %s\n", obj.Hash, val.Names[i])))
184
			}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
185
			return buf.Bytes(), nil
186
		},
187
	},
188
	Type: &AddOutput{},
189 190
}

191
func add(n *core.IpfsNode, readers []io.Reader) ([]*dag.Node, error) {
192 193 194 195 196
	mp, ok := n.Pinning.(pinning.ManualPinner)
	if !ok {
		return nil, errors.New("invalid pinner type! expected manual pinner")
	}

197
	dagnodes := make([]*dag.Node, 0)
198

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

201
	for _, reader := range readers {
202
		node, err := importer.BuildDagFromReader(reader, n.DAG, mp, chunk.DefaultSplitter)
203 204 205 206
		if err != nil {
			return nil, err
		}

207
		err = addNode(n, node)
208 209 210 211 212 213
		if err != nil {
			return nil, err
		}

		dagnodes = append(dagnodes, node)
	}
214

215
	return dagnodes, nil
216
}
217 218 219 220 221 222 223 224 225 226 227 228 229 230

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
}