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 16 matching lines...) Expand all Loading... |
27 * | 27 * |
28 * [FileSystemEntity] objects are returned from directory listing | 28 * [FileSystemEntity] objects are returned from directory listing |
29 * operations. To determine if a FileSystemEntity is a [File] or a | 29 * operations. To determine if a FileSystemEntity is a [File] or a |
30 * [Directory], perform a type check: | 30 * [Directory], perform a type check: |
31 * | 31 * |
32 * if (entity is File) (entity as File).readAsStringSync(); | 32 * if (entity is File) (entity as File).readAsStringSync(); |
33 */ | 33 */ |
34 abstract class FileSystemEntity { | 34 abstract class FileSystemEntity { |
35 String get path; | 35 String get path; |
36 | 36 |
37 external static int _getType(String path, bool followLinks); | 37 external static _getType(String path, bool followLinks); |
38 external static bool _identical(String path1, String path2); | 38 external static _identical(String path1, String path2); |
39 | 39 |
40 static int _getTypeSync(String path, bool followLinks) { | 40 static int _getTypeSync(String path, bool followLinks) { |
41 var result = _getType(path, followLinks); | 41 var result = _getType(path, followLinks); |
42 _throwIfError(result, 'Error getting type of FileSystemEntity'); | 42 _throwIfError(result, 'Error getting type of FileSystemEntity'); |
43 return result; | 43 return result; |
44 } | 44 } |
45 | 45 |
46 static Future<int> _getTypeAsync(String path, bool followLinks) => | 46 static Future<int> _getTypeAsync(String path, bool followLinks) { |
47 new Future(() => _getTypeSync(path, followLinks)); | 47 // Get a new file service port for each request. We could also cache one. |
| 48 var service = _FileUtils._newServicePort(); |
| 49 List request = new List(3); |
| 50 request[0] = _TYPE_REQUEST; |
| 51 request[1] = path; |
| 52 request[2] = followLinks; |
| 53 return service.call(request).then((response) { |
| 54 if (_isErrorResponse(response)) { |
| 55 throw _exceptionFromResponse(response, |
| 56 "Error getting type of '$_path'"); |
| 57 } |
| 58 return response; |
| 59 }); |
| 60 } |
48 | 61 |
49 /** | 62 /** |
50 * Do two paths refer to the same object in the file system? | 63 * Do two paths refer to the same object in the file system? |
51 * Links are not identical to their targets, and two links | 64 * Links are not identical to their targets, and two links |
52 * are not identical just because they point to identical targets. | 65 * are not identical just because they point to identical targets. |
53 * Links in intermediate directories in the paths are followed, though. | 66 * Links in intermediate directories in the paths are followed, though. |
54 * | 67 * |
55 * Throws an error if one of the paths points to an object that does not | 68 * Throws an error if one of the paths points to an object that does not |
56 * exist. | 69 * exist. |
57 * The target of a link can be compared by first getting it with Link.target. | 70 * The target of a link can be compared by first getting it with Link.target. |
58 */ | 71 */ |
59 static Future<bool> identical(String path1, String path2) => | 72 static Future<bool> identical(String path1, String path2) { |
60 new Future<bool>(() => identicalSync(path1, path2)); | 73 // Get a new file service port for each request. We could also cache one. |
| 74 var service = _FileUtils._newServicePort(); |
| 75 List request = new List(3); |
| 76 request[0] = _IDENTICAL_REQUEST; |
| 77 request[1] = path1; |
| 78 request[2] = path2; |
| 79 return service.call(request).then((response) { |
| 80 if (_isErrorResponse(response)) { |
| 81 throw _exceptionFromResponse(response, |
| 82 "Error in FileSystemEntity.identical($path1, $path2)"); |
| 83 } |
| 84 return response; |
| 85 }); |
| 86 } |
| 87 |
61 | 88 |
62 /** | 89 /** |
63 * Do two paths refer to the same object in the file system? | 90 * Do two paths refer to the same object in the file system? |
64 * Links are not identical to their targets, and two links | 91 * Links are not identical to their targets, and two links |
65 * are not identical just because they point to identical targets. | 92 * are not identical just because they point to identical targets. |
66 * Links in intermediate directories in the paths are followed, though. | 93 * Links in intermediate directories in the paths are followed, though. |
67 * | 94 * |
68 * Throws an error if one of the paths points to an object that does not | 95 * Throws an error if one of the paths points to an object that does not |
69 * exist. | 96 * exist. |
70 * The target of a link can be compared by first getting it with Link.target. | 97 * The target of a link can be compared by first getting it with Link.target. |
71 */ | 98 */ |
72 static bool identicalSync(String path1, String path2) { | 99 static bool identicalSync(String path1, String path2) { |
73 var result = _identical(path1, path2); | 100 var result = _identical(path1, path2); |
74 _throwIfError(result, 'Error in FileSystemEntity.identical'); | 101 _throwIfError(result, 'Error in FileSystemEntity.identicalSync'); |
75 return result; | 102 return result; |
76 } | 103 } |
77 | 104 |
78 static Future<FileSystemEntityType> type(String path, | 105 static Future<FileSystemEntityType> type(String path, |
79 {bool followLinks: true}) | 106 {bool followLinks: true}) |
80 => new Future<FileSystemEntityType>( | 107 => _getTypeAsync(path, followLinks).then(FileSystemEntityType._lookup); |
81 () => typeSync(path, followLinks: followLinks)); | |
82 | 108 |
83 static FileSystemEntityType typeSync(String path, {bool followLinks: true}) | 109 static FileSystemEntityType typeSync(String path, {bool followLinks: true}) |
84 => FileSystemEntityType._lookup(_getTypeSync(path, followLinks)); | 110 => FileSystemEntityType._lookup(_getTypeSync(path, followLinks)); |
85 | 111 |
86 static Future<bool> isLink(String path) => _getTypeAsync(path, false) | 112 static Future<bool> isLink(String path) => _getTypeAsync(path, false) |
87 .then((type) => (type == FileSystemEntityType.LINK._type)); | 113 .then((type) => (type == FileSystemEntityType.LINK._type)); |
88 | 114 |
89 static Future<bool> isFile(String path) => _getTypeAsync(path, true) | 115 static Future<bool> isFile(String path) => _getTypeAsync(path, true) |
90 .then((type) => (type == FileSystemEntityType.FILE._type)); | 116 .then((type) => (type == FileSystemEntityType.FILE._type)); |
91 | 117 |
(...skipping 10 matching lines...) Expand all Loading... |
102 (_getTypeSync(path, true) == FileSystemEntityType.DIRECTORY._type); | 128 (_getTypeSync(path, true) == FileSystemEntityType.DIRECTORY._type); |
103 | 129 |
104 static _throwIfError(Object result, String msg) { | 130 static _throwIfError(Object result, String msg) { |
105 if (result is OSError) { | 131 if (result is OSError) { |
106 throw new FileIOException(msg, result); | 132 throw new FileIOException(msg, result); |
107 } else if (result is ArgumentError) { | 133 } else if (result is ArgumentError) { |
108 throw result; | 134 throw result; |
109 } | 135 } |
110 } | 136 } |
111 } | 137 } |
OLD | NEW |