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 namespace base {
18 namespace {
19
GetStat(const FilePath & path,bool show_links,stat_wrapper_t * st)20 void GetStat(const FilePath& path, bool show_links, stat_wrapper_t* st) {
21 DCHECK(st);
22 const int res = show_links ? File::Lstat(path.value().c_str(), st)
23 : File::Stat(path.value().c_str(), st);
24 if (res < 0) {
25 // Print the stat() error message unless it was ENOENT and we're following
26 // symlinks.
27 DPLOG_IF(ERROR, errno != ENOENT || show_links)
28 << "Cannot stat '" << path << "'";
29 memset(st, 0, sizeof(*st));
30 }
31 }
32
33 #if BUILDFLAG(IS_FUCHSIA)
ShouldShowSymLinks(int file_type)34 bool ShouldShowSymLinks(int file_type) {
35 return false;
36 }
37 #else
ShouldShowSymLinks(int file_type)38 bool ShouldShowSymLinks(int file_type) {
39 return file_type & FileEnumerator::SHOW_SYM_LINKS;
40 }
41 #endif // BUILDFLAG(IS_FUCHSIA)
42
43 #if BUILDFLAG(IS_FUCHSIA)
ShouldTrackVisitedDirectories(int file_type)44 bool ShouldTrackVisitedDirectories(int file_type) {
45 return false;
46 }
47 #else
ShouldTrackVisitedDirectories(int file_type)48 bool ShouldTrackVisitedDirectories(int file_type) {
49 return !(file_type & FileEnumerator::SHOW_SYM_LINKS);
50 }
51 #endif // BUILDFLAG(IS_FUCHSIA)
52
53 } // namespace
54
55 // FileEnumerator::FileInfo ----------------------------------------------------
56
FileInfo()57 FileEnumerator::FileInfo::FileInfo() {
58 memset(&stat_, 0, sizeof(stat_));
59 }
60
IsDirectory() const61 bool FileEnumerator::FileInfo::IsDirectory() const {
62 return S_ISDIR(stat_.st_mode);
63 }
64
GetName() const65 FilePath FileEnumerator::FileInfo::GetName() const {
66 return filename_;
67 }
68
GetSize() const69 int64_t FileEnumerator::FileInfo::GetSize() const {
70 return stat_.st_size;
71 }
72
GetLastModifiedTime() const73 base::Time FileEnumerator::FileInfo::GetLastModifiedTime() const {
74 return base::Time::FromTimeT(stat_.st_mtime);
75 }
76
77 // FileEnumerator --------------------------------------------------------------
78
FileEnumerator(const FilePath & root_path,bool recursive,int file_type)79 FileEnumerator::FileEnumerator(const FilePath& root_path,
80 bool recursive,
81 int file_type)
82 : FileEnumerator(root_path,
83 recursive,
84 file_type,
85 FilePath::StringType(),
86 FolderSearchPolicy::MATCH_ONLY) {}
87
FileEnumerator(const FilePath & root_path,bool recursive,int file_type,const FilePath::StringType & pattern)88 FileEnumerator::FileEnumerator(const FilePath& root_path,
89 bool recursive,
90 int file_type,
91 const FilePath::StringType& pattern)
92 : FileEnumerator(root_path,
93 recursive,
94 file_type,
95 pattern,
96 FolderSearchPolicy::MATCH_ONLY) {}
97
FileEnumerator(const FilePath & root_path,bool recursive,int file_type,const FilePath::StringType & pattern,FolderSearchPolicy folder_search_policy)98 FileEnumerator::FileEnumerator(const FilePath& root_path,
99 bool recursive,
100 int file_type,
101 const FilePath::StringType& pattern,
102 FolderSearchPolicy folder_search_policy)
103 : FileEnumerator(root_path,
104 recursive,
105 file_type,
106 pattern,
107 folder_search_policy,
108 ErrorPolicy::IGNORE_ERRORS) {}
109
FileEnumerator(const FilePath & root_path,bool recursive,int file_type,const FilePath::StringType & pattern,FolderSearchPolicy folder_search_policy,ErrorPolicy error_policy)110 FileEnumerator::FileEnumerator(const FilePath& root_path,
111 bool recursive,
112 int file_type,
113 const FilePath::StringType& pattern,
114 FolderSearchPolicy folder_search_policy,
115 ErrorPolicy error_policy)
116 : current_directory_entry_(0),
117 root_path_(root_path),
118 recursive_(recursive),
119 file_type_(file_type),
120 pattern_(pattern),
121 folder_search_policy_(folder_search_policy),
122 error_policy_(error_policy) {
123 // INCLUDE_DOT_DOT must not be specified if recursive.
124 DCHECK(!(recursive && (INCLUDE_DOT_DOT & file_type_)));
125
126 if (file_type_ & FileType::NAMES_ONLY) {
127 DCHECK(!recursive_);
128 DCHECK_EQ(file_type_ & ~(FileType::NAMES_ONLY | FileType::INCLUDE_DOT_DOT),
129 0);
130 file_type_ |= (FileType::FILES | FileType::DIRECTORIES);
131 }
132
133 if (recursive && ShouldTrackVisitedDirectories(file_type_)) {
134 stat_wrapper_t st;
135 GetStat(root_path, false, &st);
136 visited_directories_.insert(st.st_ino);
137 }
138
139 pending_paths_.push(root_path);
140 }
141
142 FileEnumerator::~FileEnumerator() = default;
143
Next()144 FilePath FileEnumerator::Next() {
145 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
146
147 ++current_directory_entry_;
148
149 // While we've exhausted the entries in the current directory, do the next
150 while (current_directory_entry_ >= directory_entries_.size()) {
151 if (pending_paths_.empty())
152 return FilePath();
153
154 root_path_ = pending_paths_.top();
155 root_path_ = root_path_.StripTrailingSeparators();
156 pending_paths_.pop();
157
158 DIR* dir = opendir(root_path_.value().c_str());
159 if (!dir) {
160 if (errno == 0 || error_policy_ == ErrorPolicy::IGNORE_ERRORS)
161 continue;
162 error_ = File::OSErrorToFileError(errno);
163 return FilePath();
164 }
165
166 directory_entries_.clear();
167
168 #if BUILDFLAG(IS_FUCHSIA)
169 // Fuchsia does not support .. on the file system server side, see
170 // https://fuchsia.googlesource.com/docs/+/master/dotdot.md and
171 // https://crbug.com/735540. However, for UI purposes, having the parent
172 // directory show up in directory listings makes sense, so we add it here to
173 // match the expectation on other operating systems. In cases where this
174 // is useful it should be resolvable locally.
175 FileInfo dotdot;
176 dotdot.stat_.st_mode = S_IFDIR;
177 dotdot.filename_ = FilePath("..");
178 if (!ShouldSkip(dotdot.filename_)) {
179 directory_entries_.push_back(std::move(dotdot));
180 }
181 #endif // BUILDFLAG(IS_FUCHSIA)
182
183 current_directory_entry_ = 0;
184 struct dirent* dent;
185 // NOTE: Per the readdir() documentation, when the end of the directory is
186 // reached with no errors, null is returned and errno is not changed.
187 // Therefore we must reset errno to zero before calling readdir() if we
188 // wish to know whether a null result indicates an error condition.
189 while (errno = 0, dent = readdir(dir)) {
190 FileInfo info;
191 info.filename_ = FilePath(dent->d_name);
192
193 if (ShouldSkip(info.filename_))
194 continue;
195
196 const bool is_pattern_matched = IsPatternMatched(info.filename_);
197
198 // MATCH_ONLY policy enumerates files and directories which matching
199 // pattern only. So we can early skip further checks.
200 if (folder_search_policy_ == FolderSearchPolicy::MATCH_ONLY &&
201 !is_pattern_matched)
202 continue;
203
204 // Do not call OS stat/lstat if there is no sense to do it. If pattern is
205 // not matched (file will not appear in results) and search is not
206 // recursive (possible directory will not be added to pending paths) -
207 // there is no sense to obtain item below.
208 if (!recursive_ && !is_pattern_matched)
209 continue;
210
211 // If the caller only wants the names of files and directories, then
212 // continue without populating `info` further.
213 if (file_type_ & FileType::NAMES_ONLY) {
214 directory_entries_.push_back(std::move(info));
215 continue;
216 }
217
218 const FilePath full_path = root_path_.Append(info.filename_);
219 GetStat(full_path, ShouldShowSymLinks(file_type_), &info.stat_);
220
221 const bool is_dir = info.IsDirectory();
222
223 // Recursive mode: schedule traversal of a directory if either
224 // SHOW_SYM_LINKS is on or we haven't visited the directory yet.
225 if (recursive_ && is_dir &&
226 (!ShouldTrackVisitedDirectories(file_type_) ||
227 visited_directories_.insert(info.stat_.st_ino).second)) {
228 pending_paths_.push(full_path);
229 }
230
231 if (is_pattern_matched && IsTypeMatched(is_dir))
232 directory_entries_.push_back(std::move(info));
233 }
234 int readdir_errno = errno;
235 closedir(dir);
236 if (readdir_errno != 0 && error_policy_ != ErrorPolicy::IGNORE_ERRORS) {
237 error_ = File::OSErrorToFileError(readdir_errno);
238 return FilePath();
239 }
240
241 // MATCH_ONLY policy enumerates files in matched subfolders by "*" pattern.
242 // ALL policy enumerates files in all subfolders by origin pattern.
243 if (folder_search_policy_ == FolderSearchPolicy::MATCH_ONLY)
244 pattern_.clear();
245 }
246
247 return root_path_.Append(
248 directory_entries_[current_directory_entry_].filename_);
249 }
250
GetInfo() const251 FileEnumerator::FileInfo FileEnumerator::GetInfo() const {
252 DCHECK(!(file_type_ & FileType::NAMES_ONLY));
253 return directory_entries_[current_directory_entry_];
254 }
255
IsPatternMatched(const FilePath & path) const256 bool FileEnumerator::IsPatternMatched(const FilePath& path) const {
257 return pattern_.empty() ||
258 !fnmatch(pattern_.c_str(), path.value().c_str(), FNM_NOESCAPE);
259 }
260
261 } // namespace base
262