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 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 * Synchronously checks if typeSync(path) returns | 523 * Synchronously checks if typeSync(path) returns |
524 * FileSystemEntityType.DIRECTORY. | 524 * FileSystemEntityType.DIRECTORY. |
525 */ | 525 */ |
526 static bool isDirectorySync(String path) => | 526 static bool isDirectorySync(String path) => |
527 (_getTypeSync(path, true) == FileSystemEntityType.DIRECTORY._type); | 527 (_getTypeSync(path, true) == FileSystemEntityType.DIRECTORY._type); |
528 | 528 |
529 external static _getType(String path, bool followLinks); | 529 external static _getType(String path, bool followLinks); |
530 external static _identical(String path1, String path2); | 530 external static _identical(String path1, String path2); |
531 external static _resolveSymbolicLinks(String path); | 531 external static _resolveSymbolicLinks(String path); |
532 | 532 |
| 533 // Finds the next-to-last component when dividing at path separators. |
| 534 static final RegExp _parentRegExp = Platform.isWindows ? |
| 535 new RegExp(r'[^/\\][/\\]+[^/\\]') : |
| 536 new RegExp(r'[^/]/+[^/]'); |
| 537 |
| 538 /** |
| 539 * Removes the final path component of a path, using the platform's |
| 540 * path separator to split the path. Will not remove the root component |
| 541 * of a Windows path, like "C:\" or "\\server_name\". |
| 542 * Ignores trailing path separators, and leaves no trailing path separators. |
| 543 */ |
| 544 static String parentOf(String path) { |
| 545 int rootEnd = -1; |
| 546 if (Platform.isWindows) { |
| 547 if (path.startsWith(_absoluteWindowsPathPattern)) { |
| 548 // Root ends at first / or \ after the first two characters. |
| 549 rootEnd = path.indexOf(new RegExp(r'[/\\]'), 2); |
| 550 if (rootEnd == -1) return path; |
| 551 } else if (path.startsWith('\\') || path.startsWith('/')) { |
| 552 rootEnd = 0; |
| 553 } |
| 554 } else if (path.startsWith('/')) { |
| 555 rootEnd = 0; |
| 556 } |
| 557 // Ignore trailing slashes. |
| 558 // All non-trivial cases have separators between two non-separators. |
| 559 int pos = path.lastIndexOf(_parentRegExp); |
| 560 if (pos > rootEnd) { |
| 561 return path.substring(0, pos + 1); |
| 562 } else if (rootEnd > -1) { |
| 563 return path.substring(0, rootEnd + 1); |
| 564 } else { |
| 565 return '.'; |
| 566 } |
| 567 } |
| 568 |
| 569 /** |
| 570 * The directory containing [this]. If [this] is a root |
| 571 * directory, returns [this]. |
| 572 */ |
| 573 Directory get parent => new Directory(parentOf(path)); |
| 574 |
533 static int _getTypeSync(String path, bool followLinks) { | 575 static int _getTypeSync(String path, bool followLinks) { |
534 var result = _getType(path, followLinks); | 576 var result = _getType(path, followLinks); |
535 _throwIfError(result, 'Error getting type of FileSystemEntity'); | 577 _throwIfError(result, 'Error getting type of FileSystemEntity'); |
536 return result; | 578 return result; |
537 } | 579 } |
538 | 580 |
539 static Future<int> _getTypeAsync(String path, bool followLinks) { | 581 static Future<int> _getTypeAsync(String path, bool followLinks) { |
540 return _IOService.dispatch(_FILE_TYPE, [path, followLinks]).then((response)
{ | 582 return _IOService.dispatch(_FILE_TYPE, [path, followLinks]) |
541 if (_isErrorResponse(response)) { | 583 .then((response) { |
542 throw _exceptionFromResponse(response, "Error getting type", path); | 584 if (_isErrorResponse(response)) { |
543 } | 585 throw _exceptionFromResponse(response, "Error getting type", path); |
544 return response; | 586 } |
545 }); | 587 return response; |
| 588 }); |
546 } | 589 } |
547 | 590 |
548 static _throwIfError(Object result, String msg, [String path]) { | 591 static _throwIfError(Object result, String msg, [String path]) { |
549 if (result is OSError) { | 592 if (result is OSError) { |
550 throw new FileException(msg, path, result); | 593 throw new FileException(msg, path, result); |
551 } else if (result is ArgumentError) { | 594 } else if (result is ArgumentError) { |
552 throw result; | 595 throw result; |
553 } | 596 } |
554 } | 597 } |
555 | 598 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
661 } | 704 } |
662 } | 705 } |
663 | 706 |
664 | 707 |
665 abstract class _FileSystemWatcher { | 708 abstract class _FileSystemWatcher { |
666 external factory _FileSystemWatcher(String path, int events, bool recursive); | 709 external factory _FileSystemWatcher(String path, int events, bool recursive); |
667 external static bool get isSupported; | 710 external static bool get isSupported; |
668 | 711 |
669 Stream<FileSystemEvent> get stream; | 712 Stream<FileSystemEvent> get stream; |
670 } | 713 } |
OLD | NEW |