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
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     if (!(errno == ENOENT && !show_links))
28       DPLOG(ERROR) << "Couldn't stat" << path.value();
29     memset(st, 0, sizeof(*st));
30   }
31 }
32 
33 #if defined(OS_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  // defined(OS_FUCHSIA)
42 
43 #if defined(OS_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  // defined(OS_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 (recursive && ShouldTrackVisitedDirectories(file_type_)) {
127     stat_wrapper_t st;
128     GetStat(root_path, false, &st);
129     visited_directories_.insert(st.st_ino);
130   }
131 
132   pending_paths_.push(root_path);
133 }
134 
135 FileEnumerator::~FileEnumerator() = default;
136 
Next()137 FilePath FileEnumerator::Next() {
138   ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
139 
140   ++current_directory_entry_;
141 
142   // While we've exhausted the entries in the current directory, do the next
143   while (current_directory_entry_ >= directory_entries_.size()) {
144     if (pending_paths_.empty())
145       return FilePath();
146 
147     root_path_ = pending_paths_.top();
148     root_path_ = root_path_.StripTrailingSeparators();
149     pending_paths_.pop();
150 
151     DIR* dir = opendir(root_path_.value().c_str());
152     if (!dir) {
153       if (errno == 0 || error_policy_ == ErrorPolicy::IGNORE_ERRORS)
154         continue;
155       error_ = File::OSErrorToFileError(errno);
156       return FilePath();
157     }
158 
159     directory_entries_.clear();
160 
161 #if defined(OS_FUCHSIA)
162     // Fuchsia does not support .. on the file system server side, see
163     // https://fuchsia.googlesource.com/docs/+/master/dotdot.md and
164     // https://crbug.com/735540. However, for UI purposes, having the parent
165     // directory show up in directory listings makes sense, so we add it here to
166     // match the expectation on other operating systems. In cases where this
167     // is useful it should be resolvable locally.
168     FileInfo dotdot;
169     dotdot.stat_.st_mode = S_IFDIR;
170     dotdot.filename_ = FilePath("..");
171     if (!ShouldSkip(dotdot.filename_)) {
172       directory_entries_.push_back(std::move(dotdot));
173     }
174 #endif  // OS_FUCHSIA
175 
176     current_directory_entry_ = 0;
177     struct dirent* dent;
178     // NOTE: Per the readdir() documentation, when the end of the directory is
179     // reached with no errors, null is returned and errno is not changed.
180     // Therefore we must reset errno to zero before calling readdir() if we
181     // wish to know whether a null result indicates an error condition.
182     while (errno = 0, dent = readdir(dir)) {
183       FileInfo info;
184       info.filename_ = FilePath(dent->d_name);
185 
186       if (ShouldSkip(info.filename_))
187         continue;
188 
189       const bool is_pattern_matched = IsPatternMatched(info.filename_);
190 
191       // MATCH_ONLY policy enumerates files and directories which matching
192       // pattern only. So we can early skip further checks.
193       if (folder_search_policy_ == FolderSearchPolicy::MATCH_ONLY &&
194           !is_pattern_matched)
195         continue;
196 
197       // Do not call OS stat/lstat if there is no sense to do it. If pattern is
198       // not matched (file will not appear in results) and search is not
199       // recursive (possible directory will not be added to pending paths) -
200       // there is no sense to obtain item below.
201       if (!recursive_ && !is_pattern_matched)
202         continue;
203 
204       const FilePath full_path = root_path_.Append(info.filename_);
205       GetStat(full_path, ShouldShowSymLinks(file_type_), &info.stat_);
206 
207       const bool is_dir = info.IsDirectory();
208 
209       // Recursive mode: schedule traversal of a directory if either
210       // SHOW_SYM_LINKS is on or we haven't visited the directory yet.
211       if (recursive_ && is_dir &&
212           (!ShouldTrackVisitedDirectories(file_type_) ||
213            visited_directories_.insert(info.stat_.st_ino).second)) {
214         pending_paths_.push(full_path);
215       }
216 
217       if (is_pattern_matched && IsTypeMatched(is_dir))
218         directory_entries_.push_back(std::move(info));
219     }
220     int readdir_errno = errno;
221     closedir(dir);
222     if (readdir_errno != 0 && error_policy_ != ErrorPolicy::IGNORE_ERRORS) {
223       error_ = File::OSErrorToFileError(readdir_errno);
224       return FilePath();
225     }
226 
227     // MATCH_ONLY policy enumerates files in matched subfolders by "*" pattern.
228     // ALL policy enumerates files in all subfolders by origin pattern.
229     if (folder_search_policy_ == FolderSearchPolicy::MATCH_ONLY)
230       pattern_.clear();
231   }
232 
233   return root_path_.Append(
234       directory_entries_[current_directory_entry_].filename_);
235 }
236 
GetInfo() const237 FileEnumerator::FileInfo FileEnumerator::GetInfo() const {
238   return directory_entries_[current_directory_entry_];
239 }
240 
IsPatternMatched(const FilePath & path) const241 bool FileEnumerator::IsPatternMatched(const FilePath& path) const {
242   return pattern_.empty() ||
243          !fnmatch(pattern_.c_str(), path.value().c_str(), FNM_NOESCAPE);
244 }
245 
246 }  // namespace base
247