| 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/directory.h" | 8 #include "bin/directory.h" |
| 9 #include "bin/file.h" | 9 #include "bin/file.h" |
| 10 #include "bin/utils.h" | 10 #include "bin/utils.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 return reinterpret_cast<wchar_t*>(data_); | 43 return reinterpret_cast<wchar_t*>(data_); |
| 44 } | 44 } |
| 45 | 45 |
| 46 | 46 |
| 47 const char* PathBuffer::AsScopedString() const { | 47 const char* PathBuffer::AsScopedString() const { |
| 48 return StringUtilsWin::WideToUtf8(AsStringW()); | 48 return StringUtilsWin::WideToUtf8(AsStringW()); |
| 49 } | 49 } |
| 50 | 50 |
| 51 | 51 |
| 52 bool PathBuffer::Add(const char* name) { | 52 bool PathBuffer::Add(const char* name) { |
| 53 const wchar_t* wide_name = StringUtilsWin::Utf8ToWide(name); | 53 Utf8ToWideScope wide_name(name); |
| 54 return AddW(wide_name); | 54 return AddW(wide_name.wide()); |
| 55 } | 55 } |
| 56 | 56 |
| 57 | 57 |
| 58 bool PathBuffer::AddW(const wchar_t* name) { | 58 bool PathBuffer::AddW(const wchar_t* name) { |
| 59 wchar_t* data = AsStringW(); | 59 wchar_t* data = AsStringW(); |
| 60 int written = _snwprintf(data + length_, | 60 int written = _snwprintf(data + length_, |
| 61 MAX_LONG_PATH - length_, | 61 MAX_LONG_PATH - length_, |
| 62 L"%s", | 62 L"%s", |
| 63 name); | 63 name); |
| 64 data[MAX_LONG_PATH] = L'\0'; | 64 data[MAX_LONG_PATH] = L'\0'; |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 return Directory::UNKNOWN; | 370 return Directory::UNKNOWN; |
| 371 } | 371 } |
| 372 } | 372 } |
| 373 bool exists = (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; | 373 bool exists = (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| 374 exists = exists && !IsBrokenLink(dir_name); | 374 exists = exists && !IsBrokenLink(dir_name); |
| 375 return exists ? Directory::EXISTS : Directory::DOES_NOT_EXIST; | 375 return exists ? Directory::EXISTS : Directory::DOES_NOT_EXIST; |
| 376 } | 376 } |
| 377 | 377 |
| 378 | 378 |
| 379 Directory::ExistsResult Directory::Exists(const char* dir_name) { | 379 Directory::ExistsResult Directory::Exists(const char* dir_name) { |
| 380 const wchar_t* system_name = StringUtilsWin::Utf8ToWide(dir_name); | 380 Utf8ToWideScope system_name(dir_name); |
| 381 return ExistsHelper(system_name); | 381 return ExistsHelper(system_name.wide()); |
| 382 } | 382 } |
| 383 | 383 |
| 384 | 384 |
| 385 char* Directory::CurrentNoScope() { | 385 char* Directory::CurrentNoScope() { |
| 386 int length = GetCurrentDirectoryW(0, NULL); | 386 int length = GetCurrentDirectoryW(0, NULL); |
| 387 if (length == 0) { | 387 if (length == 0) { |
| 388 return NULL; | 388 return NULL; |
| 389 } | 389 } |
| 390 wchar_t* current = new wchar_t[length + 1]; | 390 wchar_t* current = new wchar_t[length + 1]; |
| 391 GetCurrentDirectoryW(length + 1, current); | 391 GetCurrentDirectoryW(length + 1, current); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 405 } | 405 } |
| 406 wchar_t* current; | 406 wchar_t* current; |
| 407 current = reinterpret_cast<wchar_t*>( | 407 current = reinterpret_cast<wchar_t*>( |
| 408 Dart_ScopeAllocate((length + 1) * sizeof(*current))); | 408 Dart_ScopeAllocate((length + 1) * sizeof(*current))); |
| 409 GetCurrentDirectoryW(length + 1, current); | 409 GetCurrentDirectoryW(length + 1, current); |
| 410 return StringUtilsWin::WideToUtf8(current); | 410 return StringUtilsWin::WideToUtf8(current); |
| 411 } | 411 } |
| 412 | 412 |
| 413 | 413 |
| 414 bool Directory::SetCurrent(const char* path) { | 414 bool Directory::SetCurrent(const char* path) { |
| 415 const wchar_t* system_path = StringUtilsWin::Utf8ToWide(path); | 415 Utf8ToWideScope system_path(path); |
| 416 bool result = SetCurrentDirectoryW(system_path) != 0; | 416 bool result = SetCurrentDirectoryW(system_path.wide()) != 0; |
| 417 return result; | 417 return result; |
| 418 } | 418 } |
| 419 | 419 |
| 420 | 420 |
| 421 bool Directory::Create(const char* dir_name) { | 421 bool Directory::Create(const char* dir_name) { |
| 422 const wchar_t* system_name = StringUtilsWin::Utf8ToWide(dir_name); | 422 Utf8ToWideScope system_name(dir_name); |
| 423 int create_status = CreateDirectoryW(system_name, NULL); | 423 int create_status = CreateDirectoryW(system_name.wide(), NULL); |
| 424 // If the directory already existed, treat it as a success. | 424 // If the directory already existed, treat it as a success. |
| 425 if ((create_status == 0) && | 425 if ((create_status == 0) && |
| 426 (GetLastError() == ERROR_ALREADY_EXISTS) && | 426 (GetLastError() == ERROR_ALREADY_EXISTS) && |
| 427 (ExistsHelper(system_name) == EXISTS)) { | 427 (ExistsHelper(system_name.wide()) == EXISTS)) { |
| 428 return true; | 428 return true; |
| 429 } | 429 } |
| 430 return (create_status != 0); | 430 return (create_status != 0); |
| 431 } | 431 } |
| 432 | 432 |
| 433 | 433 |
| 434 const char* Directory::SystemTemp() { | 434 const char* Directory::SystemTemp() { |
| 435 PathBuffer path; | 435 PathBuffer path; |
| 436 // Remove \ at end. | 436 // Remove \ at end. |
| 437 path.Reset(GetTempPathW(MAX_LONG_PATH, path.AsStringW()) - 1); | 437 path.Reset(GetTempPathW(MAX_LONG_PATH, path.AsStringW()) - 1); |
| 438 return path.AsScopedString(); | 438 return path.AsScopedString(); |
| 439 } | 439 } |
| 440 | 440 |
| 441 | 441 |
| 442 const char* Directory::CreateTemp(const char* prefix) { | 442 const char* Directory::CreateTemp(const char* prefix) { |
| 443 // Returns a new, unused directory name, adding characters to the | 443 // Returns a new, unused directory name, adding characters to the |
| 444 // end of prefix. | 444 // end of prefix. |
| 445 // Creates this directory, with a default security | 445 // Creates this directory, with a default security |
| 446 // descriptor inherited from its parent directory. | 446 // descriptor inherited from its parent directory. |
| 447 // The return value is Dart_ScopeAllocated. | 447 // The return value is Dart_ScopeAllocated. |
| 448 PathBuffer path; | 448 PathBuffer path; |
| 449 const wchar_t* system_prefix = StringUtilsWin::Utf8ToWide(prefix); | 449 Utf8ToWideScope system_prefix(prefix); |
| 450 if (!path.AddW(system_prefix)) { | 450 if (!path.AddW(system_prefix.wide())) { |
| 451 return NULL; | 451 return NULL; |
| 452 } | 452 } |
| 453 | 453 |
| 454 // Length of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is 36. | 454 // Length of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is 36. |
| 455 if (path.length() > MAX_LONG_PATH - 36) { | 455 if (path.length() > MAX_LONG_PATH - 36) { |
| 456 return NULL; | 456 return NULL; |
| 457 } | 457 } |
| 458 | 458 |
| 459 UUID uuid; | 459 UUID uuid; |
| 460 RPC_STATUS status = UuidCreateSequential(&uuid); | 460 RPC_STATUS status = UuidCreateSequential(&uuid); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 474 RpcStringFreeW(&uuid_string); | 474 RpcStringFreeW(&uuid_string); |
| 475 if (!CreateDirectoryW(path.AsStringW(), NULL)) { | 475 if (!CreateDirectoryW(path.AsStringW(), NULL)) { |
| 476 return NULL; | 476 return NULL; |
| 477 } | 477 } |
| 478 return path.AsScopedString(); | 478 return path.AsScopedString(); |
| 479 } | 479 } |
| 480 | 480 |
| 481 | 481 |
| 482 bool Directory::Delete(const char* dir_name, bool recursive) { | 482 bool Directory::Delete(const char* dir_name, bool recursive) { |
| 483 bool result = false; | 483 bool result = false; |
| 484 const wchar_t* system_dir_name = StringUtilsWin::Utf8ToWide(dir_name); | 484 Utf8ToWideScope system_dir_name(dir_name); |
| 485 if (!recursive) { | 485 if (!recursive) { |
| 486 if (File::GetType(dir_name, true) == File::kIsDirectory) { | 486 if (File::GetType(dir_name, true) == File::kIsDirectory) { |
| 487 result = (RemoveDirectoryW(system_dir_name) != 0); | 487 result = (RemoveDirectoryW(system_dir_name.wide()) != 0); |
| 488 } else { | 488 } else { |
| 489 SetLastError(ERROR_FILE_NOT_FOUND); | 489 SetLastError(ERROR_FILE_NOT_FOUND); |
| 490 } | 490 } |
| 491 } else { | 491 } else { |
| 492 PathBuffer path; | 492 PathBuffer path; |
| 493 if (path.AddW(system_dir_name)) { | 493 if (path.AddW(system_dir_name.wide())) { |
| 494 result = DeleteRecursively(&path); | 494 result = DeleteRecursively(&path); |
| 495 } | 495 } |
| 496 } | 496 } |
| 497 return result; | 497 return result; |
| 498 } | 498 } |
| 499 | 499 |
| 500 | 500 |
| 501 bool Directory::Rename(const char* path, const char* new_path) { | 501 bool Directory::Rename(const char* path, const char* new_path) { |
| 502 const wchar_t* system_path = StringUtilsWin::Utf8ToWide(path); | 502 Utf8ToWideScope system_path(path); |
| 503 const wchar_t* system_new_path = StringUtilsWin::Utf8ToWide(new_path); | 503 Utf8ToWideScope system_new_path(new_path); |
| 504 ExistsResult exists = ExistsHelper(system_path); | 504 ExistsResult exists = ExistsHelper(system_path.wide()); |
| 505 if (exists != EXISTS) { | 505 if (exists != EXISTS) { |
| 506 return false; | 506 return false; |
| 507 } | 507 } |
| 508 ExistsResult new_exists = ExistsHelper(system_new_path); | 508 ExistsResult new_exists = ExistsHelper(system_new_path.wide()); |
| 509 // MoveFile does not allow replacing exising directories. Therefore, | 509 // MoveFile does not allow replacing exising directories. Therefore, |
| 510 // if the new_path is currently a directory we need to delete it | 510 // if the new_path is currently a directory we need to delete it |
| 511 // first. | 511 // first. |
| 512 if (new_exists == EXISTS) { | 512 if (new_exists == EXISTS) { |
| 513 bool success = Delete(new_path, true); | 513 bool success = Delete(new_path, true); |
| 514 if (!success) { | 514 if (!success) { |
| 515 return false; | 515 return false; |
| 516 } | 516 } |
| 517 } | 517 } |
| 518 DWORD flags = MOVEFILE_WRITE_THROUGH; | 518 DWORD flags = MOVEFILE_WRITE_THROUGH; |
| 519 int move_status = | 519 int move_status = |
| 520 MoveFileExW(system_path, system_new_path, flags); | 520 MoveFileExW(system_path.wide(), system_new_path.wide(), flags); |
| 521 return (move_status != 0); | 521 return (move_status != 0); |
| 522 } | 522 } |
| 523 | 523 |
| 524 } // namespace bin | 524 } // namespace bin |
| 525 } // namespace dart | 525 } // namespace dart |
| 526 | 526 |
| 527 #endif // defined(TARGET_OS_WINDOWS) | 527 #endif // defined(TARGET_OS_WINDOWS) |
| OLD | NEW |