You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

6072 lines
187 KiB

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