1 //===------------------ directory_iterator.cpp ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #if defined(_LIBCPP_WIN32API)
13 #define WIN32_LEAN_AND_MEAN
20 #include "filesystem_common.h"
22 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
27 #if !defined(_LIBCPP_WIN32API)
28 template <class DirEntT, class = decltype(DirEntT::d_type)>
29 static file_type get_file_type(DirEntT* ent, int) {
30 switch (ent->d_type) {
32 return file_type::block;
34 return file_type::character;
36 return file_type::directory;
38 return file_type::fifo;
40 return file_type::symlink;
42 return file_type::regular;
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.
51 return file_type::none;
54 template <class DirEntT>
55 static file_type get_file_type(DirEntT* ent, long) {
56 return file_type::none;
59 static pair<string_view, file_type> posix_readdir(DIR* dir_stream,
61 struct dirent* dir_entry_ptr = nullptr;
62 errno = 0; // zero errno in order to detect errors
64 if ((dir_entry_ptr = ::readdir(dir_stream)) == nullptr) {
69 return {dir_entry_ptr->d_name, get_file_type(dir_entry_ptr, 0)};
74 static file_type get_file_type(const WIN32_FIND_DATA& data) {
75 //auto attrs = data.dwFileAttributes;
77 return file_type::unknown;
79 static uintmax_t get_file_size(const WIN32_FIND_DATA& data) {
80 return (data.nFileSizeHight * (MAXDWORD + 1)) + data.nFileSizeLow;
82 static file_time_type get_write_time(const WIN32_FIND_DATA& data) {
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));
95 using detail::ErrorHandler;
97 #if defined(_LIBCPP_WIN32API)
100 __dir_stream() = delete;
101 __dir_stream& operator=(const __dir_stream&) = delete;
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;
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)
122 ~__dir_stream() noexcept {
123 if (__stream_ == INVALID_HANDLE_VALUE)
128 bool good() const noexcept { return __stream_ != INVALID_HANDLE_VALUE; }
130 bool advance(error_code& ec) {
131 while (::FindNextFile(__stream_, &__data_)) {
132 if (!strcmp(__data_.cFileName, ".") || strcmp(__data_.cFileName, ".."))
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)));
144 ec = error_code(::GetLastError(), generic_category());
150 error_code close() noexcept {
152 if (!::FindClose(__stream_))
153 ec = error_code(::GetLastError(), generic_category());
154 __stream_ = INVALID_HANDLE_VALUE;
158 HANDLE __stream_{INVALID_HANDLE_VALUE};
159 WIN32_FIND_DATA __data_;
163 directory_entry __entry_;
168 __dir_stream() = delete;
169 __dir_stream& operator=(const __dir_stream&) = delete;
171 __dir_stream(__dir_stream&& other) noexcept : __stream_(other.__stream_),
172 __root_(move(other.__root_)),
173 __entry_(move(other.__entry_)) {
174 other.__stream_ = nullptr;
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)
190 ~__dir_stream() noexcept {
195 bool good() const noexcept { return __stream_ != nullptr; }
197 bool advance(error_code& ec) {
199 auto str_type_pair = detail::posix_readdir(__stream_, ec);
200 auto& str = str_type_pair.first;
201 if (str == "." || str == "..") {
203 } else if (ec || str.empty()) {
207 __entry_.__assign_iter_entry(
209 directory_entry::__create_iter_result(str_type_pair.second));
216 error_code close() noexcept {
218 if (::closedir(__stream_) == -1)
219 m_ec = detail::capture_errno();
224 DIR* __stream_{nullptr};
228 directory_entry __entry_;
232 // directory_iterator
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);
239 __imp_ = make_shared<__dir_stream>(p, opts, m_ec);
242 if (!__imp_->good()) {
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);
254 if (!__imp_->advance(m_ec)) {
255 path root = move(__imp_->__root_);
258 err.report(m_ec, "at root \"%s\"", root);
263 directory_entry const& directory_iterator::__dereference() const {
264 _LIBCPP_ASSERT(__imp_, "Attempting to dereference an invalid iterator");
265 return __imp_->__entry_;
268 // recursive_directory_iterator
270 struct recursive_directory_iterator::__shared_imp {
271 stack<__dir_stream> __stack_;
272 directory_options __options_;
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);
281 __dir_stream new_s(p, opt, m_ec);
284 if (m_ec || !new_s.good())
287 __imp_ = make_shared<__shared_imp>();
288 __imp_->__options_ = opt;
289 __imp_->__stack_.push(move(new_s));
292 void recursive_directory_iterator::__pop(error_code* ec) {
293 _LIBCPP_ASSERT(__imp_, "Popping the end iterator");
296 __imp_->__stack_.pop();
297 if (__imp_->__stack_.size() == 0)
303 directory_options recursive_directory_iterator::options() const {
304 return __imp_->__options_;
307 int recursive_directory_iterator::depth() const {
308 return __imp_->__stack_.size() - 1;
311 const directory_entry& recursive_directory_iterator::__dereference() const {
312 return __imp_->__stack_.top().__entry_;
315 recursive_directory_iterator&
316 recursive_directory_iterator::__increment(error_code* ec) {
319 if (recursion_pending()) {
320 if (__try_recursion(ec) || (ec && *ec))
328 void recursive_directory_iterator::__advance(error_code* ec) {
329 ErrorHandler<void> err("recursive_directory_iterator::operator++()", ec);
331 const directory_iterator end_it;
332 auto& stack = __imp_->__stack_;
334 while (stack.size() > 0) {
335 if (stack.top().advance(m_ec))
343 path root = move(stack.top().__root_);
345 err.report(m_ec, "at root \"%s\"", root);
351 bool recursive_directory_iterator::__try_recursion(error_code* ec) {
352 ErrorHandler<void> err("recursive_directory_iterator::operator++()", ec);
354 bool rec_sym = bool(options() & directory_options::follow_directory_symlink);
356 auto& curr_it = __imp_->__stack_.top();
358 bool skip_rec = false;
361 file_status st(curr_it.__entry_.__get_sym_ft(&m_ec));
362 if (m_ec && status_known(st))
364 if (m_ec || is_symlink(st) || !is_directory(st))
367 file_status st(curr_it.__entry_.__get_ft(&m_ec));
368 if (m_ec && status_known(st))
370 if (m_ec || !is_directory(st))
375 __dir_stream new_it(curr_it.__entry_.path(), __imp_->__options_, m_ec);
377 __imp_->__stack_.push(move(new_it));
382 const bool allow_eacess =
383 bool(__imp_->__options_ & directory_options::skip_permission_denied);
384 if (m_ec.value() == EACCES && allow_eacess) {
388 path at_ent = move(curr_it.__entry_.__p_);
390 err.report(m_ec, "attempting recursion into \"%s\"", at_ent);
396 _LIBCPP_END_NAMESPACE_FILESYSTEM