Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1 | //===--------------------- filesystem/ops.cpp -----------------------------===// |
| 2 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 9 | #include "filesystem" |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 10 | #include "array" |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 11 | #include "iterator" |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 12 | #include "string_view" |
| 13 | #include "type_traits" |
| 14 | #include "vector" |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 15 | #include "cstdlib" |
| 16 | #include "climits" |
| 17 | |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 18 | #include "filesystem_common.h" |
Eric Fiselier | 42d6d2c | 2017-07-08 04:18:41 +0000 | [diff] [blame] | 19 | |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/statvfs.h> |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 23 | #include <time.h> |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 24 | #include <fcntl.h> /* values for fchmodat */ |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 25 | |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 26 | #if __has_include(<sys/sendfile.h>) |
| 27 | # include <sys/sendfile.h> |
| 28 | # define _LIBCPP_FILESYSTEM_USE_SENDFILE |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 29 | #elif defined(__APPLE__) || __has_include(<copyfile.h>) |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 30 | # include <copyfile.h> |
| 31 | # define _LIBCPP_FILESYSTEM_USE_COPYFILE |
| 32 | #else |
| 33 | # include "fstream" |
| 34 | # define _LIBCPP_FILESYSTEM_USE_FSTREAM |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 35 | #endif |
Nico Weber | 4f1d63a | 2018-02-06 19:17:41 +0000 | [diff] [blame] | 36 | |
Louis Dionne | 678dc85 | 2020-02-12 17:01:19 +0100 | [diff] [blame] | 37 | #if !defined(CLOCK_REALTIME) |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 38 | # include <sys/time.h> // for gettimeofday and timeval |
| 39 | #endif |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 40 | |
Michał Górny | 8d676fb | 2019-12-02 11:49:20 +0100 | [diff] [blame] | 41 | #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB) |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 42 | # pragma comment(lib, "rt") |
Eric Fiselier | d8b25e3 | 2018-07-23 03:06:57 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 45 | _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 46 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 47 | namespace { |
| 48 | namespace parser { |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 49 | |
| 50 | using string_view_t = path::__string_view; |
| 51 | using string_view_pair = pair<string_view_t, string_view_t>; |
| 52 | using PosPtr = path::value_type const*; |
| 53 | |
| 54 | struct PathParser { |
| 55 | enum ParserState : unsigned char { |
| 56 | // Zero is a special sentinel value used by default constructed iterators. |
Eric Fiselier | 23a120c | 2018-07-25 03:31:48 +0000 | [diff] [blame] | 57 | PS_BeforeBegin = path::iterator::_BeforeBegin, |
| 58 | PS_InRootName = path::iterator::_InRootName, |
| 59 | PS_InRootDir = path::iterator::_InRootDir, |
| 60 | PS_InFilenames = path::iterator::_InFilenames, |
| 61 | PS_InTrailingSep = path::iterator::_InTrailingSep, |
| 62 | PS_AtEnd = path::iterator::_AtEnd |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | const string_view_t Path; |
| 66 | string_view_t RawEntry; |
| 67 | ParserState State; |
| 68 | |
| 69 | private: |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 70 | PathParser(string_view_t P, ParserState State) noexcept : Path(P), |
| 71 | State(State) {} |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 72 | |
| 73 | public: |
| 74 | PathParser(string_view_t P, string_view_t E, unsigned char S) |
| 75 | : Path(P), RawEntry(E), State(static_cast<ParserState>(S)) { |
| 76 | // S cannot be '0' or PS_BeforeBegin. |
| 77 | } |
| 78 | |
| 79 | static PathParser CreateBegin(string_view_t P) noexcept { |
| 80 | PathParser PP(P, PS_BeforeBegin); |
| 81 | PP.increment(); |
| 82 | return PP; |
| 83 | } |
| 84 | |
| 85 | static PathParser CreateEnd(string_view_t P) noexcept { |
| 86 | PathParser PP(P, PS_AtEnd); |
| 87 | return PP; |
| 88 | } |
| 89 | |
| 90 | PosPtr peek() const noexcept { |
| 91 | auto TkEnd = getNextTokenStartPos(); |
| 92 | auto End = getAfterBack(); |
| 93 | return TkEnd == End ? nullptr : TkEnd; |
| 94 | } |
| 95 | |
| 96 | void increment() noexcept { |
| 97 | const PosPtr End = getAfterBack(); |
| 98 | const PosPtr Start = getNextTokenStartPos(); |
| 99 | if (Start == End) |
| 100 | return makeState(PS_AtEnd); |
| 101 | |
| 102 | switch (State) { |
| 103 | case PS_BeforeBegin: { |
| 104 | PosPtr TkEnd = consumeSeparator(Start, End); |
| 105 | if (TkEnd) |
| 106 | return makeState(PS_InRootDir, Start, TkEnd); |
| 107 | else |
| 108 | return makeState(PS_InFilenames, Start, consumeName(Start, End)); |
| 109 | } |
| 110 | case PS_InRootDir: |
| 111 | return makeState(PS_InFilenames, Start, consumeName(Start, End)); |
| 112 | |
| 113 | case PS_InFilenames: { |
| 114 | PosPtr SepEnd = consumeSeparator(Start, End); |
| 115 | if (SepEnd != End) { |
| 116 | PosPtr TkEnd = consumeName(SepEnd, End); |
| 117 | if (TkEnd) |
| 118 | return makeState(PS_InFilenames, SepEnd, TkEnd); |
| 119 | } |
| 120 | return makeState(PS_InTrailingSep, Start, SepEnd); |
| 121 | } |
| 122 | |
| 123 | case PS_InTrailingSep: |
| 124 | return makeState(PS_AtEnd); |
| 125 | |
| 126 | case PS_InRootName: |
| 127 | case PS_AtEnd: |
| 128 | _LIBCPP_UNREACHABLE(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void decrement() noexcept { |
| 133 | const PosPtr REnd = getBeforeFront(); |
| 134 | const PosPtr RStart = getCurrentTokenStartPos() - 1; |
| 135 | if (RStart == REnd) // we're decrementing the begin |
| 136 | return makeState(PS_BeforeBegin); |
| 137 | |
| 138 | switch (State) { |
| 139 | case PS_AtEnd: { |
| 140 | // Try to consume a trailing separator or root directory first. |
| 141 | if (PosPtr SepEnd = consumeSeparator(RStart, REnd)) { |
| 142 | if (SepEnd == REnd) |
| 143 | return makeState(PS_InRootDir, Path.data(), RStart + 1); |
| 144 | return makeState(PS_InTrailingSep, SepEnd + 1, RStart + 1); |
| 145 | } else { |
| 146 | PosPtr TkStart = consumeName(RStart, REnd); |
| 147 | return makeState(PS_InFilenames, TkStart + 1, RStart + 1); |
| 148 | } |
| 149 | } |
| 150 | case PS_InTrailingSep: |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 151 | return makeState(PS_InFilenames, consumeName(RStart, REnd) + 1, |
| 152 | RStart + 1); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 153 | case PS_InFilenames: { |
| 154 | PosPtr SepEnd = consumeSeparator(RStart, REnd); |
| 155 | if (SepEnd == REnd) |
| 156 | return makeState(PS_InRootDir, Path.data(), RStart + 1); |
| 157 | PosPtr TkEnd = consumeName(SepEnd, REnd); |
| 158 | return makeState(PS_InFilenames, TkEnd + 1, SepEnd + 1); |
| 159 | } |
| 160 | case PS_InRootDir: |
| 161 | // return makeState(PS_InRootName, Path.data(), RStart + 1); |
| 162 | case PS_InRootName: |
| 163 | case PS_BeforeBegin: |
| 164 | _LIBCPP_UNREACHABLE(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /// \brief Return a view with the "preferred representation" of the current |
| 169 | /// element. For example trailing separators are represented as a '.' |
| 170 | string_view_t operator*() const noexcept { |
| 171 | switch (State) { |
| 172 | case PS_BeforeBegin: |
| 173 | case PS_AtEnd: |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 174 | return PS(""); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 175 | case PS_InRootDir: |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 176 | if (RawEntry[0] == '\\') |
| 177 | return PS("\\"); |
| 178 | else |
| 179 | return PS("/"); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 180 | case PS_InTrailingSep: |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 181 | return PS(""); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 182 | case PS_InRootName: |
| 183 | case PS_InFilenames: |
| 184 | return RawEntry; |
| 185 | } |
| 186 | _LIBCPP_UNREACHABLE(); |
| 187 | } |
| 188 | |
| 189 | explicit operator bool() const noexcept { |
| 190 | return State != PS_BeforeBegin && State != PS_AtEnd; |
| 191 | } |
| 192 | |
| 193 | PathParser& operator++() noexcept { |
| 194 | increment(); |
| 195 | return *this; |
| 196 | } |
| 197 | |
| 198 | PathParser& operator--() noexcept { |
| 199 | decrement(); |
| 200 | return *this; |
| 201 | } |
| 202 | |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 203 | bool atEnd() const noexcept { |
| 204 | return State == PS_AtEnd; |
| 205 | } |
| 206 | |
| 207 | bool inRootDir() const noexcept { |
| 208 | return State == PS_InRootDir; |
| 209 | } |
| 210 | |
| 211 | bool inRootName() const noexcept { |
| 212 | return State == PS_InRootName; |
| 213 | } |
| 214 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 215 | bool inRootPath() const noexcept { |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 216 | return inRootName() || inRootDir(); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | private: |
| 220 | void makeState(ParserState NewState, PosPtr Start, PosPtr End) noexcept { |
| 221 | State = NewState; |
| 222 | RawEntry = string_view_t(Start, End - Start); |
| 223 | } |
| 224 | void makeState(ParserState NewState) noexcept { |
| 225 | State = NewState; |
| 226 | RawEntry = {}; |
| 227 | } |
| 228 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 229 | PosPtr getAfterBack() const noexcept { return Path.data() + Path.size(); } |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 230 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 231 | PosPtr getBeforeFront() const noexcept { return Path.data() - 1; } |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 232 | |
| 233 | /// \brief Return a pointer to the first character after the currently |
| 234 | /// lexed element. |
| 235 | PosPtr getNextTokenStartPos() const noexcept { |
| 236 | switch (State) { |
| 237 | case PS_BeforeBegin: |
| 238 | return Path.data(); |
| 239 | case PS_InRootName: |
| 240 | case PS_InRootDir: |
| 241 | case PS_InFilenames: |
| 242 | return &RawEntry.back() + 1; |
| 243 | case PS_InTrailingSep: |
| 244 | case PS_AtEnd: |
| 245 | return getAfterBack(); |
| 246 | } |
| 247 | _LIBCPP_UNREACHABLE(); |
| 248 | } |
| 249 | |
| 250 | /// \brief Return a pointer to the first character in the currently lexed |
| 251 | /// element. |
| 252 | PosPtr getCurrentTokenStartPos() const noexcept { |
| 253 | switch (State) { |
| 254 | case PS_BeforeBegin: |
| 255 | case PS_InRootName: |
| 256 | return &Path.front(); |
| 257 | case PS_InRootDir: |
| 258 | case PS_InFilenames: |
| 259 | case PS_InTrailingSep: |
| 260 | return &RawEntry.front(); |
| 261 | case PS_AtEnd: |
| 262 | return &Path.back() + 1; |
| 263 | } |
| 264 | _LIBCPP_UNREACHABLE(); |
| 265 | } |
| 266 | |
| 267 | PosPtr consumeSeparator(PosPtr P, PosPtr End) const noexcept { |
| 268 | if (P == End || *P != '/') |
| 269 | return nullptr; |
| 270 | const int Inc = P < End ? 1 : -1; |
| 271 | P += Inc; |
| 272 | while (P != End && *P == '/') |
| 273 | P += Inc; |
| 274 | return P; |
| 275 | } |
| 276 | |
| 277 | PosPtr consumeName(PosPtr P, PosPtr End) const noexcept { |
| 278 | if (P == End || *P == '/') |
| 279 | return nullptr; |
| 280 | const int Inc = P < End ? 1 : -1; |
| 281 | P += Inc; |
| 282 | while (P != End && *P != '/') |
| 283 | P += Inc; |
| 284 | return P; |
| 285 | } |
| 286 | }; |
| 287 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 288 | string_view_pair separate_filename(string_view_t const& s) { |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 289 | if (s == PS(".") || s == PS("..") || s.empty()) |
| 290 | return string_view_pair{s, PS("")}; |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 291 | auto pos = s.find_last_of('.'); |
| 292 | if (pos == string_view_t::npos || pos == 0) |
| 293 | return string_view_pair{s, string_view_t{}}; |
| 294 | return string_view_pair{s.substr(0, pos), s.substr(pos)}; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | string_view_t createView(PosPtr S, PosPtr E) noexcept { |
| 298 | return {S, static_cast<size_t>(E - S) + 1}; |
| 299 | } |
| 300 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 301 | } // namespace parser |
| 302 | } // namespace |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 303 | |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 304 | // POSIX HELPERS |
| 305 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 306 | namespace detail { |
| 307 | namespace { |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 308 | |
| 309 | using value_type = path::value_type; |
| 310 | using string_type = path::string_type; |
| 311 | |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 312 | struct FileDescriptor { |
| 313 | const path& name; |
| 314 | int fd = -1; |
| 315 | StatT m_stat; |
| 316 | file_status m_status; |
| 317 | |
| 318 | template <class... Args> |
| 319 | static FileDescriptor create(const path* p, error_code& ec, Args... args) { |
| 320 | ec.clear(); |
| 321 | int fd; |
| 322 | if ((fd = ::open(p->c_str(), args...)) == -1) { |
| 323 | ec = capture_errno(); |
| 324 | return FileDescriptor{p}; |
| 325 | } |
| 326 | return FileDescriptor(p, fd); |
| 327 | } |
| 328 | |
| 329 | template <class... Args> |
| 330 | static FileDescriptor create_with_status(const path* p, error_code& ec, |
| 331 | Args... args) { |
| 332 | FileDescriptor fd = create(p, ec, args...); |
| 333 | if (!ec) |
| 334 | fd.refresh_status(ec); |
| 335 | |
| 336 | return fd; |
| 337 | } |
| 338 | |
| 339 | file_status get_status() const { return m_status; } |
| 340 | StatT const& get_stat() const { return m_stat; } |
| 341 | |
| 342 | bool status_known() const { return _VSTD_FS::status_known(m_status); } |
| 343 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 344 | file_status refresh_status(error_code& ec); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 345 | |
| 346 | void close() noexcept { |
| 347 | if (fd != -1) |
| 348 | ::close(fd); |
| 349 | fd = -1; |
| 350 | } |
| 351 | |
| 352 | FileDescriptor(FileDescriptor&& other) |
| 353 | : name(other.name), fd(other.fd), m_stat(other.m_stat), |
| 354 | m_status(other.m_status) { |
| 355 | other.fd = -1; |
| 356 | other.m_status = file_status{}; |
| 357 | } |
| 358 | |
| 359 | ~FileDescriptor() { close(); } |
| 360 | |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 361 | FileDescriptor(FileDescriptor const&) = delete; |
| 362 | FileDescriptor& operator=(FileDescriptor const&) = delete; |
| 363 | |
| 364 | private: |
| 365 | explicit FileDescriptor(const path* p, int fd = -1) : name(*p), fd(fd) {} |
| 366 | }; |
| 367 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 368 | perms posix_get_perms(const StatT& st) noexcept { |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 369 | return static_cast<perms>(st.st_mode) & perms::mask; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | ::mode_t posix_convert_perms(perms prms) { |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 373 | return static_cast< ::mode_t>(prms & perms::mask); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 376 | file_status create_file_status(error_code& m_ec, path const& p, |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 377 | const StatT& path_stat, error_code* ec) { |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 378 | if (ec) |
| 379 | *ec = m_ec; |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 380 | if (m_ec && (m_ec.value() == ENOENT || m_ec.value() == ENOTDIR)) { |
| 381 | return file_status(file_type::not_found); |
| 382 | } else if (m_ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 383 | ErrorHandler<void> err("posix_stat", ec, &p); |
| 384 | err.report(m_ec, "failed to determine attributes for the specified path"); |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 385 | return file_status(file_type::none); |
| 386 | } |
| 387 | // else |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 388 | |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 389 | file_status fs_tmp; |
| 390 | auto const mode = path_stat.st_mode; |
| 391 | if (S_ISLNK(mode)) |
| 392 | fs_tmp.type(file_type::symlink); |
| 393 | else if (S_ISREG(mode)) |
| 394 | fs_tmp.type(file_type::regular); |
| 395 | else if (S_ISDIR(mode)) |
| 396 | fs_tmp.type(file_type::directory); |
| 397 | else if (S_ISBLK(mode)) |
| 398 | fs_tmp.type(file_type::block); |
| 399 | else if (S_ISCHR(mode)) |
| 400 | fs_tmp.type(file_type::character); |
| 401 | else if (S_ISFIFO(mode)) |
| 402 | fs_tmp.type(file_type::fifo); |
| 403 | else if (S_ISSOCK(mode)) |
| 404 | fs_tmp.type(file_type::socket); |
| 405 | else |
| 406 | fs_tmp.type(file_type::unknown); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 407 | |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 408 | fs_tmp.permissions(detail::posix_get_perms(path_stat)); |
| 409 | return fs_tmp; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 412 | file_status posix_stat(path const& p, StatT& path_stat, error_code* ec) { |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 413 | error_code m_ec; |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 414 | if (::stat(p.c_str(), &path_stat) == -1) |
| 415 | m_ec = detail::capture_errno(); |
| 416 | return create_file_status(m_ec, p, path_stat, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 419 | file_status posix_stat(path const& p, error_code* ec) { |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 420 | StatT path_stat; |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 421 | return posix_stat(p, path_stat, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 424 | file_status posix_lstat(path const& p, StatT& path_stat, error_code* ec) { |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 425 | error_code m_ec; |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 426 | if (::lstat(p.c_str(), &path_stat) == -1) |
| 427 | m_ec = detail::capture_errno(); |
| 428 | return create_file_status(m_ec, p, path_stat, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 431 | file_status posix_lstat(path const& p, error_code* ec) { |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 432 | StatT path_stat; |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 433 | return posix_lstat(p, path_stat, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Dan Albert | 39b981d | 2019-01-15 19:16:25 +0000 | [diff] [blame] | 436 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html |
| 437 | bool posix_ftruncate(const FileDescriptor& fd, off_t to_size, error_code& ec) { |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 438 | if (::ftruncate(fd.fd, to_size) == -1) { |
| 439 | ec = capture_errno(); |
Eric Fiselier | f1aba0d | 2018-07-26 04:02:06 +0000 | [diff] [blame] | 440 | return true; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 441 | } |
| 442 | ec.clear(); |
Eric Fiselier | f1aba0d | 2018-07-26 04:02:06 +0000 | [diff] [blame] | 443 | return false; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | bool posix_fchmod(const FileDescriptor& fd, const StatT& st, error_code& ec) { |
| 447 | if (::fchmod(fd.fd, st.st_mode) == -1) { |
| 448 | ec = capture_errno(); |
Eric Fiselier | f1aba0d | 2018-07-26 04:02:06 +0000 | [diff] [blame] | 449 | return true; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 450 | } |
| 451 | ec.clear(); |
Eric Fiselier | f1aba0d | 2018-07-26 04:02:06 +0000 | [diff] [blame] | 452 | return false; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | bool stat_equivalent(const StatT& st1, const StatT& st2) { |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 456 | return (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 459 | file_status FileDescriptor::refresh_status(error_code& ec) { |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 460 | // FD must be open and good. |
| 461 | m_status = file_status{}; |
Eric Fiselier | d8b25e3 | 2018-07-23 03:06:57 +0000 | [diff] [blame] | 462 | m_stat = {}; |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 463 | error_code m_ec; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 464 | if (::fstat(fd, &m_stat) == -1) |
| 465 | m_ec = capture_errno(); |
| 466 | m_status = create_file_status(m_ec, name, m_stat, &ec); |
| 467 | return m_status; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 468 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 469 | } // namespace |
| 470 | } // end namespace detail |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 471 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 472 | using detail::capture_errno; |
| 473 | using detail::ErrorHandler; |
| 474 | using detail::StatT; |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 475 | using detail::TimeSpec; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 476 | using parser::createView; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 477 | using parser::PathParser; |
| 478 | using parser::string_view_t; |
| 479 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 480 | const bool _FilesystemClock::is_steady; |
| 481 | |
| 482 | _FilesystemClock::time_point _FilesystemClock::now() noexcept { |
| 483 | typedef chrono::duration<rep> __secs; |
Louis Dionne | 678dc85 | 2020-02-12 17:01:19 +0100 | [diff] [blame] | 484 | #if defined(CLOCK_REALTIME) |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 485 | typedef chrono::duration<rep, nano> __nsecs; |
| 486 | struct timespec tp; |
| 487 | if (0 != clock_gettime(CLOCK_REALTIME, &tp)) |
| 488 | __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed"); |
| 489 | return time_point(__secs(tp.tv_sec) + |
| 490 | chrono::duration_cast<duration>(__nsecs(tp.tv_nsec))); |
| 491 | #else |
| 492 | typedef chrono::duration<rep, micro> __microsecs; |
| 493 | timeval tv; |
| 494 | gettimeofday(&tv, 0); |
| 495 | return time_point(__secs(tv.tv_sec) + __microsecs(tv.tv_usec)); |
Louis Dionne | 678dc85 | 2020-02-12 17:01:19 +0100 | [diff] [blame] | 496 | #endif // CLOCK_REALTIME |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | filesystem_error::~filesystem_error() {} |
| 500 | |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 501 | #if defined(_LIBCPP_WIN32API) |
| 502 | #define PS_FMT "%ls" |
| 503 | #else |
| 504 | #define PS_FMT "%s" |
| 505 | #endif |
| 506 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 507 | void filesystem_error::__create_what(int __num_paths) { |
| 508 | const char* derived_what = system_error::what(); |
| 509 | __storage_->__what_ = [&]() -> string { |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 510 | const path::value_type* p1 = path1().native().empty() ? PS("\"\"") : path1().c_str(); |
| 511 | const path::value_type* p2 = path2().native().empty() ? PS("\"\"") : path2().c_str(); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 512 | switch (__num_paths) { |
| 513 | default: |
| 514 | return detail::format_string("filesystem error: %s", derived_what); |
| 515 | case 1: |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 516 | return detail::format_string("filesystem error: %s [" PS_FMT "]", derived_what, |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 517 | p1); |
| 518 | case 2: |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 519 | return detail::format_string("filesystem error: %s [" PS_FMT "] [" PS_FMT "]", |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 520 | derived_what, p1, p2); |
| 521 | } |
| 522 | }(); |
| 523 | } |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 524 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 525 | static path __do_absolute(const path& p, path* cwd, error_code* ec) { |
| 526 | if (ec) |
| 527 | ec->clear(); |
| 528 | if (p.is_absolute()) |
| 529 | return p; |
| 530 | *cwd = __current_path(ec); |
| 531 | if (ec && *ec) |
| 532 | return {}; |
| 533 | return (*cwd) / p; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 536 | path __absolute(const path& p, error_code* ec) { |
| 537 | path cwd; |
| 538 | return __do_absolute(p, &cwd, ec); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 541 | path __canonical(path const& orig_p, error_code* ec) { |
| 542 | path cwd; |
| 543 | ErrorHandler<path> err("canonical", ec, &orig_p, &cwd); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 544 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 545 | path p = __do_absolute(orig_p, &cwd, ec); |
YAMAMOTO Takashi | 43f1908 | 2020-10-28 15:40:16 -0400 | [diff] [blame] | 546 | #if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112 |
Eric Fiselier | b521530 | 2019-01-17 02:59:28 +0000 | [diff] [blame] | 547 | std::unique_ptr<char, decltype(&::free)> |
| 548 | hold(::realpath(p.c_str(), nullptr), &::free); |
| 549 | if (hold.get() == nullptr) |
| 550 | return err.report(capture_errno()); |
| 551 | return {hold.get()}; |
| 552 | #else |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 553 | char buff[PATH_MAX + 1]; |
| 554 | char* ret; |
| 555 | if ((ret = ::realpath(p.c_str(), buff)) == nullptr) |
| 556 | return err.report(capture_errno()); |
| 557 | return {ret}; |
Eric Fiselier | b521530 | 2019-01-17 02:59:28 +0000 | [diff] [blame] | 558 | #endif |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | void __copy(const path& from, const path& to, copy_options options, |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 562 | error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 563 | ErrorHandler<void> err("copy", ec, &from, &to); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 564 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 565 | const bool sym_status = bool( |
| 566 | options & (copy_options::create_symlinks | copy_options::skip_symlinks)); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 567 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 568 | const bool sym_status2 = bool(options & copy_options::copy_symlinks); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 569 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 570 | error_code m_ec1; |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 571 | StatT f_st = {}; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 572 | const file_status f = sym_status || sym_status2 |
| 573 | ? detail::posix_lstat(from, f_st, &m_ec1) |
| 574 | : detail::posix_stat(from, f_st, &m_ec1); |
| 575 | if (m_ec1) |
| 576 | return err.report(m_ec1); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 577 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 578 | StatT t_st = {}; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 579 | const file_status t = sym_status ? detail::posix_lstat(to, t_st, &m_ec1) |
| 580 | : detail::posix_stat(to, t_st, &m_ec1); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 581 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 582 | if (not status_known(t)) |
| 583 | return err.report(m_ec1); |
| 584 | |
| 585 | if (!exists(f) || is_other(f) || is_other(t) || |
| 586 | (is_directory(f) && is_regular_file(t)) || |
| 587 | detail::stat_equivalent(f_st, t_st)) { |
| 588 | return err.report(errc::function_not_supported); |
| 589 | } |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 590 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 591 | if (ec) |
| 592 | ec->clear(); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 593 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 594 | if (is_symlink(f)) { |
| 595 | if (bool(copy_options::skip_symlinks & options)) { |
| 596 | // do nothing |
| 597 | } else if (not exists(t)) { |
| 598 | __copy_symlink(from, to, ec); |
| 599 | } else { |
| 600 | return err.report(errc::file_exists); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 601 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 602 | return; |
| 603 | } else if (is_regular_file(f)) { |
| 604 | if (bool(copy_options::directories_only & options)) { |
| 605 | // do nothing |
| 606 | } else if (bool(copy_options::create_symlinks & options)) { |
| 607 | __create_symlink(from, to, ec); |
| 608 | } else if (bool(copy_options::create_hard_links & options)) { |
| 609 | __create_hard_link(from, to, ec); |
| 610 | } else if (is_directory(t)) { |
| 611 | __copy_file(from, to / from.filename(), options, ec); |
| 612 | } else { |
| 613 | __copy_file(from, to, options, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 614 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 615 | return; |
| 616 | } else if (is_directory(f) && bool(copy_options::create_symlinks & options)) { |
| 617 | return err.report(errc::is_a_directory); |
| 618 | } else if (is_directory(f) && (bool(copy_options::recursive & options) || |
| 619 | copy_options::none == options)) { |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 620 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 621 | if (!exists(t)) { |
| 622 | // create directory to with attributes from 'from'. |
| 623 | __create_directory(to, from, ec); |
| 624 | if (ec && *ec) { |
| 625 | return; |
| 626 | } |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 627 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 628 | directory_iterator it = |
| 629 | ec ? directory_iterator(from, *ec) : directory_iterator(from); |
| 630 | if (ec && *ec) { |
| 631 | return; |
| 632 | } |
| 633 | error_code m_ec2; |
| 634 | for (; it != directory_iterator(); it.increment(m_ec2)) { |
| 635 | if (m_ec2) { |
| 636 | return err.report(m_ec2); |
| 637 | } |
| 638 | __copy(it->path(), to / it->path().filename(), |
| 639 | options | copy_options::__in_recursive_copy, ec); |
| 640 | if (ec && *ec) { |
| 641 | return; |
| 642 | } |
| 643 | } |
| 644 | } |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 647 | namespace detail { |
| 648 | namespace { |
| 649 | |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 650 | #if defined(_LIBCPP_FILESYSTEM_USE_SENDFILE) |
| 651 | bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) { |
| 652 | size_t count = read_fd.get_stat().st_size; |
| 653 | do { |
| 654 | ssize_t res; |
| 655 | if ((res = ::sendfile(write_fd.fd, read_fd.fd, nullptr, count)) == -1) { |
| 656 | ec = capture_errno(); |
| 657 | return false; |
| 658 | } |
| 659 | count -= res; |
| 660 | } while (count > 0); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 661 | |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 662 | ec.clear(); |
| 663 | |
| 664 | return true; |
| 665 | } |
| 666 | #elif defined(_LIBCPP_FILESYSTEM_USE_COPYFILE) |
| 667 | bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) { |
| 668 | struct CopyFileState { |
| 669 | copyfile_state_t state; |
| 670 | CopyFileState() { state = copyfile_state_alloc(); } |
| 671 | ~CopyFileState() { copyfile_state_free(state); } |
| 672 | |
| 673 | private: |
| 674 | CopyFileState(CopyFileState const&) = delete; |
| 675 | CopyFileState& operator=(CopyFileState const&) = delete; |
| 676 | }; |
| 677 | |
| 678 | CopyFileState cfs; |
| 679 | if (fcopyfile(read_fd.fd, write_fd.fd, cfs.state, COPYFILE_DATA) < 0) { |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 680 | ec = capture_errno(); |
| 681 | return false; |
| 682 | } |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 683 | |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 684 | ec.clear(); |
| 685 | return true; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 686 | } |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 687 | #elif defined(_LIBCPP_FILESYSTEM_USE_FSTREAM) |
| 688 | bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) { |
| 689 | ifstream in; |
| 690 | in.__open(read_fd.fd, ios::binary); |
| 691 | if (!in.is_open()) { |
| 692 | // This assumes that __open didn't reset the error code. |
| 693 | ec = capture_errno(); |
| 694 | return false; |
| 695 | } |
Martin Storsjö | 6410435 | 2020-11-02 10:19:42 +0200 | [diff] [blame] | 696 | read_fd.fd = -1; |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 697 | ofstream out; |
| 698 | out.__open(write_fd.fd, ios::binary); |
| 699 | if (!out.is_open()) { |
| 700 | ec = capture_errno(); |
| 701 | return false; |
| 702 | } |
Martin Storsjö | 6410435 | 2020-11-02 10:19:42 +0200 | [diff] [blame] | 703 | write_fd.fd = -1; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 704 | |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 705 | if (in.good() && out.good()) { |
| 706 | using InIt = istreambuf_iterator<char>; |
| 707 | using OutIt = ostreambuf_iterator<char>; |
| 708 | InIt bin(in); |
| 709 | InIt ein; |
| 710 | OutIt bout(out); |
| 711 | copy(bin, ein, bout); |
| 712 | } |
| 713 | if (out.fail() || in.fail()) { |
| 714 | ec = make_error_code(errc::io_error); |
| 715 | return false; |
| 716 | } |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 717 | |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 718 | ec.clear(); |
| 719 | return true; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 720 | } |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 721 | #else |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 722 | # error "Unknown implementation for copy_file_impl" |
| 723 | #endif // copy_file_impl implementation |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 724 | |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 725 | } // end anonymous namespace |
| 726 | } // end namespace detail |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 727 | |
| 728 | bool __copy_file(const path& from, const path& to, copy_options options, |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 729 | error_code* ec) { |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 730 | using detail::FileDescriptor; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 731 | ErrorHandler<bool> err("copy_file", ec, &to, &from); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 732 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 733 | error_code m_ec; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 734 | FileDescriptor from_fd = |
| 735 | FileDescriptor::create_with_status(&from, m_ec, O_RDONLY | O_NONBLOCK); |
| 736 | if (m_ec) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 737 | return err.report(m_ec); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 738 | |
| 739 | auto from_st = from_fd.get_status(); |
| 740 | StatT const& from_stat = from_fd.get_stat(); |
| 741 | if (!is_regular_file(from_st)) { |
| 742 | if (not m_ec) |
| 743 | m_ec = make_error_code(errc::not_supported); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 744 | return err.report(m_ec); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | const bool skip_existing = bool(copy_options::skip_existing & options); |
| 748 | const bool update_existing = bool(copy_options::update_existing & options); |
| 749 | const bool overwrite_existing = |
| 750 | bool(copy_options::overwrite_existing & options); |
| 751 | |
| 752 | StatT to_stat_path; |
| 753 | file_status to_st = detail::posix_stat(to, to_stat_path, &m_ec); |
| 754 | if (!status_known(to_st)) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 755 | return err.report(m_ec); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 756 | |
| 757 | const bool to_exists = exists(to_st); |
| 758 | if (to_exists && !is_regular_file(to_st)) |
Eric Fiselier | 268fa83 | 2018-07-23 11:55:13 +0000 | [diff] [blame] | 759 | return err.report(errc::not_supported); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 760 | |
| 761 | if (to_exists && detail::stat_equivalent(from_stat, to_stat_path)) |
Eric Fiselier | 268fa83 | 2018-07-23 11:55:13 +0000 | [diff] [blame] | 762 | return err.report(errc::file_exists); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 763 | |
| 764 | if (to_exists && skip_existing) |
| 765 | return false; |
| 766 | |
Eric Fiselier | 455ac4b | 2018-07-22 21:15:15 +0000 | [diff] [blame] | 767 | bool ShouldCopy = [&]() { |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 768 | if (to_exists && update_existing) { |
| 769 | auto from_time = detail::extract_mtime(from_stat); |
| 770 | auto to_time = detail::extract_mtime(to_stat_path); |
| 771 | if (from_time.tv_sec < to_time.tv_sec) |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 772 | return false; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 773 | if (from_time.tv_sec == to_time.tv_sec && |
| 774 | from_time.tv_nsec <= to_time.tv_nsec) |
Eric Fiselier | e735925 | 2016-10-16 00:47:59 +0000 | [diff] [blame] | 775 | return false; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 776 | return true; |
Eric Fiselier | e735925 | 2016-10-16 00:47:59 +0000 | [diff] [blame] | 777 | } |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 778 | if (!to_exists || overwrite_existing) |
| 779 | return true; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 780 | return err.report(errc::file_exists); |
Eric Fiselier | 455ac4b | 2018-07-22 21:15:15 +0000 | [diff] [blame] | 781 | }(); |
| 782 | if (!ShouldCopy) |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 783 | return false; |
Eric Fiselier | e735925 | 2016-10-16 00:47:59 +0000 | [diff] [blame] | 784 | |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 785 | // Don't truncate right away. We may not be opening the file we originally |
| 786 | // looked at; we'll check this later. |
Eric Fiselier | 455ac4b | 2018-07-22 21:15:15 +0000 | [diff] [blame] | 787 | int to_open_flags = O_WRONLY; |
| 788 | if (!to_exists) |
| 789 | to_open_flags |= O_CREAT; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 790 | FileDescriptor to_fd = FileDescriptor::create_with_status( |
| 791 | &to, m_ec, to_open_flags, from_stat.st_mode); |
| 792 | if (m_ec) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 793 | return err.report(m_ec); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 794 | |
| 795 | if (to_exists) { |
| 796 | // Check that the file we initially stat'ed is equivalent to the one |
| 797 | // we opened. |
Eric Fiselier | 455ac4b | 2018-07-22 21:15:15 +0000 | [diff] [blame] | 798 | // FIXME: report this better. |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 799 | if (!detail::stat_equivalent(to_stat_path, to_fd.get_stat())) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 800 | return err.report(errc::bad_file_descriptor); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 801 | |
| 802 | // Set the permissions and truncate the file we opened. |
Eric Fiselier | f1aba0d | 2018-07-26 04:02:06 +0000 | [diff] [blame] | 803 | if (detail::posix_fchmod(to_fd, from_stat, m_ec)) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 804 | return err.report(m_ec); |
Eric Fiselier | f1aba0d | 2018-07-26 04:02:06 +0000 | [diff] [blame] | 805 | if (detail::posix_ftruncate(to_fd, 0, m_ec)) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 806 | return err.report(m_ec); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | if (!copy_file_impl(from_fd, to_fd, m_ec)) { |
| 810 | // FIXME: Remove the dest file if we failed, and it didn't exist previously. |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 811 | return err.report(m_ec); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | return true; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | void __copy_symlink(const path& existing_symlink, const path& new_symlink, |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 818 | error_code* ec) { |
| 819 | const path real_path(__read_symlink(existing_symlink, ec)); |
| 820 | if (ec && *ec) { |
| 821 | return; |
| 822 | } |
| 823 | // NOTE: proposal says you should detect if you should call |
| 824 | // create_symlink or create_directory_symlink. I don't think this |
| 825 | // is needed with POSIX |
| 826 | __create_symlink(real_path, new_symlink, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 829 | bool __create_directories(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 830 | ErrorHandler<bool> err("create_directories", ec, &p); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 831 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 832 | error_code m_ec; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 833 | auto const st = detail::posix_stat(p, &m_ec); |
| 834 | if (!status_known(st)) |
| 835 | return err.report(m_ec); |
| 836 | else if (is_directory(st)) |
| 837 | return false; |
| 838 | else if (exists(st)) |
| 839 | return err.report(errc::file_exists); |
| 840 | |
| 841 | const path parent = p.parent_path(); |
| 842 | if (!parent.empty()) { |
| 843 | const file_status parent_st = status(parent, m_ec); |
| 844 | if (not status_known(parent_st)) |
| 845 | return err.report(m_ec); |
| 846 | if (not exists(parent_st)) { |
| 847 | __create_directories(parent, ec); |
| 848 | if (ec && *ec) { |
| 849 | return false; |
| 850 | } |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 851 | } |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 852 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 853 | return __create_directory(p, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 856 | bool __create_directory(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 857 | ErrorHandler<bool> err("create_directory", ec, &p); |
| 858 | |
| 859 | if (::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0) |
| 860 | return true; |
Marek Kurdej | 9c12977 | 2020-12-10 08:38:41 +0100 | [diff] [blame] | 861 | |
| 862 | if (errno == EEXIST) { |
| 863 | error_code mec = capture_errno(); |
| 864 | error_code ignored_ec; |
| 865 | const file_status st = status(p, ignored_ec); |
| 866 | if (!is_directory(st)) { |
| 867 | err.report(mec); |
| 868 | } |
| 869 | } else { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 870 | err.report(capture_errno()); |
Marek Kurdej | 9c12977 | 2020-12-10 08:38:41 +0100 | [diff] [blame] | 871 | } |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 872 | return false; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 875 | bool __create_directory(path const& p, path const& attributes, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 876 | ErrorHandler<bool> err("create_directory", ec, &p, &attributes); |
| 877 | |
| 878 | StatT attr_stat; |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 879 | error_code mec; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 880 | auto st = detail::posix_stat(attributes, attr_stat, &mec); |
| 881 | if (!status_known(st)) |
| 882 | return err.report(mec); |
Eric Fiselier | 7ca3db8 | 2018-07-25 04:46:32 +0000 | [diff] [blame] | 883 | if (!is_directory(st)) |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 884 | return err.report(errc::not_a_directory, |
| 885 | "the specified attribute path is invalid"); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 886 | |
| 887 | if (::mkdir(p.c_str(), attr_stat.st_mode) == 0) |
| 888 | return true; |
Marek Kurdej | 9c12977 | 2020-12-10 08:38:41 +0100 | [diff] [blame] | 889 | |
| 890 | if (errno == EEXIST) { |
| 891 | error_code mec = capture_errno(); |
| 892 | error_code ignored_ec; |
| 893 | const file_status st = status(p, ignored_ec); |
| 894 | if (!is_directory(st)) { |
| 895 | err.report(mec); |
| 896 | } |
| 897 | } else { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 898 | err.report(capture_errno()); |
Marek Kurdej | 9c12977 | 2020-12-10 08:38:41 +0100 | [diff] [blame] | 899 | } |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 900 | return false; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 903 | void __create_directory_symlink(path const& from, path const& to, |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 904 | error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 905 | ErrorHandler<void> err("create_directory_symlink", ec, &from, &to); |
| 906 | if (::symlink(from.c_str(), to.c_str()) != 0) |
| 907 | return err.report(capture_errno()); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 910 | void __create_hard_link(const path& from, const path& to, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 911 | ErrorHandler<void> err("create_hard_link", ec, &from, &to); |
| 912 | if (::link(from.c_str(), to.c_str()) == -1) |
| 913 | return err.report(capture_errno()); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 916 | void __create_symlink(path const& from, path const& to, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 917 | ErrorHandler<void> err("create_symlink", ec, &from, &to); |
| 918 | if (::symlink(from.c_str(), to.c_str()) == -1) |
| 919 | return err.report(capture_errno()); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 922 | path __current_path(error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 923 | ErrorHandler<path> err("current_path", ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 924 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 925 | auto size = ::pathconf(".", _PC_PATH_MAX); |
| 926 | _LIBCPP_ASSERT(size >= 0, "pathconf returned a 0 as max size"); |
| 927 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 928 | auto buff = unique_ptr<char[]>(new char[size + 1]); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 929 | char* ret; |
| 930 | if ((ret = ::getcwd(buff.get(), static_cast<size_t>(size))) == nullptr) |
| 931 | return err.report(capture_errno(), "call to getcwd failed"); |
| 932 | |
| 933 | return {buff.get()}; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 936 | void __current_path(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 937 | ErrorHandler<void> err("current_path", ec, &p); |
| 938 | if (::chdir(p.c_str()) == -1) |
| 939 | err.report(capture_errno()); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 942 | bool __equivalent(const path& p1, const path& p2, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 943 | ErrorHandler<bool> err("equivalent", ec, &p1, &p2); |
| 944 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 945 | error_code ec1, ec2; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 946 | StatT st1 = {}, st2 = {}; |
| 947 | auto s1 = detail::posix_stat(p1.native(), st1, &ec1); |
| 948 | if (!exists(s1)) |
| 949 | return err.report(errc::not_supported); |
| 950 | auto s2 = detail::posix_stat(p2.native(), st2, &ec2); |
| 951 | if (!exists(s2)) |
| 952 | return err.report(errc::not_supported); |
| 953 | |
| 954 | return detail::stat_equivalent(st1, st2); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 957 | uintmax_t __file_size(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 958 | ErrorHandler<uintmax_t> err("file_size", ec, &p); |
| 959 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 960 | error_code m_ec; |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 961 | StatT st; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 962 | file_status fst = detail::posix_stat(p, st, &m_ec); |
| 963 | if (!exists(fst) || !is_regular_file(fst)) { |
| 964 | errc error_kind = |
| 965 | is_directory(fst) ? errc::is_a_directory : errc::not_supported; |
| 966 | if (!m_ec) |
| 967 | m_ec = make_error_code(error_kind); |
| 968 | return err.report(m_ec); |
| 969 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 970 | // is_regular_file(p) == true |
| 971 | return static_cast<uintmax_t>(st.st_size); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 974 | uintmax_t __hard_link_count(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 975 | ErrorHandler<uintmax_t> err("hard_link_count", ec, &p); |
| 976 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 977 | error_code m_ec; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 978 | StatT st; |
| 979 | detail::posix_stat(p, st, &m_ec); |
| 980 | if (m_ec) |
| 981 | return err.report(m_ec); |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 982 | return static_cast<uintmax_t>(st.st_nlink); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 985 | bool __fs_is_empty(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 986 | ErrorHandler<bool> err("is_empty", ec, &p); |
Eric Fiselier | aa8c61f | 2016-10-15 23:05:04 +0000 | [diff] [blame] | 987 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 988 | error_code m_ec; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 989 | StatT pst; |
| 990 | auto st = detail::posix_stat(p, pst, &m_ec); |
| 991 | if (m_ec) |
| 992 | return err.report(m_ec); |
| 993 | else if (!is_directory(st) && !is_regular_file(st)) |
| 994 | return err.report(errc::not_supported); |
| 995 | else if (is_directory(st)) { |
| 996 | auto it = ec ? directory_iterator(p, *ec) : directory_iterator(p); |
| 997 | if (ec && *ec) |
| 998 | return false; |
| 999 | return it == directory_iterator{}; |
| 1000 | } else if (is_regular_file(st)) |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1001 | return static_cast<uintmax_t>(pst.st_size) == 0; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1002 | |
| 1003 | _LIBCPP_UNREACHABLE(); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1006 | static file_time_type __extract_last_write_time(const path& p, const StatT& st, |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 1007 | error_code* ec) { |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 1008 | using detail::fs_time; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1009 | ErrorHandler<file_time_type> err("last_write_time", ec, &p); |
| 1010 | |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 1011 | auto ts = detail::extract_mtime(st); |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 1012 | if (!fs_time::is_representable(ts)) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1013 | return err.report(errc::value_too_large); |
| 1014 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 1015 | return fs_time::convert_from_timespec(ts); |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 1016 | } |
Eric Fiselier | 42d6d2c | 2017-07-08 04:18:41 +0000 | [diff] [blame] | 1017 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1018 | file_time_type __last_write_time(const path& p, error_code* ec) { |
| 1019 | using namespace chrono; |
| 1020 | ErrorHandler<file_time_type> err("last_write_time", ec, &p); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1021 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1022 | error_code m_ec; |
| 1023 | StatT st; |
| 1024 | detail::posix_stat(p, st, &m_ec); |
| 1025 | if (m_ec) |
| 1026 | return err.report(m_ec); |
| 1027 | return __extract_last_write_time(p, st, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1030 | void __last_write_time(const path& p, file_time_type new_time, error_code* ec) { |
| 1031 | using detail::fs_time; |
| 1032 | ErrorHandler<void> err("last_write_time", ec, &p); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1033 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1034 | error_code m_ec; |
| 1035 | array<TimeSpec, 2> tbuf; |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 1036 | #if !defined(_LIBCPP_USE_UTIMENSAT) |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1037 | // This implementation has a race condition between determining the |
| 1038 | // last access time and attempting to set it to the same value using |
| 1039 | // ::utimes |
| 1040 | StatT st; |
| 1041 | file_status fst = detail::posix_stat(p, st, &m_ec); |
| 1042 | if (m_ec) |
| 1043 | return err.report(m_ec); |
| 1044 | tbuf[0] = detail::extract_atime(st); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1045 | #else |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1046 | tbuf[0].tv_sec = 0; |
| 1047 | tbuf[0].tv_nsec = UTIME_OMIT; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1048 | #endif |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1049 | if (!fs_time::convert_to_timespec(tbuf[1], new_time)) |
| 1050 | return err.report(errc::value_too_large); |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 1051 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1052 | detail::set_file_times(p, tbuf, m_ec); |
| 1053 | if (m_ec) |
| 1054 | return err.report(m_ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Eric Fiselier | 4f3dc0e | 2018-03-26 06:23:55 +0000 | [diff] [blame] | 1057 | void __permissions(const path& p, perms prms, perm_options opts, |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1058 | error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1059 | ErrorHandler<void> err("permissions", ec, &p); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1060 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1061 | auto has_opt = [&](perm_options o) { return bool(o & opts); }; |
| 1062 | const bool resolve_symlinks = !has_opt(perm_options::nofollow); |
| 1063 | const bool add_perms = has_opt(perm_options::add); |
| 1064 | const bool remove_perms = has_opt(perm_options::remove); |
| 1065 | _LIBCPP_ASSERT( |
| 1066 | (add_perms + remove_perms + has_opt(perm_options::replace)) == 1, |
| 1067 | "One and only one of the perm_options constants replace, add, or remove " |
| 1068 | "is present in opts"); |
| 1069 | |
| 1070 | bool set_sym_perms = false; |
| 1071 | prms &= perms::mask; |
| 1072 | if (!resolve_symlinks || (add_perms || remove_perms)) { |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1073 | error_code m_ec; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1074 | file_status st = resolve_symlinks ? detail::posix_stat(p, &m_ec) |
| 1075 | : detail::posix_lstat(p, &m_ec); |
| 1076 | set_sym_perms = is_symlink(st); |
| 1077 | if (m_ec) |
| 1078 | return err.report(m_ec); |
| 1079 | _LIBCPP_ASSERT(st.permissions() != perms::unknown, |
| 1080 | "Permissions unexpectedly unknown"); |
| 1081 | if (add_perms) |
| 1082 | prms |= st.permissions(); |
| 1083 | else if (remove_perms) |
| 1084 | prms = st.permissions() & ~prms; |
| 1085 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1086 | const auto real_perms = detail::posix_convert_perms(prms); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1087 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1088 | #if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD) |
| 1089 | const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0; |
| 1090 | if (::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) { |
| 1091 | return err.report(capture_errno()); |
| 1092 | } |
| 1093 | #else |
| 1094 | if (set_sym_perms) |
| 1095 | return err.report(errc::operation_not_supported); |
| 1096 | if (::chmod(p.c_str(), real_perms) == -1) { |
| 1097 | return err.report(capture_errno()); |
| 1098 | } |
| 1099 | #endif |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1102 | path __read_symlink(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1103 | ErrorHandler<path> err("read_symlink", ec, &p); |
| 1104 | |
Eric Fiselier | b521530 | 2019-01-17 02:59:28 +0000 | [diff] [blame] | 1105 | #ifdef PATH_MAX |
| 1106 | struct NullDeleter { void operator()(void*) const {} }; |
| 1107 | const size_t size = PATH_MAX + 1; |
| 1108 | char stack_buff[size]; |
| 1109 | auto buff = std::unique_ptr<char[], NullDeleter>(stack_buff); |
| 1110 | #else |
| 1111 | StatT sb; |
| 1112 | if (::lstat(p.c_str(), &sb) == -1) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1113 | return err.report(capture_errno()); |
| 1114 | } |
Eric Fiselier | b521530 | 2019-01-17 02:59:28 +0000 | [diff] [blame] | 1115 | const size_t size = sb.st_size + 1; |
| 1116 | auto buff = unique_ptr<char[]>(new char[size]); |
| 1117 | #endif |
| 1118 | ::ssize_t ret; |
| 1119 | if ((ret = ::readlink(p.c_str(), buff.get(), size)) == -1) |
| 1120 | return err.report(capture_errno()); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1121 | _LIBCPP_ASSERT(ret > 0, "TODO"); |
Eric Fiselier | b521530 | 2019-01-17 02:59:28 +0000 | [diff] [blame] | 1122 | if (static_cast<size_t>(ret) >= size) |
| 1123 | return err.report(errc::value_too_large); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1124 | buff[ret] = 0; |
Eric Fiselier | b521530 | 2019-01-17 02:59:28 +0000 | [diff] [blame] | 1125 | return {buff.get()}; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1128 | bool __remove(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1129 | ErrorHandler<bool> err("remove", ec, &p); |
| 1130 | if (::remove(p.c_str()) == -1) { |
| 1131 | if (errno != ENOENT) |
| 1132 | err.report(capture_errno()); |
| 1133 | return false; |
| 1134 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1135 | return true; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | namespace { |
| 1139 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1140 | uintmax_t remove_all_impl(path const& p, error_code& ec) { |
| 1141 | const auto npos = static_cast<uintmax_t>(-1); |
| 1142 | const file_status st = __symlink_status(p, &ec); |
| 1143 | if (ec) |
| 1144 | return npos; |
| 1145 | uintmax_t count = 1; |
| 1146 | if (is_directory(st)) { |
| 1147 | for (directory_iterator it(p, ec); !ec && it != directory_iterator(); |
| 1148 | it.increment(ec)) { |
| 1149 | auto other_count = remove_all_impl(it->path(), ec); |
| 1150 | if (ec) |
| 1151 | return npos; |
| 1152 | count += other_count; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1153 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1154 | if (ec) |
| 1155 | return npos; |
| 1156 | } |
| 1157 | if (!__remove(p, &ec)) |
| 1158 | return npos; |
| 1159 | return count; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | } // end namespace |
| 1163 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1164 | uintmax_t __remove_all(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1165 | ErrorHandler<uintmax_t> err("remove_all", ec, &p); |
Ekaterina Vaartis | 52668f7 | 2018-01-11 17:04:29 +0000 | [diff] [blame] | 1166 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1167 | error_code mec; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1168 | auto count = remove_all_impl(p, mec); |
| 1169 | if (mec) { |
| 1170 | if (mec == errc::no_such_file_or_directory) |
| 1171 | return 0; |
| 1172 | return err.report(mec); |
| 1173 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1174 | return count; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1177 | void __rename(const path& from, const path& to, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1178 | ErrorHandler<void> err("rename", ec, &from, &to); |
| 1179 | if (::rename(from.c_str(), to.c_str()) == -1) |
| 1180 | err.report(capture_errno()); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1183 | void __resize_file(const path& p, uintmax_t size, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1184 | ErrorHandler<void> err("resize_file", ec, &p); |
| 1185 | if (::truncate(p.c_str(), static_cast< ::off_t>(size)) == -1) |
| 1186 | return err.report(capture_errno()); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1189 | space_info __space(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1190 | ErrorHandler<void> err("space", ec, &p); |
| 1191 | space_info si; |
| 1192 | struct statvfs m_svfs = {}; |
| 1193 | if (::statvfs(p.c_str(), &m_svfs) == -1) { |
| 1194 | err.report(capture_errno()); |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1195 | si.capacity = si.free = si.available = static_cast<uintmax_t>(-1); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1196 | return si; |
| 1197 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1198 | // Multiply with overflow checking. |
| 1199 | auto do_mult = [&](uintmax_t& out, uintmax_t other) { |
| 1200 | out = other * m_svfs.f_frsize; |
| 1201 | if (other == 0 || out / other != m_svfs.f_frsize) |
| 1202 | out = static_cast<uintmax_t>(-1); |
| 1203 | }; |
| 1204 | do_mult(si.capacity, m_svfs.f_blocks); |
| 1205 | do_mult(si.free, m_svfs.f_bfree); |
| 1206 | do_mult(si.available, m_svfs.f_bavail); |
| 1207 | return si; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1210 | file_status __status(const path& p, error_code* ec) { |
| 1211 | return detail::posix_stat(p, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1214 | file_status __symlink_status(const path& p, error_code* ec) { |
| 1215 | return detail::posix_lstat(p, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1218 | path __temp_directory_path(error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1219 | ErrorHandler<path> err("temp_directory_path", ec); |
| 1220 | |
Saleem Abdulrasool | cf279a5 | 2017-02-05 17:21:52 +0000 | [diff] [blame] | 1221 | const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"}; |
| 1222 | const char* ret = nullptr; |
| 1223 | |
| 1224 | for (auto& ep : env_paths) |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1225 | if ((ret = getenv(ep))) |
Saleem Abdulrasool | cf279a5 | 2017-02-05 17:21:52 +0000 | [diff] [blame] | 1226 | break; |
| 1227 | if (ret == nullptr) |
| 1228 | ret = "/tmp"; |
| 1229 | |
| 1230 | path p(ret); |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1231 | error_code m_ec; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1232 | file_status st = detail::posix_stat(p, &m_ec); |
| 1233 | if (!status_known(st)) |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 1234 | return err.report(m_ec, "cannot access path \"" PS_FMT "\"", p); |
Saleem Abdulrasool | cf279a5 | 2017-02-05 17:21:52 +0000 | [diff] [blame] | 1235 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1236 | if (!exists(st) || !is_directory(st)) |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 1237 | return err.report(errc::not_a_directory, "path \"" PS_FMT "\" is not a directory", |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1238 | p); |
| 1239 | |
Saleem Abdulrasool | cf279a5 | 2017-02-05 17:21:52 +0000 | [diff] [blame] | 1240 | return p; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1243 | path __weakly_canonical(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1244 | ErrorHandler<path> err("weakly_canonical", ec, &p); |
| 1245 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1246 | if (p.empty()) |
| 1247 | return __canonical("", ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1248 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1249 | path result; |
| 1250 | path tmp; |
| 1251 | tmp.__reserve(p.native().size()); |
| 1252 | auto PP = PathParser::CreateEnd(p.native()); |
| 1253 | --PP; |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1254 | vector<string_view_t> DNEParts; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1255 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1256 | while (PP.State != PathParser::PS_BeforeBegin) { |
| 1257 | tmp.assign(createView(p.native().data(), &PP.RawEntry.back())); |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1258 | error_code m_ec; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1259 | file_status st = __status(tmp, &m_ec); |
| 1260 | if (!status_known(st)) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1261 | return err.report(m_ec); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1262 | } else if (exists(st)) { |
| 1263 | result = __canonical(tmp, ec); |
| 1264 | break; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1265 | } |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1266 | DNEParts.push_back(*PP); |
| 1267 | --PP; |
| 1268 | } |
| 1269 | if (PP.State == PathParser::PS_BeforeBegin) |
| 1270 | result = __canonical("", ec); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1271 | if (ec) |
| 1272 | ec->clear(); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1273 | if (DNEParts.empty()) |
| 1274 | return result; |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1275 | for (auto It = DNEParts.rbegin(); It != DNEParts.rend(); ++It) |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1276 | result /= *It; |
| 1277 | return result.lexically_normal(); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1280 | /////////////////////////////////////////////////////////////////////////////// |
| 1281 | // path definitions |
| 1282 | /////////////////////////////////////////////////////////////////////////////// |
| 1283 | |
| 1284 | constexpr path::value_type path::preferred_separator; |
| 1285 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1286 | path& path::replace_extension(path const& replacement) { |
| 1287 | path p = extension(); |
| 1288 | if (not p.empty()) { |
| 1289 | __pn_.erase(__pn_.size() - p.native().size()); |
| 1290 | } |
| 1291 | if (!replacement.empty()) { |
| 1292 | if (replacement.native()[0] != '.') { |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 1293 | __pn_ += PS("."); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1294 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1295 | __pn_.append(replacement.__pn_); |
| 1296 | } |
| 1297 | return *this; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | /////////////////////////////////////////////////////////////////////////////// |
| 1301 | // path.decompose |
| 1302 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1303 | string_view_t path::__root_name() const { |
| 1304 | auto PP = PathParser::CreateBegin(__pn_); |
| 1305 | if (PP.State == PathParser::PS_InRootName) |
| 1306 | return *PP; |
| 1307 | return {}; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1310 | string_view_t path::__root_directory() const { |
| 1311 | auto PP = PathParser::CreateBegin(__pn_); |
| 1312 | if (PP.State == PathParser::PS_InRootName) |
| 1313 | ++PP; |
| 1314 | if (PP.State == PathParser::PS_InRootDir) |
| 1315 | return *PP; |
| 1316 | return {}; |
| 1317 | } |
| 1318 | |
| 1319 | string_view_t path::__root_path_raw() const { |
| 1320 | auto PP = PathParser::CreateBegin(__pn_); |
| 1321 | if (PP.State == PathParser::PS_InRootName) { |
| 1322 | auto NextCh = PP.peek(); |
| 1323 | if (NextCh && *NextCh == '/') { |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1324 | ++PP; |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1325 | return createView(__pn_.data(), &PP.RawEntry.back()); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1326 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1327 | return PP.RawEntry; |
| 1328 | } |
| 1329 | if (PP.State == PathParser::PS_InRootDir) |
| 1330 | return *PP; |
| 1331 | return {}; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 1334 | static bool ConsumeRootName(PathParser *PP) { |
| 1335 | static_assert(PathParser::PS_BeforeBegin == 1 && |
| 1336 | PathParser::PS_InRootName == 2, |
| 1337 | "Values for enums are incorrect"); |
| 1338 | while (PP->State <= PathParser::PS_InRootName) |
| 1339 | ++(*PP); |
| 1340 | return PP->State == PathParser::PS_AtEnd; |
| 1341 | } |
| 1342 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1343 | static bool ConsumeRootDir(PathParser* PP) { |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 1344 | static_assert(PathParser::PS_BeforeBegin == 1 && |
| 1345 | PathParser::PS_InRootName == 2 && |
| 1346 | PathParser::PS_InRootDir == 3, "Values for enums are incorrect"); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1347 | while (PP->State <= PathParser::PS_InRootDir) |
| 1348 | ++(*PP); |
| 1349 | return PP->State == PathParser::PS_AtEnd; |
| 1350 | } |
| 1351 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1352 | string_view_t path::__relative_path() const { |
| 1353 | auto PP = PathParser::CreateBegin(__pn_); |
| 1354 | if (ConsumeRootDir(&PP)) |
| 1355 | return {}; |
| 1356 | return createView(PP.RawEntry.data(), &__pn_.back()); |
| 1357 | } |
| 1358 | |
| 1359 | string_view_t path::__parent_path() const { |
| 1360 | if (empty()) |
| 1361 | return {}; |
| 1362 | // Determine if we have a root path but not a relative path. In that case |
| 1363 | // return *this. |
| 1364 | { |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1365 | auto PP = PathParser::CreateBegin(__pn_); |
| 1366 | if (ConsumeRootDir(&PP)) |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1367 | return __pn_; |
| 1368 | } |
| 1369 | // Otherwise remove a single element from the end of the path, and return |
| 1370 | // a string representing that path |
| 1371 | { |
| 1372 | auto PP = PathParser::CreateEnd(__pn_); |
| 1373 | --PP; |
| 1374 | if (PP.RawEntry.data() == __pn_.data()) |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1375 | return {}; |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1376 | --PP; |
| 1377 | return createView(__pn_.data(), &PP.RawEntry.back()); |
| 1378 | } |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1381 | string_view_t path::__filename() const { |
| 1382 | if (empty()) |
| 1383 | return {}; |
| 1384 | { |
| 1385 | PathParser PP = PathParser::CreateBegin(__pn_); |
| 1386 | if (ConsumeRootDir(&PP)) |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1387 | return {}; |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1388 | } |
| 1389 | return *(--PathParser::CreateEnd(__pn_)); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1392 | string_view_t path::__stem() const { |
| 1393 | return parser::separate_filename(__filename()).first; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1396 | string_view_t path::__extension() const { |
| 1397 | return parser::separate_filename(__filename()).second; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | //////////////////////////////////////////////////////////////////////////// |
| 1401 | // path.gen |
| 1402 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1403 | enum PathPartKind : unsigned char { |
| 1404 | PK_None, |
| 1405 | PK_RootSep, |
| 1406 | PK_Filename, |
| 1407 | PK_Dot, |
| 1408 | PK_DotDot, |
| 1409 | PK_TrailingSep |
| 1410 | }; |
| 1411 | |
| 1412 | static PathPartKind ClassifyPathPart(string_view_t Part) { |
| 1413 | if (Part.empty()) |
| 1414 | return PK_TrailingSep; |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 1415 | if (Part == PS(".")) |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1416 | return PK_Dot; |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 1417 | if (Part == PS("..")) |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1418 | return PK_DotDot; |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 1419 | if (Part == PS("/")) |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1420 | return PK_RootSep; |
| 1421 | return PK_Filename; |
| 1422 | } |
| 1423 | |
| 1424 | path path::lexically_normal() const { |
| 1425 | if (__pn_.empty()) |
| 1426 | return *this; |
| 1427 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1428 | using PartKindPair = pair<string_view_t, PathPartKind>; |
| 1429 | vector<PartKindPair> Parts; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1430 | // Guess as to how many elements the path has to avoid reallocating. |
| 1431 | Parts.reserve(32); |
| 1432 | |
| 1433 | // Track the total size of the parts as we collect them. This allows the |
| 1434 | // resulting path to reserve the correct amount of memory. |
| 1435 | size_t NewPathSize = 0; |
| 1436 | auto AddPart = [&](PathPartKind K, string_view_t P) { |
| 1437 | NewPathSize += P.size(); |
| 1438 | Parts.emplace_back(P, K); |
| 1439 | }; |
| 1440 | auto LastPartKind = [&]() { |
| 1441 | if (Parts.empty()) |
| 1442 | return PK_None; |
| 1443 | return Parts.back().second; |
| 1444 | }; |
| 1445 | |
| 1446 | bool MaybeNeedTrailingSep = false; |
| 1447 | // Build a stack containing the remaining elements of the path, popping off |
| 1448 | // elements which occur before a '..' entry. |
| 1449 | for (auto PP = PathParser::CreateBegin(__pn_); PP; ++PP) { |
| 1450 | auto Part = *PP; |
| 1451 | PathPartKind Kind = ClassifyPathPart(Part); |
| 1452 | switch (Kind) { |
| 1453 | case PK_Filename: |
| 1454 | case PK_RootSep: { |
| 1455 | // Add all non-dot and non-dot-dot elements to the stack of elements. |
| 1456 | AddPart(Kind, Part); |
| 1457 | MaybeNeedTrailingSep = false; |
| 1458 | break; |
| 1459 | } |
| 1460 | case PK_DotDot: { |
| 1461 | // Only push a ".." element if there are no elements preceding the "..", |
| 1462 | // or if the preceding element is itself "..". |
| 1463 | auto LastKind = LastPartKind(); |
| 1464 | if (LastKind == PK_Filename) { |
| 1465 | NewPathSize -= Parts.back().first.size(); |
| 1466 | Parts.pop_back(); |
| 1467 | } else if (LastKind != PK_RootSep) |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 1468 | AddPart(PK_DotDot, PS("..")); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1469 | MaybeNeedTrailingSep = LastKind == PK_Filename; |
| 1470 | break; |
| 1471 | } |
| 1472 | case PK_Dot: |
| 1473 | case PK_TrailingSep: { |
| 1474 | MaybeNeedTrailingSep = true; |
| 1475 | break; |
| 1476 | } |
| 1477 | case PK_None: |
| 1478 | _LIBCPP_UNREACHABLE(); |
| 1479 | } |
| 1480 | } |
| 1481 | // [fs.path.generic]p6.8: If the path is empty, add a dot. |
| 1482 | if (Parts.empty()) |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 1483 | return PS("."); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1484 | |
| 1485 | // [fs.path.generic]p6.7: If the last filename is dot-dot, remove any |
| 1486 | // trailing directory-separator. |
| 1487 | bool NeedTrailingSep = MaybeNeedTrailingSep && LastPartKind() == PK_Filename; |
| 1488 | |
| 1489 | path Result; |
| 1490 | Result.__pn_.reserve(Parts.size() + NewPathSize + NeedTrailingSep); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1491 | for (auto& PK : Parts) |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1492 | Result /= PK.first; |
| 1493 | |
| 1494 | if (NeedTrailingSep) |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 1495 | Result /= PS(""); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1496 | |
| 1497 | return Result; |
| 1498 | } |
| 1499 | |
| 1500 | static int DetermineLexicalElementCount(PathParser PP) { |
| 1501 | int Count = 0; |
| 1502 | for (; PP; ++PP) { |
| 1503 | auto Elem = *PP; |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 1504 | if (Elem == PS("..")) |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1505 | --Count; |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 1506 | else if (Elem != PS(".") && Elem != PS("")) |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1507 | ++Count; |
| 1508 | } |
| 1509 | return Count; |
| 1510 | } |
| 1511 | |
| 1512 | path path::lexically_relative(const path& base) const { |
| 1513 | { // perform root-name/root-directory mismatch checks |
| 1514 | auto PP = PathParser::CreateBegin(__pn_); |
| 1515 | auto PPBase = PathParser::CreateBegin(base.__pn_); |
| 1516 | auto CheckIterMismatchAtBase = [&]() { |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1517 | return PP.State != PPBase.State && |
| 1518 | (PP.inRootPath() || PPBase.inRootPath()); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1519 | }; |
Eric Fiselier | 9c4949a | 2018-12-21 04:25:40 +0000 | [diff] [blame] | 1520 | if (PP.inRootName() && PPBase.inRootName()) { |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1521 | if (*PP != *PPBase) |
| 1522 | return {}; |
| 1523 | } else if (CheckIterMismatchAtBase()) |
| 1524 | return {}; |
| 1525 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1526 | if (PP.inRootPath()) |
| 1527 | ++PP; |
| 1528 | if (PPBase.inRootPath()) |
| 1529 | ++PPBase; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1530 | if (CheckIterMismatchAtBase()) |
| 1531 | return {}; |
| 1532 | } |
| 1533 | |
| 1534 | // Find the first mismatching element |
| 1535 | auto PP = PathParser::CreateBegin(__pn_); |
| 1536 | auto PPBase = PathParser::CreateBegin(base.__pn_); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1537 | while (PP && PPBase && PP.State == PPBase.State && *PP == *PPBase) { |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1538 | ++PP; |
| 1539 | ++PPBase; |
| 1540 | } |
| 1541 | |
| 1542 | // If there is no mismatch, return ".". |
| 1543 | if (!PP && !PPBase) |
| 1544 | return "."; |
| 1545 | |
| 1546 | // Otherwise, determine the number of elements, 'n', which are not dot or |
| 1547 | // dot-dot minus the number of dot-dot elements. |
| 1548 | int ElemCount = DetermineLexicalElementCount(PPBase); |
| 1549 | if (ElemCount < 0) |
| 1550 | return {}; |
| 1551 | |
Eric Fiselier | 9c4949a | 2018-12-21 04:25:40 +0000 | [diff] [blame] | 1552 | // if n == 0 and (a == end() || a->empty()), returns path("."); otherwise |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 1553 | if (ElemCount == 0 && (PP.atEnd() || *PP == PS(""))) |
| 1554 | return PS("."); |
Eric Fiselier | 9c4949a | 2018-12-21 04:25:40 +0000 | [diff] [blame] | 1555 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1556 | // return a path constructed with 'n' dot-dot elements, followed by the the |
| 1557 | // elements of '*this' after the mismatch. |
| 1558 | path Result; |
| 1559 | // FIXME: Reserve enough room in Result that it won't have to re-allocate. |
| 1560 | while (ElemCount--) |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 1561 | Result /= PS(".."); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1562 | for (; PP; ++PP) |
| 1563 | Result /= *PP; |
| 1564 | return Result; |
| 1565 | } |
| 1566 | |
| 1567 | //////////////////////////////////////////////////////////////////////////// |
| 1568 | // path.comparisons |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 1569 | static int CompareRootName(PathParser *LHS, PathParser *RHS) { |
| 1570 | if (!LHS->inRootName() && !RHS->inRootName()) |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1571 | return 0; |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 1572 | |
| 1573 | auto GetRootName = [](PathParser *Parser) -> string_view_t { |
Martin Storsjö | e482f4b | 2020-10-27 13:09:08 +0200 | [diff] [blame^] | 1574 | return Parser->inRootName() ? **Parser : PS(""); |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 1575 | }; |
| 1576 | int res = GetRootName(LHS).compare(GetRootName(RHS)); |
| 1577 | ConsumeRootName(LHS); |
| 1578 | ConsumeRootName(RHS); |
| 1579 | return res; |
| 1580 | } |
| 1581 | |
| 1582 | static int CompareRootDir(PathParser *LHS, PathParser *RHS) { |
| 1583 | if (!LHS->inRootDir() && RHS->inRootDir()) |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1584 | return -1; |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 1585 | else if (LHS->inRootDir() && !RHS->inRootDir()) |
| 1586 | return 1; |
| 1587 | else { |
| 1588 | ConsumeRootDir(LHS); |
| 1589 | ConsumeRootDir(RHS); |
| 1590 | return 0; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | static int CompareRelative(PathParser *LHSPtr, PathParser *RHSPtr) { |
| 1595 | auto &LHS = *LHSPtr; |
| 1596 | auto &RHS = *RHSPtr; |
Stephan T. Lavavej | fb39ad7 | 2019-10-23 11:45:36 -0700 | [diff] [blame] | 1597 | |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 1598 | int res; |
| 1599 | while (LHS && RHS) { |
| 1600 | if ((res = (*LHS).compare(*RHS)) != 0) |
| 1601 | return res; |
| 1602 | ++LHS; |
| 1603 | ++RHS; |
| 1604 | } |
| 1605 | return 0; |
| 1606 | } |
| 1607 | |
| 1608 | static int CompareEndState(PathParser *LHS, PathParser *RHS) { |
| 1609 | if (LHS->atEnd() && !RHS->atEnd()) |
| 1610 | return -1; |
| 1611 | else if (!LHS->atEnd() && RHS->atEnd()) |
| 1612 | return 1; |
| 1613 | return 0; |
| 1614 | } |
| 1615 | |
| 1616 | int path::__compare(string_view_t __s) const { |
| 1617 | auto LHS = PathParser::CreateBegin(__pn_); |
| 1618 | auto RHS = PathParser::CreateBegin(__s); |
| 1619 | int res; |
| 1620 | |
| 1621 | if ((res = CompareRootName(&LHS, &RHS)) != 0) |
| 1622 | return res; |
| 1623 | |
| 1624 | if ((res = CompareRootDir(&LHS, &RHS)) != 0) |
| 1625 | return res; |
| 1626 | |
| 1627 | if ((res = CompareRelative(&LHS, &RHS)) != 0) |
| 1628 | return res; |
| 1629 | |
| 1630 | return CompareEndState(&LHS, &RHS); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | //////////////////////////////////////////////////////////////////////////// |
| 1634 | // path.nonmembers |
| 1635 | size_t hash_value(const path& __p) noexcept { |
| 1636 | auto PP = PathParser::CreateBegin(__p.native()); |
| 1637 | size_t hash_value = 0; |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1638 | hash<string_view_t> hasher; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1639 | while (PP) { |
| 1640 | hash_value = __hash_combine(hash_value, hasher(*PP)); |
| 1641 | ++PP; |
| 1642 | } |
| 1643 | return hash_value; |
| 1644 | } |
| 1645 | |
| 1646 | //////////////////////////////////////////////////////////////////////////// |
| 1647 | // path.itr |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1648 | path::iterator path::begin() const { |
| 1649 | auto PP = PathParser::CreateBegin(__pn_); |
| 1650 | iterator it; |
| 1651 | it.__path_ptr_ = this; |
| 1652 | it.__state_ = static_cast<path::iterator::_ParserState>(PP.State); |
| 1653 | it.__entry_ = PP.RawEntry; |
| 1654 | it.__stashed_elem_.__assign_view(*PP); |
| 1655 | return it; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1658 | path::iterator path::end() const { |
| 1659 | iterator it{}; |
| 1660 | it.__state_ = path::iterator::_AtEnd; |
| 1661 | it.__path_ptr_ = this; |
| 1662 | return it; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | path::iterator& path::iterator::__increment() { |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1666 | PathParser PP(__path_ptr_->native(), __entry_, __state_); |
| 1667 | ++PP; |
Eric Fiselier | 23a120c | 2018-07-25 03:31:48 +0000 | [diff] [blame] | 1668 | __state_ = static_cast<_ParserState>(PP.State); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1669 | __entry_ = PP.RawEntry; |
| 1670 | __stashed_elem_.__assign_view(*PP); |
| 1671 | return *this; |
| 1672 | } |
| 1673 | |
| 1674 | path::iterator& path::iterator::__decrement() { |
| 1675 | PathParser PP(__path_ptr_->native(), __entry_, __state_); |
| 1676 | --PP; |
Eric Fiselier | 23a120c | 2018-07-25 03:31:48 +0000 | [diff] [blame] | 1677 | __state_ = static_cast<_ParserState>(PP.State); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1678 | __entry_ = PP.RawEntry; |
| 1679 | __stashed_elem_.__assign_view(*PP); |
| 1680 | return *this; |
| 1681 | } |
| 1682 | |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 1683 | /////////////////////////////////////////////////////////////////////////////// |
| 1684 | // directory entry definitions |
| 1685 | /////////////////////////////////////////////////////////////////////////////// |
| 1686 | |
| 1687 | #ifndef _LIBCPP_WIN32API |
| 1688 | error_code directory_entry::__do_refresh() noexcept { |
| 1689 | __data_.__reset(); |
| 1690 | error_code failure_ec; |
| 1691 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 1692 | StatT full_st; |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 1693 | file_status st = detail::posix_lstat(__p_, full_st, &failure_ec); |
| 1694 | if (!status_known(st)) { |
| 1695 | __data_.__reset(); |
| 1696 | return failure_ec; |
| 1697 | } |
| 1698 | |
| 1699 | if (!_VSTD_FS::exists(st) || !_VSTD_FS::is_symlink(st)) { |
| 1700 | __data_.__cache_type_ = directory_entry::_RefreshNonSymlink; |
| 1701 | __data_.__type_ = st.type(); |
| 1702 | __data_.__non_sym_perms_ = st.permissions(); |
| 1703 | } else { // we have a symlink |
| 1704 | __data_.__sym_perms_ = st.permissions(); |
| 1705 | // Get the information about the linked entity. |
| 1706 | // Ignore errors from stat, since we don't want errors regarding symlink |
| 1707 | // resolution to be reported to the user. |
| 1708 | error_code ignored_ec; |
| 1709 | st = detail::posix_stat(__p_, full_st, &ignored_ec); |
| 1710 | |
| 1711 | __data_.__type_ = st.type(); |
| 1712 | __data_.__non_sym_perms_ = st.permissions(); |
| 1713 | |
| 1714 | // If we failed to resolve the link, then only partially populate the |
| 1715 | // cache. |
| 1716 | if (!status_known(st)) { |
| 1717 | __data_.__cache_type_ = directory_entry::_RefreshSymlinkUnresolved; |
| 1718 | return error_code{}; |
| 1719 | } |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 1720 | // Otherwise, we resolved the link, potentially as not existing. |
Eric Fiselier | e39cea9 | 2018-07-20 08:36:45 +0000 | [diff] [blame] | 1721 | // That's OK. |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 1722 | __data_.__cache_type_ = directory_entry::_RefreshSymlink; |
| 1723 | } |
| 1724 | |
| 1725 | if (_VSTD_FS::is_regular_file(st)) |
| 1726 | __data_.__size_ = static_cast<uintmax_t>(full_st.st_size); |
| 1727 | |
| 1728 | if (_VSTD_FS::exists(st)) { |
| 1729 | __data_.__nlink_ = static_cast<uintmax_t>(full_st.st_nlink); |
| 1730 | |
| 1731 | // Attempt to extract the mtime, and fail if it's not representable using |
| 1732 | // file_time_type. For now we ignore the error, as we'll report it when |
| 1733 | // the value is actually used. |
| 1734 | error_code ignored_ec; |
| 1735 | __data_.__write_time_ = |
| 1736 | __extract_last_write_time(__p_, full_st, &ignored_ec); |
| 1737 | } |
| 1738 | |
| 1739 | return failure_ec; |
| 1740 | } |
| 1741 | #else |
| 1742 | error_code directory_entry::__do_refresh() noexcept { |
| 1743 | __data_.__reset(); |
| 1744 | error_code failure_ec; |
| 1745 | |
| 1746 | file_status st = _VSTD_FS::symlink_status(__p_, failure_ec); |
| 1747 | if (!status_known(st)) { |
| 1748 | __data_.__reset(); |
| 1749 | return failure_ec; |
| 1750 | } |
| 1751 | |
| 1752 | if (!_VSTD_FS::exists(st) || !_VSTD_FS::is_symlink(st)) { |
| 1753 | __data_.__cache_type_ = directory_entry::_RefreshNonSymlink; |
| 1754 | __data_.__type_ = st.type(); |
| 1755 | __data_.__non_sym_perms_ = st.permissions(); |
| 1756 | } else { // we have a symlink |
| 1757 | __data_.__sym_perms_ = st.permissions(); |
| 1758 | // Get the information about the linked entity. |
| 1759 | // Ignore errors from stat, since we don't want errors regarding symlink |
| 1760 | // resolution to be reported to the user. |
| 1761 | error_code ignored_ec; |
| 1762 | st = _VSTD_FS::status(__p_, ignored_ec); |
| 1763 | |
| 1764 | __data_.__type_ = st.type(); |
| 1765 | __data_.__non_sym_perms_ = st.permissions(); |
| 1766 | |
| 1767 | // If we failed to resolve the link, then only partially populate the |
| 1768 | // cache. |
| 1769 | if (!status_known(st)) { |
| 1770 | __data_.__cache_type_ = directory_entry::_RefreshSymlinkUnresolved; |
| 1771 | return error_code{}; |
| 1772 | } |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 1773 | __data_.__cache_type_ = directory_entry::_RefreshSymlink; |
| 1774 | } |
| 1775 | |
| 1776 | // FIXME: This is currently broken, and the implementation only a placeholder. |
| 1777 | // We need to cache last_write_time, file_size, and hard_link_count here before |
| 1778 | // the implementation actually works. |
| 1779 | |
| 1780 | return failure_ec; |
| 1781 | } |
| 1782 | #endif |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1783 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1784 | _LIBCPP_END_NAMESPACE_FILESYSTEM |