1 //===--------------------- 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 "filesystem_time_helper.h"
19
20 #include <unistd.h>
21 #include <sys/stat.h>
22 #include <sys/statvfs.h>
23 #include <fcntl.h> /* values for fchmodat */
24
25 #if (__APPLE__)
26 #if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
27 #if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101300
28 #define _LIBCXX_USE_UTIMENSAT
29 #endif
30 #elif defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__)
31 #if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 110000
32 #define _LIBCXX_USE_UTIMENSAT
33 #endif
34 #elif defined(__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__)
35 #if __ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__ >= 110000
36 #define _LIBCXX_USE_UTIMENSAT
37 #endif
38 #elif defined(__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__)
39 #if __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__ >= 40000
40 #define _LIBCXX_USE_UTIMENSAT
41 #endif
42 #endif // __ENVIRONMENT_.*_VERSION_MIN_REQUIRED__
43 #else
44 // We can use the presence of UTIME_OMIT to detect platforms that provide
45 // utimensat.
46 #if defined(UTIME_OMIT)
47 #define _LIBCXX_USE_UTIMENSAT
48 #endif
49 #endif // __APPLE__
50
51 #if !defined(_LIBCXX_USE_UTIMENSAT)
52 #include <sys/time.h> // for ::utimes as used in __last_write_time
53 #endif
54
55 _LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_FILESYSTEM
56
~filesystem_error()57 filesystem_error::~filesystem_error() {}
58
59
60 // POSIX HELPERS
61
62 namespace detail { namespace {
63
64 using value_type = path::value_type;
65 using string_type = path::string_type;
66
capture_errno()67 inline std::error_code capture_errno() {
68 _LIBCPP_ASSERT(errno, "Expected errno to be non-zero");
69 return std::error_code(errno, std::generic_category());
70 }
71
set_or_throw(std::error_code const & m_ec,std::error_code * ec,const char * msg,path const & p={},path const & p2={})72 void set_or_throw(std::error_code const& m_ec, std::error_code* ec,
73 const char* msg, path const& p = {}, path const& p2 = {})
74 {
75 if (ec) {
76 *ec = m_ec;
77 } else {
78 string msg_s("std::experimental::filesystem::");
79 msg_s += msg;
80 __throw_filesystem_error(msg_s, p, p2, m_ec);
81 }
82 }
83
set_or_throw(std::error_code * ec,const char * msg,path const & p={},path const & p2={})84 void set_or_throw(std::error_code* ec, const char* msg,
85 path const& p = {}, path const& p2 = {})
86 {
87 return set_or_throw(capture_errno(), ec, msg, p, p2);
88 }
89
posix_get_perms(const struct::stat & st)90 perms posix_get_perms(const struct ::stat & st) noexcept {
91 return static_cast<perms>(st.st_mode) & perms::mask;
92 }
93
posix_convert_perms(perms prms)94 ::mode_t posix_convert_perms(perms prms) {
95 return static_cast< ::mode_t>(prms & perms::mask);
96 }
97
create_file_status(std::error_code & m_ec,path const & p,struct::stat & path_stat,std::error_code * ec)98 file_status create_file_status(std::error_code& m_ec, path const& p,
99 struct ::stat& path_stat,
100 std::error_code* ec)
101 {
102 if (ec) *ec = m_ec;
103 if (m_ec && (m_ec.value() == ENOENT || m_ec.value() == ENOTDIR)) {
104 return file_status(file_type::not_found);
105 }
106 else if (m_ec) {
107 set_or_throw(m_ec, ec, "posix_stat", p);
108 return file_status(file_type::none);
109 }
110 // else
111
112 file_status fs_tmp;
113 auto const mode = path_stat.st_mode;
114 if (S_ISLNK(mode)) fs_tmp.type(file_type::symlink);
115 else if (S_ISREG(mode)) fs_tmp.type(file_type::regular);
116 else if (S_ISDIR(mode)) fs_tmp.type(file_type::directory);
117 else if (S_ISBLK(mode)) fs_tmp.type(file_type::block);
118 else if (S_ISCHR(mode)) fs_tmp.type(file_type::character);
119 else if (S_ISFIFO(mode)) fs_tmp.type(file_type::fifo);
120 else if (S_ISSOCK(mode)) fs_tmp.type(file_type::socket);
121 else fs_tmp.type(file_type::unknown);
122
123 fs_tmp.permissions(detail::posix_get_perms(path_stat));
124 return fs_tmp;
125 }
126
posix_stat(path const & p,struct::stat & path_stat,std::error_code * ec)127 file_status posix_stat(path const & p, struct ::stat& path_stat,
128 std::error_code* ec)
129 {
130 std::error_code m_ec;
131 if (::stat(p.c_str(), &path_stat) == -1)
132 m_ec = detail::capture_errno();
133 return create_file_status(m_ec, p, path_stat, ec);
134 }
135
posix_stat(path const & p,std::error_code * ec)136 file_status posix_stat(path const & p, std::error_code* ec) {
137 struct ::stat path_stat;
138 return posix_stat(p, path_stat, ec);
139 }
140
posix_lstat(path const & p,struct::stat & path_stat,std::error_code * ec)141 file_status posix_lstat(path const & p, struct ::stat & path_stat,
142 std::error_code* ec)
143 {
144 std::error_code m_ec;
145 if (::lstat(p.c_str(), &path_stat) == -1)
146 m_ec = detail::capture_errno();
147 return create_file_status(m_ec, p, path_stat, ec);
148 }
149
posix_lstat(path const & p,std::error_code * ec)150 file_status posix_lstat(path const & p, std::error_code* ec) {
151 struct ::stat path_stat;
152 return posix_lstat(p, path_stat, ec);
153 }
154
stat_equivalent(struct::stat & st1,struct::stat & st2)155 bool stat_equivalent(struct ::stat& st1, struct ::stat& st2) {
156 return (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
157 }
158
159 // DETAIL::MISC
160
161
copy_file_impl(const path & from,const path & to,perms from_perms,std::error_code * ec)162 bool copy_file_impl(const path& from, const path& to, perms from_perms,
163 std::error_code *ec)
164 {
165 std::ifstream in(from.c_str(), std::ios::binary);
166 std::ofstream out(to.c_str(), std::ios::binary);
167
168 if (in.good() && out.good()) {
169 using InIt = std::istreambuf_iterator<char>;
170 using OutIt = std::ostreambuf_iterator<char>;
171 InIt bin(in);
172 InIt ein;
173 OutIt bout(out);
174 std::copy(bin, ein, bout);
175 }
176 if (out.fail() || in.fail()) {
177 set_or_throw(make_error_code(errc::operation_not_permitted),
178 ec, "copy_file", from, to);
179 return false;
180 }
181 __permissions(to, from_perms, ec);
182 // TODO what if permissions fails?
183 return true;
184 }
185
186 }} // end namespace detail
187
188 using detail::set_or_throw;
189
__canonical(path const & orig_p,const path & base,std::error_code * ec)190 path __canonical(path const & orig_p, const path& base, std::error_code *ec)
191 {
192 path p = absolute(orig_p, base);
193 char buff[PATH_MAX + 1];
194 char *ret;
195 if ((ret = ::realpath(p.c_str(), buff)) == nullptr) {
196 set_or_throw(ec, "canonical", orig_p, base);
197 return {};
198 }
199 if (ec) ec->clear();
200 return {ret};
201 }
202
__copy(const path & from,const path & to,copy_options options,std::error_code * ec)203 void __copy(const path& from, const path& to, copy_options options,
204 std::error_code *ec)
205 {
206 const bool sym_status = bool(options &
207 (copy_options::create_symlinks | copy_options::skip_symlinks));
208
209 const bool sym_status2 = bool(options &
210 copy_options::copy_symlinks);
211
212 std::error_code m_ec1;
213 struct ::stat f_st = {};
214 const file_status f = sym_status || sym_status2
215 ? detail::posix_lstat(from, f_st, &m_ec1)
216 : detail::posix_stat(from, f_st, &m_ec1);
217 if (m_ec1)
218 return set_or_throw(m_ec1, ec, "copy", from, to);
219
220 struct ::stat t_st = {};
221 const file_status t = sym_status ? detail::posix_lstat(to, t_st, &m_ec1)
222 : detail::posix_stat(to, t_st, &m_ec1);
223
224 if (not status_known(t))
225 return set_or_throw(m_ec1, ec, "copy", from, to);
226
227 if (!exists(f) || is_other(f) || is_other(t)
228 || (is_directory(f) && is_regular_file(t))
229 || detail::stat_equivalent(f_st, t_st))
230 {
231 return set_or_throw(make_error_code(errc::function_not_supported),
232 ec, "copy", from, to);
233 }
234
235 if (ec) ec->clear();
236
237 if (is_symlink(f)) {
238 if (bool(copy_options::skip_symlinks & options)) {
239 // do nothing
240 } else if (not exists(t)) {
241 __copy_symlink(from, to, ec);
242 } else {
243 set_or_throw(make_error_code(errc::file_exists),
244 ec, "copy", from, to);
245 }
246 return;
247 }
248 else if (is_regular_file(f)) {
249 if (bool(copy_options::directories_only & options)) {
250 // do nothing
251 }
252 else if (bool(copy_options::create_symlinks & options)) {
253 __create_symlink(from, to, ec);
254 }
255 else if (bool(copy_options::create_hard_links & options)) {
256 __create_hard_link(from, to, ec);
257 }
258 else if (is_directory(t)) {
259 __copy_file(from, to / from.filename(), options, ec);
260 } else {
261 __copy_file(from, to, options, ec);
262 }
263 return;
264 }
265 else if (is_directory(f) && bool(copy_options::create_symlinks & options)) {
266 return set_or_throw(make_error_code(errc::is_a_directory), ec, "copy");
267 }
268 else if (is_directory(f) && (bool(copy_options::recursive & options) ||
269 copy_options::none == options)) {
270
271 if (!exists(t)) {
272 // create directory to with attributes from 'from'.
273 __create_directory(to, from, ec);
274 if (ec && *ec) { return; }
275 }
276 directory_iterator it = ec ? directory_iterator(from, *ec)
277 : directory_iterator(from);
278 if (ec && *ec) { return; }
279 std::error_code m_ec2;
280 for (; it != directory_iterator(); it.increment(m_ec2)) {
281 if (m_ec2) return set_or_throw(m_ec2, ec, "copy", from, to);
282 __copy(it->path(), to / it->path().filename(),
283 options | copy_options::__in_recursive_copy, ec);
284 if (ec && *ec) { return; }
285 }
286 }
287 }
288
289
__copy_file(const path & from,const path & to,copy_options options,std::error_code * ec)290 bool __copy_file(const path& from, const path& to, copy_options options,
291 std::error_code *ec)
292 {
293 using StatT = struct ::stat;
294 if (ec)
295 ec->clear();
296
297 std::error_code m_ec;
298 StatT from_stat;
299 auto from_st = detail::posix_stat(from, from_stat, &m_ec);
300 if (not is_regular_file(from_st)) {
301 if (not m_ec)
302 m_ec = make_error_code(errc::not_supported);
303 set_or_throw(m_ec, ec, "copy_file", from, to);
304 return false;
305 }
306
307 StatT to_stat;
308 auto to_st = detail::posix_stat(to, to_stat, &m_ec);
309 if (!status_known(to_st)) {
310 set_or_throw(m_ec, ec, "copy_file", from, to);
311 return false;
312 }
313
314 const bool to_exists = exists(to_st);
315 if (to_exists && !is_regular_file(to_st)) {
316 set_or_throw(make_error_code(errc::not_supported), ec, "copy_file", from, to);
317 return false;
318 }
319 if (to_exists && detail::stat_equivalent(from_stat, to_stat)) {
320 set_or_throw(make_error_code(errc::file_exists), ec, "copy_file", from,
321 to);
322 return false;
323 }
324 if (to_exists && bool(copy_options::skip_existing & options)) {
325 return false;
326 }
327 else if (to_exists && bool(copy_options::update_existing & options)) {
328 auto from_time = __last_write_time(from, ec);
329 if (ec && *ec) { return false; }
330 auto to_time = __last_write_time(to, ec);
331 if (ec && *ec) { return false; }
332 if (from_time <= to_time) {
333 return false;
334 }
335 return detail::copy_file_impl(from, to, from_st.permissions(), ec);
336 }
337 else if (!to_exists || bool(copy_options::overwrite_existing & options)) {
338 return detail::copy_file_impl(from, to, from_st.permissions(), ec);
339 }
340 else {
341 set_or_throw(make_error_code(errc::file_exists), ec, "copy_file", from,
342 to);
343 return false;
344 }
345
346 _LIBCPP_UNREACHABLE();
347 }
348
__copy_symlink(const path & existing_symlink,const path & new_symlink,std::error_code * ec)349 void __copy_symlink(const path& existing_symlink, const path& new_symlink,
350 std::error_code *ec)
351 {
352 const path real_path(__read_symlink(existing_symlink, ec));
353 if (ec && *ec) { return; }
354 // NOTE: proposal says you should detect if you should call
355 // create_symlink or create_directory_symlink. I don't think this
356 // is needed with POSIX
357 __create_symlink(real_path, new_symlink, ec);
358 }
359
360
__create_directories(const path & p,std::error_code * ec)361 bool __create_directories(const path& p, std::error_code *ec)
362 {
363 std::error_code m_ec;
364 auto const st = detail::posix_stat(p, &m_ec);
365 if (!status_known(st)) {
366 set_or_throw(m_ec, ec, "create_directories", p);
367 return false;
368 }
369 else if (is_directory(st)) {
370 if (ec) ec->clear();
371 return false;
372 }
373 else if (exists(st)) {
374 set_or_throw(make_error_code(errc::file_exists),
375 ec, "create_directories", p);
376 return false;
377 }
378
379 const path parent = p.parent_path();
380 if (!parent.empty()) {
381 const file_status parent_st = status(parent, m_ec);
382 if (not status_known(parent_st)) {
383 set_or_throw(m_ec, ec, "create_directories", p);
384 return false;
385 }
386 if (not exists(parent_st)) {
387 __create_directories(parent, ec);
388 if (ec && *ec) { return false; }
389 }
390 }
391 return __create_directory(p, ec);
392 }
393
__create_directory(const path & p,std::error_code * ec)394 bool __create_directory(const path& p, std::error_code *ec)
395 {
396 if (ec) ec->clear();
397 if (::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0)
398 return true;
399 if (errno != EEXIST || !is_directory(p))
400 set_or_throw(ec, "create_directory", p);
401 return false;
402 }
403
__create_directory(path const & p,path const & attributes,std::error_code * ec)404 bool __create_directory(path const & p, path const & attributes,
405 std::error_code *ec)
406 {
407 struct ::stat attr_stat;
408 std::error_code mec;
409 auto st = detail::posix_stat(attributes, attr_stat, &mec);
410 if (!status_known(st)) {
411 set_or_throw(mec, ec, "create_directory", p, attributes);
412 return false;
413 }
414 if (ec) ec->clear();
415 if (::mkdir(p.c_str(), attr_stat.st_mode) == 0)
416 return true;
417 if (errno != EEXIST || !is_directory(p))
418 set_or_throw(ec, "create_directory", p, attributes);
419 return false;
420 }
421
__create_directory_symlink(path const & from,path const & to,std::error_code * ec)422 void __create_directory_symlink(path const & from, path const & to,
423 std::error_code *ec){
424 if (::symlink(from.c_str(), to.c_str()) != 0)
425 set_or_throw(ec, "create_directory_symlink", from, to);
426 else if (ec)
427 ec->clear();
428 }
429
__create_hard_link(const path & from,const path & to,std::error_code * ec)430 void __create_hard_link(const path& from, const path& to, std::error_code *ec){
431 if (::link(from.c_str(), to.c_str()) == -1)
432 set_or_throw(ec, "create_hard_link", from, to);
433 else if (ec)
434 ec->clear();
435 }
436
__create_symlink(path const & from,path const & to,std::error_code * ec)437 void __create_symlink(path const & from, path const & to, std::error_code *ec) {
438
439 if (::symlink(from.c_str(), to.c_str()) == -1)
440 set_or_throw(ec, "create_symlink", from, to);
441 else if (ec)
442 ec->clear();
443 }
444
__current_path(std::error_code * ec)445 path __current_path(std::error_code *ec) {
446 auto size = ::pathconf(".", _PC_PATH_MAX);
447 _LIBCPP_ASSERT(size >= 0, "pathconf returned a 0 as max size");
448
449 auto buff = std::unique_ptr<char[]>(new char[size + 1]);
450 char* ret;
451 if ((ret = ::getcwd(buff.get(), static_cast<size_t>(size))) == nullptr) {
452 set_or_throw(ec, "current_path");
453 return {};
454 }
455 if (ec) ec->clear();
456 return {buff.get()};
457 }
458
__current_path(const path & p,std::error_code * ec)459 void __current_path(const path& p, std::error_code *ec) {
460 if (::chdir(p.c_str()) == -1)
461 set_or_throw(ec, "current_path", p);
462 else if (ec)
463 ec->clear();
464 }
465
__equivalent(const path & p1,const path & p2,std::error_code * ec)466 bool __equivalent(const path& p1, const path& p2, std::error_code *ec)
467 {
468 auto make_unsupported_error = [&]() {
469 set_or_throw(make_error_code(errc::not_supported), ec,
470 "equivalent", p1, p2);
471 return false;
472 };
473 std::error_code ec1, ec2;
474 struct ::stat st1 = {};
475 struct ::stat st2 = {};
476 auto s1 = detail::posix_stat(p1.native(), st1, &ec1);
477 if (!exists(s1))
478 return make_unsupported_error();
479 auto s2 = detail::posix_stat(p2.native(), st2, &ec2);
480 if (!exists(s2))
481 return make_unsupported_error();
482 if (ec) ec->clear();
483 return detail::stat_equivalent(st1, st2);
484 }
485
486
__file_size(const path & p,std::error_code * ec)487 std::uintmax_t __file_size(const path& p, std::error_code *ec)
488 {
489 std::error_code m_ec;
490 struct ::stat st;
491 file_status fst = detail::posix_stat(p, st, &m_ec);
492 if (!exists(fst) || !is_regular_file(fst)) {
493 if (!m_ec)
494 m_ec = make_error_code(errc::not_supported);
495 set_or_throw(m_ec, ec, "file_size", p);
496 return static_cast<uintmax_t>(-1);
497 }
498 // is_regular_file(p) == true
499 if (ec) ec->clear();
500 return static_cast<std::uintmax_t>(st.st_size);
501 }
502
__hard_link_count(const path & p,std::error_code * ec)503 std::uintmax_t __hard_link_count(const path& p, std::error_code *ec)
504 {
505 std::error_code m_ec;
506 struct ::stat st;
507 detail::posix_stat(p, st, &m_ec);
508 if (m_ec) {
509 set_or_throw(m_ec, ec, "hard_link_count", p);
510 return static_cast<std::uintmax_t>(-1);
511 }
512 if (ec) ec->clear();
513 return static_cast<std::uintmax_t>(st.st_nlink);
514 }
515
516
__fs_is_empty(const path & p,std::error_code * ec)517 bool __fs_is_empty(const path& p, std::error_code *ec)
518 {
519 if (ec) ec->clear();
520 std::error_code m_ec;
521 struct ::stat pst;
522 auto st = detail::posix_stat(p, pst, &m_ec);
523 if (m_ec) {
524 set_or_throw(m_ec, ec, "is_empty", p);
525 return false;
526 }
527 else if (!is_directory(st) && !is_regular_file(st)) {
528 m_ec = make_error_code(errc::not_supported);
529 set_or_throw(m_ec, ec, "is_empty");
530 return false;
531 }
532 else if (is_directory(st)) {
533 auto it = ec ? directory_iterator(p, *ec) : directory_iterator(p);
534 if (ec && *ec)
535 return false;
536 return it == directory_iterator{};
537 }
538 else if (is_regular_file(st))
539 return static_cast<std::uintmax_t>(pst.st_size) == 0;
540
541 _LIBCPP_UNREACHABLE();
542 }
543
544
545 namespace detail { namespace {
546
547 using TimeSpec = struct timespec;
548 using StatT = struct stat;
549
550 #if defined(__APPLE__)
extract_mtime(StatT const & st)551 TimeSpec extract_mtime(StatT const& st) { return st.st_mtimespec; }
552 __attribute__((unused)) // Suppress warning
extract_atime(StatT const & st)553 TimeSpec extract_atime(StatT const& st) { return st.st_atimespec; }
554 #else
extract_mtime(StatT const & st)555 TimeSpec extract_mtime(StatT const& st) { return st.st_mtim; }
556 __attribute__((unused)) // Suppress warning
extract_atime(StatT const & st)557 TimeSpec extract_atime(StatT const& st) { return st.st_atim; }
558 #endif
559
560 }} // end namespace detail
561
562 using FSTime = fs_time_util<file_time_type, time_t, struct timespec>;
563
__last_write_time(const path & p,std::error_code * ec)564 file_time_type __last_write_time(const path& p, std::error_code *ec)
565 {
566 using namespace ::std::chrono;
567 std::error_code m_ec;
568 struct ::stat st;
569 detail::posix_stat(p, st, &m_ec);
570 if (m_ec) {
571 set_or_throw(m_ec, ec, "last_write_time", p);
572 return file_time_type::min();
573 }
574 if (ec) ec->clear();
575 auto ts = detail::extract_mtime(st);
576 if (!FSTime::is_representable(ts)) {
577 set_or_throw(error_code(EOVERFLOW, generic_category()), ec,
578 "last_write_time", p);
579 return file_time_type::min();
580 }
581 return FSTime::convert_timespec(ts);
582 }
583
__last_write_time(const path & p,file_time_type new_time,std::error_code * ec)584 void __last_write_time(const path& p, file_time_type new_time,
585 std::error_code *ec)
586 {
587 using namespace std::chrono;
588 std::error_code m_ec;
589
590 #if !defined(_LIBCXX_USE_UTIMENSAT)
591 // This implementation has a race condition between determining the
592 // last access time and attempting to set it to the same value using
593 // ::utimes
594 struct ::stat st;
595 file_status fst = detail::posix_stat(p, st, &m_ec);
596 if (m_ec && !status_known(fst)) {
597 set_or_throw(m_ec, ec, "last_write_time", p);
598 return;
599 }
600 auto atime = detail::extract_atime(st);
601 struct ::timeval tbuf[2];
602 tbuf[0].tv_sec = atime.tv_sec;
603 tbuf[0].tv_usec = duration_cast<microseconds>(nanoseconds(atime.tv_nsec)).count();
604 const bool overflowed = !FSTime::set_times_checked<microseconds>(
605 &tbuf[1].tv_sec, &tbuf[1].tv_usec, new_time);
606
607 if (overflowed) {
608 set_or_throw(make_error_code(errc::invalid_argument), ec,
609 "last_write_time", p);
610 return;
611 }
612 if (::utimes(p.c_str(), tbuf) == -1) {
613 m_ec = detail::capture_errno();
614 }
615 #else
616 struct ::timespec tbuf[2];
617 tbuf[0].tv_sec = 0;
618 tbuf[0].tv_nsec = UTIME_OMIT;
619
620 const bool overflowed = !FSTime::set_times_checked<nanoseconds>(
621 &tbuf[1].tv_sec, &tbuf[1].tv_nsec, new_time);
622 if (overflowed) {
623 set_or_throw(make_error_code(errc::invalid_argument),
624 ec, "last_write_time", p);
625 return;
626 }
627 if (::utimensat(AT_FDCWD, p.c_str(), tbuf, 0) == -1) {
628 m_ec = detail::capture_errno();
629 }
630 #endif
631 if (m_ec)
632 set_or_throw(m_ec, ec, "last_write_time", p);
633 else if (ec)
634 ec->clear();
635 }
636
637
__permissions(const path & p,perms prms,std::error_code * ec)638 void __permissions(const path& p, perms prms, std::error_code *ec)
639 {
640
641 const bool resolve_symlinks = !bool(perms::symlink_nofollow & prms);
642 const bool add_perms = bool(perms::add_perms & prms);
643 const bool remove_perms = bool(perms::remove_perms & prms);
644 _LIBCPP_ASSERT(!(add_perms && remove_perms),
645 "Both add_perms and remove_perms are set");
646
647 bool set_sym_perms = false;
648 prms &= perms::mask;
649 if (!resolve_symlinks || (add_perms || remove_perms)) {
650 std::error_code m_ec;
651 file_status st = resolve_symlinks ? detail::posix_stat(p, &m_ec)
652 : detail::posix_lstat(p, &m_ec);
653 set_sym_perms = is_symlink(st);
654 if (m_ec) return set_or_throw(m_ec, ec, "permissions", p);
655 _LIBCPP_ASSERT(st.permissions() != perms::unknown,
656 "Permissions unexpectedly unknown");
657 if (add_perms)
658 prms |= st.permissions();
659 else if (remove_perms)
660 prms = st.permissions() & ~prms;
661 }
662 const auto real_perms = detail::posix_convert_perms(prms);
663
664 # if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD)
665 const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0;
666 if (::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) {
667 return set_or_throw(ec, "permissions", p);
668 }
669 # else
670 if (set_sym_perms)
671 return set_or_throw(make_error_code(errc::operation_not_supported),
672 ec, "permissions", p);
673 if (::chmod(p.c_str(), real_perms) == -1) {
674 return set_or_throw(ec, "permissions", p);
675 }
676 # endif
677 if (ec) ec->clear();
678 }
679
680
__read_symlink(const path & p,std::error_code * ec)681 path __read_symlink(const path& p, std::error_code *ec) {
682 char buff[PATH_MAX + 1];
683 std::error_code m_ec;
684 ::ssize_t ret;
685 if ((ret = ::readlink(p.c_str(), buff, PATH_MAX)) == -1) {
686 set_or_throw(ec, "read_symlink", p);
687 return {};
688 }
689 _LIBCPP_ASSERT(ret <= PATH_MAX, "TODO");
690 _LIBCPP_ASSERT(ret > 0, "TODO");
691 if (ec) ec->clear();
692 buff[ret] = 0;
693 return {buff};
694 }
695
696
__remove(const path & p,std::error_code * ec)697 bool __remove(const path& p, std::error_code *ec) {
698 if (ec) ec->clear();
699
700 if (::remove(p.c_str()) == -1) {
701 if (errno != ENOENT)
702 set_or_throw(ec, "remove", p);
703 return false;
704 }
705 return true;
706 }
707
708 namespace {
709
remove_all_impl(path const & p,std::error_code & ec)710 std::uintmax_t remove_all_impl(path const & p, std::error_code& ec)
711 {
712 const auto npos = static_cast<std::uintmax_t>(-1);
713 const file_status st = __symlink_status(p, &ec);
714 if (ec) return npos;
715 std::uintmax_t count = 1;
716 if (is_directory(st)) {
717 for (directory_iterator it(p, ec); !ec && it != directory_iterator();
718 it.increment(ec)) {
719 auto other_count = remove_all_impl(it->path(), ec);
720 if (ec) return npos;
721 count += other_count;
722 }
723 if (ec) return npos;
724 }
725 if (!__remove(p, &ec)) return npos;
726 return count;
727 }
728
729 } // end namespace
730
__remove_all(const path & p,std::error_code * ec)731 std::uintmax_t __remove_all(const path& p, std::error_code *ec) {
732 if (ec) ec->clear();
733
734 std::error_code mec;
735 auto count = remove_all_impl(p, mec);
736 if (mec) {
737 if (mec == errc::no_such_file_or_directory) {
738 return 0;
739 } else {
740 set_or_throw(mec, ec, "remove_all", p);
741 return static_cast<std::uintmax_t>(-1);
742 }
743 }
744 return count;
745 }
746
__rename(const path & from,const path & to,std::error_code * ec)747 void __rename(const path& from, const path& to, std::error_code *ec) {
748 if (::rename(from.c_str(), to.c_str()) == -1)
749 set_or_throw(ec, "rename", from, to);
750 else if (ec)
751 ec->clear();
752 }
753
__resize_file(const path & p,std::uintmax_t size,std::error_code * ec)754 void __resize_file(const path& p, std::uintmax_t size, std::error_code *ec) {
755 if (::truncate(p.c_str(), static_cast<::off_t>(size)) == -1)
756 set_or_throw(ec, "resize_file", p);
757 else if (ec)
758 ec->clear();
759 }
760
__space(const path & p,std::error_code * ec)761 space_info __space(const path& p, std::error_code *ec) {
762 space_info si;
763 struct statvfs m_svfs = {};
764 if (::statvfs(p.c_str(), &m_svfs) == -1) {
765 set_or_throw(ec, "space", p);
766 si.capacity = si.free = si.available =
767 static_cast<std::uintmax_t>(-1);
768 return si;
769 }
770 if (ec) ec->clear();
771 // Multiply with overflow checking.
772 auto do_mult = [&](std::uintmax_t& out, std::uintmax_t other) {
773 out = other * m_svfs.f_frsize;
774 if (other == 0 || out / other != m_svfs.f_frsize)
775 out = static_cast<std::uintmax_t>(-1);
776 };
777 do_mult(si.capacity, m_svfs.f_blocks);
778 do_mult(si.free, m_svfs.f_bfree);
779 do_mult(si.available, m_svfs.f_bavail);
780 return si;
781 }
782
__status(const path & p,std::error_code * ec)783 file_status __status(const path& p, std::error_code *ec) {
784 return detail::posix_stat(p, ec);
785 }
786
__symlink_status(const path & p,std::error_code * ec)787 file_status __symlink_status(const path& p, std::error_code *ec) {
788 return detail::posix_lstat(p, ec);
789 }
790
__system_complete(const path & p,std::error_code * ec)791 path __system_complete(const path& p, std::error_code *ec) {
792 if (ec) ec->clear();
793 return absolute(p, current_path());
794 }
795
__temp_directory_path(std::error_code * ec)796 path __temp_directory_path(std::error_code* ec) {
797 const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
798 const char* ret = nullptr;
799
800 for (auto& ep : env_paths)
801 if ((ret = std::getenv(ep)))
802 break;
803 if (ret == nullptr)
804 ret = "/tmp";
805
806 path p(ret);
807 std::error_code m_ec;
808 if (!exists(p, m_ec) || !is_directory(p, m_ec)) {
809 if (!m_ec || m_ec == make_error_code(errc::no_such_file_or_directory))
810 m_ec = make_error_code(errc::not_a_directory);
811 set_or_throw(m_ec, ec, "temp_directory_path");
812 return {};
813 }
814
815 if (ec)
816 ec->clear();
817 return p;
818 }
819
820 // An absolute path is composed according to the table in [fs.op.absolute].
absolute(const path & p,const path & base)821 path absolute(const path& p, const path& base) {
822 auto root_name = p.root_name();
823 auto root_dir = p.root_directory();
824
825 if (!root_name.empty() && !root_dir.empty())
826 return p;
827
828 auto abs_base = base.is_absolute() ? base : absolute(base);
829
830 /* !has_root_name && !has_root_dir */
831 if (root_name.empty() && root_dir.empty())
832 {
833 return abs_base / p;
834 }
835 else if (!root_name.empty()) /* has_root_name && !has_root_dir */
836 {
837 return root_name / abs_base.root_directory()
838 /
839 abs_base.relative_path() / p.relative_path();
840 }
841 else /* !has_root_name && has_root_dir */
842 {
843 if (abs_base.has_root_name())
844 return abs_base.root_name() / p;
845 // else p is absolute, return outside of block
846 }
847 return p;
848 }
849
850 _LIBCPP_END_NAMESPACE_EXPERIMENTAL_FILESYSTEM
851