Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(215)

Side by Side Diff: base/file_util_win.cc

Issue 14886003: Make base:ReplaceFile return an informative error. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ToT Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/file_util_posix.cc ('k') | base/platform_file.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 ReplaceFileAndGetError(const FilePath& from_path,
152 const FilePath& to_path,
153 base::PlatformFileError* error) {
152 base::ThreadRestrictions::AssertIOAllowed(); 154 base::ThreadRestrictions::AssertIOAllowed();
153 // Try a simple move first. It will only succeed when |to_path| doesn't 155 // Try a simple move first. It will only succeed when |to_path| doesn't
154 // already exist. 156 // already exist.
155 if (::MoveFile(from_path.value().c_str(), to_path.value().c_str())) 157 if (::MoveFile(from_path.value().c_str(), to_path.value().c_str()))
156 return true; 158 return true;
157 // Try the full-blown replace if the move fails, as ReplaceFile will only 159 // 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 160 // 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 161 // not be able to change the ACLs. Ignore ACL errors then
160 // (REPLACEFILE_IGNORE_MERGE_ERRORS). 162 // (REPLACEFILE_IGNORE_MERGE_ERRORS).
161 if (::ReplaceFile(to_path.value().c_str(), from_path.value().c_str(), NULL, 163 if (::ReplaceFile(to_path.value().c_str(), from_path.value().c_str(), NULL,
162 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL)) { 164 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL)) {
163 return true; 165 return true;
164 } 166 }
167 if (error)
168 *error = base::LastErrorToPlatformFileError(GetLastError());
165 return false; 169 return false;
166 } 170 }
167 171
168 bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) { 172 bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) {
169 base::ThreadRestrictions::AssertIOAllowed(); 173 base::ThreadRestrictions::AssertIOAllowed();
170 174
171 // NOTE: I suspect we could support longer paths, but that would involve 175 // NOTE: I suspect we could support longer paths, but that would involve
172 // analyzing all our usage of files. 176 // analyzing all our usage of files.
173 if (from_path.value().length() >= MAX_PATH || 177 if (from_path.value().length() >= MAX_PATH ||
174 to_path.value().length() >= MAX_PATH) { 178 to_path.value().length() >= MAX_PATH) {
(...skipping 682 matching lines...) Expand 10 before | Expand all | Expand 10 after
857 861
858 // Length of |path| with path separator appended. 862 // Length of |path| with path separator appended.
859 size_t prefix = path.StripTrailingSeparators().value().size() + 1; 863 size_t prefix = path.StripTrailingSeparators().value().size() + 1;
860 // The whole path string must be shorter than MAX_PATH. That is, it must be 864 // 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). 865 // 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)); 866 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)); 867 return std::min(whole_path_limit, static_cast<int>(max_length));
864 } 868 }
865 869
866 } // namespace file_util 870 } // namespace file_util
OLDNEW
« no previous file with comments | « base/file_util_posix.cc ('k') | base/platform_file.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698