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

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

Issue 10443008: Implement File.lastModified. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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
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 final int _MS_PER_SECOND = 1000;
Anders Johnsen 2012/05/24 12:05:42 Could this be renamed to _kMsPerSecond? Also, woul
Mads Ager (google) 2012/05/24 12:09:21 No, that is not the style we have for constants in
Anders Johnsen 2012/05/24 12:12:59 I stand corrected. We do have a ton of k... consta
Mads Ager (google) 2012/05/24 12:18:43 That is true. We should update those. I do prefer
6
5 class _FileInputStream extends _BaseDataInputStream implements InputStream { 7 class _FileInputStream extends _BaseDataInputStream implements InputStream {
6 _FileInputStream(String name) { 8 _FileInputStream(String name) {
7 var file = new File(name); 9 var file = new File(name);
8 _data = []; 10 _data = [];
9 _position = 0; 11 _position = 0;
10 var chained = file.open(FileMode.READ).chain((openedFile) { 12 var chained = file.open(FileMode.READ).chain((openedFile) {
11 return _readDataFromFile(openedFile); 13 return _readDataFromFile(openedFile);
12 }); 14 });
13 chained.handleException((e) { 15 chained.handleException((e) {
14 _reportError(e); 16 _reportError(e);
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 static final kDeleteRequest = 2; 220 static final kDeleteRequest = 2;
219 static final kOpenRequest = 3; 221 static final kOpenRequest = 3;
220 static final kFullPathRequest = 4; 222 static final kFullPathRequest = 4;
221 static final kDirectoryRequest = 5; 223 static final kDirectoryRequest = 5;
222 static final kCloseRequest = 6; 224 static final kCloseRequest = 6;
223 static final kPositionRequest = 7; 225 static final kPositionRequest = 7;
224 static final kSetPositionRequest = 8; 226 static final kSetPositionRequest = 8;
225 static final kTruncateRequest = 9; 227 static final kTruncateRequest = 9;
226 static final kLengthRequest = 10; 228 static final kLengthRequest = 10;
227 static final kLengthFromNameRequest = 11; 229 static final kLengthFromNameRequest = 11;
228 static final kFlushRequest = 12; 230 static final kLastModifiedRequest = 12;
229 static final kReadByteRequest = 13; 231 static final kFlushRequest = 13;
230 static final kWriteByteRequest = 14; 232 static final kReadByteRequest = 14;
231 static final kReadListRequest = 15; 233 static final kWriteByteRequest = 15;
232 static final kWriteListRequest = 16; 234 static final kReadListRequest = 16;
233 static final kWriteStringRequest = 17; 235 static final kWriteListRequest = 17;
236 static final kWriteStringRequest = 18;
234 237
235 static final kSuccessResponse = 0; 238 static final kSuccessResponse = 0;
236 static final kIllegalArgumentResponse = 1; 239 static final kIllegalArgumentResponse = 1;
237 static final kOSErrorResponse = 2; 240 static final kOSErrorResponse = 2;
238 static final kFileClosedResponse = 3; 241 static final kFileClosedResponse = 3;
239 242
240 static final kErrorResponseErrorType = 0; 243 static final kErrorResponseErrorType = 0;
241 static final kOSErrorResponseErrorCode = 1; 244 static final kOSErrorResponseErrorCode = 1;
242 static final kOSErrorResponseMessage = 2; 245 static final kOSErrorResponseMessage = 2;
243 246
(...skipping 23 matching lines...) Expand all
267 return [outBuffer, outOffset]; 270 return [outBuffer, outOffset];
268 } 271 }
269 272
270 static exists(String name) native "File_Exists"; 273 static exists(String name) native "File_Exists";
271 static open(String name, int mode) native "File_Open"; 274 static open(String name, int mode) native "File_Open";
272 static create(String name) native "File_Create"; 275 static create(String name) native "File_Create";
273 static delete(String name) native "File_Delete"; 276 static delete(String name) native "File_Delete";
274 static fullPath(String name) native "File_FullPath"; 277 static fullPath(String name) native "File_FullPath";
275 static directory(String name) native "File_Directory"; 278 static directory(String name) native "File_Directory";
276 static lengthFromName(String name) native "File_LengthFromName"; 279 static lengthFromName(String name) native "File_LengthFromName";
280 static lastModified(String name) native "File_LastModified";
277 static int close(int id) native "File_Close"; 281 static int close(int id) native "File_Close";
278 static readByte(int id) native "File_ReadByte"; 282 static readByte(int id) native "File_ReadByte";
279 static readList(int id, List<int> buffer, int offset, int bytes) 283 static readList(int id, List<int> buffer, int offset, int bytes)
280 native "File_ReadList"; 284 native "File_ReadList";
281 static writeByte(int id, int value) native "File_WriteByte"; 285 static writeByte(int id, int value) native "File_WriteByte";
282 static writeList(int id, List<int> buffer, int offset, int bytes) { 286 static writeList(int id, List<int> buffer, int offset, int bytes) {
283 List result = 287 List result =
284 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); 288 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes);
285 List outBuffer = result[0]; 289 List outBuffer = result[0];
286 int outOffset = result[1]; 290 int outOffset = result[1];
287 return writeListNative(id, outBuffer, outOffset, bytes); 291 return writeListNative(id, outBuffer, outOffset, bytes);
288 } 292 }
289 static writeListNative(int id, List<int> buffer, int offset, int bytes) 293 static writeListNative(int id, List<int> buffer, int offset, int bytes)
290 native "File_WriteList"; 294 native "File_WriteList";
291 static writeString(int id, String string) native "File_WriteString"; 295 static writeString(int id, String string) native "File_WriteString";
292 static position(int id) native "File_Position"; 296 static position(int id) native "File_Position";
293 static setPosition(int id, int position) native "File_SetPosition"; 297 static setPosition(int id, int position) native "File_SetPosition";
294 static truncate(int id, int length) native "File_Truncate"; 298 static truncate(int id, int length) native "File_Truncate";
295 static length(int id) native "File_Length"; 299 static length(int id) native "File_Length";
296 static flush(int id) native "File_Flush"; 300 static flush(int id) native "File_Flush";
297 static int openStdio(int fd) native "File_OpenStdio"; 301 static int openStdio(int fd) native "File_OpenStdio";
298 static SendPort newServicePort() native "File_NewServicePort"; 302 static SendPort newServicePort() native "File_NewServicePort";
299 303
300 static bool checkedExists(String name) { 304 static bool checkedExists(String name) {
301 if (name is !String) { 305 if (name is !String) throw new IllegalArgumentException();
302 throw new IllegalArgumentException();
303 }
304 var result = exists(name); 306 var result = exists(name);
305 if (result is OSError) { 307 throwIfError(result, "Cannot check existence of file '$name'");
306 throw new FileIOException("Cannot check existence of file '$name'",
307 result);
308 }
309 return result; 308 return result;
310 } 309 }
311 310
312 static int checkedOpen(String name, int mode) { 311 static int checkedOpen(String name, int mode) {
313 if (name is !String || mode is !int) { 312 if (name is !String || mode is !int) throw new IllegalArgumentException();
314 throw new IllegalArgumentException();
315 };
316 var result = open(name, mode); 313 var result = open(name, mode);
317 if (result is OSError) { 314 throwIfError(result, "Cannot open file '$name'");
318 throw new FileIOException("Cannot open file '$name'", result);
319 }
320 return result; 315 return result;
321 } 316 }
322 317
323 static bool checkedCreate(String name) { 318 static bool checkedCreate(String name) {
324 if (name is !String) { 319 if (name is !String) throw new IllegalArgumentException();
325 throw new IllegalArgumentException();
326 };
327 var result = create(name); 320 var result = create(name);
328 if (result is OSError) { 321 throwIfError(result, "Cannot create file '$name'");
329 throw new FileIOException("Cannot create file '$name'", result);
330 }
331 return true; 322 return true;
332 } 323 }
333 324
334 static bool checkedDelete(String name) { 325 static bool checkedDelete(String name) {
335 if (name is !String) { 326 if (name is !String) throw new IllegalArgumentException();
336 throw new IllegalArgumentException();
337 };
338 var result = delete(name); 327 var result = delete(name);
339 if (result is OSError) { 328 throwIfError(result, "Cannot delete file '$name'");
340 throw new FileIOException("Cannot delete file '$name'", result);
341 }
342 return true; 329 return true;
343 } 330 }
344 331
345 static String checkedFullPath(String name) { 332 static String checkedFullPath(String name) {
346 if (name is !String) { 333 if (name is !String) throw new IllegalArgumentException();
347 throw new IllegalArgumentException();
348 };
349 var result = fullPath(name); 334 var result = fullPath(name);
350 if (result is OSError) { 335 throwIfError(result, "Cannot retrieve full path for file '$name'");
351 throw new FileIOException(
352 "Cannot retrieve full path for file '$name'", result);
353 }
354 return result; 336 return result;
355 } 337 }
356 338
357 static String checkedDirectory(String name) { 339 static String checkedDirectory(String name) {
358 if (name is !String) { 340 if (name is !String) throw new IllegalArgumentException();
359 throw new IllegalArgumentException();
360 }
361 var result = directory(name); 341 var result = directory(name);
362 if (result is OSError) { 342 throwIfError(result, "Cannot retrieve directory for file '$name'");
363 throw new FileIOException(
364 "Cannot retrieve directory for file '$name'", result);
365 }
366 return result; 343 return result;
367 } 344 }
368 345
369 static int checkedLengthFromName(String name) { 346 static int checkedLengthFromName(String name) {
370 if (name is !String) { 347 if (name is !String) throw new IllegalArgumentException();
371 throw new IllegalArgumentException();
372 }
373 var result = lengthFromName(name); 348 var result = lengthFromName(name);
374 if (result is OSError) { 349 throwIfError(result, "Cannot retrieve length of file '$name'");
375 throw new FileIOException(
376 "Cannot retrieve length of file '$name'", result);
377 }
378 return result; 350 return result;
379 } 351 }
380 352
353 static int checkedLastModified(String name) {
354 if (name is !String) throw new IllegalArgumentException();
355 var result = lastModified(name);
356 throwIfError(result, "Cannot retrieve modification time for file '$name'");
357 return result;
358 }
359
381 static int checkReadWriteListArguments(int length, int offset, int bytes) { 360 static int checkReadWriteListArguments(int length, int offset, int bytes) {
382 if (offset < 0) return offset; 361 if (offset < 0) return offset;
383 if (bytes < 0) return bytes; 362 if (bytes < 0) return bytes;
384 if ((offset + bytes) > length) return offset + bytes; 363 if ((offset + bytes) > length) return offset + bytes;
385 return 0; 364 return 0;
386 } 365 }
387 366
388 static int checkedWriteString(int id, String string) { 367 static int checkedWriteString(int id, String string) {
389 if (string is !String) return -1; 368 if (string is !String) throw new IllegalArgumentException();
390 return writeString(id, string); 369 return writeString(id, string);
391 } 370 }
392 371
372 static throwIfError(Object result, String msg) {
373 if (result is OSError) {
374 throw new FileIOException(msg, result);
375 }
376 }
393 } 377 }
394 378
395 // Base class for _File and _RandomAccessFile with shared functions. 379 // Base class for _File and _RandomAccessFile with shared functions.
396 class _FileBase { 380 class _FileBase {
397 bool _isErrorResponse(response) { 381 bool _isErrorResponse(response) {
398 return response is List && response[0] != _FileUtils.kSuccessResponse; 382 return response is List && response[0] != _FileUtils.kSuccessResponse;
399 } 383 }
400 384
401 Exception _exceptionFromResponse(response, String message) { 385 Exception _exceptionFromResponse(response, String message) {
402 assert(_isErrorResponse(response)); 386 assert(_isErrorResponse(response));
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
526 if (_isErrorResponse(response)) { 510 if (_isErrorResponse(response)) {
527 throw _exceptionFromResponse(response, 511 throw _exceptionFromResponse(response,
528 "Cannot retrieve length of " 512 "Cannot retrieve length of "
529 "file '$_name'"); 513 "file '$_name'");
530 } 514 }
531 return response; 515 return response;
532 }); 516 });
533 } 517 }
534 518
535 int lengthSync() { 519 int lengthSync() {
536 var result = _FileUtils.checkedLengthFromName(_name); 520 return _FileUtils.checkedLengthFromName(_name);
537 if (result is OSError) { 521 }
538 throw new FileIOException("Cannot retrieve length of file '$_name'", 522
539 result); 523 Future<Date> lastModified() {
540 } 524 _ensureFileService();
541 return result; 525 List request = new List(2);
526 request[0] = _FileUtils.kLastModifiedRequest;
527 request[1] = _name;
528 return _fileService.call(request).transform((response) {
529 if (_isErrorResponse(response)) {
530 throw _exceptionFromResponse(response,
531 "Cannot retrieve modification time "
532 "for file '$_name'");
533 }
534 return new Date.fromEpoch(response * _MS_PER_SECOND);
Søren Gjesse 2012/05/24 12:43:45 How about having the result be in ms so we don't h
Mads Ager (google) 2012/05/24 13:09:41 Good call. That makes sense. Will do.
535 });
536 }
537
538 Date lastModifiedSync() {
539 var result = _FileUtils.checkedLastModified(_name);
540 return new Date.fromEpoch(result * _MS_PER_SECOND);
Søren Gjesse 2012/05/24 12:43:45 Ditto.
542 } 541 }
543 542
544 RandomAccessFile openSync([FileMode mode = FileMode.READ]) { 543 RandomAccessFile openSync([FileMode mode = FileMode.READ]) {
545 if (mode != FileMode.READ && 544 if (mode != FileMode.READ &&
546 mode != FileMode.WRITE && 545 mode != FileMode.WRITE &&
547 mode != FileMode.APPEND) { 546 mode != FileMode.APPEND) {
548 throw new FileIOException("Unknown file mode. Use FileMode.READ, " 547 throw new FileIOException("Unknown file mode. Use FileMode.READ, "
549 "FileMode.WRITE or FileMode.APPEND."); 548 "FileMode.WRITE or FileMode.APPEND.");
550 } 549 }
551 var id = _FileUtils.checkedOpen(_name, mode._mode); 550 var id = _FileUtils.checkedOpen(_name, mode._mode);
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
909 "writeString failed for file '$_name'"); 908 "writeString failed for file '$_name'");
910 } 909 }
911 return this; 910 return this;
912 }); 911 });
913 } 912 }
914 913
915 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { 914 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) {
916 _checkNotClosed(); 915 _checkNotClosed();
917 var result = _FileUtils.checkedWriteString(_id, string); 916 var result = _FileUtils.checkedWriteString(_id, string);
918 if (result is OSError) { 917 if (result is OSError) {
919 throw new FileIOException("writeString failed for file '$_name'", result); 918 throw new FileIOException("writeString failed for file '$_name'");
920 } 919 }
921 return result; 920 return result;
922 } 921 }
923 922
924 Future<int> position() { 923 Future<int> position() {
925 _ensureFileService(); 924 _ensureFileService();
926 Completer<int> completer = new Completer<int>(); 925 Completer<int> completer = new Completer<int>();
927 if (_isClosed) return _completeWithClosedException(completer); 926 if (_isClosed) return _completeWithClosedException(completer);
928 List request = new List(2); 927 List request = new List(2);
929 request[0] = _FileUtils.kPositionRequest; 928 request[0] = _FileUtils.kPositionRequest;
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
1067 new FileIOException("File closed '$_name'")); 1066 new FileIOException("File closed '$_name'"));
1068 }); 1067 });
1069 return completer.future; 1068 return completer.future;
1070 } 1069 }
1071 1070
1072 final String _name; 1071 final String _name;
1073 int _id; 1072 int _id;
1074 1073
1075 SendPort _fileService; 1074 SendPort _fileService;
1076 } 1075 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698