Skip to content

Commit

Permalink
a small amount of cleanup in mfs dir
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 Jan 12, 2016
1 parent faec2a3 commit 3224ae0
Showing 1 changed file with 16 additions and 52 deletions.
68 changes: 16 additions & 52 deletions mfs/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,45 +101,6 @@ func (d *Directory) Type() NodeType {
return TDir
}

// childFile returns a file under this directory by the given name if it exists
func (d *Directory) childFile(name string) (*File, error) {
fi, ok := d.files[name]
if ok {
return fi, nil
}

fsn, err := d.childNode(name)
if err != nil {
return nil, err
}

if fi, ok := fsn.(*File); ok {
return fi, nil
}

return nil, fmt.Errorf("%s is not a file", name)
}

// childDir returns a directory under this directory by the given name if it
// exists.
func (d *Directory) childDir(name string) (*Directory, error) {
dir, ok := d.childDirs[name]
if ok {
return dir, nil
}

fsn, err := d.childNode(name)
if err != nil {
return nil, err
}

if dir, ok := fsn.(*Directory); ok {
return dir, nil
}

return nil, fmt.Errorf("%s is not a directory", name)
}

// childNode returns a FSNode under this directory by the given name if it exists.
// it does *not* check the cached dirs and files
func (d *Directory) childNode(name string) (FSNode, error) {
Expand Down Expand Up @@ -172,6 +133,13 @@ func (d *Directory) childNode(name string) (FSNode, error) {
}
}

// Child returns the child of this directory by the given name
func (d *Directory) Child(name string) (FSNode, error) {
d.lock.Lock()
defer d.lock.Unlock()
return d.childUnsync(name)
}

// childFromDag searches through this directories dag node for a child link
// with the given name
func (d *Directory) childFromDag(name string) (*dag.Node, error) {
Expand All @@ -184,13 +152,6 @@ func (d *Directory) childFromDag(name string) (*dag.Node, error) {
return nil, os.ErrNotExist
}

// Child returns the child of this directory by the given name
func (d *Directory) Child(name string) (FSNode, error) {
d.lock.Lock()
defer d.lock.Unlock()
return d.childUnsync(name)
}

// childUnsync returns the child under this directory by the given name
// without locking, useful for operations which already hold a lock
func (d *Directory) childUnsync(name string) (FSNode, error) {
Expand Down Expand Up @@ -258,13 +219,16 @@ func (d *Directory) Mkdir(name string) (*Directory, error) {
d.lock.Lock()
defer d.lock.Unlock()

child, err := d.childDir(name)
if err == nil {
return child, os.ErrExist
}
_, err = d.childFile(name)
fsn, err := d.childUnsync(name)
if err == nil {
return nil, os.ErrExist
switch fsn := fsn.(type) {
case *Directory:
return fsn, os.ErrExist
case *File:
return nil, os.ErrExist
default:
return nil, fmt.Errorf("unrecognized type: %#v", fsn)
}
}

ndir := &dag.Node{Data: ft.FolderPBData()}
Expand Down

0 comments on commit 3224ae0

Please sign in to comment.