files.go 28.8 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4
package commands

import (
	"bytes"
Jeromy's avatar
Jeromy committed
5
	"context"
Jeromy's avatar
Jeromy committed
6 7 8 9 10
	"errors"
	"fmt"
	"io"
	"os"
	gopath "path"
Lucas Molas's avatar
Lucas Molas committed
11
	"sort"
Jeromy's avatar
Jeromy committed
12 13
	"strings"

Michael Muré's avatar
Michael Muré committed
14 15
	oldcmds "github.com/ipfs/go-ipfs/commands"
	lgc "github.com/ipfs/go-ipfs/commands/legacy"
Jeromy's avatar
Jeromy committed
16
	core "github.com/ipfs/go-ipfs/core"
17
	cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
Jan Winkelmann's avatar
Jan Winkelmann committed
18
	e "github.com/ipfs/go-ipfs/core/commands/e"
Steven Allen's avatar
Steven Allen committed
19 20 21 22 23 24
	ft "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs"
	uio "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs/io"
	path "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
	resolver "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path/resolver"
	dag "gx/ipfs/QmXv5mwmQ74r4aiHcNeQ4GAmfB3aWJuqaE4WyDfDfvkgLM/go-merkledag"
	bservice "gx/ipfs/Qma2KhbQarYTkmSJAeaMGRAg8HAXAhEWK8ge4SReG7ZSD3/go-blockservice"
25

Michael Muré's avatar
Michael Muré committed
26
	humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
27
	cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
28
	cmds "gx/ipfs/QmPXR4tNdLbp8HsZiPMjpsgqphX9Vhw2J6Jh5MKH2ovW3D/go-ipfs-cmds"
29
	mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
Steven Allen's avatar
Steven Allen committed
30
	logging "gx/ipfs/QmRREK2CAZ5Re2Bd9zZFG6FeYDppUWt5cMgsoUEp3ktgSr/go-log"
Steven Allen's avatar
Steven Allen committed
31
	mfs "gx/ipfs/QmRkrpnhZqDxTxwGCsDbuZMr7uCFZHH6SGfrcjgEQwxF3t/go-mfs"
32
	cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
Steven Allen's avatar
Steven Allen committed
33
	offline "gx/ipfs/QmcRC35JF2pJQneAxa5LdQBQRumWggccWErogSrCkS1h8T/go-ipfs-exchange-offline"
34
	ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
Jeromy's avatar
Jeromy committed
35 36
)

37
var flog = logging.Logger("cmds/files")
Jeromy's avatar
Jeromy committed
38

Michael Muré's avatar
Michael Muré committed
39
// FilesCmd is the 'ipfs files' command
Jeromy's avatar
Jeromy committed
40
var FilesCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
41
	Helptext: cmdkit.HelpText{
42
		Tagline: "Interact with unixfs files.",
Jeromy's avatar
Jeromy committed
43
		ShortDescription: `
44 45
Files is an API for manipulating IPFS objects as if they were a unix
filesystem.
Jeromy's avatar
Jeromy committed
46

47
NOTE:
48 49 50 51 52 53 54
Most of the subcommands of 'ipfs files' accept the '--flush' flag. It defaults
to true. Use caution when setting this flag to false. It will improve
performance for large numbers of file operations, but it does so at the cost
of consistency guarantees. If the daemon is unexpectedly killed before running
'ipfs files flush' on the files in question, then data may be lost. This also
applies to running 'ipfs repo gc' concurrently with '--flush=false'
operations.
Jeromy's avatar
Jeromy committed
55 56
`,
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
57
	Options: []cmdkit.Option{
Michael Muré's avatar
Michael Muré committed
58
		cmdkit.BoolOption("f", "flush", "Flush target and ancestors after write.").WithDefault(true),
Jeromy's avatar
Jeromy committed
59
	},
Jeromy's avatar
Jeromy committed
60
	Subcommands: map[string]*cmds.Command{
Michael Muré's avatar
Michael Muré committed
61
		"read":  lgc.NewCommand(filesReadCmd),
62
		"write": filesWriteCmd,
Michael Muré's avatar
Michael Muré committed
63 64 65 66
		"mv":    lgc.NewCommand(filesMvCmd),
		"cp":    lgc.NewCommand(filesCpCmd),
		"ls":    lgc.NewCommand(filesLsCmd),
		"mkdir": lgc.NewCommand(filesMkdirCmd),
67
		"stat":  filesStatCmd,
Michael Muré's avatar
Michael Muré committed
68 69 70
		"rm":    lgc.NewCommand(filesRmCmd),
		"flush": lgc.NewCommand(filesFlushCmd),
		"chcid": lgc.NewCommand(filesChcidCmd),
Jeromy's avatar
Jeromy committed
71 72 73
	},
}

Jan Winkelmann's avatar
Jan Winkelmann committed
74 75
var cidVersionOption = cmdkit.IntOption("cid-version", "cid-ver", "Cid version to use. (experimental)")
var hashOption = cmdkit.StringOption("hash", "Hash function to use. Will set Cid version to 1 if used. (experimental)")
76

Michael Muré's avatar
Michael Muré committed
77
var errFormat = errors.New("format was set by multiple options. Only one format option is allowed")
78

Michael Muré's avatar
Michael Muré committed
79
type statOutput struct {
80 81 82 83 84
	Hash           string
	Size           uint64
	CumulativeSize uint64
	Blocks         int
	Type           string
Michael Muré's avatar
Michael Muré committed
85
	WithLocality   bool   `json:",omitempty"`
86 87 88 89
	Local          bool   `json:",omitempty"`
	SizeLocal      uint64 `json:",omitempty"`
}

keks's avatar
keks committed
90 91 92 93 94 95
const defaultStatFormat = `<hash>
Size: <size>
CumulativeSize: <cumulsize>
ChildBlocks: <childs>
Type: <type>`

96
var filesStatCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
97
	Helptext: cmdkit.HelpText{
rht's avatar
rht committed
98
		Tagline: "Display file status.",
Jeromy's avatar
Jeromy committed
99 100
	},

Jan Winkelmann's avatar
Jan Winkelmann committed
101 102
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("path", true, false, "Path to node to stat."),
Jeromy's avatar
Jeromy committed
103
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
104 105
	Options: []cmdkit.Option{
		cmdkit.StringOption("format", "Print statistics in given format. Allowed tokens: "+
keks's avatar
keks committed
106
			"<hash> <size> <cumulsize> <type> <childs>. Conflicts with other format options.").WithDefault(defaultStatFormat),
107 108
		cmdkit.BoolOption("hash", "Print only hash. Implies '--format=<hash>'. Conflicts with other format options."),
		cmdkit.BoolOption("size", "Print only size. Implies '--format=<cumulsize>'. Conflicts with other format options."),
109
		cmdkit.BoolOption("with-local", "Compute the amount of the dag that is local, and if possible the total size"),
110
	},
keks's avatar
keks committed
111
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
112 113 114

		_, err := statGetFormatOptions(req)
		if err != nil {
keks's avatar
keks committed
115
			return cmdkit.Errorf(cmdkit.ErrClient, err.Error())
116 117
		}

118
		node, err := cmdenv.GetNode(env)
Jeromy's avatar
Jeromy committed
119
		if err != nil {
keks's avatar
keks committed
120
			return err
Jeromy's avatar
Jeromy committed
121 122
		}

123
		path, err := checkPath(req.Arguments[0])
Jeromy's avatar
Jeromy committed
124
		if err != nil {
keks's avatar
keks committed
125
			return err
Jeromy's avatar
Jeromy committed
126 127
		}

128 129 130 131 132 133 134 135 136 137 138 139 140 141
		withLocal, _ := req.Options["with-local"].(bool)

		var dagserv ipld.DAGService
		if withLocal {
			// an offline DAGService will not fetch from the network
			dagserv = dag.NewDAGService(bservice.New(
				node.Blockstore,
				offline.Exchange(node.Blockstore),
			))
		} else {
			dagserv = node.DAG
		}

		nd, err := getNodeFromPath(req.Context, node, dagserv, path)
Jeromy's avatar
Jeromy committed
142
		if err != nil {
keks's avatar
keks committed
143
			return err
Jeromy's avatar
Jeromy committed
144 145
		}

146
		o, err := statNode(nd)
Jeromy's avatar
Jeromy committed
147
		if err != nil {
keks's avatar
keks committed
148
			return err
Jeromy's avatar
Jeromy committed
149 150
		}

151
		if !withLocal {
keks's avatar
keks committed
152
			return cmds.EmitOnce(res, o)
153 154 155 156 157 158 159 160
		}

		local, sizeLocal, err := walkBlock(req.Context, dagserv, nd)

		o.WithLocality = true
		o.Local = local
		o.SizeLocal = sizeLocal

keks's avatar
keks committed
161
		return cmds.EmitOnce(res, o)
Jeromy's avatar
Jeromy committed
162
	},
163 164
	Encoders: cmds.EncoderMap{
		cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
Michael Muré's avatar
Michael Muré committed
165
			out, ok := v.(*statOutput)
Jan Winkelmann's avatar
Jan Winkelmann committed
166
			if !ok {
167
				return e.TypeErr(out, v)
Jan Winkelmann's avatar
Jan Winkelmann committed
168
			}
169

170
			s, _ := statGetFormatOptions(req)
171 172 173 174 175 176
			s = strings.Replace(s, "<hash>", out.Hash, -1)
			s = strings.Replace(s, "<size>", fmt.Sprintf("%d", out.Size), -1)
			s = strings.Replace(s, "<cumulsize>", fmt.Sprintf("%d", out.CumulativeSize), -1)
			s = strings.Replace(s, "<childs>", fmt.Sprintf("%d", out.Blocks), -1)
			s = strings.Replace(s, "<type>", out.Type, -1)

177 178 179 180 181 182 183 184 185 186 187
			fmt.Fprintln(w, s)

			if out.WithLocality {
				fmt.Fprintf(w, "Local: %s of %s (%.2f%%)\n",
					humanize.Bytes(out.SizeLocal),
					humanize.Bytes(out.CumulativeSize),
					100.0*float64(out.SizeLocal)/float64(out.CumulativeSize),
				)
			}

			return nil
188
		}),
Jeromy's avatar
Jeromy committed
189
	},
Michael Muré's avatar
Michael Muré committed
190
	Type: statOutput{},
Jeromy's avatar
Jeromy committed
191 192
}

193 194 195 196
func moreThanOne(a, b, c bool) bool {
	return a && b || b && c || a && c
}

197
func statGetFormatOptions(req *cmds.Request) (string, error) {
198

199 200 201
	hash, _ := req.Options["hash"].(bool)
	size, _ := req.Options["size"].(bool)
	format, _ := req.Options["format"].(string)
202

keks's avatar
keks committed
203
	if moreThanOne(hash, size, format != defaultStatFormat) {
Michael Muré's avatar
Michael Muré committed
204
		return "", errFormat
205 206 207
	}

	if hash {
208 209 210 211 212
		return "<hash>", nil
	} else if size {
		return "<cumulsize>", nil
	} else {
		return format, nil
213 214 215
	}
}

