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.

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