mfs: fix copying into directory with no given filename
License: MIT Signed-off-by: Jeromy <why@ipfs.io>
Jeromy committed
Jul 19, 2016 at 07:41 UTC
660a4a9d988f663c00c78f41e2a941d8dde64f78
3 files changed
+27
core/commands/files/files.go
+6
@@ -217,12 +217,18 @@ var FilesCpCmd = &cmds.Command{
217
res.SetError(err, cmds.ErrNormal)
218
return
219
}
220
+ src = strings.TrimRight(src, "/")
221
+
222
dst, err := checkPath(req.Arguments()[1])
223
if err != nil {
224
res.SetError(err, cmds.ErrNormal)
225
return
226
}
227
228
+ if dst[len(dst)-1] == '/' {
229
+ dst += gopath.Base(src)
230
+ }
231
+
232
nd, err := getNodeFromPath(req.Context(), node, src)
233
if err != nil {
234
res.SetError(err, cmds.ErrNormal)
mfs/ops.go
+3
@@ -89,6 +89,9 @@ func lookupDir(r *Root, path string) (*Directory, error) {
89
// PutNode inserts 'nd' at 'path' in the given mfs
90
func PutNode(r *Root, path string, nd *dag.Node) error {
91
dirp, filename := gopath.Split(path)
92
+ if filename == "" {
93
+ return fmt.Errorf("cannot create file with empty name")
94
+ }
95
96
pdir, err := lookupDir(r, dirp)
97
if err != nil {
test/sharness/t0250-files-api.sh
+18
@@ -460,6 +460,24 @@ test_files_api() {
460
ipfs files rm -r /test_dir &&
461
ipfs files rm -r /test_file
462
'
463
+
464
+ test_expect_success "make a directory and a file" '
465
+ ipfs files mkdir /adir &&
466
+ echo "blah" | ipfs files write --create /foobar
467
+ '
468
+
469
+ test_expect_success "copy a file into a directory" '
470
+ ipfs files cp /foobar /adir/
471
+ '
472
+
473
+ test_expect_success "file made it into directory" '
474
+ ipfs files ls /adir | grep foobar
475
+ '
476
+
477
+ test_expect_success "clean up" '
478
+ ipfs files rm -r /foobar &&
479
+ ipfs files rm -r /adir
480
+ '
481
}
482
483
# test offline and online