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; |
11 static const CREATE_TEMP_REQUEST = 3; | 11 static const CREATE_TEMP_REQUEST = 3; |
12 static const LIST_REQUEST = 4; | 12 static const LIST_REQUEST = 4; |
13 static const RENAME_REQUEST = 5; | 13 static const RENAME_REQUEST = 5; |
14 | 14 |
15 static const SUCCESS_RESPONSE = 0; | 15 static const SUCCESS_RESPONSE = 0; |
16 static const ILLEGAL_ARGUMENT_RESPONSE = 1; | 16 static const ILLEGAL_ARGUMENT_RESPONSE = 1; |
17 static const OSERROR_RESPONSE = 2; | 17 static const OSERROR_RESPONSE = 2; |
18 | 18 |
19 _Directory(String this._path); | 19 _Directory(String this._path); |
20 _Directory.fromPath(Path path) : this(path.toNativePath()); | 20 _Directory.fromPath(Path path) : this(path.toNativePath()); |
21 _Directory.current() : this(_current()); | 21 _Directory.current() : this(_current()); |
22 | 22 |
23 external static String _current(); | 23 external static String _current(); |
24 external static _createTemp(String template); | 24 external static _createTemp(String template); |
25 external static int _exists(String path); | 25 external static int _exists(String path); |
26 external static _create(String path); | 26 external static _create(String path); |
27 external static _delete(String path, bool recursive); | 27 external static _delete(String path, bool recursive); |
28 external static _rename(String path, String newPath); | 28 external static _rename(String path, String newPath); |
29 external static List _list(String path, bool recursive); | 29 external static List _list(String path, bool recursive, bool followLinks); |
30 external static SendPort _newServicePort(); | 30 external static SendPort _newServicePort(); |
31 | 31 |
32 Future<bool> exists() { | 32 Future<bool> exists() { |
33 _ensureDirectoryService(); | 33 _ensureDirectoryService(); |
34 List request = new List(2); | 34 List request = new List(2); |
35 request[0] = EXISTS_REQUEST; | 35 request[0] = EXISTS_REQUEST; |
36 request[1] = _path; | 36 request[1] = _path; |
37 return _directoryService.call(request).then((response) { | 37 return _directoryService.call(request).then((response) { |
38 if (_isErrorResponse(response)) { | 38 if (_isErrorResponse(response)) { |
39 throw _exceptionOrErrorFromResponse(response, "Exists failed"); | 39 throw _exceptionOrErrorFromResponse(response, "Exists failed"); |
(...skipping 179 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 Stream<FileSystemEntity> list({bool recursive: false}) { | 229 Stream<FileSystemEntity> list({bool recursive: false, |
| 230 bool followLinks: true}) { |
230 const int LIST_FILE = 0; | 231 const int LIST_FILE = 0; |
231 const int LIST_DIRECTORY = 1; | 232 const int LIST_DIRECTORY = 1; |
232 const int LIST_ERROR = 2; | 233 const int LIST_LINK = 2; |
233 const int LIST_DONE = 3; | 234 const int LIST_ERROR = 3; |
| 235 const int LIST_DONE = 4; |
234 | 236 |
235 const int RESPONSE_TYPE = 0; | 237 const int RESPONSE_TYPE = 0; |
236 const int RESPONSE_PATH = 1; | 238 const int RESPONSE_PATH = 1; |
237 const int RESPONSE_COMPLETE = 1; | 239 const int RESPONSE_COMPLETE = 1; |
238 const int RESPONSE_ERROR = 2; | 240 const int RESPONSE_ERROR = 2; |
239 | 241 |
240 var controller = new StreamController<FileSystemEntity>(); | 242 var controller = new StreamController<FileSystemEntity>(); |
241 | 243 |
242 List request = [ _Directory.LIST_REQUEST, path, recursive ]; | 244 List request = [ _Directory.LIST_REQUEST, path, recursive, followLinks ]; |
243 ReceivePort responsePort = new ReceivePort(); | 245 ReceivePort responsePort = new ReceivePort(); |
244 // Use a separate directory service port for each listing as | 246 // Use a separate directory service port for each listing as |
245 // listing operations on the same directory can run in parallel. | 247 // listing operations on the same directory can run in parallel. |
246 _Directory._newServicePort().send(request, responsePort.toSendPort()); | 248 _Directory._newServicePort().send(request, responsePort.toSendPort()); |
247 responsePort.receive((message, replyTo) { | 249 responsePort.receive((message, replyTo) { |
248 if (message is !List || message[RESPONSE_TYPE] is !int) { | 250 if (message is !List || message[RESPONSE_TYPE] is !int) { |
249 responsePort.close(); | 251 responsePort.close(); |
250 controller.addError(new DirectoryIOException("Internal error")); | 252 controller.addError(new DirectoryIOException("Internal error")); |
251 return; | 253 return; |
252 } | 254 } |
253 switch (message[RESPONSE_TYPE]) { | 255 switch (message[RESPONSE_TYPE]) { |
254 case LIST_FILE: | 256 case LIST_FILE: |
255 controller.add(new File(message[RESPONSE_PATH])); | 257 controller.add(new File(message[RESPONSE_PATH])); |
256 break; | 258 break; |
257 case LIST_DIRECTORY: | 259 case LIST_DIRECTORY: |
258 controller.add(new Directory(message[RESPONSE_PATH])); | 260 controller.add(new Directory(message[RESPONSE_PATH])); |
259 break; | 261 break; |
| 262 case LIST_LINK: |
| 263 controller.add(new Link(message[RESPONSE_PATH])); |
| 264 break; |
260 case LIST_ERROR: | 265 case LIST_ERROR: |
261 var errorType = | 266 var errorType = |
262 message[RESPONSE_ERROR][_ERROR_RESPONSE_ERROR_TYPE]; | 267 message[RESPONSE_ERROR][_ERROR_RESPONSE_ERROR_TYPE]; |
263 if (errorType == _ILLEGAL_ARGUMENT_RESPONSE) { | 268 if (errorType == _ILLEGAL_ARGUMENT_RESPONSE) { |
264 controller.addError(new ArgumentError()); | 269 controller.addError(new ArgumentError()); |
265 } else if (errorType == _OSERROR_RESPONSE) { | 270 } else if (errorType == _OSERROR_RESPONSE) { |
266 var responseError = message[RESPONSE_ERROR]; | 271 var responseError = message[RESPONSE_ERROR]; |
267 var err = new OSError( | 272 var err = new OSError( |
268 responseError[_OSERROR_RESPONSE_MESSAGE], | 273 responseError[_OSERROR_RESPONSE_MESSAGE], |
269 responseError[_OSERROR_RESPONSE_ERROR_CODE]); | 274 responseError[_OSERROR_RESPONSE_ERROR_CODE]); |
(...skipping 10 matching lines...) Expand all Loading... |
280 case LIST_DONE: | 285 case LIST_DONE: |
281 responsePort.close(); | 286 responsePort.close(); |
282 controller.close(); | 287 controller.close(); |
283 break; | 288 break; |
284 } | 289 } |
285 }); | 290 }); |
286 | 291 |
287 return controller.stream; | 292 return controller.stream; |
288 } | 293 } |
289 | 294 |
290 List listSync({bool recursive: false}) { | 295 List listSync({bool recursive: false, bool followLinks: true}) { |
291 if (_path is! String || recursive is! bool) { | 296 if (_path is! String || recursive is! bool) { |
292 throw new ArgumentError(); | 297 throw new ArgumentError(); |
293 } | 298 } |
294 return _list(_path, recursive); | 299 return _list(_path, recursive, followLinks); |
295 } | 300 } |
296 | 301 |
297 String get path => _path; | 302 String get path => _path; |
298 | 303 |
299 String toString() => "Directory: '$path'"; | 304 String toString() => "Directory: '$path'"; |
300 | 305 |
301 bool _isErrorResponse(response) { | 306 bool _isErrorResponse(response) { |
302 return response is List && response[0] != _SUCCESS_RESPONSE; | 307 return response is List && response[0] != _SUCCESS_RESPONSE; |
303 } | 308 } |
304 | 309 |
(...skipping 13 matching lines...) Expand all Loading... |
318 | 323 |
319 void _ensureDirectoryService() { | 324 void _ensureDirectoryService() { |
320 if (_directoryService == null) { | 325 if (_directoryService == null) { |
321 _directoryService = _newServicePort(); | 326 _directoryService = _newServicePort(); |
322 } | 327 } |
323 } | 328 } |
324 | 329 |
325 final String _path; | 330 final String _path; |
326 SendPort _directoryService; | 331 SendPort _directoryService; |
327 } | 332 } |
OLD | NEW |