提交 9aea2c78 作者: Jeromy

fix shared node reference issue

License: MIT
Signed-off-by: 's avatarJeromy <jeromyj@gmail.com>
上级 871cc6f1
......@@ -258,9 +258,9 @@ func (d *Directory) Mkdir(name string) (*Directory, error) {
d.lock.Lock()
defer d.lock.Unlock()
_, err := d.childDir(name)
child, err := d.childDir(name)
if err == nil {
return nil, os.ErrExist
return child, os.ErrExist
}
_, err = d.childFile(name)
if err == nil {
......@@ -395,7 +395,7 @@ func (d *Directory) GetNode() (*dag.Node, error) {
return nil, err
}
return d.node, nil
return d.node.Copy(), nil
}
func (d *Directory) Lock() {
......
......@@ -65,6 +65,7 @@ func (fi *File) Close() error {
if fi.hasChanges {
err := fi.mod.Sync()
if err != nil {
fi.Unlock()
return err
}
......@@ -74,6 +75,7 @@ func (fi *File) Close() error {
// it will manage the lock for us
return fi.flushUp()
}
fi.Unlock()
return nil
}
......@@ -93,12 +95,13 @@ func (fi *File) flushUp() error {
return err
}
name := fi.name
parent := fi.parent
//name := fi.name
//parent := fi.parent
// explicit unlock *only* before closeChild call
fi.Unlock()
return parent.closeChild(name, nd)
return nil
//return parent.closeChild(name, nd)
}
// Sync flushes the changes in the file to disk
......
......@@ -576,10 +576,56 @@ func actorRemoveFile(d *Directory) error {
return d.Unlink(re.Name)
}
func actorReadFile(d *Directory) error {
d, err := randomWalk(d, rand.Intn(6))
if err != nil {
return err
}
ents, err := d.List()
if err != nil {
return err
}
var files []string
for _, e := range ents {
if e.Type == int(TFile) {
files = append(files, e.Name)
}
}
if len(files) == 0 {
return nil
}
fname := files[rand.Intn(len(files))]
fsn, err := d.Child(fname)
if err != nil {
return err
}
fi, ok := fsn.(*File)
if !ok {
return errors.New("file wasnt a file, race?")
}
_, err = fi.Size()
if err != nil {
return err
}
_, err = ioutil.ReadAll(fi)
if err != nil {
return err
}
return fi.Close()
}
func testActor(rt *Root, iterations int, errs chan error) {
d := rt.GetValue().(*Directory)
for i := 0; i < iterations; i++ {
switch rand.Intn(4) {
switch rand.Intn(5) {
case 0:
if err := actorMkdir(d); err != nil {
errs <- err
......@@ -591,10 +637,20 @@ func testActor(rt *Root, iterations int, errs chan error) {
return
}
case 3:
continue
// randomly deleting things
// doesnt really give us any sort of useful test results.
// you will never have this in a real environment where
// you expect anything productive to happen...
if err := actorRemoveFile(d); err != nil {
errs <- err
return
}
case 4:
if err := actorReadFile(d); err != nil {
errs <- err
return
}
}
}
errs <- nil
......@@ -605,7 +661,7 @@ func TestMfsStress(t *testing.T) {
defer cancel()
_, rt := setupRoot(ctx, t)
numroutines := 2
numroutines := 10
errs := make(chan error)
for i := 0; i < numroutines; i++ {
......
......@@ -99,8 +99,8 @@ func PutNode(r *Root, path string, nd *dag.Node) error {
}
// Mkdir creates a directory at 'path' under the directory 'd', creating
// intermediary directories as needed if 'parents' is set to true
func Mkdir(r *Root, pth string, parents bool, flush bool) error {
// intermediary directories as needed if 'mkparents' is set to true
func Mkdir(r *Root, pth string, mkparents bool, flush bool) error {
if pth == "" {
return nil
}
......@@ -116,7 +116,7 @@ func Mkdir(r *Root, pth string, parents bool, flush bool) error {
if len(parts) == 0 {
// this will only happen on 'mkdir /'
if parents {
if mkparents {
return nil
}
return fmt.Errorf("cannot create directory '/': Already exists")
......@@ -125,7 +125,7 @@ func Mkdir(r *Root, pth string, parents bool, flush bool) error {
cur := r.GetValue().(*Directory)
for i, d := range parts[:len(parts)-1] {
fsn, err := cur.Child(d)
if err == os.ErrNotExist && parents {
if err == os.ErrNotExist && mkparents {
mkd, err := cur.Mkdir(d)
if err != nil {
return err
......@@ -144,7 +144,7 @@ func Mkdir(r *Root, pth string, parents bool, flush bool) error {
final, err := cur.Mkdir(parts[len(parts)-1])
if err != nil {
if !parents || err != os.ErrExist {
if !mkparents || err != os.ErrExist || final == nil {
return err
}
}
......
......@@ -352,6 +352,31 @@ test_files_api() {
test_expect_success "cleanup looks good" '
verify_dir_contents /
'
# test flush flags
test_expect_success "mkdir --flush works" '
ipfs files mkdir --flush --parents /flushed/deep
'
test_expect_success "mkdir --flush works a second time" '
ipfs files mkdir --flush --parents /flushed/deep
'
test_expect_success "dir looks right" '
verify_dir_contents / flushed
'
test_expect_success "child dir looks right" '
verify_dir_contents /flushed deep
'
test_expect_success "cleanup" '
ipfs files rm -r /flushed
'
test_expect_success "child dir looks right" '
verify_dir_contents /
'
}
# test offline and online
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论