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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 | 141 |
142 if (!ret) { | 142 if (!ret) { |
143 // Leave a clue about what went wrong so that it can be (at least) picked | 143 // Leave a clue about what went wrong so that it can be (at least) picked |
144 // up by a PLOG entry. | 144 // up by a PLOG entry. |
145 ::SetLastError(last_error); | 145 ::SetLastError(last_error); |
146 } | 146 } |
147 | 147 |
148 return ret; | 148 return ret; |
149 } | 149 } |
150 | 150 |
151 bool ReplaceFile(const FilePath& from_path, const FilePath& to_path) { | 151 bool ReplaceFile(const FilePath& from_path, const FilePath& to_path, |
jar (doing other things)
2013/05/07 22:31:32
nit: One arg per line when you wrap args in declar
dgrogan
2013/05/07 22:45:16
Done.
| |
152 base::PlatformFileError* error) { | |
152 base::ThreadRestrictions::AssertIOAllowed(); | 153 base::ThreadRestrictions::AssertIOAllowed(); |
153 // Try a simple move first. It will only succeed when |to_path| doesn't | 154 // Try a simple move first. It will only succeed when |to_path| doesn't |
154 // already exist. | 155 // already exist. |
155 if (::MoveFile(from_path.value().c_str(), to_path.value().c_str())) | 156 if (::MoveFile(from_path.value().c_str(), to_path.value().c_str())) |
156 return true; | 157 return true; |
157 // Try the full-blown replace if the move fails, as ReplaceFile will only | 158 // Try the full-blown replace if the move fails, as ReplaceFile will only |
158 // succeed when |to_path| does exist. When writing to a network share, we may | 159 // succeed when |to_path| does exist. When writing to a network share, we may |
159 // not be able to change the ACLs. Ignore ACL errors then | 160 // not be able to change the ACLs. Ignore ACL errors then |
160 // (REPLACEFILE_IGNORE_MERGE_ERRORS). | 161 // (REPLACEFILE_IGNORE_MERGE_ERRORS). |
161 if (::ReplaceFile(to_path.value().c_str(), from_path.value().c_str(), NULL, | 162 if (::ReplaceFile(to_path.value().c_str(), from_path.value().c_str(), NULL, |
162 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL)) { | 163 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL)) { |
163 return true; | 164 return true; |
164 } | 165 } |
166 if (error) | |
167 *error = base::LastErrorToPlatformFileError(GetLastError()); | |
165 return false; | 168 return false; |
166 } | 169 } |
167 | 170 |
168 bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) { | 171 bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) { |
169 base::ThreadRestrictions::AssertIOAllowed(); | 172 base::ThreadRestrictions::AssertIOAllowed(); |
170 | 173 |
171 // NOTE: I suspect we could support longer paths, but that would involve | 174 // NOTE: I suspect we could support longer paths, but that would involve |
172 // analyzing all our usage of files. | 175 // analyzing all our usage of files. |
173 if (from_path.value().length() >= MAX_PATH || | 176 if (from_path.value().length() >= MAX_PATH || |
174 to_path.value().length() >= MAX_PATH) { | 177 to_path.value().length() >= MAX_PATH) { |
(...skipping 682 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
857 | 860 |
858 // Length of |path| with path separator appended. | 861 // Length of |path| with path separator appended. |
859 size_t prefix = path.StripTrailingSeparators().value().size() + 1; | 862 size_t prefix = path.StripTrailingSeparators().value().size() + 1; |
860 // The whole path string must be shorter than MAX_PATH. That is, it must be | 863 // The whole path string must be shorter than MAX_PATH. That is, it must be |
861 // prefix + component_length < MAX_PATH (or equivalently, <= MAX_PATH - 1). | 864 // prefix + component_length < MAX_PATH (or equivalently, <= MAX_PATH - 1). |
862 int whole_path_limit = std::max(0, MAX_PATH - 1 - static_cast<int>(prefix)); | 865 int whole_path_limit = std::max(0, MAX_PATH - 1 - static_cast<int>(prefix)); |
863 return std::min(whole_path_limit, static_cast<int>(max_length)); | 866 return std::min(whole_path_limit, static_cast<int>(max_length)); |
864 } | 867 } |
865 | 868 |
866 } // namespace file_util | 869 } // namespace file_util |
OLD | NEW |