Michael Muré's avatar
Michael Muré committed
216
func statNode(nd ipld.Node) (*statOutput, error) {
Jeromy's avatar
Jeromy committed
217
	c := nd.Cid()
Jeromy's avatar
Jeromy committed
218 219 220 221 222 223

	cumulsize, err := nd.Size()
	if err != nil {
		return nil, err
	}

224 225 226 227 228 229 230 231
	switch n := nd.(type) {
	case *dag.ProtoNode:
		d, err := ft.FromBytes(n.Data())
		if err != nil {
			return nil, err
		}

		var ndtype string
232 233
		switch d.GetType() {
		case ft.TDirectory, ft.THAMTShard:
234
			ndtype = "directory"
235
		case ft.TFile, ft.TMetadata, ft.TRaw:
236 237
			ndtype = "file"
		default:
238
			return nil, fmt.Errorf("unrecognized node type: %s", d.GetType())
239 240
		}

Michael Muré's avatar
Michael Muré committed
241
		return &statOutput{
242 243 244 245 246 247 248
			Hash:           c.String(),
			Blocks:         len(nd.Links()),
			Size:           d.GetFilesize(),
			CumulativeSize: cumulsize,
			Type:           ndtype,
		}, nil
	case *dag.RawNode:
Michael Muré's avatar
Michael Muré committed
249
		return &statOutput{
250 251 252 253 254 255
			Hash:           c.String(),
			Blocks:         0,
			Size:           cumulsize,
			CumulativeSize: cumulsize,
			Type:           "file",
		}, nil
Jeromy's avatar
Jeromy committed
256
	default:
257
		return nil, fmt.Errorf("not unixfs node (proto or raw)")
Jeromy's avatar
Jeromy committed
258
	}
Jeromy's avatar
Jeromy committed
259 260
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
func walkBlock(ctx context.Context, dagserv ipld.DAGService, nd ipld.Node) (bool, uint64, error) {
	// Start with the block data size
	sizeLocal := uint64(len(nd.RawData()))

	local := true

	for _, link := range nd.Links() {
		child, err := dagserv.Get(ctx, link.Cid)

		if err == ipld.ErrNotFound {
			local = false
			continue
		}

		if err != nil {
			return local, sizeLocal, err
		}

		childLocal, childLocalSize, err := walkBlock(ctx, dagserv, child)

		if err != nil {
			return local, sizeLocal, err
		}

		// Recursively add the child size
		local = local && childLocal
		sizeLocal += childLocalSize
	}

	return local, sizeLocal, nil
}

Michael Muré's avatar
Michael Muré committed
293
var filesCpCmd = &oldcmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
294
	Helptext: cmdkit.HelpText{
rht's avatar
rht committed
295
		Tagline: "Copy files into mfs.",
Jeromy's avatar
Jeromy committed
296
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
297 298 299
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("source", true, false, "Source object to copy."),
		cmdkit.StringArg("dest", true, false, "Destination to copy object to."),
Jeromy's avatar
Jeromy committed
300
	},
Michael Muré's avatar
Michael Muré committed
301
	Run: func(req oldcmds.Request, res oldcmds.Response) {
Jeromy's avatar
Jeromy committed
302 303
		node, err := req.InvocContext().GetNode()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
304
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
305 306 307
			return
		}

308
		flush, _, _ := req.Option("flush").Bool()
309

Jeromy's avatar
Jeromy committed
310 311
		src, err := checkPath(req.Arguments()[0])
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
312
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
313 314
			return
		}
315 316
		src = strings.TrimRight(src, "/")

Jeromy's avatar
Jeromy committed
317 318
		dst, err := checkPath(req.Arguments()[1])
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
319
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
320 321
			return
		}
Jeromy's avatar
Jeromy committed
322

323 324 325 326
		if dst[len(dst)-1] == '/' {
			dst += gopath.Base(src)
		}

327
		nd, err := getNodeFromPath(req.Context(), node, node.DAG, src)
Jeromy's avatar
Jeromy committed
328
		if err != nil {
329
			res.SetError(fmt.Errorf("cp: cannot get node from path %s: %s", src, err), cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
330
			return
Jeromy's avatar
Jeromy committed
331 332 333 334
		}

		err = mfs.PutNode(node.FilesRoot, dst, nd)
		if err != nil {
335
			res.SetError(fmt.Errorf("cp: cannot put node in path %s: %s", dst, err), cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
336 337
			return
		}
338 339 340 341

		if flush {
			err := mfs.FlushPath(node.FilesRoot, dst)
			if err != nil {
Lucas Molas's avatar
Lucas Molas committed
342
				res.SetError(fmt.Errorf("cp: cannot flush the created file %s: %s", dst, err), cmdkit.ErrNormal)
343 344 345
				return
			}
		}
Jan Winkelmann's avatar
Jan Winkelmann committed
346 347

		res.SetOutput(nil)
Jeromy's avatar
Jeromy committed
348 349 350
	},
}

351
func getNodeFromPath(ctx context.Context, node *core.IpfsNode, dagservice ipld.DAGService, p string) (ipld.Node, error) {
Jeromy's avatar
Jeromy committed
352 353 354 355 356 357 358
	switch {
	case strings.HasPrefix(p, "/ipfs/"):
		np, err := path.ParsePath(p)
		if err != nil {
			return nil, err
		}

359
		resolver := &resolver.Resolver{
360
			DAG:         dagservice,
361 362 363
			ResolveOnce: uio.ResolveUnixfsOnce,
		}

364
		return core.Resolve(ctx, node.Namesys, resolver, np)
Jeromy's avatar
Jeromy committed
365 366 367 368 369 370 371 372 373 374
	default:
		fsn, err := mfs.Lookup(node.FilesRoot, p)
		if err != nil {
			return nil, err
		}

		return fsn.GetNode()
	}
}

Michael Muré's avatar
Michael Muré committed
375
type filesLsOutput struct {
Jeromy's avatar
Jeromy committed
376 377 378
	Entries []mfs.NodeListing
}

Michael Muré's avatar
Michael Muré committed
379
var filesLsCmd = &oldcmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
380
	Helptext: cmdkit.HelpText{
381
		Tagline: "List directories in the local mutable namespace.",
Jeromy's avatar
Jeromy committed
382
		ShortDescription: `
383
List directories in the local mutable namespace.
Jeromy's avatar
Jeromy committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

Examples:

    $ ipfs files ls /welcome/docs/
    about
    contact
    help
    quick-start
    readme
    security-notes

    $ ipfs files ls /myfiles/a/b/c/d
    foo
    bar
`,
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
400 401
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("path", false, false, "Path to show listing for. Defaults to '/'."),
Jeromy's avatar
Jeromy committed
402
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
403 404
	Options: []cmdkit.Option{
		cmdkit.BoolOption("l", "Use long listing format."),
Lucas Molas's avatar
Lucas Molas committed
405
		cmdkit.BoolOption("U", "Do not sort; list entries in directory order."),
Jeromy's avatar
Jeromy committed
406
	},
Michael Muré's avatar
Michael Muré committed
407
	Run: func(req oldcmds.Request, res oldcmds.Response) {
408 409 410 411 412 413 414 415 416
		var arg string

		if len(req.Arguments()) == 0 {
			arg = "/"
		} else {
			arg = req.Arguments()[0]
		}

		path, err := checkPath(arg)
Jeromy's avatar
Jeromy committed
417
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
418
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
419 420 421
			return
		}

Jeromy's avatar
Jeromy committed
422 423
		nd, err := req.InvocContext().GetNode()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
424
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
425 426 427 428 429
			return
		}

		fsn, err := mfs.Lookup(nd.FilesRoot, path)
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
430
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
431 432 433
			return
		}

Jeromy's avatar
Jeromy committed
434 435
		long, _, _ := req.Option("l").Bool()

