| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "bin/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 | 11 |
| 11 #include "include/dart_api.h" | 12 #include "include/dart_api.h" |
| 12 | 13 |
| 13 dart::Mutex File::mutex_; | 14 dart::Mutex File::mutex_; |
| 14 int File::service_ports_size_ = 0; | 15 int File::service_ports_size_ = 0; |
| 15 Dart_Port* File::service_ports_ = NULL; | 16 Dart_Port* File::service_ports_ = NULL; |
| 16 int File::service_ports_index_ = 0; | 17 int File::service_ports_index_ = 0; |
| 17 | 18 |
| 18 bool File::ReadFully(void* buffer, int64_t num_bytes) { | 19 bool File::ReadFully(void* buffer, int64_t num_bytes) { |
| 19 int64_t remaining = num_bytes; | 20 int64_t remaining = num_bytes; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 66 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 66 int mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 67 int mode = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
| 67 File::DartFileOpenMode dart_file_mode = | 68 File::DartFileOpenMode dart_file_mode = |
| 68 static_cast<File::DartFileOpenMode>(mode); | 69 static_cast<File::DartFileOpenMode>(mode); |
| 69 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); | 70 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); |
| 70 // Check that the file exists before opening it only for | 71 // Check that the file exists before opening it only for |
| 71 // reading. This is to prevent the opening of directories as | 72 // reading. This is to prevent the opening of directories as |
| 72 // files. Directories can be opened for reading using the posix | 73 // files. Directories can be opened for reading using the posix |
| 73 // 'open' call. | 74 // 'open' call. |
| 74 File* file = NULL; | 75 File* file = NULL; |
| 75 if (((file_mode & File::kWrite) != 0) || File::Exists(filename)) { | 76 file = File::Open(filename, file_mode); |
| 76 file = File::Open(filename, file_mode); | 77 if (file != NULL) { |
| 78 Dart_SetReturnValue(args, |
| 79 Dart_NewInteger(reinterpret_cast<intptr_t>(file))); |
| 80 } else { |
| 81 Dart_Handle err = DartUtils::NewDartOSError(); |
| 82 if (Dart_IsError(err)) { |
| 83 Dart_PropagateError(err); |
| 84 } |
| 85 Dart_SetReturnValue(args, err); |
| 77 } | 86 } |
| 78 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file))); | |
| 79 Dart_ExitScope(); | 87 Dart_ExitScope(); |
| 80 } | 88 } |
| 81 | 89 |
| 82 | 90 |
| 83 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { | 91 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { |
| 84 Dart_EnterScope(); | 92 Dart_EnterScope(); |
| 85 const char* filename = | 93 const char* filename = |
| 86 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 94 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 87 bool exists = File::Exists(filename); | 95 bool exists = File::Exists(filename); |
| 88 Dart_SetReturnValue(args, Dart_NewBoolean(exists)); | 96 Dart_SetReturnValue(args, Dart_NewBoolean(exists)); |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 323 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 316 Dart_ExitScope(); | 324 Dart_ExitScope(); |
| 317 } | 325 } |
| 318 | 326 |
| 319 | 327 |
| 320 void FUNCTION_NAME(File_Create)(Dart_NativeArguments args) { | 328 void FUNCTION_NAME(File_Create)(Dart_NativeArguments args) { |
| 321 Dart_EnterScope(); | 329 Dart_EnterScope(); |
| 322 const char* str = | 330 const char* str = |
| 323 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 331 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 324 bool result = File::Create(str); | 332 bool result = File::Create(str); |
| 325 Dart_SetReturnValue(args, Dart_NewBoolean(result)); | 333 if (result) { |
| 334 Dart_SetReturnValue(args, Dart_NewBoolean(result)); |
| 335 } else { |
| 336 Dart_Handle err = DartUtils::NewDartOSError(); |
| 337 if (Dart_IsError(err)) { |
| 338 Dart_PropagateError(err); |
| 339 } |
| 340 Dart_SetReturnValue(args, err); |
| 341 } |
| 326 Dart_ExitScope(); | 342 Dart_ExitScope(); |
| 327 } | 343 } |
| 328 | 344 |
| 329 | 345 |
| 330 void FUNCTION_NAME(File_Delete)(Dart_NativeArguments args) { | 346 void FUNCTION_NAME(File_Delete)(Dart_NativeArguments args) { |
| 331 Dart_EnterScope(); | 347 Dart_EnterScope(); |
| 332 const char* str = | 348 const char* str = |
| 333 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 349 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 334 bool result = File::Delete(str); | 350 bool result = File::Delete(str); |
| 335 Dart_SetReturnValue(args, Dart_NewBoolean(result)); | 351 if (result) { |
| 352 Dart_SetReturnValue(args, Dart_NewBoolean(result)); |
| 353 } else { |
| 354 Dart_Handle err = DartUtils::NewDartOSError(); |
| 355 if (Dart_IsError(err)) { |
| 356 Dart_PropagateError(err); |
| 357 } |
| 358 Dart_SetReturnValue(args, err); |
| 359 } |
| 336 Dart_ExitScope(); | 360 Dart_ExitScope(); |
| 337 } | 361 } |
| 338 | 362 |
| 339 | 363 |
| 340 void FUNCTION_NAME(File_Directory)(Dart_NativeArguments args) { | 364 void FUNCTION_NAME(File_Directory)(Dart_NativeArguments args) { |
| 341 Dart_EnterScope(); | 365 Dart_EnterScope(); |
| 342 const char* str = | 366 const char* str = |
| 343 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 367 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 344 char* str_copy = strdup(str); | 368 char* str_copy = strdup(str); |
| 345 char* path = File::GetContainingDirectory(str_copy); | 369 char* path = File::GetContainingDirectory(str_copy); |
| 370 free(str_copy); |
| 346 if (path != NULL) { | 371 if (path != NULL) { |
| 347 Dart_SetReturnValue(args, Dart_NewString(path)); | 372 Dart_SetReturnValue(args, Dart_NewString(path)); |
| 373 free(path); |
| 374 } else { |
| 375 Dart_Handle err = DartUtils::NewDartOSError(); |
| 376 if (Dart_IsError(err)) { |
| 377 Dart_PropagateError(err); |
| 378 } |
| 379 Dart_SetReturnValue(args, err); |
| 348 } | 380 } |
| 349 free(str_copy); | |
| 350 free(path); | |
| 351 Dart_ExitScope(); | 381 Dart_ExitScope(); |
| 352 } | 382 } |
| 353 | 383 |
| 354 | 384 |
| 355 void FUNCTION_NAME(File_FullPath)(Dart_NativeArguments args) { | 385 void FUNCTION_NAME(File_FullPath)(Dart_NativeArguments args) { |
| 356 Dart_EnterScope(); | 386 Dart_EnterScope(); |
| 357 const char* str = | 387 const char* str = |
| 358 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 388 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 359 char* path = File::GetCanonicalPath(str); | 389 char* path = File::GetCanonicalPath(str); |
| 360 if (path != NULL) { | 390 if (path != NULL) { |
| 361 Dart_SetReturnValue(args, Dart_NewString(path)); | 391 Dart_SetReturnValue(args, Dart_NewString(path)); |
| 362 free(path); | 392 free(path); |
| 393 } else { |
| 394 Dart_Handle err = DartUtils::NewDartOSError(); |
| 395 if (Dart_IsError(err)) { |
| 396 Dart_PropagateError(err); |
| 397 } |
| 398 Dart_SetReturnValue(args, err); |
| 363 } | 399 } |
| 364 Dart_ExitScope(); | 400 Dart_ExitScope(); |
| 365 } | 401 } |
| 366 | 402 |
| 367 | 403 |
| 368 void FUNCTION_NAME(File_OpenStdio)(Dart_NativeArguments args) { | 404 void FUNCTION_NAME(File_OpenStdio)(Dart_NativeArguments args) { |
| 369 Dart_EnterScope(); | 405 Dart_EnterScope(); |
| 370 int fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); | 406 int fd = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 371 File* file = File::OpenStdio(fd); | 407 File* file = File::OpenStdio(fd); |
| 372 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file))); | 408 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file))); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 402 return reinterpret_cast<File*>(value.Value()); | 438 return reinterpret_cast<File*>(value.Value()); |
| 403 } | 439 } |
| 404 | 440 |
| 405 | 441 |
| 406 static CObject* FileExistsRequest(const CObjectArray& request) { | 442 static CObject* FileExistsRequest(const CObjectArray& request) { |
| 407 if (request.Length() == 2 && request[1]->IsString()) { | 443 if (request.Length() == 2 && request[1]->IsString()) { |
| 408 CObjectString filename(request[1]); | 444 CObjectString filename(request[1]); |
| 409 bool result = File::Exists(filename.CString()); | 445 bool result = File::Exists(filename.CString()); |
| 410 return CObject::Bool(result); | 446 return CObject::Bool(result); |
| 411 } | 447 } |
| 412 return CObject::False(); | 448 return CObject::IllegalArgumentError(); |
| 413 } | 449 } |
| 414 | 450 |
| 415 | 451 |
| 416 static CObject* FileCreateRequest(const CObjectArray& request) { | 452 static CObject* FileCreateRequest(const CObjectArray& request) { |
| 417 if (request.Length() == 2 && request[1]->IsString()) { | 453 if (request.Length() == 2 && request[1]->IsString()) { |
| 418 CObjectString filename(request[1]); | 454 CObjectString filename(request[1]); |
| 419 bool result = File::Create(filename.CString()); | 455 bool result = File::Create(filename.CString()); |
| 420 return CObject::Bool(result); | 456 if (result) { |
| 457 return CObject::True(); |
| 458 } else { |
| 459 return CObject::NewOSError(); |
| 460 } |
| 421 } | 461 } |
| 422 return CObject::False(); | 462 return CObject::IllegalArgumentError(); |
| 423 } | 463 } |
| 424 | 464 |
| 425 | |
| 426 static CObject* FileOpenRequest(const CObjectArray& request) { | 465 static CObject* FileOpenRequest(const CObjectArray& request) { |
| 427 File* file = NULL; | 466 File* file = NULL; |
| 428 if (request.Length() == 3 && | 467 if (request.Length() == 3 && |
| 429 request[1]->IsString() && | 468 request[1]->IsString() && |
| 430 request[2]->IsInt32()) { | 469 request[2]->IsInt32()) { |
| 431 CObjectString filename(request[1]); | 470 CObjectString filename(request[1]); |
| 432 CObjectInt32 mode(request[2]); | 471 CObjectInt32 mode(request[2]); |
| 433 File::DartFileOpenMode dart_file_mode = | 472 File::DartFileOpenMode dart_file_mode = |
| 434 static_cast<File::DartFileOpenMode>(mode.Value()); | 473 static_cast<File::DartFileOpenMode>(mode.Value()); |
| 435 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); | 474 File::FileOpenMode file_mode = File::DartModeToFileMode(dart_file_mode); |
| 436 if (((file_mode & File::kWrite) != 0) || File::Exists(filename.CString())) { | 475 file = File::Open(filename.CString(), file_mode); |
| 437 file = File::Open(filename.CString(), file_mode); | 476 if (file != NULL) { |
| 477 return new CObjectIntptr( |
| 478 CObject::NewIntptr(reinterpret_cast<intptr_t>(file))); |
| 479 } else { |
| 480 return CObject::NewOSError(); |
| 438 } | 481 } |
| 439 } | 482 } |
| 440 return new CObjectIntptr( | 483 return CObject::IllegalArgumentError(); |
| 441 CObject::NewIntptr(reinterpret_cast<intptr_t>(file))); | |
| 442 } | 484 } |
| 443 | 485 |
| 444 | 486 |
| 445 static CObject* FileDeleteRequest(const CObjectArray& request) { | 487 static CObject* FileDeleteRequest(const CObjectArray& request) { |
| 446 if (request.Length() == 2 && request[1]->IsString()) { | 488 if (request.Length() == 2 && request[1]->IsString()) { |
| 447 CObjectString filename(request[1]); | 489 CObjectString filename(request[1]); |
| 448 bool result = File::Delete(filename.CString()); | 490 bool result = File::Delete(filename.CString()); |
| 449 return CObject::Bool(result); | 491 if (result) { |
| 492 return CObject::True(); |
| 493 } else { |
| 494 return CObject::NewOSError(); |
| 495 } |
| 450 } | 496 } |
| 451 return CObject::False(); | 497 return CObject::False(); |
| 452 } | 498 } |
| 453 | 499 |
| 454 | 500 |
| 455 static CObject* FileFullPathRequest(const CObjectArray& request) { | 501 static CObject* FileFullPathRequest(const CObjectArray& request) { |
| 456 if (request.Length() == 2 && request[1]->IsString()) { | 502 if (request.Length() == 2 && request[1]->IsString()) { |
| 457 CObjectString filename(request[1]); | 503 CObjectString filename(request[1]); |
| 458 char* path = File::GetCanonicalPath(filename.CString()); | 504 char* result = File::GetCanonicalPath(filename.CString()); |
| 459 return new CObjectString(CObject::NewString(path)); | 505 if (result != NULL) { |
| 506 CObject* path = new CObjectString(CObject::NewString(result)); |
| 507 free(result); |
| 508 return path; |
| 509 } else { |
| 510 return CObject::NewOSError(); |
| 511 } |
| 460 } | 512 } |
| 461 return CObject::Null(); | 513 return CObject::IllegalArgumentError(); |
| 462 } | 514 } |
| 463 | 515 |
| 464 | 516 |
| 465 static CObject* FileDirectoryRequest(const CObjectArray& request) { | 517 static CObject* FileDirectoryRequest(const CObjectArray& request) { |
| 466 if (request.Length() == 2 && request[1]->IsString()) { | 518 if (request.Length() == 2 && request[1]->IsString()) { |
| 467 CObjectString filename(request[1]); | 519 CObjectString filename(request[1]); |
| 468 if (File::Exists(filename.CString())) { | 520 char* str_copy = strdup(filename.CString()); |
| 469 char* str_copy = strdup(filename.CString()); | 521 char* path = File::GetContainingDirectory(str_copy); |
| 470 char* path = File::GetContainingDirectory(str_copy); | 522 free(str_copy); |
| 471 free(str_copy); | 523 if (path != NULL) { |
| 472 if (path != NULL) { | 524 CObject* result = new CObjectString(CObject::NewString(path)); |
| 473 CObject* result = new CObjectString(CObject::NewString(path)); | 525 free(path); |
| 474 free(path); | 526 return result; |
| 475 return result; | 527 } else { |
| 476 } | 528 return CObject::NewOSError(); |
| 477 } | 529 } |
| 478 } | 530 } |
| 479 return CObject::Null(); | 531 return CObject::Null(); |
| 480 } | 532 } |
| 481 | 533 |
| 482 | 534 |
| 483 static CObject* FileCloseRequest(const CObjectArray& request) { | 535 static CObject* FileCloseRequest(const CObjectArray& request) { |
| 484 intptr_t return_value = -1; | 536 intptr_t return_value = -1; |
| 485 if (request.Length() == 2 && request[1]->IsIntptr()) { | 537 if (request.Length() == 2 && request[1]->IsIntptr()) { |
| 486 File* file = CObjectToFilePointer(request[1]); | 538 File* file = CObjectToFilePointer(request[1]); |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 Dart_EnterScope(); | 820 Dart_EnterScope(); |
| 769 Dart_SetReturnValue(args, Dart_Null()); | 821 Dart_SetReturnValue(args, Dart_Null()); |
| 770 Dart_Port service_port = File::GetServicePort(); | 822 Dart_Port service_port = File::GetServicePort(); |
| 771 if (service_port != kIllegalPort) { | 823 if (service_port != kIllegalPort) { |
| 772 // Return a send port for the service port. | 824 // Return a send port for the service port. |
| 773 Dart_Handle send_port = Dart_NewSendPort(service_port); | 825 Dart_Handle send_port = Dart_NewSendPort(service_port); |
| 774 Dart_SetReturnValue(args, send_port); | 826 Dart_SetReturnValue(args, send_port); |
| 775 } | 827 } |
| 776 Dart_ExitScope(); | 828 Dart_ExitScope(); |
| 777 } | 829 } |
| OLD | NEW |