1 //===------------------ directory_iterator.cpp ----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /* clang-format off */
10 #include "automotive/filesystem"
11 #include <__config>
12 #if defined(_LIBCPP_WIN32API)
13 #define WIN32_LEAN_AND_MEAN
14 #include <Windows.h>
15 #else
16 #include <dirent.h>
17 #endif
18 #include <errno.h>
19 
20 #include "filesystem_common.h"
21 
22 namespace android::hardware::automotive::filesystem {
23 
24 namespace detail {
25 namespace {
26 
27 #if !defined(_LIBCPP_WIN32API)
28 template <class DirEntT, class = decltype(DirEntT::d_type)>
get_file_type(DirEntT * ent,int)29 static file_type get_file_type(DirEntT* ent, int) {
30   switch (ent->d_type) {
31   case DT_BLK:
32     return file_type::block;
33   case DT_CHR:
34     return file_type::character;
35   case DT_DIR:
36     return file_type::directory;
37   case DT_FIFO:
38     return file_type::fifo;
39   case DT_LNK:
40     return file_type::symlink;
41   case DT_REG:
42     return file_type::regular;
43   case DT_SOCK:
44     return file_type::socket;
45   // Unlike in lstat, hitting "unknown" here simply means that the underlying
46   // filesystem doesn't support d_type. Report is as 'none' so we correctly
47   // set the cache to empty.
48   case DT_UNKNOWN:
49     break;
50   }
51   return file_type::none;
52 }
53 
54 template <class DirEntT>
get_file_type(DirEntT * ent,long)55 static file_type get_file_type(DirEntT* ent, long) {
56   return file_type::none;
57 }
58 
posix_readdir(DIR * dir_stream,error_code & ec)59 static pair<string_view, file_type> posix_readdir(DIR* dir_stream,
60                                                   error_code& ec) {
61   struct dirent* dir_entry_ptr = nullptr;
62   errno = 0; // zero errno in order to detect errors
63   ec.clear();
64   if ((dir_entry_ptr = ::readdir(dir_stream)) == nullptr) {
65     if (errno)
66       ec = capture_errno();
67     return {};
68   } else {
69     return {dir_entry_ptr->d_name, get_file_type(dir_entry_ptr, 0)};
70   }
71 }
72 #else
73 
74 static file_type get_file_type(const WIN32_FIND_DATA& data) {
75   //auto attrs = data.dwFileAttributes;
76   // FIXME(EricWF)
77   return file_type::unknown;
78 }
79 static uintmax_t get_file_size(const WIN32_FIND_DATA& data) {
80   return (data.nFileSizeHight * (MAXDWORD + 1)) + data.nFileSizeLow;
81 }
82 static file_time_type get_write_time(const WIN32_FIND_DATA& data) {
83   ULARGE_INTEGER tmp;
84   FILETIME& time = data.ftLastWriteTime;
85   tmp.u.LowPart = time.dwLowDateTime;
86   tmp.u.HighPart = time.dwHighDateTime;
87   return file_time_type(file_time_type::duration(time.QuadPart));
88 }
89 
90 #endif
91 
92 } // namespace
93 } // namespace detail
94 
95 using detail::ErrorHandler;
96 
97 #if defined(_LIBCPP_WIN32API)
98 class __dir_stream {
99 public:
100   __dir_stream() = delete;
101   __dir_stream& operator=(const __dir_stream&) = delete;
102 
__dir_stream(__dir_stream && __ds)103   __dir_stream(__dir_stream&& __ds) noexcept : __stream_(__ds.__stream_),
104                                                __root_(move(__ds.__root_)),
105                                                __entry_(move(__ds.__entry_)) {
106     __ds.__stream_ = INVALID_HANDLE_VALUE;
107   }
108 
__dir_stream(const path & root,directory_options opts,error_code & ec)109   __dir_stream(const path& root, directory_options opts, error_code& ec)
110       : __stream_(INVALID_HANDLE_VALUE), __root_(root) {
111     __stream_ = ::FindFirstFileEx(root.c_str(), &__data_);
112     if (__stream_ == INVALID_HANDLE_VALUE) {
113       ec = error_code(::GetLastError(), generic_category());
114       const bool ignore_permission_denied =
115           bool(opts & directory_options::skip_permission_denied);
116       if (ignore_permission_denied && ec.value() == ERROR_ACCESS_DENIED)
117         ec.clear();
118       return;
119     }
120   }
121 
~__dir_stream()122   ~__dir_stream() noexcept {
123     if (__stream_ == INVALID_HANDLE_VALUE)
124       return;
125     close();
126   }
127 
good() const128   bool good() const noexcept { return __stream_ != INVALID_HANDLE_VALUE; }
129 
advance(error_code & ec)130   bool advance(error_code& ec) {
131     while (::FindNextFile(__stream_, &__data_)) {
132       if (!strcmp(__data_.cFileName, ".") || strcmp(__data_.cFileName, ".."))
133         continue;
134       // FIXME: Cache more of this
135       //directory_entry::__cached_data cdata;
136       //cdata.__type_ = get_file_type(__data_);
137       //cdata.__size_ = get_file_size(__data_);
138       //cdata.__write_time_ = get_write_time(__data_);
139       __entry_.__assign_iter_entry(
140           __root_ / __data_.cFileName,
141           directory_entry::__create_iter_result(get_file_type(__data)));
142       return true;
143     }
144     ec = error_code(::GetLastError(), generic_category());
145     close();
146     return false;
147   }
148 
149 private:
close()150   error_code close() noexcept {
151     error_code ec;
152     if (!::FindClose(__stream_))
153       ec = error_code(::GetLastError(), generic_category());
154     __stream_ = INVALID_HANDLE_VALUE;
155     return ec;
156   }
157 
158   HANDLE __stream_{INVALID_HANDLE_VALUE};
159   WIN32_FIND_DATA __data_;
160 
161 public:
162   path __root_;
163   directory_entry __entry_;
164 };
165 #else
166 class __dir_stream {
167 public:
168   __dir_stream() = delete;
169   __dir_stream& operator=(const __dir_stream&) = delete;
170 
__dir_stream(__dir_stream && other)171   __dir_stream(__dir_stream&& other) noexcept : __stream_(other.__stream_),
172                                                 __root_(move(other.__root_)),
173                                                 __entry_(move(other.__entry_)) {
174     other.__stream_ = nullptr;
175   }
176 
__dir_stream(const path & root,directory_options opts,error_code & ec)177   __dir_stream(const path& root, directory_options opts, error_code& ec)
178       : __stream_(nullptr), __root_(root) {
179     if ((__stream_ = ::opendir(root.c_str())) == nullptr) {
180       ec = detail::capture_errno();
181       const bool allow_eacess =
182           bool(opts & directory_options::skip_permission_denied);
183       if (allow_eacess && ec.value() == EACCES)
184         ec.clear();
185       return;
186     }
187     advance(ec);
188   }
189 
~__dir_stream()190   ~__dir_stream() noexcept {
191     if (__stream_)
192       close();
193   }
194 
good() const195   bool good() const noexcept { return __stream_ != nullptr; }
196 
advance(error_code & ec)197   bool advance(error_code& ec) {
198     while (true) {
199       auto str_type_pair = detail::posix_readdir(__stream_, ec);
200       auto& str = str_type_pair.first;
201       if (str == "." || str == "..") {
202         continue;
203       } else if (ec || str.empty()) {
204         close();
205         return false;
206       } else {
207         __entry_.__assign_iter_entry(
208             __root_ / str,
209             directory_entry::__create_iter_result(str_type_pair.second));
210         return true;
211       }
212     }
213   }
214 
215 private:
close()216   error_code close() noexcept {
217     error_code m_ec;
218     if (::closedir(__stream_) == -1)
219       m_ec = detail::capture_errno();
220     __stream_ = nullptr;
221     return m_ec;
222   }
223 
224   DIR* __stream_{nullptr};
225 
226 public:
227   path __root_;
228   directory_entry __entry_;
229 };
230 #endif
231 
232 // directory_iterator
233 
directory_iterator(const path & p,error_code * ec,directory_options opts)234 directory_iterator::directory_iterator(const path& p, error_code* ec,
235                                        directory_options opts) {
236   ErrorHandler<void> err("directory_iterator::directory_iterator(...)", ec, &p);
237 
238   error_code m_ec;
239   __imp_ = make_shared<__dir_stream>(p, opts, m_ec);
240   if (ec)
241     *ec = m_ec;
242   if (!__imp_->good()) {
243     __imp_.reset();
244     if (m_ec)
245       err.report(m_ec);
246   }
247 }
248 
__increment(error_code * ec)249 directory_iterator& directory_iterator::__increment(error_code* ec) {
250   _LIBCPP_ASSERT(__imp_, "Attempting to increment an invalid iterator");
251   ErrorHandler<void> err("directory_iterator::operator++()", ec);
252 
253   error_code m_ec;
254   if (!__imp_->advance(m_ec)) {
255     path root = move(__imp_->__root_);
256     __imp_.reset();
257     if (m_ec)
258       err.report(m_ec, "at root \"%s\"", root);
259   }
260   return *this;
261 }
262 
__dereference() const263 directory_entry const& directory_iterator::__dereference() const {
264   _LIBCPP_ASSERT(__imp_, "Attempting to dereference an invalid iterator");
265   return __imp_->__entry_;
266 }
267 
268 // recursive_directory_iterator
269 
270 struct recursive_directory_iterator::__shared_imp {
271   stack<__dir_stream> __stack_;
272   directory_options __options_;
273 };
274 
recursive_directory_iterator(const path & p,directory_options opt,error_code * ec)275 recursive_directory_iterator::recursive_directory_iterator(
276     const path& p, directory_options opt, error_code* ec)
277     : __imp_(nullptr), __rec_(true) {
278   ErrorHandler<void> err("recursive_directory_iterator", ec, &p);
279 
280   error_code m_ec;
281   __dir_stream new_s(p, opt, m_ec);
282   if (m_ec)
283     err.report(m_ec);
284   if (m_ec || !new_s.good())
285     return;
286 
287   __imp_ = make_shared<__shared_imp>();
288   __imp_->__options_ = opt;
289   __imp_->__stack_.push(move(new_s));
290 }
291 
__pop(error_code * ec)292 void recursive_directory_iterator::__pop(error_code* ec) {
293   _LIBCPP_ASSERT(__imp_, "Popping the end iterator");
294   if (ec)
295     ec->clear();
296   __imp_->__stack_.pop();
297   if (__imp_->__stack_.size() == 0)
298     __imp_.reset();
299   else
300     __advance(ec);
301 }
302 
options() const303 directory_options recursive_directory_iterator::options() const {
304   return __imp_->__options_;
305 }
306 
depth() const307 int recursive_directory_iterator::depth() const {
308   return __imp_->__stack_.size() - 1;
309 }
310 
__dereference() const311 const directory_entry& recursive_directory_iterator::__dereference() const {
312   return __imp_->__stack_.top().__entry_;
313 }
314 
315 recursive_directory_iterator&
__increment(error_code * ec)316 recursive_directory_iterator::__increment(error_code* ec) {
317   if (ec)
318     ec->clear();
319   if (recursion_pending()) {
320     if (__try_recursion(ec) || (ec && *ec))
321       return *this;
322   }
323   __rec_ = true;
324   __advance(ec);
325   return *this;
326 }
327 
__advance(error_code * ec)328 void recursive_directory_iterator::__advance(error_code* ec) {
329   ErrorHandler<void> err("recursive_directory_iterator::operator++()", ec);
330 
331   const directory_iterator end_it;
332   auto& stack = __imp_->__stack_;
333   error_code m_ec;
334   while (stack.size() > 0) {
335     if (stack.top().advance(m_ec))
336       return;
337     if (m_ec)
338       break;
339     stack.pop();
340   }
341 
342   if (m_ec) {
343     path root = move(stack.top().__root_);
344     __imp_.reset();
345     err.report(m_ec, "at root \"%s\"", root);
346   } else {
347     __imp_.reset();
348   }
349 }
350 
__try_recursion(error_code * ec)351 bool recursive_directory_iterator::__try_recursion(error_code* ec) {
352   ErrorHandler<void> err("recursive_directory_iterator::operator++()", ec);
353 
354   bool rec_sym = bool(options() & directory_options::follow_directory_symlink);
355 
356   auto& curr_it = __imp_->__stack_.top();
357 
358   bool skip_rec = false;
359   error_code m_ec;
360   if (!rec_sym) {
361     file_status st(curr_it.__entry_.__get_sym_ft(&m_ec));
362     if (m_ec && status_known(st))
363       m_ec.clear();
364     if (m_ec || is_symlink(st) || !is_directory(st))
365       skip_rec = true;
366   } else {
367     file_status st(curr_it.__entry_.__get_ft(&m_ec));
368     if (m_ec && status_known(st))
369       m_ec.clear();
370     if (m_ec || !is_directory(st))
371       skip_rec = true;
372   }
373 
374   if (!skip_rec) {
375     __dir_stream new_it(curr_it.__entry_.path(), __imp_->__options_, m_ec);
376     if (new_it.good()) {
377       __imp_->__stack_.push(move(new_it));
378       return true;
379     }
380   }
381   if (m_ec) {
382     const bool allow_eacess =
383         bool(__imp_->__options_ & directory_options::skip_permission_denied);
384     if (m_ec.value() == EACCES && allow_eacess) {
385       if (ec)
386         ec->clear();
387     } else {
388       path at_ent = move(curr_it.__entry_.__p_);
389       __imp_.reset();
390       err.report(m_ec, "attempting recursion into \"%s\"", at_ent);
391     }
392   }
393   return false;
394 }
395 
396 }  // namespace android::hardware::automotive::filesystem
397 /* clang-format on */
398