14ba319b5SDimitry Andric //===--------------------- filesystem/ops.cpp -----------------------------===//
24ba319b5SDimitry Andric //
34ba319b5SDimitry Andric // The LLVM Compiler Infrastructure
44ba319b5SDimitry Andric //
54ba319b5SDimitry Andric // This file is dual licensed under the MIT and the University of Illinois Open
64ba319b5SDimitry Andric // Source Licenses. See LICENSE.TXT for details.
74ba319b5SDimitry Andric //
84ba319b5SDimitry Andric //===----------------------------------------------------------------------===//
94ba319b5SDimitry Andric
104ba319b5SDimitry Andric #include "filesystem"
114ba319b5SDimitry Andric #include "array"
124ba319b5SDimitry Andric #include "iterator"
134ba319b5SDimitry Andric #include "fstream"
144ba319b5SDimitry Andric #include "random" /* for unique_path */
154ba319b5SDimitry Andric #include "string_view"
164ba319b5SDimitry Andric #include "type_traits"
174ba319b5SDimitry Andric #include "vector"
184ba319b5SDimitry Andric #include "cstdlib"
194ba319b5SDimitry Andric #include "climits"
204ba319b5SDimitry Andric
214ba319b5SDimitry Andric #include "filesystem_common.h"
224ba319b5SDimitry Andric
234ba319b5SDimitry Andric #include <unistd.h>
244ba319b5SDimitry Andric #include <sys/stat.h>
254ba319b5SDimitry Andric #include <sys/statvfs.h>
264ba319b5SDimitry Andric #include <time.h>
274ba319b5SDimitry Andric #include <fcntl.h> /* values for fchmodat */
284ba319b5SDimitry Andric
294ba319b5SDimitry Andric #if defined(__linux__)
304ba319b5SDimitry Andric #include <linux/version.h>
314ba319b5SDimitry Andric #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)
324ba319b5SDimitry Andric #include <sys/sendfile.h>
334ba319b5SDimitry Andric #define _LIBCPP_USE_SENDFILE
344ba319b5SDimitry Andric #endif
354ba319b5SDimitry Andric #elif defined(__APPLE__) || __has_include(<copyfile.h>)
364ba319b5SDimitry Andric #include <copyfile.h>
374ba319b5SDimitry Andric #define _LIBCPP_USE_COPYFILE
384ba319b5SDimitry Andric #endif
394ba319b5SDimitry Andric
404ba319b5SDimitry Andric #if !defined(__APPLE__)
414ba319b5SDimitry Andric #define _LIBCPP_USE_CLOCK_GETTIME
424ba319b5SDimitry Andric #endif
434ba319b5SDimitry Andric
444ba319b5SDimitry Andric #if !defined(CLOCK_REALTIME) || !defined(_LIBCPP_USE_CLOCK_GETTIME)
454ba319b5SDimitry Andric #include <sys/time.h> // for gettimeofday and timeval
464ba319b5SDimitry Andric #endif // !defined(CLOCK_REALTIME)
474ba319b5SDimitry Andric
484ba319b5SDimitry Andric #if defined(_LIBCPP_COMPILER_GCC)
494ba319b5SDimitry Andric #if _GNUC_VER < 500
504ba319b5SDimitry Andric #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
514ba319b5SDimitry Andric #endif
524ba319b5SDimitry Andric #endif
534ba319b5SDimitry Andric
544ba319b5SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
554ba319b5SDimitry Andric
564ba319b5SDimitry Andric namespace {
574ba319b5SDimitry Andric namespace parser {
584ba319b5SDimitry Andric
594ba319b5SDimitry Andric using string_view_t = path::__string_view;
604ba319b5SDimitry Andric using string_view_pair = pair<string_view_t, string_view_t>;
614ba319b5SDimitry Andric using PosPtr = path::value_type const*;
624ba319b5SDimitry Andric
634ba319b5SDimitry Andric struct PathParser {
644ba319b5SDimitry Andric enum ParserState : unsigned char {
654ba319b5SDimitry Andric // Zero is a special sentinel value used by default constructed iterators.
664ba319b5SDimitry Andric PS_BeforeBegin = path::iterator::_BeforeBegin,
674ba319b5SDimitry Andric PS_InRootName = path::iterator::_InRootName,
684ba319b5SDimitry Andric PS_InRootDir = path::iterator::_InRootDir,
694ba319b5SDimitry Andric PS_InFilenames = path::iterator::_InFilenames,
704ba319b5SDimitry Andric PS_InTrailingSep = path::iterator::_InTrailingSep,
714ba319b5SDimitry Andric PS_AtEnd = path::iterator::_AtEnd
724ba319b5SDimitry Andric };
734ba319b5SDimitry Andric
744ba319b5SDimitry Andric const string_view_t Path;
754ba319b5SDimitry Andric string_view_t RawEntry;
764ba319b5SDimitry Andric ParserState State;
774ba319b5SDimitry Andric
784ba319b5SDimitry Andric private:
PathParser__anon649d79130111::parser::PathParser794ba319b5SDimitry Andric PathParser(string_view_t P, ParserState State) noexcept : Path(P),
804ba319b5SDimitry Andric State(State) {}
814ba319b5SDimitry Andric
824ba319b5SDimitry Andric public:
PathParser__anon649d79130111::parser::PathParser834ba319b5SDimitry Andric PathParser(string_view_t P, string_view_t E, unsigned char S)
844ba319b5SDimitry Andric : Path(P), RawEntry(E), State(static_cast<ParserState>(S)) {
854ba319b5SDimitry Andric // S cannot be '0' or PS_BeforeBegin.
864ba319b5SDimitry Andric }
874ba319b5SDimitry Andric
CreateBegin__anon649d79130111::parser::PathParser884ba319b5SDimitry Andric static PathParser CreateBegin(string_view_t P) noexcept {
894ba319b5SDimitry Andric PathParser PP(P, PS_BeforeBegin);
904ba319b5SDimitry Andric PP.increment();
914ba319b5SDimitry Andric return PP;
924ba319b5SDimitry Andric }
934ba319b5SDimitry Andric
CreateEnd__anon649d79130111::parser::PathParser944ba319b5SDimitry Andric static PathParser CreateEnd(string_view_t P) noexcept {
954ba319b5SDimitry Andric PathParser PP(P, PS_AtEnd);
964ba319b5SDimitry Andric return PP;
974ba319b5SDimitry Andric }
984ba319b5SDimitry Andric
peek__anon649d79130111::parser::PathParser994ba319b5SDimitry Andric PosPtr peek() const noexcept {
1004ba319b5SDimitry Andric auto TkEnd = getNextTokenStartPos();
1014ba319b5SDimitry Andric auto End = getAfterBack();
1024ba319b5SDimitry Andric return TkEnd == End ? nullptr : TkEnd;
1034ba319b5SDimitry Andric }
1044ba319b5SDimitry Andric
increment__anon649d79130111::parser::PathParser1054ba319b5SDimitry Andric void increment() noexcept {
1064ba319b5SDimitry Andric const PosPtr End = getAfterBack();
1074ba319b5SDimitry Andric const PosPtr Start = getNextTokenStartPos();
1084ba319b5SDimitry Andric if (Start == End)
1094ba319b5SDimitry Andric return makeState(PS_AtEnd);
1104ba319b5SDimitry Andric
1114ba319b5SDimitry Andric switch (State) {
1124ba319b5SDimitry Andric case PS_BeforeBegin: {
1134ba319b5SDimitry Andric PosPtr TkEnd = consumeSeparator(Start, End);
1144ba319b5SDimitry Andric if (TkEnd)
1154ba319b5SDimitry Andric return makeState(PS_InRootDir, Start, TkEnd);
1164ba319b5SDimitry Andric else
1174ba319b5SDimitry Andric return makeState(PS_InFilenames, Start, consumeName(Start, End));
1184ba319b5SDimitry Andric }
1194ba319b5SDimitry Andric case PS_InRootDir:
1204ba319b5SDimitry Andric return makeState(PS_InFilenames, Start, consumeName(Start, End));
1214ba319b5SDimitry Andric
1224ba319b5SDimitry Andric case PS_InFilenames: {
1234ba319b5SDimitry Andric PosPtr SepEnd = consumeSeparator(Start, End);
1244ba319b5SDimitry Andric if (SepEnd != End) {
1254ba319b5SDimitry Andric PosPtr TkEnd = consumeName(SepEnd, End);
1264ba319b5SDimitry Andric if (TkEnd)
1274ba319b5SDimitry Andric return makeState(PS_InFilenames, SepEnd, TkEnd);
1284ba319b5SDimitry Andric }
1294ba319b5SDimitry Andric return makeState(PS_InTrailingSep, Start, SepEnd);
1304ba319b5SDimitry Andric }
1314ba319b5SDimitry Andric
1324ba319b5SDimitry Andric case PS_InTrailingSep:
1334ba319b5SDimitry Andric return makeState(PS_AtEnd);
1344ba319b5SDimitry Andric
1354ba319b5SDimitry Andric case PS_InRootName:
1364ba319b5SDimitry Andric case PS_AtEnd:
1374ba319b5SDimitry Andric _LIBCPP_UNREACHABLE();
1384ba319b5SDimitry Andric }
1394ba319b5SDimitry Andric }
1404ba319b5SDimitry Andric
decrement__anon649d79130111::parser::PathParser1414ba319b5SDimitry Andric void decrement() noexcept {
1424ba319b5SDimitry Andric const PosPtr REnd = getBeforeFront();
1434ba319b5SDimitry Andric const PosPtr RStart = getCurrentTokenStartPos() - 1;
1444ba319b5SDimitry Andric if (RStart == REnd) // we're decrementing the begin
1454ba319b5SDimitry Andric return makeState(PS_BeforeBegin);
1464ba319b5SDimitry Andric
1474ba319b5SDimitry Andric switch (State) {
1484ba319b5SDimitry Andric case PS_AtEnd: {
1494ba319b5SDimitry Andric // Try to consume a trailing separator or root directory first.
1504ba319b5SDimitry Andric if (PosPtr SepEnd = consumeSeparator(RStart, REnd)) {
1514ba319b5SDimitry Andric if (SepEnd == REnd)
1524ba319b5SDimitry Andric return makeState(PS_InRootDir, Path.data(), RStart + 1);
1534ba319b5SDimitry Andric return makeState(PS_InTrailingSep, SepEnd + 1, RStart + 1);
1544ba319b5SDimitry Andric } else {
1554ba319b5SDimitry Andric PosPtr TkStart = consumeName(RStart, REnd);
1564ba319b5SDimitry Andric return makeState(PS_InFilenames, TkStart + 1, RStart + 1);
1574ba319b5SDimitry Andric }
1584ba319b5SDimitry Andric }
1594ba319b5SDimitry Andric case PS_InTrailingSep:
1604ba319b5SDimitry Andric return makeState(PS_InFilenames, consumeName(RStart, REnd) + 1,
1614ba319b5SDimitry Andric RStart + 1);
1624ba319b5SDimitry Andric case PS_InFilenames: {
1634ba319b5SDimitry Andric PosPtr SepEnd = consumeSeparator(RStart, REnd);
1644ba319b5SDimitry Andric if (SepEnd == REnd)
1654ba319b5SDimitry Andric return makeState(PS_InRootDir, Path.data(), RStart + 1);
1664ba319b5SDimitry Andric PosPtr TkEnd = consumeName(SepEnd, REnd);
1674ba319b5SDimitry Andric return makeState(PS_InFilenames, TkEnd + 1, SepEnd + 1);
1684ba319b5SDimitry Andric }
1694ba319b5SDimitry Andric case PS_InRootDir:
1704ba319b5SDimitry Andric // return makeState(PS_InRootName, Path.data(), RStart + 1);
1714ba319b5SDimitry Andric case PS_InRootName:
1724ba319b5SDimitry Andric case PS_BeforeBegin:
1734ba319b5SDimitry Andric _LIBCPP_UNREACHABLE();
1744ba319b5SDimitry Andric }
1754ba319b5SDimitry Andric }
1764ba319b5SDimitry Andric
1774ba319b5SDimitry Andric /// \brief Return a view with the "preferred representation" of the current
1784ba319b5SDimitry Andric /// element. For example trailing separators are represented as a '.'
operator *__anon649d79130111::parser::PathParser1794ba319b5SDimitry Andric string_view_t operator*() const noexcept {
1804ba319b5SDimitry Andric switch (State) {
1814ba319b5SDimitry Andric case PS_BeforeBegin:
1824ba319b5SDimitry Andric case PS_AtEnd:
1834ba319b5SDimitry Andric return "";
1844ba319b5SDimitry Andric case PS_InRootDir:
1854ba319b5SDimitry Andric return "/";
1864ba319b5SDimitry Andric case PS_InTrailingSep:
1874ba319b5SDimitry Andric return "";
1884ba319b5SDimitry Andric case PS_InRootName:
1894ba319b5SDimitry Andric case PS_InFilenames:
1904ba319b5SDimitry Andric return RawEntry;
1914ba319b5SDimitry Andric }
1924ba319b5SDimitry Andric _LIBCPP_UNREACHABLE();
1934ba319b5SDimitry Andric }
1944ba319b5SDimitry Andric
operator bool__anon649d79130111::parser::PathParser1954ba319b5SDimitry Andric explicit operator bool() const noexcept {
1964ba319b5SDimitry Andric return State != PS_BeforeBegin && State != PS_AtEnd;
1974ba319b5SDimitry Andric }
1984ba319b5SDimitry Andric
operator ++__anon649d79130111::parser::PathParser1994ba319b5SDimitry Andric PathParser& operator++() noexcept {
2004ba319b5SDimitry Andric increment();
2014ba319b5SDimitry Andric return *this;
2024ba319b5SDimitry Andric }
2034ba319b5SDimitry Andric
operator --__anon649d79130111::parser::PathParser2044ba319b5SDimitry Andric PathParser& operator--() noexcept {
2054ba319b5SDimitry Andric decrement();
2064ba319b5SDimitry Andric return *this;
2074ba319b5SDimitry Andric }
2084ba319b5SDimitry Andric
atEnd__anon649d79130111::parser::PathParser209*b5893f02SDimitry Andric bool atEnd() const noexcept {
210*b5893f02SDimitry Andric return State == PS_AtEnd;
211*b5893f02SDimitry Andric }
212*b5893f02SDimitry Andric
inRootDir__anon649d79130111::parser::PathParser213*b5893f02SDimitry Andric bool inRootDir() const noexcept {
214*b5893f02SDimitry Andric return State == PS_InRootDir;
215*b5893f02SDimitry Andric }
216*b5893f02SDimitry Andric
inRootName__anon649d79130111::parser::PathParser217*b5893f02SDimitry Andric bool inRootName() const noexcept {
218*b5893f02SDimitry Andric return State == PS_InRootName;
219*b5893f02SDimitry Andric }
220*b5893f02SDimitry Andric
inRootPath__anon649d79130111::parser::PathParser2214ba319b5SDimitry Andric bool inRootPath() const noexcept {
222*b5893f02SDimitry Andric return inRootName() || inRootDir();
2234ba319b5SDimitry Andric }
2244ba319b5SDimitry Andric
2254ba319b5SDimitry Andric private:
makeState__anon649d79130111::parser::PathParser2264ba319b5SDimitry Andric void makeState(ParserState NewState, PosPtr Start, PosPtr End) noexcept {
2274ba319b5SDimitry Andric State = NewState;
2284ba319b5SDimitry Andric RawEntry = string_view_t(Start, End - Start);
2294ba319b5SDimitry Andric }
makeState__anon649d79130111::parser::PathParser2304ba319b5SDimitry Andric void makeState(ParserState NewState) noexcept {
2314ba319b5SDimitry Andric State = NewState;
2324ba319b5SDimitry Andric RawEntry = {};
2334ba319b5SDimitry Andric }
2344ba319b5SDimitry Andric
getAfterBack__anon649d79130111::parser::PathParser2354ba319b5SDimitry Andric PosPtr getAfterBack() const noexcept { return Path.data() + Path.size(); }
2364ba319b5SDimitry Andric
getBeforeFront__anon649d79130111::parser::PathParser2374ba319b5SDimitry Andric PosPtr getBeforeFront() const noexcept { return Path.data() - 1; }
2384ba319b5SDimitry Andric
2394ba319b5SDimitry Andric /// \brief Return a pointer to the first character after the currently
2404ba319b5SDimitry Andric /// lexed element.
getNextTokenStartPos__anon649d79130111::parser::PathParser2414ba319b5SDimitry Andric PosPtr getNextTokenStartPos() const noexcept {
2424ba319b5SDimitry Andric switch (State) {
2434ba319b5SDimitry Andric case PS_BeforeBegin:
2444ba319b5SDimitry Andric return Path.data();
2454ba319b5SDimitry Andric case PS_InRootName:
2464ba319b5SDimitry Andric case PS_InRootDir:
2474ba319b5SDimitry Andric case PS_InFilenames:
2484ba319b5SDimitry Andric return &RawEntry.back() + 1;
2494ba319b5SDimitry Andric case PS_InTrailingSep:
2504ba319b5SDimitry Andric case PS_AtEnd:
2514ba319b5SDimitry Andric return getAfterBack();
2524ba319b5SDimitry Andric }
2534ba319b5SDimitry Andric _LIBCPP_UNREACHABLE();
2544ba319b5SDimitry Andric }
2554ba319b5SDimitry Andric
2564ba319b5SDimitry Andric /// \brief Return a pointer to the first character in the currently lexed
2574ba319b5SDimitry Andric /// element.
getCurrentTokenStartPos__anon649d79130111::parser::PathParser2584ba319b5SDimitry Andric PosPtr getCurrentTokenStartPos() const noexcept {
2594ba319b5SDimitry Andric switch (State) {
2604ba319b5SDimitry Andric case PS_BeforeBegin:
2614ba319b5SDimitry Andric case PS_InRootName:
2624ba319b5SDimitry Andric return &Path.front();
2634ba319b5SDimitry Andric case PS_InRootDir:
2644ba319b5SDimitry Andric case PS_InFilenames:
2654ba319b5SDimitry Andric case PS_InTrailingSep:
2664ba319b5SDimitry Andric return &RawEntry.front();
2674ba319b5SDimitry Andric case PS_AtEnd:
2684ba319b5SDimitry Andric return &Path.back() + 1;
2694ba319b5SDimitry Andric }
2704ba319b5SDimitry Andric _LIBCPP_UNREACHABLE();
2714ba319b5SDimitry Andric }
2724ba319b5SDimitry Andric
consumeSeparator__anon649d79130111::parser::PathParser2734ba319b5SDimitry Andric PosPtr consumeSeparator(PosPtr P, PosPtr End) const noexcept {
2744ba319b5SDimitry Andric if (P == End || *P != '/')
2754ba319b5SDimitry Andric return nullptr;
2764ba319b5SDimitry Andric const int Inc = P < End ? 1 : -1;
2774ba319b5SDimitry Andric P += Inc;
2784ba319b5SDimitry Andric while (P != End && *P == '/')
2794ba319b5SDimitry Andric P += Inc;
2804ba319b5SDimitry Andric return P;
2814ba319b5SDimitry Andric }
2824ba319b5SDimitry Andric
consumeName__anon649d79130111::parser::PathParser2834ba319b5SDimitry Andric PosPtr consumeName(PosPtr P, PosPtr End) const noexcept {
2844ba319b5SDimitry Andric if (P == End || *P == '/')
2854ba319b5SDimitry Andric return nullptr;
2864ba319b5SDimitry Andric const int Inc = P < End ? 1 : -1;
2874ba319b5SDimitry Andric P += Inc;
2884ba319b5SDimitry Andric while (P != End && *P != '/')
2894ba319b5SDimitry Andric P += Inc;
2904ba319b5SDimitry Andric return P;
2914ba319b5SDimitry Andric }
2924ba319b5SDimitry Andric };
2934ba319b5SDimitry Andric
separate_filename(string_view_t const & s)2944ba319b5SDimitry Andric string_view_pair separate_filename(string_view_t const& s) {
2954ba319b5SDimitry Andric if (s == "." || s == ".." || s.empty())
2964ba319b5SDimitry Andric return string_view_pair{s, ""};
2974ba319b5SDimitry Andric auto pos = s.find_last_of('.');
2984ba319b5SDimitry Andric if (pos == string_view_t::npos || pos == 0)
2994ba319b5SDimitry Andric return string_view_pair{s, string_view_t{}};
3004ba319b5SDimitry Andric return string_view_pair{s.substr(0, pos), s.substr(pos)};
3014ba319b5SDimitry Andric }
3024ba319b5SDimitry Andric
createView(PosPtr S,PosPtr E)3034ba319b5SDimitry Andric string_view_t createView(PosPtr S, PosPtr E) noexcept {
3044ba319b5SDimitry Andric return {S, static_cast<size_t>(E - S) + 1};
3054ba319b5SDimitry Andric }
3064ba319b5SDimitry Andric
3074ba319b5SDimitry Andric } // namespace parser
3084ba319b5SDimitry Andric } // namespace
3094ba319b5SDimitry Andric
3104ba319b5SDimitry Andric // POSIX HELPERS
3114ba319b5SDimitry Andric
3124ba319b5SDimitry Andric namespace detail {
3134ba319b5SDimitry Andric namespace {
3144ba319b5SDimitry Andric
3154ba319b5SDimitry Andric using value_type = path::value_type;
3164ba319b5SDimitry Andric using string_type = path::string_type;
3174ba319b5SDimitry Andric
3184ba319b5SDimitry Andric struct FileDescriptor {
3194ba319b5SDimitry Andric const path& name;
3204ba319b5SDimitry Andric int fd = -1;
3214ba319b5SDimitry Andric StatT m_stat;
3224ba319b5SDimitry Andric file_status m_status;
3234ba319b5SDimitry Andric
3244ba319b5SDimitry Andric template <class... Args>
createdetail::__anon649d79130211::FileDescriptor3254ba319b5SDimitry Andric static FileDescriptor create(const path* p, error_code& ec, Args... args) {
3264ba319b5SDimitry Andric ec.clear();
3274ba319b5SDimitry Andric int fd;
3284ba319b5SDimitry Andric if ((fd = ::open(p->c_str(), args...)) == -1) {
3294ba319b5SDimitry Andric ec = capture_errno();
3304ba319b5SDimitry Andric return FileDescriptor{p};
3314ba319b5SDimitry Andric }
3324ba319b5SDimitry Andric return FileDescriptor(p, fd);
3334ba319b5SDimitry Andric }
3344ba319b5SDimitry Andric
3354ba319b5SDimitry Andric template <class... Args>
create_with_statusdetail::__anon649d79130211::FileDescriptor3364ba319b5SDimitry Andric static FileDescriptor create_with_status(const path* p, error_code& ec,
3374ba319b5SDimitry Andric Args... args) {
3384ba319b5SDimitry Andric FileDescriptor fd = create(p, ec, args...);
3394ba319b5SDimitry Andric if (!ec)
3404ba319b5SDimitry Andric fd.refresh_status(ec);
3414ba319b5SDimitry Andric
3424ba319b5SDimitry Andric return fd;
3434ba319b5SDimitry Andric }
3444ba319b5SDimitry Andric
get_statusdetail::__anon649d79130211::FileDescriptor3454ba319b5SDimitry Andric file_status get_status() const { return m_status; }
get_statdetail::__anon649d79130211::FileDescriptor3464ba319b5SDimitry Andric StatT const& get_stat() const { return m_stat; }
3474ba319b5SDimitry Andric
status_knowndetail::__anon649d79130211::FileDescriptor3484ba319b5SDimitry Andric bool status_known() const { return _VSTD_FS::status_known(m_status); }
3494ba319b5SDimitry Andric
3504ba319b5SDimitry Andric file_status refresh_status(error_code& ec);
3514ba319b5SDimitry Andric
closedetail::__anon649d79130211::FileDescriptor3524ba319b5SDimitry Andric void close() noexcept {
3534ba319b5SDimitry Andric if (fd != -1)
3544ba319b5SDimitry Andric ::close(fd);
3554ba319b5SDimitry Andric fd = -1;
3564ba319b5SDimitry Andric }
3574ba319b5SDimitry Andric
FileDescriptordetail::__anon649d79130211::FileDescriptor3584ba319b5SDimitry Andric FileDescriptor(FileDescriptor&& other)
3594ba319b5SDimitry Andric : name(other.name), fd(other.fd), m_stat(other.m_stat),
3604ba319b5SDimitry Andric m_status(other.m_status) {
3614ba319b5SDimitry Andric other.fd = -1;
3624ba319b5SDimitry Andric other.m_status = file_status{};
3634ba319b5SDimitry Andric }
3644ba319b5SDimitry Andric
~FileDescriptordetail::__anon649d79130211::FileDescriptor3654ba319b5SDimitry Andric ~FileDescriptor() { close(); }
3664ba319b5SDimitry Andric
3674ba319b5SDimitry Andric FileDescriptor(FileDescriptor const&) = delete;
3684ba319b5SDimitry Andric FileDescriptor& operator=(FileDescriptor const&) = delete;
3694ba319b5SDimitry Andric
3704ba319b5SDimitry Andric private:
FileDescriptordetail::__anon649d79130211::FileDescriptor3714ba319b5SDimitry Andric explicit FileDescriptor(const path* p, int fd = -1) : name(*p), fd(fd) {}
3724ba319b5SDimitry Andric };
3734ba319b5SDimitry Andric
posix_get_perms(const StatT & st)3744ba319b5SDimitry Andric perms posix_get_perms(const StatT& st) noexcept {
3754ba319b5SDimitry Andric return static_cast<perms>(st.st_mode) & perms::mask;
3764ba319b5SDimitry Andric }
3774ba319b5SDimitry Andric
posix_convert_perms(perms prms)3784ba319b5SDimitry Andric ::mode_t posix_convert_perms(perms prms) {
3794ba319b5SDimitry Andric return static_cast< ::mode_t>(prms & perms::mask);
3804ba319b5SDimitry Andric }
3814ba319b5SDimitry Andric
create_file_status(error_code & m_ec,path const & p,const StatT & path_stat,error_code * ec)3824ba319b5SDimitry Andric file_status create_file_status(error_code& m_ec, path const& p,
3834ba319b5SDimitry Andric const StatT& path_stat, error_code* ec) {
3844ba319b5SDimitry Andric if (ec)
3854ba319b5SDimitry Andric *ec = m_ec;
3864ba319b5SDimitry Andric if (m_ec && (m_ec.value() == ENOENT || m_ec.value() == ENOTDIR)) {
3874ba319b5SDimitry Andric return file_status(file_type::not_found);
3884ba319b5SDimitry Andric } else if (m_ec) {
3894ba319b5SDimitry Andric ErrorHandler<void> err("posix_stat", ec, &p);
3904ba319b5SDimitry Andric err.report(m_ec, "failed to determine attributes for the specified path");
3914ba319b5SDimitry Andric return file_status(file_type::none);
3924ba319b5SDimitry Andric }
3934ba319b5SDimitry Andric // else
3944ba319b5SDimitry Andric
3954ba319b5SDimitry Andric file_status fs_tmp;
3964ba319b5SDimitry Andric auto const mode = path_stat.st_mode;
3974ba319b5SDimitry Andric if (S_ISLNK(mode))
3984ba319b5SDimitry Andric fs_tmp.type(file_type::symlink);
3994ba319b5SDimitry Andric else if (S_ISREG(mode))
4004ba319b5SDimitry Andric fs_tmp.type(file_type::regular);
4014ba319b5SDimitry Andric else if (S_ISDIR(mode))
4024ba319b5SDimitry Andric fs_tmp.type(file_type::directory);
4034ba319b5SDimitry Andric else if (S_ISBLK(mode))
4044ba319b5SDimitry Andric fs_tmp.type(file_type::block);
4054ba319b5SDimitry Andric else if (S_ISCHR(mode))
4064ba319b5SDimitry Andric fs_tmp.type(file_type::character);
4074ba319b5SDimitry Andric else if (S_ISFIFO(mode))
4084ba319b5SDimitry Andric fs_tmp.type(file_type::fifo);
4094ba319b5SDimitry Andric else if (S_ISSOCK(mode))
4104ba319b5SDimitry Andric fs_tmp.type(file_type::socket);
4114ba319b5SDimitry Andric else
4124ba319b5SDimitry Andric fs_tmp.type(file_type::unknown);
4134ba319b5SDimitry Andric
4144ba319b5SDimitry Andric fs_tmp.permissions(detail::posix_get_perms(path_stat));
4154ba319b5SDimitry Andric return fs_tmp;
4164ba319b5SDimitry Andric }
4174ba319b5SDimitry Andric
posix_stat(path const & p,StatT & path_stat,error_code * ec)4184ba319b5SDimitry Andric file_status posix_stat(path const& p, StatT& path_stat, error_code* ec) {
4194ba319b5SDimitry Andric error_code m_ec;
4204ba319b5SDimitry Andric if (::stat(p.c_str(), &path_stat) == -1)
4214ba319b5SDimitry Andric m_ec = detail::capture_errno();
4224ba319b5SDimitry Andric return create_file_status(m_ec, p, path_stat, ec);
4234ba319b5SDimitry Andric }
4244ba319b5SDimitry Andric
posix_stat(path const & p,error_code * ec)4254ba319b5SDimitry Andric file_status posix_stat(path const& p, error_code* ec) {
4264ba319b5SDimitry Andric StatT path_stat;
4274ba319b5SDimitry Andric return posix_stat(p, path_stat, ec);
4284ba319b5SDimitry Andric }
4294ba319b5SDimitry Andric
posix_lstat(path const & p,StatT & path_stat,error_code * ec)4304ba319b5SDimitry Andric file_status posix_lstat(path const& p, StatT& path_stat, error_code* ec) {
4314ba319b5SDimitry Andric error_code m_ec;
4324ba319b5SDimitry Andric if (::lstat(p.c_str(), &path_stat) == -1)
4334ba319b5SDimitry Andric m_ec = detail::capture_errno();
4344ba319b5SDimitry Andric return create_file_status(m_ec, p, path_stat, ec);
4354ba319b5SDimitry Andric }
4364ba319b5SDimitry Andric
posix_lstat(path const & p,error_code * ec)4374ba319b5SDimitry Andric file_status posix_lstat(path const& p, error_code* ec) {
4384ba319b5SDimitry Andric StatT path_stat;
4394ba319b5SDimitry Andric return posix_lstat(p, path_stat, ec);
4404ba319b5SDimitry Andric }
4414ba319b5SDimitry Andric
442*b5893f02SDimitry Andric // http://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html
posix_ftruncate(const FileDescriptor & fd,off_t to_size,error_code & ec)443*b5893f02SDimitry Andric bool posix_ftruncate(const FileDescriptor& fd, off_t to_size, error_code& ec) {
4444ba319b5SDimitry Andric if (::ftruncate(fd.fd, to_size) == -1) {
4454ba319b5SDimitry Andric ec = capture_errno();
4464ba319b5SDimitry Andric return true;
4474ba319b5SDimitry Andric }
4484ba319b5SDimitry Andric ec.clear();
4494ba319b5SDimitry Andric return false;
4504ba319b5SDimitry Andric }
4514ba319b5SDimitry Andric
posix_fchmod(const FileDescriptor & fd,const StatT & st,error_code & ec)4524ba319b5SDimitry Andric bool posix_fchmod(const FileDescriptor& fd, const StatT& st, error_code& ec) {
4534ba319b5SDimitry Andric if (::fchmod(fd.fd, st.st_mode) == -1) {
4544ba319b5SDimitry Andric ec = capture_errno();
4554ba319b5SDimitry Andric return true;
4564ba319b5SDimitry Andric }
4574ba319b5SDimitry Andric ec.clear();
4584ba319b5SDimitry Andric return false;
4594ba319b5SDimitry Andric }
4604ba319b5SDimitry Andric
stat_equivalent(const StatT & st1,const StatT & st2)4614ba319b5SDimitry Andric bool stat_equivalent(const StatT& st1, const StatT& st2) {
4624ba319b5SDimitry Andric return (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
4634ba319b5SDimitry Andric }
4644ba319b5SDimitry Andric
refresh_status(error_code & ec)4654ba319b5SDimitry Andric file_status FileDescriptor::refresh_status(error_code& ec) {
4664ba319b5SDimitry Andric // FD must be open and good.
4674ba319b5SDimitry Andric m_status = file_status{};
4684ba319b5SDimitry Andric m_stat = {};
4694ba319b5SDimitry Andric error_code m_ec;
4704ba319b5SDimitry Andric if (::fstat(fd, &m_stat) == -1)
4714ba319b5SDimitry Andric m_ec = capture_errno();
4724ba319b5SDimitry Andric m_status = create_file_status(m_ec, name, m_stat, &ec);
4734ba319b5SDimitry Andric return m_status;
4744ba319b5SDimitry Andric }
4754ba319b5SDimitry Andric } // namespace
4764ba319b5SDimitry Andric } // end namespace detail
4774ba319b5SDimitry Andric
4784ba319b5SDimitry Andric using detail::capture_errno;
4794ba319b5SDimitry Andric using detail::ErrorHandler;
4804ba319b5SDimitry Andric using detail::StatT;
4814ba319b5SDimitry Andric using detail::TimeSpec;
4824ba319b5SDimitry Andric using parser::createView;
4834ba319b5SDimitry Andric using parser::PathParser;
4844ba319b5SDimitry Andric using parser::string_view_t;
4854ba319b5SDimitry Andric
4864ba319b5SDimitry Andric const bool _FilesystemClock::is_steady;
4874ba319b5SDimitry Andric
now()4884ba319b5SDimitry Andric _FilesystemClock::time_point _FilesystemClock::now() noexcept {
4894ba319b5SDimitry Andric typedef chrono::duration<rep> __secs;
4904ba319b5SDimitry Andric #if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
4914ba319b5SDimitry Andric typedef chrono::duration<rep, nano> __nsecs;
4924ba319b5SDimitry Andric struct timespec tp;
4934ba319b5SDimitry Andric if (0 != clock_gettime(CLOCK_REALTIME, &tp))
4944ba319b5SDimitry Andric __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
4954ba319b5SDimitry Andric return time_point(__secs(tp.tv_sec) +
4964ba319b5SDimitry Andric chrono::duration_cast<duration>(__nsecs(tp.tv_nsec)));
4974ba319b5SDimitry Andric #else
4984ba319b5SDimitry Andric typedef chrono::duration<rep, micro> __microsecs;
4994ba319b5SDimitry Andric timeval tv;
5004ba319b5SDimitry Andric gettimeofday(&tv, 0);
5014ba319b5SDimitry Andric return time_point(__secs(tv.tv_sec) + __microsecs(tv.tv_usec));
5024ba319b5SDimitry Andric #endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME
5034ba319b5SDimitry Andric }
5044ba319b5SDimitry Andric
~filesystem_error()5054ba319b5SDimitry Andric filesystem_error::~filesystem_error() {}
5064ba319b5SDimitry Andric
__create_what(int __num_paths)5074ba319b5SDimitry Andric void filesystem_error::__create_what(int __num_paths) {
5084ba319b5SDimitry Andric const char* derived_what = system_error::what();
5094ba319b5SDimitry Andric __storage_->__what_ = [&]() -> string {
5104ba319b5SDimitry Andric const char* p1 = path1().native().empty() ? "\"\"" : path1().c_str();
5114ba319b5SDimitry Andric const char* p2 = path2().native().empty() ? "\"\"" : path2().c_str();
5124ba319b5SDimitry Andric switch (__num_paths) {
5134ba319b5SDimitry Andric default:
5144ba319b5SDimitry Andric return detail::format_string("filesystem error: %s", derived_what);
5154ba319b5SDimitry Andric case 1:
5164ba319b5SDimitry Andric return detail::format_string("filesystem error: %s [%s]", derived_what,
5174ba319b5SDimitry Andric p1);
5184ba319b5SDimitry Andric case 2:
5194ba319b5SDimitry Andric return detail::format_string("filesystem error: %s [%s] [%s]",
5204ba319b5SDimitry Andric derived_what, p1, p2);
5214ba319b5SDimitry Andric }
5224ba319b5SDimitry Andric }();
5234ba319b5SDimitry Andric }
5244ba319b5SDimitry Andric
__do_absolute(const path & p,path * cwd,error_code * ec)5254ba319b5SDimitry Andric static path __do_absolute(const path& p, path* cwd, error_code* ec) {
5264ba319b5SDimitry Andric if (ec)
5274ba319b5SDimitry Andric ec->clear();
5284ba319b5SDimitry Andric if (p.is_absolute())
5294ba319b5SDimitry Andric return p;
5304ba319b5SDimitry Andric *cwd = __current_path(ec);
5314ba319b5SDimitry Andric if (ec && *ec)
5324ba319b5SDimitry Andric return {};
5334ba319b5SDimitry Andric return (*cwd) / p;
5344ba319b5SDimitry Andric }
5354ba319b5SDimitry Andric
__absolute(const path & p,error_code * ec)5364ba319b5SDimitry Andric path __absolute(const path& p, error_code* ec) {
5374ba319b5SDimitry Andric path cwd;
5384ba319b5SDimitry Andric return __do_absolute(p, &cwd, ec);
5394ba319b5SDimitry Andric }
5404ba319b5SDimitry Andric
__canonical(path const & orig_p,error_code * ec)5414ba319b5SDimitry Andric path __canonical(path const& orig_p, error_code* ec) {
5424ba319b5SDimitry Andric path cwd;
5434ba319b5SDimitry Andric ErrorHandler<path> err("canonical", ec, &orig_p, &cwd);
5444ba319b5SDimitry Andric
5454ba319b5SDimitry Andric path p = __do_absolute(orig_p, &cwd, ec);
5464ba319b5SDimitry Andric char buff[PATH_MAX + 1];
5474ba319b5SDimitry Andric char* ret;
5484ba319b5SDimitry Andric if ((ret = ::realpath(p.c_str(), buff)) == nullptr)
5494ba319b5SDimitry Andric return err.report(capture_errno());
5504ba319b5SDimitry Andric return {ret};
5514ba319b5SDimitry Andric }
5524ba319b5SDimitry Andric
__copy(const path & from,const path & to,copy_options options,error_code * ec)5534ba319b5SDimitry Andric void __copy(const path& from, const path& to, copy_options options,
5544ba319b5SDimitry Andric error_code* ec) {
5554ba319b5SDimitry Andric ErrorHandler<void> err("copy", ec, &from, &to);
5564ba319b5SDimitry Andric
5574ba319b5SDimitry Andric const bool sym_status = bool(
5584ba319b5SDimitry Andric options & (copy_options::create_symlinks | copy_options::skip_symlinks));
5594ba319b5SDimitry Andric
5604ba319b5SDimitry Andric const bool sym_status2 = bool(options & copy_options::copy_symlinks);
5614ba319b5SDimitry Andric
5624ba319b5SDimitry Andric error_code m_ec1;
5634ba319b5SDimitry Andric StatT f_st = {};
5644ba319b5SDimitry Andric const file_status f = sym_status || sym_status2
5654ba319b5SDimitry Andric ? detail::posix_lstat(from, f_st, &m_ec1)
5664ba319b5SDimitry Andric : detail::posix_stat(from, f_st, &m_ec1);
5674ba319b5SDimitry Andric if (m_ec1)
5684ba319b5SDimitry Andric return err.report(m_ec1);
5694ba319b5SDimitry Andric
5704ba319b5SDimitry Andric StatT t_st = {};
5714ba319b5SDimitry Andric const file_status t = sym_status ? detail::posix_lstat(to, t_st, &m_ec1)
5724ba319b5SDimitry Andric : detail::posix_stat(to, t_st, &m_ec1);
5734ba319b5SDimitry Andric
5744ba319b5SDimitry Andric if (not status_known(t))
5754ba319b5SDimitry Andric return err.report(m_ec1);
5764ba319b5SDimitry Andric
5774ba319b5SDimitry Andric if (!exists(f) || is_other(f) || is_other(t) ||
5784ba319b5SDimitry Andric (is_directory(f) && is_regular_file(t)) ||
5794ba319b5SDimitry Andric detail::stat_equivalent(f_st, t_st)) {
5804ba319b5SDimitry Andric return err.report(errc::function_not_supported);
5814ba319b5SDimitry Andric }
5824ba319b5SDimitry Andric
5834ba319b5SDimitry Andric if (ec)
5844ba319b5SDimitry Andric ec->clear();
5854ba319b5SDimitry Andric
5864ba319b5SDimitry Andric if (is_symlink(f)) {
5874ba319b5SDimitry Andric if (bool(copy_options::skip_symlinks & options)) {
5884ba319b5SDimitry Andric // do nothing
5894ba319b5SDimitry Andric } else if (not exists(t)) {
5904ba319b5SDimitry Andric __copy_symlink(from, to, ec);
5914ba319b5SDimitry Andric } else {
5924ba319b5SDimitry Andric return err.report(errc::file_exists);
5934ba319b5SDimitry Andric }
5944ba319b5SDimitry Andric return;
5954ba319b5SDimitry Andric } else if (is_regular_file(f)) {
5964ba319b5SDimitry Andric if (bool(copy_options::directories_only & options)) {
5974ba319b5SDimitry Andric // do nothing
5984ba319b5SDimitry Andric } else if (bool(copy_options::create_symlinks & options)) {
5994ba319b5SDimitry Andric __create_symlink(from, to, ec);
6004ba319b5SDimitry Andric } else if (bool(copy_options::create_hard_links & options)) {
6014ba319b5SDimitry Andric __create_hard_link(from, to, ec);
6024ba319b5SDimitry Andric } else if (is_directory(t)) {
6034ba319b5SDimitry Andric __copy_file(from, to / from.filename(), options, ec);
6044ba319b5SDimitry Andric } else {
6054ba319b5SDimitry Andric __copy_file(from, to, options, ec);
6064ba319b5SDimitry Andric }
6074ba319b5SDimitry Andric return;
6084ba319b5SDimitry Andric } else if (is_directory(f) && bool(copy_options::create_symlinks & options)) {
6094ba319b5SDimitry Andric return err.report(errc::is_a_directory);
6104ba319b5SDimitry Andric } else if (is_directory(f) && (bool(copy_options::recursive & options) ||
6114ba319b5SDimitry Andric copy_options::none == options)) {
6124ba319b5SDimitry Andric
6134ba319b5SDimitry Andric if (!exists(t)) {
6144ba319b5SDimitry Andric // create directory to with attributes from 'from'.
6154ba319b5SDimitry Andric __create_directory(to, from, ec);
6164ba319b5SDimitry Andric if (ec && *ec) {
6174ba319b5SDimitry Andric return;
6184ba319b5SDimitry Andric }
6194ba319b5SDimitry Andric }
6204ba319b5SDimitry Andric directory_iterator it =
6214ba319b5SDimitry Andric ec ? directory_iterator(from, *ec) : directory_iterator(from);
6224ba319b5SDimitry Andric if (ec && *ec) {
6234ba319b5SDimitry Andric return;
6244ba319b5SDimitry Andric }
6254ba319b5SDimitry Andric error_code m_ec2;
6264ba319b5SDimitry Andric for (; it != directory_iterator(); it.increment(m_ec2)) {
6274ba319b5SDimitry Andric if (m_ec2) {
6284ba319b5SDimitry Andric return err.report(m_ec2);
6294ba319b5SDimitry Andric }
6304ba319b5SDimitry Andric __copy(it->path(), to / it->path().filename(),
6314ba319b5SDimitry Andric options | copy_options::__in_recursive_copy, ec);
6324ba319b5SDimitry Andric if (ec && *ec) {
6334ba319b5SDimitry Andric return;
6344ba319b5SDimitry Andric }
6354ba319b5SDimitry Andric }
6364ba319b5SDimitry Andric }
6374ba319b5SDimitry Andric }
6384ba319b5SDimitry Andric
6394ba319b5SDimitry Andric namespace detail {
6404ba319b5SDimitry Andric namespace {
6414ba319b5SDimitry Andric
6424ba319b5SDimitry Andric #ifdef _LIBCPP_USE_SENDFILE
copy_file_impl_sendfile(FileDescriptor & read_fd,FileDescriptor & write_fd,error_code & ec)6434ba319b5SDimitry Andric bool copy_file_impl_sendfile(FileDescriptor& read_fd, FileDescriptor& write_fd,
6444ba319b5SDimitry Andric error_code& ec) {
6454ba319b5SDimitry Andric
6464ba319b5SDimitry Andric size_t count = read_fd.get_stat().st_size;
6474ba319b5SDimitry Andric do {
6484ba319b5SDimitry Andric ssize_t res;
6494ba319b5SDimitry Andric if ((res = ::sendfile(write_fd.fd, read_fd.fd, nullptr, count)) == -1) {
6504ba319b5SDimitry Andric ec = capture_errno();
6514ba319b5SDimitry Andric return false;
6524ba319b5SDimitry Andric }
6534ba319b5SDimitry Andric count -= res;
6544ba319b5SDimitry Andric } while (count > 0);
6554ba319b5SDimitry Andric
6564ba319b5SDimitry Andric ec.clear();
6574ba319b5SDimitry Andric
6584ba319b5SDimitry Andric return true;
6594ba319b5SDimitry Andric }
6604ba319b5SDimitry Andric #elif defined(_LIBCPP_USE_COPYFILE)
6614ba319b5SDimitry Andric bool copy_file_impl_copyfile(FileDescriptor& read_fd, FileDescriptor& write_fd,
6624ba319b5SDimitry Andric error_code& ec) {
6634ba319b5SDimitry Andric struct CopyFileState {
6644ba319b5SDimitry Andric copyfile_state_t state;
6654ba319b5SDimitry Andric CopyFileState() { state = copyfile_state_alloc(); }
6664ba319b5SDimitry Andric ~CopyFileState() { copyfile_state_free(state); }
6674ba319b5SDimitry Andric
6684ba319b5SDimitry Andric private:
6694ba319b5SDimitry Andric CopyFileState(CopyFileState const&) = delete;
6704ba319b5SDimitry Andric CopyFileState& operator=(CopyFileState const&) = delete;
6714ba319b5SDimitry Andric };
6724ba319b5SDimitry Andric
6734ba319b5SDimitry Andric CopyFileState cfs;
6744ba319b5SDimitry Andric if (fcopyfile(read_fd.fd, write_fd.fd, cfs.state, COPYFILE_DATA) < 0) {
6754ba319b5SDimitry Andric ec = capture_errno();
6764ba319b5SDimitry Andric return false;
6774ba319b5SDimitry Andric }
6784ba319b5SDimitry Andric
6794ba319b5SDimitry Andric ec.clear();
6804ba319b5SDimitry Andric return true;
6814ba319b5SDimitry Andric }
6824ba319b5SDimitry Andric #endif
6834ba319b5SDimitry Andric
6844ba319b5SDimitry Andric // Note: This function isn't guarded by ifdef's even though it may be unused
6854ba319b5SDimitry Andric // in order to assure it still compiles.
copy_file_impl_default(FileDescriptor & read_fd,FileDescriptor & write_fd,error_code & ec)6864ba319b5SDimitry Andric __attribute__((unused)) bool copy_file_impl_default(FileDescriptor& read_fd,
6874ba319b5SDimitry Andric FileDescriptor& write_fd,
6884ba319b5SDimitry Andric error_code& ec) {
6894ba319b5SDimitry Andric ifstream in;
6904ba319b5SDimitry Andric in.__open(read_fd.fd, ios::binary);
6914ba319b5SDimitry Andric if (!in.is_open()) {
6924ba319b5SDimitry Andric // This assumes that __open didn't reset the error code.
6934ba319b5SDimitry Andric ec = capture_errno();
6944ba319b5SDimitry Andric return false;
6954ba319b5SDimitry Andric }
6964ba319b5SDimitry Andric ofstream out;
6974ba319b5SDimitry Andric out.__open(write_fd.fd, ios::binary);
6984ba319b5SDimitry Andric if (!out.is_open()) {
6994ba319b5SDimitry Andric ec = capture_errno();
7004ba319b5SDimitry Andric return false;
7014ba319b5SDimitry Andric }
7024ba319b5SDimitry Andric
7034ba319b5SDimitry Andric if (in.good() && out.good()) {
7044ba319b5SDimitry Andric using InIt = istreambuf_iterator<char>;
7054ba319b5SDimitry Andric using OutIt = ostreambuf_iterator<char>;
7064ba319b5SDimitry Andric InIt bin(in);
7074ba319b5SDimitry Andric InIt ein;
7084ba319b5SDimitry Andric OutIt bout(out);
7094ba319b5SDimitry Andric copy(bin, ein, bout);
7104ba319b5SDimitry Andric }
7114ba319b5SDimitry Andric if (out.fail() || in.fail()) {
7124ba319b5SDimitry Andric ec = make_error_code(errc::io_error);
7134ba319b5SDimitry Andric return false;
7144ba319b5SDimitry Andric }
7154ba319b5SDimitry Andric
7164ba319b5SDimitry Andric ec.clear();
7174ba319b5SDimitry Andric return true;
7184ba319b5SDimitry Andric }
7194ba319b5SDimitry Andric
copy_file_impl(FileDescriptor & from,FileDescriptor & to,error_code & ec)7204ba319b5SDimitry Andric bool copy_file_impl(FileDescriptor& from, FileDescriptor& to, error_code& ec) {
7214ba319b5SDimitry Andric #if defined(_LIBCPP_USE_SENDFILE)
7224ba319b5SDimitry Andric return copy_file_impl_sendfile(from, to, ec);
7234ba319b5SDimitry Andric #elif defined(_LIBCPP_USE_COPYFILE)
7244ba319b5SDimitry Andric return copy_file_impl_copyfile(from, to, ec);
7254ba319b5SDimitry Andric #else
7264ba319b5SDimitry Andric return copy_file_impl_default(from, to, ec);
7274ba319b5SDimitry Andric #endif
7284ba319b5SDimitry Andric }
7294ba319b5SDimitry Andric
7304ba319b5SDimitry Andric } // namespace
7314ba319b5SDimitry Andric } // namespace detail
7324ba319b5SDimitry Andric
__copy_file(const path & from,const path & to,copy_options options,error_code * ec)7334ba319b5SDimitry Andric bool __copy_file(const path& from, const path& to, copy_options options,
7344ba319b5SDimitry Andric error_code* ec) {
7354ba319b5SDimitry Andric using detail::FileDescriptor;
7364ba319b5SDimitry Andric ErrorHandler<bool> err("copy_file", ec, &to, &from);
7374ba319b5SDimitry Andric
7384ba319b5SDimitry Andric error_code m_ec;
7394ba319b5SDimitry Andric FileDescriptor from_fd =
7404ba319b5SDimitry Andric FileDescriptor::create_with_status(&from, m_ec, O_RDONLY | O_NONBLOCK);
7414ba319b5SDimitry Andric if (m_ec)
7424ba319b5SDimitry Andric return err.report(m_ec);
7434ba319b5SDimitry Andric
7444ba319b5SDimitry Andric auto from_st = from_fd.get_status();
7454ba319b5SDimitry Andric StatT const& from_stat = from_fd.get_stat();
7464ba319b5SDimitry Andric if (!is_regular_file(from_st)) {
7474ba319b5SDimitry Andric if (not m_ec)
7484ba319b5SDimitry Andric m_ec = make_error_code(errc::not_supported);
7494ba319b5SDimitry Andric return err.report(m_ec);
7504ba319b5SDimitry Andric }
7514ba319b5SDimitry Andric
7524ba319b5SDimitry Andric const bool skip_existing = bool(copy_options::skip_existing & options);
7534ba319b5SDimitry Andric const bool update_existing = bool(copy_options::update_existing & options);
7544ba319b5SDimitry Andric const bool overwrite_existing =
7554ba319b5SDimitry Andric bool(copy_options::overwrite_existing & options);
7564ba319b5SDimitry Andric
7574ba319b5SDimitry Andric StatT to_stat_path;
7584ba319b5SDimitry Andric file_status to_st = detail::posix_stat(to, to_stat_path, &m_ec);
7594ba319b5SDimitry Andric if (!status_known(to_st))
7604ba319b5SDimitry Andric return err.report(m_ec);
7614ba319b5SDimitry Andric
7624ba319b5SDimitry Andric const bool to_exists = exists(to_st);
7634ba319b5SDimitry Andric if (to_exists && !is_regular_file(to_st))
7644ba319b5SDimitry Andric return err.report(errc::not_supported);
7654ba319b5SDimitry Andric
7664ba319b5SDimitry Andric if (to_exists && detail::stat_equivalent(from_stat, to_stat_path))
7674ba319b5SDimitry Andric return err.report(errc::file_exists);
7684ba319b5SDimitry Andric
7694ba319b5SDimitry Andric if (to_exists && skip_existing)
7704ba319b5SDimitry Andric return false;
7714ba319b5SDimitry Andric
7724ba319b5SDimitry Andric bool ShouldCopy = [&]() {
7734ba319b5SDimitry Andric if (to_exists && update_existing) {
7744ba319b5SDimitry Andric auto from_time = detail::extract_mtime(from_stat);
7754ba319b5SDimitry Andric auto to_time = detail::extract_mtime(to_stat_path);
7764ba319b5SDimitry Andric if (from_time.tv_sec < to_time.tv_sec)
7774ba319b5SDimitry Andric return false;
7784ba319b5SDimitry Andric if (from_time.tv_sec == to_time.tv_sec &&
7794ba319b5SDimitry Andric from_time.tv_nsec <= to_time.tv_nsec)
7804ba319b5SDimitry Andric return false;
7814ba319b5SDimitry Andric return true;
7824ba319b5SDimitry Andric }
7834ba319b5SDimitry Andric if (!to_exists || overwrite_existing)
7844ba319b5SDimitry Andric return true;
7854ba319b5SDimitry Andric return err.report(errc::file_exists);
7864ba319b5SDimitry Andric }();
7874ba319b5SDimitry Andric if (!ShouldCopy)
7884ba319b5SDimitry Andric return false;
7894ba319b5SDimitry Andric
7904ba319b5SDimitry Andric // Don't truncate right away. We may not be opening the file we originally
7914ba319b5SDimitry Andric // looked at; we'll check this later.
7924ba319b5SDimitry Andric int to_open_flags = O_WRONLY;
7934ba319b5SDimitry Andric if (!to_exists)
7944ba319b5SDimitry Andric to_open_flags |= O_CREAT;
7954ba319b5SDimitry Andric FileDescriptor to_fd = FileDescriptor::create_with_status(
7964ba319b5SDimitry Andric &to, m_ec, to_open_flags, from_stat.st_mode);
7974ba319b5SDimitry Andric if (m_ec)
7984ba319b5SDimitry Andric return err.report(m_ec);
7994ba319b5SDimitry Andric
8004ba319b5SDimitry Andric if (to_exists) {
8014ba319b5SDimitry Andric // Check that the file we initially stat'ed is equivalent to the one
8024ba319b5SDimitry Andric // we opened.
8034ba319b5SDimitry Andric // FIXME: report this better.
8044ba319b5SDimitry Andric if (!detail::stat_equivalent(to_stat_path, to_fd.get_stat()))
8054ba319b5SDimitry Andric return err.report(errc::bad_file_descriptor);
8064ba319b5SDimitry Andric
8074ba319b5SDimitry Andric // Set the permissions and truncate the file we opened.
8084ba319b5SDimitry Andric if (detail::posix_fchmod(to_fd, from_stat, m_ec))
8094ba319b5SDimitry Andric return err.report(m_ec);
8104ba319b5SDimitry Andric if (detail::posix_ftruncate(to_fd, 0, m_ec))
8114ba319b5SDimitry Andric return err.report(m_ec);
8124ba319b5SDimitry Andric }
8134ba319b5SDimitry Andric
8144ba319b5SDimitry Andric if (!copy_file_impl(from_fd, to_fd, m_ec)) {
8154ba319b5SDimitry Andric // FIXME: Remove the dest file if we failed, and it didn't exist previously.
8164ba319b5SDimitry Andric return err.report(m_ec);
8174ba319b5SDimitry Andric }
8184ba319b5SDimitry Andric
8194ba319b5SDimitry Andric return true;
8204ba319b5SDimitry Andric }
8214ba319b5SDimitry Andric
__copy_symlink(const path & existing_symlink,const path & new_symlink,error_code * ec)8224ba319b5SDimitry Andric void __copy_symlink(const path& existing_symlink, const path& new_symlink,
8234ba319b5SDimitry Andric error_code* ec) {
8244ba319b5SDimitry Andric const path real_path(__read_symlink(existing_symlink, ec));
8254ba319b5SDimitry Andric if (ec && *ec) {
8264ba319b5SDimitry Andric return;
8274ba319b5SDimitry Andric }
8284ba319b5SDimitry Andric // NOTE: proposal says you should detect if you should call
8294ba319b5SDimitry Andric // create_symlink or create_directory_symlink. I don't think this
8304ba319b5SDimitry Andric // is needed with POSIX
8314ba319b5SDimitry Andric __create_symlink(real_path, new_symlink, ec);
8324ba319b5SDimitry Andric }
8334ba319b5SDimitry Andric
__create_directories(const path & p,error_code * ec)8344ba319b5SDimitry Andric bool __create_directories(const path& p, error_code* ec) {
8354ba319b5SDimitry Andric ErrorHandler<bool> err("create_directories", ec, &p);
8364ba319b5SDimitry Andric
8374ba319b5SDimitry Andric error_code m_ec;
8384ba319b5SDimitry Andric auto const st = detail::posix_stat(p, &m_ec);
8394ba319b5SDimitry Andric if (!status_known(st))
8404ba319b5SDimitry Andric return err.report(m_ec);
8414ba319b5SDimitry Andric else if (is_directory(st))
8424ba319b5SDimitry Andric return false;
8434ba319b5SDimitry Andric else if (exists(st))
8444ba319b5SDimitry Andric return err.report(errc::file_exists);
8454ba319b5SDimitry Andric
8464ba319b5SDimitry Andric const path parent = p.parent_path();
8474ba319b5SDimitry Andric if (!parent.empty()) {
8484ba319b5SDimitry Andric const file_status parent_st = status(parent, m_ec);
8494ba319b5SDimitry Andric if (not status_known(parent_st))
8504ba319b5SDimitry Andric return err.report(m_ec);
8514ba319b5SDimitry Andric if (not exists(parent_st)) {
8524ba319b5SDimitry Andric __create_directories(parent, ec);
8534ba319b5SDimitry Andric if (ec && *ec) {
8544ba319b5SDimitry Andric return false;
8554ba319b5SDimitry Andric }
8564ba319b5SDimitry Andric }
8574ba319b5SDimitry Andric }
8584ba319b5SDimitry Andric return __create_directory(p, ec);
8594ba319b5SDimitry Andric }
8604ba319b5SDimitry Andric
__create_directory(const path & p,error_code * ec)8614ba319b5SDimitry Andric bool __create_directory(const path& p, error_code* ec) {
8624ba319b5SDimitry Andric ErrorHandler<bool> err("create_directory", ec, &p);
8634ba319b5SDimitry Andric
8644ba319b5SDimitry Andric if (::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0)
8654ba319b5SDimitry Andric return true;
8664ba319b5SDimitry Andric if (errno != EEXIST)
8674ba319b5SDimitry Andric err.report(capture_errno());
8684ba319b5SDimitry Andric return false;
8694ba319b5SDimitry Andric }
8704ba319b5SDimitry Andric
__create_directory(path const & p,path const & attributes,error_code * ec)8714ba319b5SDimitry Andric bool __create_directory(path const& p, path const& attributes, error_code* ec) {
8724ba319b5SDimitry Andric ErrorHandler<bool> err("create_directory", ec, &p, &attributes);
8734ba319b5SDimitry Andric
8744ba319b5SDimitry Andric StatT attr_stat;
8754ba319b5SDimitry Andric error_code mec;
8764ba319b5SDimitry Andric auto st = detail::posix_stat(attributes, attr_stat, &mec);
8774ba319b5SDimitry Andric if (!status_known(st))
8784ba319b5SDimitry Andric return err.report(mec);
8794ba319b5SDimitry Andric if (!is_directory(st))
8804ba319b5SDimitry Andric return err.report(errc::not_a_directory,
8814ba319b5SDimitry Andric "the specified attribute path is invalid");
8824ba319b5SDimitry Andric
8834ba319b5SDimitry Andric if (::mkdir(p.c_str(), attr_stat.st_mode) == 0)
8844ba319b5SDimitry Andric return true;
8854ba319b5SDimitry Andric if (errno != EEXIST)
8864ba319b5SDimitry Andric err.report(capture_errno());
8874ba319b5SDimitry Andric return false;
8884ba319b5SDimitry Andric }
8894ba319b5SDimitry Andric
__create_directory_symlink(path const & from,path const & to,error_code * ec)8904ba319b5SDimitry Andric void __create_directory_symlink(path const& from, path const& to,
8914ba319b5SDimitry Andric error_code* ec) {
8924ba319b5SDimitry Andric ErrorHandler<void> err("create_directory_symlink", ec, &from, &to);
8934ba319b5SDimitry Andric if (::symlink(from.c_str(), to.c_str()) != 0)
8944ba319b5SDimitry Andric return err.report(capture_errno());
8954ba319b5SDimitry Andric }
8964ba319b5SDimitry Andric
__create_hard_link(const path & from,const path & to,error_code * ec)8974ba319b5SDimitry Andric void __create_hard_link(const path& from, const path& to, error_code* ec) {
8984ba319b5SDimitry Andric ErrorHandler<void> err("create_hard_link", ec, &from, &to);
8994ba319b5SDimitry Andric if (::link(from.c_str(), to.c_str()) == -1)
9004ba319b5SDimitry Andric return err.report(capture_errno());
9014ba319b5SDimitry Andric }
9024ba319b5SDimitry Andric
__create_symlink(path const & from,path const & to,error_code * ec)9034ba319b5SDimitry Andric void __create_symlink(path const& from, path const& to, error_code* ec) {
9044ba319b5SDimitry Andric ErrorHandler<void> err("create_symlink", ec, &from, &to);
9054ba319b5SDimitry Andric if (::symlink(from.c_str(), to.c_str()) == -1)
9064ba319b5SDimitry Andric return err.report(capture_errno());
9074ba319b5SDimitry Andric }
9084ba319b5SDimitry Andric
__current_path(error_code * ec)9094ba319b5SDimitry Andric path __current_path(error_code* ec) {
9104ba319b5SDimitry Andric ErrorHandler<path> err("current_path", ec);
9114ba319b5SDimitry Andric
9124ba319b5SDimitry Andric auto size = ::pathconf(".", _PC_PATH_MAX);
9134ba319b5SDimitry Andric _LIBCPP_ASSERT(size >= 0, "pathconf returned a 0 as max size");
9144ba319b5SDimitry Andric
9154ba319b5SDimitry Andric auto buff = unique_ptr<char[]>(new char[size + 1]);
9164ba319b5SDimitry Andric char* ret;
9174ba319b5SDimitry Andric if ((ret = ::getcwd(buff.get(), static_cast<size_t>(size))) == nullptr)
9184ba319b5SDimitry Andric return err.report(capture_errno(), "call to getcwd failed");
9194ba319b5SDimitry Andric
9204ba319b5SDimitry Andric return {buff.get()};
9214ba319b5SDimitry Andric }
9224ba319b5SDimitry Andric
__current_path(const path & p,error_code * ec)9234ba319b5SDimitry Andric void __current_path(const path& p, error_code* ec) {
9244ba319b5SDimitry Andric ErrorHandler<void> err("current_path", ec, &p);
9254ba319b5SDimitry Andric if (::chdir(p.c_str()) == -1)
9264ba319b5SDimitry Andric err.report(capture_errno());
9274ba319b5SDimitry Andric }
9284ba319b5SDimitry Andric
__equivalent(const path & p1,const path & p2,error_code * ec)9294ba319b5SDimitry Andric bool __equivalent(const path& p1, const path& p2, error_code* ec) {
9304ba319b5SDimitry Andric ErrorHandler<bool> err("equivalent", ec, &p1, &p2);
9314ba319b5SDimitry Andric
9324ba319b5SDimitry Andric error_code ec1, ec2;
9334ba319b5SDimitry Andric StatT st1 = {}, st2 = {};
9344ba319b5SDimitry Andric auto s1 = detail::posix_stat(p1.native(), st1, &ec1);
9354ba319b5SDimitry Andric if (!exists(s1))
9364ba319b5SDimitry Andric return err.report(errc::not_supported);
9374ba319b5SDimitry Andric auto s2 = detail::posix_stat(p2.native(), st2, &ec2);
9384ba319b5SDimitry Andric if (!exists(s2))
9394ba319b5SDimitry Andric return err.report(errc::not_supported);
9404ba319b5SDimitry Andric
9414ba319b5SDimitry Andric return detail::stat_equivalent(st1, st2);
9424ba319b5SDimitry Andric }
9434ba319b5SDimitry Andric
__file_size(const path & p,error_code * ec)9444ba319b5SDimitry Andric uintmax_t __file_size(const path& p, error_code* ec) {
9454ba319b5SDimitry Andric ErrorHandler<uintmax_t> err("file_size", ec, &p);
9464ba319b5SDimitry Andric
9474ba319b5SDimitry Andric error_code m_ec;
9484ba319b5SDimitry Andric StatT st;
9494ba319b5SDimitry Andric file_status fst = detail::posix_stat(p, st, &m_ec);
9504ba319b5SDimitry Andric if (!exists(fst) || !is_regular_file(fst)) {
9514ba319b5SDimitry Andric errc error_kind =
9524ba319b5SDimitry Andric is_directory(fst) ? errc::is_a_directory : errc::not_supported;
9534ba319b5SDimitry Andric if (!m_ec)
9544ba319b5SDimitry Andric m_ec = make_error_code(error_kind);
9554ba319b5SDimitry Andric return err.report(m_ec);
9564ba319b5SDimitry Andric }
9574ba319b5SDimitry Andric // is_regular_file(p) == true
9584ba319b5SDimitry Andric return static_cast<uintmax_t>(st.st_size);
9594ba319b5SDimitry Andric }
9604ba319b5SDimitry Andric
__hard_link_count(const path & p,error_code * ec)9614ba319b5SDimitry Andric uintmax_t __hard_link_count(const path& p, error_code* ec) {
9624ba319b5SDimitry Andric ErrorHandler<uintmax_t> err("hard_link_count", ec, &p);
9634ba319b5SDimitry Andric
9644ba319b5SDimitry Andric error_code m_ec;
9654ba319b5SDimitry Andric StatT st;
9664ba319b5SDimitry Andric detail::posix_stat(p, st, &m_ec);
9674ba319b5SDimitry Andric if (m_ec)
9684ba319b5SDimitry Andric return err.report(m_ec);
9694ba319b5SDimitry Andric return static_cast<uintmax_t>(st.st_nlink);
9704ba319b5SDimitry Andric }
9714ba319b5SDimitry Andric
__fs_is_empty(const path & p,error_code * ec)9724ba319b5SDimitry Andric bool __fs_is_empty(const path& p, error_code* ec) {
9734ba319b5SDimitry Andric ErrorHandler<bool> err("is_empty", ec, &p);
9744ba319b5SDimitry Andric
9754ba319b5SDimitry Andric error_code m_ec;
9764ba319b5SDimitry Andric StatT pst;
9774ba319b5SDimitry Andric auto st = detail::posix_stat(p, pst, &m_ec);
9784ba319b5SDimitry Andric if (m_ec)
9794ba319b5SDimitry Andric return err.report(m_ec);
9804ba319b5SDimitry Andric else if (!is_directory(st) && !is_regular_file(st))
9814ba319b5SDimitry Andric return err.report(errc::not_supported);
9824ba319b5SDimitry Andric else if (is_directory(st)) {
9834ba319b5SDimitry Andric auto it = ec ? directory_iterator(p, *ec) : directory_iterator(p);
9844ba319b5SDimitry Andric if (ec && *ec)
9854ba319b5SDimitry Andric return false;
9864ba319b5SDimitry Andric return it == directory_iterator{};
9874ba319b5SDimitry Andric } else if (is_regular_file(st))
9884ba319b5SDimitry Andric return static_cast<uintmax_t>(pst.st_size) == 0;
9894ba319b5SDimitry Andric
9904ba319b5SDimitry Andric _LIBCPP_UNREACHABLE();
9914ba319b5SDimitry Andric }
9924ba319b5SDimitry Andric
__extract_last_write_time(const path & p,const StatT & st,error_code * ec)9934ba319b5SDimitry Andric static file_time_type __extract_last_write_time(const path& p, const StatT& st,
9944ba319b5SDimitry Andric error_code* ec) {
9954ba319b5SDimitry Andric using detail::fs_time;
9964ba319b5SDimitry Andric ErrorHandler<file_time_type> err("last_write_time", ec, &p);
9974ba319b5SDimitry Andric
9984ba319b5SDimitry Andric auto ts = detail::extract_mtime(st);
9994ba319b5SDimitry Andric if (!fs_time::is_representable(ts))
10004ba319b5SDimitry Andric return err.report(errc::value_too_large);
10014ba319b5SDimitry Andric
10024ba319b5SDimitry Andric return fs_time::convert_from_timespec(ts);
10034ba319b5SDimitry Andric }
10044ba319b5SDimitry Andric
__last_write_time(const path & p,error_code * ec)10054ba319b5SDimitry Andric file_time_type __last_write_time(const path& p, error_code* ec) {
10064ba319b5SDimitry Andric using namespace chrono;
10074ba319b5SDimitry Andric ErrorHandler<file_time_type> err("last_write_time", ec, &p);
10084ba319b5SDimitry Andric
10094ba319b5SDimitry Andric error_code m_ec;
10104ba319b5SDimitry Andric StatT st;
10114ba319b5SDimitry Andric detail::posix_stat(p, st, &m_ec);
10124ba319b5SDimitry Andric if (m_ec)
10134ba319b5SDimitry Andric return err.report(m_ec);
10144ba319b5SDimitry Andric return __extract_last_write_time(p, st, ec);
10154ba319b5SDimitry Andric }
10164ba319b5SDimitry Andric
__last_write_time(const path & p,file_time_type new_time,error_code * ec)10174ba319b5SDimitry Andric void __last_write_time(const path& p, file_time_type new_time, error_code* ec) {
10184ba319b5SDimitry Andric using detail::fs_time;
10194ba319b5SDimitry Andric ErrorHandler<void> err("last_write_time", ec, &p);
10204ba319b5SDimitry Andric
10214ba319b5SDimitry Andric error_code m_ec;
10224ba319b5SDimitry Andric array<TimeSpec, 2> tbuf;
10234ba319b5SDimitry Andric #if !defined(_LIBCPP_USE_UTIMENSAT)
10244ba319b5SDimitry Andric // This implementation has a race condition between determining the
10254ba319b5SDimitry Andric // last access time and attempting to set it to the same value using
10264ba319b5SDimitry Andric // ::utimes
10274ba319b5SDimitry Andric StatT st;
10284ba319b5SDimitry Andric file_status fst = detail::posix_stat(p, st, &m_ec);
10294ba319b5SDimitry Andric if (m_ec)
10304ba319b5SDimitry Andric return err.report(m_ec);
10314ba319b5SDimitry Andric tbuf[0] = detail::extract_atime(st);
10324ba319b5SDimitry Andric #else
10334ba319b5SDimitry Andric tbuf[0].tv_sec = 0;
10344ba319b5SDimitry Andric tbuf[0].tv_nsec = UTIME_OMIT;
10354ba319b5SDimitry Andric #endif
10364ba319b5SDimitry Andric if (!fs_time::convert_to_timespec(tbuf[1], new_time))
10374ba319b5SDimitry Andric return err.report(errc::value_too_large);
10384ba319b5SDimitry Andric
10394ba319b5SDimitry Andric detail::set_file_times(p, tbuf, m_ec);
10404ba319b5SDimitry Andric if (m_ec)
10414ba319b5SDimitry Andric return err.report(m_ec);
10424ba319b5SDimitry Andric }
10434ba319b5SDimitry Andric
__permissions(const path & p,perms prms,perm_options opts,error_code * ec)10444ba319b5SDimitry Andric void __permissions(const path& p, perms prms, perm_options opts,
10454ba319b5SDimitry Andric error_code* ec) {
10464ba319b5SDimitry Andric ErrorHandler<void> err("permissions", ec, &p);
10474ba319b5SDimitry Andric
10484ba319b5SDimitry Andric auto has_opt = [&](perm_options o) { return bool(o & opts); };
10494ba319b5SDimitry Andric const bool resolve_symlinks = !has_opt(perm_options::nofollow);
10504ba319b5SDimitry Andric const bool add_perms = has_opt(perm_options::add);
10514ba319b5SDimitry Andric const bool remove_perms = has_opt(perm_options::remove);
10524ba319b5SDimitry Andric _LIBCPP_ASSERT(
10534ba319b5SDimitry Andric (add_perms + remove_perms + has_opt(perm_options::replace)) == 1,
10544ba319b5SDimitry Andric "One and only one of the perm_options constants replace, add, or remove "
10554ba319b5SDimitry Andric "is present in opts");
10564ba319b5SDimitry Andric
10574ba319b5SDimitry Andric bool set_sym_perms = false;
10584ba319b5SDimitry Andric prms &= perms::mask;
10594ba319b5SDimitry Andric if (!resolve_symlinks || (add_perms || remove_perms)) {
10604ba319b5SDimitry Andric error_code m_ec;
10614ba319b5SDimitry Andric file_status st = resolve_symlinks ? detail::posix_stat(p, &m_ec)
10624ba319b5SDimitry Andric : detail::posix_lstat(p, &m_ec);
10634ba319b5SDimitry Andric set_sym_perms = is_symlink(st);
10644ba319b5SDimitry Andric if (m_ec)
10654ba319b5SDimitry Andric return err.report(m_ec);
10664ba319b5SDimitry Andric _LIBCPP_ASSERT(st.permissions() != perms::unknown,
10674ba319b5SDimitry Andric "Permissions unexpectedly unknown");
10684ba319b5SDimitry Andric if (add_perms)
10694ba319b5SDimitry Andric prms |= st.permissions();
10704ba319b5SDimitry Andric else if (remove_perms)
10714ba319b5SDimitry Andric prms = st.permissions() & ~prms;
10724ba319b5SDimitry Andric }
10734ba319b5SDimitry Andric const auto real_perms = detail::posix_convert_perms(prms);
10744ba319b5SDimitry Andric
10754ba319b5SDimitry Andric #if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD)
10764ba319b5SDimitry Andric const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0;
10774ba319b5SDimitry Andric if (::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) {
10784ba319b5SDimitry Andric return err.report(capture_errno());
10794ba319b5SDimitry Andric }
10804ba319b5SDimitry Andric #else
10814ba319b5SDimitry Andric if (set_sym_perms)
10824ba319b5SDimitry Andric return err.report(errc::operation_not_supported);
10834ba319b5SDimitry Andric if (::chmod(p.c_str(), real_perms) == -1) {
10844ba319b5SDimitry Andric return err.report(capture_errno());
10854ba319b5SDimitry Andric }
10864ba319b5SDimitry Andric #endif
10874ba319b5SDimitry Andric }
10884ba319b5SDimitry Andric
__read_symlink(const path & p,error_code * ec)10894ba319b5SDimitry Andric path __read_symlink(const path& p, error_code* ec) {
10904ba319b5SDimitry Andric ErrorHandler<path> err("read_symlink", ec, &p);
10914ba319b5SDimitry Andric
10924ba319b5SDimitry Andric char buff[PATH_MAX + 1];
10934ba319b5SDimitry Andric error_code m_ec;
10944ba319b5SDimitry Andric ::ssize_t ret;
10954ba319b5SDimitry Andric if ((ret = ::readlink(p.c_str(), buff, PATH_MAX)) == -1) {
10964ba319b5SDimitry Andric return err.report(capture_errno());
10974ba319b5SDimitry Andric }
10984ba319b5SDimitry Andric _LIBCPP_ASSERT(ret <= PATH_MAX, "TODO");
10994ba319b5SDimitry Andric _LIBCPP_ASSERT(ret > 0, "TODO");
11004ba319b5SDimitry Andric buff[ret] = 0;
11014ba319b5SDimitry Andric return {buff};
11024ba319b5SDimitry Andric }
11034ba319b5SDimitry Andric
__remove(const path & p,error_code * ec)11044ba319b5SDimitry Andric bool __remove(const path& p, error_code* ec) {
11054ba319b5SDimitry Andric ErrorHandler<bool> err("remove", ec, &p);
11064ba319b5SDimitry Andric if (::remove(p.c_str()) == -1) {
11074ba319b5SDimitry Andric if (errno != ENOENT)
11084ba319b5SDimitry Andric err.report(capture_errno());
11094ba319b5SDimitry Andric return false;
11104ba319b5SDimitry Andric }
11114ba319b5SDimitry Andric return true;
11124ba319b5SDimitry Andric }
11134ba319b5SDimitry Andric
11144ba319b5SDimitry Andric namespace {
11154ba319b5SDimitry Andric
remove_all_impl(path const & p,error_code & ec)11164ba319b5SDimitry Andric uintmax_t remove_all_impl(path const& p, error_code& ec) {
11174ba319b5SDimitry Andric const auto npos = static_cast<uintmax_t>(-1);
11184ba319b5SDimitry Andric const file_status st = __symlink_status(p, &ec);
11194ba319b5SDimitry Andric if (ec)
11204ba319b5SDimitry Andric return npos;
11214ba319b5SDimitry Andric uintmax_t count = 1;
11224ba319b5SDimitry Andric if (is_directory(st)) {
11234ba319b5SDimitry Andric for (directory_iterator it(p, ec); !ec && it != directory_iterator();
11244ba319b5SDimitry Andric it.increment(ec)) {
11254ba319b5SDimitry Andric auto other_count = remove_all_impl(it->path(), ec);
11264ba319b5SDimitry Andric if (ec)
11274ba319b5SDimitry Andric return npos;
11284ba319b5SDimitry Andric count += other_count;
11294ba319b5SDimitry Andric }
11304ba319b5SDimitry Andric if (ec)
11314ba319b5SDimitry Andric return npos;
11324ba319b5SDimitry Andric }
11334ba319b5SDimitry Andric if (!__remove(p, &ec))
11344ba319b5SDimitry Andric return npos;
11354ba319b5SDimitry Andric return count;
11364ba319b5SDimitry Andric }
11374ba319b5SDimitry Andric
11384ba319b5SDimitry Andric } // end namespace
11394ba319b5SDimitry Andric
__remove_all(const path & p,error_code * ec)11404ba319b5SDimitry Andric uintmax_t __remove_all(const path& p, error_code* ec) {
11414ba319b5SDimitry Andric ErrorHandler<uintmax_t> err("remove_all", ec, &p);
11424ba319b5SDimitry Andric
11434ba319b5SDimitry Andric error_code mec;
11444ba319b5SDimitry Andric auto count = remove_all_impl(p, mec);
11454ba319b5SDimitry Andric if (mec) {
11464ba319b5SDimitry Andric if (mec == errc::no_such_file_or_directory)
11474ba319b5SDimitry Andric return 0;
11484ba319b5SDimitry Andric return err.report(mec);
11494ba319b5SDimitry Andric }
11504ba319b5SDimitry Andric return count;
11514ba319b5SDimitry Andric }
11524ba319b5SDimitry Andric
__rename(const path & from,const path & to,error_code * ec)11534ba319b5SDimitry Andric void __rename(const path& from, const path& to, error_code* ec) {
11544ba319b5SDimitry Andric ErrorHandler<void> err("rename", ec, &from, &to);
11554ba319b5SDimitry Andric if (::rename(from.c_str(), to.c_str()) == -1)
11564ba319b5SDimitry Andric err.report(capture_errno());
11574ba319b5SDimitry Andric }
11584ba319b5SDimitry Andric
__resize_file(const path & p,uintmax_t size,error_code * ec)11594ba319b5SDimitry Andric void __resize_file(const path& p, uintmax_t size, error_code* ec) {
11604ba319b5SDimitry Andric ErrorHandler<void> err("resize_file", ec, &p);
11614ba319b5SDimitry Andric if (::truncate(p.c_str(), static_cast< ::off_t>(size)) == -1)
11624ba319b5SDimitry Andric return err.report(capture_errno());
11634ba319b5SDimitry Andric }
11644ba319b5SDimitry Andric
__space(const path & p,error_code * ec)11654ba319b5SDimitry Andric space_info __space(const path& p, error_code* ec) {
11664ba319b5SDimitry Andric ErrorHandler<void> err("space", ec, &p);
11674ba319b5SDimitry Andric space_info si;
11684ba319b5SDimitry Andric struct statvfs m_svfs = {};
11694ba319b5SDimitry Andric if (::statvfs(p.c_str(), &m_svfs) == -1) {
11704ba319b5SDimitry Andric err.report(capture_errno());
11714ba319b5SDimitry Andric si.capacity = si.free = si.available = static_cast<uintmax_t>(-1);
11724ba319b5SDimitry Andric return si;
11734ba319b5SDimitry Andric }
11744ba319b5SDimitry Andric // Multiply with overflow checking.
11754ba319b5SDimitry Andric auto do_mult = [&](uintmax_t& out, uintmax_t other) {
11764ba319b5SDimitry Andric out = other * m_svfs.f_frsize;
11774ba319b5SDimitry Andric if (other == 0 || out / other != m_svfs.f_frsize)
11784ba319b5SDimitry Andric out = static_cast<uintmax_t>(-1);
11794ba319b5SDimitry Andric };
11804ba319b5SDimitry Andric do_mult(si.capacity, m_svfs.f_blocks);
11814ba319b5SDimitry Andric do_mult(si.free, m_svfs.f_bfree);
11824ba319b5SDimitry Andric do_mult(si.available, m_svfs.f_bavail);
11834ba319b5SDimitry Andric return si;
11844ba319b5SDimitry Andric }
11854ba319b5SDimitry Andric
__status(const path & p,error_code * ec)11864ba319b5SDimitry Andric file_status __status(const path& p, error_code* ec) {
11874ba319b5SDimitry Andric return detail::posix_stat(p, ec);
11884ba319b5SDimitry Andric }
11894ba319b5SDimitry Andric
__symlink_status(const path & p,error_code * ec)11904ba319b5SDimitry Andric file_status __symlink_status(const path& p, error_code* ec) {
11914ba319b5SDimitry Andric return detail::posix_lstat(p, ec);
11924ba319b5SDimitry Andric }
11934ba319b5SDimitry Andric
__temp_directory_path(error_code * ec)11944ba319b5SDimitry Andric path __temp_directory_path(error_code* ec) {
11954ba319b5SDimitry Andric ErrorHandler<path> err("temp_directory_path", ec);
11964ba319b5SDimitry Andric
11974ba319b5SDimitry Andric const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
11984ba319b5SDimitry Andric const char* ret = nullptr;
11994ba319b5SDimitry Andric
12004ba319b5SDimitry Andric for (auto& ep : env_paths)
12014ba319b5SDimitry Andric if ((ret = getenv(ep)))
12024ba319b5SDimitry Andric break;
12034ba319b5SDimitry Andric if (ret == nullptr)
12044ba319b5SDimitry Andric ret = "/tmp";
12054ba319b5SDimitry Andric
12064ba319b5SDimitry Andric path p(ret);
12074ba319b5SDimitry Andric error_code m_ec;
12084ba319b5SDimitry Andric file_status st = detail::posix_stat(p, &m_ec);
12094ba319b5SDimitry Andric if (!status_known(st))
12104ba319b5SDimitry Andric return err.report(m_ec, "cannot access path \"%s\"", p);
12114ba319b5SDimitry Andric
12124ba319b5SDimitry Andric if (!exists(st) || !is_directory(st))
12134ba319b5SDimitry Andric return err.report(errc::not_a_directory, "path \"%s\" is not a directory",
12144ba319b5SDimitry Andric p);
12154ba319b5SDimitry Andric
12164ba319b5SDimitry Andric return p;
12174ba319b5SDimitry Andric }
12184ba319b5SDimitry Andric
__weakly_canonical(const path & p,error_code * ec)12194ba319b5SDimitry Andric path __weakly_canonical(const path& p, error_code* ec) {
12204ba319b5SDimitry Andric ErrorHandler<path> err("weakly_canonical", ec, &p);
12214ba319b5SDimitry Andric
12224ba319b5SDimitry Andric if (p.empty())
12234ba319b5SDimitry Andric return __canonical("", ec);
12244ba319b5SDimitry Andric
12254ba319b5SDimitry Andric path result;
12264ba319b5SDimitry Andric path tmp;
12274ba319b5SDimitry Andric tmp.__reserve(p.native().size());
12284ba319b5SDimitry Andric auto PP = PathParser::CreateEnd(p.native());
12294ba319b5SDimitry Andric --PP;
12304ba319b5SDimitry Andric vector<string_view_t> DNEParts;
12314ba319b5SDimitry Andric
12324ba319b5SDimitry Andric while (PP.State != PathParser::PS_BeforeBegin) {
12334ba319b5SDimitry Andric tmp.assign(createView(p.native().data(), &PP.RawEntry.back()));
12344ba319b5SDimitry Andric error_code m_ec;
12354ba319b5SDimitry Andric file_status st = __status(tmp, &m_ec);
12364ba319b5SDimitry Andric if (!status_known(st)) {
12374ba319b5SDimitry Andric return err.report(m_ec);
12384ba319b5SDimitry Andric } else if (exists(st)) {
12394ba319b5SDimitry Andric result = __canonical(tmp, ec);
12404ba319b5SDimitry Andric break;
12414ba319b5SDimitry Andric }
12424ba319b5SDimitry Andric DNEParts.push_back(*PP);
12434ba319b5SDimitry Andric --PP;
12444ba319b5SDimitry Andric }
12454ba319b5SDimitry Andric if (PP.State == PathParser::PS_BeforeBegin)
12464ba319b5SDimitry Andric result = __canonical("", ec);
12474ba319b5SDimitry Andric if (ec)
12484ba319b5SDimitry Andric ec->clear();
12494ba319b5SDimitry Andric if (DNEParts.empty())
12504ba319b5SDimitry Andric return result;
12514ba319b5SDimitry Andric for (auto It = DNEParts.rbegin(); It != DNEParts.rend(); ++It)
12524ba319b5SDimitry Andric result /= *It;
12534ba319b5SDimitry Andric return result.lexically_normal();
12544ba319b5SDimitry Andric }
12554ba319b5SDimitry Andric
12564ba319b5SDimitry Andric ///////////////////////////////////////////////////////////////////////////////
12574ba319b5SDimitry Andric // path definitions
12584ba319b5SDimitry Andric ///////////////////////////////////////////////////////////////////////////////
12594ba319b5SDimitry Andric
12604ba319b5SDimitry Andric constexpr path::value_type path::preferred_separator;
12614ba319b5SDimitry Andric
replace_extension(path const & replacement)12624ba319b5SDimitry Andric path& path::replace_extension(path const& replacement) {
12634ba319b5SDimitry Andric path p = extension();
12644ba319b5SDimitry Andric if (not p.empty()) {
12654ba319b5SDimitry Andric __pn_.erase(__pn_.size() - p.native().size());
12664ba319b5SDimitry Andric }
12674ba319b5SDimitry Andric if (!replacement.empty()) {
12684ba319b5SDimitry Andric if (replacement.native()[0] != '.') {
12694ba319b5SDimitry Andric __pn_ += ".";
12704ba319b5SDimitry Andric }
12714ba319b5SDimitry Andric __pn_.append(replacement.__pn_);
12724ba319b5SDimitry Andric }
12734ba319b5SDimitry Andric return *this;
12744ba319b5SDimitry Andric }
12754ba319b5SDimitry Andric
12764ba319b5SDimitry Andric ///////////////////////////////////////////////////////////////////////////////
12774ba319b5SDimitry Andric // path.decompose
12784ba319b5SDimitry Andric
__root_name() const12794ba319b5SDimitry Andric string_view_t path::__root_name() const {
12804ba319b5SDimitry Andric auto PP = PathParser::CreateBegin(__pn_);
12814ba319b5SDimitry Andric if (PP.State == PathParser::PS_InRootName)
12824ba319b5SDimitry Andric return *PP;
12834ba319b5SDimitry Andric return {};
12844ba319b5SDimitry Andric }
12854ba319b5SDimitry Andric
__root_directory() const12864ba319b5SDimitry Andric string_view_t path::__root_directory() const {
12874ba319b5SDimitry Andric auto PP = PathParser::CreateBegin(__pn_);
12884ba319b5SDimitry Andric if (PP.State == PathParser::PS_InRootName)
12894ba319b5SDimitry Andric ++PP;
12904ba319b5SDimitry Andric if (PP.State == PathParser::PS_InRootDir)
12914ba319b5SDimitry Andric return *PP;
12924ba319b5SDimitry Andric return {};
12934ba319b5SDimitry Andric }
12944ba319b5SDimitry Andric
__root_path_raw() const12954ba319b5SDimitry Andric string_view_t path::__root_path_raw() const {
12964ba319b5SDimitry Andric auto PP = PathParser::CreateBegin(__pn_);
12974ba319b5SDimitry Andric if (PP.State == PathParser::PS_InRootName) {
12984ba319b5SDimitry Andric auto NextCh = PP.peek();
12994ba319b5SDimitry Andric if (NextCh && *NextCh == '/') {
13004ba319b5SDimitry Andric ++PP;
13014ba319b5SDimitry Andric return createView(__pn_.data(), &PP.RawEntry.back());
13024ba319b5SDimitry Andric }
13034ba319b5SDimitry Andric return PP.RawEntry;
13044ba319b5SDimitry Andric }
13054ba319b5SDimitry Andric if (PP.State == PathParser::PS_InRootDir)
13064ba319b5SDimitry Andric return *PP;
13074ba319b5SDimitry Andric return {};
13084ba319b5SDimitry Andric }
13094ba319b5SDimitry Andric
ConsumeRootName(PathParser * PP)1310*b5893f02SDimitry Andric static bool ConsumeRootName(PathParser *PP) {
1311*b5893f02SDimitry Andric static_assert(PathParser::PS_BeforeBegin == 1 &&
1312*b5893f02SDimitry Andric PathParser::PS_InRootName == 2,
1313*b5893f02SDimitry Andric "Values for enums are incorrect");
1314*b5893f02SDimitry Andric while (PP->State <= PathParser::PS_InRootName)
1315*b5893f02SDimitry Andric ++(*PP);
1316*b5893f02SDimitry Andric return PP->State == PathParser::PS_AtEnd;
1317*b5893f02SDimitry Andric }
1318*b5893f02SDimitry Andric
ConsumeRootDir(PathParser * PP)13194ba319b5SDimitry Andric static bool ConsumeRootDir(PathParser* PP) {
1320*b5893f02SDimitry Andric static_assert(PathParser::PS_BeforeBegin == 1 &&
1321*b5893f02SDimitry Andric PathParser::PS_InRootName == 2 &&
1322*b5893f02SDimitry Andric PathParser::PS_InRootDir == 3, "Values for enums are incorrect");
13234ba319b5SDimitry Andric while (PP->State <= PathParser::PS_InRootDir)
13244ba319b5SDimitry Andric ++(*PP);
13254ba319b5SDimitry Andric return PP->State == PathParser::PS_AtEnd;
13264ba319b5SDimitry Andric }
13274ba319b5SDimitry Andric
__relative_path() const13284ba319b5SDimitry Andric string_view_t path::__relative_path() const {
13294ba319b5SDimitry Andric auto PP = PathParser::CreateBegin(__pn_);
13304ba319b5SDimitry Andric if (ConsumeRootDir(&PP))
13314ba319b5SDimitry Andric return {};
13324ba319b5SDimitry Andric return createView(PP.RawEntry.data(), &__pn_.back());
13334ba319b5SDimitry Andric }
13344ba319b5SDimitry Andric
__parent_path() const13354ba319b5SDimitry Andric string_view_t path::__parent_path() const {
13364ba319b5SDimitry Andric if (empty())
13374ba319b5SDimitry Andric return {};
13384ba319b5SDimitry Andric // Determine if we have a root path but not a relative path. In that case
13394ba319b5SDimitry Andric // return *this.
13404ba319b5SDimitry Andric {
13414ba319b5SDimitry Andric auto PP = PathParser::CreateBegin(__pn_);
13424ba319b5SDimitry Andric if (ConsumeRootDir(&PP))
13434ba319b5SDimitry Andric return __pn_;
13444ba319b5SDimitry Andric }
13454ba319b5SDimitry Andric // Otherwise remove a single element from the end of the path, and return
13464ba319b5SDimitry Andric // a string representing that path
13474ba319b5SDimitry Andric {
13484ba319b5SDimitry Andric auto PP = PathParser::CreateEnd(__pn_);
13494ba319b5SDimitry Andric --PP;
13504ba319b5SDimitry Andric if (PP.RawEntry.data() == __pn_.data())
13514ba319b5SDimitry Andric return {};
13524ba319b5SDimitry Andric --PP;
13534ba319b5SDimitry Andric return createView(__pn_.data(), &PP.RawEntry.back());
13544ba319b5SDimitry Andric }
13554ba319b5SDimitry Andric }
13564ba319b5SDimitry Andric
__filename() const13574ba319b5SDimitry Andric string_view_t path::__filename() const {
13584ba319b5SDimitry Andric if (empty())
13594ba319b5SDimitry Andric return {};
13604ba319b5SDimitry Andric {
13614ba319b5SDimitry Andric PathParser PP = PathParser::CreateBegin(__pn_);
13624ba319b5SDimitry Andric if (ConsumeRootDir(&PP))
13634ba319b5SDimitry Andric return {};
13644ba319b5SDimitry Andric }
13654ba319b5SDimitry Andric return *(--PathParser::CreateEnd(__pn_));
13664ba319b5SDimitry Andric }
13674ba319b5SDimitry Andric
__stem() const13684ba319b5SDimitry Andric string_view_t path::__stem() const {
13694ba319b5SDimitry Andric return parser::separate_filename(__filename()).first;
13704ba319b5SDimitry Andric }
13714ba319b5SDimitry Andric
__extension() const13724ba319b5SDimitry Andric string_view_t path::__extension() const {
13734ba319b5SDimitry Andric return parser::separate_filename(__filename()).second;
13744ba319b5SDimitry Andric }
13754ba319b5SDimitry Andric
13764ba319b5SDimitry Andric ////////////////////////////////////////////////////////////////////////////
13774ba319b5SDimitry Andric // path.gen
13784ba319b5SDimitry Andric
13794ba319b5SDimitry Andric enum PathPartKind : unsigned char {
13804ba319b5SDimitry Andric PK_None,
13814ba319b5SDimitry Andric PK_RootSep,
13824ba319b5SDimitry Andric PK_Filename,
13834ba319b5SDimitry Andric PK_Dot,
13844ba319b5SDimitry Andric PK_DotDot,
13854ba319b5SDimitry Andric PK_TrailingSep
13864ba319b5SDimitry Andric };
13874ba319b5SDimitry Andric
ClassifyPathPart(string_view_t Part)13884ba319b5SDimitry Andric static PathPartKind ClassifyPathPart(string_view_t Part) {
13894ba319b5SDimitry Andric if (Part.empty())
13904ba319b5SDimitry Andric return PK_TrailingSep;
13914ba319b5SDimitry Andric if (Part == ".")
13924ba319b5SDimitry Andric return PK_Dot;
13934ba319b5SDimitry Andric if (Part == "..")
13944ba319b5SDimitry Andric return PK_DotDot;
13954ba319b5SDimitry Andric if (Part == "/")
13964ba319b5SDimitry Andric return PK_RootSep;
13974ba319b5SDimitry Andric return PK_Filename;
13984ba319b5SDimitry Andric }
13994ba319b5SDimitry Andric
lexically_normal() const14004ba319b5SDimitry Andric path path::lexically_normal() const {
14014ba319b5SDimitry Andric if (__pn_.empty())
14024ba319b5SDimitry Andric return *this;
14034ba319b5SDimitry Andric
14044ba319b5SDimitry Andric using PartKindPair = pair<string_view_t, PathPartKind>;
14054ba319b5SDimitry Andric vector<PartKindPair> Parts;
14064ba319b5SDimitry Andric // Guess as to how many elements the path has to avoid reallocating.
14074ba319b5SDimitry Andric Parts.reserve(32);
14084ba319b5SDimitry Andric
14094ba319b5SDimitry Andric // Track the total size of the parts as we collect them. This allows the
14104ba319b5SDimitry Andric // resulting path to reserve the correct amount of memory.
14114ba319b5SDimitry Andric size_t NewPathSize = 0;
14124ba319b5SDimitry Andric auto AddPart = [&](PathPartKind K, string_view_t P) {
14134ba319b5SDimitry Andric NewPathSize += P.size();
14144ba319b5SDimitry Andric Parts.emplace_back(P, K);
14154ba319b5SDimitry Andric };
14164ba319b5SDimitry Andric auto LastPartKind = [&]() {
14174ba319b5SDimitry Andric if (Parts.empty())
14184ba319b5SDimitry Andric return PK_None;
14194ba319b5SDimitry Andric return Parts.back().second;
14204ba319b5SDimitry Andric };
14214ba319b5SDimitry Andric
14224ba319b5SDimitry Andric bool MaybeNeedTrailingSep = false;
14234ba319b5SDimitry Andric // Build a stack containing the remaining elements of the path, popping off
14244ba319b5SDimitry Andric // elements which occur before a '..' entry.
14254ba319b5SDimitry Andric for (auto PP = PathParser::CreateBegin(__pn_); PP; ++PP) {
14264ba319b5SDimitry Andric auto Part = *PP;
14274ba319b5SDimitry Andric PathPartKind Kind = ClassifyPathPart(Part);
14284ba319b5SDimitry Andric switch (Kind) {
14294ba319b5SDimitry Andric case PK_Filename:
14304ba319b5SDimitry Andric case PK_RootSep: {
14314ba319b5SDimitry Andric // Add all non-dot and non-dot-dot elements to the stack of elements.
14324ba319b5SDimitry Andric AddPart(Kind, Part);
14334ba319b5SDimitry Andric MaybeNeedTrailingSep = false;
14344ba319b5SDimitry Andric break;
14354ba319b5SDimitry Andric }
14364ba319b5SDimitry Andric case PK_DotDot: {
14374ba319b5SDimitry Andric // Only push a ".." element if there are no elements preceding the "..",
14384ba319b5SDimitry Andric // or if the preceding element is itself "..".
14394ba319b5SDimitry Andric auto LastKind = LastPartKind();
14404ba319b5SDimitry Andric if (LastKind == PK_Filename) {
14414ba319b5SDimitry Andric NewPathSize -= Parts.back().first.size();
14424ba319b5SDimitry Andric Parts.pop_back();
14434ba319b5SDimitry Andric } else if (LastKind != PK_RootSep)
14444ba319b5SDimitry Andric AddPart(PK_DotDot, "..");
14454ba319b5SDimitry Andric MaybeNeedTrailingSep = LastKind == PK_Filename;
14464ba319b5SDimitry Andric break;
14474ba319b5SDimitry Andric }
14484ba319b5SDimitry Andric case PK_Dot:
14494ba319b5SDimitry Andric case PK_TrailingSep: {
14504ba319b5SDimitry Andric MaybeNeedTrailingSep = true;
14514ba319b5SDimitry Andric break;
14524ba319b5SDimitry Andric }
14534ba319b5SDimitry Andric case PK_None:
14544ba319b5SDimitry Andric _LIBCPP_UNREACHABLE();
14554ba319b5SDimitry Andric }
14564ba319b5SDimitry Andric }
14574ba319b5SDimitry Andric // [fs.path.generic]p6.8: If the path is empty, add a dot.
14584ba319b5SDimitry Andric if (Parts.empty())
14594ba319b5SDimitry Andric return ".";
14604ba319b5SDimitry Andric
14614ba319b5SDimitry Andric // [fs.path.generic]p6.7: If the last filename is dot-dot, remove any
14624ba319b5SDimitry Andric // trailing directory-separator.
14634ba319b5SDimitry Andric bool NeedTrailingSep = MaybeNeedTrailingSep && LastPartKind() == PK_Filename;
14644ba319b5SDimitry Andric
14654ba319b5SDimitry Andric path Result;
14664ba319b5SDimitry Andric Result.__pn_.reserve(Parts.size() + NewPathSize + NeedTrailingSep);
14674ba319b5SDimitry Andric for (auto& PK : Parts)
14684ba319b5SDimitry Andric Result /= PK.first;
14694ba319b5SDimitry Andric
14704ba319b5SDimitry Andric if (NeedTrailingSep)
14714ba319b5SDimitry Andric Result /= "";
14724ba319b5SDimitry Andric
14734ba319b5SDimitry Andric return Result;
14744ba319b5SDimitry Andric }
14754ba319b5SDimitry Andric
DetermineLexicalElementCount(PathParser PP)14764ba319b5SDimitry Andric static int DetermineLexicalElementCount(PathParser PP) {
14774ba319b5SDimitry Andric int Count = 0;
14784ba319b5SDimitry Andric for (; PP; ++PP) {
14794ba319b5SDimitry Andric auto Elem = *PP;
14804ba319b5SDimitry Andric if (Elem == "..")
14814ba319b5SDimitry Andric --Count;
1482*b5893f02SDimitry Andric else if (Elem != "." && Elem != "")
14834ba319b5SDimitry Andric ++Count;
14844ba319b5SDimitry Andric }
14854ba319b5SDimitry Andric return Count;
14864ba319b5SDimitry Andric }
14874ba319b5SDimitry Andric
lexically_relative(const path & base) const14884ba319b5SDimitry Andric path path::lexically_relative(const path& base) const {
14894ba319b5SDimitry Andric { // perform root-name/root-directory mismatch checks
14904ba319b5SDimitry Andric auto PP = PathParser::CreateBegin(__pn_);
14914ba319b5SDimitry Andric auto PPBase = PathParser::CreateBegin(base.__pn_);
14924ba319b5SDimitry Andric auto CheckIterMismatchAtBase = [&]() {
14934ba319b5SDimitry Andric return PP.State != PPBase.State &&
14944ba319b5SDimitry Andric (PP.inRootPath() || PPBase.inRootPath());
14954ba319b5SDimitry Andric };
1496*b5893f02SDimitry Andric if (PP.inRootName() && PPBase.inRootName()) {
14974ba319b5SDimitry Andric if (*PP != *PPBase)
14984ba319b5SDimitry Andric return {};
14994ba319b5SDimitry Andric } else if (CheckIterMismatchAtBase())
15004ba319b5SDimitry Andric return {};
15014ba319b5SDimitry Andric
15024ba319b5SDimitry Andric if (PP.inRootPath())
15034ba319b5SDimitry Andric ++PP;
15044ba319b5SDimitry Andric if (PPBase.inRootPath())
15054ba319b5SDimitry Andric ++PPBase;
15064ba319b5SDimitry Andric if (CheckIterMismatchAtBase())
15074ba319b5SDimitry Andric return {};
15084ba319b5SDimitry Andric }
15094ba319b5SDimitry Andric
15104ba319b5SDimitry Andric // Find the first mismatching element
15114ba319b5SDimitry Andric auto PP = PathParser::CreateBegin(__pn_);
15124ba319b5SDimitry Andric auto PPBase = PathParser::CreateBegin(base.__pn_);
15134ba319b5SDimitry Andric while (PP && PPBase && PP.State == PPBase.State && *PP == *PPBase) {
15144ba319b5SDimitry Andric ++PP;
15154ba319b5SDimitry Andric ++PPBase;
15164ba319b5SDimitry Andric }
15174ba319b5SDimitry Andric
15184ba319b5SDimitry Andric // If there is no mismatch, return ".".
15194ba319b5SDimitry Andric if (!PP && !PPBase)
15204ba319b5SDimitry Andric return ".";
15214ba319b5SDimitry Andric
15224ba319b5SDimitry Andric // Otherwise, determine the number of elements, 'n', which are not dot or
15234ba319b5SDimitry Andric // dot-dot minus the number of dot-dot elements.
15244ba319b5SDimitry Andric int ElemCount = DetermineLexicalElementCount(PPBase);
15254ba319b5SDimitry Andric if (ElemCount < 0)
15264ba319b5SDimitry Andric return {};
15274ba319b5SDimitry Andric
1528*b5893f02SDimitry Andric // if n == 0 and (a == end() || a->empty()), returns path("."); otherwise
1529*b5893f02SDimitry Andric if (ElemCount == 0 && (PP.atEnd() || *PP == ""))
1530*b5893f02SDimitry Andric return ".";
1531*b5893f02SDimitry Andric
15324ba319b5SDimitry Andric // return a path constructed with 'n' dot-dot elements, followed by the the
15334ba319b5SDimitry Andric // elements of '*this' after the mismatch.
15344ba319b5SDimitry Andric path Result;
15354ba319b5SDimitry Andric // FIXME: Reserve enough room in Result that it won't have to re-allocate.
15364ba319b5SDimitry Andric while (ElemCount--)
15374ba319b5SDimitry Andric Result /= "..";
15384ba319b5SDimitry Andric for (; PP; ++PP)
15394ba319b5SDimitry Andric Result /= *PP;
15404ba319b5SDimitry Andric return Result;
15414ba319b5SDimitry Andric }
15424ba319b5SDimitry Andric
15434ba319b5SDimitry Andric ////////////////////////////////////////////////////////////////////////////
15444ba319b5SDimitry Andric // path.comparisons
CompareRootName(PathParser * LHS,PathParser * RHS)1545*b5893f02SDimitry Andric static int CompareRootName(PathParser *LHS, PathParser *RHS) {
1546*b5893f02SDimitry Andric if (!LHS->inRootName() && !RHS->inRootName())
15474ba319b5SDimitry Andric return 0;
1548*b5893f02SDimitry Andric
1549*b5893f02SDimitry Andric auto GetRootName = [](PathParser *Parser) -> string_view_t {
1550*b5893f02SDimitry Andric return Parser->inRootName() ? **Parser : "";
1551*b5893f02SDimitry Andric };
1552*b5893f02SDimitry Andric int res = GetRootName(LHS).compare(GetRootName(RHS));
1553*b5893f02SDimitry Andric ConsumeRootName(LHS);
1554*b5893f02SDimitry Andric ConsumeRootName(RHS);
1555*b5893f02SDimitry Andric return res;
1556*b5893f02SDimitry Andric }
1557*b5893f02SDimitry Andric
CompareRootDir(PathParser * LHS,PathParser * RHS)1558*b5893f02SDimitry Andric static int CompareRootDir(PathParser *LHS, PathParser *RHS) {
1559*b5893f02SDimitry Andric if (!LHS->inRootDir() && RHS->inRootDir())
15604ba319b5SDimitry Andric return -1;
1561*b5893f02SDimitry Andric else if (LHS->inRootDir() && !RHS->inRootDir())
15624ba319b5SDimitry Andric return 1;
1563*b5893f02SDimitry Andric else {
1564*b5893f02SDimitry Andric ConsumeRootDir(LHS);
1565*b5893f02SDimitry Andric ConsumeRootDir(RHS);
1566*b5893f02SDimitry Andric return 0;
1567*b5893f02SDimitry Andric }
1568*b5893f02SDimitry Andric }
1569*b5893f02SDimitry Andric
CompareRelative(PathParser * LHSPtr,PathParser * RHSPtr)1570*b5893f02SDimitry Andric static int CompareRelative(PathParser *LHSPtr, PathParser *RHSPtr) {
1571*b5893f02SDimitry Andric auto &LHS = *LHSPtr;
1572*b5893f02SDimitry Andric auto &RHS = *RHSPtr;
1573*b5893f02SDimitry Andric
1574*b5893f02SDimitry Andric int res;
1575*b5893f02SDimitry Andric while (LHS && RHS) {
1576*b5893f02SDimitry Andric if ((res = (*LHS).compare(*RHS)) != 0)
1577*b5893f02SDimitry Andric return res;
1578*b5893f02SDimitry Andric ++LHS;
1579*b5893f02SDimitry Andric ++RHS;
1580*b5893f02SDimitry Andric }
1581*b5893f02SDimitry Andric return 0;
1582*b5893f02SDimitry Andric }
1583*b5893f02SDimitry Andric
CompareEndState(PathParser * LHS,PathParser * RHS)1584*b5893f02SDimitry Andric static int CompareEndState(PathParser *LHS, PathParser *RHS) {
1585*b5893f02SDimitry Andric if (LHS->atEnd() && !RHS->atEnd())
1586*b5893f02SDimitry Andric return -1;
1587*b5893f02SDimitry Andric else if (!LHS->atEnd() && RHS->atEnd())
1588*b5893f02SDimitry Andric return 1;
1589*b5893f02SDimitry Andric return 0;
1590*b5893f02SDimitry Andric }
1591*b5893f02SDimitry Andric
__compare(string_view_t __s) const1592*b5893f02SDimitry Andric int path::__compare(string_view_t __s) const {
1593*b5893f02SDimitry Andric auto LHS = PathParser::CreateBegin(__pn_);
1594*b5893f02SDimitry Andric auto RHS = PathParser::CreateBegin(__s);
1595*b5893f02SDimitry Andric int res;
1596*b5893f02SDimitry Andric
1597*b5893f02SDimitry Andric if ((res = CompareRootName(&LHS, &RHS)) != 0)
1598*b5893f02SDimitry Andric return res;
1599*b5893f02SDimitry Andric
1600*b5893f02SDimitry Andric if ((res = CompareRootDir(&LHS, &RHS)) != 0)
1601*b5893f02SDimitry Andric return res;
1602*b5893f02SDimitry Andric
1603*b5893f02SDimitry Andric if ((res = CompareRelative(&LHS, &RHS)) != 0)
1604*b5893f02SDimitry Andric return res;
1605*b5893f02SDimitry Andric
1606*b5893f02SDimitry Andric return CompareEndState(&LHS, &RHS);
16074ba319b5SDimitry Andric }
16084ba319b5SDimitry Andric
16094ba319b5SDimitry Andric ////////////////////////////////////////////////////////////////////////////
16104ba319b5SDimitry Andric // path.nonmembers
hash_value(const path & __p)16114ba319b5SDimitry Andric size_t hash_value(const path& __p) noexcept {
16124ba319b5SDimitry Andric auto PP = PathParser::CreateBegin(__p.native());
16134ba319b5SDimitry Andric size_t hash_value = 0;
16144ba319b5SDimitry Andric hash<string_view_t> hasher;
16154ba319b5SDimitry Andric while (PP) {
16164ba319b5SDimitry Andric hash_value = __hash_combine(hash_value, hasher(*PP));
16174ba319b5SDimitry Andric ++PP;
16184ba319b5SDimitry Andric }
16194ba319b5SDimitry Andric return hash_value;
16204ba319b5SDimitry Andric }
16214ba319b5SDimitry Andric
16224ba319b5SDimitry Andric ////////////////////////////////////////////////////////////////////////////
16234ba319b5SDimitry Andric // path.itr
begin() const16244ba319b5SDimitry Andric path::iterator path::begin() const {
16254ba319b5SDimitry Andric auto PP = PathParser::CreateBegin(__pn_);
16264ba319b5SDimitry Andric iterator it;
16274ba319b5SDimitry Andric it.__path_ptr_ = this;
16284ba319b5SDimitry Andric it.__state_ = static_cast<path::iterator::_ParserState>(PP.State);
16294ba319b5SDimitry Andric it.__entry_ = PP.RawEntry;
16304ba319b5SDimitry Andric it.__stashed_elem_.__assign_view(*PP);
16314ba319b5SDimitry Andric return it;
16324ba319b5SDimitry Andric }
16334ba319b5SDimitry Andric
end() const16344ba319b5SDimitry Andric path::iterator path::end() const {
16354ba319b5SDimitry Andric iterator it{};
16364ba319b5SDimitry Andric it.__state_ = path::iterator::_AtEnd;
16374ba319b5SDimitry Andric it.__path_ptr_ = this;
16384ba319b5SDimitry Andric return it;
16394ba319b5SDimitry Andric }
16404ba319b5SDimitry Andric
__increment()16414ba319b5SDimitry Andric path::iterator& path::iterator::__increment() {
16424ba319b5SDimitry Andric PathParser PP(__path_ptr_->native(), __entry_, __state_);
16434ba319b5SDimitry Andric ++PP;
16444ba319b5SDimitry Andric __state_ = static_cast<_ParserState>(PP.State);
16454ba319b5SDimitry Andric __entry_ = PP.RawEntry;
16464ba319b5SDimitry Andric __stashed_elem_.__assign_view(*PP);
16474ba319b5SDimitry Andric return *this;
16484ba319b5SDimitry Andric }
16494ba319b5SDimitry Andric
__decrement()16504ba319b5SDimitry Andric path::iterator& path::iterator::__decrement() {
16514ba319b5SDimitry Andric PathParser PP(__path_ptr_->native(), __entry_, __state_);
16524ba319b5SDimitry Andric --PP;
16534ba319b5SDimitry Andric __state_ = static_cast<_ParserState>(PP.State);
16544ba319b5SDimitry Andric __entry_ = PP.RawEntry;
16554ba319b5SDimitry Andric __stashed_elem_.__assign_view(*PP);
16564ba319b5SDimitry Andric return *this;
16574ba319b5SDimitry Andric }
16584ba319b5SDimitry Andric
16594ba319b5SDimitry Andric ///////////////////////////////////////////////////////////////////////////////
16604ba319b5SDimitry Andric // directory entry definitions
16614ba319b5SDimitry Andric ///////////////////////////////////////////////////////////////////////////////
16624ba319b5SDimitry Andric
16634ba319b5SDimitry Andric #ifndef _LIBCPP_WIN32API
__do_refresh()16644ba319b5SDimitry Andric error_code directory_entry::__do_refresh() noexcept {
16654ba319b5SDimitry Andric __data_.__reset();
16664ba319b5SDimitry Andric error_code failure_ec;
16674ba319b5SDimitry Andric
16684ba319b5SDimitry Andric StatT full_st;
16694ba319b5SDimitry Andric file_status st = detail::posix_lstat(__p_, full_st, &failure_ec);
16704ba319b5SDimitry Andric if (!status_known(st)) {
16714ba319b5SDimitry Andric __data_.__reset();
16724ba319b5SDimitry Andric return failure_ec;
16734ba319b5SDimitry Andric }
16744ba319b5SDimitry Andric
16754ba319b5SDimitry Andric if (!_VSTD_FS::exists(st) || !_VSTD_FS::is_symlink(st)) {
16764ba319b5SDimitry Andric __data_.__cache_type_ = directory_entry::_RefreshNonSymlink;
16774ba319b5SDimitry Andric __data_.__type_ = st.type();
16784ba319b5SDimitry Andric __data_.__non_sym_perms_ = st.permissions();
16794ba319b5SDimitry Andric } else { // we have a symlink
16804ba319b5SDimitry Andric __data_.__sym_perms_ = st.permissions();
16814ba319b5SDimitry Andric // Get the information about the linked entity.
16824ba319b5SDimitry Andric // Ignore errors from stat, since we don't want errors regarding symlink
16834ba319b5SDimitry Andric // resolution to be reported to the user.
16844ba319b5SDimitry Andric error_code ignored_ec;
16854ba319b5SDimitry Andric st = detail::posix_stat(__p_, full_st, &ignored_ec);
16864ba319b5SDimitry Andric
16874ba319b5SDimitry Andric __data_.__type_ = st.type();
16884ba319b5SDimitry Andric __data_.__non_sym_perms_ = st.permissions();
16894ba319b5SDimitry Andric
16904ba319b5SDimitry Andric // If we failed to resolve the link, then only partially populate the
16914ba319b5SDimitry Andric // cache.
16924ba319b5SDimitry Andric if (!status_known(st)) {
16934ba319b5SDimitry Andric __data_.__cache_type_ = directory_entry::_RefreshSymlinkUnresolved;
16944ba319b5SDimitry Andric return error_code{};
16954ba319b5SDimitry Andric }
16964ba319b5SDimitry Andric // Otherwise, we resolved the link, potentially as not existing.
16974ba319b5SDimitry Andric // That's OK.
16984ba319b5SDimitry Andric __data_.__cache_type_ = directory_entry::_RefreshSymlink;
16994ba319b5SDimitry Andric }
17004ba319b5SDimitry Andric
17014ba319b5SDimitry Andric if (_VSTD_FS::is_regular_file(st))
17024ba319b5SDimitry Andric __data_.__size_ = static_cast<uintmax_t>(full_st.st_size);
17034ba319b5SDimitry Andric
17044ba319b5SDimitry Andric if (_VSTD_FS::exists(st)) {
17054ba319b5SDimitry Andric __data_.__nlink_ = static_cast<uintmax_t>(full_st.st_nlink);
17064ba319b5SDimitry Andric
17074ba319b5SDimitry Andric // Attempt to extract the mtime, and fail if it's not representable using
17084ba319b5SDimitry Andric // file_time_type. For now we ignore the error, as we'll report it when
17094ba319b5SDimitry Andric // the value is actually used.
17104ba319b5SDimitry Andric error_code ignored_ec;
17114ba319b5SDimitry Andric __data_.__write_time_ =
17124ba319b5SDimitry Andric __extract_last_write_time(__p_, full_st, &ignored_ec);
17134ba319b5SDimitry Andric }
17144ba319b5SDimitry Andric
17154ba319b5SDimitry Andric return failure_ec;
17164ba319b5SDimitry Andric }
17174ba319b5SDimitry Andric #else
__do_refresh()17184ba319b5SDimitry Andric error_code directory_entry::__do_refresh() noexcept {
17194ba319b5SDimitry Andric __data_.__reset();
17204ba319b5SDimitry Andric error_code failure_ec;
17214ba319b5SDimitry Andric
17224ba319b5SDimitry Andric file_status st = _VSTD_FS::symlink_status(__p_, failure_ec);
17234ba319b5SDimitry Andric if (!status_known(st)) {
17244ba319b5SDimitry Andric __data_.__reset();
17254ba319b5SDimitry Andric return failure_ec;
17264ba319b5SDimitry Andric }
17274ba319b5SDimitry Andric
17284ba319b5SDimitry Andric if (!_VSTD_FS::exists(st) || !_VSTD_FS::is_symlink(st)) {
17294ba319b5SDimitry Andric __data_.__cache_type_ = directory_entry::_RefreshNonSymlink;
17304ba319b5SDimitry Andric __data_.__type_ = st.type();
17314ba319b5SDimitry Andric __data_.__non_sym_perms_ = st.permissions();
17324ba319b5SDimitry Andric } else { // we have a symlink
17334ba319b5SDimitry Andric __data_.__sym_perms_ = st.permissions();
17344ba319b5SDimitry Andric // Get the information about the linked entity.
17354ba319b5SDimitry Andric // Ignore errors from stat, since we don't want errors regarding symlink
17364ba319b5SDimitry Andric // resolution to be reported to the user.
17374ba319b5SDimitry Andric error_code ignored_ec;
17384ba319b5SDimitry Andric st = _VSTD_FS::status(__p_, ignored_ec);
17394ba319b5SDimitry Andric
17404ba319b5SDimitry Andric __data_.__type_ = st.type();
17414ba319b5SDimitry Andric __data_.__non_sym_perms_ = st.permissions();
17424ba319b5SDimitry Andric
17434ba319b5SDimitry Andric // If we failed to resolve the link, then only partially populate the
17444ba319b5SDimitry Andric // cache.
17454ba319b5SDimitry Andric if (!status_known(st)) {
17464ba319b5SDimitry Andric __data_.__cache_type_ = directory_entry::_RefreshSymlinkUnresolved;
17474ba319b5SDimitry Andric return error_code{};
17484ba319b5SDimitry Andric }
17494ba319b5SDimitry Andric __data_.__cache_type_ = directory_entry::_RefreshSymlink;
17504ba319b5SDimitry Andric }
17514ba319b5SDimitry Andric
17524ba319b5SDimitry Andric // FIXME: This is currently broken, and the implementation only a placeholder.
17534ba319b5SDimitry Andric // We need to cache last_write_time, file_size, and hard_link_count here before
17544ba319b5SDimitry Andric // the implementation actually works.
17554ba319b5SDimitry Andric
17564ba319b5SDimitry Andric return failure_ec;
17574ba319b5SDimitry Andric }
17584ba319b5SDimitry Andric #endif
17594ba319b5SDimitry Andric
17604ba319b5SDimitry Andric _LIBCPP_END_NAMESPACE_FILESYSTEM
1761