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 #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/embedded_dart_io.h" | 9 #include "bin/embedded_dart_io.h" |
10 #include "bin/io_buffer.h" | 10 #include "bin/io_buffer.h" |
11 #include "bin/utils.h" | 11 #include "bin/utils.h" |
12 | 12 |
13 #include "include/dart_api.h" | 13 #include "include/dart_api.h" |
14 #include "include/dart_tools_api.h" | 14 #include "include/dart_tools_api.h" |
15 | 15 |
16 namespace dart { | 16 namespace dart { |
17 namespace bin { | 17 namespace bin { |
18 | 18 |
19 static const int kMSPerSecond = 1000; | 19 static const int kMSPerSecond = 1000; |
20 | 20 |
21 // Are we capturing output from stdout for the VM service? | 21 // Are we capturing output from stdout for the VM service? |
22 static bool capture_stdout = false; | 22 static bool capture_stdout = false; |
23 | 23 |
24 // Are we capturing output from stderr for the VM service? | 24 // Are we capturing output from stderr for the VM service? |
25 static bool capture_stderr = false; | 25 static bool capture_stderr = false; |
26 | 26 |
27 | |
28 void SetCaptureStdout(bool value) { | 27 void SetCaptureStdout(bool value) { |
29 capture_stdout = value; | 28 capture_stdout = value; |
30 } | 29 } |
31 | 30 |
32 | 31 |
33 void SetCaptureStderr(bool value) { | 32 void SetCaptureStderr(bool value) { |
34 capture_stderr = value; | 33 capture_stderr = value; |
35 } | 34 } |
36 | 35 |
37 | 36 |
38 bool ShouldCaptureStdout() { | 37 bool ShouldCaptureStdout() { |
39 return capture_stdout; | 38 return capture_stdout; |
40 } | 39 } |
41 | 40 |
42 | 41 |
43 bool ShouldCaptureStderr() { | 42 bool ShouldCaptureStderr() { |
44 return capture_stderr; | 43 return capture_stderr; |
45 } | 44 } |
46 | 45 |
47 | 46 |
48 | |
49 // The file pointer has been passed into Dart as an intptr_t and it is safe | 47 // The file pointer has been passed into Dart as an intptr_t and it is safe |
50 // to pull it out of Dart as a 64-bit integer, cast it to an intptr_t and | 48 // to pull it out of Dart as a 64-bit integer, cast it to an intptr_t and |
51 // from there to a File pointer. | 49 // from there to a File pointer. |
52 static File* GetFilePointer(Dart_Handle handle) { | 50 static File* GetFilePointer(Dart_Handle handle) { |
53 intptr_t value = DartUtils::GetIntptrValue(handle); | 51 intptr_t value = DartUtils::GetIntptrValue(handle); |
54 return reinterpret_cast<File*>(value); | 52 return reinterpret_cast<File*>(value); |
55 } | 53 } |
56 | 54 |
57 | 55 |
58 bool File::ReadFully(void* buffer, int64_t num_bytes) { | 56 bool File::ReadFully(void* buffer, int64_t num_bytes) { |
(...skipping 17 matching lines...) Expand all Loading... |
76 while (remaining > 0) { | 74 while (remaining > 0) { |
77 int64_t bytes_written = Write(current_buffer, remaining); | 75 int64_t bytes_written = Write(current_buffer, remaining); |
78 if (bytes_written < 0) { | 76 if (bytes_written < 0) { |
79 return false; | 77 return false; |
80 } | 78 } |
81 remaining -= bytes_written; // Reduce the number of remaining bytes. | 79 remaining -= bytes_written; // Reduce the number of remaining bytes. |
82 current_buffer += bytes_written; // Move the buffer forward. | 80 current_buffer += bytes_written; // Move the buffer forward. |
83 } | 81 } |
84 if (capture_stdout || capture_stderr) { | 82 if (capture_stdout || capture_stderr) { |
85 intptr_t fd = GetFD(); | 83 intptr_t fd = GetFD(); |
86 if (fd == STDOUT_FILENO && capture_stdout) { | 84 if ((fd == STDOUT_FILENO) && capture_stdout) { |
87 Dart_ServiceSendDataEvent("Stdout", "WriteEvent", | 85 Dart_ServiceSendDataEvent("Stdout", "WriteEvent", |
88 reinterpret_cast<const uint8_t*>(buffer), | 86 reinterpret_cast<const uint8_t*>(buffer), |
89 num_bytes); | 87 num_bytes); |
90 } else if (fd == STDERR_FILENO && capture_stderr) { | 88 } else if ((fd == STDERR_FILENO) && capture_stderr) { |
91 Dart_ServiceSendDataEvent("Stderr", "WriteEvent", | 89 Dart_ServiceSendDataEvent("Stderr", "WriteEvent", |
92 reinterpret_cast<const uint8_t*>(buffer), | 90 reinterpret_cast<const uint8_t*>(buffer), |
93 num_bytes); | 91 num_bytes); |
94 } | 92 } |
95 } | 93 } |
96 return true; | 94 return true; |
97 } | 95 } |
98 | 96 |
99 | 97 |
100 File::FileOpenMode File::DartModeToFileMode(DartFileOpenMode mode) { | 98 File::FileOpenMode File::DartModeToFileMode(DartFileOpenMode mode) { |
101 ASSERT(mode == File::kDartRead || | 99 ASSERT((mode == File::kDartRead) || |
102 mode == File::kDartWrite || | 100 (mode == File::kDartWrite) || |
103 mode == File::kDartAppend || | 101 (mode == File::kDartAppend) || |
104 mode == File::kDartWriteOnly || | 102 (mode == File::kDartWriteOnly) || |
105 mode == File::kDartWriteOnlyAppend); | 103 (mode == File::kDartWriteOnlyAppend)); |
106 if (mode == File::kDartWrite) { | 104 if (mode == File::kDartWrite) { |
107 return File::kWriteTruncate; | 105 return File::kWriteTruncate; |
108 } | 106 } |
109 if (mode == File::kDartAppend) { | 107 if (mode == File::kDartAppend) { |
110 return File::kWrite; | 108 return File::kWrite; |
111 } | 109 } |
112 if (mode == File::kDartWriteOnly) { | 110 if (mode == File::kDartWriteOnly) { |
113 return File::kWriteOnlyTruncate; | 111 return File::kWriteOnlyTruncate; |
114 } | 112 } |
115 if (mode == File::kDartWriteOnlyAppend) { | 113 if (mode == File::kDartWriteOnlyAppend) { |
116 return File::kWriteOnly; | 114 return File::kWriteOnly; |
117 } | 115 } |
118 return File::kRead; | 116 return File::kRead; |
119 } | 117 } |
120 | 118 |
121 | 119 |
122 void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) { | 120 void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) { |
123 const char* filename = | 121 const char* filename = |
124 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 122 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
125 int64_t mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 123 int64_t mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
126 File::DartFileOpenMode dart_file_mode = | 124 File::DartFileOpenMode dart_file_mode = |
127 static_cast<File::DartFileOpenMode>(mode); | 125 static_cast<File::DartFileOpenMode>(mode); |
128 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); | 126 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); |
129 // Check that the file exists before opening it only for | 127 // Check that the file exists before opening it only for |
130 // reading. This is to prevent the opening of directories as | 128 // reading. This is to prevent the opening of directories as |
131 // files. Directories can be opened for reading using the posix | 129 // files. Directories can be opened for reading using the posix |
132 // 'open' call. | 130 // 'open' call. |
133 File* file = NULL; | 131 File* file = NULL; |
134 file = File::Open(filename, file_mode); | 132 file = File::ScopedOpen(filename, file_mode); |
135 if (file != NULL) { | 133 if (file != NULL) { |
136 Dart_SetReturnValue(args, | 134 Dart_SetReturnValue(args, |
137 Dart_NewInteger(reinterpret_cast<intptr_t>(file))); | 135 Dart_NewInteger(reinterpret_cast<intptr_t>(file))); |
138 } else { | 136 } else { |
139 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 137 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
140 } | 138 } |
141 } | 139 } |
142 | 140 |
143 | 141 |
144 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { | 142 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 } else { | 210 } else { |
213 if (bytes_read < length) { | 211 if (bytes_read < length) { |
214 const int kNumArgs = 3; | 212 const int kNumArgs = 3; |
215 Dart_Handle dart_args[kNumArgs]; | 213 Dart_Handle dart_args[kNumArgs]; |
216 dart_args[0] = external_array; | 214 dart_args[0] = external_array; |
217 dart_args[1] = Dart_NewInteger(0); | 215 dart_args[1] = Dart_NewInteger(0); |
218 dart_args[2] = Dart_NewInteger(bytes_read); | 216 dart_args[2] = Dart_NewInteger(bytes_read); |
219 // TODO(sgjesse): Cache the _makeUint8ListView function somewhere. | 217 // TODO(sgjesse): Cache the _makeUint8ListView function somewhere. |
220 Dart_Handle io_lib = | 218 Dart_Handle io_lib = |
221 Dart_LookupLibrary(DartUtils::NewString("dart:io")); | 219 Dart_LookupLibrary(DartUtils::NewString("dart:io")); |
222 if (Dart_IsError(io_lib)) Dart_PropagateError(io_lib); | 220 if (Dart_IsError(io_lib)) { |
| 221 Dart_PropagateError(io_lib); |
| 222 } |
223 Dart_Handle array_view = | 223 Dart_Handle array_view = |
224 Dart_Invoke(io_lib, | 224 Dart_Invoke(io_lib, |
225 DartUtils::NewString("_makeUint8ListView"), | 225 DartUtils::NewString("_makeUint8ListView"), |
226 kNumArgs, | 226 kNumArgs, |
227 dart_args); | 227 dart_args); |
228 Dart_SetReturnValue(args, array_view); | 228 Dart_SetReturnValue(args, array_view); |
229 } else { | 229 } else { |
230 Dart_SetReturnValue(args, external_array); | 230 Dart_SetReturnValue(args, external_array); |
231 } | 231 } |
232 } | 232 } |
(...skipping 13 matching lines...) Expand all Loading... |
246 // integers and have the property that end <= | 246 // integers and have the property that end <= |
247 // list.length. Therefore, it is safe to extract their value as | 247 // list.length. Therefore, it is safe to extract their value as |
248 // intptr_t. | 248 // intptr_t. |
249 intptr_t start = | 249 intptr_t start = |
250 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | 250 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); |
251 intptr_t end = | 251 intptr_t end = |
252 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | 252 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); |
253 intptr_t length = end - start; | 253 intptr_t length = end - start; |
254 intptr_t array_len = 0; | 254 intptr_t array_len = 0; |
255 Dart_Handle result = Dart_ListLength(buffer_obj, &array_len); | 255 Dart_Handle result = Dart_ListLength(buffer_obj, &array_len); |
256 if (Dart_IsError(result)) Dart_PropagateError(result); | 256 if (Dart_IsError(result)) { |
| 257 Dart_PropagateError(result); |
| 258 } |
257 ASSERT(end <= array_len); | 259 ASSERT(end <= array_len); |
258 uint8_t* buffer = new uint8_t[length]; | 260 uint8_t* buffer = Dart_ScopeAllocate(length); |
259 int64_t bytes_read = file->Read(reinterpret_cast<void*>(buffer), length); | 261 int64_t bytes_read = file->Read(reinterpret_cast<void*>(buffer), length); |
260 if (bytes_read >= 0) { | 262 if (bytes_read >= 0) { |
261 result = Dart_ListSetAsBytes(buffer_obj, start, buffer, bytes_read); | 263 result = Dart_ListSetAsBytes(buffer_obj, start, buffer, bytes_read); |
262 if (Dart_IsError(result)) { | 264 if (Dart_IsError(result)) { |
263 Dart_SetReturnValue(args, result); | 265 Dart_SetReturnValue(args, result); |
264 } else { | 266 } else { |
265 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); | 267 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); |
266 } | 268 } |
267 } else { | 269 } else { |
268 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 270 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
269 } | 271 } |
270 delete[] buffer; | |
271 } | 272 } |
272 | 273 |
273 | 274 |
274 void FUNCTION_NAME(File_WriteFrom)(Dart_NativeArguments args) { | 275 void FUNCTION_NAME(File_WriteFrom)(Dart_NativeArguments args) { |
275 File* file = GetFilePointer(Dart_GetNativeArgument(args, 0)); | 276 File* file = GetFilePointer(Dart_GetNativeArgument(args, 0)); |
276 ASSERT(file != NULL); | 277 ASSERT(file != NULL); |
277 | 278 |
278 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 279 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
279 | 280 |
280 // Offset and length arguments are checked in Dart code to be | 281 // Offset and length arguments are checked in Dart code to be |
281 // integers and have the property that (offset + length) <= | 282 // integers and have the property that (offset + length) <= |
282 // list.length. Therefore, it is safe to extract their value as | 283 // list.length. Therefore, it is safe to extract their value as |
283 // intptr_t. | 284 // intptr_t. |
284 intptr_t start = | 285 intptr_t start = |
285 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | 286 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); |
286 intptr_t end = | 287 intptr_t end = |
287 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | 288 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); |
288 | 289 |
289 // The buffer object passed in has to be an Int8List or Uint8List object. | 290 // The buffer object passed in has to be an Int8List or Uint8List object. |
290 // Acquire a direct pointer to the data area of the buffer object. | 291 // Acquire a direct pointer to the data area of the buffer object. |
291 Dart_TypedData_Type type; | 292 Dart_TypedData_Type type; |
292 intptr_t length = end - start; | 293 intptr_t length = end - start; |
293 intptr_t buffer_len = 0; | 294 intptr_t buffer_len = 0; |
294 void* buffer = NULL; | 295 void* buffer = NULL; |
295 Dart_Handle result = | 296 Dart_Handle result = |
296 Dart_TypedDataAcquireData(buffer_obj, &type, &buffer, &buffer_len); | 297 Dart_TypedDataAcquireData(buffer_obj, &type, &buffer, &buffer_len); |
297 if (Dart_IsError(result)) Dart_PropagateError(result); | 298 if (Dart_IsError(result)) { |
| 299 Dart_PropagateError(result); |
| 300 } |
298 | 301 |
299 ASSERT(type == Dart_TypedData_kUint8 || type == Dart_TypedData_kInt8); | 302 ASSERT(type == Dart_TypedData_kUint8 || type == Dart_TypedData_kInt8); |
300 ASSERT(end <= buffer_len); | 303 ASSERT(end <= buffer_len); |
301 ASSERT(buffer != NULL); | 304 ASSERT(buffer != NULL); |
302 | 305 |
303 // Write all the data out into the file. | 306 // Write all the data out into the file. |
304 bool success = file->WriteFully(buffer, length); | 307 bool success = file->WriteFully(buffer, length); |
305 | 308 |
306 // Release the direct pointer acquired above. | 309 // Release the direct pointer acquired above. |
307 result = Dart_TypedDataReleaseData(buffer_obj); | 310 result = Dart_TypedDataReleaseData(buffer_obj); |
308 if (Dart_IsError(result)) Dart_PropagateError(result); | 311 if (Dart_IsError(result)) { |
| 312 Dart_PropagateError(result); |
| 313 } |
309 | 314 |
310 if (!success) { | 315 if (!success) { |
311 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 316 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
312 } else { | 317 } else { |
313 Dart_SetReturnValue(args, Dart_Null()); | 318 Dart_SetReturnValue(args, Dart_Null()); |
314 } | 319 } |
315 } | 320 } |
316 | 321 |
317 | 322 |
318 void FUNCTION_NAME(File_Position)(Dart_NativeArguments args) { | 323 void FUNCTION_NAME(File_Position)(Dart_NativeArguments args) { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 "Non-string argument to Link.create"); | 464 "Non-string argument to Link.create"); |
460 Dart_SetReturnValue(args, err); | 465 Dart_SetReturnValue(args, err); |
461 } | 466 } |
462 } | 467 } |
463 | 468 |
464 | 469 |
465 void FUNCTION_NAME(File_LinkTarget)(Dart_NativeArguments args) { | 470 void FUNCTION_NAME(File_LinkTarget)(Dart_NativeArguments args) { |
466 if (Dart_IsString(Dart_GetNativeArgument(args, 0))) { | 471 if (Dart_IsString(Dart_GetNativeArgument(args, 0))) { |
467 const char* name = | 472 const char* name = |
468 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 473 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
469 char* target = File::LinkTarget(name); | 474 const char* target = File::LinkTarget(name); |
470 if (target == NULL) { | 475 if (target == NULL) { |
471 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 476 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
472 } else { | 477 } else { |
473 Dart_SetReturnValue(args, DartUtils::NewString(target)); | 478 Dart_SetReturnValue(args, DartUtils::NewString(target)); |
474 free(target); | |
475 } | 479 } |
476 } else { | 480 } else { |
477 Dart_Handle err = DartUtils::NewDartArgumentError( | 481 Dart_Handle err = DartUtils::NewDartArgumentError( |
478 "Non-string argument to Link.target"); | 482 "Non-string argument to Link.target"); |
479 Dart_SetReturnValue(args, err); | 483 Dart_SetReturnValue(args, err); |
480 } | 484 } |
481 } | 485 } |
482 | 486 |
483 | 487 |
484 void FUNCTION_NAME(File_Delete)(Dart_NativeArguments args) { | 488 void FUNCTION_NAME(File_Delete)(Dart_NativeArguments args) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
543 Dart_SetReturnValue(args, Dart_NewBoolean(result)); | 547 Dart_SetReturnValue(args, Dart_NewBoolean(result)); |
544 } else { | 548 } else { |
545 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 549 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
546 } | 550 } |
547 } | 551 } |
548 | 552 |
549 | 553 |
550 void FUNCTION_NAME(File_ResolveSymbolicLinks)(Dart_NativeArguments args) { | 554 void FUNCTION_NAME(File_ResolveSymbolicLinks)(Dart_NativeArguments args) { |
551 const char* str = | 555 const char* str = |
552 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 556 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
553 char* path = File::GetCanonicalPath(str); | 557 const char* path = File::GetCanonicalPath(str); |
554 if (path != NULL) { | 558 if (path != NULL) { |
555 Dart_SetReturnValue(args, DartUtils::NewString(path)); | 559 Dart_SetReturnValue(args, DartUtils::NewString(path)); |
556 free(path); | |
557 } else { | 560 } else { |
558 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 561 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
559 } | 562 } |
560 } | 563 } |
561 | 564 |
562 | 565 |
563 void FUNCTION_NAME(File_OpenStdio)(Dart_NativeArguments args) { | 566 void FUNCTION_NAME(File_OpenStdio)(Dart_NativeArguments args) { |
564 int64_t fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); | 567 int64_t fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
565 ASSERT(fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO); | 568 ASSERT(fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO); |
566 File* file = File::OpenStdio(static_cast<int>(fd)); | 569 File* file = File::OpenStdio(static_cast<int>(fd)); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 const char* path = | 601 const char* path = |
599 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 602 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
600 | 603 |
601 int64_t stat_data[File::kStatSize]; | 604 int64_t stat_data[File::kStatSize]; |
602 File::Stat(path, stat_data); | 605 File::Stat(path, stat_data); |
603 if (stat_data[File::kType] == File::kDoesNotExist) { | 606 if (stat_data[File::kType] == File::kDoesNotExist) { |
604 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 607 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
605 } else { | 608 } else { |
606 Dart_Handle returned_data = Dart_NewTypedData(Dart_TypedData_kInt64, | 609 Dart_Handle returned_data = Dart_NewTypedData(Dart_TypedData_kInt64, |
607 File::kStatSize); | 610 File::kStatSize); |
608 if (Dart_IsError(returned_data)) Dart_PropagateError(returned_data); | 611 if (Dart_IsError(returned_data)) { |
| 612 Dart_PropagateError(returned_data); |
| 613 } |
609 Dart_TypedData_Type data_type_unused; | 614 Dart_TypedData_Type data_type_unused; |
610 void* data_location; | 615 void* data_location; |
611 intptr_t data_length_unused; | 616 intptr_t data_length_unused; |
612 Dart_Handle status = Dart_TypedDataAcquireData(returned_data, | 617 Dart_Handle status = Dart_TypedDataAcquireData(returned_data, |
613 &data_type_unused, | 618 &data_type_unused, |
614 &data_location, | 619 &data_location, |
615 &data_length_unused); | 620 &data_length_unused); |
616 if (Dart_IsError(status)) Dart_PropagateError(status); | 621 if (Dart_IsError(status)) { |
| 622 Dart_PropagateError(status); |
| 623 } |
617 memmove(data_location, stat_data, File::kStatSize * sizeof(int64_t)); | 624 memmove(data_location, stat_data, File::kStatSize * sizeof(int64_t)); |
618 status = Dart_TypedDataReleaseData(returned_data); | 625 status = Dart_TypedDataReleaseData(returned_data); |
619 if (Dart_IsError(status)) Dart_PropagateError(status); | 626 if (Dart_IsError(status)) { |
| 627 Dart_PropagateError(status); |
| 628 } |
620 Dart_SetReturnValue(args, returned_data); | 629 Dart_SetReturnValue(args, returned_data); |
621 } | 630 } |
622 } else { | 631 } else { |
623 Dart_Handle err = DartUtils::NewDartArgumentError( | 632 Dart_Handle err = DartUtils::NewDartArgumentError( |
624 "Non-string argument to FileSystemEntity.stat"); | 633 "Non-string argument to FileSystemEntity.stat"); |
625 Dart_SetReturnValue(args, err); | 634 Dart_SetReturnValue(args, err); |
626 } | 635 } |
627 } | 636 } |
628 | 637 |
629 | 638 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 } | 671 } |
663 | 672 |
664 | 673 |
665 File* CObjectToFilePointer(CObject* cobject) { | 674 File* CObjectToFilePointer(CObject* cobject) { |
666 CObjectIntptr value(cobject); | 675 CObjectIntptr value(cobject); |
667 return reinterpret_cast<File*>(value.Value()); | 676 return reinterpret_cast<File*>(value.Value()); |
668 } | 677 } |
669 | 678 |
670 | 679 |
671 CObject* File::ExistsRequest(const CObjectArray& request) { | 680 CObject* File::ExistsRequest(const CObjectArray& request) { |
672 if (request.Length() == 1 && request[0]->IsString()) { | 681 if ((request.Length() == 1) && request[0]->IsString()) { |
673 CObjectString filename(request[0]); | 682 CObjectString filename(request[0]); |
674 bool result = File::Exists(filename.CString()); | 683 bool result = File::Exists(filename.CString()); |
675 return CObject::Bool(result); | 684 return CObject::Bool(result); |
676 } | 685 } |
677 return CObject::IllegalArgumentError(); | 686 return CObject::IllegalArgumentError(); |
678 } | 687 } |
679 | 688 |
680 | 689 |
681 CObject* File::CreateRequest(const CObjectArray& request) { | 690 CObject* File::CreateRequest(const CObjectArray& request) { |
682 if (request.Length() == 1 && request[0]->IsString()) { | 691 if ((request.Length() == 1) && request[0]->IsString()) { |
683 CObjectString filename(request[0]); | 692 CObjectString filename(request[0]); |
684 bool result = File::Create(filename.CString()); | 693 bool result = File::Create(filename.CString()); |
685 if (result) { | 694 if (result) { |
686 return CObject::True(); | 695 return CObject::True(); |
687 } else { | 696 } else { |
688 return CObject::NewOSError(); | 697 return CObject::NewOSError(); |
689 } | 698 } |
690 } | 699 } |
691 return CObject::IllegalArgumentError(); | 700 return CObject::IllegalArgumentError(); |
692 } | 701 } |
693 | 702 |
| 703 |
694 CObject* File::OpenRequest(const CObjectArray& request) { | 704 CObject* File::OpenRequest(const CObjectArray& request) { |
695 File* file = NULL; | 705 File* file = NULL; |
696 if (request.Length() == 2 && | 706 if ((request.Length() == 2) && |
697 request[0]->IsString() && | 707 request[0]->IsString() && |
698 request[1]->IsInt32()) { | 708 request[1]->IsInt32()) { |
699 CObjectString filename(request[0]); | 709 CObjectString filename(request[0]); |
700 CObjectInt32 mode(request[1]); | 710 CObjectInt32 mode(request[1]); |
701 File::DartFileOpenMode dart_file_mode = | 711 File::DartFileOpenMode dart_file_mode = |
702 static_cast<File::DartFileOpenMode>(mode.Value()); | 712 static_cast<File::DartFileOpenMode>(mode.Value()); |
703 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); | 713 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); |
704 file = File::Open(filename.CString(), file_mode); | 714 file = File::ScopedOpen(filename.CString(), file_mode); |
705 if (file != NULL) { | 715 if (file != NULL) { |
706 return new CObjectIntptr( | 716 return new CObjectIntptr( |
707 CObject::NewIntptr(reinterpret_cast<intptr_t>(file))); | 717 CObject::NewIntptr(reinterpret_cast<intptr_t>(file))); |
708 } else { | 718 } else { |
709 return CObject::NewOSError(); | 719 return CObject::NewOSError(); |
710 } | 720 } |
711 } | 721 } |
712 return CObject::IllegalArgumentError(); | 722 return CObject::IllegalArgumentError(); |
713 } | 723 } |
714 | 724 |
715 | 725 |
716 CObject* File::DeleteRequest(const CObjectArray& request) { | 726 CObject* File::DeleteRequest(const CObjectArray& request) { |
717 if (request.Length() == 1 && request[0]->IsString()) { | 727 if ((request.Length() == 1) && request[0]->IsString()) { |
718 CObjectString filename(request[0]); | 728 CObjectString filename(request[0]); |
719 bool result = File::Delete(filename.CString()); | 729 bool result = File::Delete(filename.CString()); |
720 if (result) { | 730 if (result) { |
721 return CObject::True(); | 731 return CObject::True(); |
722 } else { | 732 } else { |
723 return CObject::NewOSError(); | 733 return CObject::NewOSError(); |
724 } | 734 } |
725 } | 735 } |
726 return CObject::False(); | 736 return CObject::False(); |
727 } | 737 } |
728 | 738 |
729 | 739 |
730 CObject* File::RenameRequest(const CObjectArray& request) { | 740 CObject* File::RenameRequest(const CObjectArray& request) { |
731 if (request.Length() == 2 && | 741 if ((request.Length() == 2) && |
732 request[0]->IsString() && | 742 request[0]->IsString() && |
733 request[1]->IsString()) { | 743 request[1]->IsString()) { |
734 CObjectString old_path(request[0]); | 744 CObjectString old_path(request[0]); |
735 CObjectString new_path(request[1]); | 745 CObjectString new_path(request[1]); |
736 bool completed = File::Rename(old_path.CString(), new_path.CString()); | 746 bool completed = File::Rename(old_path.CString(), new_path.CString()); |
737 if (completed) return CObject::True(); | 747 if (completed) { |
| 748 return CObject::True(); |
| 749 } |
738 return CObject::NewOSError(); | 750 return CObject::NewOSError(); |
739 } | 751 } |
740 return CObject::IllegalArgumentError(); | 752 return CObject::IllegalArgumentError(); |
741 } | 753 } |
742 | 754 |
743 | 755 |
744 CObject* File::CopyRequest(const CObjectArray& request) { | 756 CObject* File::CopyRequest(const CObjectArray& request) { |
745 if (request.Length() == 2 && | 757 if ((request.Length() == 2) && |
746 request[0]->IsString() && | 758 request[0]->IsString() && |
747 request[1]->IsString()) { | 759 request[1]->IsString()) { |
748 CObjectString old_path(request[0]); | 760 CObjectString old_path(request[0]); |
749 CObjectString new_path(request[1]); | 761 CObjectString new_path(request[1]); |
750 bool completed = File::Copy(old_path.CString(), new_path.CString()); | 762 bool completed = File::Copy(old_path.CString(), new_path.CString()); |
751 if (completed) return CObject::True(); | 763 if (completed) { |
| 764 return CObject::True(); |
| 765 } |
752 return CObject::NewOSError(); | 766 return CObject::NewOSError(); |
753 } | 767 } |
754 return CObject::IllegalArgumentError(); | 768 return CObject::IllegalArgumentError(); |
755 } | 769 } |
756 | 770 |
757 | 771 |
758 CObject* File::ResolveSymbolicLinksRequest(const CObjectArray& request) { | 772 CObject* File::ResolveSymbolicLinksRequest(const CObjectArray& request) { |
759 if (request.Length() == 1 && request[0]->IsString()) { | 773 if ((request.Length() == 1) && request[0]->IsString()) { |
760 CObjectString filename(request[0]); | 774 CObjectString filename(request[0]); |
761 char* result = File::GetCanonicalPath(filename.CString()); | 775 const char* result = File::GetCanonicalPath(filename.CString()); |
762 if (result != NULL) { | 776 if (result != NULL) { |
763 CObject* path = new CObjectString(CObject::NewString(result)); | 777 CObject* path = new CObjectString(CObject::NewString(result)); |
764 free(result); | |
765 return path; | 778 return path; |
766 } else { | 779 } else { |
767 return CObject::NewOSError(); | 780 return CObject::NewOSError(); |
768 } | 781 } |
769 } | 782 } |
770 return CObject::IllegalArgumentError(); | 783 return CObject::IllegalArgumentError(); |
771 } | 784 } |
772 | 785 |
773 | 786 |
774 CObject* File::CloseRequest(const CObjectArray& request) { | 787 CObject* File::CloseRequest(const CObjectArray& request) { |
775 intptr_t return_value = -1; | 788 intptr_t return_value = -1; |
776 if (request.Length() == 1 && request[0]->IsIntptr()) { | 789 if ((request.Length() == 1) && request[0]->IsIntptr()) { |
777 File* file = CObjectToFilePointer(request[0]); | 790 File* file = CObjectToFilePointer(request[0]); |
778 ASSERT(file != NULL); | 791 ASSERT(file != NULL); |
779 delete file; | 792 delete file; |
780 return_value = 0; | 793 return_value = 0; |
781 } | 794 } |
782 return new CObjectIntptr(CObject::NewIntptr(return_value)); | 795 return new CObjectIntptr(CObject::NewIntptr(return_value)); |
783 } | 796 } |
784 | 797 |
785 | 798 |
786 CObject* File::PositionRequest(const CObjectArray& request) { | 799 CObject* File::PositionRequest(const CObjectArray& request) { |
787 if (request.Length() == 1 && request[0]->IsIntptr()) { | 800 if ((request.Length() == 1) && request[0]->IsIntptr()) { |
788 File* file = CObjectToFilePointer(request[0]); | 801 File* file = CObjectToFilePointer(request[0]); |
789 ASSERT(file != NULL); | 802 ASSERT(file != NULL); |
790 if (!file->IsClosed()) { | 803 if (!file->IsClosed()) { |
791 intptr_t return_value = file->Position(); | 804 intptr_t return_value = file->Position(); |
792 if (return_value >= 0) { | 805 if (return_value >= 0) { |
793 return new CObjectIntptr(CObject::NewIntptr(return_value)); | 806 return new CObjectIntptr(CObject::NewIntptr(return_value)); |
794 } else { | 807 } else { |
795 return CObject::NewOSError(); | 808 return CObject::NewOSError(); |
796 } | 809 } |
797 } else { | 810 } else { |
798 return CObject::FileClosedError(); | 811 return CObject::FileClosedError(); |
799 } | 812 } |
800 } | 813 } |
801 return CObject::IllegalArgumentError(); | 814 return CObject::IllegalArgumentError(); |
802 } | 815 } |
803 | 816 |
804 | 817 |
805 CObject* File::SetPositionRequest(const CObjectArray& request) { | 818 CObject* File::SetPositionRequest(const CObjectArray& request) { |
806 if (request.Length() == 2 && | 819 if ((request.Length() == 2) && |
807 request[0]->IsIntptr() && | 820 request[0]->IsIntptr() && |
808 request[1]->IsInt32OrInt64()) { | 821 request[1]->IsInt32OrInt64()) { |
809 File* file = CObjectToFilePointer(request[0]); | 822 File* file = CObjectToFilePointer(request[0]); |
810 ASSERT(file != NULL); | 823 ASSERT(file != NULL); |
811 if (!file->IsClosed()) { | 824 if (!file->IsClosed()) { |
812 int64_t position = CObjectInt32OrInt64ToInt64(request[1]); | 825 int64_t position = CObjectInt32OrInt64ToInt64(request[1]); |
813 if (file->SetPosition(position)) { | 826 if (file->SetPosition(position)) { |
814 return CObject::True(); | 827 return CObject::True(); |
815 } else { | 828 } else { |
816 return CObject::NewOSError(); | 829 return CObject::NewOSError(); |
817 } | 830 } |
818 } else { | 831 } else { |
819 return CObject::FileClosedError(); | 832 return CObject::FileClosedError(); |
820 } | 833 } |
821 } | 834 } |
822 return CObject::IllegalArgumentError(); | 835 return CObject::IllegalArgumentError(); |
823 } | 836 } |
824 | 837 |
825 | 838 |
826 CObject* File::TruncateRequest(const CObjectArray& request) { | 839 CObject* File::TruncateRequest(const CObjectArray& request) { |
827 if (request.Length() == 2 && | 840 if ((request.Length() == 2) && |
828 request[0]->IsIntptr() && | 841 request[0]->IsIntptr() && |
829 request[1]->IsInt32OrInt64()) { | 842 request[1]->IsInt32OrInt64()) { |
830 File* file = CObjectToFilePointer(request[0]); | 843 File* file = CObjectToFilePointer(request[0]); |
831 ASSERT(file != NULL); | 844 ASSERT(file != NULL); |
832 if (!file->IsClosed()) { | 845 if (!file->IsClosed()) { |
833 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); | 846 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); |
834 if (file->Truncate(length)) { | 847 if (file->Truncate(length)) { |
835 return CObject::True(); | 848 return CObject::True(); |
836 } else { | 849 } else { |
837 return CObject::NewOSError(); | 850 return CObject::NewOSError(); |
838 } | 851 } |
839 } else { | 852 } else { |
840 return CObject::FileClosedError(); | 853 return CObject::FileClosedError(); |
841 } | 854 } |
842 } | 855 } |
843 return CObject::IllegalArgumentError(); | 856 return CObject::IllegalArgumentError(); |
844 } | 857 } |
845 | 858 |
846 | 859 |
847 CObject* File::LengthRequest(const CObjectArray& request) { | 860 CObject* File::LengthRequest(const CObjectArray& request) { |
848 if (request.Length() == 1 && request[0]->IsIntptr()) { | 861 if ((request.Length() == 1) && request[0]->IsIntptr()) { |
849 File* file = CObjectToFilePointer(request[0]); | 862 File* file = CObjectToFilePointer(request[0]); |
850 ASSERT(file != NULL); | 863 ASSERT(file != NULL); |
851 if (!file->IsClosed()) { | 864 if (!file->IsClosed()) { |
852 int64_t return_value = file->Length(); | 865 int64_t return_value = file->Length(); |
853 if (return_value >= 0) { | 866 if (return_value >= 0) { |
854 return new CObjectInt64(CObject::NewInt64(return_value)); | 867 return new CObjectInt64(CObject::NewInt64(return_value)); |
855 } else { | 868 } else { |
856 return CObject::NewOSError(); | 869 return CObject::NewOSError(); |
857 } | 870 } |
858 } else { | 871 } else { |
859 return CObject::FileClosedError(); | 872 return CObject::FileClosedError(); |
860 } | 873 } |
861 } | 874 } |
862 return CObject::IllegalArgumentError(); | 875 return CObject::IllegalArgumentError(); |
863 } | 876 } |
864 | 877 |
865 | 878 |
866 CObject* File::LengthFromPathRequest(const CObjectArray& request) { | 879 CObject* File::LengthFromPathRequest(const CObjectArray& request) { |
867 if (request.Length() == 1 && request[0]->IsString()) { | 880 if ((request.Length() == 1) && request[0]->IsString()) { |
868 CObjectString filepath(request[0]); | 881 CObjectString filepath(request[0]); |
869 int64_t return_value = File::LengthFromPath(filepath.CString()); | 882 int64_t return_value = File::LengthFromPath(filepath.CString()); |
870 if (return_value >= 0) { | 883 if (return_value >= 0) { |
871 return new CObjectInt64(CObject::NewInt64(return_value)); | 884 return new CObjectInt64(CObject::NewInt64(return_value)); |
872 } else { | 885 } else { |
873 return CObject::NewOSError(); | 886 return CObject::NewOSError(); |
874 } | 887 } |
875 } | 888 } |
876 return CObject::IllegalArgumentError(); | 889 return CObject::IllegalArgumentError(); |
877 } | 890 } |
878 | 891 |
879 | 892 |
880 CObject* File::LastModifiedRequest(const CObjectArray& request) { | 893 CObject* File::LastModifiedRequest(const CObjectArray& request) { |
881 if (request.Length() == 1 && request[0]->IsString()) { | 894 if ((request.Length() == 1) && request[0]->IsString()) { |
882 CObjectString filepath(request[0]); | 895 CObjectString filepath(request[0]); |
883 int64_t return_value = File::LastModified(filepath.CString()); | 896 int64_t return_value = File::LastModified(filepath.CString()); |
884 if (return_value >= 0) { | 897 if (return_value >= 0) { |
885 return new CObjectIntptr(CObject::NewInt64(return_value * kMSPerSecond)); | 898 return new CObjectIntptr(CObject::NewInt64(return_value * kMSPerSecond)); |
886 } else { | 899 } else { |
887 return CObject::NewOSError(); | 900 return CObject::NewOSError(); |
888 } | 901 } |
889 } | 902 } |
890 return CObject::IllegalArgumentError(); | 903 return CObject::IllegalArgumentError(); |
891 } | 904 } |
892 | 905 |
893 | 906 |
894 CObject* File::FlushRequest(const CObjectArray& request) { | 907 CObject* File::FlushRequest(const CObjectArray& request) { |
895 if (request.Length() == 1 && request[0]->IsIntptr()) { | 908 if ((request.Length() == 1) && request[0]->IsIntptr()) { |
896 File* file = CObjectToFilePointer(request[0]); | 909 File* file = CObjectToFilePointer(request[0]); |
897 ASSERT(file != NULL); | 910 ASSERT(file != NULL); |
898 if (!file->IsClosed()) { | 911 if (!file->IsClosed()) { |
899 if (file->Flush()) { | 912 if (file->Flush()) { |
900 return CObject::True(); | 913 return CObject::True(); |
901 } else { | 914 } else { |
902 return CObject::NewOSError(); | 915 return CObject::NewOSError(); |
903 } | 916 } |
904 } else { | 917 } else { |
905 return CObject::FileClosedError(); | 918 return CObject::FileClosedError(); |
906 } | 919 } |
907 } | 920 } |
908 return CObject::IllegalArgumentError(); | 921 return CObject::IllegalArgumentError(); |
909 } | 922 } |
910 | 923 |
911 | 924 |
912 CObject* File::ReadByteRequest(const CObjectArray& request) { | 925 CObject* File::ReadByteRequest(const CObjectArray& request) { |
913 if (request.Length() == 1 && request[0]->IsIntptr()) { | 926 if ((request.Length() == 1) && request[0]->IsIntptr()) { |
914 File* file = CObjectToFilePointer(request[0]); | 927 File* file = CObjectToFilePointer(request[0]); |
915 ASSERT(file != NULL); | 928 ASSERT(file != NULL); |
916 if (!file->IsClosed()) { | 929 if (!file->IsClosed()) { |
917 uint8_t buffer; | 930 uint8_t buffer; |
918 int64_t bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1); | 931 int64_t bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1); |
919 if (bytes_read > 0) { | 932 if (bytes_read > 0) { |
920 return new CObjectIntptr(CObject::NewIntptr(buffer)); | 933 return new CObjectIntptr(CObject::NewIntptr(buffer)); |
921 } else if (bytes_read == 0) { | 934 } else if (bytes_read == 0) { |
922 return new CObjectIntptr(CObject::NewIntptr(-1)); | 935 return new CObjectIntptr(CObject::NewIntptr(-1)); |
923 } else { | 936 } else { |
924 return CObject::NewOSError(); | 937 return CObject::NewOSError(); |
925 } | 938 } |
926 } else { | 939 } else { |
927 return CObject::FileClosedError(); | 940 return CObject::FileClosedError(); |
928 } | 941 } |
929 } | 942 } |
930 return CObject::IllegalArgumentError(); | 943 return CObject::IllegalArgumentError(); |
931 } | 944 } |
932 | 945 |
933 | 946 |
934 CObject* File::WriteByteRequest(const CObjectArray& request) { | 947 CObject* File::WriteByteRequest(const CObjectArray& request) { |
935 if (request.Length() == 2 && | 948 if ((request.Length() == 2) && |
936 request[0]->IsIntptr() && | 949 request[0]->IsIntptr() && |
937 request[1]->IsInt32OrInt64()) { | 950 request[1]->IsInt32OrInt64()) { |
938 File* file = CObjectToFilePointer(request[0]); | 951 File* file = CObjectToFilePointer(request[0]); |
939 ASSERT(file != NULL); | 952 ASSERT(file != NULL); |
940 if (!file->IsClosed()) { | 953 if (!file->IsClosed()) { |
941 int64_t byte = CObjectInt32OrInt64ToInt64(request[1]); | 954 int64_t byte = CObjectInt32OrInt64ToInt64(request[1]); |
942 uint8_t buffer = static_cast<uint8_t>(byte & 0xff); | 955 uint8_t buffer = static_cast<uint8_t>(byte & 0xff); |
943 bool success = file->WriteFully(reinterpret_cast<void*>(&buffer), 1); | 956 bool success = file->WriteFully(reinterpret_cast<void*>(&buffer), 1); |
944 if (success) { | 957 if (success) { |
945 return new CObjectInt64(CObject::NewInt64(1)); | 958 return new CObjectInt64(CObject::NewInt64(1)); |
946 } else { | 959 } else { |
947 return CObject::NewOSError(); | 960 return CObject::NewOSError(); |
948 } | 961 } |
949 } else { | 962 } else { |
950 return CObject::FileClosedError(); | 963 return CObject::FileClosedError(); |
951 } | 964 } |
952 } | 965 } |
953 return CObject::IllegalArgumentError(); | 966 return CObject::IllegalArgumentError(); |
954 } | 967 } |
955 | 968 |
956 | 969 |
957 CObject* File::ReadRequest(const CObjectArray& request) { | 970 CObject* File::ReadRequest(const CObjectArray& request) { |
958 if (request.Length() == 2 && | 971 if ((request.Length() == 2) && |
959 request[0]->IsIntptr() && | 972 request[0]->IsIntptr() && |
960 request[1]->IsInt32OrInt64()) { | 973 request[1]->IsInt32OrInt64()) { |
961 File* file = CObjectToFilePointer(request[0]); | 974 File* file = CObjectToFilePointer(request[0]); |
962 ASSERT(file != NULL); | 975 ASSERT(file != NULL); |
963 if (!file->IsClosed()) { | 976 if (!file->IsClosed()) { |
964 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); | 977 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); |
965 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); | 978 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); |
966 ASSERT(io_buffer != NULL); | 979 ASSERT(io_buffer != NULL); |
967 uint8_t* data = io_buffer->value.as_external_typed_data.data; | 980 uint8_t* data = io_buffer->value.as_external_typed_data.data; |
968 int64_t bytes_read = file->Read(data, length); | 981 int64_t bytes_read = file->Read(data, length); |
(...skipping 11 matching lines...) Expand all Loading... |
980 } | 993 } |
981 } else { | 994 } else { |
982 return CObject::FileClosedError(); | 995 return CObject::FileClosedError(); |
983 } | 996 } |
984 } | 997 } |
985 return CObject::IllegalArgumentError(); | 998 return CObject::IllegalArgumentError(); |
986 } | 999 } |
987 | 1000 |
988 | 1001 |
989 CObject* File::ReadIntoRequest(const CObjectArray& request) { | 1002 CObject* File::ReadIntoRequest(const CObjectArray& request) { |
990 if (request.Length() == 2 && | 1003 if ((request.Length() == 2) && |
991 request[0]->IsIntptr() && | 1004 request[0]->IsIntptr() && |
992 request[1]->IsInt32OrInt64()) { | 1005 request[1]->IsInt32OrInt64()) { |
993 File* file = CObjectToFilePointer(request[0]); | 1006 File* file = CObjectToFilePointer(request[0]); |
994 ASSERT(file != NULL); | 1007 ASSERT(file != NULL); |
995 if (!file->IsClosed()) { | 1008 if (!file->IsClosed()) { |
996 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); | 1009 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); |
997 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); | 1010 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); |
998 ASSERT(io_buffer != NULL); | 1011 ASSERT(io_buffer != NULL); |
999 uint8_t* data = io_buffer->value.as_external_typed_data.data; | 1012 uint8_t* data = io_buffer->value.as_external_typed_data.data; |
1000 int64_t bytes_read = file->Read(data, length); | 1013 int64_t bytes_read = file->Read(data, length); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1038 return 8; | 1051 return 8; |
1039 default: | 1052 default: |
1040 break; | 1053 break; |
1041 } | 1054 } |
1042 UNREACHABLE(); | 1055 UNREACHABLE(); |
1043 return -1; | 1056 return -1; |
1044 } | 1057 } |
1045 | 1058 |
1046 | 1059 |
1047 CObject* File::WriteFromRequest(const CObjectArray& request) { | 1060 CObject* File::WriteFromRequest(const CObjectArray& request) { |
1048 if (request.Length() == 4 && | 1061 if ((request.Length() == 4) && |
1049 request[0]->IsIntptr() && | 1062 request[0]->IsIntptr() && |
1050 (request[1]->IsTypedData() || request[1]->IsArray()) && | 1063 (request[1]->IsTypedData() || request[1]->IsArray()) && |
1051 request[2]->IsInt32OrInt64() && | 1064 request[2]->IsInt32OrInt64() && |
1052 request[3]->IsInt32OrInt64()) { | 1065 request[3]->IsInt32OrInt64()) { |
1053 File* file = CObjectToFilePointer(request[0]); | 1066 File* file = CObjectToFilePointer(request[0]); |
1054 ASSERT(file != NULL); | 1067 ASSERT(file != NULL); |
1055 if (!file->IsClosed()) { | 1068 if (!file->IsClosed()) { |
1056 int64_t start = CObjectInt32OrInt64ToInt64(request[2]); | 1069 int64_t start = CObjectInt32OrInt64ToInt64(request[2]); |
1057 int64_t end = CObjectInt32OrInt64ToInt64(request[3]); | 1070 int64_t end = CObjectInt32OrInt64ToInt64(request[3]); |
1058 int64_t length = end - start; | 1071 int64_t length = end - start; |
1059 uint8_t* buffer_start; | 1072 uint8_t* buffer_start; |
1060 if (request[1]->IsTypedData()) { | 1073 if (request[1]->IsTypedData()) { |
1061 CObjectTypedData typed_data(request[1]); | 1074 CObjectTypedData typed_data(request[1]); |
1062 start = start * SizeInBytes(typed_data.Type()); | 1075 start = start * SizeInBytes(typed_data.Type()); |
1063 length = length * SizeInBytes(typed_data.Type()); | 1076 length = length * SizeInBytes(typed_data.Type()); |
1064 buffer_start = typed_data.Buffer() + start; | 1077 buffer_start = typed_data.Buffer() + start; |
1065 } else { | 1078 } else { |
1066 CObjectArray array(request[1]); | 1079 CObjectArray array(request[1]); |
1067 buffer_start = new uint8_t[length]; | 1080 buffer_start = Dart_ScopeAllocate(length); |
1068 for (int i = 0; i < length; i++) { | 1081 for (int i = 0; i < length; i++) { |
1069 if (array[i + start]->IsInt32OrInt64()) { | 1082 if (array[i + start]->IsInt32OrInt64()) { |
1070 int64_t value = CObjectInt32OrInt64ToInt64(array[i + start]); | 1083 int64_t value = CObjectInt32OrInt64ToInt64(array[i + start]); |
1071 buffer_start[i] = static_cast<uint8_t>(value & 0xFF); | 1084 buffer_start[i] = static_cast<uint8_t>(value & 0xFF); |
1072 } else { | 1085 } else { |
1073 // Unsupported type. | 1086 // Unsupported type. |
1074 delete[] buffer_start; | |
1075 return CObject::IllegalArgumentError(); | 1087 return CObject::IllegalArgumentError(); |
1076 } | 1088 } |
1077 } | 1089 } |
1078 start = 0; | 1090 start = 0; |
1079 } | 1091 } |
1080 bool success = | 1092 bool success = |
1081 file->WriteFully(reinterpret_cast<void*>(buffer_start), length); | 1093 file->WriteFully(reinterpret_cast<void*>(buffer_start), length); |
1082 if (!request[1]->IsTypedData()) { | |
1083 delete[] buffer_start; | |
1084 } | |
1085 if (success) { | 1094 if (success) { |
1086 return new CObjectInt64(CObject::NewInt64(length)); | 1095 return new CObjectInt64(CObject::NewInt64(length)); |
1087 } else { | 1096 } else { |
1088 return CObject::NewOSError(); | 1097 return CObject::NewOSError(); |
1089 } | 1098 } |
1090 } else { | 1099 } else { |
1091 return CObject::FileClosedError(); | 1100 return CObject::FileClosedError(); |
1092 } | 1101 } |
1093 } | 1102 } |
1094 return CObject::IllegalArgumentError(); | 1103 return CObject::IllegalArgumentError(); |
1095 } | 1104 } |
1096 | 1105 |
1097 | 1106 |
1098 CObject* File::CreateLinkRequest(const CObjectArray& request) { | 1107 CObject* File::CreateLinkRequest(const CObjectArray& request) { |
1099 if (request.Length() != 2 || | 1108 if ((request.Length() != 2) || |
1100 !request[0]->IsString() || | 1109 !request[0]->IsString() || |
1101 !request[1]->IsString()) { | 1110 !request[1]->IsString()) { |
1102 return CObject::IllegalArgumentError(); | 1111 return CObject::IllegalArgumentError(); |
1103 } | 1112 } |
1104 CObjectString link_name(request[0]); | 1113 CObjectString link_name(request[0]); |
1105 CObjectString target_name(request[1]); | 1114 CObjectString target_name(request[1]); |
1106 if (File::CreateLink(link_name.CString(), target_name.CString())) { | 1115 if (File::CreateLink(link_name.CString(), target_name.CString())) { |
1107 return CObject::True(); | 1116 return CObject::True(); |
1108 } else { | 1117 } else { |
1109 return CObject::NewOSError(); | 1118 return CObject::NewOSError(); |
1110 } | 1119 } |
1111 } | 1120 } |
1112 | 1121 |
1113 | 1122 |
1114 CObject* File::DeleteLinkRequest(const CObjectArray& request) { | 1123 CObject* File::DeleteLinkRequest(const CObjectArray& request) { |
1115 if (request.Length() == 1 && request[0]->IsString()) { | 1124 if ((request.Length() == 1) && request[0]->IsString()) { |
1116 CObjectString link_path(request[0]); | 1125 CObjectString link_path(request[0]); |
1117 bool result = File::DeleteLink(link_path.CString()); | 1126 bool result = File::DeleteLink(link_path.CString()); |
1118 if (result) { | 1127 if (result) { |
1119 return CObject::True(); | 1128 return CObject::True(); |
1120 } else { | 1129 } else { |
1121 return CObject::NewOSError(); | 1130 return CObject::NewOSError(); |
1122 } | 1131 } |
1123 } | 1132 } |
1124 return CObject::IllegalArgumentError(); | 1133 return CObject::IllegalArgumentError(); |
1125 } | 1134 } |
1126 | 1135 |
1127 | 1136 |
1128 CObject* File::RenameLinkRequest(const CObjectArray& request) { | 1137 CObject* File::RenameLinkRequest(const CObjectArray& request) { |
1129 if (request.Length() == 2 && | 1138 if ((request.Length() == 2) && |
1130 request[0]->IsString() && | 1139 request[0]->IsString() && |
1131 request[1]->IsString()) { | 1140 request[1]->IsString()) { |
1132 CObjectString old_path(request[0]); | 1141 CObjectString old_path(request[0]); |
1133 CObjectString new_path(request[1]); | 1142 CObjectString new_path(request[1]); |
1134 bool completed = File::RenameLink(old_path.CString(), new_path.CString()); | 1143 bool completed = File::RenameLink(old_path.CString(), new_path.CString()); |
1135 if (completed) return CObject::True(); | 1144 if (completed) { |
| 1145 return CObject::True(); |
| 1146 } |
1136 return CObject::NewOSError(); | 1147 return CObject::NewOSError(); |
1137 } | 1148 } |
1138 return CObject::IllegalArgumentError(); | 1149 return CObject::IllegalArgumentError(); |
1139 } | 1150 } |
1140 | 1151 |
1141 | 1152 |
1142 CObject* File::LinkTargetRequest(const CObjectArray& request) { | 1153 CObject* File::LinkTargetRequest(const CObjectArray& request) { |
1143 if (request.Length() == 1 && request[0]->IsString()) { | 1154 if ((request.Length() == 1) && request[0]->IsString()) { |
1144 CObjectString link_path(request[0]); | 1155 CObjectString link_path(request[0]); |
1145 char* target = File::LinkTarget(link_path.CString()); | 1156 const char* target = File::LinkTarget(link_path.CString()); |
1146 if (target != NULL) { | 1157 if (target != NULL) { |
1147 CObject* result = new CObjectString(CObject::NewString(target)); | 1158 CObject* result = new CObjectString(CObject::NewString(target)); |
1148 free(target); | |
1149 return result; | 1159 return result; |
1150 } else { | 1160 } else { |
1151 return CObject::NewOSError(); | 1161 return CObject::NewOSError(); |
1152 } | 1162 } |
1153 } | 1163 } |
1154 return CObject::IllegalArgumentError(); | 1164 return CObject::IllegalArgumentError(); |
1155 } | 1165 } |
1156 | 1166 |
1157 | 1167 |
1158 CObject* File::TypeRequest(const CObjectArray& request) { | 1168 CObject* File::TypeRequest(const CObjectArray& request) { |
1159 if (request.Length() == 2 && | 1169 if ((request.Length() == 2) && |
1160 request[0]->IsString() && | 1170 request[0]->IsString() && |
1161 request[1]->IsBool()) { | 1171 request[1]->IsBool()) { |
1162 CObjectString path(request[0]); | 1172 CObjectString path(request[0]); |
1163 CObjectBool follow_links(request[1]); | 1173 CObjectBool follow_links(request[1]); |
1164 File::Type type = File::GetType(path.CString(), follow_links.Value()); | 1174 File::Type type = File::GetType(path.CString(), follow_links.Value()); |
1165 return new CObjectInt32(CObject::NewInt32(type)); | 1175 return new CObjectInt32(CObject::NewInt32(type)); |
1166 } | 1176 } |
1167 return CObject::IllegalArgumentError(); | 1177 return CObject::IllegalArgumentError(); |
1168 } | 1178 } |
1169 | 1179 |
1170 | 1180 |
1171 CObject* File::IdenticalRequest(const CObjectArray& request) { | 1181 CObject* File::IdenticalRequest(const CObjectArray& request) { |
1172 if (request.Length() == 2 && | 1182 if ((request.Length() == 2) && |
1173 request[0]->IsString() && | 1183 request[0]->IsString() && |
1174 request[1]->IsString()) { | 1184 request[1]->IsString()) { |
1175 CObjectString path1(request[0]); | 1185 CObjectString path1(request[0]); |
1176 CObjectString path2(request[1]); | 1186 CObjectString path2(request[1]); |
1177 File::Identical result = File::AreIdentical(path1.CString(), | 1187 File::Identical result = File::AreIdentical(path1.CString(), |
1178 path2.CString()); | 1188 path2.CString()); |
1179 if (result == File::kError) { | 1189 if (result == File::kError) { |
1180 return CObject::NewOSError(); | 1190 return CObject::NewOSError(); |
1181 } else if (result == File::kIdentical) { | 1191 } else if (result == File::kIdentical) { |
1182 return CObject::True(); | 1192 return CObject::True(); |
1183 } else { | 1193 } else { |
1184 return CObject::False(); | 1194 return CObject::False(); |
1185 } | 1195 } |
1186 } | 1196 } |
1187 return CObject::IllegalArgumentError(); | 1197 return CObject::IllegalArgumentError(); |
1188 } | 1198 } |
1189 | 1199 |
1190 | 1200 |
1191 CObject* File::StatRequest(const CObjectArray& request) { | 1201 CObject* File::StatRequest(const CObjectArray& request) { |
1192 if (request.Length() == 1 && | 1202 if ((request.Length() == 1) && request[0]->IsString()) { |
1193 request[0]->IsString()) { | |
1194 int64_t data[File::kStatSize]; | 1203 int64_t data[File::kStatSize]; |
1195 CObjectString path(request[0]); | 1204 CObjectString path(request[0]); |
1196 File::Stat(path.CString(), data); | 1205 File::Stat(path.CString(), data); |
1197 if (data[File::kType] == File::kDoesNotExist) { | 1206 if (data[File::kType] == File::kDoesNotExist) { |
1198 return CObject::NewOSError(); | 1207 return CObject::NewOSError(); |
1199 } | 1208 } |
1200 CObjectArray* result = | 1209 CObjectArray* result = |
1201 new CObjectArray(CObject::NewArray(File::kStatSize)); | 1210 new CObjectArray(CObject::NewArray(File::kStatSize)); |
1202 for (int i = 0; i < File::kStatSize; ++i) { | 1211 for (int i = 0; i < File::kStatSize; ++i) { |
1203 result->SetAt(i, new CObjectInt64(CObject::NewInt64(data[i]))); | 1212 result->SetAt(i, new CObjectInt64(CObject::NewInt64(data[i]))); |
1204 } | 1213 } |
1205 CObjectArray* wrapper = new CObjectArray(CObject::NewArray(2)); | 1214 CObjectArray* wrapper = new CObjectArray(CObject::NewArray(2)); |
1206 wrapper->SetAt(0, new CObjectInt32(CObject::NewInt32(CObject::kSuccess))); | 1215 wrapper->SetAt(0, new CObjectInt32(CObject::NewInt32(CObject::kSuccess))); |
1207 wrapper->SetAt(1, result); | 1216 wrapper->SetAt(1, result); |
1208 return wrapper; | 1217 return wrapper; |
1209 } | 1218 } |
1210 return CObject::IllegalArgumentError(); | 1219 return CObject::IllegalArgumentError(); |
1211 } | 1220 } |
1212 | 1221 |
1213 | 1222 |
1214 CObject* File::LockRequest(const CObjectArray& request) { | 1223 CObject* File::LockRequest(const CObjectArray& request) { |
1215 if (request.Length() == 4 && | 1224 if ((request.Length() == 4) && |
1216 request[0]->IsIntptr() && | 1225 request[0]->IsIntptr() && |
1217 request[1]->IsInt32OrInt64() && | 1226 request[1]->IsInt32OrInt64() && |
1218 request[2]->IsInt32OrInt64() && | 1227 request[2]->IsInt32OrInt64() && |
1219 request[3]->IsInt32OrInt64()) { | 1228 request[3]->IsInt32OrInt64()) { |
1220 File* file = CObjectToFilePointer(request[0]); | 1229 File* file = CObjectToFilePointer(request[0]); |
1221 ASSERT(file != NULL); | 1230 ASSERT(file != NULL); |
1222 if (!file->IsClosed()) { | 1231 if (!file->IsClosed()) { |
1223 int64_t lock = CObjectInt32OrInt64ToInt64(request[1]); | 1232 int64_t lock = CObjectInt32OrInt64ToInt64(request[1]); |
1224 int64_t start = CObjectInt32OrInt64ToInt64(request[2]); | 1233 int64_t start = CObjectInt32OrInt64ToInt64(request[2]); |
1225 int64_t end = CObjectInt32OrInt64ToInt64(request[3]); | 1234 int64_t end = CObjectInt32OrInt64ToInt64(request[3]); |
1226 if (file->Lock(static_cast<File::LockType>(lock), start, end)) { | 1235 if (file->Lock(static_cast<File::LockType>(lock), start, end)) { |
1227 return CObject::True(); | 1236 return CObject::True(); |
1228 } else { | 1237 } else { |
1229 return CObject::NewOSError(); | 1238 return CObject::NewOSError(); |
1230 } | 1239 } |
1231 } else { | 1240 } else { |
1232 return CObject::FileClosedError(); | 1241 return CObject::FileClosedError(); |
1233 } | 1242 } |
1234 } | 1243 } |
1235 return CObject::IllegalArgumentError(); | 1244 return CObject::IllegalArgumentError(); |
1236 } | 1245 } |
1237 | 1246 |
1238 } // namespace bin | 1247 } // namespace bin |
1239 } // namespace dart | 1248 } // namespace dart |
OLD | NEW |