OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 class _Directory extends FileSystemEntity implements Directory { | 7 class _Directory extends FileSystemEntity implements Directory { |
8 static const CREATE_REQUEST = 0; | 8 static const CREATE_REQUEST = 0; |
9 static const DELETE_REQUEST = 1; | 9 static const DELETE_REQUEST = 1; |
10 static const EXISTS_REQUEST = 2; | 10 static const EXISTS_REQUEST = 2; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 } | 118 } |
119 } | 119 } |
120 if (future == null) { | 120 if (future == null) { |
121 return new Future.value(this); | 121 return new Future.value(this); |
122 } else { | 122 } else { |
123 return future.then((_) => this); | 123 return future.then((_) => this); |
124 } | 124 } |
125 }); | 125 }); |
126 } | 126 } |
127 | 127 |
128 Future<Directory> create({recursive: false}) { | 128 Future<Directory> create({bool recursive: false}) { |
129 if (recursive) return createRecursively(); | 129 if (recursive) return createRecursively(); |
130 return _IOService.dispatch(_DIRECTORY_CREATE, [path]).then((response) { | 130 return _IOService.dispatch(_DIRECTORY_CREATE, [path]).then((response) { |
131 if (_isErrorResponse(response)) { | 131 if (_isErrorResponse(response)) { |
132 throw _exceptionOrErrorFromResponse(response, "Creation failed"); | 132 throw _exceptionOrErrorFromResponse(response, "Creation failed"); |
133 } | 133 } |
134 return this; | 134 return this; |
135 }); | 135 }); |
136 } | 136 } |
137 | 137 |
138 void createRecursivelySync() { | 138 void createRecursivelySync() { |
139 var path = new _Path(this.path); | 139 var path = new _Path(this.path); |
140 var dirsToCreate = []; | 140 var dirsToCreate = []; |
141 var terminator = path.isAbsolute ? '/' : ''; | 141 var terminator = path.isAbsolute ? '/' : ''; |
142 while (path.toString() != terminator) { | 142 while (path.toString() != terminator) { |
143 var dir = new Directory(path.toNativePath()); | 143 var dir = new Directory(path.toNativePath()); |
144 if (dir.existsSync()) break; | 144 if (dir.existsSync()) break; |
145 dirsToCreate.add(dir); | 145 dirsToCreate.add(dir); |
146 path = path.directoryPath; | 146 path = path.directoryPath; |
147 } | 147 } |
148 for (var i = dirsToCreate.length - 1; i >= 0; i--) { | 148 for (var i = dirsToCreate.length - 1; i >= 0; i--) { |
149 dirsToCreate[i].createSync(); | 149 dirsToCreate[i].createSync(); |
150 } | 150 } |
151 } | 151 } |
152 | 152 |
153 void createSync({recursive: false}) { | 153 void createSync({bool recursive: false}) { |
154 if (recursive) return createRecursivelySync(); | 154 if (recursive) return createRecursivelySync(); |
155 var result = _create(path); | 155 var result = _create(path); |
156 if (result is OSError) { | 156 if (result is OSError) { |
157 throw new DirectoryException("Creation failed", path, result); | 157 throw new DirectoryException("Creation failed", path, result); |
158 } | 158 } |
159 } | 159 } |
160 | 160 |
161 Future<Directory> createTemp() { | 161 Future<Directory> createTemp() { |
162 return _IOService.dispatch(_DIRECTORY_CREATE_TEMP, [path]).then((response) { | 162 return _IOService.dispatch(_DIRECTORY_CREATE_TEMP, [path]).then((response) { |
163 if (_isErrorResponse(response)) { | 163 if (_isErrorResponse(response)) { |
164 throw _exceptionOrErrorFromResponse( | 164 throw _exceptionOrErrorFromResponse( |
165 response, "Creation of temporary directory failed"); | 165 response, "Creation of temporary directory failed"); |
166 } | 166 } |
167 return new Directory(response); | 167 return new Directory(response); |
168 }); | 168 }); |
169 } | 169 } |
170 | 170 |
171 Directory createTempSync() { | 171 Directory createTempSync() { |
172 var result = _createTemp(path); | 172 var result = _createTemp(path); |
173 if (result is OSError) { | 173 if (result is OSError) { |
174 throw new DirectoryException("Creation of temporary directory failed", | 174 throw new DirectoryException("Creation of temporary directory failed", |
175 path, | 175 path, |
176 result); | 176 result); |
177 } | 177 } |
178 return new Directory(result); | 178 return new Directory(result); |
179 } | 179 } |
180 | 180 |
181 Future<Directory> _delete({recursive: false}) { | 181 Future<Directory> _delete({bool recursive: false}) { |
182 return _IOService.dispatch(_DIRECTORY_DELETE, [path, recursive]) | 182 return _IOService.dispatch(_DIRECTORY_DELETE, [path, recursive]) |
183 .then((response) { | 183 .then((response) { |
184 if (_isErrorResponse(response)) { | 184 if (_isErrorResponse(response)) { |
185 throw _exceptionOrErrorFromResponse(response, "Deletion failed"); | 185 throw _exceptionOrErrorFromResponse(response, "Deletion failed"); |
186 } | 186 } |
187 return this; | 187 return this; |
188 }); | 188 }); |
189 } | 189 } |
190 | 190 |
191 void _deleteSync({recursive: false}) { | 191 void _deleteSync({bool recursive: false}) { |
192 var result = _deleteNative(path, recursive); | 192 var result = _deleteNative(path, recursive); |
193 if (result is OSError) { | 193 if (result is OSError) { |
194 throw new DirectoryException("Deletion failed", path, result); | 194 throw new DirectoryException("Deletion failed", path, result); |
195 } | 195 } |
196 } | 196 } |
197 | 197 |
198 Future<Directory> rename(String newPath) { | 198 Future<Directory> rename(String newPath) { |
199 return _IOService.dispatch(_DIRECTORY_RENAME, [path, newPath]) | 199 return _IOService.dispatch(_DIRECTORY_RENAME, [path, newPath]) |
200 .then((response) { | 200 .then((response) { |
201 if (_isErrorResponse(response)) { | 201 if (_isErrorResponse(response)) { |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 controller.addError( | 377 controller.addError( |
378 new DirectoryException("Directory listing failed", | 378 new DirectoryException("Directory listing failed", |
379 errorPath, | 379 errorPath, |
380 err)); | 380 err)); |
381 } else { | 381 } else { |
382 controller.addError( | 382 controller.addError( |
383 new DirectoryException("Internal error")); | 383 new DirectoryException("Internal error")); |
384 } | 384 } |
385 } | 385 } |
386 } | 386 } |
OLD | NEW |