Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e10d210

Browse files
committedJan 11, 2016
a small amount of cleanup in mfs dir
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent f2e4208 commit e10d210

File tree

1 file changed

+16
-52
lines changed

1 file changed

+16
-52
lines changed
 

‎mfs/dir.go

+16-52
Original file line numberDiff line numberDiff line change
@@ -101,45 +101,6 @@ func (d *Directory) Type() NodeType {
101101
return TDir
102102
}
103103

104-
// childFile returns a file under this directory by the given name if it exists
105-
func (d *Directory) childFile(name string) (*File, error) {
106-
fi, ok := d.files[name]
107-
if ok {
108-
return fi, nil
109-
}
110-
111-
fsn, err := d.childNode(name)
112-
if err != nil {
113-
return nil, err
114-
}
115-
116-
if fi, ok := fsn.(*File); ok {
117-
return fi, nil
118-
}
119-
120-
return nil, fmt.Errorf("%s is not a file", name)
121-
}
122-
123-
// childDir returns a directory under this directory by the given name if it
124-
// exists.
125-
func (d *Directory) childDir(name string) (*Directory, error) {
126-
dir, ok := d.childDirs[name]
127-
if ok {
128-
return dir, nil
129-
}
130-
131-
fsn, err := d.childNode(name)
132-
if err != nil {
133-
return nil, err
134-
}
135-
136-
if dir, ok := fsn.(*Directory); ok {
137-
return dir, nil
138-
}
139-
140-
return nil, fmt.Errorf("%s is not a directory", name)
141-
}
142-
143104
// childNode returns a FSNode under this directory by the given name if it exists.
144105
// it does *not* check the cached dirs and files
145106
func (d *Directory) childNode(name string) (FSNode, error) {
@@ -172,6 +133,13 @@ func (d *Directory) childNode(name string) (FSNode, error) {
172133
}
173134
}
174135

136+
// Child returns the child of this directory by the given name
137+
func (d *Directory) Child(name string) (FSNode, error) {
138+
d.lock.Lock()
139+
defer d.lock.Unlock()
140+
return d.childUnsync(name)
141+
}
142+
175143
// childFromDag searches through this directories dag node for a child link
176144
// with the given name
177145
func (d *Directory) childFromDag(name string) (*dag.Node, error) {
@@ -184,13 +152,6 @@ func (d *Directory) childFromDag(name string) (*dag.Node, error) {
184152
return nil, os.ErrNotExist
185153
}
186154

187-
// Child returns the child of this directory by the given name
188-
func (d *Directory) Child(name string) (FSNode, error) {
189-
d.lock.Lock()
190-
defer d.lock.Unlock()
191-
return d.childUnsync(name)
192-
}
193-
194155
// childUnsync returns the child under this directory by the given name
195156
// without locking, useful for operations which already hold a lock
196157
func (d *Directory) childUnsync(name string) (FSNode, error) {
@@ -258,13 +219,16 @@ func (d *Directory) Mkdir(name string) (*Directory, error) {
258219
d.lock.Lock()
259220
defer d.lock.Unlock()
260221

261-
child, err := d.childDir(name)
262-
if err == nil {
263-
return child, os.ErrExist
264-
}
265-
_, err = d.childFile(name)
222+
fsn, err := d.childUnsync(name)
266223
if err == nil {
267-
return nil, os.ErrExist
224+
switch fsn := fsn.(type) {
225+
case *Directory:
226+
return fsn, os.ErrExist
227+
case *File:
228+
return nil, os.ErrExist
229+
default:
230+
return nil, fmt.Errorf("unrecognized type: %#v", fsn)
231+
}
268232
}
269233

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

0 commit comments

Comments
 (0)
Please sign in to comment.