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_OPERATIONS_H
11#define _LIBCPP___FILESYSTEM_OPERATIONS_H
12
13#include <__chrono/time_point.h>
14#include <__config>
15#include <__filesystem/copy_options.h>
16#include <__filesystem/file_status.h>
17#include <__filesystem/file_time_type.h>
18#include <__filesystem/file_type.h>
19#include <__filesystem/path.h>
20#include <__filesystem/perm_options.h>
21#include <__filesystem/perms.h>
22#include <__filesystem/space_info.h>
23#include <__system_error/error_code.h>
24#include <cstdint>
25
26#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27# pragma GCC system_header
28#endif
29
30#if _LIBCPP_STD_VER >= 17 && _LIBCPP_HAS_FILESYSTEM
31
32_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
33
34_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
35_LIBCPP_EXPORTED_FROM_ABI path __absolute(const path&, error_code* __ec = nullptr);
36_LIBCPP_EXPORTED_FROM_ABI path __canonical(const path&, error_code* __ec = nullptr);
37_LIBCPP_EXPORTED_FROM_ABI bool
38__copy_file(const path& __from, const path& __to, copy_options __opt, error_code* __ec = nullptr);
39_LIBCPP_EXPORTED_FROM_ABI void
40__copy_symlink(const path& __existing_symlink, const path& __new_symlink, error_code* __ec = nullptr);
41_LIBCPP_EXPORTED_FROM_ABI void
42__copy(const path& __from, const path& __to, copy_options __opt, error_code* __ec = nullptr);
43_LIBCPP_EXPORTED_FROM_ABI bool __create_directories(const path&, error_code* = nullptr);
44_LIBCPP_EXPORTED_FROM_ABI void
45__create_directory_symlink(const path& __to, const path& __new_symlink, error_code* __ec = nullptr);
46_LIBCPP_EXPORTED_FROM_ABI bool __create_directory(const path&, error_code* = nullptr);
47_LIBCPP_EXPORTED_FROM_ABI bool __create_directory(const path&, const path& __attributes, error_code* = nullptr);
48_LIBCPP_EXPORTED_FROM_ABI void
49__create_hard_link(const path& __to, const path& __new_hard_link, error_code* __ec = nullptr);
50_LIBCPP_EXPORTED_FROM_ABI void
51__create_symlink(const path& __to, const path& __new_symlink, error_code* __ec = nullptr);
52_LIBCPP_EXPORTED_FROM_ABI path __current_path(error_code* __ec = nullptr);
53_LIBCPP_EXPORTED_FROM_ABI void __current_path(const path&, error_code* __ec = nullptr);
54_LIBCPP_EXPORTED_FROM_ABI bool __equivalent(const path&, const path&, error_code* __ec = nullptr);
55_LIBCPP_EXPORTED_FROM_ABI file_status __status(const path&, error_code* __ec = nullptr);
56_LIBCPP_EXPORTED_FROM_ABI uintmax_t __file_size(const path&, error_code* __ec = nullptr);
57_LIBCPP_EXPORTED_FROM_ABI uintmax_t __hard_link_count(const path&, error_code* __ec = nullptr);
58_LIBCPP_EXPORTED_FROM_ABI file_status __symlink_status(const path&, error_code* __ec = nullptr);
59_LIBCPP_EXPORTED_FROM_ABI file_time_type __last_write_time(const path&, error_code* __ec = nullptr);
60_LIBCPP_EXPORTED_FROM_ABI void __last_write_time(const path&, file_time_type __new_time, error_code* __ec = nullptr);
61_LIBCPP_EXPORTED_FROM_ABI path __weakly_canonical(path const& __p, error_code* __ec = nullptr);
62_LIBCPP_EXPORTED_FROM_ABI path __read_symlink(const path&, error_code* __ec = nullptr);
63_LIBCPP_EXPORTED_FROM_ABI uintmax_t __remove_all(const path&, error_code* __ec = nullptr);
64_LIBCPP_EXPORTED_FROM_ABI bool __remove(const path&, error_code* __ec = nullptr);
65_LIBCPP_EXPORTED_FROM_ABI void __rename(const path& __from, const path& __to, error_code* __ec = nullptr);
66_LIBCPP_EXPORTED_FROM_ABI void __resize_file(const path&, uintmax_t __size, error_code* = nullptr);
67_LIBCPP_EXPORTED_FROM_ABI path __temp_directory_path(error_code* __ec = nullptr);
68_LIBCPP_EXPORTED_FROM_ABI bool __fs_is_empty(const path& __p, error_code* __ec = nullptr);
69_LIBCPP_EXPORTED_FROM_ABI void __permissions(const path&, perms, perm_options, error_code* = nullptr);
70_LIBCPP_EXPORTED_FROM_ABI space_info __space(const path&, error_code* __ec = nullptr);
71_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
72
73[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path absolute(const path& __p) { return __absolute(__p); }
74[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path absolute(const path& __p, error_code& __ec) {
75 return __absolute(__p, ec: &__ec);
76}
77[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path canonical(const path& __p) { return __canonical(__p); }
78[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path canonical(const path& __p, error_code& __ec) {
79 return __canonical(__p, ec: &__ec);
80}
81inline _LIBCPP_HIDE_FROM_ABI bool copy_file(const path& __from, const path& __to) {
82 return __copy_file(__from, __to, opt: copy_options::none);
83}
84inline _LIBCPP_HIDE_FROM_ABI bool copy_file(const path& __from, const path& __to, error_code& __ec) {
85 return __copy_file(__from, __to, opt: copy_options::none, ec: &__ec);
86}
87inline _LIBCPP_HIDE_FROM_ABI bool copy_file(const path& __from, const path& __to, copy_options __opt) {
88 return __copy_file(__from, __to, __opt);
89}
90inline _LIBCPP_HIDE_FROM_ABI bool
91copy_file(const path& __from, const path& __to, copy_options __opt, error_code& __ec) {
92 return __copy_file(__from, __to, __opt, ec: &__ec);
93}
94inline _LIBCPP_HIDE_FROM_ABI void copy_symlink(const path& __from, const path& __to) { __copy_symlink(existing_symlink: __from, new_symlink: __to); }
95inline _LIBCPP_HIDE_FROM_ABI void copy_symlink(const path& __from, const path& __to, error_code& __ec) noexcept {
96 __copy_symlink(existing_symlink: __from, new_symlink: __to, ec: &__ec);
97}
98inline _LIBCPP_HIDE_FROM_ABI void copy(const path& __from, const path& __to) {
99 __copy(__from, __to, opt: copy_options::none);
100}
101inline _LIBCPP_HIDE_FROM_ABI void copy(const path& __from, const path& __to, error_code& __ec) {
102 __copy(__from, __to, opt: copy_options::none, ec: &__ec);
103}
104inline _LIBCPP_HIDE_FROM_ABI void copy(const path& __from, const path& __to, copy_options __opt) {
105 __copy(__from, __to, __opt);
106}
107inline _LIBCPP_HIDE_FROM_ABI void copy(const path& __from, const path& __to, copy_options __opt, error_code& __ec) {
108 __copy(__from, __to, __opt, ec: &__ec);
109}
110inline _LIBCPP_HIDE_FROM_ABI bool create_directories(const path& __p) { return __create_directories(__p); }
111inline _LIBCPP_HIDE_FROM_ABI bool create_directories(const path& __p, error_code& __ec) {
112 return __create_directories(__p, &__ec);
113}
114inline _LIBCPP_HIDE_FROM_ABI void create_directory_symlink(const path& __target, const path& __link) {
115 __create_directory_symlink(to: __target, new_symlink: __link);
116}
117inline _LIBCPP_HIDE_FROM_ABI void
118create_directory_symlink(const path& __target, const path& __link, error_code& __ec) noexcept {
119 __create_directory_symlink(to: __target, new_symlink: __link, ec: &__ec);
120}
121inline _LIBCPP_HIDE_FROM_ABI bool create_directory(const path& __p) { return __create_directory(__p); }
122inline _LIBCPP_HIDE_FROM_ABI bool create_directory(const path& __p, error_code& __ec) noexcept {
123 return __create_directory(__p, &__ec);
124}
125inline _LIBCPP_HIDE_FROM_ABI bool create_directory(const path& __p, const path& __attrs) {
126 return __create_directory(__p, attributes: __attrs);
127}
128inline _LIBCPP_HIDE_FROM_ABI bool create_directory(const path& __p, const path& __attrs, error_code& __ec) noexcept {
129 return __create_directory(__p, attributes: __attrs, &__ec);
130}
131inline _LIBCPP_HIDE_FROM_ABI void create_hard_link(const path& __target, const path& __link) {
132 __create_hard_link(to: __target, new_hard_link: __link);
133}
134inline _LIBCPP_HIDE_FROM_ABI void
135create_hard_link(const path& __target, const path& __link, error_code& __ec) noexcept {
136 __create_hard_link(to: __target, new_hard_link: __link, ec: &__ec);
137}
138inline _LIBCPP_HIDE_FROM_ABI void create_symlink(const path& __target, const path& __link) {
139 __create_symlink(to: __target, new_symlink: __link);
140}
141inline _LIBCPP_HIDE_FROM_ABI void create_symlink(const path& __target, const path& __link, error_code& __ec) noexcept {
142 return __create_symlink(to: __target, new_symlink: __link, ec: &__ec);
143}
144[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path current_path() { return __current_path(); }
145[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path current_path(error_code& __ec) { return __current_path(ec: &__ec); }
146inline _LIBCPP_HIDE_FROM_ABI void current_path(const path& __p) { __current_path(__p); }
147inline _LIBCPP_HIDE_FROM_ABI void current_path(const path& __p, error_code& __ec) noexcept {
148 __current_path(__p, ec: &__ec);
149}
150[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool equivalent(const path& __p1, const path& __p2) {
151 return __equivalent(__p1, __p2);
152}
153[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool
154equivalent(const path& __p1, const path& __p2, error_code& __ec) noexcept {
155 return __equivalent(__p1, __p2, ec: &__ec);
156}
157[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool status_known(file_status __s) noexcept {
158 return __s.type() != file_type::none;
159}
160[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool exists(file_status __s) noexcept {
161 return status_known(__s) && __s.type() != file_type::not_found;
162}
163[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool exists(const path& __p) { return exists(s: __status(__p)); }
164
165[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool exists(const path& __p, error_code& __ec) noexcept {
166 auto __s = __status(__p, ec: &__ec);
167 if (status_known(__s))
168 __ec.clear();
169 return exists(__s);
170}
171
172[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI uintmax_t file_size(const path& __p) { return __file_size(__p); }
173[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI uintmax_t file_size(const path& __p, error_code& __ec) noexcept {
174 return __file_size(__p, ec: &__ec);
175}
176[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI uintmax_t hard_link_count(const path& __p) { return __hard_link_count(__p); }
177[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI uintmax_t hard_link_count(const path& __p, error_code& __ec) noexcept {
178 return __hard_link_count(__p, ec: &__ec);
179}
180[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_block_file(file_status __s) noexcept {
181 return __s.type() == file_type::block;
182}
183[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_block_file(const path& __p) { return is_block_file(s: __status(__p)); }
184[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_block_file(const path& __p, error_code& __ec) noexcept {
185 return is_block_file(s: __status(__p, ec: &__ec));
186}
187[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_character_file(file_status __s) noexcept {
188 return __s.type() == file_type::character;
189}
190[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_character_file(const path& __p) {
191 return is_character_file(s: __status(__p));
192}
193[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_character_file(const path& __p, error_code& __ec) noexcept {
194 return is_character_file(s: __status(__p, ec: &__ec));
195}
196[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_directory(file_status __s) noexcept {
197 return __s.type() == file_type::directory;
198}
199[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_directory(const path& __p) { return is_directory(s: __status(__p)); }
200[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_directory(const path& __p, error_code& __ec) noexcept {
201 return is_directory(s: __status(__p, ec: &__ec));
202}
203[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_empty(const path& __p) { return __fs_is_empty(__p); }
204[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_empty(const path& __p, error_code& __ec) {
205 return __fs_is_empty(__p, ec: &__ec);
206}
207[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_fifo(file_status __s) noexcept {
208 return __s.type() == file_type::fifo;
209}
210[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_fifo(const path& __p) { return is_fifo(s: __status(__p)); }
211[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_fifo(const path& __p, error_code& __ec) noexcept {
212 return is_fifo(s: __status(__p, ec: &__ec));
213}
214[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_regular_file(file_status __s) noexcept {
215 return __s.type() == file_type::regular;
216}
217[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_regular_file(const path& __p) {
218 return is_regular_file(s: __status(__p));
219}
220[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_regular_file(const path& __p, error_code& __ec) noexcept {
221 return is_regular_file(s: __status(__p, ec: &__ec));
222}
223[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_symlink(file_status __s) noexcept {
224 return __s.type() == file_type::symlink;
225}
226[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_symlink(const path& __p) {
227 return is_symlink(s: __symlink_status(__p));
228}
229[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_symlink(const path& __p, error_code& __ec) noexcept {
230 return is_symlink(s: __symlink_status(__p, ec: &__ec));
231}
232[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_other(file_status __s) noexcept {
233 return exists(__s) && !is_regular_file(__s) && !is_directory(__s) && !is_symlink(__s);
234}
235[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_other(const path& __p) { return is_other(s: __status(__p)); }
236[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_other(const path& __p, error_code& __ec) noexcept {
237 return is_other(s: __status(__p, ec: &__ec));
238}
239[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_socket(file_status __s) noexcept {
240 return __s.type() == file_type::socket;
241}
242[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_socket(const path& __p) { return is_socket(s: __status(__p)); }
243[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool is_socket(const path& __p, error_code& __ec) noexcept {
244 return is_socket(s: __status(__p, ec: &__ec));
245}
246[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI file_time_type last_write_time(const path& __p) {
247 return __last_write_time(__p);
248}
249[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI file_time_type last_write_time(const path& __p, error_code& __ec) noexcept {
250 return __last_write_time(__p, ec: &__ec);
251}
252inline _LIBCPP_HIDE_FROM_ABI void last_write_time(const path& __p, file_time_type __t) { __last_write_time(__p, new_time: __t); }
253inline _LIBCPP_HIDE_FROM_ABI void last_write_time(const path& __p, file_time_type __t, error_code& __ec) noexcept {
254 __last_write_time(__p, new_time: __t, ec: &__ec);
255}
256inline _LIBCPP_HIDE_FROM_ABI void
257permissions(const path& __p, perms __prms, perm_options __opts = perm_options::replace) {
258 __permissions(__p, __prms, __opts);
259}
260inline _LIBCPP_HIDE_FROM_ABI void permissions(const path& __p, perms __prms, error_code& __ec) noexcept {
261 __permissions(__p, __prms, perm_options::replace, &__ec);
262}
263inline _LIBCPP_HIDE_FROM_ABI void permissions(const path& __p, perms __prms, perm_options __opts, error_code& __ec) {
264 __permissions(__p, __prms, __opts, &__ec);
265}
266
267[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path proximate(const path& __p, const path& __base, error_code& __ec) {
268 path __tmp = __weakly_canonical(__p, ec: &__ec);
269 if (__ec)
270 return {};
271 path __tmp_base = __weakly_canonical(p: __base, ec: &__ec);
272 if (__ec)
273 return {};
274 return __tmp.lexically_proximate(base: __tmp_base);
275}
276
277[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path proximate(const path& __p, error_code& __ec) {
278 return proximate(__p, base: current_path(), __ec);
279}
280[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path proximate(const path& __p, const path& __base = current_path()) {
281 return __weakly_canonical(__p).lexically_proximate(base: __weakly_canonical(p: __base));
282}
283[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path read_symlink(const path& __p) { return __read_symlink(__p); }
284[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path read_symlink(const path& __p, error_code& __ec) {
285 return __read_symlink(__p, ec: &__ec);
286}
287
288[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path relative(const path& __p, const path& __base, error_code& __ec) {
289 path __tmp = __weakly_canonical(__p, ec: &__ec);
290 if (__ec)
291 return path();
292 path __tmpbase = __weakly_canonical(p: __base, ec: &__ec);
293 if (__ec)
294 return path();
295 return __tmp.lexically_relative(base: __tmpbase);
296}
297
298[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path relative(const path& __p, error_code& __ec) {
299 return relative(__p, base: current_path(), __ec);
300}
301[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path relative(const path& __p, const path& __base = current_path()) {
302 return __weakly_canonical(__p).lexically_relative(base: __weakly_canonical(p: __base));
303}
304inline _LIBCPP_HIDE_FROM_ABI uintmax_t remove_all(const path& __p) { return __remove_all(__p); }
305inline _LIBCPP_HIDE_FROM_ABI uintmax_t remove_all(const path& __p, error_code& __ec) {
306 return __remove_all(__p, ec: &__ec);
307}
308inline _LIBCPP_HIDE_FROM_ABI bool remove(const path& __p) { return __remove(__p); }
309inline _LIBCPP_HIDE_FROM_ABI bool remove(const path& __p, error_code& __ec) noexcept { return __remove(__p, ec: &__ec); }
310inline _LIBCPP_HIDE_FROM_ABI void rename(const path& __from, const path& __to) { return __rename(__from, __to); }
311inline _LIBCPP_HIDE_FROM_ABI void rename(const path& __from, const path& __to, error_code& __ec) noexcept {
312 return __rename(__from, __to, ec: &__ec);
313}
314inline _LIBCPP_HIDE_FROM_ABI void resize_file(const path& __p, uintmax_t __ns) { return __resize_file(__p, size: __ns); }
315inline _LIBCPP_HIDE_FROM_ABI void resize_file(const path& __p, uintmax_t __ns, error_code& __ec) noexcept {
316 return __resize_file(__p, size: __ns, &__ec);
317}
318[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI space_info space(const path& __p) { return __space(__p); }
319[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI space_info space(const path& __p, error_code& __ec) noexcept {
320 return __space(__p, ec: &__ec);
321}
322[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI file_status status(const path& __p) { return __status(__p); }
323[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI file_status status(const path& __p, error_code& __ec) noexcept {
324 return __status(__p, ec: &__ec);
325}
326[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI file_status symlink_status(const path& __p) { return __symlink_status(__p); }
327[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI file_status symlink_status(const path& __p, error_code& __ec) noexcept {
328 return __symlink_status(__p, ec: &__ec);
329}
330[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path temp_directory_path() { return __temp_directory_path(); }
331[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path temp_directory_path(error_code& __ec) {
332 return __temp_directory_path(ec: &__ec);
333}
334[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path weakly_canonical(path const& __p) { return __weakly_canonical(__p); }
335[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI path weakly_canonical(path const& __p, error_code& __ec) {
336 return __weakly_canonical(__p, ec: &__ec);
337}
338
339_LIBCPP_END_NAMESPACE_FILESYSTEM
340
341#endif // _LIBCPP_STD_VER >= 17 && _LIBCPP_HAS_FILESYSTEM
342
343#endif // _LIBCPP___FILESYSTEM_OPERATIONS_H
344