| 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> |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 // happen). See MSDN for SHFileOperation and SHFILEOPTSTRUCT. | 91 // happen). See MSDN for SHFileOperation and SHFILEOPTSTRUCT. |
| 92 if (file_operation.fAnyOperationsAborted) | 92 if (file_operation.fAnyOperationsAborted) |
| 93 return false; | 93 return false; |
| 94 | 94 |
| 95 // 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 |
| 96 // 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 |
| 97 // 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. |
| 98 return (err == 0 || err == ERROR_FILE_NOT_FOUND || err == 0x402); | 98 return (err == 0 || err == ERROR_FILE_NOT_FOUND || err == 0x402); |
| 99 } | 99 } |
| 100 | 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) { | 101 bool DeleteAfterReboot(const FilePath& path) { |
| 109 base::ThreadRestrictions::AssertIOAllowed(); | 102 ThreadRestrictions::AssertIOAllowed(); |
| 110 | 103 |
| 111 if (path.value().length() >= MAX_PATH) | 104 if (path.value().length() >= MAX_PATH) |
| 112 return false; | 105 return false; |
| 113 | 106 |
| 114 return MoveFileEx(path.value().c_str(), NULL, | 107 return MoveFileEx(path.value().c_str(), NULL, |
| 115 MOVEFILE_DELAY_UNTIL_REBOOT | | 108 MOVEFILE_DELAY_UNTIL_REBOOT | |
| 116 MOVEFILE_REPLACE_EXISTING) != FALSE; | 109 MOVEFILE_REPLACE_EXISTING) != FALSE; |
| 117 } | 110 } |
| 118 | 111 |
| 119 bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) { | 112 bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) { |
| 120 base::ThreadRestrictions::AssertIOAllowed(); | 113 ThreadRestrictions::AssertIOAllowed(); |
| 121 | 114 |
| 122 // NOTE: I suspect we could support longer paths, but that would involve | 115 // NOTE: I suspect we could support longer paths, but that would involve |
| 123 // analyzing all our usage of files. | 116 // analyzing all our usage of files. |
| 124 if (from_path.value().length() >= MAX_PATH || | 117 if (from_path.value().length() >= MAX_PATH || |
| 125 to_path.value().length() >= MAX_PATH) { | 118 to_path.value().length() >= MAX_PATH) { |
| 126 return false; | 119 return false; |
| 127 } | 120 } |
| 128 if (MoveFileEx(from_path.value().c_str(), to_path.value().c_str(), | 121 if (MoveFileEx(from_path.value().c_str(), to_path.value().c_str(), |
| 129 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) != 0) | 122 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) != 0) |
| 130 return true; | 123 return true; |
| 131 | 124 |
| 132 // Keep the last error value from MoveFileEx around in case the below | 125 // Keep the last error value from MoveFileEx around in case the below |
| 133 // fails. | 126 // fails. |
| 134 bool ret = false; | 127 bool ret = false; |
| 135 DWORD last_error = ::GetLastError(); | 128 DWORD last_error = ::GetLastError(); |
| 136 | 129 |
| 137 if (DirectoryExists(from_path)) { | 130 if (file_util::DirectoryExists(from_path)) { |
| 138 // MoveFileEx fails if moving directory across volumes. We will simulate | 131 // MoveFileEx fails if moving directory across volumes. We will simulate |
| 139 // the move by using Copy and Delete. Ideally we could check whether | 132 // the move by using Copy and Delete. Ideally we could check whether |
| 140 // from_path and to_path are indeed in different volumes. | 133 // from_path and to_path are indeed in different volumes. |
| 141 ret = CopyAndDeleteDirectory(from_path, to_path); | 134 ret = file_util::CopyAndDeleteDirectory(from_path, to_path); |
| 142 } | 135 } |
| 143 | 136 |
| 144 if (!ret) { | 137 if (!ret) { |
| 145 // Leave a clue about what went wrong so that it can be (at least) picked | 138 // Leave a clue about what went wrong so that it can be (at least) picked |
| 146 // up by a PLOG entry. | 139 // up by a PLOG entry. |
| 147 ::SetLastError(last_error); | 140 ::SetLastError(last_error); |
| 148 } | 141 } |
| 149 | 142 |
| 150 return ret; | 143 return ret; |
| 151 } | 144 } |
| 152 | 145 |
| 153 bool ReplaceFileAndGetError(const FilePath& from_path, | 146 bool ReplaceFile(const FilePath& from_path, |
| 154 const FilePath& to_path, | 147 const FilePath& to_path, |
| 155 base::PlatformFileError* error) { | 148 PlatformFileError* error) { |
| 156 base::ThreadRestrictions::AssertIOAllowed(); | 149 ThreadRestrictions::AssertIOAllowed(); |
| 157 // Try a simple move first. It will only succeed when |to_path| doesn't | 150 // Try a simple move first. It will only succeed when |to_path| doesn't |
| 158 // already exist. | 151 // already exist. |
| 159 if (::MoveFile(from_path.value().c_str(), to_path.value().c_str())) | 152 if (::MoveFile(from_path.value().c_str(), to_path.value().c_str())) |
| 160 return true; | 153 return true; |
| 161 // Try the full-blown replace if the move fails, as ReplaceFile will only | 154 // Try the full-blown replace if the move fails, as ReplaceFile will only |
| 162 // succeed when |to_path| does exist. When writing to a network share, we may | 155 // succeed when |to_path| does exist. When writing to a network share, we may |
| 163 // not be able to change the ACLs. Ignore ACL errors then | 156 // not be able to change the ACLs. Ignore ACL errors then |
| 164 // (REPLACEFILE_IGNORE_MERGE_ERRORS). | 157 // (REPLACEFILE_IGNORE_MERGE_ERRORS). |
| 165 if (::ReplaceFile(to_path.value().c_str(), from_path.value().c_str(), NULL, | 158 if (::ReplaceFile(to_path.value().c_str(), from_path.value().c_str(), NULL, |
| 166 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL)) { | 159 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL)) { |
| 167 return true; | 160 return true; |
| 168 } | 161 } |
| 169 if (error) | 162 if (error) |
| 170 *error = base::LastErrorToPlatformFileError(GetLastError()); | 163 *error = LastErrorToPlatformFileError(GetLastError()); |
| 171 return false; | 164 return false; |
| 172 } | 165 } |
| 173 | 166 |
| 167 } // namespace base |
| 168 |
| 169 // ----------------------------------------------------------------------------- |
| 170 |
| 171 namespace file_util { |
| 172 |
| 173 using base::FilePath; |
| 174 using base::kFileShareAll; |
| 175 |
| 174 bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) { | 176 bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) { |
| 175 base::ThreadRestrictions::AssertIOAllowed(); | 177 base::ThreadRestrictions::AssertIOAllowed(); |
| 176 | 178 |
| 177 // NOTE: I suspect we could support longer paths, but that would involve | 179 // NOTE: I suspect we could support longer paths, but that would involve |
| 178 // analyzing all our usage of files. | 180 // analyzing all our usage of files. |
| 179 if (from_path.value().length() >= MAX_PATH || | 181 if (from_path.value().length() >= MAX_PATH || |
| 180 to_path.value().length() >= MAX_PATH) { | 182 to_path.value().length() >= MAX_PATH) { |
| 181 return false; | 183 return false; |
| 182 } | 184 } |
| 183 return (::CopyFile(from_path.value().c_str(), to_path.value().c_str(), | 185 return (::CopyFile(from_path.value().c_str(), to_path.value().c_str(), |
| (...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 741 | 743 |
| 742 // Length of |path| with path separator appended. | 744 // Length of |path| with path separator appended. |
| 743 size_t prefix = path.StripTrailingSeparators().value().size() + 1; | 745 size_t prefix = path.StripTrailingSeparators().value().size() + 1; |
| 744 // The whole path string must be shorter than MAX_PATH. That is, it must be | 746 // 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). | 747 // 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)); | 748 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)); | 749 return std::min(whole_path_limit, static_cast<int>(max_length)); |
| 748 } | 750 } |
| 749 | 751 |
| 750 } // namespace file_util | 752 } // namespace file_util |
| OLD | NEW |