Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Fix directory mv and add tests #76

Merged
merged 1 commit into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions 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 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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only substantive change in this file and is the actual source of the bug revealed in go-ipfs sharness tests. Everything else is naming changes (which I can totally revert if it's too noisy).

return nil
}

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

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