Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-mfs
Commits
94b38c2f
Unverified
Commit
94b38c2f
authored
May 09, 2019
by
Steven Allen
Committed by
GitHub
May 09, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #76 from ipfs/fix/73-mv-fails-to-clean-up
Fix directory mv and add tests
parents
30aee5fc
2b77b0a3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
131 additions
and
15 deletions
+131
-15
mfs_test.go
mfs_test.go
+116
-0
ops.go
ops.go
+15
-15
No files found.
mfs_test.go
View file @
94b38c2f
...
...
@@ -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()
...
...
@@ -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()
...
...
ops.go
View file @
94b38c2f
...
...
@@ -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)
srcDir
Name
, srcFname := gopath.Split(src)
var
dstDir
Str
string
var
file
name
string
var
dstDir
Name
string
var
dstF
name
string
if
dst[
len
(dst)-1] == '/' {
dstDir
Str
= dst
file
name = srcFname
dstDir
Name
= dst
dstF
name = srcFname
}
else
{
dstDir
Str, file
name = gopath.Split(dst)
dstDir
Name, dstF
name = gopath.Split(dst)
}
// get parent directories of both src and dest first
dstDir, err := lookupDir(r, dstDir
Str
)
dstDir, err := lookupDir(r, dstDir
Name
)
if
err != nil {
return
err
}
srcDir
Obj
, err := lookupDir(r, srcDir)
srcDir, err := lookupDir(r, srcDir
Name
)
if
err != nil {
return
err
}
srcObj, err := srcDir
Obj
.Child(srcFname)
srcObj, err := srcDir.Child(srcFname)
if
err != nil {
return
err
}
...
...
@@ -53,14 +53,14 @@ func Mv(r *Root, src, dst string) error {
return
err
}
fsn, err := dstDir.Child(
file
name)
fsn, err := dstDir.Child(
dstF
name)
if
err == nil {
switch
n := fsn.(
type
) {
case
*File:
_ = dstDir.Unlink(
file
name)
_ = dstDir.Unlink(
dstF
name)
case
*Directory:
dstDir = n
file
name = srcFname
dstF
name = srcFname
default
:
return
fmt.Errorf(
"unexpected type at path: %s"
, dst)
}
...
...
@@ -68,16 +68,16 @@ func Mv(r *Root, src, dst string) error {
return
err
}
err = dstDir.AddChild(
file
name, nd)
err = dstDir.AddChild(
dstF
name, nd)
if
err != nil {
return
err
}
if
srcDir == dstDir
Str
&& srcFname ==
file
name {
if
srcDir
.name
== dstDir
.name
&& srcFname ==
dstF
name {
return
nil
}
return
srcDir
Obj
.Unlink(srcFname)
return
srcDir.Unlink(srcFname)
}
func
lookupDir(r *Root, path
string
) (*Directory,
error
) {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment