operations.cpp 24 KB

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