1 // Filesystem directory utilities -*- C++ -*-
3 // Copyright (C) 2014-2019 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 namespace std
_GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 // constructors and destructor
54 file_status() noexcept
: file_status(file_type::none
) {}
57 file_status(file_type __ft
, perms __prms
= perms::unknown
) noexcept
58 : _M_type(__ft
), _M_perms(__prms
) { }
60 file_status(const file_status
&) noexcept
= default;
61 file_status(file_status
&&) noexcept
= default;
62 ~file_status() = default;
64 file_status
& operator=(const file_status
&) noexcept
= default;
65 file_status
& operator=(file_status
&&) noexcept
= default;
68 file_type
type() const noexcept
{ return _M_type
; }
69 perms
permissions() const noexcept
{ return _M_perms
; }
72 void type(file_type __ft
) noexcept
{ _M_type
= __ft
; }
73 void permissions(perms __prms
) noexcept
{ _M_perms
= __prms
; }
80 _GLIBCXX_BEGIN_NAMESPACE_CXX11
83 class directory_iterator
;
84 class recursive_directory_iterator
;
89 // constructors and destructor
90 directory_entry() noexcept
= default;
91 directory_entry(const directory_entry
&) = default;
92 directory_entry(directory_entry
&&) noexcept
= default;
95 directory_entry(const filesystem::path
& __p
)
99 directory_entry(const filesystem::path
& __p
, error_code
& __ec
)
107 ~directory_entry() = default;
110 directory_entry
& operator=(const directory_entry
&) = default;
111 directory_entry
& operator=(directory_entry
&&) noexcept
= default;
114 assign(const filesystem::path
& __p
)
121 assign(const filesystem::path
& __p
, error_code
& __ec
)
128 replace_filename(const filesystem::path
& __p
)
130 _M_path
.replace_filename(__p
);
135 replace_filename(const filesystem::path
& __p
, error_code
& __ec
)
137 _M_path
.replace_filename(__p
);
143 { _M_type
= symlink_status().type(); }
146 refresh(error_code
& __ec
) noexcept
147 { _M_type
= symlink_status(__ec
).type(); }
150 const filesystem::path
& path() const noexcept
{ return _M_path
; }
151 operator const filesystem::path
& () const noexcept
{ return _M_path
; }
155 { return filesystem::exists(file_status
{_M_file_type()}); }
158 exists(error_code
& __ec
) const noexcept
159 { return filesystem::exists(file_status
{_M_file_type(__ec
)}); }
162 is_block_file() const
163 { return _M_file_type() == file_type::block
; }
166 is_block_file(error_code
& __ec
) const noexcept
167 { return _M_file_type(__ec
) == file_type::block
; }
170 is_character_file() const
171 { return _M_file_type() == file_type::character
; }
174 is_character_file(error_code
& __ec
) const noexcept
175 { return _M_file_type(__ec
) == file_type::character
; }
179 { return _M_file_type() == file_type::directory
; }
182 is_directory(error_code
& __ec
) const noexcept
183 { return _M_file_type(__ec
) == file_type::directory
; }
187 { return _M_file_type() == file_type::fifo
; }
190 is_fifo(error_code
& __ec
) const noexcept
191 { return _M_file_type(__ec
) == file_type::fifo
; }
195 { return filesystem::is_other(file_status
{_M_file_type()}); }
198 is_other(error_code
& __ec
) const noexcept
199 { return filesystem::is_other(file_status
{_M_file_type(__ec
)}); }
202 is_regular_file() const
203 { return _M_file_type() == file_type::regular
; }
206 is_regular_file(error_code
& __ec
) const noexcept
207 { return _M_file_type(__ec
) == file_type::regular
; }
211 { return _M_file_type() == file_type::socket
; }
214 is_socket(error_code
& __ec
) const noexcept
215 { return _M_file_type(__ec
) == file_type::socket
; }
220 if (_M_type
!= file_type::none
)
221 return _M_type
== file_type::symlink
;
222 return symlink_status().type() == file_type::symlink
;
226 is_symlink(error_code
& __ec
) const noexcept
228 if (_M_type
!= file_type::none
)
229 return _M_type
== file_type::symlink
;
230 return symlink_status(__ec
).type() == file_type::symlink
;
235 { return filesystem::file_size(_M_path
); }
238 file_size(error_code
& __ec
) const noexcept
239 { return filesystem::file_size(_M_path
, __ec
); }
242 hard_link_count() const
243 { return filesystem::hard_link_count(_M_path
); }
246 hard_link_count(error_code
& __ec
) const noexcept
247 { return filesystem::hard_link_count(_M_path
, __ec
); }
250 last_write_time() const
251 { return filesystem::last_write_time(_M_path
); }
255 last_write_time(error_code
& __ec
) const noexcept
256 { return filesystem::last_write_time(_M_path
, __ec
); }
260 { return filesystem::status(_M_path
); }
263 status(error_code
& __ec
) const noexcept
264 { return filesystem::status(_M_path
, __ec
); }
267 symlink_status() const
268 { return filesystem::symlink_status(_M_path
); }
271 symlink_status(error_code
& __ec
) const noexcept
272 { return filesystem::symlink_status(_M_path
, __ec
); }
275 operator< (const directory_entry
& __rhs
) const noexcept
276 { return _M_path
< __rhs
._M_path
; }
279 operator==(const directory_entry
& __rhs
) const noexcept
280 { return _M_path
== __rhs
._M_path
; }
283 operator!=(const directory_entry
& __rhs
) const noexcept
284 { return _M_path
!= __rhs
._M_path
; }
287 operator<=(const directory_entry
& __rhs
) const noexcept
288 { return _M_path
<= __rhs
._M_path
; }
291 operator> (const directory_entry
& __rhs
) const noexcept
292 { return _M_path
> __rhs
._M_path
; }
295 operator>=(const directory_entry
& __rhs
) const noexcept
296 { return _M_path
>= __rhs
._M_path
; }
300 friend class directory_iterator
;
301 friend class recursive_directory_iterator
;
303 // _GLIBCXX_RESOLVE_LIB_DEFECTS
304 // 3171. LWG 2989 breaks directory_entry stream insertion
305 template<typename _CharT
, typename _Traits
>
306 friend basic_ostream
<_CharT
, _Traits
>&
307 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
308 const directory_entry
& __d
)
309 { return __os
<< __d
.path(); }
311 directory_entry(const filesystem::path
& __p
, file_type __t
)
312 : _M_path(__p
), _M_type(__t
)
315 // Equivalent to status().type() but uses cached value, if any.
319 if (_M_type
!= file_type::none
&& _M_type
!= file_type::symlink
)
321 return status().type();
324 // Equivalent to status(__ec).type() but uses cached value, if any.
326 _M_file_type(error_code
& __ec
) const noexcept
328 if (_M_type
!= file_type::none
&& _M_type
!= file_type::symlink
)
333 return status(__ec
).type();
336 filesystem::path _M_path
;
337 file_type _M_type
= file_type::none
;
340 struct __directory_iterator_proxy
342 const directory_entry
& operator*() const& noexcept
{ return _M_entry
; }
344 directory_entry
operator*() && noexcept
{ return std::move(_M_entry
); }
347 friend class directory_iterator
;
348 friend class recursive_directory_iterator
;
351 __directory_iterator_proxy(const directory_entry
& __e
) : _M_entry(__e
) { }
353 directory_entry _M_entry
;
356 class directory_iterator
359 typedef directory_entry value_type
;
360 typedef ptrdiff_t difference_type
;
361 typedef const directory_entry
* pointer
;
362 typedef const directory_entry
& reference
;
363 typedef input_iterator_tag iterator_category
;
365 directory_iterator() = default;
368 directory_iterator(const path
& __p
)
369 : directory_iterator(__p
, directory_options::none
, nullptr) { }
371 directory_iterator(const path
& __p
, directory_options __options
)
372 : directory_iterator(__p
, __options
, nullptr) { }
374 directory_iterator(const path
& __p
, error_code
& __ec
)
375 : directory_iterator(__p
, directory_options::none
, __ec
) { }
377 directory_iterator(const path
& __p
, directory_options __options
,
379 : directory_iterator(__p
, __options
, &__ec
) { }
381 directory_iterator(const directory_iterator
& __rhs
) = default;
383 directory_iterator(directory_iterator
&& __rhs
) noexcept
= default;
385 ~directory_iterator() = default;
388 operator=(const directory_iterator
& __rhs
) = default;
391 operator=(directory_iterator
&& __rhs
) noexcept
= default;
393 const directory_entry
& operator*() const;
394 const directory_entry
* operator->() const { return &**this; }
395 directory_iterator
& operator++();
396 directory_iterator
& increment(error_code
& __ec
);
398 __directory_iterator_proxy
operator++(int)
400 __directory_iterator_proxy __pr
{**this};
406 directory_iterator(const path
&, directory_options
, error_code
*);
409 operator==(const directory_iterator
& __lhs
,
410 const directory_iterator
& __rhs
);
412 friend class recursive_directory_iterator
;
414 std::__shared_ptr
<_Dir
> _M_dir
;
417 inline directory_iterator
418 begin(directory_iterator __iter
) noexcept
421 inline directory_iterator
422 end(directory_iterator
) noexcept
423 { return directory_iterator(); }
426 operator==(const directory_iterator
& __lhs
, const directory_iterator
& __rhs
)
428 return !__rhs
._M_dir
.owner_before(__lhs
._M_dir
)
429 && !__lhs
._M_dir
.owner_before(__rhs
._M_dir
);
433 operator!=(const directory_iterator
& __lhs
, const directory_iterator
& __rhs
)
434 { return !(__lhs
== __rhs
); }
436 class recursive_directory_iterator
439 typedef directory_entry value_type
;
440 typedef ptrdiff_t difference_type
;
441 typedef const directory_entry
* pointer
;
442 typedef const directory_entry
& reference
;
443 typedef input_iterator_tag iterator_category
;
445 recursive_directory_iterator() = default;
448 recursive_directory_iterator(const path
& __p
)
449 : recursive_directory_iterator(__p
, directory_options::none
, nullptr) { }
451 recursive_directory_iterator(const path
& __p
, directory_options __options
)
452 : recursive_directory_iterator(__p
, __options
, nullptr) { }
454 recursive_directory_iterator(const path
& __p
, directory_options __options
,
456 : recursive_directory_iterator(__p
, __options
, &__ec
) { }
458 recursive_directory_iterator(const path
& __p
, error_code
& __ec
)
459 : recursive_directory_iterator(__p
, directory_options::none
, &__ec
) { }
461 recursive_directory_iterator(
462 const recursive_directory_iterator
&) = default;
464 recursive_directory_iterator(recursive_directory_iterator
&&) = default;
466 ~recursive_directory_iterator();
469 directory_options
options() const { return _M_options
; }
471 bool recursion_pending() const { return _M_pending
; }
473 const directory_entry
& operator*() const;
474 const directory_entry
* operator->() const { return &**this; }
477 recursive_directory_iterator
&
478 operator=(const recursive_directory_iterator
& __rhs
) noexcept
;
479 recursive_directory_iterator
&
480 operator=(recursive_directory_iterator
&& __rhs
) noexcept
;
482 recursive_directory_iterator
& operator++();
483 recursive_directory_iterator
& increment(error_code
& __ec
);
485 __directory_iterator_proxy
operator++(int)
487 __directory_iterator_proxy __pr
{**this};
493 void pop(error_code
&);
495 void disable_recursion_pending() { _M_pending
= false; }
498 recursive_directory_iterator(const path
&, directory_options
, error_code
*);
501 operator==(const recursive_directory_iterator
& __lhs
,
502 const recursive_directory_iterator
& __rhs
);
505 std::__shared_ptr
<_Dir_stack
> _M_dirs
;
506 directory_options _M_options
= {};
507 bool _M_pending
= false;
510 inline recursive_directory_iterator
511 begin(recursive_directory_iterator __iter
) noexcept
514 inline recursive_directory_iterator
515 end(recursive_directory_iterator
) noexcept
516 { return recursive_directory_iterator(); }
519 operator==(const recursive_directory_iterator
& __lhs
,
520 const recursive_directory_iterator
& __rhs
)
522 return !__rhs
._M_dirs
.owner_before(__lhs
._M_dirs
)
523 && !__lhs
._M_dirs
.owner_before(__rhs
._M_dirs
);
527 operator!=(const recursive_directory_iterator
& __lhs
,
528 const recursive_directory_iterator
& __rhs
)
529 { return !(__lhs
== __rhs
); }
531 _GLIBCXX_END_NAMESPACE_CXX11
533 // @} group filesystem
534 } // namespace filesystem
536 // Use explicit instantiations of these types. Any inconsistency in the
537 // value of __default_lock_policy between code including this header and
538 // the library will cause a linker error.
539 extern template class
540 __shared_ptr
<filesystem::_Dir
>;
541 extern template class
542 __shared_ptr
<filesystem::recursive_directory_iterator::_Dir_stack
>;
544 _GLIBCXX_END_NAMESPACE_VERSION
549 #endif // _GLIBCXX_FS_DIR_H