OLD | NEW |
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 #include "bin/thread.h" | 9 #include "bin/thread.h" |
10 | 10 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 65 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
66 int mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 66 int mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
67 File::DartFileOpenMode dart_file_mode = | 67 File::DartFileOpenMode dart_file_mode = |
68 static_cast<File::DartFileOpenMode>(mode); | 68 static_cast<File::DartFileOpenMode>(mode); |
69 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); | 69 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); |
70 // Check that the file exists before opening it only for | 70 // Check that the file exists before opening it only for |
71 // reading. This is to prevent the opening of directories as | 71 // reading. This is to prevent the opening of directories as |
72 // files. Directories can be opened for reading using the posix | 72 // files. Directories can be opened for reading using the posix |
73 // 'open' call. | 73 // 'open' call. |
74 File* file = NULL; | 74 File* file = NULL; |
75 if (((file_mode & File::kWrite) != 0) || File::Exists(filename)) { | 75 OSError os_error; |
76 file = File::Open(filename, file_mode); | 76 file = File::Open(filename, file_mode, &os_error); |
| 77 if (file == NULL) { |
| 78 Dart_Handle os_error_handle = Dart_GetNativeArgument(args, 2); |
| 79 DartUtils::SetIntegerInstanceField( |
| 80 os_error_handle, "errorCode", os_error.errno_); |
| 81 DartUtils::SetStringInstanceField( |
| 82 os_error_handle, "message", os_error.strerror_); |
77 } | 83 } |
78 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file))); | 84 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file))); |
79 Dart_ExitScope(); | 85 Dart_ExitScope(); |
80 } | 86 } |
81 | 87 |
82 | 88 |
83 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { | 89 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { |
84 Dart_EnterScope(); | 90 Dart_EnterScope(); |
85 const char* filename = | 91 const char* filename = |
86 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 92 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 | 412 |
407 static CObject* FileCreateRequest(const CObjectArray& request) { | 413 static CObject* FileCreateRequest(const CObjectArray& request) { |
408 if (request.Length() == 2 && request[1]->IsString()) { | 414 if (request.Length() == 2 && request[1]->IsString()) { |
409 CObjectString filename(request[1]); | 415 CObjectString filename(request[1]); |
410 bool result = File::Create(filename.CString()); | 416 bool result = File::Create(filename.CString()); |
411 return CObject::Bool(result); | 417 return CObject::Bool(result); |
412 } | 418 } |
413 return CObject::False(); | 419 return CObject::False(); |
414 } | 420 } |
415 | 421 |
| 422 static CObject* MakeOsError(OSError os_error) { |
| 423 CObject* error_message = |
| 424 new CObjectString(CObject::NewString(os_error.strerror_)); |
| 425 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); |
| 426 result->SetAt(0, new CObjectInt32(CObject::NewInt32(os_error.errno_))); |
| 427 result->SetAt(1, error_message); |
| 428 return result; |
| 429 } |
416 | 430 |
417 static CObject* FileOpenRequest(const CObjectArray& request) { | 431 static CObject* FileOpenRequest(const CObjectArray& request) { |
418 File* file = NULL; | 432 File* file = NULL; |
| 433 OSError os_error; |
419 if (request.Length() == 3 && | 434 if (request.Length() == 3 && |
420 request[1]->IsString() && | 435 request[1]->IsString() && |
421 request[2]->IsInt32()) { | 436 request[2]->IsInt32()) { |
422 CObjectString filename(request[1]); | 437 CObjectString filename(request[1]); |
423 CObjectInt32 mode(request[2]); | 438 CObjectInt32 mode(request[2]); |
424 File::DartFileOpenMode dart_file_mode = | 439 File::DartFileOpenMode dart_file_mode = |
425 static_cast<File::DartFileOpenMode>(mode.Value()); | 440 static_cast<File::DartFileOpenMode>(mode.Value()); |
426 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); | 441 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); |
427 if (((file_mode & File::kWrite) != 0) || File::Exists(filename.CString())) { | 442 file = File::Open(filename.CString(), file_mode, &os_error); |
428 file = File::Open(filename.CString(), file_mode); | |
429 } | |
430 } | 443 } |
431 return new CObjectIntptr( | 444 if (file != NULL) { |
432 CObject::NewIntptr(reinterpret_cast<intptr_t>(file))); | 445 return new CObjectIntptr( |
| 446 CObject::NewIntptr(reinterpret_cast<intptr_t>(file))); |
| 447 } else { |
| 448 return MakeOsError(os_error); |
| 449 } |
433 } | 450 } |
434 | 451 |
435 | 452 |
436 static CObject* FileDeleteRequest(const CObjectArray& request) { | 453 static CObject* FileDeleteRequest(const CObjectArray& request) { |
437 if (request.Length() == 2 && request[1]->IsString()) { | 454 if (request.Length() == 2 && request[1]->IsString()) { |
438 CObjectString filename(request[1]); | 455 CObjectString filename(request[1]); |
439 bool result = File::Delete(filename.CString()); | 456 bool result = File::Delete(filename.CString()); |
440 return CObject::Bool(result); | 457 return CObject::Bool(result); |
441 } | 458 } |
442 return CObject::False(); | 459 return CObject::False(); |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 Dart_EnterScope(); | 776 Dart_EnterScope(); |
760 Dart_SetReturnValue(args, Dart_Null()); | 777 Dart_SetReturnValue(args, Dart_Null()); |
761 Dart_Port service_port = File::GetServicePort(); | 778 Dart_Port service_port = File::GetServicePort(); |
762 if (service_port != kIllegalPort) { | 779 if (service_port != kIllegalPort) { |
763 // Return a send port for the service port. | 780 // Return a send port for the service port. |
764 Dart_Handle send_port = Dart_NewSendPort(service_port); | 781 Dart_Handle send_port = Dart_NewSendPort(service_port); |
765 Dart_SetReturnValue(args, send_port); | 782 Dart_SetReturnValue(args, send_port); |
766 } | 783 } |
767 Dart_ExitScope(); | 784 Dart_ExitScope(); |
768 } | 785 } |
OLD | NEW |