OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/files/file_enumerator.h" | 5 #include "base/files/file_enumerator.h" |
6 | 6 |
7 #include <dirent.h> | 7 #include <dirent.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <fnmatch.h> | 9 #include <fnmatch.h> |
10 #include <stdint.h> | 10 #include <stdint.h> |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 DIR* dir = opendir(source.value().c_str()); | 124 DIR* dir = opendir(source.value().c_str()); |
125 if (!dir) | 125 if (!dir) |
126 return false; | 126 return false; |
127 | 127 |
128 #if !defined(OS_LINUX) && !defined(OS_MACOSX) && !defined(OS_BSD) && \ | 128 #if !defined(OS_LINUX) && !defined(OS_MACOSX) && !defined(OS_BSD) && \ |
129 !defined(OS_SOLARIS) && !defined(OS_ANDROID) | 129 !defined(OS_SOLARIS) && !defined(OS_ANDROID) |
130 #error Port warning: depending on the definition of struct dirent, \ | 130 #error Port warning: depending on the definition of struct dirent, \ |
131 additional space for pathname may be needed | 131 additional space for pathname may be needed |
132 #endif | 132 #endif |
133 | 133 |
134 // In all implementations of the C library that Chromium can run with, | 134 struct dirent dent_buf; |
135 // concurrent calls to readdir that specify different directory streams are | 135 struct dirent* dent; |
136 // thread-safe. This is the case here, since the directory stream is scoped to | 136 while (readdir_r(dir, &dent_buf, &dent) == 0 && dent) { |
137 // the current function. See https://codereview.chromium.org/2411833004/#msg3 | |
138 for (struct dirent* dent = readdir(dir); dent; dent = readdir(dir)) { | |
139 FileInfo info; | 137 FileInfo info; |
140 info.filename_ = FilePath(dent->d_name); | 138 info.filename_ = FilePath(dent->d_name); |
141 | 139 |
142 FilePath full_name = source.Append(dent->d_name); | 140 FilePath full_name = source.Append(dent->d_name); |
143 int ret; | 141 int ret; |
144 if (show_links) | 142 if (show_links) |
145 ret = lstat(full_name.value().c_str(), &info.stat_); | 143 ret = lstat(full_name.value().c_str(), &info.stat_); |
146 else | 144 else |
147 ret = stat(full_name.value().c_str(), &info.stat_); | 145 ret = stat(full_name.value().c_str(), &info.stat_); |
148 if (ret < 0) { | 146 if (ret < 0) { |
149 // Print the stat() error message unless it was ENOENT and we're | 147 // Print the stat() error message unless it was ENOENT and we're |
150 // following symlinks. | 148 // following symlinks. |
151 if (!(errno == ENOENT && !show_links)) { | 149 if (!(errno == ENOENT && !show_links)) { |
152 DPLOG(ERROR) << "Couldn't stat " | 150 DPLOG(ERROR) << "Couldn't stat " |
153 << source.Append(dent->d_name).value(); | 151 << source.Append(dent->d_name).value(); |
154 } | 152 } |
155 memset(&info.stat_, 0, sizeof(info.stat_)); | 153 memset(&info.stat_, 0, sizeof(info.stat_)); |
156 } | 154 } |
157 entries->push_back(info); | 155 entries->push_back(info); |
158 } | 156 } |
159 | 157 |
160 closedir(dir); | 158 closedir(dir); |
161 return true; | 159 return true; |
162 } | 160 } |
163 | 161 |
164 } // namespace base | 162 } // namespace base |
OLD | NEW |