Jeromy's avatar
Jeromy committed
436 437
		switch fsn := fsn.(type) {
		case *mfs.Directory:
Jeromy's avatar
Jeromy committed
438 439
			if !long {
				var output []mfs.NodeListing
Jeromy's avatar
Jeromy committed
440
				names, err := fsn.ListNames(req.Context())
441
				if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
442
					res.SetError(err, cmdkit.ErrNormal)
443 444 445 446
					return
				}

				for _, name := range names {
Jeromy's avatar
Jeromy committed
447
					output = append(output, mfs.NodeListing{
Jeromy's avatar
Jeromy committed
448
						Name: name,
Jeromy's avatar
Jeromy committed
449 450
					})
				}
Michael Muré's avatar
Michael Muré committed
451
				res.SetOutput(&filesLsOutput{output})
Jeromy's avatar
Jeromy committed
452
			} else {
Jeromy's avatar
Jeromy committed
453
				listing, err := fsn.List(req.Context())
Jeromy's avatar
Jeromy committed
454
				if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
455
					res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
456 457
					return
				}
Michael Muré's avatar
Michael Muré committed
458
				res.SetOutput(&filesLsOutput{listing})
Jeromy's avatar
Jeromy committed
459 460 461
			}
			return
		case *mfs.File:
rht's avatar
rht committed
462
			_, name := gopath.Split(path)
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
			out := &filesLsOutput{[]mfs.NodeListing{mfs.NodeListing{Name: name}}}
			if long {
				out.Entries[0].Type = int(fsn.Type())

				size, err := fsn.Size()
				if err != nil {
					res.SetError(err, cmdkit.ErrNormal)
					return
				}
				out.Entries[0].Size = size

				nd, err := fsn.GetNode()
				if err != nil {
					res.SetError(err, cmdkit.ErrNormal)
					return
				}
				out.Entries[0].Hash = nd.Cid().String()
			}
Jeromy's avatar
Jeromy committed
481 482 483
			res.SetOutput(out)
			return
		default:
Jan Winkelmann's avatar
Jan Winkelmann committed
484
			res.SetError(errors.New("unrecognized type"), cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
485 486
		}
	},
Michael Muré's avatar
Michael Muré committed
487 488
	Marshalers: oldcmds.MarshalerMap{
		oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) {
Jan Winkelmann's avatar
Jan Winkelmann committed
489 490 491 492 493
			v, err := unwrapOutput(res.Output())
			if err != nil {
				return nil, err
			}

Michael Muré's avatar
Michael Muré committed
494
			out, ok := v.(*filesLsOutput)
Jan Winkelmann's avatar
Jan Winkelmann committed
495 496 497 498
			if !ok {
				return nil, e.TypeErr(out, v)
			}

Jeromy's avatar
Jeromy committed
499 500
			buf := new(bytes.Buffer)

Lucas Molas's avatar
Lucas Molas committed
501 502 503 504 505 506 507 508
			noSort, _, _ := res.Request().Option("U").Bool()
			if !noSort {
				sort.Slice(out.Entries, func(i, j int) bool {
					return strings.Compare(out.Entries[i].Name, out.Entries[j].Name) < 0
				})
			}

			long, _, _ := res.Request().Option("l").Bool()
Jeromy's avatar
Jeromy committed
509 510 511 512 513 514 515 516 517 518
			for _, o := range out.Entries {
				if long {
					fmt.Fprintf(buf, "%s\t%s\t%d\n", o.Name, o.Hash, o.Size)
				} else {
					fmt.Fprintf(buf, "%s\n", o.Name)
				}
			}
			return buf, nil
		},
	},
Michael Muré's avatar
Michael Muré committed
519
	Type: filesLsOutput{},
Jeromy's avatar
Jeromy committed
520 521
}

Michael Muré's avatar
Michael Muré committed
522
var filesReadCmd = &oldcmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
523
	Helptext: cmdkit.HelpText{
rht's avatar
rht committed
524
		Tagline: "Read a file in a given mfs.",
Jeromy's avatar
Jeromy committed
525
		ShortDescription: `
526 527
Read a specified number of bytes from a file at a given offset. By default,
will read the entire file similar to unix cat.
Jeromy's avatar
Jeromy committed
528 529 530 531 532

Examples:

    $ ipfs files read /test/hello
    hello
Jeromy's avatar
Jeromy committed
533
        `,
Jeromy's avatar
Jeromy committed
534 535
	},

Jan Winkelmann's avatar
Jan Winkelmann committed
536 537
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("path", true, false, "Path to file to be read."),
Jeromy's avatar
Jeromy committed
538
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
539 540 541
	Options: []cmdkit.Option{
		cmdkit.IntOption("offset", "o", "Byte offset to begin reading from."),
		cmdkit.IntOption("count", "n", "Maximum number of bytes to read."),
Jeromy's avatar
Jeromy committed
542
	},
Michael Muré's avatar
Michael Muré committed
543
	Run: func(req oldcmds.Request, res oldcmds.Response) {
Jeromy's avatar
Jeromy committed
544 545
		n, err := req.InvocContext().GetNode()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
546
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
547 548 549
			return
		}

Jeromy's avatar
Jeromy committed
550 551
		path, err := checkPath(req.Arguments()[0])
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
552
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
553 554 555
			return
		}

Jeromy's avatar
Jeromy committed
556 557
		fsn, err := mfs.Lookup(n.FilesRoot, path)
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
558
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
559 560 561 562 563
			return
		}

		fi, ok := fsn.(*mfs.File)
		if !ok {
Michael Muré's avatar
Michael Muré committed
564
			res.SetError(fmt.Errorf("%s was not a file", path), cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
565 566 567
			return
		}

568 569
		rfd, err := fi.Open(mfs.OpenReadOnly, false)
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
570
			res.SetError(err, cmdkit.ErrNormal)
571 572 573 574 575
			return
		}

		defer rfd.Close()

Jeromy's avatar
Jeromy committed
576 577
		offset, _, err := req.Option("offset").Int()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
