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

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

Issue 24596003: Clean up IOService implementation to be shared between patched and non-patched code. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 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 | « sdk/lib/io/directory_impl.dart ('k') | sdk/lib/io/file_system_entity.dart » ('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) 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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 205
206 // Constructor for file. 206 // Constructor for file.
207 _File(String this.path) { 207 _File(String this.path) {
208 if (path is! String) { 208 if (path is! String) {
209 throw new ArgumentError('${Error.safeToString(path)} ' 209 throw new ArgumentError('${Error.safeToString(path)} '
210 'is not a String'); 210 'is not a String');
211 } 211 }
212 } 212 }
213 213
214 Future<bool> exists() { 214 Future<bool> exists() {
215 return IOService.dispatch(FILE_EXISTS, [path]).then((response) { 215 return _IOService.dispatch(_FILE_EXISTS, [path]).then((response) {
216 if (_isErrorResponse(response)) { 216 if (_isErrorResponse(response)) {
217 throw _exceptionFromResponse(response, "Cannot check existence", path); 217 throw _exceptionFromResponse(response, "Cannot check existence", path);
218 } 218 }
219 return response; 219 return response;
220 }); 220 });
221 } 221 }
222 222
223 external static _exists(String path); 223 external static _exists(String path);
224 224
225 bool existsSync() { 225 bool existsSync() {
226 var result = _exists(path); 226 var result = _exists(path);
227 throwIfError(result, "Cannot check existence of file", path); 227 throwIfError(result, "Cannot check existence of file", path);
228 return result; 228 return result;
229 } 229 }
230 230
231 File get absolute => new File(_absolutePath); 231 File get absolute => new File(_absolutePath);
232 232
233 Future<FileStat> stat() => FileStat.stat(path); 233 Future<FileStat> stat() => FileStat.stat(path);
234 234
235 FileStat statSync() => FileStat.statSync(path); 235 FileStat statSync() => FileStat.statSync(path);
236 236
237 Future<File> create() { 237 Future<File> create() {
238 return IOService.dispatch(FILE_CREATE, [path]).then((response) { 238 return _IOService.dispatch(_FILE_CREATE, [path]).then((response) {
239 if (_isErrorResponse(response)) { 239 if (_isErrorResponse(response)) {
240 throw _exceptionFromResponse(response, "Cannot create file", path); 240 throw _exceptionFromResponse(response, "Cannot create file", path);
241 } 241 }
242 return this; 242 return this;
243 }); 243 });
244 } 244 }
245 245
246 external static _create(String path); 246 external static _create(String path);
247 247
248 external static _createLink(String path, String target); 248 external static _createLink(String path, String target);
249 249
250 external static _linkTarget(String path); 250 external static _linkTarget(String path);
251 251
252 void createSync() { 252 void createSync() {
253 var result = _create(path); 253 var result = _create(path);
254 throwIfError(result, "Cannot create file", path); 254 throwIfError(result, "Cannot create file", path);
255 } 255 }
256 256
257 Future<File> _delete({bool recursive: false}) { 257 Future<File> _delete({bool recursive: false}) {
258 if (recursive) { 258 if (recursive) {
259 return new Directory(path).delete(recursive: true).then((_) => this); 259 return new Directory(path).delete(recursive: true).then((_) => this);
260 } 260 }
261 return IOService.dispatch(FILE_DELETE, [path]).then((response) { 261 return _IOService.dispatch(_FILE_DELETE, [path]).then((response) {
262 if (_isErrorResponse(response)) { 262 if (_isErrorResponse(response)) {
263 throw _exceptionFromResponse(response, "Cannot delete file", path); 263 throw _exceptionFromResponse(response, "Cannot delete file", path);
264 } 264 }
265 return this; 265 return this;
266 }); 266 });
267 } 267 }
268 268
269 external static _deleteNative(String path); 269 external static _deleteNative(String path);
270 270
271 external static _deleteLinkNative(String path); 271 external static _deleteLinkNative(String path);
272 272
273 void _deleteSync({bool recursive: false}) { 273 void _deleteSync({bool recursive: false}) {
274 if (recursive) { 274 if (recursive) {
275 return new Directory(path).deleteSync(recursive: true); 275 return new Directory(path).deleteSync(recursive: true);
276 } 276 }
277 var result = _deleteNative(path); 277 var result = _deleteNative(path);
278 throwIfError(result, "Cannot delete file", path); 278 throwIfError(result, "Cannot delete file", path);
279 } 279 }
280 280
281 Future<File> rename(String newPath) { 281 Future<File> rename(String newPath) {
282 return IOService.dispatch(FILE_RENAME, [path, newPath]).then((response) { 282 return _IOService.dispatch(_FILE_RENAME, [path, newPath]).then((response) {
283 if (_isErrorResponse(response)) { 283 if (_isErrorResponse(response)) {
284 throw _exceptionFromResponse( 284 throw _exceptionFromResponse(
285 response, "Cannot rename file to '$newPath'", path); 285 response, "Cannot rename file to '$newPath'", path);
286 } 286 }
287 return new File(newPath); 287 return new File(newPath);
288 }); 288 });
289 } 289 }
290 290
291 external static _rename(String oldPath, String newPath); 291 external static _rename(String oldPath, String newPath);
292 292
293 external static _renameLink(String oldPath, String newPath); 293 external static _renameLink(String oldPath, String newPath);
294 294
295 File renameSync(String newPath) { 295 File renameSync(String newPath) {
296 var result = _rename(path, newPath); 296 var result = _rename(path, newPath);
297 throwIfError(result, "Cannot rename file to '$newPath'", path); 297 throwIfError(result, "Cannot rename file to '$newPath'", path);
298 return new File(newPath); 298 return new File(newPath);
299 } 299 }
300 300
301 Directory get directory { 301 Directory get directory {
302 _Path path = new _Path(this.path).directoryPath; 302 _Path path = new _Path(this.path).directoryPath;
303 return new Directory(path.toNativePath()); 303 return new Directory(path.toNativePath());
304 } 304 }
305 305
306 Future<RandomAccessFile> open({FileMode mode: FileMode.READ}) { 306 Future<RandomAccessFile> open({FileMode mode: FileMode.READ}) {
307 if (mode != FileMode.READ && 307 if (mode != FileMode.READ &&
308 mode != FileMode.WRITE && 308 mode != FileMode.WRITE &&
309 mode != FileMode.APPEND) { 309 mode != FileMode.APPEND) {
310 return new Future.error(new ArgumentError()); 310 return new Future.error(new ArgumentError());
311 } 311 }
312 return IOService.dispatch(FILE_OPEN, [path, mode._mode]).then((response) { 312 return _IOService.dispatch(_FILE_OPEN, [path, mode._mode]).then((response) {
313 if (_isErrorResponse(response)) { 313 if (_isErrorResponse(response)) {
314 throw _exceptionFromResponse(response, "Cannot open file", path); 314 throw _exceptionFromResponse(response, "Cannot open file", path);
315 } 315 }
316 return new _RandomAccessFile(response, path); 316 return new _RandomAccessFile(response, path);
317 }); 317 });
318 } 318 }
319 319
320 Future<int> length() { 320 Future<int> length() {
321 return IOService.dispatch(FILE_LENGTH_FROM_PATH, [path]).then((response) { 321 return _IOService.dispatch(_FILE_LENGTH_FROM_PATH, [path]).then((response) {
322 if (_isErrorResponse(response)) { 322 if (_isErrorResponse(response)) {
323 throw _exceptionFromResponse(response, 323 throw _exceptionFromResponse(response,
324 "Cannot retrieve length of file", 324 "Cannot retrieve length of file",
325 path); 325 path);
326 } 326 }
327 return response; 327 return response;
328 }); 328 });
329 } 329 }
330 330
331 331
332 external static _lengthFromPath(String path); 332 external static _lengthFromPath(String path);
333 333
334 int lengthSync() { 334 int lengthSync() {
335 var result = _lengthFromPath(path); 335 var result = _lengthFromPath(path);
336 throwIfError(result, "Cannot retrieve length of file", path); 336 throwIfError(result, "Cannot retrieve length of file", path);
337 return result; 337 return result;
338 } 338 }
339 339
340 Future<DateTime> lastModified() { 340 Future<DateTime> lastModified() {
341 return IOService.dispatch(FILE_LAST_MODIFIED, [path]).then((response) { 341 return _IOService.dispatch(_FILE_LAST_MODIFIED, [path]).then((response) {
342 if (_isErrorResponse(response)) { 342 if (_isErrorResponse(response)) {
343 throw _exceptionFromResponse(response, 343 throw _exceptionFromResponse(response,
344 "Cannot retrieve modification time", 344 "Cannot retrieve modification time",
345 path); 345 path);
346 } 346 }
347 return new DateTime.fromMillisecondsSinceEpoch(response); 347 return new DateTime.fromMillisecondsSinceEpoch(response);
348 }); 348 });
349 } 349 }
350 350
351 external static _lastModified(String path); 351 external static _lastModified(String path);
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
524 SendPort _fileService; 524 SendPort _fileService;
525 525
526 _RandomAccessFile(int this._id, String this.path); 526 _RandomAccessFile(int this._id, String this.path);
527 527
528 Future<RandomAccessFile> close() { 528 Future<RandomAccessFile> close() {
529 if (closed) return _closedException(); 529 if (closed) return _closedException();
530 // Set the id_ to 0 (NULL) to ensure the no more async requests 530 // Set the id_ to 0 (NULL) to ensure the no more async requests
531 // can be issued for this file. 531 // can be issued for this file.
532 int id = _id; 532 int id = _id;
533 _id = 0; 533 _id = 0;
534 return IOService.dispatch(FILE_CLOSE, [id]).then((result) { 534 return _IOService.dispatch(_FILE_CLOSE, [id]).then((result) {
535 if (result != -1) { 535 if (result != -1) {
536 _id = result; 536 _id = result;
537 return this; 537 return this;
538 } else { 538 } else {
539 throw new FileException("Cannot close file", path); 539 throw new FileException("Cannot close file", path);
540 } 540 }
541 }); 541 });
542 } 542 }
543 543
544 external static int _close(int id); 544 external static int _close(int id);
545 545
546 void closeSync() { 546 void closeSync() {
547 _checkNotClosed(); 547 _checkNotClosed();
548 var id = _close(_id); 548 var id = _close(_id);
549 if (id == -1) { 549 if (id == -1) {
550 throw new FileException("Cannot close file", path); 550 throw new FileException("Cannot close file", path);
551 } 551 }
552 _id = id; 552 _id = id;
553 } 553 }
554 554
555 Future<int> readByte() { 555 Future<int> readByte() {
556 if (closed) return _closedException(); 556 if (closed) return _closedException();
557 return IOService.dispatch(FILE_READ_BYTE, [_id]).then((response) { 557 return _IOService.dispatch(_FILE_READ_BYTE, [_id]).then((response) {
558 if (_isErrorResponse(response)) { 558 if (_isErrorResponse(response)) {
559 throw _exceptionFromResponse(response, "readByte failed", path); 559 throw _exceptionFromResponse(response, "readByte failed", path);
560 } 560 }
561 return response; 561 return response;
562 }); 562 });
563 } 563 }
564 564
565 external static _readByte(int id); 565 external static _readByte(int id);
566 566
567 int readByteSync() { 567 int readByteSync() {
568 _checkNotClosed(); 568 _checkNotClosed();
569 var result = _readByte(_id); 569 var result = _readByte(_id);
570 if (result is OSError) { 570 if (result is OSError) {
571 throw new FileException("readByte failed", path, result); 571 throw new FileException("readByte failed", path, result);
572 } 572 }
573 return result; 573 return result;
574 } 574 }
575 575
576 Future<List<int>> read(int bytes) { 576 Future<List<int>> read(int bytes) {
577 if (bytes is !int) { 577 if (bytes is !int) {
578 throw new ArgumentError(bytes); 578 throw new ArgumentError(bytes);
579 } 579 }
580 if (closed) return _closedException(); 580 if (closed) return _closedException();
581 return IOService.dispatch(FILE_READ, [_id, bytes]).then((response) { 581 return _IOService.dispatch(_FILE_READ, [_id, bytes]).then((response) {
582 if (_isErrorResponse(response)) { 582 if (_isErrorResponse(response)) {
583 throw _exceptionFromResponse(response, "read failed", path); 583 throw _exceptionFromResponse(response, "read failed", path);
584 } 584 }
585 return response[1]; 585 return response[1];
586 }); 586 });
587 } 587 }
588 588
589 external static _read(int id, int bytes); 589 external static _read(int id, int bytes);
590 590
591 List<int> readSync(int bytes) { 591 List<int> readSync(int bytes) {
(...skipping 11 matching lines...) Expand all
603 Future<int> readInto(List<int> buffer, [int start, int end]) { 603 Future<int> readInto(List<int> buffer, [int start, int end]) {
604 if (buffer is !List || 604 if (buffer is !List ||
605 (start != null && start is !int) || 605 (start != null && start is !int) ||
606 (end != null && end is !int)) { 606 (end != null && end is !int)) {
607 throw new ArgumentError(); 607 throw new ArgumentError();
608 } 608 }
609 if (closed) return _closedException(); 609 if (closed) return _closedException();
610 if (start == null) start = 0; 610 if (start == null) start = 0;
611 if (end == null) end = buffer.length; 611 if (end == null) end = buffer.length;
612 int length = end - start; 612 int length = end - start;
613 return IOService.dispatch(FILE_READ_INTO, [_id, length]).then((response) { 613 return _IOService.dispatch(_FILE_READ_INTO, [_id, length]).then((response) {
614 if (_isErrorResponse(response)) { 614 if (_isErrorResponse(response)) {
615 throw _exceptionFromResponse(response, "readInto failed", path); 615 throw _exceptionFromResponse(response, "readInto failed", path);
616 } 616 }
617 var read = response[1]; 617 var read = response[1];
618 var data = response[2]; 618 var data = response[2];
619 buffer.setRange(start, start + read, data); 619 buffer.setRange(start, start + read, data);
620 return read; 620 return read;
621 }); 621 });
622 } 622 }
623 623
(...skipping 23 matching lines...) Expand all
647 throw new FileException("readInto failed", path, result); 647 throw new FileException("readInto failed", path, result);
648 } 648 }
649 return result; 649 return result;
650 } 650 }
651 651
652 Future<RandomAccessFile> writeByte(int value) { 652 Future<RandomAccessFile> writeByte(int value) {
653 if (value is !int) { 653 if (value is !int) {
654 throw new ArgumentError(value); 654 throw new ArgumentError(value);
655 } 655 }
656 if (closed) return _closedException(); 656 if (closed) return _closedException();
657 return IOService.dispatch(FILE_WRITE_BYTE, [_id, value]).then((response) { 657 return _IOService.dispatch(_FILE_WRITE_BYTE, [_id, value]).then((response) {
658 if (_isErrorResponse(response)) { 658 if (_isErrorResponse(response)) {
659 throw _exceptionFromResponse(response, "writeByte failed", path); 659 throw _exceptionFromResponse(response, "writeByte failed", path);
660 } 660 }
661 return this; 661 return this;
662 }); 662 });
663 } 663 }
664 664
665 external static _writeByte(int id, int value); 665 external static _writeByte(int id, int value);
666 666
667 int writeByteSync(int value) { 667 int writeByteSync(int value) {
(...skipping 22 matching lines...) Expand all
690 result = _ensureFastAndSerializableByteData(buffer, start, end); 690 result = _ensureFastAndSerializableByteData(buffer, start, end);
691 } catch (e) { 691 } catch (e) {
692 return new Future.error(e); 692 return new Future.error(e);
693 } 693 }
694 694
695 List request = new List(4); 695 List request = new List(4);
696 request[0] = _id; 696 request[0] = _id;
697 request[1] = result.buffer; 697 request[1] = result.buffer;
698 request[2] = result.start; 698 request[2] = result.start;
699 request[3] = end - (start - result.start); 699 request[3] = end - (start - result.start);
700 return IOService.dispatch(FILE_WRITE_FROM, request).then((response) { 700 return _IOService.dispatch(_FILE_WRITE_FROM, request).then((response) {
701 if (_isErrorResponse(response)) { 701 if (_isErrorResponse(response)) {
702 throw _exceptionFromResponse(response, "writeFrom failed", path); 702 throw _exceptionFromResponse(response, "writeFrom failed", path);
703 } 703 }
704 return this; 704 return this;
705 }); 705 });
706 } 706 }
707 707
708 external static _writeFrom(int id, List<int> buffer, int start, int end); 708 external static _writeFrom(int id, List<int> buffer, int start, int end);
709 709
710 void writeFromSync(List<int> buffer, [int start, int end]) { 710 void writeFromSync(List<int> buffer, [int start, int end]) {
(...skipping 30 matching lines...) Expand all
741 void writeStringSync(String string, {Encoding encoding: UTF8}) { 741 void writeStringSync(String string, {Encoding encoding: UTF8}) {
742 if (encoding is! Encoding) { 742 if (encoding is! Encoding) {
743 throw new ArgumentError(encoding); 743 throw new ArgumentError(encoding);
744 } 744 }
745 var data = encoding.encode(string); 745 var data = encoding.encode(string);
746 writeFromSync(data, 0, data.length); 746 writeFromSync(data, 0, data.length);
747 } 747 }
748 748
749 Future<int> position() { 749 Future<int> position() {
750 if (closed) return _closedException(); 750 if (closed) return _closedException();
751 return IOService.dispatch(FILE_POSITION, [_id]).then((response) { 751 return _IOService.dispatch(_FILE_POSITION, [_id]).then((response) {
752 if (_isErrorResponse(response)) { 752 if (_isErrorResponse(response)) {
753 throw _exceptionFromResponse(response, "position failed", path); 753 throw _exceptionFromResponse(response, "position failed", path);
754 } 754 }
755 return response; 755 return response;
756 }); 756 });
757 } 757 }
758 758
759 external static _position(int id); 759 external static _position(int id);
760 760
761 int positionSync() { 761 int positionSync() {
762 _checkNotClosed(); 762 _checkNotClosed();
763 var result = _position(_id); 763 var result = _position(_id);
764 if (result is OSError) { 764 if (result is OSError) {
765 throw new FileException("position failed", path, result); 765 throw new FileException("position failed", path, result);
766 } 766 }
767 return result; 767 return result;
768 } 768 }
769 769
770 Future<RandomAccessFile> setPosition(int position) { 770 Future<RandomAccessFile> setPosition(int position) {
771 if (closed) return _closedException(); 771 if (closed) return _closedException();
772 return IOService.dispatch(FILE_SET_POSITION, [_id, position]) 772 return _IOService.dispatch(_FILE_SET_POSITION, [_id, position])
773 .then((response) { 773 .then((response) {
774 if (_isErrorResponse(response)) { 774 if (_isErrorResponse(response)) {
775 throw _exceptionFromResponse(response, "setPosition failed", path); 775 throw _exceptionFromResponse(response, "setPosition failed", path);
776 } 776 }
777 return this; 777 return this;
778 }); 778 });
779 } 779 }
780 780
781 external static _setPosition(int id, int position); 781 external static _setPosition(int id, int position);
782 782
783 void setPositionSync(int position) { 783 void setPositionSync(int position) {
784 _checkNotClosed(); 784 _checkNotClosed();
785 var result = _setPosition(_id, position); 785 var result = _setPosition(_id, position);
786 if (result is OSError) { 786 if (result is OSError) {
787 throw new FileException("setPosition failed", path, result); 787 throw new FileException("setPosition failed", path, result);
788 } 788 }
789 } 789 }
790 790
791 Future<RandomAccessFile> truncate(int length) { 791 Future<RandomAccessFile> truncate(int length) {
792 if (closed) return _closedException(); 792 if (closed) return _closedException();
793 return IOService.dispatch(FILE_TRUNCATE, [_id, length]).then((response) { 793 return _IOService.dispatch(_FILE_TRUNCATE, [_id, length]).then((response) {
794 if (_isErrorResponse(response)) { 794 if (_isErrorResponse(response)) {
795 throw _exceptionFromResponse(response, "truncate failed", path); 795 throw _exceptionFromResponse(response, "truncate failed", path);
796 } 796 }
797 return this; 797 return this;
798 }); 798 });
799 } 799 }
800 800
801 external static _truncate(int id, int length); 801 external static _truncate(int id, int length);
802 802
803 void truncateSync(int length) { 803 void truncateSync(int length) {
804 _checkNotClosed(); 804 _checkNotClosed();
805 var result = _truncate(_id, length); 805 var result = _truncate(_id, length);
806 if (result is OSError) { 806 if (result is OSError) {
807 throw new FileException("truncate failed", path, result); 807 throw new FileException("truncate failed", path, result);
808 } 808 }
809 } 809 }
810 810
811 Future<int> length() { 811 Future<int> length() {
812 if (closed) return _closedException(); 812 if (closed) return _closedException();
813 return IOService.dispatch(FILE_LENGTH, [_id]).then((response) { 813 return _IOService.dispatch(_FILE_LENGTH, [_id]).then((response) {
814 if (_isErrorResponse(response)) { 814 if (_isErrorResponse(response)) {
815 throw _exceptionFromResponse(response, "length failed", path); 815 throw _exceptionFromResponse(response, "length failed", path);
816 } 816 }
817 return response; 817 return response;
818 }); 818 });
819 } 819 }
820 820
821 external static _length(int id); 821 external static _length(int id);
822 822
823 int lengthSync() { 823 int lengthSync() {
824 _checkNotClosed(); 824 _checkNotClosed();
825 var result = _length(_id); 825 var result = _length(_id);
826 if (result is OSError) { 826 if (result is OSError) {
827 throw new FileException("length failed", path, result); 827 throw new FileException("length failed", path, result);
828 } 828 }
829 return result; 829 return result;
830 } 830 }
831 831
832 Future<RandomAccessFile> flush() { 832 Future<RandomAccessFile> flush() {
833 if (closed) return _closedException(); 833 if (closed) return _closedException();
834 return IOService.dispatch(FILE_FLUSH, [_id]).then((response) { 834 return _IOService.dispatch(_FILE_FLUSH, [_id]).then((response) {
835 if (_isErrorResponse(response)) { 835 if (_isErrorResponse(response)) {
836 throw _exceptionFromResponse(response, 836 throw _exceptionFromResponse(response,
837 "flush failed", 837 "flush failed",
838 path); 838 path);
839 } 839 }
840 return this; 840 return this;
841 }); 841 });
842 } 842 }
843 843
844 external static _flush(int id); 844 external static _flush(int id);
(...skipping 11 matching lines...) Expand all
856 void _checkNotClosed() { 856 void _checkNotClosed() {
857 if (closed) { 857 if (closed) {
858 throw new FileException("File closed", path); 858 throw new FileException("File closed", path);
859 } 859 }
860 } 860 }
861 861
862 Future _closedException() { 862 Future _closedException() {
863 return new Future.error(new FileException("File closed", path)); 863 return new Future.error(new FileException("File closed", path));
864 } 864 }
865 } 865 }
OLDNEW
« no previous file with comments | « sdk/lib/io/directory_impl.dart ('k') | sdk/lib/io/file_system_entity.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698