1 //===--------------------- filesystem/ops.cpp -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "experimental/filesystem"
13 #include "type_traits"
14 #include "random" /* for unique_path */
20 #include <sys/statvfs.h>
21 #include <fcntl.h> /* values for fchmodat */
22 #if !defined(UTIME_OMIT)
23 #include <sys/time.h> // for ::utimes as used in __last_write_time
26 _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_FILESYSTEM
28 filesystem_error::~filesystem_error() {}
33 namespace detail { namespace {
35 using value_type = path::value_type;
36 using string_type = path::string_type;
38 inline std::error_code capture_errno() {
39 _LIBCPP_ASSERT(errno, "Expected errno to be non-zero");
40 return std::error_code(errno, std::generic_category());
43 void set_or_throw(std::error_code const& m_ec, std::error_code* ec,
44 const char* msg, path const& p = {}, path const& p2 = {})
49 string msg_s("std::experimental::filesystem::");
51 __throw_filesystem_error(msg_s, p, p2, m_ec);
55 void set_or_throw(std::error_code* ec, const char* msg,
56 path const& p = {}, path const& p2 = {})
58 return set_or_throw(capture_errno(), ec, msg, p, p2);
61 perms posix_get_perms(const struct ::stat & st) noexcept {
62 return static_cast<perms>(st.st_mode) & perms::mask;
65 ::mode_t posix_convert_perms(perms prms) {
66 return static_cast< ::mode_t>(prms & perms::mask);
69 file_status create_file_status(std::error_code& m_ec, path const& p,
70 struct ::stat& path_stat,
74 if (m_ec && (m_ec.value() == ENOENT || m_ec.value() == ENOTDIR)) {
75 return file_status(file_type::not_found);
78 set_or_throw(m_ec, ec, "posix_stat", p);
79 return file_status(file_type::none);
84 auto const mode = path_stat.st_mode;
85 if (S_ISLNK(mode)) fs_tmp.type(file_type::symlink);
86 else if (S_ISREG(mode)) fs_tmp.type(file_type::regular);
87 else if (S_ISDIR(mode)) fs_tmp.type(file_type::directory);
88 else if (S_ISBLK(mode)) fs_tmp.type(file_type::block);
89 else if (S_ISCHR(mode)) fs_tmp.type(file_type::character);
90 else if (S_ISFIFO(mode)) fs_tmp.type(file_type::fifo);
91 else if (S_ISSOCK(mode)) fs_tmp.type(file_type::socket);
92 else fs_tmp.type(file_type::unknown);
94 fs_tmp.permissions(detail::posix_get_perms(path_stat));
98 file_status posix_stat(path const & p, struct ::stat& path_stat,
101 std::error_code m_ec;
102 if (::stat(p.c_str(), &path_stat) == -1)
103 m_ec = detail::capture_errno();
104 return create_file_status(m_ec, p, path_stat, ec);
107 file_status posix_stat(path const & p, std::error_code* ec) {
108 struct ::stat path_stat;
109 return posix_stat(p, path_stat, ec);
112 file_status posix_lstat(path const & p, struct ::stat & path_stat,
115 std::error_code m_ec;
116 if (::lstat(p.c_str(), &path_stat) == -1)
117 m_ec = detail::capture_errno();
118 return create_file_status(m_ec, p, path_stat, ec);
121 file_status posix_lstat(path const & p, std::error_code* ec) {
122 struct ::stat path_stat;
123 return posix_lstat(p, path_stat, ec);
126 bool stat_equivalent(struct ::stat& st1, struct ::stat& st2) {
127 return (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
133 bool copy_file_impl(const path& from, const path& to, perms from_perms,
136 std::ifstream in(from.c_str(), std::ios::binary);
137 std::ofstream out(to.c_str(), std::ios::binary);
139 if (in.good() && out.good()) {
140 using InIt = std::istreambuf_iterator<char>;
141 using OutIt = std::ostreambuf_iterator<char>;
145 std::copy(bin, ein, bout);
147 if (out.fail() || in.fail()) {
148 set_or_throw(make_error_code(errc::operation_not_permitted),
149 ec, "copy_file", from, to);
152 __permissions(to, from_perms, ec);
153 // TODO what if permissions fails?
157 }} // end namespace detail
159 using detail::set_or_throw;
161 path __canonical(path const & orig_p, const path& base, std::error_code *ec)
163 path p = absolute(orig_p, base);
164 char buff[PATH_MAX + 1];
166 if ((ret = ::realpath(p.c_str(), buff)) == nullptr) {
167 set_or_throw(ec, "canonical", orig_p, base);
174 void __copy(const path& from, const path& to, copy_options options,
177 const bool sym_status = bool(options &
178 (copy_options::create_symlinks | copy_options::skip_symlinks));
180 const bool sym_status2 = bool(options &
181 copy_options::copy_symlinks);
183 std::error_code m_ec;
184 struct ::stat f_st = {};
185 const file_status f = sym_status || sym_status2
186 ? detail::posix_lstat(from, f_st, &m_ec)
187 : detail::posix_stat(from, f_st, &m_ec);
189 return set_or_throw(m_ec, ec, "copy", from, to);
191 struct ::stat t_st = {};
192 const file_status t = sym_status ? detail::posix_lstat(to, t_st, &m_ec)
193 : detail::posix_stat(to, t_st, &m_ec);
195 if (not status_known(t))
196 return set_or_throw(m_ec, ec, "copy", from, to);
198 if (!exists(f) || is_other(f) || is_other(t)
199 || (is_directory(f) && is_regular_file(t))
200 || detail::stat_equivalent(f_st, t_st))
202 return set_or_throw(make_error_code(errc::function_not_supported),
203 ec, "copy", from, to);
209 if (bool(copy_options::skip_symlinks & options)) {
211 } else if (not exists(t)) {
212 __copy_symlink(from, to, ec);
214 set_or_throw(make_error_code(errc::file_exists),
215 ec, "copy", from, to);
219 else if (is_regular_file(f)) {
220 if (bool(copy_options::directories_only & options)) {
223 else if (bool(copy_options::create_symlinks & options)) {
224 __create_symlink(from, to, ec);
226 else if (bool(copy_options::create_hard_links & options)) {
227 __create_hard_link(from, to, ec);
229 else if (is_directory(t)) {
230 __copy_file(from, to / from.filename(), options, ec);
232 __copy_file(from, to, options, ec);
236 else if (is_directory(f) && bool(copy_options::create_symlinks & options)) {
237 return set_or_throw(make_error_code(errc::is_a_directory), ec, "copy");
239 else if (is_directory(f) && (bool(copy_options::recursive & options) ||
240 copy_options::none == options)) {
243 // create directory to with attributes from 'from'.
244 __create_directory(to, from, ec);
245 if (ec && *ec) { return; }
247 directory_iterator it = ec ? directory_iterator(from, *ec)
248 : directory_iterator(from);
249 if (ec && *ec) { return; }
250 std::error_code m_ec;
251 for (; it != directory_iterator(); it.increment(m_ec)) {
252 if (m_ec) return set_or_throw(m_ec, ec, "copy", from, to);
253 __copy(it->path(), to / it->path().filename(),
254 options | copy_options::__in_recursive_copy, ec);
255 if (ec && *ec) { return; }
261 bool __copy_file(const path& from, const path& to, copy_options options,
266 std::error_code m_ec;
267 auto from_st = detail::posix_stat(from, &m_ec);
268 if (not is_regular_file(from_st)) {
270 m_ec = make_error_code(errc::not_supported);
271 set_or_throw(m_ec, ec, "copy_file", from, to);
275 auto to_st = detail::posix_stat(to, &m_ec);
276 if (!status_known(to_st)) {
277 set_or_throw(m_ec, ec, "copy_file", from, to);
281 const bool to_exists = exists(to_st);
282 if (to_exists && !is_regular_file(to_st)) {
283 set_or_throw(make_error_code(errc::not_supported), ec, "copy_file", from, to);
286 if (to_exists && bool(copy_options::skip_existing & options)) {
289 else if (to_exists && bool(copy_options::update_existing & options)) {
290 auto from_time = __last_write_time(from, ec);
291 if (ec && *ec) { return false; }
292 auto to_time = __last_write_time(to, ec);
293 if (ec && *ec) { return false; }
294 if (from_time <= to_time) {
297 return detail::copy_file_impl(from, to, from_st.permissions(), ec);
299 else if (!to_exists || bool(copy_options::overwrite_existing & options)) {
300 return detail::copy_file_impl(from, to, from_st.permissions(), ec);
303 set_or_throw(make_error_code(errc::file_exists), ec, "copy", from, to);
307 _LIBCPP_UNREACHABLE();
310 void __copy_symlink(const path& existing_symlink, const path& new_symlink,
313 const path real_path(__read_symlink(existing_symlink, ec));
314 if (ec && *ec) { return; }
315 // NOTE: proposal says you should detect if you should call
316 // create_symlink or create_directory_symlink. I don't think this
317 // is needed with POSIX
318 __create_symlink(real_path, new_symlink, ec);
322 bool __create_directories(const path& p, std::error_code *ec)
324 std::error_code m_ec;
325 auto const st = detail::posix_stat(p, &m_ec);
326 if (!status_known(st)) {
327 set_or_throw(m_ec, ec, "create_directories", p);
330 else if (is_directory(st)) {
334 else if (exists(st)) {
335 set_or_throw(make_error_code(errc::file_exists),
336 ec, "create_directories", p);
340 const path parent = p.parent_path();
341 if (!parent.empty()) {
342 const file_status parent_st = status(parent, m_ec);
343 if (not status_known(parent_st)) {
344 set_or_throw(m_ec, ec, "create_directories", p);
347 if (not exists(parent_st)) {
348 __create_directories(parent, ec);
349 if (ec && *ec) { return false; }
352 return __create_directory(p, ec);
355 bool __create_directory(const path& p, std::error_code *ec)
358 if (::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0)
360 if (errno != EEXIST || !is_directory(p))
361 set_or_throw(ec, "create_directory", p);
365 bool __create_directory(path const & p, path const & attributes,
368 struct ::stat attr_stat;
370 auto st = detail::posix_stat(attributes, attr_stat, &mec);
371 if (!status_known(st)) {
372 set_or_throw(mec, ec, "create_directory", p, attributes);
376 if (::mkdir(p.c_str(), attr_stat.st_mode) == 0)
378 if (errno != EEXIST || !is_directory(p))
379 set_or_throw(ec, "create_directory", p, attributes);
383 void __create_directory_symlink(path const & from, path const & to,
384 std::error_code *ec){
385 if (::symlink(from.c_str(), to.c_str()) != 0)
386 set_or_throw(ec, "create_directory_symlink", from, to);
391 void __create_hard_link(const path& from, const path& to, std::error_code *ec){
392 if (::link(from.c_str(), to.c_str()) == -1)
393 set_or_throw(ec, "create_hard_link", from, to);
398 void __create_symlink(path const & from, path const & to, std::error_code *ec) {
400 if (::symlink(from.c_str(), to.c_str()) == -1)
401 set_or_throw(ec, "create_symlink", from, to);
406 path __current_path(std::error_code *ec) {
407 auto size = ::pathconf(".", _PC_PATH_MAX);
408 _LIBCPP_ASSERT(size >= 0, "pathconf returned a 0 as max size");
410 auto buff = std::unique_ptr<char[]>(new char[size + 1]);
412 if ((ret = ::getcwd(buff.get(), static_cast<size_t>(size))) == nullptr) {
413 set_or_throw(ec, "current_path");
420 void __current_path(const path& p, std::error_code *ec) {
421 if (::chdir(p.c_str()) == -1)
422 set_or_throw(ec, "current_path", p);
427 bool __equivalent(const path& p1, const path& p2, std::error_code *ec)
429 std::error_code ec1, ec2;
430 struct ::stat st1 = {};
431 struct ::stat st2 = {};
432 auto s1 = detail::posix_stat(p1.native(), st1, &ec1);
433 auto s2 = detail::posix_stat(p2.native(), st2, &ec2);
435 if ((!exists(s1) && !exists(s2)) || (is_other(s1) && is_other(s2))) {
436 set_or_throw(make_error_code(errc::not_supported), ec,
437 "equivalent", p1, p2);
441 return (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
445 std::uintmax_t __file_size(const path& p, std::error_code *ec)
447 std::error_code m_ec;
449 file_status fst = detail::posix_stat(p, st, &m_ec);
450 if (!exists(fst) || !is_regular_file(fst)) {
452 m_ec = make_error_code(errc::not_supported);
453 set_or_throw(m_ec, ec, "file_size", p);
454 return static_cast<uintmax_t>(-1);
456 // is_regular_file(p) == true
458 return static_cast<std::uintmax_t>(st.st_size);
461 std::uintmax_t __hard_link_count(const path& p, std::error_code *ec)
463 std::error_code m_ec;
465 detail::posix_stat(p, st, &m_ec);
467 set_or_throw(m_ec, ec, "hard_link_count", p);
468 return static_cast<std::uintmax_t>(-1);
471 return static_cast<std::uintmax_t>(st.st_nlink);
475 bool __fs_is_empty(const path& p, std::error_code *ec)
478 std::error_code m_ec;
480 auto st = detail::posix_stat(p, pst, &m_ec);
482 set_or_throw(m_ec, ec, "is_empty", p);
485 else if (!is_directory(st) && !is_regular_file(st)) {
486 m_ec = make_error_code(errc::not_supported);
487 set_or_throw(m_ec, ec, "is_empty");
490 else if (is_directory(st)) {
491 auto it = ec ? directory_iterator(p, *ec) : directory_iterator(p);
494 return it == directory_iterator{};
496 else if (is_regular_file(st))
497 return static_cast<std::uintmax_t>(pst.st_size) == 0;
499 _LIBCPP_UNREACHABLE();
503 namespace detail { namespace {
505 using namespace std::chrono;
507 template <class CType, class ChronoType>
508 bool checked_set(CType* out, ChronoType time) {
509 using Lim = numeric_limits<CType>;
510 if (time > Lim::max() || time < Lim::min())
512 *out = static_cast<CType>(time);
516 using TimeSpec = struct timespec;
517 using StatT = struct stat;
519 #if defined(__APPLE__)
520 TimeSpec extract_mtime(StatT const& st) { return st.st_mtimespec; }
521 TimeSpec extract_atime(StatT const& st) { return st.st_atimespec; }
523 TimeSpec extract_mtime(StatT const& st) { return st.st_mtim; }
524 __attribute__((unused)) // Suppress warning
525 TimeSpec extract_atime(StatT const& st) { return st.st_atim; }
528 constexpr auto max_seconds = duration_cast<seconds>(
529 file_time_type::duration::max()).count();
531 constexpr auto max_nsec = duration_cast<nanoseconds>(
532 file_time_type::duration::max() - seconds(max_seconds)).count();
534 constexpr auto min_seconds = duration_cast<seconds>(
535 file_time_type::duration::min()).count();
537 constexpr auto min_nsec_timespec = duration_cast<nanoseconds>(
538 (file_time_type::duration::min() - seconds(min_seconds)) + seconds(1)).count();
540 // Static assert that these values properly round trip.
541 static_assert((seconds(min_seconds) + duration_cast<microseconds>(nanoseconds(min_nsec_timespec)))
542 - duration_cast<microseconds>(seconds(1))
543 == file_time_type::duration::min(), "");
545 constexpr auto max_time_t = numeric_limits<time_t>::max();
546 constexpr auto min_time_t = numeric_limits<time_t>::min();
548 #if !defined(__LP64__) && defined(__clang__)
549 #pragma clang diagnostic push
550 #pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
553 _LIBCPP_CONSTEXPR_AFTER_CXX11
554 bool is_representable(TimeSpec const& tm) {
555 if (tm.tv_sec >= 0) {
556 return (tm.tv_sec < max_seconds) ||
557 (tm.tv_sec == max_seconds && tm.tv_nsec <= max_nsec);
558 } else if (tm.tv_sec == (min_seconds - 1)) {
559 return tm.tv_nsec >= min_nsec_timespec;
561 return (tm.tv_sec >= min_seconds);
564 #ifndef _LIBCPP_HAS_NO_CXX14_CONSTEXPR
565 #if defined(__LP64__)
566 static_assert(is_representable({max_seconds, max_nsec}), "");
567 static_assert(!is_representable({max_seconds + 1, 0}), "");
568 static_assert(!is_representable({max_seconds, max_nsec + 1}), "");
569 static_assert(!is_representable({max_time_t, 0}), "");
570 static_assert(is_representable({min_seconds, 0}), "");
571 static_assert(is_representable({min_seconds - 1, min_nsec_timespec}), "");
572 static_assert(is_representable({min_seconds - 1, min_nsec_timespec + 1}), "");
573 static_assert(!is_representable({min_seconds - 1, min_nsec_timespec - 1}), "");
574 static_assert(!is_representable({min_time_t, 999999999}), "");
576 static_assert(is_representable({max_time_t, 999999999}), "");
577 static_assert(is_representable({max_time_t, 1000000000}), "");
578 static_assert(is_representable({min_time_t, 0}), "");
582 _LIBCPP_CONSTEXPR_AFTER_CXX11
583 bool is_representable(file_time_type const& tm) {
584 auto secs = duration_cast<seconds>(tm.time_since_epoch());
585 auto nsecs = duration_cast<nanoseconds>(tm.time_since_epoch() - secs);
586 if (nsecs.count() < 0) {
587 secs = secs + seconds(1);
588 nsecs = nsecs + seconds(1);
590 using TLim = numeric_limits<time_t>;
591 if (secs.count() >= 0)
592 return secs.count() <= TLim::max();
593 return secs.count() >= TLim::min();
595 #ifndef _LIBCPP_HAS_NO_CXX14_CONSTEXPR
596 #if defined(__LP64__)
597 static_assert(is_representable(file_time_type::max()), "");
598 static_assert(is_representable(file_time_type::min()), "");
600 static_assert(!is_representable(file_time_type::max()), "");
601 static_assert(!is_representable(file_time_type::min()), "");
602 static_assert(is_representable(file_time_type(seconds(max_time_t))), "");
603 static_assert(is_representable(file_time_type(seconds(min_time_t))), "");
607 _LIBCPP_CONSTEXPR_AFTER_CXX11
608 file_time_type convert_timespec(TimeSpec const& tm) {
609 auto adj_msec = duration_cast<microseconds>(nanoseconds(tm.tv_nsec));
610 if (tm.tv_sec >= 0) {
611 auto Dur = seconds(tm.tv_sec) + microseconds(adj_msec);
612 return file_time_type(Dur);
613 } else if (duration_cast<microseconds>(nanoseconds(tm.tv_nsec)).count() == 0) {
614 return file_time_type(seconds(tm.tv_sec));
615 } else { // tm.tv_sec < 0
616 auto adj_subsec = duration_cast<microseconds>(seconds(1) - nanoseconds(tm.tv_nsec));
617 auto Dur = seconds(tm.tv_sec + 1) - adj_subsec;
618 return file_time_type(Dur);
621 #ifndef _LIBCPP_HAS_NO_CXX14_CONSTEXPR
622 #if defined(__LP64__)
623 static_assert(convert_timespec({max_seconds, max_nsec}) == file_time_type::max(), "");
624 static_assert(convert_timespec({max_seconds, max_nsec - 1}) < file_time_type::max(), "");
625 static_assert(convert_timespec({max_seconds - 1, 999999999}) < file_time_type::max(), "");
626 static_assert(convert_timespec({min_seconds - 1, min_nsec_timespec}) == file_time_type::min(), "");
627 static_assert(convert_timespec({min_seconds - 1, min_nsec_timespec + 1}) > file_time_type::min(), "");
628 static_assert(convert_timespec({min_seconds , 0}) > file_time_type::min(), "");
630 // FIXME add tests for 32 bit builds
634 #if !defined(__LP64__) && defined(__clang__)
635 #pragma clang diagnostic pop
638 template <class SubSecDurT, class SubSecT>
639 bool set_times_checked(time_t* sec_out, SubSecT* subsec_out, file_time_type tp) {
640 using namespace chrono;
641 auto dur = tp.time_since_epoch();
642 auto sec_dur = duration_cast<seconds>(dur);
643 auto subsec_dur = duration_cast<SubSecDurT>(dur - sec_dur);
644 // The tv_nsec and tv_usec fields must not be negative so adjust accordingly
645 if (subsec_dur.count() < 0) {
646 if (sec_dur.count() > min_seconds) {
647 sec_dur -= seconds(1);
648 subsec_dur += seconds(1);
650 subsec_dur = SubSecDurT::zero();
653 return checked_set(sec_out, sec_dur.count())
654 && checked_set(subsec_out, subsec_dur.count());
657 }} // end namespace detail
659 file_time_type __last_write_time(const path& p, std::error_code *ec)
661 using namespace ::std::chrono;
662 std::error_code m_ec;
664 detail::posix_stat(p, st, &m_ec);
666 set_or_throw(m_ec, ec, "last_write_time", p);
667 return file_time_type::min();
670 auto ts = detail::extract_mtime(st);
671 if (!detail::is_representable(ts)) {
672 set_or_throw(error_code(EOVERFLOW, generic_category()), ec,
673 "last_write_time", p);
674 return file_time_type::min();
676 return detail::convert_timespec(ts);
679 void __last_write_time(const path& p, file_time_type new_time,
682 using namespace std::chrono;
683 std::error_code m_ec;
685 // We can use the presence of UTIME_OMIT to detect platforms that do not
686 // provide utimensat.
687 #if !defined(UTIME_OMIT)
688 // This implementation has a race condition between determining the
689 // last access time and attempting to set it to the same value using
692 file_status fst = detail::posix_stat(p, st, &m_ec);
693 if (m_ec && !status_known(fst)) {
694 set_or_throw(m_ec, ec, "last_write_time", p);
697 auto atime = detail::extract_atime(st);
698 struct ::timeval tbuf[2];
699 tbuf[0].tv_sec = atime.tv_sec;
700 tbuf[0].tv_usec = duration_cast<microseconds>(nanoseconds(atime.tv_nsec)).count();
701 const bool overflowed = !detail::set_times_checked<microseconds>(
702 &tbuf[1].tv_sec, &tbuf[1].tv_usec, new_time);
705 set_or_throw(make_error_code(errc::invalid_argument), ec,
706 "last_write_time", p);
709 if (::utimes(p.c_str(), tbuf) == -1) {
710 m_ec = detail::capture_errno();
713 struct ::timespec tbuf[2];
715 tbuf[0].tv_nsec = UTIME_OMIT;
717 const bool overflowed = !detail::set_times_checked<nanoseconds>(
718 &tbuf[1].tv_sec, &tbuf[1].tv_nsec, new_time);
720 set_or_throw(make_error_code(errc::invalid_argument),
721 ec, "last_write_time", p);
724 if (::utimensat(AT_FDCWD, p.c_str(), tbuf, 0) == -1) {
725 m_ec = detail::capture_errno();
729 set_or_throw(m_ec, ec, "last_write_time", p);
735 void __permissions(const path& p, perms prms, std::error_code *ec)
738 const bool resolve_symlinks = !bool(perms::symlink_nofollow & prms);
739 const bool add_perms = bool(perms::add_perms & prms);
740 const bool remove_perms = bool(perms::remove_perms & prms);
741 _LIBCPP_ASSERT(!(add_perms && remove_perms),
742 "Both add_perms and remove_perms are set");
744 bool set_sym_perms = false;
746 if (!resolve_symlinks || (add_perms || remove_perms)) {
747 std::error_code m_ec;
748 file_status st = resolve_symlinks ? detail::posix_stat(p, &m_ec)
749 : detail::posix_lstat(p, &m_ec);
750 set_sym_perms = is_symlink(st);
751 if (m_ec) return set_or_throw(m_ec, ec, "permissions", p);
752 _LIBCPP_ASSERT(st.permissions() != perms::unknown,
753 "Permissions unexpectedly unknown");
755 prms |= st.permissions();
756 else if (remove_perms)
757 prms = st.permissions() & ~prms;
759 const auto real_perms = detail::posix_convert_perms(prms);
761 # if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD)
762 const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0;
763 if (::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) {
764 return set_or_throw(ec, "permissions", p);
768 return set_or_throw(make_error_code(errc::operation_not_supported),
769 ec, "permissions", p);
770 if (::chmod(p.c_str(), real_perms) == -1) {
771 return set_or_throw(ec, "permissions", p);
778 path __read_symlink(const path& p, std::error_code *ec) {
779 char buff[PATH_MAX + 1];
780 std::error_code m_ec;
782 if ((ret = ::readlink(p.c_str(), buff, PATH_MAX)) == -1) {
783 set_or_throw(ec, "read_symlink", p);
786 _LIBCPP_ASSERT(ret <= PATH_MAX, "TODO");
787 _LIBCPP_ASSERT(ret > 0, "TODO");
794 bool __remove(const path& p, std::error_code *ec) {
796 if (::remove(p.c_str()) == -1) {
797 set_or_throw(ec, "remove", p);
805 std::uintmax_t remove_all_impl(path const & p, std::error_code& ec)
807 const auto npos = static_cast<std::uintmax_t>(-1);
808 const file_status st = __symlink_status(p, &ec);
810 std::uintmax_t count = 1;
811 if (is_directory(st)) {
812 for (directory_iterator it(p, ec); !ec && it != directory_iterator();
814 auto other_count = remove_all_impl(it->path(), ec);
816 count += other_count;
820 if (!__remove(p, &ec)) return npos;
826 std::uintmax_t __remove_all(const path& p, std::error_code *ec) {
828 auto count = remove_all_impl(p, mec);
830 set_or_throw(mec, ec, "remove_all", p);
831 return static_cast<std::uintmax_t>(-1);
837 void __rename(const path& from, const path& to, std::error_code *ec) {
838 if (::rename(from.c_str(), to.c_str()) == -1)
839 set_or_throw(ec, "rename", from, to);
844 void __resize_file(const path& p, std::uintmax_t size, std::error_code *ec) {
845 if (::truncate(p.c_str(), static_cast<long>(size)) == -1)
846 set_or_throw(ec, "resize_file", p);
851 space_info __space(const path& p, std::error_code *ec) {
853 struct statvfs m_svfs = {};
854 if (::statvfs(p.c_str(), &m_svfs) == -1) {
855 set_or_throw(ec, "space", p);
856 si.capacity = si.free = si.available =
857 static_cast<std::uintmax_t>(-1);
861 // Multiply with overflow checking.
862 auto do_mult = [&](std::uintmax_t& out, std::uintmax_t other) {
863 out = other * m_svfs.f_frsize;
864 if (other == 0 || out / other != m_svfs.f_frsize)
865 out = static_cast<std::uintmax_t>(-1);
867 do_mult(si.capacity, m_svfs.f_blocks);
868 do_mult(si.free, m_svfs.f_bfree);
869 do_mult(si.available, m_svfs.f_bavail);
873 file_status __status(const path& p, std::error_code *ec) {
874 return detail::posix_stat(p, ec);
877 file_status __symlink_status(const path& p, std::error_code *ec) {
878 return detail::posix_lstat(p, ec);
881 path __system_complete(const path& p, std::error_code *ec) {
883 return absolute(p, current_path());
886 path __temp_directory_path(std::error_code* ec) {
887 const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
888 const char* ret = nullptr;
890 for (auto& ep : env_paths)
891 if ((ret = std::getenv(ep)))
897 std::error_code m_ec;
898 if (!exists(p, m_ec) || !is_directory(p, m_ec)) {
899 if (!m_ec || m_ec == make_error_code(errc::no_such_file_or_directory))
900 m_ec = make_error_code(errc::not_a_directory);
901 set_or_throw(m_ec, ec, "temp_directory_path");
910 // An absolute path is composed according to the table in [fs.op.absolute].
911 path absolute(const path& p, const path& base) {
912 auto root_name = p.root_name();
913 auto root_dir = p.root_directory();
915 if (!root_name.empty() && !root_dir.empty())
918 auto abs_base = base.is_absolute() ? base : absolute(base);
920 /* !has_root_name && !has_root_dir */
921 if (root_name.empty() && root_dir.empty())
925 else if (!root_name.empty()) /* has_root_name && !has_root_dir */
927 return root_name / abs_base.root_directory()
929 abs_base.relative_path() / p.relative_path();
931 else /* !has_root_name && has_root_dir */
933 if (abs_base.has_root_name())
934 return abs_base.root_name() / p;
935 // else p is absolute, return outside of block
940 _LIBCPP_END_NAMESPACE_EXPERIMENTAL_FILESYSTEM