-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add file modes for opening a file write only
Added two new file modes WRITE_ONLY and WRITE_ONLY_APPEND. The current file modes WRITE and WRITE_APPEND are kept as the read/write mode with no change. Kept the same overall handling of create/truncate/append semantics for the new file modes. R=ager@google.com, ahe@google.com BUG= Review URL: https://codereview.chromium.org//1193653002.
- Loading branch information
Showing
9 changed files
with
158 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | ||
// 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. | ||
// | ||
// Dart test program for testing file I/O. | ||
|
||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import "package:async_helper/async_helper.dart"; | ||
import "package:expect/expect.dart"; | ||
|
||
Future withTempDir(String prefix, void test(Directory dir)) async { | ||
var tempDir = Directory.systemTemp.createTempSync(prefix); | ||
try { | ||
await test(tempDir); | ||
} finally { | ||
tempDir.deleteSync(recursive: true); | ||
} | ||
} | ||
|
||
void withTempDirSync(String prefix, void test(Directory dir)) async { | ||
var tempDir = Directory.systemTemp.createTempSync(prefix); | ||
try { | ||
test(tempDir); | ||
} finally { | ||
tempDir.deleteSync(recursive: true); | ||
} | ||
} | ||
|
||
Futire expectThrowsAsync(Future future, String message) { | ||
return future.then((r) => Expect.fail(message)) | ||
.catchError((e) {}); | ||
} | ||
|
||
|
||
Future write(Directory dir) async { | ||
var f = new File('x'); | ||
var raf = await f.open(mode: WRITE_ONLY); | ||
await raf.writeString('Hello'); | ||
await raf.setPosition(0); | ||
await raf.writeString('Hello'); | ||
await raf.setPosition(0); | ||
await expectThrowsAsync( | ||
raf.readByte(), 'Read from write only file succeeded'); | ||
await raf.close(); | ||
raf = await f.open(mode: WRITE_ONLY_APPEND); | ||
await raf.writeString('Hello'); | ||
await expectThrowsAsync( | ||
raf.readByte(), 'Read from write only file succeeded'); | ||
await raf.setPosition(0); | ||
await raf.writeString('Hello'); | ||
await raf.close(); | ||
Expect.equals(f.lengthSync(), 10); | ||
} | ||
|
||
Future writeSync(Directory dir) { | ||
var f = new File('x'); | ||
var raf = f.openSync(mode: WRITE_ONLY); | ||
raf.writeStringSync('Hello'); | ||
raf.setPositionSync(0); | ||
raf.writeStringSync('Hello'); | ||
raf.setPositionSync(0); | ||
Expect.throws(() => raf.readByteSync()); | ||
} | ||
|
||
Future openWrite(Directory dir) async { | ||
var f = new File('x'); | ||
var sink = f.openWrite(mode: WRITE_ONLY); | ||
sink.write('Hello'); | ||
await sink.close(); | ||
sink = await f.openWrite(mode: WRITE_ONLY_APPEND); | ||
sink.write('Hello'); | ||
await sink.close(); | ||
Expect.equals(f.lengthSync(), 10); | ||
} | ||
|
||
main() async { | ||
asyncStart(); | ||
await withTempDir('file_write_only_test', write); | ||
withTempDirSync('file_write_only_test', writeSync); | ||
await withTempDir('file_write_only_test', openWrite); | ||
asyncEnd(); | ||
} |