| OLD | NEW |
| 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 #if !defined(DART_IO_DISABLED) |
| 6 |
| 5 #include "bin/file.h" | 7 #include "bin/file.h" |
| 6 | 8 |
| 7 #include "bin/builtin.h" | 9 #include "bin/builtin.h" |
| 8 #include "bin/dartutils.h" | 10 #include "bin/dartutils.h" |
| 9 #include "bin/embedded_dart_io.h" | 11 #include "bin/embedded_dart_io.h" |
| 10 #include "bin/io_buffer.h" | 12 #include "bin/io_buffer.h" |
| 11 #include "bin/utils.h" | 13 #include "bin/utils.h" |
| 12 | 14 |
| 13 #include "include/dart_api.h" | 15 #include "include/dart_api.h" |
| 14 #include "include/dart_tools_api.h" | 16 #include "include/dart_tools_api.h" |
| 15 | 17 |
| 16 namespace dart { | 18 namespace dart { |
| 17 namespace bin { | 19 namespace bin { |
| 18 | 20 |
| 19 static const int kMSPerSecond = 1000; | 21 static const int kMSPerSecond = 1000; |
| 20 | 22 |
| 21 // Are we capturing output from stdout for the VM service? | |
| 22 static bool capture_stdout = false; | |
| 23 | |
| 24 // Are we capturing output from stderr for the VM service? | |
| 25 static bool capture_stderr = false; | |
| 26 | |
| 27 void SetCaptureStdout(bool value) { | |
| 28 capture_stdout = value; | |
| 29 } | |
| 30 | |
| 31 | |
| 32 void SetCaptureStderr(bool value) { | |
| 33 capture_stderr = value; | |
| 34 } | |
| 35 | |
| 36 | |
| 37 bool ShouldCaptureStdout() { | |
| 38 return capture_stdout; | |
| 39 } | |
| 40 | |
| 41 | |
| 42 bool ShouldCaptureStderr() { | |
| 43 return capture_stderr; | |
| 44 } | |
| 45 | |
| 46 | |
| 47 // The file pointer has been passed into Dart as an intptr_t and it is safe | 23 // The file pointer has been passed into Dart as an intptr_t and it is safe |
| 48 // to pull it out of Dart as a 64-bit integer, cast it to an intptr_t and | 24 // to pull it out of Dart as a 64-bit integer, cast it to an intptr_t and |
| 49 // from there to a File pointer. | 25 // from there to a File pointer. |
| 50 static File* GetFilePointer(Dart_Handle handle) { | 26 static File* GetFilePointer(Dart_Handle handle) { |
| 51 intptr_t value = DartUtils::GetIntptrValue(handle); | 27 intptr_t value = DartUtils::GetIntptrValue(handle); |
| 52 return reinterpret_cast<File*>(value); | 28 return reinterpret_cast<File*>(value); |
| 53 } | 29 } |
| 54 | 30 |
| 55 | 31 |
| 56 bool File::ReadFully(void* buffer, int64_t num_bytes) { | |
| 57 int64_t remaining = num_bytes; | |
| 58 char* current_buffer = reinterpret_cast<char*>(buffer); | |
| 59 while (remaining > 0) { | |
| 60 int64_t bytes_read = Read(current_buffer, remaining); | |
| 61 if (bytes_read <= 0) { | |
| 62 return false; | |
| 63 } | |
| 64 remaining -= bytes_read; // Reduce the number of remaining bytes. | |
| 65 current_buffer += bytes_read; // Move the buffer forward. | |
| 66 } | |
| 67 return true; | |
| 68 } | |
| 69 | |
| 70 | |
| 71 bool File::WriteFully(const void* buffer, int64_t num_bytes) { | |
| 72 int64_t remaining = num_bytes; | |
| 73 const char* current_buffer = reinterpret_cast<const char*>(buffer); | |
| 74 while (remaining > 0) { | |
| 75 int64_t bytes_written = Write(current_buffer, remaining); | |
| 76 if (bytes_written < 0) { | |
| 77 return false; | |
| 78 } | |
| 79 remaining -= bytes_written; // Reduce the number of remaining bytes. | |
| 80 current_buffer += bytes_written; // Move the buffer forward. | |
| 81 } | |
| 82 if (capture_stdout || capture_stderr) { | |
| 83 intptr_t fd = GetFD(); | |
| 84 if ((fd == STDOUT_FILENO) && capture_stdout) { | |
| 85 Dart_ServiceSendDataEvent("Stdout", "WriteEvent", | |
| 86 reinterpret_cast<const uint8_t*>(buffer), | |
| 87 num_bytes); | |
| 88 } else if ((fd == STDERR_FILENO) && capture_stderr) { | |
| 89 Dart_ServiceSendDataEvent("Stderr", "WriteEvent", | |
| 90 reinterpret_cast<const uint8_t*>(buffer), | |
| 91 num_bytes); | |
| 92 } | |
| 93 } | |
| 94 return true; | |
| 95 } | |
| 96 | |
| 97 | |
| 98 File::FileOpenMode File::DartModeToFileMode(DartFileOpenMode mode) { | |
| 99 ASSERT((mode == File::kDartRead) || | |
| 100 (mode == File::kDartWrite) || | |
| 101 (mode == File::kDartAppend) || | |
| 102 (mode == File::kDartWriteOnly) || | |
| 103 (mode == File::kDartWriteOnlyAppend)); | |
| 104 if (mode == File::kDartWrite) { | |
| 105 return File::kWriteTruncate; | |
| 106 } | |
| 107 if (mode == File::kDartAppend) { | |
| 108 return File::kWrite; | |
| 109 } | |
| 110 if (mode == File::kDartWriteOnly) { | |
| 111 return File::kWriteOnlyTruncate; | |
| 112 } | |
| 113 if (mode == File::kDartWriteOnlyAppend) { | |
| 114 return File::kWriteOnly; | |
| 115 } | |
| 116 return File::kRead; | |
| 117 } | |
| 118 | |
| 119 | |
| 120 void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) { | 32 void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) { |
| 121 const char* filename = | 33 const char* filename = |
| 122 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 34 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 123 int64_t mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 35 int64_t mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
| 124 File::DartFileOpenMode dart_file_mode = | 36 File::DartFileOpenMode dart_file_mode = |
| 125 static_cast<File::DartFileOpenMode>(mode); | 37 static_cast<File::DartFileOpenMode>(mode); |
| 126 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); | 38 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); |
| 127 // Check that the file exists before opening it only for | 39 // Check that the file exists before opening it only for |
| 128 // reading. This is to prevent the opening of directories as | 40 // reading. This is to prevent the opening of directories as |
| 129 // files. Directories can be opened for reading using the posix | 41 // files. Directories can be opened for reading using the posix |
| (...skipping 1113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1243 } | 1155 } |
| 1244 } else { | 1156 } else { |
| 1245 return CObject::FileClosedError(); | 1157 return CObject::FileClosedError(); |
| 1246 } | 1158 } |
| 1247 } | 1159 } |
| 1248 return CObject::IllegalArgumentError(); | 1160 return CObject::IllegalArgumentError(); |
| 1249 } | 1161 } |
| 1250 | 1162 |
| 1251 } // namespace bin | 1163 } // namespace bin |
| 1252 } // namespace dart | 1164 } // namespace dart |
| 1165 |
| 1166 #endif // !defined(DART_IO_DISABLED) |
| OLD | NEW |