Path: blob/main/contrib/llvm-project/libcxx/include/__filesystem/operations.h
35262 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___FILESYSTEM_OPERATIONS_H10#define _LIBCPP___FILESYSTEM_OPERATIONS_H1112#include <__chrono/time_point.h>13#include <__config>14#include <__filesystem/copy_options.h>15#include <__filesystem/file_status.h>16#include <__filesystem/file_time_type.h>17#include <__filesystem/file_type.h>18#include <__filesystem/path.h>19#include <__filesystem/perm_options.h>20#include <__filesystem/perms.h>21#include <__filesystem/space_info.h>22#include <__system_error/error_code.h>23#include <cstdint>2425#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)26# pragma GCC system_header27#endif2829#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)3031_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM3233_LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_PUSH3435_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 bool38__copy_file(const path& __from, const path& __to, copy_options __opt, error_code* __ec = nullptr);39_LIBCPP_EXPORTED_FROM_ABI void40__copy_symlink(const path& __existing_symlink, const path& __new_symlink, error_code* __ec = nullptr);41_LIBCPP_EXPORTED_FROM_ABI void42__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 void45__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 void49__create_hard_link(const path& __to, const path& __new_hard_link, error_code* __ec = nullptr);50_LIBCPP_EXPORTED_FROM_ABI void51__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);6869inline _LIBCPP_HIDE_FROM_ABI path absolute(const path& __p) { return __absolute(__p); }70inline _LIBCPP_HIDE_FROM_ABI path absolute(const path& __p, error_code& __ec) { return __absolute(__p, &__ec); }71inline _LIBCPP_HIDE_FROM_ABI path canonical(const path& __p) { return __canonical(__p); }72inline _LIBCPP_HIDE_FROM_ABI path canonical(const path& __p, error_code& __ec) { return __canonical(__p, &__ec); }73inline _LIBCPP_HIDE_FROM_ABI bool copy_file(const path& __from, const path& __to) {74return __copy_file(__from, __to, copy_options::none);75}76inline _LIBCPP_HIDE_FROM_ABI bool copy_file(const path& __from, const path& __to, error_code& __ec) {77return __copy_file(__from, __to, copy_options::none, &__ec);78}79inline _LIBCPP_HIDE_FROM_ABI bool copy_file(const path& __from, const path& __to, copy_options __opt) {80return __copy_file(__from, __to, __opt);81}82inline _LIBCPP_HIDE_FROM_ABI bool83copy_file(const path& __from, const path& __to, copy_options __opt, error_code& __ec) {84return __copy_file(__from, __to, __opt, &__ec);85}86inline _LIBCPP_HIDE_FROM_ABI void copy_symlink(const path& __from, const path& __to) { __copy_symlink(__from, __to); }87inline _LIBCPP_HIDE_FROM_ABI void copy_symlink(const path& __from, const path& __to, error_code& __ec) noexcept {88__copy_symlink(__from, __to, &__ec);89}90inline _LIBCPP_HIDE_FROM_ABI void copy(const path& __from, const path& __to) {91__copy(__from, __to, copy_options::none);92}93inline _LIBCPP_HIDE_FROM_ABI void copy(const path& __from, const path& __to, error_code& __ec) {94__copy(__from, __to, copy_options::none, &__ec);95}96inline _LIBCPP_HIDE_FROM_ABI void copy(const path& __from, const path& __to, copy_options __opt) {97__copy(__from, __to, __opt);98}99inline _LIBCPP_HIDE_FROM_ABI void copy(const path& __from, const path& __to, copy_options __opt, error_code& __ec) {100__copy(__from, __to, __opt, &__ec);101}102inline _LIBCPP_HIDE_FROM_ABI bool create_directories(const path& __p) { return __create_directories(__p); }103inline _LIBCPP_HIDE_FROM_ABI bool create_directories(const path& __p, error_code& __ec) {104return __create_directories(__p, &__ec);105}106inline _LIBCPP_HIDE_FROM_ABI void create_directory_symlink(const path& __target, const path& __link) {107__create_directory_symlink(__target, __link);108}109inline _LIBCPP_HIDE_FROM_ABI void110create_directory_symlink(const path& __target, const path& __link, error_code& __ec) noexcept {111__create_directory_symlink(__target, __link, &__ec);112}113inline _LIBCPP_HIDE_FROM_ABI bool create_directory(const path& __p) { return __create_directory(__p); }114inline _LIBCPP_HIDE_FROM_ABI bool create_directory(const path& __p, error_code& __ec) noexcept {115return __create_directory(__p, &__ec);116}117inline _LIBCPP_HIDE_FROM_ABI bool create_directory(const path& __p, const path& __attrs) {118return __create_directory(__p, __attrs);119}120inline _LIBCPP_HIDE_FROM_ABI bool create_directory(const path& __p, const path& __attrs, error_code& __ec) noexcept {121return __create_directory(__p, __attrs, &__ec);122}123inline _LIBCPP_HIDE_FROM_ABI void create_hard_link(const path& __target, const path& __link) {124__create_hard_link(__target, __link);125}126inline _LIBCPP_HIDE_FROM_ABI void127create_hard_link(const path& __target, const path& __link, error_code& __ec) noexcept {128__create_hard_link(__target, __link, &__ec);129}130inline _LIBCPP_HIDE_FROM_ABI void create_symlink(const path& __target, const path& __link) {131__create_symlink(__target, __link);132}133inline _LIBCPP_HIDE_FROM_ABI void create_symlink(const path& __target, const path& __link, error_code& __ec) noexcept {134return __create_symlink(__target, __link, &__ec);135}136inline _LIBCPP_HIDE_FROM_ABI path current_path() { return __current_path(); }137inline _LIBCPP_HIDE_FROM_ABI path current_path(error_code& __ec) { return __current_path(&__ec); }138inline _LIBCPP_HIDE_FROM_ABI void current_path(const path& __p) { __current_path(__p); }139inline _LIBCPP_HIDE_FROM_ABI void current_path(const path& __p, error_code& __ec) noexcept {140__current_path(__p, &__ec);141}142inline _LIBCPP_HIDE_FROM_ABI bool equivalent(const path& __p1, const path& __p2) { return __equivalent(__p1, __p2); }143inline _LIBCPP_HIDE_FROM_ABI bool equivalent(const path& __p1, const path& __p2, error_code& __ec) noexcept {144return __equivalent(__p1, __p2, &__ec);145}146inline _LIBCPP_HIDE_FROM_ABI bool status_known(file_status __s) noexcept { return __s.type() != file_type::none; }147inline _LIBCPP_HIDE_FROM_ABI bool exists(file_status __s) noexcept {148return status_known(__s) && __s.type() != file_type::not_found;149}150inline _LIBCPP_HIDE_FROM_ABI bool exists(const path& __p) { return exists(__status(__p)); }151152inline _LIBCPP_HIDE_FROM_ABI bool exists(const path& __p, error_code& __ec) noexcept {153auto __s = __status(__p, &__ec);154if (status_known(__s))155__ec.clear();156return exists(__s);157}158159inline _LIBCPP_HIDE_FROM_ABI uintmax_t file_size(const path& __p) { return __file_size(__p); }160inline _LIBCPP_HIDE_FROM_ABI uintmax_t file_size(const path& __p, error_code& __ec) noexcept {161return __file_size(__p, &__ec);162}163inline _LIBCPP_HIDE_FROM_ABI uintmax_t hard_link_count(const path& __p) { return __hard_link_count(__p); }164inline _LIBCPP_HIDE_FROM_ABI uintmax_t hard_link_count(const path& __p, error_code& __ec) noexcept {165return __hard_link_count(__p, &__ec);166}167inline _LIBCPP_HIDE_FROM_ABI bool is_block_file(file_status __s) noexcept { return __s.type() == file_type::block; }168inline _LIBCPP_HIDE_FROM_ABI bool is_block_file(const path& __p) { return is_block_file(__status(__p)); }169inline _LIBCPP_HIDE_FROM_ABI bool is_block_file(const path& __p, error_code& __ec) noexcept {170return is_block_file(__status(__p, &__ec));171}172inline _LIBCPP_HIDE_FROM_ABI bool is_character_file(file_status __s) noexcept {173return __s.type() == file_type::character;174}175inline _LIBCPP_HIDE_FROM_ABI bool is_character_file(const path& __p) { return is_character_file(__status(__p)); }176inline _LIBCPP_HIDE_FROM_ABI bool is_character_file(const path& __p, error_code& __ec) noexcept {177return is_character_file(__status(__p, &__ec));178}179inline _LIBCPP_HIDE_FROM_ABI bool is_directory(file_status __s) noexcept { return __s.type() == file_type::directory; }180inline _LIBCPP_HIDE_FROM_ABI bool is_directory(const path& __p) { return is_directory(__status(__p)); }181inline _LIBCPP_HIDE_FROM_ABI bool is_directory(const path& __p, error_code& __ec) noexcept {182return is_directory(__status(__p, &__ec));183}184_LIBCPP_EXPORTED_FROM_ABI bool __fs_is_empty(const path& __p, error_code* __ec = nullptr);185inline _LIBCPP_HIDE_FROM_ABI bool is_empty(const path& __p) { return __fs_is_empty(__p); }186inline _LIBCPP_HIDE_FROM_ABI bool is_empty(const path& __p, error_code& __ec) { return __fs_is_empty(__p, &__ec); }187inline _LIBCPP_HIDE_FROM_ABI bool is_fifo(file_status __s) noexcept { return __s.type() == file_type::fifo; }188inline _LIBCPP_HIDE_FROM_ABI bool is_fifo(const path& __p) { return is_fifo(__status(__p)); }189inline _LIBCPP_HIDE_FROM_ABI bool is_fifo(const path& __p, error_code& __ec) noexcept {190return is_fifo(__status(__p, &__ec));191}192inline _LIBCPP_HIDE_FROM_ABI bool is_regular_file(file_status __s) noexcept { return __s.type() == file_type::regular; }193inline _LIBCPP_HIDE_FROM_ABI bool is_regular_file(const path& __p) { return is_regular_file(__status(__p)); }194inline _LIBCPP_HIDE_FROM_ABI bool is_regular_file(const path& __p, error_code& __ec) noexcept {195return is_regular_file(__status(__p, &__ec));196}197inline _LIBCPP_HIDE_FROM_ABI bool is_symlink(file_status __s) noexcept { return __s.type() == file_type::symlink; }198inline _LIBCPP_HIDE_FROM_ABI bool is_symlink(const path& __p) { return is_symlink(__symlink_status(__p)); }199inline _LIBCPP_HIDE_FROM_ABI bool is_symlink(const path& __p, error_code& __ec) noexcept {200return is_symlink(__symlink_status(__p, &__ec));201}202inline _LIBCPP_HIDE_FROM_ABI bool is_other(file_status __s) noexcept {203return exists(__s) && !is_regular_file(__s) && !is_directory(__s) && !is_symlink(__s);204}205inline _LIBCPP_HIDE_FROM_ABI bool is_other(const path& __p) { return is_other(__status(__p)); }206inline _LIBCPP_HIDE_FROM_ABI bool is_other(const path& __p, error_code& __ec) noexcept {207return is_other(__status(__p, &__ec));208}209inline _LIBCPP_HIDE_FROM_ABI bool is_socket(file_status __s) noexcept { return __s.type() == file_type::socket; }210inline _LIBCPP_HIDE_FROM_ABI bool is_socket(const path& __p) { return is_socket(__status(__p)); }211inline _LIBCPP_HIDE_FROM_ABI bool is_socket(const path& __p, error_code& __ec) noexcept {212return is_socket(__status(__p, &__ec));213}214inline _LIBCPP_HIDE_FROM_ABI file_time_type last_write_time(const path& __p) { return __last_write_time(__p); }215inline _LIBCPP_HIDE_FROM_ABI file_time_type last_write_time(const path& __p, error_code& __ec) noexcept {216return __last_write_time(__p, &__ec);217}218inline _LIBCPP_HIDE_FROM_ABI void last_write_time(const path& __p, file_time_type __t) { __last_write_time(__p, __t); }219inline _LIBCPP_HIDE_FROM_ABI void last_write_time(const path& __p, file_time_type __t, error_code& __ec) noexcept {220__last_write_time(__p, __t, &__ec);221}222_LIBCPP_EXPORTED_FROM_ABI void __permissions(const path&, perms, perm_options, error_code* = nullptr);223inline _LIBCPP_HIDE_FROM_ABI void224permissions(const path& __p, perms __prms, perm_options __opts = perm_options::replace) {225__permissions(__p, __prms, __opts);226}227inline _LIBCPP_HIDE_FROM_ABI void permissions(const path& __p, perms __prms, error_code& __ec) noexcept {228__permissions(__p, __prms, perm_options::replace, &__ec);229}230inline _LIBCPP_HIDE_FROM_ABI void permissions(const path& __p, perms __prms, perm_options __opts, error_code& __ec) {231__permissions(__p, __prms, __opts, &__ec);232}233234inline _LIBCPP_HIDE_FROM_ABI path proximate(const path& __p, const path& __base, error_code& __ec) {235path __tmp = __weakly_canonical(__p, &__ec);236if (__ec)237return {};238path __tmp_base = __weakly_canonical(__base, &__ec);239if (__ec)240return {};241return __tmp.lexically_proximate(__tmp_base);242}243244inline _LIBCPP_HIDE_FROM_ABI path proximate(const path& __p, error_code& __ec) {245return proximate(__p, current_path(), __ec);246}247inline _LIBCPP_HIDE_FROM_ABI path proximate(const path& __p, const path& __base = current_path()) {248return __weakly_canonical(__p).lexically_proximate(__weakly_canonical(__base));249}250inline _LIBCPP_HIDE_FROM_ABI path read_symlink(const path& __p) { return __read_symlink(__p); }251inline _LIBCPP_HIDE_FROM_ABI path read_symlink(const path& __p, error_code& __ec) { return __read_symlink(__p, &__ec); }252253inline _LIBCPP_HIDE_FROM_ABI path relative(const path& __p, const path& __base, error_code& __ec) {254path __tmp = __weakly_canonical(__p, &__ec);255if (__ec)256return path();257path __tmpbase = __weakly_canonical(__base, &__ec);258if (__ec)259return path();260return __tmp.lexically_relative(__tmpbase);261}262263inline _LIBCPP_HIDE_FROM_ABI path relative(const path& __p, error_code& __ec) {264return relative(__p, current_path(), __ec);265}266inline _LIBCPP_HIDE_FROM_ABI path relative(const path& __p, const path& __base = current_path()) {267return __weakly_canonical(__p).lexically_relative(__weakly_canonical(__base));268}269inline _LIBCPP_HIDE_FROM_ABI uintmax_t remove_all(const path& __p) { return __remove_all(__p); }270inline _LIBCPP_HIDE_FROM_ABI uintmax_t remove_all(const path& __p, error_code& __ec) {271return __remove_all(__p, &__ec);272}273inline _LIBCPP_HIDE_FROM_ABI bool remove(const path& __p) { return __remove(__p); }274inline _LIBCPP_HIDE_FROM_ABI bool remove(const path& __p, error_code& __ec) noexcept { return __remove(__p, &__ec); }275inline _LIBCPP_HIDE_FROM_ABI void rename(const path& __from, const path& __to) { return __rename(__from, __to); }276inline _LIBCPP_HIDE_FROM_ABI void rename(const path& __from, const path& __to, error_code& __ec) noexcept {277return __rename(__from, __to, &__ec);278}279inline _LIBCPP_HIDE_FROM_ABI void resize_file(const path& __p, uintmax_t __ns) { return __resize_file(__p, __ns); }280inline _LIBCPP_HIDE_FROM_ABI void resize_file(const path& __p, uintmax_t __ns, error_code& __ec) noexcept {281return __resize_file(__p, __ns, &__ec);282}283_LIBCPP_EXPORTED_FROM_ABI space_info __space(const path&, error_code* __ec = nullptr);284inline _LIBCPP_HIDE_FROM_ABI space_info space(const path& __p) { return __space(__p); }285inline _LIBCPP_HIDE_FROM_ABI space_info space(const path& __p, error_code& __ec) noexcept {286return __space(__p, &__ec);287}288inline _LIBCPP_HIDE_FROM_ABI file_status status(const path& __p) { return __status(__p); }289inline _LIBCPP_HIDE_FROM_ABI file_status status(const path& __p, error_code& __ec) noexcept {290return __status(__p, &__ec);291}292inline _LIBCPP_HIDE_FROM_ABI file_status symlink_status(const path& __p) { return __symlink_status(__p); }293inline _LIBCPP_HIDE_FROM_ABI file_status symlink_status(const path& __p, error_code& __ec) noexcept {294return __symlink_status(__p, &__ec);295}296inline _LIBCPP_HIDE_FROM_ABI path temp_directory_path() { return __temp_directory_path(); }297inline _LIBCPP_HIDE_FROM_ABI path temp_directory_path(error_code& __ec) { return __temp_directory_path(&__ec); }298inline _LIBCPP_HIDE_FROM_ABI path weakly_canonical(path const& __p) { return __weakly_canonical(__p); }299inline _LIBCPP_HIDE_FROM_ABI path weakly_canonical(path const& __p, error_code& __ec) {300return __weakly_canonical(__p, &__ec);301}302303_LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY_POP304305_LIBCPP_END_NAMESPACE_FILESYSTEM306307#endif // _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM)308309#endif // _LIBCPP___FILESYSTEM_OPERATIONS_H310311312