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 // Read the file in blocks of size 64k. | 7 // Read the file in blocks of size 64k. |
8 const int _BLOCK_SIZE = 64 * 1024; | 8 const int _BLOCK_SIZE = 64 * 1024; |
9 | 9 |
10 | 10 |
(...skipping 922 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
933 external static _flush(int id); | 933 external static _flush(int id); |
934 | 934 |
935 void flushSync() { | 935 void flushSync() { |
936 _checkAvailable(); | 936 _checkAvailable(); |
937 var result = _flush(_id); | 937 var result = _flush(_id); |
938 if (result is OSError) { | 938 if (result is OSError) { |
939 throw new FileSystemException("flush failed", path, result); | 939 throw new FileSystemException("flush failed", path, result); |
940 } | 940 } |
941 } | 941 } |
942 | 942 |
| 943 static final int LOCK_UNLOCK = 0; |
| 944 static final int LOCK_SHARED = 1; |
| 945 static final int LOCK_EXCLUSIVE = 2; |
| 946 |
| 947 Future<RandomAccessFile> lock( |
| 948 [FileLock mode = FileLock.EXCLUSIVE, int start = 0, int end]) { |
| 949 if ((start != null && start is !int) || |
| 950 (end != null && end is !int) || |
| 951 mode is !FileLock) { |
| 952 throw new ArgumentError(); |
| 953 } |
| 954 if (start == null) start = 0; |
| 955 if (end == null) end = -1; |
| 956 if (start < 0 || end < -1 || (end != -1 && start >= end)) { |
| 957 throw new ArgumentError(); |
| 958 } |
| 959 int lock = mode == FileLock.EXCLUSIVE ? LOCK_EXCLUSIVE : LOCK_SHARED; |
| 960 return _dispatch(_FILE_LOCK, [_id, lock, start, end]) |
| 961 .then((response) { |
| 962 if (_isErrorResponse(response)) { |
| 963 throw _exceptionFromResponse(response, 'lock failed', path); |
| 964 } |
| 965 return this; |
| 966 }); |
| 967 } |
| 968 |
| 969 Future<RandomAccessFile> unlock([int start = 0, int end]) { |
| 970 if ((start != null && start is !int) || |
| 971 (end != null && end is !int)) { |
| 972 throw new ArgumentError(); |
| 973 } |
| 974 if (start == null) start = 0; |
| 975 if (end == null) end = -1; |
| 976 if (start == end) throw new ArgumentError(); |
| 977 return _dispatch(_FILE_LOCK, [_id, LOCK_UNLOCK, start, end]) |
| 978 .then((response) { |
| 979 if (_isErrorResponse(response)) { |
| 980 throw _exceptionFromResponse(response, 'unlock failed', path); |
| 981 } |
| 982 return this; |
| 983 }); |
| 984 } |
| 985 |
| 986 external static _lock(int id, int lock, int start, int end); |
| 987 |
| 988 void lockSync([FileLock mode = FileLock.EXCLUSIVE, int start = 0, int end]) { |
| 989 _checkAvailable(); |
| 990 if ((start != null && start is !int) || |
| 991 (end != null && end is !int) || |
| 992 mode is !FileLock) { |
| 993 throw new ArgumentError(); |
| 994 } |
| 995 if (start == null) start = 0; |
| 996 if (end == null) end = -1; |
| 997 if (start < 0 || end < -1 || (end != -1 && start >= end)) { |
| 998 throw new ArgumentError(); |
| 999 } |
| 1000 int lock = mode == FileLock.EXCLUSIVE ? LOCK_EXCLUSIVE : LOCK_SHARED; |
| 1001 var result = _lock(_id, lock, start, end); |
| 1002 if (result is OSError) { |
| 1003 throw new FileSystemException('lock failed', path, result); |
| 1004 } |
| 1005 } |
| 1006 |
| 1007 void unlockSync([int start = 0, int end]) { |
| 1008 _checkAvailable(); |
| 1009 if ((start != null && start is !int) || |
| 1010 (end != null && end is !int)) { |
| 1011 throw new ArgumentError(); |
| 1012 } |
| 1013 if (start == null) start = 0; |
| 1014 if (end == null) end = -1; |
| 1015 if (start == end) throw new ArgumentError(); |
| 1016 var result = _lock(_id, LOCK_UNLOCK, start, end); |
| 1017 if (result is OSError) { |
| 1018 throw new FileSystemException('unlock failed', path, result); |
| 1019 } |
| 1020 } |
| 1021 |
943 bool get closed => _id == 0; | 1022 bool get closed => _id == 0; |
944 | 1023 |
945 Future _dispatch(int request, List data, { bool markClosed: false }) { | 1024 Future _dispatch(int request, List data, { bool markClosed: false }) { |
946 if (closed) { | 1025 if (closed) { |
947 return new Future.error(new FileSystemException("File closed", path)); | 1026 return new Future.error(new FileSystemException("File closed", path)); |
948 } | 1027 } |
949 if (_asyncDispatched) { | 1028 if (_asyncDispatched) { |
950 var msg = "An async operation is currently pending"; | 1029 var msg = "An async operation is currently pending"; |
951 return new Future.error(new FileSystemException(msg, path)); | 1030 return new Future.error(new FileSystemException(msg, path)); |
952 } | 1031 } |
(...skipping 12 matching lines...) Expand all Loading... |
965 void _checkAvailable() { | 1044 void _checkAvailable() { |
966 if (_asyncDispatched) { | 1045 if (_asyncDispatched) { |
967 throw new FileSystemException("An async operation is currently pending", | 1046 throw new FileSystemException("An async operation is currently pending", |
968 path); | 1047 path); |
969 } | 1048 } |
970 if (closed) { | 1049 if (closed) { |
971 throw new FileSystemException("File closed", path); | 1050 throw new FileSystemException("File closed", path); |
972 } | 1051 } |
973 } | 1052 } |
974 } | 1053 } |
OLD | NEW |