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