OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/test_file_util.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <fts.h> |
| 9 #include <sys/types.h> |
| 10 #include <sys/stat.h> |
| 11 |
| 12 #include <string> |
| 13 |
| 14 #include "base/logging.h" |
| 15 #include "base/file_path.h" |
| 16 #include "base/file_util.h" |
| 17 #include "base/string_util.h" |
| 18 |
| 19 namespace file_util { |
| 20 |
| 21 bool CopyRecursiveDirNoCache(const std::wstring& source_dir, |
| 22 const std::wstring& dest_dir) { |
| 23 const FilePath from_path(FilePath::FromWStringHack(source_dir)); |
| 24 const FilePath to_path(FilePath::FromWStringHack(dest_dir)); |
| 25 |
| 26 char top_dir[PATH_MAX]; |
| 27 if (base::strlcpy(top_dir, from_path.value().c_str(), |
| 28 arraysize(top_dir)) >= arraysize(top_dir)) { |
| 29 return false; |
| 30 } |
| 31 |
| 32 char* dir_list[] = { top_dir, NULL }; |
| 33 FTS* fts = fts_open(dir_list, FTS_PHYSICAL | FTS_NOSTAT, NULL); |
| 34 if (!fts) { |
| 35 LOG(ERROR) << "fts_open failed: " << strerror(errno); |
| 36 return false; |
| 37 } |
| 38 |
| 39 int error = 0; |
| 40 FTSENT* ent; |
| 41 while (!error && (ent = fts_read(fts)) != NULL) { |
| 42 // ent->fts_path is the source path, including from_path, so paste |
| 43 // the suffix after from_path onto to_path to create the target_path. |
| 44 std::string suffix(&ent->fts_path[from_path.value().size()]); |
| 45 // Strip the leading '/' (if any). |
| 46 if (!suffix.empty()) { |
| 47 DCHECK(suffix[0] == '/'); |
| 48 suffix.erase(0, 1); |
| 49 } |
| 50 const FilePath target_path = to_path.Append(suffix); |
| 51 switch (ent->fts_info) { |
| 52 case FTS_D: // Preorder directory. |
| 53 // Try creating the target dir, continuing on it if it exists already. |
| 54 if (mkdir(target_path.value().c_str(), 0777) != 0) { |
| 55 if (errno != EEXIST) |
| 56 error = errno; |
| 57 } |
| 58 break; |
| 59 case FTS_F: // Regular file. |
| 60 case FTS_NSOK: // File, no stat info requested. |
| 61 { |
| 62 errno = 0; |
| 63 FilePath source_path(ent->fts_path); |
| 64 if (CopyFile(source_path, target_path)) { |
| 65 bool success = EvictFileFromSystemCache( |
| 66 target_path.Append(source_path.BaseName())); |
| 67 DCHECK(success); |
| 68 } else { |
| 69 error = errno ? errno : EINVAL; |
| 70 } |
| 71 } |
| 72 break; |
| 73 case FTS_DP: // Postorder directory. |
| 74 case FTS_DOT: // "." or ".." |
| 75 // Skip it. |
| 76 continue; |
| 77 case FTS_DC: // Directory causing a cycle. |
| 78 // Skip this branch. |
| 79 if (fts_set(fts, ent, FTS_SKIP) != 0) |
| 80 error = errno; |
| 81 break; |
| 82 case FTS_DNR: // Directory cannot be read. |
| 83 case FTS_ERR: // Error. |
| 84 case FTS_NS: // Stat failed. |
| 85 // Abort with the error. |
| 86 error = ent->fts_errno; |
| 87 break; |
| 88 case FTS_SL: // Symlink. |
| 89 case FTS_SLNONE: // Symlink with broken target. |
| 90 LOG(WARNING) << "skipping symbolic link."; |
| 91 continue; |
| 92 case FTS_DEFAULT: // Some other sort of file. |
| 93 LOG(WARNING) << "skipping weird file."; |
| 94 continue; |
| 95 default: |
| 96 NOTREACHED(); |
| 97 continue; // Hope for the best! |
| 98 } |
| 99 } |
| 100 // fts_read may have returned NULL and set errno to indicate an error. |
| 101 if (!error && errno != 0) |
| 102 error = errno; |
| 103 |
| 104 if (!fts_close(fts)) { |
| 105 // If we already have an error, let's use that error instead of the error |
| 106 // fts_close set. |
| 107 if (!error) |
| 108 error = errno; |
| 109 } |
| 110 |
| 111 if (error) { |
| 112 LOG(ERROR) << strerror(error); |
| 113 return false; |
| 114 } |
| 115 return true; |
| 116 } |
| 117 |
| 118 } // namespace file_util |
OLD | NEW |