578
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
579 580 581
			return
		}
		if offset < 0 {
Michael Muré's avatar
Michael Muré committed
582
			res.SetError(fmt.Errorf("cannot specify negative offset"), cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
583 584 585
			return
		}

586
		filen, err := rfd.Size()
Jeromy's avatar
Jeromy committed
587
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
588
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
589 590 591 592
			return
		}

		if int64(offset) > filen {
Michael Muré's avatar
Michael Muré committed
593
			res.SetError(fmt.Errorf("offset was past end of file (%d > %d)", offset, filen), cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
594 595
			return
		}
Jeromy's avatar
Jeromy committed
596

597
		_, err = rfd.Seek(int64(offset), io.SeekStart)
Jeromy's avatar
Jeromy committed
598
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
599
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
600 601
			return
		}
602 603

		var r io.Reader = &contextReaderWrapper{R: rfd, ctx: req.Context()}
Jeromy's avatar
Jeromy committed
604
		count, found, err := req.Option("count").Int()
Jeromy's avatar
Jeromy committed
605
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
606
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
607 608 609 610
			return
		}
		if found {
			if count < 0 {
Michael Muré's avatar
Michael Muré committed
611
				res.SetError(fmt.Errorf("cannot specify negative 'count'"), cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
612 613
				return
			}
614
			r = io.LimitReader(r, int64(count))
Jeromy's avatar
Jeromy committed
615 616 617 618 619 620
		}

		res.SetOutput(r)
	},
}

Jeromy's avatar
Jeromy committed
621 622 623 624 625 626 627 628 629 630 631 632 633
type contextReader interface {
	CtxReadFull(context.Context, []byte) (int, error)
}

type contextReaderWrapper struct {
	R   contextReader
	ctx context.Context
}

func (crw *contextReaderWrapper) Read(b []byte) (int, error) {
	return crw.R.CtxReadFull(crw.ctx, b)
}

Michael Muré's avatar
Michael Muré committed
634
var filesMvCmd = &oldcmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
635
	Helptext: cmdkit.HelpText{
rht's avatar
rht committed
636
		Tagline: "Move files.",
Jeromy's avatar
Jeromy committed
637 638 639 640 641 642 643
		ShortDescription: `
Move files around. Just like traditional unix mv.

Example:

    $ ipfs files mv /myfs/a/b/c /myfs/foo/newc

Jeromy's avatar
Jeromy committed
644
`,
Jeromy's avatar
Jeromy committed
645 646
	},

Jan Winkelmann's avatar
Jan Winkelmann committed
647 648 649
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("source", true, false, "Source file to move."),
		cmdkit.StringArg("dest", true, false, "Destination path for file to be moved to."),
Jeromy's avatar
Jeromy committed
650
	},
Michael Muré's avatar
Michael Muré committed
651
	Run: func(req oldcmds.Request, res oldcmds.Response) {
Jeromy's avatar
Jeromy committed
652 653
		n, err := req.InvocContext().GetNode()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
654
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
655 656 657
			return
		}

Jeromy's avatar
Jeromy committed
658 659
		src, err := checkPath(req.Arguments()[0])
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
660
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
661 662 663 664
			return
		}
		dst, err := checkPath(req.Arguments()[1])
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
665
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
666 667
			return
		}
Jeromy's avatar
Jeromy committed
668 669 670

		err = mfs.Mv(n.FilesRoot, src, dst)
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
671
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
672 673
			return
		}
Jan Winkelmann's avatar
Jan Winkelmann committed
674 675

		res.SetOutput(nil)
Jeromy's avatar
Jeromy committed
676 677 678
	},
}

