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

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

Issue 9416096: Implement File.directory() and File.directorySync() to get (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove bogus assert 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
« no previous file with comments | « runtime/bin/file.dart ('k') | runtime/bin/file_linux.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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(RandomAccessFile this._file, int this._length) { 6 _FileInputStream(RandomAccessFile this._file, int this._length) {
7 _streamMarkedClosed = true; 7 _streamMarkedClosed = true;
8 _checkScheduleCallbacks(); 8 _checkScheduleCallbacks();
9 } 9 }
10 10
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 _DeleteOperation(String this._name); 397 _DeleteOperation(String this._name);
398 398
399 void execute(ReceivePort port) { 399 void execute(ReceivePort port) {
400 _replyPort.send(_FileUtils.checkedDelete(_name), port.toSendPort()); 400 _replyPort.send(_FileUtils.checkedDelete(_name), port.toSendPort());
401 } 401 }
402 402
403 String _name; 403 String _name;
404 } 404 }
405 405
406 406
407 class _DirectoryOperation extends _FileOperation {
408 _DirectoryOperation(String this._name);
409
410 void execute(ReceivePort port) {
411 if (_name is String) {
412 if (_FileUtils.exists(_name)) {
413 _replyPort.send(_FileUtils.directory(_name), port.toSendPort());
414 return;
415 }
416 }
417 // Either _name is not a string or the file does not exist.
418 _replyPort.send(null, port.toSendPort());
419 }
420
421 String _name;
422 }
423
424
407 class _ExitOperation extends _FileOperation { 425 class _ExitOperation extends _FileOperation {
408 void execute(ReceivePort port) { 426 void execute(ReceivePort port) {
409 port.close(); 427 port.close();
410 } 428 }
411 } 429 }
412 430
413 431
414 class _FileOperationIsolate extends Isolate { 432 class _FileOperationIsolate extends Isolate {
415 _FileOperationIsolate() : super.heavy(); 433 _FileOperationIsolate() : super.heavy();
416 434
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
472 _FileOperationIsolate _isolate; 490 _FileOperationIsolate _isolate;
473 } 491 }
474 492
475 493
476 // Helper class containing static file helper methods. 494 // Helper class containing static file helper methods.
477 class _FileUtils { 495 class _FileUtils {
478 static bool exists(String name) native "File_Exists"; 496 static bool exists(String name) native "File_Exists";
479 static int open(String name, int mode) native "File_Open"; 497 static int open(String name, int mode) native "File_Open";
480 static bool create(String name) native "File_Create"; 498 static bool create(String name) native "File_Create";
481 static bool delete(String name) native "File_Delete"; 499 static bool delete(String name) native "File_Delete";
500 static String directory(String name) native "File_Directory";
482 static String fullPath(String name) native "File_FullPath"; 501 static String fullPath(String name) native "File_FullPath";
483 static int close(int id) native "File_Close"; 502 static int close(int id) native "File_Close";
484 static int readByte(int id) native "File_ReadByte"; 503 static int readByte(int id) native "File_ReadByte";
485 static int readList(int id, List<int> buffer, int offset, int bytes) 504 static int readList(int id, List<int> buffer, int offset, int bytes)
486 native "File_ReadList"; 505 native "File_ReadList";
487 static int writeByte(int id, int value) native "File_WriteByte"; 506 static int writeByte(int id, int value) native "File_WriteByte";
488 static int writeList(int id, List<int> buffer, int offset, int bytes) { 507 static int writeList(int id, List<int> buffer, int offset, int bytes) {
489 // When using the Dart C API to access raw data, using a ByteArray is 508 // When using the Dart C API to access raw data, using a ByteArray is
490 // currently much faster. This function will make a copy of the 509 // currently much faster. This function will make a copy of the
491 // supplied List to a ByteArray if it isn't already. 510 // supplied List to a ByteArray if it isn't already.
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
630 if (_asyncUsed) { 649 if (_asyncUsed) {
631 throw new FileIOException( 650 throw new FileIOException(
632 "Mixed use of synchronous and asynchronous API"); 651 "Mixed use of synchronous and asynchronous API");
633 } 652 }
634 bool deleted = _FileUtils.checkedDelete(_name); 653 bool deleted = _FileUtils.checkedDelete(_name);
635 if (!deleted) { 654 if (!deleted) {
636 throw new FileIOException("Cannot delete file: $_name"); 655 throw new FileIOException("Cannot delete file: $_name");
637 } 656 }
638 } 657 }
639 658
659 void directory() {
660 _asyncUsed = true;
661 var handleDirectoryResult = (path, ignored) {
662 var handler =
663 (_directoryHandler != null) ? _directoryHandler : (s) => null;
664 if (path != null) {
665 handler(new Directory(path));
666 } else if (_errorHandler != null) {
667 _errorHandler("Cannot get containing directory for: ${_name}");
668 }
669 };
670 var operation = new _DirectoryOperation(_name);
671 _scheduler.enqueue(operation, handleDirectoryResult);
672 }
673
674 void directorySync() {
675 if (_asyncUsed) {
676 throw new FileIOException(
677 "Mixed use of synchronous and asynchronous API");
678 }
679 if (!existsSync()) {
680 throw new FileIOException("Cannot get directory for: $_name");
681 }
682 return new Directory(_FileUtils.directory(_name));
683 }
684
640 void open([FileMode mode = FileMode.READ]) { 685 void open([FileMode mode = FileMode.READ]) {
641 _asyncUsed = true; 686 _asyncUsed = true;
642 if (mode != FileMode.READ && 687 if (mode != FileMode.READ &&
643 mode != FileMode.WRITE && 688 mode != FileMode.WRITE &&
644 mode != FileMode.APPEND) { 689 mode != FileMode.APPEND) {
645 if (_errorHandler != null) { 690 if (_errorHandler != null) {
646 _errorHandler("Unknown file mode. Use FileMode.READ, FileMode.WRITE " + 691 _errorHandler("Unknown file mode. Use FileMode.READ, FileMode.WRITE " +
647 "or FileMode.APPEND."); 692 "or FileMode.APPEND.");
648 return; 693 return;
649 } 694 }
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 } 834 }
790 835
791 void set createHandler(void handler()) { 836 void set createHandler(void handler()) {
792 _createHandler = handler; 837 _createHandler = handler;
793 } 838 }
794 839
795 void set deleteHandler(void handler()) { 840 void set deleteHandler(void handler()) {
796 _deleteHandler = handler; 841 _deleteHandler = handler;
797 } 842 }
798 843
844 void set directoryHandler(void handler(Directory directory)) {
845 _directoryHandler = handler;
846 }
847
799 void set openHandler(void handler(RandomAccessFile file)) { 848 void set openHandler(void handler(RandomAccessFile file)) {
800 _openHandler = handler; 849 _openHandler = handler;
801 } 850 }
802 851
803 void set inputStreamHandler(void handler(InputStream stream)) { 852 void set inputStreamHandler(void handler(InputStream stream)) {
804 _inputStreamHandler = handler; 853 _inputStreamHandler = handler;
805 } 854 }
806 855
807 void set outputStreamHandler(void handler(OutputStream stream)) { 856 void set outputStreamHandler(void handler(OutputStream stream)) {
808 _outputStreamHandler = handler; 857 _outputStreamHandler = handler;
809 } 858 }
810 859
811 void set fullPathHandler(void handler(String)) { 860 void set fullPathHandler(void handler(String)) {
812 _fullPathHandler = handler; 861 _fullPathHandler = handler;
813 } 862 }
814 863
815 void set errorHandler(void handler(String error)) { 864 void set errorHandler(void handler(String error)) {
816 _errorHandler = handler; 865 _errorHandler = handler;
817 } 866 }
818 867
819 String _name; 868 String _name;
820 bool _asyncUsed; 869 bool _asyncUsed;
821 870
822 _FileOperationScheduler _scheduler; 871 _FileOperationScheduler _scheduler;
823 872
824 Function _existsHandler; 873 Function _existsHandler;
825 Function _createHandler; 874 Function _createHandler;
826 Function _deleteHandler; 875 Function _deleteHandler;
876 Function _directoryHandler;
827 Function _openHandler; 877 Function _openHandler;
828 Function _inputStreamHandler; 878 Function _inputStreamHandler;
829 Function _outputStreamHandler; 879 Function _outputStreamHandler;
830 Function _fullPathHandler; 880 Function _fullPathHandler;
831 Function _errorHandler; 881 Function _errorHandler;
832 } 882 }
833 883
834 884
835 class _RandomAccessFile implements RandomAccessFile { 885 class _RandomAccessFile implements RandomAccessFile {
836 _RandomAccessFile(int this._id, String this._name) 886 _RandomAccessFile(int this._id, String this._name)
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
1233 Function _readByteHandler; 1283 Function _readByteHandler;
1234 Function _readListHandler; 1284 Function _readListHandler;
1235 Function _noPendingWriteHandler; 1285 Function _noPendingWriteHandler;
1236 Function _positionHandler; 1286 Function _positionHandler;
1237 Function _setPositionHandler; 1287 Function _setPositionHandler;
1238 Function _truncateHandler; 1288 Function _truncateHandler;
1239 Function _lengthHandler; 1289 Function _lengthHandler;
1240 Function _flushHandler; 1290 Function _flushHandler;
1241 Function _errorHandler; 1291 Function _errorHandler;
1242 } 1292 }
OLDNEW
« no previous file with comments | « runtime/bin/file.dart ('k') | runtime/bin/file_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698