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 17 matching lines...) Expand all Loading... |
28 */ | 28 */ |
29 class FileStat { | 29 class FileStat { |
30 // These must agree with enum FileStat in file.h. | 30 // These must agree with enum FileStat in file.h. |
31 static const _TYPE = 0; | 31 static const _TYPE = 0; |
32 static const _CHANGED_TIME = 1; | 32 static const _CHANGED_TIME = 1; |
33 static const _MODIFIED_TIME = 2; | 33 static const _MODIFIED_TIME = 2; |
34 static const _ACCESSED_TIME = 3; | 34 static const _ACCESSED_TIME = 3; |
35 static const _MODE = 4; | 35 static const _MODE = 4; |
36 static const _SIZE = 5; | 36 static const _SIZE = 5; |
37 | 37 |
| 38 static const _notFound = const FileStat._internalNotFound(); |
| 39 |
38 /** | 40 /** |
39 * The time of the last change to the data or metadata of the file system | 41 * The time of the last change to the data or metadata of the file system |
40 * object. On Windows platforms, this is instead the file creation time. | 42 * object. On Windows platforms, this is instead the file creation time. |
41 */ | 43 */ |
42 final DateTime changed; | 44 final DateTime changed; |
43 /** | 45 /** |
44 * The time of the last change to the data of the file system | 46 * The time of the last change to the data of the file system |
45 * object. | 47 * object. |
46 */ | 48 */ |
47 final DateTime modified; | 49 final DateTime modified; |
(...skipping 18 matching lines...) Expand all Loading... |
66 */ | 68 */ |
67 final int size; | 69 final int size; |
68 | 70 |
69 FileStat._internal(this.changed, | 71 FileStat._internal(this.changed, |
70 this.modified, | 72 this.modified, |
71 this.accessed, | 73 this.accessed, |
72 this.type, | 74 this.type, |
73 this.mode, | 75 this.mode, |
74 this.size); | 76 this.size); |
75 | 77 |
| 78 const FileStat._internalNotFound() : |
| 79 changed = null, modified = null, accessed = null, |
| 80 type = FileSystemEntityType.NOT_FOUND, mode = 0, size = -1; |
| 81 |
76 external static List<int> _statSync(String path); | 82 external static List<int> _statSync(String path); |
77 | 83 |
78 | 84 |
79 /** | 85 /** |
80 * Calls the operating system's stat() function on [path]. | 86 * Calls the operating system's stat() function on [path]. |
81 * Returns a [FileStat] object containing the data returned by stat(). | 87 * Returns a [FileStat] object containing the data returned by stat(). |
82 * If the call fails, returns a [FileStat] object with .type set to | 88 * If the call fails, returns a [FileStat] object with .type set to |
83 * FileSystemEntityType.NOT_FOUND and the other fields invalid. | 89 * FileSystemEntityType.NOT_FOUND and the other fields invalid. |
84 */ | 90 */ |
85 static FileStat statSync(String path) { | 91 static FileStat statSync(String path) { |
86 // Trailing path is not supported on Windows. | 92 // Trailing path is not supported on Windows. |
87 if (Platform.isWindows) { | 93 if (Platform.isWindows) { |
88 path = FileSystemEntity._trimTrailingPathSeparators(path); | 94 path = FileSystemEntity._trimTrailingPathSeparators(path); |
89 } | 95 } |
90 var data = _statSync(path); | 96 var data = _statSync(path); |
91 if (data is OSError) throw data; | 97 if (data is OSError) return FileStat._notFound; |
92 return new FileStat._internal( | 98 return new FileStat._internal( |
93 new DateTime.fromMillisecondsSinceEpoch(data[_CHANGED_TIME] * 1000), | 99 new DateTime.fromMillisecondsSinceEpoch(data[_CHANGED_TIME] * 1000), |
94 new DateTime.fromMillisecondsSinceEpoch(data[_MODIFIED_TIME] * 1000), | 100 new DateTime.fromMillisecondsSinceEpoch(data[_MODIFIED_TIME] * 1000), |
95 new DateTime.fromMillisecondsSinceEpoch(data[_ACCESSED_TIME] * 1000), | 101 new DateTime.fromMillisecondsSinceEpoch(data[_ACCESSED_TIME] * 1000), |
96 FileSystemEntityType._lookup(data[_TYPE]), | 102 FileSystemEntityType._lookup(data[_TYPE]), |
97 data[_MODE], | 103 data[_MODE], |
98 data[_SIZE]); | 104 data[_SIZE]); |
99 } | 105 } |
100 | 106 |
101 /** | 107 /** |
102 * Asynchronously calls the operating system's stat() function on [path]. | 108 * Asynchronously calls the operating system's stat() function on [path]. |
103 * Returns a Future which completes with a [FileStat] object containing | 109 * Returns a Future which completes with a [FileStat] object containing |
104 * the data returned by stat(). | 110 * the data returned by stat(). |
105 * If the call fails, completes the future with a [FileStat] object with | 111 * If the call fails, completes the future with a [FileStat] object with |
106 * .type set to FileSystemEntityType.NOT_FOUND and the other fields invalid. | 112 * .type set to FileSystemEntityType.NOT_FOUND and the other fields invalid. |
107 */ | 113 */ |
108 static Future<FileStat> stat(String path) { | 114 static Future<FileStat> stat(String path) { |
109 // Trailing path is not supported on Windows. | 115 // Trailing path is not supported on Windows. |
110 if (Platform.isWindows) { | 116 if (Platform.isWindows) { |
111 path = FileSystemEntity._trimTrailingPathSeparators(path); | 117 path = FileSystemEntity._trimTrailingPathSeparators(path); |
112 } | 118 } |
113 return _IOService.dispatch(_FILE_STAT, [path]).then((response) { | 119 return _IOService.dispatch(_FILE_STAT, [path]).then((response) { |
114 if (_isErrorResponse(response)) { | 120 if (_isErrorResponse(response)) { |
115 throw _exceptionFromResponse(response, | 121 return FileStat._notFound; |
116 "Error getting stat", | |
117 path); | |
118 } | 122 } |
119 // Unwrap the real list from the "I'm not an error" wrapper. | 123 // Unwrap the real list from the "I'm not an error" wrapper. |
120 List data = response[1]; | 124 List data = response[1]; |
121 return new FileStat._internal( | 125 return new FileStat._internal( |
122 new DateTime.fromMillisecondsSinceEpoch(data[_CHANGED_TIME] * 1000), | 126 new DateTime.fromMillisecondsSinceEpoch(data[_CHANGED_TIME] * 1000), |
123 new DateTime.fromMillisecondsSinceEpoch(data[_MODIFIED_TIME] * 1000), | 127 new DateTime.fromMillisecondsSinceEpoch(data[_MODIFIED_TIME] * 1000), |
124 new DateTime.fromMillisecondsSinceEpoch(data[_ACCESSED_TIME] * 1000), | 128 new DateTime.fromMillisecondsSinceEpoch(data[_ACCESSED_TIME] * 1000), |
125 FileSystemEntityType._lookup(data[_TYPE]), | 129 FileSystemEntityType._lookup(data[_TYPE]), |
126 data[_MODE], | 130 data[_MODE], |
127 data[_SIZE]); | 131 data[_SIZE]); |
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
764 return buffer.toString(); | 768 return buffer.toString(); |
765 } | 769 } |
766 } | 770 } |
767 | 771 |
768 | 772 |
769 class _FileSystemWatcher { | 773 class _FileSystemWatcher { |
770 external static Stream<FileSystemEvent> watch( | 774 external static Stream<FileSystemEvent> watch( |
771 String path, int events, bool recursive); | 775 String path, int events, bool recursive); |
772 external static bool get isSupported; | 776 external static bool get isSupported; |
773 } | 777 } |
OLD | NEW |