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 #include "bin/utils.h" | |
10 | 11 |
11 #include "include/dart_api.h" | 12 #include "include/dart_api.h" |
12 | 13 |
13 dart::Mutex File::mutex_; | 14 dart::Mutex File::mutex_; |
14 int File::service_ports_size_ = 0; | 15 int File::service_ports_size_ = 0; |
15 Dart_Port* File::service_ports_ = NULL; | 16 Dart_Port* File::service_ports_ = NULL; |
16 int File::service_ports_index_ = 0; | 17 int File::service_ports_index_ = 0; |
17 | 18 |
18 bool File::ReadFully(void* buffer, int64_t num_bytes) { | 19 bool File::ReadFully(void* buffer, int64_t num_bytes) { |
19 int64_t remaining = num_bytes; | 20 int64_t remaining = num_bytes; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 if (mode == File::kDartWrite) { | 53 if (mode == File::kDartWrite) { |
53 return File::kWriteTruncate; | 54 return File::kWriteTruncate; |
54 } | 55 } |
55 if (mode == File::kDartAppend) { | 56 if (mode == File::kDartAppend) { |
56 return File::kWrite; | 57 return File::kWrite; |
57 } | 58 } |
58 return File::kRead; | 59 return File::kRead; |
59 } | 60 } |
60 | 61 |
61 | 62 |
63 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/
| |
64 Dart_Handle url = Dart_NewString("dart:io"); | |
65 if (Dart_IsError(url)) return url; | |
66 Dart_Handle lib = Dart_LookupLibrary(url); | |
67 if (Dart_IsError(lib)) return lib; | |
68 Dart_Handle cls = Dart_NewString("IOUtils"); | |
69 if (Dart_IsError(cls)) return cls; | |
70 Dart_Handle method = Dart_NewString("makeOSError"); | |
71 if (Dart_IsError(method)) return method; | |
72 Dart_Handle args[2]; | |
73 args[0] = Dart_NewString(os_error->message()); | |
74 if (Dart_IsError(args[0])) return args[0]; | |
75 args[1] = Dart_NewInteger(os_error->code()); | |
76 if (Dart_IsError(args[1])) return args[1]; | |
77 Dart_Handle err = Dart_InvokeStatic(lib, cls, method, 2, args); | |
78 return err; | |
79 } | |
80 | |
81 | |
62 void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) { | 82 void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) { |
63 Dart_EnterScope(); | 83 Dart_EnterScope(); |
64 const char* filename = | 84 const char* filename = |
65 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 85 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
66 int mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 86 int mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
67 File::DartFileOpenMode dart_file_mode = | 87 File::DartFileOpenMode dart_file_mode = |
68 static_cast<File::DartFileOpenMode>(mode); | 88 static_cast<File::DartFileOpenMode>(mode); |
69 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); | 89 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); |
70 // Check that the file exists before opening it only for | 90 // Check that the file exists before opening it only for |
71 // reading. This is to prevent the opening of directories as | 91 // reading. This is to prevent the opening of directories as |
72 // files. Directories can be opened for reading using the posix | 92 // files. Directories can be opened for reading using the posix |
73 // 'open' call. | 93 // 'open' call. |
74 File* file = NULL; | 94 File* file = NULL; |
75 if (((file_mode & File::kWrite) != 0) || File::Exists(filename)) { | 95 OSError os_error; |
76 file = File::Open(filename, file_mode); | 96 file = File::Open(filename, file_mode, &os_error); |
97 if (file == NULL) { | |
98 Dart_Handle err = MakeDartOSError(&os_error); | |
99 if (Dart_IsError(err)) { | |
100 Dart_PropagateError(err); | |
101 } | |
102 Dart_SetReturnValue(args, err); | |
103 } else { | |
104 Dart_SetReturnValue(args, | |
105 Dart_NewInteger(reinterpret_cast<intptr_t>(file))); | |
77 } | 106 } |
78 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file))); | |
79 Dart_ExitScope(); | 107 Dart_ExitScope(); |
80 } | 108 } |
81 | 109 |
82 | 110 |
83 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { | 111 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { |
84 Dart_EnterScope(); | 112 Dart_EnterScope(); |
85 const char* filename = | 113 const char* filename = |
86 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 114 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
87 bool exists = File::Exists(filename); | 115 bool exists = File::Exists(filename); |
88 Dart_SetReturnValue(args, Dart_NewBoolean(exists)); | 116 Dart_SetReturnValue(args, Dart_NewBoolean(exists)); |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
406 | 434 |
407 static CObject* FileCreateRequest(const CObjectArray& request) { | 435 static CObject* FileCreateRequest(const CObjectArray& request) { |
408 if (request.Length() == 2 && request[1]->IsString()) { | 436 if (request.Length() == 2 && request[1]->IsString()) { |
409 CObjectString filename(request[1]); | 437 CObjectString filename(request[1]); |
410 bool result = File::Create(filename.CString()); | 438 bool result = File::Create(filename.CString()); |
411 return CObject::Bool(result); | 439 return CObject::Bool(result); |
412 } | 440 } |
413 return CObject::False(); | 441 return CObject::False(); |
414 } | 442 } |
415 | 443 |
444 static CObject* MakeOsError(OSError* os_error) { | |
445 CObject* error_message = | |
446 new CObjectString(CObject::NewString(os_error->message())); | |
447 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); | |
448 result->SetAt(0, new CObjectInt32(CObject::NewInt32(os_error->code()))); | |
449 result->SetAt(1, error_message); | |
450 return result; | |
451 } | |
416 | 452 |
417 static CObject* FileOpenRequest(const CObjectArray& request) { | 453 static CObject* FileOpenRequest(const CObjectArray& request) { |
418 File* file = NULL; | 454 File* file = NULL; |
455 OSError os_error; | |
419 if (request.Length() == 3 && | 456 if (request.Length() == 3 && |
420 request[1]->IsString() && | 457 request[1]->IsString() && |
421 request[2]->IsInt32()) { | 458 request[2]->IsInt32()) { |
422 CObjectString filename(request[1]); | 459 CObjectString filename(request[1]); |
423 CObjectInt32 mode(request[2]); | 460 CObjectInt32 mode(request[2]); |
424 File::DartFileOpenMode dart_file_mode = | 461 File::DartFileOpenMode dart_file_mode = |
425 static_cast<File::DartFileOpenMode>(mode.Value()); | 462 static_cast<File::DartFileOpenMode>(mode.Value()); |
426 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); | 463 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); |
427 if (((file_mode & File::kWrite) != 0) || File::Exists(filename.CString())) { | 464 file = File::Open(filename.CString(), file_mode, &os_error); |
428 file = File::Open(filename.CString(), file_mode); | |
429 } | |
430 } | 465 } |
431 return new CObjectIntptr( | 466 if (file != NULL) { |
432 CObject::NewIntptr(reinterpret_cast<intptr_t>(file))); | 467 return new CObjectIntptr( |
468 CObject::NewIntptr(reinterpret_cast<intptr_t>(file))); | |
469 } else { | |
470 return MakeOsError(&os_error); | |
471 } | |
433 } | 472 } |
434 | 473 |
435 | 474 |
436 static CObject* FileDeleteRequest(const CObjectArray& request) { | 475 static CObject* FileDeleteRequest(const CObjectArray& request) { |
437 if (request.Length() == 2 && request[1]->IsString()) { | 476 if (request.Length() == 2 && request[1]->IsString()) { |
438 CObjectString filename(request[1]); | 477 CObjectString filename(request[1]); |
439 bool result = File::Delete(filename.CString()); | 478 bool result = File::Delete(filename.CString()); |
440 return CObject::Bool(result); | 479 return CObject::Bool(result); |
441 } | 480 } |
442 return CObject::False(); | 481 return CObject::False(); |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
759 Dart_EnterScope(); | 798 Dart_EnterScope(); |
760 Dart_SetReturnValue(args, Dart_Null()); | 799 Dart_SetReturnValue(args, Dart_Null()); |
761 Dart_Port service_port = File::GetServicePort(); | 800 Dart_Port service_port = File::GetServicePort(); |
762 if (service_port != kIllegalPort) { | 801 if (service_port != kIllegalPort) { |
763 // Return a send port for the service port. | 802 // Return a send port for the service port. |
764 Dart_Handle send_port = Dart_NewSendPort(service_port); | 803 Dart_Handle send_port = Dart_NewSendPort(service_port); |
765 Dart_SetReturnValue(args, send_port); | 804 Dart_SetReturnValue(args, send_port); |
766 } | 805 } |
767 Dart_ExitScope(); | 806 Dart_ExitScope(); |
768 } | 807 } |
OLD | NEW |