Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / include / __filesystem / directory_entry.h
blob7d0c01b98def6b5dd544a287ecae88ff48ad9ca2
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___FILESYSTEM_DIRECTORY_ENTRY_H
11 #define _LIBCPP___FILESYSTEM_DIRECTORY_ENTRY_H
13 #include <__chrono/time_point.h>
14 #include <__compare/ordering.h>
15 #include <__config>
16 #include <__filesystem/file_status.h>
17 #include <__filesystem/file_time_type.h>
18 #include <__filesystem/file_type.h>
19 #include <__filesystem/filesystem_error.h>
20 #include <__filesystem/operations.h>
21 #include <__filesystem/path.h>
22 #include <__filesystem/perms.h>
23 #include <__fwd/ostream.h>
24 #include <__system_error/errc.h>
25 #include <__system_error/error_code.h>
26 #include <__utility/move.h>
27 #include <__utility/unreachable.h>
28 #include <cstdint>
30 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31 # pragma GCC system_header
32 #endif
34 _LIBCPP_PUSH_MACROS
35 #include <__undef_macros>
37 #if _LIBCPP_STD_VER >= 17 && _LIBCPP_HAS_FILESYSTEM
39 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
41 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH
43 class directory_entry {
44 typedef filesystem::path _Path;
46 public:
47 // constructors and destructors
48 _LIBCPP_HIDE_FROM_ABI directory_entry() noexcept = default;
49 _LIBCPP_HIDE_FROM_ABI directory_entry(directory_entry const&) = default;
50 _LIBCPP_HIDE_FROM_ABI directory_entry(directory_entry&&) noexcept = default;
52 _LIBCPP_HIDE_FROM_ABI explicit directory_entry(_Path const& __p) : __p_(__p) {
53 error_code __ec;
54 __refresh(&__ec);
57 _LIBCPP_HIDE_FROM_ABI directory_entry(_Path const& __p, error_code& __ec) : __p_(__p) { __refresh(&__ec); }
59 _LIBCPP_HIDE_FROM_ABI ~directory_entry() {}
61 _LIBCPP_HIDE_FROM_ABI directory_entry& operator=(directory_entry const&) = default;
62 _LIBCPP_HIDE_FROM_ABI directory_entry& operator=(directory_entry&&) noexcept = default;
64 _LIBCPP_HIDE_FROM_ABI void assign(_Path const& __p) {
65 __p_ = __p;
66 error_code __ec;
67 __refresh(&__ec);
70 _LIBCPP_HIDE_FROM_ABI void assign(_Path const& __p, error_code& __ec) {
71 __p_ = __p;
72 __refresh(&__ec);
75 _LIBCPP_HIDE_FROM_ABI void replace_filename(_Path const& __p) {
76 __p_.replace_filename(__p);
77 error_code __ec;
78 __refresh(&__ec);
81 _LIBCPP_HIDE_FROM_ABI void replace_filename(_Path const& __p, error_code& __ec) {
82 __p_ = __p_.parent_path() / __p;
83 __refresh(&__ec);
86 _LIBCPP_HIDE_FROM_ABI void refresh() { __refresh(); }
88 _LIBCPP_HIDE_FROM_ABI void refresh(error_code& __ec) noexcept { __refresh(&__ec); }
90 _LIBCPP_HIDE_FROM_ABI _Path const& path() const noexcept { return __p_; }
92 _LIBCPP_HIDE_FROM_ABI operator const _Path&() const noexcept { return __p_; }
94 _LIBCPP_HIDE_FROM_ABI bool exists() const { return filesystem::exists(file_status{__get_ft()}); }
96 _LIBCPP_HIDE_FROM_ABI bool exists(error_code& __ec) const noexcept {
97 return filesystem::exists(file_status{__get_ft(&__ec)});
100 _LIBCPP_HIDE_FROM_ABI bool is_block_file() const { return __get_ft() == file_type::block; }
102 _LIBCPP_HIDE_FROM_ABI bool is_block_file(error_code& __ec) const noexcept {
103 return __get_ft(&__ec) == file_type::block;
106 _LIBCPP_HIDE_FROM_ABI bool is_character_file() const { return __get_ft() == file_type::character; }
108 _LIBCPP_HIDE_FROM_ABI bool is_character_file(error_code& __ec) const noexcept {
109 return __get_ft(&__ec) == file_type::character;
112 _LIBCPP_HIDE_FROM_ABI bool is_directory() const { return __get_ft() == file_type::directory; }
114 _LIBCPP_HIDE_FROM_ABI bool is_directory(error_code& __ec) const noexcept {
115 return __get_ft(&__ec) == file_type::directory;
118 _LIBCPP_HIDE_FROM_ABI bool is_fifo() const { return __get_ft() == file_type::fifo; }
120 _LIBCPP_HIDE_FROM_ABI bool is_fifo(error_code& __ec) const noexcept { return __get_ft(&__ec) == file_type::fifo; }
122 _LIBCPP_HIDE_FROM_ABI bool is_other() const { return filesystem::is_other(file_status{__get_ft()}); }
124 _LIBCPP_HIDE_FROM_ABI bool is_other(error_code& __ec) const noexcept {
125 return filesystem::is_other(file_status{__get_ft(&__ec)});
128 _LIBCPP_HIDE_FROM_ABI bool is_regular_file() const { return __get_ft() == file_type::regular; }
130 _LIBCPP_HIDE_FROM_ABI bool is_regular_file(error_code& __ec) const noexcept {
131 return __get_ft(&__ec) == file_type::regular;
134 _LIBCPP_HIDE_FROM_ABI bool is_socket() const { return __get_ft() == file_type::socket; }
136 _LIBCPP_HIDE_FROM_ABI bool is_socket(error_code& __ec) const noexcept { return __get_ft(&__ec) == file_type::socket; }
138 _LIBCPP_HIDE_FROM_ABI bool is_symlink() const { return __get_sym_ft() == file_type::symlink; }
140 _LIBCPP_HIDE_FROM_ABI bool is_symlink(error_code& __ec) const noexcept {
141 return __get_sym_ft(&__ec) == file_type::symlink;
143 _LIBCPP_HIDE_FROM_ABI uintmax_t file_size() const { return __get_size(); }
145 _LIBCPP_HIDE_FROM_ABI uintmax_t file_size(error_code& __ec) const noexcept { return __get_size(&__ec); }
147 _LIBCPP_HIDE_FROM_ABI uintmax_t hard_link_count() const { return __get_nlink(); }
149 _LIBCPP_HIDE_FROM_ABI uintmax_t hard_link_count(error_code& __ec) const noexcept { return __get_nlink(&__ec); }
151 _LIBCPP_HIDE_FROM_ABI file_time_type last_write_time() const { return __get_write_time(); }
153 _LIBCPP_HIDE_FROM_ABI file_time_type last_write_time(error_code& __ec) const noexcept {
154 return __get_write_time(&__ec);
157 _LIBCPP_HIDE_FROM_ABI file_status status() const { return __get_status(); }
159 _LIBCPP_HIDE_FROM_ABI file_status status(error_code& __ec) const noexcept { return __get_status(&__ec); }
161 _LIBCPP_HIDE_FROM_ABI file_status symlink_status() const { return __get_symlink_status(); }
163 _LIBCPP_HIDE_FROM_ABI file_status symlink_status(error_code& __ec) const noexcept {
164 return __get_symlink_status(&__ec);
167 _LIBCPP_HIDE_FROM_ABI bool operator==(directory_entry const& __rhs) const noexcept { return __p_ == __rhs.__p_; }
169 # if _LIBCPP_STD_VER <= 17
170 _LIBCPP_HIDE_FROM_ABI bool operator!=(directory_entry const& __rhs) const noexcept { return __p_ != __rhs.__p_; }
172 _LIBCPP_HIDE_FROM_ABI bool operator<(directory_entry const& __rhs) const noexcept { return __p_ < __rhs.__p_; }
174 _LIBCPP_HIDE_FROM_ABI bool operator<=(directory_entry const& __rhs) const noexcept { return __p_ <= __rhs.__p_; }
176 _LIBCPP_HIDE_FROM_ABI bool operator>(directory_entry const& __rhs) const noexcept { return __p_ > __rhs.__p_; }
178 _LIBCPP_HIDE_FROM_ABI bool operator>=(directory_entry const& __rhs) const noexcept { return __p_ >= __rhs.__p_; }
180 # else // _LIBCPP_STD_VER <= 17
182 _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(const directory_entry& __rhs) const noexcept {
183 return __p_ <=> __rhs.__p_;
186 # endif // _LIBCPP_STD_VER <= 17
188 template <class _CharT, class _Traits>
189 _LIBCPP_HIDE_FROM_ABI friend basic_ostream<_CharT, _Traits>&
190 operator<<(basic_ostream<_CharT, _Traits>& __os, const directory_entry& __d) {
191 return __os << __d.path();
194 private:
195 friend class directory_iterator;
196 friend class recursive_directory_iterator;
197 friend class _LIBCPP_HIDDEN __dir_stream;
199 enum _CacheType : unsigned char {
200 _Empty,
201 _IterSymlink,
202 _IterNonSymlink,
203 _RefreshSymlink,
204 _RefreshSymlinkUnresolved,
205 _RefreshNonSymlink,
206 _IterCachedSymlink,
207 _IterCachedNonSymlink
210 struct __cached_data {
211 uintmax_t __size_;
212 uintmax_t __nlink_;
213 file_time_type __write_time_;
214 perms __sym_perms_;
215 perms __non_sym_perms_;
216 file_type __type_;
217 _CacheType __cache_type_;
219 _LIBCPP_HIDE_FROM_ABI __cached_data() noexcept { __reset(); }
221 _LIBCPP_HIDE_FROM_ABI void __reset() {
222 __cache_type_ = _Empty;
223 __type_ = file_type::none;
224 __sym_perms_ = __non_sym_perms_ = perms::unknown;
225 __size_ = __nlink_ = uintmax_t(-1);
226 __write_time_ = file_time_type::min();
230 _LIBCPP_HIDE_FROM_ABI static __cached_data __create_iter_result(file_type __ft) {
231 __cached_data __data;
232 __data.__type_ = __ft;
233 __data.__cache_type_ = [&]() {
234 switch (__ft) {
235 case file_type::none:
236 return _Empty;
237 case file_type::symlink:
238 return _IterSymlink;
239 default:
240 return _IterNonSymlink;
242 }();
243 return __data;
246 _LIBCPP_HIDE_FROM_ABI static __cached_data
247 __create_iter_cached_result(file_type __ft, uintmax_t __size, perms __perm, file_time_type __write_time) {
248 __cached_data __data;
249 __data.__type_ = __ft;
250 __data.__size_ = __size;
251 __data.__write_time_ = __write_time;
252 if (__ft == file_type::symlink)
253 __data.__sym_perms_ = __perm;
254 else
255 __data.__non_sym_perms_ = __perm;
256 __data.__cache_type_ = [&]() {
257 switch (__ft) {
258 case file_type::none:
259 return _Empty;
260 case file_type::symlink:
261 return _IterCachedSymlink;
262 default:
263 return _IterCachedNonSymlink;
265 }();
266 return __data;
269 _LIBCPP_HIDE_FROM_ABI void __assign_iter_entry(_Path&& __p, __cached_data __dt) {
270 __p_ = std::move(__p);
271 __data_ = __dt;
274 _LIBCPP_EXPORTED_FROM_ABI error_code __do_refresh() noexcept;
276 _LIBCPP_HIDE_FROM_ABI static bool __is_dne_error(error_code const& __ec) {
277 if (!__ec)
278 return true;
279 switch (static_cast<errc>(__ec.value())) {
280 case errc::no_such_file_or_directory:
281 case errc::not_a_directory:
282 return true;
283 default:
284 return false;
288 _LIBCPP_HIDE_FROM_ABI void
289 __handle_error(const char* __msg, error_code* __dest_ec, error_code const& __ec, bool __allow_dne = false) const {
290 if (__dest_ec) {
291 *__dest_ec = __ec;
292 return;
294 if (__ec && (!__allow_dne || !__is_dne_error(__ec)))
295 __throw_filesystem_error(__msg, __p_, __ec);
298 _LIBCPP_HIDE_FROM_ABI void __refresh(error_code* __ec = nullptr) {
299 __handle_error("in directory_entry::refresh",
300 __ec,
301 __do_refresh(),
302 /*allow_dne*/ true);
305 _LIBCPP_HIDE_FROM_ABI file_type __get_sym_ft(error_code* __ec = nullptr) const {
306 switch (__data_.__cache_type_) {
307 case _Empty:
308 return __symlink_status(__p_, __ec).type();
309 case _IterSymlink:
310 case _IterCachedSymlink:
311 case _RefreshSymlink:
312 case _RefreshSymlinkUnresolved:
313 if (__ec)
314 __ec->clear();
315 return file_type::symlink;
316 case _IterCachedNonSymlink:
317 case _IterNonSymlink:
318 case _RefreshNonSymlink: {
319 file_status __st(__data_.__type_);
320 if (__ec && !filesystem::exists(__st))
321 *__ec = make_error_code(errc::no_such_file_or_directory);
322 else if (__ec)
323 __ec->clear();
324 return __data_.__type_;
327 __libcpp_unreachable();
330 _LIBCPP_HIDE_FROM_ABI file_type __get_ft(error_code* __ec = nullptr) const {
331 switch (__data_.__cache_type_) {
332 case _Empty:
333 case _IterSymlink:
334 case _IterCachedSymlink:
335 case _RefreshSymlinkUnresolved:
336 return __status(__p_, __ec).type();
337 case _IterCachedNonSymlink:
338 case _IterNonSymlink:
339 case _RefreshNonSymlink:
340 case _RefreshSymlink: {
341 file_status __st(__data_.__type_);
342 if (__ec && !filesystem::exists(__st))
343 *__ec = make_error_code(errc::no_such_file_or_directory);
344 else if (__ec)
345 __ec->clear();
346 return __data_.__type_;
349 __libcpp_unreachable();
352 _LIBCPP_HIDE_FROM_ABI file_status __get_status(error_code* __ec = nullptr) const {
353 switch (__data_.__cache_type_) {
354 case _Empty:
355 case _IterNonSymlink:
356 case _IterSymlink:
357 case _IterCachedSymlink:
358 case _RefreshSymlinkUnresolved:
359 return __status(__p_, __ec);
360 case _IterCachedNonSymlink:
361 case _RefreshNonSymlink:
362 case _RefreshSymlink:
363 return file_status(__get_ft(__ec), __data_.__non_sym_perms_);
365 __libcpp_unreachable();
368 _LIBCPP_HIDE_FROM_ABI file_status __get_symlink_status(error_code* __ec = nullptr) const {
369 switch (__data_.__cache_type_) {
370 case _Empty:
371 case _IterNonSymlink:
372 case _IterSymlink:
373 return __symlink_status(__p_, __ec);
374 case _IterCachedNonSymlink:
375 case _RefreshNonSymlink:
376 return file_status(__get_sym_ft(__ec), __data_.__non_sym_perms_);
377 case _IterCachedSymlink:
378 case _RefreshSymlink:
379 case _RefreshSymlinkUnresolved:
380 return file_status(__get_sym_ft(__ec), __data_.__sym_perms_);
382 __libcpp_unreachable();
385 _LIBCPP_HIDE_FROM_ABI uintmax_t __get_size(error_code* __ec = nullptr) const {
386 switch (__data_.__cache_type_) {
387 case _Empty:
388 case _IterNonSymlink:
389 case _IterSymlink:
390 case _IterCachedSymlink:
391 case _RefreshSymlinkUnresolved:
392 return filesystem::__file_size(__p_, __ec);
393 case _IterCachedNonSymlink:
394 case _RefreshSymlink:
395 case _RefreshNonSymlink: {
396 error_code __m_ec;
397 file_status __st(__get_ft(&__m_ec));
398 __handle_error("in directory_entry::file_size", __ec, __m_ec);
399 if (filesystem::exists(__st) && !filesystem::is_regular_file(__st)) {
400 errc __err_kind = filesystem::is_directory(__st) ? errc::is_a_directory : errc::not_supported;
401 __handle_error("in directory_entry::file_size", __ec, make_error_code(__err_kind));
403 return __data_.__size_;
406 __libcpp_unreachable();
409 _LIBCPP_HIDE_FROM_ABI uintmax_t __get_nlink(error_code* __ec = nullptr) const {
410 switch (__data_.__cache_type_) {
411 case _Empty:
412 case _IterNonSymlink:
413 case _IterSymlink:
414 case _IterCachedNonSymlink:
415 case _IterCachedSymlink:
416 case _RefreshSymlinkUnresolved:
417 return filesystem::__hard_link_count(__p_, __ec);
418 case _RefreshSymlink:
419 case _RefreshNonSymlink: {
420 error_code __m_ec;
421 (void)__get_ft(&__m_ec);
422 __handle_error("in directory_entry::hard_link_count", __ec, __m_ec);
423 return __data_.__nlink_;
426 __libcpp_unreachable();
429 _LIBCPP_HIDE_FROM_ABI file_time_type __get_write_time(error_code* __ec = nullptr) const {
430 switch (__data_.__cache_type_) {
431 case _Empty:
432 case _IterNonSymlink:
433 case _IterSymlink:
434 case _IterCachedSymlink:
435 case _RefreshSymlinkUnresolved:
436 return filesystem::__last_write_time(__p_, __ec);
437 case _IterCachedNonSymlink:
438 case _RefreshSymlink:
439 case _RefreshNonSymlink: {
440 error_code __m_ec;
441 file_status __st(__get_ft(&__m_ec));
442 __handle_error("in directory_entry::last_write_time", __ec, __m_ec);
443 if (filesystem::exists(__st) && __data_.__write_time_ == file_time_type::min())
444 __handle_error("in directory_entry::last_write_time", __ec, make_error_code(errc::value_too_large));
445 return __data_.__write_time_;
448 __libcpp_unreachable();
451 private:
452 _Path __p_;
453 __cached_data __data_;
456 class __dir_element_proxy {
457 public:
458 inline _LIBCPP_HIDE_FROM_ABI directory_entry operator*() { return std::move(__elem_); }
460 private:
461 friend class directory_iterator;
462 friend class recursive_directory_iterator;
463 _LIBCPP_HIDE_FROM_ABI explicit __dir_element_proxy(directory_entry const& __e) : __elem_(__e) {}
464 _LIBCPP_HIDE_FROM_ABI __dir_element_proxy(__dir_element_proxy&& __o) : __elem_(std::move(__o.__elem_)) {}
465 directory_entry __elem_;
468 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
470 _LIBCPP_END_NAMESPACE_FILESYSTEM
472 #endif // _LIBCPP_STD_VER >= 17 && _LIBCPP_HAS_FILESYSTEM
474 _LIBCPP_POP_MACROS
476 #endif // _LIBCPP___FILESYSTEM_DIRECTORY_ENTRY_H