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

Side by Side Diff: runtime/bin/file.cc

Issue 9415043: Port async file operations to be using native ports instead or an isolate (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Forgot to remove #import from op.dart 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.h ('k') | runtime/bin/file_impl.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) 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 #include "bin/file.h" 5 #include "bin/file.h"
6 6
7 #include "bin/builtin.h" 7 #include "bin/builtin.h"
8 #include "bin/dartutils.h" 8 #include "bin/dartutils.h"
9 9
10 #include "include/dart_api.h" 10 #include "include/dart_api.h"
(...skipping 22 matching lines...) Expand all
33 if (bytes_read < 0) { 33 if (bytes_read < 0) {
34 return false; 34 return false;
35 } 35 }
36 remaining -= bytes_read; // Reduce the number of remaining bytes. 36 remaining -= bytes_read; // Reduce the number of remaining bytes.
37 current_buffer += bytes_read; // Move the buffer forward. 37 current_buffer += bytes_read; // Move the buffer forward.
38 } 38 }
39 return true; 39 return true;
40 } 40 }
41 41
42 42
43 File::FileOpenMode File::DartModeToFileMode(DartFileOpenMode mode) {
44 ASSERT(mode == File::kDartRead ||
45 mode == File::kDartWrite ||
46 mode == File::kDartAppend);
47 if (mode == File::kDartWrite) {
48 return File::kWriteTruncate;
49 }
50 if (mode == File::kDartAppend) {
51 return File::kWrite;
52 }
53 return File::kRead;
54 }
55
56
43 void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) { 57 void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) {
44 // These values have to be kept in sync with the mode values of
45 // FileMode.READ, FileMode.WRITE and FileMode.APPEND in file.dart.
46 static const int kRead = 0;
47 static const int kWrite = 1;
48 static const int kAppend = 2;
49
50 Dart_EnterScope(); 58 Dart_EnterScope();
51 const char* filename = 59 const char* filename =
52 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 60 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0));
53 int mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); 61 int mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1));
54 ASSERT(mode == kRead || mode == kWrite || mode == kAppend); 62 File::DartFileOpenMode dart_file_mode =
55 File::FileOpenMode file_mode = File::kRead; 63 static_cast<File::DartFileOpenMode>(mode);
56 if (mode == kWrite) { 64 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode);
57 file_mode = File::kWriteTruncate;
58 }
59 if (mode == kAppend) {
60 file_mode = File::kWrite;
61 }
62 // Check that the file exists before opening it only for 65 // Check that the file exists before opening it only for
63 // reading. This is to prevent the opening of directories as 66 // reading. This is to prevent the opening of directories as
64 // files. Directories can be opened for reading using the posix 67 // files. Directories can be opened for reading using the posix
65 // 'open' call. 68 // 'open' call.
66 File* file = NULL; 69 File* file = NULL;
67 if (((file_mode & File::kWrite) != 0) || File::Exists(filename)) { 70 if (((file_mode & File::kWrite) != 0) || File::Exists(filename)) {
68 file = File::Open(filename, file_mode); 71 file = File::Open(filename, file_mode);
69 } 72 }
70 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file))); 73 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file)));
71 Dart_ExitScope(); 74 Dart_ExitScope();
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 } 360 }
358 361
359 362
360 void FUNCTION_NAME(File_GetStdioHandleType)(Dart_NativeArguments args) { 363 void FUNCTION_NAME(File_GetStdioHandleType)(Dart_NativeArguments args) {
361 Dart_EnterScope(); 364 Dart_EnterScope();
362 int fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); 365 int fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0));
363 File::StdioHandleType type = File::GetStdioHandleType(fd); 366 File::StdioHandleType type = File::GetStdioHandleType(fd);
364 Dart_SetReturnValue(args, Dart_NewInteger(type)); 367 Dart_SetReturnValue(args, Dart_NewInteger(type));
365 Dart_ExitScope(); 368 Dart_ExitScope();
366 } 369 }
370
371
372 static int64_t CObjectInt32OrInt64ToInt64(CObject* cobject) {
373 ASSERT(cobject->IsInt32OrInt64());
374 int64_t result;
375 if (cobject->IsInt32()) {
376 CObjectInt32 value(cobject);
377 result = value.Value();
378 } else {
379 CObjectInt64 value(cobject);
380 result = value.Value();
381 }
382 return result;
383 }
384
385
386 File* CObjectToFilePointer(CObject* cobject) {
387 CObjectIntptr value(cobject);
388 return reinterpret_cast<File*>(value.Value());
389 }
390
391
392 static CObject* FileExistsRequest(const CObjectArray& request) {
393 if (request.Length() == 2 && request[1]->IsString()) {
394 CObjectString filename(request[1]);
395 bool result = File::Exists(filename.CString());
396 return CObject::Bool(result);
397 }
398 return CObject::False();
399 }
400
401
402 static CObject* FileCreateRequest(const CObjectArray& request) {
403 if (request.Length() == 2 && request[1]->IsString()) {
404 CObjectString filename(request[1]);
405 bool result = File::Create(filename.CString());
406 return CObject::Bool(result);
407 }
408 return CObject::False();
409 }
410
411
412 static CObject* FileOpenRequest(const CObjectArray& request) {
413 File* file = NULL;
414 if (request.Length() == 3 &&
415 request[1]->IsString() &&
416 request[2]->IsInt32()) {
417 CObjectString filename(request[1]);
418 CObjectInt32 mode(request[2]);
419 File::DartFileOpenMode dart_file_mode =
420 static_cast<File::DartFileOpenMode>(mode.Value());
421 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode);
422 if (((file_mode & File::kWrite) != 0) || File::Exists(filename.CString())) {
423 file = File::Open(filename.CString(), file_mode);
424 }
425 }
426 return new CObjectIntptr(
427 CObject::NewIntptr(reinterpret_cast<intptr_t>(file)));
428 }
429
430
431 static CObject* FileDeleteRequest(const CObjectArray& request) {
432 if (request.Length() == 2 && request[1]->IsString()) {
433 CObjectString filename(request[1]);
434 bool result = File::Delete(filename.CString());
435 return CObject::Bool(result);
436 }
437 return CObject::False();
438 }
439
440
441 static CObject* FileFullPathRequest(const CObjectArray& request) {
442 if (request.Length() == 2 && request[1]->IsString()) {
443 CObjectString filename(request[1]);
444 char* path = File::GetCanonicalPath(filename.CString());
445 return new CObjectString(CObject::NewString(path));
446 }
447 return CObject::Null();
448 }
449
450
451 static CObject* FileDirectoryRequest(const CObjectArray& request) {
452 if (request.Length() == 2 && request[1]->IsString()) {
453 CObjectString filename(request[1]);
454 if (File::Exists(filename.CString())) {
455 char* str_copy = strdup(filename.CString());
456 char* path = File::GetContainingDirectory(str_copy);
457 free(str_copy);
458 if (path != NULL) {
459 CObject* result = new CObjectString(CObject::NewString(path));
460 free(path);
461 return result;
462 }
463 }
464 }
465 return CObject::Null();
466 }
467
468
469 static CObject* FileCloseRequest(const CObjectArray& request) {
470 intptr_t return_value = -1;
471 if (request.Length() == 2 && request[1]->IsIntptr()) {
472 File* file = CObjectToFilePointer(request[1]);
473 if (file != NULL) {
474 delete file;
475 return_value = 0;
476 }
477 }
478 return new CObjectIntptr(CObject::NewIntptr(return_value));
479 }
480
481
482 static CObject* FilePositionRequest(const CObjectArray& request) {
483 intptr_t return_value = -1;
484 if (request.Length() == 2 && request[1]->IsIntptr()) {
485 File* file = CObjectToFilePointer(request[1]);
486 if (file != NULL) {
487 return_value = file->Position();
488 }
489 }
490 return new CObjectIntptr(CObject::NewIntptr(return_value));
491 }
492
493
494 static CObject* FileSetPositionRequest(const CObjectArray& request) {
495 bool return_value = false;
496 if (request.Length() == 3 &&
497 request[1]->IsIntptr() &&
498 request[2]->IsInt32OrInt64()) {
499 File* file = CObjectToFilePointer(request[1]);
500 if (file != NULL) {
501 int64_t position = CObjectInt32OrInt64ToInt64(request[2]);
502 return_value = file->SetPosition(position);
503 }
504 }
505 return CObject::Bool(return_value);
506 }
507
508
509 static CObject* FileTruncateRequest(const CObjectArray& request) {
510 bool return_value = false;
511 if (request.Length() == 3 &&
512 request[1]->IsIntptr() &&
513 request[2]->IsInt32OrInt64()) {
514 File* file = CObjectToFilePointer(request[1]);
515 if (file != NULL) {
516 int64_t length = CObjectInt32OrInt64ToInt64(request[2]);
517 return_value = file->Truncate(length);
518 }
519 }
520 return CObject::Bool(return_value);
521 }
522
523
524 static CObject* FileLengthRequest(const CObjectArray& request) {
525 intptr_t return_value = -1;
526 if (request.Length() == 2 && request[1]->IsIntptr()) {
527 File* file = CObjectToFilePointer(request[1]);
528 if (file != NULL) {
529 return_value = file->Length();
530 }
531 }
532 return new CObjectIntptr(CObject::NewIntptr(return_value));
533 }
534
535
536 static CObject* FileFlushRequest(const CObjectArray& request) {
537 intptr_t return_value = -1;
538 if (request.Length() == 2 && request[1]->IsIntptr()) {
539 File* file = CObjectToFilePointer(request[1]);
540 if (file != NULL) {
541 file->Flush();
542 return_value = 0;
543 }
544 }
545 return new CObjectIntptr(CObject::NewIntptr(return_value));
546 }
547
548
549 static CObject* FileReadByteRequest(const CObjectArray& request) {
550 intptr_t return_value = -1;
551 if (request.Length() == 2 && request[1]->IsIntptr()) {
552 File* file = CObjectToFilePointer(request[1]);
553 if (file != NULL) {
554 uint8_t buffer;
555 int bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1);
556 if (bytes_read == 1) {
557 return_value = static_cast<intptr_t>(buffer);
558 } else {
559 return_value = -1;
560 }
561 }
562 }
563 return new CObjectIntptr(CObject::NewIntptr(return_value));
564 }
565
566 static CObject* FileWriteByteRequest(const CObjectArray& request) {
567 intptr_t return_value = -1;
568 if (request.Length() == 3 &&
569 request[1]->IsIntptr() &&
570 request[2]->IsInt32OrInt64()) {
571 File* file = CObjectToFilePointer(request[1]);
572 if (file != NULL) {
573 int64_t byte = CObjectInt32OrInt64ToInt64(request[2]);
574 uint8_t buffer = static_cast<uint8_t>(byte & 0xff);
575 int bytes_written = file->Write(reinterpret_cast<void*>(&buffer), 1);
576 if (bytes_written >= 0) {
577 return_value = bytes_written;
578 }
579 }
580 }
581 return new CObjectIntptr(CObject::NewIntptr(return_value));
582 }
583
584 static CObject* FileReadListRequest(const CObjectArray& request) {
585 if (request.Length() == 3 &&
586 request[1]->IsIntptr() &&
587 request[2]->IsInt32OrInt64()) {
588 File* file = CObjectToFilePointer(request[1]);
589 int64_t length = CObjectInt32OrInt64ToInt64(request[2]);
590 CObjectByteArray* byte_array =
591 new CObjectByteArray(CObject::NewByteArray(length));
592 void* buffer = reinterpret_cast<void*>(byte_array->Buffer());
593 int total_bytes_read = file->Read(buffer, byte_array->Length());
594 CObjectArray* result = new CObjectArray(CObject::NewArray(2));
595 result->SetAt(0, new CObjectIntptr(CObject::NewIntptr(total_bytes_read)));
596 result->SetAt(1, byte_array);
597 return result;
598 }
599 return CObject::Null();
600 }
601
602
603 static CObject* FileWriteListRequest(const CObjectArray& request) {
604 if (request.Length() == 5 &&
605 request[1]->IsIntptr() &&
606 (request[2]->IsByteArray() || request[2]->IsArray()) &&
607 request[3]->IsInt32OrInt64() &&
608 request[4]->IsInt32OrInt64()) {
609 File* file = CObjectToFilePointer(request[1]);
610 int64_t offset = CObjectInt32OrInt64ToInt64(request[3]);
611 int64_t length = CObjectInt32OrInt64ToInt64(request[4]);
612 uint8_t* buffer_start;
613 if (request[2]->IsByteArray()) {
614 CObjectByteArray byte_array(request[2]);
615 buffer_start = byte_array.Buffer() + offset;
616 } else {
617 CObjectArray array(request[2]);
618 buffer_start = new uint8_t[length];
619 for (int i = 0; i < length; i++) {
620 if (array[i + offset]->IsInt32OrInt64()) {
621 int64_t value = CObjectInt32OrInt64ToInt64(array[i + offset]);
622 buffer_start[i] = value & 0xFF;
623 } else {
624 // Unsupported type.
625 delete[] buffer_start;
626 return new CObjectInt32(CObject::NewInt32(-1));
627 }
628 }
629 offset = 0;
630 }
631 int64_t total_bytes_written =
632 file->Write(reinterpret_cast<void*>(buffer_start), length);
633 ASSERT(total_bytes_written == length);
634 if (!request[2]->IsByteArray()) {
635 delete[] buffer_start;
636 }
637 return new CObjectInt64(CObject::NewInt64(total_bytes_written));
638 }
639 return CObject::Null();
640 }
641
642
643 static CObject* FileWriteStringRequest(const CObjectArray& request) {
644 if (request.Length() == 3 &&
645 request[1]->IsIntptr() &&
646 request[2]->IsString()) {
647 File* file = CObjectToFilePointer(request[1]);
648 CObjectString str(request[2]);
649 const void* buffer = reinterpret_cast<const void*>(str.CString());
650 int64_t total_bytes_written = file->Write(buffer, str.Length());
651 ASSERT(total_bytes_written == str.Length());
652 return new CObjectInt64(CObject::NewInt64(total_bytes_written));
653 }
654 return CObject::Null();
655 }
656
657
658 void FileService(Dart_Port dest_port_id,
659 Dart_Port reply_port_id,
660 Dart_CObject* message) {
661 CObject* response = CObject::False();
662 CObjectArray request(message);
663 if (message->type == Dart_CObject::kArray) {
664 if (request.Length() > 1 && request[0]->IsInt32()) {
665 CObjectInt32 requestType(request[0]);
666 switch (requestType.Value()) {
667 case File::kExistsRequest:
668 response = FileExistsRequest(request);
669 break;
670 case File::kCreateRequest:
671 response = FileCreateRequest(request);
672 break;
673 case File::kOpenRequest:
674 response = FileOpenRequest(request);
675 break;
676 case File::kDeleteRequest:
677 response = FileDeleteRequest(request);
678 break;
679 case File::kFullPathRequest:
680 response = FileFullPathRequest(request);
681 break;
682 case File::kDirectoryRequest:
683 response = FileDirectoryRequest(request);
684 break;
685 case File::kCloseRequest:
686 response = FileCloseRequest(request);
687 break;
688 case File::kPositionRequest:
689 response = FilePositionRequest(request);
690 break;
691 case File::kSetPositionRequest:
692 response = FileSetPositionRequest(request);
693 break;
694 case File::kTruncateRequest:
695 response = FileTruncateRequest(request);
696 break;
697 case File::kLengthRequest:
698 response = FileLengthRequest(request);
699 break;
700 case File::kFlushRequest:
701 response = FileFlushRequest(request);
702 break;
703 case File::kReadByteRequest:
704 response = FileReadByteRequest(request);
705 break;
706 case File::kWriteByteRequest:
707 response = FileWriteByteRequest(request);
708 break;
709 case File::kReadListRequest:
710 response = FileReadListRequest(request);
711 break;
712 case File::kWriteListRequest:
713 response = FileWriteListRequest(request);
714 break;
715 case File::kWriteStringRequest:
716 response = FileWriteStringRequest(request);
717 break;
718 default:
719 UNREACHABLE();
720 }
721 }
722 }
723
724 Dart_PostCObject(reply_port_id, response->AsApiCObject());
725 }
726
727
728 void FUNCTION_NAME(File_NewServicePort)(Dart_NativeArguments args) {
729 Dart_EnterScope();
730 Dart_SetReturnValue(args, Dart_Null());
731 Dart_Port service_port = kIllegalPort;
732 service_port = Dart_NewNativePort("FileService",
733 FileService,
734 true);
735 if (service_port != kIllegalPort) {
736 // Return a send port for the service port.
737 Dart_Handle send_port = Dart_NewSendPort(service_port);
738 Dart_SetReturnValue(args, send_port);
739 }
740 Dart_ExitScope();
741 }
OLDNEW
« no previous file with comments | « runtime/bin/file.h ('k') | runtime/bin/file_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698