| 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 implements Directory { | 7 class _Directory 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 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 if (_path is !String || newPath is !String) { | 219 if (_path is !String || newPath is !String) { |
| 220 throw new ArgumentError(); | 220 throw new ArgumentError(); |
| 221 } | 221 } |
| 222 var result = _rename(_path, newPath); | 222 var result = _rename(_path, newPath); |
| 223 if (result is OSError) { | 223 if (result is OSError) { |
| 224 throw new DirectoryIOException("Rename failed", _path, result); | 224 throw new DirectoryIOException("Rename failed", _path, result); |
| 225 } | 225 } |
| 226 return new Directory(newPath); | 226 return new Directory(newPath); |
| 227 } | 227 } |
| 228 | 228 |
| 229 DirectoryLister list({bool recursive: false}) { | 229 Stream<FileSystemEntity> list({bool recursive: false}) { |
| 230 return new _DirectoryLister(_path, recursive); | 230 const int LIST_FILE = 0; |
| 231 } | 231 const int LIST_DIRECTORY = 1; |
| 232 | |
| 233 List listSync({bool recursive: false}) { | |
| 234 if (_path is! String || recursive is! bool) { | |
| 235 throw new ArgumentError(); | |
| 236 } | |
| 237 return _list(_path, recursive); | |
| 238 } | |
| 239 | |
| 240 String get path => _path; | |
| 241 | |
| 242 String toString() => "Directory: '$path'"; | |
| 243 | |
| 244 bool _isErrorResponse(response) { | |
| 245 return response is List && response[0] != _SUCCESS_RESPONSE; | |
| 246 } | |
| 247 | |
| 248 _exceptionOrErrorFromResponse(response, String message) { | |
| 249 assert(_isErrorResponse(response)); | |
| 250 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { | |
| 251 case _ILLEGAL_ARGUMENT_RESPONSE: | |
| 252 return new ArgumentError(); | |
| 253 case _OSERROR_RESPONSE: | |
| 254 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], | |
| 255 response[_OSERROR_RESPONSE_ERROR_CODE]); | |
| 256 return new DirectoryIOException(message, _path, err); | |
| 257 default: | |
| 258 return new Exception("Unknown error"); | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 void _ensureDirectoryService() { | |
| 263 if (_directoryService == null) { | |
| 264 _directoryService = _newServicePort(); | |
| 265 } | |
| 266 } | |
| 267 | |
| 268 final String _path; | |
| 269 SendPort _directoryService; | |
| 270 } | |
| 271 | |
| 272 class _DirectoryLister implements DirectoryLister { | |
| 273 _DirectoryLister(String path, bool recursive) { | |
| 274 const int LIST_DIRECTORY = 0; | |
| 275 const int LIST_FILE = 1; | |
| 276 const int LIST_ERROR = 2; | 232 const int LIST_ERROR = 2; |
| 277 const int LIST_DONE = 3; | 233 const int LIST_DONE = 3; |
| 278 | 234 |
| 279 final int RESPONSE_TYPE = 0; | 235 const int RESPONSE_TYPE = 0; |
| 280 final int RESPONSE_PATH = 1; | 236 const int RESPONSE_PATH = 1; |
| 281 final int RESPONSE_COMPLETE = 1; | 237 const int RESPONSE_COMPLETE = 1; |
| 282 final int RESPONSE_ERROR = 2; | 238 const int RESPONSE_ERROR = 2; |
| 283 | 239 |
| 284 List request = new List.fixedLength(3); | 240 var controller = new StreamController<FileSystemEntity>(); |
| 285 request[0] = _Directory.LIST_REQUEST; | 241 |
| 286 request[1] = path; | 242 List request = [ _Directory.LIST_REQUEST, path, recursive ]; |
| 287 request[2] = recursive; | |
| 288 ReceivePort responsePort = new ReceivePort(); | 243 ReceivePort responsePort = new ReceivePort(); |
| 289 // Use a separate directory service port for each listing as | 244 // Use a separate directory service port for each listing as |
| 290 // listing operations on the same directory can run in parallel. | 245 // listing operations on the same directory can run in parallel. |
| 291 _Directory._newServicePort().send(request, responsePort.toSendPort()); | 246 _Directory._newServicePort().send(request, responsePort.toSendPort()); |
| 292 responsePort.receive((message, replyTo) { | 247 responsePort.receive((message, replyTo) { |
| 293 if (message is !List || message[RESPONSE_TYPE] is !int) { | 248 if (message is !List || message[RESPONSE_TYPE] is !int) { |
| 294 responsePort.close(); | 249 responsePort.close(); |
| 295 _reportError(new DirectoryIOException("Internal error")); | 250 controller.signalError(new DirectoryIOException("Internal error")); |
| 296 return; | 251 return; |
| 297 } | 252 } |
| 298 switch (message[RESPONSE_TYPE]) { | 253 switch (message[RESPONSE_TYPE]) { |
| 254 case LIST_FILE: |
| 255 controller.add(new File(message[RESPONSE_PATH])); |
| 256 break; |
| 299 case LIST_DIRECTORY: | 257 case LIST_DIRECTORY: |
| 300 if (_onDir != null) _onDir(message[RESPONSE_PATH]); | 258 controller.add(new Directory(message[RESPONSE_PATH])); |
| 301 break; | |
| 302 case LIST_FILE: | |
| 303 if (_onFile != null) _onFile(message[RESPONSE_PATH]); | |
| 304 break; | 259 break; |
| 305 case LIST_ERROR: | 260 case LIST_ERROR: |
| 306 var errorType = | 261 var errorType = |
| 307 message[RESPONSE_ERROR][_ERROR_RESPONSE_ERROR_TYPE]; | 262 message[RESPONSE_ERROR][_ERROR_RESPONSE_ERROR_TYPE]; |
| 308 if (errorType == _ILLEGAL_ARGUMENT_RESPONSE) { | 263 if (errorType == _ILLEGAL_ARGUMENT_RESPONSE) { |
| 309 _reportError(new ArgumentError()); | 264 controller.signalError(new ArgumentError()); |
| 310 } else if (errorType == _OSERROR_RESPONSE) { | 265 } else if (errorType == _OSERROR_RESPONSE) { |
| 311 var responseError = message[RESPONSE_ERROR]; | 266 var responseError = message[RESPONSE_ERROR]; |
| 312 var err = new OSError( | 267 var err = new OSError( |
| 313 responseError[_OSERROR_RESPONSE_MESSAGE], | 268 responseError[_OSERROR_RESPONSE_MESSAGE], |
| 314 responseError[_OSERROR_RESPONSE_ERROR_CODE]); | 269 responseError[_OSERROR_RESPONSE_ERROR_CODE]); |
| 315 var errorPath = message[RESPONSE_PATH]; | 270 var errorPath = message[RESPONSE_PATH]; |
| 316 if (errorPath == null) errorPath = path; | 271 if (errorPath == null) errorPath = path; |
| 317 _reportError(new DirectoryIOException("Directory listing failed", | 272 controller.signalError( |
| 318 errorPath, | 273 new DirectoryIOException("Directory listing failed", |
| 319 err)); | 274 errorPath, |
| 275 err)); |
| 320 } else { | 276 } else { |
| 321 _reportError(new DirectoryIOException("Internal error")); | 277 controller.signalError(new DirectoryIOException("Internal error")); |
| 322 } | 278 } |
| 323 break; | 279 break; |
| 324 case LIST_DONE: | 280 case LIST_DONE: |
| 325 responsePort.close(); | 281 responsePort.close(); |
| 326 if (_onDone != null) _onDone(message[RESPONSE_COMPLETE]); | 282 controller.close(); |
| 327 break; | 283 break; |
| 328 } | 284 } |
| 329 }); | 285 }); |
| 286 |
| 287 return controller.stream; |
| 330 } | 288 } |
| 331 | 289 |
| 332 void set onDir(void onDir(String dir)) { | 290 List listSync({bool recursive: false}) { |
| 333 _onDir = onDir; | 291 if (_path is! String || recursive is! bool) { |
| 292 throw new ArgumentError(); |
| 293 } |
| 294 return _list(_path, recursive); |
| 334 } | 295 } |
| 335 | 296 |
| 336 void set onFile(void onFile(String file)) { | 297 String get path => _path; |
| 337 _onFile = onFile; | 298 |
| 299 String toString() => "Directory: '$path'"; |
| 300 |
| 301 bool _isErrorResponse(response) { |
| 302 return response is List && response[0] != _SUCCESS_RESPONSE; |
| 338 } | 303 } |
| 339 | 304 |
| 340 void set onDone(void onDone(bool completed)) { | 305 _exceptionOrErrorFromResponse(response, String message) { |
| 341 _onDone = onDone; | 306 assert(_isErrorResponse(response)); |
| 342 } | 307 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { |
| 343 | 308 case _ILLEGAL_ARGUMENT_RESPONSE: |
| 344 void set onError(void onError(e)) { | 309 return new ArgumentError(); |
| 345 _onError = onError; | 310 case _OSERROR_RESPONSE: |
| 346 } | 311 var err = new OSError(response[_OSERROR_RESPONSE_MESSAGE], |
| 347 | 312 response[_OSERROR_RESPONSE_ERROR_CODE]); |
| 348 void _reportError(e) { | 313 return new DirectoryIOException(message, _path, err); |
| 349 if (_onError != null) { | 314 default: |
| 350 _onError(e); | 315 return new Exception("Unknown error"); |
| 351 } else { | |
| 352 throw e; | |
| 353 } | 316 } |
| 354 } | 317 } |
| 355 | 318 |
| 356 Function _onDir; | 319 void _ensureDirectoryService() { |
| 357 Function _onFile; | 320 if (_directoryService == null) { |
| 358 Function _onDone; | 321 _directoryService = _newServicePort(); |
| 359 Function _onError; | 322 } |
| 323 } |
| 324 |
| 325 final String _path; |
| 326 SendPort _directoryService; |
| 360 } | 327 } |
| OLD | NEW |