llvm-project
573 строки · 21.9 Кб
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
10#ifndef _LIBCPP_FILESYSTEM
11#define _LIBCPP_FILESYSTEM
12
13/*
14filesystem synopsis
15
16namespace std::filesystem {
17
18// `class path` from http://eel.is/c++draft/fs.class.path.general#6
19class path {
20public:
21using value_type = see below;
22using string_type = basic_string<value_type>;
23static constexpr value_type preferred_separator = see below;
24
25enum format;
26
27path() noexcept;
28path(const path& p);
29path(path&& p) noexcept;
30path(string_type&& source, format fmt = auto_format);
31template<class Source>
32path(const Source& source, format fmt = auto_format);
33template<class InputIterator>
34path(InputIterator first, InputIterator last, format fmt = auto_format);
35template<class Source>
36path(const Source& source, const locale& loc, format fmt = auto_format);
37template<class InputIterator>
38path(InputIterator first, InputIterator last, const locale& loc, format fmt = auto_format);
39~path();
40
41path& operator=(const path& p);
42path& operator=(path&& p) noexcept;
43path& operator=(string_type&& source);
44path& assign(string_type&& source);
45template<class Source>
46path& operator=(const Source& source);
47template<class Source>
48path& assign(const Source& source);
49template<class InputIterator>
50path& assign(InputIterator first, InputIterator last);
51
52path& operator/=(const path& p);
53template<class Source>
54path& operator/=(const Source& source);
55template<class Source>
56path& append(const Source& source);
57template<class InputIterator>
58path& append(InputIterator first, InputIterator last);
59
60path& operator+=(const path& x);
61path& operator+=(const string_type& x);
62path& operator+=(basic_string_view<value_type> x);
63path& operator+=(const value_type* x);
64path& operator+=(value_type x);
65template<class Source>
66path& operator+=(const Source& x);
67template<class EcharT>
68path& operator+=(EcharT x);
69template<class Source>
70path& concat(const Source& x);
71template<class InputIterator>
72path& concat(InputIterator first, InputIterator last);
73
74void clear() noexcept;
75path& make_preferred();
76path& remove_filename();
77path& replace_filename(const path& replacement);
78path& replace_extension(const path& replacement = path());
79void swap(path& rhs) noexcept;
80
81friend bool operator==(const path& lhs, const path& rhs) noexcept;
82friend bool operator!=(const path& lhs, const path& rhs) noexcept; // removed in C++20
83friend bool operator< (const path& lhs, const path& rhs) noexcept; // removed in C++20
84friend bool operator<=(const path& lhs, const path& rhs) noexcept; // removed in C++20
85friend bool operator> (const path& lhs, const path& rhs) noexcept; // removed in C++20
86friend bool operator>=(const path& lhs, const path& rhs) noexcept; // removed in C++20
87friend strong_ordering operator<=>(const path& lhs, const path& rhs) noexcept; // C++20
88
89friend path operator/(const path& lhs, const path& rhs);
90
91const string_type& native() const noexcept;
92const value_type* c_str() const noexcept;
93operator string_type() const;
94
95template<class EcharT, class traits = char_traits<EcharT>,
96class Allocator = allocator<EcharT>>
97basic_string<EcharT, traits, Allocator>
98string(const Allocator& a = Allocator()) const;
99std::string string() const;
100std::wstring wstring() const;
101std::u8string u8string() const;
102std::u16string u16string() const;
103std::u32string u32string() const;
104
105template<class EcharT, class traits = char_traits<EcharT>,
106class Allocator = allocator<EcharT>>
107basic_string<EcharT, traits, Allocator>
108generic_string(const Allocator& a = Allocator()) const;
109std::string generic_string() const;
110std::wstring generic_wstring() const;
111std::u8string generic_u8string() const;
112std::u16string generic_u16string() const;
113std::u32string generic_u32string() const;
114
115int compare(const path& p) const noexcept;
116int compare(const string_type& s) const;
117int compare(basic_string_view<value_type> s) const;
118int compare(const value_type* s) const;
119
120path root_name() const;
121path root_directory() const;
122path root_path() const;
123path relative_path() const;
124path parent_path() const;
125path filename() const;
126path stem() const;
127path extension() const;
128
129[[nodiscard]] bool empty() const noexcept;
130bool has_root_name() const;
131bool has_root_directory() const;
132bool has_root_path() const;
133bool has_relative_path() const;
134bool has_parent_path() const;
135bool has_filename() const;
136bool has_stem() const;
137bool has_extension() const;
138bool is_absolute() const;
139bool is_relative() const;
140
141path lexically_normal() const;
142path lexically_relative(const path& base) const;
143path lexically_proximate(const path& base) const;
144
145class iterator;
146using const_iterator = iterator;
147
148iterator begin() const;
149iterator end() const;
150
151template<class charT, class traits>
152friend basic_ostream<charT, traits>&
153operator<<(basic_ostream<charT, traits>& os, const path& p);
154template<class charT, class traits>
155friend basic_istream<charT, traits>&
156operator>>(basic_istream<charT, traits>& is, path& p);
157};
158
159void swap(path& lhs, path& rhs) noexcept;
160size_t hash_value(const path& p) noexcept;
161
162// [fs.path.hash], hash support
163template<> struct hash<filesystem::path>;
164
165template <class Source>
166path u8path(const Source& source);
167template <class InputIterator>
168path u8path(InputIterator first, InputIterator last);
169
170class filesystem_error;
171
172class directory_entry {
173public:
174directory_entry() noexcept = default;
175directory_entry(const directory_entry&) = default;
176directory_entry(directory_entry&&) noexcept = default;
177explicit directory_entry(const filesystem::path& p);
178directory_entry(const filesystem::path& p, error_code& ec);
179~directory_entry();
180
181directory_entry& operator=(const directory_entry&) = default;
182directory_entry& operator=(directory_entry&&) noexcept = default;
183
184void assign(const filesystem::path& p);
185void assign(const filesystem::path& p, error_code& ec);
186void replace_filename(const filesystem::path& p);
187void replace_filename(const filesystem::path& p, error_code& ec);
188void refresh();
189void refresh(error_code& ec) noexcept;
190
191const filesystem::path& path() const noexcept;
192operator const filesystem::path&() const noexcept;
193bool exists() const;
194bool exists(error_code& ec) const noexcept;
195bool is_block_file() const;
196bool is_block_file(error_code& ec) const noexcept;
197bool is_character_file() const;
198bool is_character_file(error_code& ec) const noexcept;
199bool is_directory() const;
200bool is_directory(error_code& ec) const noexcept;
201bool is_fifo() const;
202bool is_fifo(error_code& ec) const noexcept;
203bool is_other() const;
204bool is_other(error_code& ec) const noexcept;
205bool is_regular_file() const;
206bool is_regular_file(error_code& ec) const noexcept;
207bool is_socket() const;
208bool is_socket(error_code& ec) const noexcept;
209bool is_symlink() const;
210bool is_symlink(error_code& ec) const noexcept;
211uintmax_t file_size() const;
212uintmax_t file_size(error_code& ec) const noexcept;
213uintmax_t hard_link_count() const;
214uintmax_t hard_link_count(error_code& ec) const noexcept;
215file_time_type last_write_time() const;
216file_time_type last_write_time(error_code& ec) const noexcept;
217file_status status() const;
218file_status status(error_code& ec) const noexcept;
219file_status symlink_status() const;
220file_status symlink_status(error_code& ec) const noexcept;
221
222bool operator==(const directory_entry& rhs) const noexcept;
223bool operator!=(const directory_entry& rhs) const noexcept; // removed in C++20
224bool operator< (const directory_entry& rhs) const noexcept; // removed in C++20
225bool operator<=(const directory_entry& rhs) const noexcept; // removed in C++20
226bool operator> (const directory_entry& rhs) const noexcept; // removed in C++20
227bool operator>=(const directory_entry& rhs) const noexcept; // removed in C++20
228strong_ordering operator<=>(const directory_entry& rhs) const noexcept; // since C++20
229
230template<class charT, class traits>
231friend basic_ostream<charT, traits>&
232operator<<(basic_ostream<charT, traits>& os, const directory_entry& d);
233
234private:
235filesystem::path pathobject; // exposition only
236friend class directory_iterator; // exposition only
237};
238
239class directory_iterator {
240public:
241using iterator_category = input_iterator_tag;
242using value_type = directory_entry;
243using difference_type = ptrdiff_t;
244using pointer = const directory_entry*;
245using reference = const directory_entry&;
246
247// [fs.dir.itr.members], member functions
248directory_iterator() noexcept;
249explicit directory_iterator(const path& p);
250directory_iterator(const path& p, directory_options options);
251directory_iterator(const path& p, error_code& ec);
252directory_iterator(const path& p, directory_options options,
253error_code& ec);
254directory_iterator(const directory_iterator& rhs);
255directory_iterator(directory_iterator&& rhs) noexcept;
256~directory_iterator();
257
258directory_iterator& operator=(const directory_iterator& rhs);
259directory_iterator& operator=(directory_iterator&& rhs) noexcept;
260
261const directory_entry& operator*() const;
262const directory_entry* operator->() const;
263directory_iterator& operator++();
264directory_iterator& increment(error_code& ec);
265
266bool operator==(default_sentinel_t) const noexcept { // since C++20
267return *this == directory_iterator();
268}
269
270// other members as required by [input.iterators], input iterators
271};
272
273// enable directory_iterator range-based for statements
274directory_iterator begin(directory_iterator iter) noexcept;
275directory_iterator end(directory_iterator) noexcept;
276
277class recursive_directory_iterator {
278public:
279using iterator_category = input_iterator_tag;
280using value_type = directory_entry;
281using difference_type = ptrdiff_t;
282using pointer = const directory_entry*;
283using reference = const directory_entry&;
284
285// [fs.rec.dir.itr.members], constructors and destructor
286recursive_directory_iterator() noexcept;
287explicit recursive_directory_iterator(const path& p);
288recursive_directory_iterator(const path& p, directory_options options);
289recursive_directory_iterator(const path& p, directory_options options,
290error_code& ec);
291recursive_directory_iterator(const path& p, error_code& ec);
292recursive_directory_iterator(const recursive_directory_iterator& rhs);
293recursive_directory_iterator(recursive_directory_iterator&& rhs) noexcept;
294~recursive_directory_iterator();
295
296// [fs.rec.dir.itr.members], observers
297directory_options options() const;
298int depth() const;
299bool recursion_pending() const;
300
301const directory_entry& operator*() const;
302const directory_entry* operator->() const;
303
304// [fs.rec.dir.itr.members], modifiers
305recursive_directory_iterator&
306operator=(const recursive_directory_iterator& rhs);
307recursive_directory_iterator&
308operator=(recursive_directory_iterator&& rhs) noexcept;
309
310recursive_directory_iterator& operator++();
311recursive_directory_iterator& increment(error_code& ec);
312
313void pop();
314void pop(error_code& ec);
315void disable_recursion_pending();
316
317bool operator==(default_sentinel_t) const noexcept { // since C++20
318return *this == recursive_directory_iterator();
319}
320
321// other members as required by [input.iterators], input iterators
322};
323
324// enable recursive_directory_iterator range-based for statements
325recursive_directory_iterator begin(recursive_directory_iterator iter) noexcept;
326recursive_directory_iterator end(recursive_directory_iterator) noexcept;
327
328class file_status {
329public:
330// [fs.file.status.cons], constructors and destructor
331file_status() noexcept : file_status(file_type::none) {}
332explicit file_status(file_type ft,
333perms prms = perms::unknown) noexcept;
334file_status(const file_status&) noexcept = default;
335file_status(file_status&&) noexcept = default;
336~file_status();
337
338// assignments
339file_status& operator=(const file_status&) noexcept = default;
340file_status& operator=(file_status&&) noexcept = default;
341
342// [fs.file.status.mods], modifiers
343void type(file_type ft) noexcept;
344void permissions(perms prms) noexcept;
345
346// [fs.file.status.obs], observers
347file_type type() const noexcept;
348perms permissions() const noexcept;
349
350friend bool operator==(const file_status& lhs, const file_status& rhs) noexcept
351{ return lhs.type() == rhs.type() && lhs.permissions() == rhs.permissions(); } // C++20
352};
353
354struct space_info
355{
356uintmax_t capacity;
357uintmax_t free;
358uintmax_t available;
359
360friend bool operator==(const space_info&, const space_info&) = default; // C++20
361};
362
363enum class file_type;
364enum class perms;
365enum class perm_options;
366enum class copy_options;
367enum class directory_options;
368
369typedef chrono::time_point<trivial-clock> file_time_type;
370
371// operational functions
372
373path absolute(const path& p);
374path absolute(const path& p, error_code &ec);
375
376path canonical(const path& p);
377path canonical(const path& p, error_code& ec);
378
379void copy(const path& from, const path& to);
380void copy(const path& from, const path& to, error_code& ec);
381void copy(const path& from, const path& to, copy_options options);
382void copy(const path& from, const path& to, copy_options options,
383error_code& ec);
384
385bool copy_file(const path& from, const path& to);
386bool copy_file(const path& from, const path& to, error_code& ec);
387bool copy_file(const path& from, const path& to, copy_options option);
388bool copy_file(const path& from, const path& to, copy_options option,
389error_code& ec);
390
391void copy_symlink(const path& existing_symlink, const path& new_symlink);
392void copy_symlink(const path& existing_symlink, const path& new_symlink,
393error_code& ec) noexcept;
394
395bool create_directories(const path& p);
396bool create_directories(const path& p, error_code& ec);
397
398bool create_directory(const path& p);
399bool create_directory(const path& p, error_code& ec) noexcept;
400
401bool create_directory(const path& p, const path& attributes);
402bool create_directory(const path& p, const path& attributes,
403error_code& ec) noexcept;
404
405void create_directory_symlink(const path& to, const path& new_symlink);
406void create_directory_symlink(const path& to, const path& new_symlink,
407error_code& ec) noexcept;
408
409void create_hard_link(const path& to, const path& new_hard_link);
410void create_hard_link(const path& to, const path& new_hard_link,
411error_code& ec) noexcept;
412
413void create_symlink(const path& to, const path& new_symlink);
414void create_symlink(const path& to, const path& new_symlink,
415error_code& ec) noexcept;
416
417path current_path();
418path current_path(error_code& ec);
419void current_path(const path& p);
420void current_path(const path& p, error_code& ec) noexcept;
421
422bool exists(file_status s) noexcept;
423bool exists(const path& p);
424bool exists(const path& p, error_code& ec) noexcept;
425
426bool equivalent(const path& p1, const path& p2);
427bool equivalent(const path& p1, const path& p2, error_code& ec) noexcept;
428
429uintmax_t file_size(const path& p);
430uintmax_t file_size(const path& p, error_code& ec) noexcept;
431
432uintmax_t hard_link_count(const path& p);
433uintmax_t hard_link_count(const path& p, error_code& ec) noexcept;
434
435bool is_block_file(file_status s) noexcept;
436bool is_block_file(const path& p);
437bool is_block_file(const path& p, error_code& ec) noexcept;
438
439bool is_character_file(file_status s) noexcept;
440bool is_character_file(const path& p);
441bool is_character_file(const path& p, error_code& ec) noexcept;
442
443bool is_directory(file_status s) noexcept;
444bool is_directory(const path& p);
445bool is_directory(const path& p, error_code& ec) noexcept;
446
447bool is_empty(const path& p);
448bool is_empty(const path& p, error_code& ec) noexcept;
449
450bool is_fifo(file_status s) noexcept;
451bool is_fifo(const path& p);
452bool is_fifo(const path& p, error_code& ec) noexcept;
453
454bool is_other(file_status s) noexcept;
455bool is_other(const path& p);
456bool is_other(const path& p, error_code& ec) noexcept;
457
458bool is_regular_file(file_status s) noexcept;
459bool is_regular_file(const path& p);
460bool is_regular_file(const path& p, error_code& ec) noexcept;
461
462bool is_socket(file_status s) noexcept;
463bool is_socket(const path& p);
464bool is_socket(const path& p, error_code& ec) noexcept;
465
466bool is_symlink(file_status s) noexcept;
467bool is_symlink(const path& p);
468bool is_symlink(const path& p, error_code& ec) noexcept;
469
470file_time_type last_write_time(const path& p);
471file_time_type last_write_time(const path& p, error_code& ec) noexcept;
472void last_write_time(const path& p, file_time_type new_time);
473void last_write_time(const path& p, file_time_type new_time,
474error_code& ec) noexcept;
475
476void permissions(const path& p, perms prms,
477perm_options opts=perm_options::replace);
478void permissions(const path& p, perms prms, error_code& ec) noexcept;
479void permissions(const path& p, perms prms, perm_options opts,
480error_code& ec);
481
482path proximate(const path& p, error_code& ec);
483path proximate(const path& p, const path& base = current_path());
484path proximate(const path& p, const path& base, error_code &ec);
485
486path read_symlink(const path& p);
487path read_symlink(const path& p, error_code& ec);
488
489path relative(const path& p, error_code& ec);
490path relative(const path& p, const path& base=current_path());
491path relative(const path& p, const path& base, error_code& ec);
492
493bool remove(const path& p);
494bool remove(const path& p, error_code& ec) noexcept;
495
496uintmax_t remove_all(const path& p);
497uintmax_t remove_all(const path& p, error_code& ec);
498
499void rename(const path& from, const path& to);
500void rename(const path& from, const path& to, error_code& ec) noexcept;
501
502void resize_file(const path& p, uintmax_t size);
503void resize_file(const path& p, uintmax_t size, error_code& ec) noexcept;
504
505space_info space(const path& p);
506space_info space(const path& p, error_code& ec) noexcept;
507
508file_status status(const path& p);
509file_status status(const path& p, error_code& ec) noexcept;
510
511bool status_known(file_status s) noexcept;
512
513file_status symlink_status(const path& p);
514file_status symlink_status(const path& p, error_code& ec) noexcept;
515
516path temp_directory_path();
517path temp_directory_path(error_code& ec);
518
519path weakly_canonical(path const& p);
520path weakly_canonical(path const& p, error_code& ec);
521
522} // namespace std::filesystem
523
524template <>
525inline constexpr bool std::ranges::enable_borrowed_range<std::filesystem::directory_iterator> = true;
526template <>
527inline constexpr bool std::ranges::enable_borrowed_range<std::filesystem::recursive_directory_iterator> = true;
528
529template <>
530inline constexpr bool std::ranges::enable_view<std::filesystem::directory_iterator> = true;
531template <>
532inline constexpr bool std::ranges::enable_view<std::filesystem::recursive_directory_iterator> = true;
533
534*/
535
536#include <__config>
537#include <__filesystem/copy_options.h>
538#include <__filesystem/directory_entry.h>
539#include <__filesystem/directory_iterator.h>
540#include <__filesystem/directory_options.h>
541#include <__filesystem/file_status.h>
542#include <__filesystem/file_time_type.h>
543#include <__filesystem/file_type.h>
544#include <__filesystem/filesystem_error.h>
545#include <__filesystem/operations.h>
546#include <__filesystem/path.h>
547#include <__filesystem/path_iterator.h>
548#include <__filesystem/perm_options.h>
549#include <__filesystem/perms.h>
550#include <__filesystem/recursive_directory_iterator.h>
551#include <__filesystem/space_info.h>
552#include <__filesystem/u8path.h>
553#include <version>
554
555// standard-mandated includes
556
557// [fs.filesystem.syn]
558#include <compare>
559
560#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
561# pragma GCC system_header
562#endif
563
564#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
565# include <concepts>
566# include <cstdlib>
567# include <cstring>
568# include <iosfwd>
569# include <new>
570# include <system_error>
571#endif
572
573#endif // _LIBCPP_FILESYSTEM
574