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