| 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; |
| 11 static const CREATE_TEMP_REQUEST = 3; | 11 static const CREATE_TEMP_REQUEST = 3; |
| 12 static const LIST_START_REQUEST = 4; | 12 static const LIST_START_REQUEST = 4; |
| 13 static const LIST_NEXT_REQUEST = 5; | 13 static const LIST_NEXT_REQUEST = 5; |
| 14 static const LIST_STOP_REQUEST = 6; | 14 static const LIST_STOP_REQUEST = 6; |
| 15 static const RENAME_REQUEST = 7; | 15 static const RENAME_REQUEST = 7; |
| 16 | 16 |
| 17 final String path; | 17 final String path; |
| 18 SendPort _directoryService; | |
| 19 | 18 |
| 20 _Directory(String this.path) { | 19 _Directory(String this.path) { |
| 21 if (path is! String) { | 20 if (path is! String) { |
| 22 throw new ArgumentError('${Error.safeToString(path)} ' | 21 throw new ArgumentError('${Error.safeToString(path)} ' |
| 23 'is not a String'); | 22 'is not a String'); |
| 24 } | 23 } |
| 25 } | 24 } |
| 26 | 25 |
| 27 external static _current(); | 26 external static _current(); |
| 28 external static _setCurrent(path); | 27 external static _setCurrent(path); |
| 29 external static _createTemp(String template); | 28 external static _createTemp(String template); |
| 30 external static int _exists(String path); | 29 external static int _exists(String path); |
| 31 external static _create(String path); | 30 external static _create(String path); |
| 32 external static _deleteNative(String path, bool recursive); | 31 external static _deleteNative(String path, bool recursive); |
| 33 external static _rename(String path, String newPath); | 32 external static _rename(String path, String newPath); |
| 34 external static List _list(String path, bool recursive, bool followLinks); | 33 external static List _list(String path, bool recursive, bool followLinks); |
| 35 external static SendPort _newServicePort(); | |
| 36 | 34 |
| 37 static Directory get current { | 35 static Directory get current { |
| 38 var result = _current(); | 36 var result = _current(); |
| 39 if (result is OSError) { | 37 if (result is OSError) { |
| 40 throw new DirectoryException( | 38 throw new DirectoryException( |
| 41 "Getting current working directory failed", "", result); | 39 "Getting current working directory failed", "", result); |
| 42 } | 40 } |
| 43 return new _Directory(result); | 41 return new _Directory(result); |
| 44 } | 42 } |
| 45 | 43 |
| 46 static void set current(path) { | 44 static void set current(path) { |
| 47 if (path is Directory) path = path.path; | 45 if (path is Directory) path = path.path; |
| 48 var result = _setCurrent(path); | 46 var result = _setCurrent(path); |
| 49 if (result is ArgumentError) throw result; | 47 if (result is ArgumentError) throw result; |
| 50 if (result is OSError) { | 48 if (result is OSError) { |
| 51 throw new DirectoryException( | 49 throw new DirectoryException( |
| 52 "Setting current working directory failed", path, result); | 50 "Setting current working directory failed", path, result); |
| 53 } | 51 } |
| 54 } | 52 } |
| 55 | 53 |
| 56 Future<bool> exists() { | 54 Future<bool> exists() { |
| 57 _ensureDirectoryService(); | 55 return IOService.dispatch(DIRECTORY_EXISTS, [path]).then((response) { |
| 58 List request = new List(2); | |
| 59 request[0] = EXISTS_REQUEST; | |
| 60 request[1] = path; | |
| 61 return _directoryService.call(request).then((response) { | |
| 62 if (_isErrorResponse(response)) { | 56 if (_isErrorResponse(response)) { |
| 63 throw _exceptionOrErrorFromResponse(response, "Exists failed"); | 57 throw _exceptionOrErrorFromResponse(response, "Exists failed"); |
| 64 } | 58 } |
| 65 return response == 1; | 59 return response == 1; |
| 66 }); | 60 }); |
| 67 } | 61 } |
| 68 | 62 |
| 69 bool existsSync() { | 63 bool existsSync() { |
| 70 var result = _exists(path); | 64 var result = _exists(path); |
| 71 if (result is OSError) { | 65 if (result is OSError) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 if (future == null) { | 120 if (future == null) { |
| 127 return new Future.value(this); | 121 return new Future.value(this); |
| 128 } else { | 122 } else { |
| 129 return future.then((_) => this); | 123 return future.then((_) => this); |
| 130 } | 124 } |
| 131 }); | 125 }); |
| 132 } | 126 } |
| 133 | 127 |
| 134 Future<Directory> create({recursive: false}) { | 128 Future<Directory> create({recursive: false}) { |
| 135 if (recursive) return createRecursively(); | 129 if (recursive) return createRecursively(); |
| 136 _ensureDirectoryService(); | 130 return IOService.dispatch(DIRECTORY_CREATE, [path]).then((response) { |
| 137 List request = new List(2); | |
| 138 request[0] = CREATE_REQUEST; | |
| 139 request[1] = path; | |
| 140 return _directoryService.call(request).then((response) { | |
| 141 if (_isErrorResponse(response)) { | 131 if (_isErrorResponse(response)) { |
| 142 throw _exceptionOrErrorFromResponse(response, "Creation failed"); | 132 throw _exceptionOrErrorFromResponse(response, "Creation failed"); |
| 143 } | 133 } |
| 144 return this; | 134 return this; |
| 145 }); | 135 }); |
| 146 } | 136 } |
| 147 | 137 |
| 148 void createRecursivelySync() { | 138 void createRecursivelySync() { |
| 149 var path = new _Path(this.path); | 139 var path = new _Path(this.path); |
| 150 var dirsToCreate = []; | 140 var dirsToCreate = []; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 162 | 152 |
| 163 void createSync({recursive: false}) { | 153 void createSync({recursive: false}) { |
| 164 if (recursive) return createRecursivelySync(); | 154 if (recursive) return createRecursivelySync(); |
| 165 var result = _create(path); | 155 var result = _create(path); |
| 166 if (result is OSError) { | 156 if (result is OSError) { |
| 167 throw new DirectoryException("Creation failed", path, result); | 157 throw new DirectoryException("Creation failed", path, result); |
| 168 } | 158 } |
| 169 } | 159 } |
| 170 | 160 |
| 171 Future<Directory> createTemp() { | 161 Future<Directory> createTemp() { |
| 172 _ensureDirectoryService(); | 162 return IOService.dispatch(DIRECTORY_CREATE_TEMP, [path]).then((response) { |
| 173 List request = new List(2); | |
| 174 request[0] = CREATE_TEMP_REQUEST; | |
| 175 request[1] = path; | |
| 176 return _directoryService.call(request).then((response) { | |
| 177 if (_isErrorResponse(response)) { | 163 if (_isErrorResponse(response)) { |
| 178 throw _exceptionOrErrorFromResponse( | 164 throw _exceptionOrErrorFromResponse( |
| 179 response, "Creation of temporary directory failed"); | 165 response, "Creation of temporary directory failed"); |
| 180 } | 166 } |
| 181 return new Directory(response); | 167 return new Directory(response); |
| 182 }); | 168 }); |
| 183 } | 169 } |
| 184 | 170 |
| 185 Directory createTempSync() { | 171 Directory createTempSync() { |
| 186 var result = _createTemp(path); | 172 var result = _createTemp(path); |
| 187 if (result is OSError) { | 173 if (result is OSError) { |
| 188 throw new DirectoryException("Creation of temporary directory failed", | 174 throw new DirectoryException("Creation of temporary directory failed", |
| 189 path, | 175 path, |
| 190 result); | 176 result); |
| 191 } | 177 } |
| 192 return new Directory(result); | 178 return new Directory(result); |
| 193 } | 179 } |
| 194 | 180 |
| 195 Future<Directory> _delete({recursive: false}) { | 181 Future<Directory> _delete({recursive: false}) { |
| 196 _ensureDirectoryService(); | 182 return IOService.dispatch(DIRECTORY_DELETE, [path, recursive]) |
| 197 List request = new List(3); | 183 .then((response) { |
| 198 request[0] = DELETE_REQUEST; | 184 if (_isErrorResponse(response)) { |
| 199 request[1] = path; | 185 throw _exceptionOrErrorFromResponse(response, "Deletion failed"); |
| 200 request[2] = recursive; | 186 } |
| 201 return _directoryService.call(request).then((response) { | 187 return this; |
| 202 if (_isErrorResponse(response)) { | 188 }); |
| 203 throw _exceptionOrErrorFromResponse(response, "Deletion failed"); | |
| 204 } | |
| 205 return this; | |
| 206 }); | |
| 207 } | 189 } |
| 208 | 190 |
| 209 void _deleteSync({recursive: false}) { | 191 void _deleteSync({recursive: false}) { |
| 210 var result = _deleteNative(path, recursive); | 192 var result = _deleteNative(path, recursive); |
| 211 if (result is OSError) { | 193 if (result is OSError) { |
| 212 throw new DirectoryException("Deletion failed", path, result); | 194 throw new DirectoryException("Deletion failed", path, result); |
| 213 } | 195 } |
| 214 } | 196 } |
| 215 | 197 |
| 216 Future<Directory> rename(String newPath) { | 198 Future<Directory> rename(String newPath) { |
| 217 _ensureDirectoryService(); | 199 return IOService.dispatch(DIRECTORY_RENAME, [path, newPath]) |
| 218 List request = new List(3); | 200 .then((response) { |
| 219 request[0] = RENAME_REQUEST; | 201 if (_isErrorResponse(response)) { |
| 220 request[1] = path; | 202 throw _exceptionOrErrorFromResponse(response, "Rename failed"); |
| 221 request[2] = newPath; | 203 } |
| 222 return _directoryService.call(request).then((response) { | 204 return new Directory(newPath); |
| 223 if (_isErrorResponse(response)) { | 205 }); |
| 224 throw _exceptionOrErrorFromResponse(response, "Rename failed"); | |
| 225 } | |
| 226 return new Directory(newPath); | |
| 227 }); | |
| 228 } | 206 } |
| 229 | 207 |
| 230 Directory renameSync(String newPath) { | 208 Directory renameSync(String newPath) { |
| 231 if (newPath is !String) { | 209 if (newPath is !String) { |
| 232 throw new ArgumentError(); | 210 throw new ArgumentError(); |
| 233 } | 211 } |
| 234 var result = _rename(path, newPath); | 212 var result = _rename(path, newPath); |
| 235 if (result is OSError) { | 213 if (result is OSError) { |
| 236 throw new DirectoryException("Rename failed", path, result); | 214 throw new DirectoryException("Rename failed", path, result); |
| 237 } | 215 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 268 case _ILLEGAL_ARGUMENT_RESPONSE: | 246 case _ILLEGAL_ARGUMENT_RESPONSE: |
| 269 return new ArgumentError(); | 247 return new ArgumentError(); |
| 270 case _OSERROR_RESPONSE: | 248 case _OSERROR_RESPONSE: |
| 271 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], | 249 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], |
| 272 response[_OSERROR_RESPONSE_ERROR_CODE]); | 250 response[_OSERROR_RESPONSE_ERROR_CODE]); |
| 273 return new DirectoryException(message, path, err); | 251 return new DirectoryException(message, path, err); |
| 274 default: | 252 default: |
| 275 return new Exception("Unknown error"); | 253 return new Exception("Unknown error"); |
| 276 } | 254 } |
| 277 } | 255 } |
| 278 | |
| 279 void _ensureDirectoryService() { | |
| 280 if (_directoryService == null) { | |
| 281 _directoryService = _newServicePort(); | |
| 282 } | |
| 283 } | |
| 284 } | 256 } |
| 285 | 257 |
| 286 class _AsyncDirectoryLister { | 258 class _AsyncDirectoryLister { |
| 287 static const int LIST_FILE = 0; | 259 static const int LIST_FILE = 0; |
| 288 static const int LIST_DIRECTORY = 1; | 260 static const int LIST_DIRECTORY = 1; |
| 289 static const int LIST_LINK = 2; | 261 static const int LIST_LINK = 2; |
| 290 static const int LIST_ERROR = 3; | 262 static const int LIST_ERROR = 3; |
| 291 static const int LIST_DONE = 4; | 263 static const int LIST_DONE = 4; |
| 292 | 264 |
| 293 static const int RESPONSE_TYPE = 0; | 265 static const int RESPONSE_TYPE = 0; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 310 bool this.followLinks) { | 282 bool this.followLinks) { |
| 311 controller = new StreamController(onListen: onListen, | 283 controller = new StreamController(onListen: onListen, |
| 312 onResume: onResume, | 284 onResume: onResume, |
| 313 onCancel: onCancel, | 285 onCancel: onCancel, |
| 314 sync: true); | 286 sync: true); |
| 315 } | 287 } |
| 316 | 288 |
| 317 Stream get stream => controller.stream; | 289 Stream get stream => controller.stream; |
| 318 | 290 |
| 319 void onListen() { | 291 void onListen() { |
| 320 var request = [_Directory.LIST_START_REQUEST, path, recursive, followLinks]; | 292 IOService.dispatch(DIRECTORY_LIST_START, [path, recursive, followLinks]) |
| 321 _Directory._newServicePort().call(request) | |
| 322 .then((response) { | 293 .then((response) { |
| 323 if (response is int) { | 294 if (response is int) { |
| 324 id = response; | 295 id = response; |
| 325 next(); | 296 next(); |
| 326 } else { | 297 } else { |
| 327 error(response); | 298 error(response); |
| 328 controller.close(); | 299 controller.close(); |
| 329 } | 300 } |
| 330 }); | 301 }); |
| 331 } | 302 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 344 | 315 |
| 345 void next() { | 316 void next() { |
| 346 if (canceled) { | 317 if (canceled) { |
| 347 close(); | 318 close(); |
| 348 return; | 319 return; |
| 349 } | 320 } |
| 350 if (id == null) return; | 321 if (id == null) return; |
| 351 if (controller.isPaused) return; | 322 if (controller.isPaused) return; |
| 352 assert(!nextRunning); | 323 assert(!nextRunning); |
| 353 nextRunning = true; | 324 nextRunning = true; |
| 354 _Directory._newServicePort().call([_Directory.LIST_NEXT_REQUEST, id]) | 325 IOService.dispatch(DIRECTORY_LIST_NEXT, [id]).then((result) { |
| 355 .then((result) { | 326 if (result is List) { |
| 356 if (result is List) { | 327 assert(result.length % 2 == 0); |
| 357 assert(result.length % 2 == 0); | 328 for (int i = 0; i < result.length; i++) { |
| 358 for (int i = 0; i < result.length; i++) { | 329 assert(i % 2 == 0); |
| 359 assert(i % 2 == 0); | 330 switch (result[i++]) { |
| 360 switch (result[i++]) { | 331 case LIST_FILE: |
| 361 case LIST_FILE: | 332 controller.add(new File(result[i])); |
| 362 controller.add(new File(result[i])); | 333 break; |
| 363 break; | 334 case LIST_DIRECTORY: |
| 364 case LIST_DIRECTORY: | 335 controller.add(new Directory(result[i])); |
| 365 controller.add(new Directory(result[i])); | 336 break; |
| 366 break; | 337 case LIST_LINK: |
| 367 case LIST_LINK: | 338 controller.add(new Link(result[i])); |
| 368 controller.add(new Link(result[i])); | 339 break; |
| 369 break; | 340 case LIST_ERROR: |
| 370 case LIST_ERROR: | 341 error(result[i]); |
| 371 error(result[i]); | 342 break; |
| 372 break; | 343 case LIST_DONE: |
| 373 case LIST_DONE: | 344 close(); |
| 374 close(); | 345 return; |
| 375 return; | |
| 376 } | |
| 377 } | |
| 378 } else { | |
| 379 controller.addError(new DirectoryException("Internal error")); | |
| 380 } | 346 } |
| 381 nextRunning = false; | 347 } |
| 382 next(); | 348 } else { |
| 383 }); | 349 controller.addError(new DirectoryException("Internal error")); |
| 350 } |
| 351 nextRunning = false; |
| 352 next(); |
| 353 }); |
| 384 } | 354 } |
| 385 | 355 |
| 386 void close() { | 356 void close() { |
| 387 if (closed) return; | 357 if (closed) return; |
| 388 if (id == null) return; | 358 if (id == null) return; |
| 389 closed = true; | 359 closed = true; |
| 390 _Directory._newServicePort().call([_Directory.LIST_STOP_REQUEST, id]) | 360 IOService.dispatch(DIRECTORY_LIST_STOP, [id]).then((_) { |
| 391 .then((_) { | 361 controller.close(); |
| 392 controller.close(); | 362 }); |
| 393 }); | |
| 394 } | 363 } |
| 395 | 364 |
| 396 void error(message) { | 365 void error(message) { |
| 397 var errorType = | 366 var errorType = |
| 398 message[RESPONSE_ERROR][_ERROR_RESPONSE_ERROR_TYPE]; | 367 message[RESPONSE_ERROR][_ERROR_RESPONSE_ERROR_TYPE]; |
| 399 if (errorType == _ILLEGAL_ARGUMENT_RESPONSE) { | 368 if (errorType == _ILLEGAL_ARGUMENT_RESPONSE) { |
| 400 controller.addError(new ArgumentError()); | 369 controller.addError(new ArgumentError()); |
| 401 } else if (errorType == _OSERROR_RESPONSE) { | 370 } else if (errorType == _OSERROR_RESPONSE) { |
| 402 var responseError = message[RESPONSE_ERROR]; | 371 var responseError = message[RESPONSE_ERROR]; |
| 403 var err = new OSError( | 372 var err = new OSError( |
| 404 responseError[_OSERROR_RESPONSE_MESSAGE], | 373 responseError[_OSERROR_RESPONSE_MESSAGE], |
| 405 responseError[_OSERROR_RESPONSE_ERROR_CODE]); | 374 responseError[_OSERROR_RESPONSE_ERROR_CODE]); |
| 406 var errorPath = message[RESPONSE_PATH]; | 375 var errorPath = message[RESPONSE_PATH]; |
| 407 if (errorPath == null) errorPath = path; | 376 if (errorPath == null) errorPath = path; |
| 408 controller.addError( | 377 controller.addError( |
| 409 new DirectoryException("Directory listing failed", | 378 new DirectoryException("Directory listing failed", |
| 410 errorPath, | 379 errorPath, |
| 411 err)); | 380 err)); |
| 412 } else { | 381 } else { |
| 413 controller.addError( | 382 controller.addError( |
| 414 new DirectoryException("Internal error")); | 383 new DirectoryException("Internal error")); |
| 415 } | 384 } |
| 416 } | 385 } |
| 417 } | 386 } |
| OLD | NEW |