OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 // |
| 5 // Script used by the file_lock_test.dart test. |
| 6 |
| 7 import "dart:async"; |
| 8 import "dart:io"; |
| 9 |
| 10 Future<int> testLockWholeFile(File file, int len) async { |
| 11 var raf = await file.open(mode: APPEND); |
| 12 await raf.setPosition(0); |
| 13 int nextToWrite = 1; |
| 14 while (nextToWrite <= len) { |
| 15 await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, len); |
| 16 |
| 17 int at; |
| 18 int p; |
| 19 while (true) { |
| 20 p = await raf.position(); |
| 21 at = await raf.readByte(); |
| 22 if (at == 0 || at == -1) break; |
| 23 nextToWrite++; |
| 24 } |
| 25 await raf.setPosition(p); |
| 26 await raf.writeByte(nextToWrite); |
| 27 await raf.flush(); |
| 28 nextToWrite++; |
| 29 await raf.unlock(0, len); |
| 30 } |
| 31 |
| 32 await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, len); |
| 33 await raf.setPosition(0); |
| 34 for (int i = 1; i <= len; i++) { |
| 35 if ((await raf.readByte()) != i) { |
| 36 await raf.unlock(0, len); |
| 37 await raf.close(); |
| 38 return 1; |
| 39 } |
| 40 } |
| 41 await raf.unlock(0, len); |
| 42 await raf.close(); |
| 43 return 0; |
| 44 } |
| 45 |
| 46 main(List<String> args) async { |
| 47 File file = new File(args[0]); |
| 48 int len = int.parse(args[1]); |
| 49 exit(await testLockWholeFile(file, len)); |
| 50 } |
OLD | NEW |