add.go 4.61 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
	"path"
9 10

	cmds "github.com/jbenet/go-ipfs/commands"
11
	files "github.com/jbenet/go-ipfs/commands/files"
12 13
	core "github.com/jbenet/go-ipfs/core"
	importer "github.com/jbenet/go-ipfs/importer"
14
	"github.com/jbenet/go-ipfs/importer/chunk"
15
	dag "github.com/jbenet/go-ipfs/merkledag"
16
	pinning "github.com/jbenet/go-ipfs/pin"
17
	ft "github.com/jbenet/go-ipfs/unixfs"
18
	u "github.com/jbenet/go-ipfs/util"
19 20 21 22 23
)

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

24 25 26
type AddedObject struct {
	Name string
	Hash string
27 28
}

29
var AddCmd = &cmds.Command{
30 31 32 33 34 35 36 37 38 39
	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.
`,
	},

40
	Arguments: []cmds.Argument{
41
		cmds.FileArg("path", true, true, "The path to a file to be added to IPFS").EnableRecursive().EnableStdin(),
42
	},
43 44
	Options: []cmds.Option{
		cmds.OptionRecursivePath, // a builtin option that allows recursive paths (-r, --recursive)
45
		cmds.BoolOption("quiet", "q", "Write minimal output"),
46
	},
47
	Run: func(req cmds.Request, res cmds.Response) {
48 49
		n, err := req.Context().GetNode()
		if err != nil {
50 51
			res.SetError(err, cmds.ErrNormal)
			return
52
		}
53

54
		outChan := make(chan interface{})
55
		res.SetOutput((<-chan interface{})(outChan))
56

57 58
		go func() {
			defer close(outChan)
59

60 61 62 63 64
			for {
				file, err := req.Files().NextFile()
				if (err != nil && err != io.EOF) || file == nil {
					return
				}
65

66 67 68 69 70 71
				_, err = addFile(n, file, outChan)
				if err != nil {
					return
				}
			}
		}()
72
	},
73
	Marshalers: cmds.MarshalerMap{
74
		cmds.Text: func(res cmds.Response) (io.Reader, error) {
75
			outChan, ok := res.Output().(<-chan interface{})
Brian Tiger Chow's avatar
Brian Tiger Chow committed
76
			if !ok {
77
				return nil, u.ErrCast()
78
			}
79

80 81 82 83
			quiet, _, err := res.Request().Option("quiet").Bool()
			if err != nil {
				return nil, err
			}
84

85 86 87 88 89 90 91 92 93
			marshal := func(v interface{}) (io.Reader, error) {
				obj, ok := v.(*AddedObject)
				if !ok {
					return nil, u.ErrCast()
				}

				var buf bytes.Buffer
				if quiet {
					buf.WriteString(fmt.Sprintf("%s\n", obj.Hash))
94
				} else {
95
					buf.WriteString(fmt.Sprintf("added %s %s\n", obj.Hash, obj.Name))
96
				}
97
				return &buf, nil
98
			}
99 100 101 102 103

			return &cmds.ChannelMarshaler{
				Channel:   outChan,
				Marshaler: marshal,
			}, nil
104
		},
105
	},
106
	Type: AddedObject{},
107 108
}

109
func add(n *core.IpfsNode, readers []io.Reader) ([]*dag.Node, error) {
110 111 112 113 114
	mp, ok := n.Pinning.(pinning.ManualPinner)
	if !ok {
		return nil, errors.New("invalid pinner type! expected manual pinner")
	}

115
	dagnodes := make([]*dag.Node, 0)
116

117
	for _, reader := range readers {
118
		node, err := importer.BuildDagFromReader(reader, n.DAG, mp, chunk.DefaultSplitter)
119 120 121 122 123
		if err != nil {
			return nil, err
		}
		dagnodes = append(dagnodes, node)
	}
124

125 126 127 128 129
	err := n.Pinning.Flush()
	if err != nil {
		return nil, err
	}

130
	return dagnodes, nil
131
}
132 133 134 135 136 137 138 139 140 141 142 143 144 145

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
}
146

147
func addFile(n *core.IpfsNode, file files.File, out chan interface{}) (*dag.Node, error) {
148
	if file.IsDirectory() {
149
		return addDir(n, file, out)
150 151 152 153 154 155 156 157
	}

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

	log.Infof("adding file: %s", file.FileName())
158
	if err := outputDagnode(out, file.FileName(), dns[len(dns)-1]); err != nil {
159 160 161 162 163
		return nil, err
	}
	return dns[len(dns)-1], nil // last dag node is the file.
}

164
func addDir(n *core.IpfsNode, dir files.File, out chan interface{}) (*dag.Node, error) {
165 166 167 168 169 170 171 172 173 174 175 176 177
	log.Infof("adding directory: %s", dir.FileName())

	tree := &dag.Node{Data: ft.FolderPBData()}

	for {
		file, err := dir.NextFile()
		if err != nil && err != io.EOF {
			return nil, err
		}
		if file == nil {
			break
		}

178
		node, err := addFile(n, file, out)
179 180 181 182
		if err != nil {
			return nil, err
		}

183
		_, name := path.Split(file.FileName())
184 185 186 187 188 189 190

		err = tree.AddNodeLink(name, node)
		if err != nil {
			return nil, err
		}
	}

191
	err := outputDagnode(out, dir.FileName(), tree)
192 193 194 195 196 197 198 199
	if err != nil {
		return nil, err
	}

	err = addNode(n, tree)
	if err != nil {
		return nil, err
	}
200

201
	return tree, nil
202 203
}

204 205
// outputDagnode sends dagnode info over the output channel
func outputDagnode(out chan interface{}, name string, dn *dag.Node) error {
206 207 208 209 210
	o, err := getOutput(dn)
	if err != nil {
		return err
	}

211 212 213 214
	out <- &AddedObject{
		Hash: o.Hash,
		Name: name,
	}
215

216
	return nil
217
}