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