Skip to content

Commit

Permalink
Merge pull request ipfs/go-mfs#76 from ipfs/fix/73-mv-fails-to-clean-up
Browse files Browse the repository at this point in the history
Fix directory mv and add tests

This commit was moved from ipfs/go-mfs@94b38c2
  • Loading branch information
Stebalien authored May 9, 2019
2 parents bebcce3 + a5254e2 commit 6ec227e
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 15 deletions.
116 changes: 116 additions & 0 deletions mfs/mfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ func mkdirP(t *testing.T, root *Directory, pth string) *Directory {
return cur
}

func assertDirNotAtPath(root *Directory, pth string) error {
_, err := DirLookup(root, pth)
if err == nil {
return fmt.Errorf("%s exists in %s", pth, root.name)
}
return nil
}

func assertDirAtPath(root *Directory, pth string, children []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -370,6 +378,114 @@ func TestDirectoryLoadFromDag(t *testing.T) {
}
}

func TestMvFile(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dagService, rt := setupRoot(ctx, t)
rootDir := rt.GetDirectory()

fi := getRandFile(t, dagService, 1000)

err := rootDir.AddChild("afile", fi)
if err != nil {
t.Fatal(err)
}

err = Mv(rt, "/afile", "/bfile")
if err != nil {
t.Fatal(err)
}

err = assertFileAtPath(dagService, rootDir, fi, "bfile")
if err != nil {
t.Fatal(err)
}
}

func TestMvFileToSubdir(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dagService, rt := setupRoot(ctx, t)
rootDir := rt.GetDirectory()

_ = mkdirP(t, rootDir, "test1")

fi := getRandFile(t, dagService, 1000)

err := rootDir.AddChild("afile", fi)
if err != nil {
t.Fatal(err)
}

err = Mv(rt, "/afile", "/test1")
if err != nil {
t.Fatal(err)
}

err = assertFileAtPath(dagService, rootDir, fi, "test1/afile")
if err != nil {
t.Fatal(err)
}
}

func TestMvFileToSubdirWithRename(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dagService, rt := setupRoot(ctx, t)
rootDir := rt.GetDirectory()

_ = mkdirP(t, rootDir, "test1")

fi := getRandFile(t, dagService, 1000)

err := rootDir.AddChild("afile", fi)
if err != nil {
t.Fatal(err)
}

err = Mv(rt, "/afile", "/test1/bfile")
if err != nil {
t.Fatal(err)
}

err = assertFileAtPath(dagService, rootDir, fi, "test1/bfile")
if err != nil {
t.Fatal(err)
}
}

func TestMvDir(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dagService, rt := setupRoot(ctx, t)
rootDir := rt.GetDirectory()

_ = mkdirP(t, rootDir, "test1")
d2 := mkdirP(t, rootDir, "test2")

fi := getRandFile(t, dagService, 1000)

err := d2.AddChild("afile", fi)
if err != nil {
t.Fatal(err)
}

err = Mv(rt, "/test2", "/test1")
if err != nil {
t.Fatal(err)
}

err = assertDirNotAtPath(rootDir, "test2")
if err != nil {
t.Fatal(err)
}

err = assertFileAtPath(dagService, rootDir, fi, "test1/test2/afile")
if err != nil {
t.Fatal(err)
}
}

func TestMfsFile(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
30 changes: 15 additions & 15 deletions mfs/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ import (
// Mv moves the file or directory at 'src' to 'dst'
// TODO: Document what the strings 'src' and 'dst' represent.
func Mv(r *Root, src, dst string) error {
srcDir, srcFname := gopath.Split(src)
srcDirName, srcFname := gopath.Split(src)

var dstDirStr string
var filename string
var dstDirName string
var dstFname string
if dst[len(dst)-1] == '/' {
dstDirStr = dst
filename = srcFname
dstDirName = dst
dstFname = srcFname
} else {
dstDirStr, filename = gopath.Split(dst)
dstDirName, dstFname = gopath.Split(dst)
}

// get parent directories of both src and dest first
dstDir, err := lookupDir(r, dstDirStr)
dstDir, err := lookupDir(r, dstDirName)
if err != nil {
return err
}

srcDirObj, err := lookupDir(r, srcDir)
srcDir, err := lookupDir(r, srcDirName)
if err != nil {
return err
}

srcObj, err := srcDirObj.Child(srcFname)
srcObj, err := srcDir.Child(srcFname)
if err != nil {
return err
}
Expand All @@ -53,31 +53,31 @@ func Mv(r *Root, src, dst string) error {
return err
}

fsn, err := dstDir.Child(filename)
fsn, err := dstDir.Child(dstFname)
if err == nil {
switch n := fsn.(type) {
case *File:
_ = dstDir.Unlink(filename)
_ = dstDir.Unlink(dstFname)
case *Directory:
dstDir = n
filename = srcFname
dstFname = srcFname
default:
return fmt.Errorf("unexpected type at path: %s", dst)
}
} else if err != os.ErrNotExist {
return err
}

err = dstDir.AddChild(filename, nd)
err = dstDir.AddChild(dstFname, nd)
if err != nil {
return err
}

if srcDir == dstDirStr && srcFname == filename {
if srcDir.name == dstDir.name && srcFname == dstFname {
return nil
}

return srcDirObj.Unlink(srcFname)
return srcDir.Unlink(srcFname)
}

func lookupDir(r *Root, path string) (*Directory, error) {
Expand Down

0 comments on commit 6ec227e

Please sign in to comment.