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 int stat_status = _wstat(system_name, &st); | 474 HANDLE file_handle = CreateFileW( |
475 if (stat_status != 0) { | 475 system_name, |
476 SetLastError(ERROR_FILE_NOT_FOUND); | 476 0, |
| 477 FILE_SHARE_READ, |
| 478 NULL, |
| 479 OPEN_EXISTING, |
| 480 FILE_FLAG_BACKUP_SEMANTICS, |
| 481 NULL); |
| 482 if (file_handle == INVALID_HANDLE_VALUE) { |
477 free(const_cast<wchar_t*>(system_name)); | 483 free(const_cast<wchar_t*>(system_name)); |
478 return NULL; | 484 return NULL; |
479 } | 485 } |
480 int required_size = GetFullPathNameW(system_name, 0, NULL, NULL); | 486 wchar_t dummy_buffer[1]; |
| 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 } |
481 wchar_t* path = | 498 wchar_t* path = |
482 static_cast<wchar_t*>(malloc(required_size * sizeof(wchar_t))); | 499 static_cast<wchar_t*>(malloc(required_size * sizeof(wchar_t))); |
483 int written = GetFullPathNameW(system_name, required_size, path, NULL); | 500 int result_size = GetFinalPathNameByHandle(file_handle, |
| 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 } |
484 free(const_cast<wchar_t*>(system_name)); | 515 free(const_cast<wchar_t*>(system_name)); |
485 ASSERT(written <= (required_size - 1)); | |
486 char* result = StringUtils::WideToUtf8(path); | |
487 free(path); | 516 free(path); |
| 517 CloseHandle(file_handle); |
488 return result; | 518 return result; |
489 } | 519 } |
490 | 520 |
491 | 521 |
492 const char* File::PathSeparator() { | 522 const char* File::PathSeparator() { |
493 // This is already UTF-8 encoded. | 523 // This is already UTF-8 encoded. |
494 return "\\"; | 524 return "\\"; |
495 } | 525 } |
496 | 526 |
497 | 527 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 return kIdentical; | 606 return kIdentical; |
577 } else { | 607 } else { |
578 return kDifferent; | 608 return kDifferent; |
579 } | 609 } |
580 } | 610 } |
581 | 611 |
582 } // namespace bin | 612 } // namespace bin |
583 } // namespace dart | 613 } // namespace dart |
584 | 614 |
585 #endif // defined(TARGET_OS_WINDOWS) | 615 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |