| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <psapi.h> | 8 #include <psapi.h> |
| 9 #include <shellapi.h> | 9 #include <shellapi.h> |
| 10 #include <shlobj.h> | 10 #include <shlobj.h> |
| 11 #include <time.h> | 11 #include <time.h> |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <limits> | 14 #include <limits> |
| 15 #include <string> | 15 #include <string> |
| 16 | 16 |
| 17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
| 20 #include "base/process_util.h" | 20 #include "base/process_util.h" |
| 21 #include "base/rand_util.h" | 21 #include "base/rand_util.h" |
| 22 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
| 23 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
| 24 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
| 25 #include "base/threading/thread_restrictions.h" | 25 #include "base/threading/thread_restrictions.h" |
| 26 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 27 #include "base/win/scoped_handle.h" | 27 #include "base/win/scoped_handle.h" |
| 28 #include "base/win/windows_version.h" | 28 #include "base/win/windows_version.h" |
| 29 | 29 |
| 30 using base::FilePath; | |
| 31 using base::g_bug108724_debug; | |
| 32 | |
| 33 namespace base { | 30 namespace base { |
| 34 | 31 |
| 35 FilePath MakeAbsoluteFilePath(const FilePath& input) { | |
| 36 base::ThreadRestrictions::AssertIOAllowed(); | |
| 37 wchar_t file_path[MAX_PATH]; | |
| 38 if (!_wfullpath(file_path, input.value().c_str(), MAX_PATH)) | |
| 39 return FilePath(); | |
| 40 return FilePath(file_path); | |
| 41 } | |
| 42 | |
| 43 } // namespace base | |
| 44 | |
| 45 namespace file_util { | |
| 46 | |
| 47 namespace { | 32 namespace { |
| 48 | 33 |
| 49 const DWORD kFileShareAll = | 34 const DWORD kFileShareAll = |
| 50 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | 35 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
| 51 | 36 |
| 52 } // namespace | 37 } // namespace |
| 53 | 38 |
| 39 FilePath MakeAbsoluteFilePath(const FilePath& input) { |
| 40 ThreadRestrictions::AssertIOAllowed(); |
| 41 wchar_t file_path[MAX_PATH]; |
| 42 if (!_wfullpath(file_path, input.value().c_str(), MAX_PATH)) |
| 43 return FilePath(); |
| 44 return FilePath(file_path); |
| 45 } |
| 46 |
| 54 bool Delete(const FilePath& path, bool recursive) { | 47 bool Delete(const FilePath& path, bool recursive) { |
| 55 base::ThreadRestrictions::AssertIOAllowed(); | 48 ThreadRestrictions::AssertIOAllowed(); |
| 56 | 49 |
| 57 if (path.value().length() >= MAX_PATH) | 50 if (path.value().length() >= MAX_PATH) |
| 58 return false; | 51 return false; |
| 59 | 52 |
| 60 if (!recursive) { | 53 if (!recursive) { |
| 61 // If not recursing, then first check to see if |path| is a directory. | 54 // If not recursing, then first check to see if |path| is a directory. |
| 62 // If it is, then remove it with RemoveDirectory. | 55 // If it is, then remove it with RemoveDirectory. |
| 63 base::PlatformFileInfo file_info; | 56 PlatformFileInfo file_info; |
| 64 if (GetFileInfo(path, &file_info) && file_info.is_directory) | 57 if (file_util::GetFileInfo(path, &file_info) && file_info.is_directory) |
| 65 return RemoveDirectory(path.value().c_str()) != 0; | 58 return RemoveDirectory(path.value().c_str()) != 0; |
| 66 | 59 |
| 67 // Otherwise, it's a file, wildcard or non-existant. Try DeleteFile first | 60 // Otherwise, it's a file, wildcard or non-existant. Try DeleteFile first |
| 68 // because it should be faster. If DeleteFile fails, then we fall through | 61 // because it should be faster. If DeleteFile fails, then we fall through |
| 69 // to SHFileOperation, which will do the right thing. | 62 // to SHFileOperation, which will do the right thing. |
| 70 if (DeleteFile(path.value().c_str()) != 0) | 63 if (DeleteFile(path.value().c_str()) != 0) |
| 71 return true; | 64 return true; |
| 72 } | 65 } |
| 73 | 66 |
| 74 // SHFILEOPSTRUCT wants the path to be terminated with two NULLs, | 67 // SHFILEOPSTRUCT wants the path to be terminated with two NULLs, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 98 // happen). See MSDN for SHFileOperation and SHFILEOPTSTRUCT. | 91 // happen). See MSDN for SHFileOperation and SHFILEOPTSTRUCT. |
| 99 if (file_operation.fAnyOperationsAborted) | 92 if (file_operation.fAnyOperationsAborted) |
| 100 return false; | 93 return false; |
| 101 | 94 |
| 102 // Some versions of Windows return ERROR_FILE_NOT_FOUND (0x2) when deleting | 95 // Some versions of Windows return ERROR_FILE_NOT_FOUND (0x2) when deleting |
| 103 // an empty directory and some return 0x402 when they should be returning | 96 // an empty directory and some return 0x402 when they should be returning |
| 104 // ERROR_FILE_NOT_FOUND. MSDN says Vista and up won't return 0x402. | 97 // ERROR_FILE_NOT_FOUND. MSDN says Vista and up won't return 0x402. |
| 105 return (err == 0 || err == ERROR_FILE_NOT_FOUND || err == 0x402); | 98 return (err == 0 || err == ERROR_FILE_NOT_FOUND || err == 0x402); |
| 106 } | 99 } |
| 107 | 100 |
| 101 } // namespace base |
| 102 |
| 103 namespace file_util { |
| 104 |
| 105 using base::FilePath; |
| 106 using base::kFileShareAll; |
| 107 |
| 108 bool DeleteAfterReboot(const FilePath& path) { | 108 bool DeleteAfterReboot(const FilePath& path) { |
| 109 base::ThreadRestrictions::AssertIOAllowed(); | 109 base::ThreadRestrictions::AssertIOAllowed(); |
| 110 | 110 |
| 111 if (path.value().length() >= MAX_PATH) | 111 if (path.value().length() >= MAX_PATH) |
| 112 return false; | 112 return false; |
| 113 | 113 |
| 114 return MoveFileEx(path.value().c_str(), NULL, | 114 return MoveFileEx(path.value().c_str(), NULL, |
| 115 MOVEFILE_DELAY_UNTIL_REBOOT | | 115 MOVEFILE_DELAY_UNTIL_REBOOT | |
| 116 MOVEFILE_REPLACE_EXISTING) != FALSE; | 116 MOVEFILE_REPLACE_EXISTING) != FALSE; |
| 117 } | 117 } |
| (...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 741 | 741 |
| 742 // Length of |path| with path separator appended. | 742 // Length of |path| with path separator appended. |
| 743 size_t prefix = path.StripTrailingSeparators().value().size() + 1; | 743 size_t prefix = path.StripTrailingSeparators().value().size() + 1; |
| 744 // The whole path string must be shorter than MAX_PATH. That is, it must be | 744 // The whole path string must be shorter than MAX_PATH. That is, it must be |
| 745 // prefix + component_length < MAX_PATH (or equivalently, <= MAX_PATH - 1). | 745 // prefix + component_length < MAX_PATH (or equivalently, <= MAX_PATH - 1). |
| 746 int whole_path_limit = std::max(0, MAX_PATH - 1 - static_cast<int>(prefix)); | 746 int whole_path_limit = std::max(0, MAX_PATH - 1 - static_cast<int>(prefix)); |
| 747 return std::min(whole_path_limit, static_cast<int>(max_length)); | 747 return std::min(whole_path_limit, static_cast<int>(max_length)); |
| 748 } | 748 } |
| 749 | 749 |
| 750 } // namespace file_util | 750 } // namespace file_util |
| OLD | NEW |