Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: runtime/bin/file.cc

Issue 9839053: Add error handling to random access file (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased to r5810 Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/file.h ('k') | runtime/bin/file.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "bin/file.h" 5 #include "bin/file.h"
6 6
7 #include "bin/builtin.h" 7 #include "bin/builtin.h"
8 #include "bin/dartutils.h" 8 #include "bin/dartutils.h"
9 #include "bin/thread.h" 9 #include "bin/thread.h"
10 #include "bin/utils.h" 10 #include "bin/utils.h"
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 delete file; 108 delete file;
109 return_value = 0; 109 return_value = 0;
110 } 110 }
111 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); 111 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
112 Dart_ExitScope(); 112 Dart_ExitScope();
113 } 113 }
114 114
115 115
116 void FUNCTION_NAME(File_ReadByte)(Dart_NativeArguments args) { 116 void FUNCTION_NAME(File_ReadByte)(Dart_NativeArguments args) {
117 Dart_EnterScope(); 117 Dart_EnterScope();
118 intptr_t return_value = -1;
119 intptr_t value = 118 intptr_t value =
120 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); 119 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0));
121 File* file = reinterpret_cast<File*>(value); 120 File* file = reinterpret_cast<File*>(value);
122 if (file != NULL) { 121 if (file != NULL) {
123 uint8_t buffer; 122 uint8_t buffer;
124 int bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1); 123 int bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1);
125 if (bytes_read == 1) { 124 if (bytes_read == 1) {
126 return_value = static_cast<intptr_t>(buffer); 125 Dart_SetReturnValue(args, Dart_NewInteger(buffer));
126 } else if (bytes_read == 0) {
127 Dart_SetReturnValue(args, Dart_NewInteger(-1));
127 } else { 128 } else {
128 return_value = -1; 129 Dart_Handle err = DartUtils::NewDartOSError();
130 if (Dart_IsError(err)) {
131 Dart_PropagateError(err);
132 }
133 Dart_SetReturnValue(args, err);
129 } 134 }
130 } 135 }
131 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
132 Dart_ExitScope(); 136 Dart_ExitScope();
133 } 137 }
134 138
135 139
136 void FUNCTION_NAME(File_WriteByte)(Dart_NativeArguments args) { 140 void FUNCTION_NAME(File_WriteByte)(Dart_NativeArguments args) {
137 Dart_EnterScope(); 141 Dart_EnterScope();
138 intptr_t return_value = -1;
139 intptr_t value = 142 intptr_t value =
140 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); 143 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0));
141 File* file = reinterpret_cast<File*>(value); 144 File* file = reinterpret_cast<File*>(value);
142 if (file != NULL) { 145 if (file != NULL) {
143 int64_t value = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); 146 int64_t value = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1));
144 uint8_t buffer = static_cast<uint8_t>(value & 0xff); 147 uint8_t buffer = static_cast<uint8_t>(value & 0xff);
145 int bytes_written = file->Write(reinterpret_cast<void*>(&buffer), 1); 148 int bytes_written = file->Write(reinterpret_cast<void*>(&buffer), 1);
146 if (bytes_written >= 0) { 149 if (bytes_written >= 0) {
147 return_value = bytes_written; 150 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written));
151 } else {
152 Dart_Handle err = DartUtils::NewDartOSError();
153 if (Dart_IsError(err)) {
154 Dart_PropagateError(err);
155 }
156 Dart_SetReturnValue(args, err);
148 } 157 }
149 } 158 }
150 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
151 Dart_ExitScope(); 159 Dart_ExitScope();
152 } 160 }
153 161
154 162
155 void FUNCTION_NAME(File_WriteString)(Dart_NativeArguments args) { 163 void FUNCTION_NAME(File_WriteString)(Dart_NativeArguments args) {
156 Dart_EnterScope(); 164 Dart_EnterScope();
157 intptr_t return_value = -1;
158 intptr_t value = 165 intptr_t value =
159 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); 166 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0));
160 File* file = reinterpret_cast<File*>(value); 167 File* file = reinterpret_cast<File*>(value);
161 if (file != NULL) { 168 if (file != NULL) {
162 const char* str = 169 const char* str =
163 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); 170 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
164 int bytes_written = file->Write(reinterpret_cast<const void*>(str), 171 int bytes_written = file->Write(reinterpret_cast<const void*>(str),
165 strlen(str)); 172 strlen(str));
166 if (bytes_written >= 0) { 173 if (bytes_written >= 0) {
167 return_value = bytes_written; 174 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written));
175 } else {
176 Dart_Handle err = DartUtils::NewDartOSError();
177 if (Dart_IsError(err)) {
178 Dart_PropagateError(err);
179 }
180 Dart_SetReturnValue(args, err);
168 } 181 }
169 } 182 }
170 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
171 Dart_ExitScope(); 183 Dart_ExitScope();
172 } 184 }
173 185
174 186
175 void FUNCTION_NAME(File_ReadList)(Dart_NativeArguments args) { 187 void FUNCTION_NAME(File_ReadList)(Dart_NativeArguments args) {
176 Dart_EnterScope(); 188 Dart_EnterScope();
177 intptr_t return_value = -1;
178 intptr_t value = 189 intptr_t value =
179 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); 190 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0));
180 File* file = reinterpret_cast<File*>(value); 191 File* file = reinterpret_cast<File*>(value);
181 if (file != NULL) { 192 if (file != NULL) {
182 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); 193 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1);
183 ASSERT(Dart_IsList(buffer_obj)); 194 ASSERT(Dart_IsList(buffer_obj));
184 int64_t offset = 195 int64_t offset =
185 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); 196 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2));
186 int64_t length = 197 int64_t length =
187 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); 198 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3));
188 intptr_t array_len = 0; 199 intptr_t array_len = 0;
189 Dart_Handle result = Dart_ListLength(buffer_obj, &array_len); 200 Dart_Handle result = Dart_ListLength(buffer_obj, &array_len);
190 if (Dart_IsError(result)) { 201 if (Dart_IsError(result)) {
191 Dart_PropagateError(result); 202 Dart_PropagateError(result);
192 } 203 }
193 ASSERT((offset + length) <= array_len); 204 ASSERT((offset + length) <= array_len);
194 uint8_t* buffer = new uint8_t[length]; 205 uint8_t* buffer = new uint8_t[length];
195 int total_bytes_read = 206 int bytes_read = file->Read(reinterpret_cast<void*>(buffer), length);
196 file->Read(reinterpret_cast<void*>(buffer), length); 207 if (bytes_read >= 0) {
197 /*
198 * Reading 0 indicates end of file.
199 */
200 if (total_bytes_read >= 0) {
201 result = 208 result =
202 Dart_ListSetAsBytes(buffer_obj, offset, buffer, total_bytes_read); 209 Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read);
203 if (Dart_IsError(result)) { 210 if (Dart_IsError(result)) {
204 delete[] buffer; 211 delete[] buffer;
205 Dart_PropagateError(result); 212 Dart_PropagateError(result);
206 } 213 }
207 return_value = total_bytes_read; 214 Dart_SetReturnValue(args, Dart_NewInteger(bytes_read));
215 } else {
216 Dart_Handle err = DartUtils::NewDartOSError();
217 if (Dart_IsError(err)) {
218 Dart_PropagateError(err);
219 }
220 Dart_SetReturnValue(args, err);
208 } 221 }
209 delete[] buffer; 222 delete[] buffer;
210 } 223 }
211 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
212 Dart_ExitScope(); 224 Dart_ExitScope();
213 } 225 }
214 226
215 227
216 void FUNCTION_NAME(File_WriteList)(Dart_NativeArguments args) { 228 void FUNCTION_NAME(File_WriteList)(Dart_NativeArguments args) {
217 Dart_EnterScope(); 229 Dart_EnterScope();
218 intptr_t return_value = -1;
219 intptr_t value = 230 intptr_t value =
220 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); 231 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0));
221 File* file = reinterpret_cast<File*>(value); 232 File* file = reinterpret_cast<File*>(value);
222 if (file != NULL) { 233 if (file != NULL) {
223 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); 234 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1);
224 ASSERT(Dart_IsList(buffer_obj)); 235 ASSERT(Dart_IsList(buffer_obj));
225 int64_t offset = 236 int64_t offset =
226 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); 237 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2));
227 int64_t length = 238 int64_t length =
228 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); 239 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3));
229 intptr_t buffer_len = 0; 240 intptr_t buffer_len = 0;
230 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len); 241 Dart_Handle result = Dart_ListLength(buffer_obj, &buffer_len);
231 if (Dart_IsError(result)) { 242 if (Dart_IsError(result)) {
232 Dart_PropagateError(result); 243 Dart_PropagateError(result);
233 } 244 }
234 ASSERT((offset + length) <= buffer_len); 245 ASSERT((offset + length) <= buffer_len);
235 uint8_t* buffer = new uint8_t[length]; 246 uint8_t* buffer = new uint8_t[length];
236 result = Dart_ListGetAsBytes(buffer_obj, offset, buffer, length); 247 result = Dart_ListGetAsBytes(buffer_obj, offset, buffer, length);
237 if (Dart_IsError(result)) { 248 if (Dart_IsError(result)) {
238 delete[] buffer; 249 delete[] buffer;
239 Dart_PropagateError(result); 250 Dart_PropagateError(result);
240 } 251 }
241 int total_bytes_written = 252 int bytes_written = file->Write(reinterpret_cast<void*>(buffer), length);
242 file->Write(reinterpret_cast<void*>(buffer), length); 253 if (bytes_written >= 0) {
243 if (total_bytes_written >= 0) { 254 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written));
244 return_value = total_bytes_written; 255 } else {
256 Dart_Handle err = DartUtils::NewDartOSError();
257 if (Dart_IsError(err)) {
258 Dart_PropagateError(err);
259 }
260 Dart_SetReturnValue(args, err);
245 } 261 }
246 delete[] buffer; 262 delete[] buffer;
247 } 263 }
248 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
249 Dart_ExitScope(); 264 Dart_ExitScope();
250 } 265 }
251 266
252 267
253 void FUNCTION_NAME(File_Position)(Dart_NativeArguments args) { 268 void FUNCTION_NAME(File_Position)(Dart_NativeArguments args) {
254 Dart_EnterScope(); 269 Dart_EnterScope();
255 intptr_t return_value = -1;
256 intptr_t value = 270 intptr_t value =
257 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); 271 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0));
258 File* file = reinterpret_cast<File*>(value); 272 File* file = reinterpret_cast<File*>(value);
259 if (file != NULL) { 273 if (file != NULL) {
260 return_value = file->Position(); 274 intptr_t return_value = file->Position();
275 if (return_value >= 0) {
276 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
277 } else {
278 Dart_Handle err = DartUtils::NewDartOSError();
279 if (Dart_IsError(err)) {
280 Dart_PropagateError(err);
281 }
282 Dart_SetReturnValue(args, err);
283 }
261 } 284 }
262 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
263 Dart_ExitScope(); 285 Dart_ExitScope();
264 } 286 }
265 287
266 288
267 void FUNCTION_NAME(File_SetPosition)(Dart_NativeArguments args) { 289 void FUNCTION_NAME(File_SetPosition)(Dart_NativeArguments args) {
268 Dart_EnterScope(); 290 Dart_EnterScope();
269 bool return_value = false;
270 intptr_t value = 291 intptr_t value =
271 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); 292 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0));
272 File* file = reinterpret_cast<File*>(value); 293 File* file = reinterpret_cast<File*>(value);
273 if (file != NULL) { 294 if (file != NULL) {
274 int64_t position = 295 int64_t position =
275 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); 296 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1));
276 return_value = file->SetPosition(position); 297 if (file->SetPosition(position)) {
298 Dart_SetReturnValue(args, Dart_True());
299 } else {
300 Dart_Handle err = DartUtils::NewDartOSError();
301 if (Dart_IsError(err)) {
302 Dart_PropagateError(err);
303 }
304 Dart_SetReturnValue(args, err);
305 }
277 } 306 }
278 Dart_SetReturnValue(args, Dart_NewBoolean(return_value));
279 Dart_ExitScope(); 307 Dart_ExitScope();
280 } 308 }
281 309
282 310
283 void FUNCTION_NAME(File_Truncate)(Dart_NativeArguments args) { 311 void FUNCTION_NAME(File_Truncate)(Dart_NativeArguments args) {
284 Dart_EnterScope(); 312 Dart_EnterScope();
285 bool return_value = false;
286 intptr_t value = 313 intptr_t value =
287 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); 314 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0));
288 File* file = reinterpret_cast<File*>(value); 315 File* file = reinterpret_cast<File*>(value);
289 if (file != NULL) { 316 if (file != NULL) {
290 int64_t length = 317 int64_t length =
291 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); 318 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1));
292 return_value = file->Truncate(length); 319 if (file->Truncate(length)) {
320 Dart_SetReturnValue(args, Dart_True());
321 } else {
322 Dart_Handle err = DartUtils::NewDartOSError();
323 if (Dart_IsError(err)) {
324 Dart_PropagateError(err);
325 }
326 Dart_SetReturnValue(args, err);
327 }
293 } 328 }
294 Dart_SetReturnValue(args, Dart_NewBoolean(return_value));
295 Dart_ExitScope(); 329 Dart_ExitScope();
296 } 330 }
297 331
298 332
299 void FUNCTION_NAME(File_Length)(Dart_NativeArguments args) { 333 void FUNCTION_NAME(File_Length)(Dart_NativeArguments args) {
300 Dart_EnterScope(); 334 Dart_EnterScope();
301 intptr_t return_value = -1;
302 intptr_t value = 335 intptr_t value =
303 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); 336 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0));
304 File* file = reinterpret_cast<File*>(value); 337 File* file = reinterpret_cast<File*>(value);
305 if (file != NULL) { 338 if (file != NULL) {
306 return_value = file->Length(); 339 intptr_t return_value = file->Length();
340 if (return_value >= 0) {
341 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
342 } else {
343 Dart_Handle err = DartUtils::NewDartOSError();
344 if (Dart_IsError(err)) {
345 Dart_PropagateError(err);
346 }
347 Dart_SetReturnValue(args, err);
348 }
307 } 349 }
308 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
309 Dart_ExitScope(); 350 Dart_ExitScope();
310 } 351 }
311 352
312 353
313 void FUNCTION_NAME(File_Flush)(Dart_NativeArguments args) { 354 void FUNCTION_NAME(File_Flush)(Dart_NativeArguments args) {
314 Dart_EnterScope(); 355 Dart_EnterScope();
315 intptr_t return_value = -1; 356 intptr_t return_value = -1;
316 intptr_t value = 357 intptr_t value =
317 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); 358 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0));
318 File* file = reinterpret_cast<File*>(value); 359 File* file = reinterpret_cast<File*>(value);
319 if (file != NULL) { 360 if (file != NULL) {
320 file->Flush(); 361 if (file->Flush()) {
362 Dart_SetReturnValue(args, Dart_True());
363 } else {
364 Dart_Handle err = DartUtils::NewDartOSError();
365 if (Dart_IsError(err)) {
366 Dart_PropagateError(err);
367 }
368 Dart_SetReturnValue(args, err);
369 }
321 return_value = 0; 370 return_value = 0;
322 } 371 }
323 Dart_SetReturnValue(args, Dart_NewInteger(return_value));
324 Dart_ExitScope(); 372 Dart_ExitScope();
325 } 373 }
326 374
327 375
328 void FUNCTION_NAME(File_Create)(Dart_NativeArguments args) { 376 void FUNCTION_NAME(File_Create)(Dart_NativeArguments args) {
329 Dart_EnterScope(); 377 Dart_EnterScope();
330 const char* str = 378 const char* str =
331 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); 379 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0));
332 bool result = File::Create(str); 380 bool result = File::Create(str);
333 if (result) { 381 if (result) {
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
539 if (file != NULL) { 587 if (file != NULL) {
540 delete file; 588 delete file;
541 return_value = 0; 589 return_value = 0;
542 } 590 }
543 } 591 }
544 return new CObjectIntptr(CObject::NewIntptr(return_value)); 592 return new CObjectIntptr(CObject::NewIntptr(return_value));
545 } 593 }
546 594
547 595
548 static CObject* FilePositionRequest(const CObjectArray& request) { 596 static CObject* FilePositionRequest(const CObjectArray& request) {
549 intptr_t return_value = -1;
550 if (request.Length() == 2 && request[1]->IsIntptr()) { 597 if (request.Length() == 2 && request[1]->IsIntptr()) {
551 File* file = CObjectToFilePointer(request[1]); 598 File* file = CObjectToFilePointer(request[1]);
552 if (file != NULL) { 599 if (file != NULL && !file->IsClosed()) {
553 return_value = file->Position(); 600 intptr_t return_value = file->Position();
601 if (return_value >= 0) {
602 return new CObjectIntptr(CObject::NewIntptr(return_value));
603 } else {
604 return CObject::NewOSError();
605 }
606 } else {
607 return CObject::FileClosedError();
554 } 608 }
555 } 609 }
556 return new CObjectIntptr(CObject::NewIntptr(return_value)); 610 return CObject::IllegalArgumentError();
557 } 611 }
558 612
559 613
560 static CObject* FileSetPositionRequest(const CObjectArray& request) { 614 static CObject* FileSetPositionRequest(const CObjectArray& request) {
561 bool return_value = false;
562 if (request.Length() == 3 && 615 if (request.Length() == 3 &&
563 request[1]->IsIntptr() && 616 request[1]->IsIntptr() &&
564 request[2]->IsInt32OrInt64()) { 617 request[2]->IsInt32OrInt64()) {
565 File* file = CObjectToFilePointer(request[1]); 618 File* file = CObjectToFilePointer(request[1]);
566 if (file != NULL) { 619 if (file != NULL && !file->IsClosed()) {
567 int64_t position = CObjectInt32OrInt64ToInt64(request[2]); 620 int64_t position = CObjectInt32OrInt64ToInt64(request[2]);
568 return_value = file->SetPosition(position); 621 if (file->SetPosition(position)) {
622 return CObject::True();
623 } else {
624 return CObject::NewOSError();
625 }
626 } else {
627 return CObject::FileClosedError();
569 } 628 }
570 } 629 }
571 return CObject::Bool(return_value); 630 return CObject::IllegalArgumentError();
572 } 631 }
573 632
574 633
575 static CObject* FileTruncateRequest(const CObjectArray& request) { 634 static CObject* FileTruncateRequest(const CObjectArray& request) {
576 bool return_value = false;
577 if (request.Length() == 3 && 635 if (request.Length() == 3 &&
578 request[1]->IsIntptr() && 636 request[1]->IsIntptr() &&
579 request[2]->IsInt32OrInt64()) { 637 request[2]->IsInt32OrInt64()) {
580 File* file = CObjectToFilePointer(request[1]); 638 File* file = CObjectToFilePointer(request[1]);
581 if (file != NULL) { 639 if (file != NULL && !file->IsClosed()) {
582 int64_t length = CObjectInt32OrInt64ToInt64(request[2]); 640 int64_t length = CObjectInt32OrInt64ToInt64(request[2]);
583 return_value = file->Truncate(length); 641 if (file->Truncate(length)) {
642 return CObject::True();
643 } else {
644 return CObject::NewOSError();
645 }
646 } else {
647 return CObject::FileClosedError();
584 } 648 }
585 } 649 }
586 return CObject::Bool(return_value); 650 return CObject::IllegalArgumentError();
587 } 651 }
588 652
589 653
590 static CObject* FileLengthRequest(const CObjectArray& request) { 654 static CObject* FileLengthRequest(const CObjectArray& request) {
591 intptr_t return_value = -1;
592 if (request.Length() == 2 && request[1]->IsIntptr()) { 655 if (request.Length() == 2 && request[1]->IsIntptr()) {
593 File* file = CObjectToFilePointer(request[1]); 656 File* file = CObjectToFilePointer(request[1]);
594 if (file != NULL) { 657 if (file != NULL && !file->IsClosed()) {
595 return_value = file->Length(); 658 intptr_t return_value = file->Length();
659 if (return_value >= 0) {
660 return new CObjectIntptr(CObject::NewIntptr(return_value));
661 } else {
662 return CObject::NewOSError();
663 }
664 } else {
665 return CObject::FileClosedError();
596 } 666 }
597 } 667 }
598 return new CObjectIntptr(CObject::NewIntptr(return_value)); 668 return CObject::IllegalArgumentError();
599 } 669 }
600 670
601 671
602 static CObject* FileFlushRequest(const CObjectArray& request) { 672 static CObject* FileFlushRequest(const CObjectArray& request) {
603 intptr_t return_value = -1; 673 intptr_t return_value = -1;
604 if (request.Length() == 2 && request[1]->IsIntptr()) { 674 if (request.Length() == 2 && request[1]->IsIntptr()) {
605 File* file = CObjectToFilePointer(request[1]); 675 File* file = CObjectToFilePointer(request[1]);
606 if (file != NULL) { 676 if (file != NULL && !file->IsClosed()) {
607 file->Flush(); 677 if (file->Flush()) {
608 return_value = 0; 678 return CObject::True();
679 } else {
680 return CObject::NewOSError();
681 }
682 } else {
683 return CObject::FileClosedError();
609 } 684 }
610 } 685 }
686 return CObject::IllegalArgumentError();
611 return new CObjectIntptr(CObject::NewIntptr(return_value)); 687 return new CObjectIntptr(CObject::NewIntptr(return_value));
612 } 688 }
613 689
614 690
615 static CObject* FileReadByteRequest(const CObjectArray& request) { 691 static CObject* FileReadByteRequest(const CObjectArray& request) {
616 intptr_t return_value = -1;
617 if (request.Length() == 2 && request[1]->IsIntptr()) { 692 if (request.Length() == 2 && request[1]->IsIntptr()) {
618 File* file = CObjectToFilePointer(request[1]); 693 File* file = CObjectToFilePointer(request[1]);
619 if (file != NULL) { 694 if (file != NULL && !file->IsClosed()) {
620 uint8_t buffer; 695 uint8_t buffer;
621 int bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1); 696 int bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1);
622 if (bytes_read == 1) { 697 if (bytes_read > 0) {
623 return_value = static_cast<intptr_t>(buffer); 698 return new CObjectIntptr(CObject::NewIntptr(buffer));
699 } else if (bytes_read == 0) {
700 return new CObjectIntptr(CObject::NewIntptr(-1));
624 } else { 701 } else {
625 return_value = -1; 702 return CObject::NewOSError();
626 } 703 }
704 } else {
705 return CObject::FileClosedError();
627 } 706 }
628 } 707 }
629 return new CObjectIntptr(CObject::NewIntptr(return_value)); 708 return CObject::IllegalArgumentError();
630 } 709 }
631 710
711
632 static CObject* FileWriteByteRequest(const CObjectArray& request) { 712 static CObject* FileWriteByteRequest(const CObjectArray& request) {
633 intptr_t return_value = -1;
634 if (request.Length() == 3 && 713 if (request.Length() == 3 &&
635 request[1]->IsIntptr() && 714 request[1]->IsIntptr() &&
636 request[2]->IsInt32OrInt64()) { 715 request[2]->IsInt32OrInt64()) {
637 File* file = CObjectToFilePointer(request[1]); 716 File* file = CObjectToFilePointer(request[1]);
638 if (file != NULL) { 717 if (file != NULL && !file->IsClosed()) {
639 int64_t byte = CObjectInt32OrInt64ToInt64(request[2]); 718 int64_t byte = CObjectInt32OrInt64ToInt64(request[2]);
640 uint8_t buffer = static_cast<uint8_t>(byte & 0xff); 719 uint8_t buffer = static_cast<uint8_t>(byte & 0xff);
641 int bytes_written = file->Write(reinterpret_cast<void*>(&buffer), 1); 720 int bytes_written = file->Write(reinterpret_cast<void*>(&buffer), 1);
642 if (bytes_written >= 0) { 721 if (bytes_written > 0) {
643 return_value = bytes_written; 722 return new CObjectIntptr(CObject::NewIntptr(bytes_written));
723 } else {
724 return CObject::NewOSError();
644 } 725 }
726 } else {
727 return CObject::FileClosedError();
645 } 728 }
646 } 729 }
647 return new CObjectIntptr(CObject::NewIntptr(return_value)); 730 return CObject::IllegalArgumentError();
648 } 731 }
649 732
733
650 static CObject* FileReadListRequest(const CObjectArray& request) { 734 static CObject* FileReadListRequest(const CObjectArray& request) {
651 if (request.Length() == 3 && 735 if (request.Length() == 3 &&
652 request[1]->IsIntptr() && 736 request[1]->IsIntptr() &&
653 request[2]->IsInt32OrInt64()) { 737 request[2]->IsInt32OrInt64()) {
654 File* file = CObjectToFilePointer(request[1]); 738 File* file = CObjectToFilePointer(request[1]);
655 int64_t length = CObjectInt32OrInt64ToInt64(request[2]); 739 if (file != NULL && !file->IsClosed()) {
656 CObjectByteArray* byte_array = 740 int64_t length = CObjectInt32OrInt64ToInt64(request[2]);
657 new CObjectByteArray(CObject::NewByteArray(length)); 741 CObjectByteArray* byte_array =
658 void* buffer = reinterpret_cast<void*>(byte_array->Buffer()); 742 new CObjectByteArray(CObject::NewByteArray(length));
659 int total_bytes_read = file->Read(buffer, byte_array->Length()); 743 void* buffer = reinterpret_cast<void*>(byte_array->Buffer());
660 CObjectArray* result = new CObjectArray(CObject::NewArray(2)); 744 int bytes_read = file->Read(buffer, byte_array->Length());
661 result->SetAt(0, new CObjectIntptr(CObject::NewIntptr(total_bytes_read))); 745 if (bytes_read >= 0) {
662 result->SetAt(1, byte_array); 746 CObjectArray* result = new CObjectArray(CObject::NewArray(3));
663 return result; 747 result->SetAt(0, new CObjectIntptr(CObject::NewInt32(0)));
748 result->SetAt(1, new CObjectIntptr(CObject::NewIntptr(bytes_read)));
749 result->SetAt(2, byte_array);
750 return result;
751 } else {
752 return CObject::NewOSError();
753 }
754 } else {
755 return CObject::FileClosedError();
756 }
664 } 757 }
665 return CObject::Null(); 758 return CObject::IllegalArgumentError();
666 } 759 }
667 760
668 761
669 static CObject* FileWriteListRequest(const CObjectArray& request) { 762 static CObject* FileWriteListRequest(const CObjectArray& request) {
670 if (request.Length() == 5 && 763 if (request.Length() == 5 &&
671 request[1]->IsIntptr() && 764 request[1]->IsIntptr() &&
672 (request[2]->IsByteArray() || request[2]->IsArray()) && 765 (request[2]->IsByteArray() || request[2]->IsArray()) &&
673 request[3]->IsInt32OrInt64() && 766 request[3]->IsInt32OrInt64() &&
674 request[4]->IsInt32OrInt64()) { 767 request[4]->IsInt32OrInt64()) {
675 File* file = CObjectToFilePointer(request[1]); 768 File* file = CObjectToFilePointer(request[1]);
676 int64_t offset = CObjectInt32OrInt64ToInt64(request[3]); 769 if (file != NULL && !file->IsClosed()) {
677 int64_t length = CObjectInt32OrInt64ToInt64(request[4]); 770 int64_t offset = CObjectInt32OrInt64ToInt64(request[3]);
678 uint8_t* buffer_start; 771 int64_t length = CObjectInt32OrInt64ToInt64(request[4]);
679 if (request[2]->IsByteArray()) { 772 uint8_t* buffer_start;
680 CObjectByteArray byte_array(request[2]); 773 if (request[2]->IsByteArray()) {
681 buffer_start = byte_array.Buffer() + offset; 774 CObjectByteArray byte_array(request[2]);
775 buffer_start = byte_array.Buffer() + offset;
776 } else {
777 CObjectArray array(request[2]);
778 buffer_start = new uint8_t[length];
779 for (int i = 0; i < length; i++) {
780 if (array[i + offset]->IsInt32OrInt64()) {
781 int64_t value = CObjectInt32OrInt64ToInt64(array[i + offset]);
782 buffer_start[i] = value & 0xFF;
783 } else {
784 // Unsupported type.
785 delete[] buffer_start;
786 return CObject::IllegalArgumentError();
787 }
788 }
789 offset = 0;
790 }
791 int64_t bytes_written =
792 file->Write(reinterpret_cast<void*>(buffer_start), length);
793 if (!request[2]->IsByteArray()) {
794 delete[] buffer_start;
795 }
796 if (bytes_written >= 0) {
797 return new CObjectIntptr(CObject::NewIntptr(bytes_written));
798 } else {
799 return CObject::NewOSError();
800 }
682 } else { 801 } else {
683 CObjectArray array(request[2]); 802 return CObject::FileClosedError();
684 buffer_start = new uint8_t[length];
685 for (int i = 0; i < length; i++) {
686 if (array[i + offset]->IsInt32OrInt64()) {
687 int64_t value = CObjectInt32OrInt64ToInt64(array[i + offset]);
688 buffer_start[i] = value & 0xFF;
689 } else {
690 // Unsupported type.
691 delete[] buffer_start;
692 return new CObjectInt32(CObject::NewInt32(-1));
693 }
694 }
695 offset = 0;
696 } 803 }
697 int64_t total_bytes_written =
698 file->Write(reinterpret_cast<void*>(buffer_start), length);
699 ASSERT(total_bytes_written == length);
700 if (!request[2]->IsByteArray()) {
701 delete[] buffer_start;
702 }
703 return new CObjectInt64(CObject::NewInt64(total_bytes_written));
704 } 804 }
705 return CObject::Null(); 805 return CObject::IllegalArgumentError();
706 } 806 }
707 807
708 808
709 static CObject* FileWriteStringRequest(const CObjectArray& request) { 809 static CObject* FileWriteStringRequest(const CObjectArray& request) {
710 if (request.Length() == 3 && 810 if (request.Length() == 3 &&
711 request[1]->IsIntptr() && 811 request[1]->IsIntptr() &&
712 request[2]->IsString()) { 812 request[2]->IsString()) {
713 File* file = CObjectToFilePointer(request[1]); 813 File* file = CObjectToFilePointer(request[1]);
714 CObjectString str(request[2]); 814 if (file != NULL && !file->IsClosed()) {
715 const void* buffer = reinterpret_cast<const void*>(str.CString()); 815 CObjectString str(request[2]);
716 int64_t total_bytes_written = file->Write(buffer, str.Length()); 816 const void* buffer = reinterpret_cast<const void*>(str.CString());
717 ASSERT(total_bytes_written == str.Length()); 817 int64_t bytes_written = file->Write(buffer, str.Length());
718 return new CObjectInt64(CObject::NewInt64(total_bytes_written)); 818 return new CObjectInt64(CObject::NewInt64(bytes_written));
819 } else {
820 return CObject::FileClosedError();
821 }
719 } 822 }
720 return CObject::Null(); 823 return CObject::IllegalArgumentError();
721 } 824 }
722 825
723 826
724 void FileService(Dart_Port dest_port_id, 827 void FileService(Dart_Port dest_port_id,
725 Dart_Port reply_port_id, 828 Dart_Port reply_port_id,
726 Dart_CObject* message) { 829 Dart_CObject* message) {
727 CObject* response = CObject::False(); 830 CObject* response = CObject::False();
728 CObjectArray request(message); 831 CObjectArray request(message);
729 if (message->type == Dart_CObject::kArray) { 832 if (message->type == Dart_CObject::kArray) {
730 if (request.Length() > 1 && request[0]->IsInt32()) { 833 if (request.Length() > 1 && request[0]->IsInt32()) {
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
820 Dart_EnterScope(); 923 Dart_EnterScope();
821 Dart_SetReturnValue(args, Dart_Null()); 924 Dart_SetReturnValue(args, Dart_Null());
822 Dart_Port service_port = File::GetServicePort(); 925 Dart_Port service_port = File::GetServicePort();
823 if (service_port != kIllegalPort) { 926 if (service_port != kIllegalPort) {
824 // Return a send port for the service port. 927 // Return a send port for the service port.
825 Dart_Handle send_port = Dart_NewSendPort(service_port); 928 Dart_Handle send_port = Dart_NewSendPort(service_port);
826 Dart_SetReturnValue(args, send_port); 929 Dart_SetReturnValue(args, send_port);
827 } 930 }
828 Dart_ExitScope(); 931 Dart_ExitScope();
829 } 932 }
OLDNEW
« no previous file with comments | « runtime/bin/file.h ('k') | runtime/bin/file.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698