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

Side by Side Diff: base/file_util_posix.cc

Issue 269083: More CopyDirectory tests and fixes... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 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 | « no previous file | base/file_util_unittest.cc » ('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) 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 <dirent.h> 7 #include <dirent.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <fcntl.h> 9 #include <fcntl.h>
10 #include <fnmatch.h> 10 #include <fnmatch.h>
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 while (success && !directories.empty()) { 163 while (success && !directories.empty()) {
164 FilePath dir = FilePath(directories.top()); 164 FilePath dir = FilePath(directories.top());
165 directories.pop(); 165 directories.pop();
166 success = (rmdir(dir.value().c_str()) == 0); 166 success = (rmdir(dir.value().c_str()) == 0);
167 } 167 }
168 168
169 return success; 169 return success;
170 } 170 }
171 171
172 bool Move(const FilePath& from_path, const FilePath& to_path) { 172 bool Move(const FilePath& from_path, const FilePath& to_path) {
173 // Windows compatibility: if to_path exists, from_path and to_path
174 // must be the same type, either both files, or both directories.
175 stat_wrapper_t to_file_info;
176 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) {
177 stat_wrapper_t from_file_info;
178 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) {
179 if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode))
180 return false;
181 } else {
182 return false;
183 }
184 }
185
173 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0) 186 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0)
174 return true; 187 return true;
175 188
176 if (!CopyDirectory(from_path, to_path, true)) 189 if (!CopyDirectory(from_path, to_path, true))
177 return false; 190 return false;
178 191
179 Delete(from_path, true); 192 Delete(from_path, true);
180 return true; 193 return true;
181 } 194 }
182 195
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 bool success = true; 234 bool success = true;
222 FileEnumerator::FILE_TYPE traverse_type = 235 FileEnumerator::FILE_TYPE traverse_type =
223 static_cast<FileEnumerator::FILE_TYPE>(FileEnumerator::FILES | 236 static_cast<FileEnumerator::FILE_TYPE>(FileEnumerator::FILES |
224 FileEnumerator::SHOW_SYM_LINKS); 237 FileEnumerator::SHOW_SYM_LINKS);
225 if (recursive) 238 if (recursive)
226 traverse_type = static_cast<FileEnumerator::FILE_TYPE>( 239 traverse_type = static_cast<FileEnumerator::FILE_TYPE>(
227 traverse_type | FileEnumerator::DIRECTORIES); 240 traverse_type | FileEnumerator::DIRECTORIES);
228 FileEnumerator traversal(from_path, recursive, traverse_type); 241 FileEnumerator traversal(from_path, recursive, traverse_type);
229 242
230 // We have to mimic windows behavior here. |to_path| may not exist yet, 243 // We have to mimic windows behavior here. |to_path| may not exist yet,
231 // start the loop with |to_path|. If this is a recursive copy and 244 // start the loop with |to_path|.
232 // the destination already exists, we have to copy the source directory
233 // as well.
234 FileEnumerator::FindInfo info; 245 FileEnumerator::FindInfo info;
235 FilePath current = from_path; 246 FilePath current = from_path;
236 FilePath from_path_base = from_path;
237 if (recursive && stat(to_path.value().c_str(), &info.stat) == 0) {
238 // If the destination already exists, then the top level of source
239 // needs to be copied.
240 from_path_base = from_path.DirName();
241 }
242 if (stat(from_path.value().c_str(), &info.stat) < 0) { 247 if (stat(from_path.value().c_str(), &info.stat) < 0) {
243 LOG(ERROR) << "CopyDirectory() couldn't stat source directory: " << 248 LOG(ERROR) << "CopyDirectory() couldn't stat source directory: " <<
244 from_path.value() << " errno = " << errno; 249 from_path.value() << " errno = " << errno;
245 success = false; 250 success = false;
246 } 251 }
252 struct stat to_path_stat;
253 FilePath from_path_base = from_path;
254 if (recursive && stat(to_path.value().c_str(), &to_path_stat) == 0 &&
255 S_ISDIR(to_path_stat.st_mode)) {
256 // If the destination already exists and is a directory, then the
257 // top level of source needs to be copied.
258 from_path_base = from_path.DirName();
259 }
260
261 // The Windows version of this function assumes that non-recursive calls
262 // will always have a directory for from_path.
263 DCHECK(recursive || S_ISDIR(info.stat.st_mode));
247 264
248 while (success && !current.empty()) { 265 while (success && !current.empty()) {
249 // current is the source path, including from_path, so paste 266 // current is the source path, including from_path, so paste
250 // the suffix after from_path onto to_path to create the target_path. 267 // the suffix after from_path onto to_path to create the target_path.
251 std::string suffix(&current.value().c_str()[from_path_base.value().size()]); 268 std::string suffix(&current.value().c_str()[from_path_base.value().size()]);
252 // Strip the leading '/' (if any). 269 // Strip the leading '/' (if any).
253 if (!suffix.empty()) { 270 if (!suffix.empty()) {
254 DCHECK_EQ('/', suffix[0]); 271 DCHECK_EQ('/', suffix[0]);
255 suffix.erase(0, 1); 272 suffix.erase(0, 1);
256 } 273 }
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 munmap(data_, length_); 700 munmap(data_, length_);
684 if (file_ != -1) 701 if (file_ != -1)
685 close(file_); 702 close(file_);
686 703
687 data_ = NULL; 704 data_ = NULL;
688 length_ = 0; 705 length_ = 0;
689 file_ = -1; 706 file_ = -1;
690 } 707 }
691 708
692 } // namespace file_util 709 } // namespace file_util
OLDNEW
« no previous file with comments | « no previous file | base/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698