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