679
var filesWriteCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
680
	Helptext: cmdkit.HelpText{
rht's avatar
rht committed
681
		Tagline: "Write to a mutable file in a given filesystem.",
Jeromy's avatar
Jeromy committed
682 683
		ShortDescription: `
Write data to a file in a given filesystem. This command allows you to specify
684 685
a beginning offset to write to. The entire length of the input will be
written.
Jeromy's avatar
Jeromy committed
686

Jeromy's avatar
Jeromy committed
687
If the '--create' option is specified, the file will be created if it does not
Jeromy's avatar
Jeromy committed
688 689
exist. Nonexistant intermediate directories will not be created.

690 691 692 693 694 695 696
Newly created files will have the same CID version and hash function of the
parent directory unless the --cid-version and --hash options are used.

Newly created leaves will be in the legacy format (Protobuf) if the
CID version is 0, or raw is the CID version is non-zero.  Use of the
--raw-leaves option will override this behavior.

697 698 699 700
If the '--flush' option is set to false, changes will not be propogated to the
merkledag root. This can make operations much faster when doing a large number
of writes to a deeper directory structure.

701
EXAMPLE:
Jeromy's avatar
Jeromy committed
702

Jeromy's avatar
Jeromy committed
703 704
    echo "hello world" | ipfs files write --create /myfs/a/b/file
    echo "hello world" | ipfs files write --truncate /myfs/a/b/file
705

706
WARNING:
707

708 709 710
Usage of the '--flush=false' option does not guarantee data durability until
the tree has been flushed. This can be accomplished by running 'ipfs files
stat' on the file or any of its ancestors.
Jeromy's avatar
Jeromy committed
711
`,
Jeromy's avatar
Jeromy committed
712
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
713 714 715
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("path", true, false, "Path to write to."),
		cmdkit.FileArg("data", true, false, "Data to write.").EnableStdin(),
Jeromy's avatar
Jeromy committed
716
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
717 718 719
	Options: []cmdkit.Option{
		cmdkit.IntOption("offset", "o", "Byte offset to begin writing at."),
		cmdkit.BoolOption("create", "e", "Create the file if it does not exist."),
720
		cmdkit.BoolOption("parents", "p", "Make parent directories as needed."),
Jan Winkelmann's avatar
Jan Winkelmann committed
721 722 723
		cmdkit.BoolOption("truncate", "t", "Truncate the file to size zero before writing."),
		cmdkit.IntOption("count", "n", "Maximum number of bytes to read."),
		cmdkit.BoolOption("raw-leaves", "Use raw blocks for newly created leaf nodes. (experimental)"),
724 725
		cidVersionOption,
		hashOption,
Jeromy's avatar
Jeromy committed
726
	},
keks's avatar
keks committed
727
	Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) (retErr error) {
728
		path, err := checkPath(req.Arguments[0])
Jeromy's avatar
Jeromy committed
729
		if err != nil {
keks's avatar
keks committed
730
			return err
Jeromy's avatar
Jeromy committed
731 732
		}

733
		create, _ := req.Options["create"].(bool)
734
		mkParents, _ := req.Options["parents"].(bool)
735 736 737
		trunc, _ := req.Options["truncate"].(bool)
		flush, _ := req.Options["flush"].(bool)
		rawLeaves, rawLeavesDef := req.Options["raw-leaves"].(bool)
738

739
		prefix, err := getPrefixNew(req)
740
		if err != nil {
keks's avatar
keks committed
741
			return err
742
		}
Jeromy's avatar
Jeromy committed
743

744
		nd, err := cmdenv.GetNode(env)
Jeromy's avatar
Jeromy committed
745
		if err != nil {
keks's avatar
keks committed
746
			return err
Jeromy's avatar
Jeromy committed
747 748
		}

749
		offset, _ := req.Options["offset"].(int)
Jeromy's avatar
Jeromy committed
750
		if offset < 0 {
keks's avatar
keks committed
751
			return fmt.Errorf("cannot have negative write offset")
Jeromy's avatar
Jeromy committed
752 753
		}

754 755 756
		if mkParents {
			err := ensureContainingDirectoryExists(nd.FilesRoot, path, prefix)
			if err != nil {
keks's avatar
keks committed
757
				return err
758 759 760
			}
		}

761
		fi, err := getFileHandle(nd.FilesRoot, path, create, prefix)
Jeromy's avatar
Jeromy committed
762
		if err != nil {
keks's avatar
keks committed
763
			return err
Jeromy's avatar
Jeromy committed
764
		}
765 766 767
		if rawLeavesDef {
			fi.RawLeaves = rawLeaves
		}
768

769 770
		wfd, err := fi.Open(mfs.OpenWriteOnly, flush)
		if err != nil {
keks's avatar
keks committed
771
			return err
772
		}
Jeromy's avatar
Jeromy committed
773

774 775 776
		defer func() {
			err := wfd.Close()
			if err != nil {
keks's avatar
keks committed
777 778 779 780 781
				if retErr == nil {
					retErr = err
				} else {
					log.Error("files: error closing file mfs file descriptor", err)
				}
782 783
			}
		}()
784

Jeromy's avatar
Jeromy committed
785
		if trunc {
786
			if err := wfd.Truncate(0); err != nil {
keks's avatar
keks committed
787
				return err
Jeromy's avatar
Jeromy committed
788 789 790
			}
		}

791
		count, countfound := req.Options["count"].(int)
Jeromy's avatar
Jeromy committed
792
		if countfound && count < 0 {
keks's avatar
keks committed
793
			return fmt.Errorf("cannot have negative byte count")
Jeromy's avatar
Jeromy committed
794
		}
Jeromy's avatar
Jeromy committed
795

796
		_, err = wfd.Seek(int64(offset), io.SeekStart)
Jeromy's avatar
Jeromy committed
797
		if err != nil {
798
			flog.Error("seekfail: ", err)
keks's avatar
keks committed
799
			return err
Jeromy's avatar
Jeromy committed
800 801
		}

802
		input, err := req.Files.NextFile()
Jeromy's avatar
Jeromy committed
803
		if err != nil {
keks's avatar
keks committed
804
			return err
Jeromy's avatar
Jeromy committed
805 806
		}

zramsay's avatar
zramsay committed
807 808 809 810 811
		var r io.Reader = input
		if countfound {
			r = io.LimitReader(r, int64(count))
		}

Jan Winkelmann's avatar
Jan Winkelmann committed
812
		_, err = io.Copy(wfd, r)
keks's avatar
keks committed
813
		return err
Jeromy's avatar
Jeromy committed
814 815 816
	},
}

Michael Muré's avatar
Michael Muré committed
817
var filesMkdirCmd = &oldcmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
818
	Helptext: cmdkit.HelpText{
rht's avatar
rht committed
819
		Tagline: "Make directories.",
Jeromy's avatar
Jeromy committed
820 821 822
		ShortDescription: `
Create the directory if it does not already exist.

823 824 825
The directory will have the same CID version and hash function of the
parent directory unless the --cid-version and --hash options are used.

826
NOTE: All paths must be absolute.
Jeromy's avatar
Jeromy committed
827 828 829

Examples:

830 831
    $ ipfs files mkdir /test/newdir
    $ ipfs files mkdir -p /test/does/not/exist/yet
Jeromy's avatar
Jeromy committed
832 833 834
`,
	},

Jan Winkelmann's avatar
Jan Winkelmann committed
835 836
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("path", true, false, "Path to dir to make."),
Jeromy's avatar
Jeromy committed
837
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
838 839
	Options: []cmdkit.Option{
		cmdkit.BoolOption("parents", "p", "No error if existing, make parent directories as needed."),
840 841
		cidVersionOption,
		hashOption,
Jeromy's avatar
Jeromy committed
842
	},
Michael Muré's avatar
Michael Muré committed
843
	Run: func(req oldcmds.Request, res oldcmds.Response) {
Jeromy's avatar
Jeromy committed
844 845
		n, err := req.InvocContext().GetNode()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
846
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
847 848 849 850
			return
		}

		dashp, _, _ := req.Option("parents").Bool()
Jeromy's avatar
Jeromy committed
851 852
		dirtomake, err := checkPath(req.Arguments()[0])
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
853
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
854 855 856
			return
		}

857
		flush, _, _ := req.Option("flush").Bool()
Jeromy's avatar
Jeromy committed
858

859 860
		prefix, err := getPrefix(req)
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
861
			res.SetError(err, cmdkit.ErrNormal)
862 863 864 865
			return
		}
		root := n.FilesRoot

866
		err = mfs.Mkdir(root, dirtomake, mfs.MkdirOpts{
867 868 869
			Mkparents:  dashp,
			Flush:      flush,
			CidBuilder: prefix,
870
		})
871
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
872
			res.SetError(err, cmdkit.ErrNormal)
873
			return
Jeromy's avatar
Jeromy committed
874
		}
keks's avatar
keks committed
875

Jan Winkelmann's avatar
Jan Winkelmann committed
876
		res.SetOutput(nil)
Jeromy's avatar
Jeromy committed
877 878 879
	},
}

Michael Muré's avatar
Michael Muré committed
880
var filesFlushCmd = &oldcmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
881
	Helptext: cmdkit.HelpText{
882
		Tagline: "Flush a given path's data to disk.",
Jeromy's avatar
Jeromy committed
883
		ShortDescription: `
884
Flush a given path to disk. This is only useful when other commands
Jeromy's avatar
Jeromy committed
885 886 887
are run with the '--flush=false'.
`,
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
888 889
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("path", false, false, "Path to flush. Default: '/'."),
Jeromy's avatar
Jeromy committed
890
	},
