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 "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
7 | 7 |
8 #include "bin/file.h" | 8 #include "bin/file.h" |
9 | 9 |
10 #include <fcntl.h> // NOLINT | 10 #include <fcntl.h> // NOLINT |
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 if (pathname == NULL) return false; | 464 if (pathname == NULL) return false; |
465 return (strlen(pathname) > 2) && | 465 return (strlen(pathname) > 2) && |
466 (pathname[1] == ':') && | 466 (pathname[1] == ':') && |
467 (pathname[2] == '\\' || pathname[2] == '/'); | 467 (pathname[2] == '\\' || pathname[2] == '/'); |
468 } | 468 } |
469 | 469 |
470 | 470 |
471 char* File::GetCanonicalPath(const char* pathname) { | 471 char* File::GetCanonicalPath(const char* pathname) { |
472 struct _stat st; | 472 struct _stat st; |
473 const wchar_t* system_name = StringUtils::Utf8ToWide(pathname); | 473 const wchar_t* system_name = StringUtils::Utf8ToWide(pathname); |
474 HANDLE file_handle = CreateFileW( | 474 int stat_status = _wstat(system_name, &st); |
475 system_name, | 475 if (stat_status != 0) { |
476 0, | 476 SetLastError(ERROR_FILE_NOT_FOUND); |
477 FILE_SHARE_READ, | |
478 NULL, | |
479 OPEN_EXISTING, | |
480 FILE_FLAG_BACKUP_SEMANTICS, | |
481 NULL); | |
482 if (file_handle == INVALID_HANDLE_VALUE) { | |
483 free(const_cast<wchar_t*>(system_name)); | 477 free(const_cast<wchar_t*>(system_name)); |
484 return NULL; | 478 return NULL; |
485 } | 479 } |
486 wchar_t dummy_buffer[1]; | 480 int required_size = GetFullPathNameW(system_name, 0, NULL, NULL); |
487 int required_size = GetFinalPathNameByHandle(file_handle, | |
488 dummy_buffer, | |
489 0, | |
490 VOLUME_NAME_DOS); | |
491 if (required_size == 0) { | |
492 free(const_cast<wchar_t*>(system_name)); | |
493 DWORD error = GetLastError(); | |
494 CloseHandle(file_handle); | |
495 SetLastError(error); | |
496 return NULL; | |
497 } | |
498 wchar_t* path = | 481 wchar_t* path = |
499 static_cast<wchar_t*>(malloc(required_size * sizeof(wchar_t))); | 482 static_cast<wchar_t*>(malloc(required_size * sizeof(wchar_t))); |
500 int result_size = GetFinalPathNameByHandle(file_handle, | 483 int written = GetFullPathNameW(system_name, required_size, path, NULL); |
501 path, | |
502 required_size, | |
503 VOLUME_NAME_DOS); | |
504 ASSERT(result_size == required_size - 1); | |
505 // Remove leading \\?\ if possible, unless input used it. | |
506 char* result; | |
507 if (result_size < MAX_PATH - 1 + 4 && | |
508 result_size > 4 && | |
509 wcsncmp(path, L"\\\\?\\", 4) == 0 && | |
510 wcsncmp(system_name, L"\\\\?\\", 4) != 0) { | |
511 result = StringUtils::WideToUtf8(path + 4); | |
512 } else { | |
513 result = StringUtils::WideToUtf8(path); | |
514 } | |
515 free(const_cast<wchar_t*>(system_name)); | 484 free(const_cast<wchar_t*>(system_name)); |
| 485 ASSERT(written <= (required_size - 1)); |
| 486 char* result = StringUtils::WideToUtf8(path); |
516 free(path); | 487 free(path); |
517 CloseHandle(file_handle); | |
518 return result; | 488 return result; |
519 } | 489 } |
520 | 490 |
521 | 491 |
522 const char* File::PathSeparator() { | 492 const char* File::PathSeparator() { |
523 // This is already UTF-8 encoded. | 493 // This is already UTF-8 encoded. |
524 return "\\"; | 494 return "\\"; |
525 } | 495 } |
526 | 496 |
527 | 497 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 return kIdentical; | 576 return kIdentical; |
607 } else { | 577 } else { |
608 return kDifferent; | 578 return kDifferent; |
609 } | 579 } |
610 } | 580 } |
611 | 581 |
612 } // namespace bin | 582 } // namespace bin |
613 } // namespace dart | 583 } // namespace dart |
614 | 584 |
615 #endif // defined(TARGET_OS_WINDOWS) | 585 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |