Skip to content

Commit

Permalink
mfs: take the read/write lock *before* extracting the node.
Browse files Browse the repository at this point in the history
Otherwise, it could be exchanged out from under us!

fixes #4514

License: MIT
Signed-off-by: Steven Allen <[email protected]>
  • Loading branch information
Stebalien committed Dec 21, 2017
1 parent 9beaa38 commit e68ef05
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions mfs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,24 @@ func NewFile(name string, node node.Node, parent childCloser, dserv dag.DAGServi
return fi, nil
}

func (fi *File) Open(flags Flags) (FileDescriptor, error) {
func (fi *File) Open(flags Flags) (_ FileDescriptor, _retErr error) {
if flags.Write {
fi.desclock.Lock()
defer func() {
if _retErr != nil {
fi.desclock.Unlock()
}
}()
} else if flags.Read {
fi.desclock.RLock()
defer func() {
if _retErr != nil {
fi.desclock.Unlock()
}
}()
} else {
return nil, fmt.Errorf("file opened for neither reading nor writing")
}
fi.nodelk.Lock()
node := fi.node
fi.nodelk.Unlock()
Expand All @@ -66,14 +83,6 @@ func (fi *File) Open(flags Flags) (FileDescriptor, error) {
// Ok as well.
}

if flags.Write {
fi.desclock.Lock()
} else if flags.Read {
fi.desclock.RLock()
} else {
return nil, fmt.Errorf("file opened for neither reading nor writing")
}

dmod, err := mod.NewDagModifier(context.TODO(), node, fi.dserv, chunk.DefaultSplitter)
if err != nil {
return nil, err
Expand Down

0 comments on commit e68ef05

Please sign in to comment.