blob: 764ec6573a502922ce9628ae7e42b1838ec0d06f [file] [log] [blame]
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001// -*- C++ -*-
2//===--------------------------- filesystem -------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Eric Fiselier02cea5e2018-07-27 03:07:09 +00007//
8//===----------------------------------------------------------------------===//
9#ifndef _LIBCPP_FILESYSTEM
10#define _LIBCPP_FILESYSTEM
11/*
12 filesystem synopsis
13
14 namespace std { namespace filesystem {
15
16 class path;
17
18 void swap(path& lhs, path& rhs) noexcept;
19 size_t hash_value(const path& p) noexcept;
20
21 bool operator==(const path& lhs, const path& rhs) noexcept;
22 bool operator!=(const path& lhs, const path& rhs) noexcept;
23 bool operator< (const path& lhs, const path& rhs) noexcept;
24 bool operator<=(const path& lhs, const path& rhs) noexcept;
25 bool operator> (const path& lhs, const path& rhs) noexcept;
26 bool operator>=(const path& lhs, const path& rhs) noexcept;
27
28 path operator/ (const path& lhs, const path& rhs);
29
30 // fs.path.io operators are friends of path.
31 template <class charT, class traits>
32 friend basic_ostream<charT, traits>&
33 operator<<(basic_ostream<charT, traits>& os, const path& p);
34
35 template <class charT, class traits>
36 friend basic_istream<charT, traits>&
37 operator>>(basic_istream<charT, traits>& is, path& p);
38
39 template <class Source>
40 path u8path(const Source& source);
41 template <class InputIterator>
42 path u8path(InputIterator first, InputIterator last);
43
44 class filesystem_error;
45 class directory_entry;
46
47 class directory_iterator;
48
49 // enable directory_iterator range-based for statements
50 directory_iterator begin(directory_iterator iter) noexcept;
51 directory_iterator end(const directory_iterator&) noexcept;
52
53 class recursive_directory_iterator;
54
55 // enable recursive_directory_iterator range-based for statements
56 recursive_directory_iterator begin(recursive_directory_iterator iter) noexcept;
57 recursive_directory_iterator end(const recursive_directory_iterator&) noexcept;
58
59 class file_status;
60
61 struct space_info
62 {
63 uintmax_t capacity;
64 uintmax_t free;
65 uintmax_t available;
66 };
67
68 enum class file_type;
69 enum class perms;
70 enum class perm_options;
71 enum class copy_options;
72 enum class directory_options;
73
74 typedef chrono::time_point<trivial-clock> file_time_type;
75
76 // operational functions
77
78 path absolute(const path& p);
79 path absolute(const path& p, error_code &ec);
80
81 path canonical(const path& p);
82 path canonical(const path& p, error_code& ec);
83
84 void copy(const path& from, const path& to);
85 void copy(const path& from, const path& to, error_code& ec);
86 void copy(const path& from, const path& to, copy_options options);
87 void copy(const path& from, const path& to, copy_options options,
88 error_code& ec);
89
90 bool copy_file(const path& from, const path& to);
91 bool copy_file(const path& from, const path& to, error_code& ec);
92 bool copy_file(const path& from, const path& to, copy_options option);
93 bool copy_file(const path& from, const path& to, copy_options option,
94 error_code& ec);
95
96 void copy_symlink(const path& existing_symlink, const path& new_symlink);
97 void copy_symlink(const path& existing_symlink, const path& new_symlink,
98 error_code& ec) noexcept;
99
100 bool create_directories(const path& p);
101 bool create_directories(const path& p, error_code& ec);
102
103 bool create_directory(const path& p);
104 bool create_directory(const path& p, error_code& ec) noexcept;
105
106 bool create_directory(const path& p, const path& attributes);
107 bool create_directory(const path& p, const path& attributes,
108 error_code& ec) noexcept;
109
110 void create_directory_symlink(const path& to, const path& new_symlink);
111 void create_directory_symlink(const path& to, const path& new_symlink,
112 error_code& ec) noexcept;
113
114 void create_hard_link(const path& to, const path& new_hard_link);
115 void create_hard_link(const path& to, const path& new_hard_link,
116 error_code& ec) noexcept;
117
118 void create_symlink(const path& to, const path& new_symlink);
119 void create_symlink(const path& to, const path& new_symlink,
120 error_code& ec) noexcept;
121
122 path current_path();
123 path current_path(error_code& ec);
124 void current_path(const path& p);
125 void current_path(const path& p, error_code& ec) noexcept;
126
127 bool exists(file_status s) noexcept;
128 bool exists(const path& p);
129 bool exists(const path& p, error_code& ec) noexcept;
130
131 bool equivalent(const path& p1, const path& p2);
132 bool equivalent(const path& p1, const path& p2, error_code& ec) noexcept;
133
134 uintmax_t file_size(const path& p);
135 uintmax_t file_size(const path& p, error_code& ec) noexcept;
136
137 uintmax_t hard_link_count(const path& p);
138 uintmax_t hard_link_count(const path& p, error_code& ec) noexcept;
139
140 bool is_block_file(file_status s) noexcept;
141 bool is_block_file(const path& p);
142 bool is_block_file(const path& p, error_code& ec) noexcept;
143
144 bool is_character_file(file_status s) noexcept;
145 bool is_character_file(const path& p);
146 bool is_character_file(const path& p, error_code& ec) noexcept;
147
148 bool is_directory(file_status s) noexcept;
149 bool is_directory(const path& p);
150 bool is_directory(const path& p, error_code& ec) noexcept;
151
152 bool is_empty(const path& p);
153 bool is_empty(const path& p, error_code& ec) noexcept;
154
155 bool is_fifo(file_status s) noexcept;
156 bool is_fifo(const path& p);
157 bool is_fifo(const path& p, error_code& ec) noexcept;
158
159 bool is_other(file_status s) noexcept;
160 bool is_other(const path& p);
161 bool is_other(const path& p, error_code& ec) noexcept;
162
163 bool is_regular_file(file_status s) noexcept;
164 bool is_regular_file(const path& p);
165 bool is_regular_file(const path& p, error_code& ec) noexcept;
166
167 bool is_socket(file_status s) noexcept;
168 bool is_socket(const path& p);
169 bool is_socket(const path& p, error_code& ec) noexcept;
170
171 bool is_symlink(file_status s) noexcept;
172 bool is_symlink(const path& p);
173 bool is_symlink(const path& p, error_code& ec) noexcept;
174
175 file_time_type last_write_time(const path& p);
176 file_time_type last_write_time(const path& p, error_code& ec) noexcept;
177 void last_write_time(const path& p, file_time_type new_time);
178 void last_write_time(const path& p, file_time_type new_time,
179 error_code& ec) noexcept;
180
181 void permissions(const path& p, perms prms,
182 perm_options opts=perm_options::replace);
183 void permissions(const path& p, perms prms, error_code& ec) noexcept;
184 void permissions(const path& p, perms prms, perm_options opts,
185 error_code& ec);
186
187 path proximate(const path& p, error_code& ec);
188 path proximate(const path& p, const path& base = current_path());
189 path proximate(const path& p, const path& base, error_code &ec);
190
191 path read_symlink(const path& p);
192 path read_symlink(const path& p, error_code& ec);
193
194 path relative(const path& p, error_code& ec);
195 path relative(const path& p, const path& base=current_path());
196 path relative(const path& p, const path& base, error_code& ec);
197
198 bool remove(const path& p);
199 bool remove(const path& p, error_code& ec) noexcept;
200
201 uintmax_t remove_all(const path& p);
202 uintmax_t remove_all(const path& p, error_code& ec);
203
204 void rename(const path& from, const path& to);
205 void rename(const path& from, const path& to, error_code& ec) noexcept;
206
207 void resize_file(const path& p, uintmax_t size);
208 void resize_file(const path& p, uintmax_t size, error_code& ec) noexcept;
209
210 space_info space(const path& p);
211 space_info space(const path& p, error_code& ec) noexcept;
212
213 file_status status(const path& p);
214 file_status status(const path& p, error_code& ec) noexcept;
215
216 bool status_known(file_status s) noexcept;
217
218 file_status symlink_status(const path& p);
219 file_status symlink_status(const path& p, error_code& ec) noexcept;
220
221 path temp_directory_path();
222 path temp_directory_path(error_code& ec);
223
224 path weakly_canonical(path const& p);
225 path weakly_canonical(path const& p, error_code& ec);
226
227
228} } // namespaces std::filesystem
229
230*/
231
232#include <__config>
Louis Dionne73912b22020-11-04 15:01:25 -0500233#include <__availability>
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000234#include <cstddef>
235#include <cstdlib>
236#include <chrono>
237#include <iterator>
238#include <iosfwd>
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000239#include <memory>
240#include <stack>
241#include <string>
242#include <system_error>
243#include <utility>
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000244#include <string_view>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000245#include <version>
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000246
Louis Dionne8d053eb2020-10-09 15:31:05 -0400247#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
248# include <locale>
249# include <iomanip> // for quoted
250#endif
251
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000252#include <__debug>
253
254#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
255#pragma GCC system_header
256#endif
257
258_LIBCPP_PUSH_MACROS
259#include <__undef_macros>
260
261#ifndef _LIBCPP_CXX03_LANG
262
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000263_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
264
Louis Dionnef6bf76a2019-03-20 21:18:14 +0000265_LIBCPP_AVAILABILITY_FILESYSTEM_PUSH
266
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000267typedef chrono::time_point<_FilesystemClock> file_time_type;
268
269struct _LIBCPP_TYPE_VIS space_info {
270 uintmax_t capacity;
271 uintmax_t free;
272 uintmax_t available;
273};
274
275enum class _LIBCPP_ENUM_VIS file_type : signed char {
276 none = 0,
277 not_found = -1,
278 regular = 1,
279 directory = 2,
280 symlink = 3,
281 block = 4,
282 character = 5,
283 fifo = 6,
284 socket = 7,
285 unknown = 8
286};
287
288enum class _LIBCPP_ENUM_VIS perms : unsigned {
289 none = 0,
290
291 owner_read = 0400,
292 owner_write = 0200,
293 owner_exec = 0100,
294 owner_all = 0700,
295
296 group_read = 040,
297 group_write = 020,
298 group_exec = 010,
299 group_all = 070,
300
301 others_read = 04,
302 others_write = 02,
303 others_exec = 01,
304 others_all = 07,
305
306 all = 0777,
307
308 set_uid = 04000,
309 set_gid = 02000,
310 sticky_bit = 01000,
311 mask = 07777,
312 unknown = 0xFFFF,
313};
314
315_LIBCPP_INLINE_VISIBILITY
316inline constexpr perms operator&(perms _LHS, perms _RHS) {
317 return static_cast<perms>(static_cast<unsigned>(_LHS) &
318 static_cast<unsigned>(_RHS));
319}
320
321_LIBCPP_INLINE_VISIBILITY
322inline constexpr perms operator|(perms _LHS, perms _RHS) {
323 return static_cast<perms>(static_cast<unsigned>(_LHS) |
324 static_cast<unsigned>(_RHS));
325}
326
327_LIBCPP_INLINE_VISIBILITY
328inline constexpr perms operator^(perms _LHS, perms _RHS) {
329 return static_cast<perms>(static_cast<unsigned>(_LHS) ^
330 static_cast<unsigned>(_RHS));
331}
332
333_LIBCPP_INLINE_VISIBILITY
334inline constexpr perms operator~(perms _LHS) {
335 return static_cast<perms>(~static_cast<unsigned>(_LHS));
336}
337
338_LIBCPP_INLINE_VISIBILITY
339inline perms& operator&=(perms& _LHS, perms _RHS) { return _LHS = _LHS & _RHS; }
340
341_LIBCPP_INLINE_VISIBILITY
342inline perms& operator|=(perms& _LHS, perms _RHS) { return _LHS = _LHS | _RHS; }
343
344_LIBCPP_INLINE_VISIBILITY
345inline perms& operator^=(perms& _LHS, perms _RHS) { return _LHS = _LHS ^ _RHS; }
346
347enum class _LIBCPP_ENUM_VIS perm_options : unsigned char {
348 replace = 1,
349 add = 2,
350 remove = 4,
351 nofollow = 8
352};
353
354_LIBCPP_INLINE_VISIBILITY
355inline constexpr perm_options operator&(perm_options _LHS, perm_options _RHS) {
356 return static_cast<perm_options>(static_cast<unsigned>(_LHS) &
357 static_cast<unsigned>(_RHS));
358}
359
360_LIBCPP_INLINE_VISIBILITY
361inline constexpr perm_options operator|(perm_options _LHS, perm_options _RHS) {
362 return static_cast<perm_options>(static_cast<unsigned>(_LHS) |
363 static_cast<unsigned>(_RHS));
364}
365
366_LIBCPP_INLINE_VISIBILITY
367inline constexpr perm_options operator^(perm_options _LHS, perm_options _RHS) {
368 return static_cast<perm_options>(static_cast<unsigned>(_LHS) ^
369 static_cast<unsigned>(_RHS));
370}
371
372_LIBCPP_INLINE_VISIBILITY
373inline constexpr perm_options operator~(perm_options _LHS) {
374 return static_cast<perm_options>(~static_cast<unsigned>(_LHS));
375}
376
377_LIBCPP_INLINE_VISIBILITY
378inline perm_options& operator&=(perm_options& _LHS, perm_options _RHS) {
379 return _LHS = _LHS & _RHS;
380}
381
382_LIBCPP_INLINE_VISIBILITY
383inline perm_options& operator|=(perm_options& _LHS, perm_options _RHS) {
384 return _LHS = _LHS | _RHS;
385}
386
387_LIBCPP_INLINE_VISIBILITY
388inline perm_options& operator^=(perm_options& _LHS, perm_options _RHS) {
389 return _LHS = _LHS ^ _RHS;
390}
391
392enum class _LIBCPP_ENUM_VIS copy_options : unsigned short {
393 none = 0,
394 skip_existing = 1,
395 overwrite_existing = 2,
396 update_existing = 4,
397 recursive = 8,
398 copy_symlinks = 16,
399 skip_symlinks = 32,
400 directories_only = 64,
401 create_symlinks = 128,
402 create_hard_links = 256,
403 __in_recursive_copy = 512,
404};
405
406_LIBCPP_INLINE_VISIBILITY
407inline constexpr copy_options operator&(copy_options _LHS, copy_options _RHS) {
408 return static_cast<copy_options>(static_cast<unsigned short>(_LHS) &
409 static_cast<unsigned short>(_RHS));
410}
411
412_LIBCPP_INLINE_VISIBILITY
413inline constexpr copy_options operator|(copy_options _LHS, copy_options _RHS) {
414 return static_cast<copy_options>(static_cast<unsigned short>(_LHS) |
415 static_cast<unsigned short>(_RHS));
416}
417
418_LIBCPP_INLINE_VISIBILITY
419inline constexpr copy_options operator^(copy_options _LHS, copy_options _RHS) {
420 return static_cast<copy_options>(static_cast<unsigned short>(_LHS) ^
421 static_cast<unsigned short>(_RHS));
422}
423
424_LIBCPP_INLINE_VISIBILITY
425inline constexpr copy_options operator~(copy_options _LHS) {
426 return static_cast<copy_options>(~static_cast<unsigned short>(_LHS));
427}
428
429_LIBCPP_INLINE_VISIBILITY
430inline copy_options& operator&=(copy_options& _LHS, copy_options _RHS) {
431 return _LHS = _LHS & _RHS;
432}
433
434_LIBCPP_INLINE_VISIBILITY
435inline copy_options& operator|=(copy_options& _LHS, copy_options _RHS) {
436 return _LHS = _LHS | _RHS;
437}
438
439_LIBCPP_INLINE_VISIBILITY
440inline copy_options& operator^=(copy_options& _LHS, copy_options _RHS) {
441 return _LHS = _LHS ^ _RHS;
442}
443
444enum class _LIBCPP_ENUM_VIS directory_options : unsigned char {
445 none = 0,
446 follow_directory_symlink = 1,
447 skip_permission_denied = 2
448};
449
450_LIBCPP_INLINE_VISIBILITY
451inline constexpr directory_options operator&(directory_options _LHS,
452 directory_options _RHS) {
453 return static_cast<directory_options>(static_cast<unsigned char>(_LHS) &
454 static_cast<unsigned char>(_RHS));
455}
456
457_LIBCPP_INLINE_VISIBILITY
458inline constexpr directory_options operator|(directory_options _LHS,
459 directory_options _RHS) {
460 return static_cast<directory_options>(static_cast<unsigned char>(_LHS) |
461 static_cast<unsigned char>(_RHS));
462}
463
464_LIBCPP_INLINE_VISIBILITY
465inline constexpr directory_options operator^(directory_options _LHS,
466 directory_options _RHS) {
467 return static_cast<directory_options>(static_cast<unsigned char>(_LHS) ^
468 static_cast<unsigned char>(_RHS));
469}
470
471_LIBCPP_INLINE_VISIBILITY
472inline constexpr directory_options operator~(directory_options _LHS) {
473 return static_cast<directory_options>(~static_cast<unsigned char>(_LHS));
474}
475
476_LIBCPP_INLINE_VISIBILITY
477inline directory_options& operator&=(directory_options& _LHS,
478 directory_options _RHS) {
479 return _LHS = _LHS & _RHS;
480}
481
482_LIBCPP_INLINE_VISIBILITY
483inline directory_options& operator|=(directory_options& _LHS,
484 directory_options _RHS) {
485 return _LHS = _LHS | _RHS;
486}
487
488_LIBCPP_INLINE_VISIBILITY
489inline directory_options& operator^=(directory_options& _LHS,
490 directory_options _RHS) {
491 return _LHS = _LHS ^ _RHS;
492}
493
494class _LIBCPP_TYPE_VIS file_status {
495public:
496 // constructors
497 _LIBCPP_INLINE_VISIBILITY
498 file_status() noexcept : file_status(file_type::none) {}
499 _LIBCPP_INLINE_VISIBILITY
500 explicit file_status(file_type __ft, perms __prms = perms::unknown) noexcept
501 : __ft_(__ft),
502 __prms_(__prms) {}
503
504 file_status(const file_status&) noexcept = default;
505 file_status(file_status&&) noexcept = default;
506
507 _LIBCPP_INLINE_VISIBILITY
508 ~file_status() {}
509
510 file_status& operator=(const file_status&) noexcept = default;
511 file_status& operator=(file_status&&) noexcept = default;
512
513 // observers
514 _LIBCPP_INLINE_VISIBILITY
515 file_type type() const noexcept { return __ft_; }
516
517 _LIBCPP_INLINE_VISIBILITY
518 perms permissions() const noexcept { return __prms_; }
519
520 // modifiers
521 _LIBCPP_INLINE_VISIBILITY
522 void type(file_type __ft) noexcept { __ft_ = __ft; }
523
524 _LIBCPP_INLINE_VISIBILITY
525 void permissions(perms __p) noexcept { __prms_ = __p; }
526
527private:
528 file_type __ft_;
529 perms __prms_;
530};
531
532class _LIBCPP_TYPE_VIS directory_entry;
533
534template <class _Tp>
535struct __can_convert_char {
536 static const bool value = false;
537};
538template <class _Tp>
539struct __can_convert_char<const _Tp> : public __can_convert_char<_Tp> {};
540template <>
541struct __can_convert_char<char> {
542 static const bool value = true;
543 using __char_type = char;
544};
545template <>
546struct __can_convert_char<wchar_t> {
547 static const bool value = true;
548 using __char_type = wchar_t;
549};
Martin Storsjöe3a71972020-10-26 13:18:46 +0200550#ifndef _LIBCPP_NO_HAS_CHAR8_T
551template <>
552struct __can_convert_char<char8_t> {
553 static const bool value = true;
554 using __char_type = char8_t;
555};
556#endif
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000557template <>
558struct __can_convert_char<char16_t> {
559 static const bool value = true;
560 using __char_type = char16_t;
561};
562template <>
563struct __can_convert_char<char32_t> {
564 static const bool value = true;
565 using __char_type = char32_t;
566};
567
568template <class _ECharT>
569typename enable_if<__can_convert_char<_ECharT>::value, bool>::type
570__is_separator(_ECharT __e) {
571 return __e == _ECharT('/');
Eric Fiselierb41db9a2018-10-01 01:59:37 +0000572}
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000573
Martin Storsjö2ae96532020-10-27 11:46:06 +0200574struct _NullSentinel {};
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000575
576template <class _Tp>
577using _Void = void;
578
579template <class _Tp, class = void>
580struct __is_pathable_string : public false_type {};
581
582template <class _ECharT, class _Traits, class _Alloc>
583struct __is_pathable_string<
584 basic_string<_ECharT, _Traits, _Alloc>,
585 _Void<typename __can_convert_char<_ECharT>::__char_type> >
586 : public __can_convert_char<_ECharT> {
587 using _Str = basic_string<_ECharT, _Traits, _Alloc>;
588 using _Base = __can_convert_char<_ECharT>;
589 static _ECharT const* __range_begin(_Str const& __s) { return __s.data(); }
590 static _ECharT const* __range_end(_Str const& __s) {
591 return __s.data() + __s.length();
592 }
593 static _ECharT __first_or_null(_Str const& __s) {
594 return __s.empty() ? _ECharT{} : __s[0];
595 }
596};
597
598template <class _ECharT, class _Traits>
599struct __is_pathable_string<
600 basic_string_view<_ECharT, _Traits>,
601 _Void<typename __can_convert_char<_ECharT>::__char_type> >
602 : public __can_convert_char<_ECharT> {
603 using _Str = basic_string_view<_ECharT, _Traits>;
604 using _Base = __can_convert_char<_ECharT>;
605 static _ECharT const* __range_begin(_Str const& __s) { return __s.data(); }
606 static _ECharT const* __range_end(_Str const& __s) {
607 return __s.data() + __s.length();
608 }
609 static _ECharT __first_or_null(_Str const& __s) {
610 return __s.empty() ? _ECharT{} : __s[0];
611 }
612};
613
614template <class _Source, class _DS = typename decay<_Source>::type,
615 class _UnqualPtrType =
616 typename remove_const<typename remove_pointer<_DS>::type>::type,
617 bool _IsCharPtr = is_pointer<_DS>::value&&
618 __can_convert_char<_UnqualPtrType>::value>
619struct __is_pathable_char_array : false_type {};
620
621template <class _Source, class _ECharT, class _UPtr>
622struct __is_pathable_char_array<_Source, _ECharT*, _UPtr, true>
623 : __can_convert_char<typename remove_const<_ECharT>::type> {
624 using _Base = __can_convert_char<typename remove_const<_ECharT>::type>;
625
626 static _ECharT const* __range_begin(const _ECharT* __b) { return __b; }
627 static _ECharT const* __range_end(const _ECharT* __b) {
628 using _Iter = const _ECharT*;
Martin Storsjö2ae96532020-10-27 11:46:06 +0200629 const _ECharT __sentinel = _ECharT{};
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000630 _Iter __e = __b;
Martin Storsjö2ae96532020-10-27 11:46:06 +0200631 for (; *__e != __sentinel; ++__e)
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000632 ;
633 return __e;
634 }
635
636 static _ECharT __first_or_null(const _ECharT* __b) { return *__b; }
637};
638
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500639template <class _Iter, bool _IsIt = __is_cpp17_input_iterator<_Iter>::value,
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000640 class = void>
641struct __is_pathable_iter : false_type {};
642
643template <class _Iter>
644struct __is_pathable_iter<
645 _Iter, true,
646 _Void<typename __can_convert_char<
647 typename iterator_traits<_Iter>::value_type>::__char_type> >
648 : __can_convert_char<typename iterator_traits<_Iter>::value_type> {
649 using _ECharT = typename iterator_traits<_Iter>::value_type;
650 using _Base = __can_convert_char<_ECharT>;
651
652 static _Iter __range_begin(_Iter __b) { return __b; }
Martin Storsjö2ae96532020-10-27 11:46:06 +0200653 static _NullSentinel __range_end(_Iter) { return _NullSentinel{}; }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000654
655 static _ECharT __first_or_null(_Iter __b) { return *__b; }
656};
657
658template <class _Tp, bool _IsStringT = __is_pathable_string<_Tp>::value,
659 bool _IsCharIterT = __is_pathable_char_array<_Tp>::value,
660 bool _IsIterT = !_IsCharIterT && __is_pathable_iter<_Tp>::value>
661struct __is_pathable : false_type {
662 static_assert(!_IsStringT && !_IsCharIterT && !_IsIterT, "Must all be false");
663};
664
665template <class _Tp>
666struct __is_pathable<_Tp, true, false, false> : __is_pathable_string<_Tp> {};
667
668template <class _Tp>
669struct __is_pathable<_Tp, false, true, false> : __is_pathable_char_array<_Tp> {
670};
671
672template <class _Tp>
673struct __is_pathable<_Tp, false, false, true> : __is_pathable_iter<_Tp> {};
674
675template <class _ECharT>
Louis Dionne8d053eb2020-10-09 15:31:05 -0400676struct _PathCVT;
677
678#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
679template <class _ECharT>
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000680struct _PathCVT {
681 static_assert(__can_convert_char<_ECharT>::value,
682 "Char type not convertible");
683
684 typedef __narrow_to_utf8<sizeof(_ECharT) * __CHAR_BIT__> _Narrower;
685
686 static void __append_range(string& __dest, _ECharT const* __b,
687 _ECharT const* __e) {
688 _Narrower()(back_inserter(__dest), __b, __e);
689 }
690
691 template <class _Iter>
692 static void __append_range(string& __dest, _Iter __b, _Iter __e) {
693 static_assert(!is_same<_Iter, _ECharT*>::value, "Call const overload");
694 if (__b == __e)
695 return;
696 basic_string<_ECharT> __tmp(__b, __e);
697 _Narrower()(back_inserter(__dest), __tmp.data(),
698 __tmp.data() + __tmp.length());
699 }
700
701 template <class _Iter>
Martin Storsjö2ae96532020-10-27 11:46:06 +0200702 static void __append_range(string& __dest, _Iter __b, _NullSentinel) {
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000703 static_assert(!is_same<_Iter, _ECharT*>::value, "Call const overload");
Martin Storsjö2ae96532020-10-27 11:46:06 +0200704 const _ECharT __sentinel = _ECharT{};
705 if (*__b == __sentinel)
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000706 return;
707 basic_string<_ECharT> __tmp;
Martin Storsjö2ae96532020-10-27 11:46:06 +0200708 for (; *__b != __sentinel; ++__b)
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000709 __tmp.push_back(*__b);
710 _Narrower()(back_inserter(__dest), __tmp.data(),
711 __tmp.data() + __tmp.length());
712 }
713
714 template <class _Source>
715 static void __append_source(string& __dest, _Source const& __s) {
716 using _Traits = __is_pathable<_Source>;
717 __append_range(__dest, _Traits::__range_begin(__s),
718 _Traits::__range_end(__s));
719 }
720};
Louis Dionne8d053eb2020-10-09 15:31:05 -0400721#endif // !_LIBCPP_HAS_NO_LOCALIZATION
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000722
723template <>
724struct _PathCVT<char> {
725
726 template <class _Iter>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500727 static typename enable_if<__is_exactly_cpp17_input_iterator<_Iter>::value>::type
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000728 __append_range(string& __dest, _Iter __b, _Iter __e) {
729 for (; __b != __e; ++__b)
730 __dest.push_back(*__b);
731 }
732
733 template <class _Iter>
Eric Fiseliercd5a6772019-11-18 01:46:58 -0500734 static typename enable_if<__is_cpp17_forward_iterator<_Iter>::value>::type
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000735 __append_range(string& __dest, _Iter __b, _Iter __e) {
736 __dest.__append_forward_unsafe(__b, __e);
737 }
738
739 template <class _Iter>
Martin Storsjö2ae96532020-10-27 11:46:06 +0200740 static void __append_range(string& __dest, _Iter __b, _NullSentinel) {
741 const char __sentinel = char{};
742 for (; *__b != __sentinel; ++__b)
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000743 __dest.push_back(*__b);
744 }
745
746 template <class _Source>
747 static void __append_source(string& __dest, _Source const& __s) {
748 using _Traits = __is_pathable<_Source>;
749 __append_range(__dest, _Traits::__range_begin(__s),
750 _Traits::__range_end(__s));
751 }
752};
753
754class _LIBCPP_TYPE_VIS path {
755 template <class _SourceOrIter, class _Tp = path&>
756 using _EnableIfPathable =
757 typename enable_if<__is_pathable<_SourceOrIter>::value, _Tp>::type;
758
759 template <class _Tp>
760 using _SourceChar = typename __is_pathable<_Tp>::__char_type;
761
762 template <class _Tp>
763 using _SourceCVT = _PathCVT<_SourceChar<_Tp> >;
764
765public:
766 typedef char value_type;
767 typedef basic_string<value_type> string_type;
768 typedef _VSTD::string_view __string_view;
769 static constexpr value_type preferred_separator = '/';
770
771 enum class _LIBCPP_ENUM_VIS format : unsigned char {
772 auto_format,
773 native_format,
774 generic_format
775 };
776
777 // constructors and destructor
778 _LIBCPP_INLINE_VISIBILITY path() noexcept {}
779 _LIBCPP_INLINE_VISIBILITY path(const path& __p) : __pn_(__p.__pn_) {}
780 _LIBCPP_INLINE_VISIBILITY path(path&& __p) noexcept
781 : __pn_(_VSTD::move(__p.__pn_)) {}
782
783 _LIBCPP_INLINE_VISIBILITY
784 path(string_type&& __s, format = format::auto_format) noexcept
785 : __pn_(_VSTD::move(__s)) {}
786
787 template <class _Source, class = _EnableIfPathable<_Source, void> >
788 path(const _Source& __src, format = format::auto_format) {
789 _SourceCVT<_Source>::__append_source(__pn_, __src);
790 }
791
792 template <class _InputIt>
793 path(_InputIt __first, _InputIt __last, format = format::auto_format) {
794 typedef typename iterator_traits<_InputIt>::value_type _ItVal;
795 _PathCVT<_ItVal>::__append_range(__pn_, __first, __last);
796 }
797
Louis Dionne8d053eb2020-10-09 15:31:05 -0400798#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000799 // TODO Implement locale conversions.
800 template <class _Source, class = _EnableIfPathable<_Source, void> >
801 path(const _Source& __src, const locale& __loc, format = format::auto_format);
802 template <class _InputIt>
803 path(_InputIt __first, _InputIt _last, const locale& __loc,
804 format = format::auto_format);
Louis Dionne8d053eb2020-10-09 15:31:05 -0400805#endif
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000806
807 _LIBCPP_INLINE_VISIBILITY
808 ~path() = default;
809
810 // assignments
811 _LIBCPP_INLINE_VISIBILITY
812 path& operator=(const path& __p) {
813 __pn_ = __p.__pn_;
814 return *this;
815 }
816
817 _LIBCPP_INLINE_VISIBILITY
818 path& operator=(path&& __p) noexcept {
819 __pn_ = _VSTD::move(__p.__pn_);
820 return *this;
821 }
822
823 template <class = void>
824 _LIBCPP_INLINE_VISIBILITY path& operator=(string_type&& __s) noexcept {
825 __pn_ = _VSTD::move(__s);
826 return *this;
827 }
828
829 _LIBCPP_INLINE_VISIBILITY
830 path& assign(string_type&& __s) noexcept {
831 __pn_ = _VSTD::move(__s);
832 return *this;
833 }
834
835 template <class _Source>
836 _LIBCPP_INLINE_VISIBILITY _EnableIfPathable<_Source>
837 operator=(const _Source& __src) {
838 return this->assign(__src);
839 }
840
841 template <class _Source>
842 _EnableIfPathable<_Source> assign(const _Source& __src) {
843 __pn_.clear();
844 _SourceCVT<_Source>::__append_source(__pn_, __src);
845 return *this;
846 }
847
848 template <class _InputIt>
849 path& assign(_InputIt __first, _InputIt __last) {
850 typedef typename iterator_traits<_InputIt>::value_type _ItVal;
851 __pn_.clear();
852 _PathCVT<_ItVal>::__append_range(__pn_, __first, __last);
853 return *this;
854 }
855
856private:
857 template <class _ECharT>
858 static bool __source_is_absolute(_ECharT __first_or_null) {
859 return __is_separator(__first_or_null);
860 }
861
862public:
863 // appends
864 path& operator/=(const path& __p) {
865 if (__p.is_absolute()) {
866 __pn_ = __p.__pn_;
867 return *this;
868 }
869 if (has_filename())
870 __pn_ += preferred_separator;
871 __pn_ += __p.native();
872 return *this;
873 }
874
875 // FIXME: Use _LIBCPP_DIAGNOSE_WARNING to produce a diagnostic when __src
876 // is known at compile time to be "/' since the user almost certainly intended
877 // to append a separator instead of overwriting the path with "/"
878 template <class _Source>
879 _LIBCPP_INLINE_VISIBILITY _EnableIfPathable<_Source>
880 operator/=(const _Source& __src) {
881 return this->append(__src);
882 }
883
884 template <class _Source>
885 _EnableIfPathable<_Source> append(const _Source& __src) {
886 using _Traits = __is_pathable<_Source>;
887 using _CVT = _PathCVT<_SourceChar<_Source> >;
888 if (__source_is_absolute(_Traits::__first_or_null(__src)))
889 __pn_.clear();
890 else if (has_filename())
891 __pn_ += preferred_separator;
892 _CVT::__append_source(__pn_, __src);
893 return *this;
894 }
895
896 template <class _InputIt>
897 path& append(_InputIt __first, _InputIt __last) {
898 typedef typename iterator_traits<_InputIt>::value_type _ItVal;
899 static_assert(__can_convert_char<_ItVal>::value, "Must convertible");
900 using _CVT = _PathCVT<_ItVal>;
901 if (__first != __last && __source_is_absolute(*__first))
902 __pn_.clear();
903 else if (has_filename())
904 __pn_ += preferred_separator;
905 _CVT::__append_range(__pn_, __first, __last);
906 return *this;
907 }
908
909 // concatenation
910 _LIBCPP_INLINE_VISIBILITY
911 path& operator+=(const path& __x) {
912 __pn_ += __x.__pn_;
913 return *this;
914 }
915
916 _LIBCPP_INLINE_VISIBILITY
917 path& operator+=(const string_type& __x) {
918 __pn_ += __x;
919 return *this;
920 }
921
922 _LIBCPP_INLINE_VISIBILITY
923 path& operator+=(__string_view __x) {
924 __pn_ += __x;
925 return *this;
926 }
927
928 _LIBCPP_INLINE_VISIBILITY
929 path& operator+=(const value_type* __x) {
930 __pn_ += __x;
931 return *this;
932 }
933
934 _LIBCPP_INLINE_VISIBILITY
935 path& operator+=(value_type __x) {
936 __pn_ += __x;
937 return *this;
938 }
939
940 template <class _ECharT>
941 typename enable_if<__can_convert_char<_ECharT>::value, path&>::type
942 operator+=(_ECharT __x) {
Marek Kurdej306a1b02020-12-07 20:07:25 +0100943 _PathCVT<_ECharT>::__append_source(__pn_,
944 basic_string_view<_ECharT>(&__x, 1));
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000945 return *this;
946 }
947
948 template <class _Source>
949 _EnableIfPathable<_Source> operator+=(const _Source& __x) {
950 return this->concat(__x);
951 }
952
953 template <class _Source>
954 _EnableIfPathable<_Source> concat(const _Source& __x) {
955 _SourceCVT<_Source>::__append_source(__pn_, __x);
956 return *this;
957 }
958
959 template <class _InputIt>
960 path& concat(_InputIt __first, _InputIt __last) {
961 typedef typename iterator_traits<_InputIt>::value_type _ItVal;
962 _PathCVT<_ItVal>::__append_range(__pn_, __first, __last);
963 return *this;
964 }
965
966 // modifiers
967 _LIBCPP_INLINE_VISIBILITY
968 void clear() noexcept { __pn_.clear(); }
969
970 path& make_preferred() { return *this; }
971
972 _LIBCPP_INLINE_VISIBILITY
973 path& remove_filename() {
974 auto __fname = __filename();
975 if (!__fname.empty())
976 __pn_.erase(__fname.data() - __pn_.data());
977 return *this;
978 }
979
980 path& replace_filename(const path& __replacement) {
981 remove_filename();
982 return (*this /= __replacement);
983 }
984
985 path& replace_extension(const path& __replacement = path());
986
987 _LIBCPP_INLINE_VISIBILITY
988 void swap(path& __rhs) noexcept { __pn_.swap(__rhs.__pn_); }
989
990 // private helper to allow reserving memory in the path
991 _LIBCPP_INLINE_VISIBILITY
992 void __reserve(size_t __s) { __pn_.reserve(__s); }
993
994 // native format observers
995 _LIBCPP_INLINE_VISIBILITY
996 const string_type& native() const noexcept { return __pn_; }
997
998 _LIBCPP_INLINE_VISIBILITY
999 const value_type* c_str() const noexcept { return __pn_.c_str(); }
1000
1001 _LIBCPP_INLINE_VISIBILITY operator string_type() const { return __pn_; }
1002
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001003 _LIBCPP_INLINE_VISIBILITY _VSTD::string string() const { return __pn_; }
Martin Storsjöe3a71972020-10-26 13:18:46 +02001004#ifndef _LIBCPP_NO_HAS_CHAR8_T
1005 _LIBCPP_INLINE_VISIBILITY _VSTD::u8string u8string() const { return _VSTD::u8string(__pn_.begin(), __pn_.end()); }
1006#else
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001007 _LIBCPP_INLINE_VISIBILITY _VSTD::string u8string() const { return __pn_; }
Martin Storsjöe3a71972020-10-26 13:18:46 +02001008#endif
Louis Dionne8d053eb2020-10-09 15:31:05 -04001009
1010#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001011 template <class _ECharT, class _Traits = char_traits<_ECharT>,
1012 class _Allocator = allocator<_ECharT> >
1013 basic_string<_ECharT, _Traits, _Allocator>
1014 string(const _Allocator& __a = _Allocator()) const {
1015 using _CVT = __widen_from_utf8<sizeof(_ECharT) * __CHAR_BIT__>;
1016 using _Str = basic_string<_ECharT, _Traits, _Allocator>;
1017 _Str __s(__a);
1018 __s.reserve(__pn_.size());
1019 _CVT()(back_inserter(__s), __pn_.data(), __pn_.data() + __pn_.size());
1020 return __s;
1021 }
1022
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001023 _LIBCPP_INLINE_VISIBILITY _VSTD::wstring wstring() const {
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001024 return string<wchar_t>();
1025 }
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001026 _LIBCPP_INLINE_VISIBILITY _VSTD::u16string u16string() const {
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001027 return string<char16_t>();
1028 }
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001029 _LIBCPP_INLINE_VISIBILITY _VSTD::u32string u32string() const {
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001030 return string<char32_t>();
1031 }
Louis Dionne8d053eb2020-10-09 15:31:05 -04001032#endif
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001033
1034 // generic format observers
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001035 _VSTD::string generic_string() const { return __pn_; }
Martin Storsjöe3a71972020-10-26 13:18:46 +02001036#ifndef _LIBCPP_NO_HAS_CHAR8_T
1037 _VSTD::u8string generic_u8string() const { return _VSTD::u8string(__pn_.begin(), __pn_.end()); }
1038#else
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001039 _VSTD::string generic_u8string() const { return __pn_; }
Martin Storsjöe3a71972020-10-26 13:18:46 +02001040#endif
Louis Dionne8d053eb2020-10-09 15:31:05 -04001041
1042#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001043 template <class _ECharT, class _Traits = char_traits<_ECharT>,
1044 class _Allocator = allocator<_ECharT> >
1045 basic_string<_ECharT, _Traits, _Allocator>
1046 generic_string(const _Allocator& __a = _Allocator()) const {
1047 return string<_ECharT, _Traits, _Allocator>(__a);
1048 }
1049
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001050 _VSTD::wstring generic_wstring() const { return string<wchar_t>(); }
1051 _VSTD::u16string generic_u16string() const { return string<char16_t>(); }
1052 _VSTD::u32string generic_u32string() const { return string<char32_t>(); }
Louis Dionne8d053eb2020-10-09 15:31:05 -04001053#endif
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001054
1055private:
1056 int __compare(__string_view) const;
1057 __string_view __root_name() const;
1058 __string_view __root_directory() const;
1059 __string_view __root_path_raw() const;
1060 __string_view __relative_path() const;
1061 __string_view __parent_path() const;
1062 __string_view __filename() const;
1063 __string_view __stem() const;
1064 __string_view __extension() const;
1065
1066public:
1067 // compare
1068 _LIBCPP_INLINE_VISIBILITY int compare(const path& __p) const noexcept {
1069 return __compare(__p.__pn_);
1070 }
1071 _LIBCPP_INLINE_VISIBILITY int compare(const string_type& __s) const {
1072 return __compare(__s);
1073 }
1074 _LIBCPP_INLINE_VISIBILITY int compare(__string_view __s) const {
1075 return __compare(__s);
1076 }
1077 _LIBCPP_INLINE_VISIBILITY int compare(const value_type* __s) const {
1078 return __compare(__s);
1079 }
1080
1081 // decomposition
1082 _LIBCPP_INLINE_VISIBILITY path root_name() const {
1083 return string_type(__root_name());
1084 }
1085 _LIBCPP_INLINE_VISIBILITY path root_directory() const {
1086 return string_type(__root_directory());
1087 }
1088 _LIBCPP_INLINE_VISIBILITY path root_path() const {
1089 return root_name().append(string_type(__root_directory()));
1090 }
1091 _LIBCPP_INLINE_VISIBILITY path relative_path() const {
1092 return string_type(__relative_path());
1093 }
1094 _LIBCPP_INLINE_VISIBILITY path parent_path() const {
1095 return string_type(__parent_path());
1096 }
1097 _LIBCPP_INLINE_VISIBILITY path filename() const {
1098 return string_type(__filename());
1099 }
1100 _LIBCPP_INLINE_VISIBILITY path stem() const { return string_type(__stem()); }
1101 _LIBCPP_INLINE_VISIBILITY path extension() const {
1102 return string_type(__extension());
1103 }
1104
1105 // query
1106 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY bool
1107 empty() const noexcept {
1108 return __pn_.empty();
1109 }
1110
1111 _LIBCPP_INLINE_VISIBILITY bool has_root_name() const {
1112 return !__root_name().empty();
1113 }
1114 _LIBCPP_INLINE_VISIBILITY bool has_root_directory() const {
1115 return !__root_directory().empty();
1116 }
1117 _LIBCPP_INLINE_VISIBILITY bool has_root_path() const {
1118 return !__root_path_raw().empty();
1119 }
1120 _LIBCPP_INLINE_VISIBILITY bool has_relative_path() const {
1121 return !__relative_path().empty();
1122 }
1123 _LIBCPP_INLINE_VISIBILITY bool has_parent_path() const {
1124 return !__parent_path().empty();
1125 }
1126 _LIBCPP_INLINE_VISIBILITY bool has_filename() const {
1127 return !__filename().empty();
1128 }
1129 _LIBCPP_INLINE_VISIBILITY bool has_stem() const { return !__stem().empty(); }
1130 _LIBCPP_INLINE_VISIBILITY bool has_extension() const {
1131 return !__extension().empty();
1132 }
1133
1134 _LIBCPP_INLINE_VISIBILITY bool is_absolute() const {
1135 return has_root_directory();
1136 }
1137 _LIBCPP_INLINE_VISIBILITY bool is_relative() const { return !is_absolute(); }
1138
1139 // relative paths
1140 path lexically_normal() const;
1141 path lexically_relative(const path& __base) const;
1142
1143 _LIBCPP_INLINE_VISIBILITY path lexically_proximate(const path& __base) const {
1144 path __result = this->lexically_relative(__base);
1145 if (__result.native().empty())
1146 return *this;
1147 return __result;
1148 }
1149
1150 // iterators
1151 class _LIBCPP_TYPE_VIS iterator;
1152 typedef iterator const_iterator;
1153
1154 iterator begin() const;
1155 iterator end() const;
1156
Louis Dionne8d053eb2020-10-09 15:31:05 -04001157#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001158 template <class _CharT, class _Traits>
1159 _LIBCPP_INLINE_VISIBILITY friend
1160 typename enable_if<is_same<_CharT, char>::value &&
1161 is_same<_Traits, char_traits<char> >::value,
1162 basic_ostream<_CharT, _Traits>&>::type
1163 operator<<(basic_ostream<_CharT, _Traits>& __os, const path& __p) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001164 __os << _VSTD::__quoted(__p.native());
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001165 return __os;
1166 }
1167
1168 template <class _CharT, class _Traits>
1169 _LIBCPP_INLINE_VISIBILITY friend
1170 typename enable_if<!is_same<_CharT, char>::value ||
1171 !is_same<_Traits, char_traits<char> >::value,
1172 basic_ostream<_CharT, _Traits>&>::type
1173 operator<<(basic_ostream<_CharT, _Traits>& __os, const path& __p) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001174 __os << _VSTD::__quoted(__p.string<_CharT, _Traits>());
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001175 return __os;
1176 }
1177
1178 template <class _CharT, class _Traits>
1179 _LIBCPP_INLINE_VISIBILITY friend basic_istream<_CharT, _Traits>&
1180 operator>>(basic_istream<_CharT, _Traits>& __is, path& __p) {
1181 basic_string<_CharT, _Traits> __tmp;
1182 __is >> __quoted(__tmp);
1183 __p = __tmp;
1184 return __is;
1185 }
Louis Dionne8d053eb2020-10-09 15:31:05 -04001186#endif // !_LIBCPP_HAS_NO_LOCALIZATION
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001187
Eric Fiselierab3f9d62018-12-21 04:09:01 +00001188 friend _LIBCPP_INLINE_VISIBILITY bool operator==(const path& __lhs, const path& __rhs) noexcept {
1189 return __lhs.compare(__rhs) == 0;
1190 }
1191 friend _LIBCPP_INLINE_VISIBILITY bool operator!=(const path& __lhs, const path& __rhs) noexcept {
1192 return __lhs.compare(__rhs) != 0;
1193 }
1194 friend _LIBCPP_INLINE_VISIBILITY bool operator<(const path& __lhs, const path& __rhs) noexcept {
1195 return __lhs.compare(__rhs) < 0;
1196 }
1197 friend _LIBCPP_INLINE_VISIBILITY bool operator<=(const path& __lhs, const path& __rhs) noexcept {
1198 return __lhs.compare(__rhs) <= 0;
1199 }
1200 friend _LIBCPP_INLINE_VISIBILITY bool operator>(const path& __lhs, const path& __rhs) noexcept {
1201 return __lhs.compare(__rhs) > 0;
1202 }
1203 friend _LIBCPP_INLINE_VISIBILITY bool operator>=(const path& __lhs, const path& __rhs) noexcept {
1204 return __lhs.compare(__rhs) >= 0;
1205 }
1206
1207 friend _LIBCPP_INLINE_VISIBILITY path operator/(const path& __lhs,
1208 const path& __rhs) {
1209 path __result(__lhs);
1210 __result /= __rhs;
1211 return __result;
1212 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001213private:
1214 inline _LIBCPP_INLINE_VISIBILITY path&
1215 __assign_view(__string_view const& __s) noexcept {
1216 __pn_ = string_type(__s);
1217 return *this;
1218 }
1219 string_type __pn_;
1220};
1221
1222inline _LIBCPP_INLINE_VISIBILITY void swap(path& __lhs, path& __rhs) noexcept {
1223 __lhs.swap(__rhs);
1224}
1225
1226_LIBCPP_FUNC_VIS
1227size_t hash_value(const path& __p) noexcept;
1228
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001229template <class _Source>
Martin Storsjöe3a71972020-10-26 13:18:46 +02001230_LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_WITH_CHAR8_T
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001231 typename enable_if<__is_pathable<_Source>::value, path>::type
1232 u8path(const _Source& __s) {
1233 static_assert(
Martin Storsjöe3a71972020-10-26 13:18:46 +02001234#ifndef _LIBCPP_NO_HAS_CHAR8_T
1235 is_same<typename __is_pathable<_Source>::__char_type, char8_t>::value ||
1236#endif
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001237 is_same<typename __is_pathable<_Source>::__char_type, char>::value,
1238 "u8path(Source const&) requires Source have a character type of type "
Martin Storsjö929dff92020-12-15 09:15:37 +02001239 "'char' or 'char8_t'");
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001240 return path(__s);
1241}
1242
1243template <class _InputIt>
Martin Storsjöe3a71972020-10-26 13:18:46 +02001244_LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_WITH_CHAR8_T
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001245 typename enable_if<__is_pathable<_InputIt>::value, path>::type
1246 u8path(_InputIt __f, _InputIt __l) {
1247 static_assert(
Martin Storsjöe3a71972020-10-26 13:18:46 +02001248#ifndef _LIBCPP_NO_HAS_CHAR8_T
1249 is_same<typename __is_pathable<_InputIt>::__char_type, char8_t>::value ||
1250#endif
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001251 is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
Martin Storsjöe3a71972020-10-26 13:18:46 +02001252 "u8path(Iter, Iter) requires Iter have a value_type of type 'char'"
Martin Storsjö929dff92020-12-15 09:15:37 +02001253 " or 'char8_t'");
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001254 return path(__f, __l);
1255}
1256
1257class _LIBCPP_TYPE_VIS path::iterator {
1258public:
1259 enum _ParserState : unsigned char {
1260 _Singular,
1261 _BeforeBegin,
1262 _InRootName,
1263 _InRootDir,
1264 _InFilenames,
1265 _InTrailingSep,
1266 _AtEnd
1267 };
1268
1269public:
1270 typedef bidirectional_iterator_tag iterator_category;
1271
1272 typedef path value_type;
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001273 typedef ptrdiff_t difference_type;
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001274 typedef const path* pointer;
1275 typedef const path& reference;
1276
1277 typedef void
1278 __stashing_iterator_tag; // See reverse_iterator and __is_stashing_iterator
1279
1280public:
1281 _LIBCPP_INLINE_VISIBILITY
1282 iterator()
1283 : __stashed_elem_(), __path_ptr_(nullptr), __entry_(),
1284 __state_(_Singular) {}
1285
1286 iterator(const iterator&) = default;
1287 ~iterator() = default;
1288
1289 iterator& operator=(const iterator&) = default;
1290
1291 _LIBCPP_INLINE_VISIBILITY
1292 reference operator*() const { return __stashed_elem_; }
1293
1294 _LIBCPP_INLINE_VISIBILITY
1295 pointer operator->() const { return &__stashed_elem_; }
1296
1297 _LIBCPP_INLINE_VISIBILITY
1298 iterator& operator++() {
1299 _LIBCPP_ASSERT(__state_ != _Singular,
1300 "attempting to increment a singular iterator");
1301 _LIBCPP_ASSERT(__state_ != _AtEnd,
1302 "attempting to increment the end iterator");
1303 return __increment();
1304 }
1305
1306 _LIBCPP_INLINE_VISIBILITY
1307 iterator operator++(int) {
1308 iterator __it(*this);
1309 this->operator++();
1310 return __it;
1311 }
1312
1313 _LIBCPP_INLINE_VISIBILITY
1314 iterator& operator--() {
1315 _LIBCPP_ASSERT(__state_ != _Singular,
1316 "attempting to decrement a singular iterator");
1317 _LIBCPP_ASSERT(__entry_.data() != __path_ptr_->native().data(),
1318 "attempting to decrement the begin iterator");
1319 return __decrement();
1320 }
1321
1322 _LIBCPP_INLINE_VISIBILITY
1323 iterator operator--(int) {
1324 iterator __it(*this);
1325 this->operator--();
1326 return __it;
1327 }
1328
1329private:
1330 friend class path;
1331
1332 inline _LIBCPP_INLINE_VISIBILITY friend bool operator==(const iterator&,
1333 const iterator&);
1334
1335 iterator& __increment();
1336 iterator& __decrement();
1337
1338 path __stashed_elem_;
1339 const path* __path_ptr_;
1340 path::__string_view __entry_;
1341 _ParserState __state_;
1342};
1343
1344inline _LIBCPP_INLINE_VISIBILITY bool operator==(const path::iterator& __lhs,
1345 const path::iterator& __rhs) {
1346 return __lhs.__path_ptr_ == __rhs.__path_ptr_ &&
1347 __lhs.__entry_.data() == __rhs.__entry_.data();
1348}
1349
1350inline _LIBCPP_INLINE_VISIBILITY bool operator!=(const path::iterator& __lhs,
1351 const path::iterator& __rhs) {
1352 return !(__lhs == __rhs);
1353}
1354
Louis Dionnef6bf76a2019-03-20 21:18:14 +00001355// TODO(ldionne): We need to pop the pragma and push it again after
1356// filesystem_error to work around PR41078.
1357_LIBCPP_AVAILABILITY_FILESYSTEM_POP
1358
1359class _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_EXCEPTION_ABI filesystem_error : public system_error {
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001360public:
1361 _LIBCPP_INLINE_VISIBILITY
1362 filesystem_error(const string& __what, error_code __ec)
1363 : system_error(__ec, __what),
1364 __storage_(make_shared<_Storage>(path(), path())) {
1365 __create_what(0);
1366 }
1367
1368 _LIBCPP_INLINE_VISIBILITY
1369 filesystem_error(const string& __what, const path& __p1, error_code __ec)
1370 : system_error(__ec, __what),
1371 __storage_(make_shared<_Storage>(__p1, path())) {
1372 __create_what(1);
1373 }
1374
1375 _LIBCPP_INLINE_VISIBILITY
1376 filesystem_error(const string& __what, const path& __p1, const path& __p2,
1377 error_code __ec)
1378 : system_error(__ec, __what),
1379 __storage_(make_shared<_Storage>(__p1, __p2)) {
1380 __create_what(2);
1381 }
1382
1383 _LIBCPP_INLINE_VISIBILITY
1384 const path& path1() const noexcept { return __storage_->__p1_; }
1385
1386 _LIBCPP_INLINE_VISIBILITY
1387 const path& path2() const noexcept { return __storage_->__p2_; }
1388
Dimitry Andric47269ce2020-03-13 19:36:26 +01001389 filesystem_error(const filesystem_error&) = default;
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001390 ~filesystem_error() override; // key function
1391
1392 _LIBCPP_INLINE_VISIBILITY
1393 const char* what() const noexcept override {
1394 return __storage_->__what_.c_str();
1395 }
1396
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001397 void __create_what(int __num_paths);
1398
1399private:
Louis Dionne48ae8892019-03-19 17:47:53 +00001400 struct _LIBCPP_HIDDEN _Storage {
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001401 _LIBCPP_INLINE_VISIBILITY
1402 _Storage(const path& __p1, const path& __p2) : __p1_(__p1), __p2_(__p2) {}
1403
1404 path __p1_;
1405 path __p2_;
1406 string __what_;
1407 };
1408 shared_ptr<_Storage> __storage_;
1409};
1410
Louis Dionnef6bf76a2019-03-20 21:18:14 +00001411_LIBCPP_AVAILABILITY_FILESYSTEM_PUSH
1412
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001413template <class... _Args>
1414_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
1415#ifndef _LIBCPP_NO_EXCEPTIONS
Louis Dionne6251ef02019-02-05 15:46:52 +00001416void __throw_filesystem_error(_Args&&... __args) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001417 throw filesystem_error(_VSTD::forward<_Args>(__args)...);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001418}
1419#else
Louis Dionne6251ef02019-02-05 15:46:52 +00001420void __throw_filesystem_error(_Args&&...) {
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001421 _VSTD::abort();
1422}
1423#endif
1424
1425// operational functions
1426
1427_LIBCPP_FUNC_VIS
1428path __absolute(const path&, error_code* __ec = nullptr);
1429_LIBCPP_FUNC_VIS
1430path __canonical(const path&, error_code* __ec = nullptr);
1431_LIBCPP_FUNC_VIS
1432void __copy(const path& __from, const path& __to, copy_options __opt,
1433 error_code* __ec = nullptr);
1434_LIBCPP_FUNC_VIS
1435bool __copy_file(const path& __from, const path& __to, copy_options __opt,
1436 error_code* __ec = nullptr);
1437_LIBCPP_FUNC_VIS
1438void __copy_symlink(const path& __existing_symlink, const path& __new_symlink,
1439 error_code* __ec = nullptr);
1440_LIBCPP_FUNC_VIS
1441bool __create_directories(const path& p, error_code* ec = nullptr);
1442_LIBCPP_FUNC_VIS
1443bool __create_directory(const path& p, error_code* ec = nullptr);
1444_LIBCPP_FUNC_VIS
1445bool __create_directory(const path& p, const path& attributes,
1446 error_code* ec = nullptr);
1447_LIBCPP_FUNC_VIS
1448void __create_directory_symlink(const path& __to, const path& __new_symlink,
1449 error_code* __ec = nullptr);
1450_LIBCPP_FUNC_VIS
1451void __create_hard_link(const path& __to, const path& __new_hard_link,
1452 error_code* __ec = nullptr);
1453_LIBCPP_FUNC_VIS
1454void __create_symlink(const path& __to, const path& __new_symlink,
1455 error_code* __ec = nullptr);
1456_LIBCPP_FUNC_VIS
1457path __current_path(error_code* __ec = nullptr);
1458_LIBCPP_FUNC_VIS
1459void __current_path(const path&, error_code* __ec = nullptr);
1460_LIBCPP_FUNC_VIS
1461bool __equivalent(const path&, const path&, error_code* __ec = nullptr);
1462_LIBCPP_FUNC_VIS
1463uintmax_t __file_size(const path&, error_code* __ec = nullptr);
1464_LIBCPP_FUNC_VIS
1465uintmax_t __hard_link_count(const path&, error_code* __ec = nullptr);
1466_LIBCPP_FUNC_VIS
1467bool __fs_is_empty(const path& p, error_code* ec = nullptr);
1468_LIBCPP_FUNC_VIS
1469file_time_type __last_write_time(const path& p, error_code* ec = nullptr);
1470_LIBCPP_FUNC_VIS
1471void __last_write_time(const path& p, file_time_type new_time,
1472 error_code* ec = nullptr);
1473_LIBCPP_FUNC_VIS
1474void __permissions(const path&, perms, perm_options, error_code* = nullptr);
1475_LIBCPP_FUNC_VIS
1476path __read_symlink(const path& p, error_code* ec = nullptr);
1477_LIBCPP_FUNC_VIS
1478bool __remove(const path& p, error_code* ec = nullptr);
1479_LIBCPP_FUNC_VIS
1480uintmax_t __remove_all(const path& p, error_code* ec = nullptr);
1481_LIBCPP_FUNC_VIS
1482void __rename(const path& from, const path& to, error_code* ec = nullptr);
1483_LIBCPP_FUNC_VIS
1484void __resize_file(const path& p, uintmax_t size, error_code* ec = nullptr);
1485_LIBCPP_FUNC_VIS
1486space_info __space(const path&, error_code* __ec = nullptr);
1487_LIBCPP_FUNC_VIS
1488file_status __status(const path&, error_code* __ec = nullptr);
1489_LIBCPP_FUNC_VIS
1490file_status __symlink_status(const path&, error_code* __ec = nullptr);
1491_LIBCPP_FUNC_VIS
1492path __system_complete(const path&, error_code* __ec = nullptr);
1493_LIBCPP_FUNC_VIS
1494path __temp_directory_path(error_code* __ec = nullptr);
1495_LIBCPP_FUNC_VIS
1496path __weakly_canonical(path const& __p, error_code* __ec = nullptr);
1497
1498inline _LIBCPP_INLINE_VISIBILITY path current_path() {
1499 return __current_path();
1500}
1501
1502inline _LIBCPP_INLINE_VISIBILITY path current_path(error_code& __ec) {
1503 return __current_path(&__ec);
1504}
1505
1506inline _LIBCPP_INLINE_VISIBILITY void current_path(const path& __p) {
1507 __current_path(__p);
1508}
1509
1510inline _LIBCPP_INLINE_VISIBILITY void current_path(const path& __p,
1511 error_code& __ec) noexcept {
1512 __current_path(__p, &__ec);
1513}
1514
1515inline _LIBCPP_INLINE_VISIBILITY path absolute(const path& __p) {
1516 return __absolute(__p);
1517}
1518
1519inline _LIBCPP_INLINE_VISIBILITY path absolute(const path& __p,
1520 error_code& __ec) {
1521 return __absolute(__p, &__ec);
1522}
1523
1524inline _LIBCPP_INLINE_VISIBILITY path canonical(const path& __p) {
1525 return __canonical(__p);
1526}
1527
1528inline _LIBCPP_INLINE_VISIBILITY path canonical(const path& __p,
1529 error_code& __ec) {
1530 return __canonical(__p, &__ec);
1531}
1532
1533inline _LIBCPP_INLINE_VISIBILITY void copy(const path& __from,
1534 const path& __to) {
1535 __copy(__from, __to, copy_options::none);
1536}
1537
1538inline _LIBCPP_INLINE_VISIBILITY void copy(const path& __from, const path& __to,
1539 error_code& __ec) {
1540 __copy(__from, __to, copy_options::none, &__ec);
1541}
1542
1543inline _LIBCPP_INLINE_VISIBILITY void copy(const path& __from, const path& __to,
1544 copy_options __opt) {
1545 __copy(__from, __to, __opt);
1546}
1547
1548inline _LIBCPP_INLINE_VISIBILITY void copy(const path& __from, const path& __to,
1549 copy_options __opt,
1550 error_code& __ec) {
1551 __copy(__from, __to, __opt, &__ec);
1552}
1553
1554inline _LIBCPP_INLINE_VISIBILITY bool copy_file(const path& __from,
1555 const path& __to) {
1556 return __copy_file(__from, __to, copy_options::none);
1557}
1558
1559inline _LIBCPP_INLINE_VISIBILITY bool
1560copy_file(const path& __from, const path& __to, error_code& __ec) {
1561 return __copy_file(__from, __to, copy_options::none, &__ec);
1562}
1563
1564inline _LIBCPP_INLINE_VISIBILITY bool
1565copy_file(const path& __from, const path& __to, copy_options __opt) {
1566 return __copy_file(__from, __to, __opt);
1567}
1568
1569inline _LIBCPP_INLINE_VISIBILITY bool copy_file(const path& __from,
1570 const path& __to,
1571 copy_options __opt,
1572 error_code& __ec) {
1573 return __copy_file(__from, __to, __opt, &__ec);
1574}
1575
1576inline _LIBCPP_INLINE_VISIBILITY void copy_symlink(const path& __existing,
1577 const path& __new) {
1578 __copy_symlink(__existing, __new);
1579}
1580
1581inline _LIBCPP_INLINE_VISIBILITY void
1582copy_symlink(const path& __ext, const path& __new, error_code& __ec) noexcept {
1583 __copy_symlink(__ext, __new, &__ec);
1584}
1585
1586inline _LIBCPP_INLINE_VISIBILITY bool create_directories(const path& __p) {
1587 return __create_directories(__p);
1588}
1589
1590inline _LIBCPP_INLINE_VISIBILITY bool create_directories(const path& __p,
1591 error_code& __ec) {
1592 return __create_directories(__p, &__ec);
1593}
1594
1595inline _LIBCPP_INLINE_VISIBILITY bool create_directory(const path& __p) {
1596 return __create_directory(__p);
1597}
1598
1599inline _LIBCPP_INLINE_VISIBILITY bool
1600create_directory(const path& __p, error_code& __ec) noexcept {
1601 return __create_directory(__p, &__ec);
1602}
1603
1604inline _LIBCPP_INLINE_VISIBILITY bool create_directory(const path& __p,
1605 const path& __attrs) {
1606 return __create_directory(__p, __attrs);
1607}
1608
1609inline _LIBCPP_INLINE_VISIBILITY bool
1610create_directory(const path& __p, const path& __attrs,
1611 error_code& __ec) noexcept {
1612 return __create_directory(__p, __attrs, &__ec);
1613}
1614
1615inline _LIBCPP_INLINE_VISIBILITY void
1616create_directory_symlink(const path& __to, const path& __new) {
1617 __create_directory_symlink(__to, __new);
1618}
1619
1620inline _LIBCPP_INLINE_VISIBILITY void
1621create_directory_symlink(const path& __to, const path& __new,
1622 error_code& __ec) noexcept {
1623 __create_directory_symlink(__to, __new, &__ec);
1624}
1625
1626inline _LIBCPP_INLINE_VISIBILITY void create_hard_link(const path& __to,
1627 const path& __new) {
1628 __create_hard_link(__to, __new);
1629}
1630
1631inline _LIBCPP_INLINE_VISIBILITY void
1632create_hard_link(const path& __to, const path& __new,
1633 error_code& __ec) noexcept {
1634 __create_hard_link(__to, __new, &__ec);
1635}
1636
1637inline _LIBCPP_INLINE_VISIBILITY void create_symlink(const path& __to,
1638 const path& __new) {
1639 __create_symlink(__to, __new);
1640}
1641
1642inline _LIBCPP_INLINE_VISIBILITY void
1643create_symlink(const path& __to, const path& __new, error_code& __ec) noexcept {
1644 return __create_symlink(__to, __new, &__ec);
1645}
1646
1647inline _LIBCPP_INLINE_VISIBILITY bool status_known(file_status __s) noexcept {
1648 return __s.type() != file_type::none;
1649}
1650
1651inline _LIBCPP_INLINE_VISIBILITY bool exists(file_status __s) noexcept {
1652 return status_known(__s) && __s.type() != file_type::not_found;
1653}
1654
1655inline _LIBCPP_INLINE_VISIBILITY bool exists(const path& __p) {
1656 return exists(__status(__p));
1657}
1658
1659inline _LIBCPP_INLINE_VISIBILITY bool exists(const path& __p,
1660 error_code& __ec) noexcept {
1661 auto __s = __status(__p, &__ec);
1662 if (status_known(__s))
1663 __ec.clear();
1664 return exists(__s);
1665}
1666
1667inline _LIBCPP_INLINE_VISIBILITY bool equivalent(const path& __p1,
1668 const path& __p2) {
1669 return __equivalent(__p1, __p2);
1670}
1671
1672inline _LIBCPP_INLINE_VISIBILITY bool
1673equivalent(const path& __p1, const path& __p2, error_code& __ec) noexcept {
1674 return __equivalent(__p1, __p2, &__ec);
1675}
1676
1677inline _LIBCPP_INLINE_VISIBILITY uintmax_t file_size(const path& __p) {
1678 return __file_size(__p);
1679}
1680
1681inline _LIBCPP_INLINE_VISIBILITY uintmax_t
1682file_size(const path& __p, error_code& __ec) noexcept {
1683 return __file_size(__p, &__ec);
1684}
1685
1686inline _LIBCPP_INLINE_VISIBILITY uintmax_t hard_link_count(const path& __p) {
1687 return __hard_link_count(__p);
1688}
1689
1690inline _LIBCPP_INLINE_VISIBILITY uintmax_t
1691hard_link_count(const path& __p, error_code& __ec) noexcept {
1692 return __hard_link_count(__p, &__ec);
1693}
1694
1695inline _LIBCPP_INLINE_VISIBILITY bool is_block_file(file_status __s) noexcept {
1696 return __s.type() == file_type::block;
1697}
1698
1699inline _LIBCPP_INLINE_VISIBILITY bool is_block_file(const path& __p) {
1700 return is_block_file(__status(__p));
1701}
1702
1703inline _LIBCPP_INLINE_VISIBILITY bool is_block_file(const path& __p,
1704 error_code& __ec) noexcept {
1705 return is_block_file(__status(__p, &__ec));
1706}
1707
1708inline _LIBCPP_INLINE_VISIBILITY bool
1709is_character_file(file_status __s) noexcept {
1710 return __s.type() == file_type::character;
1711}
1712
1713inline _LIBCPP_INLINE_VISIBILITY bool is_character_file(const path& __p) {
1714 return is_character_file(__status(__p));
1715}
1716
1717inline _LIBCPP_INLINE_VISIBILITY bool
1718is_character_file(const path& __p, error_code& __ec) noexcept {
1719 return is_character_file(__status(__p, &__ec));
1720}
1721
1722inline _LIBCPP_INLINE_VISIBILITY bool is_directory(file_status __s) noexcept {
1723 return __s.type() == file_type::directory;
1724}
1725
1726inline _LIBCPP_INLINE_VISIBILITY bool is_directory(const path& __p) {
1727 return is_directory(__status(__p));
1728}
1729
1730inline _LIBCPP_INLINE_VISIBILITY bool is_directory(const path& __p,
1731 error_code& __ec) noexcept {
1732 return is_directory(__status(__p, &__ec));
1733}
1734
1735inline _LIBCPP_INLINE_VISIBILITY bool is_empty(const path& __p) {
1736 return __fs_is_empty(__p);
1737}
1738
1739inline _LIBCPP_INLINE_VISIBILITY bool is_empty(const path& __p,
1740 error_code& __ec) {
1741 return __fs_is_empty(__p, &__ec);
1742}
1743
1744inline _LIBCPP_INLINE_VISIBILITY bool is_fifo(file_status __s) noexcept {
1745 return __s.type() == file_type::fifo;
1746}
1747inline _LIBCPP_INLINE_VISIBILITY bool is_fifo(const path& __p) {
1748 return is_fifo(__status(__p));
1749}
1750
1751inline _LIBCPP_INLINE_VISIBILITY bool is_fifo(const path& __p,
1752 error_code& __ec) noexcept {
1753 return is_fifo(__status(__p, &__ec));
1754}
1755
1756inline _LIBCPP_INLINE_VISIBILITY bool
1757is_regular_file(file_status __s) noexcept {
1758 return __s.type() == file_type::regular;
1759}
1760
1761inline _LIBCPP_INLINE_VISIBILITY bool is_regular_file(const path& __p) {
1762 return is_regular_file(__status(__p));
1763}
1764
1765inline _LIBCPP_INLINE_VISIBILITY bool
1766is_regular_file(const path& __p, error_code& __ec) noexcept {
1767 return is_regular_file(__status(__p, &__ec));
1768}
1769
1770inline _LIBCPP_INLINE_VISIBILITY bool is_socket(file_status __s) noexcept {
1771 return __s.type() == file_type::socket;
1772}
1773
1774inline _LIBCPP_INLINE_VISIBILITY bool is_socket(const path& __p) {
1775 return is_socket(__status(__p));
1776}
1777
1778inline _LIBCPP_INLINE_VISIBILITY bool is_socket(const path& __p,
1779 error_code& __ec) noexcept {
1780 return is_socket(__status(__p, &__ec));
1781}
1782
1783inline _LIBCPP_INLINE_VISIBILITY bool is_symlink(file_status __s) noexcept {
1784 return __s.type() == file_type::symlink;
1785}
1786
1787inline _LIBCPP_INLINE_VISIBILITY bool is_symlink(const path& __p) {
1788 return is_symlink(__symlink_status(__p));
1789}
1790
1791inline _LIBCPP_INLINE_VISIBILITY bool is_symlink(const path& __p,
1792 error_code& __ec) noexcept {
1793 return is_symlink(__symlink_status(__p, &__ec));
1794}
1795
1796inline _LIBCPP_INLINE_VISIBILITY bool is_other(file_status __s) noexcept {
1797 return exists(__s) && !is_regular_file(__s) && !is_directory(__s) &&
1798 !is_symlink(__s);
1799}
1800
1801inline _LIBCPP_INLINE_VISIBILITY bool is_other(const path& __p) {
1802 return is_other(__status(__p));
1803}
1804
1805inline _LIBCPP_INLINE_VISIBILITY bool is_other(const path& __p,
1806 error_code& __ec) noexcept {
1807 return is_other(__status(__p, &__ec));
1808}
1809
1810inline _LIBCPP_INLINE_VISIBILITY file_time_type
1811last_write_time(const path& __p) {
1812 return __last_write_time(__p);
1813}
1814
1815inline _LIBCPP_INLINE_VISIBILITY file_time_type
1816last_write_time(const path& __p, error_code& __ec) noexcept {
1817 return __last_write_time(__p, &__ec);
1818}
1819
1820inline _LIBCPP_INLINE_VISIBILITY void last_write_time(const path& __p,
1821 file_time_type __t) {
1822 __last_write_time(__p, __t);
1823}
1824
1825inline _LIBCPP_INLINE_VISIBILITY void
1826last_write_time(const path& __p, file_time_type __t,
1827 error_code& __ec) noexcept {
1828 __last_write_time(__p, __t, &__ec);
1829}
1830
1831inline _LIBCPP_INLINE_VISIBILITY void
1832permissions(const path& __p, perms __prms,
1833 perm_options __opts = perm_options::replace) {
1834 __permissions(__p, __prms, __opts);
1835}
1836
1837inline _LIBCPP_INLINE_VISIBILITY void permissions(const path& __p, perms __prms,
1838 error_code& __ec) noexcept {
1839 __permissions(__p, __prms, perm_options::replace, &__ec);
1840}
1841
1842inline _LIBCPP_INLINE_VISIBILITY void permissions(const path& __p, perms __prms,
1843 perm_options __opts,
1844 error_code& __ec) {
1845 __permissions(__p, __prms, __opts, &__ec);
1846}
1847
1848inline _LIBCPP_INLINE_VISIBILITY path proximate(const path& __p,
1849 const path& __base,
1850 error_code& __ec) {
1851 path __tmp = __weakly_canonical(__p, &__ec);
1852 if (__ec)
1853 return {};
1854 path __tmp_base = __weakly_canonical(__base, &__ec);
1855 if (__ec)
1856 return {};
1857 return __tmp.lexically_proximate(__tmp_base);
1858}
1859
1860inline _LIBCPP_INLINE_VISIBILITY path proximate(const path& __p,
1861 error_code& __ec) {
1862 return proximate(__p, current_path(), __ec);
1863}
1864
1865inline _LIBCPP_INLINE_VISIBILITY path
1866proximate(const path& __p, const path& __base = current_path()) {
1867 return __weakly_canonical(__p).lexically_proximate(
1868 __weakly_canonical(__base));
1869}
1870
1871inline _LIBCPP_INLINE_VISIBILITY path read_symlink(const path& __p) {
1872 return __read_symlink(__p);
1873}
1874
1875inline _LIBCPP_INLINE_VISIBILITY path read_symlink(const path& __p,
1876 error_code& __ec) {
1877 return __read_symlink(__p, &__ec);
1878}
1879
1880inline _LIBCPP_INLINE_VISIBILITY path relative(const path& __p,
1881 const path& __base,
1882 error_code& __ec) {
1883 path __tmp = __weakly_canonical(__p, &__ec);
1884 if (__ec)
1885 return path();
1886 path __tmpbase = __weakly_canonical(__base, &__ec);
1887 if (__ec)
1888 return path();
1889 return __tmp.lexically_relative(__tmpbase);
1890}
1891
1892inline _LIBCPP_INLINE_VISIBILITY path relative(const path& __p,
1893 error_code& __ec) {
1894 return relative(__p, current_path(), __ec);
1895}
1896
1897inline _LIBCPP_INLINE_VISIBILITY path
1898relative(const path& __p, const path& __base = current_path()) {
1899 return __weakly_canonical(__p).lexically_relative(__weakly_canonical(__base));
1900}
1901
1902inline _LIBCPP_INLINE_VISIBILITY bool remove(const path& __p) {
1903 return __remove(__p);
1904}
1905
1906inline _LIBCPP_INLINE_VISIBILITY bool remove(const path& __p,
1907 error_code& __ec) noexcept {
1908 return __remove(__p, &__ec);
1909}
1910
1911inline _LIBCPP_INLINE_VISIBILITY uintmax_t remove_all(const path& __p) {
1912 return __remove_all(__p);
1913}
1914
1915inline _LIBCPP_INLINE_VISIBILITY uintmax_t remove_all(const path& __p,
1916 error_code& __ec) {
1917 return __remove_all(__p, &__ec);
1918}
1919
1920inline _LIBCPP_INLINE_VISIBILITY void rename(const path& __from,
1921 const path& __to) {
1922 return __rename(__from, __to);
1923}
1924
1925inline _LIBCPP_INLINE_VISIBILITY void
1926rename(const path& __from, const path& __to, error_code& __ec) noexcept {
1927 return __rename(__from, __to, &__ec);
1928}
1929
1930inline _LIBCPP_INLINE_VISIBILITY void resize_file(const path& __p,
1931 uintmax_t __ns) {
1932 return __resize_file(__p, __ns);
1933}
1934
1935inline _LIBCPP_INLINE_VISIBILITY void
1936resize_file(const path& __p, uintmax_t __ns, error_code& __ec) noexcept {
1937 return __resize_file(__p, __ns, &__ec);
1938}
1939
1940inline _LIBCPP_INLINE_VISIBILITY space_info space(const path& __p) {
1941 return __space(__p);
1942}
1943
1944inline _LIBCPP_INLINE_VISIBILITY space_info space(const path& __p,
1945 error_code& __ec) noexcept {
1946 return __space(__p, &__ec);
1947}
1948
1949inline _LIBCPP_INLINE_VISIBILITY file_status status(const path& __p) {
1950 return __status(__p);
1951}
1952
1953inline _LIBCPP_INLINE_VISIBILITY file_status status(const path& __p,
1954 error_code& __ec) noexcept {
1955 return __status(__p, &__ec);
1956}
1957
1958inline _LIBCPP_INLINE_VISIBILITY file_status symlink_status(const path& __p) {
1959 return __symlink_status(__p);
1960}
1961
1962inline _LIBCPP_INLINE_VISIBILITY file_status
1963symlink_status(const path& __p, error_code& __ec) noexcept {
1964 return __symlink_status(__p, &__ec);
1965}
1966
1967inline _LIBCPP_INLINE_VISIBILITY path temp_directory_path() {
1968 return __temp_directory_path();
1969}
1970
1971inline _LIBCPP_INLINE_VISIBILITY path temp_directory_path(error_code& __ec) {
1972 return __temp_directory_path(&__ec);
1973}
1974
1975inline _LIBCPP_INLINE_VISIBILITY path weakly_canonical(path const& __p) {
1976 return __weakly_canonical(__p);
1977}
1978
1979inline _LIBCPP_INLINE_VISIBILITY path weakly_canonical(path const& __p,
1980 error_code& __ec) {
1981 return __weakly_canonical(__p, &__ec);
1982}
1983
1984class directory_iterator;
1985class recursive_directory_iterator;
Louis Dionne48ae8892019-03-19 17:47:53 +00001986class _LIBCPP_HIDDEN __dir_stream;
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001987
1988class directory_entry {
1989 typedef _VSTD_FS::path _Path;
1990
1991public:
1992 // constructors and destructors
1993 directory_entry() noexcept = default;
1994 directory_entry(directory_entry const&) = default;
1995 directory_entry(directory_entry&&) noexcept = default;
1996
1997 _LIBCPP_INLINE_VISIBILITY
1998 explicit directory_entry(_Path const& __p) : __p_(__p) {
1999 error_code __ec;
2000 __refresh(&__ec);
2001 }
2002
2003 _LIBCPP_INLINE_VISIBILITY
2004 directory_entry(_Path const& __p, error_code& __ec) : __p_(__p) {
2005 __refresh(&__ec);
2006 }
2007
2008 ~directory_entry() {}
2009
2010 directory_entry& operator=(directory_entry const&) = default;
2011 directory_entry& operator=(directory_entry&&) noexcept = default;
2012
2013 _LIBCPP_INLINE_VISIBILITY
2014 void assign(_Path const& __p) {
2015 __p_ = __p;
2016 error_code __ec;
2017 __refresh(&__ec);
2018 }
2019
2020 _LIBCPP_INLINE_VISIBILITY
2021 void assign(_Path const& __p, error_code& __ec) {
2022 __p_ = __p;
2023 __refresh(&__ec);
2024 }
2025
2026 _LIBCPP_INLINE_VISIBILITY
2027 void replace_filename(_Path const& __p) {
2028 __p_.replace_filename(__p);
2029 error_code __ec;
2030 __refresh(&__ec);
2031 }
2032
2033 _LIBCPP_INLINE_VISIBILITY
2034 void replace_filename(_Path const& __p, error_code& __ec) {
2035 __p_ = __p_.parent_path() / __p;
2036 __refresh(&__ec);
2037 }
2038
2039 _LIBCPP_INLINE_VISIBILITY
2040 void refresh() { __refresh(); }
2041
2042 _LIBCPP_INLINE_VISIBILITY
2043 void refresh(error_code& __ec) noexcept { __refresh(&__ec); }
2044
2045 _LIBCPP_INLINE_VISIBILITY
2046 _Path const& path() const noexcept { return __p_; }
2047
2048 _LIBCPP_INLINE_VISIBILITY
2049 operator const _Path&() const noexcept { return __p_; }
2050
2051 _LIBCPP_INLINE_VISIBILITY
2052 bool exists() const { return _VSTD_FS::exists(file_status{__get_ft()}); }
2053
2054 _LIBCPP_INLINE_VISIBILITY
2055 bool exists(error_code& __ec) const noexcept {
2056 return _VSTD_FS::exists(file_status{__get_ft(&__ec)});
2057 }
2058
2059 _LIBCPP_INLINE_VISIBILITY
2060 bool is_block_file() const { return __get_ft() == file_type::block; }
2061
2062 _LIBCPP_INLINE_VISIBILITY
2063 bool is_block_file(error_code& __ec) const noexcept {
2064 return __get_ft(&__ec) == file_type::block;
2065 }
2066
2067 _LIBCPP_INLINE_VISIBILITY
2068 bool is_character_file() const { return __get_ft() == file_type::character; }
2069
2070 _LIBCPP_INLINE_VISIBILITY
2071 bool is_character_file(error_code& __ec) const noexcept {
2072 return __get_ft(&__ec) == file_type::character;
2073 }
2074
2075 _LIBCPP_INLINE_VISIBILITY
2076 bool is_directory() const { return __get_ft() == file_type::directory; }
2077
2078 _LIBCPP_INLINE_VISIBILITY
2079 bool is_directory(error_code& __ec) const noexcept {
2080 return __get_ft(&__ec) == file_type::directory;
2081 }
2082
2083 _LIBCPP_INLINE_VISIBILITY
2084 bool is_fifo() const { return __get_ft() == file_type::fifo; }
2085
2086 _LIBCPP_INLINE_VISIBILITY
2087 bool is_fifo(error_code& __ec) const noexcept {
2088 return __get_ft(&__ec) == file_type::fifo;
2089 }
2090
2091 _LIBCPP_INLINE_VISIBILITY
2092 bool is_other() const { return _VSTD_FS::is_other(file_status{__get_ft()}); }
2093
2094 _LIBCPP_INLINE_VISIBILITY
2095 bool is_other(error_code& __ec) const noexcept {
2096 return _VSTD_FS::is_other(file_status{__get_ft(&__ec)});
2097 }
2098
2099 _LIBCPP_INLINE_VISIBILITY
2100 bool is_regular_file() const { return __get_ft() == file_type::regular; }
2101
2102 _LIBCPP_INLINE_VISIBILITY
2103 bool is_regular_file(error_code& __ec) const noexcept {
2104 return __get_ft(&__ec) == file_type::regular;
2105 }
2106
2107 _LIBCPP_INLINE_VISIBILITY
2108 bool is_socket() const { return __get_ft() == file_type::socket; }
2109
2110 _LIBCPP_INLINE_VISIBILITY
2111 bool is_socket(error_code& __ec) const noexcept {
2112 return __get_ft(&__ec) == file_type::socket;
2113 }
2114
2115 _LIBCPP_INLINE_VISIBILITY
2116 bool is_symlink() const { return __get_sym_ft() == file_type::symlink; }
2117
2118 _LIBCPP_INLINE_VISIBILITY
2119 bool is_symlink(error_code& __ec) const noexcept {
2120 return __get_sym_ft(&__ec) == file_type::symlink;
2121 }
2122 _LIBCPP_INLINE_VISIBILITY
2123 uintmax_t file_size() const { return __get_size(); }
2124
2125 _LIBCPP_INLINE_VISIBILITY
2126 uintmax_t file_size(error_code& __ec) const noexcept {
2127 return __get_size(&__ec);
2128 }
2129
2130 _LIBCPP_INLINE_VISIBILITY
2131 uintmax_t hard_link_count() const { return __get_nlink(); }
2132
2133 _LIBCPP_INLINE_VISIBILITY
2134 uintmax_t hard_link_count(error_code& __ec) const noexcept {
2135 return __get_nlink(&__ec);
2136 }
2137
2138 _LIBCPP_INLINE_VISIBILITY
2139 file_time_type last_write_time() const { return __get_write_time(); }
2140
2141 _LIBCPP_INLINE_VISIBILITY
2142 file_time_type last_write_time(error_code& __ec) const noexcept {
2143 return __get_write_time(&__ec);
2144 }
2145
2146 _LIBCPP_INLINE_VISIBILITY
2147 file_status status() const { return __get_status(); }
2148
2149 _LIBCPP_INLINE_VISIBILITY
2150 file_status status(error_code& __ec) const noexcept {
2151 return __get_status(&__ec);
2152 }
2153
2154 _LIBCPP_INLINE_VISIBILITY
2155 file_status symlink_status() const { return __get_symlink_status(); }
2156
2157 _LIBCPP_INLINE_VISIBILITY
2158 file_status symlink_status(error_code& __ec) const noexcept {
2159 return __get_symlink_status(&__ec);
2160 }
2161
2162 _LIBCPP_INLINE_VISIBILITY
2163 bool operator<(directory_entry const& __rhs) const noexcept {
2164 return __p_ < __rhs.__p_;
2165 }
2166
2167 _LIBCPP_INLINE_VISIBILITY
2168 bool operator==(directory_entry const& __rhs) const noexcept {
2169 return __p_ == __rhs.__p_;
2170 }
2171
2172 _LIBCPP_INLINE_VISIBILITY
2173 bool operator!=(directory_entry const& __rhs) const noexcept {
2174 return __p_ != __rhs.__p_;
2175 }
2176
2177 _LIBCPP_INLINE_VISIBILITY
2178 bool operator<=(directory_entry const& __rhs) const noexcept {
2179 return __p_ <= __rhs.__p_;
2180 }
2181
2182 _LIBCPP_INLINE_VISIBILITY
2183 bool operator>(directory_entry const& __rhs) const noexcept {
2184 return __p_ > __rhs.__p_;
2185 }
2186
2187 _LIBCPP_INLINE_VISIBILITY
2188 bool operator>=(directory_entry const& __rhs) const noexcept {
2189 return __p_ >= __rhs.__p_;
2190 }
2191
2192private:
2193 friend class directory_iterator;
2194 friend class recursive_directory_iterator;
2195 friend class __dir_stream;
2196
2197 enum _CacheType : unsigned char {
2198 _Empty,
2199 _IterSymlink,
2200 _IterNonSymlink,
2201 _RefreshSymlink,
2202 _RefreshSymlinkUnresolved,
2203 _RefreshNonSymlink
2204 };
2205
2206 struct __cached_data {
2207 uintmax_t __size_;
2208 uintmax_t __nlink_;
2209 file_time_type __write_time_;
2210 perms __sym_perms_;
2211 perms __non_sym_perms_;
2212 file_type __type_;
2213 _CacheType __cache_type_;
2214
2215 _LIBCPP_INLINE_VISIBILITY
2216 __cached_data() noexcept { __reset(); }
2217
2218 _LIBCPP_INLINE_VISIBILITY
2219 void __reset() {
2220 __cache_type_ = _Empty;
2221 __type_ = file_type::none;
2222 __sym_perms_ = __non_sym_perms_ = perms::unknown;
2223 __size_ = __nlink_ = uintmax_t(-1);
2224 __write_time_ = file_time_type::min();
2225 }
2226 };
2227
2228 _LIBCPP_INLINE_VISIBILITY
2229 static __cached_data __create_iter_result(file_type __ft) {
2230 __cached_data __data;
2231 __data.__type_ = __ft;
2232 __data.__cache_type_ = [&]() {
2233 switch (__ft) {
2234 case file_type::none:
2235 return _Empty;
2236 case file_type::symlink:
2237 return _IterSymlink;
2238 default:
2239 return _IterNonSymlink;
2240 }
2241 }();
2242 return __data;
2243 }
2244
2245 _LIBCPP_INLINE_VISIBILITY
2246 void __assign_iter_entry(_Path&& __p, __cached_data __dt) {
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002247 __p_ = _VSTD::move(__p);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00002248 __data_ = __dt;
2249 }
2250
2251 _LIBCPP_FUNC_VIS
2252 error_code __do_refresh() noexcept;
2253
2254 _LIBCPP_INLINE_VISIBILITY
2255 static bool __is_dne_error(error_code const& __ec) {
2256 if (!__ec)
2257 return true;
2258 switch (static_cast<errc>(__ec.value())) {
2259 case errc::no_such_file_or_directory:
2260 case errc::not_a_directory:
2261 return true;
2262 default:
2263 return false;
2264 }
2265 }
2266
2267 _LIBCPP_INLINE_VISIBILITY
2268 void __handle_error(const char* __msg, error_code* __dest_ec,
2269 error_code const& __ec, bool __allow_dne = false) const {
2270 if (__dest_ec) {
2271 *__dest_ec = __ec;
2272 return;
2273 }
2274 if (__ec && (!__allow_dne || !__is_dne_error(__ec)))
2275 __throw_filesystem_error(__msg, __p_, __ec);
2276 }
2277
2278 _LIBCPP_INLINE_VISIBILITY
2279 void __refresh(error_code* __ec = nullptr) {
2280 __handle_error("in directory_entry::refresh", __ec, __do_refresh(),
2281 /*allow_dne*/ true);
2282 }
2283
2284 _LIBCPP_INLINE_VISIBILITY
2285 file_type __get_sym_ft(error_code* __ec = nullptr) const {
2286 switch (__data_.__cache_type_) {
2287 case _Empty:
2288 return __symlink_status(__p_, __ec).type();
2289 case _IterSymlink:
2290 case _RefreshSymlink:
2291 case _RefreshSymlinkUnresolved:
2292 if (__ec)
2293 __ec->clear();
2294 return file_type::symlink;
2295 case _IterNonSymlink:
2296 case _RefreshNonSymlink:
2297 file_status __st(__data_.__type_);
2298 if (__ec && !_VSTD_FS::exists(__st))
2299 *__ec = make_error_code(errc::no_such_file_or_directory);
2300 else if (__ec)
2301 __ec->clear();
2302 return __data_.__type_;
2303 }
2304 _LIBCPP_UNREACHABLE();
2305 }
2306
2307 _LIBCPP_INLINE_VISIBILITY
2308 file_type __get_ft(error_code* __ec = nullptr) const {
2309 switch (__data_.__cache_type_) {
2310 case _Empty:
2311 case _IterSymlink:
2312 case _RefreshSymlinkUnresolved:
2313 return __status(__p_, __ec).type();
2314 case _IterNonSymlink:
2315 case _RefreshNonSymlink:
2316 case _RefreshSymlink: {
2317 file_status __st(__data_.__type_);
2318 if (__ec && !_VSTD_FS::exists(__st))
2319 *__ec = make_error_code(errc::no_such_file_or_directory);
2320 else if (__ec)
2321 __ec->clear();
2322 return __data_.__type_;
2323 }
2324 }
2325 _LIBCPP_UNREACHABLE();
2326 }
2327
2328 _LIBCPP_INLINE_VISIBILITY
2329 file_status __get_status(error_code* __ec = nullptr) const {
2330 switch (__data_.__cache_type_) {
2331 case _Empty:
2332 case _IterNonSymlink:
2333 case _IterSymlink:
2334 case _RefreshSymlinkUnresolved:
2335 return __status(__p_, __ec);
2336 case _RefreshNonSymlink:
2337 case _RefreshSymlink:
2338 return file_status(__get_ft(__ec), __data_.__non_sym_perms_);
2339 }
2340 _LIBCPP_UNREACHABLE();
2341 }
2342
2343 _LIBCPP_INLINE_VISIBILITY
2344 file_status __get_symlink_status(error_code* __ec = nullptr) const {
2345 switch (__data_.__cache_type_) {
2346 case _Empty:
2347 case _IterNonSymlink:
2348 case _IterSymlink:
2349 return __symlink_status(__p_, __ec);
2350 case _RefreshNonSymlink:
2351 return file_status(__get_sym_ft(__ec), __data_.__non_sym_perms_);
2352 case _RefreshSymlink:
2353 case _RefreshSymlinkUnresolved:
2354 return file_status(__get_sym_ft(__ec), __data_.__sym_perms_);
2355 }
2356 _LIBCPP_UNREACHABLE();
2357 }
2358
2359 _LIBCPP_INLINE_VISIBILITY
2360 uintmax_t __get_size(error_code* __ec = nullptr) const {
2361 switch (__data_.__cache_type_) {
2362 case _Empty:
2363 case _IterNonSymlink:
2364 case _IterSymlink:
2365 case _RefreshSymlinkUnresolved:
2366 return _VSTD_FS::__file_size(__p_, __ec);
2367 case _RefreshSymlink:
2368 case _RefreshNonSymlink: {
2369 error_code __m_ec;
2370 file_status __st(__get_ft(&__m_ec));
2371 __handle_error("in directory_entry::file_size", __ec, __m_ec);
2372 if (_VSTD_FS::exists(__st) && !_VSTD_FS::is_regular_file(__st)) {
2373 errc __err_kind = _VSTD_FS::is_directory(__st) ? errc::is_a_directory
2374 : errc::not_supported;
2375 __handle_error("in directory_entry::file_size", __ec,
2376 make_error_code(__err_kind));
2377 }
2378 return __data_.__size_;
2379 }
2380 }
2381 _LIBCPP_UNREACHABLE();
2382 }
2383
2384 _LIBCPP_INLINE_VISIBILITY
2385 uintmax_t __get_nlink(error_code* __ec = nullptr) const {
2386 switch (__data_.__cache_type_) {
2387 case _Empty:
2388 case _IterNonSymlink:
2389 case _IterSymlink:
2390 case _RefreshSymlinkUnresolved:
2391 return _VSTD_FS::__hard_link_count(__p_, __ec);
2392 case _RefreshSymlink:
2393 case _RefreshNonSymlink: {
2394 error_code __m_ec;
2395 (void)__get_ft(&__m_ec);
2396 __handle_error("in directory_entry::hard_link_count", __ec, __m_ec);
2397 return __data_.__nlink_;
2398 }
2399 }
2400 _LIBCPP_UNREACHABLE();
2401 }
2402
2403 _LIBCPP_INLINE_VISIBILITY
2404 file_time_type __get_write_time(error_code* __ec = nullptr) const {
2405 switch (__data_.__cache_type_) {
2406 case _Empty:
2407 case _IterNonSymlink:
2408 case _IterSymlink:
2409 case _RefreshSymlinkUnresolved:
2410 return _VSTD_FS::__last_write_time(__p_, __ec);
2411 case _RefreshSymlink:
2412 case _RefreshNonSymlink: {
2413 error_code __m_ec;
2414 file_status __st(__get_ft(&__m_ec));
2415 __handle_error("in directory_entry::last_write_time", __ec, __m_ec);
2416 if (_VSTD_FS::exists(__st) &&
2417 __data_.__write_time_ == file_time_type::min())
2418 __handle_error("in directory_entry::last_write_time", __ec,
2419 make_error_code(errc::value_too_large));
2420 return __data_.__write_time_;
2421 }
2422 }
2423 _LIBCPP_UNREACHABLE();
2424 }
2425
2426private:
2427 _Path __p_;
2428 __cached_data __data_;
2429};
2430
2431class __dir_element_proxy {
2432public:
2433 inline _LIBCPP_INLINE_VISIBILITY directory_entry operator*() {
2434 return _VSTD::move(__elem_);
2435 }
2436
2437private:
2438 friend class directory_iterator;
2439 friend class recursive_directory_iterator;
2440 explicit __dir_element_proxy(directory_entry const& __e) : __elem_(__e) {}
2441 __dir_element_proxy(__dir_element_proxy&& __o)
2442 : __elem_(_VSTD::move(__o.__elem_)) {}
2443 directory_entry __elem_;
2444};
2445
2446class directory_iterator {
2447public:
2448 typedef directory_entry value_type;
2449 typedef ptrdiff_t difference_type;
2450 typedef value_type const* pointer;
2451 typedef value_type const& reference;
2452 typedef input_iterator_tag iterator_category;
2453
2454public:
2455 //ctor & dtor
2456 directory_iterator() noexcept {}
2457
2458 explicit directory_iterator(const path& __p)
2459 : directory_iterator(__p, nullptr) {}
2460
2461 directory_iterator(const path& __p, directory_options __opts)
2462 : directory_iterator(__p, nullptr, __opts) {}
2463
2464 directory_iterator(const path& __p, error_code& __ec)
2465 : directory_iterator(__p, &__ec) {}
2466
2467 directory_iterator(const path& __p, directory_options __opts,
2468 error_code& __ec)
2469 : directory_iterator(__p, &__ec, __opts) {}
2470
2471 directory_iterator(const directory_iterator&) = default;
2472 directory_iterator(directory_iterator&&) = default;
2473 directory_iterator& operator=(const directory_iterator&) = default;
2474
2475 directory_iterator& operator=(directory_iterator&& __o) noexcept {
2476 // non-default implementation provided to support self-move assign.
2477 if (this != &__o) {
2478 __imp_ = _VSTD::move(__o.__imp_);
2479 }
2480 return *this;
2481 }
2482
2483 ~directory_iterator() = default;
2484
2485 const directory_entry& operator*() const {
2486 _LIBCPP_ASSERT(__imp_, "The end iterator cannot be dereferenced");
2487 return __dereference();
2488 }
2489
2490 const directory_entry* operator->() const { return &**this; }
2491
2492 directory_iterator& operator++() { return __increment(); }
2493
2494 __dir_element_proxy operator++(int) {
2495 __dir_element_proxy __p(**this);
2496 __increment();
2497 return __p;
2498 }
2499
2500 directory_iterator& increment(error_code& __ec) { return __increment(&__ec); }
2501
2502private:
2503 inline _LIBCPP_INLINE_VISIBILITY friend bool
2504 operator==(const directory_iterator& __lhs,
2505 const directory_iterator& __rhs) noexcept;
2506
2507 // construct the dir_stream
2508 _LIBCPP_FUNC_VIS
2509 directory_iterator(const path&, error_code*,
2510 directory_options = directory_options::none);
2511
2512 _LIBCPP_FUNC_VIS
2513 directory_iterator& __increment(error_code* __ec = nullptr);
2514
2515 _LIBCPP_FUNC_VIS
2516 const directory_entry& __dereference() const;
2517
2518private:
2519 shared_ptr<__dir_stream> __imp_;
2520};
2521
2522inline _LIBCPP_INLINE_VISIBILITY bool
2523operator==(const directory_iterator& __lhs,
2524 const directory_iterator& __rhs) noexcept {
2525 return __lhs.__imp_ == __rhs.__imp_;
2526}
2527
2528inline _LIBCPP_INLINE_VISIBILITY bool
2529operator!=(const directory_iterator& __lhs,
2530 const directory_iterator& __rhs) noexcept {
2531 return !(__lhs == __rhs);
2532}
2533
2534// enable directory_iterator range-based for statements
2535inline _LIBCPP_INLINE_VISIBILITY directory_iterator
2536begin(directory_iterator __iter) noexcept {
2537 return __iter;
2538}
2539
2540inline _LIBCPP_INLINE_VISIBILITY directory_iterator
2541end(const directory_iterator&) noexcept {
2542 return directory_iterator();
2543}
2544
2545class recursive_directory_iterator {
2546public:
2547 using value_type = directory_entry;
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002548 using difference_type = ptrdiff_t;
Eric Fiselier02cea5e2018-07-27 03:07:09 +00002549 using pointer = directory_entry const*;
2550 using reference = directory_entry const&;
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05002551 using iterator_category = input_iterator_tag;
Eric Fiselier02cea5e2018-07-27 03:07:09 +00002552
2553public:
2554 // constructors and destructor
2555 _LIBCPP_INLINE_VISIBILITY
2556 recursive_directory_iterator() noexcept : __rec_(false) {}
2557
2558 _LIBCPP_INLINE_VISIBILITY
2559 explicit recursive_directory_iterator(
2560 const path& __p, directory_options __xoptions = directory_options::none)
2561 : recursive_directory_iterator(__p, __xoptions, nullptr) {}
2562
2563 _LIBCPP_INLINE_VISIBILITY
2564 recursive_directory_iterator(const path& __p, directory_options __xoptions,
2565 error_code& __ec)
2566 : recursive_directory_iterator(__p, __xoptions, &__ec) {}
2567
2568 _LIBCPP_INLINE_VISIBILITY
2569 recursive_directory_iterator(const path& __p, error_code& __ec)
2570 : recursive_directory_iterator(__p, directory_options::none, &__ec) {}
2571
2572 recursive_directory_iterator(const recursive_directory_iterator&) = default;
2573 recursive_directory_iterator(recursive_directory_iterator&&) = default;
2574
2575 recursive_directory_iterator&
2576 operator=(const recursive_directory_iterator&) = default;
2577
2578 _LIBCPP_INLINE_VISIBILITY
2579 recursive_directory_iterator&
2580 operator=(recursive_directory_iterator&& __o) noexcept {
2581 // non-default implementation provided to support self-move assign.
2582 if (this != &__o) {
2583 __imp_ = _VSTD::move(__o.__imp_);
2584 __rec_ = __o.__rec_;
2585 }
2586 return *this;
2587 }
2588
2589 ~recursive_directory_iterator() = default;
2590
2591 _LIBCPP_INLINE_VISIBILITY
2592 const directory_entry& operator*() const { return __dereference(); }
2593
2594 _LIBCPP_INLINE_VISIBILITY
2595 const directory_entry* operator->() const { return &__dereference(); }
2596
2597 recursive_directory_iterator& operator++() { return __increment(); }
2598
2599 _LIBCPP_INLINE_VISIBILITY
2600 __dir_element_proxy operator++(int) {
2601 __dir_element_proxy __p(**this);
2602 __increment();
2603 return __p;
2604 }
2605
2606 _LIBCPP_INLINE_VISIBILITY
2607 recursive_directory_iterator& increment(error_code& __ec) {
2608 return __increment(&__ec);
2609 }
2610
2611 _LIBCPP_FUNC_VIS directory_options options() const;
2612 _LIBCPP_FUNC_VIS int depth() const;
2613
2614 _LIBCPP_INLINE_VISIBILITY
2615 void pop() { __pop(); }
2616
2617 _LIBCPP_INLINE_VISIBILITY
2618 void pop(error_code& __ec) { __pop(&__ec); }
2619
2620 _LIBCPP_INLINE_VISIBILITY
2621 bool recursion_pending() const { return __rec_; }
2622
2623 _LIBCPP_INLINE_VISIBILITY
2624 void disable_recursion_pending() { __rec_ = false; }
2625
2626private:
Louis Dionne0ba10dc2019-08-13 15:02:53 +00002627 _LIBCPP_FUNC_VIS
Eric Fiselier02cea5e2018-07-27 03:07:09 +00002628 recursive_directory_iterator(const path& __p, directory_options __opt,
2629 error_code* __ec);
2630
2631 _LIBCPP_FUNC_VIS
2632 const directory_entry& __dereference() const;
2633
2634 _LIBCPP_FUNC_VIS
2635 bool __try_recursion(error_code* __ec);
2636
2637 _LIBCPP_FUNC_VIS
2638 void __advance(error_code* __ec = nullptr);
2639
2640 _LIBCPP_FUNC_VIS
2641 recursive_directory_iterator& __increment(error_code* __ec = nullptr);
2642
2643 _LIBCPP_FUNC_VIS
2644 void __pop(error_code* __ec = nullptr);
2645
2646 inline _LIBCPP_INLINE_VISIBILITY friend bool
2647 operator==(const recursive_directory_iterator&,
2648 const recursive_directory_iterator&) noexcept;
2649
Louis Dionne48ae8892019-03-19 17:47:53 +00002650 struct _LIBCPP_HIDDEN __shared_imp;
Eric Fiselier02cea5e2018-07-27 03:07:09 +00002651 shared_ptr<__shared_imp> __imp_;
2652 bool __rec_;
2653}; // class recursive_directory_iterator
2654
2655inline _LIBCPP_INLINE_VISIBILITY bool
2656operator==(const recursive_directory_iterator& __lhs,
2657 const recursive_directory_iterator& __rhs) noexcept {
2658 return __lhs.__imp_ == __rhs.__imp_;
2659}
2660
2661_LIBCPP_INLINE_VISIBILITY
2662inline bool operator!=(const recursive_directory_iterator& __lhs,
2663 const recursive_directory_iterator& __rhs) noexcept {
2664 return !(__lhs == __rhs);
2665}
2666// enable recursive_directory_iterator range-based for statements
2667inline _LIBCPP_INLINE_VISIBILITY recursive_directory_iterator
2668begin(recursive_directory_iterator __iter) noexcept {
2669 return __iter;
2670}
2671
2672inline _LIBCPP_INLINE_VISIBILITY recursive_directory_iterator
2673end(const recursive_directory_iterator&) noexcept {
2674 return recursive_directory_iterator();
2675}
2676
Louis Dionnef6bf76a2019-03-20 21:18:14 +00002677_LIBCPP_AVAILABILITY_FILESYSTEM_POP
2678
Eric Fiselier02cea5e2018-07-27 03:07:09 +00002679_LIBCPP_END_NAMESPACE_FILESYSTEM
2680
2681#endif // !_LIBCPP_CXX03_LANG
2682
2683_LIBCPP_POP_MACROS
2684
2685#endif // _LIBCPP_FILESYSTEM