| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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/test_file_util.h" | 5 #include "base/test_file_util.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fts.h> | 8 #include <fts.h> |
| 9 #include <sys/types.h> | 9 #include <sys/types.h> |
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 } | 37 } |
| 38 | 38 |
| 39 int error = 0; | 39 int error = 0; |
| 40 FTSENT* ent; | 40 FTSENT* ent; |
| 41 while (!error && (ent = fts_read(fts)) != NULL) { | 41 while (!error && (ent = fts_read(fts)) != NULL) { |
| 42 // ent->fts_path is the source path, including from_path, so paste | 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. | 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()]); | 44 std::string suffix(&ent->fts_path[from_path.value().size()]); |
| 45 // Strip the leading '/' (if any). | 45 // Strip the leading '/' (if any). |
| 46 if (!suffix.empty()) { | 46 if (!suffix.empty()) { |
| 47 DCHECK(suffix[0] == '/'); | 47 DCHECK_EQ('/', suffix[0]); |
| 48 suffix.erase(0, 1); | 48 suffix.erase(0, 1); |
| 49 } | 49 } |
| 50 const FilePath target_path = to_path.Append(suffix); | 50 const FilePath target_path = to_path.Append(suffix); |
| 51 switch (ent->fts_info) { | 51 switch (ent->fts_info) { |
| 52 case FTS_D: // Preorder directory. | 52 case FTS_D: // Preorder directory. |
| 53 // Try creating the target dir, continuing on it if it exists already. | 53 // Try creating the target dir, continuing on it if it exists already. |
| 54 // Rely on the user's umask to produce correct permissions. |
| 54 if (mkdir(target_path.value().c_str(), 0777) != 0) { | 55 if (mkdir(target_path.value().c_str(), 0777) != 0) { |
| 55 if (errno != EEXIST) | 56 if (errno != EEXIST) |
| 56 error = errno; | 57 error = errno; |
| 57 } | 58 } |
| 58 break; | 59 break; |
| 59 case FTS_F: // Regular file. | 60 case FTS_F: // Regular file. |
| 60 case FTS_NSOK: // File, no stat info requested. | 61 case FTS_NSOK: // File, no stat info requested. |
| 61 { | 62 { |
| 62 errno = 0; | 63 errno = 0; |
| 63 FilePath source_path(ent->fts_path); | 64 FilePath source_path(ent->fts_path); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 80 error = errno; | 81 error = errno; |
| 81 break; | 82 break; |
| 82 case FTS_DNR: // Directory cannot be read. | 83 case FTS_DNR: // Directory cannot be read. |
| 83 case FTS_ERR: // Error. | 84 case FTS_ERR: // Error. |
| 84 case FTS_NS: // Stat failed. | 85 case FTS_NS: // Stat failed. |
| 85 // Abort with the error. | 86 // Abort with the error. |
| 86 error = ent->fts_errno; | 87 error = ent->fts_errno; |
| 87 break; | 88 break; |
| 88 case FTS_SL: // Symlink. | 89 case FTS_SL: // Symlink. |
| 89 case FTS_SLNONE: // Symlink with broken target. | 90 case FTS_SLNONE: // Symlink with broken target. |
| 90 LOG(WARNING) << "skipping symbolic link."; | 91 LOG(WARNING) << "skipping symbolic link: " << ent->fts_path; |
| 91 continue; | 92 continue; |
| 92 case FTS_DEFAULT: // Some other sort of file. | 93 case FTS_DEFAULT: // Some other sort of file. |
| 93 LOG(WARNING) << "skipping weird file."; | 94 LOG(WARNING) << "skipping file of unknown type: " << ent->fts_path; |
| 94 continue; | 95 continue; |
| 95 default: | 96 default: |
| 96 NOTREACHED(); | 97 NOTREACHED(); |
| 97 continue; // Hope for the best! | 98 continue; // Hope for the best! |
| 98 } | 99 } |
| 99 } | 100 } |
| 100 // fts_read may have returned NULL and set errno to indicate an error. | 101 // fts_read may have returned NULL and set errno to indicate an error. |
| 101 if (!error && errno != 0) | 102 if (!error && errno != 0) |
| 102 error = errno; | 103 error = errno; |
| 103 | 104 |
| 104 if (!fts_close(fts)) { | 105 if (!fts_close(fts)) { |
| 105 // If we already have an error, let's use that error instead of the error | 106 // If we already have an error, let's use that error instead of the error |
| 106 // fts_close set. | 107 // fts_close set. |
| 107 if (!error) | 108 if (!error) |
| 108 error = errno; | 109 error = errno; |
| 109 } | 110 } |
| 110 | 111 |
| 111 if (error) { | 112 if (error) { |
| 112 LOG(ERROR) << strerror(error); | 113 LOG(ERROR) << strerror(error); |
| 113 return false; | 114 return false; |
| 114 } | 115 } |
| 115 return true; | 116 return true; |
| 116 } | 117 } |
| 117 | 118 |
| 118 } // namespace file_util | 119 } // namespace file_util |
| OLD | NEW |