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