Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: runtime/bin/directory_impl.dart

Issue 10443008: Implement File.lastModified. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Return ms from C Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/builtin_natives.cc ('k') | runtime/bin/file.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 5
6 class _Directory implements Directory { 6 class _Directory implements Directory {
7 static final kCreateRequest = 0; 7 static final CREATE_REQUEST = 0;
8 static final kDeleteRequest = 1; 8 static final DELETE_REQUEST = 1;
9 static final kExistsRequest = 2; 9 static final EXISTS_REQUEST = 2;
10 static final kCreateTempRequest = 3; 10 static final CREATE_TEMP_REQUEST = 3;
11 static final kListRequest = 4; 11 static final LIST_REQUEST = 4;
12 static final kRenameRequest = 5; 12 static final RENAME_REQUEST = 5;
13 13
14 static final kSuccessResponse = 0; 14 static final SUCCESS_RESPONSE = 0;
15 static final kIllegalArgumentResponse = 1; 15 static final ILLEGAL_ARGUMENT_RESPONSE = 1;
16 static final kOSErrorResponse = 2; 16 static final OSERROR_RESPONSE = 2;
17 17
18 _Directory(String this._path); 18 _Directory(String this._path);
19 _Directory.current() : _path = _current(); 19 _Directory.current() : _path = _current();
20 20
21 static String _current() native "Directory_Current"; 21 static String _current() native "Directory_Current";
22 static _createTemp(String template) native "Directory_CreateTemp"; 22 static _createTemp(String template) native "Directory_CreateTemp";
23 static int _exists(String path) native "Directory_Exists"; 23 static int _exists(String path) native "Directory_Exists";
24 static _create(String path) native "Directory_Create"; 24 static _create(String path) native "Directory_Create";
25 static _delete(String path, bool recursive) native "Directory_Delete"; 25 static _delete(String path, bool recursive) native "Directory_Delete";
26 static _rename(String path, String newPath) native "Directory_Rename"; 26 static _rename(String path, String newPath) native "Directory_Rename";
27 static SendPort _newServicePort() native "Directory_NewServicePort"; 27 static SendPort _newServicePort() native "Directory_NewServicePort";
28 28
29 Future<bool> exists() { 29 Future<bool> exists() {
30 _ensureDirectoryService(); 30 _ensureDirectoryService();
31 List request = new List(2); 31 List request = new List(2);
32 request[0] = kExistsRequest; 32 request[0] = EXISTS_REQUEST;
33 request[1] = _path; 33 request[1] = _path;
34 return _directoryService.call(request).transform((response) { 34 return _directoryService.call(request).transform((response) {
35 if (_isErrorResponse(response)) { 35 if (_isErrorResponse(response)) {
36 throw _exceptionFromResponse(response, "Exists failed"); 36 throw _exceptionFromResponse(response, "Exists failed");
37 } 37 }
38 return response == 1; 38 return response == 1;
39 }); 39 });
40 } 40 }
41 41
42 bool existsSync() { 42 bool existsSync() {
43 if (_path is !String) { 43 if (_path is !String) {
44 throw new IllegalArgumentException(); 44 throw new IllegalArgumentException();
45 } 45 }
46 var result = _exists(_path); 46 var result = _exists(_path);
47 if (result is OSError) { 47 if (result is OSError) {
48 throw new DirectoryIOException("Exists failed", _path, result); 48 throw new DirectoryIOException("Exists failed", _path, result);
49 } 49 }
50 return (result == 1); 50 return (result == 1);
51 } 51 }
52 52
53 Future<Directory> create() { 53 Future<Directory> create() {
54 _ensureDirectoryService(); 54 _ensureDirectoryService();
55 List request = new List(2); 55 List request = new List(2);
56 request[0] = kCreateRequest; 56 request[0] = CREATE_REQUEST;
57 request[1] = _path; 57 request[1] = _path;
58 return _directoryService.call(request).transform((response) { 58 return _directoryService.call(request).transform((response) {
59 if (_isErrorResponse(response)) { 59 if (_isErrorResponse(response)) {
60 throw _exceptionFromResponse(response, "Creation failed"); 60 throw _exceptionFromResponse(response, "Creation failed");
61 } 61 }
62 return this; 62 return this;
63 }); 63 });
64 } 64 }
65 65
66 void createSync() { 66 void createSync() {
67 if (_path is !String) { 67 if (_path is !String) {
68 throw new IllegalArgumentException(); 68 throw new IllegalArgumentException();
69 } 69 }
70 var result = _create(_path); 70 var result = _create(_path);
71 if (result is OSError) { 71 if (result is OSError) {
72 throw new DirectoryIOException("Creation failed", _path, result); 72 throw new DirectoryIOException("Creation failed", _path, result);
73 } 73 }
74 } 74 }
75 75
76 Future<Directory> createTemp() { 76 Future<Directory> createTemp() {
77 _ensureDirectoryService(); 77 _ensureDirectoryService();
78 List request = new List(2); 78 List request = new List(2);
79 request[0] = kCreateTempRequest; 79 request[0] = CREATE_TEMP_REQUEST;
80 request[1] = _path; 80 request[1] = _path;
81 return _directoryService.call(request).transform((response) { 81 return _directoryService.call(request).transform((response) {
82 if (_isErrorResponse(response)) { 82 if (_isErrorResponse(response)) {
83 throw _exceptionFromResponse(response, 83 throw _exceptionFromResponse(response,
84 "Creation of temporary directory failed"); 84 "Creation of temporary directory failed");
85 } 85 }
86 return new Directory(response); 86 return new Directory(response);
87 }); 87 });
88 } 88 }
89 89
90 void createTempSync() { 90 void createTempSync() {
91 if (_path is !String) { 91 if (_path is !String) {
92 throw new IllegalArgumentException(); 92 throw new IllegalArgumentException();
93 } 93 }
94 var result = _createTemp(path); 94 var result = _createTemp(path);
95 if (result is OSError) { 95 if (result is OSError) {
96 throw new DirectoryIOException("Creation of temporary directory failed", 96 throw new DirectoryIOException("Creation of temporary directory failed",
97 _path, 97 _path,
98 result); 98 result);
99 } 99 }
100 return new Directory(result); 100 return new Directory(result);
101 } 101 }
102 102
103 Future<Directory> _deleteHelper(bool recursive, String errorMsg) { 103 Future<Directory> _deleteHelper(bool recursive, String errorMsg) {
104 _ensureDirectoryService(); 104 _ensureDirectoryService();
105 List request = new List(3); 105 List request = new List(3);
106 request[0] = kDeleteRequest; 106 request[0] = DELETE_REQUEST;
107 request[1] = _path; 107 request[1] = _path;
108 request[2] = recursive; 108 request[2] = recursive;
109 return _directoryService.call(request).transform((response) { 109 return _directoryService.call(request).transform((response) {
110 if (_isErrorResponse(response)) { 110 if (_isErrorResponse(response)) {
111 throw _exceptionFromResponse(response, errorMsg); 111 throw _exceptionFromResponse(response, errorMsg);
112 } 112 }
113 return this; 113 return this;
114 }); 114 });
115 return completer.future; 115 return completer.future;
116 } 116 }
(...skipping 22 matching lines...) Expand all
139 } 139 }
140 var result = _delete(_path, true); 140 var result = _delete(_path, true);
141 if (result is OSError) { 141 if (result is OSError) {
142 throw new DirectoryIOException("Deletion failed", _path, result); 142 throw new DirectoryIOException("Deletion failed", _path, result);
143 } 143 }
144 } 144 }
145 145
146 Future<Directory> rename(String newPath) { 146 Future<Directory> rename(String newPath) {
147 _ensureDirectoryService(); 147 _ensureDirectoryService();
148 List request = new List(3); 148 List request = new List(3);
149 request[0] = kRenameRequest; 149 request[0] = RENAME_REQUEST;
150 request[1] = _path; 150 request[1] = _path;
151 request[2] = newPath; 151 request[2] = newPath;
152 return _directoryService.call(request).transform((response) { 152 return _directoryService.call(request).transform((response) {
153 if (_isErrorResponse(response)) { 153 if (_isErrorResponse(response)) {
154 throw _exceptionFromResponse(response, "Rename failed"); 154 throw _exceptionFromResponse(response, "Rename failed");
155 } 155 }
156 return new Directory(newPath); 156 return new Directory(newPath);
157 }); 157 });
158 } 158 }
159 159
160 Directory renameSync(String newPath) { 160 Directory renameSync(String newPath) {
161 if (_path is !String || newPath is !String) { 161 if (_path is !String || newPath is !String) {
162 throw new IllegalArgumentException(); 162 throw new IllegalArgumentException();
163 } 163 }
164 var result = _rename(_path, newPath); 164 var result = _rename(_path, newPath);
165 if (result is OSError) { 165 if (result is OSError) {
166 throw new DirectoryIOException("Rename failed", _path, result); 166 throw new DirectoryIOException("Rename failed", _path, result);
167 } 167 }
168 return new Directory(newPath); 168 return new Directory(newPath);
169 } 169 }
170 170
171 DirectoryLister list([bool recursive = false]) { 171 DirectoryLister list([bool recursive = false]) {
172 return new _DirectoryLister(_path, recursive); 172 return new _DirectoryLister(_path, recursive);
173 } 173 }
174 174
175 String get path() { return _path; } 175 String get path() { return _path; }
176 176
177 bool _isErrorResponse(response) { 177 bool _isErrorResponse(response) {
178 return response is List && response[0] != _FileUtils.kSuccessResponse; 178 return response is List && response[0] != _FileUtils.SUCCESS_RESPONSE;
179 } 179 }
180 180
181 Exception _exceptionFromResponse(response, String message) { 181 Exception _exceptionFromResponse(response, String message) {
182 assert(_isErrorResponse(response)); 182 assert(_isErrorResponse(response));
183 switch (response[_FileUtils.kErrorResponseErrorType]) { 183 switch (response[_FileUtils.ERROR_RESPONSE_ERROR_TYPE]) {
184 case _FileUtils.kIllegalArgumentResponse: 184 case _FileUtils.ILLEGAL_ARGUMENT_RESPONSE:
185 return new IllegalArgumentException(); 185 return new IllegalArgumentException();
186 case _FileUtils.kOSErrorResponse: 186 case _FileUtils.OSERROR_RESPONSE:
187 var err = new OSError(response[_FileUtils.kOSErrorResponseMessage], 187 var err = new OSError(response[_FileUtils.OSERROR_RESPONSE_MESSAGE],
188 response[_FileUtils.kOSErrorResponseErrorCode]); 188 response[_FileUtils.OSERROR_RESPONSE_ERROR_CODE]);
189 return new DirectoryIOException(message, _path, err); 189 return new DirectoryIOException(message, _path, err);
190 default: 190 default:
191 return new Exception("Unknown error"); 191 return new Exception("Unknown error");
192 } 192 }
193 } 193 }
194 194
195 void _ensureDirectoryService() { 195 void _ensureDirectoryService() {
196 if (_directoryService == null) { 196 if (_directoryService == null) {
197 _directoryService = _newServicePort(); 197 _directoryService = _newServicePort();
198 } 198 }
199 } 199 }
200 200
201 final String _path; 201 final String _path;
202 SendPort _directoryService; 202 SendPort _directoryService;
203 } 203 }
204 204
205 class _DirectoryLister implements DirectoryLister { 205 class _DirectoryLister implements DirectoryLister {
206 _DirectoryLister(String path, bool recursive) { 206 _DirectoryLister(String path, bool recursive) {
207 final int kListDirectory = 0; 207 final int LIST_DIRECTORY = 0;
208 final int kListFile = 1; 208 final int LIST_FILE = 1;
209 final int kListError = 2; 209 final int LIST_ERROR = 2;
210 final int kListDone = 3; 210 final int LIST_DONE = 3;
211 211
212 final int kResponseType = 0; 212 final int RESPONSE_TYPE = 0;
213 final int kResponsePath = 1; 213 final int RESPONSE_PATH = 1;
214 final int kResponseComplete = 1; 214 final int RESPONSE_COMPLETE = 1;
215 final int kResponseError = 2; 215 final int RESPONSE_ERROR = 2;
216 216
217 List request = new List(3); 217 List request = new List(3);
218 request[0] = _Directory.kListRequest; 218 request[0] = _Directory.LIST_REQUEST;
219 request[1] = path; 219 request[1] = path;
220 request[2] = recursive; 220 request[2] = recursive;
221 ReceivePort responsePort = new ReceivePort(); 221 ReceivePort responsePort = new ReceivePort();
222 // Use a separate directory service port for each listing as 222 // Use a separate directory service port for each listing as
223 // listing operations on the same directory can run in parallel. 223 // listing operations on the same directory can run in parallel.
224 _Directory._newServicePort().send(request, responsePort.toSendPort()); 224 _Directory._newServicePort().send(request, responsePort.toSendPort());
225 responsePort.receive((message, replyTo) { 225 responsePort.receive((message, replyTo) {
226 if (message is !List || message[kResponseType] is !int) { 226 if (message is !List || message[RESPONSE_TYPE] is !int) {
227 responsePort.close(); 227 responsePort.close();
228 _reportError(new DirectoryIOException("Internal error")); 228 _reportError(new DirectoryIOException("Internal error"));
229 return; 229 return;
230 } 230 }
231 switch (message[kResponseType]) { 231 switch (message[RESPONSE_TYPE]) {
232 case kListDirectory: 232 case LIST_DIRECTORY:
233 if (_onDir != null) _onDir(message[kResponsePath]); 233 if (_onDir != null) _onDir(message[RESPONSE_PATH]);
234 break; 234 break;
235 case kListFile: 235 case LIST_FILE:
236 if (_onFile != null) _onFile(message[kResponsePath]); 236 if (_onFile != null) _onFile(message[RESPONSE_PATH]);
237 break; 237 break;
238 case kListError: 238 case LIST_ERROR:
239 var errorType = 239 var errorType =
240 message[kResponseError][_FileUtils.kErrorResponseErrorType]; 240 message[RESPONSE_ERROR][_FileUtils.ERROR_RESPONSE_ERROR_TYPE];
241 if (errorType == _FileUtils.kIllegalArgumentResponse) { 241 if (errorType == _FileUtils.ILLEGAL_ARGUMENT_RESPONSE) {
242 _reportError(new IllegalArgumentException()); 242 _reportError(new IllegalArgumentException());
243 } else if (errorType == _FileUtils.kOSErrorResponse) { 243 } else if (errorType == _FileUtils.OSERROR_RESPONSE) {
244 var responseError = message[RESPONSE_ERROR];
244 var err = new OSError( 245 var err = new OSError(
245 message[kResponseError][_FileUtils.kOSErrorResponseMessage], 246 responseError[_FileUtils.OSERROR_RESPONSE_MESSAGE],
246 message[kResponseError][_FileUtils.kOSErrorResponseErrorCode]); 247 responseError[_FileUtils.OSERROR_RESPONSE_ERROR_CODE]);
247 var errorPath = message[kResponsePath]; 248 var errorPath = message[RESPONSE_PATH];
248 if (errorPath == null) errorPath = path; 249 if (errorPath == null) errorPath = path;
249 _reportError(new DirectoryIOException("Directory listing failed", 250 _reportError(new DirectoryIOException("Directory listing failed",
250 errorPath, 251 errorPath,
251 err)); 252 err));
252 } else { 253 } else {
253 _reportError(new DirectoryIOException("Internal error")); 254 _reportError(new DirectoryIOException("Internal error"));
254 } 255 }
255 break; 256 break;
256 case kListDone: 257 case LIST_DONE:
257 responsePort.close(); 258 responsePort.close();
258 if (_onDone != null) _onDone(message[kResponseComplete]); 259 if (_onDone != null) _onDone(message[RESPONSE_COMPLETE]);
259 break; 260 break;
260 } 261 }
261 }); 262 });
262 } 263 }
263 264
264 void set onDir(void onDir(String dir)) { 265 void set onDir(void onDir(String dir)) {
265 _onDir = onDir; 266 _onDir = onDir;
266 } 267 }
267 268
268 void set onFile(void onFile(String file)) { 269 void set onFile(void onFile(String file)) {
(...skipping 14 matching lines...) Expand all
283 } else { 284 } else {
284 throw e; 285 throw e;
285 } 286 }
286 } 287 }
287 288
288 Function _onDir; 289 Function _onDir;
289 Function _onFile; 290 Function _onFile;
290 Function _onDone; 291 Function _onDone;
291 Function _onError; 292 Function _onError;
292 } 293 }
OLDNEW
« no previous file with comments | « runtime/bin/builtin_natives.cc ('k') | runtime/bin/file.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698