Michael Muré's avatar
Michael Muré committed
891
	Run: func(req oldcmds.Request, res oldcmds.Response) {
Jeromy's avatar
Jeromy committed
892 893
		nd, err := req.InvocContext().GetNode()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
894
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
895 896 897 898 899 900 901 902 903 904
			return
		}

		path := "/"
		if len(req.Arguments()) > 0 {
			path = req.Arguments()[0]
		}

		err = mfs.FlushPath(nd.FilesRoot, path)
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
905
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
906 907
			return
		}
Jan Winkelmann's avatar
Jan Winkelmann committed
908 909

		res.SetOutput(nil)
Jeromy's avatar
Jeromy committed
910 911 912
	},
}

Michael Muré's avatar
Michael Muré committed
913
var filesChcidCmd = &oldcmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
914
	Helptext: cmdkit.HelpText{
915
		Tagline: "Change the cid version or hash function of the root node of a given path.",
Kevin Atkinson's avatar
Kevin Atkinson committed
916
		ShortDescription: `
917
Change the cid version or hash function of the root node of a given path.
Kevin Atkinson's avatar
Kevin Atkinson committed
918 919
`,
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
920 921
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("path", false, false, "Path to change. Default: '/'."),
Kevin Atkinson's avatar
Kevin Atkinson committed
922
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
923
	Options: []cmdkit.Option{
924 925 926
		cidVersionOption,
		hashOption,
	},
Michael Muré's avatar
Michael Muré committed
927
	Run: func(req oldcmds.Request, res oldcmds.Response) {
Kevin Atkinson's avatar
Kevin Atkinson committed
928 929
		nd, err := req.InvocContext().GetNode()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
930
			res.SetError(err, cmdkit.ErrNormal)
Kevin Atkinson's avatar
Kevin Atkinson committed
931 932 933 934 935 936 937 938 939 940 941 942
			return
		}

		path := "/"
		if len(req.Arguments()) > 0 {
			path = req.Arguments()[0]
		}

		flush, _, _ := req.Option("flush").Bool()

		prefix, err := getPrefix(req)
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
943
			res.SetError(err, cmdkit.ErrNormal)
Kevin Atkinson's avatar
Kevin Atkinson committed
944 945 946 947 948
			return
		}

		err = updatePath(nd.FilesRoot, path, prefix, flush)
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
949
			res.SetError(err, cmdkit.ErrNormal)
Kevin Atkinson's avatar
Kevin Atkinson committed
950 951
			return
		}
keks's avatar
keks committed
952 953

		res.SetOutput(nil)
Kevin Atkinson's avatar
Kevin Atkinson committed
954 955 956
	},
}

957 958
func updatePath(rt *mfs.Root, pth string, builder cid.Builder, flush bool) error {
	if builder == nil {
Kevin Atkinson's avatar
Kevin Atkinson committed
959 960 961 962 963 964 965 966 967 968
		return nil
	}

	nd, err := mfs.Lookup(rt, pth)
	if err != nil {
		return err
	}

	switch n := nd.(type) {
	case *mfs.Directory:
969
		n.SetCidBuilder(builder)
Kevin Atkinson's avatar
Kevin Atkinson committed
970 971 972 973 974 975 976 977 978 979 980
	default:
		return fmt.Errorf("can only update directories")
	}

	if flush {
		nd.Flush()
	}

	return nil
}

Michael Muré's avatar
Michael Muré committed
981
var filesRmCmd = &oldcmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
982
	Helptext: cmdkit.HelpText{
rht's avatar
rht committed
983
		Tagline: "Remove a file.",
Jeromy's avatar
Jeromy committed
984
		ShortDescription: `
985
Remove files or directories.
Jeromy's avatar
Jeromy committed
986 987 988 989 990 991 992 993

    $ ipfs files rm /foo
    $ ipfs files ls /bar
    cat
    dog
    fish
    $ ipfs files rm -r /bar
`,
Jeromy's avatar
Jeromy committed
994 995
	},

Jan Winkelmann's avatar
Jan Winkelmann committed
996 997
	Arguments: []cmdkit.Argument{
		cmdkit.StringArg("path", true, true, "File to remove."),
Jeromy's avatar
Jeromy committed
998
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
999 1000
	Options: []cmdkit.Option{
		cmdkit.BoolOption("recursive", "r", "Recursively remove directories."),
Jeromy's avatar
Jeromy committed
1001
	},
Michael Muré's avatar
Michael Muré committed
1002
	Run: func(req oldcmds.Request, res oldcmds.Response) {
Jan Winkelmann's avatar
Jan Winkelmann committed
1003 1004
		defer res.SetOutput(nil)

Jeromy's avatar
Jeromy committed
1005 1006
		nd, err := req.InvocContext().GetNode()
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
1007
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
1008 1009 1010
			return
		}

Jeromy's avatar
Jeromy committed
1011 1012
		path, err := checkPath(req.Arguments()[0])
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
1013
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
1014 1015 1016 1017
			return
		}

		if path == "/" {
Jan Winkelmann's avatar
Jan Winkelmann committed
1018
			res.SetError(fmt.Errorf("cannot delete root"), cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
1019 1020 1021 1022 1023 1024 1025 1026
			return
		}

		// 'rm a/b/c/' will fail unless we trim the slash at the end
		if path[len(path)-1] == '/' {
			path = path[:len(path)-1]
		}

Jeromy's avatar
Jeromy committed
1027 1028 1029
		dir, name := gopath.Split(path)
		parent, err := mfs.Lookup(nd.FilesRoot, dir)
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
1030
			res.SetError(fmt.Errorf("parent lookup: %s", err), cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
1031 1032 1033 1034 1035
			return
		}

		pdir, ok := parent.(*mfs.Directory)
		if !ok {
Michael Muré's avatar
Michael Muré committed
1036
			res.SetError(fmt.Errorf("no such file or directory: %s", path), cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
1037 1038 1039
			return
		}

Jeromy's avatar
Jeromy committed
1040 1041
		dashr, _, _ := req.Option("r").Bool()

Jeromy's avatar
Jeromy committed
1042 1043 1044 1045 1046
		var success bool
		defer func() {
			if success {
				err := pdir.Flush()
				if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
1047
					res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
1048 1049 1050 1051 1052
					return
				}
			}
		}()

Jeromy's avatar
Jeromy committed
1053 1054 1055 1056
		// if '-r' specified, don't check file type (in bad scenarios, the block may not exist)
		if dashr {
			err := pdir.Unlink(name)
			if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
1057
				res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
1058 1059 1060
				return
			}

Jeromy's avatar
Jeromy committed
1061
			success = true
Jeromy's avatar
Jeromy committed
1062 1063 1064
			return
		}

