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

Side by Side Diff: base/file_util_posix.cc

Issue 270086: Match Window's behavior when recursively copying directories that exist. (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 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 220
221 bool success = true; 221 bool success = true;
222 FileEnumerator::FILE_TYPE traverse_type = 222 FileEnumerator::FILE_TYPE traverse_type =
223 static_cast<FileEnumerator::FILE_TYPE>(FileEnumerator::FILES | 223 static_cast<FileEnumerator::FILE_TYPE>(FileEnumerator::FILES |
224 FileEnumerator::SHOW_SYM_LINKS); 224 FileEnumerator::SHOW_SYM_LINKS);
225 if (recursive) 225 if (recursive)
226 traverse_type = static_cast<FileEnumerator::FILE_TYPE>( 226 traverse_type = static_cast<FileEnumerator::FILE_TYPE>(
227 traverse_type | FileEnumerator::DIRECTORIES); 227 traverse_type | FileEnumerator::DIRECTORIES);
228 FileEnumerator traversal(from_path, recursive, traverse_type); 228 FileEnumerator traversal(from_path, recursive, traverse_type);
229 229
230 // to_path may not exist yet, start the loop with to_path 230 // We have to mimic windows behavoir here. |to_path| may not exist yet,
sgk 2009/10/13 17:49:13 Nit: "behavior"
231 // start the loop with |to_path|. If this is a recrursive copy and
sgk 2009/10/13 17:49:13 Nit: "recursive"
232 // the destination already exists, we have to copy the source directory
233 // as well.
231 FileEnumerator::FindInfo info; 234 FileEnumerator::FindInfo info;
232 FilePath current = from_path; 235 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 }
233 if (stat(from_path.value().c_str(), &info.stat) < 0) { 242 if (stat(from_path.value().c_str(), &info.stat) < 0) {
234 LOG(ERROR) << "CopyDirectory() couldn't stat source directory: " << 243 LOG(ERROR) << "CopyDirectory() couldn't stat source directory: " <<
235 from_path.value() << " errno = " << errno; 244 from_path.value() << " errno = " << errno;
236 success = false; 245 success = false;
237 } 246 }
238 247
239 while (success && !current.empty()) { 248 while (success && !current.empty()) {
240 // current is the source path, including from_path, so paste 249 // current is the source path, including from_path, so paste
241 // the suffix after from_path onto to_path to create the target_path. 250 // the suffix after from_path onto to_path to create the target_path.
242 std::string suffix(&current.value().c_str()[from_path.value().size()]); 251 std::string suffix(&current.value().c_str()[from_path_base.value().size()]);
243 // Strip the leading '/' (if any). 252 // Strip the leading '/' (if any).
244 if (!suffix.empty()) { 253 if (!suffix.empty()) {
245 DCHECK_EQ('/', suffix[0]); 254 DCHECK_EQ('/', suffix[0]);
246 suffix.erase(0, 1); 255 suffix.erase(0, 1);
247 } 256 }
248 const FilePath target_path = to_path.Append(suffix); 257 const FilePath target_path = to_path.Append(suffix);
249 258
250 if (S_ISDIR(info.stat.st_mode)) { 259 if (S_ISDIR(info.stat.st_mode)) {
251 if (mkdir(target_path.value().c_str(), info.stat.st_mode & 01777) != 0 && 260 if (mkdir(target_path.value().c_str(), info.stat.st_mode & 01777) != 0 &&
252 errno != EEXIST) { 261 errno != EEXIST) {
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 munmap(data_, length_); 684 munmap(data_, length_);
676 if (file_ != -1) 685 if (file_ != -1)
677 close(file_); 686 close(file_);
678 687
679 data_ = NULL; 688 data_ = NULL;
680 length_ = 0; 689 length_ = 0;
681 file_ = -1; 690 file_ = -1;
682 } 691 }
683 692
684 } // namespace file_util 693 } // 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