1 // Copyright 2013 The Chromium Authors
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/files/file_enumerator.h"
6
7 #include <dirent.h>
8 #include <errno.h>
9 #include <fnmatch.h>
10 #include <stdint.h>
11 #include <string.h>
12
13 #include "base/logging.h"
14 #include "base/threading/scoped_blocking_call.h"
15 #include "build/build_config.h"
16
17 #if BUILDFLAG(IS_ANDROID)
18 #include "base/android/content_uri_utils.h"
19 #endif
20
21 namespace base {
22 namespace {
23
GetStat(const FilePath & path,bool show_links,stat_wrapper_t * st)24 bool GetStat(const FilePath& path, bool show_links, stat_wrapper_t* st) {
25 DCHECK(st);
26 const int res = show_links ? File::Lstat(path, st) : File::Stat(path, st);
27 if (res < 0) {
28 // Print the stat() error message unless it was ENOENT and we're following
29 // symlinks.
30 DPLOG_IF(ERROR, errno != ENOENT || show_links)
31 << "Cannot stat '" << path << "'";
32 memset(st, 0, sizeof(*st));
33 return false;
34 }
35
36 return true;
37 }
38
39 #if BUILDFLAG(IS_FUCHSIA)
ShouldShowSymLinks(int file_type)40 bool ShouldShowSymLinks(int file_type) {
41 return false;
42 }
43 #else
ShouldShowSymLinks(int file_type)44 bool ShouldShowSymLinks(int file_type) {
45 return file_type & FileEnumerator::SHOW_SYM_LINKS;
46 }
47 #endif // BUILDFLAG(IS_FUCHSIA)
48
49 #if BUILDFLAG(IS_FUCHSIA)
ShouldTrackVisitedDirectories(int file_type)50 bool ShouldTrackVisitedDirectories(int file_type) {
51 return false;
52 }
53 #else
ShouldTrackVisitedDirectories(int file_type)54 bool ShouldTrackVisitedDirectories(int file_type) {
55 return !(file_type & FileEnumerator::SHOW_SYM_LINKS);
56 }
57 #endif // BUILDFLAG(IS_FUCHSIA)
58
59 } // namespace
60
61 // FileEnumerator::FileInfo ----------------------------------------------------
62
FileInfo()63 FileEnumerator::FileInfo::FileInfo() {
64 memset(&stat_, 0, sizeof(stat_));
65 }
66
67 #if BUILDFLAG(IS_ANDROID)
FileInfo(base::FilePath content_uri,base::FilePath filename,bool is_directory,off_t size,Time time)68 FileEnumerator::FileInfo::FileInfo(base::FilePath content_uri,
69 base::FilePath filename,
70 bool is_directory,
71 off_t size,
72 Time time)
73 : content_uri_(std::move(content_uri)), filename_(std::move(filename)) {
74 memset(&stat_, 0, sizeof(stat_));
75 stat_.st_mode = is_directory ? S_IFDIR : S_IFREG;
76 stat_.st_size = size;
77 stat_.st_mtime = time.ToTimeT();
78 }
79 #endif
80
IsDirectory() const81 bool FileEnumerator::FileInfo::IsDirectory() const {
82 return S_ISDIR(stat_.st_mode);
83 }
84
GetName() const85 FilePath FileEnumerator::FileInfo::GetName() const {
86 return filename_;
87 }
88
GetSize() const89 int64_t FileEnumerator::FileInfo::GetSize() const {
90 return stat_.st_size;
91 }
92
GetLastModifiedTime() const93 base::Time FileEnumerator::FileInfo::GetLastModifiedTime() const {
94 return base::Time::FromTimeT(stat_.st_mtime);
95 }
96
97 // FileEnumerator --------------------------------------------------------------
98
FileEnumerator(const FilePath & root_path,bool recursive,int file_type)99 FileEnumerator::FileEnumerator(const FilePath& root_path,
100 bool recursive,
101 int file_type)
102 : FileEnumerator(root_path,
103 recursive,
104 file_type,
105 FilePath::StringType(),
106 FolderSearchPolicy::MATCH_ONLY) {}
107
FileEnumerator(const FilePath & root_path,bool recursive,int file_type,const FilePath::StringType & pattern)108 FileEnumerator::FileEnumerator(const FilePath& root_path,
109 bool recursive,
110 int file_type,
111 const FilePath::StringType& pattern)
112 : FileEnumerator(root_path,
113 recursive,
114 file_type,
115 pattern,
116 FolderSearchPolicy::MATCH_ONLY) {}
117
FileEnumerator(const FilePath & root_path,bool recursive,int file_type,const FilePath::StringType & pattern,FolderSearchPolicy folder_search_policy)118 FileEnumerator::FileEnumerator(const FilePath& root_path,
119 bool recursive,
120 int file_type,
121 const FilePath::StringType& pattern,
122 FolderSearchPolicy folder_search_policy)
123 : FileEnumerator(root_path,
124 recursive,
125 file_type,
126 pattern,
127 folder_search_policy,
128 ErrorPolicy::IGNORE_ERRORS) {}
129
FileEnumerator(const FilePath & root_path,bool recursive,int file_type,const FilePath::StringType & pattern,FolderSearchPolicy folder_search_policy,ErrorPolicy error_policy)130 FileEnumerator::FileEnumerator(const FilePath& root_path,
131 bool recursive,
132 int file_type,
133 const FilePath::StringType& pattern,
134 FolderSearchPolicy folder_search_policy,
135 ErrorPolicy error_policy)
136 : current_directory_entry_(0),
137 root_path_(root_path),
138 recursive_(recursive),
139 file_type_(file_type),
140 pattern_(pattern),
141 folder_search_policy_(folder_search_policy),
142 error_policy_(error_policy) {
143 // INCLUDE_DOT_DOT must not be specified if recursive.
144 DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_)));
145
146 #if BUILDFLAG(IS_ANDROID)
147 // Content-URIs have limited support.
148 if (root_path.IsContentUri()) {
149 CHECK_EQ(file_type_, FileType::FILES | FileType::DIRECTORIES);
150 // Get display-name of root path.
151 FileInfo root_info;
152 internal::ContentUriGetFileInfo(root_path, &root_info);
153 pending_subdirs_.push(
154 std::vector<std::string>({root_info.GetName().value()}));
155 }
156 #endif
157
158 if (file_type_ & FileType::NAMES_ONLY) {
159 DCHECK(!recursive_);
160 DCHECK_EQ(file_type_ & ~(FileType::NAMES_ONLY | FileType::INCLUDE_DOT_DOT),
161 0);
162 file_type_ |= (FileType::FILES | FileType::DIRECTORIES);
163 }
164
165 if (recursive && ShouldTrackVisitedDirectories(file_type_)) {
166 if (stat_wrapper_t st; GetStat(root_path, false, &st)) {
167 MarkVisited(st);
168 }
169 }
170
171 pending_paths_.push(root_path);
172 }
173
174 FileEnumerator::~FileEnumerator() = default;
175
Next()176 FilePath FileEnumerator::Next() {
177 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
178
179 ++current_directory_entry_;
180
181 // While we've exhausted the entries in the current directory, do the next
182 while (current_directory_entry_ >= directory_entries_.size()) {
183 if (pending_paths_.empty())
184 return FilePath();
185
186 root_path_ = pending_paths_.top();
187 root_path_ = root_path_.StripTrailingSeparators();
188 pending_paths_.pop();
189
190 #if BUILDFLAG(IS_ANDROID)
191 if (root_path_.IsContentUri()) {
192 subdirs_ = pending_subdirs_.top();
193 pending_subdirs_.pop();
194 directory_entries_ = internal::ListContentUriDirectory(root_path_);
195 current_directory_entry_ = 0;
196 if (directory_entries_.empty()) {
197 continue;
198 }
199 if (recursive_) {
200 for (auto& info : directory_entries_) {
201 info.subdirs_ = subdirs_;
202 if (info.IsDirectory()) {
203 pending_paths_.push(info.content_uri_);
204 pending_subdirs_.push(subdirs_);
205 pending_subdirs_.top().push_back(info.GetName().value());
206 }
207 }
208 }
209 break;
210 }
211 #endif
212
213 DIR* dir = opendir(root_path_.value().c_str());
214 if (!dir) {
215 if (errno == 0 || error_policy_ == ErrorPolicy::IGNORE_ERRORS)
216 continue;
217 error_ = File::OSErrorToFileError(errno);
218 return FilePath();
219 }
220
221 directory_entries_.clear();
222
223 #if BUILDFLAG(IS_FUCHSIA)
224 // Fuchsia does not support .. on the file system server side, see
225 // https://fuchsia.googlesource.com/docs/+/master/dotdot.md and
226 // https://crbug.com/735540. However, for UI purposes, having the parent
227 // directory show up in directory listings makes sense, so we add it here to
228 // match the expectation on other operating systems. In cases where this
229 // is useful it should be resolvable locally.
230 FileInfo dotdot;
231 dotdot.stat_.st_mode = S_IFDIR;
232 dotdot.filename_ = FilePath("..");
233 if (!ShouldSkip(dotdot.filename_)) {
234 directory_entries_.push_back(std::move(dotdot));
235 }
236 #endif // BUILDFLAG(IS_FUCHSIA)
237
238 current_directory_entry_ = 0;
239 struct dirent* dent;
240 // NOTE: Per the readdir() documentation, when the end of the directory is
241 // reached with no errors, null is returned and errno is not changed.
242 // Therefore we must reset errno to zero before calling readdir() if we
243 // wish to know whether a null result indicates an error condition.
244 while (errno = 0, dent = readdir(dir)) {
245 FileInfo info;
246 info.filename_ = FilePath(dent->d_name);
247
248 if (ShouldSkip(info.filename_))
249 continue;
250
251 const bool is_pattern_matched = IsPatternMatched(info.filename_);
252
253 // MATCH_ONLY policy enumerates files and directories which matching
254 // pattern only. So we can early skip further checks.
255 if (folder_search_policy_ == FolderSearchPolicy::MATCH_ONLY &&
256 !is_pattern_matched)
257 continue;
258
259 // Do not call OS stat/lstat if there is no sense to do it. If pattern is
260 // not matched (file will not appear in results) and search is not
261 // recursive (possible directory will not be added to pending paths) -
262 // there is no sense to obtain item below.
263 if (!recursive_ && !is_pattern_matched)
264 continue;
265
266 // If the caller only wants the names of files and directories, then
267 // continue without populating `info` further.
268 if (file_type_ & FileType::NAMES_ONLY) {
269 directory_entries_.push_back(std::move(info));
270 continue;
271 }
272
273 FilePath full_path = root_path_.Append(info.filename_);
274 if (!GetStat(full_path, ShouldShowSymLinks(file_type_), &info.stat_)) {
275 continue;
276 }
277
278 const bool is_dir = info.IsDirectory();
279
280 // Recursive mode: schedule traversal of a directory if either
281 // SHOW_SYM_LINKS is on or we haven't visited the directory yet.
282 if (recursive_ && is_dir &&
283 (!ShouldTrackVisitedDirectories(file_type_) ||
284 MarkVisited(info.stat_))) {
285 pending_paths_.push(std::move(full_path));
286 }
287
288 if (is_pattern_matched && IsTypeMatched(is_dir))
289 directory_entries_.push_back(std::move(info));
290 }
291 int readdir_errno = errno;
292 closedir(dir);
293 if (readdir_errno != 0 && error_policy_ != ErrorPolicy::IGNORE_ERRORS) {
294 error_ = File::OSErrorToFileError(readdir_errno);
295 return FilePath();
296 }
297
298 // MATCH_ONLY policy enumerates files in matched subfolders by "*" pattern.
299 // ALL policy enumerates files in all subfolders by origin pattern.
300 if (folder_search_policy_ == FolderSearchPolicy::MATCH_ONLY)
301 pattern_.clear();
302 }
303
304 #if BUILDFLAG(IS_ANDROID)
305 if (root_path_.IsContentUri()) {
306 return directory_entries_[current_directory_entry_].content_uri_;
307 }
308 #endif
309
310 return root_path_.Append(
311 directory_entries_[current_directory_entry_].filename_);
312 }
313
GetInfo() const314 FileEnumerator::FileInfo FileEnumerator::GetInfo() const {
315 DCHECK(!(file_type_ & FileType::NAMES_ONLY));
316 return directory_entries_[current_directory_entry_];
317 }
318
IsPatternMatched(const FilePath & path) const319 bool FileEnumerator::IsPatternMatched(const FilePath& path) const {
320 return pattern_.empty() ||
321 !fnmatch(pattern_.c_str(), path.value().c_str(), FNM_NOESCAPE);
322 }
323
324 } // namespace base
325