1 // Filesystem directory utilities -*- C++ -*-
3 // Copyright (C) 2014-2023 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/bits/fs_dir.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{filesystem}
30 #ifndef _GLIBCXX_FS_DIR_H
31 #define _GLIBCXX_FS_DIR_H 1
33 #if __cplusplus >= 201703L
35 # include <ext/concurrence.h>
36 # include <bits/unique_ptr.h>
37 # include <bits/shared_ptr.h>
39 #if __cplusplus >= 202002L
40 # include <compare> // std::strong_ordering
41 # include <bits/iterator_concepts.h> // std::default_sentinel_t
44 namespace std
_GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 /** @addtogroup filesystem
54 /// Information about a file's type and permissions.
56 * @headerfile filesystem
62 // constructors and destructor
63 file_status() noexcept
: file_status(file_type::none
) {}
66 file_status(file_type __ft
, perms __prms
= perms::unknown
) noexcept
67 : _M_type(__ft
), _M_perms(__prms
) { }
69 file_status(const file_status
&) noexcept
= default;
70 file_status(file_status
&&) noexcept
= default;
71 ~file_status() = default;
73 file_status
& operator=(const file_status
&) noexcept
= default;
74 file_status
& operator=(file_status
&&) noexcept
= default;
77 file_type
type() const noexcept
{ return _M_type
; }
78 perms
permissions() const noexcept
{ return _M_perms
; }
81 void type(file_type __ft
) noexcept
{ _M_type
= __ft
; }
82 void permissions(perms __prms
) noexcept
{ _M_perms
= __prms
; }
84 #if __cpp_lib_three_way_comparison
86 operator==(const file_status
&, const file_status
&) noexcept
= default;
94 _GLIBCXX_BEGIN_NAMESPACE_CXX11
97 class directory_iterator
;
98 class recursive_directory_iterator
;
100 /// The value type used by directory iterators
102 * @headerfile filesystem
105 class directory_entry
108 // constructors and destructor
109 directory_entry() noexcept
= default;
110 directory_entry(const directory_entry
&) = default;
111 directory_entry(directory_entry
&&) noexcept
= default;
114 directory_entry(const filesystem::path
& __p
)
118 directory_entry(const filesystem::path
& __p
, error_code
& __ec
)
126 ~directory_entry() = default;
129 directory_entry
& operator=(const directory_entry
&) = default;
130 directory_entry
& operator=(directory_entry
&&) noexcept
= default;
133 assign(const filesystem::path
& __p
)
140 assign(const filesystem::path
& __p
, error_code
& __ec
)
147 replace_filename(const filesystem::path
& __p
)
149 _M_path
.replace_filename(__p
);
154 replace_filename(const filesystem::path
& __p
, error_code
& __ec
)
156 _M_path
.replace_filename(__p
);
162 { _M_type
= symlink_status().type(); }
165 refresh(error_code
& __ec
) noexcept
166 { _M_type
= symlink_status(__ec
).type(); }
169 const filesystem::path
& path() const noexcept
{ return _M_path
; }
170 operator const filesystem::path
& () const noexcept
{ return _M_path
; }
174 { return filesystem::exists(file_status
{_M_file_type()}); }
177 exists(error_code
& __ec
) const noexcept
178 { return filesystem::exists(file_status
{_M_file_type(__ec
)}); }
181 is_block_file() const
182 { return _M_file_type() == file_type::block
; }
185 is_block_file(error_code
& __ec
) const noexcept
186 { return _M_file_type(__ec
) == file_type::block
; }
189 is_character_file() const
190 { return _M_file_type() == file_type::character
; }
193 is_character_file(error_code
& __ec
) const noexcept
194 { return _M_file_type(__ec
) == file_type::character
; }
198 { return _M_file_type() == file_type::directory
; }
201 is_directory(error_code
& __ec
) const noexcept
202 { return _M_file_type(__ec
) == file_type::directory
; }
206 { return _M_file_type() == file_type::fifo
; }
209 is_fifo(error_code
& __ec
) const noexcept
210 { return _M_file_type(__ec
) == file_type::fifo
; }
214 { return filesystem::is_other(file_status
{_M_file_type()}); }
217 is_other(error_code
& __ec
) const noexcept
218 { return filesystem::is_other(file_status
{_M_file_type(__ec
)}); }
221 is_regular_file() const
222 { return _M_file_type() == file_type::regular
; }
225 is_regular_file(error_code
& __ec
) const noexcept
226 { return _M_file_type(__ec
) == file_type::regular
; }
230 { return _M_file_type() == file_type::socket
; }
233 is_socket(error_code
& __ec
) const noexcept
234 { return _M_file_type(__ec
) == file_type::socket
; }
239 if (_M_type
!= file_type::none
)
240 return _M_type
== file_type::symlink
;
241 return symlink_status().type() == file_type::symlink
;
245 is_symlink(error_code
& __ec
) const noexcept
247 if (_M_type
!= file_type::none
)
248 return _M_type
== file_type::symlink
;
249 return symlink_status(__ec
).type() == file_type::symlink
;
254 { return filesystem::file_size(_M_path
); }
257 file_size(error_code
& __ec
) const noexcept
258 { return filesystem::file_size(_M_path
, __ec
); }
261 hard_link_count() const
262 { return filesystem::hard_link_count(_M_path
); }
265 hard_link_count(error_code
& __ec
) const noexcept
266 { return filesystem::hard_link_count(_M_path
, __ec
); }
269 last_write_time() const
270 { return filesystem::last_write_time(_M_path
); }
274 last_write_time(error_code
& __ec
) const noexcept
275 { return filesystem::last_write_time(_M_path
, __ec
); }
279 { return filesystem::status(_M_path
); }
282 status(error_code
& __ec
) const noexcept
283 { return filesystem::status(_M_path
, __ec
); }
286 symlink_status() const
287 { return filesystem::symlink_status(_M_path
); }
290 symlink_status(error_code
& __ec
) const noexcept
291 { return filesystem::symlink_status(_M_path
, __ec
); }
294 operator==(const directory_entry
& __rhs
) const noexcept
295 { return _M_path
== __rhs
._M_path
; }
297 #if __cpp_lib_three_way_comparison
299 operator<=>(const directory_entry
& __rhs
) const noexcept
300 { return _M_path
<=> __rhs
._M_path
; }
303 operator!=(const directory_entry
& __rhs
) const noexcept
304 { return _M_path
!= __rhs
._M_path
; }
307 operator< (const directory_entry
& __rhs
) const noexcept
308 { return _M_path
< __rhs
._M_path
; }
311 operator<=(const directory_entry
& __rhs
) const noexcept
312 { return _M_path
<= __rhs
._M_path
; }
315 operator> (const directory_entry
& __rhs
) const noexcept
316 { return _M_path
> __rhs
._M_path
; }
319 operator>=(const directory_entry
& __rhs
) const noexcept
320 { return _M_path
>= __rhs
._M_path
; }
325 friend class directory_iterator
;
326 friend class recursive_directory_iterator
;
328 // _GLIBCXX_RESOLVE_LIB_DEFECTS
329 // 3171. LWG 2989 breaks directory_entry stream insertion
330 template<typename _CharT
, typename _Traits
>
331 friend basic_ostream
<_CharT
, _Traits
>&
332 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
333 const directory_entry
& __d
)
334 { return __os
<< __d
.path(); }
336 directory_entry(const filesystem::path
& __p
, file_type __t
)
337 : _M_path(__p
), _M_type(__t
)
340 // Equivalent to status().type() but uses cached value, if any.
344 if (_M_type
!= file_type::none
&& _M_type
!= file_type::symlink
)
346 return status().type();
349 // Equivalent to status(__ec).type() but uses cached value, if any.
351 _M_file_type(error_code
& __ec
) const noexcept
353 if (_M_type
!= file_type::none
&& _M_type
!= file_type::symlink
)
358 return status(__ec
).type();
361 filesystem::path _M_path
;
362 file_type _M_type
= file_type::none
;
365 /// @cond undocumented
367 /// Proxy returned by post-increment on directory iterators.
369 * @headerfile filesystem
372 struct __directory_iterator_proxy
374 const directory_entry
& operator*() const& noexcept
{ return _M_entry
; }
376 directory_entry
operator*() && noexcept
{ return std::move(_M_entry
); }
379 friend class directory_iterator
;
380 friend class recursive_directory_iterator
;
383 __directory_iterator_proxy(const directory_entry
& __e
) : _M_entry(__e
) { }
385 directory_entry _M_entry
;
389 /// Iterator type for traversing the entries in a single directory.
391 * @headerfile filesystem
394 class directory_iterator
397 typedef directory_entry value_type
;
398 typedef ptrdiff_t difference_type
;
399 typedef const directory_entry
* pointer
;
400 typedef const directory_entry
& reference
;
401 typedef input_iterator_tag iterator_category
;
403 directory_iterator() = default;
406 directory_iterator(const path
& __p
)
407 : directory_iterator(__p
, directory_options::none
, nullptr) { }
409 directory_iterator(const path
& __p
, directory_options __options
)
410 : directory_iterator(__p
, __options
, nullptr) { }
412 directory_iterator(const path
& __p
, error_code
& __ec
)
413 : directory_iterator(__p
, directory_options::none
, __ec
) { }
415 directory_iterator(const path
& __p
, directory_options __options
,
417 : directory_iterator(__p
, __options
, &__ec
) { }
419 directory_iterator(const directory_iterator
& __rhs
) = default;
421 directory_iterator(directory_iterator
&& __rhs
) noexcept
= default;
423 ~directory_iterator() = default;
426 operator=(const directory_iterator
& __rhs
) = default;
429 operator=(directory_iterator
&& __rhs
) noexcept
= default;
431 const directory_entry
& operator*() const noexcept
;
432 const directory_entry
* operator->() const noexcept
{ return &**this; }
433 directory_iterator
& operator++();
434 directory_iterator
& increment(error_code
& __ec
);
436 __directory_iterator_proxy
operator++(int)
438 __directory_iterator_proxy __pr
{**this};
444 operator==(const directory_iterator
& __lhs
,
445 const directory_iterator
& __rhs
) noexcept
447 return !__rhs
._M_dir
.owner_before(__lhs
._M_dir
)
448 && !__lhs
._M_dir
.owner_before(__rhs
._M_dir
);
451 #if __cplusplus >= 202002L
452 // _GLIBCXX_RESOLVE_LIB_DEFECTS
453 // 3719. Directory iterators should be usable with default sentinel
454 bool operator==(default_sentinel_t
) const noexcept
458 #if __cpp_impl_three_way_comparison < 201907L
460 operator!=(const directory_iterator
& __lhs
,
461 const directory_iterator
& __rhs
) noexcept
462 { return !(__lhs
== __rhs
); }
466 directory_iterator(const path
&, directory_options
, error_code
*);
468 friend class recursive_directory_iterator
;
470 std::__shared_ptr
<_Dir
> _M_dir
;
473 /** @relates std::filesystem::directory_iterator
474 * @headerfile filesystem
479 /** @brief Enable range-based `for` using directory_iterator.
481 * e.g. `for (auto& entry : std::filesystem::directory_iterator(".")) ...`
483 inline directory_iterator
484 begin(directory_iterator __iter
) noexcept
487 /// Return a past-the-end directory_iterator
488 inline directory_iterator
489 end(directory_iterator
) noexcept
490 { return directory_iterator(); }
493 /// Iterator type for recursively traversing a directory hierarchy.
495 * @headerfile filesystem
498 class recursive_directory_iterator
501 typedef directory_entry value_type
;
502 typedef ptrdiff_t difference_type
;
503 typedef const directory_entry
* pointer
;
504 typedef const directory_entry
& reference
;
505 typedef input_iterator_tag iterator_category
;
507 recursive_directory_iterator() = default;
510 recursive_directory_iterator(const path
& __p
)
511 : recursive_directory_iterator(__p
, directory_options::none
, nullptr) { }
513 recursive_directory_iterator(const path
& __p
, directory_options __options
)
514 : recursive_directory_iterator(__p
, __options
, nullptr) { }
516 recursive_directory_iterator(const path
& __p
, directory_options __options
,
518 : recursive_directory_iterator(__p
, __options
, &__ec
) { }
520 recursive_directory_iterator(const path
& __p
, error_code
& __ec
)
521 : recursive_directory_iterator(__p
, directory_options::none
, &__ec
) { }
523 recursive_directory_iterator(
524 const recursive_directory_iterator
&) = default;
526 recursive_directory_iterator(recursive_directory_iterator
&&) = default;
528 ~recursive_directory_iterator();
531 directory_options
options() const noexcept
;
532 int depth() const noexcept
;
533 bool recursion_pending() const noexcept
;
535 const directory_entry
& operator*() const noexcept
;
536 const directory_entry
* operator->() const noexcept
{ return &**this; }
539 recursive_directory_iterator
&
540 operator=(const recursive_directory_iterator
& __rhs
) noexcept
;
541 recursive_directory_iterator
&
542 operator=(recursive_directory_iterator
&& __rhs
) noexcept
;
544 recursive_directory_iterator
& operator++();
545 recursive_directory_iterator
& increment(error_code
& __ec
);
547 __directory_iterator_proxy
operator++(int)
549 __directory_iterator_proxy __pr
{**this};
555 void pop(error_code
&);
557 void disable_recursion_pending() noexcept
;
560 operator==(const recursive_directory_iterator
& __lhs
,
561 const recursive_directory_iterator
& __rhs
) noexcept
563 return !__rhs
._M_dirs
.owner_before(__lhs
._M_dirs
)
564 && !__lhs
._M_dirs
.owner_before(__rhs
._M_dirs
);
567 #if __cplusplus >= 202002L
568 // _GLIBCXX_RESOLVE_LIB_DEFECTS
569 // 3719. Directory iterators should be usable with default sentinel
570 bool operator==(default_sentinel_t
) const noexcept
574 #if __cpp_impl_three_way_comparison < 201907L
576 operator!=(const recursive_directory_iterator
& __lhs
,
577 const recursive_directory_iterator
& __rhs
) noexcept
578 { return !(__lhs
== __rhs
); }
582 recursive_directory_iterator(const path
&, directory_options
, error_code
*);
585 std::__shared_ptr
<_Dir_stack
> _M_dirs
;
587 recursive_directory_iterator
&
588 __erase(error_code
* = nullptr);
591 filesystem::remove_all(const path
&, error_code
&);
593 filesystem::remove_all(const path
&);
596 /** @relates std::filesystem::directory_iterator
597 * @headerfile filesystem
602 /** @brief Enable range-based `for` using recursive_directory_iterator.
604 * e.g. `for (auto& entry : recursive_directory_iterator(".")) ...`
606 inline recursive_directory_iterator
607 begin(recursive_directory_iterator __iter
) noexcept
610 /// Return a past-the-end recursive_directory_iterator
611 inline recursive_directory_iterator
612 end(recursive_directory_iterator
) noexcept
613 { return recursive_directory_iterator(); }
616 _GLIBCXX_END_NAMESPACE_CXX11
618 /// @} group filesystem
619 } // namespace filesystem
621 // Use explicit instantiations of these types. Any inconsistency in the
622 // value of __default_lock_policy between code including this header and
623 // the library will cause a linker error.
624 extern template class
625 __shared_ptr
<filesystem::_Dir
>;
626 extern template class
627 __shared_ptr
<filesystem::recursive_directory_iterator::_Dir_stack
>;
629 _GLIBCXX_END_NAMESPACE_VERSION
634 #endif // _GLIBCXX_FS_DIR_H