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 |
| 25 class FileStat { |
| 26 // These must agree with enum FileStat in file.h. |
| 27 static const TYPE = 0; |
| 28 static const CHANGED_TIME = 1; |
| 29 static const MODIFIED_TIME = 2; |
| 30 static const ACCESSED_TIME = 3; |
| 31 static const MODE = 4; |
| 32 static const SIZE = 5; |
| 33 |
| 34 FileStat._internal(this.changed, |
| 35 this.modified, |
| 36 this.accessed, |
| 37 this.type, |
| 38 this.mode, |
| 39 this.size); |
| 40 |
| 41 external static List<int> _statSync(String path); |
| 42 |
| 43 static FileStat statSync(String path) { |
| 44 var data = _statSync(path); |
| 45 if (data is Error) throw data; |
| 46 return new FileStat._internal( |
| 47 new DateTime.fromMillisecondsSinceEpoch(data[CHANGED_TIME] * 1000), |
| 48 new DateTime.fromMillisecondsSinceEpoch(data[MODIFIED_TIME] * 1000), |
| 49 new DateTime.fromMillisecondsSinceEpoch(data[ACCESSED_TIME] * 1000), |
| 50 FileSystemEntityType._lookup(data[TYPE]), |
| 51 data[MODE], |
| 52 data[SIZE]); |
| 53 } |
| 54 |
| 55 static Future<FileStat> stat(String path) { |
| 56 // Get a new file service port for each request. We could also cache one. |
| 57 var service = _FileUtils._newServicePort(); |
| 58 List request = new List(2); |
| 59 request[0] = _STAT_REQUEST; |
| 60 request[1] = path; |
| 61 return service.call(request).then((response) { |
| 62 if (_isErrorResponse(response)) { |
| 63 throw _exceptionFromResponse(response, |
| 64 "Error getting stat of '$path'"); |
| 65 } |
| 66 // Unwrap the real list from the "I'm not an error" wrapper. |
| 67 List data = response[1]; |
| 68 return new FileStat._internal( |
| 69 new DateTime.fromMillisecondsSinceEpoch(data[CHANGED_TIME] * 1000), |
| 70 new DateTime.fromMillisecondsSinceEpoch(data[MODIFIED_TIME] * 1000), |
| 71 new DateTime.fromMillisecondsSinceEpoch(data[ACCESSED_TIME] * 1000), |
| 72 FileSystemEntityType._lookup(data[TYPE]), |
| 73 data[MODE], |
| 74 data[SIZE]); |
| 75 }); |
| 76 } |
| 77 |
| 78 String toString() => """ |
| 79 FileStat: type $type |
| 80 changed $changed |
| 81 modified $modified |
| 82 accessed $accessed |
| 83 mode ${modeString()} |
| 84 size $size"""; |
| 85 |
| 86 String modeString() { |
| 87 var permissions = mode & 0xFFF; |
| 88 var codes = const ['---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx']; |
| 89 var result = []; |
| 90 if (permissions & 0x800) result.add("(suid) "); |
| 91 if (permissions & 0x400) result.add("(guid) "); |
| 92 if (permissions & 0x200) result.add("(sticky) "); |
| 93 result.add(codes[(permissions >> 6) & 0x7]); |
| 94 result.add(codes[(permissions >> 3) & 0x7]); |
| 95 result.add(codes[permissions & 0x7]); |
| 96 return result.join(); |
| 97 } |
| 98 |
| 99 final DateTime changed; |
| 100 final DateTime modified; |
| 101 final DateTime accessed; |
| 102 final FileSystemEntityType type; |
| 103 final int mode; |
| 104 final int size; |
| 105 } |
| 106 |
| 107 |
24 /** | 108 /** |
25 * A [FileSystemEntity] is a common super class for [File] and | 109 * A [FileSystemEntity] is a common super class for [File] and |
26 * [Directory] objects. | 110 * [Directory] objects. |
27 * | 111 * |
28 * [FileSystemEntity] objects are returned from directory listing | 112 * [FileSystemEntity] objects are returned from directory listing |
29 * operations. To determine if a FileSystemEntity is a [File] or a | 113 * operations. To determine if a FileSystemEntity is a [File] or a |
30 * [Directory], perform a type check: | 114 * [Directory], perform a type check: |
31 * | 115 * |
32 * if (entity is File) (entity as File).readAsStringSync(); | 116 * if (entity is File) (entity as File).readAsStringSync(); |
33 */ | 117 */ |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 (_getTypeSync(path, true) == FileSystemEntityType.DIRECTORY._type); | 212 (_getTypeSync(path, true) == FileSystemEntityType.DIRECTORY._type); |
129 | 213 |
130 static _throwIfError(Object result, String msg) { | 214 static _throwIfError(Object result, String msg) { |
131 if (result is OSError) { | 215 if (result is OSError) { |
132 throw new FileIOException(msg, result); | 216 throw new FileIOException(msg, result); |
133 } else if (result is ArgumentError) { | 217 } else if (result is ArgumentError) { |
134 throw result; | 218 throw result; |
135 } | 219 } |
136 } | 220 } |
137 } | 221 } |
OLD | NEW |