@@ -101,45 +101,6 @@ func (d *Directory) Type() NodeType {
101
101
return TDir
102
102
}
103
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
-
143
104
// childNode returns a FSNode under this directory by the given name if it exists.
144
105
// it does *not* check the cached dirs and files
145
106
func (d * Directory ) childNode (name string ) (FSNode , error ) {
@@ -172,6 +133,13 @@ func (d *Directory) childNode(name string) (FSNode, error) {
172
133
}
173
134
}
174
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
+
175
143
// childFromDag searches through this directories dag node for a child link
176
144
// with the given name
177
145
func (d * Directory ) childFromDag (name string ) (* dag.Node , error ) {
@@ -184,13 +152,6 @@ func (d *Directory) childFromDag(name string) (*dag.Node, error) {
184
152
return nil , os .ErrNotExist
185
153
}
186
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
-
194
155
// childUnsync returns the child under this directory by the given name
195
156
// without locking, useful for operations which already hold a lock
196
157
func (d * Directory ) childUnsync (name string ) (FSNode , error ) {
@@ -258,13 +219,16 @@ func (d *Directory) Mkdir(name string) (*Directory, error) {
258
219
d .lock .Lock ()
259
220
defer d .lock .Unlock ()
260
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 )
266
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
+ }
268
232
}
269
233
270
234
ndir := & dag.Node {Data : ft .FolderPBData ()}
0 commit comments