2 //===----------------------------------------------------------------------===//
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
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>
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_category.h>
26 #include <__system_error/error_code.h>
27 #include <__system_error/error_condition.h>
28 #include <__utility/move.h>
29 #include <__utility/unreachable.h>
32 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
33 # pragma GCC system_header
37 #include <__undef_macros>
39 #if _LIBCPP_STD_VER >= 17 && _LIBCPP_HAS_FILESYSTEM
41 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
43 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH
45 class directory_entry
{
46 typedef filesystem::path _Path
;
49 // constructors and destructors
50 _LIBCPP_HIDE_FROM_ABI
directory_entry() noexcept
= default;
51 _LIBCPP_HIDE_FROM_ABI
directory_entry(directory_entry
const&) = default;
52 _LIBCPP_HIDE_FROM_ABI
directory_entry(directory_entry
&&) noexcept
= default;
54 _LIBCPP_HIDE_FROM_ABI
explicit directory_entry(_Path
const& __p
) : __p_(__p
) {
59 _LIBCPP_HIDE_FROM_ABI
directory_entry(_Path
const& __p
, error_code
& __ec
) : __p_(__p
) { __refresh(&__ec
); }
61 _LIBCPP_HIDE_FROM_ABI
~directory_entry() {}
63 _LIBCPP_HIDE_FROM_ABI directory_entry
& operator=(directory_entry
const&) = default;
64 _LIBCPP_HIDE_FROM_ABI directory_entry
& operator=(directory_entry
&&) noexcept
= default;
66 _LIBCPP_HIDE_FROM_ABI
void assign(_Path
const& __p
) {
72 _LIBCPP_HIDE_FROM_ABI
void assign(_Path
const& __p
, error_code
& __ec
) {
77 _LIBCPP_HIDE_FROM_ABI
void replace_filename(_Path
const& __p
) {
78 __p_
.replace_filename(__p
);
83 _LIBCPP_HIDE_FROM_ABI
void replace_filename(_Path
const& __p
, error_code
& __ec
) {
84 __p_
= __p_
.parent_path() / __p
;
88 _LIBCPP_HIDE_FROM_ABI
void refresh() { __refresh(); }
90 _LIBCPP_HIDE_FROM_ABI
void refresh(error_code
& __ec
) noexcept
{ __refresh(&__ec
); }
92 _LIBCPP_HIDE_FROM_ABI _Path
const& path() const noexcept
{ return __p_
; }
94 _LIBCPP_HIDE_FROM_ABI
operator const _Path
&() const noexcept
{ return __p_
; }
96 _LIBCPP_HIDE_FROM_ABI
bool exists() const { return filesystem::exists(file_status
{__get_ft()}); }
98 _LIBCPP_HIDE_FROM_ABI
bool exists(error_code
& __ec
) const noexcept
{
99 return filesystem::exists(file_status
{__get_ft(&__ec
)});
102 _LIBCPP_HIDE_FROM_ABI
bool is_block_file() const { return __get_ft() == file_type::block
; }
104 _LIBCPP_HIDE_FROM_ABI
bool is_block_file(error_code
& __ec
) const noexcept
{
105 return __get_ft(&__ec
) == file_type::block
;
108 _LIBCPP_HIDE_FROM_ABI
bool is_character_file() const { return __get_ft() == file_type::character
; }
110 _LIBCPP_HIDE_FROM_ABI
bool is_character_file(error_code
& __ec
) const noexcept
{
111 return __get_ft(&__ec
) == file_type::character
;
114 _LIBCPP_HIDE_FROM_ABI
bool is_directory() const { return __get_ft() == file_type::directory
; }
116 _LIBCPP_HIDE_FROM_ABI
bool is_directory(error_code
& __ec
) const noexcept
{
117 return __get_ft(&__ec
) == file_type::directory
;
120 _LIBCPP_HIDE_FROM_ABI
bool is_fifo() const { return __get_ft() == file_type::fifo
; }
122 _LIBCPP_HIDE_FROM_ABI
bool is_fifo(error_code
& __ec
) const noexcept
{ return __get_ft(&__ec
) == file_type::fifo
; }
124 _LIBCPP_HIDE_FROM_ABI
bool is_other() const { return filesystem::is_other(file_status
{__get_ft()}); }
126 _LIBCPP_HIDE_FROM_ABI
bool is_other(error_code
& __ec
) const noexcept
{
127 return filesystem::is_other(file_status
{__get_ft(&__ec
)});
130 _LIBCPP_HIDE_FROM_ABI
bool is_regular_file() const { return __get_ft() == file_type::regular
; }
132 _LIBCPP_HIDE_FROM_ABI
bool is_regular_file(error_code
& __ec
) const noexcept
{
133 return __get_ft(&__ec
) == file_type::regular
;
136 _LIBCPP_HIDE_FROM_ABI
bool is_socket() const { return __get_ft() == file_type::socket
; }
138 _LIBCPP_HIDE_FROM_ABI
bool is_socket(error_code
& __ec
) const noexcept
{ return __get_ft(&__ec
) == file_type::socket
; }
140 _LIBCPP_HIDE_FROM_ABI
bool is_symlink() const { return __get_sym_ft() == file_type::symlink
; }
142 _LIBCPP_HIDE_FROM_ABI
bool is_symlink(error_code
& __ec
) const noexcept
{
143 return __get_sym_ft(&__ec
) == file_type::symlink
;
145 _LIBCPP_HIDE_FROM_ABI
uintmax_t file_size() const { return __get_size(); }
147 _LIBCPP_HIDE_FROM_ABI
uintmax_t file_size(error_code
& __ec
) const noexcept
{ return __get_size(&__ec
); }
149 _LIBCPP_HIDE_FROM_ABI
uintmax_t hard_link_count() const { return __get_nlink(); }
151 _LIBCPP_HIDE_FROM_ABI
uintmax_t hard_link_count(error_code
& __ec
) const noexcept
{ return __get_nlink(&__ec
); }
153 _LIBCPP_HIDE_FROM_ABI file_time_type
last_write_time() const { return __get_write_time(); }
155 _LIBCPP_HIDE_FROM_ABI file_time_type
last_write_time(error_code
& __ec
) const noexcept
{
156 return __get_write_time(&__ec
);
159 _LIBCPP_HIDE_FROM_ABI file_status
status() const { return __get_status(); }
161 _LIBCPP_HIDE_FROM_ABI file_status
status(error_code
& __ec
) const noexcept
{ return __get_status(&__ec
); }
163 _LIBCPP_HIDE_FROM_ABI file_status
symlink_status() const { return __get_symlink_status(); }
165 _LIBCPP_HIDE_FROM_ABI file_status
symlink_status(error_code
& __ec
) const noexcept
{
166 return __get_symlink_status(&__ec
);
169 _LIBCPP_HIDE_FROM_ABI
bool operator==(directory_entry
const& __rhs
) const noexcept
{ return __p_
== __rhs
.__p_
; }
171 # if _LIBCPP_STD_VER <= 17
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 _LIBCPP_HIDE_FROM_ABI
bool operator>=(directory_entry
const& __rhs
) const noexcept
{ return __p_
>= __rhs
.__p_
; }
182 # else // _LIBCPP_STD_VER <= 17
184 _LIBCPP_HIDE_FROM_ABI strong_ordering
operator<=>(const directory_entry
& __rhs
) const noexcept
{
185 return __p_
<=> __rhs
.__p_
;
188 # endif // _LIBCPP_STD_VER <= 17
190 template <class _CharT
, class _Traits
>
191 _LIBCPP_HIDE_FROM_ABI
friend basic_ostream
<_CharT
, _Traits
>&
192 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const directory_entry
& __d
) {
193 return __os
<< __d
.path();
197 friend class directory_iterator
;
198 friend class recursive_directory_iterator
;
199 friend class _LIBCPP_HIDDEN __dir_stream
;
201 enum _CacheType
: unsigned char {
206 _RefreshSymlinkUnresolved
,
209 _IterCachedNonSymlink
212 struct __cached_data
{
215 file_time_type __write_time_
;
217 perms __non_sym_perms_
;
219 _CacheType __cache_type_
;
221 _LIBCPP_HIDE_FROM_ABI
__cached_data() noexcept
{ __reset(); }
223 _LIBCPP_HIDE_FROM_ABI
void __reset() {
224 __cache_type_
= _Empty
;
225 __type_
= file_type::none
;
226 __sym_perms_
= __non_sym_perms_
= perms::unknown
;
227 __size_
= __nlink_
= uintmax_t(-1);
228 __write_time_
= file_time_type::min();
232 _LIBCPP_HIDE_FROM_ABI
static __cached_data
__create_iter_result(file_type __ft
) {
233 __cached_data __data
;
234 __data
.__type_
= __ft
;
235 __data
.__cache_type_
= [&]() {
237 case file_type::none
:
239 case file_type::symlink
:
242 return _IterNonSymlink
;
248 _LIBCPP_HIDE_FROM_ABI
static __cached_data
249 __create_iter_cached_result(file_type __ft
, uintmax_t __size
, perms __perm
, file_time_type __write_time
) {
250 __cached_data __data
;
251 __data
.__type_
= __ft
;
252 __data
.__size_
= __size
;
253 __data
.__write_time_
= __write_time
;
254 if (__ft
== file_type::symlink
)
255 __data
.__sym_perms_
= __perm
;
257 __data
.__non_sym_perms_
= __perm
;
258 __data
.__cache_type_
= [&]() {
260 case file_type::none
:
262 case file_type::symlink
:
263 return _IterCachedSymlink
;
265 return _IterCachedNonSymlink
;
271 _LIBCPP_HIDE_FROM_ABI
void __assign_iter_entry(_Path
&& __p
, __cached_data __dt
) {
272 __p_
= std::move(__p
);
276 _LIBCPP_EXPORTED_FROM_ABI error_code
__do_refresh() noexcept
;
278 _LIBCPP_HIDE_FROM_ABI
static bool __is_dne_error(error_code
const& __ec
) {
279 return !__ec
|| __ec
== errc::no_such_file_or_directory
|| __ec
== errc::not_a_directory
;
282 _LIBCPP_HIDE_FROM_ABI
void
283 __handle_error(const char* __msg
, error_code
* __dest_ec
, error_code
const& __ec
, bool __allow_dne
= false) const {
288 if (__ec
&& (!__allow_dne
|| !__is_dne_error(__ec
)))
289 __throw_filesystem_error(__msg
, __p_
, __ec
);
292 _LIBCPP_HIDE_FROM_ABI
void __refresh(error_code
* __ec
= nullptr) {
293 __handle_error("in directory_entry::refresh",
299 _LIBCPP_HIDE_FROM_ABI file_type
__get_sym_ft(error_code
* __ec
= nullptr) const {
300 switch (__data_
.__cache_type_
) {
302 return __symlink_status(__p_
, __ec
).type();
304 case _IterCachedSymlink
:
305 case _RefreshSymlink
:
306 case _RefreshSymlinkUnresolved
:
309 return file_type::symlink
;
310 case _IterCachedNonSymlink
:
311 case _IterNonSymlink
:
312 case _RefreshNonSymlink
: {
313 file_status
__st(__data_
.__type_
);
314 if (__ec
&& !filesystem::exists(__st
))
315 *__ec
= make_error_code(errc::no_such_file_or_directory
);
318 return __data_
.__type_
;
321 __libcpp_unreachable();
324 _LIBCPP_HIDE_FROM_ABI file_type
__get_ft(error_code
* __ec
= nullptr) const {
325 switch (__data_
.__cache_type_
) {
328 case _IterCachedSymlink
:
329 case _RefreshSymlinkUnresolved
:
330 return __status(__p_
, __ec
).type();
331 case _IterCachedNonSymlink
:
332 case _IterNonSymlink
:
333 case _RefreshNonSymlink
:
334 case _RefreshSymlink
: {
335 file_status
__st(__data_
.__type_
);
336 if (__ec
&& !filesystem::exists(__st
))
337 *__ec
= make_error_code(errc::no_such_file_or_directory
);
340 return __data_
.__type_
;
343 __libcpp_unreachable();
346 _LIBCPP_HIDE_FROM_ABI file_status
__get_status(error_code
* __ec
= nullptr) const {
347 switch (__data_
.__cache_type_
) {
349 case _IterNonSymlink
:
351 case _IterCachedSymlink
:
352 case _RefreshSymlinkUnresolved
:
353 return __status(__p_
, __ec
);
354 case _IterCachedNonSymlink
:
355 case _RefreshNonSymlink
:
356 case _RefreshSymlink
:
357 return file_status(__get_ft(__ec
), __data_
.__non_sym_perms_
);
359 __libcpp_unreachable();
362 _LIBCPP_HIDE_FROM_ABI file_status
__get_symlink_status(error_code
* __ec
= nullptr) const {
363 switch (__data_
.__cache_type_
) {
365 case _IterNonSymlink
:
367 return __symlink_status(__p_
, __ec
);
368 case _IterCachedNonSymlink
:
369 case _RefreshNonSymlink
:
370 return file_status(__get_sym_ft(__ec
), __data_
.__non_sym_perms_
);
371 case _IterCachedSymlink
:
372 case _RefreshSymlink
:
373 case _RefreshSymlinkUnresolved
:
374 return file_status(__get_sym_ft(__ec
), __data_
.__sym_perms_
);
376 __libcpp_unreachable();
379 _LIBCPP_HIDE_FROM_ABI
uintmax_t __get_size(error_code
* __ec
= nullptr) const {
380 switch (__data_
.__cache_type_
) {
382 case _IterNonSymlink
:
384 case _IterCachedSymlink
:
385 case _RefreshSymlinkUnresolved
:
386 return filesystem::__file_size(__p_
, __ec
);
387 case _IterCachedNonSymlink
:
388 case _RefreshSymlink
:
389 case _RefreshNonSymlink
: {
391 file_status
__st(__get_ft(&__m_ec
));
392 __handle_error("in directory_entry::file_size", __ec
, __m_ec
);
393 if (filesystem::exists(__st
) && !filesystem::is_regular_file(__st
)) {
394 errc __err_kind
= filesystem::is_directory(__st
) ? errc::is_a_directory
: errc::not_supported
;
395 __handle_error("in directory_entry::file_size", __ec
, make_error_code(__err_kind
));
397 return __data_
.__size_
;
400 __libcpp_unreachable();
403 _LIBCPP_HIDE_FROM_ABI
uintmax_t __get_nlink(error_code
* __ec
= nullptr) const {
404 switch (__data_
.__cache_type_
) {
406 case _IterNonSymlink
:
408 case _IterCachedNonSymlink
:
409 case _IterCachedSymlink
:
410 case _RefreshSymlinkUnresolved
:
411 return filesystem::__hard_link_count(__p_
, __ec
);
412 case _RefreshSymlink
:
413 case _RefreshNonSymlink
: {
415 (void)__get_ft(&__m_ec
);
416 __handle_error("in directory_entry::hard_link_count", __ec
, __m_ec
);
417 return __data_
.__nlink_
;
420 __libcpp_unreachable();
423 _LIBCPP_HIDE_FROM_ABI file_time_type
__get_write_time(error_code
* __ec
= nullptr) const {
424 switch (__data_
.__cache_type_
) {
426 case _IterNonSymlink
:
428 case _IterCachedSymlink
:
429 case _RefreshSymlinkUnresolved
:
430 return filesystem::__last_write_time(__p_
, __ec
);
431 case _IterCachedNonSymlink
:
432 case _RefreshSymlink
:
433 case _RefreshNonSymlink
: {
435 file_status
__st(__get_ft(&__m_ec
));
436 __handle_error("in directory_entry::last_write_time", __ec
, __m_ec
);
437 if (filesystem::exists(__st
) && __data_
.__write_time_
== file_time_type::min())
438 __handle_error("in directory_entry::last_write_time", __ec
, make_error_code(errc::value_too_large
));
439 return __data_
.__write_time_
;
442 __libcpp_unreachable();
447 __cached_data __data_
;
450 class __dir_element_proxy
{
452 inline _LIBCPP_HIDE_FROM_ABI directory_entry
operator*() { return std::move(__elem_
); }
455 friend class directory_iterator
;
456 friend class recursive_directory_iterator
;
457 _LIBCPP_HIDE_FROM_ABI
explicit __dir_element_proxy(directory_entry
const& __e
) : __elem_(__e
) {}
458 _LIBCPP_HIDE_FROM_ABI
__dir_element_proxy(__dir_element_proxy
&& __o
) : __elem_(std::move(__o
.__elem_
)) {}
459 directory_entry __elem_
;
462 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP
464 _LIBCPP_END_NAMESPACE_FILESYSTEM
466 #endif // _LIBCPP_STD_VER >= 17 && _LIBCPP_HAS_FILESYSTEM
470 #endif // _LIBCPP___FILESYSTEM_DIRECTORY_ENTRY_H