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 28 matching lines...) Expand all Loading... |
39 this.modified, | 39 this.modified, |
40 this.accessed, | 40 this.accessed, |
41 this.type, | 41 this.type, |
42 this.mode, | 42 this.mode, |
43 this.size); | 43 this.size); |
44 | 44 |
45 external static List<int> _statSync(String path); | 45 external static List<int> _statSync(String path); |
46 | 46 |
47 | 47 |
48 /** | 48 /** |
49 * Call the operating system's stat() function on [path]. | 49 * Calls the operating system's stat() function on [path]. |
50 * Returns a [FileStat] object containing the data returned by stat(). | 50 * Returns a [FileStat] object containing the data returned by stat(). |
51 * If the call fails, returns a [FileStat] object with .type set to | 51 * If the call fails, returns a [FileStat] object with .type set to |
52 * FileSystemEntityType.NOT_FOUND and the other fields invalid. | 52 * FileSystemEntityType.NOT_FOUND and the other fields invalid. |
53 */ | 53 */ |
54 static FileStat statSync(String path) { | 54 static FileStat statSync(String path) { |
55 var data = _statSync(path); | 55 var data = _statSync(path); |
56 if (data is Error) throw data; | 56 if (data is Error) throw data; |
57 return new FileStat._internal( | 57 return new FileStat._internal( |
58 new DateTime.fromMillisecondsSinceEpoch(data[_CHANGED_TIME] * 1000), | 58 new DateTime.fromMillisecondsSinceEpoch(data[_CHANGED_TIME] * 1000), |
59 new DateTime.fromMillisecondsSinceEpoch(data[_MODIFIED_TIME] * 1000), | 59 new DateTime.fromMillisecondsSinceEpoch(data[_MODIFIED_TIME] * 1000), |
60 new DateTime.fromMillisecondsSinceEpoch(data[_ACCESSED_TIME] * 1000), | 60 new DateTime.fromMillisecondsSinceEpoch(data[_ACCESSED_TIME] * 1000), |
61 FileSystemEntityType._lookup(data[_TYPE]), | 61 FileSystemEntityType._lookup(data[_TYPE]), |
62 data[_MODE], | 62 data[_MODE], |
63 data[_SIZE]); | 63 data[_SIZE]); |
64 } | 64 } |
65 | 65 |
66 /** | 66 /** |
67 * Asynchronously call the operating system's stat() function on [path]. | 67 * Asynchronously calls the operating system's stat() function on [path]. |
68 * Returns a Future which completes with a [FileStat] object containing | 68 * Returns a Future which completes with a [FileStat] object containing |
69 * the data returned by stat(). | 69 * the data returned by stat(). |
70 * If the call fails, completes the future with a [FileStat] object with | 70 * If the call fails, completes the future with a [FileStat] object with |
71 * .type set to FileSystemEntityType.NOT_FOUND and the other fields invalid. | 71 * .type set to FileSystemEntityType.NOT_FOUND and the other fields invalid. |
72 */ | 72 */ |
73 static Future<FileStat> stat(String path) { | 73 static Future<FileStat> stat(String path) { |
74 // Get a new file service port for each request. We could also cache one. | 74 // Get a new file service port for each request. We could also cache one. |
75 var service = _FileUtils._newServicePort(); | 75 var service = _FileUtils._newServicePort(); |
76 List request = new List(2); | 76 List request = new List(2); |
77 request[0] = _STAT_REQUEST; | 77 request[0] = _STAT_REQUEST; |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 return service.call(request).then((response) { | 186 return service.call(request).then((response) { |
187 if (_isErrorResponse(response)) { | 187 if (_isErrorResponse(response)) { |
188 throw _exceptionFromResponse(response, | 188 throw _exceptionFromResponse(response, |
189 "Error getting type of '$path'"); | 189 "Error getting type of '$path'"); |
190 } | 190 } |
191 return response; | 191 return response; |
192 }); | 192 }); |
193 } | 193 } |
194 | 194 |
195 /** | 195 /** |
196 * Do two paths refer to the same object in the file system? | 196 * Synchronously checks whether two paths refer to the same object in the |
197 * Links are not identical to their targets, and two links | 197 * file system. Returns a [:Future<bool>:] that completes with the result. |
198 * are not identical just because they point to identical targets. | |
199 * Links in intermediate directories in the paths are followed, though. | |
200 * | 198 * |
201 * Throws an error if one of the paths points to an object that does not | 199 * Comparing a link to its target returns false, as does comparing two links |
202 * exist. | 200 * that point to the same target. To check the target of a link, use |
203 * The target of a link can be compared by first getting it with Link.target. | 201 * Link.target explicitly to fetch it. Directory links appearing |
| 202 * inside a path are followed, though, to find the file system object. |
| 203 * |
| 204 * Completes the returned Future with an error if one of the paths points |
| 205 * to an object that does not exist. |
204 */ | 206 */ |
205 static Future<bool> identical(String path1, String path2) { | 207 static Future<bool> identical(String path1, String path2) { |
206 // Get a new file service port for each request. We could also cache one. | 208 // Get a new file service port for each request. We could also cache one. |
207 var service = _FileUtils._newServicePort(); | 209 var service = _FileUtils._newServicePort(); |
208 List request = new List(3); | 210 List request = new List(3); |
209 request[0] = _IDENTICAL_REQUEST; | 211 request[0] = _IDENTICAL_REQUEST; |
210 request[1] = path1; | 212 request[1] = path1; |
211 request[2] = path2; | 213 request[2] = path2; |
212 return service.call(request).then((response) { | 214 return service.call(request).then((response) { |
213 if (_isErrorResponse(response)) { | 215 if (_isErrorResponse(response)) { |
214 throw _exceptionFromResponse(response, | 216 throw _exceptionFromResponse(response, |
215 "Error in FileSystemEntity.identical($path1, $path2)"); | 217 "Error in FileSystemEntity.identical($path1, $path2)"); |
216 } | 218 } |
217 return response; | 219 return response; |
218 }); | 220 }); |
219 } | 221 } |
220 | 222 |
221 | 223 |
222 /** | 224 /** |
223 * Do two paths refer to the same object in the file system? | 225 * Synchronously checks whether two paths refer to the same object in the |
224 * Links are not identical to their targets, and two links | 226 * file system. |
225 * are not identical just because they point to identical targets. | 227 * |
226 * Links in intermediate directories in the paths are followed, though. | 228 * Comparing a link to its target returns false, as does comparing two links |
| 229 * that point to the same target. To check the target of a link, use |
| 230 * Link.target explicitly to fetch it. Directory links appearing |
| 231 * inside a path are followed, though, to find the file system object. |
227 * | 232 * |
228 * Throws an error if one of the paths points to an object that does not | 233 * Throws an error if one of the paths points to an object that does not |
229 * exist. | 234 * exist. |
230 * The target of a link can be compared by first getting it with Link.target. | |
231 */ | 235 */ |
232 static bool identicalSync(String path1, String path2) { | 236 static bool identicalSync(String path1, String path2) { |
233 var result = _identical(path1, path2); | 237 var result = _identical(path1, path2); |
234 _throwIfError(result, 'Error in FileSystemEntity.identicalSync'); | 238 _throwIfError(result, 'Error in FileSystemEntity.identicalSync'); |
235 return result; | 239 return result; |
236 } | 240 } |
237 | 241 |
238 /** | 242 /** |
239 * Check whether the file system entity with this path exists. Returns | 243 * Checks whether the file system entity with this path exists. Returns |
240 * a [:Future<bool>:] that completes with the result. | 244 * a [:Future<bool>:] that completes with the result. |
241 * | 245 * |
242 * Since FileSystemEntity is abstract, every FileSystemEntity object | 246 * Since FileSystemEntity is abstract, every FileSystemEntity object |
243 * is actually an instance of one of the subclasses [File], | 247 * is actually an instance of one of the subclasses [File], |
244 * [Directory], and [Link]. Calling [exists] on an instance of one | 248 * [Directory], and [Link]. Calling [exists] on an instance of one |
245 * of these subclasses checks whether the object exists in the file | 249 * of these subclasses checks whether the object exists in the file |
246 * system object exists and is of the correct type (file, directory, | 250 * system object exists and is of the correct type (file, directory, |
247 * or link). To check whether a path points to an object on the | 251 * or link). To check whether a path points to an object on the |
248 * file system, regardless of the object's type, use the [type] | 252 * file system, regardless of the object's type, use the [type] |
249 * static method. | 253 * static method. |
250 * | 254 * |
251 */ | 255 */ |
252 Future<bool> exists(); | 256 Future<bool> exists(); |
253 | 257 |
254 /** | 258 /** |
255 * Synchronously check whether the file system entity with this path | 259 * Synchronously checks whether the file system entity with this path |
256 * exists. | 260 * exists. |
257 * | 261 * |
258 * Since FileSystemEntity is abstract, every FileSystemEntity object | 262 * Since FileSystemEntity is abstract, every FileSystemEntity object |
259 * is actually an instance of one of the subclasses [File], | 263 * is actually an instance of one of the subclasses [File], |
260 * [Directory], and [Link]. Calling [existsSync] on an instance of | 264 * [Directory], and [Link]. Calling [existsSync] on an instance of |
261 * one of these subclasses checks whether the object exists in the | 265 * one of these subclasses checks whether the object exists in the |
262 * file system object exists and is of the correct type (file, | 266 * file system object exists and is of the correct type (file, |
263 * directory, or link). To check whether a path points to an object | 267 * directory, or link). To check whether a path points to an object |
264 * on the file system, regardless of the object's type, use the | 268 * on the file system, regardless of the object's type, use the |
265 * [typeSync] static method. | 269 * [typeSync] static method. |
266 */ | 270 */ |
267 bool existsSync(); | 271 bool existsSync(); |
268 | 272 |
| 273 /** |
| 274 * Calls the operating system's stat() function on the [path] of this |
| 275 * [FileSystemEntity]. Identical to [:FileStat.stat(this.path):]. |
| 276 * |
| 277 * Returns a [:Future<FileStat>:] object containing the data returned by |
| 278 * stat(). |
| 279 * |
| 280 * If the call fails, completes the future with a [FileStat] object |
| 281 * with .type set to |
| 282 * FileSystemEntityType.NOT_FOUND and the other fields invalid. |
| 283 */ |
| 284 Future<FileStat> stat(); |
| 285 |
| 286 /** |
| 287 * Synchronously calls the operating system's stat() function on the |
| 288 * [path] of this [FileSystemEntity]. |
| 289 * Identical to [:FileStat.statSync(this.path):]. |
| 290 * |
| 291 * Returns a [FileStat] object containing the data returned by stat(). |
| 292 * |
| 293 * If the call fails, returns a [FileStat] object with .type set to |
| 294 * FileSystemEntityType.NOT_FOUND and the other fields invalid. |
| 295 */ |
| 296 FileStat statSync(); |
| 297 |
| 298 |
| 299 /** |
| 300 * Finds the type of file system object that a path points to. Returns |
| 301 * a [:Future<FileSystemEntityType>:] that completes with the result. |
| 302 * |
| 303 * [FileSystemEntityType] has the constant instances FILE, DIRECTORY, |
| 304 * LINK, and NOT_FOUND. [type] will return LINK only if the optional |
| 305 * named argument [followLinks] is false, and [path] points to a link. |
| 306 * If the path does not point to a file system object, or any other error |
| 307 * occurs in looking up the path, NOT_FOUND is returned. The only |
| 308 * error or exception that may be put on the returned future is ArgumentError, |
| 309 * caused by passing the wrong type of arguments to the function. |
| 310 */ |
269 static Future<FileSystemEntityType> type(String path, | 311 static Future<FileSystemEntityType> type(String path, |
270 {bool followLinks: true}) | 312 {bool followLinks: true}) |
271 => _getTypeAsync(path, followLinks).then(FileSystemEntityType._lookup); | 313 => _getTypeAsync(path, followLinks).then(FileSystemEntityType._lookup); |
272 | 314 |
| 315 /** |
| 316 * Synchronously finds the type of file system object that a path points to. |
| 317 * Returns a [FileSystemEntityType]. |
| 318 * |
| 319 * [FileSystemEntityType] has the constant instances FILE, DIRECTORY, |
| 320 * LINK, and NOT_FOUND. [type] will return LINK only if the optional |
| 321 * named argument [followLinks] is false, and [path] points to a link. |
| 322 * If the path does not point to a file system object, or any other error |
| 323 * occurs in looking up the path, NOT_FOUND is returned. The only |
| 324 * error or exception that may be thrown is ArgumentError, |
| 325 * caused by passing the wrong type of arguments to the function. |
| 326 */ |
273 static FileSystemEntityType typeSync(String path, {bool followLinks: true}) | 327 static FileSystemEntityType typeSync(String path, {bool followLinks: true}) |
274 => FileSystemEntityType._lookup(_getTypeSync(path, followLinks)); | 328 => FileSystemEntityType._lookup(_getTypeSync(path, followLinks)); |
275 | 329 |
| 330 |
| 331 /** |
| 332 * Checks if type(path, followLinks: false) returns |
| 333 * FileSystemEntityType.LINK. |
| 334 */ |
276 static Future<bool> isLink(String path) => _getTypeAsync(path, false) | 335 static Future<bool> isLink(String path) => _getTypeAsync(path, false) |
277 .then((type) => (type == FileSystemEntityType.LINK._type)); | 336 .then((type) => (type == FileSystemEntityType.LINK._type)); |
278 | 337 |
| 338 /** |
| 339 * Checks if type(path) returns FileSystemEntityType.FILE. |
| 340 */ |
279 static Future<bool> isFile(String path) => _getTypeAsync(path, true) | 341 static Future<bool> isFile(String path) => _getTypeAsync(path, true) |
280 .then((type) => (type == FileSystemEntityType.FILE._type)); | 342 .then((type) => (type == FileSystemEntityType.FILE._type)); |
281 | 343 |
| 344 /** |
| 345 * Checks if type(path) returns FileSystemEntityType.DIRECTORY. |
| 346 */ |
282 static Future<bool> isDirectory(String path) => _getTypeAsync(path, true) | 347 static Future<bool> isDirectory(String path) => _getTypeAsync(path, true) |
283 .then((type) => (type == FileSystemEntityType.DIRECTORY._type)); | 348 .then((type) => (type == FileSystemEntityType.DIRECTORY._type)); |
284 | 349 |
| 350 /** |
| 351 * Synchronously checks if typeSync(path, followLinks: false) returns |
| 352 * FileSystemEntityType.LINK. |
| 353 */ |
285 static bool isLinkSync(String path) => | 354 static bool isLinkSync(String path) => |
286 (_getTypeSync(path, false) == FileSystemEntityType.LINK._type); | 355 (_getTypeSync(path, false) == FileSystemEntityType.LINK._type); |
287 | 356 |
| 357 /** |
| 358 * Synchronously checks if typeSync(path) returns |
| 359 * FileSystemEntityType.FILE. |
| 360 */ |
288 static bool isFileSync(String path) => | 361 static bool isFileSync(String path) => |
289 (_getTypeSync(path, true) == FileSystemEntityType.FILE._type); | 362 (_getTypeSync(path, true) == FileSystemEntityType.FILE._type); |
290 | 363 |
| 364 /** |
| 365 * Synchronously checks if typeSync(path) returns |
| 366 * FileSystemEntityType.DIRECTORY. |
| 367 */ |
291 static bool isDirectorySync(String path) => | 368 static bool isDirectorySync(String path) => |
292 (_getTypeSync(path, true) == FileSystemEntityType.DIRECTORY._type); | 369 (_getTypeSync(path, true) == FileSystemEntityType.DIRECTORY._type); |
293 | 370 |
| 371 |
294 static _throwIfError(Object result, String msg) { | 372 static _throwIfError(Object result, String msg) { |
295 if (result is OSError) { | 373 if (result is OSError) { |
296 throw new FileIOException(msg, result); | 374 throw new FileIOException(msg, result); |
297 } else if (result is ArgumentError) { | 375 } else if (result is ArgumentError) { |
298 throw result; | 376 throw result; |
299 } | 377 } |
300 } | 378 } |
301 } | 379 } |
OLD | NEW |