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 path absolute(const path& __p) { return __absolute(__p); }
74[[nodiscard]] inline path absolute(const path& __p, error_code& __ec) { return __absolute(__p, ec: &__ec); }
75[[nodiscard]] inline path canonical(const path& __p) { return __canonical(__p); }
76[[nodiscard]] inline path canonical(const path& __p, error_code& __ec) { return __canonical(__p, ec: &__ec); }
77inline bool copy_file(const path& __from, const path& __to) { return __copy_file(__from, __to, opt: copy_options::none); }
78inline bool copy_file(const path& __from, const path& __to, error_code& __ec) {
79 return __copy_file(__from, __to, opt: copy_options::none, ec: &__ec);
80}
81inline bool copy_file(const path& __from, const path& __to, copy_options __opt) {
82 return __copy_file(__from, __to, __opt);
83}
84inline bool copy_file(const path& __from, const path& __to, copy_options __opt, error_code& __ec) {
85 return __copy_file(__from, __to, __opt, ec: &__ec);
86}
87inline void copy_symlink(const path& __from, const path& __to) { __copy_symlink(existing_symlink: __from, new_symlink: __to); }
88inline void copy_symlink(const path& __from, const path& __to, error_code& __ec) noexcept {
89 __copy_symlink(existing_symlink: __from, new_symlink: __to, ec: &__ec);
90}
91inline void copy(const path& __from, const path& __to) { __copy(__from, __to, opt: copy_options::none); }
92inline void copy(const path& __from, const path& __to, error_code& __ec) {
93 __copy(__from, __to, opt: copy_options::none, ec: &__ec);
94}
95inline void copy(const path& __from, const path& __to, copy_options __opt) { __copy(__from, __to, __opt); }
96inline void copy(const path& __from, const path& __to, copy_options __opt, error_code& __ec) {
97 __copy(__from, __to, __opt, ec: &__ec);
98}
99inline bool create_directories(const path& __p) { return __create_directories(__p); }
100inline bool create_directories(const path& __p, error_code& __ec) { return __create_directories(__p, &__ec); }
101inline void create_directory_symlink(const path& __target, const path& __link) {
102 __create_directory_symlink(to: __target, new_symlink: __link);
103}
104inline void create_directory_symlink(const path& __target, const path& __link, error_code& __ec) noexcept {
105 __create_directory_symlink(to: __target, new_symlink: __link, ec: &__ec);
106}
107inline bool create_directory(const path& __p) { return __create_directory(__p); }
108inline bool create_directory(const path& __p, error_code& __ec) noexcept { return __create_directory(__p, &__ec); }
109inline bool create_directory(const path& __p, const path& __attrs) { return __create_directory(__p, attributes: __attrs); }
110inline bool create_directory(const path& __p, const path& __attrs, error_code& __ec) noexcept {
111 return __create_directory(__p, attributes: __attrs, &__ec);
112}
113inline void create_hard_link(const path& __target, const path& __link) { __create_hard_link(to: __target, new_hard_link: __link); }
114inline void create_hard_link(const path& __target, const path& __link, error_code& __ec) noexcept {
115 __create_hard_link(to: __target, new_hard_link: __link, ec: &__ec);
116}
117inline void create_symlink(const path& __target, const path& __link) { __create_symlink(to: __target, new_symlink: __link); }
118inline void create_symlink(const path& __target, const path& __link, error_code& __ec) noexcept {
119 return __create_symlink(to: __target, new_symlink: __link, ec: &__ec);
120}
121[[nodiscard]] inline path current_path() { return __current_path(); }
122[[nodiscard]] inline path current_path(error_code& __ec) { return __current_path(ec: &__ec); }
123inline void current_path(const path& __p) { __current_path(__p); }
124inline void current_path(const path& __p, error_code& __ec) noexcept { __current_path(__p, ec: &__ec); }
125[[nodiscard]] inline bool equivalent(const path& __p1, const path& __p2) { return __equivalent(__p1, __p2); }
126[[nodiscard]] inline bool equivalent(const path& __p1, const path& __p2, error_code& __ec) noexcept {
127 return __equivalent(__p1, __p2, ec: &__ec);
128}
129[[nodiscard]] inline bool status_known(file_status __s) noexcept { return __s.type() != file_type::none; }
130[[nodiscard]] inline bool exists(file_status __s) noexcept {
131 return status_known(__s) && __s.type() != file_type::not_found;
132}
133[[nodiscard]] inline bool exists(const path& __p) { return exists(s: __status(__p)); }
134
135[[nodiscard]] inline bool exists(const path& __p, error_code& __ec) noexcept {
136 auto __s = __status(__p, ec: &__ec);
137 if (status_known(__s))
138 __ec.clear();
139 return exists(__s);
140}
141
142[[nodiscard]] inline uintmax_t file_size(const path& __p) { return __file_size(__p); }
143[[nodiscard]] inline uintmax_t file_size(const path& __p, error_code& __ec) noexcept { return __file_size(__p, ec: &__ec); }
144[[nodiscard]] inline uintmax_t hard_link_count(const path& __p) { return __hard_link_count(__p); }
145[[nodiscard]] inline uintmax_t hard_link_count(const path& __p, error_code& __ec) noexcept {
146 return __hard_link_count(__p, ec: &__ec);
147}
148[[nodiscard]] inline bool is_block_file(file_status __s) noexcept { return __s.type() == file_type::block; }
149[[nodiscard]] inline bool is_block_file(const path& __p) { return is_block_file(s: __status(__p)); }
150[[nodiscard]] inline bool is_block_file(const path& __p, error_code& __ec) noexcept {
151 return is_block_file(s: __status(__p, ec: &__ec));
152}
153[[nodiscard]] inline bool is_character_file(file_status __s) noexcept { return __s.type() == file_type::character; }
154[[nodiscard]] inline bool is_character_file(const path& __p) { return is_character_file(s: __status(__p)); }
155[[nodiscard]] inline bool is_character_file(const path& __p, error_code& __ec) noexcept {
156 return is_character_file(s: __status(__p, ec: &__ec));
157}
158[[nodiscard]] inline bool is_directory(file_status __s) noexcept { return __s.type() == file_type::directory; }
159[[nodiscard]] inline bool is_directory(const path& __p) { return is_directory(s: __status(__p)); }
160[[nodiscard]] inline bool is_directory(const path& __p, error_code& __ec) noexcept {
161 return is_directory(s: __status(__p, ec: &__ec));
162}
163[[nodiscard]] inline bool is_empty(const path& __p) { return __fs_is_empty(__p); }
164[[nodiscard]] inline bool is_empty(const path& __p, error_code& __ec) { return __fs_is_empty(__p, ec: &__ec); }
165[[nodiscard]] inline bool is_fifo(file_status __s) noexcept { return __s.type() == file_type::fifo; }
166[[nodiscard]] inline bool is_fifo(const path& __p) { return is_fifo(s: __status(__p)); }
167[[nodiscard]] inline bool is_fifo(const path& __p, error_code& __ec) noexcept { return is_fifo(s: __status(__p, ec: &__ec)); }
168[[nodiscard]] inline bool is_regular_file(file_status __s) noexcept { return __s.type() == file_type::regular; }
169[[nodiscard]] inline bool is_regular_file(const path& __p) { return is_regular_file(s: __status(__p)); }
170[[nodiscard]] inline bool is_regular_file(const path& __p, error_code& __ec) noexcept {
171 return is_regular_file(s: __status(__p, ec: &__ec));
172}
173[[nodiscard]] inline bool is_symlink(file_status __s) noexcept { return __s.type() == file_type::symlink; }
174[[nodiscard]] inline bool is_symlink(const path& __p) { return is_symlink(s: __symlink_status(__p)); }
175[[nodiscard]] inline bool is_symlink(const path& __p, error_code& __ec) noexcept {
176 return is_symlink(s: __symlink_status(__p, ec: &__ec));
177}
178[[nodiscard]] inline bool is_other(file_status __s) noexcept {
179 return exists(__s) && !is_regular_file(__s) && !is_directory(__s) && !is_symlink(__s);
180}
181[[nodiscard]] inline bool is_other(const path& __p) { return is_other(s: __status(__p)); }
182[[nodiscard]] inline bool is_other(const path& __p, error_code& __ec) noexcept {
183 return is_other(s: __status(__p, ec: &__ec));
184}
185[[nodiscard]] inline bool is_socket(file_status __s) noexcept { return __s.type() == file_type::socket; }
186[[nodiscard]] inline bool is_socket(const path& __p) { return is_socket(s: __status(__p)); }
187[[nodiscard]] inline bool is_socket(const path& __p, error_code& __ec) noexcept {
188 return is_socket(s: __status(__p, ec: &__ec));
189}
190[[nodiscard]] inline file_time_type last_write_time(const path& __p) { return __last_write_time(__p); }
191[[nodiscard]] inline file_time_type last_write_time(const path& __p, error_code& __ec) noexcept {
192 return __last_write_time(__p, ec: &__ec);
193}
194inline void last_write_time(const path& __p, file_time_type __t) { __last_write_time(__p, new_time: __t); }
195inline void last_write_time(const path& __p, file_time_type __t, error_code& __ec) noexcept {
196 __last_write_time(__p, new_time: __t, ec: &__ec);
197}
198inline void permissions(const path& __p, perms __prms, perm_options __opts = perm_options::replace) {
199 __permissions(__p, __prms, __opts);
200}
201inline void permissions(const path& __p, perms __prms, error_code& __ec) noexcept {
202 __permissions(__p, __prms, perm_options::replace, &__ec);
203}
204inline void permissions(const path& __p, perms __prms, perm_options __opts, error_code& __ec) {
205 __permissions(__p, __prms, __opts, &__ec);
206}
207
208[[nodiscard]] inline path proximate(const path& __p, const path& __base, error_code& __ec) {
209 path __tmp = __weakly_canonical(__p, ec: &__ec);
210 if (__ec)
211 return {};
212 path __tmp_base = __weakly_canonical(p: __base, ec: &__ec);
213 if (__ec)
214 return {};
215 return __tmp.lexically_proximate(base: __tmp_base);
216}
217
218[[nodiscard]] inline path proximate(const path& __p, error_code& __ec) { return proximate(__p, base: current_path(), __ec); }
219[[nodiscard]] inline path proximate(const path& __p, const path& __base = current_path()) {
220 return __weakly_canonical(__p).lexically_proximate(base: __weakly_canonical(p: __base));
221}
222[[nodiscard]] inline path read_symlink(const path& __p) { return __read_symlink(__p); }
223[[nodiscard]] inline path read_symlink(const path& __p, error_code& __ec) { return __read_symlink(__p, ec: &__ec); }
224
225[[nodiscard]] inline path relative(const path& __p, const path& __base, error_code& __ec) {
226 path __tmp = __weakly_canonical(__p, ec: &__ec);
227 if (__ec)
228 return path();
229 path __tmpbase = __weakly_canonical(p: __base, ec: &__ec);
230 if (__ec)
231 return path();
232 return __tmp.lexically_relative(base: __tmpbase);
233}
234
235[[nodiscard]] inline path relative(const path& __p, error_code& __ec) { return relative(__p, base: current_path(), __ec); }
236[[nodiscard]] inline path relative(const path& __p, const path& __base = current_path()) {
237 return __weakly_canonical(__p).lexically_relative(base: __weakly_canonical(p: __base));
238}
239inline uintmax_t remove_all(const path& __p) { return __remove_all(__p); }
240inline uintmax_t remove_all(const path& __p, error_code& __ec) { return __remove_all(__p, ec: &__ec); }
241inline bool remove(const path& __p) { return __remove(__p); }
242inline bool remove(const path& __p, error_code& __ec) noexcept { return __remove(__p, ec: &__ec); }
243inline void rename(const path& __from, const path& __to) { return __rename(__from, __to); }
244inline void rename(const path& __from, const path& __to, error_code& __ec) noexcept {
245 return __rename(__from, __to, ec: &__ec);
246}
247inline void resize_file(const path& __p, uintmax_t __ns) { return __resize_file(__p, size: __ns); }
248inline void resize_file(const path& __p, uintmax_t __ns, error_code& __ec) noexcept {
249 return __resize_file(__p, size: __ns, &__ec);
250}
251[[nodiscard]] inline space_info space(const path& __p) { return __space(__p); }
252[[nodiscard]] inline space_info space(const path& __p, error_code& __ec) noexcept { return __space(__p, ec: &__ec); }
253[[nodiscard]] inline file_status status(const path& __p) { return __status(__p); }
254[[nodiscard]] inline file_status status(const path& __p, error_code& __ec) noexcept { return __status(__p, ec: &__ec); }
255[[nodiscard]] inline file_status symlink_status(const path& __p) { return __symlink_status(__p); }
256[[nodiscard]] inline file_status symlink_status(const path& __p, error_code& __ec) noexcept {
257 return __symlink_status(__p, ec: &__ec);
258}
259[[nodiscard]] inline path temp_directory_path() { return __temp_directory_path(); }
260[[nodiscard]] inline path temp_directory_path(error_code& __ec) { return __temp_directory_path(ec: &__ec); }
261[[nodiscard]] inline path weakly_canonical(path const& __p) { return __weakly_canonical(__p); }
262[[nodiscard]] inline path weakly_canonical(path const& __p, error_code& __ec) { return __weakly_canonical(__p, ec: &__ec); }
263
264_LIBCPP_END_NAMESPACE_FILESYSTEM
265
266#endif // _LIBCPP_STD_VER >= 17 && _LIBCPP_HAS_FILESYSTEM
267
268#endif // _LIBCPP___FILESYSTEM_OPERATIONS_H
269