blob: 369996fcbe62d445e5ad5f63643adb6b99934363 [file] [log] [blame]
Eric Fiselier6e9a6942016-06-17 19:46:40 +00001//===--------------------- filesystem/ops.cpp -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "experimental/filesystem"
11#include "iterator"
12#include "fstream"
13#include "type_traits"
14#include "random" /* for unique_path */
15#include "cstdlib"
16#include "climits"
17
18#include <unistd.h>
19#include <sys/stat.h>
20#include <sys/statvfs.h>
21#include <fcntl.h> /* values for fchmodat */
Eric Fiselier2730c252016-06-18 19:11:40 +000022#if !defined(UTIME_OMIT)
Eric Fiselier6e9a6942016-06-17 19:46:40 +000023#include <sys/time.h> // for ::utimes as used in __last_write_time
24#endif
25
26_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_FILESYSTEM
27
28filesystem_error::~filesystem_error() {}
29
30
31// POSIX HELPERS
32
33namespace detail { namespace {
34
35using value_type = path::value_type;
36using string_type = path::string_type;
37
38
39
40inline 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());
43 return m_ec;
44}
45
46void set_or_throw(std::error_code const& m_ec, std::error_code* ec,
47 const char* msg, path const& p = {}, path const& p2 = {})
48{
49 if (ec) {
50 *ec = m_ec;
51 } else {
52 string msg_s("std::experimental::filesystem::");
53 msg_s += msg;
54 __libcpp_throw(filesystem_error(msg_s, p, p2, m_ec));
55 }
56}
57
58void set_or_throw(std::error_code* ec, const char* msg,
59 path const& p = {}, path const& p2 = {})
60{
61 return set_or_throw(capture_errno(), ec, msg, p, p2);
62}
63
64perms posix_get_perms(const struct ::stat & st) noexcept {
65 return static_cast<perms>(st.st_mode) & perms::mask;
66}
67
68::mode_t posix_convert_perms(perms prms) {
69 return static_cast< ::mode_t>(prms & perms::mask);
70}
71
72file_status create_file_status(std::error_code& m_ec, path const& p,
73 struct ::stat& path_stat,
74 std::error_code* ec)
75{
76 if (ec) *ec = m_ec;
77 if (m_ec && (m_ec.value() == ENOENT || m_ec.value() == ENOTDIR)) {
78 return file_status(file_type::not_found);
79 }
80 else if (m_ec) {
81 set_or_throw(m_ec, ec, "posix_stat", p);
82 return file_status(file_type::none);
83 }
84 // else
85
86 file_status fs_tmp;
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);
96
97 fs_tmp.permissions(detail::posix_get_perms(path_stat));
98 return fs_tmp;
99}
100
101file_status posix_stat(path const & p, struct ::stat& path_stat,
102 std::error_code* ec)
103{
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);
108}
109
110file_status posix_stat(path const & p, std::error_code* ec) {
111 struct ::stat path_stat;
112 return posix_stat(p, path_stat, ec);
113}
114
115file_status posix_lstat(path const & p, struct ::stat & path_stat,
116 std::error_code* ec)
117{
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);
122}
123
124file_status posix_lstat(path const & p, std::error_code* ec) {
125 struct ::stat path_stat;
126 return posix_lstat(p, path_stat, ec);
127}
128
129bool stat_equivalent(struct ::stat& st1, struct ::stat& st2) {
130 return (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
131}
132
133// DETAIL::MISC
134
135
136bool copy_file_impl(const path& from, const path& to, perms from_perms,
137 std::error_code *ec)
138{
139 std::ifstream in(from.c_str(), std::ios::binary);
140 std::ofstream out(to.c_str(), std::ios::binary);
141
142 if (in.good() && out.good()) {
143 using InIt = std::istreambuf_iterator<char>;
144 using OutIt = std::ostreambuf_iterator<char>;
145 InIt bin(in);
146 InIt ein;
147 OutIt bout(out);
148 std::copy(bin, ein, bout);
149 }
150 if (out.fail() || in.fail()) {
151 set_or_throw(make_error_code(errc::operation_not_permitted),
152 ec, "copy_file", from, to);
153 return false;
154 }
155 __permissions(to, from_perms, ec);
156 // TODO what if permissions fails?
157 return true;
158}
159
160}} // end namespace detail
161
162using detail::set_or_throw;
163
164path __canonical(path const & orig_p, const path& base, std::error_code *ec)
165{
166 path p = absolute(orig_p, base);
167 char buff[PATH_MAX + 1];
168 char *ret;
169 if ((ret = ::realpath(p.c_str(), buff)) == nullptr) {
170 set_or_throw(ec, "canonical", orig_p, base);
171 return {};
172 }
173 if (ec) ec->clear();
174 return {ret};
175}
176
177void __copy(const path& from, const path& to, copy_options options,
178 std::error_code *ec)
179{
180 const bool sym_status = bool(options &
181 (copy_options::create_symlinks | copy_options::skip_symlinks));
182
183 const bool sym_status2 = bool(options &
184 copy_options::copy_symlinks);
185
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);
191 if (m_ec)
192 return set_or_throw(m_ec, ec, "copy", from, to);
193
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);
197
198 if (not status_known(t))
199 return set_or_throw(m_ec, ec, "copy", from, to);
200
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))
204 {
205 return set_or_throw(make_error_code(errc::function_not_supported),
206 ec, "copy", from, to);
207 }
208
209 if (ec) ec->clear();
210
211 if (is_symlink(f)) {
212 if (bool(copy_options::skip_symlinks & options)) {
213 // do nothing
214 } else if (not exists(t)) {
215 __copy_symlink(from, to, ec);
216 } else {
217 set_or_throw(make_error_code(errc::file_exists),
218 ec, "copy", from, to);
219 }
220 return;
221 }
222 else if (is_regular_file(f)) {
223 if (bool(copy_options::directories_only & options)) {
224 // do nothing
225 }
226 else if (bool(copy_options::create_symlinks & options)) {
227 __create_symlink(from, to, ec);
228 }
229 else if (bool(copy_options::create_hard_links & options)) {
230 __create_hard_link(from, to, ec);
231 }
232 else if (is_directory(t)) {
233 __copy_file(from, to / from.filename(), options, ec);
234 } else {
235 __copy_file(from, to, options, ec);
236 }
237 return;
238 }
239 else if (is_directory(f)) {
240 if (not bool(copy_options::recursive & options) &&
241 bool(copy_options::__in_recursive_copy & options))
242 {
243 return;
244 }
245
246 if (!exists(t)) {
247 // create directory to with attributes from 'from'.
248 __create_directory(to, from, ec);
249 if (ec && *ec) { return; }
250 }
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; }
260 }
261 }
262}
263
264
265bool __copy_file(const path& from, const path& to, copy_options options,
266 std::error_code *ec)
267{
268 if (ec) ec->clear();
269
270 std::error_code m_ec;
271 auto from_st = detail::posix_stat(from, &m_ec);
272 if (not is_regular_file(from_st)) {
273 if (not m_ec)
274 m_ec = make_error_code(errc::not_supported);
275 set_or_throw(m_ec, ec, "copy_file", from, to);
276 return false;
277 }
278
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);
282 return false;
283 }
284
285 const bool to_exists = exists(to_st);
286 if (to_exists && bool(copy_options::skip_existing & options)) {
287 return false;
288 }
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) {
295 return false;
296 }
297 return detail::copy_file_impl(from, to, from_st.permissions(), ec);
298 }
299 else if (!to_exists || bool(copy_options::overwrite_existing & options)) {
300 return detail::copy_file_impl(from, to, from_st.permissions(), ec);
301 }
302 else {
303 set_or_throw(make_error_code(errc::file_exists), ec, "copy", from, to);
304 return false;
305 }
306}
307
308void __copy_symlink(const path& existing_symlink, const path& new_symlink,
309 std::error_code *ec)
310{
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);
317}
318
319
320bool __create_directories(const path& p, std::error_code *ec)
321{
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);
326 return false;
327 }
328 else if (is_directory(st)) {
329 if (ec) ec->clear();
330 return false;
331 }
332 else if (exists(st)) {
333 set_or_throw(make_error_code(errc::file_exists),
334 ec, "create_directories", p);
335 return false;
336 }
337
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);
343 return false;
344 }
345 if (not exists(parent_st)) {
346 __create_directories(parent, ec);
347 if (ec && *ec) { return false; }
348 }
349 }
350 return __create_directory(p, ec);
351}
352
353bool __create_directory(const path& p, std::error_code *ec)
354{
355 if (ec) ec->clear();
356 if (::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0)
357 return true;
358 if (errno != EEXIST || !is_directory(p))
359 set_or_throw(ec, "create_directory", p);
360 return false;
361}
362
363bool __create_directory(path const & p, path const & attributes,
364 std::error_code *ec)
365{
366 struct ::stat attr_stat;
367 std::error_code mec;
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);
371 return false;
372 }
373 if (ec) ec->clear();
374 if (::mkdir(p.c_str(), attr_stat.st_mode) == 0)
375 return true;
376 if (errno != EEXIST || !is_directory(p))
377 set_or_throw(ec, "create_directory", p, attributes);
378 return false;
379}
380
381void __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);
385 else if (ec)
386 ec->clear();
387}
388
389void __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);
392 else if (ec)
393 ec->clear();
394}
395
396void __create_symlink(path const & from, path const & to, std::error_code *ec) {
397
398 if (::symlink(from.c_str(), to.c_str()) == -1)
399 set_or_throw(ec, "create_symlink", from, to);
400 else if (ec)
401 ec->clear();
402}
403
404path __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");
407
408 auto buff = std::unique_ptr<char[]>(new char[size + 1]);
409 char* ret;
410 if ((ret = ::getcwd(buff.get(), static_cast<size_t>(size))) == nullptr) {
411 set_or_throw(ec, "current_path");
412 return {};
413 }
414 if (ec) ec->clear();
415 return {buff.get()};
416}
417
418void __current_path(const path& p, std::error_code *ec) {
419 if (::chdir(p.c_str()) == -1)
420 set_or_throw(ec, "current_path", p);
421 else if (ec)
422 ec->clear();
423}
424
425bool __equivalent(const path& p1, const path& p2, std::error_code *ec)
426{
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);
432
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);
436 return false;
437 }
438 if (ec) ec->clear();
439 return (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
440}
441
442
443std::uintmax_t __file_size(const path& p, std::error_code *ec)
444{
445 std::error_code m_ec;
446 struct ::stat st;
447 file_status fst = detail::posix_stat(p, st, &m_ec);
448 if (!exists(fst) || !is_regular_file(fst)) {
449 if (!m_ec)
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);
453 }
454 // is_regular_file(p) == true
455 if (ec) ec->clear();
456 return static_cast<std::uintmax_t>(st.st_size);
457}
458
459std::uintmax_t __hard_link_count(const path& p, std::error_code *ec)
460{
461 std::error_code m_ec;
462 struct ::stat st;
463 detail::posix_stat(p, st, &m_ec);
464 if (m_ec) {
465 set_or_throw(m_ec, ec, "hard_link_count", p);
466 return static_cast<std::uintmax_t>(-1);
467 }
468 if (ec) ec->clear();
469 return static_cast<std::uintmax_t>(st.st_nlink);
470}
471
472
473bool __fs_is_empty(const path& p, std::error_code *ec)
474{
475 if (ec) ec->clear();
476 std::error_code m_ec;
477 struct ::stat pst;
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;
483 // else
484 set_or_throw(m_ec, ec, "is_empty", p);
485 return false;
486}
487
488
Eric Fiselier70f7afe2016-06-19 02:04:49 +0000489namespace detail { namespace {
490
491template <class CType, class ChronoType>
492bool checked_set(CType* out, ChronoType time) {
493 using Lim = numeric_limits<CType>;
494 if (time > Lim::max() || time < Lim::min())
495 return false;
496 *out = static_cast<CType>(time);
497 return true;
498}
499
500constexpr long long min_seconds = file_time_type::duration::min().count()
501 / file_time_type::period::den;
502
503template <class SubSecDurT, class SubSecT>
504bool 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) {
512
513 sec_dur -= seconds(1);
514 subsec_dur += seconds(1);
515 } else {
516 subsec_dur = SubSecDurT::zero();
517 }
518 }
519 return checked_set(sec_out, sec_dur.count())
520 && checked_set(subsec_out, subsec_dur.count());
521}
522
523}} // end namespace detail
524
525
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000526file_time_type __last_write_time(const path& p, std::error_code *ec)
527{
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000528 std::error_code m_ec;
529 struct ::stat st;
530 detail::posix_stat(p, st, &m_ec);
531 if (m_ec) {
532 set_or_throw(m_ec, ec, "last_write_time", p);
533 return file_time_type::min();
534 }
535 if (ec) ec->clear();
Eric Fiselier70f7afe2016-06-19 02:04:49 +0000536 return file_time_type::clock::from_time_t(st.st_mtime);
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000537}
538
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000539void __last_write_time(const path& p, file_time_type new_time,
540 std::error_code *ec)
541{
542 using namespace std::chrono;
543 std::error_code m_ec;
544
Eric Fiselier2730c252016-06-18 19:11:40 +0000545 // We can use the presence of UTIME_OMIT to detect platforms that do not
546 // provide utimensat.
547#if !defined(UTIME_OMIT)
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000548 // This implementation has a race condition between determining the
549 // last access time and attempting to set it to the same value using
550 // ::utimes
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000551 struct ::stat st;
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);
555 return;
556 }
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000557 struct ::timeval tbuf[2];
Eric Fiselier70f7afe2016-06-19 02:04:49 +0000558 tbuf[0].tv_sec = st.st_atime;
559 tbuf[0].tv_usec = 0;
560 const bool overflowed = !detail::set_times_checked<microseconds>(
561 &tbuf[1].tv_sec, &tbuf[1].tv_usec, new_time);
562
563 if (overflowed) {
564 set_or_throw(make_error_code(errc::invalid_argument), ec,
565 "last_write_time", p);
566 return;
567 }
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000568 if (::utimes(p.c_str(), tbuf) == -1) {
569 m_ec = detail::capture_errno();
570 }
571#else
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000572 struct ::timespec tbuf[2];
573 tbuf[0].tv_sec = 0;
574 tbuf[0].tv_nsec = UTIME_OMIT;
Eric Fiselier70f7afe2016-06-19 02:04:49 +0000575
576 const bool overflowed = !detail::set_times_checked<nanoseconds>(
577 &tbuf[1].tv_sec, &tbuf[1].tv_nsec, new_time);
578 if (overflowed) {
579 set_or_throw(make_error_code(errc::invalid_argument),
580 ec, "last_write_time", p);
581 return;
582 }
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000583 if (::utimensat(AT_FDCWD, p.c_str(), tbuf, 0) == -1) {
584 m_ec = detail::capture_errno();
585 }
586#endif
587 if (m_ec)
588 set_or_throw(m_ec, ec, "last_write_time", p);
589 else if (ec)
590 ec->clear();
591}
592
593
594void __permissions(const path& p, perms prms, std::error_code *ec)
595{
596
Eric Fiselier7c96ddb2016-06-21 22:42:42 +0000597 const bool resolve_symlinks = !bool(perms::symlink_nofollow & prms);
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000598 const bool add_perms = bool(perms::add_perms & prms);
599 const bool remove_perms = bool(perms::remove_perms & prms);
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000600 _LIBCPP_ASSERT(!(add_perms && remove_perms),
601 "Both add_perms and remove_perms are set");
602
Eric Fiselier55e084d2016-06-22 07:57:38 +0000603 bool set_sym_perms = false;
604 prms &= perms::mask;
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);
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000610 if (m_ec) return set_or_throw(m_ec, ec, "permissions", p);
Eric Fiselier55e084d2016-06-22 07:57:38 +0000611 _LIBCPP_ASSERT(st.permissions() != perms::unknown,
612 "Permissions unexpectedly unknown");
613 if (add_perms)
614 prms |= st.permissions();
615 else if (remove_perms)
616 prms = st.permissions() & ~prms;
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000617 }
Eric Fiselier55e084d2016-06-22 07:57:38 +0000618 const auto real_perms = detail::posix_convert_perms(prms);
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000619
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) {
Eric Fiselier7c96ddb2016-06-21 22:42:42 +0000623 return set_or_throw(ec, "permissions", p);
624 }
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000625# else
626 if (set_sym_perms)
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) {
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000630 return set_or_throw(ec, "permissions", p);
631 }
Eric Fiselier7c96ddb2016-06-21 22:42:42 +0000632# endif
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000633 if (ec) ec->clear();
634}
635
636
637path __read_symlink(const path& p, std::error_code *ec) {
638 char buff[PATH_MAX + 1];
639 std::error_code m_ec;
640 ::ssize_t ret;
641 if ((ret = ::readlink(p.c_str(), buff, PATH_MAX)) == -1) {
642 set_or_throw(ec, "read_symlink", p);
643 return {};
644 }
645 _LIBCPP_ASSERT(ret <= PATH_MAX, "TODO");
646 _LIBCPP_ASSERT(ret > 0, "TODO");
647 if (ec) ec->clear();
648 buff[ret] = 0;
649 return {buff};
650}
651
652
653bool __remove(const path& p, std::error_code *ec) {
654 if (ec) ec->clear();
655 if (::remove(p.c_str()) == -1) {
656 set_or_throw(ec, "remove", p);
657 return false;
658 }
659 return true;
660}
661
662namespace {
663
664std::uintmax_t remove_all_impl(path const & p, std::error_code& ec)
665{
666 const auto npos = static_cast<std::uintmax_t>(-1);
667 const file_status st = __symlink_status(p, &ec);
668 if (ec) return npos;
669 std::uintmax_t count = 1;
670 if (is_directory(st)) {
671 for (directory_iterator it(p, ec); !ec && it != directory_iterator();
672 it.increment(ec)) {
673 auto other_count = remove_all_impl(it->path(), ec);
674 if (ec) return npos;
675 count += other_count;
676 }
677 if (ec) return npos;
678 }
679 if (!__remove(p, &ec)) return npos;
680 return count;
681}
682
683} // end namespace
684
685std::uintmax_t __remove_all(const path& p, std::error_code *ec) {
686 std::error_code mec;
687 auto count = remove_all_impl(p, mec);
688 if (mec) {
689 set_or_throw(mec, ec, "remove_all", p);
690 return static_cast<std::uintmax_t>(-1);
691 }
692 if (ec) ec->clear();
693 return count;
694}
695
696void __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);
699 else if (ec)
700 ec->clear();
701}
702
703void __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);
706 else if (ec)
707 ec->clear();
708}
709
710space_info __space(const path& p, std::error_code *ec) {
711 space_info si;
Eric Fiselier591e18f2016-06-18 02:11:48 +0000712 struct statvfs m_svfs = {};
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000713 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);
717 return si;
718 }
719 if (ec) ec->clear();
720 // Multiply with overflow checking.
Eric Fiselier591e18f2016-06-18 02:11:48 +0000721 auto do_mult = [&](std::uintmax_t& out, std::uintmax_t other) {
Eric Fiselier6e9a6942016-06-17 19:46:40 +0000722 out = other * m_svfs.f_frsize;
723 if (out / other != m_svfs.f_frsize || other == 0)
724 out = static_cast<std::uintmax_t>(-1);
725 };
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);
729 return si;
730}
731
732file_status __status(const path& p, std::error_code *ec) {
733 return detail::posix_stat(p, ec);
734}
735
736file_status __symlink_status(const path& p, std::error_code *ec) {
737 return detail::posix_lstat(p, ec);
738}
739
740path __system_complete(const path& p, std::error_code *ec) {
741 if (ec) ec->clear();
742 return absolute(p, current_path());
743}
744
745path __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)))
750 break;
751 }
752 path p(ret ? ret : "/tmp");
753 std::error_code m_ec;
754 if (is_directory(p, m_ec)) {
755 if (ec) ec->clear();
756 return p;
757 }
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");
761 return {};
762}
763
764// An absolute path is composed according to the table in [fs.op.absolute].
765path absolute(const path& p, const path& base) {
766 auto root_name = p.root_name();
767 auto root_dir = p.root_directory();
768
769 if (!root_name.empty() && !root_dir.empty())
770 return p;
771
772 auto abs_base = base.is_absolute() ? base : absolute(base);
773
774 /* !has_root_name && !has_root_dir */
775 if (root_name.empty() && root_dir.empty())
776 {
777 return abs_base / p;
778 }
779 else if (!root_name.empty()) /* has_root_name && !has_root_dir */
780 {
781 return root_name / abs_base.root_directory()
782 /
783 abs_base.relative_path() / p.relative_path();
784 }
785 else /* !has_root_name && has_root_dir */
786 {
787 if (abs_base.has_root_name())
788 return abs_base.root_name() / p;
789 // else p is absolute, return outside of block
790 }
791 return p;
792}
793
794_LIBCPP_END_NAMESPACE_EXPERIMENTAL_FILESYSTEM