Chromium Code Reviews| Index: runtime/bin/file_impl.dart |
| diff --git a/runtime/bin/file_impl.dart b/runtime/bin/file_impl.dart |
| index 4df9e7a4748e5c254fbb04ce8e35e6984af59426..452c9a7479b2d6af2ccff12fc81dd7b87e49443d 100644 |
| --- a/runtime/bin/file_impl.dart |
| +++ b/runtime/bin/file_impl.dart |
| @@ -2,6 +2,8 @@ |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| +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
|
| + |
| class _FileInputStream extends _BaseDataInputStream implements InputStream { |
| _FileInputStream(String name) { |
| var file = new File(name); |
| @@ -225,12 +227,13 @@ class _FileUtils { |
| static final kTruncateRequest = 9; |
| static final kLengthRequest = 10; |
| static final kLengthFromNameRequest = 11; |
| - static final kFlushRequest = 12; |
| - static final kReadByteRequest = 13; |
| - static final kWriteByteRequest = 14; |
| - static final kReadListRequest = 15; |
| - static final kWriteListRequest = 16; |
| - static final kWriteStringRequest = 17; |
| + static final kLastModifiedRequest = 12; |
| + static final kFlushRequest = 13; |
| + static final kReadByteRequest = 14; |
| + static final kWriteByteRequest = 15; |
| + static final kReadListRequest = 16; |
| + static final kWriteListRequest = 17; |
| + static final kWriteStringRequest = 18; |
| static final kSuccessResponse = 0; |
| static final kIllegalArgumentResponse = 1; |
| @@ -274,6 +277,7 @@ class _FileUtils { |
| static fullPath(String name) native "File_FullPath"; |
| static directory(String name) native "File_Directory"; |
| static lengthFromName(String name) native "File_LengthFromName"; |
| + static lastModified(String name) native "File_LastModified"; |
| static int close(int id) native "File_Close"; |
| static readByte(int id) native "File_ReadByte"; |
| static readList(int id, List<int> buffer, int offset, int bytes) |
| @@ -298,83 +302,58 @@ class _FileUtils { |
| static SendPort newServicePort() native "File_NewServicePort"; |
| static bool checkedExists(String name) { |
| - if (name is !String) { |
| - throw new IllegalArgumentException(); |
| - } |
| + if (name is !String) throw new IllegalArgumentException(); |
| var result = exists(name); |
| - if (result is OSError) { |
| - throw new FileIOException("Cannot check existence of file '$name'", |
| - result); |
| - } |
| + throwIfError(result, "Cannot check existence of file '$name'"); |
| return result; |
| } |
| static int checkedOpen(String name, int mode) { |
| - if (name is !String || mode is !int) { |
| - throw new IllegalArgumentException(); |
| - }; |
| + if (name is !String || mode is !int) throw new IllegalArgumentException(); |
| var result = open(name, mode); |
| - if (result is OSError) { |
| - throw new FileIOException("Cannot open file '$name'", result); |
| - } |
| + throwIfError(result, "Cannot open file '$name'"); |
| return result; |
| } |
| static bool checkedCreate(String name) { |
| - if (name is !String) { |
| - throw new IllegalArgumentException(); |
| - }; |
| + if (name is !String) throw new IllegalArgumentException(); |
| var result = create(name); |
| - if (result is OSError) { |
| - throw new FileIOException("Cannot create file '$name'", result); |
| - } |
| + throwIfError(result, "Cannot create file '$name'"); |
| return true; |
| } |
| static bool checkedDelete(String name) { |
| - if (name is !String) { |
| - throw new IllegalArgumentException(); |
| - }; |
| + if (name is !String) throw new IllegalArgumentException(); |
| var result = delete(name); |
| - if (result is OSError) { |
| - throw new FileIOException("Cannot delete file '$name'", result); |
| - } |
| + throwIfError(result, "Cannot delete file '$name'"); |
| return true; |
| } |
| static String checkedFullPath(String name) { |
| - if (name is !String) { |
| - throw new IllegalArgumentException(); |
| - }; |
| + if (name is !String) throw new IllegalArgumentException(); |
| var result = fullPath(name); |
| - if (result is OSError) { |
| - throw new FileIOException( |
| - "Cannot retrieve full path for file '$name'", result); |
| - } |
| + throwIfError(result, "Cannot retrieve full path for file '$name'"); |
| return result; |
| } |
| static String checkedDirectory(String name) { |
| - if (name is !String) { |
| - throw new IllegalArgumentException(); |
| - } |
| + if (name is !String) throw new IllegalArgumentException(); |
| var result = directory(name); |
| - if (result is OSError) { |
| - throw new FileIOException( |
| - "Cannot retrieve directory for file '$name'", result); |
| - } |
| + throwIfError(result, "Cannot retrieve directory for file '$name'"); |
| return result; |
| } |
| static int checkedLengthFromName(String name) { |
| - if (name is !String) { |
| - throw new IllegalArgumentException(); |
| - } |
| + if (name is !String) throw new IllegalArgumentException(); |
| var result = lengthFromName(name); |
| - if (result is OSError) { |
| - throw new FileIOException( |
| - "Cannot retrieve length of file '$name'", result); |
| - } |
| + throwIfError(result, "Cannot retrieve length of file '$name'"); |
| + return result; |
| + } |
| + |
| + static int checkedLastModified(String name) { |
| + if (name is !String) throw new IllegalArgumentException(); |
| + var result = lastModified(name); |
| + throwIfError(result, "Cannot retrieve modification time for file '$name'"); |
| return result; |
| } |
| @@ -386,10 +365,15 @@ class _FileUtils { |
| } |
| static int checkedWriteString(int id, String string) { |
| - if (string is !String) return -1; |
| + if (string is !String) throw new IllegalArgumentException(); |
| return writeString(id, string); |
| } |
| + static throwIfError(Object result, String msg) { |
| + if (result is OSError) { |
| + throw new FileIOException(msg, result); |
| + } |
| + } |
| } |
| // Base class for _File and _RandomAccessFile with shared functions. |
| @@ -533,12 +517,27 @@ class _File extends _FileBase implements File { |
| } |
| int lengthSync() { |
| - var result = _FileUtils.checkedLengthFromName(_name); |
| - if (result is OSError) { |
| - throw new FileIOException("Cannot retrieve length of file '$_name'", |
| - result); |
| - } |
| - return result; |
| + return _FileUtils.checkedLengthFromName(_name); |
| + } |
| + |
| + Future<Date> lastModified() { |
| + _ensureFileService(); |
| + List request = new List(2); |
| + request[0] = _FileUtils.kLastModifiedRequest; |
| + request[1] = _name; |
| + return _fileService.call(request).transform((response) { |
| + if (_isErrorResponse(response)) { |
| + throw _exceptionFromResponse(response, |
| + "Cannot retrieve modification time " |
| + "for file '$_name'"); |
| + } |
| + 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.
|
| + }); |
| + } |
| + |
| + Date lastModifiedSync() { |
| + var result = _FileUtils.checkedLastModified(_name); |
| + return new Date.fromEpoch(result * _MS_PER_SECOND); |
|
Søren Gjesse
2012/05/24 12:43:45
Ditto.
|
| } |
| RandomAccessFile openSync([FileMode mode = FileMode.READ]) { |
| @@ -916,7 +915,7 @@ class _RandomAccessFile extends _FileBase implements RandomAccessFile { |
| _checkNotClosed(); |
| var result = _FileUtils.checkedWriteString(_id, string); |
| if (result is OSError) { |
| - throw new FileIOException("writeString failed for file '$_name'", result); |
| + throw new FileIOException("writeString failed for file '$_name'"); |
| } |
| return result; |
| } |