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

Side by Side Diff: sdk/lib/io/file_impl.dart

Issue 2050413002: Adds blocking file locks. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Update docs and CHANGELOG Created 4 years, 6 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
OLDNEW
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 901 matching lines...) Expand 10 before | Expand all | Expand 10 after
912 _checkAvailable(); 912 _checkAvailable();
913 var result = _ops.flush(); 913 var result = _ops.flush();
914 if (result is OSError) { 914 if (result is OSError) {
915 throw new FileSystemException("flush failed", path, result); 915 throw new FileSystemException("flush failed", path, result);
916 } 916 }
917 } 917 }
918 918
919 static final int LOCK_UNLOCK = 0; 919 static final int LOCK_UNLOCK = 0;
920 static final int LOCK_SHARED = 1; 920 static final int LOCK_SHARED = 1;
921 static final int LOCK_EXCLUSIVE = 2; 921 static final int LOCK_EXCLUSIVE = 2;
922 static final int LOCK_BLOCKING_SHARED = 3;
923 static final int LOCK_BLOCKING_EXCLUSIVE = 4;
924
925 int _fileLockValue(FileLock fl) {
926 switch (fl) {
927 case FileLock.SHARED: return LOCK_SHARED;
928 case FileLock.EXCLUSIVE: return LOCK_EXCLUSIVE;
929 case FileLock.BLOCKING_SHARED: return LOCK_BLOCKING_SHARED;
930 case FileLock.BLOCKING_EXCLUSIVE: return LOCK_BLOCKING_EXCLUSIVE;
931 default: return -1;
932 }
933 }
922 934
923 Future<RandomAccessFile> lock( 935 Future<RandomAccessFile> lock(
924 [FileLock mode = FileLock.EXCLUSIVE, int start = 0, int end = -1]) { 936 [FileLock mode = FileLock.EXCLUSIVE, int start = 0, int end = -1]) {
925 if ((mode is !FileLock) || (start is !int) || (end is !int)) { 937 if ((mode is !FileLock) || (start is !int) || (end is !int)) {
926 throw new ArgumentError(); 938 throw new ArgumentError();
927 } 939 }
928 if ((start < 0) || (end < -1) || ((end != -1) && (start >= end))) { 940 if ((start < 0) || (end < -1) || ((end != -1) && (start >= end))) {
929 throw new ArgumentError(); 941 throw new ArgumentError();
930 } 942 }
931 int lock = (mode == FileLock.EXCLUSIVE) ? LOCK_EXCLUSIVE : LOCK_SHARED; 943 int lock = _fileLockValue(mode);
932 return _dispatch(_FILE_LOCK, [null, lock, start, end]) 944 return _dispatch(_FILE_LOCK, [null, lock, start, end])
933 .then((response) { 945 .then((response) {
934 if (_isErrorResponse(response)) { 946 if (_isErrorResponse(response)) {
935 throw _exceptionFromResponse(response, 'lock failed', path); 947 throw _exceptionFromResponse(response, 'lock failed', path);
936 } 948 }
937 return this; 949 return this;
938 }); 950 });
939 } 951 }
940 952
941 Future<RandomAccessFile> unlock([int start = 0, int end = -1]) { 953 Future<RandomAccessFile> unlock([int start = 0, int end = -1]) {
(...skipping 14 matching lines...) Expand all
956 968
957 void lockSync( 969 void lockSync(
958 [FileLock mode = FileLock.EXCLUSIVE, int start = 0, int end = -1]) { 970 [FileLock mode = FileLock.EXCLUSIVE, int start = 0, int end = -1]) {
959 _checkAvailable(); 971 _checkAvailable();
960 if ((mode is !FileLock) || (start is !int) || (end is !int)) { 972 if ((mode is !FileLock) || (start is !int) || (end is !int)) {
961 throw new ArgumentError(); 973 throw new ArgumentError();
962 } 974 }
963 if ((start < 0) || (end < -1) || ((end != -1) && (start >= end))) { 975 if ((start < 0) || (end < -1) || ((end != -1) && (start >= end))) {
964 throw new ArgumentError(); 976 throw new ArgumentError();
965 } 977 }
966 int lock = (mode == FileLock.EXCLUSIVE) ? LOCK_EXCLUSIVE : LOCK_SHARED; 978 int lock = _fileLockValue(mode);
967 var result = _ops.lock(lock, start, end); 979 var result = _ops.lock(lock, start, end);
968 if (result is OSError) { 980 if (result is OSError) {
969 throw new FileSystemException('lock failed', path, result); 981 throw new FileSystemException('lock failed', path, result);
970 } 982 }
971 } 983 }
972 984
973 void unlockSync([int start = 0, int end = -1]) { 985 void unlockSync([int start = 0, int end = -1]) {
974 _checkAvailable(); 986 _checkAvailable();
975 if ((start is !int) || (end is !int)) { 987 if ((start is !int) || (end is !int)) {
976 throw new ArgumentError(); 988 throw new ArgumentError();
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1016 void _checkAvailable() { 1028 void _checkAvailable() {
1017 if (_asyncDispatched) { 1029 if (_asyncDispatched) {
1018 throw new FileSystemException("An async operation is currently pending", 1030 throw new FileSystemException("An async operation is currently pending",
1019 path); 1031 path);
1020 } 1032 }
1021 if (closed) { 1033 if (closed) {
1022 throw new FileSystemException("File closed", path); 1034 throw new FileSystemException("File closed", path);
1023 } 1035 }
1024 } 1036 }
1025 } 1037 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698