| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <shellapi.h> | 8 #include <shellapi.h> |
| 9 #include <shlobj.h> | 9 #include <shlobj.h> |
| 10 #include <time.h> | 10 #include <time.h> |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 } | 161 } |
| 162 | 162 |
| 163 FilePath directory = from_path.Append(L"*.*"); | 163 FilePath directory = from_path.Append(L"*.*"); |
| 164 return ShellCopy(directory, to_path, false); | 164 return ShellCopy(directory, to_path, false); |
| 165 } | 165 } |
| 166 | 166 |
| 167 bool PathExists(const FilePath& path) { | 167 bool PathExists(const FilePath& path) { |
| 168 return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES); | 168 return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES); |
| 169 } | 169 } |
| 170 | 170 |
| 171 bool PathIsWritable(const std::wstring& path) { | 171 bool PathIsWritable(const FilePath& path) { |
| 172 HANDLE dir = | 172 HANDLE dir = |
| 173 CreateFile(path.c_str(), FILE_ADD_FILE, | 173 CreateFile(path.value().c_str(), FILE_ADD_FILE, |
| 174 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, | 174 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 175 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | 175 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 176 | 176 |
| 177 if (dir == INVALID_HANDLE_VALUE) | 177 if (dir == INVALID_HANDLE_VALUE) |
| 178 return false; | 178 return false; |
| 179 | 179 |
| 180 CloseHandle(dir); | 180 CloseHandle(dir); |
| 181 return true; | 181 return true; |
| 182 } | 182 } |
| 183 | 183 |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 return false; // fail! | 414 return false; // fail! |
| 415 | 415 |
| 416 DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH); | 416 DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH); |
| 417 if (path_len > MAX_PATH + 1 || path_len == 0) | 417 if (path_len > MAX_PATH + 1 || path_len == 0) |
| 418 return false; // fail! | 418 return false; // fail! |
| 419 | 419 |
| 420 temp_file->assign(temp_name, path_len); | 420 temp_file->assign(temp_name, path_len); |
| 421 return true; | 421 return true; |
| 422 } | 422 } |
| 423 | 423 |
| 424 bool CreateNewTempDirectory(const std::wstring& prefix, | 424 bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
| 425 std::wstring* new_temp_path) { | 425 FilePath* new_temp_path) { |
| 426 std::wstring system_temp_dir; | 426 FilePath system_temp_dir; |
| 427 if (!GetTempDir(&system_temp_dir)) | 427 if (!GetTempDir(&system_temp_dir)) |
| 428 return false; | 428 return false; |
| 429 | 429 |
| 430 std::wstring path_to_create; | 430 FilePath path_to_create; |
| 431 srand(static_cast<uint32>(time(NULL))); | 431 srand(static_cast<uint32>(time(NULL))); |
| 432 | 432 |
| 433 int count = 0; | 433 int count = 0; |
| 434 while (count < 50) { | 434 while (count < 50) { |
| 435 // Try create a new temporary directory with random generated name. If | 435 // Try create a new temporary directory with random generated name. If |
| 436 // the one exists, keep trying another path name until we reach some limit. | 436 // the one exists, keep trying another path name until we reach some limit. |
| 437 path_to_create.assign(system_temp_dir); | 437 path_to_create = system_temp_dir; |
| 438 std::wstring new_dir_name; | 438 std::wstring new_dir_name; |
| 439 new_dir_name.assign(prefix); | 439 new_dir_name.assign(prefix); |
| 440 new_dir_name.append(IntToWString(rand() % kint16max)); | 440 new_dir_name.append(IntToWString(rand() % kint16max)); |
| 441 file_util::AppendToPath(&path_to_create, new_dir_name); | 441 path_to_create = path_to_create.Append(new_dir_name); |
| 442 | 442 |
| 443 if (::CreateDirectory(path_to_create.c_str(), NULL)) | 443 if (::CreateDirectory(path_to_create.value().c_str(), NULL)) |
| 444 break; | 444 break; |
| 445 count++; | 445 count++; |
| 446 } | 446 } |
| 447 | 447 |
| 448 if (count == 50) { | 448 if (count == 50) { |
| 449 return false; | 449 return false; |
| 450 } | 450 } |
| 451 | 451 |
| 452 new_temp_path->assign(path_to_create); | 452 *new_temp_path = path_to_create; |
| 453 return true; | 453 return true; |
| 454 } | 454 } |
| 455 | 455 |
| 456 bool CreateDirectory(const FilePath& full_path) { | 456 bool CreateDirectory(const FilePath& full_path) { |
| 457 if (DirectoryExists(full_path)) | 457 if (DirectoryExists(full_path)) |
| 458 return true; | 458 return true; |
| 459 int err = SHCreateDirectoryEx(NULL, full_path.value().c_str(), NULL); | 459 int err = SHCreateDirectoryEx(NULL, full_path.value().c_str(), NULL); |
| 460 return err == ERROR_SUCCESS; | 460 return err == ERROR_SUCCESS; |
| 461 } | 461 } |
| 462 | 462 |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 749 void PathComponents(const std::wstring& path, | 749 void PathComponents(const std::wstring& path, |
| 750 std::vector<std::wstring>* components) { | 750 std::vector<std::wstring>* components) { |
| 751 PathComponents(FilePath(path), components); | 751 PathComponents(FilePath(path), components); |
| 752 } | 752 } |
| 753 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { | 753 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { |
| 754 FilePath path(*file_name); | 754 FilePath path(*file_name); |
| 755 ReplaceExtension(&path, extension); | 755 ReplaceExtension(&path, extension); |
| 756 file_name->assign(path.value()); | 756 file_name->assign(path.value()); |
| 757 } | 757 } |
| 758 } // namespace file_util | 758 } // namespace file_util |
| OLD | NEW |