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