add.go 4.5 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) (interface{}, error) {
48 49 50 51
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}
52

53
		outChan := make(chan interface{})
54

55 56
		go func() {
			defer close(outChan)
57

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

64 65 66 67 68 69
				_, err = addFile(n, file, outChan)
				if err != nil {
					return
				}
			}
		}()
70

71
		return outChan, nil
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
	return dagnodes, nil
126
}
127 128 129 130 131 132 133 134 135 136 137 138 139 140

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

142
func addFile(n *core.IpfsNode, file files.File, out chan interface{}) (*dag.Node, error) {
143
	if file.IsDirectory() {
144
		return addDir(n, file, out)
145 146 147 148 149 150 151 152
	}

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

	log.Infof("adding file: %s", file.FileName())
153
	if err := outputDagnode(out, file.FileName(), dns[len(dns)-1]); err != nil {
154 155 156 157 158
		return nil, err
	}
	return dns[len(dns)-1], nil // last dag node is the file.
}

159
func addDir(n *core.IpfsNode, dir files.File, out chan interface{}) (*dag.Node, error) {
160 161 162 163 164 165 166 167 168 169 170 171 172
	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
		}

173
		node, err := addFile(n, file, out)
174 175 176 177
		if err != nil {
			return nil, err
		}

178
		_, name := path.Split(file.FileName())
179 180 181 182 183 184 185

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

186
	err := outputDagnode(out, dir.FileName(), tree)
187 188 189 190 191 192 193 194
	if err != nil {
		return nil, err
	}

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

196
	return tree, nil
197 198
}

199 200
// outputDagnode sends dagnode info over the output channel
func outputDagnode(out chan interface{}, name string, dn *dag.Node) error {
201 202 203 204 205
	o, err := getOutput(dn)
	if err != nil {
		return err
	}

206 207 208 209
	out <- &AddedObject{
		Hash: o.Hash,
		Name: name,
	}
210

211
	return nil
212
}