Skip to content
Snippets Groups Projects
Commit 3224ae09 authored by Jeromy Johnson's avatar Jeromy Johnson
Browse files

a small amount of cleanup in mfs dir


License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent faec2a32
No related branches found
No related tags found
1 merge request!1New
...@@ -101,45 +101,6 @@ func (d *Directory) Type() NodeType { ...@@ -101,45 +101,6 @@ func (d *Directory) Type() NodeType {
return TDir 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. // childNode returns a FSNode under this directory by the given name if it exists.
// it does *not* check the cached dirs and files // it does *not* check the cached dirs and files
func (d *Directory) childNode(name string) (FSNode, error) { func (d *Directory) childNode(name string) (FSNode, error) {
...@@ -172,6 +133,13 @@ func (d *Directory) childNode(name string) (FSNode, error) { ...@@ -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 // childFromDag searches through this directories dag node for a child link
// with the given name // with the given name
func (d *Directory) childFromDag(name string) (*dag.Node, error) { func (d *Directory) childFromDag(name string) (*dag.Node, error) {
...@@ -184,13 +152,6 @@ func (d *Directory) childFromDag(name string) (*dag.Node, error) { ...@@ -184,13 +152,6 @@ func (d *Directory) childFromDag(name string) (*dag.Node, error) {
return nil, os.ErrNotExist 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 // childUnsync returns the child under this directory by the given name
// without locking, useful for operations which already hold a lock // without locking, useful for operations which already hold a lock
func (d *Directory) childUnsync(name string) (FSNode, error) { func (d *Directory) childUnsync(name string) (FSNode, error) {
...@@ -258,13 +219,16 @@ func (d *Directory) Mkdir(name string) (*Directory, error) { ...@@ -258,13 +219,16 @@ func (d *Directory) Mkdir(name string) (*Directory, error) {
d.lock.Lock() d.lock.Lock()
defer d.lock.Unlock() defer d.lock.Unlock()
child, err := d.childDir(name) fsn, err := d.childUnsync(name)
if err == nil {
return child, os.ErrExist
}
_, err = d.childFile(name)
if err == nil { if err == nil {
switch fsn := fsn.(type) {
case *Directory:
return fsn, os.ErrExist
case *File:
return nil, os.ErrExist return nil, os.ErrExist
default:
return nil, fmt.Errorf("unrecognized type: %#v", fsn)
}
} }
ndir := &dag.Node{Data: ft.FolderPBData()} ndir := &dag.Node{Data: ft.FolderPBData()}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment