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) | 5 #if !defined(DART_IO_DISABLED) |
6 | 6 |
7 #include "bin/file.h" | 7 #include "bin/file.h" |
8 | 8 |
9 #include "bin/builtin.h" | 9 #include "bin/builtin.h" |
10 #include "bin/dartutils.h" | 10 #include "bin/dartutils.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 // from there to a File pointer. | 25 // from there to a File pointer. |
26 static File* GetFile(Dart_NativeArguments args) { | 26 static File* GetFile(Dart_NativeArguments args) { |
27 File* file; | 27 File* file; |
28 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); | 28 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); |
29 ASSERT(Dart_IsInstance(dart_this)); | 29 ASSERT(Dart_IsInstance(dart_this)); |
30 ThrowIfError(Dart_GetNativeInstanceField(dart_this, kFileNativeFieldIndex, | 30 ThrowIfError(Dart_GetNativeInstanceField(dart_this, kFileNativeFieldIndex, |
31 reinterpret_cast<intptr_t*>(&file))); | 31 reinterpret_cast<intptr_t*>(&file))); |
32 return file; | 32 return file; |
33 } | 33 } |
34 | 34 |
35 | |
36 static void SetFile(Dart_Handle dart_this, intptr_t file_pointer) { | 35 static void SetFile(Dart_Handle dart_this, intptr_t file_pointer) { |
37 Dart_Handle result = Dart_SetNativeInstanceField( | 36 Dart_Handle result = Dart_SetNativeInstanceField( |
38 dart_this, kFileNativeFieldIndex, file_pointer); | 37 dart_this, kFileNativeFieldIndex, file_pointer); |
39 if (Dart_IsError(result)) { | 38 if (Dart_IsError(result)) { |
40 Log::PrintErr("SetNativeInstanceField in SetFile() failed\n"); | 39 Log::PrintErr("SetNativeInstanceField in SetFile() failed\n"); |
41 Dart_PropagateError(result); | 40 Dart_PropagateError(result); |
42 } | 41 } |
43 } | 42 } |
44 | 43 |
45 | |
46 void FUNCTION_NAME(File_GetPointer)(Dart_NativeArguments args) { | 44 void FUNCTION_NAME(File_GetPointer)(Dart_NativeArguments args) { |
47 File* file = GetFile(args); | 45 File* file = GetFile(args); |
48 // If the file is already closed, GetFile() will return NULL. | 46 // If the file is already closed, GetFile() will return NULL. |
49 if (file != NULL) { | 47 if (file != NULL) { |
50 // Increment file's reference count. File_GetPointer() should only be called | 48 // Increment file's reference count. File_GetPointer() should only be called |
51 // when we are about to send the File* to the IO Service. | 49 // when we are about to send the File* to the IO Service. |
52 file->Retain(); | 50 file->Retain(); |
53 } | 51 } |
54 intptr_t file_pointer = reinterpret_cast<intptr_t>(file); | 52 intptr_t file_pointer = reinterpret_cast<intptr_t>(file); |
55 Dart_SetReturnValue(args, Dart_NewInteger(file_pointer)); | 53 Dart_SetReturnValue(args, Dart_NewInteger(file_pointer)); |
56 } | 54 } |
57 | 55 |
58 | |
59 static void ReleaseFile(void* isolate_callback_data, | 56 static void ReleaseFile(void* isolate_callback_data, |
60 Dart_WeakPersistentHandle handle, | 57 Dart_WeakPersistentHandle handle, |
61 void* peer) { | 58 void* peer) { |
62 File* file = reinterpret_cast<File*>(peer); | 59 File* file = reinterpret_cast<File*>(peer); |
63 file->Release(); | 60 file->Release(); |
64 } | 61 } |
65 | 62 |
66 | |
67 void FUNCTION_NAME(File_SetPointer)(Dart_NativeArguments args) { | 63 void FUNCTION_NAME(File_SetPointer)(Dart_NativeArguments args) { |
68 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); | 64 Dart_Handle dart_this = ThrowIfError(Dart_GetNativeArgument(args, 0)); |
69 intptr_t file_pointer = | 65 intptr_t file_pointer = |
70 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); | 66 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); |
71 File* file = reinterpret_cast<File*>(file_pointer); | 67 File* file = reinterpret_cast<File*>(file_pointer); |
72 Dart_WeakPersistentHandle handle = Dart_NewWeakPersistentHandle( | 68 Dart_WeakPersistentHandle handle = Dart_NewWeakPersistentHandle( |
73 dart_this, reinterpret_cast<void*>(file), sizeof(*file), ReleaseFile); | 69 dart_this, reinterpret_cast<void*>(file), sizeof(*file), ReleaseFile); |
74 file->SetWeakHandle(handle); | 70 file->SetWeakHandle(handle); |
75 SetFile(dart_this, file_pointer); | 71 SetFile(dart_this, file_pointer); |
76 } | 72 } |
77 | 73 |
78 | |
79 void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) { | 74 void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) { |
80 const char* filename = | 75 const char* filename = |
81 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 76 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
82 int64_t mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 77 int64_t mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
83 File::DartFileOpenMode dart_file_mode = | 78 File::DartFileOpenMode dart_file_mode = |
84 static_cast<File::DartFileOpenMode>(mode); | 79 static_cast<File::DartFileOpenMode>(mode); |
85 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); | 80 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); |
86 // Check that the file exists before opening it only for | 81 // Check that the file exists before opening it only for |
87 // reading. This is to prevent the opening of directories as | 82 // reading. This is to prevent the opening of directories as |
88 // files. Directories can be opened for reading using the posix | 83 // files. Directories can be opened for reading using the posix |
89 // 'open' call. | 84 // 'open' call. |
90 File* file = File::Open(filename, file_mode); | 85 File* file = File::Open(filename, file_mode); |
91 if (file != NULL) { | 86 if (file != NULL) { |
92 Dart_SetReturnValue(args, | 87 Dart_SetReturnValue(args, |
93 Dart_NewInteger(reinterpret_cast<intptr_t>(file))); | 88 Dart_NewInteger(reinterpret_cast<intptr_t>(file))); |
94 } else { | 89 } else { |
95 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 90 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
96 } | 91 } |
97 } | 92 } |
98 | 93 |
99 | |
100 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { | 94 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { |
101 const char* filename = | 95 const char* filename = |
102 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 96 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
103 bool exists = File::Exists(filename); | 97 bool exists = File::Exists(filename); |
104 Dart_SetReturnValue(args, Dart_NewBoolean(exists)); | 98 Dart_SetReturnValue(args, Dart_NewBoolean(exists)); |
105 } | 99 } |
106 | 100 |
107 | |
108 void FUNCTION_NAME(File_Close)(Dart_NativeArguments args) { | 101 void FUNCTION_NAME(File_Close)(Dart_NativeArguments args) { |
109 File* file = GetFile(args); | 102 File* file = GetFile(args); |
110 ASSERT(file != NULL); | 103 ASSERT(file != NULL); |
111 file->Close(); | 104 file->Close(); |
112 file->DeleteWeakHandle(Dart_CurrentIsolate()); | 105 file->DeleteWeakHandle(Dart_CurrentIsolate()); |
113 file->Release(); | 106 file->Release(); |
114 | 107 |
115 // NULL-out the now potentially dangling pointer. | 108 // NULL-out the now potentially dangling pointer. |
116 Dart_Handle dart_this = Dart_GetNativeArgument(args, 0); | 109 Dart_Handle dart_this = Dart_GetNativeArgument(args, 0); |
117 SetFile(dart_this, 0); | 110 SetFile(dart_this, 0); |
118 Dart_SetReturnValue(args, Dart_NewInteger(0)); | 111 Dart_SetReturnValue(args, Dart_NewInteger(0)); |
119 } | 112 } |
120 | 113 |
121 | |
122 void FUNCTION_NAME(File_ReadByte)(Dart_NativeArguments args) { | 114 void FUNCTION_NAME(File_ReadByte)(Dart_NativeArguments args) { |
123 File* file = GetFile(args); | 115 File* file = GetFile(args); |
124 ASSERT(file != NULL); | 116 ASSERT(file != NULL); |
125 uint8_t buffer; | 117 uint8_t buffer; |
126 int64_t bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1); | 118 int64_t bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1); |
127 if (bytes_read == 1) { | 119 if (bytes_read == 1) { |
128 Dart_SetReturnValue(args, Dart_NewInteger(buffer)); | 120 Dart_SetReturnValue(args, Dart_NewInteger(buffer)); |
129 } else if (bytes_read == 0) { | 121 } else if (bytes_read == 0) { |
130 Dart_SetReturnValue(args, Dart_NewInteger(-1)); | 122 Dart_SetReturnValue(args, Dart_NewInteger(-1)); |
131 } else { | 123 } else { |
132 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 124 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
133 } | 125 } |
134 } | 126 } |
135 | 127 |
136 | |
137 void FUNCTION_NAME(File_WriteByte)(Dart_NativeArguments args) { | 128 void FUNCTION_NAME(File_WriteByte)(Dart_NativeArguments args) { |
138 File* file = GetFile(args); | 129 File* file = GetFile(args); |
139 ASSERT(file != NULL); | 130 ASSERT(file != NULL); |
140 int64_t byte = 0; | 131 int64_t byte = 0; |
141 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &byte)) { | 132 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &byte)) { |
142 uint8_t buffer = static_cast<uint8_t>(byte & 0xff); | 133 uint8_t buffer = static_cast<uint8_t>(byte & 0xff); |
143 bool success = file->WriteFully(reinterpret_cast<void*>(&buffer), 1); | 134 bool success = file->WriteFully(reinterpret_cast<void*>(&buffer), 1); |
144 if (success) { | 135 if (success) { |
145 Dart_SetReturnValue(args, Dart_NewInteger(1)); | 136 Dart_SetReturnValue(args, Dart_NewInteger(1)); |
146 } else { | 137 } else { |
147 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 138 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
148 } | 139 } |
149 } else { | 140 } else { |
150 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | 141 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
151 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 142 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
152 } | 143 } |
153 } | 144 } |
154 | 145 |
155 | |
156 void FUNCTION_NAME(File_Read)(Dart_NativeArguments args) { | 146 void FUNCTION_NAME(File_Read)(Dart_NativeArguments args) { |
157 File* file = GetFile(args); | 147 File* file = GetFile(args); |
158 ASSERT(file != NULL); | 148 ASSERT(file != NULL); |
159 Dart_Handle length_object = Dart_GetNativeArgument(args, 1); | 149 Dart_Handle length_object = Dart_GetNativeArgument(args, 1); |
160 int64_t length = 0; | 150 int64_t length = 0; |
161 if (DartUtils::GetInt64Value(length_object, &length)) { | 151 if (DartUtils::GetInt64Value(length_object, &length)) { |
162 uint8_t* buffer = NULL; | 152 uint8_t* buffer = NULL; |
163 Dart_Handle external_array = IOBuffer::Allocate(length, &buffer); | 153 Dart_Handle external_array = IOBuffer::Allocate(length, &buffer); |
164 int64_t bytes_read = file->Read(reinterpret_cast<void*>(buffer), length); | 154 int64_t bytes_read = file->Read(reinterpret_cast<void*>(buffer), length); |
165 if (bytes_read < 0) { | 155 if (bytes_read < 0) { |
(...skipping 18 matching lines...) Expand all Loading... |
184 } else { | 174 } else { |
185 Dart_SetReturnValue(args, external_array); | 175 Dart_SetReturnValue(args, external_array); |
186 } | 176 } |
187 } | 177 } |
188 } else { | 178 } else { |
189 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | 179 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
190 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 180 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
191 } | 181 } |
192 } | 182 } |
193 | 183 |
194 | |
195 void FUNCTION_NAME(File_ReadInto)(Dart_NativeArguments args) { | 184 void FUNCTION_NAME(File_ReadInto)(Dart_NativeArguments args) { |
196 File* file = GetFile(args); | 185 File* file = GetFile(args); |
197 ASSERT(file != NULL); | 186 ASSERT(file != NULL); |
198 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 187 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
199 ASSERT(Dart_IsList(buffer_obj)); | 188 ASSERT(Dart_IsList(buffer_obj)); |
200 // start and end arguments are checked in Dart code to be | 189 // start and end arguments are checked in Dart code to be |
201 // integers and have the property that end <= | 190 // integers and have the property that end <= |
202 // list.length. Therefore, it is safe to extract their value as | 191 // list.length. Therefore, it is safe to extract their value as |
203 // intptr_t. | 192 // intptr_t. |
204 intptr_t start = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | 193 intptr_t start = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); |
(...skipping 12 matching lines...) Expand all Loading... |
217 if (Dart_IsError(result)) { | 206 if (Dart_IsError(result)) { |
218 Dart_SetReturnValue(args, result); | 207 Dart_SetReturnValue(args, result); |
219 } else { | 208 } else { |
220 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); | 209 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read)); |
221 } | 210 } |
222 } else { | 211 } else { |
223 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 212 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
224 } | 213 } |
225 } | 214 } |
226 | 215 |
227 | |
228 void FUNCTION_NAME(File_WriteFrom)(Dart_NativeArguments args) { | 216 void FUNCTION_NAME(File_WriteFrom)(Dart_NativeArguments args) { |
229 File* file = GetFile(args); | 217 File* file = GetFile(args); |
230 ASSERT(file != NULL); | 218 ASSERT(file != NULL); |
231 | 219 |
232 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 220 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
233 | 221 |
234 // Offset and length arguments are checked in Dart code to be | 222 // Offset and length arguments are checked in Dart code to be |
235 // integers and have the property that (offset + length) <= | 223 // integers and have the property that (offset + length) <= |
236 // list.length. Therefore, it is safe to extract their value as | 224 // list.length. Therefore, it is safe to extract their value as |
237 // intptr_t. | 225 // intptr_t. |
(...skipping 26 matching lines...) Expand all Loading... |
264 Dart_PropagateError(result); | 252 Dart_PropagateError(result); |
265 } | 253 } |
266 | 254 |
267 if (!success) { | 255 if (!success) { |
268 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 256 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
269 } else { | 257 } else { |
270 Dart_SetReturnValue(args, Dart_Null()); | 258 Dart_SetReturnValue(args, Dart_Null()); |
271 } | 259 } |
272 } | 260 } |
273 | 261 |
274 | |
275 void FUNCTION_NAME(File_Position)(Dart_NativeArguments args) { | 262 void FUNCTION_NAME(File_Position)(Dart_NativeArguments args) { |
276 File* file = GetFile(args); | 263 File* file = GetFile(args); |
277 ASSERT(file != NULL); | 264 ASSERT(file != NULL); |
278 intptr_t return_value = file->Position(); | 265 intptr_t return_value = file->Position(); |
279 if (return_value >= 0) { | 266 if (return_value >= 0) { |
280 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 267 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
281 } else { | 268 } else { |
282 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 269 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
283 } | 270 } |
284 } | 271 } |
285 | 272 |
286 | |
287 void FUNCTION_NAME(File_SetPosition)(Dart_NativeArguments args) { | 273 void FUNCTION_NAME(File_SetPosition)(Dart_NativeArguments args) { |
288 File* file = GetFile(args); | 274 File* file = GetFile(args); |
289 ASSERT(file != NULL); | 275 ASSERT(file != NULL); |
290 int64_t position = 0; | 276 int64_t position = 0; |
291 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &position)) { | 277 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &position)) { |
292 if (file->SetPosition(position)) { | 278 if (file->SetPosition(position)) { |
293 Dart_SetReturnValue(args, Dart_True()); | 279 Dart_SetReturnValue(args, Dart_True()); |
294 } else { | 280 } else { |
295 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 281 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
296 } | 282 } |
297 } else { | 283 } else { |
298 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | 284 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
299 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 285 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
300 } | 286 } |
301 } | 287 } |
302 | 288 |
303 | |
304 void FUNCTION_NAME(File_Truncate)(Dart_NativeArguments args) { | 289 void FUNCTION_NAME(File_Truncate)(Dart_NativeArguments args) { |
305 File* file = GetFile(args); | 290 File* file = GetFile(args); |
306 ASSERT(file != NULL); | 291 ASSERT(file != NULL); |
307 int64_t length = 0; | 292 int64_t length = 0; |
308 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { | 293 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { |
309 if (file->Truncate(length)) { | 294 if (file->Truncate(length)) { |
310 Dart_SetReturnValue(args, Dart_True()); | 295 Dart_SetReturnValue(args, Dart_True()); |
311 } else { | 296 } else { |
312 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 297 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
313 } | 298 } |
314 } else { | 299 } else { |
315 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | 300 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
316 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 301 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
317 } | 302 } |
318 } | 303 } |
319 | 304 |
320 | |
321 void FUNCTION_NAME(File_Length)(Dart_NativeArguments args) { | 305 void FUNCTION_NAME(File_Length)(Dart_NativeArguments args) { |
322 File* file = GetFile(args); | 306 File* file = GetFile(args); |
323 ASSERT(file != NULL); | 307 ASSERT(file != NULL); |
324 int64_t return_value = file->Length(); | 308 int64_t return_value = file->Length(); |
325 if (return_value >= 0) { | 309 if (return_value >= 0) { |
326 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 310 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
327 } else { | 311 } else { |
328 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 312 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
329 } | 313 } |
330 } | 314 } |
331 | 315 |
332 | |
333 void FUNCTION_NAME(File_LengthFromPath)(Dart_NativeArguments args) { | 316 void FUNCTION_NAME(File_LengthFromPath)(Dart_NativeArguments args) { |
334 const char* path = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 317 const char* path = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
335 int64_t return_value = File::LengthFromPath(path); | 318 int64_t return_value = File::LengthFromPath(path); |
336 if (return_value >= 0) { | 319 if (return_value >= 0) { |
337 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 320 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
338 } else { | 321 } else { |
339 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 322 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
340 } | 323 } |
341 } | 324 } |
342 | 325 |
343 | |
344 void FUNCTION_NAME(File_LastModified)(Dart_NativeArguments args) { | 326 void FUNCTION_NAME(File_LastModified)(Dart_NativeArguments args) { |
345 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 327 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
346 int64_t return_value = File::LastModified(name); | 328 int64_t return_value = File::LastModified(name); |
347 if (return_value >= 0) { | 329 if (return_value >= 0) { |
348 Dart_SetReturnValue(args, | 330 Dart_SetReturnValue(args, |
349 Dart_NewInteger(return_value * kMillisecondsPerSecond)); | 331 Dart_NewInteger(return_value * kMillisecondsPerSecond)); |
350 } else { | 332 } else { |
351 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 333 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
352 } | 334 } |
353 } | 335 } |
354 | 336 |
355 | |
356 void FUNCTION_NAME(File_SetLastModified)(Dart_NativeArguments args) { | 337 void FUNCTION_NAME(File_SetLastModified)(Dart_NativeArguments args) { |
357 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 338 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
358 int64_t millis; | 339 int64_t millis; |
359 if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &millis)) { | 340 if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &millis)) { |
360 Dart_ThrowException(DartUtils::NewDartArgumentError( | 341 Dart_ThrowException(DartUtils::NewDartArgumentError( |
361 "The second argument must be a 64-bit int.")); | 342 "The second argument must be a 64-bit int.")); |
362 } | 343 } |
363 if (!File::SetLastModified(name, millis)) { | 344 if (!File::SetLastModified(name, millis)) { |
364 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 345 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
365 } | 346 } |
366 } | 347 } |
367 | 348 |
368 | |
369 void FUNCTION_NAME(File_LastAccessed)(Dart_NativeArguments args) { | 349 void FUNCTION_NAME(File_LastAccessed)(Dart_NativeArguments args) { |
370 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 350 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
371 int64_t return_value = File::LastAccessed(name); | 351 int64_t return_value = File::LastAccessed(name); |
372 if (return_value >= 0) { | 352 if (return_value >= 0) { |
373 Dart_SetReturnValue(args, | 353 Dart_SetReturnValue(args, |
374 Dart_NewInteger(return_value * kMillisecondsPerSecond)); | 354 Dart_NewInteger(return_value * kMillisecondsPerSecond)); |
375 } else { | 355 } else { |
376 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 356 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
377 } | 357 } |
378 } | 358 } |
379 | 359 |
380 | |
381 void FUNCTION_NAME(File_SetLastAccessed)(Dart_NativeArguments args) { | 360 void FUNCTION_NAME(File_SetLastAccessed)(Dart_NativeArguments args) { |
382 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 361 const char* name = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
383 int64_t millis; | 362 int64_t millis; |
384 if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &millis)) { | 363 if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &millis)) { |
385 Dart_ThrowException(DartUtils::NewDartArgumentError( | 364 Dart_ThrowException(DartUtils::NewDartArgumentError( |
386 "The second argument must be a 64-bit int.")); | 365 "The second argument must be a 64-bit int.")); |
387 } | 366 } |
388 if (!File::SetLastAccessed(name, millis)) { | 367 if (!File::SetLastAccessed(name, millis)) { |
389 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 368 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
390 } | 369 } |
391 } | 370 } |
392 | 371 |
393 | |
394 void FUNCTION_NAME(File_Flush)(Dart_NativeArguments args) { | 372 void FUNCTION_NAME(File_Flush)(Dart_NativeArguments args) { |
395 File* file = GetFile(args); | 373 File* file = GetFile(args); |
396 ASSERT(file != NULL); | 374 ASSERT(file != NULL); |
397 if (file->Flush()) { | 375 if (file->Flush()) { |
398 Dart_SetReturnValue(args, Dart_True()); | 376 Dart_SetReturnValue(args, Dart_True()); |
399 } else { | 377 } else { |
400 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 378 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
401 } | 379 } |
402 } | 380 } |
403 | 381 |
404 | |
405 void FUNCTION_NAME(File_Lock)(Dart_NativeArguments args) { | 382 void FUNCTION_NAME(File_Lock)(Dart_NativeArguments args) { |
406 File* file = GetFile(args); | 383 File* file = GetFile(args); |
407 ASSERT(file != NULL); | 384 ASSERT(file != NULL); |
408 int64_t lock; | 385 int64_t lock; |
409 int64_t start; | 386 int64_t start; |
410 int64_t end; | 387 int64_t end; |
411 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &lock) && | 388 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &lock) && |
412 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 2), &start) && | 389 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 2), &start) && |
413 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 3), &end)) { | 390 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 3), &end)) { |
414 if ((lock >= File::kLockMin) && (lock <= File::kLockMax) && (start >= 0) && | 391 if ((lock >= File::kLockMin) && (lock <= File::kLockMax) && (start >= 0) && |
415 (end == -1 || end > start)) { | 392 (end == -1 || end > start)) { |
416 if (file->Lock(static_cast<File::LockType>(lock), start, end)) { | 393 if (file->Lock(static_cast<File::LockType>(lock), start, end)) { |
417 Dart_SetReturnValue(args, Dart_True()); | 394 Dart_SetReturnValue(args, Dart_True()); |
418 } else { | 395 } else { |
419 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 396 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
420 } | 397 } |
421 return; | 398 return; |
422 } | 399 } |
423 } | 400 } |
424 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | 401 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
425 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 402 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
426 } | 403 } |
427 | 404 |
428 | |
429 void FUNCTION_NAME(File_Create)(Dart_NativeArguments args) { | 405 void FUNCTION_NAME(File_Create)(Dart_NativeArguments args) { |
430 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 406 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
431 bool result = File::Create(str); | 407 bool result = File::Create(str); |
432 if (result) { | 408 if (result) { |
433 Dart_SetReturnValue(args, Dart_NewBoolean(result)); | 409 Dart_SetReturnValue(args, Dart_NewBoolean(result)); |
434 } else { | 410 } else { |
435 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 411 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
436 } | 412 } |
437 } | 413 } |
438 | 414 |
439 | |
440 void FUNCTION_NAME(File_CreateLink)(Dart_NativeArguments args) { | 415 void FUNCTION_NAME(File_CreateLink)(Dart_NativeArguments args) { |
441 if (Dart_IsString(Dart_GetNativeArgument(args, 0)) && | 416 if (Dart_IsString(Dart_GetNativeArgument(args, 0)) && |
442 Dart_IsString(Dart_GetNativeArgument(args, 1))) { | 417 Dart_IsString(Dart_GetNativeArgument(args, 1))) { |
443 const char* name = | 418 const char* name = |
444 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 419 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
445 const char* target = | 420 const char* target = |
446 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 421 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
447 if (!File::CreateLink(name, target)) { | 422 if (!File::CreateLink(name, target)) { |
448 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 423 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
449 } | 424 } |
450 } else { | 425 } else { |
451 Dart_Handle err = | 426 Dart_Handle err = |
452 DartUtils::NewDartArgumentError("Non-string argument to Link.create"); | 427 DartUtils::NewDartArgumentError("Non-string argument to Link.create"); |
453 Dart_SetReturnValue(args, err); | 428 Dart_SetReturnValue(args, err); |
454 } | 429 } |
455 } | 430 } |
456 | 431 |
457 | |
458 void FUNCTION_NAME(File_LinkTarget)(Dart_NativeArguments args) { | 432 void FUNCTION_NAME(File_LinkTarget)(Dart_NativeArguments args) { |
459 if (Dart_IsString(Dart_GetNativeArgument(args, 0))) { | 433 if (Dart_IsString(Dart_GetNativeArgument(args, 0))) { |
460 const char* name = | 434 const char* name = |
461 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 435 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
462 const char* target = File::LinkTarget(name); | 436 const char* target = File::LinkTarget(name); |
463 if (target == NULL) { | 437 if (target == NULL) { |
464 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 438 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
465 } else { | 439 } else { |
466 Dart_SetReturnValue(args, DartUtils::NewString(target)); | 440 Dart_SetReturnValue(args, DartUtils::NewString(target)); |
467 } | 441 } |
468 } else { | 442 } else { |
469 Dart_Handle err = | 443 Dart_Handle err = |
470 DartUtils::NewDartArgumentError("Non-string argument to Link.target"); | 444 DartUtils::NewDartArgumentError("Non-string argument to Link.target"); |
471 Dart_SetReturnValue(args, err); | 445 Dart_SetReturnValue(args, err); |
472 } | 446 } |
473 } | 447 } |
474 | 448 |
475 | |
476 void FUNCTION_NAME(File_Delete)(Dart_NativeArguments args) { | 449 void FUNCTION_NAME(File_Delete)(Dart_NativeArguments args) { |
477 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 450 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
478 bool result = File::Delete(str); | 451 bool result = File::Delete(str); |
479 if (result) { | 452 if (result) { |
480 Dart_SetReturnValue(args, Dart_NewBoolean(result)); | 453 Dart_SetReturnValue(args, Dart_NewBoolean(result)); |
481 } else { | 454 } else { |
482 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 455 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
483 } | 456 } |
484 } | 457 } |
485 | 458 |
486 | |
487 void FUNCTION_NAME(File_DeleteLink)(Dart_NativeArguments args) { | 459 void FUNCTION_NAME(File_DeleteLink)(Dart_NativeArguments args) { |
488 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 460 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
489 bool result = File::DeleteLink(str); | 461 bool result = File::DeleteLink(str); |
490 if (result) { | 462 if (result) { |
491 Dart_SetReturnValue(args, Dart_NewBoolean(result)); | 463 Dart_SetReturnValue(args, Dart_NewBoolean(result)); |
492 } else { | 464 } else { |
493 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 465 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
494 } | 466 } |
495 } | 467 } |
496 | 468 |
497 | |
498 void FUNCTION_NAME(File_Rename)(Dart_NativeArguments args) { | 469 void FUNCTION_NAME(File_Rename)(Dart_NativeArguments args) { |
499 const char* old_path = | 470 const char* old_path = |
500 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 471 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
501 const char* new_path = | 472 const char* new_path = |
502 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 473 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
503 bool result = File::Rename(old_path, new_path); | 474 bool result = File::Rename(old_path, new_path); |
504 if (result) { | 475 if (result) { |
505 Dart_SetReturnValue(args, Dart_NewBoolean(result)); | 476 Dart_SetReturnValue(args, Dart_NewBoolean(result)); |
506 } else { | 477 } else { |
507 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 478 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
508 } | 479 } |
509 } | 480 } |
510 | 481 |
511 | |
512 void FUNCTION_NAME(File_RenameLink)(Dart_NativeArguments args) { | 482 void FUNCTION_NAME(File_RenameLink)(Dart_NativeArguments args) { |
513 const char* old_path = | 483 const char* old_path = |
514 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 484 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
515 const char* new_path = | 485 const char* new_path = |
516 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 486 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
517 bool result = File::RenameLink(old_path, new_path); | 487 bool result = File::RenameLink(old_path, new_path); |
518 if (result) { | 488 if (result) { |
519 Dart_SetReturnValue(args, Dart_NewBoolean(result)); | 489 Dart_SetReturnValue(args, Dart_NewBoolean(result)); |
520 } else { | 490 } else { |
521 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 491 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
522 } | 492 } |
523 } | 493 } |
524 | 494 |
525 | |
526 void FUNCTION_NAME(File_Copy)(Dart_NativeArguments args) { | 495 void FUNCTION_NAME(File_Copy)(Dart_NativeArguments args) { |
527 const char* old_path = | 496 const char* old_path = |
528 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 497 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
529 const char* new_path = | 498 const char* new_path = |
530 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 499 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
531 bool result = File::Copy(old_path, new_path); | 500 bool result = File::Copy(old_path, new_path); |
532 if (result) { | 501 if (result) { |
533 Dart_SetReturnValue(args, Dart_NewBoolean(result)); | 502 Dart_SetReturnValue(args, Dart_NewBoolean(result)); |
534 } else { | 503 } else { |
535 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 504 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
536 } | 505 } |
537 } | 506 } |
538 | 507 |
539 | |
540 void FUNCTION_NAME(File_ResolveSymbolicLinks)(Dart_NativeArguments args) { | 508 void FUNCTION_NAME(File_ResolveSymbolicLinks)(Dart_NativeArguments args) { |
541 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 509 const char* str = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
542 const char* path = File::GetCanonicalPath(str); | 510 const char* path = File::GetCanonicalPath(str); |
543 if (path != NULL) { | 511 if (path != NULL) { |
544 Dart_SetReturnValue(args, DartUtils::NewString(path)); | 512 Dart_SetReturnValue(args, DartUtils::NewString(path)); |
545 } else { | 513 } else { |
546 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 514 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
547 } | 515 } |
548 } | 516 } |
549 | 517 |
550 | |
551 void FUNCTION_NAME(File_OpenStdio)(Dart_NativeArguments args) { | 518 void FUNCTION_NAME(File_OpenStdio)(Dart_NativeArguments args) { |
552 int64_t fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); | 519 int64_t fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
553 ASSERT((fd == STDIN_FILENO) || (fd == STDOUT_FILENO) || | 520 ASSERT((fd == STDIN_FILENO) || (fd == STDOUT_FILENO) || |
554 (fd == STDERR_FILENO)); | 521 (fd == STDERR_FILENO)); |
555 File* file = File::OpenStdio(static_cast<int>(fd)); | 522 File* file = File::OpenStdio(static_cast<int>(fd)); |
556 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file))); | 523 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file))); |
557 } | 524 } |
558 | 525 |
559 | |
560 void FUNCTION_NAME(File_GetStdioHandleType)(Dart_NativeArguments args) { | 526 void FUNCTION_NAME(File_GetStdioHandleType)(Dart_NativeArguments args) { |
561 int64_t fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); | 527 int64_t fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
562 ASSERT((fd == STDIN_FILENO) || (fd == STDOUT_FILENO) || | 528 ASSERT((fd == STDIN_FILENO) || (fd == STDOUT_FILENO) || |
563 (fd == STDERR_FILENO)); | 529 (fd == STDERR_FILENO)); |
564 File::StdioHandleType type = File::GetStdioHandleType(static_cast<int>(fd)); | 530 File::StdioHandleType type = File::GetStdioHandleType(static_cast<int>(fd)); |
565 Dart_SetReturnValue(args, Dart_NewInteger(type)); | 531 Dart_SetReturnValue(args, Dart_NewInteger(type)); |
566 } | 532 } |
567 | 533 |
568 | |
569 void FUNCTION_NAME(File_GetType)(Dart_NativeArguments args) { | 534 void FUNCTION_NAME(File_GetType)(Dart_NativeArguments args) { |
570 if (Dart_IsString(Dart_GetNativeArgument(args, 0)) && | 535 if (Dart_IsString(Dart_GetNativeArgument(args, 0)) && |
571 Dart_IsBoolean(Dart_GetNativeArgument(args, 1))) { | 536 Dart_IsBoolean(Dart_GetNativeArgument(args, 1))) { |
572 const char* str = | 537 const char* str = |
573 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 538 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
574 bool follow_links = | 539 bool follow_links = |
575 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 1)); | 540 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 1)); |
576 File::Type type = File::GetType(str, follow_links); | 541 File::Type type = File::GetType(str, follow_links); |
577 Dart_SetReturnValue(args, Dart_NewInteger(static_cast<int>(type))); | 542 Dart_SetReturnValue(args, Dart_NewInteger(static_cast<int>(type))); |
578 } else { | 543 } else { |
579 Dart_Handle err = DartUtils::NewDartArgumentError( | 544 Dart_Handle err = DartUtils::NewDartArgumentError( |
580 "Non-string argument to FileSystemEntity.type"); | 545 "Non-string argument to FileSystemEntity.type"); |
581 Dart_SetReturnValue(args, err); | 546 Dart_SetReturnValue(args, err); |
582 } | 547 } |
583 } | 548 } |
584 | 549 |
585 | |
586 void FUNCTION_NAME(File_Stat)(Dart_NativeArguments args) { | 550 void FUNCTION_NAME(File_Stat)(Dart_NativeArguments args) { |
587 if (Dart_IsString(Dart_GetNativeArgument(args, 0))) { | 551 if (Dart_IsString(Dart_GetNativeArgument(args, 0))) { |
588 const char* path = | 552 const char* path = |
589 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 553 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
590 | 554 |
591 int64_t stat_data[File::kStatSize]; | 555 int64_t stat_data[File::kStatSize]; |
592 File::Stat(path, stat_data); | 556 File::Stat(path, stat_data); |
593 if (stat_data[File::kType] == File::kDoesNotExist) { | 557 if (stat_data[File::kType] == File::kDoesNotExist) { |
594 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 558 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
595 } else { | 559 } else { |
(...skipping 18 matching lines...) Expand all Loading... |
614 } | 578 } |
615 Dart_SetReturnValue(args, returned_data); | 579 Dart_SetReturnValue(args, returned_data); |
616 } | 580 } |
617 } else { | 581 } else { |
618 Dart_Handle err = DartUtils::NewDartArgumentError( | 582 Dart_Handle err = DartUtils::NewDartArgumentError( |
619 "Non-string argument to FileSystemEntity.stat"); | 583 "Non-string argument to FileSystemEntity.stat"); |
620 Dart_SetReturnValue(args, err); | 584 Dart_SetReturnValue(args, err); |
621 } | 585 } |
622 } | 586 } |
623 | 587 |
624 | |
625 void FUNCTION_NAME(File_AreIdentical)(Dart_NativeArguments args) { | 588 void FUNCTION_NAME(File_AreIdentical)(Dart_NativeArguments args) { |
626 if (Dart_IsString(Dart_GetNativeArgument(args, 0)) && | 589 if (Dart_IsString(Dart_GetNativeArgument(args, 0)) && |
627 Dart_IsString(Dart_GetNativeArgument(args, 1))) { | 590 Dart_IsString(Dart_GetNativeArgument(args, 1))) { |
628 const char* path_1 = | 591 const char* path_1 = |
629 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 592 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
630 const char* path_2 = | 593 const char* path_2 = |
631 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 594 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
632 File::Identical result = File::AreIdentical(path_1, path_2); | 595 File::Identical result = File::AreIdentical(path_1, path_2); |
633 if (result == File::kError) { | 596 if (result == File::kError) { |
634 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 597 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
635 } else { | 598 } else { |
636 Dart_SetReturnValue(args, Dart_NewBoolean(result == File::kIdentical)); | 599 Dart_SetReturnValue(args, Dart_NewBoolean(result == File::kIdentical)); |
637 } | 600 } |
638 } else { | 601 } else { |
639 Dart_Handle err = DartUtils::NewDartArgumentError( | 602 Dart_Handle err = DartUtils::NewDartArgumentError( |
640 "Non-string argument to FileSystemEntity.identical"); | 603 "Non-string argument to FileSystemEntity.identical"); |
641 Dart_SetReturnValue(args, err); | 604 Dart_SetReturnValue(args, err); |
642 } | 605 } |
643 } | 606 } |
644 | 607 |
645 | |
646 static int64_t CObjectInt32OrInt64ToInt64(CObject* cobject) { | 608 static int64_t CObjectInt32OrInt64ToInt64(CObject* cobject) { |
647 ASSERT(cobject->IsInt32OrInt64()); | 609 ASSERT(cobject->IsInt32OrInt64()); |
648 int64_t result; | 610 int64_t result; |
649 if (cobject->IsInt32()) { | 611 if (cobject->IsInt32()) { |
650 CObjectInt32 value(cobject); | 612 CObjectInt32 value(cobject); |
651 result = value.Value(); | 613 result = value.Value(); |
652 } else { | 614 } else { |
653 CObjectInt64 value(cobject); | 615 CObjectInt64 value(cobject); |
654 result = value.Value(); | 616 result = value.Value(); |
655 } | 617 } |
656 return result; | 618 return result; |
657 } | 619 } |
658 | 620 |
659 | |
660 File* CObjectToFilePointer(CObject* cobject) { | 621 File* CObjectToFilePointer(CObject* cobject) { |
661 CObjectIntptr value(cobject); | 622 CObjectIntptr value(cobject); |
662 return reinterpret_cast<File*>(value.Value()); | 623 return reinterpret_cast<File*>(value.Value()); |
663 } | 624 } |
664 | 625 |
665 | |
666 CObject* File::ExistsRequest(const CObjectArray& request) { | 626 CObject* File::ExistsRequest(const CObjectArray& request) { |
667 if ((request.Length() == 1) && request[0]->IsString()) { | 627 if ((request.Length() == 1) && request[0]->IsString()) { |
668 CObjectString filename(request[0]); | 628 CObjectString filename(request[0]); |
669 bool result = File::Exists(filename.CString()); | 629 bool result = File::Exists(filename.CString()); |
670 return CObject::Bool(result); | 630 return CObject::Bool(result); |
671 } | 631 } |
672 return CObject::IllegalArgumentError(); | 632 return CObject::IllegalArgumentError(); |
673 } | 633 } |
674 | 634 |
675 | |
676 CObject* File::CreateRequest(const CObjectArray& request) { | 635 CObject* File::CreateRequest(const CObjectArray& request) { |
677 if ((request.Length() == 1) && request[0]->IsString()) { | 636 if ((request.Length() == 1) && request[0]->IsString()) { |
678 CObjectString filename(request[0]); | 637 CObjectString filename(request[0]); |
679 bool result = File::Create(filename.CString()); | 638 bool result = File::Create(filename.CString()); |
680 if (result) { | 639 if (result) { |
681 return CObject::True(); | 640 return CObject::True(); |
682 } else { | 641 } else { |
683 return CObject::NewOSError(); | 642 return CObject::NewOSError(); |
684 } | 643 } |
685 } | 644 } |
686 return CObject::IllegalArgumentError(); | 645 return CObject::IllegalArgumentError(); |
687 } | 646 } |
688 | 647 |
689 | |
690 CObject* File::OpenRequest(const CObjectArray& request) { | 648 CObject* File::OpenRequest(const CObjectArray& request) { |
691 File* file = NULL; | 649 File* file = NULL; |
692 if ((request.Length() == 2) && request[0]->IsString() && | 650 if ((request.Length() == 2) && request[0]->IsString() && |
693 request[1]->IsInt32()) { | 651 request[1]->IsInt32()) { |
694 CObjectString filename(request[0]); | 652 CObjectString filename(request[0]); |
695 CObjectInt32 mode(request[1]); | 653 CObjectInt32 mode(request[1]); |
696 File::DartFileOpenMode dart_file_mode = | 654 File::DartFileOpenMode dart_file_mode = |
697 static_cast<File::DartFileOpenMode>(mode.Value()); | 655 static_cast<File::DartFileOpenMode>(mode.Value()); |
698 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); | 656 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); |
699 file = File::Open(filename.CString(), file_mode); | 657 file = File::Open(filename.CString(), file_mode); |
700 if (file != NULL) { | 658 if (file != NULL) { |
701 return new CObjectIntptr( | 659 return new CObjectIntptr( |
702 CObject::NewIntptr(reinterpret_cast<intptr_t>(file))); | 660 CObject::NewIntptr(reinterpret_cast<intptr_t>(file))); |
703 } else { | 661 } else { |
704 return CObject::NewOSError(); | 662 return CObject::NewOSError(); |
705 } | 663 } |
706 } | 664 } |
707 return CObject::IllegalArgumentError(); | 665 return CObject::IllegalArgumentError(); |
708 } | 666 } |
709 | 667 |
710 | |
711 CObject* File::DeleteRequest(const CObjectArray& request) { | 668 CObject* File::DeleteRequest(const CObjectArray& request) { |
712 if ((request.Length() == 1) && request[0]->IsString()) { | 669 if ((request.Length() == 1) && request[0]->IsString()) { |
713 CObjectString filename(request[0]); | 670 CObjectString filename(request[0]); |
714 bool result = File::Delete(filename.CString()); | 671 bool result = File::Delete(filename.CString()); |
715 if (result) { | 672 if (result) { |
716 return CObject::True(); | 673 return CObject::True(); |
717 } else { | 674 } else { |
718 return CObject::NewOSError(); | 675 return CObject::NewOSError(); |
719 } | 676 } |
720 } | 677 } |
721 return CObject::False(); | 678 return CObject::False(); |
722 } | 679 } |
723 | 680 |
724 | |
725 CObject* File::RenameRequest(const CObjectArray& request) { | 681 CObject* File::RenameRequest(const CObjectArray& request) { |
726 if ((request.Length() == 2) && request[0]->IsString() && | 682 if ((request.Length() == 2) && request[0]->IsString() && |
727 request[1]->IsString()) { | 683 request[1]->IsString()) { |
728 CObjectString old_path(request[0]); | 684 CObjectString old_path(request[0]); |
729 CObjectString new_path(request[1]); | 685 CObjectString new_path(request[1]); |
730 bool completed = File::Rename(old_path.CString(), new_path.CString()); | 686 bool completed = File::Rename(old_path.CString(), new_path.CString()); |
731 if (completed) { | 687 if (completed) { |
732 return CObject::True(); | 688 return CObject::True(); |
733 } | 689 } |
734 return CObject::NewOSError(); | 690 return CObject::NewOSError(); |
735 } | 691 } |
736 return CObject::IllegalArgumentError(); | 692 return CObject::IllegalArgumentError(); |
737 } | 693 } |
738 | 694 |
739 | |
740 CObject* File::CopyRequest(const CObjectArray& request) { | 695 CObject* File::CopyRequest(const CObjectArray& request) { |
741 if ((request.Length() == 2) && request[0]->IsString() && | 696 if ((request.Length() == 2) && request[0]->IsString() && |
742 request[1]->IsString()) { | 697 request[1]->IsString()) { |
743 CObjectString old_path(request[0]); | 698 CObjectString old_path(request[0]); |
744 CObjectString new_path(request[1]); | 699 CObjectString new_path(request[1]); |
745 bool completed = File::Copy(old_path.CString(), new_path.CString()); | 700 bool completed = File::Copy(old_path.CString(), new_path.CString()); |
746 if (completed) { | 701 if (completed) { |
747 return CObject::True(); | 702 return CObject::True(); |
748 } | 703 } |
749 return CObject::NewOSError(); | 704 return CObject::NewOSError(); |
750 } | 705 } |
751 return CObject::IllegalArgumentError(); | 706 return CObject::IllegalArgumentError(); |
752 } | 707 } |
753 | 708 |
754 | |
755 CObject* File::ResolveSymbolicLinksRequest(const CObjectArray& request) { | 709 CObject* File::ResolveSymbolicLinksRequest(const CObjectArray& request) { |
756 if ((request.Length() == 1) && request[0]->IsString()) { | 710 if ((request.Length() == 1) && request[0]->IsString()) { |
757 CObjectString filename(request[0]); | 711 CObjectString filename(request[0]); |
758 const char* result = File::GetCanonicalPath(filename.CString()); | 712 const char* result = File::GetCanonicalPath(filename.CString()); |
759 if (result != NULL) { | 713 if (result != NULL) { |
760 CObject* path = new CObjectString(CObject::NewString(result)); | 714 CObject* path = new CObjectString(CObject::NewString(result)); |
761 return path; | 715 return path; |
762 } else { | 716 } else { |
763 return CObject::NewOSError(); | 717 return CObject::NewOSError(); |
764 } | 718 } |
765 } | 719 } |
766 return CObject::IllegalArgumentError(); | 720 return CObject::IllegalArgumentError(); |
767 } | 721 } |
768 | 722 |
769 | |
770 CObject* File::CloseRequest(const CObjectArray& request) { | 723 CObject* File::CloseRequest(const CObjectArray& request) { |
771 intptr_t return_value = -1; | 724 intptr_t return_value = -1; |
772 if ((request.Length() == 1) && request[0]->IsIntptr()) { | 725 if ((request.Length() == 1) && request[0]->IsIntptr()) { |
773 File* file = CObjectToFilePointer(request[0]); | 726 File* file = CObjectToFilePointer(request[0]); |
774 RefCntReleaseScope<File> rs(file); | 727 RefCntReleaseScope<File> rs(file); |
775 return_value = 0; | 728 return_value = 0; |
776 // We have retained a reference to the file here. Therefore the file's | 729 // We have retained a reference to the file here. Therefore the file's |
777 // destructor can't be running. Since no further requests are dispatched by | 730 // destructor can't be running. Since no further requests are dispatched by |
778 // the Dart code after an async close call, this Close() can't be racing | 731 // the Dart code after an async close call, this Close() can't be racing |
779 // with any other call on the file. We don't do an extra Release(), and we | 732 // with any other call on the file. We don't do an extra Release(), and we |
780 // don't delete the weak persistent handle. The file is closed here, but the | 733 // don't delete the weak persistent handle. The file is closed here, but the |
781 // memory will be cleaned up when the finalizer runs. | 734 // memory will be cleaned up when the finalizer runs. |
782 ASSERT(!file->IsClosed()); | 735 ASSERT(!file->IsClosed()); |
783 file->Close(); | 736 file->Close(); |
784 } | 737 } |
785 return new CObjectIntptr(CObject::NewIntptr(return_value)); | 738 return new CObjectIntptr(CObject::NewIntptr(return_value)); |
786 } | 739 } |
787 | 740 |
788 | |
789 CObject* File::PositionRequest(const CObjectArray& request) { | 741 CObject* File::PositionRequest(const CObjectArray& request) { |
790 if ((request.Length() == 1) && request[0]->IsIntptr()) { | 742 if ((request.Length() == 1) && request[0]->IsIntptr()) { |
791 File* file = CObjectToFilePointer(request[0]); | 743 File* file = CObjectToFilePointer(request[0]); |
792 RefCntReleaseScope<File> rs(file); | 744 RefCntReleaseScope<File> rs(file); |
793 if (!file->IsClosed()) { | 745 if (!file->IsClosed()) { |
794 intptr_t return_value = file->Position(); | 746 intptr_t return_value = file->Position(); |
795 if (return_value >= 0) { | 747 if (return_value >= 0) { |
796 return new CObjectIntptr(CObject::NewIntptr(return_value)); | 748 return new CObjectIntptr(CObject::NewIntptr(return_value)); |
797 } else { | 749 } else { |
798 return CObject::NewOSError(); | 750 return CObject::NewOSError(); |
799 } | 751 } |
800 } else { | 752 } else { |
801 return CObject::FileClosedError(); | 753 return CObject::FileClosedError(); |
802 } | 754 } |
803 } | 755 } |
804 return CObject::IllegalArgumentError(); | 756 return CObject::IllegalArgumentError(); |
805 } | 757 } |
806 | 758 |
807 | |
808 CObject* File::SetPositionRequest(const CObjectArray& request) { | 759 CObject* File::SetPositionRequest(const CObjectArray& request) { |
809 if ((request.Length() >= 1) && request[0]->IsIntptr()) { | 760 if ((request.Length() >= 1) && request[0]->IsIntptr()) { |
810 File* file = CObjectToFilePointer(request[0]); | 761 File* file = CObjectToFilePointer(request[0]); |
811 RefCntReleaseScope<File> rs(file); | 762 RefCntReleaseScope<File> rs(file); |
812 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { | 763 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { |
813 if (!file->IsClosed()) { | 764 if (!file->IsClosed()) { |
814 int64_t position = CObjectInt32OrInt64ToInt64(request[1]); | 765 int64_t position = CObjectInt32OrInt64ToInt64(request[1]); |
815 if (file->SetPosition(position)) { | 766 if (file->SetPosition(position)) { |
816 return CObject::True(); | 767 return CObject::True(); |
817 } else { | 768 } else { |
818 return CObject::NewOSError(); | 769 return CObject::NewOSError(); |
819 } | 770 } |
820 } else { | 771 } else { |
821 return CObject::FileClosedError(); | 772 return CObject::FileClosedError(); |
822 } | 773 } |
823 } else { | 774 } else { |
824 return CObject::IllegalArgumentError(); | 775 return CObject::IllegalArgumentError(); |
825 } | 776 } |
826 } | 777 } |
827 return CObject::IllegalArgumentError(); | 778 return CObject::IllegalArgumentError(); |
828 } | 779 } |
829 | 780 |
830 | |
831 CObject* File::TruncateRequest(const CObjectArray& request) { | 781 CObject* File::TruncateRequest(const CObjectArray& request) { |
832 if ((request.Length() >= 1) && request[0]->IsIntptr()) { | 782 if ((request.Length() >= 1) && request[0]->IsIntptr()) { |
833 File* file = CObjectToFilePointer(request[0]); | 783 File* file = CObjectToFilePointer(request[0]); |
834 RefCntReleaseScope<File> rs(file); | 784 RefCntReleaseScope<File> rs(file); |
835 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { | 785 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { |
836 if (!file->IsClosed()) { | 786 if (!file->IsClosed()) { |
837 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); | 787 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); |
838 if (file->Truncate(length)) { | 788 if (file->Truncate(length)) { |
839 return CObject::True(); | 789 return CObject::True(); |
840 } else { | 790 } else { |
841 return CObject::NewOSError(); | 791 return CObject::NewOSError(); |
842 } | 792 } |
843 } else { | 793 } else { |
844 return CObject::FileClosedError(); | 794 return CObject::FileClosedError(); |
845 } | 795 } |
846 } else { | 796 } else { |
847 return CObject::IllegalArgumentError(); | 797 return CObject::IllegalArgumentError(); |
848 } | 798 } |
849 } | 799 } |
850 return CObject::IllegalArgumentError(); | 800 return CObject::IllegalArgumentError(); |
851 } | 801 } |
852 | 802 |
853 | |
854 CObject* File::LengthRequest(const CObjectArray& request) { | 803 CObject* File::LengthRequest(const CObjectArray& request) { |
855 if ((request.Length() == 1) && request[0]->IsIntptr()) { | 804 if ((request.Length() == 1) && request[0]->IsIntptr()) { |
856 File* file = CObjectToFilePointer(request[0]); | 805 File* file = CObjectToFilePointer(request[0]); |
857 RefCntReleaseScope<File> rs(file); | 806 RefCntReleaseScope<File> rs(file); |
858 if (!file->IsClosed()) { | 807 if (!file->IsClosed()) { |
859 int64_t return_value = file->Length(); | 808 int64_t return_value = file->Length(); |
860 if (return_value >= 0) { | 809 if (return_value >= 0) { |
861 return new CObjectInt64(CObject::NewInt64(return_value)); | 810 return new CObjectInt64(CObject::NewInt64(return_value)); |
862 } else { | 811 } else { |
863 return CObject::NewOSError(); | 812 return CObject::NewOSError(); |
864 } | 813 } |
865 } else { | 814 } else { |
866 return CObject::FileClosedError(); | 815 return CObject::FileClosedError(); |
867 } | 816 } |
868 } | 817 } |
869 return CObject::IllegalArgumentError(); | 818 return CObject::IllegalArgumentError(); |
870 } | 819 } |
871 | 820 |
872 | |
873 CObject* File::LengthFromPathRequest(const CObjectArray& request) { | 821 CObject* File::LengthFromPathRequest(const CObjectArray& request) { |
874 if ((request.Length() == 1) && request[0]->IsString()) { | 822 if ((request.Length() == 1) && request[0]->IsString()) { |
875 CObjectString filepath(request[0]); | 823 CObjectString filepath(request[0]); |
876 int64_t return_value = File::LengthFromPath(filepath.CString()); | 824 int64_t return_value = File::LengthFromPath(filepath.CString()); |
877 if (return_value >= 0) { | 825 if (return_value >= 0) { |
878 return new CObjectInt64(CObject::NewInt64(return_value)); | 826 return new CObjectInt64(CObject::NewInt64(return_value)); |
879 } else { | 827 } else { |
880 return CObject::NewOSError(); | 828 return CObject::NewOSError(); |
881 } | 829 } |
882 } | 830 } |
883 return CObject::IllegalArgumentError(); | 831 return CObject::IllegalArgumentError(); |
884 } | 832 } |
885 | 833 |
886 | |
887 CObject* File::LastAccessedRequest(const CObjectArray& request) { | 834 CObject* File::LastAccessedRequest(const CObjectArray& request) { |
888 if ((request.Length() == 1) && request[0]->IsString()) { | 835 if ((request.Length() == 1) && request[0]->IsString()) { |
889 CObjectString filepath(request[0]); | 836 CObjectString filepath(request[0]); |
890 int64_t return_value = File::LastAccessed(filepath.CString()); | 837 int64_t return_value = File::LastAccessed(filepath.CString()); |
891 if (return_value >= 0) { | 838 if (return_value >= 0) { |
892 return new CObjectIntptr( | 839 return new CObjectIntptr( |
893 CObject::NewInt64(return_value * kMillisecondsPerSecond)); | 840 CObject::NewInt64(return_value * kMillisecondsPerSecond)); |
894 } else { | 841 } else { |
895 return CObject::NewOSError(); | 842 return CObject::NewOSError(); |
896 } | 843 } |
897 } | 844 } |
898 return CObject::IllegalArgumentError(); | 845 return CObject::IllegalArgumentError(); |
899 } | 846 } |
900 | 847 |
901 | |
902 CObject* File::SetLastAccessedRequest(const CObjectArray& request) { | 848 CObject* File::SetLastAccessedRequest(const CObjectArray& request) { |
903 if ((request.Length() == 2) && request[0]->IsString() && | 849 if ((request.Length() == 2) && request[0]->IsString() && |
904 request[1]->IsInt32OrInt64()) { | 850 request[1]->IsInt32OrInt64()) { |
905 CObjectString filepath(request[0]); | 851 CObjectString filepath(request[0]); |
906 const int64_t millis = CObjectInt32OrInt64ToInt64(request[1]); | 852 const int64_t millis = CObjectInt32OrInt64ToInt64(request[1]); |
907 if (File::SetLastAccessed(filepath.CString(), millis)) { | 853 if (File::SetLastAccessed(filepath.CString(), millis)) { |
908 return CObject::Null(); | 854 return CObject::Null(); |
909 } else { | 855 } else { |
910 return CObject::NewOSError(); | 856 return CObject::NewOSError(); |
911 } | 857 } |
912 } | 858 } |
913 return CObject::IllegalArgumentError(); | 859 return CObject::IllegalArgumentError(); |
914 } | 860 } |
915 | 861 |
916 | |
917 CObject* File::LastModifiedRequest(const CObjectArray& request) { | 862 CObject* File::LastModifiedRequest(const CObjectArray& request) { |
918 if ((request.Length() == 1) && request[0]->IsString()) { | 863 if ((request.Length() == 1) && request[0]->IsString()) { |
919 CObjectString filepath(request[0]); | 864 CObjectString filepath(request[0]); |
920 int64_t return_value = File::LastModified(filepath.CString()); | 865 int64_t return_value = File::LastModified(filepath.CString()); |
921 if (return_value >= 0) { | 866 if (return_value >= 0) { |
922 return new CObjectIntptr( | 867 return new CObjectIntptr( |
923 CObject::NewInt64(return_value * kMillisecondsPerSecond)); | 868 CObject::NewInt64(return_value * kMillisecondsPerSecond)); |
924 } else { | 869 } else { |
925 return CObject::NewOSError(); | 870 return CObject::NewOSError(); |
926 } | 871 } |
927 } | 872 } |
928 return CObject::IllegalArgumentError(); | 873 return CObject::IllegalArgumentError(); |
929 } | 874 } |
930 | 875 |
931 | |
932 CObject* File::SetLastModifiedRequest(const CObjectArray& request) { | 876 CObject* File::SetLastModifiedRequest(const CObjectArray& request) { |
933 if ((request.Length() == 2) && request[0]->IsString() && | 877 if ((request.Length() == 2) && request[0]->IsString() && |
934 request[1]->IsInt32OrInt64()) { | 878 request[1]->IsInt32OrInt64()) { |
935 CObjectString filepath(request[0]); | 879 CObjectString filepath(request[0]); |
936 const int64_t millis = CObjectInt32OrInt64ToInt64(request[1]); | 880 const int64_t millis = CObjectInt32OrInt64ToInt64(request[1]); |
937 if (File::SetLastModified(filepath.CString(), millis)) { | 881 if (File::SetLastModified(filepath.CString(), millis)) { |
938 return CObject::Null(); | 882 return CObject::Null(); |
939 } else { | 883 } else { |
940 return CObject::NewOSError(); | 884 return CObject::NewOSError(); |
941 } | 885 } |
942 } | 886 } |
943 return CObject::IllegalArgumentError(); | 887 return CObject::IllegalArgumentError(); |
944 } | 888 } |
945 | 889 |
946 | |
947 CObject* File::FlushRequest(const CObjectArray& request) { | 890 CObject* File::FlushRequest(const CObjectArray& request) { |
948 if ((request.Length() == 1) && request[0]->IsIntptr()) { | 891 if ((request.Length() == 1) && request[0]->IsIntptr()) { |
949 File* file = CObjectToFilePointer(request[0]); | 892 File* file = CObjectToFilePointer(request[0]); |
950 RefCntReleaseScope<File> rs(file); | 893 RefCntReleaseScope<File> rs(file); |
951 if (!file->IsClosed()) { | 894 if (!file->IsClosed()) { |
952 if (file->Flush()) { | 895 if (file->Flush()) { |
953 return CObject::True(); | 896 return CObject::True(); |
954 } else { | 897 } else { |
955 return CObject::NewOSError(); | 898 return CObject::NewOSError(); |
956 } | 899 } |
957 } else { | 900 } else { |
958 return CObject::FileClosedError(); | 901 return CObject::FileClosedError(); |
959 } | 902 } |
960 } | 903 } |
961 return CObject::IllegalArgumentError(); | 904 return CObject::IllegalArgumentError(); |
962 } | 905 } |
963 | 906 |
964 | |
965 CObject* File::ReadByteRequest(const CObjectArray& request) { | 907 CObject* File::ReadByteRequest(const CObjectArray& request) { |
966 if ((request.Length() == 1) && request[0]->IsIntptr()) { | 908 if ((request.Length() == 1) && request[0]->IsIntptr()) { |
967 File* file = CObjectToFilePointer(request[0]); | 909 File* file = CObjectToFilePointer(request[0]); |
968 RefCntReleaseScope<File> rs(file); | 910 RefCntReleaseScope<File> rs(file); |
969 if (!file->IsClosed()) { | 911 if (!file->IsClosed()) { |
970 uint8_t buffer; | 912 uint8_t buffer; |
971 int64_t bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1); | 913 int64_t bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1); |
972 if (bytes_read > 0) { | 914 if (bytes_read > 0) { |
973 return new CObjectIntptr(CObject::NewIntptr(buffer)); | 915 return new CObjectIntptr(CObject::NewIntptr(buffer)); |
974 } else if (bytes_read == 0) { | 916 } else if (bytes_read == 0) { |
975 return new CObjectIntptr(CObject::NewIntptr(-1)); | 917 return new CObjectIntptr(CObject::NewIntptr(-1)); |
976 } else { | 918 } else { |
977 return CObject::NewOSError(); | 919 return CObject::NewOSError(); |
978 } | 920 } |
979 } else { | 921 } else { |
980 return CObject::FileClosedError(); | 922 return CObject::FileClosedError(); |
981 } | 923 } |
982 } | 924 } |
983 return CObject::IllegalArgumentError(); | 925 return CObject::IllegalArgumentError(); |
984 } | 926 } |
985 | 927 |
986 | |
987 CObject* File::WriteByteRequest(const CObjectArray& request) { | 928 CObject* File::WriteByteRequest(const CObjectArray& request) { |
988 if ((request.Length() >= 1) && request[0]->IsIntptr()) { | 929 if ((request.Length() >= 1) && request[0]->IsIntptr()) { |
989 File* file = CObjectToFilePointer(request[0]); | 930 File* file = CObjectToFilePointer(request[0]); |
990 RefCntReleaseScope<File> rs(file); | 931 RefCntReleaseScope<File> rs(file); |
991 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { | 932 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { |
992 if (!file->IsClosed()) { | 933 if (!file->IsClosed()) { |
993 int64_t byte = CObjectInt32OrInt64ToInt64(request[1]); | 934 int64_t byte = CObjectInt32OrInt64ToInt64(request[1]); |
994 uint8_t buffer = static_cast<uint8_t>(byte & 0xff); | 935 uint8_t buffer = static_cast<uint8_t>(byte & 0xff); |
995 bool success = file->WriteFully(reinterpret_cast<void*>(&buffer), 1); | 936 bool success = file->WriteFully(reinterpret_cast<void*>(&buffer), 1); |
996 if (success) { | 937 if (success) { |
997 return new CObjectInt64(CObject::NewInt64(1)); | 938 return new CObjectInt64(CObject::NewInt64(1)); |
998 } else { | 939 } else { |
999 return CObject::NewOSError(); | 940 return CObject::NewOSError(); |
1000 } | 941 } |
1001 } else { | 942 } else { |
1002 return CObject::FileClosedError(); | 943 return CObject::FileClosedError(); |
1003 } | 944 } |
1004 } else { | 945 } else { |
1005 return CObject::IllegalArgumentError(); | 946 return CObject::IllegalArgumentError(); |
1006 } | 947 } |
1007 } | 948 } |
1008 return CObject::IllegalArgumentError(); | 949 return CObject::IllegalArgumentError(); |
1009 } | 950 } |
1010 | 951 |
1011 | |
1012 CObject* File::ReadRequest(const CObjectArray& request) { | 952 CObject* File::ReadRequest(const CObjectArray& request) { |
1013 if ((request.Length() >= 1) && request[0]->IsIntptr()) { | 953 if ((request.Length() >= 1) && request[0]->IsIntptr()) { |
1014 File* file = CObjectToFilePointer(request[0]); | 954 File* file = CObjectToFilePointer(request[0]); |
1015 RefCntReleaseScope<File> rs(file); | 955 RefCntReleaseScope<File> rs(file); |
1016 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { | 956 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { |
1017 if (!file->IsClosed()) { | 957 if (!file->IsClosed()) { |
1018 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); | 958 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); |
1019 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); | 959 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); |
1020 ASSERT(io_buffer != NULL); | 960 ASSERT(io_buffer != NULL); |
1021 uint8_t* data = io_buffer->value.as_external_typed_data.data; | 961 uint8_t* data = io_buffer->value.as_external_typed_data.data; |
(...skipping 13 matching lines...) Expand all Loading... |
1035 } else { | 975 } else { |
1036 return CObject::FileClosedError(); | 976 return CObject::FileClosedError(); |
1037 } | 977 } |
1038 } else { | 978 } else { |
1039 return CObject::IllegalArgumentError(); | 979 return CObject::IllegalArgumentError(); |
1040 } | 980 } |
1041 } | 981 } |
1042 return CObject::IllegalArgumentError(); | 982 return CObject::IllegalArgumentError(); |
1043 } | 983 } |
1044 | 984 |
1045 | |
1046 CObject* File::ReadIntoRequest(const CObjectArray& request) { | 985 CObject* File::ReadIntoRequest(const CObjectArray& request) { |
1047 if ((request.Length() >= 1) && request[0]->IsIntptr()) { | 986 if ((request.Length() >= 1) && request[0]->IsIntptr()) { |
1048 File* file = CObjectToFilePointer(request[0]); | 987 File* file = CObjectToFilePointer(request[0]); |
1049 RefCntReleaseScope<File> rs(file); | 988 RefCntReleaseScope<File> rs(file); |
1050 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { | 989 if ((request.Length() == 2) && request[1]->IsInt32OrInt64()) { |
1051 if (!file->IsClosed()) { | 990 if (!file->IsClosed()) { |
1052 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); | 991 int64_t length = CObjectInt32OrInt64ToInt64(request[1]); |
1053 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); | 992 Dart_CObject* io_buffer = CObject::NewIOBuffer(length); |
1054 ASSERT(io_buffer != NULL); | 993 ASSERT(io_buffer != NULL); |
1055 uint8_t* data = io_buffer->value.as_external_typed_data.data; | 994 uint8_t* data = io_buffer->value.as_external_typed_data.data; |
(...skipping 14 matching lines...) Expand all Loading... |
1070 } else { | 1009 } else { |
1071 return CObject::FileClosedError(); | 1010 return CObject::FileClosedError(); |
1072 } | 1011 } |
1073 } else { | 1012 } else { |
1074 return CObject::IllegalArgumentError(); | 1013 return CObject::IllegalArgumentError(); |
1075 } | 1014 } |
1076 } | 1015 } |
1077 return CObject::IllegalArgumentError(); | 1016 return CObject::IllegalArgumentError(); |
1078 } | 1017 } |
1079 | 1018 |
1080 | |
1081 static int SizeInBytes(Dart_TypedData_Type type) { | 1019 static int SizeInBytes(Dart_TypedData_Type type) { |
1082 switch (type) { | 1020 switch (type) { |
1083 case Dart_TypedData_kInt8: | 1021 case Dart_TypedData_kInt8: |
1084 case Dart_TypedData_kUint8: | 1022 case Dart_TypedData_kUint8: |
1085 case Dart_TypedData_kUint8Clamped: | 1023 case Dart_TypedData_kUint8Clamped: |
1086 return 1; | 1024 return 1; |
1087 case Dart_TypedData_kInt16: | 1025 case Dart_TypedData_kInt16: |
1088 case Dart_TypedData_kUint16: | 1026 case Dart_TypedData_kUint16: |
1089 return 2; | 1027 return 2; |
1090 case Dart_TypedData_kInt32: | 1028 case Dart_TypedData_kInt32: |
1091 case Dart_TypedData_kUint32: | 1029 case Dart_TypedData_kUint32: |
1092 case Dart_TypedData_kFloat32: | 1030 case Dart_TypedData_kFloat32: |
1093 return 4; | 1031 return 4; |
1094 case Dart_TypedData_kInt64: | 1032 case Dart_TypedData_kInt64: |
1095 case Dart_TypedData_kUint64: | 1033 case Dart_TypedData_kUint64: |
1096 case Dart_TypedData_kFloat64: | 1034 case Dart_TypedData_kFloat64: |
1097 return 8; | 1035 return 8; |
1098 default: | 1036 default: |
1099 break; | 1037 break; |
1100 } | 1038 } |
1101 UNREACHABLE(); | 1039 UNREACHABLE(); |
1102 return -1; | 1040 return -1; |
1103 } | 1041 } |
1104 | 1042 |
1105 | |
1106 CObject* File::WriteFromRequest(const CObjectArray& request) { | 1043 CObject* File::WriteFromRequest(const CObjectArray& request) { |
1107 if ((request.Length() >= 1) && request[0]->IsIntptr()) { | 1044 if ((request.Length() >= 1) && request[0]->IsIntptr()) { |
1108 File* file = CObjectToFilePointer(request[0]); | 1045 File* file = CObjectToFilePointer(request[0]); |
1109 RefCntReleaseScope<File> rs(file); | 1046 RefCntReleaseScope<File> rs(file); |
1110 if ((request.Length() == 4) && | 1047 if ((request.Length() == 4) && |
1111 (request[1]->IsTypedData() || request[1]->IsArray()) && | 1048 (request[1]->IsTypedData() || request[1]->IsArray()) && |
1112 request[2]->IsInt32OrInt64() && request[3]->IsInt32OrInt64()) { | 1049 request[2]->IsInt32OrInt64() && request[3]->IsInt32OrInt64()) { |
1113 if (!file->IsClosed()) { | 1050 if (!file->IsClosed()) { |
1114 int64_t start = CObjectInt32OrInt64ToInt64(request[2]); | 1051 int64_t start = CObjectInt32OrInt64ToInt64(request[2]); |
1115 int64_t end = CObjectInt32OrInt64ToInt64(request[3]); | 1052 int64_t end = CObjectInt32OrInt64ToInt64(request[3]); |
(...skipping 28 matching lines...) Expand all Loading... |
1144 } else { | 1081 } else { |
1145 return CObject::FileClosedError(); | 1082 return CObject::FileClosedError(); |
1146 } | 1083 } |
1147 } else { | 1084 } else { |
1148 return CObject::IllegalArgumentError(); | 1085 return CObject::IllegalArgumentError(); |
1149 } | 1086 } |
1150 } | 1087 } |
1151 return CObject::IllegalArgumentError(); | 1088 return CObject::IllegalArgumentError(); |
1152 } | 1089 } |
1153 | 1090 |
1154 | |
1155 CObject* File::CreateLinkRequest(const CObjectArray& request) { | 1091 CObject* File::CreateLinkRequest(const CObjectArray& request) { |
1156 if ((request.Length() != 2) || !request[0]->IsString() || | 1092 if ((request.Length() != 2) || !request[0]->IsString() || |
1157 !request[1]->IsString()) { | 1093 !request[1]->IsString()) { |
1158 return CObject::IllegalArgumentError(); | 1094 return CObject::IllegalArgumentError(); |
1159 } | 1095 } |
1160 CObjectString link_name(request[0]); | 1096 CObjectString link_name(request[0]); |
1161 CObjectString target_name(request[1]); | 1097 CObjectString target_name(request[1]); |
1162 if (File::CreateLink(link_name.CString(), target_name.CString())) { | 1098 if (File::CreateLink(link_name.CString(), target_name.CString())) { |
1163 return CObject::True(); | 1099 return CObject::True(); |
1164 } else { | 1100 } else { |
1165 return CObject::NewOSError(); | 1101 return CObject::NewOSError(); |
1166 } | 1102 } |
1167 } | 1103 } |
1168 | 1104 |
1169 | |
1170 CObject* File::DeleteLinkRequest(const CObjectArray& request) { | 1105 CObject* File::DeleteLinkRequest(const CObjectArray& request) { |
1171 if ((request.Length() == 1) && request[0]->IsString()) { | 1106 if ((request.Length() == 1) && request[0]->IsString()) { |
1172 CObjectString link_path(request[0]); | 1107 CObjectString link_path(request[0]); |
1173 bool result = File::DeleteLink(link_path.CString()); | 1108 bool result = File::DeleteLink(link_path.CString()); |
1174 if (result) { | 1109 if (result) { |
1175 return CObject::True(); | 1110 return CObject::True(); |
1176 } else { | 1111 } else { |
1177 return CObject::NewOSError(); | 1112 return CObject::NewOSError(); |
1178 } | 1113 } |
1179 } | 1114 } |
1180 return CObject::IllegalArgumentError(); | 1115 return CObject::IllegalArgumentError(); |
1181 } | 1116 } |
1182 | 1117 |
1183 | |
1184 CObject* File::RenameLinkRequest(const CObjectArray& request) { | 1118 CObject* File::RenameLinkRequest(const CObjectArray& request) { |
1185 if ((request.Length() == 2) && request[0]->IsString() && | 1119 if ((request.Length() == 2) && request[0]->IsString() && |
1186 request[1]->IsString()) { | 1120 request[1]->IsString()) { |
1187 CObjectString old_path(request[0]); | 1121 CObjectString old_path(request[0]); |
1188 CObjectString new_path(request[1]); | 1122 CObjectString new_path(request[1]); |
1189 bool completed = File::RenameLink(old_path.CString(), new_path.CString()); | 1123 bool completed = File::RenameLink(old_path.CString(), new_path.CString()); |
1190 if (completed) { | 1124 if (completed) { |
1191 return CObject::True(); | 1125 return CObject::True(); |
1192 } | 1126 } |
1193 return CObject::NewOSError(); | 1127 return CObject::NewOSError(); |
1194 } | 1128 } |
1195 return CObject::IllegalArgumentError(); | 1129 return CObject::IllegalArgumentError(); |
1196 } | 1130 } |
1197 | 1131 |
1198 | |
1199 CObject* File::LinkTargetRequest(const CObjectArray& request) { | 1132 CObject* File::LinkTargetRequest(const CObjectArray& request) { |
1200 if ((request.Length() == 1) && request[0]->IsString()) { | 1133 if ((request.Length() == 1) && request[0]->IsString()) { |
1201 CObjectString link_path(request[0]); | 1134 CObjectString link_path(request[0]); |
1202 const char* target = File::LinkTarget(link_path.CString()); | 1135 const char* target = File::LinkTarget(link_path.CString()); |
1203 if (target != NULL) { | 1136 if (target != NULL) { |
1204 CObject* result = new CObjectString(CObject::NewString(target)); | 1137 CObject* result = new CObjectString(CObject::NewString(target)); |
1205 return result; | 1138 return result; |
1206 } else { | 1139 } else { |
1207 return CObject::NewOSError(); | 1140 return CObject::NewOSError(); |
1208 } | 1141 } |
1209 } | 1142 } |
1210 return CObject::IllegalArgumentError(); | 1143 return CObject::IllegalArgumentError(); |
1211 } | 1144 } |
1212 | 1145 |
1213 | |
1214 CObject* File::TypeRequest(const CObjectArray& request) { | 1146 CObject* File::TypeRequest(const CObjectArray& request) { |
1215 if ((request.Length() == 2) && request[0]->IsString() && | 1147 if ((request.Length() == 2) && request[0]->IsString() && |
1216 request[1]->IsBool()) { | 1148 request[1]->IsBool()) { |
1217 CObjectString path(request[0]); | 1149 CObjectString path(request[0]); |
1218 CObjectBool follow_links(request[1]); | 1150 CObjectBool follow_links(request[1]); |
1219 File::Type type = File::GetType(path.CString(), follow_links.Value()); | 1151 File::Type type = File::GetType(path.CString(), follow_links.Value()); |
1220 return new CObjectInt32(CObject::NewInt32(type)); | 1152 return new CObjectInt32(CObject::NewInt32(type)); |
1221 } | 1153 } |
1222 return CObject::IllegalArgumentError(); | 1154 return CObject::IllegalArgumentError(); |
1223 } | 1155 } |
1224 | 1156 |
1225 | |
1226 CObject* File::IdenticalRequest(const CObjectArray& request) { | 1157 CObject* File::IdenticalRequest(const CObjectArray& request) { |
1227 if ((request.Length() == 2) && request[0]->IsString() && | 1158 if ((request.Length() == 2) && request[0]->IsString() && |
1228 request[1]->IsString()) { | 1159 request[1]->IsString()) { |
1229 CObjectString path1(request[0]); | 1160 CObjectString path1(request[0]); |
1230 CObjectString path2(request[1]); | 1161 CObjectString path2(request[1]); |
1231 File::Identical result = | 1162 File::Identical result = |
1232 File::AreIdentical(path1.CString(), path2.CString()); | 1163 File::AreIdentical(path1.CString(), path2.CString()); |
1233 if (result == File::kError) { | 1164 if (result == File::kError) { |
1234 return CObject::NewOSError(); | 1165 return CObject::NewOSError(); |
1235 } else if (result == File::kIdentical) { | 1166 } else if (result == File::kIdentical) { |
1236 return CObject::True(); | 1167 return CObject::True(); |
1237 } else { | 1168 } else { |
1238 return CObject::False(); | 1169 return CObject::False(); |
1239 } | 1170 } |
1240 } | 1171 } |
1241 return CObject::IllegalArgumentError(); | 1172 return CObject::IllegalArgumentError(); |
1242 } | 1173 } |
1243 | 1174 |
1244 | |
1245 CObject* File::StatRequest(const CObjectArray& request) { | 1175 CObject* File::StatRequest(const CObjectArray& request) { |
1246 if ((request.Length() == 1) && request[0]->IsString()) { | 1176 if ((request.Length() == 1) && request[0]->IsString()) { |
1247 int64_t data[File::kStatSize]; | 1177 int64_t data[File::kStatSize]; |
1248 CObjectString path(request[0]); | 1178 CObjectString path(request[0]); |
1249 File::Stat(path.CString(), data); | 1179 File::Stat(path.CString(), data); |
1250 if (data[File::kType] == File::kDoesNotExist) { | 1180 if (data[File::kType] == File::kDoesNotExist) { |
1251 return CObject::NewOSError(); | 1181 return CObject::NewOSError(); |
1252 } | 1182 } |
1253 CObjectArray* result = new CObjectArray(CObject::NewArray(File::kStatSize)); | 1183 CObjectArray* result = new CObjectArray(CObject::NewArray(File::kStatSize)); |
1254 for (int i = 0; i < File::kStatSize; ++i) { | 1184 for (int i = 0; i < File::kStatSize; ++i) { |
1255 result->SetAt(i, new CObjectInt64(CObject::NewInt64(data[i]))); | 1185 result->SetAt(i, new CObjectInt64(CObject::NewInt64(data[i]))); |
1256 } | 1186 } |
1257 CObjectArray* wrapper = new CObjectArray(CObject::NewArray(2)); | 1187 CObjectArray* wrapper = new CObjectArray(CObject::NewArray(2)); |
1258 wrapper->SetAt(0, new CObjectInt32(CObject::NewInt32(CObject::kSuccess))); | 1188 wrapper->SetAt(0, new CObjectInt32(CObject::NewInt32(CObject::kSuccess))); |
1259 wrapper->SetAt(1, result); | 1189 wrapper->SetAt(1, result); |
1260 return wrapper; | 1190 return wrapper; |
1261 } | 1191 } |
1262 return CObject::IllegalArgumentError(); | 1192 return CObject::IllegalArgumentError(); |
1263 } | 1193 } |
1264 | 1194 |
1265 | |
1266 CObject* File::LockRequest(const CObjectArray& request) { | 1195 CObject* File::LockRequest(const CObjectArray& request) { |
1267 if ((request.Length() >= 1) && request[0]->IsIntptr()) { | 1196 if ((request.Length() >= 1) && request[0]->IsIntptr()) { |
1268 File* file = CObjectToFilePointer(request[0]); | 1197 File* file = CObjectToFilePointer(request[0]); |
1269 RefCntReleaseScope<File> rs(file); | 1198 RefCntReleaseScope<File> rs(file); |
1270 if ((request.Length() == 4) && request[1]->IsInt32OrInt64() && | 1199 if ((request.Length() == 4) && request[1]->IsInt32OrInt64() && |
1271 request[2]->IsInt32OrInt64() && request[3]->IsInt32OrInt64()) { | 1200 request[2]->IsInt32OrInt64() && request[3]->IsInt32OrInt64()) { |
1272 if (!file->IsClosed()) { | 1201 if (!file->IsClosed()) { |
1273 int64_t lock = CObjectInt32OrInt64ToInt64(request[1]); | 1202 int64_t lock = CObjectInt32OrInt64ToInt64(request[1]); |
1274 int64_t start = CObjectInt32OrInt64ToInt64(request[2]); | 1203 int64_t start = CObjectInt32OrInt64ToInt64(request[2]); |
1275 int64_t end = CObjectInt32OrInt64ToInt64(request[3]); | 1204 int64_t end = CObjectInt32OrInt64ToInt64(request[3]); |
1276 if (file->Lock(static_cast<File::LockType>(lock), start, end)) { | 1205 if (file->Lock(static_cast<File::LockType>(lock), start, end)) { |
1277 return CObject::True(); | 1206 return CObject::True(); |
1278 } else { | 1207 } else { |
1279 return CObject::NewOSError(); | 1208 return CObject::NewOSError(); |
1280 } | 1209 } |
1281 } else { | 1210 } else { |
1282 return CObject::FileClosedError(); | 1211 return CObject::FileClosedError(); |
1283 } | 1212 } |
1284 } else { | 1213 } else { |
1285 return CObject::IllegalArgumentError(); | 1214 return CObject::IllegalArgumentError(); |
1286 } | 1215 } |
1287 } | 1216 } |
1288 return CObject::IllegalArgumentError(); | 1217 return CObject::IllegalArgumentError(); |
1289 } | 1218 } |
1290 | 1219 |
1291 } // namespace bin | 1220 } // namespace bin |
1292 } // namespace dart | 1221 } // namespace dart |
1293 | 1222 |
1294 #endif // !defined(DART_IO_DISABLED) | 1223 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |