| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 FileSystemEntityType { | 7 class FileSystemEntityType { |
| 8 static const FILE = const FileSystemEntityType._internal(0); | 8 static const FILE = const FileSystemEntityType._internal(0); |
| 9 static const DIRECTORY = const FileSystemEntityType._internal(1); | 9 static const DIRECTORY = const FileSystemEntityType._internal(1); |
| 10 static const LINK = const FileSystemEntityType._internal(2); | 10 static const LINK = const FileSystemEntityType._internal(2); |
| (...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 /** | 413 /** |
| 414 * Synchronously checks if typeSync(path) returns | 414 * Synchronously checks if typeSync(path) returns |
| 415 * FileSystemEntityType.DIRECTORY. | 415 * FileSystemEntityType.DIRECTORY. |
| 416 */ | 416 */ |
| 417 static bool isDirectorySync(String path) => | 417 static bool isDirectorySync(String path) => |
| 418 (_getTypeSync(path, true) == FileSystemEntityType.DIRECTORY._type); | 418 (_getTypeSync(path, true) == FileSystemEntityType.DIRECTORY._type); |
| 419 | 419 |
| 420 | 420 |
| 421 static _throwIfError(Object result, String msg, [String path]) { | 421 static _throwIfError(Object result, String msg, [String path]) { |
| 422 if (result is OSError) { | 422 if (result is OSError) { |
| 423 throw new FileException(msg, result, path); | 423 throw new FileException(msg, path, result); |
| 424 } else if (result is ArgumentError) { | 424 } else if (result is ArgumentError) { |
| 425 throw result; | 425 throw result; |
| 426 } | 426 } |
| 427 } | 427 } |
| 428 | 428 |
| 429 static String _trimTrailingPathSeparators(String path) { | 429 static String _trimTrailingPathSeparators(String path) { |
| 430 // Don't handle argument errors here. | 430 // Don't handle argument errors here. |
| 431 if (path is! String) return path; | 431 if (path is! String) return path; |
| 432 if (Platform.operatingSystem == 'windows') { | 432 if (Platform.operatingSystem == 'windows') { |
| 433 while (path.length > 1 && | 433 while (path.length > 1 && |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 String toString() { | 528 String toString() { |
| 529 var buffer = new StringBuffer(); | 529 var buffer = new StringBuffer(); |
| 530 buffer.write("FileSystemMoveEvent('$path'"); | 530 buffer.write("FileSystemMoveEvent('$path'"); |
| 531 if (destination != null) buffer.write(", '$destination'"); | 531 if (destination != null) buffer.write(", '$destination'"); |
| 532 buffer.write(')'); | 532 buffer.write(')'); |
| 533 return buffer.toString(); | 533 return buffer.toString(); |
| 534 } | 534 } |
| 535 } | 535 } |
| 536 | 536 |
| 537 | 537 |
| 538 class _FileSystemWatcher extends NativeFieldWrapperClass1 { | 538 abstract class _FileSystemWatcher { |
| 539 final String _path; | 539 external factory _FileSystemWatcher(String path, int events, bool recursive); |
| 540 final int _events; | |
| 541 final bool _recursive; | |
| 542 | 540 |
| 543 StreamController _controller; | 541 Stream<FileSystemEvent> get stream; |
| 544 StreamSubscription _subscription; | |
| 545 | |
| 546 _FileSystemWatcher(this._path, this._events, this._recursive) { | |
| 547 _controller = new StreamController(onListen: _listen, onCancel: _cancel); | |
| 548 } | |
| 549 | |
| 550 void _listen() { | |
| 551 int socketId; | |
| 552 try { | |
| 553 socketId = _watchPath(_path, _events, identical(true, _recursive)); | |
| 554 } catch (e) { | |
| 555 throw new FileException( | |
| 556 "Failed to watch path", | |
| 557 _path, | |
| 558 e); | |
| 559 } | |
| 560 var socket = new _RawSocket(new _NativeSocket.watch(socketId)); | |
| 561 _subscription = socket.expand((event) { | |
| 562 var events = []; | |
| 563 var pair = {}; | |
| 564 if (event == RawSocketEvent.READ) { | |
| 565 String getPath(event) { | |
| 566 var path = _path; | |
| 567 if (event[2] != null) { | |
| 568 path += Platform.pathSeparator; | |
| 569 path += event[2]; | |
| 570 } | |
| 571 return path; | |
| 572 } | |
| 573 while (socket.available() > 0) { | |
| 574 for (var event in _readEvents()) { | |
| 575 if (event == null) continue; | |
| 576 var path = getPath(event); | |
| 577 if ((event[0] & FileSystemEvent.CREATE) != 0) { | |
| 578 events.add(new FileSystemCreateEvent._(path)); | |
| 579 } | |
| 580 if ((event[0] & FileSystemEvent.MODIFY) != 0) { | |
| 581 events.add(new FileSystemModifyEvent._(path, true)); | |
| 582 } | |
| 583 if ((event[0] & FileSystemEvent._MODIFY_ATTRIBUTES) != 0) { | |
| 584 events.add(new FileSystemModifyEvent._(path, false)); | |
| 585 } | |
| 586 if ((event[0] & FileSystemEvent.MOVE) != 0) { | |
| 587 int link = event[1]; | |
| 588 if (link > 0) { | |
| 589 if (pair.containsKey(link)) { | |
| 590 events.add( | |
| 591 new FileSystemMoveEvent._(getPath(pair[link]), path)); | |
| 592 pair.remove(link); | |
| 593 } else { | |
| 594 pair[link] = event; | |
| 595 } | |
| 596 } else { | |
| 597 events.add(new FileSystemMoveEvent._(path, null)); | |
| 598 } | |
| 599 } | |
| 600 if ((event[0] & FileSystemEvent.DELETE) != 0) { | |
| 601 events.add(new FileSystemDeleteEvent._(path)); | |
| 602 } | |
| 603 } | |
| 604 } | |
| 605 for (var event in pair.values) { | |
| 606 events.add(new FileSystemMoveEvent._(getPath(event), null)); | |
| 607 } | |
| 608 } else if (event == RawSocketEvent.CLOSED) { | |
| 609 } else if (event == RawSocketEvent.READ_CLOSED) { | |
| 610 } else { | |
| 611 assert(false); | |
| 612 } | |
| 613 return events; | |
| 614 }) | |
| 615 .where((event) => (event.type & _events) != 0) | |
| 616 .listen(_controller.add, onDone: _cancel); | |
| 617 } | |
| 618 | |
| 619 void _cancel() { | |
| 620 _unwatchPath(); | |
| 621 if (_subscription != null) { | |
| 622 _subscription.cancel(); | |
| 623 } | |
| 624 } | |
| 625 | |
| 626 Stream<FileSystemEvent> get stream => _controller.stream; | |
| 627 | |
| 628 external int _watchPath(String path, int events, bool recursive); | |
| 629 external void _unwatchPath(); | |
| 630 external List _readEvents(); | |
| 631 } | 542 } |
| OLD | NEW |