Chromium Code Reviews| Index: runtime/bin/file.cc |
| diff --git a/runtime/bin/file.cc b/runtime/bin/file.cc |
| index 093ff4d4655fa7d49f9f64d5a08a1ac85e838c2c..52b38f2b3f1b37a74efc26f91466f847b52c25e5 100644 |
| --- a/runtime/bin/file.cc |
| +++ b/runtime/bin/file.cc |
| @@ -7,6 +7,7 @@ |
| #include "bin/builtin.h" |
| #include "bin/dartutils.h" |
| #include "bin/thread.h" |
| +#include "bin/utils.h" |
| #include "include/dart_api.h" |
| @@ -59,6 +60,25 @@ File::FileOpenMode File::DartModeToFileMode(DartFileOpenMode mode) { |
| } |
| +Dart_Handle MakeDartOSError(OSError* os_error) { |
|
Mads Ager (google)
2012/03/09 09:40:13
This is fine for now, but we should figure out how
Søren Gjesse
2012/03/13 08:25:55
Opened issue http://code.google.com/p/dart/issues/
|
| + Dart_Handle url = Dart_NewString("dart:io"); |
| + if (Dart_IsError(url)) return url; |
| + Dart_Handle lib = Dart_LookupLibrary(url); |
| + if (Dart_IsError(lib)) return lib; |
| + Dart_Handle cls = Dart_NewString("IOUtils"); |
| + if (Dart_IsError(cls)) return cls; |
| + Dart_Handle method = Dart_NewString("makeOSError"); |
| + if (Dart_IsError(method)) return method; |
| + Dart_Handle args[2]; |
| + args[0] = Dart_NewString(os_error->message()); |
| + if (Dart_IsError(args[0])) return args[0]; |
| + args[1] = Dart_NewInteger(os_error->code()); |
| + if (Dart_IsError(args[1])) return args[1]; |
| + Dart_Handle err = Dart_InvokeStatic(lib, cls, method, 2, args); |
| + return err; |
| +} |
| + |
| + |
| void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) { |
| Dart_EnterScope(); |
| const char* filename = |
| @@ -72,10 +92,18 @@ void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) { |
| // files. Directories can be opened for reading using the posix |
| // 'open' call. |
| File* file = NULL; |
| - if (((file_mode & File::kWrite) != 0) || File::Exists(filename)) { |
| - file = File::Open(filename, file_mode); |
| + OSError os_error; |
| + file = File::Open(filename, file_mode, &os_error); |
| + if (file == NULL) { |
| + Dart_Handle err = MakeDartOSError(&os_error); |
| + if (Dart_IsError(err)) { |
| + Dart_PropagateError(err); |
| + } |
| + Dart_SetReturnValue(args, err); |
| + } else { |
| + Dart_SetReturnValue(args, |
| + Dart_NewInteger(reinterpret_cast<intptr_t>(file))); |
| } |
| - Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file))); |
| Dart_ExitScope(); |
| } |
| @@ -413,9 +441,18 @@ static CObject* FileCreateRequest(const CObjectArray& request) { |
| return CObject::False(); |
| } |
| +static CObject* MakeOsError(OSError* os_error) { |
| + CObject* error_message = |
| + new CObjectString(CObject::NewString(os_error->message())); |
| + CObjectArray* result = new CObjectArray(CObject::NewArray(2)); |
| + result->SetAt(0, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
| + result->SetAt(1, error_message); |
| + return result; |
| +} |
| static CObject* FileOpenRequest(const CObjectArray& request) { |
| File* file = NULL; |
| + OSError os_error; |
| if (request.Length() == 3 && |
| request[1]->IsString() && |
| request[2]->IsInt32()) { |
| @@ -424,12 +461,14 @@ static CObject* FileOpenRequest(const CObjectArray& request) { |
| File::DartFileOpenMode dart_file_mode = |
| static_cast<File::DartFileOpenMode>(mode.Value()); |
| File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); |
| - if (((file_mode & File::kWrite) != 0) || File::Exists(filename.CString())) { |
| - file = File::Open(filename.CString(), file_mode); |
| - } |
| + file = File::Open(filename.CString(), file_mode, &os_error); |
| + } |
| + if (file != NULL) { |
| + return new CObjectIntptr( |
| + CObject::NewIntptr(reinterpret_cast<intptr_t>(file))); |
| + } else { |
| + return MakeOsError(&os_error); |
| } |
| - return new CObjectIntptr( |
| - CObject::NewIntptr(reinterpret_cast<intptr_t>(file))); |
| } |