1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include <__assert> 10 #include <__config> 11 #include <__utility/unreachable.h> 12 #include <array> 13 #include <climits> 14 #include <cstdlib> 15 #include <filesystem> 16 #include <iterator> 17 #include <string_view> 18 #include <type_traits> 19 #include <vector> 20 21 #include "error.h" 22 #include "file_descriptor.h" 23 #include "path_parser.h" 24 #include "posix_compat.h" 25 #include "time_utils.h" 26 27 #if defined(_LIBCPP_WIN32API) 28 # define WIN32_LEAN_AND_MEAN 29 # define NOMINMAX 30 # include <windows.h> 31 #else 32 # include <dirent.h> 33 # include <sys/stat.h> 34 # include <sys/statvfs.h> 35 # include <unistd.h> 36 #endif 37 #include <fcntl.h> /* values for fchmodat */ 38 #include <time.h> 39 40 #if __has_include(<sys/sendfile.h>) 41 # include <sys/sendfile.h> 42 # define _LIBCPP_FILESYSTEM_USE_SENDFILE 43 #elif defined(__APPLE__) || __has_include(<copyfile.h>) 44 # include <copyfile.h> 45 # define _LIBCPP_FILESYSTEM_USE_COPYFILE 46 #else 47 # include <fstream> 48 # define _LIBCPP_FILESYSTEM_USE_FSTREAM 49 #endif 50 51 #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB) 52 # pragma comment(lib, "rt") 53 #endif 54 55 _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM 56 57 using detail::capture_errno; 58 using detail::ErrorHandler; 59 using detail::StatT; 60 using detail::TimeSpec; 61 using parser::createView; 62 using parser::PathParser; 63 using parser::string_view_t; 64 65 static path __do_absolute(const path& p, path* cwd, error_code* ec) { 66 if (ec) 67 ec->clear(); 68 if (p.is_absolute()) 69 return p; 70 *cwd = __current_path(ec); 71 if (ec && *ec) 72 return {}; 73 return (*cwd) / p; 74 } 75 76 path __absolute(const path& p, error_code* ec) { 77 path cwd; 78 return __do_absolute(p, &cwd, ec); 79 } 80 81 path __canonical(path const& orig_p, error_code* ec) { 82 path cwd; 83 ErrorHandler<path> err("canonical", ec, &orig_p, &cwd); 84 85 path p = __do_absolute(orig_p, &cwd, ec); 86 #if (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112) || defined(_LIBCPP_WIN32API) 87 std::unique_ptr<path::value_type, decltype(&::free)> hold(detail::realpath(p.c_str(), nullptr), &::free); 88 if (hold.get() == nullptr) 89 return err.report(capture_errno()); 90 return {hold.get()}; 91 #else 92 # if defined(__MVS__) && !defined(PATH_MAX) 93 path::value_type buff[_XOPEN_PATH_MAX + 1]; 94 # else 95 path::value_type buff[PATH_MAX + 1]; 96 # endif 97 path::value_type* ret; 98 if ((ret = detail::realpath(p.c_str(), buff)) == nullptr) 99 return err.report(capture_errno()); 100 return {ret}; 101 #endif 102 } 103 104 void __copy(const path& from, const path& to, copy_options options, error_code* ec) { 105 ErrorHandler<void> err("copy", ec, &from, &to); 106 107 const bool sym_status = bool(options & (copy_options::create_symlinks | copy_options::skip_symlinks)); 108 109 const bool sym_status2 = bool(options & copy_options::copy_symlinks); 110 111 error_code m_ec1; 112 StatT f_st = {}; 113 const file_status f = 114 sym_status || sym_status2 ? detail::posix_lstat(from, f_st, &m_ec1) : detail::posix_stat(from, f_st, &m_ec1); 115 if (m_ec1) 116 return err.report(m_ec1); 117 118 StatT t_st = {}; 119 const file_status t = sym_status ? detail::posix_lstat(to, t_st, &m_ec1) : detail::posix_stat(to, t_st, &m_ec1); 120 121 if (not status_known(t)) 122 return err.report(m_ec1); 123 124 if (!exists(f) || is_other(f) || is_other(t) || (is_directory(f) && is_regular_file(t)) || 125 detail::stat_equivalent(f_st, t_st)) { 126 return err.report(errc::function_not_supported); 127 } 128 129 if (ec) 130 ec->clear(); 131 132 if (is_symlink(f)) { 133 if (bool(copy_options::skip_symlinks & options)) { 134 // do nothing 135 } else if (not exists(t)) { 136 __copy_symlink(from, to, ec); 137 } else { 138 return err.report(errc::file_exists); 139 } 140 return; 141 } else if (is_regular_file(f)) { 142 if (bool(copy_options::directories_only & options)) { 143 // do nothing 144 } else if (bool(copy_options::create_symlinks & options)) { 145 __create_symlink(from, to, ec); 146 } else if (bool(copy_options::create_hard_links & options)) { 147 __create_hard_link(from, to, ec); 148 } else if (is_directory(t)) { 149 __copy_file(from, to / from.filename(), options, ec); 150 } else { 151 __copy_file(from, to, options, ec); 152 } 153 return; 154 } else if (is_directory(f) && bool(copy_options::create_symlinks & options)) { 155 return err.report(errc::is_a_directory); 156 } else if (is_directory(f) && (bool(copy_options::recursive & options) || copy_options::none == options)) { 157 if (!exists(t)) { 158 // create directory to with attributes from 'from'. 159 __create_directory(to, from, ec); 160 if (ec && *ec) { 161 return; 162 } 163 } 164 directory_iterator it = ec ? directory_iterator(from, *ec) : directory_iterator(from); 165 if (ec && *ec) { 166 return; 167 } 168 error_code m_ec2; 169 for (; it != directory_iterator(); it.increment(m_ec2)) { 170 if (m_ec2) { 171 return err.report(m_ec2); 172 } 173 __copy(it->path(), to / it->path().filename(), options | copy_options::__in_recursive_copy, ec); 174 if (ec && *ec) { 175 return; 176 } 177 } 178 } 179 } 180 181 namespace detail { 182 namespace { 183 184 #if defined(_LIBCPP_FILESYSTEM_USE_SENDFILE) 185 bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) { 186 size_t count = read_fd.get_stat().st_size; 187 do { 188 ssize_t res; 189 if ((res = ::sendfile(write_fd.fd, read_fd.fd, nullptr, count)) == -1) { 190 ec = capture_errno(); 191 return false; 192 } 193 count -= res; 194 } while (count > 0); 195 196 ec.clear(); 197 198 return true; 199 } 200 #elif defined(_LIBCPP_FILESYSTEM_USE_COPYFILE) 201 bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) { 202 struct CopyFileState { 203 copyfile_state_t state; 204 CopyFileState() { state = copyfile_state_alloc(); } 205 ~CopyFileState() { copyfile_state_free(state); } 206 207 private: 208 CopyFileState(CopyFileState const&) = delete; 209 CopyFileState& operator=(CopyFileState const&) = delete; 210 }; 211 212 CopyFileState cfs; 213 if (fcopyfile(read_fd.fd, write_fd.fd, cfs.state, COPYFILE_DATA) < 0) { 214 ec = capture_errno(); 215 return false; 216 } 217 218 ec.clear(); 219 return true; 220 } 221 #elif defined(_LIBCPP_FILESYSTEM_USE_FSTREAM) 222 bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) { 223 ifstream in; 224 in.__open(read_fd.fd, ios::binary); 225 if (!in.is_open()) { 226 // This assumes that __open didn't reset the error code. 227 ec = capture_errno(); 228 return false; 229 } 230 read_fd.fd = -1; 231 ofstream out; 232 out.__open(write_fd.fd, ios::binary); 233 if (!out.is_open()) { 234 ec = capture_errno(); 235 return false; 236 } 237 write_fd.fd = -1; 238 239 if (in.good() && out.good()) { 240 using InIt = istreambuf_iterator<char>; 241 using OutIt = ostreambuf_iterator<char>; 242 InIt bin(in); 243 InIt ein; 244 OutIt bout(out); 245 copy(bin, ein, bout); 246 } 247 if (out.fail() || in.fail()) { 248 ec = make_error_code(errc::io_error); 249 return false; 250 } 251 252 ec.clear(); 253 return true; 254 } 255 #else 256 # error "Unknown implementation for copy_file_impl" 257 #endif // copy_file_impl implementation 258 259 } // end anonymous namespace 260 } // end namespace detail 261 262 bool __copy_file(const path& from, const path& to, copy_options options, error_code* ec) { 263 using detail::FileDescriptor; 264 ErrorHandler<bool> err("copy_file", ec, &to, &from); 265 266 error_code m_ec; 267 FileDescriptor from_fd = FileDescriptor::create_with_status(&from, m_ec, O_RDONLY | O_NONBLOCK | O_BINARY); 268 if (m_ec) 269 return err.report(m_ec); 270 271 auto from_st = from_fd.get_status(); 272 StatT const& from_stat = from_fd.get_stat(); 273 if (!is_regular_file(from_st)) { 274 if (not m_ec) 275 m_ec = make_error_code(errc::not_supported); 276 return err.report(m_ec); 277 } 278 279 const bool skip_existing = bool(copy_options::skip_existing & options); 280 const bool update_existing = bool(copy_options::update_existing & options); 281 const bool overwrite_existing = bool(copy_options::overwrite_existing & options); 282 283 StatT to_stat_path; 284 file_status to_st = detail::posix_stat(to, to_stat_path, &m_ec); 285 if (!status_known(to_st)) 286 return err.report(m_ec); 287 288 const bool to_exists = exists(to_st); 289 if (to_exists && !is_regular_file(to_st)) 290 return err.report(errc::not_supported); 291 292 if (to_exists && detail::stat_equivalent(from_stat, to_stat_path)) 293 return err.report(errc::file_exists); 294 295 if (to_exists && skip_existing) 296 return false; 297 298 bool ShouldCopy = [&]() { 299 if (to_exists && update_existing) { 300 auto from_time = detail::extract_mtime(from_stat); 301 auto to_time = detail::extract_mtime(to_stat_path); 302 if (from_time.tv_sec < to_time.tv_sec) 303 return false; 304 if (from_time.tv_sec == to_time.tv_sec && from_time.tv_nsec <= to_time.tv_nsec) 305 return false; 306 return true; 307 } 308 if (!to_exists || overwrite_existing) 309 return true; 310 return err.report(errc::file_exists); 311 }(); 312 if (!ShouldCopy) 313 return false; 314 315 // Don't truncate right away. We may not be opening the file we originally 316 // looked at; we'll check this later. 317 int to_open_flags = O_WRONLY | O_BINARY; 318 if (!to_exists) 319 to_open_flags |= O_CREAT; 320 FileDescriptor to_fd = FileDescriptor::create_with_status(&to, m_ec, to_open_flags, from_stat.st_mode); 321 if (m_ec) 322 return err.report(m_ec); 323 324 if (to_exists) { 325 // Check that the file we initially stat'ed is equivalent to the one 326 // we opened. 327 // FIXME: report this better. 328 if (!detail::stat_equivalent(to_stat_path, to_fd.get_stat())) 329 return err.report(errc::bad_file_descriptor); 330 331 // Set the permissions and truncate the file we opened. 332 if (detail::posix_fchmod(to_fd, from_stat, m_ec)) 333 return err.report(m_ec); 334 if (detail::posix_ftruncate(to_fd, 0, m_ec)) 335 return err.report(m_ec); 336 } 337 338 if (!detail::copy_file_impl(from_fd, to_fd, m_ec)) { 339 // FIXME: Remove the dest file if we failed, and it didn't exist previously. 340 return err.report(m_ec); 341 } 342 343 return true; 344 } 345 346 void __copy_symlink(const path& existing_symlink, const path& new_symlink, error_code* ec) { 347 const path real_path(__read_symlink(existing_symlink, ec)); 348 if (ec && *ec) { 349 return; 350 } 351 #if defined(_LIBCPP_WIN32API) 352 error_code local_ec; 353 if (is_directory(real_path, local_ec)) 354 __create_directory_symlink(real_path, new_symlink, ec); 355 else 356 #endif 357 __create_symlink(real_path, new_symlink, ec); 358 } 359 360 bool __create_directories(const path& p, error_code* ec) { 361 ErrorHandler<bool> err("create_directories", ec, &p); 362 363 error_code m_ec; 364 auto const st = detail::posix_stat(p, &m_ec); 365 if (!status_known(st)) 366 return err.report(m_ec); 367 else if (is_directory(st)) 368 return false; 369 else if (exists(st)) 370 return err.report(errc::file_exists); 371 372 const path parent = p.parent_path(); 373 if (!parent.empty()) { 374 const file_status parent_st = status(parent, m_ec); 375 if (not status_known(parent_st)) 376 return err.report(m_ec); 377 if (not exists(parent_st)) { 378 if (parent == p) 379 return err.report(errc::invalid_argument); 380 __create_directories(parent, ec); 381 if (ec && *ec) { 382 return false; 383 } 384 } else if (not is_directory(parent_st)) 385 return err.report(errc::not_a_directory); 386 } 387 bool ret = __create_directory(p, &m_ec); 388 if (m_ec) 389 return err.report(m_ec); 390 return ret; 391 } 392 393 bool __create_directory(const path& p, error_code* ec) { 394 ErrorHandler<bool> err("create_directory", ec, &p); 395 396 if (detail::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0) 397 return true; 398 399 if (errno != EEXIST) 400 return err.report(capture_errno()); 401 error_code mec = capture_errno(); 402 error_code ignored_ec; 403 const file_status st = status(p, ignored_ec); 404 if (!is_directory(st)) 405 return err.report(mec); 406 return false; 407 } 408 409 bool __create_directory(path const& p, path const& attributes, error_code* ec) { 410 ErrorHandler<bool> err("create_directory", ec, &p, &attributes); 411 412 StatT attr_stat; 413 error_code mec; 414 file_status st = detail::posix_stat(attributes, attr_stat, &mec); 415 if (!status_known(st)) 416 return err.report(mec); 417 if (!is_directory(st)) 418 return err.report(errc::not_a_directory, "the specified attribute path is invalid"); 419 420 if (detail::mkdir(p.c_str(), attr_stat.st_mode) == 0) 421 return true; 422 423 if (errno != EEXIST) 424 return err.report(capture_errno()); 425 426 mec = capture_errno(); 427 error_code ignored_ec; 428 st = status(p, ignored_ec); 429 if (!is_directory(st)) 430 return err.report(mec); 431 return false; 432 } 433 434 void __create_directory_symlink(path const& from, path const& to, error_code* ec) { 435 ErrorHandler<void> err("create_directory_symlink", ec, &from, &to); 436 if (detail::symlink_dir(from.c_str(), to.c_str()) == -1) 437 return err.report(capture_errno()); 438 } 439 440 void __create_hard_link(const path& from, const path& to, error_code* ec) { 441 ErrorHandler<void> err("create_hard_link", ec, &from, &to); 442 if (detail::link(from.c_str(), to.c_str()) == -1) 443 return err.report(capture_errno()); 444 } 445 446 void __create_symlink(path const& from, path const& to, error_code* ec) { 447 ErrorHandler<void> err("create_symlink", ec, &from, &to); 448 if (detail::symlink_file(from.c_str(), to.c_str()) == -1) 449 return err.report(capture_errno()); 450 } 451 452 path __current_path(error_code* ec) { 453 ErrorHandler<path> err("current_path", ec); 454 455 #if defined(_LIBCPP_WIN32API) || defined(__GLIBC__) || defined(__APPLE__) 456 // Common extension outside of POSIX getcwd() spec, without needing to 457 // preallocate a buffer. Also supported by a number of other POSIX libcs. 458 int size = 0; 459 path::value_type* ptr = nullptr; 460 typedef decltype(&::free) Deleter; 461 Deleter deleter = &::free; 462 #else 463 auto size = ::pathconf(".", _PC_PATH_MAX); 464 _LIBCPP_ASSERT_UNCATEGORIZED(size >= 0, "pathconf returned a 0 as max size"); 465 466 auto buff = unique_ptr<path::value_type[]>(new path::value_type[size + 1]); 467 path::value_type* ptr = buff.get(); 468 469 // Preallocated buffer, don't free the buffer in the second unique_ptr 470 // below. 471 struct Deleter { 472 void operator()(void*) const {} 473 }; 474 Deleter deleter; 475 #endif 476 477 unique_ptr<path::value_type, Deleter> hold(detail::getcwd(ptr, size), deleter); 478 if (hold.get() == nullptr) 479 return err.report(capture_errno(), "call to getcwd failed"); 480 481 return {hold.get()}; 482 } 483 484 void __current_path(const path& p, error_code* ec) { 485 ErrorHandler<void> err("current_path", ec, &p); 486 if (detail::chdir(p.c_str()) == -1) 487 err.report(capture_errno()); 488 } 489 490 bool __equivalent(const path& p1, const path& p2, error_code* ec) { 491 ErrorHandler<bool> err("equivalent", ec, &p1, &p2); 492 493 error_code ec1, ec2; 494 StatT st1 = {}, st2 = {}; 495 auto s1 = detail::posix_stat(p1.native(), st1, &ec1); 496 if (!exists(s1)) 497 return err.report(errc::not_supported); 498 auto s2 = detail::posix_stat(p2.native(), st2, &ec2); 499 if (!exists(s2)) 500 return err.report(errc::not_supported); 501 502 return detail::stat_equivalent(st1, st2); 503 } 504 505 uintmax_t __file_size(const path& p, error_code* ec) { 506 ErrorHandler<uintmax_t> err("file_size", ec, &p); 507 508 error_code m_ec; 509 StatT st; 510 file_status fst = detail::posix_stat(p, st, &m_ec); 511 if (!exists(fst) || !is_regular_file(fst)) { 512 errc error_kind = is_directory(fst) ? errc::is_a_directory : errc::not_supported; 513 if (!m_ec) 514 m_ec = make_error_code(error_kind); 515 return err.report(m_ec); 516 } 517 // is_regular_file(p) == true 518 return static_cast<uintmax_t>(st.st_size); 519 } 520 521 uintmax_t __hard_link_count(const path& p, error_code* ec) { 522 ErrorHandler<uintmax_t> err("hard_link_count", ec, &p); 523 524 error_code m_ec; 525 StatT st; 526 detail::posix_stat(p, st, &m_ec); 527 if (m_ec) 528 return err.report(m_ec); 529 return static_cast<uintmax_t>(st.st_nlink); 530 } 531 532 bool __fs_is_empty(const path& p, error_code* ec) { 533 ErrorHandler<bool> err("is_empty", ec, &p); 534 535 error_code m_ec; 536 StatT pst; 537 auto st = detail::posix_stat(p, pst, &m_ec); 538 if (m_ec) 539 return err.report(m_ec); 540 else if (!is_directory(st) && !is_regular_file(st)) 541 return err.report(errc::not_supported); 542 else if (is_directory(st)) { 543 auto it = ec ? directory_iterator(p, *ec) : directory_iterator(p); 544 if (ec && *ec) 545 return false; 546 return it == directory_iterator{}; 547 } else if (is_regular_file(st)) 548 return static_cast<uintmax_t>(pst.st_size) == 0; 549 550 __libcpp_unreachable(); 551 } 552 553 file_time_type __last_write_time(const path& p, error_code* ec) { 554 using namespace chrono; 555 ErrorHandler<file_time_type> err("last_write_time", ec, &p); 556 557 error_code m_ec; 558 StatT st; 559 detail::posix_stat(p, st, &m_ec); 560 if (m_ec) 561 return err.report(m_ec); 562 return detail::__extract_last_write_time(p, st, ec); 563 } 564 565 void __last_write_time(const path& p, file_time_type new_time, error_code* ec) { 566 using detail::fs_time; 567 ErrorHandler<void> err("last_write_time", ec, &p); 568 569 #if defined(_LIBCPP_WIN32API) 570 TimeSpec ts; 571 if (!fs_time::convert_to_timespec(ts, new_time)) 572 return err.report(errc::value_too_large); 573 detail::WinHandle h(p.c_str(), FILE_WRITE_ATTRIBUTES, 0); 574 if (!h) 575 return err.report(detail::make_windows_error(GetLastError())); 576 FILETIME last_write = timespec_to_filetime(ts); 577 if (!SetFileTime(h, nullptr, nullptr, &last_write)) 578 return err.report(detail::make_windows_error(GetLastError())); 579 #else 580 error_code m_ec; 581 array<TimeSpec, 2> tbuf; 582 # if !defined(_LIBCPP_USE_UTIMENSAT) 583 // This implementation has a race condition between determining the 584 // last access time and attempting to set it to the same value using 585 // ::utimes 586 StatT st; 587 file_status fst = detail::posix_stat(p, st, &m_ec); 588 if (m_ec) 589 return err.report(m_ec); 590 tbuf[0] = detail::extract_atime(st); 591 # else 592 tbuf[0].tv_sec = 0; 593 tbuf[0].tv_nsec = UTIME_OMIT; 594 # endif 595 if (!fs_time::convert_to_timespec(tbuf[1], new_time)) 596 return err.report(errc::value_too_large); 597 598 detail::set_file_times(p, tbuf, m_ec); 599 if (m_ec) 600 return err.report(m_ec); 601 #endif 602 } 603 604 void __permissions(const path& p, perms prms, perm_options opts, error_code* ec) { 605 ErrorHandler<void> err("permissions", ec, &p); 606 607 auto has_opt = [&](perm_options o) { return bool(o & opts); }; 608 const bool resolve_symlinks = !has_opt(perm_options::nofollow); 609 const bool add_perms = has_opt(perm_options::add); 610 const bool remove_perms = has_opt(perm_options::remove); 611 _LIBCPP_ASSERT_UNCATEGORIZED( 612 (add_perms + remove_perms + has_opt(perm_options::replace)) == 1, 613 "One and only one of the perm_options constants replace, add, or remove " 614 "is present in opts"); 615 616 bool set_sym_perms = false; 617 prms &= perms::mask; 618 if (!resolve_symlinks || (add_perms || remove_perms)) { 619 error_code m_ec; 620 file_status st = resolve_symlinks ? detail::posix_stat(p, &m_ec) : detail::posix_lstat(p, &m_ec); 621 set_sym_perms = is_symlink(st); 622 if (m_ec) 623 return err.report(m_ec); 624 _LIBCPP_ASSERT_UNCATEGORIZED(st.permissions() != perms::unknown, "Permissions unexpectedly unknown"); 625 if (add_perms) 626 prms |= st.permissions(); 627 else if (remove_perms) 628 prms = st.permissions() & ~prms; 629 } 630 const auto real_perms = static_cast<detail::ModeT>(prms & perms::mask); 631 632 #if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD) 633 const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0; 634 if (detail::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) { 635 return err.report(capture_errno()); 636 } 637 #else 638 if (set_sym_perms) 639 return err.report(errc::operation_not_supported); 640 if (::chmod(p.c_str(), real_perms) == -1) { 641 return err.report(capture_errno()); 642 } 643 #endif 644 } 645 646 path __read_symlink(const path& p, error_code* ec) { 647 ErrorHandler<path> err("read_symlink", ec, &p); 648 649 #if defined(PATH_MAX) || defined(MAX_SYMLINK_SIZE) 650 struct NullDeleter { 651 void operator()(void*) const {} 652 }; 653 # ifdef MAX_SYMLINK_SIZE 654 const size_t size = MAX_SYMLINK_SIZE + 1; 655 # else 656 const size_t size = PATH_MAX + 1; 657 # endif 658 path::value_type stack_buff[size]; 659 auto buff = std::unique_ptr<path::value_type[], NullDeleter>(stack_buff); 660 #else 661 StatT sb; 662 if (detail::lstat(p.c_str(), &sb) == -1) { 663 return err.report(capture_errno()); 664 } 665 const size_t size = sb.st_size + 1; 666 auto buff = unique_ptr<path::value_type[]>(new path::value_type[size]); 667 #endif 668 detail::SSizeT ret; 669 if ((ret = detail::readlink(p.c_str(), buff.get(), size)) == -1) 670 return err.report(capture_errno()); 671 _LIBCPP_ASSERT_UNCATEGORIZED(ret > 0, "TODO"); 672 if (static_cast<size_t>(ret) >= size) 673 return err.report(errc::value_too_large); 674 buff[ret] = 0; 675 return {buff.get()}; 676 } 677 678 bool __remove(const path& p, error_code* ec) { 679 ErrorHandler<bool> err("remove", ec, &p); 680 if (detail::remove(p.c_str()) == -1) { 681 if (errno != ENOENT) 682 err.report(capture_errno()); 683 return false; 684 } 685 return true; 686 } 687 688 // We currently have two implementations of `__remove_all`. The first one is general and 689 // used on platforms where we don't have access to the `openat()` family of POSIX functions. 690 // That implementation uses `directory_iterator`, however it is vulnerable to some race 691 // conditions, see https://reviews.llvm.org/D118134 for details. 692 // 693 // The second implementation is used on platforms where `openat()` & friends are available, 694 // and it threads file descriptors through recursive calls to avoid such race conditions. 695 #if defined(_LIBCPP_WIN32API) || defined(__MVS__) 696 # define REMOVE_ALL_USE_DIRECTORY_ITERATOR 697 #endif 698 699 #if defined(REMOVE_ALL_USE_DIRECTORY_ITERATOR) 700 701 namespace { 702 703 uintmax_t remove_all_impl(path const& p, error_code& ec) { 704 const auto npos = static_cast<uintmax_t>(-1); 705 const file_status st = __symlink_status(p, &ec); 706 if (ec) 707 return npos; 708 uintmax_t count = 1; 709 if (is_directory(st)) { 710 for (directory_iterator it(p, ec); !ec && it != directory_iterator(); it.increment(ec)) { 711 auto other_count = remove_all_impl(it->path(), ec); 712 if (ec) 713 return npos; 714 count += other_count; 715 } 716 if (ec) 717 return npos; 718 } 719 if (!__remove(p, &ec)) 720 return npos; 721 return count; 722 } 723 724 } // end namespace 725 726 uintmax_t __remove_all(const path& p, error_code* ec) { 727 ErrorHandler<uintmax_t> err("remove_all", ec, &p); 728 729 error_code mec; 730 auto count = remove_all_impl(p, mec); 731 if (mec) { 732 if (mec == errc::no_such_file_or_directory) 733 return 0; 734 return err.report(mec); 735 } 736 return count; 737 } 738 739 #else // !REMOVE_ALL_USE_DIRECTORY_ITERATOR 740 741 namespace { 742 743 template <class Cleanup> 744 struct scope_exit { 745 explicit scope_exit(Cleanup const& cleanup) : cleanup_(cleanup) {} 746 747 ~scope_exit() { cleanup_(); } 748 749 private: 750 Cleanup cleanup_; 751 }; 752 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(scope_exit); 753 754 uintmax_t remove_all_impl(int parent_directory, const path& p, error_code& ec) { 755 // First, try to open the path as a directory. 756 const int options = O_CLOEXEC | O_RDONLY | O_DIRECTORY | O_NOFOLLOW; 757 int fd = ::openat(parent_directory, p.c_str(), options); 758 if (fd != -1) { 759 // If that worked, iterate over the contents of the directory and 760 // remove everything in it, recursively. 761 DIR* stream = ::fdopendir(fd); 762 if (stream == nullptr) { 763 ::close(fd); 764 ec = detail::capture_errno(); 765 return 0; 766 } 767 // Note: `::closedir` will also close the associated file descriptor, so 768 // there should be no call to `close(fd)`. 769 scope_exit close_stream([=] { ::closedir(stream); }); 770 771 uintmax_t count = 0; 772 while (true) { 773 auto [str, type] = detail::posix_readdir(stream, ec); 774 static_assert(std::is_same_v<decltype(str), std::string_view>); 775 if (str == "." || str == "..") { 776 continue; 777 } else if (ec || str.empty()) { 778 break; // we're done iterating through the directory 779 } else { 780 count += remove_all_impl(fd, str, ec); 781 } 782 } 783 784 // Then, remove the now-empty directory itself. 785 if (::unlinkat(parent_directory, p.c_str(), AT_REMOVEDIR) == -1) { 786 ec = detail::capture_errno(); 787 return count; 788 } 789 790 return count + 1; // the contents of the directory + the directory itself 791 } 792 793 ec = detail::capture_errno(); 794 795 // If we failed to open `p` because it didn't exist, it's not an 796 // error -- it might have moved or have been deleted already. 797 if (ec == errc::no_such_file_or_directory) { 798 ec.clear(); 799 return 0; 800 } 801 802 // If opening `p` failed because it wasn't a directory, remove it as 803 // a normal file instead. Note that `openat()` can return either ENOTDIR 804 // or ELOOP depending on the exact reason of the failure. On FreeBSD it 805 // may return EMLINK instead of ELOOP, contradicting POSIX. 806 if (ec == errc::not_a_directory || ec == errc::too_many_symbolic_link_levels || ec == errc::too_many_links) { 807 ec.clear(); 808 if (::unlinkat(parent_directory, p.c_str(), /* flags = */ 0) == -1) { 809 ec = detail::capture_errno(); 810 return 0; 811 } 812 return 1; 813 } 814 815 // Otherwise, it's a real error -- we don't remove anything. 816 return 0; 817 } 818 819 } // end namespace 820 821 uintmax_t __remove_all(const path& p, error_code* ec) { 822 ErrorHandler<uintmax_t> err("remove_all", ec, &p); 823 error_code mec; 824 uintmax_t count = remove_all_impl(AT_FDCWD, p, mec); 825 if (mec) 826 return err.report(mec); 827 return count; 828 } 829 830 #endif // REMOVE_ALL_USE_DIRECTORY_ITERATOR 831 832 void __rename(const path& from, const path& to, error_code* ec) { 833 ErrorHandler<void> err("rename", ec, &from, &to); 834 if (detail::rename(from.c_str(), to.c_str()) == -1) 835 err.report(capture_errno()); 836 } 837 838 void __resize_file(const path& p, uintmax_t size, error_code* ec) { 839 ErrorHandler<void> err("resize_file", ec, &p); 840 if (detail::truncate(p.c_str(), static_cast< ::off_t>(size)) == -1) 841 return err.report(capture_errno()); 842 } 843 844 space_info __space(const path& p, error_code* ec) { 845 ErrorHandler<void> err("space", ec, &p); 846 space_info si; 847 detail::StatVFS m_svfs = {}; 848 if (detail::statvfs(p.c_str(), &m_svfs) == -1) { 849 err.report(capture_errno()); 850 si.capacity = si.free = si.available = static_cast<uintmax_t>(-1); 851 return si; 852 } 853 // Multiply with overflow checking. 854 auto do_mult = [&](uintmax_t& out, uintmax_t other) { 855 out = other * m_svfs.f_frsize; 856 if (other == 0 || out / other != m_svfs.f_frsize) 857 out = static_cast<uintmax_t>(-1); 858 }; 859 do_mult(si.capacity, m_svfs.f_blocks); 860 do_mult(si.free, m_svfs.f_bfree); 861 do_mult(si.available, m_svfs.f_bavail); 862 return si; 863 } 864 865 file_status __status(const path& p, error_code* ec) { return detail::posix_stat(p, ec); } 866 867 file_status __symlink_status(const path& p, error_code* ec) { return detail::posix_lstat(p, ec); } 868 869 path __temp_directory_path(error_code* ec) { 870 ErrorHandler<path> err("temp_directory_path", ec); 871 872 #if defined(_LIBCPP_WIN32API) 873 wchar_t buf[MAX_PATH]; 874 DWORD retval = GetTempPathW(MAX_PATH, buf); 875 if (!retval) 876 return err.report(detail::make_windows_error(GetLastError())); 877 if (retval > MAX_PATH) 878 return err.report(errc::filename_too_long); 879 // GetTempPathW returns a path with a trailing slash, which we 880 // shouldn't include for consistency. 881 if (buf[retval - 1] == L'\\') 882 buf[retval - 1] = L'\0'; 883 path p(buf); 884 #else 885 const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"}; 886 const char* ret = nullptr; 887 888 for (auto& ep : env_paths) 889 if ((ret = getenv(ep))) 890 break; 891 if (ret == nullptr) { 892 # if defined(__ANDROID__) 893 ret = "/data/local/tmp"; 894 # else 895 ret = "/tmp"; 896 # endif 897 } 898 899 path p(ret); 900 #endif 901 error_code m_ec; 902 file_status st = detail::posix_stat(p, &m_ec); 903 if (!status_known(st)) 904 return err.report(m_ec, "cannot access path " PATH_CSTR_FMT, p.c_str()); 905 906 if (!exists(st) || !is_directory(st)) 907 return err.report(errc::not_a_directory, "path " PATH_CSTR_FMT " is not a directory", p.c_str()); 908 909 return p; 910 } 911 912 path __weakly_canonical(const path& p, error_code* ec) { 913 ErrorHandler<path> err("weakly_canonical", ec, &p); 914 915 if (p.empty()) 916 return __canonical("", ec); 917 918 path result; 919 path tmp; 920 tmp.__reserve(p.native().size()); 921 auto PP = PathParser::CreateEnd(p.native()); 922 --PP; 923 vector<string_view_t> DNEParts; 924 925 while (PP.State != PathParser::PS_BeforeBegin) { 926 tmp.assign(createView(p.native().data(), &PP.RawEntry.back())); 927 error_code m_ec; 928 file_status st = __status(tmp, &m_ec); 929 if (!status_known(st)) { 930 return err.report(m_ec); 931 } else if (exists(st)) { 932 result = __canonical(tmp, ec); 933 break; 934 } 935 DNEParts.push_back(*PP); 936 --PP; 937 } 938 if (PP.State == PathParser::PS_BeforeBegin) 939 result = __canonical("", ec); 940 if (ec) 941 ec->clear(); 942 if (DNEParts.empty()) 943 return result; 944 for (auto It = DNEParts.rbegin(); It != DNEParts.rend(); ++It) 945 result /= *It; 946 return result.lexically_normal(); 947 } 948 949 _LIBCPP_END_NAMESPACE_FILESYSTEM 950