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); |
11 static const NOT_FOUND = const FileSystemEntityType._internal(3); | 11 static const NOT_FOUND = const FileSystemEntityType._internal(3); |
12 static const _typeList = const [FileSystemEntityType.FILE, | 12 static const _typeList = const [FileSystemEntityType.FILE, |
13 FileSystemEntityType.DIRECTORY, | 13 FileSystemEntityType.DIRECTORY, |
14 FileSystemEntityType.LINK, | 14 FileSystemEntityType.LINK, |
15 FileSystemEntityType.NOT_FOUND]; | 15 FileSystemEntityType.NOT_FOUND]; |
16 const FileSystemEntityType._internal(int this._type); | 16 const FileSystemEntityType._internal(int this._type); |
17 | 17 |
18 static FileSystemEntityType _lookup(int type) => _typeList[type]; | 18 static FileSystemEntityType _lookup(int type) => _typeList[type]; |
19 String toString() => const ['FILE', 'DIRECTORY', 'LINK', 'NOT_FOUND'][_type]; | 19 String toString() => const ['FILE', 'DIRECTORY', 'LINK', 'NOT_FOUND'][_type]; |
20 | 20 |
21 final int _type; | 21 final int _type; |
22 } | 22 } |
23 | 23 |
24 /** | 24 /** |
25 * A FileStat object represents the result of calling the POSIX stat() function | |
26 * on a file system object. It is an immutable object, representing the | |
27 * snapshotted values returned by the stat() call. | |
28 */ | |
29 class FileStat { | |
30 // These must agree with enum FileStat in file.h. | |
31 static const _TYPE = 0; | |
32 static const _CHANGED_TIME = 1; | |
33 static const _MODIFIED_TIME = 2; | |
34 static const _ACCESSED_TIME = 3; | |
35 static const _MODE = 4; | |
36 static const _SIZE = 5; | |
37 | |
38 FileStat._internal(this.changed, | |
39 this.modified, | |
40 this.accessed, | |
41 this.type, | |
42 this.mode, | |
43 this.size); | |
44 | |
45 external static List<int> _statSync(String path); | |
46 | |
47 | |
48 /** | |
49 * Call the operating system's stat() function on [path]. | |
50 * Returns a [FileStat] object containing the data returned by stat(). | |
51 * If the call fails, returns a [FileStat] object with .type set to | |
52 * FileSystemEntityType.NOT_FOUND and the other fields invalid. | |
53 */ | |
54 static FileStat statSync(String path) { | |
55 var data = _statSync(path); | |
56 if (data is Error) throw data; | |
57 return new FileStat._internal( | |
58 new DateTime.fromMillisecondsSinceEpoch(data[_CHANGED_TIME] * 1000), | |
59 new DateTime.fromMillisecondsSinceEpoch(data[_MODIFIED_TIME] * 1000), | |
60 new DateTime.fromMillisecondsSinceEpoch(data[_ACCESSED_TIME] * 1000), | |
61 FileSystemEntityType._lookup(data[_TYPE]), | |
62 data[_MODE], | |
63 data[_SIZE]); | |
64 } | |
65 | |
66 /** | |
67 * Asynchronously call the operating system's stat() function on [path]. | |
68 * Returns a Future which completes with a [FileStat] object containing | |
69 * the data returned by stat(). | |
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. | |
72 */ | |
73 static Future<FileStat> stat(String path) { | |
74 // Get a new file service port for each request. We could also cache one. | |
75 var service = _FileUtils._newServicePort(); | |
76 List request = new List(2); | |
77 request[0] = _STAT_REQUEST; | |
78 request[1] = path; | |
79 return service.call(request).then((response) { | |
80 if (_isErrorResponse(response)) { | |
81 throw _exceptionFromResponse(response, | |
82 "Error getting stat of '$path'"); | |
83 } | |
84 // Unwrap the real list from the "I'm not an error" wrapper. | |
85 List data = response[1]; | |
86 return new FileStat._internal( | |
87 new DateTime.fromMillisecondsSinceEpoch(data[_CHANGED_TIME] * 1000), | |
88 new DateTime.fromMillisecondsSinceEpoch(data[_MODIFIED_TIME] * 1000), | |
89 new DateTime.fromMillisecondsSinceEpoch(data[_ACCESSED_TIME] * 1000), | |
90 FileSystemEntityType._lookup(data[_TYPE]), | |
91 data[_MODE], | |
92 data[_SIZE]); | |
93 }); | |
94 } | |
95 | |
96 String toString() => """ | |
97 FileStat: type $type | |
98 changed $changed | |
99 modified $modified | |
100 accessed $accessed | |
101 mode ${modeString()} | |
102 size $size"""; | |
103 | |
104 /** | |
105 * Returns the mode value as a human-readable string, in the format | |
106 * "rwxrwxrwx", reflecting the user, group, and world permissions to | |
107 * read, write, and execute the file system object, with "-" replacing the | |
108 * letter for missing permissions. Extra permission bits may be represented | |
109 * by prepending "(suid)", "(guid)", and/or "(sticky)" to the mode string. | |
110 */ | |
111 String modeString() { | |
112 var permissions = mode & 0xFFF; | |
113 var codes = const ['---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx']; | |
114 var result = []; | |
Søren Gjesse
2013/05/14 13:50:36
These flags does not match the content of the man
Søren Gjesse
2013/05/14 13:50:36
Want does Windows report for these mode flags?
Bill Hesse
2013/05/14 16:33:58
Windows creates reasonable values for them, based
Bill Hesse
2013/05/14 16:33:58
Those numbers are in octal, and mine are in hex or
Søren Gjesse
2013/05/15 14:06:35
My bad, sorry for the confusion.
| |
115 if ((permissions & 0x800) != 0) result.add("(suid) "); | |
116 if ((permissions & 0x400) != 0) result.add("(guid) "); | |
117 if ((permissions & 0x200) != 0) result.add("(sticky) "); | |
118 result.add(codes[(permissions >> 6) & 0x7]); | |
119 result.add(codes[(permissions >> 3) & 0x7]); | |
120 result.add(codes[permissions & 0x7]); | |
121 return result.join(); | |
122 } | |
123 | |
124 /** | |
125 * The time of the last change to the data or metadata of the file system | |
126 * object. On Windows platforms, this is instead the file creation time. | |
127 */ | |
128 final DateTime changed; | |
129 /** | |
130 * The time of the last change to the data of the file system | |
131 * object. | |
132 */ | |
133 final DateTime modified; | |
134 /** | |
135 * The time of the last access to the data of the file system | |
136 * object. On Windows platforms, this may have 1 day granularity, and be | |
137 * out of date by an hour. | |
138 */ | |
139 final DateTime accessed; | |
140 /** | |
141 * The type of the object (file, directory, or link). If the call to | |
142 * stat() fails, the type of the returned object is NOT_FOUND. | |
143 */ | |
144 final FileSystemEntityType type; | |
145 /** | |
146 * The mode of the file system object. Permissions are encoded in the lower | |
147 * 16 bits of this number, and can be decoded using the [modeString] getter. | |
148 */ | |
149 final int mode; | |
150 /** | |
151 * The size of the file system object. | |
152 */ | |
153 final int size; | |
154 } | |
155 | |
156 | |
157 /** | |
25 * A [FileSystemEntity] is a common super class for [File] and | 158 * A [FileSystemEntity] is a common super class for [File] and |
26 * [Directory] objects. | 159 * [Directory] objects. |
27 * | 160 * |
28 * [FileSystemEntity] objects are returned from directory listing | 161 * [FileSystemEntity] objects are returned from directory listing |
29 * operations. To determine if a FileSystemEntity is a [File] or a | 162 * operations. To determine if a FileSystemEntity is a [File] or a |
30 * [Directory], perform a type check: | 163 * [Directory], perform a type check: |
31 * | 164 * |
32 * if (entity is File) (entity as File).readAsStringSync(); | 165 * if (entity is File) (entity as File).readAsStringSync(); |
33 */ | 166 */ |
34 abstract class FileSystemEntity { | 167 abstract class FileSystemEntity { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 (_getTypeSync(path, true) == FileSystemEntityType.DIRECTORY._type); | 261 (_getTypeSync(path, true) == FileSystemEntityType.DIRECTORY._type); |
129 | 262 |
130 static _throwIfError(Object result, String msg) { | 263 static _throwIfError(Object result, String msg) { |
131 if (result is OSError) { | 264 if (result is OSError) { |
132 throw new FileIOException(msg, result); | 265 throw new FileIOException(msg, result); |
133 } else if (result is ArgumentError) { | 266 } else if (result is ArgumentError) { |
134 throw result; | 267 throw result; |
135 } | 268 } |
136 } | 269 } |
137 } | 270 } |
OLD | NEW |