filesystem 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_FILESYSTEM
  10. #define _LIBCPP_FILESYSTEM
  11. /*
  12. filesystem synopsis
  13. namespace std::filesystem {
  14. // `class path` from http://eel.is/c++draft/fs.class.path.general#6
  15. class path {
  16. public:
  17. using value_type = see below;
  18. using string_type = basic_string<value_type>;
  19. static constexpr value_type preferred_separator = see below;
  20. enum format;
  21. path() noexcept;
  22. path(const path& p);
  23. path(path&& p) noexcept;
  24. path(string_type&& source, format fmt = auto_format);
  25. template<class Source>
  26. path(const Source& source, format fmt = auto_format);
  27. template<class InputIterator>
  28. path(InputIterator first, InputIterator last, format fmt = auto_format);
  29. template<class Source>
  30. path(const Source& source, const locale& loc, format fmt = auto_format);
  31. template<class InputIterator>
  32. path(InputIterator first, InputIterator last, const locale& loc, format fmt = auto_format);
  33. ~path();
  34. path& operator=(const path& p);
  35. path& operator=(path&& p) noexcept;
  36. path& operator=(string_type&& source);
  37. path& assign(string_type&& source);
  38. template<class Source>
  39. path& operator=(const Source& source);
  40. template<class Source>
  41. path& assign(const Source& source);
  42. template<class InputIterator>
  43. path& assign(InputIterator first, InputIterator last);
  44. path& operator/=(const path& p);
  45. template<class Source>
  46. path& operator/=(const Source& source);
  47. template<class Source>
  48. path& append(const Source& source);
  49. template<class InputIterator>
  50. path& append(InputIterator first, InputIterator last);
  51. path& operator+=(const path& x);
  52. path& operator+=(const string_type& x);
  53. path& operator+=(basic_string_view<value_type> x);
  54. path& operator+=(const value_type* x);
  55. path& operator+=(value_type x);
  56. template<class Source>
  57. path& operator+=(const Source& x);
  58. template<class EcharT>
  59. path& operator+=(EcharT x);
  60. template<class Source>
  61. path& concat(const Source& x);
  62. template<class InputIterator>
  63. path& concat(InputIterator first, InputIterator last);
  64. void clear() noexcept;
  65. path& make_preferred();
  66. path& remove_filename();
  67. path& replace_filename(const path& replacement);
  68. path& replace_extension(const path& replacement = path());
  69. void swap(path& rhs) noexcept;
  70. friend bool operator==(const path& lhs, const path& rhs) noexcept;
  71. friend bool operator!=(const path& lhs, const path& rhs) noexcept; // removed in C++20
  72. friend bool operator< (const path& lhs, const path& rhs) noexcept; // removed in C++20
  73. friend bool operator<=(const path& lhs, const path& rhs) noexcept; // removed in C++20
  74. friend bool operator> (const path& lhs, const path& rhs) noexcept; // removed in C++20
  75. friend bool operator>=(const path& lhs, const path& rhs) noexcept; // removed in C++20
  76. friend strong_ordering operator<=>(const path& lhs, const path& rhs) noexcept; // C++20
  77. friend path operator/(const path& lhs, const path& rhs);
  78. const string_type& native() const noexcept;
  79. const value_type* c_str() const noexcept;
  80. operator string_type() const;
  81. template<class EcharT, class traits = char_traits<EcharT>,
  82. class Allocator = allocator<EcharT>>
  83. basic_string<EcharT, traits, Allocator>
  84. string(const Allocator& a = Allocator()) const;
  85. std::string string() const;
  86. std::wstring wstring() const;
  87. std::u8string u8string() const;
  88. std::u16string u16string() const;
  89. std::u32string u32string() const;
  90. template<class EcharT, class traits = char_traits<EcharT>,
  91. class Allocator = allocator<EcharT>>
  92. basic_string<EcharT, traits, Allocator>
  93. generic_string(const Allocator& a = Allocator()) const;
  94. std::string generic_string() const;
  95. std::wstring generic_wstring() const;
  96. std::u8string generic_u8string() const;
  97. std::u16string generic_u16string() const;
  98. std::u32string generic_u32string() const;
  99. int compare(const path& p) const noexcept;
  100. int compare(const string_type& s) const;
  101. int compare(basic_string_view<value_type> s) const;
  102. int compare(const value_type* s) const;
  103. path root_name() const;
  104. path root_directory() const;
  105. path root_path() const;
  106. path relative_path() const;
  107. path parent_path() const;
  108. path filename() const;
  109. path stem() const;
  110. path extension() const;
  111. [[nodiscard]] bool empty() const noexcept;
  112. bool has_root_name() const;
  113. bool has_root_directory() const;
  114. bool has_root_path() const;
  115. bool has_relative_path() const;
  116. bool has_parent_path() const;
  117. bool has_filename() const;
  118. bool has_stem() const;
  119. bool has_extension() const;
  120. bool is_absolute() const;
  121. bool is_relative() const;
  122. path lexically_normal() const;
  123. path lexically_relative(const path& base) const;
  124. path lexically_proximate(const path& base) const;
  125. class iterator;
  126. using const_iterator = iterator;
  127. iterator begin() const;
  128. iterator end() const;
  129. template<class charT, class traits>
  130. friend basic_ostream<charT, traits>&
  131. operator<<(basic_ostream<charT, traits>& os, const path& p);
  132. template<class charT, class traits>
  133. friend basic_istream<charT, traits>&
  134. operator>>(basic_istream<charT, traits>& is, path& p);
  135. };
  136. void swap(path& lhs, path& rhs) noexcept;
  137. size_t hash_value(const path& p) noexcept;
  138. template <class Source>
  139. path u8path(const Source& source);
  140. template <class InputIterator>
  141. path u8path(InputIterator first, InputIterator last);
  142. class filesystem_error;
  143. class directory_entry {
  144. public:
  145. directory_entry() noexcept = default;
  146. directory_entry(const directory_entry&) = default;
  147. directory_entry(directory_entry&&) noexcept = default;
  148. explicit directory_entry(const filesystem::path& p);
  149. directory_entry(const filesystem::path& p, error_code& ec);
  150. ~directory_entry();
  151. directory_entry& operator=(const directory_entry&) = default;
  152. directory_entry& operator=(directory_entry&&) noexcept = default;
  153. void assign(const filesystem::path& p);
  154. void assign(const filesystem::path& p, error_code& ec);
  155. void replace_filename(const filesystem::path& p);
  156. void replace_filename(const filesystem::path& p, error_code& ec);
  157. void refresh();
  158. void refresh(error_code& ec) noexcept;
  159. const filesystem::path& path() const noexcept;
  160. operator const filesystem::path&() const noexcept;
  161. bool exists() const;
  162. bool exists(error_code& ec) const noexcept;
  163. bool is_block_file() const;
  164. bool is_block_file(error_code& ec) const noexcept;
  165. bool is_character_file() const;
  166. bool is_character_file(error_code& ec) const noexcept;
  167. bool is_directory() const;
  168. bool is_directory(error_code& ec) const noexcept;
  169. bool is_fifo() const;
  170. bool is_fifo(error_code& ec) const noexcept;
  171. bool is_other() const;
  172. bool is_other(error_code& ec) const noexcept;
  173. bool is_regular_file() const;
  174. bool is_regular_file(error_code& ec) const noexcept;
  175. bool is_socket() const;
  176. bool is_socket(error_code& ec) const noexcept;
  177. bool is_symlink() const;
  178. bool is_symlink(error_code& ec) const noexcept;
  179. uintmax_t file_size() const;
  180. uintmax_t file_size(error_code& ec) const noexcept;
  181. uintmax_t hard_link_count() const;
  182. uintmax_t hard_link_count(error_code& ec) const noexcept;
  183. file_time_type last_write_time() const;
  184. file_time_type last_write_time(error_code& ec) const noexcept;
  185. file_status status() const;
  186. file_status status(error_code& ec) const noexcept;
  187. file_status symlink_status() const;
  188. file_status symlink_status(error_code& ec) const noexcept;
  189. bool operator==(const directory_entry& rhs) const noexcept;
  190. bool operator!=(const directory_entry& rhs) const noexcept; // removed in C++20
  191. bool operator< (const directory_entry& rhs) const noexcept; // removed in C++20
  192. bool operator<=(const directory_entry& rhs) const noexcept; // removed in C++20
  193. bool operator> (const directory_entry& rhs) const noexcept; // removed in C++20
  194. bool operator>=(const directory_entry& rhs) const noexcept; // removed in C++20
  195. strong_ordering operator<=>(const directory_entry& rhs) const noexcept; // since C++20
  196. template<class charT, class traits>
  197. friend basic_ostream<charT, traits>&
  198. operator<<(basic_ostream<charT, traits>& os, const directory_entry& d);
  199. private:
  200. filesystem::path pathobject; // exposition only
  201. friend class directory_iterator; // exposition only
  202. };
  203. class directory_iterator;
  204. // enable directory_iterator range-based for statements
  205. directory_iterator begin(directory_iterator iter) noexcept;
  206. directory_iterator end(directory_iterator) noexcept;
  207. class recursive_directory_iterator;
  208. // enable recursive_directory_iterator range-based for statements
  209. recursive_directory_iterator begin(recursive_directory_iterator iter) noexcept;
  210. recursive_directory_iterator end(recursive_directory_iterator) noexcept;
  211. class file_status;
  212. struct space_info
  213. {
  214. uintmax_t capacity;
  215. uintmax_t free;
  216. uintmax_t available;
  217. friend bool operator==(const space_info&, const space_info&) = default; // C++20
  218. };
  219. enum class file_type;
  220. enum class perms;
  221. enum class perm_options;
  222. enum class copy_options;
  223. enum class directory_options;
  224. typedef chrono::time_point<trivial-clock> file_time_type;
  225. // operational functions
  226. path absolute(const path& p);
  227. path absolute(const path& p, error_code &ec);
  228. path canonical(const path& p);
  229. path canonical(const path& p, error_code& ec);
  230. void copy(const path& from, const path& to);
  231. void copy(const path& from, const path& to, error_code& ec);
  232. void copy(const path& from, const path& to, copy_options options);
  233. void copy(const path& from, const path& to, copy_options options,
  234. error_code& ec);
  235. bool copy_file(const path& from, const path& to);
  236. bool copy_file(const path& from, const path& to, error_code& ec);
  237. bool copy_file(const path& from, const path& to, copy_options option);
  238. bool copy_file(const path& from, const path& to, copy_options option,
  239. error_code& ec);
  240. void copy_symlink(const path& existing_symlink, const path& new_symlink);
  241. void copy_symlink(const path& existing_symlink, const path& new_symlink,
  242. error_code& ec) noexcept;
  243. bool create_directories(const path& p);
  244. bool create_directories(const path& p, error_code& ec);
  245. bool create_directory(const path& p);
  246. bool create_directory(const path& p, error_code& ec) noexcept;
  247. bool create_directory(const path& p, const path& attributes);
  248. bool create_directory(const path& p, const path& attributes,
  249. error_code& ec) noexcept;
  250. void create_directory_symlink(const path& to, const path& new_symlink);
  251. void create_directory_symlink(const path& to, const path& new_symlink,
  252. error_code& ec) noexcept;
  253. void create_hard_link(const path& to, const path& new_hard_link);
  254. void create_hard_link(const path& to, const path& new_hard_link,
  255. error_code& ec) noexcept;
  256. void create_symlink(const path& to, const path& new_symlink);
  257. void create_symlink(const path& to, const path& new_symlink,
  258. error_code& ec) noexcept;
  259. path current_path();
  260. path current_path(error_code& ec);
  261. void current_path(const path& p);
  262. void current_path(const path& p, error_code& ec) noexcept;
  263. bool exists(file_status s) noexcept;
  264. bool exists(const path& p);
  265. bool exists(const path& p, error_code& ec) noexcept;
  266. bool equivalent(const path& p1, const path& p2);
  267. bool equivalent(const path& p1, const path& p2, error_code& ec) noexcept;
  268. uintmax_t file_size(const path& p);
  269. uintmax_t file_size(const path& p, error_code& ec) noexcept;
  270. uintmax_t hard_link_count(const path& p);
  271. uintmax_t hard_link_count(const path& p, error_code& ec) noexcept;
  272. bool is_block_file(file_status s) noexcept;
  273. bool is_block_file(const path& p);
  274. bool is_block_file(const path& p, error_code& ec) noexcept;
  275. bool is_character_file(file_status s) noexcept;
  276. bool is_character_file(const path& p);
  277. bool is_character_file(const path& p, error_code& ec) noexcept;
  278. bool is_directory(file_status s) noexcept;
  279. bool is_directory(const path& p);
  280. bool is_directory(const path& p, error_code& ec) noexcept;
  281. bool is_empty(const path& p);
  282. bool is_empty(const path& p, error_code& ec) noexcept;
  283. bool is_fifo(file_status s) noexcept;
  284. bool is_fifo(const path& p);
  285. bool is_fifo(const path& p, error_code& ec) noexcept;
  286. bool is_other(file_status s) noexcept;
  287. bool is_other(const path& p);
  288. bool is_other(const path& p, error_code& ec) noexcept;
  289. bool is_regular_file(file_status s) noexcept;
  290. bool is_regular_file(const path& p);
  291. bool is_regular_file(const path& p, error_code& ec) noexcept;
  292. bool is_socket(file_status s) noexcept;
  293. bool is_socket(const path& p);
  294. bool is_socket(const path& p, error_code& ec) noexcept;
  295. bool is_symlink(file_status s) noexcept;
  296. bool is_symlink(const path& p);
  297. bool is_symlink(const path& p, error_code& ec) noexcept;
  298. file_time_type last_write_time(const path& p);
  299. file_time_type last_write_time(const path& p, error_code& ec) noexcept;
  300. void last_write_time(const path& p, file_time_type new_time);
  301. void last_write_time(const path& p, file_time_type new_time,
  302. error_code& ec) noexcept;
  303. void permissions(const path& p, perms prms,
  304. perm_options opts=perm_options::replace);
  305. void permissions(const path& p, perms prms, error_code& ec) noexcept;
  306. void permissions(const path& p, perms prms, perm_options opts,
  307. error_code& ec);
  308. path proximate(const path& p, error_code& ec);
  309. path proximate(const path& p, const path& base = current_path());
  310. path proximate(const path& p, const path& base, error_code &ec);
  311. path read_symlink(const path& p);
  312. path read_symlink(const path& p, error_code& ec);
  313. path relative(const path& p, error_code& ec);
  314. path relative(const path& p, const path& base=current_path());
  315. path relative(const path& p, const path& base, error_code& ec);
  316. bool remove(const path& p);
  317. bool remove(const path& p, error_code& ec) noexcept;
  318. uintmax_t remove_all(const path& p);
  319. uintmax_t remove_all(const path& p, error_code& ec);
  320. void rename(const path& from, const path& to);
  321. void rename(const path& from, const path& to, error_code& ec) noexcept;
  322. void resize_file(const path& p, uintmax_t size);
  323. void resize_file(const path& p, uintmax_t size, error_code& ec) noexcept;
  324. space_info space(const path& p);
  325. space_info space(const path& p, error_code& ec) noexcept;
  326. file_status status(const path& p);
  327. file_status status(const path& p, error_code& ec) noexcept;
  328. bool status_known(file_status s) noexcept;
  329. file_status symlink_status(const path& p);
  330. file_status symlink_status(const path& p, error_code& ec) noexcept;
  331. path temp_directory_path();
  332. path temp_directory_path(error_code& ec);
  333. path weakly_canonical(path const& p);
  334. path weakly_canonical(path const& p, error_code& ec);
  335. } // namespace std::filesystem
  336. template <>
  337. inline constexpr bool std::ranges::enable_borrowed_range<std::filesystem::directory_iterator> = true;
  338. template <>
  339. inline constexpr bool std::ranges::enable_borrowed_range<std::filesystem::recursive_directory_iterator> = true;
  340. template <>
  341. inline constexpr bool std::ranges::enable_view<std::filesystem::directory_iterator> = true;
  342. template <>
  343. inline constexpr bool std::ranges::enable_view<std::filesystem::recursive_directory_iterator> = true;
  344. */
  345. #include <__assert> // all public C++ headers provide the assertion handler
  346. #include <__config>
  347. #include <__filesystem/copy_options.h>
  348. #include <__filesystem/directory_entry.h>
  349. #include <__filesystem/directory_iterator.h>
  350. #include <__filesystem/directory_options.h>
  351. #include <__filesystem/file_status.h>
  352. #include <__filesystem/file_time_type.h>
  353. #include <__filesystem/file_type.h>
  354. #include <__filesystem/filesystem_error.h>
  355. #include <__filesystem/operations.h>
  356. #include <__filesystem/path.h>
  357. #include <__filesystem/path_iterator.h>
  358. #include <__filesystem/perm_options.h>
  359. #include <__filesystem/perms.h>
  360. #include <__filesystem/recursive_directory_iterator.h>
  361. #include <__filesystem/space_info.h>
  362. #include <__filesystem/u8path.h>
  363. #include <version>
  364. // standard-mandated includes
  365. // [fs.filesystem.syn]
  366. #include <compare>
  367. #if defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
  368. # error "The <filesystem> library is not supported since libc++ has been configured without support for a filesystem."
  369. #endif
  370. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  371. # pragma GCC system_header
  372. #endif
  373. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  374. # include <concepts>
  375. #endif
  376. #endif // _LIBCPP_FILESYSTEM