Path: blob/main/system/lib/libcxx/src/filesystem/operations.cpp
6178 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include <__algorithm/copy.h>9#include <__assert>10#include <__config>11#include <__utility/unreachable.h>12#include <array>13#include <climits>14#include <cstdlib>15#include <filesystem>16#include <iterator>17#include <string_view>18#include <system_error>19#include <type_traits>20#include <vector>2122#include "error.h"23#include "file_descriptor.h"24#include "path_parser.h"25#include "posix_compat.h"26#include "time_utils.h"2728#if defined(_LIBCPP_WIN32API)29# define WIN32_LEAN_AND_MEAN30# define NOMINMAX31# include <windows.h>32#else33# include <dirent.h>34# include <sys/stat.h>35# include <sys/statvfs.h>36# include <sys/types.h>37# include <unistd.h>38#endif39#include <fcntl.h> /* values for fchmodat */40#include <time.h>4142// since Linux 4.5 and FreeBSD 13, but the Linux libc wrapper is only provided by glibc >= 2.27 and musl43#if defined(__linux__)44# if defined(_LIBCPP_GLIBC_PREREQ)45# if _LIBCPP_GLIBC_PREREQ(2, 27)46# define _LIBCPP_FILESYSTEM_USE_COPY_FILE_RANGE47# endif48# elif _LIBCPP_HAS_MUSL_LIBC49# define _LIBCPP_FILESYSTEM_USE_COPY_FILE_RANGE50# endif51#elif defined(__FreeBSD__)52# define _LIBCPP_FILESYSTEM_USE_COPY_FILE_RANGE53#endif54#if __has_include(<sys/sendfile.h>)55# include <sys/sendfile.h>56# define _LIBCPP_FILESYSTEM_USE_SENDFILE57#elif defined(__APPLE__) || __has_include(<copyfile.h>)58# include <copyfile.h>59# define _LIBCPP_FILESYSTEM_USE_COPYFILE60#else61# define _LIBCPP_FILESYSTEM_USE_FSTREAM62#endif6364// sendfile and copy_file_range need to fall back65// to the fstream implementation for special files66#if (defined(_LIBCPP_FILESYSTEM_USE_SENDFILE) || defined(_LIBCPP_FILESYSTEM_USE_COPY_FILE_RANGE) || \67defined(_LIBCPP_FILESYSTEM_USE_FSTREAM)) && \68_LIBCPP_HAS_LOCALIZATION69# include <fstream>70# define _LIBCPP_FILESYSTEM_NEED_FSTREAM71#endif7273#if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)74# pragma comment(lib, "rt")75#endif7677_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM7879using detail::capture_errno;80using detail::ErrorHandler;81using detail::StatT;82using detail::TimeSpec;83using parser::createView;84using parser::PathParser;85using parser::string_view_t;8687static path __do_absolute(const path& p, path* cwd, error_code* ec) {88if (ec)89ec->clear();90if (p.is_absolute())91return p;92*cwd = __current_path(ec);93if (ec && *ec)94return {};95return (*cwd) / p;96}9798path __absolute(const path& p, error_code* ec) {99path cwd;100return __do_absolute(p, &cwd, ec);101}102103path __canonical(path const& orig_p, error_code* ec) {104path cwd;105ErrorHandler<path> err("canonical", ec, &orig_p, &cwd);106107path p = __do_absolute(orig_p, &cwd, ec);108#if (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112) || defined(_LIBCPP_WIN32API)109std::unique_ptr<path::value_type, decltype(&::free)> hold(detail::realpath(p.c_str(), nullptr), &::free);110if (hold.get() == nullptr)111return err.report(detail::get_last_error());112return {hold.get()};113#else114# if defined(__MVS__) && !defined(PATH_MAX)115path::value_type buff[_XOPEN_PATH_MAX + 1];116# else117path::value_type buff[PATH_MAX + 1];118# endif119path::value_type* ret;120if ((ret = detail::realpath(p.c_str(), buff)) == nullptr)121return err.report(detail::get_last_error());122return {ret};123#endif124}125126void __copy(const path& from, const path& to, copy_options options, error_code* ec) {127ErrorHandler<void> err("copy", ec, &from, &to);128129const bool sym_status = bool(options & (copy_options::create_symlinks | copy_options::skip_symlinks));130131const bool sym_status2 = bool(options & copy_options::copy_symlinks);132133error_code m_ec1;134StatT f_st;135const file_status f =136sym_status || sym_status2 ? detail::posix_lstat(from, f_st, &m_ec1) : detail::posix_stat(from, f_st, &m_ec1);137if (m_ec1)138return err.report(m_ec1);139140StatT t_st;141const file_status t = sym_status ? detail::posix_lstat(to, t_st, &m_ec1) : detail::posix_stat(to, t_st, &m_ec1);142143if (not status_known(t))144return err.report(m_ec1);145146if (!exists(f) || is_other(f) || is_other(t) || (is_directory(f) && is_regular_file(t)) ||147(exists(t) && detail::stat_equivalent(f_st, t_st))) {148return err.report(errc::function_not_supported);149}150151if (is_symlink(f)) {152if (bool(copy_options::skip_symlinks & options)) {153// do nothing154} else if (not exists(t)) {155__copy_symlink(from, to, ec);156} else {157return err.report(errc::file_exists);158}159return;160} else if (is_regular_file(f)) {161if (bool(copy_options::directories_only & options)) {162// do nothing163} else if (bool(copy_options::create_symlinks & options)) {164__create_symlink(from, to, ec);165} else if (bool(copy_options::create_hard_links & options)) {166__create_hard_link(from, to, ec);167} else if (is_directory(t)) {168__copy_file(from, to / from.filename(), options, ec);169} else {170__copy_file(from, to, options, ec);171}172return;173} else if (is_directory(f) && bool(copy_options::create_symlinks & options)) {174return err.report(errc::is_a_directory);175} else if (is_directory(f) && (bool(copy_options::recursive & options) || copy_options::none == options)) {176if (!exists(t)) {177// create directory to with attributes from 'from'.178__create_directory(to, from, ec);179if (ec && *ec) {180return;181}182}183directory_iterator it = ec ? directory_iterator(from, *ec) : directory_iterator(from);184if (ec && *ec) {185return;186}187error_code m_ec2;188for (; !m_ec2 && it != directory_iterator(); it.increment(m_ec2)) {189__copy(it->path(), to / it->path().filename(), options | copy_options::__in_recursive_copy, ec);190if (ec && *ec) {191return;192}193}194if (m_ec2) {195return err.report(m_ec2);196}197}198}199200namespace detail {201namespace {202203#if defined(_LIBCPP_FILESYSTEM_NEED_FSTREAM)204bool copy_file_impl_fstream(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {205ifstream in;206in.__open(read_fd.fd, ios::binary);207if (!in.is_open()) {208// This assumes that __open didn't reset the error code.209ec = capture_errno();210return false;211}212read_fd.fd = -1;213ofstream out;214out.__open(write_fd.fd, ios::binary);215if (!out.is_open()) {216ec = capture_errno();217return false;218}219write_fd.fd = -1;220221if (in.good() && out.good()) {222using InIt = istreambuf_iterator<char>;223using OutIt = ostreambuf_iterator<char>;224InIt bin(in);225InIt ein;226OutIt bout(out);227copy(bin, ein, bout);228}229if (out.fail() || in.fail()) {230ec = make_error_code(errc::io_error);231return false;232}233234ec.clear();235return true;236}237#endif238239#if defined(_LIBCPP_FILESYSTEM_USE_COPY_FILE_RANGE)240bool copy_file_impl_copy_file_range(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {241size_t count = read_fd.get_stat().st_size;242// a zero-length file is either empty, or not copyable by this syscall243// return early to avoid the syscall cost244if (count == 0) {245ec = {EINVAL, generic_category()};246return false;247}248// do not modify the fd positions as copy_file_impl_sendfile may be called after a partial copy249# if defined(__linux__)250loff_t off_in = 0;251loff_t off_out = 0;252# else253off_t off_in = 0;254off_t off_out = 0;255# endif256257do {258ssize_t res;259260if ((res = ::copy_file_range(read_fd.fd, &off_in, write_fd.fd, &off_out, count, 0)) == -1) {261ec = capture_errno();262return false;263}264count -= res;265} while (count > 0);266267ec.clear();268269return true;270}271#endif272273#if defined(_LIBCPP_FILESYSTEM_USE_SENDFILE)274bool copy_file_impl_sendfile(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {275size_t count = read_fd.get_stat().st_size;276// a zero-length file is either empty, or not copyable by this syscall277// return early to avoid the syscall cost278// however, we can't afford this luxury in the no-locale build,279// as we can't utilize the fstream impl to copy empty files280# if _LIBCPP_HAS_LOCALIZATION281if (count == 0) {282ec = {EINVAL, generic_category()};283return false;284}285# endif286do {287ssize_t res;288if ((res = ::sendfile(write_fd.fd, read_fd.fd, nullptr, count)) == -1) {289ec = capture_errno();290return false;291}292count -= res;293} while (count > 0);294295ec.clear();296297return true;298}299#endif300301#if defined(_LIBCPP_FILESYSTEM_USE_COPY_FILE_RANGE) || defined(_LIBCPP_FILESYSTEM_USE_SENDFILE)302// If we have copy_file_range or sendfile, try both in succession (if available).303// If both fail, fall back to using fstream.304bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {305# if defined(_LIBCPP_FILESYSTEM_USE_COPY_FILE_RANGE)306if (copy_file_impl_copy_file_range(read_fd, write_fd, ec)) {307return true;308}309// EINVAL: src and dst are the same file (this is not cheaply310// detectable from userspace)311// EINVAL: copy_file_range is unsupported for this file type by the312// underlying filesystem313// ENOTSUP: undocumented, can arise with old kernels and NFS314// EOPNOTSUPP: filesystem does not implement copy_file_range315// ETXTBSY: src or dst is an active swapfile (nonsensical, but allowed316// with normal copying)317// EXDEV: src and dst are on different filesystems that do not support318// cross-fs copy_file_range319// ENOENT: undocumented, can arise with CIFS320// ENOSYS: unsupported by kernel or blocked by seccomp321if (ec.value() != EINVAL && ec.value() != ENOTSUP && ec.value() != EOPNOTSUPP && ec.value() != ETXTBSY &&322ec.value() != EXDEV && ec.value() != ENOENT && ec.value() != ENOSYS) {323return false;324}325ec.clear();326# endif327328# if defined(_LIBCPP_FILESYSTEM_USE_SENDFILE)329if (copy_file_impl_sendfile(read_fd, write_fd, ec)) {330return true;331}332// EINVAL: unsupported file type333if (ec.value() != EINVAL) {334return false;335}336ec.clear();337# endif338339# if defined(_LIBCPP_FILESYSTEM_NEED_FSTREAM)340return copy_file_impl_fstream(read_fd, write_fd, ec);341# else342// since iostreams are unavailable in the no-locale build, just fail after a failed sendfile343ec.assign(EINVAL, std::system_category());344return false;345# endif346}347#elif defined(_LIBCPP_FILESYSTEM_USE_COPYFILE)348bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {349struct CopyFileState {350copyfile_state_t state;351CopyFileState() { state = copyfile_state_alloc(); }352~CopyFileState() { copyfile_state_free(state); }353354private:355CopyFileState(CopyFileState const&) = delete;356CopyFileState& operator=(CopyFileState const&) = delete;357};358359CopyFileState cfs;360if (fcopyfile(read_fd.fd, write_fd.fd, cfs.state, COPYFILE_DATA) < 0) {361ec = capture_errno();362return false;363}364365ec.clear();366return true;367}368#elif defined(_LIBCPP_FILESYSTEM_USE_FSTREAM)369bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {370return copy_file_impl_fstream(read_fd, write_fd, ec);371}372#else373# error "Unknown implementation for copy_file_impl"374#endif // copy_file_impl implementation375376} // end anonymous namespace377} // namespace detail378379bool __copy_file(const path& from, const path& to, copy_options options, error_code* ec) {380using detail::FileDescriptor;381ErrorHandler<bool> err("copy_file", ec, &to, &from);382383error_code m_ec;384FileDescriptor from_fd = FileDescriptor::create_with_status(&from, m_ec, O_RDONLY | O_NONBLOCK | O_BINARY);385if (m_ec)386return err.report(m_ec);387388auto from_st = from_fd.get_status();389StatT const& from_stat = from_fd.get_stat();390if (!is_regular_file(from_st)) {391if (not m_ec)392m_ec = make_error_code(errc::not_supported);393return err.report(m_ec);394}395396const bool skip_existing = bool(copy_options::skip_existing & options);397const bool update_existing = bool(copy_options::update_existing & options);398const bool overwrite_existing = bool(copy_options::overwrite_existing & options);399400StatT to_stat_path;401file_status to_st = detail::posix_stat(to, to_stat_path, &m_ec);402if (!status_known(to_st))403return err.report(m_ec);404405const bool to_exists = exists(to_st);406if (to_exists && !is_regular_file(to_st))407return err.report(errc::not_supported);408409if (to_exists && detail::stat_equivalent(from_stat, to_stat_path))410return err.report(errc::file_exists);411412if (to_exists && skip_existing)413return false;414415bool ShouldCopy = [&]() {416if (to_exists && update_existing) {417auto from_time = detail::extract_mtime(from_stat);418auto to_time = detail::extract_mtime(to_stat_path);419if (from_time.tv_sec < to_time.tv_sec)420return false;421if (from_time.tv_sec == to_time.tv_sec && from_time.tv_nsec <= to_time.tv_nsec)422return false;423return true;424}425if (!to_exists || overwrite_existing)426return true;427return err.report(errc::file_exists);428}();429if (!ShouldCopy)430return false;431432// Don't truncate right away. We may not be opening the file we originally433// looked at; we'll check this later.434int to_open_flags = O_WRONLY | O_BINARY;435if (!to_exists)436to_open_flags |= O_CREAT;437FileDescriptor to_fd = FileDescriptor::create_with_status(&to, m_ec, to_open_flags, from_stat.st_mode);438if (m_ec)439return err.report(m_ec);440441if (to_exists) {442// Check that the file we initially stat'ed is equivalent to the one443// we opened.444// FIXME: report this better.445if (!detail::stat_equivalent(to_stat_path, to_fd.get_stat()))446return err.report(errc::bad_file_descriptor);447448// Set the permissions and truncate the file we opened.449if (detail::posix_fchmod(to_fd, from_stat, m_ec))450return err.report(m_ec);451if (detail::posix_ftruncate(to_fd, 0, m_ec))452return err.report(m_ec);453}454455if (!detail::copy_file_impl(from_fd, to_fd, m_ec)) {456// FIXME: Remove the dest file if we failed, and it didn't exist previously.457return err.report(m_ec);458}459460return true;461}462463void __copy_symlink(const path& existing_symlink, const path& new_symlink, error_code* ec) {464const path real_path(__read_symlink(existing_symlink, ec));465if (ec && *ec) {466return;467}468#if defined(_LIBCPP_WIN32API)469error_code local_ec;470if (is_directory(real_path, local_ec))471__create_directory_symlink(real_path, new_symlink, ec);472else473#endif474__create_symlink(real_path, new_symlink, ec);475}476477bool __create_directories(const path& p, error_code* ec) {478ErrorHandler<bool> err("create_directories", ec, &p);479480error_code m_ec;481auto const st = detail::posix_stat(p, &m_ec);482if (!status_known(st))483return err.report(m_ec);484else if (is_directory(st))485return false;486else if (exists(st))487return err.report(errc::file_exists);488489const path parent = p.parent_path();490if (!parent.empty()) {491const file_status parent_st = status(parent, m_ec);492if (not status_known(parent_st))493return err.report(m_ec);494if (not exists(parent_st)) {495if (parent == p)496return err.report(errc::invalid_argument);497__create_directories(parent, ec);498if (ec && *ec) {499return false;500}501} else if (not is_directory(parent_st))502return err.report(errc::not_a_directory);503}504bool ret = __create_directory(p, &m_ec);505if (m_ec)506return err.report(m_ec);507return ret;508}509510bool __create_directory(const path& p, error_code* ec) {511ErrorHandler<bool> err("create_directory", ec, &p);512513if (detail::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0)514return true;515516error_code mec = detail::get_last_error();517if (mec != errc::file_exists)518return err.report(mec);519error_code ignored_ec;520const file_status st = status(p, ignored_ec);521if (!is_directory(st))522return err.report(mec);523return false;524}525526bool __create_directory(path const& p, path const& attributes, error_code* ec) {527ErrorHandler<bool> err("create_directory", ec, &p, &attributes);528529StatT attr_stat;530error_code mec;531file_status st = detail::posix_stat(attributes, attr_stat, &mec);532if (!status_known(st))533return err.report(mec);534if (!is_directory(st))535return err.report(errc::not_a_directory, "the specified attribute path is invalid");536537if (detail::mkdir(p.c_str(), attr_stat.st_mode) == 0)538return true;539540mec = detail::get_last_error();541if (mec != errc::file_exists)542return err.report(mec);543544error_code ignored_ec;545st = status(p, ignored_ec);546if (!is_directory(st))547return err.report(mec);548return false;549}550551void __create_directory_symlink(path const& from, path const& to, error_code* ec) {552ErrorHandler<void> err("create_directory_symlink", ec, &from, &to);553if (detail::symlink_dir(from.c_str(), to.c_str()) == -1)554return err.report(detail::get_last_error());555}556557void __create_hard_link(const path& from, const path& to, error_code* ec) {558ErrorHandler<void> err("create_hard_link", ec, &from, &to);559if (detail::link(from.c_str(), to.c_str()) == -1)560return err.report(detail::get_last_error());561}562563void __create_symlink(path const& from, path const& to, error_code* ec) {564ErrorHandler<void> err("create_symlink", ec, &from, &to);565if (detail::symlink_file(from.c_str(), to.c_str()) == -1)566return err.report(detail::get_last_error());567}568569path __current_path(error_code* ec) {570ErrorHandler<path> err("current_path", ec);571572#if defined(_LIBCPP_WIN32API) || defined(__GLIBC__) || defined(__APPLE__)573// Common extension outside of POSIX getcwd() spec, without needing to574// preallocate a buffer. Also supported by a number of other POSIX libcs.575int size = 0;576path::value_type* ptr = nullptr;577typedef decltype(&::free) Deleter;578Deleter deleter = &::free;579#else580errno = 0; // Note: POSIX mandates that modifying `errno` is thread-safe.581auto size = ::pathconf(".", _PC_PATH_MAX);582if (size == -1) {583if (errno != 0) {584return err.report(capture_errno(), "call to pathconf failed");585586// `pathconf` returns `-1` without an error to indicate no limit.587} else {588# if defined(__MVS__) && !defined(PATH_MAX)589size = _XOPEN_PATH_MAX + 1;590# else591size = PATH_MAX + 1;592# endif593}594}595596auto buff = unique_ptr<path::value_type[]>(new path::value_type[size + 1]);597path::value_type* ptr = buff.get();598599// Preallocated buffer, don't free the buffer in the second unique_ptr600// below.601struct Deleter {602void operator()(void*) const {}603};604Deleter deleter;605#endif606607unique_ptr<path::value_type, Deleter> hold(detail::getcwd(ptr, size), deleter);608if (hold.get() == nullptr)609return err.report(detail::get_last_error(), "call to getcwd failed");610611return {hold.get()};612}613614void __current_path(const path& p, error_code* ec) {615ErrorHandler<void> err("current_path", ec, &p);616if (detail::chdir(p.c_str()) == -1)617err.report(detail::get_last_error());618}619620bool __equivalent(const path& p1, const path& p2, error_code* ec) {621ErrorHandler<bool> err("equivalent", ec, &p1, &p2);622623error_code ec1, ec2;624StatT st1 = {}, st2 = {};625auto s1 = detail::posix_stat(p1.native(), st1, &ec1);626if (!exists(s1))627return err.report(errc::not_supported);628auto s2 = detail::posix_stat(p2.native(), st2, &ec2);629if (!exists(s2))630return err.report(errc::not_supported);631632return detail::stat_equivalent(st1, st2);633}634635uintmax_t __file_size(const path& p, error_code* ec) {636ErrorHandler<uintmax_t> err("file_size", ec, &p);637638error_code m_ec;639StatT st;640file_status fst = detail::posix_stat(p, st, &m_ec);641if (!exists(fst) || !is_regular_file(fst)) {642errc error_kind = is_directory(fst) ? errc::is_a_directory : errc::not_supported;643if (!m_ec)644m_ec = make_error_code(error_kind);645return err.report(m_ec);646}647// is_regular_file(p) == true648return static_cast<uintmax_t>(st.st_size);649}650651uintmax_t __hard_link_count(const path& p, error_code* ec) {652ErrorHandler<uintmax_t> err("hard_link_count", ec, &p);653654error_code m_ec;655StatT st;656detail::posix_stat(p, st, &m_ec);657if (m_ec)658return err.report(m_ec);659return static_cast<uintmax_t>(st.st_nlink);660}661662bool __fs_is_empty(const path& p, error_code* ec) {663ErrorHandler<bool> err("is_empty", ec, &p);664665error_code m_ec;666StatT pst;667auto st = detail::posix_stat(p, pst, &m_ec);668if (m_ec)669return err.report(m_ec);670else if (!is_directory(st) && !is_regular_file(st))671return err.report(errc::not_supported);672else if (is_directory(st)) {673auto it = ec ? directory_iterator(p, *ec) : directory_iterator(p);674if (ec && *ec)675return false;676return it == directory_iterator{};677} else if (is_regular_file(st))678return static_cast<uintmax_t>(pst.st_size) == 0;679680__libcpp_unreachable();681}682683file_time_type __last_write_time(const path& p, error_code* ec) {684using namespace chrono;685ErrorHandler<file_time_type> err("last_write_time", ec, &p);686687error_code m_ec;688StatT st;689detail::posix_stat(p, st, &m_ec);690if (m_ec)691return err.report(m_ec);692return detail::__extract_last_write_time(p, st, ec);693}694695void __last_write_time(const path& p, file_time_type new_time, error_code* ec) {696using detail::fs_time;697ErrorHandler<void> err("last_write_time", ec, &p);698699#if defined(_LIBCPP_WIN32API)700TimeSpec ts;701if (!fs_time::convert_to_timespec(ts, new_time))702return err.report(errc::value_too_large);703detail::WinHandle h(p.c_str(), FILE_WRITE_ATTRIBUTES, 0);704if (!h)705return err.report(detail::get_last_error());706FILETIME last_write = timespec_to_filetime(ts);707if (!SetFileTime(h, nullptr, nullptr, &last_write))708return err.report(detail::get_last_error());709#else710error_code m_ec;711array<TimeSpec, 2> tbuf;712# if !defined(_LIBCPP_USE_UTIMENSAT)713// This implementation has a race condition between determining the714// last access time and attempting to set it to the same value using715// ::utimes716StatT st;717file_status fst = detail::posix_stat(p, st, &m_ec);718if (m_ec)719return err.report(m_ec);720tbuf[0] = detail::extract_atime(st);721# else722tbuf[0].tv_sec = 0;723tbuf[0].tv_nsec = UTIME_OMIT;724# endif725if (!fs_time::convert_to_timespec(tbuf[1], new_time))726return err.report(errc::value_too_large);727728detail::set_file_times(p, tbuf, m_ec);729if (m_ec)730return err.report(m_ec);731#endif732}733734void __permissions(const path& p, perms prms, perm_options opts, error_code* ec) {735ErrorHandler<void> err("permissions", ec, &p);736737auto has_opt = [&](perm_options o) { return bool(o & opts); };738const bool resolve_symlinks = !has_opt(perm_options::nofollow);739const bool add_perms = has_opt(perm_options::add);740const bool remove_perms = has_opt(perm_options::remove);741_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(742(add_perms + remove_perms + has_opt(perm_options::replace)) == 1,743"One and only one of the perm_options constants 'replace', 'add', or 'remove' must be present in opts");744745bool set_sym_perms = false;746prms &= perms::mask;747if (!resolve_symlinks || (add_perms || remove_perms)) {748error_code m_ec;749file_status st = resolve_symlinks ? detail::posix_stat(p, &m_ec) : detail::posix_lstat(p, &m_ec);750set_sym_perms = is_symlink(st);751if (m_ec)752return err.report(m_ec);753// TODO(hardening): double-check this assertion -- it might be a valid (if rare) case when the permissions are754// unknown.755_LIBCPP_ASSERT_VALID_EXTERNAL_API_CALL(st.permissions() != perms::unknown, "Permissions unexpectedly unknown");756if (add_perms)757prms |= st.permissions();758else if (remove_perms)759prms = st.permissions() & ~prms;760}761const auto real_perms = static_cast<detail::ModeT>(prms & perms::mask);762763#if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD)764const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0;765if (detail::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) {766return err.report(detail::get_last_error());767}768#else769if (set_sym_perms)770return err.report(errc::operation_not_supported);771if (::chmod(p.c_str(), real_perms) == -1) {772return err.report(capture_errno());773}774#endif775}776777path __read_symlink(const path& p, error_code* ec) {778ErrorHandler<path> err("read_symlink", ec, &p);779780#if defined(PATH_MAX) || defined(MAX_SYMLINK_SIZE)781struct NullDeleter {782void operator()(void*) const {}783};784# ifdef MAX_SYMLINK_SIZE785const size_t size = MAX_SYMLINK_SIZE + 1;786# else787const size_t size = PATH_MAX + 1;788# endif789path::value_type stack_buff[size];790auto buff = std::unique_ptr<path::value_type[], NullDeleter>(stack_buff);791#else792StatT sb;793if (detail::lstat(p.c_str(), &sb) == -1) {794return err.report(detail::get_last_error());795}796const size_t size = sb.st_size + 1;797auto buff = unique_ptr<path::value_type[]>(new path::value_type[size]);798#endif799detail::SSizeT ret;800if ((ret = detail::readlink(p.c_str(), buff.get(), size)) == -1)801return err.report(detail::get_last_error());802// Note that `ret` returning `0` would work, resulting in a valid empty string being returned.803if (static_cast<size_t>(ret) >= size)804return err.report(errc::value_too_large);805buff[ret] = 0;806return {buff.get()};807}808809bool __remove(const path& p, error_code* ec) {810ErrorHandler<bool> err("remove", ec, &p);811if (detail::remove(p.c_str()) == -1) {812error_code mec = detail::get_last_error();813if (mec != errc::no_such_file_or_directory)814err.report(mec);815return false;816}817return true;818}819820// We currently have two implementations of `__remove_all`. The first one is general and821// used on platforms where we don't have access to the `openat()` family of POSIX functions.822// That implementation uses `directory_iterator`, however it is vulnerable to some race823// conditions, see https://reviews.llvm.org/D118134 for details.824//825// The second implementation is used on platforms where `openat()` & friends are available,826// and it threads file descriptors through recursive calls to avoid such race conditions.827#if defined(_LIBCPP_WIN32API) || defined(__MVS__)828# define REMOVE_ALL_USE_DIRECTORY_ITERATOR829#endif830831#if defined(REMOVE_ALL_USE_DIRECTORY_ITERATOR)832833namespace {834835uintmax_t remove_all_impl(path const& p, error_code& ec) {836const auto npos = static_cast<uintmax_t>(-1);837const file_status st = __symlink_status(p, &ec);838if (ec)839return npos;840uintmax_t count = 1;841if (is_directory(st)) {842for (directory_iterator it(p, ec); !ec && it != directory_iterator(); it.increment(ec)) {843auto other_count = remove_all_impl(it->path(), ec);844if (ec)845return npos;846count += other_count;847}848if (ec)849return npos;850}851if (!__remove(p, &ec))852return npos;853return count;854}855856} // namespace857858uintmax_t __remove_all(const path& p, error_code* ec) {859ErrorHandler<uintmax_t> err("remove_all", ec, &p);860861error_code mec;862auto count = remove_all_impl(p, mec);863if (mec) {864if (mec == errc::no_such_file_or_directory)865return 0;866return err.report(mec);867}868return count;869}870871#else // !REMOVE_ALL_USE_DIRECTORY_ITERATOR872873namespace {874875template <class Cleanup>876struct scope_exit {877explicit scope_exit(Cleanup const& cleanup) : cleanup_(cleanup) {}878879~scope_exit() { cleanup_(); }880881private:882Cleanup cleanup_;883};884_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(scope_exit);885886uintmax_t remove_all_impl(int parent_directory, const path& p, error_code& ec) {887// First, try to open the path as a directory.888const int options = O_CLOEXEC | O_RDONLY | O_DIRECTORY | O_NOFOLLOW;889int fd = ::openat(parent_directory, p.c_str(), options);890if (fd != -1) {891// If that worked, iterate over the contents of the directory and892// remove everything in it, recursively.893DIR* stream = ::fdopendir(fd);894if (stream == nullptr) {895::close(fd);896ec = detail::capture_errno();897return 0;898}899// Note: `::closedir` will also close the associated file descriptor, so900// there should be no call to `close(fd)`.901scope_exit close_stream([=] { ::closedir(stream); });902903uintmax_t count = 0;904while (true) {905auto [str, type] = detail::posix_readdir(stream, ec);906static_assert(std::is_same_v<decltype(str), std::string_view>);907if (str == "." || str == "..") {908continue;909} else if (ec || str.empty()) {910break; // we're done iterating through the directory911} else {912count += remove_all_impl(fd, str, ec);913}914}915916// Then, remove the now-empty directory itself.917if (::unlinkat(parent_directory, p.c_str(), AT_REMOVEDIR) == -1) {918ec = detail::capture_errno();919return count;920}921922return count + 1; // the contents of the directory + the directory itself923}924925ec = detail::capture_errno();926927// If we failed to open `p` because it didn't exist, it's not an928// error -- it might have moved or have been deleted already.929if (ec == errc::no_such_file_or_directory) {930ec.clear();931return 0;932}933934// If opening `p` failed because it wasn't a directory, remove it as935// a normal file instead. Note that `openat()` can return either ENOTDIR936// or ELOOP depending on the exact reason of the failure. On FreeBSD it937// may return EMLINK instead of ELOOP, contradicting POSIX.938if (ec == errc::not_a_directory || ec == errc::too_many_symbolic_link_levels || ec == errc::too_many_links) {939ec.clear();940if (::unlinkat(parent_directory, p.c_str(), /* flags = */ 0) == -1) {941ec = detail::capture_errno();942return 0;943}944return 1;945}946947// Otherwise, it's a real error -- we don't remove anything.948return 0;949}950951} // namespace952953uintmax_t __remove_all(const path& p, error_code* ec) {954ErrorHandler<uintmax_t> err("remove_all", ec, &p);955error_code mec;956uintmax_t count = remove_all_impl(AT_FDCWD, p, mec);957if (mec)958return err.report(mec);959return count;960}961962#endif // REMOVE_ALL_USE_DIRECTORY_ITERATOR963964void __rename(const path& from, const path& to, error_code* ec) {965ErrorHandler<void> err("rename", ec, &from, &to);966if (detail::rename(from.c_str(), to.c_str()) == -1)967err.report(detail::get_last_error());968}969970void __resize_file(const path& p, uintmax_t size, error_code* ec) {971ErrorHandler<void> err("resize_file", ec, &p);972if (detail::truncate(p.c_str(), static_cast< ::off_t>(size)) == -1)973return err.report(detail::get_last_error());974}975976space_info __space(const path& p, error_code* ec) {977ErrorHandler<void> err("space", ec, &p);978space_info si;979detail::StatVFS m_svfs = {};980if (detail::statvfs(p.c_str(), &m_svfs) == -1) {981err.report(detail::get_last_error());982si.capacity = si.free = si.available = static_cast<uintmax_t>(-1);983return si;984}985// Multiply with overflow checking.986auto do_mult = [&](uintmax_t& out, uintmax_t other) {987out = other * m_svfs.f_frsize;988if (other == 0 || out / other != m_svfs.f_frsize)989out = static_cast<uintmax_t>(-1);990};991do_mult(si.capacity, m_svfs.f_blocks);992do_mult(si.free, m_svfs.f_bfree);993do_mult(si.available, m_svfs.f_bavail);994return si;995}996997file_status __status(const path& p, error_code* ec) { return detail::posix_stat(p, ec); }998999file_status __symlink_status(const path& p, error_code* ec) { return detail::posix_lstat(p, ec); }10001001path __temp_directory_path(error_code* ec) {1002ErrorHandler<path> err("temp_directory_path", ec);10031004#if defined(_LIBCPP_WIN32API)1005wchar_t buf[MAX_PATH];1006DWORD retval = GetTempPathW(MAX_PATH, buf);1007if (!retval)1008return err.report(detail::get_last_error());1009if (retval > MAX_PATH)1010return err.report(errc::filename_too_long);1011// GetTempPathW returns a path with a trailing slash, which we1012// shouldn't include for consistency.1013if (buf[retval - 1] == L'\\')1014buf[retval - 1] = L'\0';1015path p(buf);1016#else1017const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};1018const char* ret = nullptr;10191020for (auto& ep : env_paths)1021if ((ret = getenv(ep)))1022break;1023if (ret == nullptr) {1024# if defined(__ANDROID__)1025ret = "/data/local/tmp";1026# else1027ret = "/tmp";1028# endif1029}10301031path p(ret);1032#endif1033error_code m_ec;1034file_status st = detail::posix_stat(p, &m_ec);1035if (!status_known(st))1036return err.report(m_ec, "cannot access path " PATH_CSTR_FMT, p.c_str());10371038if (!exists(st) || !is_directory(st))1039return err.report(errc::not_a_directory, "path " PATH_CSTR_FMT " is not a directory", p.c_str());10401041return p;1042}10431044path __weakly_canonical(const path& p, error_code* ec) {1045ErrorHandler<path> err("weakly_canonical", ec, &p);10461047if (p.empty())1048return __canonical("", ec);10491050path result;1051path tmp;1052tmp.__reserve(p.native().size());1053auto PP = PathParser::CreateEnd(p.native());1054--PP;1055vector<string_view_t> DNEParts;10561057error_code m_ec;1058while (PP.State_ != PathParser::PS_BeforeBegin) {1059tmp.assign(createView(p.native().data(), &PP.RawEntry.back()));1060file_status st = __status(tmp, &m_ec);1061if (!status_known(st)) {1062return err.report(m_ec);1063} else if (exists(st)) {1064result = __canonical(tmp, &m_ec);1065if (m_ec) {1066return err.report(m_ec);1067}1068break;1069}1070DNEParts.push_back(*PP);1071--PP;1072}1073if (PP.State_ == PathParser::PS_BeforeBegin) {1074result = __canonical("", &m_ec);1075if (m_ec) {1076return err.report(m_ec);1077}1078}1079if (DNEParts.empty())1080return result;1081for (auto It = DNEParts.rbegin(); It != DNEParts.rend(); ++It)1082result /= *It;1083return result.lexically_normal();1084}10851086_LIBCPP_END_NAMESPACE_FILESYSTEM108710881089