1 // Filesystem directory utilities -*- C++ -*-
3 // Copyright (C) 2014-2021 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 > 201703L
40 # include <compare> // std::strong_ordering
43 namespace std
_GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 /** @addtogroup filesystem
53 /// Information about a file's type and permissions.
57 // constructors and destructor
58 file_status() noexcept
: file_status(file_type::none
) {}
61 file_status(file_type __ft
, perms __prms
= perms::unknown
) noexcept
62 : _M_type(__ft
), _M_perms(__prms
) { }
64 file_status(const file_status
&) noexcept
= default;
65 file_status(file_status
&&) noexcept
= default;
66 ~file_status() = default;
68 file_status
& operator=(const file_status
&) noexcept
= default;
69 file_status
& operator=(file_status
&&) noexcept
= default;
72 file_type
type() const noexcept
{ return _M_type
; }
73 perms
permissions() const noexcept
{ return _M_perms
; }
76 void type(file_type __ft
) noexcept
{ _M_type
= __ft
; }
77 void permissions(perms __prms
) noexcept
{ _M_perms
= __prms
; }
79 #if __cpp_lib_three_way_comparison
81 operator==(const file_status
&, const file_status
&) noexcept
= default;
89 _GLIBCXX_BEGIN_NAMESPACE_CXX11
92 class directory_iterator
;
93 class recursive_directory_iterator
;
95 /// The value type used by directory iterators
99 // constructors and destructor
100 directory_entry() noexcept
= default;
101 directory_entry(const directory_entry
&) = default;
102 directory_entry(directory_entry
&&) noexcept
= default;
105 directory_entry(const filesystem::path
& __p
)
109 directory_entry(const filesystem::path
& __p
, error_code
& __ec
)
117 ~directory_entry() = default;
120 directory_entry
& operator=(const directory_entry
&) = default;
121 directory_entry
& operator=(directory_entry
&&) noexcept
= default;
124 assign(const filesystem::path
& __p
)
131 assign(const filesystem::path
& __p
, error_code
& __ec
)
138 replace_filename(const filesystem::path
& __p
)
140 _M_path
.replace_filename(__p
);
145 replace_filename(const filesystem::path
& __p
, error_code
& __ec
)
147 _M_path
.replace_filename(__p
);
153 { _M_type
= symlink_status().type(); }
156 refresh(error_code
& __ec
) noexcept
157 { _M_type
= symlink_status(__ec
).type(); }
160 const filesystem::path
& path() const noexcept
{ return _M_path
; }
161 operator const filesystem::path
& () const noexcept
{ return _M_path
; }
165 { return filesystem::exists(file_status
{_M_file_type()}); }
168 exists(error_code
& __ec
) const noexcept
169 { return filesystem::exists(file_status
{_M_file_type(__ec
)}); }
172 is_block_file() const
173 { return _M_file_type() == file_type::block
; }
176 is_block_file(error_code
& __ec
) const noexcept
177 { return _M_file_type(__ec
) == file_type::block
; }
180 is_character_file() const
181 { return _M_file_type() == file_type::character
; }
184 is_character_file(error_code
& __ec
) const noexcept
185 { return _M_file_type(__ec
) == file_type::character
; }
189 { return _M_file_type() == file_type::directory
; }
192 is_directory(error_code
& __ec
) const noexcept
193 { return _M_file_type(__ec
) == file_type::directory
; }
197 { return _M_file_type() == file_type::fifo
; }
200 is_fifo(error_code
& __ec
) const noexcept
201 { return _M_file_type(__ec
) == file_type::fifo
; }
205 { return filesystem::is_other(file_status
{_M_file_type()}); }
208 is_other(error_code
& __ec
) const noexcept
209 { return filesystem::is_other(file_status
{_M_file_type(__ec
)}); }
212 is_regular_file() const
213 { return _M_file_type() == file_type::regular
; }
216 is_regular_file(error_code
& __ec
) const noexcept
217 { return _M_file_type(__ec
) == file_type::regular
; }
221 { return _M_file_type() == file_type::socket
; }
224 is_socket(error_code
& __ec
) const noexcept
225 { return _M_file_type(__ec
) == file_type::socket
; }
230 if (_M_type
!= file_type::none
)
231 return _M_type
== file_type::symlink
;
232 return symlink_status().type() == file_type::symlink
;
236 is_symlink(error_code
& __ec
) const noexcept
238 if (_M_type
!= file_type::none
)
239 return _M_type
== file_type::symlink
;
240 return symlink_status(__ec
).type() == file_type::symlink
;
245 { return filesystem::file_size(_M_path
); }
248 file_size(error_code
& __ec
) const noexcept
249 { return filesystem::file_size(_M_path
, __ec
); }
252 hard_link_count() const
253 { return filesystem::hard_link_count(_M_path
); }
256 hard_link_count(error_code
& __ec
) const noexcept
257 { return filesystem::hard_link_count(_M_path
, __ec
); }
260 last_write_time() const
261 { return filesystem::last_write_time(_M_path
); }
265 last_write_time(error_code
& __ec
) const noexcept
266 { return filesystem::last_write_time(_M_path
, __ec
); }
270 { return filesystem::status(_M_path
); }
273 status(error_code
& __ec
) const noexcept
274 { return filesystem::status(_M_path
, __ec
); }
277 symlink_status() const
278 { return filesystem::symlink_status(_M_path
); }
281 symlink_status(error_code
& __ec
) const noexcept
282 { return filesystem::symlink_status(_M_path
, __ec
); }
285 operator==(const directory_entry
& __rhs
) const noexcept
286 { return _M_path
== __rhs
._M_path
; }
288 #if __cpp_lib_three_way_comparison
290 operator<=>(const directory_entry
& __rhs
) const noexcept
291 { return _M_path
<=> __rhs
._M_path
; }
294 operator!=(const directory_entry
& __rhs
) const noexcept
295 { return _M_path
!= __rhs
._M_path
; }
298 operator< (const directory_entry
& __rhs
) const noexcept
299 { return _M_path
< __rhs
._M_path
; }
302 operator<=(const directory_entry
& __rhs
) const noexcept
303 { return _M_path
<= __rhs
._M_path
; }
306 operator> (const directory_entry
& __rhs
) const noexcept
307 { return _M_path
> __rhs
._M_path
; }
310 operator>=(const directory_entry
& __rhs
) const noexcept
311 { return _M_path
>= __rhs
._M_path
; }
316 friend class directory_iterator
;
317 friend class recursive_directory_iterator
;
319 // _GLIBCXX_RESOLVE_LIB_DEFECTS
320 // 3171. LWG 2989 breaks directory_entry stream insertion
321 template<typename _CharT
, typename _Traits
>
322 friend basic_ostream
<_CharT
, _Traits
>&
323 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
324 const directory_entry
& __d
)
325 { return __os
<< __d
.path(); }
327 directory_entry(const filesystem::path
& __p
, file_type __t
)
328 : _M_path(__p
), _M_type(__t
)
331 // Equivalent to status().type() but uses cached value, if any.
335 if (_M_type
!= file_type::none
&& _M_type
!= file_type::symlink
)
337 return status().type();
340 // Equivalent to status(__ec).type() but uses cached value, if any.
342 _M_file_type(error_code
& __ec
) const noexcept
344 if (_M_type
!= file_type::none
&& _M_type
!= file_type::symlink
)
349 return status(__ec
).type();
352 filesystem::path _M_path
;
353 file_type _M_type
= file_type::none
;
356 /// Proxy returned by post-increment on directory iterators.
357 struct __directory_iterator_proxy
359 const directory_entry
& operator*() const& noexcept
{ return _M_entry
; }
361 directory_entry
operator*() && noexcept
{ return std::move(_M_entry
); }
364 friend class directory_iterator
;
365 friend class recursive_directory_iterator
;
368 __directory_iterator_proxy(const directory_entry
& __e
) : _M_entry(__e
) { }
370 directory_entry _M_entry
;
373 /// Iterator type for traversing the entries in a single directory.
374 class directory_iterator
377 typedef directory_entry value_type
;
378 typedef ptrdiff_t difference_type
;
379 typedef const directory_entry
* pointer
;
380 typedef const directory_entry
& reference
;
381 typedef input_iterator_tag iterator_category
;
383 directory_iterator() = default;
386 directory_iterator(const path
& __p
)
387 : directory_iterator(__p
, directory_options::none
, nullptr) { }
389 directory_iterator(const path
& __p
, directory_options __options
)
390 : directory_iterator(__p
, __options
, nullptr) { }
392 directory_iterator(const path
& __p
, error_code
& __ec
)
393 : directory_iterator(__p
, directory_options::none
, __ec
) { }
395 directory_iterator(const path
& __p
, directory_options __options
,
397 : directory_iterator(__p
, __options
, &__ec
) { }
399 directory_iterator(const directory_iterator
& __rhs
) = default;
401 directory_iterator(directory_iterator
&& __rhs
) noexcept
= default;
403 ~directory_iterator() = default;
406 operator=(const directory_iterator
& __rhs
) = default;
409 operator=(directory_iterator
&& __rhs
) noexcept
= default;
411 const directory_entry
& operator*() const noexcept
;
412 const directory_entry
* operator->() const noexcept
{ return &**this; }
413 directory_iterator
& operator++();
414 directory_iterator
& increment(error_code
& __ec
);
416 __directory_iterator_proxy
operator++(int)
418 __directory_iterator_proxy __pr
{**this};
424 directory_iterator(const path
&, directory_options
, error_code
*);
427 operator==(const directory_iterator
& __lhs
,
428 const directory_iterator
& __rhs
) noexcept
430 return !__rhs
._M_dir
.owner_before(__lhs
._M_dir
)
431 && !__lhs
._M_dir
.owner_before(__rhs
._M_dir
);
435 operator!=(const directory_iterator
& __lhs
,
436 const directory_iterator
& __rhs
) noexcept
437 { return !(__lhs
== __rhs
); }
439 friend class recursive_directory_iterator
;
441 std::__shared_ptr
<_Dir
> _M_dir
;
444 /// @relates std::filesystem::directory_iterator @{
446 /** @brief Enable range-based `for` using directory_iterator.
448 * e.g. `for (auto& entry : std::filesystem::directory_iterator(".")) ...`
450 inline directory_iterator
451 begin(directory_iterator __iter
) noexcept
454 /// Return a past-the-end directory_iterator
455 inline directory_iterator
456 end(directory_iterator
) noexcept
457 { return directory_iterator(); }
460 /// Iterator type for recursively traversing a directory hierarchy.
461 class recursive_directory_iterator
464 typedef directory_entry value_type
;
465 typedef ptrdiff_t difference_type
;
466 typedef const directory_entry
* pointer
;
467 typedef const directory_entry
& reference
;
468 typedef input_iterator_tag iterator_category
;
470 recursive_directory_iterator() = default;
473 recursive_directory_iterator(const path
& __p
)
474 : recursive_directory_iterator(__p
, directory_options::none
, nullptr) { }
476 recursive_directory_iterator(const path
& __p
, directory_options __options
)
477 : recursive_directory_iterator(__p
, __options
, nullptr) { }
479 recursive_directory_iterator(const path
& __p
, directory_options __options
,
481 : recursive_directory_iterator(__p
, __options
, &__ec
) { }
483 recursive_directory_iterator(const path
& __p
, error_code
& __ec
)
484 : recursive_directory_iterator(__p
, directory_options::none
, &__ec
) { }
486 recursive_directory_iterator(
487 const recursive_directory_iterator
&) = default;
489 recursive_directory_iterator(recursive_directory_iterator
&&) = default;
491 ~recursive_directory_iterator();
494 directory_options
options() const noexcept
;
495 int depth() const noexcept
;
496 bool recursion_pending() const noexcept
;
498 const directory_entry
& operator*() const noexcept
;
499 const directory_entry
* operator->() const noexcept
{ return &**this; }
502 recursive_directory_iterator
&
503 operator=(const recursive_directory_iterator
& __rhs
) noexcept
;
504 recursive_directory_iterator
&
505 operator=(recursive_directory_iterator
&& __rhs
) noexcept
;
507 recursive_directory_iterator
& operator++();
508 recursive_directory_iterator
& increment(error_code
& __ec
);
510 __directory_iterator_proxy
operator++(int)
512 __directory_iterator_proxy __pr
{**this};
518 void pop(error_code
&);
520 void disable_recursion_pending() noexcept
;
523 recursive_directory_iterator(const path
&, directory_options
, error_code
*);
526 operator==(const recursive_directory_iterator
& __lhs
,
527 const recursive_directory_iterator
& __rhs
) noexcept
529 return !__rhs
._M_dirs
.owner_before(__lhs
._M_dirs
)
530 && !__lhs
._M_dirs
.owner_before(__rhs
._M_dirs
);
534 operator!=(const recursive_directory_iterator
& __lhs
,
535 const recursive_directory_iterator
& __rhs
) noexcept
536 { return !(__lhs
== __rhs
); }
539 std::__shared_ptr
<_Dir_stack
> _M_dirs
;
542 /// @relates std::filesystem::recursive_directory_iterator @{
544 /** @brief Enable range-based `for` using recursive_directory_iterator.
546 * e.g. `for (auto& entry : recursive_directory_iterator(".")) ...`
548 inline recursive_directory_iterator
549 begin(recursive_directory_iterator __iter
) noexcept
552 /// Return a past-the-end recursive_directory_iterator
553 inline recursive_directory_iterator
554 end(recursive_directory_iterator
) noexcept
555 { return recursive_directory_iterator(); }
558 _GLIBCXX_END_NAMESPACE_CXX11
560 /// @} group filesystem
561 } // namespace filesystem
563 // Use explicit instantiations of these types. Any inconsistency in the
564 // value of __default_lock_policy between code including this header and
565 // the library will cause a linker error.
566 extern template class
567 __shared_ptr
<filesystem::_Dir
>;
568 extern template class
569 __shared_ptr
<filesystem::recursive_directory_iterator::_Dir_stack
>;
571 _GLIBCXX_END_NAMESPACE_VERSION
576 #endif // _GLIBCXX_FS_DIR_H