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/dartutils.h" | 5 #include "bin/dartutils.h" |
6 | 6 |
7 #include "bin/extensions.h" | 7 #include "bin/extensions.h" |
8 #include "bin/directory.h" | 8 #include "bin/directory.h" |
9 #include "bin/file.h" | 9 #include "bin/file.h" |
10 #include "bin/io_buffer.h" | 10 #include "bin/io_buffer.h" |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 } | 184 } |
185 // Calculate the canonical path. | 185 // Calculate the canonical path. |
186 const char* canon_url_str = GetCanonicalPath(library_url_str, url_str); | 186 const char* canon_url_str = GetCanonicalPath(library_url_str, url_str); |
187 Dart_Handle canon_url = NewString(canon_url_str); | 187 Dart_Handle canon_url = NewString(canon_url_str); |
188 free(const_cast<char*>(canon_url_str)); | 188 free(const_cast<char*>(canon_url_str)); |
189 | 189 |
190 return canon_url; | 190 return canon_url; |
191 } | 191 } |
192 | 192 |
193 | 193 |
194 void* DartUtils::OpenFile(const char* name, bool write) { | 194 static const uint8_t* ReadFile(const char* filename, |
195 File* file = File::Open(name, write ? File::kWriteTruncate : File::kRead); | 195 intptr_t* file_len, |
196 return reinterpret_cast<void*>(file); | 196 const char** error_msg) { |
197 } | 197 File* file = File::Open(filename, File::kRead); |
198 | 198 if (file == NULL) { |
199 | |
200 void DartUtils::ReadFile(const uint8_t** data, | |
201 intptr_t* file_len, | |
202 void* stream) { | |
203 ASSERT(data != NULL); | |
204 ASSERT(file_len != NULL); | |
205 ASSERT(stream != NULL); | |
206 File* file_stream = reinterpret_cast<File*>(stream); | |
207 *file_len = file_stream->Length(); | |
208 ASSERT(*file_len > 0); | |
209 uint8_t* text_buffer = reinterpret_cast<uint8_t*>(malloc(*file_len)); | |
210 ASSERT(text_buffer != NULL); | |
211 if (!file_stream->ReadFully(text_buffer, *file_len)) { | |
212 *data = NULL; | |
213 *file_len = -1; // Indicates read was not successful. | |
214 return; | |
215 } | |
216 *data = text_buffer; | |
217 } | |
218 | |
219 | |
220 void DartUtils::WriteFile(const void* buffer, | |
221 intptr_t num_bytes, | |
222 void* stream) { | |
223 ASSERT(stream != NULL); | |
224 File* file_stream = reinterpret_cast<File*>(stream); | |
225 bool bytes_written = file_stream->WriteFully(buffer, num_bytes); | |
226 ASSERT(bytes_written); | |
227 } | |
228 | |
229 | |
230 void DartUtils::CloseFile(void* stream) { | |
231 delete reinterpret_cast<File*>(stream); | |
232 } | |
233 | |
234 | |
235 static const uint8_t* ReadFileFully(const char* filename, | |
236 intptr_t* file_len, | |
237 const char** error_msg) { | |
238 void* stream = DartUtils::OpenFile(filename, false); | |
239 if (stream == NULL) { | |
240 const char* format = "Unable to open file: %s"; | 199 const char* format = "Unable to open file: %s"; |
241 intptr_t len = snprintf(NULL, 0, format, filename); | 200 intptr_t len = snprintf(NULL, 0, format, filename); |
242 // TODO(iposva): Allocate from the zone instead of leaking error string | 201 // TODO(iposva): Allocate from the zone instead of leaking error string |
243 // here. On the other hand the binary is about the exit anyway. | 202 // here. On the other hand the binary is about the exit anyway. |
244 char* msg = reinterpret_cast<char*>(malloc(len + 1)); | 203 char* msg = reinterpret_cast<char*>(malloc(len + 1)); |
245 snprintf(msg, len + 1, format, filename); | 204 snprintf(msg, len + 1, format, filename); |
246 *error_msg = msg; | 205 *error_msg = msg; |
247 return NULL; | 206 return NULL; |
248 } | 207 } |
249 *file_len = -1; | 208 *file_len = file->Length(); |
250 const uint8_t* text_buffer = NULL; | 209 uint8_t* text_buffer = reinterpret_cast<uint8_t*>(malloc(*file_len)); |
251 DartUtils::ReadFile(&text_buffer, file_len, stream); | 210 if (text_buffer == NULL) { |
252 if (text_buffer == NULL || *file_len == -1) { | 211 delete file; |
253 *error_msg = "Unable to read file contents"; | 212 *error_msg = "Unable to allocate buffer"; |
254 text_buffer = NULL; | 213 return NULL; |
255 } | 214 } |
256 DartUtils::CloseFile(stream); | 215 if (!file->ReadFully(text_buffer, *file_len)) { |
| 216 delete file; |
| 217 free(text_buffer); |
| 218 *error_msg = "Unable to fully read contents"; |
| 219 return NULL; |
| 220 } |
| 221 delete file; |
257 return text_buffer; | 222 return text_buffer; |
258 } | 223 } |
259 | 224 |
260 | 225 |
261 Dart_Handle DartUtils::ReadStringFromFile(const char* filename) { | 226 Dart_Handle DartUtils::ReadStringFromFile(const char* filename) { |
262 const char* error_msg = NULL; | 227 const char* error_msg = NULL; |
263 intptr_t len; | 228 intptr_t len; |
264 const uint8_t* text_buffer = ReadFileFully(filename, &len, &error_msg); | 229 const uint8_t* text_buffer = ReadFile(filename, &len, &error_msg); |
265 if (text_buffer == NULL) { | 230 if (text_buffer == NULL) { |
266 return Dart_Error(error_msg); | 231 return Dart_Error(error_msg); |
267 } | 232 } |
268 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len); | 233 Dart_Handle str = Dart_NewStringFromUTF8(text_buffer, len); |
269 return str; | 234 return str; |
270 } | 235 } |
271 | 236 |
272 | 237 |
273 Dart_Handle DartUtils::ResolveScriptUri(Dart_Handle script_uri, | 238 Dart_Handle DartUtils::ResolveScriptUri(Dart_Handle script_uri, |
274 Dart_Handle builtin_lib) { | 239 Dart_Handle builtin_lib) { |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 } | 377 } |
413 Dart_Handle script_path = DartUtils::FilePathFromUri(resolved_script_uri, | 378 Dart_Handle script_path = DartUtils::FilePathFromUri(resolved_script_uri, |
414 builtin_lib); | 379 builtin_lib); |
415 if (Dart_IsError(script_path)) { | 380 if (Dart_IsError(script_path)) { |
416 return script_path; | 381 return script_path; |
417 } | 382 } |
418 const char* script_path_cstr; | 383 const char* script_path_cstr; |
419 Dart_StringToCString(script_path, &script_path_cstr); | 384 Dart_StringToCString(script_path, &script_path_cstr); |
420 const char* error_msg = NULL; | 385 const char* error_msg = NULL; |
421 intptr_t len; | 386 intptr_t len; |
422 const uint8_t* text_buffer = ReadFileFully(script_path_cstr, | 387 const uint8_t* text_buffer = ReadFile(script_path_cstr, &len, &error_msg); |
423 &len, | |
424 &error_msg); | |
425 if (text_buffer == NULL) { | 388 if (text_buffer == NULL) { |
426 return Dart_Error(error_msg); | 389 return Dart_Error(error_msg); |
427 } | 390 } |
428 bool is_snapshot = false; | 391 bool is_snapshot = false; |
429 text_buffer = SniffForMagicNumber(text_buffer, &len, &is_snapshot); | 392 text_buffer = SniffForMagicNumber(text_buffer, &len, &is_snapshot); |
430 if (is_snapshot) { | 393 if (is_snapshot) { |
431 return Dart_LoadScriptFromSnapshot(text_buffer, len); | 394 return Dart_LoadScriptFromSnapshot(text_buffer, len); |
432 } else { | 395 } else { |
433 Dart_Handle source = Dart_NewStringFromUTF8(text_buffer, len); | 396 Dart_Handle source = Dart_NewStringFromUTF8(text_buffer, len); |
434 if (Dart_IsError(source)) { | 397 if (Dart_IsError(source)) { |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
789 new CObjectString(CObject::NewString(os_error->message())); | 752 new CObjectString(CObject::NewString(os_error->message())); |
790 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); | 753 CObjectArray* result = new CObjectArray(CObject::NewArray(3)); |
791 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); | 754 result->SetAt(0, new CObjectInt32(CObject::NewInt32(kOSError))); |
792 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); | 755 result->SetAt(1, new CObjectInt32(CObject::NewInt32(os_error->code()))); |
793 result->SetAt(2, error_message); | 756 result->SetAt(2, error_message); |
794 return result; | 757 return result; |
795 } | 758 } |
796 | 759 |
797 } // namespace bin | 760 } // namespace bin |
798 } // namespace dart | 761 } // namespace dart |
OLD | NEW |