| 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(HOST_OS_WINDOWS) | 6 #if defined(HOST_OS_WINDOWS) |
| 7 | 7 |
| 8 #include "bin/directory.h" | 8 #include "bin/directory.h" |
| 9 #include "bin/file.h" | |
| 10 #include "bin/utils.h" | |
| 11 #include "bin/utils_win.h" | |
| 12 | 9 |
| 13 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
| 14 #include <sys/stat.h> // NOLINT | 11 #include <sys/stat.h> // NOLINT |
| 15 | 12 |
| 16 #include "bin/dartutils.h" | 13 #include "bin/dartutils.h" |
| 14 #include "bin/file.h" |
| 17 #include "bin/log.h" | 15 #include "bin/log.h" |
| 16 #include "bin/namespace.h" |
| 17 #include "bin/utils.h" |
| 18 #include "bin/utils_win.h" |
| 18 | 19 |
| 19 #undef DeleteFile | 20 #undef DeleteFile |
| 20 | 21 |
| 21 #define MAX_LONG_PATH 32767 | 22 #define MAX_LONG_PATH 32767 |
| 22 | 23 |
| 23 namespace dart { | 24 namespace dart { |
| 24 namespace bin { | 25 namespace bin { |
| 25 | 26 |
| 26 PathBuffer::PathBuffer() : length_(0) { | 27 PathBuffer::PathBuffer() : length_(0) { |
| 27 data_ = calloc(MAX_LONG_PATH + 1, sizeof(wchar_t)); // NOLINT | 28 data_ = calloc(MAX_LONG_PATH + 1, sizeof(wchar_t)); // NOLINT |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 // reasons such as lack of permissions. In that case we do | 338 // reasons such as lack of permissions. In that case we do |
| 338 // not know if the directory exists. | 339 // not know if the directory exists. |
| 339 return Directory::UNKNOWN; | 340 return Directory::UNKNOWN; |
| 340 } | 341 } |
| 341 } | 342 } |
| 342 bool exists = (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; | 343 bool exists = (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| 343 exists = exists && !IsBrokenLink(dir_name); | 344 exists = exists && !IsBrokenLink(dir_name); |
| 344 return exists ? Directory::EXISTS : Directory::DOES_NOT_EXIST; | 345 return exists ? Directory::EXISTS : Directory::DOES_NOT_EXIST; |
| 345 } | 346 } |
| 346 | 347 |
| 347 Directory::ExistsResult Directory::Exists(const char* dir_name) { | 348 Directory::ExistsResult Directory::Exists(Namespace* namespc, |
| 349 const char* dir_name) { |
| 348 Utf8ToWideScope system_name(dir_name); | 350 Utf8ToWideScope system_name(dir_name); |
| 349 return ExistsHelper(system_name.wide()); | 351 return ExistsHelper(system_name.wide()); |
| 350 } | 352 } |
| 351 | 353 |
| 352 char* Directory::CurrentNoScope() { | 354 char* Directory::CurrentNoScope() { |
| 353 int length = GetCurrentDirectoryW(0, NULL); | 355 int length = GetCurrentDirectoryW(0, NULL); |
| 354 if (length == 0) { | 356 if (length == 0) { |
| 355 return NULL; | 357 return NULL; |
| 356 } | 358 } |
| 357 wchar_t* current = new wchar_t[length + 1]; | 359 wchar_t* current = new wchar_t[length + 1]; |
| 358 GetCurrentDirectoryW(length + 1, current); | 360 GetCurrentDirectoryW(length + 1, current); |
| 359 int utf8_len = | 361 int utf8_len = |
| 360 WideCharToMultiByte(CP_UTF8, 0, current, -1, NULL, 0, NULL, NULL); | 362 WideCharToMultiByte(CP_UTF8, 0, current, -1, NULL, 0, NULL, NULL); |
| 361 char* result = reinterpret_cast<char*>(malloc(utf8_len)); | 363 char* result = reinterpret_cast<char*>(malloc(utf8_len)); |
| 362 WideCharToMultiByte(CP_UTF8, 0, current, -1, result, utf8_len, NULL, NULL); | 364 WideCharToMultiByte(CP_UTF8, 0, current, -1, result, utf8_len, NULL, NULL); |
| 363 delete[] current; | 365 delete[] current; |
| 364 return result; | 366 return result; |
| 365 } | 367 } |
| 366 | 368 |
| 367 const char* Directory::Current() { | 369 bool Directory::Create(Namespace* namespc, const char* dir_name) { |
| 368 int length = GetCurrentDirectoryW(0, NULL); | |
| 369 if (length == 0) { | |
| 370 return NULL; | |
| 371 } | |
| 372 wchar_t* current; | |
| 373 current = reinterpret_cast<wchar_t*>( | |
| 374 Dart_ScopeAllocate((length + 1) * sizeof(*current))); | |
| 375 GetCurrentDirectoryW(length + 1, current); | |
| 376 return StringUtilsWin::WideToUtf8(current); | |
| 377 } | |
| 378 | |
| 379 bool Directory::SetCurrent(const char* path) { | |
| 380 Utf8ToWideScope system_path(path); | |
| 381 bool result = SetCurrentDirectoryW(system_path.wide()) != 0; | |
| 382 return result; | |
| 383 } | |
| 384 | |
| 385 bool Directory::Create(const char* dir_name) { | |
| 386 Utf8ToWideScope system_name(dir_name); | 370 Utf8ToWideScope system_name(dir_name); |
| 387 int create_status = CreateDirectoryW(system_name.wide(), NULL); | 371 int create_status = CreateDirectoryW(system_name.wide(), NULL); |
| 388 // If the directory already existed, treat it as a success. | 372 // If the directory already existed, treat it as a success. |
| 389 if ((create_status == 0) && (GetLastError() == ERROR_ALREADY_EXISTS) && | 373 if ((create_status == 0) && (GetLastError() == ERROR_ALREADY_EXISTS) && |
| 390 (ExistsHelper(system_name.wide()) == EXISTS)) { | 374 (ExistsHelper(system_name.wide()) == EXISTS)) { |
| 391 return true; | 375 return true; |
| 392 } | 376 } |
| 393 return (create_status != 0); | 377 return (create_status != 0); |
| 394 } | 378 } |
| 395 | 379 |
| 396 const char* Directory::SystemTemp() { | 380 const char* Directory::SystemTemp(Namespace* namespc) { |
| 397 PathBuffer path; | 381 PathBuffer path; |
| 398 // Remove \ at end. | 382 // Remove \ at end. |
| 399 path.Reset(GetTempPathW(MAX_LONG_PATH, path.AsStringW()) - 1); | 383 path.Reset(GetTempPathW(MAX_LONG_PATH, path.AsStringW()) - 1); |
| 400 return path.AsScopedString(); | 384 return path.AsScopedString(); |
| 401 } | 385 } |
| 402 | 386 |
| 403 const char* Directory::CreateTemp(const char* prefix) { | 387 const char* Directory::CreateTemp(Namespace* namespc, const char* prefix) { |
| 404 // Returns a new, unused directory name, adding characters to the | 388 // Returns a new, unused directory name, adding characters to the |
| 405 // end of prefix. | 389 // end of prefix. |
| 406 // Creates this directory, with a default security | 390 // Creates this directory, with a default security |
| 407 // descriptor inherited from its parent directory. | 391 // descriptor inherited from its parent directory. |
| 408 // The return value is Dart_ScopeAllocated. | 392 // The return value is Dart_ScopeAllocated. |
| 409 PathBuffer path; | 393 PathBuffer path; |
| 410 Utf8ToWideScope system_prefix(prefix); | 394 Utf8ToWideScope system_prefix(prefix); |
| 411 if (!path.AddW(system_prefix.wide())) { | 395 if (!path.AddW(system_prefix.wide())) { |
| 412 return NULL; | 396 return NULL; |
| 413 } | 397 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 432 if (!path.AddW(reinterpret_cast<wchar_t*>(uuid_string))) { | 416 if (!path.AddW(reinterpret_cast<wchar_t*>(uuid_string))) { |
| 433 return NULL; | 417 return NULL; |
| 434 } | 418 } |
| 435 RpcStringFreeW(&uuid_string); | 419 RpcStringFreeW(&uuid_string); |
| 436 if (!CreateDirectoryW(path.AsStringW(), NULL)) { | 420 if (!CreateDirectoryW(path.AsStringW(), NULL)) { |
| 437 return NULL; | 421 return NULL; |
| 438 } | 422 } |
| 439 return path.AsScopedString(); | 423 return path.AsScopedString(); |
| 440 } | 424 } |
| 441 | 425 |
| 442 bool Directory::Delete(const char* dir_name, bool recursive) { | 426 bool Directory::Delete(Namespace* namespc, |
| 427 const char* dir_name, |
| 428 bool recursive) { |
| 443 bool result = false; | 429 bool result = false; |
| 444 Utf8ToWideScope system_dir_name(dir_name); | 430 Utf8ToWideScope system_dir_name(dir_name); |
| 445 if (!recursive) { | 431 if (!recursive) { |
| 446 if (File::GetType(dir_name, true) == File::kIsDirectory) { | 432 if (File::GetType(namespc, dir_name, true) == File::kIsDirectory) { |
| 447 result = (RemoveDirectoryW(system_dir_name.wide()) != 0); | 433 result = (RemoveDirectoryW(system_dir_name.wide()) != 0); |
| 448 } else { | 434 } else { |
| 449 SetLastError(ERROR_FILE_NOT_FOUND); | 435 SetLastError(ERROR_FILE_NOT_FOUND); |
| 450 } | 436 } |
| 451 } else { | 437 } else { |
| 452 PathBuffer path; | 438 PathBuffer path; |
| 453 if (path.AddW(system_dir_name.wide())) { | 439 if (path.AddW(system_dir_name.wide())) { |
| 454 result = DeleteRecursively(&path); | 440 result = DeleteRecursively(&path); |
| 455 } | 441 } |
| 456 } | 442 } |
| 457 return result; | 443 return result; |
| 458 } | 444 } |
| 459 | 445 |
| 460 bool Directory::Rename(const char* path, const char* new_path) { | 446 bool Directory::Rename(Namespace* namespc, |
| 447 const char* path, |
| 448 const char* new_path) { |
| 461 Utf8ToWideScope system_path(path); | 449 Utf8ToWideScope system_path(path); |
| 462 Utf8ToWideScope system_new_path(new_path); | 450 Utf8ToWideScope system_new_path(new_path); |
| 463 ExistsResult exists = ExistsHelper(system_path.wide()); | 451 ExistsResult exists = ExistsHelper(system_path.wide()); |
| 464 if (exists != EXISTS) { | 452 if (exists != EXISTS) { |
| 465 return false; | 453 return false; |
| 466 } | 454 } |
| 467 ExistsResult new_exists = ExistsHelper(system_new_path.wide()); | 455 ExistsResult new_exists = ExistsHelper(system_new_path.wide()); |
| 468 // MoveFile does not allow replacing existing directories. Therefore, | 456 // MoveFile does not allow replacing existing directories. Therefore, |
| 469 // if the new_path is currently a directory we need to delete it | 457 // if the new_path is currently a directory we need to delete it |
| 470 // first. | 458 // first. |
| 471 if (new_exists == EXISTS) { | 459 if (new_exists == EXISTS) { |
| 472 bool success = Delete(new_path, true); | 460 bool success = Delete(namespc, new_path, true); |
| 473 if (!success) { | 461 if (!success) { |
| 474 return false; | 462 return false; |
| 475 } | 463 } |
| 476 } | 464 } |
| 477 DWORD flags = MOVEFILE_WRITE_THROUGH; | 465 DWORD flags = MOVEFILE_WRITE_THROUGH; |
| 478 int move_status = | 466 int move_status = |
| 479 MoveFileExW(system_path.wide(), system_new_path.wide(), flags); | 467 MoveFileExW(system_path.wide(), system_new_path.wide(), flags); |
| 480 return (move_status != 0); | 468 return (move_status != 0); |
| 481 } | 469 } |
| 482 | 470 |
| 483 } // namespace bin | 471 } // namespace bin |
| 484 } // namespace dart | 472 } // namespace dart |
| 485 | 473 |
| 486 #endif // defined(HOST_OS_WINDOWS) | 474 #endif // defined(HOST_OS_WINDOWS) |
| OLD | NEW |