Debian Codegraph @

gulrak-filesystem/include/ghc/filesystem.hpp

raw
1 //---------------------------------------------------------------------------------------
2 //
3 // ghc::filesystem - A C++17-like filesystem implementation for C++11/C++14/C++17/C++20
4 //
5 //---------------------------------------------------------------------------------------
6 //
7 // Copyright (c) 2018, Steffen Schümann <[email protected]>
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
26 //
27 //---------------------------------------------------------------------------------------
28 //
29 // To dynamically select std::filesystem where available on most platforms,
30 // you could use:
31 //
32 // #if ((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || (defined(__cplusplus) && __cplusplus >= 201703L)) && defined(__has_include)
33 // #if __has_include(<filesystem>) && (!defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500)
34 // #define GHC_USE_STD_FS
35 // #include <filesystem>
36 // namespace fs = std::filesystem;
37 // #endif
38 // #endif
39 // #ifndef GHC_USE_STD_FS
40 // #include <ghc/filesystem.hpp>
41 // namespace fs = ghc::filesystem;
42 // #endif
43 //
44 //---------------------------------------------------------------------------------------
45 #ifndef GHC_FILESYSTEM_H
46 #define GHC_FILESYSTEM_H
47
48 // #define BSD manifest constant only in
49 // sys/param.h
50 #ifndef _WIN32
51 #include <sys/param.h>
52 #endif
53
54 #ifndef GHC_OS_DETECTED
55 #if defined(__APPLE__) && defined(__MACH__)
56 #define GHC_OS_MACOS
57 #elif defined(__linux__)
58 #define GHC_OS_LINUX
59 #if defined(__ANDROID__)
60 #define GHC_OS_ANDROID
61 #endif
62 #elif defined(_WIN64)
63 #define GHC_OS_WINDOWS
64 #define GHC_OS_WIN64
65 #elif defined(_WIN32)
66 #define GHC_OS_WINDOWS
67 #define GHC_OS_WIN32
68 #elif defined(__CYGWIN__)
69 #define GHC_OS_CYGWIN
70 #elif defined(__svr4__)
71 #define GHC_OS_SYS5R4
72 #elif defined(BSD)
73 #define GHC_OS_BSD
74 #elif defined(__EMSCRIPTEN__)
75 #define GHC_OS_WEB
76 #include <wasi/api.h>
77 #elif defined(__QNX__)
78 #define GHC_OS_QNX
79 #define GHC_NO_DIRENT_D_TYPE
80 #else
81 #error "Operating system currently not supported!"
82 #endif
83 #define GHC_OS_DETECTED
84 #if (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
85 #if _MSVC_LANG == 201703L
86 #define GHC_FILESYSTEM_RUNNING_CPP17
87 #else
88 #define GHC_FILESYSTEM_RUNNING_CPP20
89 #endif
90 #elif (defined(__cplusplus) && __cplusplus >= 201703L)
91 #if __cplusplus == 201703L
92 #define GHC_FILESYSTEM_RUNNING_CPP17
93 #else
94 #define GHC_FILESYSTEM_RUNNING_CPP20
95 #endif
96 #endif
97 #endif
98
99 #if defined(GHC_FILESYSTEM_IMPLEMENTATION)
100 #define GHC_EXPAND_IMPL
101 #define GHC_INLINE
102 #ifdef GHC_OS_WINDOWS
103 #ifndef GHC_FS_API
104 #define GHC_FS_API
105 #endif
106 #ifndef GHC_FS_API_CLASS
107 #define GHC_FS_API_CLASS
108 #endif
109 #else
110 #ifndef GHC_FS_API
111 #define GHC_FS_API __attribute__((visibility("default")))
112 #endif
113 #ifndef GHC_FS_API_CLASS
114 #define GHC_FS_API_CLASS __attribute__((visibility("default")))
115 #endif
116 #endif
117 #elif defined(GHC_FILESYSTEM_FWD)
118 #define GHC_INLINE
119 #ifdef GHC_OS_WINDOWS
120 #ifndef GHC_FS_API
121 #define GHC_FS_API extern
122 #endif
123 #ifndef GHC_FS_API_CLASS
124 #define GHC_FS_API_CLASS
125 #endif
126 #else
127 #ifndef GHC_FS_API
128 #define GHC_FS_API extern
129 #endif
130 #ifndef GHC_FS_API_CLASS
131 #define GHC_FS_API_CLASS
132 #endif
133 #endif
134 #else
135 #define GHC_EXPAND_IMPL
136 #define GHC_INLINE inline
137 #ifndef GHC_FS_API
138 #define GHC_FS_API
139 #endif
140 #ifndef GHC_FS_API_CLASS
141 #define GHC_FS_API_CLASS
142 #endif
143 #endif
144
145 #ifdef GHC_EXPAND_IMPL
146
147 #ifdef GHC_OS_WINDOWS
148 #include <windows.h>
149 // additional includes
150 #include <shellapi.h>
151 #include <sys/stat.h>
152 #include <sys/types.h>
153 #include <wchar.h>
154 #include <winioctl.h>
155 #else
156 #include <dirent.h>
157 #include <fcntl.h>
158 #include <limits.h>
159 #include <sys/param.h>
160 #include <sys/stat.h>
161 #include <sys/time.h>
162 #include <sys/types.h>
163 #include <unistd.h>
164 #ifdef GHC_OS_ANDROID
165 #include <android/api-level.h>
166 #if __ANDROID_API__ < 12
167 #include <sys/syscall.h>
168 #endif
169 #include <sys/vfs.h>
170 #define statvfs statfs
171 #else
172 #include <sys/statvfs.h>
173 #endif
174 #ifdef GHC_OS_CYGWIN
175 #include <strings.h>
176 #endif
177 #if !defined(__ANDROID__) || __ANDROID_API__ >= 26
178 #include <langinfo.h>
179 #endif
180 #endif
181 #ifdef GHC_OS_MACOS
182 #include <Availability.h>
183 #endif
184
185 #if defined(__cpp_impl_three_way_comparison) && defined(__has_include)
186 #if __has_include(<compare>)
187 #define GHC_HAS_THREEWAY_COMP
188 #include <compare>
189 #endif
190 #endif
191
192 #include <algorithm>
193 #include <cctype>
194 #include <chrono>
195 #include <clocale>
196 #include <cstdlib>
197 #include <cstring>
198 #include <fstream>
199 #include <functional>
200 #include <memory>
201 #include <stack>
202 #include <stdexcept>
203 #include <string>
204 #include <system_error>
205 #include <type_traits>
206 #include <utility>
207 #include <vector>
208
209 #else // GHC_EXPAND_IMPL
210
211 #if defined(__cpp_impl_three_way_comparison) && defined(__has_include)
212 #if __has_include(<compare>)
213 #define GHC_HAS_THREEWAY_COMP
214 #include <compare>
215 #endif
216 #endif
217 #include <chrono>
218 #include <fstream>
219 #include <memory>
220 #include <stack>
221 #include <stdexcept>
222 #include <string>
223 #include <system_error>
224 #ifdef GHC_OS_WINDOWS
225 #include <vector>
226 #endif
227 #endif // GHC_EXPAND_IMPL
228
229 // After standard library includes.
230 // Standard library support for std::string_view.
231 #if defined(__cpp_lib_string_view)
232 #define GHC_HAS_STD_STRING_VIEW
233 #elif defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 4000) && (__cplusplus >= 201402)
234 #define GHC_HAS_STD_STRING_VIEW
235 #elif defined(_GLIBCXX_RELEASE) && (_GLIBCXX_RELEASE >= 7) && (__cplusplus >= 201703)
236 #define GHC_HAS_STD_STRING_VIEW
237 #elif defined(_MSC_VER) && (_MSC_VER >= 1910 && _MSVC_LANG >= 201703)
238 #define GHC_HAS_STD_STRING_VIEW
239 #endif
240
241 // Standard library support for std::experimental::string_view.
242 #if defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 3700 && _LIBCPP_VERSION < 7000) && (__cplusplus >= 201402)
243 #define GHC_HAS_STD_EXPERIMENTAL_STRING_VIEW
244 #elif defined(__GNUC__) && (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 9)) || (__GNUC__ > 4)) && (__cplusplus >= 201402)
245 #define GHC_HAS_STD_EXPERIMENTAL_STRING_VIEW
246 #elif defined(__GLIBCXX__) && defined(_GLIBCXX_USE_DUAL_ABI) && (__cplusplus >= 201402)
247 // macro _GLIBCXX_USE_DUAL_ABI is always defined in libstdc++ from gcc-5 and newer
248 #define GHC_HAS_STD_EXPERIMENTAL_STRING_VIEW
249 #endif
250
251 #if defined(GHC_HAS_STD_STRING_VIEW)
252 #include <string_view>
253 #elif defined(GHC_HAS_STD_EXPERIMENTAL_STRING_VIEW)
254 #include <experimental/string_view>
255 #endif
256
257 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
258 // Behaviour Switches (see README.md, should match the config in test/filesystem_test.cpp):
259 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
260 // Enforce C++17 API where possible when compiling for C++20, handles the following cases:
261 // * fs::path::u8string() returns std::string instead of std::u8string
262 // #define GHC_FILESYSTEM_ENFORCE_CPP17_API
263 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
264 // LWG #2682 disables the since then invalid use of the copy option create_symlinks on directories
265 // configure LWG conformance ()
266 #define LWG_2682_BEHAVIOUR
267 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
268 // LWG #2395 makes crate_directory/create_directories not emit an error if there is a regular
269 // file with that name, it is superseded by P1164R1, so only activate if really needed
270 // #define LWG_2935_BEHAVIOUR
271 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
272 // LWG #2936 enables new element wise (more expensive) path comparison
273 // * if this->root_name().native().compare(p.root_name().native()) != 0 return result
274 // * if this->has_root_directory() and !p.has_root_directory() return -1
275 // * if !this->has_root_directory() and p.has_root_directory() return -1
276 // * else result of element wise comparison of path iteration where first comparison is != 0 or 0
277 // if all comparisons are 0 (on Windows this implementation does case insensitive root_name()
278 // comparison)
279 #define LWG_2936_BEHAVIOUR
280 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
281 // LWG #2937 enforces that fs::equivalent emits an error, if !fs::exists(p1)||!exists(p2)
282 #define LWG_2937_BEHAVIOUR
283 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
284 // UTF8-Everywhere is the original behaviour of ghc::filesystem. But since v1.5 the windows
285 // version defaults to std::wstring storage backend. Still all std::string will be interpreted
286 // as UTF-8 encoded. With this define you can enfoce the old behavior on Windows, using
287 // std::string as backend and for fs::path::native() and char for fs::path::c_str(). This
288 // needs more conversions so it is (an was before v1.5) slower, bot might help keeping source
289 // homogeneous in a multi platform project.
290 // #define GHC_WIN_DISABLE_WSTRING_STORAGE_TYPE
291 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
292 // Raise errors/exceptions when invalid unicode codepoints or UTF-8 sequences are found,
293 // instead of replacing them with the unicode replacement character (U+FFFD).
294 // #define GHC_RAISE_UNICODE_ERRORS
295 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
296 // Automatic prefix windows path with "\\?\" if they would break the MAX_PATH length.
297 // instead of replacing them with the unicode replacement character (U+FFFD).
298 #ifndef GHC_WIN_DISABLE_AUTO_PREFIXES
299 #define GHC_WIN_AUTO_PREFIX_LONG_PATH
300 #endif // GHC_WIN_DISABLE_AUTO_PREFIXES
301 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
302
303 // ghc::filesystem version in decimal (major * 10000 + minor * 100 + patch)
304 #define GHC_FILESYSTEM_VERSION 10510L
305
306 #if !defined(GHC_WITH_EXCEPTIONS) && (defined(__EXCEPTIONS) || defined(__cpp_exceptions) || defined(_CPPUNWIND))
307 #define GHC_WITH_EXCEPTIONS
308 #endif
309 #if !defined(GHC_WITH_EXCEPTIONS) && defined(GHC_RAISE_UNICODE_ERRORS)
310 #error "Can't raise unicode errors with exception support disabled"
311 #endif
312
313 namespace ghc {
314 namespace filesystem {
315
316 #if defined(GHC_HAS_CUSTOM_STRING_VIEW)
317 #define GHC_WITH_STRING_VIEW
318 #elif defined(GHC_HAS_STD_STRING_VIEW)
319 #define GHC_WITH_STRING_VIEW
320 using std::basic_string_view;
321 #elif defined(GHC_HAS_STD_EXPERIMENTAL_STRING_VIEW)
322 #define GHC_WITH_STRING_VIEW
323 using std::experimental::basic_string_view;
324 #endif
325
326 // temporary existing exception type for yet unimplemented parts
327 class GHC_FS_API_CLASS not_implemented_exception : public std::logic_error
328 {
329 public:
330 not_implemented_exception()
331 : std::logic_error("function not implemented yet.")
332 {
333 }
334 };
335
336 template <typename char_type>
337 class path_helper_base
338 {
339 public:
340 using value_type = char_type;
341 #ifdef GHC_OS_WINDOWS
342 static constexpr value_type preferred_separator = '\\';
343 #else
344 static constexpr value_type preferred_separator = '/';
345 #endif
346 };
347
348 #if __cplusplus < 201703L
349 template <typename char_type>
350 constexpr char_type path_helper_base<char_type>::preferred_separator;
351 #endif
352
353 #ifdef GHC_OS_WINDOWS
354 class path;
355 namespace detail {
356 bool has_executable_extension(const path& p);
357 }
358 #endif
359
360 // [fs.class.path] class path
361 class GHC_FS_API_CLASS path
362 #if defined(GHC_OS_WINDOWS) && !defined(GHC_WIN_DISABLE_WSTRING_STORAGE_TYPE)
363 #define GHC_USE_WCHAR_T
364 #define GHC_NATIVEWP(p) p.c_str()
365 #define GHC_PLATFORM_LITERAL(str) L##str
366 : private path_helper_base<std::wstring::value_type>
367 {
368 public:
369 using path_helper_base<std::wstring::value_type>::value_type;
370 #else
371 #define GHC_NATIVEWP(p) p.wstring().c_str()
372 #define GHC_PLATFORM_LITERAL(str) str
373 : private path_helper_base<std::string::value_type>
374 {
375 public:
376 using path_helper_base<std::string::value_type>::value_type;
377 #endif
378 using string_type = std::basic_string<value_type>;
379 using path_helper_base<value_type>::preferred_separator;
380
381 // [fs.enum.path.format] enumeration format
382 /// The path format in which the constructor argument is given.
383 enum format {
384 generic_format, ///< The generic format, internally used by
385 ///< ghc::filesystem::path with slashes
386 native_format, ///< The format native to the current platform this code
387 ///< is build for
388 auto_format, ///< Try to auto-detect the format, fallback to native
389 };
390
391 template <class T>
392 struct _is_basic_string : std::false_type
393 {
394 };
395 template <class CharT, class Traits, class Alloc>
396 struct _is_basic_string<std::basic_string<CharT, Traits, Alloc>> : std::true_type
397 {
398 };
399 template <class CharT>
400 struct _is_basic_string<std::basic_string<CharT, std::char_traits<CharT>, std::allocator<CharT>>> : std::true_type
401 {
402 };
403 #ifdef GHC_WITH_STRING_VIEW
404 template <class CharT, class Traits>
405 struct _is_basic_string<basic_string_view<CharT, Traits>> : std::true_type
406 {
407 };
408 template <class CharT>
409 struct _is_basic_string<basic_string_view<CharT, std::char_traits<CharT>>> : std::true_type
410 {
411 };
412 #endif
413
414 template <typename T1, typename T2 = void>
415 using path_type = typename std::enable_if<!std::is_same<path, T1>::value, path>::type;
416 template <typename T>
417 #if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
418 using path_from_string =
419 typename std::enable_if<_is_basic_string<T>::value || std::is_same<char const*, typename std::decay<T>::type>::value || std::is_same<char*, typename std::decay<T>::type>::value || std::is_same<char8_t const*, typename std::decay<T>::type>::value ||
420 std::is_same<char8_t*, typename std::decay<T>::type>::value || std::is_same<char16_t const*, typename std::decay<T>::type>::value || std::is_same<char16_t*, typename std::decay<T>::type>::value ||
421 std::is_same<char32_t const*, typename std::decay<T>::type>::value || std::is_same<char32_t*, typename std::decay<T>::type>::value || std::is_same<wchar_t const*, typename std::decay<T>::type>::value ||
422 std::is_same<wchar_t*, typename std::decay<T>::type>::value,
423 path>::type;
424 template <typename T>
425 using path_type_EcharT = typename std::enable_if<std::is_same<T, char>::value || std::is_same<T, char8_t>::value || std::is_same<T, char16_t>::value || std::is_same<T, char32_t>::value || std::is_same<T, wchar_t>::value, path>::type;
426 #else
427 using path_from_string =
428 typename std::enable_if<_is_basic_string<T>::value || std::is_same<char const*, typename std::decay<T>::type>::value || std::is_same<char*, typename std::decay<T>::type>::value ||
429 std::is_same<char16_t const*, typename std::decay<T>::type>::value || std::is_same<char16_t*, typename std::decay<T>::type>::value || std::is_same<char32_t const*, typename std::decay<T>::type>::value ||
430 std::is_same<char32_t*, typename std::decay<T>::type>::value || std::is_same<wchar_t const*, typename std::decay<T>::type>::value || std::is_same<wchar_t*, typename std::decay<T>::type>::value,
431 path>::type;
432 template <typename T>
433 using path_type_EcharT = typename std::enable_if<std::is_same<T, char>::value || std::is_same<T, char16_t>::value || std::is_same<T, char32_t>::value || std::is_same<T, wchar_t>::value, path>::type;
434 #endif
435 // [fs.path.construct] constructors and destructor
436 path() noexcept;
437 path(const path& p);
438 path(path&& p) noexcept;
439 path(string_type&& source, format fmt = auto_format);
440 template <class Source, typename = path_from_string<Source>>
441 path(const Source& source, format fmt = auto_format);
442 template <class InputIterator>
443 path(InputIterator first, InputIterator last, format fmt = auto_format);
444 #ifdef GHC_WITH_EXCEPTIONS
445 template <class Source, typename = path_from_string<Source>>
446 path(const Source& source, const std::locale& loc, format fmt = auto_format);
447 template <class InputIterator>
448 path(InputIterator first, InputIterator last, const std::locale& loc, format fmt = auto_format);
449 #endif
450 ~path();
451
452 // [fs.path.assign] assignments
453 path& operator=(const path& p);
454 path& operator=(path&& p) noexcept;
455 path& operator=(string_type&& source);
456 path& assign(string_type&& source);
457 template <class Source>
458 path& operator=(const Source& source);
459 template <class Source>
460 path& assign(const Source& source);
461 template <class InputIterator>
462 path& assign(InputIterator first, InputIterator last);
463
464 // [fs.path.append] appends
465 path& operator/=(const path& p);
466 template <class Source>
467 path& operator/=(const Source& source);
468 template <class Source>
469 path& append(const Source& source);
470 template <class InputIterator>
471 path& append(InputIterator first, InputIterator last);
472
473 // [fs.path.concat] concatenation
474 path& operator+=(const path& x);
475 path& operator+=(const string_type& x);
476 #ifdef GHC_WITH_STRING_VIEW
477 path& operator+=(basic_string_view<value_type> x);
478 #endif
479 path& operator+=(const value_type* x);
480 path& operator+=(value_type x);
481 template <class Source>
482 path_from_string<Source>& operator+=(const Source& x);
483 template <class EcharT>
484 path_type_EcharT<EcharT>& operator+=(EcharT x);
485 template <class Source>
486 path& concat(const Source& x);
487 template <class InputIterator>
488 path& concat(InputIterator first, InputIterator last);
489
490 // [fs.path.modifiers] modifiers
491 void clear() noexcept;
492 path& make_preferred();
493 path& remove_filename();
494 path& replace_filename(const path& replacement);
495 path& replace_extension(const path& replacement = path());
496 void swap(path& rhs) noexcept;
497
498 // [fs.path.native.obs] native format observers
499 const string_type& native() const noexcept;
500 const value_type* c_str() const noexcept;
501 operator string_type() const;
502 template <class EcharT, class traits = std::char_traits<EcharT>, class Allocator = std::allocator<EcharT>>
503 std::basic_string<EcharT, traits, Allocator> string(const Allocator& a = Allocator()) const;
504 std::string string() const;
505 std::wstring wstring() const;
506 #if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
507 std::u8string u8string() const;
508 #else
509 std::string u8string() const;
510 #endif
511 std::u16string u16string() const;
512 std::u32string u32string() const;
513
514 // [fs.path.generic.obs] generic format observers
515 template <class EcharT, class traits = std::char_traits<EcharT>, class Allocator = std::allocator<EcharT>>
516 std::basic_string<EcharT, traits, Allocator> generic_string(const Allocator& a = Allocator()) const;
517 std::string generic_string() const;
518 std::wstring generic_wstring() const;
519 #if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
520 std::u8string generic_u8string() const;
521 #else
522 std::string generic_u8string() const;
523 #endif
524 std::u16string generic_u16string() const;
525 std::u32string generic_u32string() const;
526
527 // [fs.path.compare] compare
528 int compare(const path& p) const noexcept;
529 int compare(const string_type& s) const;
530 #ifdef GHC_WITH_STRING_VIEW
531 int compare(basic_string_view<value_type> s) const;
532 #endif
533 int compare(const value_type* s) const;
534
535 // [fs.path.decompose] decomposition
536 path root_name() const;
537 path root_directory() const;
538 path root_path() const;
539 path relative_path() const;
540 path parent_path() const;
541 path filename() const;
542 path stem() const;
543 path extension() const;
544
545 // [fs.path.query] query
546 bool empty() const noexcept;
547 bool has_root_name() const;
548 bool has_root_directory() const;
549 bool has_root_path() const;
550 bool has_relative_path() const;
551 bool has_parent_path() const;
552 bool has_filename() const;
553 bool has_stem() const;
554 bool has_extension() const;
555 bool is_absolute() const;
556 bool is_relative() const;
557
558 // [fs.path.gen] generation
559 path lexically_normal() const;
560 path lexically_relative(const path& base) const;
561 path lexically_proximate(const path& base) const;
562
563 // [fs.path.itr] iterators
564 class iterator;
565 using const_iterator = iterator;
566 iterator begin() const;
567 iterator end() const;
568
569 private:
570 using impl_value_type = value_type;
571 using impl_string_type = std::basic_string<impl_value_type>;
572 friend class directory_iterator;
573 void append_name(const value_type* name);
574 static constexpr impl_value_type generic_separator = '/';
575 template <typename InputIterator>
576 class input_iterator_range
577 {
578 public:
579 typedef InputIterator iterator;
580 typedef InputIterator const_iterator;
581 typedef typename InputIterator::difference_type difference_type;
582
583 input_iterator_range(const InputIterator& first, const InputIterator& last)
584 : _first(first)
585 , _last(last)
586 {
587 }
588
589 InputIterator begin() const { return _first; }
590 InputIterator end() const { return _last; }
591
592 private:
593 InputIterator _first;
594 InputIterator _last;
595 };
596 friend void swap(path& lhs, path& rhs) noexcept;
597 friend size_t hash_value(const path& p) noexcept;
598 friend path canonical(const path& p, std::error_code& ec);
599 friend bool create_directories(const path& p, std::error_code& ec) noexcept;
600 string_type::size_type root_name_length() const noexcept;
601 void postprocess_path_with_format(format fmt);
602 void check_long_path();
603 impl_string_type _path;
604 #ifdef GHC_OS_WINDOWS
605 void handle_prefixes();
606 friend bool detail::has_executable_extension(const path& p);
607 #ifdef GHC_WIN_AUTO_PREFIX_LONG_PATH
608 string_type::size_type _prefixLength{0};
609 #else // GHC_WIN_AUTO_PREFIX_LONG_PATH
610 static const string_type::size_type _prefixLength{0};
611 #endif // GHC_WIN_AUTO_PREFIX_LONG_PATH
612 #else
613 static const string_type::size_type _prefixLength{0};
614 #endif
615 };
616
617 // [fs.path.nonmember] path non-member functions
618 GHC_FS_API void swap(path& lhs, path& rhs) noexcept;
619 GHC_FS_API size_t hash_value(const path& p) noexcept;
620 #ifdef GHC_HAS_THREEWAY_COMP
621 GHC_FS_API std::strong_ordering operator<=>(const path& lhs, const path& rhs) noexcept;
622 #endif
623 GHC_FS_API bool operator==(const path& lhs, const path& rhs) noexcept;
624 GHC_FS_API bool operator!=(const path& lhs, const path& rhs) noexcept;
625 GHC_FS_API bool operator<(const path& lhs, const path& rhs) noexcept;
626 GHC_FS_API bool operator<=(const path& lhs, const path& rhs) noexcept;
627 GHC_FS_API bool operator>(const path& lhs, const path& rhs) noexcept;
628 GHC_FS_API bool operator>=(const path& lhs, const path& rhs) noexcept;
629 GHC_FS_API path operator/(const path& lhs, const path& rhs);
630
631 // [fs.path.io] path inserter and extractor
632 template <class charT, class traits>
633 std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const path& p);
634 template <class charT, class traits>
635 std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, path& p);
636
637 // [pfs.path.factory] path factory functions
638 template <class Source, typename = path::path_from_string<Source>>
639 #if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
640 [[deprecated("use ghc::filesystem::path::path() with std::u8string instead")]]
641 #endif
642 path u8path(const Source& source);
643 template <class InputIterator>
644 #if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
645 [[deprecated("use ghc::filesystem::path::path() with std::u8string instead")]]
646 #endif
647 path u8path(InputIterator first, InputIterator last);
648
649 // [fs.class.filesystem_error] class filesystem_error
650 class GHC_FS_API_CLASS filesystem_error : public std::system_error
651 {
652 public:
653 filesystem_error(const std::string& what_arg, std::error_code ec);
654 filesystem_error(const std::string& what_arg, const path& p1, std::error_code ec);
655 filesystem_error(const std::string& what_arg, const path& p1, const path& p2, std::error_code ec);
656 const path& path1() const noexcept;
657 const path& path2() const noexcept;
658 const char* what() const noexcept override;
659
660 private:
661 std::string _what_arg;
662 std::error_code _ec;
663 path _p1, _p2;
664 };
665
666 class GHC_FS_API_CLASS path::iterator
667 {
668 public:
669 using value_type = const path;
670 using difference_type = std::ptrdiff_t;
671 using pointer = const path*;
672 using reference = const path&;
673 using iterator_category = std::bidirectional_iterator_tag;
674
675 iterator();
676 iterator(const path& p, const impl_string_type::const_iterator& pos);
677 iterator& operator++();
678 iterator operator++(int);
679 iterator& operator--();
680 iterator operator--(int);
681 bool operator==(const iterator& other) const;
682 bool operator!=(const iterator& other) const;
683 reference operator*() const;
684 pointer operator->() const;
685
686 private:
687 friend class path;
688 impl_string_type::const_iterator increment(const impl_string_type::const_iterator& pos) const;
689 impl_string_type::const_iterator decrement(const impl_string_type::const_iterator& pos) const;
690 void updateCurrent();
691 impl_string_type::const_iterator _first;
692 impl_string_type::const_iterator _last;
693 impl_string_type::const_iterator _prefix;
694 impl_string_type::const_iterator _root;
695 impl_string_type::const_iterator _iter;
696 path _current;
697 };
698
699 struct space_info
700 {
701 uintmax_t capacity;
702 uintmax_t free;
703 uintmax_t available;
704 };
705
706 // [fs.enum] enumerations
707 // [fs.enum.file_type]
708 enum class file_type {
709 none,
710 not_found,
711 regular,
712 directory,
713 symlink,
714 block,
715 character,
716 fifo,
717 socket,
718 unknown,
719 };
720
721 // [fs.enum.perms]
722 enum class perms : uint16_t {
723 none = 0,
724
725 owner_read = 0400,
726 owner_write = 0200,
727 owner_exec = 0100,
728 owner_all = 0700,
729
730 group_read = 040,
731 group_write = 020,
732 group_exec = 010,
733 group_all = 070,
734
735 others_read = 04,
736 others_write = 02,
737 others_exec = 01,
738 others_all = 07,
739
740 all = 0777,
741 set_uid = 04000,
742 set_gid = 02000,
743 sticky_bit = 01000,
744
745 mask = 07777,
746 unknown = 0xffff
747 };
748
749 // [fs.enum.perm.opts]
750 enum class perm_options : uint16_t {
751 replace = 3,
752 add = 1,
753 remove = 2,
754 nofollow = 4,
755 };
756
757 // [fs.enum.copy.opts]
758 enum class copy_options : uint16_t {
759 none = 0,
760
761 skip_existing = 1,
762 overwrite_existing = 2,
763 update_existing = 4,
764
765 recursive = 8,
766
767 copy_symlinks = 0x10,
768 skip_symlinks = 0x20,
769
770 directories_only = 0x40,
771 create_symlinks = 0x80,
772 #ifndef GHC_OS_WEB
773 create_hard_links = 0x100
774 #endif
775 };
776
777 // [fs.enum.dir.opts]
778 enum class directory_options : uint16_t {
779 none = 0,
780 follow_directory_symlink = 1,
781 skip_permission_denied = 2,
782 };
783
784 // [fs.class.file_status] class file_status
785 class GHC_FS_API_CLASS file_status
786 {
787 public:
788 // [fs.file_status.cons] constructors and destructor
789 file_status() noexcept;
790 explicit file_status(file_type ft, perms prms = perms::unknown) noexcept;
791 file_status(const file_status&) noexcept;
792 file_status(file_status&&) noexcept;
793 ~file_status();
794 // assignments:
795 file_status& operator=(const file_status&) noexcept;
796 file_status& operator=(file_status&&) noexcept;
797 // [fs.file_status.mods] modifiers
798 void type(file_type ft) noexcept;
799 void permissions(perms prms) noexcept;
800 // [fs.file_status.obs] observers
801 file_type type() const noexcept;
802 perms permissions() const noexcept;
803 friend bool operator==(const file_status& lhs, const file_status& rhs) noexcept { return lhs.type() == rhs.type() && lhs.permissions() == rhs.permissions(); }
804
805 private:
806 file_type _type;
807 perms _perms;
808 };
809
810 using file_time_type = std::chrono::time_point<std::chrono::system_clock>;
811
812 // [fs.class.directory_entry] Class directory_entry
813 class GHC_FS_API_CLASS directory_entry
814 {
815 public:
816 // [fs.dir.entry.cons] constructors and destructor
817 directory_entry() noexcept = default;
818 directory_entry(const directory_entry&) = default;
819 directory_entry(directory_entry&&) noexcept = default;
820 #ifdef GHC_WITH_EXCEPTIONS
821 explicit directory_entry(const path& p);
822 #endif
823 directory_entry(const path& p, std::error_code& ec);
824 ~directory_entry();
825
826 // assignments:
827 directory_entry& operator=(const directory_entry&) = default;
828 directory_entry& operator=(directory_entry&&) noexcept = default;
829
830 // [fs.dir.entry.mods] modifiers
831 #ifdef GHC_WITH_EXCEPTIONS
832 void assign(const path& p);
833 void replace_filename(const path& p);
834 void refresh();
835 #endif
836 void assign(const path& p, std::error_code& ec);
837 void replace_filename(const path& p, std::error_code& ec);
838 void refresh(std::error_code& ec) noexcept;
839
840 // [fs.dir.entry.obs] observers
841 const filesystem::path& path() const noexcept;
842 operator const filesystem::path&() const noexcept;
843 #ifdef GHC_WITH_EXCEPTIONS
844 bool exists() const;
845 bool is_block_file() const;
846 bool is_character_file() const;
847 bool is_directory() const;
848 bool is_fifo() const;
849 bool is_other() const;
850 bool is_regular_file() const;
851 bool is_socket() const;
852 bool is_symlink() const;
853 uintmax_t file_size() const;
854 file_time_type last_write_time() const;
855 file_status status() const;
856 file_status symlink_status() const;
857 #endif
858 bool exists(std::error_code& ec) const noexcept;
859 bool is_block_file(std::error_code& ec) const noexcept;
860 bool is_character_file(std::error_code& ec) const noexcept;
861 bool is_directory(std::error_code& ec) const noexcept;
862 bool is_fifo(std::error_code& ec) const noexcept;
863 bool is_other(std::error_code& ec) const noexcept;
864 bool is_regular_file(std::error_code& ec) const noexcept;
865 bool is_socket(std::error_code& ec) const noexcept;
866 bool is_symlink(std::error_code& ec) const noexcept;
867 uintmax_t file_size(std::error_code& ec) const noexcept;
868 file_time_type last_write_time(std::error_code& ec) const noexcept;
869 file_status status(std::error_code& ec) const noexcept;
870 file_status symlink_status(std::error_code& ec) const noexcept;
871
872 #ifndef GHC_OS_WEB
873 #ifdef GHC_WITH_EXCEPTIONS
874 uintmax_t hard_link_count() const;
875 #endif
876 uintmax_t hard_link_count(std::error_code& ec) const noexcept;
877 #endif
878
879 #ifdef GHC_HAS_THREEWAY_COMP
880 std::strong_ordering operator<=>(const directory_entry& rhs) const noexcept;
881 #endif
882 bool operator<(const directory_entry& rhs) const noexcept;
883 bool operator==(const directory_entry& rhs) const noexcept;
884 bool operator!=(const directory_entry& rhs) const noexcept;
885 bool operator<=(const directory_entry& rhs) const noexcept;
886 bool operator>(const directory_entry& rhs) const noexcept;
887 bool operator>=(const directory_entry& rhs) const noexcept;
888
889 private:
890 friend class directory_iterator;
891 #ifdef GHC_WITH_EXCEPTIONS
892 file_type status_file_type() const;
893 #endif
894 file_type status_file_type(std::error_code& ec) const noexcept;
895 filesystem::path _path;
896 file_status _status;
897 file_status _symlink_status;
898 uintmax_t _file_size = static_cast<uintmax_t>(-1);
899 #ifndef GHC_OS_WINDOWS
900 uintmax_t _hard_link_count = static_cast<uintmax_t>(-1);
901 #endif
902 time_t _last_write_time = 0;
903 };
904
905 // [fs.class.directory.iterator] Class directory_iterator
906 class GHC_FS_API_CLASS directory_iterator
907 {
908 public:
909 class GHC_FS_API_CLASS proxy
910 {
911 public:
912 const directory_entry& operator*() const& noexcept { return _dir_entry; }
913 directory_entry operator*() && noexcept { return std::move(_dir_entry); }
914
915 private:
916 explicit proxy(const directory_entry& dir_entry)
917 : _dir_entry(dir_entry)
918 {
919 }
920 friend class directory_iterator;
921 friend class recursive_directory_iterator;
922 directory_entry _dir_entry;
923 };
924 using iterator_category = std::input_iterator_tag;
925 using value_type = directory_entry;
926 using difference_type = std::ptrdiff_t;
927 using pointer = const directory_entry*;
928 using reference = const directory_entry&;
929
930 // [fs.dir.itr.members] member functions
931 directory_iterator() noexcept;
932 #ifdef GHC_WITH_EXCEPTIONS
933 explicit directory_iterator(const path& p);
934 directory_iterator(const path& p, directory_options options);
935 #endif
936 directory_iterator(const path& p, std::error_code& ec) noexcept;
937 directory_iterator(const path& p, directory_options options, std::error_code& ec) noexcept;
938 directory_iterator(const directory_iterator& rhs);
939 directory_iterator(directory_iterator&& rhs) noexcept;
940 ~directory_iterator();
941 directory_iterator& operator=(const directory_iterator& rhs);
942 directory_iterator& operator=(directory_iterator&& rhs) noexcept;
943 const directory_entry& operator*() const;
944 const directory_entry* operator->() const;
945 #ifdef GHC_WITH_EXCEPTIONS
946 directory_iterator& operator++();
947 #endif
948 directory_iterator& increment(std::error_code& ec) noexcept;
949
950 // other members as required by [input.iterators]
951 #ifdef GHC_WITH_EXCEPTIONS
952 proxy operator++(int)
953 {
954 proxy p{**this};
955 ++*this;
956 return p;
957 }
958 #endif
959 bool operator==(const directory_iterator& rhs) const;
960 bool operator!=(const directory_iterator& rhs) const;
961
962 private:
963 friend class recursive_directory_iterator;
964 class impl;
965 std::shared_ptr<impl> _impl;
966 };
967
968 // [fs.dir.itr.nonmembers] directory_iterator non-member functions
969 GHC_FS_API directory_iterator begin(directory_iterator iter) noexcept;
970 GHC_FS_API directory_iterator end(const directory_iterator&) noexcept;
971
972 // [fs.class.re.dir.itr] class recursive_directory_iterator
973 class GHC_FS_API_CLASS recursive_directory_iterator
974 {
975 public:
976 using iterator_category = std::input_iterator_tag;
977 using value_type = directory_entry;
978 using difference_type = std::ptrdiff_t;
979 using pointer = const directory_entry*;
980 using reference = const directory_entry&;
981
982 // [fs.rec.dir.itr.members] constructors and destructor
983 recursive_directory_iterator() noexcept;
984 #ifdef GHC_WITH_EXCEPTIONS
985 explicit recursive_directory_iterator(const path& p);
986 recursive_directory_iterator(const path& p, directory_options options);
987 #endif
988 recursive_directory_iterator(const path& p, directory_options options, std::error_code& ec) noexcept;
989 recursive_directory_iterator(const path& p, std::error_code& ec) noexcept;
990 recursive_directory_iterator(const recursive_directory_iterator& rhs);
991 recursive_directory_iterator(recursive_directory_iterator&& rhs) noexcept;
992 ~recursive_directory_iterator();
993
994 // [fs.rec.dir.itr.members] observers
995 directory_options options() const;
996 int depth() const;
997 bool recursion_pending() const;
998
999 const directory_entry& operator*() const;
1000 const directory_entry* operator->() const;
1001
1002 // [fs.rec.dir.itr.members] modifiers recursive_directory_iterator&
1003 recursive_directory_iterator& operator=(const recursive_directory_iterator& rhs);
1004 recursive_directory_iterator& operator=(recursive_directory_iterator&& rhs) noexcept;
1005 #ifdef GHC_WITH_EXCEPTIONS
1006 recursive_directory_iterator& operator++();
1007 #endif
1008 recursive_directory_iterator& increment(std::error_code& ec) noexcept;
1009
1010 #ifdef GHC_WITH_EXCEPTIONS
1011 void pop();
1012 #endif
1013 void pop(std::error_code& ec);
1014 void disable_recursion_pending();
1015
1016 // other members as required by [input.iterators]
1017 #ifdef GHC_WITH_EXCEPTIONS
1018 directory_iterator::proxy operator++(int)
1019 {
1020 directory_iterator::proxy proxy{**this};
1021 ++*this;
1022 return proxy;
1023 }
1024 #endif
1025 bool operator==(const recursive_directory_iterator& rhs) const;
1026 bool operator!=(const recursive_directory_iterator& rhs) const;
1027
1028 private:
1029 struct recursive_directory_iterator_impl
1030 {
1031 directory_options _options;
1032 bool _recursion_pending;
1033 std::stack<directory_iterator> _dir_iter_stack;
1034 recursive_directory_iterator_impl(directory_options options, bool recursion_pending)
1035 : _options(options)
1036 , _recursion_pending(recursion_pending)
1037 {
1038 }
1039 };
1040 std::shared_ptr<recursive_directory_iterator_impl> _impl;
1041 };
1042
1043 // [fs.rec.dir.itr.nonmembers] directory_iterator non-member functions
1044 GHC_FS_API recursive_directory_iterator begin(recursive_directory_iterator iter) noexcept;
1045 GHC_FS_API recursive_directory_iterator end(const recursive_directory_iterator&) noexcept;
1046
1047 // [fs.op.funcs] filesystem operations
1048 #ifdef GHC_WITH_EXCEPTIONS
1049 GHC_FS_API path absolute(const path& p);
1050 GHC_FS_API path canonical(const path& p);
1051 GHC_FS_API void copy(const path& from, const path& to);
1052 GHC_FS_API void copy(const path& from, const path& to, copy_options options);
1053 GHC_FS_API bool copy_file(const path& from, const path& to);
1054 GHC_FS_API bool copy_file(const path& from, const path& to, copy_options option);
1055 GHC_FS_API void copy_symlink(const path& existing_symlink, const path& new_symlink);
1056 GHC_FS_API bool create_directories(const path& p);
1057 GHC_FS_API bool create_directory(const path& p);
1058 GHC_FS_API bool create_directory(const path& p, const path& attributes);
1059 GHC_FS_API void create_directory_symlink(const path& to, const path& new_symlink);
1060 GHC_FS_API void create_symlink(const path& to, const path& new_symlink);
1061 GHC_FS_API path current_path();
1062 GHC_FS_API void current_path(const path& p);
1063 GHC_FS_API bool exists(const path& p);
1064 GHC_FS_API bool equivalent(const path& p1, const path& p2);
1065 GHC_FS_API uintmax_t file_size(const path& p);
1066 GHC_FS_API bool is_block_file(const path& p);
1067 GHC_FS_API bool is_character_file(const path& p);
1068 GHC_FS_API bool is_directory(const path& p);
1069 GHC_FS_API bool is_empty(const path& p);
1070 GHC_FS_API bool is_fifo(const path& p);
1071 GHC_FS_API bool is_other(const path& p);
1072 GHC_FS_API bool is_regular_file(const path& p);
1073 GHC_FS_API bool is_socket(const path& p);
1074 GHC_FS_API bool is_symlink(const path& p);
1075 GHC_FS_API file_time_type last_write_time(const path& p);
1076 GHC_FS_API void last_write_time(const path& p, file_time_type new_time);
1077 GHC_FS_API void permissions(const path& p, perms prms, perm_options opts = perm_options::replace);
1078 GHC_FS_API path proximate(const path& p, const path& base = current_path());
1079 GHC_FS_API path read_symlink(const path& p);
1080 GHC_FS_API path relative(const path& p, const path& base = current_path());
1081 GHC_FS_API bool remove(const path& p);
1082 GHC_FS_API uintmax_t remove_all(const path& p);
1083 GHC_FS_API void rename(const path& from, const path& to);
1084 GHC_FS_API void resize_file(const path& p, uintmax_t size);
1085 GHC_FS_API space_info space(const path& p);
1086 GHC_FS_API file_status status(const path& p);
1087 GHC_FS_API file_status symlink_status(const path& p);
1088 GHC_FS_API path temp_directory_path();
1089 GHC_FS_API path weakly_canonical(const path& p);
1090 #endif
1091 GHC_FS_API path absolute(const path& p, std::error_code& ec);
1092 GHC_FS_API path canonical(const path& p, std::error_code& ec);
1093 GHC_FS_API void copy(const path& from, const path& to, std::error_code& ec) noexcept;
1094 GHC_FS_API void copy(const path& from, const path& to, copy_options options, std::error_code& ec) noexcept;
1095 GHC_FS_API bool copy_file(const path& from, const path& to, std::error_code& ec) noexcept;
1096 GHC_FS_API bool copy_file(const path& from, const path& to, copy_options option, std::error_code& ec) noexcept;
1097 GHC_FS_API void copy_symlink(const path& existing_symlink, const path& new_symlink, std::error_code& ec) noexcept;
1098 GHC_FS_API bool create_directories(const path& p, std::error_code& ec) noexcept;
1099 GHC_FS_API bool create_directory(const path& p, std::error_code& ec) noexcept;
1100 GHC_FS_API bool create_directory(const path& p, const path& attributes, std::error_code& ec) noexcept;
1101 GHC_FS_API void create_directory_symlink(const path& to, const path& new_symlink, std::error_code& ec) noexcept;
1102 GHC_FS_API void create_symlink(const path& to, const path& new_symlink, std::error_code& ec) noexcept;
1103 GHC_FS_API path current_path(std::error_code& ec);
1104 GHC_FS_API void current_path(const path& p, std::error_code& ec) noexcept;
1105 GHC_FS_API bool exists(file_status s) noexcept;
1106 GHC_FS_API bool exists(const path& p, std::error_code& ec) noexcept;
1107 GHC_FS_API bool equivalent(const path& p1, const path& p2, std::error_code& ec) noexcept;
1108 GHC_FS_API uintmax_t file_size(const path& p, std::error_code& ec) noexcept;
1109 GHC_FS_API bool is_block_file(file_status s) noexcept;
1110 GHC_FS_API bool is_block_file(const path& p, std::error_code& ec) noexcept;
1111 GHC_FS_API bool is_character_file(file_status s) noexcept;
1112 GHC_FS_API bool is_character_file(const path& p, std::error_code& ec) noexcept;
1113 GHC_FS_API bool is_directory(file_status s) noexcept;
1114 GHC_FS_API bool is_directory(const path& p, std::error_code& ec) noexcept;
1115 GHC_FS_API bool is_empty(const path& p, std::error_code& ec) noexcept;
1116 GHC_FS_API bool is_fifo(file_status s) noexcept;
1117 GHC_FS_API bool is_fifo(const path& p, std::error_code& ec) noexcept;
1118 GHC_FS_API bool is_other(file_status s) noexcept;
1119 GHC_FS_API bool is_other(const path& p, std::error_code& ec) noexcept;
1120 GHC_FS_API bool is_regular_file(file_status s) noexcept;
1121 GHC_FS_API bool is_regular_file(const path& p, std::error_code& ec) noexcept;
1122 GHC_FS_API bool is_socket(file_status s) noexcept;
1123 GHC_FS_API bool is_socket(const path& p, std::error_code& ec) noexcept;
1124 GHC_FS_API bool is_symlink(file_status s) noexcept;
1125 GHC_FS_API bool is_symlink(const path& p, std::error_code& ec) noexcept;
1126 GHC_FS_API file_time_type last_write_time(const path& p, std::error_code& ec) noexcept;
1127 GHC_FS_API void last_write_time(const path& p, file_time_type new_time, std::error_code& ec) noexcept;
1128 GHC_FS_API void permissions(const path& p, perms prms, std::error_code& ec) noexcept;
1129 GHC_FS_API void permissions(const path& p, perms prms, perm_options opts, std::error_code& ec) noexcept;
1130 GHC_FS_API path proximate(const path& p, std::error_code& ec);
1131 GHC_FS_API path proximate(const path& p, const path& base, std::error_code& ec);
1132 GHC_FS_API path read_symlink(const path& p, std::error_code& ec);
1133 GHC_FS_API path relative(const path& p, std::error_code& ec);
1134 GHC_FS_API path relative(const path& p, const path& base, std::error_code& ec);
1135 GHC_FS_API bool remove(const path& p, std::error_code& ec) noexcept;
1136 GHC_FS_API uintmax_t remove_all(const path& p, std::error_code& ec) noexcept;
1137 GHC_FS_API void rename(const path& from, const path& to, std::error_code& ec) noexcept;
1138 GHC_FS_API void resize_file(const path& p, uintmax_t size, std::error_code& ec) noexcept;
1139 GHC_FS_API space_info space(const path& p, std::error_code& ec) noexcept;
1140 GHC_FS_API file_status status(const path& p, std::error_code& ec) noexcept;
1141 GHC_FS_API bool status_known(file_status s) noexcept;
1142 GHC_FS_API file_status symlink_status(const path& p, std::error_code& ec) noexcept;
1143 GHC_FS_API path temp_directory_path(std::error_code& ec) noexcept;
1144 GHC_FS_API path weakly_canonical(const path& p, std::error_code& ec) noexcept;
1145
1146 #ifndef GHC_OS_WEB
1147 #ifdef GHC_WITH_EXCEPTIONS
1148 GHC_FS_API void create_hard_link(const path& to, const path& new_hard_link);
1149 GHC_FS_API uintmax_t hard_link_count(const path& p);
1150 #endif
1151 GHC_FS_API void create_hard_link(const path& to, const path& new_hard_link, std::error_code& ec) noexcept;
1152 GHC_FS_API uintmax_t hard_link_count(const path& p, std::error_code& ec) noexcept;
1153 #endif
1154
1155 // Non-C++17 add-on std::fstream wrappers with path
1156 template <class charT, class traits = std::char_traits<charT>>
1157 class basic_filebuf : public std::basic_filebuf<charT, traits>
1158 {
1159 public:
1160 basic_filebuf() {}
1161 ~basic_filebuf() override {}
1162 basic_filebuf(const basic_filebuf&) = delete;
1163 const basic_filebuf& operator=(const basic_filebuf&) = delete;
1164 basic_filebuf<charT, traits>* open(const path& p, std::ios_base::openmode mode)
1165 {
1166 #if defined(GHC_OS_WINDOWS) && !defined(__GLIBCXX__)
1167 return std::basic_filebuf<charT, traits>::open(p.wstring().c_str(), mode) ? this : 0;
1168 #else
1169 return std::basic_filebuf<charT, traits>::open(p.string().c_str(), mode) ? this : 0;
1170 #endif
1171 }
1172 };
1173
1174 template <class charT, class traits = std::char_traits<charT>>
1175 class basic_ifstream : public std::basic_ifstream<charT, traits>
1176 {
1177 public:
1178 basic_ifstream() {}
1179 #if defined(GHC_OS_WINDOWS) && !defined(__GLIBCXX__)
1180 explicit basic_ifstream(const path& p, std::ios_base::openmode mode = std::ios_base::in)
1181 : std::basic_ifstream<charT, traits>(p.wstring().c_str(), mode)
1182 {
1183 }
1184 void open(const path& p, std::ios_base::openmode mode = std::ios_base::in) { std::basic_ifstream<charT, traits>::open(p.wstring().c_str(), mode); }
1185 #else
1186 explicit basic_ifstream(const path& p, std::ios_base::openmode mode = std::ios_base::in)
1187 : std::basic_ifstream<charT, traits>(p.string().c_str(), mode)
1188 {
1189 }
1190 void open(const path& p, std::ios_base::openmode mode = std::ios_base::in) { std::basic_ifstream<charT, traits>::open(p.string().c_str(), mode); }
1191 #endif
1192 basic_ifstream(const basic_ifstream&) = delete;
1193 const basic_ifstream& operator=(const basic_ifstream&) = delete;
1194 ~basic_ifstream() override {}
1195 };
1196
1197 template <class charT, class traits = std::char_traits<charT>>
1198 class basic_ofstream : public std::basic_ofstream<charT, traits>
1199 {
1200 public:
1201 basic_ofstream() {}
1202 #if defined(GHC_OS_WINDOWS) && !defined(__GLIBCXX__)
1203 explicit basic_ofstream(const path& p, std::ios_base::openmode mode = std::ios_base::out)
1204 : std::basic_ofstream<charT, traits>(p.wstring().c_str(), mode)
1205 {
1206 }
1207 void open(const path& p, std::ios_base::openmode mode = std::ios_base::out) { std::basic_ofstream<charT, traits>::open(p.wstring().c_str(), mode); }
1208 #else
1209 explicit basic_ofstream(const path& p, std::ios_base::openmode mode = std::ios_base::out)
1210 : std::basic_ofstream<charT, traits>(p.string().c_str(), mode)
1211 {
1212 }
1213 void open(const path& p, std::ios_base::openmode mode = std::ios_base::out) { std::basic_ofstream<charT, traits>::open(p.string().c_str(), mode); }
1214 #endif
1215 basic_ofstream(const basic_ofstream&) = delete;
1216 const basic_ofstream& operator=(const basic_ofstream&) = delete;
1217 ~basic_ofstream() override {}
1218 };
1219
1220 template <class charT, class traits = std::char_traits<charT>>
1221 class basic_fstream : public std::basic_fstream<charT, traits>
1222 {
1223 public:
1224 basic_fstream() {}
1225 #if defined(GHC_OS_WINDOWS) && !defined(__GLIBCXX__)
1226 explicit basic_fstream(const path& p, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
1227 : std::basic_fstream<charT, traits>(p.wstring().c_str(), mode)
1228 {
1229 }
1230 void open(const path& p, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) { std::basic_fstream<charT, traits>::open(p.wstring().c_str(), mode); }
1231 #else
1232 explicit basic_fstream(const path& p, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
1233 : std::basic_fstream<charT, traits>(p.string().c_str(), mode)
1234 {
1235 }
1236 void open(const path& p, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) { std::basic_fstream<charT, traits>::open(p.string().c_str(), mode); }
1237 #endif
1238 basic_fstream(const basic_fstream&) = delete;
1239 const basic_fstream& operator=(const basic_fstream&) = delete;
1240 ~basic_fstream() override {}
1241 };
1242
1243 typedef basic_filebuf<char> filebuf;
1244 typedef basic_filebuf<wchar_t> wfilebuf;
1245 typedef basic_ifstream<char> ifstream;
1246 typedef basic_ifstream<wchar_t> wifstream;
1247 typedef basic_ofstream<char> ofstream;
1248 typedef basic_ofstream<wchar_t> wofstream;
1249 typedef basic_fstream<char> fstream;
1250 typedef basic_fstream<wchar_t> wfstream;
1251
1252 class GHC_FS_API_CLASS u8arguments
1253 {
1254 public:
1255 u8arguments(int& argc, char**& argv);
1256 ~u8arguments()
1257 {
1258 _refargc = _argc;
1259 _refargv = _argv;
1260 }
1261
1262 bool valid() const { return _isvalid; }
1263
1264 private:
1265 int _argc;
1266 char** _argv;
1267 int& _refargc;
1268 char**& _refargv;
1269 bool _isvalid;
1270 #ifdef GHC_OS_WINDOWS
1271 std::vector<std::string> _args;
1272 std::vector<char*> _argp;
1273 #endif
1274 };
1275
1276 //-------------------------------------------------------------------------------------------------
1277 // Implementation
1278 //-------------------------------------------------------------------------------------------------
1279
1280 namespace detail {
1281 enum utf8_states_t { S_STRT = 0, S_RJCT = 8 };
1282 GHC_FS_API void appendUTF8(std::string& str, uint32_t unicode);
1283 GHC_FS_API bool is_surrogate(uint32_t c);
1284 GHC_FS_API bool is_high_surrogate(uint32_t c);
1285 GHC_FS_API bool is_low_surrogate(uint32_t c);
1286 GHC_FS_API unsigned consumeUtf8Fragment(const unsigned state, const uint8_t fragment, uint32_t& codepoint);
1287 enum class portable_error {
1288 none = 0,
1289 exists,
1290 not_found,
1291 not_supported,
1292 not_implemented,
1293 invalid_argument,
1294 is_a_directory,
1295 };
1296 GHC_FS_API std::error_code make_error_code(portable_error err);
1297 #ifdef GHC_OS_WINDOWS
1298 GHC_FS_API std::error_code make_system_error(uint32_t err = 0);
1299 #else
1300 GHC_FS_API std::error_code make_system_error(int err = 0);
1301 #endif
1302 } // namespace detail
1303
1304 namespace detail {
1305
1306 #ifdef GHC_EXPAND_IMPL
1307
1308 GHC_INLINE std::error_code make_error_code(portable_error err)
1309 {
1310 #ifdef GHC_OS_WINDOWS
1311 switch (err) {
1312 case portable_error::none:
1313 return std::error_code();
1314 case portable_error::exists:
1315 return std::error_code(ERROR_ALREADY_EXISTS, std::system_category());
1316 case portable_error::not_found:
1317 return std::error_code(ERROR_PATH_NOT_FOUND, std::system_category());
1318 case portable_error::not_supported:
1319 return std::error_code(ERROR_NOT_SUPPORTED, std::system_category());
1320 case portable_error::not_implemented:
1321 return std::error_code(ERROR_CALL_NOT_IMPLEMENTED, std::system_category());
1322 case portable_error::invalid_argument:
1323 return std::error_code(ERROR_INVALID_PARAMETER, std::system_category());
1324 case portable_error::is_a_directory:
1325 #ifdef ERROR_DIRECTORY_NOT_SUPPORTED
1326 return std::error_code(ERROR_DIRECTORY_NOT_SUPPORTED, std::system_category());
1327 #else
1328 return std::error_code(ERROR_NOT_SUPPORTED, std::system_category());
1329 #endif
1330 }
1331 #else
1332 switch (err) {
1333 case portable_error::none:
1334 return std::error_code();
1335 case portable_error::exists:
1336 return std::error_code(EEXIST, std::system_category());
1337 case portable_error::not_found:
1338 return std::error_code(ENOENT, std::system_category());
1339 case portable_error::not_supported:
1340 return std::error_code(ENOTSUP, std::system_category());
1341 case portable_error::not_implemented:
1342 return std::error_code(ENOSYS, std::system_category());
1343 case portable_error::invalid_argument:
1344 return std::error_code(EINVAL, std::system_category());
1345 case portable_error::is_a_directory:
1346 return std::error_code(EISDIR, std::system_category());
1347 }
1348 #endif
1349 return std::error_code();
1350 }
1351
1352 #ifdef GHC_OS_WINDOWS
1353 GHC_INLINE std::error_code make_system_error(uint32_t err)
1354 {
1355 return std::error_code(err ? static_cast<int>(err) : static_cast<int>(::GetLastError()), std::system_category());
1356 }
1357 #else
1358 GHC_INLINE std::error_code make_system_error(int err)
1359 {
1360 return std::error_code(err ? err : errno, std::system_category());
1361 }
1362 #endif
1363
1364 #endif // GHC_EXPAND_IMPL
1365
1366 template <typename Enum>
1367 using EnableBitmask = typename std::enable_if<std::is_same<Enum, perms>::value || std::is_same<Enum, perm_options>::value || std::is_same<Enum, copy_options>::value || std::is_same<Enum, directory_options>::value, Enum>::type;
1368 } // namespace detail
1369
1370 template <typename Enum>
1371 constexpr detail::EnableBitmask<Enum> operator&(Enum X, Enum Y)
1372 {
1373 using underlying = typename std::underlying_type<Enum>::type;
1374 return static_cast<Enum>(static_cast<underlying>(X) & static_cast<underlying>(Y));
1375 }
1376
1377 template <typename Enum>
1378 constexpr detail::EnableBitmask<Enum> operator|(Enum X, Enum Y)
1379 {
1380 using underlying = typename std::underlying_type<Enum>::type;
1381 return static_cast<Enum>(static_cast<underlying>(X) | static_cast<underlying>(Y));
1382 }
1383
1384 template <typename Enum>
1385 constexpr detail::EnableBitmask<Enum> operator^(Enum X, Enum Y)
1386 {
1387 using underlying = typename std::underlying_type<Enum>::type;
1388 return static_cast<Enum>(static_cast<underlying>(X) ^ static_cast<underlying>(Y));
1389 }
1390
1391 template <typename Enum>
1392 constexpr detail::EnableBitmask<Enum> operator~(Enum X)
1393 {
1394 using underlying = typename std::underlying_type<Enum>::type;
1395 return static_cast<Enum>(~static_cast<underlying>(X));
1396 }
1397
1398 template <typename Enum>
1399 detail::EnableBitmask<Enum>& operator&=(Enum& X, Enum Y)
1400 {
1401 X = X & Y;
1402 return X;
1403 }
1404
1405 template <typename Enum>
1406 detail::EnableBitmask<Enum>& operator|=(Enum& X, Enum Y)
1407 {
1408 X = X | Y;
1409 return X;
1410 }
1411
1412 template <typename Enum>
1413 detail::EnableBitmask<Enum>& operator^=(Enum& X, Enum Y)
1414 {
1415 X = X ^ Y;
1416 return X;
1417 }
1418
1419 #ifdef GHC_EXPAND_IMPL
1420
1421 namespace detail {
1422
1423 GHC_INLINE bool in_range(uint32_t c, uint32_t lo, uint32_t hi)
1424 {
1425 return (static_cast<uint32_t>(c - lo) < (hi - lo + 1));
1426 }
1427
1428 GHC_INLINE bool is_surrogate(uint32_t c)
1429 {
1430 return in_range(c, 0xd800, 0xdfff);
1431 }
1432
1433 GHC_INLINE bool is_high_surrogate(uint32_t c)
1434 {
1435 return (c & 0xfffffc00) == 0xd800;
1436 }
1437
1438 GHC_INLINE bool is_low_surrogate(uint32_t c)
1439 {
1440 return (c & 0xfffffc00) == 0xdc00;
1441 }
1442
1443 GHC_INLINE void appendUTF8(std::string& str, uint32_t unicode)
1444 {
1445 if (unicode <= 0x7f) {
1446 str.push_back(static_cast<char>(unicode));
1447 }
1448 else if (unicode >= 0x80 && unicode <= 0x7ff) {
1449 str.push_back(static_cast<char>((unicode >> 6) + 192));
1450 str.push_back(static_cast<char>((unicode & 0x3f) + 128));
1451 }
1452 else if ((unicode >= 0x800 && unicode <= 0xd7ff) || (unicode >= 0xe000 && unicode <= 0xffff)) {
1453 str.push_back(static_cast<char>((unicode >> 12) + 224));
1454 str.push_back(static_cast<char>(((unicode & 0xfff) >> 6) + 128));
1455 str.push_back(static_cast<char>((unicode & 0x3f) + 128));
1456 }
1457 else if (unicode >= 0x10000 && unicode <= 0x10ffff) {
1458 str.push_back(static_cast<char>((unicode >> 18) + 240));
1459 str.push_back(static_cast<char>(((unicode & 0x3ffff) >> 12) + 128));
1460 str.push_back(static_cast<char>(((unicode & 0xfff) >> 6) + 128));
1461 str.push_back(static_cast<char>((unicode & 0x3f) + 128));
1462 }
1463 else {
1464 #ifdef GHC_RAISE_UNICODE_ERRORS
1465 throw filesystem_error("Illegal code point for unicode character.", str, std::make_error_code(std::errc::illegal_byte_sequence));
1466 #else
1467 appendUTF8(str, 0xfffd);
1468 #endif
1469 }
1470 }
1471
1472 // Thanks to Bjoern Hoehrmann (https://bjoern.hoehrmann.de/utf-8/decoder/dfa/)
1473 // and Taylor R Campbell for the ideas to this DFA approach of UTF-8 decoding;
1474 // Generating debugging and shrinking my own DFA from scratch was a day of fun!
1475 GHC_INLINE unsigned consumeUtf8Fragment(const unsigned state, const uint8_t fragment, uint32_t& codepoint)
1476 {
1477 static const uint32_t utf8_state_info[] = {
1478 // encoded states
1479 0x11111111u, 0x11111111u, 0x77777777u, 0x77777777u, 0x88888888u, 0x88888888u, 0x88888888u, 0x88888888u, 0x22222299u, 0x22222222u, 0x22222222u, 0x22222222u, 0x3333333au, 0x33433333u, 0x9995666bu, 0x99999999u,
1480 0x88888880u, 0x22818108u, 0x88888881u, 0x88888882u, 0x88888884u, 0x88888887u, 0x88888886u, 0x82218108u, 0x82281108u, 0x88888888u, 0x88888883u, 0x88888885u, 0u, 0u, 0u, 0u,
1481 };
1482 uint8_t category = fragment < 128 ? 0 : (utf8_state_info[(fragment >> 3) & 0xf] >> ((fragment & 7) << 2)) & 0xf;
1483 codepoint = (state ? (codepoint << 6) | (fragment & 0x3fu) : (0xffu >> category) & fragment);
1484 return state == S_RJCT ? static_cast<unsigned>(S_RJCT) : static_cast<unsigned>((utf8_state_info[category + 16] >> (state << 2)) & 0xf);
1485 }
1486
1487 GHC_INLINE bool validUtf8(const std::string& utf8String)
1488 {
1489 std::string::const_iterator iter = utf8String.begin();
1490 unsigned utf8_state = S_STRT;
1491 std::uint32_t codepoint = 0;
1492 while (iter < utf8String.end()) {
1493 if ((utf8_state = consumeUtf8Fragment(utf8_state, static_cast<uint8_t>(*iter++), codepoint)) == S_RJCT) {
1494 return false;
1495 }
1496 }
1497 if (utf8_state) {
1498 return false;
1499 }
1500 return true;
1501 }
1502
1503 } // namespace detail
1504
1505 #endif
1506
1507 namespace detail {
1508
1509 template <class StringType, class Utf8String, typename std::enable_if<path::_is_basic_string<Utf8String>::value && (sizeof(typename Utf8String::value_type) == 1) && (sizeof(typename StringType::value_type) == 1)>::type* = nullptr>
1510 inline StringType fromUtf8(const Utf8String& utf8String, const typename StringType::allocator_type& alloc = typename StringType::allocator_type())
1511 {
1512 return StringType(utf8String.begin(), utf8String.end(), alloc);
1513 }
1514
1515 template <class StringType, class Utf8String, typename std::enable_if<path::_is_basic_string<Utf8String>::value && (sizeof(typename Utf8String::value_type) == 1) && (sizeof(typename StringType::value_type) == 2)>::type* = nullptr>
1516 inline StringType fromUtf8(const Utf8String& utf8String, const typename StringType::allocator_type& alloc = typename StringType::allocator_type())
1517 {
1518 StringType result(alloc);
1519 result.reserve(utf8String.length());
1520 auto iter = utf8String.cbegin();
1521 unsigned utf8_state = S_STRT;
1522 std::uint32_t codepoint = 0;
1523 while (iter < utf8String.cend()) {
1524 if ((utf8_state = consumeUtf8Fragment(utf8_state, static_cast<uint8_t>(*iter++), codepoint)) == S_STRT) {
1525 if (codepoint <= 0xffff) {
1526 result += static_cast<typename StringType::value_type>(codepoint);
1527 }
1528 else {
1529 codepoint -= 0x10000;
1530 result += static_cast<typename StringType::value_type>((codepoint >> 10) + 0xd800);
1531 result += static_cast<typename StringType::value_type>((codepoint & 0x3ff) + 0xdc00);
1532 }
1533 codepoint = 0;
1534 }
1535 else if (utf8_state == S_RJCT) {
1536 #ifdef GHC_RAISE_UNICODE_ERRORS
1537 throw filesystem_error("Illegal byte sequence for unicode character.", utf8String, std::make_error_code(std::errc::illegal_byte_sequence));
1538 #else
1539 result += static_cast<typename StringType::value_type>(0xfffd);
1540 utf8_state = S_STRT;
1541 codepoint = 0;
1542 #endif
1543 }
1544 }
1545 if (utf8_state) {
1546 #ifdef GHC_RAISE_UNICODE_ERRORS
1547 throw filesystem_error("Illegal byte sequence for unicode character.", utf8String, std::make_error_code(std::errc::illegal_byte_sequence));
1548 #else
1549 result += static_cast<typename StringType::value_type>(0xfffd);
1550 #endif
1551 }
1552 return result;
1553 }
1554
1555 template <class StringType, class Utf8String, typename std::enable_if<path::_is_basic_string<Utf8String>::value && (sizeof(typename Utf8String::value_type) == 1) && (sizeof(typename StringType::value_type) == 4)>::type* = nullptr>
1556 inline StringType fromUtf8(const Utf8String& utf8String, const typename StringType::allocator_type& alloc = typename StringType::allocator_type())
1557 {
1558 StringType result(alloc);
1559 result.reserve(utf8String.length());
1560 auto iter = utf8String.cbegin();
1561 unsigned utf8_state = S_STRT;
1562 std::uint32_t codepoint = 0;
1563 while (iter < utf8String.cend()) {
1564 if ((utf8_state = consumeUtf8Fragment(utf8_state, static_cast<uint8_t>(*iter++), codepoint)) == S_STRT) {
1565 result += static_cast<typename StringType::value_type>(codepoint);
1566 codepoint = 0;
1567 }
1568 else if (utf8_state == S_RJCT) {
1569 #ifdef GHC_RAISE_UNICODE_ERRORS
1570 throw filesystem_error("Illegal byte sequence for unicode character.", utf8String, std::make_error_code(std::errc::illegal_byte_sequence));
1571 #else
1572 result += static_cast<typename StringType::value_type>(0xfffd);
1573 utf8_state = S_STRT;
1574 codepoint = 0;
1575 #endif
1576 }
1577 }
1578 if (utf8_state) {
1579 #ifdef GHC_RAISE_UNICODE_ERRORS
1580 throw filesystem_error("Illegal byte sequence for unicode character.", utf8String, std::make_error_code(std::errc::illegal_byte_sequence));
1581 #else
1582 result += static_cast<typename StringType::value_type>(0xfffd);
1583 #endif
1584 }
1585 return result;
1586 }
1587
1588 template <class StringType, typename charT, std::size_t N>
1589 inline StringType fromUtf8(const charT (&utf8String)[N])
1590 {
1591 #ifdef GHC_WITH_STRING_VIEW
1592 return fromUtf8<StringType>(basic_string_view<charT>(utf8String, N - 1));
1593 #else
1594 return fromUtf8<StringType>(std::basic_string<charT>(utf8String, N - 1));
1595 #endif
1596 }
1597
1598 template <typename strT, typename std::enable_if<path::_is_basic_string<strT>::value && (sizeof(typename strT::value_type) == 1), int>::type size = 1>
1599 inline std::string toUtf8(const strT& unicodeString)
1600 {
1601 return std::string(unicodeString.begin(), unicodeString.end());
1602 }
1603
1604 template <typename strT, typename std::enable_if<path::_is_basic_string<strT>::value && (sizeof(typename strT::value_type) == 2), int>::type size = 2>
1605 inline std::string toUtf8(const strT& unicodeString)
1606 {
1607 std::string result;
1608 for (auto iter = unicodeString.begin(); iter != unicodeString.end(); ++iter) {
1609 char32_t c = *iter;
1610 if (is_surrogate(c)) {
1611 ++iter;
1612 if (iter != unicodeString.end() && is_high_surrogate(c) && is_low_surrogate(*iter)) {
1613 appendUTF8(result, (char32_t(c) << 10) + *iter - 0x35fdc00);
1614 }
1615 else {
1616 #ifdef GHC_RAISE_UNICODE_ERRORS
1617 throw filesystem_error("Illegal code point for unicode character.", result, std::make_error_code(std::errc::illegal_byte_sequence));
1618 #else
1619 appendUTF8(result, 0xfffd);
1620 if (iter == unicodeString.end()) {
1621 break;
1622 }
1623 #endif
1624 }
1625 }
1626 else {
1627 appendUTF8(result, c);
1628 }
1629 }
1630 return result;
1631 }
1632
1633 template <typename strT, typename std::enable_if<path::_is_basic_string<strT>::value && (sizeof(typename strT::value_type) == 4), int>::type size = 4>
1634 inline std::string toUtf8(const strT& unicodeString)
1635 {
1636 std::string result;
1637 for (auto c : unicodeString) {
1638 appendUTF8(result, static_cast<uint32_t>(c));
1639 }
1640 return result;
1641 }
1642
1643 template <typename charT>
1644 inline std::string toUtf8(const charT* unicodeString)
1645 {
1646 #ifdef GHC_WITH_STRING_VIEW
1647 return toUtf8(basic_string_view<charT, std::char_traits<charT>>(unicodeString));
1648 #else
1649 return toUtf8(std::basic_string<charT, std::char_traits<charT>>(unicodeString));
1650 #endif
1651 }
1652
1653 #ifdef GHC_USE_WCHAR_T
1654 template <class StringType, class WString, typename std::enable_if<path::_is_basic_string<WString>::value && (sizeof(typename WString::value_type) == 2) && (sizeof(typename StringType::value_type) == 1), bool>::type = false>
1655 inline StringType fromWChar(const WString& wString, const typename StringType::allocator_type& alloc = typename StringType::allocator_type())
1656 {
1657 auto temp = toUtf8(wString);
1658 return StringType(temp.begin(), temp.end(), alloc);
1659 }
1660
1661 template <class StringType, class WString, typename std::enable_if<path::_is_basic_string<WString>::value && (sizeof(typename WString::value_type) == 2) && (sizeof(typename StringType::value_type) == 2), bool>::type = false>
1662 inline StringType fromWChar(const WString& wString, const typename StringType::allocator_type& alloc = typename StringType::allocator_type())
1663 {
1664 return StringType(wString.begin(), wString.end(), alloc);
1665 }
1666
1667 template <class StringType, class WString, typename std::enable_if<path::_is_basic_string<WString>::value && (sizeof(typename WString::value_type) == 2) && (sizeof(typename StringType::value_type) == 4), bool>::type = false>
1668 inline StringType fromWChar(const WString& wString, const typename StringType::allocator_type& alloc = typename StringType::allocator_type())
1669 {
1670 auto temp = toUtf8(wString);
1671 return fromUtf8<StringType>(temp, alloc);
1672 }
1673
1674 template <typename strT, typename std::enable_if<path::_is_basic_string<strT>::value && (sizeof(typename strT::value_type) == 1), bool>::type = false>
1675 inline std::wstring toWChar(const strT& unicodeString)
1676 {
1677 return fromUtf8<std::wstring>(unicodeString);
1678 }
1679
1680 template <typename strT, typename std::enable_if<path::_is_basic_string<strT>::value && (sizeof(typename strT::value_type) == 2), bool>::type = false>
1681 inline std::wstring toWChar(const strT& unicodeString)
1682 {
1683 return std::wstring(unicodeString.begin(), unicodeString.end());
1684 }
1685
1686 template <typename strT, typename std::enable_if<path::_is_basic_string<strT>::value && (sizeof(typename strT::value_type) == 4), bool>::type = false>
1687 inline std::wstring toWChar(const strT& unicodeString)
1688 {
1689 auto temp = toUtf8(unicodeString);
1690 return fromUtf8<std::wstring>(temp);
1691 }
1692
1693 template <typename charT>
1694 inline std::wstring toWChar(const charT* unicodeString)
1695 {
1696 #ifdef GHC_WITH_STRING_VIEW
1697 return toWChar(basic_string_view<charT, std::char_traits<charT>>(unicodeString));
1698 #else
1699 return toWChar(std::basic_string<charT, std::char_traits<charT>>(unicodeString));
1700 #endif
1701 }
1702 #endif // GHC_USE_WCHAR_T
1703
1704 } // namespace detail
1705
1706 #ifdef GHC_EXPAND_IMPL
1707
1708 namespace detail {
1709
1710 template <typename strT, typename std::enable_if<path::_is_basic_string<strT>::value, bool>::type = true>
1711 GHC_INLINE bool startsWith(const strT& what, const strT& with)
1712 {
1713 return with.length() <= what.length() && equal(with.begin(), with.end(), what.begin());
1714 }
1715
1716 template <typename strT, typename std::enable_if<path::_is_basic_string<strT>::value, bool>::type = true>
1717 GHC_INLINE bool endsWith(const strT& what, const strT& with)
1718 {
1719 return with.length() <= what.length() && what.compare(what.length() - with.length(), with.size(), with) == 0;
1720 }
1721
1722 } // namespace detail
1723
1724 GHC_INLINE void path::check_long_path()
1725 {
1726 #if defined(GHC_OS_WINDOWS) && defined(GHC_WIN_AUTO_PREFIX_LONG_PATH)
1727 if (is_absolute() && _path.length() >= MAX_PATH - 12 && !detail::startsWith(_path, impl_string_type(GHC_PLATFORM_LITERAL("\\\\?\\")))) {
1728 postprocess_path_with_format(native_format);
1729 }
1730 #endif
1731 }
1732
1733 GHC_INLINE void path::postprocess_path_with_format(path::format fmt)
1734 {
1735 #ifdef GHC_RAISE_UNICODE_ERRORS
1736 if (!detail::validUtf8(_path)) {
1737 path t;
1738 t._path = _path;
1739 throw filesystem_error("Illegal byte sequence for unicode character.", t, std::make_error_code(std::errc::illegal_byte_sequence));
1740 }
1741 #endif
1742 switch (fmt) {
1743 #ifdef GHC_OS_WINDOWS
1744 case path::native_format:
1745 case path::auto_format:
1746 case path::generic_format:
1747 for (auto& c : _path) {
1748 if (c == generic_separator) {
1749 c = preferred_separator;
1750 }
1751 }
1752 #ifdef GHC_WIN_AUTO_PREFIX_LONG_PATH
1753 if (is_absolute() && _path.length() >= MAX_PATH - 12 && !detail::startsWith(_path, impl_string_type(GHC_PLATFORM_LITERAL("\\\\?\\")))) {
1754 _path = GHC_PLATFORM_LITERAL("\\\\?\\") + _path;
1755 }
1756 #endif
1757 handle_prefixes();
1758 break;
1759 #else
1760 case path::auto_format:
1761 case path::native_format:
1762 case path::generic_format:
1763 // nothing to do
1764 break;
1765 #endif
1766 }
1767 if (_path.length() > _prefixLength + 2 && _path[_prefixLength] == preferred_separator && _path[_prefixLength + 1] == preferred_separator && _path[_prefixLength + 2] != preferred_separator) {
1768 impl_string_type::iterator new_end = std::unique(_path.begin() + static_cast<string_type::difference_type>(_prefixLength) + 2, _path.end(), [](path::value_type lhs, path::value_type rhs) { return lhs == rhs && lhs == preferred_separator; });
1769 _path.erase(new_end, _path.end());
1770 }
1771 else {
1772 impl_string_type::iterator new_end = std::unique(_path.begin() + static_cast<string_type::difference_type>(_prefixLength), _path.end(), [](path::value_type lhs, path::value_type rhs) { return lhs == rhs && lhs == preferred_separator; });
1773 _path.erase(new_end, _path.end());
1774 }
1775 }
1776
1777 #endif // GHC_EXPAND_IMPL
1778
1779 template <class Source, typename>
1780 inline path::path(const Source& source, format fmt)
1781 #ifdef GHC_USE_WCHAR_T
1782 : _path(detail::toWChar(source))
1783 #else
1784 : _path(detail::toUtf8(source))
1785 #endif
1786 {
1787 postprocess_path_with_format(fmt);
1788 }
1789
1790 template <class Source, typename>
1791 inline path u8path(const Source& source)
1792 {
1793 return path(source);
1794 }
1795 template <class InputIterator>
1796 inline path u8path(InputIterator first, InputIterator last)
1797 {
1798 return path(first, last);
1799 }
1800
1801 template <class InputIterator>
1802 inline path::path(InputIterator first, InputIterator last, format fmt)
1803 : path(std::basic_string<typename std::iterator_traits<InputIterator>::value_type>(first, last), fmt)
1804 {
1805 // delegated
1806 }
1807
1808 #ifdef GHC_EXPAND_IMPL
1809
1810 namespace detail {
1811
1812 GHC_INLINE bool equals_simple_insensitive(const path::value_type* str1, const path::value_type* str2)
1813 {
1814 #ifdef GHC_OS_WINDOWS
1815 #ifdef __GNUC__
1816 while (::tolower((unsigned char)*str1) == ::tolower((unsigned char)*str2++)) {
1817 if (*str1++ == 0)
1818 return true;
1819 }
1820 return false;
1821 #else // __GNUC__
1822 #ifdef GHC_USE_WCHAR_T
1823 return 0 == ::_wcsicmp(str1, str2);
1824 #else // GHC_USE_WCHAR_T
1825 return 0 == ::_stricmp(str1, str2);
1826 #endif // GHC_USE_WCHAR_T
1827 #endif // __GNUC__
1828 #else // GHC_OS_WINDOWS
1829 return 0 == ::strcasecmp(str1, str2);
1830 #endif // GHC_OS_WINDOWS
1831 }
1832
1833 GHC_INLINE int compare_simple_insensitive(const path::value_type* str1, size_t len1, const path::value_type* str2, size_t len2)
1834 {
1835 while (len1 > 0 && len2 > 0 && ::tolower(static_cast<unsigned char>(*str1)) == ::tolower(static_cast<unsigned char>(*str2))) {
1836 --len1;
1837 --len2;
1838 ++str1;
1839 ++str2;
1840 }
1841 if (len1 && len2) {
1842 return *str1 < *str2 ? -1 : 1;
1843 }
1844 if (len1 == 0 && len2 == 0) {
1845 return 0;
1846 }
1847 return len1 == 0 ? -1 : 1;
1848 }
1849
1850 GHC_INLINE const char* strerror_adapter(char* gnu, char*)
1851 {
1852 return gnu;
1853 }
1854
1855 GHC_INLINE const char* strerror_adapter(int posix, char* buffer)
1856 {
1857 if (posix) {
1858 return "Error in strerror_r!";
1859 }
1860 return buffer;
1861 }
1862
1863 template <typename ErrorNumber>
1864 GHC_INLINE std::string systemErrorText(ErrorNumber code = 0)
1865 {
1866 #if defined(GHC_OS_WINDOWS)
1867 LPVOID msgBuf;
1868 DWORD dw = code ? static_cast<DWORD>(code) : ::GetLastError();
1869 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&msgBuf, 0, NULL);
1870 std::string msg = toUtf8(std::wstring((LPWSTR)msgBuf));
1871 LocalFree(msgBuf);
1872 return msg;
1873 #else
1874 char buffer[512];
1875 return strerror_adapter(strerror_r(code ? code : errno, buffer, sizeof(buffer)), buffer);
1876 #endif
1877 }
1878
1879 #ifdef GHC_OS_WINDOWS
1880 using CreateSymbolicLinkW_fp = BOOLEAN(WINAPI*)(LPCWSTR, LPCWSTR, DWORD);
1881 using CreateHardLinkW_fp = BOOLEAN(WINAPI*)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
1882
1883 GHC_INLINE void create_symlink(const path& target_name, const path& new_symlink, bool to_directory, std::error_code& ec)
1884 {
1885 std::error_code tec;
1886 auto fs = status(target_name, tec);
1887 if ((fs.type() == file_type::directory && !to_directory) || (fs.type() == file_type::regular && to_directory)) {
1888 ec = detail::make_error_code(detail::portable_error::not_supported);
1889 return;
1890 }
1891 #if defined(__GNUC__) && __GNUC__ >= 8
1892 #pragma GCC diagnostic push
1893 #pragma GCC diagnostic ignored "-Wcast-function-type"
1894 #endif
1895 static CreateSymbolicLinkW_fp api_call = reinterpret_cast<CreateSymbolicLinkW_fp>(GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "CreateSymbolicLinkW"));
1896 #if defined(__GNUC__) && __GNUC__ >= 8
1897 #pragma GCC diagnostic pop
1898 #endif
1899 if (api_call) {
1900 if (api_call(GHC_NATIVEWP(new_symlink), GHC_NATIVEWP(target_name), to_directory ? 1 : 0) == 0) {
1901 auto result = ::GetLastError();
1902 if (result == ERROR_PRIVILEGE_NOT_HELD && api_call(GHC_NATIVEWP(new_symlink), GHC_NATIVEWP(target_name), to_directory ? 3 : 2) != 0) {
1903 return;
1904 }
1905 ec = detail::make_system_error(result);
1906 }
1907 }
1908 else {
1909 ec = detail::make_system_error(ERROR_NOT_SUPPORTED);
1910 }
1911 }
1912
1913 GHC_INLINE void create_hardlink(const path& target_name, const path& new_hardlink, std::error_code& ec)
1914 {
1915 #if defined(__GNUC__) && __GNUC__ >= 8
1916 #pragma GCC diagnostic push
1917 #pragma GCC diagnostic ignored "-Wcast-function-type"
1918 #endif
1919 static CreateHardLinkW_fp api_call = reinterpret_cast<CreateHardLinkW_fp>(GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "CreateHardLinkW"));
1920 #if defined(__GNUC__) && __GNUC__ >= 8
1921 #pragma GCC diagnostic pop
1922 #endif
1923 if (api_call) {
1924 if (api_call(GHC_NATIVEWP(new_hardlink), GHC_NATIVEWP(target_name), NULL) == 0) {
1925 ec = detail::make_system_error();
1926 }
1927 }
1928 else {
1929 ec = detail::make_system_error(ERROR_NOT_SUPPORTED);
1930 }
1931 }
1932
1933 GHC_INLINE path getFullPathName(const wchar_t* p, std::error_code& ec)
1934 {
1935 ULONG size = ::GetFullPathNameW(p, 0, 0, 0);
1936 if (size) {
1937 std::vector<wchar_t> buf(size, 0);
1938 ULONG s2 = GetFullPathNameW(p, size, buf.data(), nullptr);
1939 if (s2 && s2 < size) {
1940 return path(std::wstring(buf.data(), s2));
1941 }
1942 }
1943 ec = detail::make_system_error();
1944 return path();
1945 }
1946
1947 #else
1948 GHC_INLINE void create_symlink(const path& target_name, const path& new_symlink, bool, std::error_code& ec)
1949 {
1950 if (::symlink(target_name.c_str(), new_symlink.c_str()) != 0) {
1951 ec = detail::make_system_error();
1952 }
1953 }
1954
1955 #ifndef GHC_OS_WEB
1956 GHC_INLINE void create_hardlink(const path& target_name, const path& new_hardlink, std::error_code& ec)
1957 {
1958 if (::link(target_name.c_str(), new_hardlink.c_str()) != 0) {
1959 ec = detail::make_system_error();
1960 }
1961 }
1962 #endif
1963 #endif
1964
1965 template <typename T>
1966 GHC_INLINE file_status file_status_from_st_mode(T mode)
1967 {
1968 #ifdef GHC_OS_WINDOWS
1969 file_type ft = file_type::unknown;
1970 if ((mode & _S_IFDIR) == _S_IFDIR) {
1971 ft = file_type::directory;
1972 }
1973 else if ((mode & _S_IFREG) == _S_IFREG) {
1974 ft = file_type::regular;
1975 }
1976 else if ((mode & _S_IFCHR) == _S_IFCHR) {
1977 ft = file_type::character;
1978 }
1979 perms prms = static_cast<perms>(mode & 0xfff);
1980 return file_status(ft, prms);
1981 #else
1982 file_type ft = file_type::unknown;
1983 if (S_ISDIR(mode)) {
1984 ft = file_type::directory;
1985 }
1986 else if (S_ISREG(mode)) {
1987 ft = file_type::regular;
1988 }
1989 else if (S_ISCHR(mode)) {
1990 ft = file_type::character;
1991 }
1992 else if (S_ISBLK(mode)) {
1993 ft = file_type::block;
1994 }
1995 else if (S_ISFIFO(mode)) {
1996 ft = file_type::fifo;
1997 }
1998 else if (S_ISLNK(mode)) {
1999 ft = file_type::symlink;
2000 }
2001 else if (S_ISSOCK(mode)) {
2002 ft = file_type::socket;
2003 }
2004 perms prms = static_cast<perms>(mode & 0xfff);
2005 return file_status(ft, prms);
2006 #endif
2007 }
2008
2009 #ifdef GHC_OS_WINDOWS
2010
2011 class unique_handle
2012 {
2013 public:
2014 typedef HANDLE element_type;
2015
2016 unique_handle() noexcept
2017 : _handle(INVALID_HANDLE_VALUE)
2018 {
2019 }
2020 explicit unique_handle(element_type h) noexcept
2021 : _handle(h)
2022 {
2023 }
2024 unique_handle(unique_handle&& u) noexcept
2025 : _handle(u.release())
2026 {
2027 }
2028 ~unique_handle() { reset(); }
2029 unique_handle& operator=(unique_handle&& u) noexcept
2030 {
2031 reset(u.release());
2032 return *this;
2033 }
2034 element_type get() const noexcept { return _handle; }
2035 explicit operator bool() const noexcept { return _handle != INVALID_HANDLE_VALUE; }
2036 element_type release() noexcept
2037 {
2038 element_type tmp = _handle;
2039 _handle = INVALID_HANDLE_VALUE;
2040 return tmp;
2041 }
2042 void reset(element_type h = INVALID_HANDLE_VALUE) noexcept
2043 {
2044 element_type tmp = _handle;
2045 _handle = h;
2046 if (tmp != INVALID_HANDLE_VALUE) {
2047 CloseHandle(tmp);
2048 }
2049 }
2050 void swap(unique_handle& u) noexcept { std::swap(_handle, u._handle); }
2051
2052 private:
2053 element_type _handle;
2054 };
2055
2056 #ifndef REPARSE_DATA_BUFFER_HEADER_SIZE
2057 typedef struct _REPARSE_DATA_BUFFER
2058 {
2059 ULONG ReparseTag;
2060 USHORT ReparseDataLength;
2061 USHORT Reserved;
2062 union
2063 {
2064 struct
2065 {
2066 USHORT SubstituteNameOffset;
2067 USHORT SubstituteNameLength;
2068 USHORT PrintNameOffset;
2069 USHORT PrintNameLength;
2070 ULONG Flags;
2071 WCHAR PathBuffer[1];
2072 } SymbolicLinkReparseBuffer;
2073 struct
2074 {
2075 USHORT SubstituteNameOffset;
2076 USHORT SubstituteNameLength;
2077 USHORT PrintNameOffset;
2078 USHORT PrintNameLength;
2079 WCHAR PathBuffer[1];
2080 } MountPointReparseBuffer;
2081 struct
2082 {
2083 UCHAR DataBuffer[1];
2084 } GenericReparseBuffer;
2085 } DUMMYUNIONNAME;
2086 } REPARSE_DATA_BUFFER;
2087 #ifndef MAXIMUM_REPARSE_DATA_BUFFER_SIZE
2088 #define MAXIMUM_REPARSE_DATA_BUFFER_SIZE (16 * 1024)
2089 #endif
2090 #endif
2091
2092 template <class T>
2093 struct free_deleter
2094 {
2095 void operator()(T* p) const { std::free(p); }
2096 };
2097
2098 GHC_INLINE std::unique_ptr<REPARSE_DATA_BUFFER, free_deleter<REPARSE_DATA_BUFFER>> getReparseData(const path& p, std::error_code& ec)
2099 {
2100 unique_handle file(CreateFileW(GHC_NATIVEWP(p), 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, 0));
2101 if (!file) {
2102 ec = detail::make_system_error();
2103 return nullptr;
2104 }
2105
2106 std::unique_ptr<REPARSE_DATA_BUFFER, free_deleter<REPARSE_DATA_BUFFER>> reparseData(reinterpret_cast<REPARSE_DATA_BUFFER*>(std::calloc(1, MAXIMUM_REPARSE_DATA_BUFFER_SIZE)));
2107 ULONG bufferUsed;
2108 if (DeviceIoControl(file.get(), FSCTL_GET_REPARSE_POINT, 0, 0, reparseData.get(), MAXIMUM_REPARSE_DATA_BUFFER_SIZE, &bufferUsed, 0)) {
2109 return reparseData;
2110 }
2111 else {
2112 ec = detail::make_system_error();
2113 }
2114 return nullptr;
2115 }
2116 #endif
2117
2118 GHC_INLINE path resolveSymlink(const path& p, std::error_code& ec)
2119 {
2120 #ifdef GHC_OS_WINDOWS
2121 path result;
2122 auto reparseData = detail::getReparseData(p, ec);
2123 if (!ec) {
2124 if (reparseData && IsReparseTagMicrosoft(reparseData->ReparseTag)) {
2125 switch (reparseData->ReparseTag) {
2126 case IO_REPARSE_TAG_SYMLINK: {
2127 auto printName = std::wstring(&reparseData->SymbolicLinkReparseBuffer.PathBuffer[reparseData->SymbolicLinkReparseBuffer.PrintNameOffset / sizeof(WCHAR)], reparseData->SymbolicLinkReparseBuffer.PrintNameLength / sizeof(WCHAR));
2128 auto substituteName =
2129 std::wstring(&reparseData->SymbolicLinkReparseBuffer.PathBuffer[reparseData->SymbolicLinkReparseBuffer.SubstituteNameOffset / sizeof(WCHAR)], reparseData->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(WCHAR));
2130 if (detail::endsWith(substituteName, printName) && detail::startsWith(substituteName, std::wstring(L"\\??\\"))) {
2131 result = printName;
2132 }
2133 else {
2134 result = substituteName;
2135 }
2136 if (reparseData->SymbolicLinkReparseBuffer.Flags & 0x1 /*SYMLINK_FLAG_RELATIVE*/) {
2137 result = p.parent_path() / result;
2138 }
2139 break;
2140 }
2141 case IO_REPARSE_TAG_MOUNT_POINT:
2142 result = detail::getFullPathName(GHC_NATIVEWP(p), ec);
2143 // result = std::wstring(&reparseData->MountPointReparseBuffer.PathBuffer[reparseData->MountPointReparseBuffer.SubstituteNameOffset / sizeof(WCHAR)], reparseData->MountPointReparseBuffer.SubstituteNameLength / sizeof(WCHAR));
2144 break;
2145 default:
2146 break;
2147 }
2148 }
2149 }
2150 return result;
2151 #else
2152 size_t bufferSize = 256;
2153 while (true) {
2154 std::vector<char> buffer(bufferSize, static_cast<char>(0));
2155 auto rc = ::readlink(p.c_str(), buffer.data(), buffer.size());
2156 if (rc < 0) {
2157 ec = detail::make_system_error();
2158 return path();
2159 }
2160 else if (rc < static_cast<int>(bufferSize)) {
2161 return path(std::string(buffer.data(), static_cast<std::string::size_type>(rc)));
2162 }
2163 bufferSize *= 2;
2164 }
2165 return path();
2166 #endif
2167 }
2168
2169 #ifdef GHC_OS_WINDOWS
2170 GHC_INLINE time_t timeFromFILETIME(const FILETIME& ft)
2171 {
2172 ULARGE_INTEGER ull;
2173 ull.LowPart = ft.dwLowDateTime;
2174 ull.HighPart = ft.dwHighDateTime;
2175 return static_cast<time_t>(ull.QuadPart / 10000000ULL - 11644473600ULL);
2176 }
2177
2178 GHC_INLINE void timeToFILETIME(time_t t, FILETIME& ft)
2179 {
2180 LONGLONG ll;
2181 ll = Int32x32To64(t, 10000000) + 116444736000000000;
2182 ft.dwLowDateTime = static_cast<DWORD>(ll);
2183 ft.dwHighDateTime = static_cast<DWORD>(ll >> 32);
2184 }
2185
2186 template <typename INFO>
2187 GHC_INLINE uintmax_t hard_links_from_INFO(const INFO* info)
2188 {
2189 return static_cast<uintmax_t>(-1);
2190 }
2191
2192 template <>
2193 GHC_INLINE uintmax_t hard_links_from_INFO<BY_HANDLE_FILE_INFORMATION>(const BY_HANDLE_FILE_INFORMATION* info)
2194 {
2195 return info->nNumberOfLinks;
2196 }
2197
2198 template <typename INFO>
2199 GHC_INLINE DWORD reparse_tag_from_INFO(const INFO*)
2200 {
2201 return 0;
2202 }
2203
2204 template <>
2205 GHC_INLINE DWORD reparse_tag_from_INFO(const WIN32_FIND_DATAW* info)
2206 {
2207 return info->dwReserved0;
2208 }
2209
2210 template <typename INFO>
2211 GHC_INLINE file_status status_from_INFO(const path& p, const INFO* info, std::error_code& ec, uintmax_t* sz = nullptr, time_t* lwt = nullptr)
2212 {
2213 file_type ft = file_type::unknown;
2214 if (sizeof(INFO) == sizeof(WIN32_FIND_DATAW)) {
2215 if (detail::reparse_tag_from_INFO(info) == IO_REPARSE_TAG_SYMLINK) {
2216 ft = file_type::symlink;
2217 }
2218 }
2219 else {
2220 if ((info->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
2221 auto reparseData = detail::getReparseData(p, ec);
2222 if (!ec && reparseData && IsReparseTagMicrosoft(reparseData->ReparseTag) && reparseData->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
2223 ft = file_type::symlink;
2224 }
2225 }
2226 }
2227 if (ft == file_type::unknown) {
2228 if ((info->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
2229 ft = file_type::directory;
2230 }
2231 else {
2232 ft = file_type::regular;
2233 }
2234 }
2235 perms prms = perms::owner_read | perms::group_read | perms::others_read;
2236 if (!(info->dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
2237 prms = prms | perms::owner_write | perms::group_write | perms::others_write;
2238 }
2239 if (has_executable_extension(p)) {
2240 prms = prms | perms::owner_exec | perms::group_exec | perms::others_exec;
2241 }
2242 if (sz) {
2243 *sz = static_cast<uintmax_t>(info->nFileSizeHigh) << (sizeof(info->nFileSizeHigh) * 8) | info->nFileSizeLow;
2244 }
2245 if (lwt) {
2246 *lwt = detail::timeFromFILETIME(info->ftLastWriteTime);
2247 }
2248 return file_status(ft, prms);
2249 }
2250
2251 #endif
2252
2253 GHC_INLINE bool is_not_found_error(std::error_code& ec)
2254 {
2255 #ifdef GHC_OS_WINDOWS
2256 return ec.value() == ERROR_FILE_NOT_FOUND || ec.value() == ERROR_PATH_NOT_FOUND || ec.value() == ERROR_INVALID_NAME;
2257 #else
2258 return ec.value() == ENOENT || ec.value() == ENOTDIR;
2259 #endif
2260 }
2261
2262 GHC_INLINE file_status symlink_status_ex(const path& p, std::error_code& ec, uintmax_t* sz = nullptr, uintmax_t* nhl = nullptr, time_t* lwt = nullptr) noexcept
2263 {
2264 #ifdef GHC_OS_WINDOWS
2265 file_status fs;
2266 WIN32_FILE_ATTRIBUTE_DATA attr;
2267 if (!GetFileAttributesExW(GHC_NATIVEWP(p), GetFileExInfoStandard, &attr)) {
2268 ec = detail::make_system_error();
2269 }
2270 else {
2271 ec.clear();
2272 fs = detail::status_from_INFO(p, &attr, ec, sz, lwt);
2273 if (nhl) {
2274 *nhl = 0;
2275 }
2276 }
2277 if (detail::is_not_found_error(ec)) {
2278 return file_status(file_type::not_found);
2279 }
2280 return ec ? file_status(file_type::none) : fs;
2281 #else
2282 (void)sz;
2283 (void)nhl;
2284 (void)lwt;
2285 struct ::stat fs;
2286 auto result = ::lstat(p.c_str(), &fs);
2287 if (result == 0) {
2288 ec.clear();
2289 file_status f_s = detail::file_status_from_st_mode(fs.st_mode);
2290 return f_s;
2291 }
2292 ec = detail::make_system_error();
2293 if (detail::is_not_found_error(ec)) {
2294 return file_status(file_type::not_found, perms::unknown);
2295 }
2296 return file_status(file_type::none);
2297 #endif
2298 }
2299
2300 GHC_INLINE file_status status_ex(const path& p, std::error_code& ec, file_status* sls = nullptr, uintmax_t* sz = nullptr, uintmax_t* nhl = nullptr, time_t* lwt = nullptr, int recurse_count = 0) noexcept
2301 {
2302 ec.clear();
2303 #ifdef GHC_OS_WINDOWS
2304 if (recurse_count > 16) {
2305 ec = detail::make_system_error(0x2A9 /*ERROR_STOPPED_ON_SYMLINK*/);
2306 return file_status(file_type::unknown);
2307 }
2308 WIN32_FILE_ATTRIBUTE_DATA attr;
2309 if (!::GetFileAttributesExW(GHC_NATIVEWP(p), GetFileExInfoStandard, &attr)) {
2310 ec = detail::make_system_error();
2311 }
2312 else if (attr.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2313 auto reparseData = detail::getReparseData(p, ec);
2314 if (!ec && reparseData && IsReparseTagMicrosoft(reparseData->ReparseTag) && reparseData->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
2315 path target = resolveSymlink(p, ec);
2316 file_status result;
2317 if (!ec && !target.empty()) {
2318 if (sls) {
2319 *sls = status_from_INFO(p, &attr, ec);
2320 }
2321 return detail::status_ex(target, ec, nullptr, sz, nhl, lwt, recurse_count + 1);
2322 }
2323 return file_status(file_type::unknown);
2324 }
2325 }
2326 if (ec) {
2327 if (detail::is_not_found_error(ec)) {
2328 return file_status(file_type::not_found);
2329 }
2330 return file_status(file_type::none);
2331 }
2332 if (nhl) {
2333 *nhl = 0;
2334 }
2335 return detail::status_from_INFO(p, &attr, ec, sz, lwt);
2336 #else
2337 (void)recurse_count;
2338 struct ::stat st;
2339 auto result = ::lstat(p.c_str(), &st);
2340 if (result == 0) {
2341 ec.clear();
2342 file_status fs = detail::file_status_from_st_mode(st.st_mode);
2343 if (sls) {
2344 *sls = fs;
2345 }
2346 if (fs.type() == file_type::symlink) {
2347 result = ::stat(p.c_str(), &st);
2348 if (result == 0) {
2349 fs = detail::file_status_from_st_mode(st.st_mode);
2350 }
2351 else {
2352 ec = detail::make_system_error();
2353 if (detail::is_not_found_error(ec)) {
2354 return file_status(file_type::not_found, perms::unknown);
2355 }
2356 return file_status(file_type::none);
2357 }
2358 }
2359 if (sz) {
2360 *sz = static_cast<uintmax_t>(st.st_size);
2361 }
2362 if (nhl) {
2363 *nhl = st.st_nlink;
2364 }
2365 if (lwt) {
2366 *lwt = st.st_mtime;
2367 }
2368 return fs;
2369 }
2370 else {
2371 ec = detail::make_system_error();
2372 if (detail::is_not_found_error(ec)) {
2373 return file_status(file_type::not_found, perms::unknown);
2374 }
2375 return file_status(file_type::none);
2376 }
2377 #endif
2378 }
2379
2380 } // namespace detail
2381
2382 GHC_INLINE u8arguments::u8arguments(int& argc, char**& argv)
2383 : _argc(argc)
2384 , _argv(argv)
2385 , _refargc(argc)
2386 , _refargv(argv)
2387 , _isvalid(false)
2388 {
2389 #ifdef GHC_OS_WINDOWS
2390 LPWSTR* p;
2391 p = ::CommandLineToArgvW(::GetCommandLineW(), &argc);
2392 _args.reserve(static_cast<size_t>(argc));
2393 _argp.reserve(static_cast<size_t>(argc));
2394 for (size_t i = 0; i < static_cast<size_t>(argc); ++i) {
2395 _args.push_back(detail::toUtf8(std::wstring(p[i])));
2396 _argp.push_back((char*)_args[i].data());
2397 }
2398 argv = _argp.data();
2399 ::LocalFree(p);
2400 _isvalid = true;
2401 #else
2402 std::setlocale(LC_ALL, "");
2403 #if defined(__ANDROID__) && __ANDROID_API__ < 26
2404 _isvalid = true;
2405 #else
2406 if (detail::equals_simple_insensitive(::nl_langinfo(CODESET), "UTF-8")) {
2407 _isvalid = true;
2408 }
2409 #endif
2410 #endif
2411 }
2412
2413 //-----------------------------------------------------------------------------
2414 // [fs.path.construct] constructors and destructor
2415
2416 GHC_INLINE path::path() noexcept {}
2417
2418 GHC_INLINE path::path(const path& p)
2419 : _path(p._path)
2420 #if defined(GHC_OS_WINDOWS) && defined(GHC_WIN_AUTO_PREFIX_LONG_PATH)
2421 , _prefixLength(p._prefixLength)
2422 #endif
2423 {
2424 }
2425
2426 GHC_INLINE path::path(path&& p) noexcept
2427 : _path(std::move(p._path))
2428 #if defined(GHC_OS_WINDOWS) && defined(GHC_WIN_AUTO_PREFIX_LONG_PATH)
2429 , _prefixLength(p._prefixLength)
2430 #endif
2431 {
2432 }
2433
2434 GHC_INLINE path::path(string_type&& source, format fmt)
2435 : _path(std::move(source))
2436 {
2437 postprocess_path_with_format(fmt);
2438 }
2439
2440 #endif // GHC_EXPAND_IMPL
2441
2442 #ifdef GHC_WITH_EXCEPTIONS
2443 template <class Source, typename>
2444 inline path::path(const Source& source, const std::locale& loc, format fmt)
2445 : path(source, fmt)
2446 {
2447 std::string locName = loc.name();
2448 if (!(locName.length() >= 5 && (locName.substr(locName.length() - 5) == "UTF-8" || locName.substr(locName.length() - 5) == "utf-8"))) {
2449 throw filesystem_error("This implementation only supports UTF-8 locales!", path(_path), detail::make_error_code(detail::portable_error::not_supported));
2450 }
2451 }
2452
2453 template <class InputIterator>
2454 inline path::path(InputIterator first, InputIterator last, const std::locale& loc, format fmt)
2455 : path(std::basic_string<typename std::iterator_traits<InputIterator>::value_type>(first, last), fmt)
2456 {
2457 std::string locName = loc.name();
2458 if (!(locName.length() >= 5 && (locName.substr(locName.length() - 5) == "UTF-8" || locName.substr(locName.length() - 5) == "utf-8"))) {
2459 throw filesystem_error("This implementation only supports UTF-8 locales!", path(_path), detail::make_error_code(detail::portable_error::not_supported));
2460 }
2461 }
2462 #endif
2463
2464 #ifdef GHC_EXPAND_IMPL
2465
2466 GHC_INLINE path::~path() {}
2467
2468 //-----------------------------------------------------------------------------
2469 // [fs.path.assign] assignments
2470
2471 GHC_INLINE path& path::operator=(const path& p)
2472 {
2473 _path = p._path;
2474 #if defined(GHC_OS_WINDOWS) && defined(GHC_WIN_AUTO_PREFIX_LONG_PATH)
2475 _prefixLength = p._prefixLength;
2476 #endif
2477 return *this;
2478 }
2479
2480 GHC_INLINE path& path::operator=(path&& p) noexcept
2481 {
2482 _path = std::move(p._path);
2483 #if defined(GHC_OS_WINDOWS) && defined(GHC_WIN_AUTO_PREFIX_LONG_PATH)
2484 _prefixLength = p._prefixLength;
2485 #endif
2486 return *this;
2487 }
2488
2489 GHC_INLINE path& path::operator=(path::string_type&& source)
2490 {
2491 return assign(source);
2492 }
2493
2494 GHC_INLINE path& path::assign(path::string_type&& source)
2495 {
2496 _path = std::move(source);
2497 postprocess_path_with_format(native_format);
2498 return *this;
2499 }
2500
2501 #endif // GHC_EXPAND_IMPL
2502
2503 template <class Source>
2504 inline path& path::operator=(const Source& source)
2505 {
2506 return assign(source);
2507 }
2508
2509 template <class Source>
2510 inline path& path::assign(const Source& source)
2511 {
2512 #ifdef GHC_USE_WCHAR_T
2513 _path.assign(detail::toWChar(source));
2514 #else
2515 _path.assign(detail::toUtf8(source));
2516 #endif
2517 postprocess_path_with_format(native_format);
2518 return *this;
2519 }
2520
2521 template <>
2522 inline path& path::assign<path>(const path& source)
2523 {
2524 _path = source._path;
2525 #if defined(GHC_OS_WINDOWS) && defined(GHC_WIN_AUTO_PREFIX_LONG_PATH)
2526 _prefixLength = source._prefixLength;
2527 #endif
2528 return *this;
2529 }
2530
2531 template <class InputIterator>
2532 inline path& path::assign(InputIterator first, InputIterator last)
2533 {
2534 _path.assign(first, last);
2535 postprocess_path_with_format(native_format);
2536 return *this;
2537 }
2538
2539 #ifdef GHC_EXPAND_IMPL
2540
2541 //-----------------------------------------------------------------------------
2542 // [fs.path.append] appends
2543
2544 GHC_INLINE path& path::operator/=(const path& p)
2545 {
2546 if (p.empty()) {
2547 // was: if ((!has_root_directory() && is_absolute()) || has_filename())
2548 if (!_path.empty() && _path[_path.length() - 1] != preferred_separator && _path[_path.length() - 1] != ':') {
2549 _path += preferred_separator;
2550 }
2551 return *this;
2552 }
2553 if ((p.is_absolute() && (_path != root_name()._path || p._path != "/")) || (p.has_root_name() && p.root_name() != root_name())) {
2554 assign(p);
2555 return *this;
2556 }
2557 if (p.has_root_directory()) {
2558 assign(root_name());
2559 }
2560 else if ((!has_root_directory() && is_absolute()) || has_filename()) {
2561 _path += preferred_separator;
2562 }
2563 auto iter = p.begin();
2564 bool first = true;
2565 if (p.has_root_name()) {
2566 ++iter;
2567 }
2568 while (iter != p.end()) {
2569 if (!first && !(!_path.empty() && _path[_path.length() - 1] == preferred_separator)) {
2570 _path += preferred_separator;
2571 }
2572 first = false;
2573 _path += (*iter++).native();
2574 }
2575 check_long_path();
2576 return *this;
2577 }
2578
2579 GHC_INLINE void path::append_name(const value_type* name)
2580 {
2581 if (_path.empty()) {
2582 this->operator/=(path(name));
2583 }
2584 else {
2585 if (_path.back() != path::preferred_separator) {
2586 _path.push_back(path::preferred_separator);
2587 }
2588 _path += name;
2589 check_long_path();
2590 }
2591 }
2592
2593 #endif // GHC_EXPAND_IMPL
2594
2595 template <class Source>
2596 inline path& path::operator/=(const Source& source)
2597 {
2598 return append(source);
2599 }
2600
2601 template <class Source>
2602 inline path& path::append(const Source& source)
2603 {
2604 return this->operator/=(path(source));
2605 }
2606
2607 template <>
2608 inline path& path::append<path>(const path& p)
2609 {
2610 return this->operator/=(p);
2611 }
2612
2613 template <class InputIterator>
2614 inline path& path::append(InputIterator first, InputIterator last)
2615 {
2616 std::basic_string<typename std::iterator_traits<InputIterator>::value_type> part(first, last);
2617 return append(part);
2618 }
2619
2620 #ifdef GHC_EXPAND_IMPL
2621
2622 //-----------------------------------------------------------------------------
2623 // [fs.path.concat] concatenation
2624
2625 GHC_INLINE path& path::operator+=(const path& x)
2626 {
2627 return concat(x._path);
2628 }
2629
2630 GHC_INLINE path& path::operator+=(const string_type& x)
2631 {
2632 return concat(x);
2633 }
2634
2635 #ifdef GHC_WITH_STRING_VIEW
2636 GHC_INLINE path& path::operator+=(basic_string_view<value_type> x)
2637 {
2638 return concat(x);
2639 }
2640 #endif
2641
2642 GHC_INLINE path& path::operator+=(const value_type* x)
2643 {
2644 #ifdef GHC_WITH_STRING_VIEW
2645 basic_string_view<value_type> part(x);
2646 #else
2647 string_type part(x);
2648 #endif
2649 return concat(part);
2650 }
2651
2652 GHC_INLINE path& path::operator+=(value_type x)
2653 {
2654 #ifdef GHC_OS_WINDOWS
2655 if (x == generic_separator) {
2656 x = preferred_separator;
2657 }
2658 #endif
2659 if (_path.empty() || _path.back() != preferred_separator) {
2660 _path += x;
2661 }
2662 check_long_path();
2663 return *this;
2664 }
2665
2666 #endif // GHC_EXPAND_IMPL
2667
2668 template <class Source>
2669 inline path::path_from_string<Source>& path::operator+=(const Source& x)
2670 {
2671 return concat(x);
2672 }
2673
2674 template <class EcharT>
2675 inline path::path_type_EcharT<EcharT>& path::operator+=(EcharT x)
2676 {
2677 #ifdef GHC_WITH_STRING_VIEW
2678 basic_string_view<EcharT> part(&x, 1);
2679 #else
2680 std::basic_string<EcharT> part(1, x);
2681 #endif
2682 concat(part);
2683 return *this;
2684 }
2685
2686 template <class Source>
2687 inline path& path::concat(const Source& x)
2688 {
2689 path p(x);
2690 _path += p._path;
2691 postprocess_path_with_format(native_format);
2692 return *this;
2693 }
2694 template <class InputIterator>
2695 inline path& path::concat(InputIterator first, InputIterator last)
2696 {
2697 _path.append(first, last);
2698 postprocess_path_with_format(native_format);
2699 return *this;
2700 }
2701
2702 #ifdef GHC_EXPAND_IMPL
2703
2704 //-----------------------------------------------------------------------------
2705 // [fs.path.modifiers] modifiers
2706 GHC_INLINE void path::clear() noexcept
2707 {
2708 _path.clear();
2709 #if defined(GHC_OS_WINDOWS) && defined(GHC_WIN_AUTO_PREFIX_LONG_PATH)
2710 _prefixLength = 0;
2711 #endif
2712 }
2713
2714 GHC_INLINE path& path::make_preferred()
2715 {
2716 // as this filesystem implementation only uses generic_format
2717 // internally, this must be a no-op
2718 return *this;
2719 }
2720
2721 GHC_INLINE path& path::remove_filename()
2722 {
2723 if (has_filename()) {
2724 _path.erase(_path.size() - filename()._path.size());
2725 }
2726 return *this;
2727 }
2728
2729 GHC_INLINE path& path::replace_filename(const path& replacement)
2730 {
2731 remove_filename();
2732 return append(replacement);
2733 }
2734
2735 GHC_INLINE path& path::replace_extension(const path& replacement)
2736 {
2737 if (has_extension()) {
2738 _path.erase(_path.size() - extension()._path.size());
2739 }
2740 if (!replacement.empty() && replacement._path[0] != '.') {
2741 _path += '.';
2742 }
2743 return concat(replacement);
2744 }
2745
2746 GHC_INLINE void path::swap(path& rhs) noexcept
2747 {
2748 _path.swap(rhs._path);
2749 #if defined(GHC_OS_WINDOWS) && defined(GHC_WIN_AUTO_PREFIX_LONG_PATH)
2750 std::swap(_prefixLength, rhs._prefixLength);
2751 #endif
2752 }
2753
2754 //-----------------------------------------------------------------------------
2755 // [fs.path.native.obs] native format observers
2756 GHC_INLINE const path::string_type& path::native() const noexcept
2757 {
2758 return _path;
2759 }
2760
2761 GHC_INLINE const path::value_type* path::c_str() const noexcept
2762 {
2763 return native().c_str();
2764 }
2765
2766 GHC_INLINE path::operator path::string_type() const
2767 {
2768 return native();
2769 }
2770
2771 #endif // GHC_EXPAND_IMPL
2772
2773 template <class EcharT, class traits, class Allocator>
2774 inline std::basic_string<EcharT, traits, Allocator> path::string(const Allocator& a) const
2775 {
2776 #ifdef GHC_USE_WCHAR_T
2777 return detail::fromWChar<std::basic_string<EcharT, traits, Allocator>>(_path, a);
2778 #else
2779 return detail::fromUtf8<std::basic_string<EcharT, traits, Allocator>>(_path, a);
2780 #endif
2781 }
2782
2783 #ifdef GHC_EXPAND_IMPL
2784
2785 GHC_INLINE std::string path::string() const
2786 {
2787 #ifdef GHC_USE_WCHAR_T
2788 return detail::toUtf8(native());
2789 #else
2790 return native();
2791 #endif
2792 }
2793
2794 GHC_INLINE std::wstring path::wstring() const
2795 {
2796 #ifdef GHC_USE_WCHAR_T
2797 return native();
2798 #else
2799 return detail::fromUtf8<std::wstring>(native());
2800 #endif
2801 }
2802
2803 #if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
2804 GHC_INLINE std::u8string path::u8string() const
2805 {
2806 #ifdef GHC_USE_WCHAR_T
2807 return std::u8string(reinterpret_cast<const char8_t*>(detail::toUtf8(native()).c_str()));
2808 #else
2809 return std::u8string(reinterpret_cast<const char8_t*>(c_str()));
2810 #endif
2811 }
2812 #else
2813 GHC_INLINE std::string path::u8string() const
2814 {
2815 #ifdef GHC_USE_WCHAR_T
2816 return detail::toUtf8(native());
2817 #else
2818 return native();
2819 #endif
2820 }
2821 #endif
2822
2823 GHC_INLINE std::u16string path::u16string() const
2824 {
2825 // TODO: optimize
2826 return detail::fromUtf8<std::u16string>(string());
2827 }
2828
2829 GHC_INLINE std::u32string path::u32string() const
2830 {
2831 // TODO: optimize
2832 return detail::fromUtf8<std::u32string>(string());
2833 }
2834
2835 #endif // GHC_EXPAND_IMPL
2836
2837 //-----------------------------------------------------------------------------
2838 // [fs.path.generic.obs] generic format observers
2839 template <class EcharT, class traits, class Allocator>
2840 inline std::basic_string<EcharT, traits, Allocator> path::generic_string(const Allocator& a) const
2841 {
2842 #ifdef GHC_OS_WINDOWS
2843 #ifdef GHC_USE_WCHAR_T
2844 auto result = detail::fromWChar<std::basic_string<EcharT, traits, Allocator>, path::string_type>(_path, a);
2845 #else
2846 auto result = detail::fromUtf8<std::basic_string<EcharT, traits, Allocator>>(_path, a);
2847 #endif
2848 for (auto& c : result) {
2849 if (c == preferred_separator) {
2850 c = generic_separator;
2851 }
2852 }
2853 return result;
2854 #else
2855 return detail::fromUtf8<std::basic_string<EcharT, traits, Allocator>>(_path, a);
2856 #endif
2857 }
2858
2859 #ifdef GHC_EXPAND_IMPL
2860
2861 GHC_INLINE std::string path::generic_string() const
2862 {
2863 #ifdef GHC_OS_WINDOWS
2864 return generic_string<std::string::value_type, std::string::traits_type, std::string::allocator_type>();
2865 #else
2866 return _path;
2867 #endif
2868 }
2869
2870 GHC_INLINE std::wstring path::generic_wstring() const
2871 {
2872 #ifdef GHC_OS_WINDOWS
2873 return generic_string<std::wstring::value_type, std::wstring::traits_type, std::wstring::allocator_type>();
2874 #else
2875 return detail::fromUtf8<std::wstring>(_path);
2876 #endif
2877 } // namespace filesystem
2878
2879 #if defined(__cpp_lib_char8_t) && !defined(GHC_FILESYSTEM_ENFORCE_CPP17_API)
2880 GHC_INLINE std::u8string path::generic_u8string() const
2881 {
2882 #ifdef GHC_OS_WINDOWS
2883 return generic_string<std::u8string::value_type, std::u8string::traits_type, std::u8string::allocator_type>();
2884 #else
2885 return std::u8string(reinterpret_cast<const char8_t*>(_path.c_str()));
2886 #endif
2887 }
2888 #else
2889 GHC_INLINE std::string path::generic_u8string() const
2890 {
2891 #ifdef GHC_OS_WINDOWS
2892 return generic_string<std::string::value_type, std::string::traits_type, std::string::allocator_type>();
2893 #else
2894 return _path;
2895 #endif
2896 }
2897 #endif
2898
2899 GHC_INLINE std::u16string path::generic_u16string() const
2900 {
2901 #ifdef GHC_OS_WINDOWS
2902 return generic_string<std::u16string::value_type, std::u16string::traits_type, std::u16string::allocator_type>();
2903 #else
2904 return detail::fromUtf8<std::u16string>(_path);
2905 #endif
2906 }
2907
2908 GHC_INLINE std::u32string path::generic_u32string() const
2909 {
2910 #ifdef GHC_OS_WINDOWS
2911 return generic_string<std::u32string::value_type, std::u32string::traits_type, std::u32string::allocator_type>();
2912 #else
2913 return detail::fromUtf8<std::u32string>(_path);
2914 #endif
2915 }
2916
2917 //-----------------------------------------------------------------------------
2918 // [fs.path.compare] compare
2919 GHC_INLINE int path::compare(const path& p) const noexcept
2920 {
2921 #ifdef LWG_2936_BEHAVIOUR
2922 auto rnl1 = root_name_length();
2923 auto rnl2 = p.root_name_length();
2924 #ifdef GHC_OS_WINDOWS
2925 auto rnc = detail::compare_simple_insensitive(_path.c_str(), rnl1, p._path.c_str(), rnl2);
2926 #else
2927 auto rnc = _path.compare(0, rnl1, p._path, 0, (std::min(rnl1, rnl2)));
2928 #endif
2929 if (rnc) {
2930 return rnc;
2931 }
2932 bool hrd1 = has_root_directory(), hrd2 = p.has_root_directory();
2933 if (hrd1 != hrd2) {
2934 return hrd1 ? 1 : -1;
2935 }
2936 if (hrd1) {
2937 ++rnl1;
2938 ++rnl2;
2939 }
2940 auto iter1 = _path.begin() + static_cast<int>(rnl1);
2941 auto iter2 = p._path.begin() + static_cast<int>(rnl2);
2942 while (iter1 != _path.end() && iter2 != p._path.end() && *iter1 == *iter2) {
2943 ++iter1;
2944 ++iter2;
2945 }
2946 if (iter1 == _path.end()) {
2947 return iter2 == p._path.end() ? 0 : -1;
2948 }
2949 if (iter2 == p._path.end()) {
2950 return 1;
2951 }
2952 if (*iter1 == preferred_separator) {
2953 return -1;
2954 }
2955 if (*iter2 == preferred_separator) {
2956 return 1;
2957 }
2958 return *iter1 < *iter2 ? -1 : 1;
2959 #else // LWG_2936_BEHAVIOUR
2960 #ifdef GHC_OS_WINDOWS
2961 auto rnl1 = root_name_length();
2962 auto rnl2 = p.root_name_length();
2963 auto rnc = detail::compare_simple_insensitive(_path.c_str(), rnl1, p._path.c_str(), rnl2);
2964 if (rnc) {
2965 return rnc;
2966 }
2967 return _path.compare(rnl1, std::string::npos, p._path, rnl2, std::string::npos);
2968 #else
2969 return _path.compare(p._path);
2970 #endif
2971 #endif
2972 }
2973
2974 GHC_INLINE int path::compare(const string_type& s) const
2975 {
2976 return compare(path(s));
2977 }
2978
2979 #ifdef GHC_WITH_STRING_VIEW
2980 GHC_INLINE int path::compare(basic_string_view<value_type> s) const
2981 {
2982 return compare(path(s));
2983 }
2984 #endif
2985
2986 GHC_INLINE int path::compare(const value_type* s) const
2987 {
2988 return compare(path(s));
2989 }
2990
2991 //-----------------------------------------------------------------------------
2992 // [fs.path.decompose] decomposition
2993 #ifdef GHC_OS_WINDOWS
2994 GHC_INLINE void path::handle_prefixes()
2995 {
2996 #if defined(GHC_WIN_AUTO_PREFIX_LONG_PATH)
2997 _prefixLength = 0;
2998 if (_path.length() >= 6 && _path[2] == '?' && std::toupper(static_cast<unsigned char>(_path[4])) >= 'A' && std::toupper(static_cast<unsigned char>(_path[4])) <= 'Z' && _path[5] == ':') {
2999 if (detail::startsWith(_path, impl_string_type(GHC_PLATFORM_LITERAL("\\\\?\\"))) || detail::startsWith(_path, impl_string_type(GHC_PLATFORM_LITERAL("\\??\\")))) {
3000 _prefixLength = 4;
3001 }
3002 }
3003 #endif // GHC_WIN_AUTO_PREFIX_LONG_PATH
3004 }
3005 #endif
3006
3007 GHC_INLINE path::string_type::size_type path::root_name_length() const noexcept
3008 {
3009 #ifdef GHC_OS_WINDOWS
3010 if (_path.length() >= _prefixLength + 2 && std::toupper(static_cast<unsigned char>(_path[_prefixLength])) >= 'A' && std::toupper(static_cast<unsigned char>(_path[_prefixLength])) <= 'Z' && _path[_prefixLength + 1] == ':') {
3011 return 2;
3012 }
3013 #endif
3014 if (_path.length() > _prefixLength + 2 && _path[_prefixLength] == preferred_separator && _path[_prefixLength + 1] == preferred_separator && _path[_prefixLength + 2] != preferred_separator && std::isprint(_path[_prefixLength + 2])) {
3015 impl_string_type::size_type pos = _path.find(preferred_separator, _prefixLength + 3);
3016 if (pos == impl_string_type::npos) {
3017 return _path.length();
3018 }
3019 else {
3020 return pos;
3021 }
3022 }
3023 return 0;
3024 }
3025
3026 GHC_INLINE path path::root_name() const
3027 {
3028 return path(_path.substr(_prefixLength, root_name_length()), native_format);
3029 }
3030
3031 GHC_INLINE path path::root_directory() const
3032 {
3033 if (has_root_directory()) {
3034 static const path _root_dir(std::string(1, preferred_separator), native_format);
3035 return _root_dir;
3036 }
3037 return path();
3038 }
3039
3040 GHC_INLINE path path::root_path() const
3041 {
3042 return path(root_name().string() + root_directory().string(), native_format);
3043 }
3044
3045 GHC_INLINE path path::relative_path() const
3046 {
3047 auto rootPathLen = _prefixLength + root_name_length() + (has_root_directory() ? 1 : 0);
3048 return path(_path.substr((std::min)(rootPathLen, _path.length())), generic_format);
3049 }
3050
3051 GHC_INLINE path path::parent_path() const
3052 {
3053 auto rootPathLen = _prefixLength + root_name_length() + (has_root_directory() ? 1 : 0);
3054 if (rootPathLen < _path.length()) {
3055 if (empty()) {
3056 return path();
3057 }
3058 else {
3059 auto piter = end();
3060 auto iter = piter.decrement(_path.end());
3061 if (iter > _path.begin() + static_cast<long>(rootPathLen) && *iter != preferred_separator) {
3062 --iter;
3063 }
3064 return path(_path.begin(), iter, native_format);
3065 }
3066 }
3067 else {
3068 return *this;
3069 }
3070 }
3071
3072 GHC_INLINE path path::filename() const
3073 {
3074 return !has_relative_path() ? path() : path(*--end());
3075 }
3076
3077 GHC_INLINE path path::stem() const
3078 {
3079 impl_string_type fn = filename().native();
3080 if (fn != "." && fn != "..") {
3081 impl_string_type::size_type pos = fn.rfind('.');
3082 if (pos != impl_string_type::npos && pos > 0) {
3083 return path{fn.substr(0, pos), native_format};
3084 }
3085 }
3086 return path{fn, native_format};
3087 }
3088
3089 GHC_INLINE path path::extension() const
3090 {
3091 if (has_relative_path()) {
3092 auto iter = end();
3093 const auto& fn = *--iter;
3094 impl_string_type::size_type pos = fn._path.rfind('.');
3095 if (pos != std::string::npos && pos > 0) {
3096 return path(fn._path.substr(pos), native_format);
3097 }
3098 }
3099 return path();
3100 }
3101
3102 #ifdef GHC_OS_WINDOWS
3103 namespace detail {
3104 GHC_INLINE bool has_executable_extension(const path& p)
3105 {
3106 if (p.has_relative_path()) {
3107 auto iter = p.end();
3108 const auto& fn = *--iter;
3109 auto pos = fn._path.find_last_of('.');
3110 if (pos == std::string::npos || pos == 0 || fn._path.length() - pos != 3) {
3111 return false;
3112 }
3113 const path::value_type* ext = fn._path.c_str() + pos + 1;
3114 if (detail::equals_simple_insensitive(ext, GHC_PLATFORM_LITERAL("exe")) || detail::equals_simple_insensitive(ext, GHC_PLATFORM_LITERAL("cmd")) || detail::equals_simple_insensitive(ext, GHC_PLATFORM_LITERAL("bat")) ||
3115 detail::equals_simple_insensitive(ext, GHC_PLATFORM_LITERAL("com"))) {
3116 return true;
3117 }
3118 }
3119 return false;
3120 }
3121 } // namespace detail
3122 #endif
3123
3124 //-----------------------------------------------------------------------------
3125 // [fs.path.query] query
3126 GHC_INLINE bool path::empty() const noexcept
3127 {
3128 return _path.empty();
3129 }
3130
3131 GHC_INLINE bool path::has_root_name() const
3132 {
3133 return root_name_length() > 0;
3134 }
3135
3136 GHC_INLINE bool path::has_root_directory() const
3137 {
3138 auto rootLen = _prefixLength + root_name_length();
3139 return (_path.length() > rootLen && _path[rootLen] == preferred_separator);
3140 }
3141
3142 GHC_INLINE bool path::has_root_path() const
3143 {
3144 return has_root_name() || has_root_directory();
3145 }
3146
3147 GHC_INLINE bool path::has_relative_path() const
3148 {
3149 auto rootPathLen = _prefixLength + root_name_length() + (has_root_directory() ? 1 : 0);
3150 return rootPathLen < _path.length();
3151 }
3152
3153 GHC_INLINE bool path::has_parent_path() const
3154 {
3155 return !parent_path().empty();
3156 }
3157
3158 GHC_INLINE bool path::has_filename() const
3159 {
3160 return has_relative_path() && !filename().empty();
3161 }
3162
3163 GHC_INLINE bool path::has_stem() const
3164 {
3165 return !stem().empty();
3166 }
3167
3168 GHC_INLINE bool path::has_extension() const
3169 {
3170 return !extension().empty();
3171 }
3172
3173 GHC_INLINE bool path::is_absolute() const
3174 {
3175 #ifdef GHC_OS_WINDOWS
3176 return has_root_name() && has_root_directory();
3177 #else
3178 return has_root_directory();
3179 #endif
3180 }
3181
3182 GHC_INLINE bool path::is_relative() const
3183 {
3184 return !is_absolute();
3185 }
3186
3187 //-----------------------------------------------------------------------------
3188 // [fs.path.gen] generation
3189 GHC_INLINE path path::lexically_normal() const
3190 {
3191 path dest;
3192 bool lastDotDot = false;
3193 for (string_type s : *this) {
3194 if (s == ".") {
3195 dest /= "";
3196 continue;
3197 }
3198 else if (s == ".." && !dest.empty()) {
3199 auto root = root_path();
3200 if (dest == root) {
3201 continue;
3202 }
3203 else if (*(--dest.end()) != "..") {
3204 if (dest._path.back() == preferred_separator) {
3205 dest._path.pop_back();
3206 }
3207 dest.remove_filename();
3208 continue;
3209 }
3210 }
3211 if (!(s.empty() && lastDotDot)) {
3212 dest /= s;
3213 }
3214 lastDotDot = s == "..";
3215 }
3216 if (dest.empty()) {
3217 dest = ".";
3218 }
3219 return dest;
3220 }
3221
3222 GHC_INLINE path path::lexically_relative(const path& base) const
3223 {
3224 if (root_name() != base.root_name() || is_absolute() != base.is_absolute() || (!has_root_directory() && base.has_root_directory())) {
3225 return path();
3226 }
3227 const_iterator a = begin(), b = base.begin();
3228 while (a != end() && b != base.end() && *a == *b) {
3229 ++a;
3230 ++b;
3231 }
3232 if (a == end() && b == base.end()) {
3233 return path(".");
3234 }
3235 int count = 0;
3236 for (const auto& element : input_iterator_range<const_iterator>(b, base.end())) {
3237 if (element != "." && element != "" && element != "..") {
3238 ++count;
3239 }
3240 else if (element == "..") {
3241 --count;
3242 }
3243 }
3244 if (count < 0) {
3245 return path();
3246 }
3247 path result;
3248 for (int i = 0; i < count; ++i) {
3249 result /= "..";
3250 }
3251 for (const auto& element : input_iterator_range<const_iterator>(a, end())) {
3252 result /= element;
3253 }
3254 return result;
3255 }
3256
3257 GHC_INLINE path path::lexically_proximate(const path& base) const
3258 {
3259 path result = lexically_relative(base);
3260 return result.empty() ? *this : result;
3261 }
3262
3263 //-----------------------------------------------------------------------------
3264 // [fs.path.itr] iterators
3265 GHC_INLINE path::iterator::iterator() {}
3266
3267 GHC_INLINE path::iterator::iterator(const path& p, const impl_string_type::const_iterator& pos)
3268 : _first(p._path.begin())
3269 , _last(p._path.end())
3270 , _prefix(_first + static_cast<string_type::difference_type>(p._prefixLength))
3271 , _root(p.has_root_directory() ? _first + static_cast<string_type::difference_type>(p._prefixLength + p.root_name_length()) : _last)
3272 , _iter(pos)
3273 {
3274 if (pos != _last) {
3275 updateCurrent();
3276 }
3277 }
3278
3279 GHC_INLINE path::impl_string_type::const_iterator path::iterator::increment(const path::impl_string_type::const_iterator& pos) const
3280 {
3281 path::impl_string_type::const_iterator i = pos;
3282 bool fromStart = i == _first || i == _prefix;
3283 if (i != _last) {
3284 if (fromStart && i == _first && _prefix > _first) {
3285 i = _prefix;
3286 }
3287 else if (*i++ == preferred_separator) {
3288 // we can only sit on a slash if it is a network name or a root
3289 if (i != _last && *i == preferred_separator) {
3290 if (fromStart && !(i + 1 != _last && *(i + 1) == preferred_separator)) {
3291 // leadind double slashes detected, treat this and the
3292 // following until a slash as one unit
3293 i = std::find(++i, _last, preferred_separator);
3294 }
3295 else {
3296 // skip redundant slashes
3297 while (i != _last && *i == preferred_separator) {
3298 ++i;
3299 }
3300 }
3301 }
3302 }
3303 else {
3304 if (fromStart && i != _last && *i == ':') {
3305 ++i;
3306 }
3307 else {
3308 i = std::find(i, _last, preferred_separator);
3309 }
3310 }
3311 }
3312 return i;
3313 }
3314
3315 GHC_INLINE path::impl_string_type::const_iterator path::iterator::decrement(const path::impl_string_type::const_iterator& pos) const
3316 {
3317 path::impl_string_type::const_iterator i = pos;
3318 if (i != _first) {
3319 --i;
3320 // if this is now the root slash or the trailing slash, we are done,
3321 // else check for network name
3322 if (i != _root && (pos != _last || *i != preferred_separator)) {
3323 #ifdef GHC_OS_WINDOWS
3324 static const impl_string_type seps = GHC_PLATFORM_LITERAL("\\:");
3325 i = std::find_first_of(std::reverse_iterator<path::impl_string_type::const_iterator>(i), std::reverse_iterator<path::impl_string_type::const_iterator>(_first), seps.begin(), seps.end()).base();
3326 if (i > _first && *i == ':') {
3327 i++;
3328 }
3329 #else
3330 i = std::find(std::reverse_iterator<path::impl_string_type::const_iterator>(i), std::reverse_iterator<path::impl_string_type::const_iterator>(_first), preferred_separator).base();
3331 #endif
3332 // Now we have to check if this is a network name
3333 if (i - _first == 2 && *_first == preferred_separator && *(_first + 1) == preferred_separator) {
3334 i -= 2;
3335 }
3336 }
3337 }
3338 return i;
3339 }
3340
3341 GHC_INLINE void path::iterator::updateCurrent()
3342 {
3343 if ((_iter == _last) || (_iter != _first && _iter != _last && (*_iter == preferred_separator && _iter != _root) && (_iter + 1 == _last))) {
3344 _current.clear();
3345 }
3346 else {
3347 _current.assign(_iter, increment(_iter));
3348 }
3349 }
3350
3351 GHC_INLINE path::iterator& path::iterator::operator++()
3352 {
3353 _iter = increment(_iter);
3354 while (_iter != _last && // we didn't reach the end
3355 _iter != _root && // this is not a root position
3356 *_iter == preferred_separator && // we are on a separator
3357 (_iter + 1) != _last // the slash is not the last char
3358 ) {
3359 ++_iter;
3360 }
3361 updateCurrent();
3362 return *this;
3363 }
3364
3365 GHC_INLINE path::iterator path::iterator::operator++(int)
3366 {
3367 path::iterator i{*this};
3368 ++(*this);
3369 return i;
3370 }
3371
3372 GHC_INLINE path::iterator& path::iterator::operator--()
3373 {
3374 _iter = decrement(_iter);
3375 updateCurrent();
3376 return *this;
3377 }
3378
3379 GHC_INLINE path::iterator path::iterator::operator--(int)
3380 {
3381 auto i = *this;
3382 --(*this);
3383 return i;
3384 }
3385
3386 GHC_INLINE bool path::iterator::operator==(const path::iterator& other) const
3387 {
3388 return _iter == other._iter;
3389 }
3390
3391 GHC_INLINE bool path::iterator::operator!=(const path::iterator& other) const
3392 {
3393 return _iter != other._iter;
3394 }
3395
3396 GHC_INLINE path::iterator::reference path::iterator::operator*() const
3397 {
3398 return _current;
3399 }
3400
3401 GHC_INLINE path::iterator::pointer path::iterator::operator->() const
3402 {
3403 return &_current;
3404 }
3405
3406 GHC_INLINE path::iterator path::begin() const
3407 {
3408 return iterator(*this, _path.begin());
3409 }
3410
3411 GHC_INLINE path::iterator path::end() const
3412 {
3413 return iterator(*this, _path.end());
3414 }
3415
3416 //-----------------------------------------------------------------------------
3417 // [fs.path.nonmember] path non-member functions
3418 GHC_INLINE void swap(path& lhs, path& rhs) noexcept
3419 {
3420 swap(lhs._path, rhs._path);
3421 }
3422
3423 GHC_INLINE size_t hash_value(const path& p) noexcept
3424 {
3425 return std::hash<std::string>()(p.generic_string());
3426 }
3427
3428 #ifdef GHC_HAS_THREEWAY_COMP
3429 GHC_INLINE std::strong_ordering operator<=>(const path& lhs, const path& rhs) noexcept
3430 {
3431 return lhs.compare(rhs) <=> 0;
3432 }
3433 #endif
3434
3435 GHC_INLINE bool operator==(const path& lhs, const path& rhs) noexcept
3436 {
3437 return lhs.compare(rhs) == 0;
3438 }
3439
3440 GHC_INLINE bool operator!=(const path& lhs, const path& rhs) noexcept
3441 {
3442 return !(lhs == rhs);
3443 }
3444
3445 GHC_INLINE bool operator<(const path& lhs, const path& rhs) noexcept
3446 {
3447 return lhs.compare(rhs) < 0;
3448 }
3449
3450 GHC_INLINE bool operator<=(const path& lhs, const path& rhs) noexcept
3451 {
3452 return lhs.compare(rhs) <= 0;
3453 }
3454
3455 GHC_INLINE bool operator>(const path& lhs, const path& rhs) noexcept
3456 {
3457 return lhs.compare(rhs) > 0;
3458 }
3459
3460 GHC_INLINE bool operator>=(const path& lhs, const path& rhs) noexcept
3461 {
3462 return lhs.compare(rhs) >= 0;
3463 }
3464
3465 GHC_INLINE path operator/(const path& lhs, const path& rhs)
3466 {
3467 path result(lhs);
3468 result /= rhs;
3469 return result;
3470 }
3471
3472 #endif // GHC_EXPAND_IMPL
3473
3474 //-----------------------------------------------------------------------------
3475 // [fs.path.io] path inserter and extractor
3476 template <class charT, class traits>
3477 inline std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const path& p)
3478 {
3479 os << "\"";
3480 auto ps = p.string<charT, traits>();
3481 for (auto c : ps) {
3482 if (c == '"' || c == '\\') {
3483 os << '\\';
3484 }
3485 os << c;
3486 }
3487 os << "\"";
3488 return os;
3489 }
3490
3491 template <class charT, class traits>
3492 inline std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, path& p)
3493 {
3494 std::basic_string<charT, traits> tmp;
3495 charT c;
3496 is >> c;
3497 if (c == '"') {
3498 auto sf = is.flags();
3499 is >> std::noskipws;
3500 while (is) {
3501 auto c2 = is.get();
3502 if (is) {
3503 if (c2 == '\\') {
3504 c2 = is.get();
3505 if (is) {
3506 tmp += static_cast<charT>(c2);
3507 }
3508 }
3509 else if (c2 == '"') {
3510 break;
3511 }
3512 else {
3513 tmp += static_cast<charT>(c2);
3514 }
3515 }
3516 }
3517 if ((sf & std::ios_base::skipws) == std::ios_base::skipws) {
3518 is >> std::skipws;
3519 }
3520 p = path(tmp);
3521 }
3522 else {
3523 is >> tmp;
3524 p = path(static_cast<charT>(c) + tmp);
3525 }
3526 return is;
3527 }
3528
3529 #ifdef GHC_EXPAND_IMPL
3530
3531 //-----------------------------------------------------------------------------
3532 // [fs.class.filesystem_error] Class filesystem_error
3533 GHC_INLINE filesystem_error::filesystem_error(const std::string& what_arg, std::error_code ec)
3534 : std::system_error(ec, what_arg)
3535 , _what_arg(what_arg)
3536 , _ec(ec)
3537 {
3538 }
3539
3540 GHC_INLINE filesystem_error::filesystem_error(const std::string& what_arg, const path& p1, std::error_code ec)
3541 : std::system_error(ec, what_arg)
3542 , _what_arg(what_arg)
3543 , _ec(ec)
3544 , _p1(p1)
3545 {
3546 if (!_p1.empty()) {
3547 _what_arg += ": '" + _p1.string() + "'";
3548 }
3549 }
3550
3551 GHC_INLINE filesystem_error::filesystem_error(const std::string& what_arg, const path& p1, const path& p2, std::error_code ec)
3552 : std::system_error(ec, what_arg)
3553 , _what_arg(what_arg)
3554 , _ec(ec)
3555 , _p1(p1)
3556 , _p2(p2)
3557 {
3558 if (!_p1.empty()) {
3559 _what_arg += ": '" + _p1.string() + "'";
3560 }
3561 if (!_p2.empty()) {
3562 _what_arg += ", '" + _p2.string() + "'";
3563 }
3564 }
3565
3566 GHC_INLINE const path& filesystem_error::path1() const noexcept
3567 {
3568 return _p1;
3569 }
3570
3571 GHC_INLINE const path& filesystem_error::path2() const noexcept
3572 {
3573 return _p2;
3574 }
3575
3576 GHC_INLINE const char* filesystem_error::what() const noexcept
3577 {
3578 return _what_arg.c_str();
3579 }
3580
3581 //-----------------------------------------------------------------------------
3582 // [fs.op.funcs] filesystem operations
3583 #ifdef GHC_WITH_EXCEPTIONS
3584 GHC_INLINE path absolute(const path& p)
3585 {
3586 std::error_code ec;
3587 path result = absolute(p, ec);
3588 if (ec) {
3589 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
3590 }
3591 return result;
3592 }
3593 #endif
3594
3595 GHC_INLINE path absolute(const path& p, std::error_code& ec)
3596 {
3597 ec.clear();
3598 #ifdef GHC_OS_WINDOWS
3599 if (p.empty()) {
3600 return absolute(current_path(ec), ec) / "";
3601 }
3602 ULONG size = ::GetFullPathNameW(GHC_NATIVEWP(p), 0, 0, 0);
3603 if (size) {
3604 std::vector<wchar_t> buf(size, 0);
3605 ULONG s2 = GetFullPathNameW(GHC_NATIVEWP(p), size, buf.data(), nullptr);
3606 if (s2 && s2 < size) {
3607 path result = path(std::wstring(buf.data(), s2));
3608 if (p.filename() == ".") {
3609 result /= ".";
3610 }
3611 return result;
3612 }
3613 }
3614 ec = detail::make_system_error();
3615 return path();
3616 #else
3617 path base = current_path(ec);
3618 if (!ec) {
3619 if (p.empty()) {
3620 return base / p;
3621 }
3622 if (p.has_root_name()) {
3623 if (p.has_root_directory()) {
3624 return p;
3625 }
3626 else {
3627 return p.root_name() / base.root_directory() / base.relative_path() / p.relative_path();
3628 }
3629 }
3630 else {
3631 if (p.has_root_directory()) {
3632 return base.root_name() / p;
3633 }
3634 else {
3635 return base / p;
3636 }
3637 }
3638 }
3639 ec = detail::make_system_error();
3640 return path();
3641 #endif
3642 }
3643
3644 #ifdef GHC_WITH_EXCEPTIONS
3645 GHC_INLINE path canonical(const path& p)
3646 {
3647 std::error_code ec;
3648 auto result = canonical(p, ec);
3649 if (ec) {
3650 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
3651 }
3652 return result;
3653 }
3654 #endif
3655
3656 GHC_INLINE path canonical(const path& p, std::error_code& ec)
3657 {
3658 if (p.empty()) {
3659 ec = detail::make_error_code(detail::portable_error::not_found);
3660 return path();
3661 }
3662 path work = p.is_absolute() ? p : absolute(p, ec);
3663 path result;
3664
3665 auto fs = status(work, ec);
3666 if (ec) {
3667 return path();
3668 }
3669 if (fs.type() == file_type::not_found) {
3670 ec = detail::make_error_code(detail::portable_error::not_found);
3671 return path();
3672 }
3673 bool redo;
3674 do {
3675 auto rootPathLen = work._prefixLength + work.root_name_length() + (work.has_root_directory() ? 1 : 0);
3676 redo = false;
3677 result.clear();
3678 for (auto pe : work) {
3679 if (pe.empty() || pe == ".") {
3680 continue;
3681 }
3682 else if (pe == "..") {
3683 result = result.parent_path();
3684 continue;
3685 }
3686 else if ((result / pe).string().length() <= rootPathLen) {
3687 result /= pe;
3688 continue;
3689 }
3690 auto sls = symlink_status(result / pe, ec);
3691 if (ec) {
3692 return path();
3693 }
3694 if (is_symlink(sls)) {
3695 redo = true;
3696 auto target = read_symlink(result / pe, ec);
3697 if (ec) {
3698 return path();
3699 }
3700 if (target.is_absolute()) {
3701 result = target;
3702 continue;
3703 }
3704 else {
3705 result /= target;
3706 continue;
3707 }
3708 }
3709 else {
3710 result /= pe;
3711 }
3712 }
3713 work = result;
3714 } while (redo);
3715 ec.clear();
3716 return result;
3717 }
3718
3719 #ifdef GHC_WITH_EXCEPTIONS
3720 GHC_INLINE void copy(const path& from, const path& to)
3721 {
3722 copy(from, to, copy_options::none);
3723 }
3724
3725 GHC_INLINE void copy(const path& from, const path& to, copy_options options)
3726 {
3727 std::error_code ec;
3728 copy(from, to, options, ec);
3729 if (ec) {
3730 throw filesystem_error(detail::systemErrorText(ec.value()), from, to, ec);
3731 }
3732 }
3733 #endif
3734
3735 GHC_INLINE void copy(const path& from, const path& to, std::error_code& ec) noexcept
3736 {
3737 copy(from, to, copy_options::none, ec);
3738 }
3739
3740 GHC_INLINE void copy(const path& from, const path& to, copy_options options, std::error_code& ec) noexcept
3741 {
3742 std::error_code tec;
3743 file_status fs_from, fs_to;
3744 ec.clear();
3745 if ((options & (copy_options::skip_symlinks | copy_options::copy_symlinks | copy_options::create_symlinks)) != copy_options::none) {
3746 fs_from = symlink_status(from, ec);
3747 }
3748 else {
3749 fs_from = status(from, ec);
3750 }
3751 if (!exists(fs_from)) {
3752 if (!ec) {
3753 ec = detail::make_error_code(detail::portable_error::not_found);
3754 }
3755 return;
3756 }
3757 if ((options & (copy_options::skip_symlinks | copy_options::create_symlinks)) != copy_options::none) {
3758 fs_to = symlink_status(to, tec);
3759 }
3760 else {
3761 fs_to = status(to, tec);
3762 }
3763 if (is_other(fs_from) || is_other(fs_to) || (is_directory(fs_from) && is_regular_file(fs_to)) || (exists(fs_to) && equivalent(from, to, ec))) {
3764 ec = detail::make_error_code(detail::portable_error::invalid_argument);
3765 }
3766 else if (is_symlink(fs_from)) {
3767 if ((options & copy_options::skip_symlinks) == copy_options::none) {
3768 if (!exists(fs_to) && (options & copy_options::copy_symlinks) != copy_options::none) {
3769 copy_symlink(from, to, ec);
3770 }
3771 else {
3772 ec = detail::make_error_code(detail::portable_error::invalid_argument);
3773 }
3774 }
3775 }
3776 else if (is_regular_file(fs_from)) {
3777 if ((options & copy_options::directories_only) == copy_options::none) {
3778 if ((options & copy_options::create_symlinks) != copy_options::none) {
3779 create_symlink(from.is_absolute() ? from : canonical(from, ec), to, ec);
3780 }
3781 #ifndef GHC_OS_WEB
3782 else if ((options & copy_options::create_hard_links) != copy_options::none) {
3783 create_hard_link(from, to, ec);
3784 }
3785 #endif
3786 else if (is_directory(fs_to)) {
3787 copy_file(from, to / from.filename(), options, ec);
3788 }
3789 else {
3790 copy_file(from, to, options, ec);
3791 }
3792 }
3793 }
3794 #ifdef LWG_2682_BEHAVIOUR
3795 else if (is_directory(fs_from) && (options & copy_options::create_symlinks) != copy_options::none) {
3796 ec = detail::make_error_code(detail::portable_error::is_a_directory);
3797 }
3798 #endif
3799 else if (is_directory(fs_from) && (options == copy_options::none || (options & copy_options::recursive) != copy_options::none)) {
3800 if (!exists(fs_to)) {
3801 create_directory(to, from, ec);
3802 if (ec) {
3803 return;
3804 }
3805 }
3806 for (auto iter = directory_iterator(from, ec); iter != directory_iterator(); iter.increment(ec)) {
3807 if (!ec) {
3808 copy(iter->path(), to / iter->path().filename(), options | static_cast<copy_options>(0x8000), ec);
3809 }
3810 if (ec) {
3811 return;
3812 }
3813 }
3814 }
3815 return;
3816 }
3817
3818 #ifdef GHC_WITH_EXCEPTIONS
3819 GHC_INLINE bool copy_file(const path& from, const path& to)
3820 {
3821 return copy_file(from, to, copy_options::none);
3822 }
3823
3824 GHC_INLINE bool copy_file(const path& from, const path& to, copy_options option)
3825 {
3826 std::error_code ec;
3827 auto result = copy_file(from, to, option, ec);
3828 if (ec) {
3829 throw filesystem_error(detail::systemErrorText(ec.value()), from, to, ec);
3830 }
3831 return result;
3832 }
3833 #endif
3834
3835 GHC_INLINE bool copy_file(const path& from, const path& to, std::error_code& ec) noexcept
3836 {
3837 return copy_file(from, to, copy_options::none, ec);
3838 }
3839
3840 GHC_INLINE bool copy_file(const path& from, const path& to, copy_options options, std::error_code& ec) noexcept
3841 {
3842 std::error_code tecf, tect;
3843 auto sf = status(from, tecf);
3844 auto st = status(to, tect);
3845 bool overwrite = false;
3846 ec.clear();
3847 if (!is_regular_file(sf)) {
3848 ec = tecf;
3849 return false;
3850 }
3851 if (exists(st) && (!is_regular_file(st) || equivalent(from, to, ec) || (options & (copy_options::skip_existing | copy_options::overwrite_existing | copy_options::update_existing)) == copy_options::none)) {
3852 ec = tect ? tect : detail::make_error_code(detail::portable_error::exists);
3853 return false;
3854 }
3855 if (exists(st)) {
3856 if ((options & copy_options::update_existing) == copy_options::update_existing) {
3857 auto from_time = last_write_time(from, ec);
3858 if (ec) {
3859 ec = detail::make_system_error();
3860 return false;
3861 }
3862 auto to_time = last_write_time(to, ec);
3863 if (ec) {
3864 ec = detail::make_system_error();
3865 return false;
3866 }
3867 if (from_time <= to_time) {
3868 return false;
3869 }
3870 }
3871 overwrite = true;
3872 }
3873 #ifdef GHC_OS_WINDOWS
3874 if (!::CopyFileW(GHC_NATIVEWP(from), GHC_NATIVEWP(to), !overwrite)) {
3875 ec = detail::make_system_error();
3876 return false;
3877 }
3878 return true;
3879 #else
3880 std::vector<char> buffer(16384, '\0');
3881 int in = -1, out = -1;
3882 if ((in = ::open(from.c_str(), O_RDONLY)) < 0) {
3883 ec = detail::make_system_error();
3884 return false;
3885 }
3886 int mode = O_CREAT | O_WRONLY | O_TRUNC;
3887 if (!overwrite) {
3888 mode |= O_EXCL;
3889 }
3890 if ((out = ::open(to.c_str(), mode, static_cast<int>(sf.permissions() & perms::all))) < 0) {
3891 ec = detail::make_system_error();
3892 ::close(in);
3893 return false;
3894 }
3895 ssize_t br, bw;
3896 while ((br = ::read(in, buffer.data(), buffer.size())) > 0) {
3897 ssize_t offset = 0;
3898 do {
3899 if ((bw = ::write(out, buffer.data() + offset, static_cast<size_t>(br))) > 0) {
3900 br -= bw;
3901 offset += bw;
3902 }
3903 else if (bw < 0) {
3904 ec = detail::make_system_error();
3905 ::close(in);
3906 ::close(out);
3907 return false;
3908 }
3909 } while (br);
3910 }
3911 ::close(in);
3912 ::close(out);
3913 return true;
3914 #endif
3915 }
3916
3917 #ifdef GHC_WITH_EXCEPTIONS
3918 GHC_INLINE void copy_symlink(const path& existing_symlink, const path& new_symlink)
3919 {
3920 std::error_code ec;
3921 copy_symlink(existing_symlink, new_symlink, ec);
3922 if (ec) {
3923 throw filesystem_error(detail::systemErrorText(ec.value()), existing_symlink, new_symlink, ec);
3924 }
3925 }
3926 #endif
3927
3928 GHC_INLINE void copy_symlink(const path& existing_symlink, const path& new_symlink, std::error_code& ec) noexcept
3929 {
3930 ec.clear();
3931 auto to = read_symlink(existing_symlink, ec);
3932 if (!ec) {
3933 if (exists(to, ec) && is_directory(to, ec)) {
3934 create_directory_symlink(to, new_symlink, ec);
3935 }
3936 else {
3937 create_symlink(to, new_symlink, ec);
3938 }
3939 }
3940 }
3941
3942 #ifdef GHC_WITH_EXCEPTIONS
3943 GHC_INLINE bool create_directories(const path& p)
3944 {
3945 std::error_code ec;
3946 auto result = create_directories(p, ec);
3947 if (ec) {
3948 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
3949 }
3950 return result;
3951 }
3952 #endif
3953
3954 GHC_INLINE bool create_directories(const path& p, std::error_code& ec) noexcept
3955 {
3956 path current;
3957 ec.clear();
3958 bool didCreate = false;
3959 auto rootPathLen = p._prefixLength + p.root_name_length() + (p.has_root_directory() ? 1 : 0);
3960 current = p.native().substr(0, rootPathLen);
3961 path folders(p._path.substr(rootPathLen));
3962 for (path::string_type part : folders) {
3963 current /= part;
3964 std::error_code tec;
3965 auto fs = status(current, tec);
3966 if (tec && fs.type() != file_type::not_found) {
3967 ec = tec;
3968 return false;
3969 }
3970 if (!exists(fs)) {
3971 create_directory(current, ec);
3972 if (ec) {
3973 std::error_code tmp_ec;
3974 if (is_directory(current, tmp_ec)) {
3975 ec.clear();
3976 }
3977 else {
3978 return false;
3979 }
3980 }
3981 didCreate = true;
3982 }
3983 #ifndef LWG_2935_BEHAVIOUR
3984 else if (!is_directory(fs)) {
3985 ec = detail::make_error_code(detail::portable_error::exists);
3986 return false;
3987 }
3988 #endif
3989 }
3990 return didCreate;
3991 }
3992
3993 #ifdef GHC_WITH_EXCEPTIONS
3994 GHC_INLINE bool create_directory(const path& p)
3995 {
3996 std::error_code ec;
3997 auto result = create_directory(p, path(), ec);
3998 if (ec) {
3999 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
4000 }
4001 return result;
4002 }
4003 #endif
4004
4005 GHC_INLINE bool create_directory(const path& p, std::error_code& ec) noexcept
4006 {
4007 return create_directory(p, path(), ec);
4008 }
4009
4010 #ifdef GHC_WITH_EXCEPTIONS
4011 GHC_INLINE bool create_directory(const path& p, const path& attributes)
4012 {
4013 std::error_code ec;
4014 auto result = create_directory(p, attributes, ec);
4015 if (ec) {
4016 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
4017 }
4018 return result;
4019 }
4020 #endif
4021
4022 GHC_INLINE bool create_directory(const path& p, const path& attributes, std::error_code& ec) noexcept
4023 {
4024 std::error_code tec;
4025 ec.clear();
4026 auto fs = status(p, tec);
4027 #ifdef LWG_2935_BEHAVIOUR
4028 if (status_known(fs) && exists(fs)) {
4029 return false;
4030 }
4031 #else
4032 if (status_known(fs) && exists(fs) && is_directory(fs)) {
4033 return false;
4034 }
4035 #endif
4036 #ifdef GHC_OS_WINDOWS
4037 if (!attributes.empty()) {
4038 if (!::CreateDirectoryExW(GHC_NATIVEWP(attributes), GHC_NATIVEWP(p), NULL)) {
4039 ec = detail::make_system_error();
4040 return false;
4041 }
4042 }
4043 else if (!::CreateDirectoryW(GHC_NATIVEWP(p), NULL)) {
4044 ec = detail::make_system_error();
4045 return false;
4046 }
4047 #else
4048 ::mode_t attribs = static_cast<mode_t>(perms::all);
4049 if (!attributes.empty()) {
4050 struct ::stat fileStat;
4051 if (::stat(attributes.c_str(), &fileStat) != 0) {
4052 ec = detail::make_system_error();
4053 return false;
4054 }
4055 attribs = fileStat.st_mode;
4056 }
4057 if (::mkdir(p.c_str(), attribs) != 0) {
4058 ec = detail::make_system_error();
4059 return false;
4060 }
4061 #endif
4062 return true;
4063 }
4064
4065 #ifdef GHC_WITH_EXCEPTIONS
4066 GHC_INLINE void create_directory_symlink(const path& to, const path& new_symlink)
4067 {
4068 std::error_code ec;
4069 create_directory_symlink(to, new_symlink, ec);
4070 if (ec) {
4071 throw filesystem_error(detail::systemErrorText(ec.value()), to, new_symlink, ec);
4072 }
4073 }
4074 #endif
4075
4076 GHC_INLINE void create_directory_symlink(const path& to, const path& new_symlink, std::error_code& ec) noexcept
4077 {
4078 detail::create_symlink(to, new_symlink, true, ec);
4079 }
4080
4081 #ifndef GHC_OS_WEB
4082 #ifdef GHC_WITH_EXCEPTIONS
4083 GHC_INLINE void create_hard_link(const path& to, const path& new_hard_link)
4084 {
4085 std::error_code ec;
4086 create_hard_link(to, new_hard_link, ec);
4087 if (ec) {
4088 throw filesystem_error(detail::systemErrorText(ec.value()), to, new_hard_link, ec);
4089 }
4090 }
4091 #endif
4092
4093 GHC_INLINE void create_hard_link(const path& to, const path& new_hard_link, std::error_code& ec) noexcept
4094 {
4095 detail::create_hardlink(to, new_hard_link, ec);
4096 }
4097 #endif
4098
4099 #ifdef GHC_WITH_EXCEPTIONS
4100 GHC_INLINE void create_symlink(const path& to, const path& new_symlink)
4101 {
4102 std::error_code ec;
4103 create_symlink(to, new_symlink, ec);
4104 if (ec) {
4105 throw filesystem_error(detail::systemErrorText(ec.value()), to, new_symlink, ec);
4106 }
4107 }
4108 #endif
4109
4110 GHC_INLINE void create_symlink(const path& to, const path& new_symlink, std::error_code& ec) noexcept
4111 {
4112 detail::create_symlink(to, new_symlink, false, ec);
4113 }
4114
4115 #ifdef GHC_WITH_EXCEPTIONS
4116 GHC_INLINE path current_path()
4117 {
4118 std::error_code ec;
4119 auto result = current_path(ec);
4120 if (ec) {
4121 throw filesystem_error(detail::systemErrorText(ec.value()), ec);
4122 }
4123 return result;
4124 }
4125 #endif
4126
4127 GHC_INLINE path current_path(std::error_code& ec)
4128 {
4129 ec.clear();
4130 #ifdef GHC_OS_WINDOWS
4131 DWORD pathlen = ::GetCurrentDirectoryW(0, 0);
4132 std::unique_ptr<wchar_t[]> buffer(new wchar_t[size_t(pathlen) + 1]);
4133 if (::GetCurrentDirectoryW(pathlen, buffer.get()) == 0) {
4134 ec = detail::make_system_error();
4135 return path();
4136 }
4137 return path(std::wstring(buffer.get()), path::native_format);
4138 #else
4139 size_t pathlen = static_cast<size_t>(std::max(int(::pathconf(".", _PC_PATH_MAX)), int(PATH_MAX)));
4140 std::unique_ptr<char[]> buffer(new char[pathlen + 1]);
4141 if (::getcwd(buffer.get(), pathlen) == nullptr) {
4142 ec = detail::make_system_error();
4143 return path();
4144 }
4145 return path(buffer.get());
4146 #endif
4147 }
4148
4149 #ifdef GHC_WITH_EXCEPTIONS
4150 GHC_INLINE void current_path(const path& p)
4151 {
4152 std::error_code ec;
4153 current_path(p, ec);
4154 if (ec) {
4155 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
4156 }
4157 }
4158 #endif
4159
4160 GHC_INLINE void current_path(const path& p, std::error_code& ec) noexcept
4161 {
4162 ec.clear();
4163 #ifdef GHC_OS_WINDOWS
4164 if (!::SetCurrentDirectoryW(GHC_NATIVEWP(p))) {
4165 ec = detail::make_system_error();
4166 }
4167 #else
4168 if (::chdir(p.string().c_str()) == -1) {
4169 ec = detail::make_system_error();
4170 }
4171 #endif
4172 }
4173
4174 GHC_INLINE bool exists(file_status s) noexcept
4175 {
4176 return status_known(s) && s.type() != file_type::not_found;
4177 }
4178
4179 #ifdef GHC_WITH_EXCEPTIONS
4180 GHC_INLINE bool exists(const path& p)
4181 {
4182 return exists(status(p));
4183 }
4184 #endif
4185
4186 GHC_INLINE bool exists(const path& p, std::error_code& ec) noexcept
4187 {
4188 file_status s = status(p, ec);
4189 if (status_known(s)) {
4190 ec.clear();
4191 }
4192 return exists(s);
4193 }
4194
4195 #ifdef GHC_WITH_EXCEPTIONS
4196 GHC_INLINE bool equivalent(const path& p1, const path& p2)
4197 {
4198 std::error_code ec;
4199 bool result = equivalent(p1, p2, ec);
4200 if (ec) {
4201 throw filesystem_error(detail::systemErrorText(ec.value()), p1, p2, ec);
4202 }
4203 return result;
4204 }
4205 #endif
4206
4207 GHC_INLINE bool equivalent(const path& p1, const path& p2, std::error_code& ec) noexcept
4208 {
4209 ec.clear();
4210 #ifdef GHC_OS_WINDOWS
4211 detail::unique_handle file1(::CreateFileW(GHC_NATIVEWP(p1), 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0));
4212 auto e1 = ::GetLastError();
4213 detail::unique_handle file2(::CreateFileW(GHC_NATIVEWP(p2), 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0));
4214 if (!file1 || !file2) {
4215 #ifdef LWG_2937_BEHAVIOUR
4216 ec = detail::make_system_error(e1 ? e1 : ::GetLastError());
4217 #else
4218 if (file1 == file2) {
4219 ec = detail::make_system_error(e1 ? e1 : ::GetLastError());
4220 }
4221 #endif
4222 return false;
4223 }
4224 BY_HANDLE_FILE_INFORMATION inf1, inf2;
4225 if (!::GetFileInformationByHandle(file1.get(), &inf1)) {
4226 ec = detail::make_system_error();
4227 return false;
4228 }
4229 if (!::GetFileInformationByHandle(file2.get(), &inf2)) {
4230 ec = detail::make_system_error();
4231 return false;
4232 }
4233 return inf1.ftLastWriteTime.dwLowDateTime == inf2.ftLastWriteTime.dwLowDateTime && inf1.ftLastWriteTime.dwHighDateTime == inf2.ftLastWriteTime.dwHighDateTime && inf1.nFileIndexHigh == inf2.nFileIndexHigh && inf1.nFileIndexLow == inf2.nFileIndexLow &&
4234 inf1.nFileSizeHigh == inf2.nFileSizeHigh && inf1.nFileSizeLow == inf2.nFileSizeLow && inf1.dwVolumeSerialNumber == inf2.dwVolumeSerialNumber;
4235 #else
4236 struct ::stat s1, s2;
4237 auto rc1 = ::stat(p1.c_str(), &s1);
4238 auto e1 = errno;
4239 auto rc2 = ::stat(p2.c_str(), &s2);
4240 if (rc1 || rc2) {
4241 #ifdef LWG_2937_BEHAVIOUR
4242 ec = detail::make_system_error(e1 ? e1 : errno);
4243 #else
4244 if (rc1 && rc2) {
4245 ec = detail::make_system_error(e1 ? e1 : errno);
4246 }
4247 #endif
4248 return false;
4249 }
4250 return s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino && s1.st_size == s2.st_size && s1.st_mtime == s2.st_mtime;
4251 #endif
4252 }
4253
4254 #ifdef GHC_WITH_EXCEPTIONS
4255 GHC_INLINE uintmax_t file_size(const path& p)
4256 {
4257 std::error_code ec;
4258 auto result = file_size(p, ec);
4259 if (ec) {
4260 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
4261 }
4262 return result;
4263 }
4264 #endif
4265
4266 GHC_INLINE uintmax_t file_size(const path& p, std::error_code& ec) noexcept
4267 {
4268 ec.clear();
4269 #ifdef GHC_OS_WINDOWS
4270 WIN32_FILE_ATTRIBUTE_DATA attr;
4271 if (!GetFileAttributesExW(GHC_NATIVEWP(p), GetFileExInfoStandard, &attr)) {
4272 ec = detail::make_system_error();
4273 return static_cast<uintmax_t>(-1);
4274 }
4275 return static_cast<uintmax_t>(attr.nFileSizeHigh) << (sizeof(attr.nFileSizeHigh) * 8) | attr.nFileSizeLow;
4276 #else
4277 struct ::stat fileStat;
4278 if (::stat(p.c_str(), &fileStat) == -1) {
4279 ec = detail::make_system_error();
4280 return static_cast<uintmax_t>(-1);
4281 }
4282 return static_cast<uintmax_t>(fileStat.st_size);
4283 #endif
4284 }
4285
4286 #ifndef GHC_OS_WEB
4287 #ifdef GHC_WITH_EXCEPTIONS
4288 GHC_INLINE uintmax_t hard_link_count(const path& p)
4289 {
4290 std::error_code ec;
4291 auto result = hard_link_count(p, ec);
4292 if (ec) {
4293 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
4294 }
4295 return result;
4296 }
4297 #endif
4298
4299 GHC_INLINE uintmax_t hard_link_count(const path& p, std::error_code& ec) noexcept
4300 {
4301 ec.clear();
4302 #ifdef GHC_OS_WINDOWS
4303 uintmax_t result = static_cast<uintmax_t>(-1);
4304 detail::unique_handle file(::CreateFileW(GHC_NATIVEWP(p), 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0));
4305 BY_HANDLE_FILE_INFORMATION inf;
4306 if (!file) {
4307 ec = detail::make_system_error();
4308 }
4309 else {
4310 if (!::GetFileInformationByHandle(file.get(), &inf)) {
4311 ec = detail::make_system_error();
4312 }
4313 else {
4314 result = inf.nNumberOfLinks;
4315 }
4316 }
4317 return result;
4318 #else
4319 uintmax_t result = 0;
4320 file_status fs = detail::status_ex(p, ec, nullptr, nullptr, &result, nullptr);
4321 if (fs.type() == file_type::not_found) {
4322 ec = detail::make_error_code(detail::portable_error::not_found);
4323 }
4324 return ec ? static_cast<uintmax_t>(-1) : result;
4325 #endif
4326 }
4327 #endif
4328
4329 GHC_INLINE bool is_block_file(file_status s) noexcept
4330 {
4331 return s.type() == file_type::block;
4332 }
4333
4334 #ifdef GHC_WITH_EXCEPTIONS
4335 GHC_INLINE bool is_block_file(const path& p)
4336 {
4337 return is_block_file(status(p));
4338 }
4339 #endif
4340
4341 GHC_INLINE bool is_block_file(const path& p, std::error_code& ec) noexcept
4342 {
4343 return is_block_file(status(p, ec));
4344 }
4345
4346 GHC_INLINE bool is_character_file(file_status s) noexcept
4347 {
4348 return s.type() == file_type::character;
4349 }
4350
4351 #ifdef GHC_WITH_EXCEPTIONS
4352 GHC_INLINE bool is_character_file(const path& p)
4353 {
4354 return is_character_file(status(p));
4355 }
4356 #endif
4357
4358 GHC_INLINE bool is_character_file(const path& p, std::error_code& ec) noexcept
4359 {
4360 return is_character_file(status(p, ec));
4361 }
4362
4363 GHC_INLINE bool is_directory(file_status s) noexcept
4364 {
4365 return s.type() == file_type::directory;
4366 }
4367
4368 #ifdef GHC_WITH_EXCEPTIONS
4369 GHC_INLINE bool is_directory(const path& p)
4370 {
4371 return is_directory(status(p));
4372 }
4373 #endif
4374
4375 GHC_INLINE bool is_directory(const path& p, std::error_code& ec) noexcept
4376 {
4377 return is_directory(status(p, ec));
4378 }
4379
4380 #ifdef GHC_WITH_EXCEPTIONS
4381 GHC_INLINE bool is_empty(const path& p)
4382 {
4383 if (is_directory(p)) {
4384 return directory_iterator(p) == directory_iterator();
4385 }
4386 else {
4387 return file_size(p) == 0;
4388 }
4389 }
4390 #endif
4391
4392 GHC_INLINE bool is_empty(const path& p, std::error_code& ec) noexcept
4393 {
4394 auto fs = status(p, ec);
4395 if (ec) {
4396 return false;
4397 }
4398 if (is_directory(fs)) {
4399 directory_iterator iter(p, ec);
4400 if (ec) {
4401 return false;
4402 }
4403 return iter == directory_iterator();
4404 }
4405 else {
4406 auto sz = file_size(p, ec);
4407 if (ec) {
4408 return false;
4409 }
4410 return sz == 0;
4411 }
4412 }
4413
4414 GHC_INLINE bool is_fifo(file_status s) noexcept
4415 {
4416 return s.type() == file_type::fifo;
4417 }
4418
4419 #ifdef GHC_WITH_EXCEPTIONS
4420 GHC_INLINE bool is_fifo(const path& p)
4421 {
4422 return is_fifo(status(p));
4423 }
4424 #endif
4425
4426 GHC_INLINE bool is_fifo(const path& p, std::error_code& ec) noexcept
4427 {
4428 return is_fifo(status(p, ec));
4429 }
4430
4431 GHC_INLINE bool is_other(file_status s) noexcept
4432 {
4433 return exists(s) && !is_regular_file(s) && !is_directory(s) && !is_symlink(s);
4434 }
4435
4436 #ifdef GHC_WITH_EXCEPTIONS
4437 GHC_INLINE bool is_other(const path& p)
4438 {
4439 return is_other(status(p));
4440 }
4441 #endif
4442
4443 GHC_INLINE bool is_other(const path& p, std::error_code& ec) noexcept
4444 {
4445 return is_other(status(p, ec));
4446 }
4447
4448 GHC_INLINE bool is_regular_file(file_status s) noexcept
4449 {
4450 return s.type() == file_type::regular;
4451 }
4452
4453 #ifdef GHC_WITH_EXCEPTIONS
4454 GHC_INLINE bool is_regular_file(const path& p)
4455 {
4456 return is_regular_file(status(p));
4457 }
4458 #endif
4459
4460 GHC_INLINE bool is_regular_file(const path& p, std::error_code& ec) noexcept
4461 {
4462 return is_regular_file(status(p, ec));
4463 }
4464
4465 GHC_INLINE bool is_socket(file_status s) noexcept
4466 {
4467 return s.type() == file_type::socket;
4468 }
4469
4470 #ifdef GHC_WITH_EXCEPTIONS
4471 GHC_INLINE bool is_socket(const path& p)
4472 {
4473 return is_socket(status(p));
4474 }
4475 #endif
4476
4477 GHC_INLINE bool is_socket(const path& p, std::error_code& ec) noexcept
4478 {
4479 return is_socket(status(p, ec));
4480 }
4481
4482 GHC_INLINE bool is_symlink(file_status s) noexcept
4483 {
4484 return s.type() == file_type::symlink;
4485 }
4486
4487 #ifdef GHC_WITH_EXCEPTIONS
4488 GHC_INLINE bool is_symlink(const path& p)
4489 {
4490 return is_symlink(symlink_status(p));
4491 }
4492 #endif
4493
4494 GHC_INLINE bool is_symlink(const path& p, std::error_code& ec) noexcept
4495 {
4496 return is_symlink(symlink_status(p, ec));
4497 }
4498
4499 #ifdef GHC_WITH_EXCEPTIONS
4500 GHC_INLINE file_time_type last_write_time(const path& p)
4501 {
4502 std::error_code ec;
4503 auto result = last_write_time(p, ec);
4504 if (ec) {
4505 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
4506 }
4507 return result;
4508 }
4509 #endif
4510
4511 GHC_INLINE file_time_type last_write_time(const path& p, std::error_code& ec) noexcept
4512 {
4513 time_t result = 0;
4514 ec.clear();
4515 file_status fs = detail::status_ex(p, ec, nullptr, nullptr, nullptr, &result);
4516 return ec ? (file_time_type::min)() : std::chrono::system_clock::from_time_t(result);
4517 }
4518
4519 #ifdef GHC_WITH_EXCEPTIONS
4520 GHC_INLINE void last_write_time(const path& p, file_time_type new_time)
4521 {
4522 std::error_code ec;
4523 last_write_time(p, new_time, ec);
4524 if (ec) {
4525 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
4526 }
4527 }
4528 #endif
4529
4530 GHC_INLINE void last_write_time(const path& p, file_time_type new_time, std::error_code& ec) noexcept
4531 {
4532 ec.clear();
4533 auto d = new_time.time_since_epoch();
4534 #ifdef GHC_OS_WINDOWS
4535 detail::unique_handle file(::CreateFileW(GHC_NATIVEWP(p), FILE_WRITE_ATTRIBUTES, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL));
4536 FILETIME ft;
4537 auto tt = std::chrono::duration_cast<std::chrono::microseconds>(d).count() * 10 + 116444736000000000;
4538 ft.dwLowDateTime = static_cast<DWORD>(tt);
4539 ft.dwHighDateTime = static_cast<DWORD>(tt >> 32);
4540 if (!::SetFileTime(file.get(), 0, 0, &ft)) {
4541 ec = detail::make_system_error();
4542 }
4543 #elif defined(GHC_OS_MACOS)
4544 #ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
4545 #if __MAC_OS_X_VERSION_MIN_REQUIRED < 101300
4546 struct ::stat fs;
4547 if (::stat(p.c_str(), &fs) == 0) {
4548 struct ::timeval tv[2];
4549 tv[0].tv_sec = fs.st_atimespec.tv_sec;
4550 tv[0].tv_usec = static_cast<int>(fs.st_atimespec.tv_nsec / 1000);
4551 tv[1].tv_sec = std::chrono::duration_cast<std::chrono::seconds>(d).count();
4552 tv[1].tv_usec = static_cast<int>(std::chrono::duration_cast<std::chrono::microseconds>(d).count() % 1000000);
4553 if (::utimes(p.c_str(), tv) == 0) {
4554 return;
4555 }
4556 }
4557 ec = detail::make_system_error();
4558 return;
4559 #else
4560 struct ::timespec times[2];
4561 times[0].tv_sec = 0;
4562 times[0].tv_nsec = UTIME_OMIT;
4563 times[1].tv_sec = std::chrono::duration_cast<std::chrono::seconds>(d).count();
4564 times[1].tv_nsec = 0; // std::chrono::duration_cast<std::chrono::nanoseconds>(d).count() % 1000000000;
4565 if (::utimensat(AT_FDCWD, p.c_str(), times, AT_SYMLINK_NOFOLLOW) != 0) {
4566 ec = detail::make_system_error();
4567 }
4568 return;
4569 #endif
4570 #endif
4571 #else
4572 #ifndef UTIME_OMIT
4573 #define UTIME_OMIT ((1l << 30) - 2l)
4574 #endif
4575 struct ::timespec times[2];
4576 times[0].tv_sec = 0;
4577 times[0].tv_nsec = UTIME_OMIT;
4578 times[1].tv_sec = static_cast<decltype(times[1].tv_sec)>(std::chrono::duration_cast<std::chrono::seconds>(d).count());
4579 times[1].tv_nsec = static_cast<decltype(times[1].tv_nsec)>(std::chrono::duration_cast<std::chrono::nanoseconds>(d).count() % 1000000000);
4580 #if defined(__ANDROID_API__) && __ANDROID_API__ < 12
4581 if (syscall(__NR_utimensat, AT_FDCWD, p.c_str(), times, AT_SYMLINK_NOFOLLOW) != 0) {
4582 #else
4583 if (::utimensat(AT_FDCWD, p.c_str(), times, AT_SYMLINK_NOFOLLOW) != 0) {
4584 #endif
4585 ec = detail::make_system_error();
4586 }
4587 return;
4588 #endif
4589 }
4590
4591 #ifdef GHC_WITH_EXCEPTIONS
4592 GHC_INLINE void permissions(const path& p, perms prms, perm_options opts)
4593 {
4594 std::error_code ec;
4595 permissions(p, prms, opts, ec);
4596 if (ec) {
4597 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
4598 }
4599 }
4600 #endif
4601
4602 GHC_INLINE void permissions(const path& p, perms prms, std::error_code& ec) noexcept
4603 {
4604 permissions(p, prms, perm_options::replace, ec);
4605 }
4606
4607 GHC_INLINE void permissions(const path& p, perms prms, perm_options opts, std::error_code& ec) noexcept
4608 {
4609 if (static_cast<int>(opts & (perm_options::replace | perm_options::add | perm_options::remove)) == 0) {
4610 ec = detail::make_error_code(detail::portable_error::invalid_argument);
4611 return;
4612 }
4613 auto fs = symlink_status(p, ec);
4614 if ((opts & perm_options::replace) != perm_options::replace) {
4615 if ((opts & perm_options::add) == perm_options::add) {
4616 prms = fs.permissions() | prms;
4617 }
4618 else {
4619 prms = fs.permissions() & ~prms;
4620 }
4621 }
4622 #ifdef GHC_OS_WINDOWS
4623 #ifdef __GNUC__
4624 auto oldAttr = GetFileAttributesW(GHC_NATIVEWP(p));
4625 if (oldAttr != INVALID_FILE_ATTRIBUTES) {
4626 DWORD newAttr = ((prms & perms::owner_write) == perms::owner_write) ? oldAttr & ~(static_cast<DWORD>(FILE_ATTRIBUTE_READONLY)) : oldAttr | FILE_ATTRIBUTE_READONLY;
4627 if (oldAttr == newAttr || SetFileAttributesW(GHC_NATIVEWP(p), newAttr)) {
4628 return;
4629 }
4630 }
4631 ec = detail::make_system_error();
4632 #else
4633 int mode = 0;
4634 if ((prms & perms::owner_read) == perms::owner_read) {
4635 mode |= _S_IREAD;
4636 }
4637 if ((prms & perms::owner_write) == perms::owner_write) {
4638 mode |= _S_IWRITE;
4639 }
4640 if (::_wchmod(p.wstring().c_str(), mode) != 0) {
4641 ec = detail::make_system_error();
4642 }
4643 #endif
4644 #else
4645 if ((opts & perm_options::nofollow) != perm_options::nofollow) {
4646 if (::chmod(p.c_str(), static_cast<mode_t>(prms)) != 0) {
4647 ec = detail::make_system_error();
4648 }
4649 }
4650 #endif
4651 }
4652
4653 #ifdef GHC_WITH_EXCEPTIONS
4654 GHC_INLINE path proximate(const path& p, std::error_code& ec)
4655 {
4656 auto cp = current_path(ec);
4657 if (!ec) {
4658 return proximate(p, cp, ec);
4659 }
4660 return path();
4661 }
4662 #endif
4663
4664 #ifdef GHC_WITH_EXCEPTIONS
4665 GHC_INLINE path proximate(const path& p, const path& base)
4666 {
4667 return weakly_canonical(p).lexically_proximate(weakly_canonical(base));
4668 }
4669 #endif
4670
4671 GHC_INLINE path proximate(const path& p, const path& base, std::error_code& ec)
4672 {
4673 return weakly_canonical(p, ec).lexically_proximate(weakly_canonical(base, ec));
4674 }
4675
4676 #ifdef GHC_WITH_EXCEPTIONS
4677 GHC_INLINE path read_symlink(const path& p)
4678 {
4679 std::error_code ec;
4680 auto result = read_symlink(p, ec);
4681 if (ec) {
4682 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
4683 }
4684 return result;
4685 }
4686 #endif
4687
4688 GHC_INLINE path read_symlink(const path& p, std::error_code& ec)
4689 {
4690 file_status fs = symlink_status(p, ec);
4691 if (fs.type() != file_type::symlink) {
4692 ec = detail::make_error_code(detail::portable_error::invalid_argument);
4693 return path();
4694 }
4695 auto result = detail::resolveSymlink(p, ec);
4696 return ec ? path() : result;
4697 }
4698
4699 GHC_INLINE path relative(const path& p, std::error_code& ec)
4700 {
4701 return relative(p, current_path(ec), ec);
4702 }
4703
4704 #ifdef GHC_WITH_EXCEPTIONS
4705 GHC_INLINE path relative(const path& p, const path& base)
4706 {
4707 return weakly_canonical(p).lexically_relative(weakly_canonical(base));
4708 }
4709 #endif
4710
4711 GHC_INLINE path relative(const path& p, const path& base, std::error_code& ec)
4712 {
4713 return weakly_canonical(p, ec).lexically_relative(weakly_canonical(base, ec));
4714 }
4715
4716 #ifdef GHC_WITH_EXCEPTIONS
4717 GHC_INLINE bool remove(const path& p)
4718 {
4719 std::error_code ec;
4720 auto result = remove(p, ec);
4721 if (ec) {
4722 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
4723 }
4724 return result;
4725 }
4726 #endif
4727
4728 GHC_INLINE bool remove(const path& p, std::error_code& ec) noexcept
4729 {
4730 ec.clear();
4731 #ifdef GHC_OS_WINDOWS
4732 #ifdef GHC_USE_WCHAR_T
4733 auto cstr = p.c_str();
4734 #else
4735 std::wstring np = detail::fromUtf8<std::wstring>(p.u8string());
4736 auto cstr = np.c_str();
4737 #endif
4738 DWORD attr = GetFileAttributesW(cstr);
4739 if (attr == INVALID_FILE_ATTRIBUTES) {
4740 auto error = ::GetLastError();
4741 if (error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND) {
4742 return false;
4743 }
4744 ec = detail::make_system_error(error);
4745 }
4746 else if (attr & FILE_ATTRIBUTE_READONLY) {
4747 auto new_attr = attr & ~static_cast<DWORD>(FILE_ATTRIBUTE_READONLY);
4748 if (!SetFileAttributesW(cstr, new_attr)) {
4749 auto error = ::GetLastError();
4750 ec = detail::make_system_error(error);
4751 }
4752 }
4753 if (!ec) {
4754 if (attr & FILE_ATTRIBUTE_DIRECTORY) {
4755 if (!RemoveDirectoryW(cstr)) {
4756 ec = detail::make_system_error();
4757 }
4758 }
4759 else {
4760 if (!DeleteFileW(cstr)) {
4761 ec = detail::make_system_error();
4762 }
4763 }
4764 }
4765 #else
4766 if (::remove(p.c_str()) == -1) {
4767 auto error = errno;
4768 if (error == ENOENT) {
4769 return false;
4770 }
4771 ec = detail::make_system_error();
4772 }
4773 #endif
4774 return ec ? false : true;
4775 }
4776
4777 #ifdef GHC_WITH_EXCEPTIONS
4778 GHC_INLINE uintmax_t remove_all(const path& p)
4779 {
4780 std::error_code ec;
4781 auto result = remove_all(p, ec);
4782 if (ec) {
4783 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
4784 }
4785 return result;
4786 }
4787 #endif
4788
4789 GHC_INLINE uintmax_t remove_all(const path& p, std::error_code& ec) noexcept
4790 {
4791 ec.clear();
4792 uintmax_t count = 0;
4793 if (p == "/") {
4794 ec = detail::make_error_code(detail::portable_error::not_supported);
4795 return static_cast<uintmax_t>(-1);
4796 }
4797 std::error_code tec;
4798 auto fs = symlink_status(p, tec);
4799 if (exists(fs) && is_directory(fs)) {
4800 for (auto iter = directory_iterator(p, ec); iter != directory_iterator(); iter.increment(ec)) {
4801 if (ec && !detail::is_not_found_error(ec)) {
4802 break;
4803 }
4804 bool is_symlink_result = iter->is_symlink(ec);
4805 if (ec)
4806 return static_cast<uintmax_t>(-1);
4807 if (!is_symlink_result && iter->is_directory(ec)) {
4808 count += remove_all(iter->path(), ec);
4809 if (ec) {
4810 return static_cast<uintmax_t>(-1);
4811 }
4812 }
4813 else {
4814 if (!ec) {
4815 remove(iter->path(), ec);
4816 }
4817 if (ec) {
4818 return static_cast<uintmax_t>(-1);
4819 }
4820 ++count;
4821 }
4822 }
4823 }
4824 if (!ec) {
4825 if (remove(p, ec)) {
4826 ++count;
4827 }
4828 }
4829 if (ec) {
4830 return static_cast<uintmax_t>(-1);
4831 }
4832 return count;
4833 }
4834
4835 #ifdef GHC_WITH_EXCEPTIONS
4836 GHC_INLINE void rename(const path& from, const path& to)
4837 {
4838 std::error_code ec;
4839 rename(from, to, ec);
4840 if (ec) {
4841 throw filesystem_error(detail::systemErrorText(ec.value()), from, to, ec);
4842 }
4843 }
4844 #endif
4845
4846 GHC_INLINE void rename(const path& from, const path& to, std::error_code& ec) noexcept
4847 {
4848 ec.clear();
4849 #ifdef GHC_OS_WINDOWS
4850 if (from != to) {
4851 if (!MoveFileExW(GHC_NATIVEWP(from), GHC_NATIVEWP(to), (DWORD)MOVEFILE_REPLACE_EXISTING)) {
4852 ec = detail::make_system_error();
4853 }
4854 }
4855 #else
4856 if (from != to) {
4857 if (::rename(from.c_str(), to.c_str()) != 0) {
4858 ec = detail::make_system_error();
4859 }
4860 }
4861 #endif
4862 }
4863
4864 #ifdef GHC_WITH_EXCEPTIONS
4865 GHC_INLINE void resize_file(const path& p, uintmax_t size)
4866 {
4867 std::error_code ec;
4868 resize_file(p, size, ec);
4869 if (ec) {
4870 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
4871 }
4872 }
4873 #endif
4874
4875 GHC_INLINE void resize_file(const path& p, uintmax_t size, std::error_code& ec) noexcept
4876 {
4877 ec.clear();
4878 #ifdef GHC_OS_WINDOWS
4879 LARGE_INTEGER lisize;
4880 lisize.QuadPart = static_cast<LONGLONG>(size);
4881 if (lisize.QuadPart < 0) {
4882 #ifdef ERROR_FILE_TOO_LARGE
4883 ec = detail::make_system_error(ERROR_FILE_TOO_LARGE);
4884 #else
4885 ec = detail::make_system_error(223);
4886 #endif
4887 return;
4888 }
4889 detail::unique_handle file(CreateFileW(GHC_NATIVEWP(p), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL));
4890 if (!file) {
4891 ec = detail::make_system_error();
4892 }
4893 else if (SetFilePointerEx(file.get(), lisize, NULL, FILE_BEGIN) == 0 || SetEndOfFile(file.get()) == 0) {
4894 ec = detail::make_system_error();
4895 }
4896 #else
4897 if (::truncate(p.c_str(), static_cast<off_t>(size)) != 0) {
4898 ec = detail::make_system_error();
4899 }
4900 #endif
4901 }
4902
4903 #ifdef GHC_WITH_EXCEPTIONS
4904 GHC_INLINE space_info space(const path& p)
4905 {
4906 std::error_code ec;
4907 auto result = space(p, ec);
4908 if (ec) {
4909 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
4910 }
4911 return result;
4912 }
4913 #endif
4914
4915 GHC_INLINE space_info space(const path& p, std::error_code& ec) noexcept
4916 {
4917 ec.clear();
4918 #ifdef GHC_OS_WINDOWS
4919 ULARGE_INTEGER freeBytesAvailableToCaller = {{ 0, 0 }};
4920 ULARGE_INTEGER totalNumberOfBytes = {{ 0, 0 }};
4921 ULARGE_INTEGER totalNumberOfFreeBytes = {{ 0, 0 }};
4922 if (!GetDiskFreeSpaceExW(GHC_NATIVEWP(p), &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes)) {
4923 ec = detail::make_system_error();
4924 return {static_cast<uintmax_t>(-1), static_cast<uintmax_t>(-1), static_cast<uintmax_t>(-1)};
4925 }
4926 return {static_cast<uintmax_t>(totalNumberOfBytes.QuadPart), static_cast<uintmax_t>(totalNumberOfFreeBytes.QuadPart), static_cast<uintmax_t>(freeBytesAvailableToCaller.QuadPart)};
4927 #else
4928 struct ::statvfs sfs;
4929 if (::statvfs(p.c_str(), &sfs) != 0) {
4930 ec = detail::make_system_error();
4931 return {static_cast<uintmax_t>(-1), static_cast<uintmax_t>(-1), static_cast<uintmax_t>(-1)};
4932 }
4933 return {static_cast<uintmax_t>(sfs.f_blocks) * static_cast<uintmax_t>(sfs.f_frsize), static_cast<uintmax_t>(sfs.f_bfree) * static_cast<uintmax_t>(sfs.f_frsize), static_cast<uintmax_t>(sfs.f_bavail) * static_cast<uintmax_t>(sfs.f_frsize)};
4934 #endif
4935 }
4936
4937 #ifdef GHC_WITH_EXCEPTIONS
4938 GHC_INLINE file_status status(const path& p)
4939 {
4940 std::error_code ec;
4941 auto result = status(p, ec);
4942 if (result.type() == file_type::none) {
4943 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
4944 }
4945 return result;
4946 }
4947 #endif
4948
4949 GHC_INLINE file_status status(const path& p, std::error_code& ec) noexcept
4950 {
4951 return detail::status_ex(p, ec);
4952 }
4953
4954 GHC_INLINE bool status_known(file_status s) noexcept
4955 {
4956 return s.type() != file_type::none;
4957 }
4958
4959 #ifdef GHC_WITH_EXCEPTIONS
4960 GHC_INLINE file_status symlink_status(const path& p)
4961 {
4962 std::error_code ec;
4963 auto result = symlink_status(p, ec);
4964 if (result.type() == file_type::none) {
4965 throw filesystem_error(detail::systemErrorText(ec.value()), ec);
4966 }
4967 return result;
4968 }
4969 #endif
4970
4971 GHC_INLINE file_status symlink_status(const path& p, std::error_code& ec) noexcept
4972 {
4973 return detail::symlink_status_ex(p, ec);
4974 }
4975
4976 #ifdef GHC_WITH_EXCEPTIONS
4977 GHC_INLINE path temp_directory_path()
4978 {
4979 std::error_code ec;
4980 path result = temp_directory_path(ec);
4981 if (ec) {
4982 throw filesystem_error(detail::systemErrorText(ec.value()), ec);
4983 }
4984 return result;
4985 }
4986 #endif
4987
4988 GHC_INLINE path temp_directory_path(std::error_code& ec) noexcept
4989 {
4990 ec.clear();
4991 #ifdef GHC_OS_WINDOWS
4992 wchar_t buffer[512];
4993 auto rc = GetTempPathW(511, buffer);
4994 if (!rc || rc > 511) {
4995 ec = detail::make_system_error();
4996 return path();
4997 }
4998 return path(std::wstring(buffer));
4999 #else
5000 static const char* temp_vars[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR", nullptr};
5001 const char* temp_path = nullptr;
5002 for (auto temp_name = temp_vars; *temp_name != nullptr; ++temp_name) {
5003 temp_path = std::getenv(*temp_name);
5004 if (temp_path) {
5005 return path(temp_path);
5006 }
5007 }
5008 return path("/tmp");
5009 #endif
5010 }
5011
5012 #ifdef GHC_WITH_EXCEPTIONS
5013 GHC_INLINE path weakly_canonical(const path& p)
5014 {
5015 std::error_code ec;
5016 auto result = weakly_canonical(p, ec);
5017 if (ec) {
5018 throw filesystem_error(detail::systemErrorText(ec.value()), p, ec);
5019 }
5020 return result;
5021 }
5022 #endif
5023
5024 GHC_INLINE path weakly_canonical(const path& p, std::error_code& ec) noexcept
5025 {
5026 path result;
5027 ec.clear();
5028 bool scan = true;
5029 for (auto pe : p) {
5030 if (scan) {
5031 std::error_code tec;
5032 if (exists(result / pe, tec)) {
5033 result /= pe;
5034 }
5035 else {
5036 if (ec) {
5037 return path();
5038 }
5039 scan = false;
5040 if (!result.empty()) {
5041 result = canonical(result, ec) / pe;
5042 if (ec) {
5043 break;
5044 }
5045 }
5046 else {
5047 result /= pe;
5048 }
5049 }
5050 }
5051 else {
5052 result /= pe;
5053 }
5054 }
5055 if (scan) {
5056 if (!result.empty()) {
5057 result = canonical(result, ec);
5058 }
5059 }
5060 return ec ? path() : result.lexically_normal();
5061 }
5062
5063 //-----------------------------------------------------------------------------
5064 // [fs.class.file_status] class file_status
5065 // [fs.file_status.cons] constructors and destructor
5066 GHC_INLINE file_status::file_status() noexcept
5067 : file_status(file_type::none)
5068 {
5069 }
5070
5071 GHC_INLINE file_status::file_status(file_type ft, perms prms) noexcept
5072 : _type(ft)
5073 , _perms(prms)
5074 {
5075 }
5076
5077 GHC_INLINE file_status::file_status(const file_status& other) noexcept
5078 : _type(other._type)
5079 , _perms(other._perms)
5080 {
5081 }
5082
5083 GHC_INLINE file_status::file_status(file_status&& other) noexcept
5084 : _type(other._type)
5085 , _perms(other._perms)
5086 {
5087 }
5088
5089 GHC_INLINE file_status::~file_status() {}
5090
5091 // assignments:
5092 GHC_INLINE file_status& file_status::operator=(const file_status& rhs) noexcept
5093 {
5094 _type = rhs._type;
5095 _perms = rhs._perms;
5096 return *this;
5097 }
5098
5099 GHC_INLINE file_status& file_status::operator=(file_status&& rhs) noexcept
5100 {
5101 _type = rhs._type;
5102 _perms = rhs._perms;
5103 return *this;
5104 }
5105
5106 // [fs.file_status.mods] modifiers
5107 GHC_INLINE void file_status::type(file_type ft) noexcept
5108 {
5109 _type = ft;
5110 }
5111
5112 GHC_INLINE void file_status::permissions(perms prms) noexcept
5113 {
5114 _perms = prms;
5115 }
5116
5117 // [fs.file_status.obs] observers
5118 GHC_INLINE file_type file_status::type() const noexcept
5119 {
5120 return _type;
5121 }
5122
5123 GHC_INLINE perms file_status::permissions() const noexcept
5124 {
5125 return _perms;
5126 }
5127
5128 //-----------------------------------------------------------------------------
5129 // [fs.class.directory_entry] class directory_entry
5130 // [fs.dir.entry.cons] constructors and destructor
5131 // directory_entry::directory_entry() noexcept = default;
5132 // directory_entry::directory_entry(const directory_entry&) = default;
5133 // directory_entry::directory_entry(directory_entry&&) noexcept = default;
5134 #ifdef GHC_WITH_EXCEPTIONS
5135 GHC_INLINE directory_entry::directory_entry(const filesystem::path& p)
5136 : _path(p)
5137 , _file_size(static_cast<uintmax_t>(-1))
5138 #ifndef GHC_OS_WINDOWS
5139 , _hard_link_count(static_cast<uintmax_t>(-1))
5140 #endif
5141 , _last_write_time(0)
5142 {
5143 refresh();
5144 }
5145 #endif
5146
5147 GHC_INLINE directory_entry::directory_entry(const filesystem::path& p, std::error_code& ec)
5148 : _path(p)
5149 , _file_size(static_cast<uintmax_t>(-1))
5150 #ifndef GHC_OS_WINDOWS
5151 , _hard_link_count(static_cast<uintmax_t>(-1))
5152 #endif
5153 , _last_write_time(0)
5154 {
5155 refresh(ec);
5156 }
5157
5158 GHC_INLINE directory_entry::~directory_entry() {}
5159
5160 // assignments:
5161 // directory_entry& directory_entry::operator=(const directory_entry&) = default;
5162 // directory_entry& directory_entry::operator=(directory_entry&&) noexcept = default;
5163
5164 // [fs.dir.entry.mods] directory_entry modifiers
5165 #ifdef GHC_WITH_EXCEPTIONS
5166 GHC_INLINE void directory_entry::assign(const filesystem::path& p)
5167 {
5168 _path = p;
5169 refresh();
5170 }
5171 #endif
5172
5173 GHC_INLINE void directory_entry::assign(const filesystem::path& p, std::error_code& ec)
5174 {
5175 _path = p;
5176 refresh(ec);
5177 }
5178
5179 #ifdef GHC_WITH_EXCEPTIONS
5180 GHC_INLINE void directory_entry::replace_filename(const filesystem::path& p)
5181 {
5182 _path.replace_filename(p);
5183 refresh();
5184 }
5185 #endif
5186
5187 GHC_INLINE void directory_entry::replace_filename(const filesystem::path& p, std::error_code& ec)
5188 {
5189 _path.replace_filename(p);
5190 refresh(ec);
5191 }
5192
5193 #ifdef GHC_WITH_EXCEPTIONS
5194 GHC_INLINE void directory_entry::refresh()
5195 {
5196 std::error_code ec;
5197 refresh(ec);
5198 if (ec) {
5199 throw filesystem_error(detail::systemErrorText(ec.value()), _path, ec);
5200 }
5201 }
5202 #endif
5203
5204 GHC_INLINE void directory_entry::refresh(std::error_code& ec) noexcept
5205 {
5206 #ifdef GHC_OS_WINDOWS
5207 _status = detail::status_ex(_path, ec, &_symlink_status, &_file_size, nullptr, &_last_write_time);
5208 #else
5209 _status = detail::status_ex(_path, ec, &_symlink_status, &_file_size, &_hard_link_count, &_last_write_time);
5210 #endif
5211 }
5212
5213 // [fs.dir.entry.obs] directory_entry observers
5214 GHC_INLINE const filesystem::path& directory_entry::path() const noexcept
5215 {
5216 return _path;
5217 }
5218
5219 GHC_INLINE directory_entry::operator const filesystem::path&() const noexcept
5220 {
5221 return _path;
5222 }
5223
5224 #ifdef GHC_WITH_EXCEPTIONS
5225 GHC_INLINE file_type directory_entry::status_file_type() const
5226 {
5227 return _status.type() != file_type::none ? _status.type() : filesystem::status(path()).type();
5228 }
5229 #endif
5230
5231 GHC_INLINE file_type directory_entry::status_file_type(std::error_code& ec) const noexcept
5232 {
5233 if (_status.type() != file_type::none) {
5234 ec.clear();
5235 return _status.type();
5236 }
5237 return filesystem::status(path(), ec).type();
5238 }
5239
5240 #ifdef GHC_WITH_EXCEPTIONS
5241 GHC_INLINE bool directory_entry::exists() const
5242 {
5243 return status_file_type() != file_type::not_found;
5244 }
5245 #endif
5246
5247 GHC_INLINE bool directory_entry::exists(std::error_code& ec) const noexcept
5248 {
5249 return status_file_type(ec) != file_type::not_found;
5250 }
5251
5252 #ifdef GHC_WITH_EXCEPTIONS
5253 GHC_INLINE bool directory_entry::is_block_file() const
5254 {
5255 return status_file_type() == file_type::block;
5256 }
5257 #endif
5258 GHC_INLINE bool directory_entry::is_block_file(std::error_code& ec) const noexcept
5259 {
5260 return status_file_type(ec) == file_type::block;
5261 }
5262
5263 #ifdef GHC_WITH_EXCEPTIONS
5264 GHC_INLINE bool directory_entry::is_character_file() const
5265 {
5266 return status_file_type() == file_type::character;
5267 }
5268 #endif
5269
5270 GHC_INLINE bool directory_entry::is_character_file(std::error_code& ec) const noexcept
5271 {
5272 return status_file_type(ec) == file_type::character;
5273 }
5274
5275 #ifdef GHC_WITH_EXCEPTIONS
5276 GHC_INLINE bool directory_entry::is_directory() const
5277 {
5278 return status_file_type() == file_type::directory;
5279 }
5280 #endif
5281
5282 GHC_INLINE bool directory_entry::is_directory(std::error_code& ec) const noexcept
5283 {
5284 return status_file_type(ec) == file_type::directory;
5285 }
5286
5287 #ifdef GHC_WITH_EXCEPTIONS
5288 GHC_INLINE bool directory_entry::is_fifo() const
5289 {
5290 return status_file_type() == file_type::fifo;
5291 }
5292 #endif
5293
5294 GHC_INLINE bool directory_entry::is_fifo(std::error_code& ec) const noexcept
5295 {
5296 return status_file_type(ec) == file_type::fifo;
5297 }
5298
5299 #ifdef GHC_WITH_EXCEPTIONS
5300 GHC_INLINE bool directory_entry::is_other() const
5301 {
5302 auto ft = status_file_type();
5303 return ft != file_type::none && ft != file_type::not_found && ft != file_type::regular && ft != file_type::directory && !is_symlink();
5304 }
5305 #endif
5306
5307 GHC_INLINE bool directory_entry::is_other(std::error_code& ec) const noexcept
5308 {
5309 auto ft = status_file_type(ec);
5310 bool other = ft != file_type::none && ft != file_type::not_found && ft != file_type::regular && ft != file_type::directory && !is_symlink(ec);
5311 return !ec && other;
5312 }
5313
5314 #ifdef GHC_WITH_EXCEPTIONS
5315 GHC_INLINE bool directory_entry::is_regular_file() const
5316 {
5317 return status_file_type() == file_type::regular;
5318 }
5319 #endif
5320
5321 GHC_INLINE bool directory_entry::is_regular_file(std::error_code& ec) const noexcept
5322 {
5323 return status_file_type(ec) == file_type::regular;
5324 }
5325
5326 #ifdef GHC_WITH_EXCEPTIONS
5327 GHC_INLINE bool directory_entry::is_socket() const
5328 {
5329 return status_file_type() == file_type::socket;
5330 }
5331 #endif
5332
5333 GHC_INLINE bool directory_entry::is_socket(std::error_code& ec) const noexcept
5334 {
5335 return status_file_type(ec) == file_type::socket;
5336 }
5337
5338 #ifdef GHC_WITH_EXCEPTIONS
5339 GHC_INLINE bool directory_entry::is_symlink() const
5340 {
5341 return _symlink_status.type() != file_type::none ? _symlink_status.type() == file_type::symlink : filesystem::is_symlink(symlink_status());
5342 }
5343 #endif
5344
5345 GHC_INLINE bool directory_entry::is_symlink(std::error_code& ec) const noexcept
5346 {
5347 if (_symlink_status.type() != file_type::none) {
5348 ec.clear();
5349 return _symlink_status.type() == file_type::symlink;
5350 }
5351 return filesystem::is_symlink(symlink_status(ec));
5352 }
5353
5354 #ifdef GHC_WITH_EXCEPTIONS
5355 GHC_INLINE uintmax_t directory_entry::file_size() const
5356 {
5357 if (_file_size != static_cast<uintmax_t>(-1)) {
5358 return _file_size;
5359 }
5360 return filesystem::file_size(path());
5361 }
5362 #endif
5363
5364 GHC_INLINE uintmax_t directory_entry::file_size(std::error_code& ec) const noexcept
5365 {
5366 if (_file_size != static_cast<uintmax_t>(-1)) {
5367 ec.clear();
5368 return _file_size;
5369 }
5370 return filesystem::file_size(path(), ec);
5371 }
5372
5373 #ifndef GHC_OS_WEB
5374 #ifdef GHC_WITH_EXCEPTIONS
5375 GHC_INLINE uintmax_t directory_entry::hard_link_count() const
5376 {
5377 #ifndef GHC_OS_WINDOWS
5378 if (_hard_link_count != static_cast<uintmax_t>(-1)) {
5379 return _hard_link_count;
5380 }
5381 #endif
5382 return filesystem::hard_link_count(path());
5383 }
5384 #endif
5385
5386 GHC_INLINE uintmax_t directory_entry::hard_link_count(std::error_code& ec) const noexcept
5387 {
5388 #ifndef GHC_OS_WINDOWS
5389 if (_hard_link_count != static_cast<uintmax_t>(-1)) {
5390 ec.clear();
5391 return _hard_link_count;
5392 }
5393 #endif
5394 return filesystem::hard_link_count(path(), ec);
5395 }
5396 #endif
5397
5398 #ifdef GHC_WITH_EXCEPTIONS
5399 GHC_INLINE file_time_type directory_entry::last_write_time() const
5400 {
5401 if (_last_write_time != 0) {
5402 return std::chrono::system_clock::from_time_t(_last_write_time);
5403 }
5404 return filesystem::last_write_time(path());
5405 }
5406 #endif
5407
5408 GHC_INLINE file_time_type directory_entry::last_write_time(std::error_code& ec) const noexcept
5409 {
5410 if (_last_write_time != 0) {
5411 ec.clear();
5412 return std::chrono::system_clock::from_time_t(_last_write_time);
5413 }
5414 return filesystem::last_write_time(path(), ec);
5415 }
5416
5417 #ifdef GHC_WITH_EXCEPTIONS
5418 GHC_INLINE file_status directory_entry::status() const
5419 {
5420 if (_status.type() != file_type::none && _status.permissions() != perms::unknown) {
5421 return _status;
5422 }
5423 return filesystem::status(path());
5424 }
5425 #endif
5426
5427 GHC_INLINE file_status directory_entry::status(std::error_code& ec) const noexcept
5428 {
5429 if (_status.type() != file_type::none && _status.permissions() != perms::unknown) {
5430 ec.clear();
5431 return _status;
5432 }
5433 return filesystem::status(path(), ec);
5434 }
5435
5436 #ifdef GHC_WITH_EXCEPTIONS
5437 GHC_INLINE file_status directory_entry::symlink_status() const
5438 {
5439 if (_symlink_status.type() != file_type::none && _symlink_status.permissions() != perms::unknown) {
5440 return _symlink_status;
5441 }
5442 return filesystem::symlink_status(path());
5443 }
5444 #endif
5445
5446 GHC_INLINE file_status directory_entry::symlink_status(std::error_code& ec) const noexcept
5447 {
5448 if (_symlink_status.type() != file_type::none && _symlink_status.permissions() != perms::unknown) {
5449 ec.clear();
5450 return _symlink_status;
5451 }
5452 return filesystem::symlink_status(path(), ec);
5453 }
5454
5455 #ifdef GHC_HAS_THREEWAY_COMP
5456 GHC_INLINE std::strong_ordering directory_entry::operator<=>(const directory_entry& rhs) const noexcept
5457 {
5458 return _path <=> rhs._path;
5459 }
5460 #endif
5461
5462 GHC_INLINE bool directory_entry::operator<(const directory_entry& rhs) const noexcept
5463 {
5464 return _path < rhs._path;
5465 }
5466
5467 GHC_INLINE bool directory_entry::operator==(const directory_entry& rhs) const noexcept
5468 {
5469 return _path == rhs._path;
5470 }
5471
5472 GHC_INLINE bool directory_entry::operator!=(const directory_entry& rhs) const noexcept
5473 {
5474 return _path != rhs._path;
5475 }
5476
5477 GHC_INLINE bool directory_entry::operator<=(const directory_entry& rhs) const noexcept
5478 {
5479 return _path <= rhs._path;
5480 }
5481
5482 GHC_INLINE bool directory_entry::operator>(const directory_entry& rhs) const noexcept
5483 {
5484 return _path > rhs._path;
5485 }
5486
5487 GHC_INLINE bool directory_entry::operator>=(const directory_entry& rhs) const noexcept
5488 {
5489 return _path >= rhs._path;
5490 }
5491
5492 //-----------------------------------------------------------------------------
5493 // [fs.class.directory_iterator] class directory_iterator
5494
5495 #ifdef GHC_OS_WINDOWS
5496 class directory_iterator::impl
5497 {
5498 public:
5499 impl(const path& p, directory_options options)
5500 : _base(p)
5501 , _options(options)
5502 , _dirHandle(INVALID_HANDLE_VALUE)
5503 {
5504 if (!_base.empty()) {
5505 ZeroMemory(&_findData, sizeof(WIN32_FIND_DATAW));
5506 if ((_dirHandle = FindFirstFileW(GHC_NATIVEWP((_base / "*")), &_findData)) != INVALID_HANDLE_VALUE) {
5507 if (std::wstring(_findData.cFileName) == L"." || std::wstring(_findData.cFileName) == L"..") {
5508 increment(_ec);
5509 }
5510 else {
5511 _dir_entry._path = _base / std::wstring(_findData.cFileName);
5512 copyToDirEntry(_ec);
5513 }
5514 }
5515 else {
5516 auto error = ::GetLastError();
5517 _base = filesystem::path();
5518 if (error != ERROR_ACCESS_DENIED || (options & directory_options::skip_permission_denied) == directory_options::none) {
5519 _ec = detail::make_system_error();
5520 }
5521 }
5522 }
5523 }
5524 impl(const impl& other) = delete;
5525 ~impl()
5526 {
5527 if (_dirHandle != INVALID_HANDLE_VALUE) {
5528 FindClose(_dirHandle);
5529 _dirHandle = INVALID_HANDLE_VALUE;
5530 }
5531 }
5532 void increment(std::error_code& ec)
5533 {
5534 if (_dirHandle != INVALID_HANDLE_VALUE) {
5535 do {
5536 if (FindNextFileW(_dirHandle, &_findData)) {
5537 _dir_entry._path = _base;
5538 #ifdef GHC_USE_WCHAR_T
5539 _dir_entry._path.append_name(_findData.cFileName);
5540 #else
5541 #ifdef GHC_RAISE_UNICODE_ERRORS
5542 try {
5543 _dir_entry._path.append_name(detail::toUtf8(_findData.cFileName).c_str());
5544 }
5545 catch (filesystem_error& fe) {
5546 ec = fe.code();
5547 return;
5548 }
5549 #else
5550 _dir_entry._path.append_name(detail::toUtf8(_findData.cFileName).c_str());
5551 #endif
5552 #endif
5553 copyToDirEntry(ec);
5554 }
5555 else {
5556 auto err = ::GetLastError();
5557 if (err != ERROR_NO_MORE_FILES) {
5558 _ec = ec = detail::make_system_error(err);
5559 }
5560 FindClose(_dirHandle);
5561 _dirHandle = INVALID_HANDLE_VALUE;
5562 _dir_entry._path.clear();
5563 break;
5564 }
5565 } while (std::wstring(_findData.cFileName) == L"." || std::wstring(_findData.cFileName) == L"..");
5566 }
5567 else {
5568 ec = _ec;
5569 }
5570 }
5571 void copyToDirEntry(std::error_code& ec)
5572 {
5573 if (_findData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
5574 _dir_entry._status = detail::status_ex(_dir_entry._path, ec, &_dir_entry._symlink_status, &_dir_entry._file_size, nullptr, &_dir_entry._last_write_time);
5575 }
5576 else {
5577 _dir_entry._status = detail::status_from_INFO(_dir_entry._path, &_findData, ec, &_dir_entry._file_size, &_dir_entry._last_write_time);
5578 _dir_entry._symlink_status = _dir_entry._status;
5579 }
5580 if (ec) {
5581 if (_dir_entry._status.type() != file_type::none && _dir_entry._symlink_status.type() != file_type::none) {
5582 ec.clear();
5583 }
5584 else {
5585 _dir_entry._file_size = static_cast<uintmax_t>(-1);
5586 _dir_entry._last_write_time = 0;
5587 }
5588 }
5589 }
5590 path _base;
5591 directory_options _options;
5592 WIN32_FIND_DATAW _findData;
5593 HANDLE _dirHandle;
5594 directory_entry _dir_entry;
5595 std::error_code _ec;
5596 };
5597 #else
5598 // POSIX implementation
5599 class directory_iterator::impl
5600 {
5601 public:
5602 impl(const path& path, directory_options options)
5603 : _base(path)
5604 , _options(options)
5605 , _dir(nullptr)
5606 , _entry(nullptr)
5607 {
5608 if (!path.empty()) {
5609 _dir = ::opendir(path.native().c_str());
5610 if (!_dir) {
5611 auto error = errno;
5612 _base = filesystem::path();
5613 if ((error != EACCES && error != EPERM) || (options & directory_options::skip_permission_denied) == directory_options::none) {
5614 _ec = detail::make_system_error();
5615 }
5616 }
5617 else {
5618 increment(_ec);
5619 }
5620 }
5621 }
5622 impl(const impl& other) = delete;
5623 ~impl()
5624 {
5625 if (_dir) {
5626 ::closedir(_dir);
5627 }
5628 }
5629 void increment(std::error_code& ec)
5630 {
5631 if (_dir) {
5632 bool skip;
5633 do {
5634 skip = false;
5635 errno = 0;
5636 _entry = ::readdir(_dir);
5637 if (_entry) {
5638 _dir_entry._path = _base;
5639 _dir_entry._path.append_name(_entry->d_name);
5640 copyToDirEntry();
5641 if (ec && (ec.value() == EACCES || ec.value() == EPERM) && (_options & directory_options::skip_permission_denied) == directory_options::skip_permission_denied) {
5642 ec.clear();
5643 skip = true;
5644 }
5645 }
5646 else {
5647 ::closedir(_dir);
5648 _dir = nullptr;
5649 _dir_entry._path.clear();
5650 if (errno) {
5651 ec = detail::make_system_error();
5652 }
5653 break;
5654 }
5655 } while (skip || std::strcmp(_entry->d_name, ".") == 0 || std::strcmp(_entry->d_name, "..") == 0);
5656 }
5657 }
5658
5659 void copyToDirEntry()
5660 {
5661 #ifdef GHC_NO_DIRENT_D_TYPE
5662 _dir_entry._symlink_status = file_status();
5663 _dir_entry._status = file_status();
5664 #else
5665 _dir_entry._symlink_status.permissions(perms::unknown);
5666 switch (_entry->d_type) {
5667 case DT_BLK:
5668 _dir_entry._symlink_status.type(file_type::block);
5669 break;
5670 case DT_CHR:
5671 _dir_entry._symlink_status.type(file_type::character);
5672 break;
5673 case DT_DIR:
5674 _dir_entry._symlink_status.type(file_type::directory);
5675 break;
5676 case DT_FIFO:
5677 _dir_entry._symlink_status.type(file_type::fifo);
5678 break;
5679 case DT_LNK:
5680 _dir_entry._symlink_status.type(file_type::symlink);
5681 break;
5682 case DT_REG:
5683 _dir_entry._symlink_status.type(file_type::regular);
5684 break;
5685 case DT_SOCK:
5686 _dir_entry._symlink_status.type(file_type::socket);
5687 break;
5688 case DT_UNKNOWN:
5689 _dir_entry._symlink_status.type(file_type::none);
5690 break;
5691 default:
5692 _dir_entry._symlink_status.type(file_type::unknown);
5693 break;
5694 }
5695 if (_entry->d_type != DT_LNK) {
5696 _dir_entry._status = _dir_entry._symlink_status;
5697 }
5698 else {
5699 _dir_entry._status.type(file_type::none);
5700 _dir_entry._status.permissions(perms::unknown);
5701 }
5702 #endif
5703 _dir_entry._file_size = static_cast<uintmax_t>(-1);
5704 _dir_entry._hard_link_count = static_cast<uintmax_t>(-1);
5705 _dir_entry._last_write_time = 0;
5706 }
5707 path _base;
5708 directory_options _options;
5709 DIR* _dir;
5710 struct ::dirent* _entry;
5711 directory_entry _dir_entry;
5712 std::error_code _ec;
5713 };
5714 #endif
5715
5716 // [fs.dir.itr.members] member functions
5717 GHC_INLINE directory_iterator::directory_iterator() noexcept
5718 : _impl(new impl(path(), directory_options::none))
5719 {
5720 }
5721
5722 #ifdef GHC_WITH_EXCEPTIONS
5723 GHC_INLINE directory_iterator::directory_iterator(const path& p)
5724 : _impl(new impl(p, directory_options::none))
5725 {
5726 if (_impl->_ec) {
5727 throw filesystem_error(detail::systemErrorText(_impl->_ec.value()), p, _impl->_ec);
5728 }
5729 _impl->_ec.clear();
5730 }
5731
5732 GHC_INLINE directory_iterator::directory_iterator(const path& p, directory_options options)
5733 : _impl(new impl(p, options))
5734 {
5735 if (_impl->_ec) {
5736 throw filesystem_error(detail::systemErrorText(_impl->_ec.value()), p, _impl->_ec);
5737 }
5738 }
5739 #endif
5740
5741 GHC_INLINE directory_iterator::directory_iterator(const path& p, std::error_code& ec) noexcept
5742 : _impl(new impl(p, directory_options::none))
5743 {
5744 if (_impl->_ec) {
5745 ec = _impl->_ec;
5746 }
5747 }
5748
5749 GHC_INLINE directory_iterator::directory_iterator(const path& p, directory_options options, std::error_code& ec) noexcept
5750 : _impl(new impl(p, options))
5751 {
5752 if (_impl->_ec) {
5753 ec = _impl->_ec;
5754 }
5755 }
5756
5757 GHC_INLINE directory_iterator::directory_iterator(const directory_iterator& rhs)
5758 : _impl(rhs._impl)
5759 {
5760 }
5761
5762 GHC_INLINE directory_iterator::directory_iterator(directory_iterator&& rhs) noexcept
5763 : _impl(std::move(rhs._impl))
5764 {
5765 }
5766
5767 GHC_INLINE directory_iterator::~directory_iterator() {}
5768
5769 GHC_INLINE directory_iterator& directory_iterator::operator=(const directory_iterator& rhs)
5770 {
5771 _impl = rhs._impl;
5772 return *this;
5773 }
5774
5775 GHC_INLINE directory_iterator& directory_iterator::operator=(directory_iterator&& rhs) noexcept
5776 {
5777 _impl = std::move(rhs._impl);
5778 return *this;
5779 }
5780
5781 GHC_INLINE const directory_entry& directory_iterator::operator*() const
5782 {
5783 return _impl->_dir_entry;
5784 }
5785
5786 GHC_INLINE const directory_entry* directory_iterator::operator->() const
5787 {
5788 return &_impl->_dir_entry;
5789 }
5790
5791 #ifdef GHC_WITH_EXCEPTIONS
5792 GHC_INLINE directory_iterator& directory_iterator::operator++()
5793 {
5794 std::error_code ec;
5795 _impl->increment(ec);
5796 if (ec) {
5797 throw filesystem_error(detail::systemErrorText(ec.value()), _impl->_dir_entry._path, ec);
5798 }
5799 return *this;
5800 }
5801 #endif
5802
5803 GHC_INLINE directory_iterator& directory_iterator::increment(std::error_code& ec) noexcept
5804 {
5805 _impl->increment(ec);
5806 return *this;
5807 }
5808
5809 GHC_INLINE bool directory_iterator::operator==(const directory_iterator& rhs) const
5810 {
5811 return _impl->_dir_entry._path == rhs._impl->_dir_entry._path;
5812 }
5813
5814 GHC_INLINE bool directory_iterator::operator!=(const directory_iterator& rhs) const
5815 {
5816 return _impl->_dir_entry._path != rhs._impl->_dir_entry._path;
5817 }
5818
5819 // [fs.dir.itr.nonmembers] directory_iterator non-member functions
5820
5821 GHC_INLINE directory_iterator begin(directory_iterator iter) noexcept
5822 {
5823 return iter;
5824 }
5825
5826 GHC_INLINE directory_iterator end(const directory_iterator&) noexcept
5827 {
5828 return directory_iterator();
5829 }
5830
5831 //-----------------------------------------------------------------------------
5832 // [fs.class.rec.dir.itr] class recursive_directory_iterator
5833
5834 GHC_INLINE recursive_directory_iterator::recursive_directory_iterator() noexcept
5835 : _impl(new recursive_directory_iterator_impl(directory_options::none, true))
5836 {
5837 _impl->_dir_iter_stack.push(directory_iterator());
5838 }
5839
5840 #ifdef GHC_WITH_EXCEPTIONS
5841 GHC_INLINE recursive_directory_iterator::recursive_directory_iterator(const path& p)
5842 : _impl(new recursive_directory_iterator_impl(directory_options::none, true))
5843 {
5844 _impl->_dir_iter_stack.push(directory_iterator(p));
5845 }
5846
5847 GHC_INLINE recursive_directory_iterator::recursive_directory_iterator(const path& p, directory_options options)
5848 : _impl(new recursive_directory_iterator_impl(options, true))
5849 {
5850 _impl->_dir_iter_stack.push(directory_iterator(p, options));
5851 }
5852 #endif
5853
5854 GHC_INLINE recursive_directory_iterator::recursive_directory_iterator(const path& p, directory_options options, std::error_code& ec) noexcept
5855 : _impl(new recursive_directory_iterator_impl(options, true))
5856 {
5857 _impl->_dir_iter_stack.push(directory_iterator(p, options, ec));
5858 }
5859
5860 GHC_INLINE recursive_directory_iterator::recursive_directory_iterator(const path& p, std::error_code& ec) noexcept
5861 : _impl(new recursive_directory_iterator_impl(directory_options::none, true))
5862 {
5863 _impl->_dir_iter_stack.push(directory_iterator(p, ec));
5864 }
5865
5866 GHC_INLINE recursive_directory_iterator::recursive_directory_iterator(const recursive_directory_iterator& rhs)
5867 : _impl(rhs._impl)
5868 {
5869 }
5870
5871 GHC_INLINE recursive_directory_iterator::recursive_directory_iterator(recursive_directory_iterator&& rhs) noexcept
5872 : _impl(std::move(rhs._impl))
5873 {
5874 }
5875
5876 GHC_INLINE recursive_directory_iterator::~recursive_directory_iterator() {}
5877
5878 // [fs.rec.dir.itr.members] observers
5879 GHC_INLINE directory_options recursive_directory_iterator::options() const
5880 {
5881 return _impl->_options;
5882 }
5883
5884 GHC_INLINE int recursive_directory_iterator::depth() const
5885 {
5886 return static_cast<int>(_impl->_dir_iter_stack.size() - 1);
5887 }
5888
5889 GHC_INLINE bool recursive_directory_iterator::recursion_pending() const
5890 {
5891 return _impl->_recursion_pending;
5892 }
5893
5894 GHC_INLINE const directory_entry& recursive_directory_iterator::operator*() const
5895 {
5896 return *(_impl->_dir_iter_stack.top());
5897 }
5898
5899 GHC_INLINE const directory_entry* recursive_directory_iterator::operator->() const
5900 {
5901 return &(*(_impl->_dir_iter_stack.top()));
5902 }
5903
5904 // [fs.rec.dir.itr.members] modifiers recursive_directory_iterator&
5905 GHC_INLINE recursive_directory_iterator& recursive_directory_iterator::operator=(const recursive_directory_iterator& rhs)
5906 {
5907 _impl = rhs._impl;
5908 return *this;
5909 }
5910
5911 GHC_INLINE recursive_directory_iterator& recursive_directory_iterator::operator=(recursive_directory_iterator&& rhs) noexcept
5912 {
5913 _impl = std::move(rhs._impl);
5914 return *this;
5915 }
5916
5917 #ifdef GHC_WITH_EXCEPTIONS
5918 GHC_INLINE recursive_directory_iterator& recursive_directory_iterator::operator++()
5919 {
5920 std::error_code ec;
5921 increment(ec);
5922 if (ec) {
5923 throw filesystem_error(detail::systemErrorText(ec.value()), _impl->_dir_iter_stack.empty() ? path() : _impl->_dir_iter_stack.top()->path(), ec);
5924 }
5925 return *this;
5926 }
5927 #endif
5928
5929 GHC_INLINE recursive_directory_iterator& recursive_directory_iterator::increment(std::error_code& ec) noexcept
5930 {
5931 bool isSymLink = (*this)->is_symlink(ec);
5932 bool isDir = !ec && (*this)->is_directory(ec);
5933 if (isSymLink && detail::is_not_found_error(ec)) {
5934 ec.clear();
5935 }
5936 if (!ec) {
5937 if (recursion_pending() && isDir && (!isSymLink || (options() & directory_options::follow_directory_symlink) != directory_options::none)) {
5938 _impl->_dir_iter_stack.push(directory_iterator((*this)->path(), _impl->_options, ec));
5939 }
5940 else {
5941 _impl->_dir_iter_stack.top().increment(ec);
5942 }
5943 if (!ec) {
5944 while (depth() && _impl->_dir_iter_stack.top() == directory_iterator()) {
5945 _impl->_dir_iter_stack.pop();
5946 _impl->_dir_iter_stack.top().increment(ec);
5947 }
5948 }
5949 else if (!_impl->_dir_iter_stack.empty()) {
5950 _impl->_dir_iter_stack.pop();
5951 }
5952 _impl->_recursion_pending = true;
5953 }
5954 return *this;
5955 }
5956
5957 #ifdef GHC_WITH_EXCEPTIONS
5958 GHC_INLINE void recursive_directory_iterator::pop()
5959 {
5960 std::error_code ec;
5961 pop(ec);
5962 if (ec) {
5963 throw filesystem_error(detail::systemErrorText(ec.value()), _impl->_dir_iter_stack.empty() ? path() : _impl->_dir_iter_stack.top()->path(), ec);
5964 }
5965 }
5966 #endif
5967
5968 GHC_INLINE void recursive_directory_iterator::pop(std::error_code& ec)
5969 {
5970 if (depth() == 0) {
5971 *this = recursive_directory_iterator();
5972 }
5973 else {
5974 do {
5975 _impl->_dir_iter_stack.pop();
5976 _impl->_dir_iter_stack.top().increment(ec);
5977 } while (depth() && _impl->_dir_iter_stack.top() == directory_iterator());
5978 }
5979 }
5980
5981 GHC_INLINE void recursive_directory_iterator::disable_recursion_pending()
5982 {
5983 _impl->_recursion_pending = false;
5984 }
5985
5986 // other members as required by [input.iterators]
5987 GHC_INLINE bool recursive_directory_iterator::operator==(const recursive_directory_iterator& rhs) const
5988 {
5989 return _impl->_dir_iter_stack.top() == rhs._impl->_dir_iter_stack.top();
5990 }
5991
5992 GHC_INLINE bool recursive_directory_iterator::operator!=(const recursive_directory_iterator& rhs) const
5993 {
5994 return _impl->_dir_iter_stack.top() != rhs._impl->_dir_iter_stack.top();
5995 }
5996
5997 // [fs.rec.dir.itr.nonmembers] directory_iterator non-member functions
5998 GHC_INLINE recursive_directory_iterator begin(recursive_directory_iterator iter) noexcept
5999 {
6000 return iter;
6001 }
6002
6003 GHC_INLINE recursive_directory_iterator end(const recursive_directory_iterator&) noexcept
6004 {
6005 return recursive_directory_iterator();
6006 }
6007
6008 #endif // GHC_EXPAND_IMPL
6009
6010 } // namespace filesystem
6011 } // namespace ghc
6012
6013 // cleanup some macros
6014 #undef GHC_INLINE
6015 #undef GHC_EXPAND_IMPL
6016
6017 #endif // GHC_FILESYSTEM_H