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;
40 inline std::error_code capture_errno() {
41 _LIBCPP_ASSERT(errno, "Expected errno to be non-zero");
42 std::error_code m_ec(errno, std::generic_category());
46 void set_or_throw(std::error_code const& m_ec, std::error_code* ec,
47 const char* msg, path const& p = {}, path const& p2 = {})
52 string msg_s("std::experimental::filesystem::");
54 __libcpp_throw(filesystem_error(msg_s, p, p2, m_ec));
58 void set_or_throw(std::error_code* ec, const char* msg,
59 path const& p = {}, path const& p2 = {})
61 return set_or_throw(capture_errno(), ec, msg, p, p2);
64 perms posix_get_perms(const struct ::stat & st) noexcept {
65 return static_cast<perms>(st.st_mode) & perms::mask;
68 ::mode_t posix_convert_perms(perms prms) {
69 return static_cast< ::mode_t>(prms & perms::mask);
72 file_status create_file_status(std::error_code& m_ec, path const& p,
73 struct ::stat& path_stat,
77 if (m_ec && (m_ec.value() == ENOENT || m_ec.value() == ENOTDIR)) {
78 return file_status(file_type::not_found);
81 set_or_throw(m_ec, ec, "posix_stat", p);
82 return file_status(file_type::none);
87 auto const mode = path_stat.st_mode;
88 if (S_ISLNK(mode)) fs_tmp.type(file_type::symlink);
89 else if (S_ISREG(mode)) fs_tmp.type(file_type::regular);
90 else if (S_ISDIR(mode)) fs_tmp.type(file_type::directory);
91 else if (S_ISBLK(mode)) fs_tmp.type(file_type::block);
92 else if (S_ISCHR(mode)) fs_tmp.type(file_type::character);
93 else if (S_ISFIFO(mode)) fs_tmp.type(file_type::fifo);
94 else if (S_ISSOCK(mode)) fs_tmp.type(file_type::socket);
95 else fs_tmp.type(file_type::unknown);
97 fs_tmp.permissions(detail::posix_get_perms(path_stat));
101 file_status posix_stat(path const & p, struct ::stat& path_stat,
104 std::error_code m_ec;
105 if (::stat(p.c_str(), &path_stat) == -1)
106 m_ec = detail::capture_errno();
107 return create_file_status(m_ec, p, path_stat, ec);
110 file_status posix_stat(path const & p, std::error_code* ec) {
111 struct ::stat path_stat;
112 return posix_stat(p, path_stat, ec);
115 file_status posix_lstat(path const & p, struct ::stat & path_stat,
118 std::error_code m_ec;
119 if (::lstat(p.c_str(), &path_stat) == -1)
120 m_ec = detail::capture_errno();
121 return create_file_status(m_ec, p, path_stat, ec);
124 file_status posix_lstat(path const & p, std::error_code* ec) {
125 struct ::stat path_stat;
126 return posix_lstat(p, path_stat, ec);
129 bool stat_equivalent(struct ::stat& st1, struct ::stat& st2) {
130 return (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
136 bool copy_file_impl(const path& from, const path& to, perms from_perms,
139 std::ifstream in(from.c_str(), std::ios::binary);
140 std::ofstream out(to.c_str(), std::ios::binary);
142 if (in.good() && out.good()) {
143 using InIt = std::istreambuf_iterator<char>;
144 using OutIt = std::ostreambuf_iterator<char>;
148 std::copy(bin, ein, bout);
150 if (out.fail() || in.fail()) {
151 set_or_throw(make_error_code(errc::operation_not_permitted),
152 ec, "copy_file", from, to);
155 __permissions(to, from_perms, ec);
156 // TODO what if permissions fails?
160 }} // end namespace detail
162 using detail::set_or_throw;
164 path __canonical(path const & orig_p, const path& base, std::error_code *ec)
166 path p = absolute(orig_p, base);
167 char buff[PATH_MAX + 1];
169 if ((ret = ::realpath(p.c_str(), buff)) == nullptr) {
170 set_or_throw(ec, "canonical", orig_p, base);
177 void __copy(const path& from, const path& to, copy_options options,
180 const bool sym_status = bool(options &
181 (copy_options::create_symlinks | copy_options::skip_symlinks));
183 const bool sym_status2 = bool(options &
184 copy_options::copy_symlinks);
186 std::error_code m_ec;
187 struct ::stat f_st = {};
188 const file_status f = sym_status || sym_status2
189 ? detail::posix_lstat(from, f_st, &m_ec)
190 : detail::posix_stat(from, f_st, &m_ec);
192 return set_or_throw(m_ec, ec, "copy", from, to);
194 struct ::stat t_st = {};
195 const file_status t = sym_status ? detail::posix_lstat(to, t_st, &m_ec)
196 : detail::posix_stat(to, t_st, &m_ec);
198 if (not status_known(t))
199 return set_or_throw(m_ec, ec, "copy", from, to);
201 if (!exists(f) || is_other(f) || is_other(t)
202 || (is_directory(f) && is_regular_file(t))
203 || detail::stat_equivalent(f_st, t_st))
205 return set_or_throw(make_error_code(errc::function_not_supported),
206 ec, "copy", from, to);
212 if (bool(copy_options::skip_symlinks & options)) {
214 } else if (not exists(t)) {
215 __copy_symlink(from, to, ec);
217 set_or_throw(make_error_code(errc::file_exists),
218 ec, "copy", from, to);
222 else if (is_regular_file(f)) {
223 if (bool(copy_options::directories_only & options)) {
226 else if (bool(copy_options::create_symlinks & options)) {
227 __create_symlink(from, to, ec);
229 else if (bool(copy_options::create_hard_links & options)) {
230 __create_hard_link(from, to, ec);
232 else if (is_directory(t)) {
233 __copy_file(from, to / from.filename(), options, ec);
235 __copy_file(from, to, options, ec);
239 else if (is_directory(f)) {
240 if (not bool(copy_options::recursive & options) &&
241 bool(copy_options::__in_recursive_copy & options))
247 // create directory to with attributes from 'from'.
248 __create_directory(to, from, ec);
249 if (ec && *ec) { return; }
251 directory_iterator it = ec ? directory_iterator(from, *ec)
252 : directory_iterator(from);
253 if (ec && *ec) { return; }
254 std::error_code m_ec;
255 for (; it != directory_iterator(); it.increment(m_ec)) {
256 if (m_ec) return set_or_throw(m_ec, ec, "copy", from, to);
257 __copy(it->path(), to / it->path().filename(),
258 options | copy_options::__in_recursive_copy, ec);
259 if (ec && *ec) { return; }
265 bool __copy_file(const path& from, const path& to, copy_options options,
270 std::error_code m_ec;
271 auto from_st = detail::posix_stat(from, &m_ec);
272 if (not is_regular_file(from_st)) {
274 m_ec = make_error_code(errc::not_supported);
275 set_or_throw(m_ec, ec, "copy_file", from, to);
279 auto to_st = detail::posix_stat(to, &m_ec);
280 if (!status_known(to_st)) {
281 set_or_throw(m_ec, ec, "copy_file", from, to);
285 const bool to_exists = exists(to_st);
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);
308 void __copy_symlink(const path& existing_symlink, const path& new_symlink,
311 const path real_path(__read_symlink(existing_symlink, ec));
312 if (ec && *ec) { return; }
313 // NOTE: proposal says you should detect if you should call
314 // create_symlink or create_directory_symlink. I don't think this
315 // is needed with POSIX
316 __create_symlink(real_path, new_symlink, ec);
320 bool __create_directories(const path& p, std::error_code *ec)
322 std::error_code m_ec;
323 auto const st = detail::posix_stat(p, &m_ec);
324 if (!status_known(st)) {
325 set_or_throw(m_ec, ec, "create_directories", p);
328 else if (is_directory(st)) {
332 else if (exists(st)) {
333 set_or_throw(make_error_code(errc::file_exists),
334 ec, "create_directories", p);
338 const path parent = p.parent_path();
339 if (!parent.empty()) {
340 const file_status parent_st = status(parent, m_ec);
341 if (not status_known(parent_st)) {
342 set_or_throw(m_ec, ec, "create_directories", p);
345 if (not exists(parent_st)) {
346 __create_directories(parent, ec);
347 if (ec && *ec) { return false; }
350 return __create_directory(p, ec);
353 bool __create_directory(const path& p, std::error_code *ec)
356 if (::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0)
358 if (errno != EEXIST || !is_directory(p))
359 set_or_throw(ec, "create_directory", p);
363 bool __create_directory(path const & p, path const & attributes,
366 struct ::stat attr_stat;
368 auto st = detail::posix_stat(attributes, attr_stat, &mec);
369 if (!status_known(st)) {
370 set_or_throw(mec, ec, "create_directory", p, attributes);
374 if (::mkdir(p.c_str(), attr_stat.st_mode) == 0)
376 if (errno != EEXIST || !is_directory(p))
377 set_or_throw(ec, "create_directory", p, attributes);
381 void __create_directory_symlink(path const & from, path const & to,
382 std::error_code *ec){
383 if (::symlink(from.c_str(), to.c_str()) != 0)
384 set_or_throw(ec, "create_directory_symlink", from, to);
389 void __create_hard_link(const path& from, const path& to, std::error_code *ec){
390 if (::link(from.c_str(), to.c_str()) == -1)
391 set_or_throw(ec, "create_hard_link", from, to);
396 void __create_symlink(path const & from, path const & to, std::error_code *ec) {
398 if (::symlink(from.c_str(), to.c_str()) == -1)
399 set_or_throw(ec, "create_symlink", from, to);
404 path __current_path(std::error_code *ec) {
405 auto size = ::pathconf(".", _PC_PATH_MAX);
406 _LIBCPP_ASSERT(size >= 0, "pathconf returned a 0 as max size");
408 auto buff = std::unique_ptr<char[]>(new char[size + 1]);
410 if ((ret = ::getcwd(buff.get(), static_cast<size_t>(size))) == nullptr) {
411 set_or_throw(ec, "current_path");
418 void __current_path(const path& p, std::error_code *ec) {
419 if (::chdir(p.c_str()) == -1)
420 set_or_throw(ec, "current_path", p);
425 bool __equivalent(const path& p1, const path& p2, std::error_code *ec)
427 std::error_code ec1, ec2;
428 struct ::stat st1 = {};
429 struct ::stat st2 = {};
430 auto s1 = detail::posix_stat(p1.native(), st1, &ec1);
431 auto s2 = detail::posix_stat(p2.native(), st2, &ec2);
433 if ((!exists(s1) && !exists(s2)) || (is_other(s1) && is_other(s2))) {
434 set_or_throw(make_error_code(errc::not_supported), ec,
435 "equivalent", p1, p2);
439 return (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
443 std::uintmax_t __file_size(const path& p, std::error_code *ec)
445 std::error_code m_ec;
447 file_status fst = detail::posix_stat(p, st, &m_ec);
448 if (!exists(fst) || !is_regular_file(fst)) {
450 m_ec = make_error_code(errc::not_supported);
451 set_or_throw(m_ec, ec, "file_size", p);
452 return static_cast<uintmax_t>(-1);
454 // is_regular_file(p) == true
456 return static_cast<std::uintmax_t>(st.st_size);
459 std::uintmax_t __hard_link_count(const path& p, std::error_code *ec)
461 std::error_code m_ec;
463 detail::posix_stat(p, st, &m_ec);
465 set_or_throw(m_ec, ec, "hard_link_count", p);
466 return static_cast<std::uintmax_t>(-1);
469 return static_cast<std::uintmax_t>(st.st_nlink);
473 bool __fs_is_empty(const path& p, std::error_code *ec)
476 std::error_code m_ec;
478 auto st = detail::posix_stat(p, pst, &m_ec);
479 if (is_directory(st))
480 return directory_iterator(p) == directory_iterator{};
481 else if (is_regular_file(st))
482 return static_cast<std::uintmax_t>(pst.st_size) == 0;
484 set_or_throw(m_ec, ec, "is_empty", p);
489 namespace detail { namespace {
491 template <class CType, class ChronoType>
492 bool checked_set(CType* out, ChronoType time) {
493 using Lim = numeric_limits<CType>;
494 if (time > Lim::max() || time < Lim::min())
496 *out = static_cast<CType>(time);
500 constexpr long long min_seconds = file_time_type::duration::min().count()
501 / file_time_type::period::den;
503 template <class SubSecDurT, class SubSecT>
504 bool set_times_checked(time_t* sec_out, SubSecT* subsec_out, file_time_type tp) {
505 using namespace chrono;
506 auto dur = tp.time_since_epoch();
507 auto sec_dur = duration_cast<seconds>(dur);
508 auto subsec_dur = duration_cast<SubSecDurT>(dur - sec_dur);
509 // The tv_nsec and tv_usec fields must not be negative so adjust accordingly
510 if (subsec_dur.count() < 0) {
511 if (sec_dur.count() > min_seconds) {
513 sec_dur -= seconds(1);
514 subsec_dur += seconds(1);
516 subsec_dur = SubSecDurT::zero();
519 return checked_set(sec_out, sec_dur.count())
520 && checked_set(subsec_out, subsec_dur.count());
523 }} // end namespace detail
526 file_time_type __last_write_time(const path& p, std::error_code *ec)
528 std::error_code m_ec;
530 detail::posix_stat(p, st, &m_ec);
532 set_or_throw(m_ec, ec, "last_write_time", p);
533 return file_time_type::min();
536 return file_time_type::clock::from_time_t(st.st_mtime);
539 void __last_write_time(const path& p, file_time_type new_time,
542 using namespace std::chrono;
543 std::error_code m_ec;
545 // We can use the presence of UTIME_OMIT to detect platforms that do not
546 // provide utimensat.
547 #if !defined(UTIME_OMIT)
548 // This implementation has a race condition between determining the
549 // last access time and attempting to set it to the same value using
552 file_status fst = detail::posix_stat(p, st, &m_ec);
553 if (m_ec && !status_known(fst)) {
554 set_or_throw(m_ec, ec, "last_write_time", p);
557 struct ::timeval tbuf[2];
558 tbuf[0].tv_sec = st.st_atime;
560 const bool overflowed = !detail::set_times_checked<microseconds>(
561 &tbuf[1].tv_sec, &tbuf[1].tv_usec, new_time);
564 set_or_throw(make_error_code(errc::invalid_argument), ec,
565 "last_write_time", p);
568 if (::utimes(p.c_str(), tbuf) == -1) {
569 m_ec = detail::capture_errno();
572 struct ::timespec tbuf[2];
574 tbuf[0].tv_nsec = UTIME_OMIT;
576 const bool overflowed = !detail::set_times_checked<nanoseconds>(
577 &tbuf[1].tv_sec, &tbuf[1].tv_nsec, new_time);
579 set_or_throw(make_error_code(errc::invalid_argument),
580 ec, "last_write_time", p);
583 if (::utimensat(AT_FDCWD, p.c_str(), tbuf, 0) == -1) {
584 m_ec = detail::capture_errno();
588 set_or_throw(m_ec, ec, "last_write_time", p);
594 void __permissions(const path& p, perms prms, std::error_code *ec)
597 const bool resolve_symlinks = !bool(perms::symlink_nofollow & prms);
598 const bool add_perms = bool(perms::add_perms & prms);
599 const bool remove_perms = bool(perms::remove_perms & prms);
600 _LIBCPP_ASSERT(!(add_perms && remove_perms),
601 "Both add_perms and remove_perms are set");
603 bool set_sym_perms = false;
605 if (!resolve_symlinks || (add_perms || remove_perms)) {
606 std::error_code m_ec;
607 file_status st = resolve_symlinks ? detail::posix_stat(p, &m_ec)
608 : detail::posix_lstat(p, &m_ec);
609 set_sym_perms = is_symlink(st);
610 if (m_ec) return set_or_throw(m_ec, ec, "permissions", p);
611 _LIBCPP_ASSERT(st.permissions() != perms::unknown,
612 "Permissions unexpectedly unknown");
614 prms |= st.permissions();
615 else if (remove_perms)
616 prms = st.permissions() & ~prms;
618 const auto real_perms = detail::posix_convert_perms(prms);
620 # if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD)
621 const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0;
622 if (::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) {
623 return set_or_throw(ec, "permissions", p);
627 return set_or_throw(make_error_code(errc::operation_not_supported),
628 ec, "permissions", p);
629 if (::chmod(p.c_str(), real_perms) == -1) {
630 return set_or_throw(ec, "permissions", p);
637 path __read_symlink(const path& p, std::error_code *ec) {
638 char buff[PATH_MAX + 1];
639 std::error_code m_ec;
641 if ((ret = ::readlink(p.c_str(), buff, PATH_MAX)) == -1) {
642 set_or_throw(ec, "read_symlink", p);
645 _LIBCPP_ASSERT(ret <= PATH_MAX, "TODO");
646 _LIBCPP_ASSERT(ret > 0, "TODO");
653 bool __remove(const path& p, std::error_code *ec) {
655 if (::remove(p.c_str()) == -1) {
656 set_or_throw(ec, "remove", p);
664 std::uintmax_t remove_all_impl(path const & p, std::error_code& ec)
666 const auto npos = static_cast<std::uintmax_t>(-1);
667 const file_status st = __symlink_status(p, &ec);
669 std::uintmax_t count = 1;
670 if (is_directory(st)) {
671 for (directory_iterator it(p, ec); !ec && it != directory_iterator();
673 auto other_count = remove_all_impl(it->path(), ec);
675 count += other_count;
679 if (!__remove(p, &ec)) return npos;
685 std::uintmax_t __remove_all(const path& p, std::error_code *ec) {
687 auto count = remove_all_impl(p, mec);
689 set_or_throw(mec, ec, "remove_all", p);
690 return static_cast<std::uintmax_t>(-1);
696 void __rename(const path& from, const path& to, std::error_code *ec) {
697 if (::rename(from.c_str(), to.c_str()) == -1)
698 set_or_throw(ec, "rename", from, to);
703 void __resize_file(const path& p, std::uintmax_t size, std::error_code *ec) {
704 if (::truncate(p.c_str(), static_cast<long>(size)) == -1)
705 set_or_throw(ec, "resize_file", p);
710 space_info __space(const path& p, std::error_code *ec) {
712 struct statvfs m_svfs = {};
713 if (::statvfs(p.c_str(), &m_svfs) == -1) {
714 set_or_throw(ec, "space", p);
715 si.capacity = si.free = si.available =
716 static_cast<std::uintmax_t>(-1);
720 // Multiply with overflow checking.
721 auto do_mult = [&](std::uintmax_t& out, std::uintmax_t other) {
722 out = other * m_svfs.f_frsize;
723 if (out / other != m_svfs.f_frsize || other == 0)
724 out = static_cast<std::uintmax_t>(-1);
726 do_mult(si.capacity, m_svfs.f_blocks);
727 do_mult(si.free, m_svfs.f_bfree);
728 do_mult(si.available, m_svfs.f_bavail);
732 file_status __status(const path& p, std::error_code *ec) {
733 return detail::posix_stat(p, ec);
736 file_status __symlink_status(const path& p, std::error_code *ec) {
737 return detail::posix_lstat(p, ec);
740 path __system_complete(const path& p, std::error_code *ec) {
742 return absolute(p, current_path());
745 path __temp_directory_path(std::error_code *ec) {
746 const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
747 const char* ret = nullptr;
748 for (auto & ep : env_paths) {
749 if ((ret = std::getenv(ep)))
752 path p(ret ? ret : "/tmp");
753 std::error_code m_ec;
754 if (is_directory(p, m_ec)) {
758 if (!m_ec || m_ec == make_error_code(errc::no_such_file_or_directory))
759 m_ec = make_error_code(errc::not_a_directory);
760 set_or_throw(m_ec, ec, "temp_directory_path");
764 // An absolute path is composed according to the table in [fs.op.absolute].
765 path absolute(const path& p, const path& base) {
766 auto root_name = p.root_name();
767 auto root_dir = p.root_directory();
769 if (!root_name.empty() && !root_dir.empty())
772 auto abs_base = base.is_absolute() ? base : absolute(base);
774 /* !has_root_name && !has_root_dir */
775 if (root_name.empty() && root_dir.empty())
779 else if (!root_name.empty()) /* has_root_name && !has_root_dir */
781 return root_name / abs_base.root_directory()
783 abs_base.relative_path() / p.relative_path();
785 else /* !has_root_name && has_root_dir */
787 if (abs_base.has_root_name())
788 return abs_base.root_name() / p;
789 // else p is absolute, return outside of block
794 _LIBCPP_END_NAMESPACE_EXPERIMENTAL_FILESYSTEM