filesystem 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644
  1. // -*- C++ -*-
  2. //===--------------------------- filesystem -------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_FILESYSTEM
  10. #define _LIBCPP_FILESYSTEM
  11. /*
  12. filesystem synopsis
  13. namespace std { namespace filesystem {
  14. class path;
  15. void swap(path& lhs, path& rhs) noexcept;
  16. size_t hash_value(const path& p) noexcept;
  17. bool operator==(const path& lhs, const path& rhs) noexcept;
  18. bool operator!=(const path& lhs, const path& rhs) noexcept;
  19. bool operator< (const path& lhs, const path& rhs) noexcept;
  20. bool operator<=(const path& lhs, const path& rhs) noexcept;
  21. bool operator> (const path& lhs, const path& rhs) noexcept;
  22. bool operator>=(const path& lhs, const path& rhs) noexcept;
  23. path operator/ (const path& lhs, const path& rhs);
  24. // fs.path.io operators are friends of path.
  25. template <class charT, class traits>
  26. friend basic_ostream<charT, traits>&
  27. operator<<(basic_ostream<charT, traits>& os, const path& p);
  28. template <class charT, class traits>
  29. friend basic_istream<charT, traits>&
  30. operator>>(basic_istream<charT, traits>& is, path& p);
  31. template <class Source>
  32. path u8path(const Source& source);
  33. template <class InputIterator>
  34. path u8path(InputIterator first, InputIterator last);
  35. class filesystem_error;
  36. class directory_entry;
  37. class directory_iterator;
  38. // enable directory_iterator range-based for statements
  39. directory_iterator begin(directory_iterator iter) noexcept;
  40. directory_iterator end(const directory_iterator&) noexcept;
  41. class recursive_directory_iterator;
  42. // enable recursive_directory_iterator range-based for statements
  43. recursive_directory_iterator begin(recursive_directory_iterator iter) noexcept;
  44. recursive_directory_iterator end(const recursive_directory_iterator&) noexcept;
  45. class file_status;
  46. struct space_info
  47. {
  48. uintmax_t capacity;
  49. uintmax_t free;
  50. uintmax_t available;
  51. };
  52. enum class file_type;
  53. enum class perms;
  54. enum class perm_options;
  55. enum class copy_options;
  56. enum class directory_options;
  57. typedef chrono::time_point<trivial-clock> file_time_type;
  58. // operational functions
  59. path absolute(const path& p);
  60. path absolute(const path& p, error_code &ec);
  61. path canonical(const path& p);
  62. path canonical(const path& p, error_code& ec);
  63. void copy(const path& from, const path& to);
  64. void copy(const path& from, const path& to, error_code& ec);
  65. void copy(const path& from, const path& to, copy_options options);
  66. void copy(const path& from, const path& to, copy_options options,
  67. error_code& ec);
  68. bool copy_file(const path& from, const path& to);
  69. bool copy_file(const path& from, const path& to, error_code& ec);
  70. bool copy_file(const path& from, const path& to, copy_options option);
  71. bool copy_file(const path& from, const path& to, copy_options option,
  72. error_code& ec);
  73. void copy_symlink(const path& existing_symlink, const path& new_symlink);
  74. void copy_symlink(const path& existing_symlink, const path& new_symlink,
  75. error_code& ec) noexcept;
  76. bool create_directories(const path& p);
  77. bool create_directories(const path& p, error_code& ec);
  78. bool create_directory(const path& p);
  79. bool create_directory(const path& p, error_code& ec) noexcept;
  80. bool create_directory(const path& p, const path& attributes);
  81. bool create_directory(const path& p, const path& attributes,
  82. error_code& ec) noexcept;
  83. void create_directory_symlink(const path& to, const path& new_symlink);
  84. void create_directory_symlink(const path& to, const path& new_symlink,
  85. error_code& ec) noexcept;
  86. void create_hard_link(const path& to, const path& new_hard_link);
  87. void create_hard_link(const path& to, const path& new_hard_link,
  88. error_code& ec) noexcept;
  89. void create_symlink(const path& to, const path& new_symlink);
  90. void create_symlink(const path& to, const path& new_symlink,
  91. error_code& ec) noexcept;
  92. path current_path();
  93. path current_path(error_code& ec);
  94. void current_path(const path& p);
  95. void current_path(const path& p, error_code& ec) noexcept;
  96. bool exists(file_status s) noexcept;
  97. bool exists(const path& p);
  98. bool exists(const path& p, error_code& ec) noexcept;
  99. bool equivalent(const path& p1, const path& p2);
  100. bool equivalent(const path& p1, const path& p2, error_code& ec) noexcept;
  101. uintmax_t file_size(const path& p);
  102. uintmax_t file_size(const path& p, error_code& ec) noexcept;
  103. uintmax_t hard_link_count(const path& p);
  104. uintmax_t hard_link_count(const path& p, error_code& ec) noexcept;
  105. bool is_block_file(file_status s) noexcept;
  106. bool is_block_file(const path& p);
  107. bool is_block_file(const path& p, error_code& ec) noexcept;
  108. bool is_character_file(file_status s) noexcept;
  109. bool is_character_file(const path& p);
  110. bool is_character_file(const path& p, error_code& ec) noexcept;
  111. bool is_directory(file_status s) noexcept;
  112. bool is_directory(const path& p);
  113. bool is_directory(const path& p, error_code& ec) noexcept;
  114. bool is_empty(const path& p);
  115. bool is_empty(const path& p, error_code& ec) noexcept;
  116. bool is_fifo(file_status s) noexcept;
  117. bool is_fifo(const path& p);
  118. bool is_fifo(const path& p, error_code& ec) noexcept;
  119. bool is_other(file_status s) noexcept;
  120. bool is_other(const path& p);
  121. bool is_other(const path& p, error_code& ec) noexcept;
  122. bool is_regular_file(file_status s) noexcept;
  123. bool is_regular_file(const path& p);
  124. bool is_regular_file(const path& p, error_code& ec) noexcept;
  125. bool is_socket(file_status s) noexcept;
  126. bool is_socket(const path& p);
  127. bool is_socket(const path& p, error_code& ec) noexcept;
  128. bool is_symlink(file_status s) noexcept;
  129. bool is_symlink(const path& p);
  130. bool is_symlink(const path& p, error_code& ec) noexcept;
  131. file_time_type last_write_time(const path& p);
  132. file_time_type last_write_time(const path& p, error_code& ec) noexcept;
  133. void last_write_time(const path& p, file_time_type new_time);
  134. void last_write_time(const path& p, file_time_type new_time,
  135. error_code& ec) noexcept;
  136. void permissions(const path& p, perms prms,
  137. perm_options opts=perm_options::replace);
  138. void permissions(const path& p, perms prms, error_code& ec) noexcept;
  139. void permissions(const path& p, perms prms, perm_options opts,
  140. error_code& ec);
  141. path proximate(const path& p, error_code& ec);
  142. path proximate(const path& p, const path& base = current_path());
  143. path proximate(const path& p, const path& base, error_code &ec);
  144. path read_symlink(const path& p);
  145. path read_symlink(const path& p, error_code& ec);
  146. path relative(const path& p, error_code& ec);
  147. path relative(const path& p, const path& base=current_path());
  148. path relative(const path& p, const path& base, error_code& ec);
  149. bool remove(const path& p);
  150. bool remove(const path& p, error_code& ec) noexcept;
  151. uintmax_t remove_all(const path& p);
  152. uintmax_t remove_all(const path& p, error_code& ec);
  153. void rename(const path& from, const path& to);
  154. void rename(const path& from, const path& to, error_code& ec) noexcept;
  155. void resize_file(const path& p, uintmax_t size);
  156. void resize_file(const path& p, uintmax_t size, error_code& ec) noexcept;
  157. space_info space(const path& p);
  158. space_info space(const path& p, error_code& ec) noexcept;
  159. file_status status(const path& p);
  160. file_status status(const path& p, error_code& ec) noexcept;
  161. bool status_known(file_status s) noexcept;
  162. file_status symlink_status(const path& p);
  163. file_status symlink_status(const path& p, error_code& ec) noexcept;
  164. path temp_directory_path();
  165. path temp_directory_path(error_code& ec);
  166. path weakly_canonical(path const& p);
  167. path weakly_canonical(path const& p, error_code& ec);
  168. } } // namespaces std::filesystem
  169. */
  170. #include <__config>
  171. #include <cstddef>
  172. #include <cstdlib>
  173. #include <chrono>
  174. #include <iterator>
  175. #include <iosfwd>
  176. #include <locale>
  177. #include <memory>
  178. #include <stack>
  179. #include <string>
  180. #include <system_error>
  181. #include <utility>
  182. #include <iomanip> // for quoted
  183. #include <string_view>
  184. #include <version>
  185. #include <__debug>
  186. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  187. #pragma GCC system_header
  188. #endif
  189. _LIBCPP_PUSH_MACROS
  190. #include <__undef_macros>
  191. #ifndef _LIBCPP_CXX03_LANG
  192. _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
  193. _LIBCPP_AVAILABILITY_FILESYSTEM_PUSH
  194. typedef chrono::time_point<_FilesystemClock> file_time_type;
  195. struct _LIBCPP_TYPE_VIS space_info {
  196. uintmax_t capacity;
  197. uintmax_t free;
  198. uintmax_t available;
  199. };
  200. enum class _LIBCPP_ENUM_VIS file_type : signed char {
  201. none = 0,
  202. not_found = -1,
  203. regular = 1,
  204. directory = 2,
  205. symlink = 3,
  206. block = 4,
  207. character = 5,
  208. fifo = 6,
  209. socket = 7,
  210. unknown = 8
  211. };
  212. enum class _LIBCPP_ENUM_VIS perms : unsigned {
  213. none = 0,
  214. owner_read = 0400,
  215. owner_write = 0200,
  216. owner_exec = 0100,
  217. owner_all = 0700,
  218. group_read = 040,
  219. group_write = 020,
  220. group_exec = 010,
  221. group_all = 070,
  222. others_read = 04,
  223. others_write = 02,
  224. others_exec = 01,
  225. others_all = 07,
  226. all = 0777,
  227. set_uid = 04000,
  228. set_gid = 02000,
  229. sticky_bit = 01000,
  230. mask = 07777,
  231. unknown = 0xFFFF,
  232. };
  233. _LIBCPP_INLINE_VISIBILITY
  234. inline constexpr perms operator&(perms _LHS, perms _RHS) {
  235. return static_cast<perms>(static_cast<unsigned>(_LHS) &
  236. static_cast<unsigned>(_RHS));
  237. }
  238. _LIBCPP_INLINE_VISIBILITY
  239. inline constexpr perms operator|(perms _LHS, perms _RHS) {
  240. return static_cast<perms>(static_cast<unsigned>(_LHS) |
  241. static_cast<unsigned>(_RHS));
  242. }
  243. _LIBCPP_INLINE_VISIBILITY
  244. inline constexpr perms operator^(perms _LHS, perms _RHS) {
  245. return static_cast<perms>(static_cast<unsigned>(_LHS) ^
  246. static_cast<unsigned>(_RHS));
  247. }
  248. _LIBCPP_INLINE_VISIBILITY
  249. inline constexpr perms operator~(perms _LHS) {
  250. return static_cast<perms>(~static_cast<unsigned>(_LHS));
  251. }
  252. _LIBCPP_INLINE_VISIBILITY
  253. inline perms& operator&=(perms& _LHS, perms _RHS) { return _LHS = _LHS & _RHS; }
  254. _LIBCPP_INLINE_VISIBILITY
  255. inline perms& operator|=(perms& _LHS, perms _RHS) { return _LHS = _LHS | _RHS; }
  256. _LIBCPP_INLINE_VISIBILITY
  257. inline perms& operator^=(perms& _LHS, perms _RHS) { return _LHS = _LHS ^ _RHS; }
  258. enum class _LIBCPP_ENUM_VIS perm_options : unsigned char {
  259. replace = 1,
  260. add = 2,
  261. remove = 4,
  262. nofollow = 8
  263. };
  264. _LIBCPP_INLINE_VISIBILITY
  265. inline constexpr perm_options operator&(perm_options _LHS, perm_options _RHS) {
  266. return static_cast<perm_options>(static_cast<unsigned>(_LHS) &
  267. static_cast<unsigned>(_RHS));
  268. }
  269. _LIBCPP_INLINE_VISIBILITY
  270. inline constexpr perm_options operator|(perm_options _LHS, perm_options _RHS) {
  271. return static_cast<perm_options>(static_cast<unsigned>(_LHS) |
  272. static_cast<unsigned>(_RHS));
  273. }
  274. _LIBCPP_INLINE_VISIBILITY
  275. inline constexpr perm_options operator^(perm_options _LHS, perm_options _RHS) {
  276. return static_cast<perm_options>(static_cast<unsigned>(_LHS) ^
  277. static_cast<unsigned>(_RHS));
  278. }
  279. _LIBCPP_INLINE_VISIBILITY
  280. inline constexpr perm_options operator~(perm_options _LHS) {
  281. return static_cast<perm_options>(~static_cast<unsigned>(_LHS));
  282. }
  283. _LIBCPP_INLINE_VISIBILITY
  284. inline perm_options& operator&=(perm_options& _LHS, perm_options _RHS) {
  285. return _LHS = _LHS & _RHS;
  286. }
  287. _LIBCPP_INLINE_VISIBILITY
  288. inline perm_options& operator|=(perm_options& _LHS, perm_options _RHS) {
  289. return _LHS = _LHS | _RHS;
  290. }
  291. _LIBCPP_INLINE_VISIBILITY
  292. inline perm_options& operator^=(perm_options& _LHS, perm_options _RHS) {
  293. return _LHS = _LHS ^ _RHS;
  294. }
  295. enum class _LIBCPP_ENUM_VIS copy_options : unsigned short {
  296. none = 0,
  297. skip_existing = 1,
  298. overwrite_existing = 2,
  299. update_existing = 4,
  300. recursive = 8,
  301. copy_symlinks = 16,
  302. skip_symlinks = 32,
  303. directories_only = 64,
  304. create_symlinks = 128,
  305. create_hard_links = 256,
  306. __in_recursive_copy = 512,
  307. };
  308. _LIBCPP_INLINE_VISIBILITY
  309. inline constexpr copy_options operator&(copy_options _LHS, copy_options _RHS) {
  310. return static_cast<copy_options>(static_cast<unsigned short>(_LHS) &
  311. static_cast<unsigned short>(_RHS));
  312. }
  313. _LIBCPP_INLINE_VISIBILITY
  314. inline constexpr copy_options operator|(copy_options _LHS, copy_options _RHS) {
  315. return static_cast<copy_options>(static_cast<unsigned short>(_LHS) |
  316. static_cast<unsigned short>(_RHS));
  317. }
  318. _LIBCPP_INLINE_VISIBILITY
  319. inline constexpr copy_options operator^(copy_options _LHS, copy_options _RHS) {
  320. return static_cast<copy_options>(static_cast<unsigned short>(_LHS) ^
  321. static_cast<unsigned short>(_RHS));
  322. }
  323. _LIBCPP_INLINE_VISIBILITY
  324. inline constexpr copy_options operator~(copy_options _LHS) {
  325. return static_cast<copy_options>(~static_cast<unsigned short>(_LHS));
  326. }
  327. _LIBCPP_INLINE_VISIBILITY
  328. inline copy_options& operator&=(copy_options& _LHS, copy_options _RHS) {
  329. return _LHS = _LHS & _RHS;
  330. }
  331. _LIBCPP_INLINE_VISIBILITY
  332. inline copy_options& operator|=(copy_options& _LHS, copy_options _RHS) {
  333. return _LHS = _LHS | _RHS;
  334. }
  335. _LIBCPP_INLINE_VISIBILITY
  336. inline copy_options& operator^=(copy_options& _LHS, copy_options _RHS) {
  337. return _LHS = _LHS ^ _RHS;
  338. }
  339. enum class _LIBCPP_ENUM_VIS directory_options : unsigned char {
  340. none = 0,
  341. follow_directory_symlink = 1,
  342. skip_permission_denied = 2
  343. };
  344. _LIBCPP_INLINE_VISIBILITY
  345. inline constexpr directory_options operator&(directory_options _LHS,
  346. directory_options _RHS) {
  347. return static_cast<directory_options>(static_cast<unsigned char>(_LHS) &
  348. static_cast<unsigned char>(_RHS));
  349. }
  350. _LIBCPP_INLINE_VISIBILITY
  351. inline constexpr directory_options operator|(directory_options _LHS,
  352. directory_options _RHS) {
  353. return static_cast<directory_options>(static_cast<unsigned char>(_LHS) |
  354. static_cast<unsigned char>(_RHS));
  355. }
  356. _LIBCPP_INLINE_VISIBILITY
  357. inline constexpr directory_options operator^(directory_options _LHS,
  358. directory_options _RHS) {
  359. return static_cast<directory_options>(static_cast<unsigned char>(_LHS) ^
  360. static_cast<unsigned char>(_RHS));
  361. }
  362. _LIBCPP_INLINE_VISIBILITY
  363. inline constexpr directory_options operator~(directory_options _LHS) {
  364. return static_cast<directory_options>(~static_cast<unsigned char>(_LHS));
  365. }
  366. _LIBCPP_INLINE_VISIBILITY
  367. inline directory_options& operator&=(directory_options& _LHS,
  368. directory_options _RHS) {
  369. return _LHS = _LHS & _RHS;
  370. }
  371. _LIBCPP_INLINE_VISIBILITY
  372. inline directory_options& operator|=(directory_options& _LHS,
  373. directory_options _RHS) {
  374. return _LHS = _LHS | _RHS;
  375. }
  376. _LIBCPP_INLINE_VISIBILITY
  377. inline directory_options& operator^=(directory_options& _LHS,
  378. directory_options _RHS) {
  379. return _LHS = _LHS ^ _RHS;
  380. }
  381. class _LIBCPP_TYPE_VIS file_status {
  382. public:
  383. // constructors
  384. _LIBCPP_INLINE_VISIBILITY
  385. file_status() noexcept : file_status(file_type::none) {}
  386. _LIBCPP_INLINE_VISIBILITY
  387. explicit file_status(file_type __ft, perms __prms = perms::unknown) noexcept
  388. : __ft_(__ft),
  389. __prms_(__prms) {}
  390. file_status(const file_status&) noexcept = default;
  391. file_status(file_status&&) noexcept = default;
  392. _LIBCPP_INLINE_VISIBILITY
  393. ~file_status() {}
  394. file_status& operator=(const file_status&) noexcept = default;
  395. file_status& operator=(file_status&&) noexcept = default;
  396. // observers
  397. _LIBCPP_INLINE_VISIBILITY
  398. file_type type() const noexcept { return __ft_; }
  399. _LIBCPP_INLINE_VISIBILITY
  400. perms permissions() const noexcept { return __prms_; }
  401. // modifiers
  402. _LIBCPP_INLINE_VISIBILITY
  403. void type(file_type __ft) noexcept { __ft_ = __ft; }
  404. _LIBCPP_INLINE_VISIBILITY
  405. void permissions(perms __p) noexcept { __prms_ = __p; }
  406. private:
  407. file_type __ft_;
  408. perms __prms_;
  409. };
  410. class _LIBCPP_TYPE_VIS directory_entry;
  411. template <class _Tp>
  412. struct __can_convert_char {
  413. static const bool value = false;
  414. };
  415. template <class _Tp>
  416. struct __can_convert_char<const _Tp> : public __can_convert_char<_Tp> {};
  417. template <>
  418. struct __can_convert_char<char> {
  419. static const bool value = true;
  420. using __char_type = char;
  421. };
  422. template <>
  423. struct __can_convert_char<wchar_t> {
  424. static const bool value = true;
  425. using __char_type = wchar_t;
  426. };
  427. template <>
  428. struct __can_convert_char<char16_t> {
  429. static const bool value = true;
  430. using __char_type = char16_t;
  431. };
  432. template <>
  433. struct __can_convert_char<char32_t> {
  434. static const bool value = true;
  435. using __char_type = char32_t;
  436. };
  437. template <class _ECharT>
  438. typename enable_if<__can_convert_char<_ECharT>::value, bool>::type
  439. __is_separator(_ECharT __e) {
  440. return __e == _ECharT('/');
  441. }
  442. struct _NullSentinal {};
  443. template <class _Tp>
  444. using _Void = void;
  445. template <class _Tp, class = void>
  446. struct __is_pathable_string : public false_type {};
  447. template <class _ECharT, class _Traits, class _Alloc>
  448. struct __is_pathable_string<
  449. basic_string<_ECharT, _Traits, _Alloc>,
  450. _Void<typename __can_convert_char<_ECharT>::__char_type> >
  451. : public __can_convert_char<_ECharT> {
  452. using _Str = basic_string<_ECharT, _Traits, _Alloc>;
  453. using _Base = __can_convert_char<_ECharT>;
  454. static _ECharT const* __range_begin(_Str const& __s) { return __s.data(); }
  455. static _ECharT const* __range_end(_Str const& __s) {
  456. return __s.data() + __s.length();
  457. }
  458. static _ECharT __first_or_null(_Str const& __s) {
  459. return __s.empty() ? _ECharT{} : __s[0];
  460. }
  461. };
  462. template <class _ECharT, class _Traits>
  463. struct __is_pathable_string<
  464. basic_string_view<_ECharT, _Traits>,
  465. _Void<typename __can_convert_char<_ECharT>::__char_type> >
  466. : public __can_convert_char<_ECharT> {
  467. using _Str = basic_string_view<_ECharT, _Traits>;
  468. using _Base = __can_convert_char<_ECharT>;
  469. static _ECharT const* __range_begin(_Str const& __s) { return __s.data(); }
  470. static _ECharT const* __range_end(_Str const& __s) {
  471. return __s.data() + __s.length();
  472. }
  473. static _ECharT __first_or_null(_Str const& __s) {
  474. return __s.empty() ? _ECharT{} : __s[0];
  475. }
  476. };
  477. template <class _Source, class _DS = typename decay<_Source>::type,
  478. class _UnqualPtrType =
  479. typename remove_const<typename remove_pointer<_DS>::type>::type,
  480. bool _IsCharPtr = is_pointer<_DS>::value&&
  481. __can_convert_char<_UnqualPtrType>::value>
  482. struct __is_pathable_char_array : false_type {};
  483. template <class _Source, class _ECharT, class _UPtr>
  484. struct __is_pathable_char_array<_Source, _ECharT*, _UPtr, true>
  485. : __can_convert_char<typename remove_const<_ECharT>::type> {
  486. using _Base = __can_convert_char<typename remove_const<_ECharT>::type>;
  487. static _ECharT const* __range_begin(const _ECharT* __b) { return __b; }
  488. static _ECharT const* __range_end(const _ECharT* __b) {
  489. using _Iter = const _ECharT*;
  490. const _ECharT __sentinal = _ECharT{};
  491. _Iter __e = __b;
  492. for (; *__e != __sentinal; ++__e)
  493. ;
  494. return __e;
  495. }
  496. static _ECharT __first_or_null(const _ECharT* __b) { return *__b; }
  497. };
  498. template <class _Iter, bool _IsIt = __is_input_iterator<_Iter>::value,
  499. class = void>
  500. struct __is_pathable_iter : false_type {};
  501. template <class _Iter>
  502. struct __is_pathable_iter<
  503. _Iter, true,
  504. _Void<typename __can_convert_char<
  505. typename iterator_traits<_Iter>::value_type>::__char_type> >
  506. : __can_convert_char<typename iterator_traits<_Iter>::value_type> {
  507. using _ECharT = typename iterator_traits<_Iter>::value_type;
  508. using _Base = __can_convert_char<_ECharT>;
  509. static _Iter __range_begin(_Iter __b) { return __b; }
  510. static _NullSentinal __range_end(_Iter) { return _NullSentinal{}; }
  511. static _ECharT __first_or_null(_Iter __b) { return *__b; }
  512. };
  513. template <class _Tp, bool _IsStringT = __is_pathable_string<_Tp>::value,
  514. bool _IsCharIterT = __is_pathable_char_array<_Tp>::value,
  515. bool _IsIterT = !_IsCharIterT && __is_pathable_iter<_Tp>::value>
  516. struct __is_pathable : false_type {
  517. static_assert(!_IsStringT && !_IsCharIterT && !_IsIterT, "Must all be false");
  518. };
  519. template <class _Tp>
  520. struct __is_pathable<_Tp, true, false, false> : __is_pathable_string<_Tp> {};
  521. template <class _Tp>
  522. struct __is_pathable<_Tp, false, true, false> : __is_pathable_char_array<_Tp> {
  523. };
  524. template <class _Tp>
  525. struct __is_pathable<_Tp, false, false, true> : __is_pathable_iter<_Tp> {};
  526. template <class _ECharT>
  527. struct _PathCVT {
  528. static_assert(__can_convert_char<_ECharT>::value,
  529. "Char type not convertible");
  530. typedef __narrow_to_utf8<sizeof(_ECharT) * __CHAR_BIT__> _Narrower;
  531. static void __append_range(string& __dest, _ECharT const* __b,
  532. _ECharT const* __e) {
  533. _Narrower()(back_inserter(__dest), __b, __e);
  534. }
  535. template <class _Iter>
  536. static void __append_range(string& __dest, _Iter __b, _Iter __e) {
  537. static_assert(!is_same<_Iter, _ECharT*>::value, "Call const overload");
  538. if (__b == __e)
  539. return;
  540. basic_string<_ECharT> __tmp(__b, __e);
  541. _Narrower()(back_inserter(__dest), __tmp.data(),
  542. __tmp.data() + __tmp.length());
  543. }
  544. template <class _Iter>
  545. static void __append_range(string& __dest, _Iter __b, _NullSentinal) {
  546. static_assert(!is_same<_Iter, _ECharT*>::value, "Call const overload");
  547. const _ECharT __sentinal = _ECharT{};
  548. if (*__b == __sentinal)
  549. return;
  550. basic_string<_ECharT> __tmp;
  551. for (; *__b != __sentinal; ++__b)
  552. __tmp.push_back(*__b);
  553. _Narrower()(back_inserter(__dest), __tmp.data(),
  554. __tmp.data() + __tmp.length());
  555. }
  556. template <class _Source>
  557. static void __append_source(string& __dest, _Source const& __s) {
  558. using _Traits = __is_pathable<_Source>;
  559. __append_range(__dest, _Traits::__range_begin(__s),
  560. _Traits::__range_end(__s));
  561. }
  562. };
  563. template <>
  564. struct _PathCVT<char> {
  565. template <class _Iter>
  566. static typename enable_if<__is_exactly_input_iterator<_Iter>::value>::type
  567. __append_range(string& __dest, _Iter __b, _Iter __e) {
  568. for (; __b != __e; ++__b)
  569. __dest.push_back(*__b);
  570. }
  571. template <class _Iter>
  572. static typename enable_if<__is_forward_iterator<_Iter>::value>::type
  573. __append_range(string& __dest, _Iter __b, _Iter __e) {
  574. __dest.__append_forward_unsafe(__b, __e);
  575. }
  576. template <class _Iter>
  577. static void __append_range(string& __dest, _Iter __b, _NullSentinal) {
  578. const char __sentinal = char{};
  579. for (; *__b != __sentinal; ++__b)
  580. __dest.push_back(*__b);
  581. }
  582. template <class _Source>
  583. static void __append_source(string& __dest, _Source const& __s) {
  584. using _Traits = __is_pathable<_Source>;
  585. __append_range(__dest, _Traits::__range_begin(__s),
  586. _Traits::__range_end(__s));
  587. }
  588. };
  589. class _LIBCPP_TYPE_VIS path {
  590. template <class _SourceOrIter, class _Tp = path&>
  591. using _EnableIfPathable =
  592. typename enable_if<__is_pathable<_SourceOrIter>::value, _Tp>::type;
  593. template <class _Tp>
  594. using _SourceChar = typename __is_pathable<_Tp>::__char_type;
  595. template <class _Tp>
  596. using _SourceCVT = _PathCVT<_SourceChar<_Tp> >;
  597. public:
  598. typedef char value_type;
  599. typedef basic_string<value_type> string_type;
  600. typedef _VSTD::string_view __string_view;
  601. static constexpr value_type preferred_separator = '/';
  602. enum class _LIBCPP_ENUM_VIS format : unsigned char {
  603. auto_format,
  604. native_format,
  605. generic_format
  606. };
  607. // constructors and destructor
  608. _LIBCPP_INLINE_VISIBILITY path() noexcept {}
  609. _LIBCPP_INLINE_VISIBILITY path(const path& __p) : __pn_(__p.__pn_) {}
  610. _LIBCPP_INLINE_VISIBILITY path(path&& __p) noexcept
  611. : __pn_(_VSTD::move(__p.__pn_)) {}
  612. _LIBCPP_INLINE_VISIBILITY
  613. path(string_type&& __s, format = format::auto_format) noexcept
  614. : __pn_(_VSTD::move(__s)) {}
  615. template <class _Source, class = _EnableIfPathable<_Source, void> >
  616. path(const _Source& __src, format = format::auto_format) {
  617. _SourceCVT<_Source>::__append_source(__pn_, __src);
  618. }
  619. template <class _InputIt>
  620. path(_InputIt __first, _InputIt __last, format = format::auto_format) {
  621. typedef typename iterator_traits<_InputIt>::value_type _ItVal;
  622. _PathCVT<_ItVal>::__append_range(__pn_, __first, __last);
  623. }
  624. // TODO Implement locale conversions.
  625. template <class _Source, class = _EnableIfPathable<_Source, void> >
  626. path(const _Source& __src, const locale& __loc, format = format::auto_format);
  627. template <class _InputIt>
  628. path(_InputIt __first, _InputIt _last, const locale& __loc,
  629. format = format::auto_format);
  630. _LIBCPP_INLINE_VISIBILITY
  631. ~path() = default;
  632. // assignments
  633. _LIBCPP_INLINE_VISIBILITY
  634. path& operator=(const path& __p) {
  635. __pn_ = __p.__pn_;
  636. return *this;
  637. }
  638. _LIBCPP_INLINE_VISIBILITY
  639. path& operator=(path&& __p) noexcept {
  640. __pn_ = _VSTD::move(__p.__pn_);
  641. return *this;
  642. }
  643. template <class = void>
  644. _LIBCPP_INLINE_VISIBILITY path& operator=(string_type&& __s) noexcept {
  645. __pn_ = _VSTD::move(__s);
  646. return *this;
  647. }
  648. _LIBCPP_INLINE_VISIBILITY
  649. path& assign(string_type&& __s) noexcept {
  650. __pn_ = _VSTD::move(__s);
  651. return *this;
  652. }
  653. template <class _Source>
  654. _LIBCPP_INLINE_VISIBILITY _EnableIfPathable<_Source>
  655. operator=(const _Source& __src) {
  656. return this->assign(__src);
  657. }
  658. template <class _Source>
  659. _EnableIfPathable<_Source> assign(const _Source& __src) {
  660. __pn_.clear();
  661. _SourceCVT<_Source>::__append_source(__pn_, __src);
  662. return *this;
  663. }
  664. template <class _InputIt>
  665. path& assign(_InputIt __first, _InputIt __last) {
  666. typedef typename iterator_traits<_InputIt>::value_type _ItVal;
  667. __pn_.clear();
  668. _PathCVT<_ItVal>::__append_range(__pn_, __first, __last);
  669. return *this;
  670. }
  671. private:
  672. template <class _ECharT>
  673. static bool __source_is_absolute(_ECharT __first_or_null) {
  674. return __is_separator(__first_or_null);
  675. }
  676. public:
  677. // appends
  678. path& operator/=(const path& __p) {
  679. if (__p.is_absolute()) {
  680. __pn_ = __p.__pn_;
  681. return *this;
  682. }
  683. if (has_filename())
  684. __pn_ += preferred_separator;
  685. __pn_ += __p.native();
  686. return *this;
  687. }
  688. // FIXME: Use _LIBCPP_DIAGNOSE_WARNING to produce a diagnostic when __src
  689. // is known at compile time to be "/' since the user almost certainly intended
  690. // to append a separator instead of overwriting the path with "/"
  691. template <class _Source>
  692. _LIBCPP_INLINE_VISIBILITY _EnableIfPathable<_Source>
  693. operator/=(const _Source& __src) {
  694. return this->append(__src);
  695. }
  696. template <class _Source>
  697. _EnableIfPathable<_Source> append(const _Source& __src) {
  698. using _Traits = __is_pathable<_Source>;
  699. using _CVT = _PathCVT<_SourceChar<_Source> >;
  700. if (__source_is_absolute(_Traits::__first_or_null(__src)))
  701. __pn_.clear();
  702. else if (has_filename())
  703. __pn_ += preferred_separator;
  704. _CVT::__append_source(__pn_, __src);
  705. return *this;
  706. }
  707. template <class _InputIt>
  708. path& append(_InputIt __first, _InputIt __last) {
  709. typedef typename iterator_traits<_InputIt>::value_type _ItVal;
  710. static_assert(__can_convert_char<_ItVal>::value, "Must convertible");
  711. using _CVT = _PathCVT<_ItVal>;
  712. if (__first != __last && __source_is_absolute(*__first))
  713. __pn_.clear();
  714. else if (has_filename())
  715. __pn_ += preferred_separator;
  716. _CVT::__append_range(__pn_, __first, __last);
  717. return *this;
  718. }
  719. // concatenation
  720. _LIBCPP_INLINE_VISIBILITY
  721. path& operator+=(const path& __x) {
  722. __pn_ += __x.__pn_;
  723. return *this;
  724. }
  725. _LIBCPP_INLINE_VISIBILITY
  726. path& operator+=(const string_type& __x) {
  727. __pn_ += __x;
  728. return *this;
  729. }
  730. _LIBCPP_INLINE_VISIBILITY
  731. path& operator+=(__string_view __x) {
  732. __pn_ += __x;
  733. return *this;
  734. }
  735. _LIBCPP_INLINE_VISIBILITY
  736. path& operator+=(const value_type* __x) {
  737. __pn_ += __x;
  738. return *this;
  739. }
  740. _LIBCPP_INLINE_VISIBILITY
  741. path& operator+=(value_type __x) {
  742. __pn_ += __x;
  743. return *this;
  744. }
  745. template <class _ECharT>
  746. typename enable_if<__can_convert_char<_ECharT>::value, path&>::type
  747. operator+=(_ECharT __x) {
  748. basic_string<_ECharT> __tmp;
  749. __tmp += __x;
  750. _PathCVT<_ECharT>::__append_source(__pn_, __tmp);
  751. return *this;
  752. }
  753. template <class _Source>
  754. _EnableIfPathable<_Source> operator+=(const _Source& __x) {
  755. return this->concat(__x);
  756. }
  757. template <class _Source>
  758. _EnableIfPathable<_Source> concat(const _Source& __x) {
  759. _SourceCVT<_Source>::__append_source(__pn_, __x);
  760. return *this;
  761. }
  762. template <class _InputIt>
  763. path& concat(_InputIt __first, _InputIt __last) {
  764. typedef typename iterator_traits<_InputIt>::value_type _ItVal;
  765. _PathCVT<_ItVal>::__append_range(__pn_, __first, __last);
  766. return *this;
  767. }
  768. // modifiers
  769. _LIBCPP_INLINE_VISIBILITY
  770. void clear() noexcept { __pn_.clear(); }
  771. path& make_preferred() { return *this; }
  772. _LIBCPP_INLINE_VISIBILITY
  773. path& remove_filename() {
  774. auto __fname = __filename();
  775. if (!__fname.empty())
  776. __pn_.erase(__fname.data() - __pn_.data());
  777. return *this;
  778. }
  779. path& replace_filename(const path& __replacement) {
  780. remove_filename();
  781. return (*this /= __replacement);
  782. }
  783. path& replace_extension(const path& __replacement = path());
  784. _LIBCPP_INLINE_VISIBILITY
  785. void swap(path& __rhs) noexcept { __pn_.swap(__rhs.__pn_); }
  786. // private helper to allow reserving memory in the path
  787. _LIBCPP_INLINE_VISIBILITY
  788. void __reserve(size_t __s) { __pn_.reserve(__s); }
  789. // native format observers
  790. _LIBCPP_INLINE_VISIBILITY
  791. const string_type& native() const noexcept { return __pn_; }
  792. _LIBCPP_INLINE_VISIBILITY
  793. const value_type* c_str() const noexcept { return __pn_.c_str(); }
  794. _LIBCPP_INLINE_VISIBILITY operator string_type() const { return __pn_; }
  795. template <class _ECharT, class _Traits = char_traits<_ECharT>,
  796. class _Allocator = allocator<_ECharT> >
  797. basic_string<_ECharT, _Traits, _Allocator>
  798. string(const _Allocator& __a = _Allocator()) const {
  799. using _CVT = __widen_from_utf8<sizeof(_ECharT) * __CHAR_BIT__>;
  800. using _Str = basic_string<_ECharT, _Traits, _Allocator>;
  801. _Str __s(__a);
  802. __s.reserve(__pn_.size());
  803. _CVT()(back_inserter(__s), __pn_.data(), __pn_.data() + __pn_.size());
  804. return __s;
  805. }
  806. _LIBCPP_INLINE_VISIBILITY std::string string() const { return __pn_; }
  807. _LIBCPP_INLINE_VISIBILITY std::wstring wstring() const {
  808. return string<wchar_t>();
  809. }
  810. _LIBCPP_INLINE_VISIBILITY std::string u8string() const { return __pn_; }
  811. _LIBCPP_INLINE_VISIBILITY std::u16string u16string() const {
  812. return string<char16_t>();
  813. }
  814. _LIBCPP_INLINE_VISIBILITY std::u32string u32string() const {
  815. return string<char32_t>();
  816. }
  817. // generic format observers
  818. template <class _ECharT, class _Traits = char_traits<_ECharT>,
  819. class _Allocator = allocator<_ECharT> >
  820. basic_string<_ECharT, _Traits, _Allocator>
  821. generic_string(const _Allocator& __a = _Allocator()) const {
  822. return string<_ECharT, _Traits, _Allocator>(__a);
  823. }
  824. std::string generic_string() const { return __pn_; }
  825. std::wstring generic_wstring() const { return string<wchar_t>(); }
  826. std::string generic_u8string() const { return __pn_; }
  827. std::u16string generic_u16string() const { return string<char16_t>(); }
  828. std::u32string generic_u32string() const { return string<char32_t>(); }
  829. private:
  830. int __compare(__string_view) const;
  831. __string_view __root_name() const;
  832. __string_view __root_directory() const;
  833. __string_view __root_path_raw() const;
  834. __string_view __relative_path() const;
  835. __string_view __parent_path() const;
  836. __string_view __filename() const;
  837. __string_view __stem() const;
  838. __string_view __extension() const;
  839. public:
  840. // compare
  841. _LIBCPP_INLINE_VISIBILITY int compare(const path& __p) const noexcept {
  842. return __compare(__p.__pn_);
  843. }
  844. _LIBCPP_INLINE_VISIBILITY int compare(const string_type& __s) const {
  845. return __compare(__s);
  846. }
  847. _LIBCPP_INLINE_VISIBILITY int compare(__string_view __s) const {
  848. return __compare(__s);
  849. }
  850. _LIBCPP_INLINE_VISIBILITY int compare(const value_type* __s) const {
  851. return __compare(__s);
  852. }
  853. // decomposition
  854. _LIBCPP_INLINE_VISIBILITY path root_name() const {
  855. return string_type(__root_name());
  856. }
  857. _LIBCPP_INLINE_VISIBILITY path root_directory() const {
  858. return string_type(__root_directory());
  859. }
  860. _LIBCPP_INLINE_VISIBILITY path root_path() const {
  861. return root_name().append(string_type(__root_directory()));
  862. }
  863. _LIBCPP_INLINE_VISIBILITY path relative_path() const {
  864. return string_type(__relative_path());
  865. }
  866. _LIBCPP_INLINE_VISIBILITY path parent_path() const {
  867. return string_type(__parent_path());
  868. }
  869. _LIBCPP_INLINE_VISIBILITY path filename() const {
  870. return string_type(__filename());
  871. }
  872. _LIBCPP_INLINE_VISIBILITY path stem() const { return string_type(__stem()); }
  873. _LIBCPP_INLINE_VISIBILITY path extension() const {
  874. return string_type(__extension());
  875. }
  876. // query
  877. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY bool
  878. empty() const noexcept {
  879. return __pn_.empty();
  880. }
  881. _LIBCPP_INLINE_VISIBILITY bool has_root_name() const {
  882. return !__root_name().empty();
  883. }
  884. _LIBCPP_INLINE_VISIBILITY bool has_root_directory() const {
  885. return !__root_directory().empty();
  886. }
  887. _LIBCPP_INLINE_VISIBILITY bool has_root_path() const {
  888. return !__root_path_raw().empty();
  889. }
  890. _LIBCPP_INLINE_VISIBILITY bool has_relative_path() const {
  891. return !__relative_path().empty();
  892. }
  893. _LIBCPP_INLINE_VISIBILITY bool has_parent_path() const {
  894. return !__parent_path().empty();
  895. }
  896. _LIBCPP_INLINE_VISIBILITY bool has_filename() const {
  897. return !__filename().empty();
  898. }
  899. _LIBCPP_INLINE_VISIBILITY bool has_stem() const { return !__stem().empty(); }
  900. _LIBCPP_INLINE_VISIBILITY bool has_extension() const {
  901. return !__extension().empty();
  902. }
  903. _LIBCPP_INLINE_VISIBILITY bool is_absolute() const {
  904. return has_root_directory();
  905. }
  906. _LIBCPP_INLINE_VISIBILITY bool is_relative() const { return !is_absolute(); }
  907. // relative paths
  908. path lexically_normal() const;
  909. path lexically_relative(const path& __base) const;
  910. _LIBCPP_INLINE_VISIBILITY path lexically_proximate(const path& __base) const {
  911. path __result = this->lexically_relative(__base);
  912. if (__result.native().empty())
  913. return *this;
  914. return __result;
  915. }
  916. // iterators
  917. class _LIBCPP_TYPE_VIS iterator;
  918. typedef iterator const_iterator;
  919. iterator begin() const;
  920. iterator end() const;
  921. template <class _CharT, class _Traits>
  922. _LIBCPP_INLINE_VISIBILITY friend
  923. typename enable_if<is_same<_CharT, char>::value &&
  924. is_same<_Traits, char_traits<char> >::value,
  925. basic_ostream<_CharT, _Traits>&>::type
  926. operator<<(basic_ostream<_CharT, _Traits>& __os, const path& __p) {
  927. __os << std::__quoted(__p.native());
  928. return __os;
  929. }
  930. template <class _CharT, class _Traits>
  931. _LIBCPP_INLINE_VISIBILITY friend
  932. typename enable_if<!is_same<_CharT, char>::value ||
  933. !is_same<_Traits, char_traits<char> >::value,
  934. basic_ostream<_CharT, _Traits>&>::type
  935. operator<<(basic_ostream<_CharT, _Traits>& __os, const path& __p) {
  936. __os << std::__quoted(__p.string<_CharT, _Traits>());
  937. return __os;
  938. }
  939. template <class _CharT, class _Traits>
  940. _LIBCPP_INLINE_VISIBILITY friend basic_istream<_CharT, _Traits>&
  941. operator>>(basic_istream<_CharT, _Traits>& __is, path& __p) {
  942. basic_string<_CharT, _Traits> __tmp;
  943. __is >> __quoted(__tmp);
  944. __p = __tmp;
  945. return __is;
  946. }
  947. friend _LIBCPP_INLINE_VISIBILITY bool operator==(const path& __lhs, const path& __rhs) noexcept {
  948. return __lhs.compare(__rhs) == 0;
  949. }
  950. friend _LIBCPP_INLINE_VISIBILITY bool operator!=(const path& __lhs, const path& __rhs) noexcept {
  951. return __lhs.compare(__rhs) != 0;
  952. }
  953. friend _LIBCPP_INLINE_VISIBILITY bool operator<(const path& __lhs, const path& __rhs) noexcept {
  954. return __lhs.compare(__rhs) < 0;
  955. }
  956. friend _LIBCPP_INLINE_VISIBILITY bool operator<=(const path& __lhs, const path& __rhs) noexcept {
  957. return __lhs.compare(__rhs) <= 0;
  958. }
  959. friend _LIBCPP_INLINE_VISIBILITY bool operator>(const path& __lhs, const path& __rhs) noexcept {
  960. return __lhs.compare(__rhs) > 0;
  961. }
  962. friend _LIBCPP_INLINE_VISIBILITY bool operator>=(const path& __lhs, const path& __rhs) noexcept {
  963. return __lhs.compare(__rhs) >= 0;
  964. }
  965. friend _LIBCPP_INLINE_VISIBILITY path operator/(const path& __lhs,
  966. const path& __rhs) {
  967. path __result(__lhs);
  968. __result /= __rhs;
  969. return __result;
  970. }
  971. private:
  972. inline _LIBCPP_INLINE_VISIBILITY path&
  973. __assign_view(__string_view const& __s) noexcept {
  974. __pn_ = string_type(__s);
  975. return *this;
  976. }
  977. string_type __pn_;
  978. };
  979. inline _LIBCPP_INLINE_VISIBILITY void swap(path& __lhs, path& __rhs) noexcept {
  980. __lhs.swap(__rhs);
  981. }
  982. _LIBCPP_FUNC_VIS
  983. size_t hash_value(const path& __p) noexcept;
  984. template <class _Source>
  985. _LIBCPP_INLINE_VISIBILITY
  986. typename enable_if<__is_pathable<_Source>::value, path>::type
  987. u8path(const _Source& __s) {
  988. static_assert(
  989. is_same<typename __is_pathable<_Source>::__char_type, char>::value,
  990. "u8path(Source const&) requires Source have a character type of type "
  991. "'char'");
  992. return path(__s);
  993. }
  994. template <class _InputIt>
  995. _LIBCPP_INLINE_VISIBILITY
  996. typename enable_if<__is_pathable<_InputIt>::value, path>::type
  997. u8path(_InputIt __f, _InputIt __l) {
  998. static_assert(
  999. is_same<typename __is_pathable<_InputIt>::__char_type, char>::value,
  1000. "u8path(Iter, Iter) requires Iter have a value_type of type 'char'");
  1001. return path(__f, __l);
  1002. }
  1003. class _LIBCPP_TYPE_VIS path::iterator {
  1004. public:
  1005. enum _ParserState : unsigned char {
  1006. _Singular,
  1007. _BeforeBegin,
  1008. _InRootName,
  1009. _InRootDir,
  1010. _InFilenames,
  1011. _InTrailingSep,
  1012. _AtEnd
  1013. };
  1014. public:
  1015. typedef bidirectional_iterator_tag iterator_category;
  1016. typedef path value_type;
  1017. typedef std::ptrdiff_t difference_type;
  1018. typedef const path* pointer;
  1019. typedef const path& reference;
  1020. typedef void
  1021. __stashing_iterator_tag; // See reverse_iterator and __is_stashing_iterator
  1022. public:
  1023. _LIBCPP_INLINE_VISIBILITY
  1024. iterator()
  1025. : __stashed_elem_(), __path_ptr_(nullptr), __entry_(),
  1026. __state_(_Singular) {}
  1027. iterator(const iterator&) = default;
  1028. ~iterator() = default;
  1029. iterator& operator=(const iterator&) = default;
  1030. _LIBCPP_INLINE_VISIBILITY
  1031. reference operator*() const { return __stashed_elem_; }
  1032. _LIBCPP_INLINE_VISIBILITY
  1033. pointer operator->() const { return &__stashed_elem_; }
  1034. _LIBCPP_INLINE_VISIBILITY
  1035. iterator& operator++() {
  1036. _LIBCPP_ASSERT(__state_ != _Singular,
  1037. "attempting to increment a singular iterator");
  1038. _LIBCPP_ASSERT(__state_ != _AtEnd,
  1039. "attempting to increment the end iterator");
  1040. return __increment();
  1041. }
  1042. _LIBCPP_INLINE_VISIBILITY
  1043. iterator operator++(int) {
  1044. iterator __it(*this);
  1045. this->operator++();
  1046. return __it;
  1047. }
  1048. _LIBCPP_INLINE_VISIBILITY
  1049. iterator& operator--() {
  1050. _LIBCPP_ASSERT(__state_ != _Singular,
  1051. "attempting to decrement a singular iterator");
  1052. _LIBCPP_ASSERT(__entry_.data() != __path_ptr_->native().data(),
  1053. "attempting to decrement the begin iterator");
  1054. return __decrement();
  1055. }
  1056. _LIBCPP_INLINE_VISIBILITY
  1057. iterator operator--(int) {
  1058. iterator __it(*this);
  1059. this->operator--();
  1060. return __it;
  1061. }
  1062. private:
  1063. friend class path;
  1064. inline _LIBCPP_INLINE_VISIBILITY friend bool operator==(const iterator&,
  1065. const iterator&);
  1066. iterator& __increment();
  1067. iterator& __decrement();
  1068. path __stashed_elem_;
  1069. const path* __path_ptr_;
  1070. path::__string_view __entry_;
  1071. _ParserState __state_;
  1072. };
  1073. inline _LIBCPP_INLINE_VISIBILITY bool operator==(const path::iterator& __lhs,
  1074. const path::iterator& __rhs) {
  1075. return __lhs.__path_ptr_ == __rhs.__path_ptr_ &&
  1076. __lhs.__entry_.data() == __rhs.__entry_.data();
  1077. }
  1078. inline _LIBCPP_INLINE_VISIBILITY bool operator!=(const path::iterator& __lhs,
  1079. const path::iterator& __rhs) {
  1080. return !(__lhs == __rhs);
  1081. }
  1082. // TODO(ldionne): We need to pop the pragma and push it again after
  1083. // filesystem_error to work around PR41078.
  1084. _LIBCPP_AVAILABILITY_FILESYSTEM_POP
  1085. class _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_EXCEPTION_ABI filesystem_error : public system_error {
  1086. public:
  1087. _LIBCPP_INLINE_VISIBILITY
  1088. filesystem_error(const string& __what, error_code __ec)
  1089. : system_error(__ec, __what),
  1090. __storage_(make_shared<_Storage>(path(), path())) {
  1091. __create_what(0);
  1092. }
  1093. _LIBCPP_INLINE_VISIBILITY
  1094. filesystem_error(const string& __what, const path& __p1, error_code __ec)
  1095. : system_error(__ec, __what),
  1096. __storage_(make_shared<_Storage>(__p1, path())) {
  1097. __create_what(1);
  1098. }
  1099. _LIBCPP_INLINE_VISIBILITY
  1100. filesystem_error(const string& __what, const path& __p1, const path& __p2,
  1101. error_code __ec)
  1102. : system_error(__ec, __what),
  1103. __storage_(make_shared<_Storage>(__p1, __p2)) {
  1104. __create_what(2);
  1105. }
  1106. _LIBCPP_INLINE_VISIBILITY
  1107. const path& path1() const noexcept { return __storage_->__p1_; }
  1108. _LIBCPP_INLINE_VISIBILITY
  1109. const path& path2() const noexcept { return __storage_->__p2_; }
  1110. ~filesystem_error() override; // key function
  1111. _LIBCPP_INLINE_VISIBILITY
  1112. const char* what() const noexcept override {
  1113. return __storage_->__what_.c_str();
  1114. }
  1115. void __create_what(int __num_paths);
  1116. private:
  1117. struct _LIBCPP_HIDDEN _Storage {
  1118. _LIBCPP_INLINE_VISIBILITY
  1119. _Storage(const path& __p1, const path& __p2) : __p1_(__p1), __p2_(__p2) {}
  1120. path __p1_;
  1121. path __p2_;
  1122. string __what_;
  1123. };
  1124. shared_ptr<_Storage> __storage_;
  1125. };
  1126. _LIBCPP_AVAILABILITY_FILESYSTEM_PUSH
  1127. template <class... _Args>
  1128. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  1129. #ifndef _LIBCPP_NO_EXCEPTIONS
  1130. void __throw_filesystem_error(_Args&&... __args) {
  1131. throw filesystem_error(std::forward<_Args>(__args)...);
  1132. }
  1133. #else
  1134. void __throw_filesystem_error(_Args&&...) {
  1135. _VSTD::abort();
  1136. }
  1137. #endif
  1138. // operational functions
  1139. _LIBCPP_FUNC_VIS
  1140. path __absolute(const path&, error_code* __ec = nullptr);
  1141. _LIBCPP_FUNC_VIS
  1142. path __canonical(const path&, error_code* __ec = nullptr);
  1143. _LIBCPP_FUNC_VIS
  1144. void __copy(const path& __from, const path& __to, copy_options __opt,
  1145. error_code* __ec = nullptr);
  1146. _LIBCPP_FUNC_VIS
  1147. bool __copy_file(const path& __from, const path& __to, copy_options __opt,
  1148. error_code* __ec = nullptr);
  1149. _LIBCPP_FUNC_VIS
  1150. void __copy_symlink(const path& __existing_symlink, const path& __new_symlink,
  1151. error_code* __ec = nullptr);
  1152. _LIBCPP_FUNC_VIS
  1153. bool __create_directories(const path& p, error_code* ec = nullptr);
  1154. _LIBCPP_FUNC_VIS
  1155. bool __create_directory(const path& p, error_code* ec = nullptr);
  1156. _LIBCPP_FUNC_VIS
  1157. bool __create_directory(const path& p, const path& attributes,
  1158. error_code* ec = nullptr);
  1159. _LIBCPP_FUNC_VIS
  1160. void __create_directory_symlink(const path& __to, const path& __new_symlink,
  1161. error_code* __ec = nullptr);
  1162. _LIBCPP_FUNC_VIS
  1163. void __create_hard_link(const path& __to, const path& __new_hard_link,
  1164. error_code* __ec = nullptr);
  1165. _LIBCPP_FUNC_VIS
  1166. void __create_symlink(const path& __to, const path& __new_symlink,
  1167. error_code* __ec = nullptr);
  1168. _LIBCPP_FUNC_VIS
  1169. path __current_path(error_code* __ec = nullptr);
  1170. _LIBCPP_FUNC_VIS
  1171. void __current_path(const path&, error_code* __ec = nullptr);
  1172. _LIBCPP_FUNC_VIS
  1173. bool __equivalent(const path&, const path&, error_code* __ec = nullptr);
  1174. _LIBCPP_FUNC_VIS
  1175. uintmax_t __file_size(const path&, error_code* __ec = nullptr);
  1176. _LIBCPP_FUNC_VIS
  1177. uintmax_t __hard_link_count(const path&, error_code* __ec = nullptr);
  1178. _LIBCPP_FUNC_VIS
  1179. bool __fs_is_empty(const path& p, error_code* ec = nullptr);
  1180. _LIBCPP_FUNC_VIS
  1181. file_time_type __last_write_time(const path& p, error_code* ec = nullptr);
  1182. _LIBCPP_FUNC_VIS
  1183. void __last_write_time(const path& p, file_time_type new_time,
  1184. error_code* ec = nullptr);
  1185. _LIBCPP_FUNC_VIS
  1186. void __permissions(const path&, perms, perm_options, error_code* = nullptr);
  1187. _LIBCPP_FUNC_VIS
  1188. path __read_symlink(const path& p, error_code* ec = nullptr);
  1189. _LIBCPP_FUNC_VIS
  1190. bool __remove(const path& p, error_code* ec = nullptr);
  1191. _LIBCPP_FUNC_VIS
  1192. uintmax_t __remove_all(const path& p, error_code* ec = nullptr);
  1193. _LIBCPP_FUNC_VIS
  1194. void __rename(const path& from, const path& to, error_code* ec = nullptr);
  1195. _LIBCPP_FUNC_VIS
  1196. void __resize_file(const path& p, uintmax_t size, error_code* ec = nullptr);
  1197. _LIBCPP_FUNC_VIS
  1198. space_info __space(const path&, error_code* __ec = nullptr);
  1199. _LIBCPP_FUNC_VIS
  1200. file_status __status(const path&, error_code* __ec = nullptr);
  1201. _LIBCPP_FUNC_VIS
  1202. file_status __symlink_status(const path&, error_code* __ec = nullptr);
  1203. _LIBCPP_FUNC_VIS
  1204. path __system_complete(const path&, error_code* __ec = nullptr);
  1205. _LIBCPP_FUNC_VIS
  1206. path __temp_directory_path(error_code* __ec = nullptr);
  1207. _LIBCPP_FUNC_VIS
  1208. path __weakly_canonical(path const& __p, error_code* __ec = nullptr);
  1209. inline _LIBCPP_INLINE_VISIBILITY path current_path() {
  1210. return __current_path();
  1211. }
  1212. inline _LIBCPP_INLINE_VISIBILITY path current_path(error_code& __ec) {
  1213. return __current_path(&__ec);
  1214. }
  1215. inline _LIBCPP_INLINE_VISIBILITY void current_path(const path& __p) {
  1216. __current_path(__p);
  1217. }
  1218. inline _LIBCPP_INLINE_VISIBILITY void current_path(const path& __p,
  1219. error_code& __ec) noexcept {
  1220. __current_path(__p, &__ec);
  1221. }
  1222. inline _LIBCPP_INLINE_VISIBILITY path absolute(const path& __p) {
  1223. return __absolute(__p);
  1224. }
  1225. inline _LIBCPP_INLINE_VISIBILITY path absolute(const path& __p,
  1226. error_code& __ec) {
  1227. return __absolute(__p, &__ec);
  1228. }
  1229. inline _LIBCPP_INLINE_VISIBILITY path canonical(const path& __p) {
  1230. return __canonical(__p);
  1231. }
  1232. inline _LIBCPP_INLINE_VISIBILITY path canonical(const path& __p,
  1233. error_code& __ec) {
  1234. return __canonical(__p, &__ec);
  1235. }
  1236. inline _LIBCPP_INLINE_VISIBILITY void copy(const path& __from,
  1237. const path& __to) {
  1238. __copy(__from, __to, copy_options::none);
  1239. }
  1240. inline _LIBCPP_INLINE_VISIBILITY void copy(const path& __from, const path& __to,
  1241. error_code& __ec) {
  1242. __copy(__from, __to, copy_options::none, &__ec);
  1243. }
  1244. inline _LIBCPP_INLINE_VISIBILITY void copy(const path& __from, const path& __to,
  1245. copy_options __opt) {
  1246. __copy(__from, __to, __opt);
  1247. }
  1248. inline _LIBCPP_INLINE_VISIBILITY void copy(const path& __from, const path& __to,
  1249. copy_options __opt,
  1250. error_code& __ec) {
  1251. __copy(__from, __to, __opt, &__ec);
  1252. }
  1253. inline _LIBCPP_INLINE_VISIBILITY bool copy_file(const path& __from,
  1254. const path& __to) {
  1255. return __copy_file(__from, __to, copy_options::none);
  1256. }
  1257. inline _LIBCPP_INLINE_VISIBILITY bool
  1258. copy_file(const path& __from, const path& __to, error_code& __ec) {
  1259. return __copy_file(__from, __to, copy_options::none, &__ec);
  1260. }
  1261. inline _LIBCPP_INLINE_VISIBILITY bool
  1262. copy_file(const path& __from, const path& __to, copy_options __opt) {
  1263. return __copy_file(__from, __to, __opt);
  1264. }
  1265. inline _LIBCPP_INLINE_VISIBILITY bool copy_file(const path& __from,
  1266. const path& __to,
  1267. copy_options __opt,
  1268. error_code& __ec) {
  1269. return __copy_file(__from, __to, __opt, &__ec);
  1270. }
  1271. inline _LIBCPP_INLINE_VISIBILITY void copy_symlink(const path& __existing,
  1272. const path& __new) {
  1273. __copy_symlink(__existing, __new);
  1274. }
  1275. inline _LIBCPP_INLINE_VISIBILITY void
  1276. copy_symlink(const path& __ext, const path& __new, error_code& __ec) noexcept {
  1277. __copy_symlink(__ext, __new, &__ec);
  1278. }
  1279. inline _LIBCPP_INLINE_VISIBILITY bool create_directories(const path& __p) {
  1280. return __create_directories(__p);
  1281. }
  1282. inline _LIBCPP_INLINE_VISIBILITY bool create_directories(const path& __p,
  1283. error_code& __ec) {
  1284. return __create_directories(__p, &__ec);
  1285. }
  1286. inline _LIBCPP_INLINE_VISIBILITY bool create_directory(const path& __p) {
  1287. return __create_directory(__p);
  1288. }
  1289. inline _LIBCPP_INLINE_VISIBILITY bool
  1290. create_directory(const path& __p, error_code& __ec) noexcept {
  1291. return __create_directory(__p, &__ec);
  1292. }
  1293. inline _LIBCPP_INLINE_VISIBILITY bool create_directory(const path& __p,
  1294. const path& __attrs) {
  1295. return __create_directory(__p, __attrs);
  1296. }
  1297. inline _LIBCPP_INLINE_VISIBILITY bool
  1298. create_directory(const path& __p, const path& __attrs,
  1299. error_code& __ec) noexcept {
  1300. return __create_directory(__p, __attrs, &__ec);
  1301. }
  1302. inline _LIBCPP_INLINE_VISIBILITY void
  1303. create_directory_symlink(const path& __to, const path& __new) {
  1304. __create_directory_symlink(__to, __new);
  1305. }
  1306. inline _LIBCPP_INLINE_VISIBILITY void
  1307. create_directory_symlink(const path& __to, const path& __new,
  1308. error_code& __ec) noexcept {
  1309. __create_directory_symlink(__to, __new, &__ec);
  1310. }
  1311. inline _LIBCPP_INLINE_VISIBILITY void create_hard_link(const path& __to,
  1312. const path& __new) {
  1313. __create_hard_link(__to, __new);
  1314. }
  1315. inline _LIBCPP_INLINE_VISIBILITY void
  1316. create_hard_link(const path& __to, const path& __new,
  1317. error_code& __ec) noexcept {
  1318. __create_hard_link(__to, __new, &__ec);
  1319. }
  1320. inline _LIBCPP_INLINE_VISIBILITY void create_symlink(const path& __to,
  1321. const path& __new) {
  1322. __create_symlink(__to, __new);
  1323. }
  1324. inline _LIBCPP_INLINE_VISIBILITY void
  1325. create_symlink(const path& __to, const path& __new, error_code& __ec) noexcept {
  1326. return __create_symlink(__to, __new, &__ec);
  1327. }
  1328. inline _LIBCPP_INLINE_VISIBILITY bool status_known(file_status __s) noexcept {
  1329. return __s.type() != file_type::none;
  1330. }
  1331. inline _LIBCPP_INLINE_VISIBILITY bool exists(file_status __s) noexcept {
  1332. return status_known(__s) && __s.type() != file_type::not_found;
  1333. }
  1334. inline _LIBCPP_INLINE_VISIBILITY bool exists(const path& __p) {
  1335. return exists(__status(__p));
  1336. }
  1337. inline _LIBCPP_INLINE_VISIBILITY bool exists(const path& __p,
  1338. error_code& __ec) noexcept {
  1339. auto __s = __status(__p, &__ec);
  1340. if (status_known(__s))
  1341. __ec.clear();
  1342. return exists(__s);
  1343. }
  1344. inline _LIBCPP_INLINE_VISIBILITY bool equivalent(const path& __p1,
  1345. const path& __p2) {
  1346. return __equivalent(__p1, __p2);
  1347. }
  1348. inline _LIBCPP_INLINE_VISIBILITY bool
  1349. equivalent(const path& __p1, const path& __p2, error_code& __ec) noexcept {
  1350. return __equivalent(__p1, __p2, &__ec);
  1351. }
  1352. inline _LIBCPP_INLINE_VISIBILITY uintmax_t file_size(const path& __p) {
  1353. return __file_size(__p);
  1354. }
  1355. inline _LIBCPP_INLINE_VISIBILITY uintmax_t
  1356. file_size(const path& __p, error_code& __ec) noexcept {
  1357. return __file_size(__p, &__ec);
  1358. }
  1359. inline _LIBCPP_INLINE_VISIBILITY uintmax_t hard_link_count(const path& __p) {
  1360. return __hard_link_count(__p);
  1361. }
  1362. inline _LIBCPP_INLINE_VISIBILITY uintmax_t
  1363. hard_link_count(const path& __p, error_code& __ec) noexcept {
  1364. return __hard_link_count(__p, &__ec);
  1365. }
  1366. inline _LIBCPP_INLINE_VISIBILITY bool is_block_file(file_status __s) noexcept {
  1367. return __s.type() == file_type::block;
  1368. }
  1369. inline _LIBCPP_INLINE_VISIBILITY bool is_block_file(const path& __p) {
  1370. return is_block_file(__status(__p));
  1371. }
  1372. inline _LIBCPP_INLINE_VISIBILITY bool is_block_file(const path& __p,
  1373. error_code& __ec) noexcept {
  1374. return is_block_file(__status(__p, &__ec));
  1375. }
  1376. inline _LIBCPP_INLINE_VISIBILITY bool
  1377. is_character_file(file_status __s) noexcept {
  1378. return __s.type() == file_type::character;
  1379. }
  1380. inline _LIBCPP_INLINE_VISIBILITY bool is_character_file(const path& __p) {
  1381. return is_character_file(__status(__p));
  1382. }
  1383. inline _LIBCPP_INLINE_VISIBILITY bool
  1384. is_character_file(const path& __p, error_code& __ec) noexcept {
  1385. return is_character_file(__status(__p, &__ec));
  1386. }
  1387. inline _LIBCPP_INLINE_VISIBILITY bool is_directory(file_status __s) noexcept {
  1388. return __s.type() == file_type::directory;
  1389. }
  1390. inline _LIBCPP_INLINE_VISIBILITY bool is_directory(const path& __p) {
  1391. return is_directory(__status(__p));
  1392. }
  1393. inline _LIBCPP_INLINE_VISIBILITY bool is_directory(const path& __p,
  1394. error_code& __ec) noexcept {
  1395. return is_directory(__status(__p, &__ec));
  1396. }
  1397. inline _LIBCPP_INLINE_VISIBILITY bool is_empty(const path& __p) {
  1398. return __fs_is_empty(__p);
  1399. }
  1400. inline _LIBCPP_INLINE_VISIBILITY bool is_empty(const path& __p,
  1401. error_code& __ec) {
  1402. return __fs_is_empty(__p, &__ec);
  1403. }
  1404. inline _LIBCPP_INLINE_VISIBILITY bool is_fifo(file_status __s) noexcept {
  1405. return __s.type() == file_type::fifo;
  1406. }
  1407. inline _LIBCPP_INLINE_VISIBILITY bool is_fifo(const path& __p) {
  1408. return is_fifo(__status(__p));
  1409. }
  1410. inline _LIBCPP_INLINE_VISIBILITY bool is_fifo(const path& __p,
  1411. error_code& __ec) noexcept {
  1412. return is_fifo(__status(__p, &__ec));
  1413. }
  1414. inline _LIBCPP_INLINE_VISIBILITY bool
  1415. is_regular_file(file_status __s) noexcept {
  1416. return __s.type() == file_type::regular;
  1417. }
  1418. inline _LIBCPP_INLINE_VISIBILITY bool is_regular_file(const path& __p) {
  1419. return is_regular_file(__status(__p));
  1420. }
  1421. inline _LIBCPP_INLINE_VISIBILITY bool
  1422. is_regular_file(const path& __p, error_code& __ec) noexcept {
  1423. return is_regular_file(__status(__p, &__ec));
  1424. }
  1425. inline _LIBCPP_INLINE_VISIBILITY bool is_socket(file_status __s) noexcept {
  1426. return __s.type() == file_type::socket;
  1427. }
  1428. inline _LIBCPP_INLINE_VISIBILITY bool is_socket(const path& __p) {
  1429. return is_socket(__status(__p));
  1430. }
  1431. inline _LIBCPP_INLINE_VISIBILITY bool is_socket(const path& __p,
  1432. error_code& __ec) noexcept {
  1433. return is_socket(__status(__p, &__ec));
  1434. }
  1435. inline _LIBCPP_INLINE_VISIBILITY bool is_symlink(file_status __s) noexcept {
  1436. return __s.type() == file_type::symlink;
  1437. }
  1438. inline _LIBCPP_INLINE_VISIBILITY bool is_symlink(const path& __p) {
  1439. return is_symlink(__symlink_status(__p));
  1440. }
  1441. inline _LIBCPP_INLINE_VISIBILITY bool is_symlink(const path& __p,
  1442. error_code& __ec) noexcept {
  1443. return is_symlink(__symlink_status(__p, &__ec));
  1444. }
  1445. inline _LIBCPP_INLINE_VISIBILITY bool is_other(file_status __s) noexcept {
  1446. return exists(__s) && !is_regular_file(__s) && !is_directory(__s) &&
  1447. !is_symlink(__s);
  1448. }
  1449. inline _LIBCPP_INLINE_VISIBILITY bool is_other(const path& __p) {
  1450. return is_other(__status(__p));
  1451. }
  1452. inline _LIBCPP_INLINE_VISIBILITY bool is_other(const path& __p,
  1453. error_code& __ec) noexcept {
  1454. return is_other(__status(__p, &__ec));
  1455. }
  1456. inline _LIBCPP_INLINE_VISIBILITY file_time_type
  1457. last_write_time(const path& __p) {
  1458. return __last_write_time(__p);
  1459. }
  1460. inline _LIBCPP_INLINE_VISIBILITY file_time_type
  1461. last_write_time(const path& __p, error_code& __ec) noexcept {
  1462. return __last_write_time(__p, &__ec);
  1463. }
  1464. inline _LIBCPP_INLINE_VISIBILITY void last_write_time(const path& __p,
  1465. file_time_type __t) {
  1466. __last_write_time(__p, __t);
  1467. }
  1468. inline _LIBCPP_INLINE_VISIBILITY void
  1469. last_write_time(const path& __p, file_time_type __t,
  1470. error_code& __ec) noexcept {
  1471. __last_write_time(__p, __t, &__ec);
  1472. }
  1473. inline _LIBCPP_INLINE_VISIBILITY void
  1474. permissions(const path& __p, perms __prms,
  1475. perm_options __opts = perm_options::replace) {
  1476. __permissions(__p, __prms, __opts);
  1477. }
  1478. inline _LIBCPP_INLINE_VISIBILITY void permissions(const path& __p, perms __prms,
  1479. error_code& __ec) noexcept {
  1480. __permissions(__p, __prms, perm_options::replace, &__ec);
  1481. }
  1482. inline _LIBCPP_INLINE_VISIBILITY void permissions(const path& __p, perms __prms,
  1483. perm_options __opts,
  1484. error_code& __ec) {
  1485. __permissions(__p, __prms, __opts, &__ec);
  1486. }
  1487. inline _LIBCPP_INLINE_VISIBILITY path proximate(const path& __p,
  1488. const path& __base,
  1489. error_code& __ec) {
  1490. path __tmp = __weakly_canonical(__p, &__ec);
  1491. if (__ec)
  1492. return {};
  1493. path __tmp_base = __weakly_canonical(__base, &__ec);
  1494. if (__ec)
  1495. return {};
  1496. return __tmp.lexically_proximate(__tmp_base);
  1497. }
  1498. inline _LIBCPP_INLINE_VISIBILITY path proximate(const path& __p,
  1499. error_code& __ec) {
  1500. return proximate(__p, current_path(), __ec);
  1501. }
  1502. inline _LIBCPP_INLINE_VISIBILITY path
  1503. proximate(const path& __p, const path& __base = current_path()) {
  1504. return __weakly_canonical(__p).lexically_proximate(
  1505. __weakly_canonical(__base));
  1506. }
  1507. inline _LIBCPP_INLINE_VISIBILITY path read_symlink(const path& __p) {
  1508. return __read_symlink(__p);
  1509. }
  1510. inline _LIBCPP_INLINE_VISIBILITY path read_symlink(const path& __p,
  1511. error_code& __ec) {
  1512. return __read_symlink(__p, &__ec);
  1513. }
  1514. inline _LIBCPP_INLINE_VISIBILITY path relative(const path& __p,
  1515. const path& __base,
  1516. error_code& __ec) {
  1517. path __tmp = __weakly_canonical(__p, &__ec);
  1518. if (__ec)
  1519. return path();
  1520. path __tmpbase = __weakly_canonical(__base, &__ec);
  1521. if (__ec)
  1522. return path();
  1523. return __tmp.lexically_relative(__tmpbase);
  1524. }
  1525. inline _LIBCPP_INLINE_VISIBILITY path relative(const path& __p,
  1526. error_code& __ec) {
  1527. return relative(__p, current_path(), __ec);
  1528. }
  1529. inline _LIBCPP_INLINE_VISIBILITY path
  1530. relative(const path& __p, const path& __base = current_path()) {
  1531. return __weakly_canonical(__p).lexically_relative(__weakly_canonical(__base));
  1532. }
  1533. inline _LIBCPP_INLINE_VISIBILITY bool remove(const path& __p) {
  1534. return __remove(__p);
  1535. }
  1536. inline _LIBCPP_INLINE_VISIBILITY bool remove(const path& __p,
  1537. error_code& __ec) noexcept {
  1538. return __remove(__p, &__ec);
  1539. }
  1540. inline _LIBCPP_INLINE_VISIBILITY uintmax_t remove_all(const path& __p) {
  1541. return __remove_all(__p);
  1542. }
  1543. inline _LIBCPP_INLINE_VISIBILITY uintmax_t remove_all(const path& __p,
  1544. error_code& __ec) {
  1545. return __remove_all(__p, &__ec);
  1546. }
  1547. inline _LIBCPP_INLINE_VISIBILITY void rename(const path& __from,
  1548. const path& __to) {
  1549. return __rename(__from, __to);
  1550. }
  1551. inline _LIBCPP_INLINE_VISIBILITY void
  1552. rename(const path& __from, const path& __to, error_code& __ec) noexcept {
  1553. return __rename(__from, __to, &__ec);
  1554. }
  1555. inline _LIBCPP_INLINE_VISIBILITY void resize_file(const path& __p,
  1556. uintmax_t __ns) {
  1557. return __resize_file(__p, __ns);
  1558. }
  1559. inline _LIBCPP_INLINE_VISIBILITY void
  1560. resize_file(const path& __p, uintmax_t __ns, error_code& __ec) noexcept {
  1561. return __resize_file(__p, __ns, &__ec);
  1562. }
  1563. inline _LIBCPP_INLINE_VISIBILITY space_info space(const path& __p) {
  1564. return __space(__p);
  1565. }
  1566. inline _LIBCPP_INLINE_VISIBILITY space_info space(const path& __p,
  1567. error_code& __ec) noexcept {
  1568. return __space(__p, &__ec);
  1569. }
  1570. inline _LIBCPP_INLINE_VISIBILITY file_status status(const path& __p) {
  1571. return __status(__p);
  1572. }
  1573. inline _LIBCPP_INLINE_VISIBILITY file_status status(const path& __p,
  1574. error_code& __ec) noexcept {
  1575. return __status(__p, &__ec);
  1576. }
  1577. inline _LIBCPP_INLINE_VISIBILITY file_status symlink_status(const path& __p) {
  1578. return __symlink_status(__p);
  1579. }
  1580. inline _LIBCPP_INLINE_VISIBILITY file_status
  1581. symlink_status(const path& __p, error_code& __ec) noexcept {
  1582. return __symlink_status(__p, &__ec);
  1583. }
  1584. inline _LIBCPP_INLINE_VISIBILITY path temp_directory_path() {
  1585. return __temp_directory_path();
  1586. }
  1587. inline _LIBCPP_INLINE_VISIBILITY path temp_directory_path(error_code& __ec) {
  1588. return __temp_directory_path(&__ec);
  1589. }
  1590. inline _LIBCPP_INLINE_VISIBILITY path weakly_canonical(path const& __p) {
  1591. return __weakly_canonical(__p);
  1592. }
  1593. inline _LIBCPP_INLINE_VISIBILITY path weakly_canonical(path const& __p,
  1594. error_code& __ec) {
  1595. return __weakly_canonical(__p, &__ec);
  1596. }
  1597. class directory_iterator;
  1598. class recursive_directory_iterator;
  1599. class _LIBCPP_HIDDEN __dir_stream;
  1600. class directory_entry {
  1601. typedef _VSTD_FS::path _Path;
  1602. public:
  1603. // constructors and destructors
  1604. directory_entry() noexcept = default;
  1605. directory_entry(directory_entry const&) = default;
  1606. directory_entry(directory_entry&&) noexcept = default;
  1607. _LIBCPP_INLINE_VISIBILITY
  1608. explicit directory_entry(_Path const& __p) : __p_(__p) {
  1609. error_code __ec;
  1610. __refresh(&__ec);
  1611. }
  1612. _LIBCPP_INLINE_VISIBILITY
  1613. directory_entry(_Path const& __p, error_code& __ec) : __p_(__p) {
  1614. __refresh(&__ec);
  1615. }
  1616. ~directory_entry() {}
  1617. directory_entry& operator=(directory_entry const&) = default;
  1618. directory_entry& operator=(directory_entry&&) noexcept = default;
  1619. _LIBCPP_INLINE_VISIBILITY
  1620. void assign(_Path const& __p) {
  1621. __p_ = __p;
  1622. error_code __ec;
  1623. __refresh(&__ec);
  1624. }
  1625. _LIBCPP_INLINE_VISIBILITY
  1626. void assign(_Path const& __p, error_code& __ec) {
  1627. __p_ = __p;
  1628. __refresh(&__ec);
  1629. }
  1630. _LIBCPP_INLINE_VISIBILITY
  1631. void replace_filename(_Path const& __p) {
  1632. __p_.replace_filename(__p);
  1633. error_code __ec;
  1634. __refresh(&__ec);
  1635. }
  1636. _LIBCPP_INLINE_VISIBILITY
  1637. void replace_filename(_Path const& __p, error_code& __ec) {
  1638. __p_ = __p_.parent_path() / __p;
  1639. __refresh(&__ec);
  1640. }
  1641. _LIBCPP_INLINE_VISIBILITY
  1642. void refresh() { __refresh(); }
  1643. _LIBCPP_INLINE_VISIBILITY
  1644. void refresh(error_code& __ec) noexcept { __refresh(&__ec); }
  1645. _LIBCPP_INLINE_VISIBILITY
  1646. _Path const& path() const noexcept { return __p_; }
  1647. _LIBCPP_INLINE_VISIBILITY
  1648. operator const _Path&() const noexcept { return __p_; }
  1649. _LIBCPP_INLINE_VISIBILITY
  1650. bool exists() const { return _VSTD_FS::exists(file_status{__get_ft()}); }
  1651. _LIBCPP_INLINE_VISIBILITY
  1652. bool exists(error_code& __ec) const noexcept {
  1653. return _VSTD_FS::exists(file_status{__get_ft(&__ec)});
  1654. }
  1655. _LIBCPP_INLINE_VISIBILITY
  1656. bool is_block_file() const { return __get_ft() == file_type::block; }
  1657. _LIBCPP_INLINE_VISIBILITY
  1658. bool is_block_file(error_code& __ec) const noexcept {
  1659. return __get_ft(&__ec) == file_type::block;
  1660. }
  1661. _LIBCPP_INLINE_VISIBILITY
  1662. bool is_character_file() const { return __get_ft() == file_type::character; }
  1663. _LIBCPP_INLINE_VISIBILITY
  1664. bool is_character_file(error_code& __ec) const noexcept {
  1665. return __get_ft(&__ec) == file_type::character;
  1666. }
  1667. _LIBCPP_INLINE_VISIBILITY
  1668. bool is_directory() const { return __get_ft() == file_type::directory; }
  1669. _LIBCPP_INLINE_VISIBILITY
  1670. bool is_directory(error_code& __ec) const noexcept {
  1671. return __get_ft(&__ec) == file_type::directory;
  1672. }
  1673. _LIBCPP_INLINE_VISIBILITY
  1674. bool is_fifo() const { return __get_ft() == file_type::fifo; }
  1675. _LIBCPP_INLINE_VISIBILITY
  1676. bool is_fifo(error_code& __ec) const noexcept {
  1677. return __get_ft(&__ec) == file_type::fifo;
  1678. }
  1679. _LIBCPP_INLINE_VISIBILITY
  1680. bool is_other() const { return _VSTD_FS::is_other(file_status{__get_ft()}); }
  1681. _LIBCPP_INLINE_VISIBILITY
  1682. bool is_other(error_code& __ec) const noexcept {
  1683. return _VSTD_FS::is_other(file_status{__get_ft(&__ec)});
  1684. }
  1685. _LIBCPP_INLINE_VISIBILITY
  1686. bool is_regular_file() const { return __get_ft() == file_type::regular; }
  1687. _LIBCPP_INLINE_VISIBILITY
  1688. bool is_regular_file(error_code& __ec) const noexcept {
  1689. return __get_ft(&__ec) == file_type::regular;
  1690. }
  1691. _LIBCPP_INLINE_VISIBILITY
  1692. bool is_socket() const { return __get_ft() == file_type::socket; }
  1693. _LIBCPP_INLINE_VISIBILITY
  1694. bool is_socket(error_code& __ec) const noexcept {
  1695. return __get_ft(&__ec) == file_type::socket;
  1696. }
  1697. _LIBCPP_INLINE_VISIBILITY
  1698. bool is_symlink() const { return __get_sym_ft() == file_type::symlink; }
  1699. _LIBCPP_INLINE_VISIBILITY
  1700. bool is_symlink(error_code& __ec) const noexcept {
  1701. return __get_sym_ft(&__ec) == file_type::symlink;
  1702. }
  1703. _LIBCPP_INLINE_VISIBILITY
  1704. uintmax_t file_size() const { return __get_size(); }
  1705. _LIBCPP_INLINE_VISIBILITY
  1706. uintmax_t file_size(error_code& __ec) const noexcept {
  1707. return __get_size(&__ec);
  1708. }
  1709. _LIBCPP_INLINE_VISIBILITY
  1710. uintmax_t hard_link_count() const { return __get_nlink(); }
  1711. _LIBCPP_INLINE_VISIBILITY
  1712. uintmax_t hard_link_count(error_code& __ec) const noexcept {
  1713. return __get_nlink(&__ec);
  1714. }
  1715. _LIBCPP_INLINE_VISIBILITY
  1716. file_time_type last_write_time() const { return __get_write_time(); }
  1717. _LIBCPP_INLINE_VISIBILITY
  1718. file_time_type last_write_time(error_code& __ec) const noexcept {
  1719. return __get_write_time(&__ec);
  1720. }
  1721. _LIBCPP_INLINE_VISIBILITY
  1722. file_status status() const { return __get_status(); }
  1723. _LIBCPP_INLINE_VISIBILITY
  1724. file_status status(error_code& __ec) const noexcept {
  1725. return __get_status(&__ec);
  1726. }
  1727. _LIBCPP_INLINE_VISIBILITY
  1728. file_status symlink_status() const { return __get_symlink_status(); }
  1729. _LIBCPP_INLINE_VISIBILITY
  1730. file_status symlink_status(error_code& __ec) const noexcept {
  1731. return __get_symlink_status(&__ec);
  1732. }
  1733. _LIBCPP_INLINE_VISIBILITY
  1734. bool operator<(directory_entry const& __rhs) const noexcept {
  1735. return __p_ < __rhs.__p_;
  1736. }
  1737. _LIBCPP_INLINE_VISIBILITY
  1738. bool operator==(directory_entry const& __rhs) const noexcept {
  1739. return __p_ == __rhs.__p_;
  1740. }
  1741. _LIBCPP_INLINE_VISIBILITY
  1742. bool operator!=(directory_entry const& __rhs) const noexcept {
  1743. return __p_ != __rhs.__p_;
  1744. }
  1745. _LIBCPP_INLINE_VISIBILITY
  1746. bool operator<=(directory_entry const& __rhs) const noexcept {
  1747. return __p_ <= __rhs.__p_;
  1748. }
  1749. _LIBCPP_INLINE_VISIBILITY
  1750. bool operator>(directory_entry const& __rhs) const noexcept {
  1751. return __p_ > __rhs.__p_;
  1752. }
  1753. _LIBCPP_INLINE_VISIBILITY
  1754. bool operator>=(directory_entry const& __rhs) const noexcept {
  1755. return __p_ >= __rhs.__p_;
  1756. }
  1757. private:
  1758. friend class directory_iterator;
  1759. friend class recursive_directory_iterator;
  1760. friend class __dir_stream;
  1761. enum _CacheType : unsigned char {
  1762. _Empty,
  1763. _IterSymlink,
  1764. _IterNonSymlink,
  1765. _RefreshSymlink,
  1766. _RefreshSymlinkUnresolved,
  1767. _RefreshNonSymlink
  1768. };
  1769. struct __cached_data {
  1770. uintmax_t __size_;
  1771. uintmax_t __nlink_;
  1772. file_time_type __write_time_;
  1773. perms __sym_perms_;
  1774. perms __non_sym_perms_;
  1775. file_type __type_;
  1776. _CacheType __cache_type_;
  1777. _LIBCPP_INLINE_VISIBILITY
  1778. __cached_data() noexcept { __reset(); }
  1779. _LIBCPP_INLINE_VISIBILITY
  1780. void __reset() {
  1781. __cache_type_ = _Empty;
  1782. __type_ = file_type::none;
  1783. __sym_perms_ = __non_sym_perms_ = perms::unknown;
  1784. __size_ = __nlink_ = uintmax_t(-1);
  1785. __write_time_ = file_time_type::min();
  1786. }
  1787. };
  1788. _LIBCPP_INLINE_VISIBILITY
  1789. static __cached_data __create_iter_result(file_type __ft) {
  1790. __cached_data __data;
  1791. __data.__type_ = __ft;
  1792. __data.__cache_type_ = [&]() {
  1793. switch (__ft) {
  1794. case file_type::none:
  1795. return _Empty;
  1796. case file_type::symlink:
  1797. return _IterSymlink;
  1798. default:
  1799. return _IterNonSymlink;
  1800. }
  1801. }();
  1802. return __data;
  1803. }
  1804. _LIBCPP_INLINE_VISIBILITY
  1805. void __assign_iter_entry(_Path&& __p, __cached_data __dt) {
  1806. __p_ = std::move(__p);
  1807. __data_ = __dt;
  1808. }
  1809. _LIBCPP_FUNC_VIS
  1810. error_code __do_refresh() noexcept;
  1811. _LIBCPP_INLINE_VISIBILITY
  1812. static bool __is_dne_error(error_code const& __ec) {
  1813. if (!__ec)
  1814. return true;
  1815. switch (static_cast<errc>(__ec.value())) {
  1816. case errc::no_such_file_or_directory:
  1817. case errc::not_a_directory:
  1818. return true;
  1819. default:
  1820. return false;
  1821. }
  1822. }
  1823. _LIBCPP_INLINE_VISIBILITY
  1824. void __handle_error(const char* __msg, error_code* __dest_ec,
  1825. error_code const& __ec, bool __allow_dne = false) const {
  1826. if (__dest_ec) {
  1827. *__dest_ec = __ec;
  1828. return;
  1829. }
  1830. if (__ec && (!__allow_dne || !__is_dne_error(__ec)))
  1831. __throw_filesystem_error(__msg, __p_, __ec);
  1832. }
  1833. _LIBCPP_INLINE_VISIBILITY
  1834. void __refresh(error_code* __ec = nullptr) {
  1835. __handle_error("in directory_entry::refresh", __ec, __do_refresh(),
  1836. /*allow_dne*/ true);
  1837. }
  1838. _LIBCPP_INLINE_VISIBILITY
  1839. file_type __get_sym_ft(error_code* __ec = nullptr) const {
  1840. switch (__data_.__cache_type_) {
  1841. case _Empty:
  1842. return __symlink_status(__p_, __ec).type();
  1843. case _IterSymlink:
  1844. case _RefreshSymlink:
  1845. case _RefreshSymlinkUnresolved:
  1846. if (__ec)
  1847. __ec->clear();
  1848. return file_type::symlink;
  1849. case _IterNonSymlink:
  1850. case _RefreshNonSymlink:
  1851. file_status __st(__data_.__type_);
  1852. if (__ec && !_VSTD_FS::exists(__st))
  1853. *__ec = make_error_code(errc::no_such_file_or_directory);
  1854. else if (__ec)
  1855. __ec->clear();
  1856. return __data_.__type_;
  1857. }
  1858. _LIBCPP_UNREACHABLE();
  1859. }
  1860. _LIBCPP_INLINE_VISIBILITY
  1861. file_type __get_ft(error_code* __ec = nullptr) const {
  1862. switch (__data_.__cache_type_) {
  1863. case _Empty:
  1864. case _IterSymlink:
  1865. case _RefreshSymlinkUnresolved:
  1866. return __status(__p_, __ec).type();
  1867. case _IterNonSymlink:
  1868. case _RefreshNonSymlink:
  1869. case _RefreshSymlink: {
  1870. file_status __st(__data_.__type_);
  1871. if (__ec && !_VSTD_FS::exists(__st))
  1872. *__ec = make_error_code(errc::no_such_file_or_directory);
  1873. else if (__ec)
  1874. __ec->clear();
  1875. return __data_.__type_;
  1876. }
  1877. }
  1878. _LIBCPP_UNREACHABLE();
  1879. }
  1880. _LIBCPP_INLINE_VISIBILITY
  1881. file_status __get_status(error_code* __ec = nullptr) const {
  1882. switch (__data_.__cache_type_) {
  1883. case _Empty:
  1884. case _IterNonSymlink:
  1885. case _IterSymlink:
  1886. case _RefreshSymlinkUnresolved:
  1887. return __status(__p_, __ec);
  1888. case _RefreshNonSymlink:
  1889. case _RefreshSymlink:
  1890. return file_status(__get_ft(__ec), __data_.__non_sym_perms_);
  1891. }
  1892. _LIBCPP_UNREACHABLE();
  1893. }
  1894. _LIBCPP_INLINE_VISIBILITY
  1895. file_status __get_symlink_status(error_code* __ec = nullptr) const {
  1896. switch (__data_.__cache_type_) {
  1897. case _Empty:
  1898. case _IterNonSymlink:
  1899. case _IterSymlink:
  1900. return __symlink_status(__p_, __ec);
  1901. case _RefreshNonSymlink:
  1902. return file_status(__get_sym_ft(__ec), __data_.__non_sym_perms_);
  1903. case _RefreshSymlink:
  1904. case _RefreshSymlinkUnresolved:
  1905. return file_status(__get_sym_ft(__ec), __data_.__sym_perms_);
  1906. }
  1907. _LIBCPP_UNREACHABLE();
  1908. }
  1909. _LIBCPP_INLINE_VISIBILITY
  1910. uintmax_t __get_size(error_code* __ec = nullptr) const {
  1911. switch (__data_.__cache_type_) {
  1912. case _Empty:
  1913. case _IterNonSymlink:
  1914. case _IterSymlink:
  1915. case _RefreshSymlinkUnresolved:
  1916. return _VSTD_FS::__file_size(__p_, __ec);
  1917. case _RefreshSymlink:
  1918. case _RefreshNonSymlink: {
  1919. error_code __m_ec;
  1920. file_status __st(__get_ft(&__m_ec));
  1921. __handle_error("in directory_entry::file_size", __ec, __m_ec);
  1922. if (_VSTD_FS::exists(__st) && !_VSTD_FS::is_regular_file(__st)) {
  1923. errc __err_kind = _VSTD_FS::is_directory(__st) ? errc::is_a_directory
  1924. : errc::not_supported;
  1925. __handle_error("in directory_entry::file_size", __ec,
  1926. make_error_code(__err_kind));
  1927. }
  1928. return __data_.__size_;
  1929. }
  1930. }
  1931. _LIBCPP_UNREACHABLE();
  1932. }
  1933. _LIBCPP_INLINE_VISIBILITY
  1934. uintmax_t __get_nlink(error_code* __ec = nullptr) const {
  1935. switch (__data_.__cache_type_) {
  1936. case _Empty:
  1937. case _IterNonSymlink:
  1938. case _IterSymlink:
  1939. case _RefreshSymlinkUnresolved:
  1940. return _VSTD_FS::__hard_link_count(__p_, __ec);
  1941. case _RefreshSymlink:
  1942. case _RefreshNonSymlink: {
  1943. error_code __m_ec;
  1944. (void)__get_ft(&__m_ec);
  1945. __handle_error("in directory_entry::hard_link_count", __ec, __m_ec);
  1946. return __data_.__nlink_;
  1947. }
  1948. }
  1949. _LIBCPP_UNREACHABLE();
  1950. }
  1951. _LIBCPP_INLINE_VISIBILITY
  1952. file_time_type __get_write_time(error_code* __ec = nullptr) const {
  1953. switch (__data_.__cache_type_) {
  1954. case _Empty:
  1955. case _IterNonSymlink:
  1956. case _IterSymlink:
  1957. case _RefreshSymlinkUnresolved:
  1958. return _VSTD_FS::__last_write_time(__p_, __ec);
  1959. case _RefreshSymlink:
  1960. case _RefreshNonSymlink: {
  1961. error_code __m_ec;
  1962. file_status __st(__get_ft(&__m_ec));
  1963. __handle_error("in directory_entry::last_write_time", __ec, __m_ec);
  1964. if (_VSTD_FS::exists(__st) &&
  1965. __data_.__write_time_ == file_time_type::min())
  1966. __handle_error("in directory_entry::last_write_time", __ec,
  1967. make_error_code(errc::value_too_large));
  1968. return __data_.__write_time_;
  1969. }
  1970. }
  1971. _LIBCPP_UNREACHABLE();
  1972. }
  1973. private:
  1974. _Path __p_;
  1975. __cached_data __data_;
  1976. };
  1977. class __dir_element_proxy {
  1978. public:
  1979. inline _LIBCPP_INLINE_VISIBILITY directory_entry operator*() {
  1980. return _VSTD::move(__elem_);
  1981. }
  1982. private:
  1983. friend class directory_iterator;
  1984. friend class recursive_directory_iterator;
  1985. explicit __dir_element_proxy(directory_entry const& __e) : __elem_(__e) {}
  1986. __dir_element_proxy(__dir_element_proxy&& __o)
  1987. : __elem_(_VSTD::move(__o.__elem_)) {}
  1988. directory_entry __elem_;
  1989. };
  1990. class directory_iterator {
  1991. public:
  1992. typedef directory_entry value_type;
  1993. typedef ptrdiff_t difference_type;
  1994. typedef value_type const* pointer;
  1995. typedef value_type const& reference;
  1996. typedef input_iterator_tag iterator_category;
  1997. public:
  1998. //ctor & dtor
  1999. directory_iterator() noexcept {}
  2000. explicit directory_iterator(const path& __p)
  2001. : directory_iterator(__p, nullptr) {}
  2002. directory_iterator(const path& __p, directory_options __opts)
  2003. : directory_iterator(__p, nullptr, __opts) {}
  2004. directory_iterator(const path& __p, error_code& __ec)
  2005. : directory_iterator(__p, &__ec) {}
  2006. directory_iterator(const path& __p, directory_options __opts,
  2007. error_code& __ec)
  2008. : directory_iterator(__p, &__ec, __opts) {}
  2009. directory_iterator(const directory_iterator&) = default;
  2010. directory_iterator(directory_iterator&&) = default;
  2011. directory_iterator& operator=(const directory_iterator&) = default;
  2012. directory_iterator& operator=(directory_iterator&& __o) noexcept {
  2013. // non-default implementation provided to support self-move assign.
  2014. if (this != &__o) {
  2015. __imp_ = _VSTD::move(__o.__imp_);
  2016. }
  2017. return *this;
  2018. }
  2019. ~directory_iterator() = default;
  2020. const directory_entry& operator*() const {
  2021. _LIBCPP_ASSERT(__imp_, "The end iterator cannot be dereferenced");
  2022. return __dereference();
  2023. }
  2024. const directory_entry* operator->() const { return &**this; }
  2025. directory_iterator& operator++() { return __increment(); }
  2026. __dir_element_proxy operator++(int) {
  2027. __dir_element_proxy __p(**this);
  2028. __increment();
  2029. return __p;
  2030. }
  2031. directory_iterator& increment(error_code& __ec) { return __increment(&__ec); }
  2032. private:
  2033. inline _LIBCPP_INLINE_VISIBILITY friend bool
  2034. operator==(const directory_iterator& __lhs,
  2035. const directory_iterator& __rhs) noexcept;
  2036. // construct the dir_stream
  2037. _LIBCPP_FUNC_VIS
  2038. directory_iterator(const path&, error_code*,
  2039. directory_options = directory_options::none);
  2040. _LIBCPP_FUNC_VIS
  2041. directory_iterator& __increment(error_code* __ec = nullptr);
  2042. _LIBCPP_FUNC_VIS
  2043. const directory_entry& __dereference() const;
  2044. private:
  2045. shared_ptr<__dir_stream> __imp_;
  2046. };
  2047. inline _LIBCPP_INLINE_VISIBILITY bool
  2048. operator==(const directory_iterator& __lhs,
  2049. const directory_iterator& __rhs) noexcept {
  2050. return __lhs.__imp_ == __rhs.__imp_;
  2051. }
  2052. inline _LIBCPP_INLINE_VISIBILITY bool
  2053. operator!=(const directory_iterator& __lhs,
  2054. const directory_iterator& __rhs) noexcept {
  2055. return !(__lhs == __rhs);
  2056. }
  2057. // enable directory_iterator range-based for statements
  2058. inline _LIBCPP_INLINE_VISIBILITY directory_iterator
  2059. begin(directory_iterator __iter) noexcept {
  2060. return __iter;
  2061. }
  2062. inline _LIBCPP_INLINE_VISIBILITY directory_iterator
  2063. end(const directory_iterator&) noexcept {
  2064. return directory_iterator();
  2065. }
  2066. class recursive_directory_iterator {
  2067. public:
  2068. using value_type = directory_entry;
  2069. using difference_type = std::ptrdiff_t;
  2070. using pointer = directory_entry const*;
  2071. using reference = directory_entry const&;
  2072. using iterator_category = std::input_iterator_tag;
  2073. public:
  2074. // constructors and destructor
  2075. _LIBCPP_INLINE_VISIBILITY
  2076. recursive_directory_iterator() noexcept : __rec_(false) {}
  2077. _LIBCPP_INLINE_VISIBILITY
  2078. explicit recursive_directory_iterator(
  2079. const path& __p, directory_options __xoptions = directory_options::none)
  2080. : recursive_directory_iterator(__p, __xoptions, nullptr) {}
  2081. _LIBCPP_INLINE_VISIBILITY
  2082. recursive_directory_iterator(const path& __p, directory_options __xoptions,
  2083. error_code& __ec)
  2084. : recursive_directory_iterator(__p, __xoptions, &__ec) {}
  2085. _LIBCPP_INLINE_VISIBILITY
  2086. recursive_directory_iterator(const path& __p, error_code& __ec)
  2087. : recursive_directory_iterator(__p, directory_options::none, &__ec) {}
  2088. recursive_directory_iterator(const recursive_directory_iterator&) = default;
  2089. recursive_directory_iterator(recursive_directory_iterator&&) = default;
  2090. recursive_directory_iterator&
  2091. operator=(const recursive_directory_iterator&) = default;
  2092. _LIBCPP_INLINE_VISIBILITY
  2093. recursive_directory_iterator&
  2094. operator=(recursive_directory_iterator&& __o) noexcept {
  2095. // non-default implementation provided to support self-move assign.
  2096. if (this != &__o) {
  2097. __imp_ = _VSTD::move(__o.__imp_);
  2098. __rec_ = __o.__rec_;
  2099. }
  2100. return *this;
  2101. }
  2102. ~recursive_directory_iterator() = default;
  2103. _LIBCPP_INLINE_VISIBILITY
  2104. const directory_entry& operator*() const { return __dereference(); }
  2105. _LIBCPP_INLINE_VISIBILITY
  2106. const directory_entry* operator->() const { return &__dereference(); }
  2107. recursive_directory_iterator& operator++() { return __increment(); }
  2108. _LIBCPP_INLINE_VISIBILITY
  2109. __dir_element_proxy operator++(int) {
  2110. __dir_element_proxy __p(**this);
  2111. __increment();
  2112. return __p;
  2113. }
  2114. _LIBCPP_INLINE_VISIBILITY
  2115. recursive_directory_iterator& increment(error_code& __ec) {
  2116. return __increment(&__ec);
  2117. }
  2118. _LIBCPP_FUNC_VIS directory_options options() const;
  2119. _LIBCPP_FUNC_VIS int depth() const;
  2120. _LIBCPP_INLINE_VISIBILITY
  2121. void pop() { __pop(); }
  2122. _LIBCPP_INLINE_VISIBILITY
  2123. void pop(error_code& __ec) { __pop(&__ec); }
  2124. _LIBCPP_INLINE_VISIBILITY
  2125. bool recursion_pending() const { return __rec_; }
  2126. _LIBCPP_INLINE_VISIBILITY
  2127. void disable_recursion_pending() { __rec_ = false; }
  2128. private:
  2129. _LIBCPP_FUNC_VIS
  2130. recursive_directory_iterator(const path& __p, directory_options __opt,
  2131. error_code* __ec);
  2132. _LIBCPP_FUNC_VIS
  2133. const directory_entry& __dereference() const;
  2134. _LIBCPP_FUNC_VIS
  2135. bool __try_recursion(error_code* __ec);
  2136. _LIBCPP_FUNC_VIS
  2137. void __advance(error_code* __ec = nullptr);
  2138. _LIBCPP_FUNC_VIS
  2139. recursive_directory_iterator& __increment(error_code* __ec = nullptr);
  2140. _LIBCPP_FUNC_VIS
  2141. void __pop(error_code* __ec = nullptr);
  2142. inline _LIBCPP_INLINE_VISIBILITY friend bool
  2143. operator==(const recursive_directory_iterator&,
  2144. const recursive_directory_iterator&) noexcept;
  2145. struct _LIBCPP_HIDDEN __shared_imp;
  2146. shared_ptr<__shared_imp> __imp_;
  2147. bool __rec_;
  2148. }; // class recursive_directory_iterator
  2149. inline _LIBCPP_INLINE_VISIBILITY bool
  2150. operator==(const recursive_directory_iterator& __lhs,
  2151. const recursive_directory_iterator& __rhs) noexcept {
  2152. return __lhs.__imp_ == __rhs.__imp_;
  2153. }
  2154. _LIBCPP_INLINE_VISIBILITY
  2155. inline bool operator!=(const recursive_directory_iterator& __lhs,
  2156. const recursive_directory_iterator& __rhs) noexcept {
  2157. return !(__lhs == __rhs);
  2158. }
  2159. // enable recursive_directory_iterator range-based for statements
  2160. inline _LIBCPP_INLINE_VISIBILITY recursive_directory_iterator
  2161. begin(recursive_directory_iterator __iter) noexcept {
  2162. return __iter;
  2163. }
  2164. inline _LIBCPP_INLINE_VISIBILITY recursive_directory_iterator
  2165. end(const recursive_directory_iterator&) noexcept {
  2166. return recursive_directory_iterator();
  2167. }
  2168. _LIBCPP_AVAILABILITY_FILESYSTEM_POP
  2169. _LIBCPP_END_NAMESPACE_FILESYSTEM
  2170. #endif // !_LIBCPP_CXX03_LANG
  2171. _LIBCPP_POP_MACROS
  2172. #endif // _LIBCPP_FILESYSTEM