Chromium Code Reviews| Index: sdk/lib/io/file_system_entity.dart |
| diff --git a/sdk/lib/io/file_system_entity.dart b/sdk/lib/io/file_system_entity.dart |
| index fdeb923c6de41dc31715e1dc13192afcd6546ee2..b2b315f97e337cf38873747f45e2dccc3ae58a10 100644 |
| --- a/sdk/lib/io/file_system_entity.dart |
| +++ b/sdk/lib/io/file_system_entity.dart |
| @@ -530,6 +530,42 @@ abstract class FileSystemEntity { |
| external static _identical(String path1, String path2); |
| external static _resolveSymbolicLinks(String path); |
| + /** |
| + * The directory containing [this]. If [this] is a root |
| + * directory, returns [this]. |
| + */ |
| + Directory get parent { |
|
Søren Gjesse
2013/10/04 12:20:16
How about
return new Directory(new Uri.file(path)
Bill Hesse
2013/10/04 12:41:06
This will not give the desired result, since it wi
|
| + if (Platform.isWindows) { |
| + int rootEnd = -1; |
| + if (path.startsWith(_absoluteWindowsPathPattern)) { |
| + // Root ends at first / or \ after the first two characters. |
| + rootEnd = path.indexOf(new RegExp(r'[/\\]'), 2); |
| + if (rootEnd == -1) return new Directory(path); |
| + } |
| + // Ignore trailing slashes - find a slash followed by a non-slash. |
| + int pos = path.lastIndexOf(new RegExp(r'[/\\][^/\\]')); |
| + if (pos <= rootEnd) { |
| + if (rootEnd == -1) { |
| + return new Directory('.'); |
| + } else { |
| + return new Directory(path.substring(0, rootEnd + 1)); |
| + } |
| + } |
| + while (pos > rootEnd + 1 && |
| + (path[pos - 1] == '\\' || path[pos - 1] == '/')) { |
| + pos--; |
| + } |
| + return new Directory(path.substring(0, pos)); |
| + } else { |
| + // Ignore trailing slashes - find a slash followed by a non-slash. |
| + int pos = path.lastIndexOf(new RegExp(r'/[^/]')); |
| + if (pos < 0) return new Directory('.'); |
| + while (pos > 0 && path[pos - 1] == '/') --pos; |
| + if (pos == 0) return new Directory('/'); |
| + return new Directory(path.substring(0, pos)); |
| + } |
| + } |
| + |
| static int _getTypeSync(String path, bool followLinks) { |
| var result = _getType(path, followLinks); |
| _throwIfError(result, 'Error getting type of FileSystemEntity'); |
| @@ -537,12 +573,13 @@ abstract class FileSystemEntity { |
| } |
| static Future<int> _getTypeAsync(String path, bool followLinks) { |
| - return _IOService.dispatch(_FILE_TYPE, [path, followLinks]).then((response) { |
| - if (_isErrorResponse(response)) { |
| - throw _exceptionFromResponse(response, "Error getting type", path); |
| - } |
| - return response; |
| - }); |
| + return _IOService.dispatch(_FILE_TYPE, [path, followLinks]) |
| + .then((response) { |
| + if (_isErrorResponse(response)) { |
| + throw _exceptionFromResponse(response, "Error getting type", path); |
| + } |
| + return response; |
| + }); |
| } |
| static _throwIfError(Object result, String msg, [String path]) { |