| 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 wchar_t double_terminated_path[MAX_PATH + 1] = {0}; | 81 wchar_t double_terminated_path[MAX_PATH + 1] = {0}; |
| 82 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation | 82 #pragma warning(suppress:4996) // don't complain about wcscpy deprecation |
| 83 wcscpy(double_terminated_path, path.value().c_str()); | 83 wcscpy(double_terminated_path, path.value().c_str()); |
| 84 | 84 |
| 85 SHFILEOPSTRUCT file_operation = {0}; | 85 SHFILEOPSTRUCT file_operation = {0}; |
| 86 file_operation.wFunc = FO_DELETE; | 86 file_operation.wFunc = FO_DELETE; |
| 87 file_operation.pFrom = double_terminated_path; | 87 file_operation.pFrom = double_terminated_path; |
| 88 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION; | 88 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION; |
| 89 if (!recursive) | 89 if (!recursive) |
| 90 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; | 90 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; |
| 91 return (SHFileOperation(&file_operation) == 0); | 91 int err = SHFileOperation(&file_operation); |
| 92 // Some versions of Windows return ERROR_FILE_NOT_FOUND when |
| 93 // deleting an empty directory. |
| 94 return (err == 0 || err == ERROR_FILE_NOT_FOUND); |
| 92 } | 95 } |
| 93 | 96 |
| 94 bool Move(const FilePath& from_path, const FilePath& to_path) { | 97 bool Move(const FilePath& from_path, const FilePath& to_path) { |
| 95 // NOTE: I suspect we could support longer paths, but that would involve | 98 // NOTE: I suspect we could support longer paths, but that would involve |
| 96 // analyzing all our usage of files. | 99 // analyzing all our usage of files. |
| 97 if (from_path.value().length() >= MAX_PATH || | 100 if (from_path.value().length() >= MAX_PATH || |
| 98 to_path.value().length() >= MAX_PATH) { | 101 to_path.value().length() >= MAX_PATH) { |
| 99 return false; | 102 return false; |
| 100 } | 103 } |
| 101 return (MoveFileEx(from_path.value().c_str(), to_path.value().c_str(), | 104 return (MoveFileEx(from_path.value().c_str(), to_path.value().c_str(), |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 std::wstring temp_path, temp_file; | 399 std::wstring temp_path, temp_file; |
| 397 | 400 |
| 398 if (!GetTempDir(&temp_path)) | 401 if (!GetTempDir(&temp_path)) |
| 399 return false; | 402 return false; |
| 400 | 403 |
| 401 if (CreateTemporaryFileNameInDir(temp_path, &temp_file)) { | 404 if (CreateTemporaryFileNameInDir(temp_path, &temp_file)) { |
| 402 *path = FilePath(temp_file); | 405 *path = FilePath(temp_file); |
| 403 return true; | 406 return true; |
| 404 } | 407 } |
| 405 | 408 |
| 406 return false; | 409 return false; |
| 407 } | 410 } |
| 408 | 411 |
| 409 bool CreateTemporaryFileNameInDir(const std::wstring& dir, | 412 bool CreateTemporaryFileNameInDir(const std::wstring& dir, |
| 410 std::wstring* temp_file) { | 413 std::wstring* temp_file) { |
| 411 wchar_t temp_name[MAX_PATH + 1]; | 414 wchar_t temp_name[MAX_PATH + 1]; |
| 412 | 415 |
| 413 if (!GetTempFileName(dir.c_str(), L"", 0, temp_name)) | 416 if (!GetTempFileName(dir.c_str(), L"", 0, temp_name)) |
| 414 return false; // fail! | 417 return false; // fail! |
| 415 | 418 |
| 416 DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH); | 419 DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH); |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 target.append(1, L'\0'); | 562 target.append(1, L'\0'); |
| 560 | 563 |
| 561 SHFILEOPSTRUCT move_info = {0}; | 564 SHFILEOPSTRUCT move_info = {0}; |
| 562 move_info.wFunc = FO_MOVE; | 565 move_info.wFunc = FO_MOVE; |
| 563 move_info.pFrom = source.c_str(); | 566 move_info.pFrom = source.c_str(); |
| 564 move_info.pTo = target.c_str(); | 567 move_info.pTo = target.c_str(); |
| 565 move_info.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | | 568 move_info.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | |
| 566 FOF_NOCONFIRMMKDIR | FOF_NOCOPYSECURITYATTRIBS; | 569 FOF_NOCONFIRMMKDIR | FOF_NOCOPYSECURITYATTRIBS; |
| 567 | 570 |
| 568 if (0 != SHFileOperation(&move_info)) | 571 if (0 != SHFileOperation(&move_info)) |
| 569 return false; | 572 return false; |
| 570 | 573 |
| 571 return true; | 574 return true; |
| 572 } | 575 } |
| 573 | 576 |
| 574 // Gets the current working directory for the process. | 577 // Gets the current working directory for the process. |
| 575 bool GetCurrentDirectory(FilePath* dir) { | 578 bool GetCurrentDirectory(FilePath* dir) { |
| 576 wchar_t system_buffer[MAX_PATH]; | 579 wchar_t system_buffer[MAX_PATH]; |
| 577 system_buffer[0] = 0; | 580 system_buffer[0] = 0; |
| 578 DWORD len = ::GetCurrentDirectory(MAX_PATH, system_buffer); | 581 DWORD len = ::GetCurrentDirectory(MAX_PATH, system_buffer); |
| 579 if (len == 0 || len > MAX_PATH) | 582 if (len == 0 || len > MAX_PATH) |
| 580 return false; | 583 return false; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 748 void PathComponents(const std::wstring& path, | 751 void PathComponents(const std::wstring& path, |
| 749 std::vector<std::wstring>* components) { | 752 std::vector<std::wstring>* components) { |
| 750 PathComponents(FilePath(path), components); | 753 PathComponents(FilePath(path), components); |
| 751 } | 754 } |
| 752 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { | 755 void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) { |
| 753 FilePath path(*file_name); | 756 FilePath path(*file_name); |
| 754 ReplaceExtension(&path, extension); | 757 ReplaceExtension(&path, extension); |
| 755 file_name->assign(path.value()); | 758 file_name->assign(path.value()); |
| 756 } | 759 } |
| 757 } // namespace file_util | 760 } // namespace file_util |
| OLD | NEW |