Jeromy's avatar
Jeromy committed
1065 1066
		childi, err := pdir.Child(name)
		if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
1067
			res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
1068 1069 1070 1071 1072
			return
		}

		switch childi.(type) {
		case *mfs.Directory:
Jan Winkelmann's avatar
Jan Winkelmann committed
1073
			res.SetError(fmt.Errorf("%s is a directory, use -r to remove directories", path), cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
1074
			return
Jeromy's avatar
Jeromy committed
1075 1076 1077
		default:
			err := pdir.Unlink(name)
			if err != nil {
Jan Winkelmann's avatar
Jan Winkelmann committed
1078
				res.SetError(err, cmdkit.ErrNormal)
Jeromy's avatar
Jeromy committed
1079 1080
				return
			}
Jeromy's avatar
Jeromy committed
1081 1082

			success = true
Jeromy's avatar
Jeromy committed
1083 1084 1085 1086
		}
	},
}

1087
func getPrefixNew(req *cmds.Request) (cid.Builder, error) {
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	cidVer, cidVerSet := req.Options["cid-version"].(int)
	hashFunStr, hashFunSet := req.Options["hash"].(string)

	if !cidVerSet && !hashFunSet {
		return nil, nil
	}

	if hashFunSet && cidVer == 0 {
		cidVer = 1
	}

	prefix, err := dag.PrefixForCidVersion(cidVer)
	if err != nil {
		return nil, err
	}

	if hashFunSet {
		hashFunCode, ok := mh.Names[strings.ToLower(hashFunStr)]
		if !ok {
			return nil, fmt.Errorf("unrecognized hash function: %s", strings.ToLower(hashFunStr))
		}
		prefix.MhType = hashFunCode
		prefix.MhLength = -1
	}

	return &prefix, nil
}

1116
func getPrefix(req oldcmds.Request) (cid.Builder, error) {
1117
	cidVer, cidVerSet, _ := req.Option("cid-version").Int()
1118
	hashFunStr, hashFunSet, _ := req.Option("hash").String()
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131

	if !cidVerSet && !hashFunSet {
		return nil, nil
	}

	if hashFunSet && cidVer == 0 {
		cidVer = 1
	}

	prefix, err := dag.PrefixForCidVersion(cidVer)
	if err != nil {
		return nil, err
	}
Jeromy's avatar
Jeromy committed
1132

1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	if hashFunSet {
		hashFunCode, ok := mh.Names[strings.ToLower(hashFunStr)]
		if !ok {
			return nil, fmt.Errorf("unrecognized hash function: %s", strings.ToLower(hashFunStr))
		}
		prefix.MhType = hashFunCode
		prefix.MhLength = -1
	}

	return &prefix, nil
}

1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
func ensureContainingDirectoryExists(r *mfs.Root, path string, builder cid.Builder) error {
	dirtomake := gopath.Dir(path)

	if dirtomake == "/" {
		return nil
	}

	return mfs.Mkdir(r, dirtomake, mfs.MkdirOpts{
		Mkparents:  true,
		CidBuilder: builder,
	})
}

1158
func getFileHandle(r *mfs.Root, path string, create bool, builder cid.Builder) (*mfs.File, error) {
Jeromy's avatar
Jeromy committed
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
	target, err := mfs.Lookup(r, path)
	switch err {
	case nil:
		fi, ok := target.(*mfs.File)
		if !ok {
			return nil, fmt.Errorf("%s was not a file", path)
		}
		return fi, nil

	case os.ErrNotExist:
		if !create {
			return nil, err
		}

		// if create is specified and the file doesnt exist, we create the file
		dirname, fname := gopath.Split(path)
		pdiri, err := mfs.Lookup(r, dirname)
		if err != nil {
1177
			flog.Error("lookupfail ", dirname)
Jeromy's avatar
Jeromy committed
1178 1179 1180 1181 1182 1183
			return nil, err
		}
		pdir, ok := pdiri.(*mfs.Directory)
		if !ok {
			return nil, fmt.Errorf("%s was not a directory", dirname)
		}
1184 1185
		if builder == nil {
			builder = pdir.GetCidBuilder()
1186
		}
Jeromy's avatar
Jeromy committed
1187

1188
		nd := dag.NodeWithData(ft.FilePBData(nil, 0))
1189
		nd.SetCidBuilder(builder)
Jeromy's avatar
Jeromy committed
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
		err = pdir.AddChild(fname, nd)
		if err != nil {
			return nil, err
		}

		fsn, err := pdir.Child(fname)
		if err != nil {
			return nil, err
		}

Jeromy's avatar
Jeromy committed
1200 1201
		fi, ok := fsn.(*mfs.File)
		if !ok {
Michael Muré's avatar
Michael Muré committed
1202
			return nil, errors.New("expected *mfs.File, didnt get it. This is likely a race condition")
Jeromy's avatar
Jeromy committed
1203 1204
		}
		return fi, nil
Jeromy's avatar
Jeromy committed
1205 1206 1207 1208 1209

	default:
		return nil, err
	}
}
Jeromy's avatar
Jeromy committed
1210 1211 1212

func checkPath(p string) (string, error) {
	if len(p) == 0 {
Michael Muré's avatar
Michael Muré committed
1213
		return "", fmt.Errorf("paths must not be empty")
Jeromy's avatar
Jeromy committed
1214 1215 1216
	}

	if p[0] != '/' {
Michael Muré's avatar
Michael Muré committed
1217
		return "", fmt.Errorf("paths must start with a leading slash")
Jeromy's avatar
Jeromy committed
1218 1219 1220 1221 1222 1223 1224 1225
	}

	cleaned := gopath.Clean(p)
	if p[len(p)-1] == '/' && p != "/" {
		cleaned += "/"
	}
	return cleaned, nil
}