| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 if (path is Directory) path = path.path; | 45 if (path is Directory) path = path.path; |
| 46 var result = _setCurrent(path); | 46 var result = _setCurrent(path); |
| 47 if (result is ArgumentError) throw result; | 47 if (result is ArgumentError) throw result; |
| 48 if (result is OSError) { | 48 if (result is OSError) { |
| 49 throw new DirectoryException( | 49 throw new DirectoryException( |
| 50 "Setting current working directory failed", path, result); | 50 "Setting current working directory failed", path, result); |
| 51 } | 51 } |
| 52 } | 52 } |
| 53 | 53 |
| 54 Future<bool> exists() { | 54 Future<bool> exists() { |
| 55 return IOService.dispatch(DIRECTORY_EXISTS, [path]).then((response) { | 55 return _IOService.dispatch(_DIRECTORY_EXISTS, [path]).then((response) { |
| 56 if (_isErrorResponse(response)) { | 56 if (_isErrorResponse(response)) { |
| 57 throw _exceptionOrErrorFromResponse(response, "Exists failed"); | 57 throw _exceptionOrErrorFromResponse(response, "Exists failed"); |
| 58 } | 58 } |
| 59 return response == 1; | 59 return response == 1; |
| 60 }); | 60 }); |
| 61 } | 61 } |
| 62 | 62 |
| 63 bool existsSync() { | 63 bool existsSync() { |
| 64 var result = _exists(path); | 64 var result = _exists(path); |
| 65 if (result is OSError) { | 65 if (result is OSError) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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({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 = []; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 152 | 152 |
| 153 void createSync({recursive: false}) { | 153 void createSync({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({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({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)) { |
| 202 throw _exceptionOrErrorFromResponse(response, "Rename failed"); | 202 throw _exceptionOrErrorFromResponse(response, "Rename failed"); |
| 203 } | 203 } |
| 204 return new Directory(newPath); | 204 return new Directory(newPath); |
| 205 }); | 205 }); |
| 206 } | 206 } |
| 207 | 207 |
| 208 Directory renameSync(String newPath) { | 208 Directory renameSync(String newPath) { |
| 209 if (newPath is !String) { | 209 if (newPath is !String) { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 bool this.followLinks) { | 282 bool this.followLinks) { |
| 283 controller = new StreamController(onListen: onListen, | 283 controller = new StreamController(onListen: onListen, |
| 284 onResume: onResume, | 284 onResume: onResume, |
| 285 onCancel: onCancel, | 285 onCancel: onCancel, |
| 286 sync: true); | 286 sync: true); |
| 287 } | 287 } |
| 288 | 288 |
| 289 Stream get stream => controller.stream; | 289 Stream get stream => controller.stream; |
| 290 | 290 |
| 291 void onListen() { | 291 void onListen() { |
| 292 IOService.dispatch(DIRECTORY_LIST_START, [path, recursive, followLinks]) | 292 _IOService.dispatch(_DIRECTORY_LIST_START, [path, recursive, followLinks]) |
| 293 .then((response) { | 293 .then((response) { |
| 294 if (response is int) { | 294 if (response is int) { |
| 295 id = response; | 295 id = response; |
| 296 next(); | 296 next(); |
| 297 } else { | 297 } else { |
| 298 error(response); | 298 error(response); |
| 299 controller.close(); | 299 controller.close(); |
| 300 } | 300 } |
| 301 }); | 301 }); |
| 302 } | 302 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 315 | 315 |
| 316 void next() { | 316 void next() { |
| 317 if (canceled) { | 317 if (canceled) { |
| 318 close(); | 318 close(); |
| 319 return; | 319 return; |
| 320 } | 320 } |
| 321 if (id == null) return; | 321 if (id == null) return; |
| 322 if (controller.isPaused) return; | 322 if (controller.isPaused) return; |
| 323 assert(!nextRunning); | 323 assert(!nextRunning); |
| 324 nextRunning = true; | 324 nextRunning = true; |
| 325 IOService.dispatch(DIRECTORY_LIST_NEXT, [id]).then((result) { | 325 _IOService.dispatch(_DIRECTORY_LIST_NEXT, [id]).then((result) { |
| 326 if (result is List) { | 326 if (result is List) { |
| 327 assert(result.length % 2 == 0); | 327 assert(result.length % 2 == 0); |
| 328 for (int i = 0; i < result.length; i++) { | 328 for (int i = 0; i < result.length; i++) { |
| 329 assert(i % 2 == 0); | 329 assert(i % 2 == 0); |
| 330 switch (result[i++]) { | 330 switch (result[i++]) { |
| 331 case LIST_FILE: | 331 case LIST_FILE: |
| 332 controller.add(new File(result[i])); | 332 controller.add(new File(result[i])); |
| 333 break; | 333 break; |
| 334 case LIST_DIRECTORY: | 334 case LIST_DIRECTORY: |
| 335 controller.add(new Directory(result[i])); | 335 controller.add(new Directory(result[i])); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 350 } | 350 } |
| 351 nextRunning = false; | 351 nextRunning = false; |
| 352 next(); | 352 next(); |
| 353 }); | 353 }); |
| 354 } | 354 } |
| 355 | 355 |
| 356 void close() { | 356 void close() { |
| 357 if (closed) return; | 357 if (closed) return; |
| 358 if (id == null) return; | 358 if (id == null) return; |
| 359 closed = true; | 359 closed = true; |
| 360 IOService.dispatch(DIRECTORY_LIST_STOP, [id]).then((_) { | 360 _IOService.dispatch(_DIRECTORY_LIST_STOP, [id]).then((_) { |
| 361 controller.close(); | 361 controller.close(); |
| 362 }); | 362 }); |
| 363 } | 363 } |
| 364 | 364 |
| 365 void error(message) { | 365 void error(message) { |
| 366 var errorType = | 366 var errorType = |
| 367 message[RESPONSE_ERROR][_ERROR_RESPONSE_ERROR_TYPE]; | 367 message[RESPONSE_ERROR][_ERROR_RESPONSE_ERROR_TYPE]; |
| 368 if (errorType == _ILLEGAL_ARGUMENT_RESPONSE) { | 368 if (errorType == _ILLEGAL_ARGUMENT_RESPONSE) { |
| 369 controller.addError(new ArgumentError()); | 369 controller.addError(new ArgumentError()); |
| 370 } else if (errorType == _OSERROR_RESPONSE) { | 370 } else if (errorType == _OSERROR_RESPONSE) { |
| 371 var responseError = message[RESPONSE_ERROR]; | 371 var responseError = message[RESPONSE_ERROR]; |
| 372 var err = new OSError( | 372 var err = new OSError( |
| 373 responseError[_OSERROR_RESPONSE_MESSAGE], | 373 responseError[_OSERROR_RESPONSE_MESSAGE], |
| 374 responseError[_OSERROR_RESPONSE_ERROR_CODE]); | 374 responseError[_OSERROR_RESPONSE_ERROR_CODE]); |
| 375 var errorPath = message[RESPONSE_PATH]; | 375 var errorPath = message[RESPONSE_PATH]; |
| 376 if (errorPath == null) errorPath = path; | 376 if (errorPath == null) errorPath = path; |
| 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 |