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

Side by Side Diff: runtime/bin/file_impl.dart

Issue 9346015: Change file opening operation to set position at the end of the file when FileMode.APPEND is used. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 8 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 class _FileInputStream extends _BaseDataInputStream implements InputStream { 5 class _FileInputStream extends _BaseDataInputStream implements InputStream {
6 _FileInputStream(File file) { 6 _FileInputStream(File file) {
7 _file = file.openSync(); 7 _file = file.openSync();
8 _length = _file.lengthSync(); 8 _length = _file.lengthSync();
9 _streamMarkedClosed = true; 9 _streamMarkedClosed = true;
10 _checkScheduleCallbacks(); 10 _checkScheduleCallbacks();
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 _closed = true; 42 _closed = true;
43 } 43 }
44 44
45 RandomAccessFile _file; 45 RandomAccessFile _file;
46 int _length; 46 int _length;
47 bool _closed = false; 47 bool _closed = false;
48 } 48 }
49 49
50 50
51 class _FileOutputStream implements OutputStream { 51 class _FileOutputStream implements OutputStream {
52 _FileOutputStream(File file) { 52 _FileOutputStream(File file, FileMode mode) {
53 _file = file.openSync(FileMode.WRITE); 53 _file = file.openSync(mode);
54 } 54 }
55 55
56 bool write(List<int> buffer, [bool copyBuffer = false]) { 56 bool write(List<int> buffer, [bool copyBuffer = false]) {
57 return _write(buffer, 0, buffer.length); 57 return _write(buffer, 0, buffer.length);
58 } 58 }
59 59
60 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { 60 bool writeFrom(List<int> buffer, [int offset = 0, int len]) {
61 return _write( 61 return _write(
62 buffer, offset, (len == null) ? buffer.length - offset : len); 62 buffer, offset, (len == null) ? buffer.length - offset : len);
63 } 63 }
(...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 } 584 }
585 var handleOpenResult = (id, ignored) { 585 var handleOpenResult = (id, ignored) {
586 // If no open handler is present, close the file immediately to 586 // If no open handler is present, close the file immediately to
587 // avoid leaking an open file descriptor. 587 // avoid leaking an open file descriptor.
588 var handler = _openHandler; 588 var handler = _openHandler;
589 if (handler === null) { 589 if (handler === null) {
590 handler = (file) => file.close(); 590 handler = (file) => file.close();
591 } 591 }
592 if (id != 0) { 592 if (id != 0) {
593 var randomAccessFile = new _RandomAccessFile(id, _name); 593 var randomAccessFile = new _RandomAccessFile(id, _name);
594 if (mode == FileMode.APPEND) {
595 _FileUtils.setPosition(id, _FileUtils.length(id));
Mads Ager (google) 2012/02/07 08:37:31 Please do this in the C++ code instead. Here you a
ricow1 2012/02/07 09:16:22 Done, I placed these in the platform specific file
596 }
594 handler(randomAccessFile); 597 handler(randomAccessFile);
595 } else if (_errorHandler != null) { 598 } else if (_errorHandler != null) {
596 _errorHandler("Cannot open file: $_name"); 599 _errorHandler("Cannot open file: $_name");
597 } 600 }
598 }; 601 };
599 var operation = new _OpenOperation(_name, mode._mode); 602 var operation = new _OpenOperation(_name, mode._mode);
600 _scheduler.enqueue(operation, handleOpenResult); 603 _scheduler.enqueue(operation, handleOpenResult);
601 } 604 }
602 605
603 RandomAccessFile openSync([FileMode mode = FileMode.READ]) { 606 RandomAccessFile openSync([FileMode mode = FileMode.READ]) {
604 if (_asyncUsed) { 607 if (_asyncUsed) {
605 throw new FileIOException( 608 throw new FileIOException(
606 "Mixed use of synchronous and asynchronous API"); 609 "Mixed use of synchronous and asynchronous API");
607 } 610 }
608 if (mode != FileMode.READ && 611 if (mode != FileMode.READ &&
609 mode != FileMode.WRITE && 612 mode != FileMode.WRITE &&
610 mode != FileMode.APPEND) { 613 mode != FileMode.APPEND) {
611 throw new FileIOException("Unknown file mode. Use FileMode.READ, " + 614 throw new FileIOException("Unknown file mode. Use FileMode.READ, " +
612 "FileMode.WRITE or FileMode.APPEND."); 615 "FileMode.WRITE or FileMode.APPEND.");
613 } 616 }
614 var id = _FileUtils.checkedOpen(_name, mode._mode); 617 var id = _FileUtils.checkedOpen(_name, mode._mode);
615 if (id == 0) { 618 if (id == 0) {
616 throw new FileIOException("Cannot open file: $_name"); 619 throw new FileIOException("Cannot open file: $_name");
617 } 620 }
621 if (mode == FileMode.APPEND) {
622 _FileUtils.setPosition(id, _FileUtils.length(id));
Mads Ager (google) 2012/02/07 08:37:31 Ditto. This is actually fine because this is the s
ricow1 2012/02/07 09:16:22 Done.
623 }
618 return new _RandomAccessFile(id, _name); 624 return new _RandomAccessFile(id, _name);
619 } 625 }
620 626
621 void fullPath() { 627 void fullPath() {
622 _asyncUsed = true; 628 _asyncUsed = true;
623 var handleFullPathResult = (result, ignored) { 629 var handleFullPathResult = (result, ignored) {
624 var handler = _fullPathHandler; 630 var handler = _fullPathHandler;
625 if (handler == null) handler = (path) => null; 631 if (handler == null) handler = (path) => null;
626 if (result != null) { 632 if (result != null) {
627 handler(result); 633 handler(result);
(...skipping 12 matching lines...) Expand all
640 } 646 }
641 String result = _FileUtils.checkedFullPath(_name); 647 String result = _FileUtils.checkedFullPath(_name);
642 if (result == null) { 648 if (result == null) {
643 throw new FileIOException("fullPath failed"); 649 throw new FileIOException("fullPath failed");
644 } 650 }
645 return result; 651 return result;
646 } 652 }
647 653
648 InputStream openInputStream() => new _FileInputStream(this); 654 InputStream openInputStream() => new _FileInputStream(this);
649 655
650 OutputStream openOutputStream() => new _FileOutputStream(this); 656 OutputStream openOutputStream([FileMode mode = FileMode.WRITE]) {
657 if (mode != FileMode.WRITE &&
658 mode != FileMode.APPEND) {
659 throw new FileIOException(
660 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND");
661 }
662 return new _FileOutputStream(this, mode);
663 }
651 664
652 String get name() => _name; 665 String get name() => _name;
653 666
654 void set existsHandler(void handler(bool exists)) { 667 void set existsHandler(void handler(bool exists)) {
655 _existsHandler = handler; 668 _existsHandler = handler;
656 } 669 }
657 670
658 void set createHandler(void handler()) { 671 void set createHandler(void handler()) {
659 _createHandler = handler; 672 _createHandler = handler;
660 } 673 }
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after
1090 var _readByteHandler; 1103 var _readByteHandler;
1091 var _readListHandler; 1104 var _readListHandler;
1092 var _noPendingWriteHandler; 1105 var _noPendingWriteHandler;
1093 var _positionHandler; 1106 var _positionHandler;
1094 var _setPositionHandler; 1107 var _setPositionHandler;
1095 var _truncateHandler; 1108 var _truncateHandler;
1096 var _lengthHandler; 1109 var _lengthHandler;
1097 var _flushHandler; 1110 var _flushHandler;
1098 var _errorHandler; 1111 var _errorHandler;
1099 } 1112 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698