Skip to content

Commit

Permalink
Ensure that flush on the mfs root flushes its directory
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
  • Loading branch information
whyrusleeping committed Dec 20, 2017
1 parent 5ad9f4d commit 4895033
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
18 changes: 15 additions & 3 deletions mfs/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (d *Directory) Unlink(name string) error {
}

func (d *Directory) Flush() error {
nd, err := d.GetNode()
nd, err := d.getNode(true)
if err != nil {
return err
}
Expand Down Expand Up @@ -368,7 +368,7 @@ func (d *Directory) AddChild(name string, nd node.Node) error {
return nil
}

func (d *Directory) sync() error {
func (d *Directory) sync(clear bool) error {
for name, dir := range d.childDirs {
nd, err := dir.GetNode()
if err != nil {
Expand All @@ -379,6 +379,10 @@ func (d *Directory) sync() error {
if err != nil {
return err
}

if clear {
delete(d.childDirs, name)
}
}

for name, file := range d.files {
Expand All @@ -391,6 +395,10 @@ func (d *Directory) sync() error {
if err != nil {
return err
}

if clear {
delete(d.files, name)
}
}

return nil
Expand All @@ -407,10 +415,14 @@ func (d *Directory) Path() string {
}

func (d *Directory) GetNode() (node.Node, error) {
return d.getNode(false)
}

func (d *Directory) getNode(clear bool) (node.Node, error) {
d.lock.Lock()
defer d.lock.Unlock()

err := d.sync()
err := d.sync(clear)
if err != nil {
return nil, err
}
Expand Down
9 changes: 7 additions & 2 deletions mfs/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,17 @@ func (kr *Root) GetValue() FSNode {
}

func (kr *Root) Flush() error {
nd, err := kr.GetValue().GetNode()
if err != nil {
v := kr.GetValue()
if err := v.Flush(); err != nil {
return err
}

if kr.repub != nil {
nd, err := v.GetNode()
if err != nil {
return err
}

kr.repub.Update(nd.Cid())
}
return nil
Expand Down

0 comments on commit 4895033

Please sign in to comment.