a small amount of cleanup in mfs dir
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Jan 11, 2016 at 04:15 UTC
3224ae091769eb47f67f1e4cfe1b4622eabb8fe9
1 file changed
+16
-52
mfs/dir.go
+16
-52
@@ -101,45 +101,6 @@ func (d *Directory) Type() NodeType {
101
return TDir
102
}
103
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
-
104
// childNode returns a FSNode under this directory by the given name if it exists.
105
// it does *not* check the cached dirs and files
106
func (d *Directory) childNode(name string) (FSNode, error) {
@@ -172,6 +133,13 @@ func (d *Directory) childNode(name string) (FSNode, error) {
133
}
134
}
135
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
+
143
// childFromDag searches through this directories dag node for a child link
144
// with the given name
145
func (d *Directory) childFromDag(name string) (*dag.Node, error) {
@@ -184,13 +152,6 @@ func (d *Directory) childFromDag(name string) (*dag.Node, error) {
152
return nil, os.ErrNotExist
153
}
154
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
-
155
// childUnsync returns the child under this directory by the given name
156
// without locking, useful for operations which already hold a lock
157
func (d *Directory) childUnsync(name string) (FSNode, error) {
@@ -258,13 +219,16 @@ func (d *Directory) Mkdir(name string) (*Directory, error) {
219
d.lock.Lock()
220
defer d.lock.Unlock()
221
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)
223
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
+ }
232
}
233
234
ndir := &dag.Node{Data: ft.FolderPBData()}