operations.hpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  1. // boost/filesystem/operations.hpp ---------------------------------------------------//
  2. // Copyright Beman Dawes 2002-2009
  3. // Copyright Jan Langer 2002
  4. // Copyright Dietmar Kuehl 2001
  5. // Copyright Vladimir Prus 2002
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // See http://www.boost.org/LICENSE_1_0.txt
  8. // Library home page: http://www.boost.org/libs/filesystem
  9. //--------------------------------------------------------------------------------------//
  10. #ifndef BOOST_FILESYSTEM3_OPERATIONS_HPP
  11. #define BOOST_FILESYSTEM3_OPERATIONS_HPP
  12. #include <boost/config.hpp>
  13. # if defined( BOOST_NO_STD_WSTRING )
  14. # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support
  15. # endif
  16. #include <boost/filesystem/config.hpp>
  17. #include <boost/filesystem/path.hpp>
  18. #include <boost/core/scoped_enum.hpp>
  19. #include <boost/detail/bitmask.hpp>
  20. #include <boost/system/error_code.hpp>
  21. #include <boost/system/system_error.hpp>
  22. #include <boost/smart_ptr/intrusive_ptr.hpp>
  23. #include <boost/smart_ptr/intrusive_ref_counter.hpp>
  24. #include <boost/type_traits/is_same.hpp>
  25. #include <boost/cstdint.hpp>
  26. #include <boost/assert.hpp>
  27. #include <string>
  28. #include <utility> // std::move
  29. #include <ctime>
  30. #include <vector>
  31. #include <stack>
  32. #include <new> // std::nothrow, std::bad_alloc
  33. #ifdef BOOST_WINDOWS_API
  34. # include <fstream>
  35. #endif
  36. #include <boost/config/abi_prefix.hpp> // must be the last #include
  37. //--------------------------------------------------------------------------------------//
  38. namespace boost
  39. {
  40. namespace filesystem
  41. {
  42. //--------------------------------------------------------------------------------------//
  43. // //
  44. // class filesystem_error //
  45. // //
  46. //--------------------------------------------------------------------------------------//
  47. class BOOST_SYMBOL_VISIBLE filesystem_error :
  48. public system::system_error
  49. {
  50. // see http://www.boost.org/more/error_handling.html for design rationale
  51. // all functions are inline to avoid issues with crossing dll boundaries
  52. // functions previously throw() are now BOOST_NOEXCEPT_OR_NOTHROW
  53. // functions previously without throw() are now BOOST_NOEXCEPT
  54. public:
  55. // compiler generates copy constructor and copy assignment
  56. filesystem_error(
  57. const std::string & what_arg, system::error_code ec) BOOST_NOEXCEPT :
  58. system::system_error(ec, what_arg)
  59. {
  60. try
  61. {
  62. m_imp_ptr.reset(new impl());
  63. }
  64. catch (...) { m_imp_ptr.reset(); }
  65. }
  66. filesystem_error(
  67. const std::string & what_arg, const path& path1_arg,
  68. system::error_code ec) BOOST_NOEXCEPT :
  69. system::system_error(ec, what_arg)
  70. {
  71. try
  72. {
  73. m_imp_ptr.reset(new impl(path1_arg));
  74. }
  75. catch (...) { m_imp_ptr.reset(); }
  76. }
  77. filesystem_error(
  78. const std::string & what_arg, const path& path1_arg,
  79. const path& path2_arg, system::error_code ec) BOOST_NOEXCEPT :
  80. system::system_error(ec, what_arg)
  81. {
  82. try
  83. {
  84. m_imp_ptr.reset(new impl(path1_arg, path2_arg));
  85. }
  86. catch (...) { m_imp_ptr.reset(); }
  87. }
  88. ~filesystem_error() BOOST_NOEXCEPT_OR_NOTHROW {}
  89. const path& path1() const BOOST_NOEXCEPT
  90. {
  91. static const path empty_path;
  92. return m_imp_ptr.get() ? m_imp_ptr->m_path1 : empty_path;
  93. }
  94. const path& path2() const BOOST_NOEXCEPT
  95. {
  96. static const path empty_path;
  97. return m_imp_ptr.get() ? m_imp_ptr->m_path2 : empty_path;
  98. }
  99. const char* what() const BOOST_NOEXCEPT_OR_NOTHROW
  100. {
  101. if (!m_imp_ptr.get())
  102. return system::system_error::what();
  103. try
  104. {
  105. if (m_imp_ptr->m_what.empty())
  106. {
  107. m_imp_ptr->m_what = system::system_error::what();
  108. if (!m_imp_ptr->m_path1.empty())
  109. {
  110. m_imp_ptr->m_what += ": \"";
  111. m_imp_ptr->m_what += m_imp_ptr->m_path1.string();
  112. m_imp_ptr->m_what += "\"";
  113. }
  114. if (!m_imp_ptr->m_path2.empty())
  115. {
  116. m_imp_ptr->m_what += ", \"";
  117. m_imp_ptr->m_what += m_imp_ptr->m_path2.string();
  118. m_imp_ptr->m_what += "\"";
  119. }
  120. }
  121. return m_imp_ptr->m_what.c_str();
  122. }
  123. catch (...)
  124. {
  125. return system::system_error::what();
  126. }
  127. }
  128. private:
  129. struct impl :
  130. public boost::intrusive_ref_counter< impl >
  131. {
  132. path m_path1; // may be empty()
  133. path m_path2; // may be empty()
  134. std::string m_what; // not built until needed
  135. BOOST_DEFAULTED_FUNCTION(impl(), {})
  136. explicit impl(path const& path1) : m_path1(path1) {}
  137. impl(path const& path1, path const& path2) : m_path1(path1), m_path2(path2) {}
  138. };
  139. boost::intrusive_ptr< impl > m_imp_ptr;
  140. };
  141. //--------------------------------------------------------------------------------------//
  142. // file_type //
  143. //--------------------------------------------------------------------------------------//
  144. enum file_type
  145. {
  146. status_error,
  147. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  148. status_unknown = status_error,
  149. # endif
  150. file_not_found,
  151. regular_file,
  152. directory_file,
  153. // the following may not apply to some operating systems or file systems
  154. symlink_file,
  155. block_file,
  156. character_file,
  157. fifo_file,
  158. socket_file,
  159. reparse_file, // Windows: FILE_ATTRIBUTE_REPARSE_POINT that is not a symlink
  160. type_unknown, // file does exist, but isn't one of the above types or
  161. // we don't have strong enough permission to find its type
  162. _detail_directory_symlink // internal use only; never exposed to users
  163. };
  164. //--------------------------------------------------------------------------------------//
  165. // perms //
  166. //--------------------------------------------------------------------------------------//
  167. enum perms
  168. {
  169. no_perms = 0, // file_not_found is no_perms rather than perms_not_known
  170. // POSIX equivalent macros given in comments.
  171. // Values are from POSIX and are given in octal per the POSIX standard.
  172. // permission bits
  173. owner_read = 0400, // S_IRUSR, Read permission, owner
  174. owner_write = 0200, // S_IWUSR, Write permission, owner
  175. owner_exe = 0100, // S_IXUSR, Execute/search permission, owner
  176. owner_all = 0700, // S_IRWXU, Read, write, execute/search by owner
  177. group_read = 040, // S_IRGRP, Read permission, group
  178. group_write = 020, // S_IWGRP, Write permission, group
  179. group_exe = 010, // S_IXGRP, Execute/search permission, group
  180. group_all = 070, // S_IRWXG, Read, write, execute/search by group
  181. others_read = 04, // S_IROTH, Read permission, others
  182. others_write = 02, // S_IWOTH, Write permission, others
  183. others_exe = 01, // S_IXOTH, Execute/search permission, others
  184. others_all = 07, // S_IRWXO, Read, write, execute/search by others
  185. all_all = 0777, // owner_all|group_all|others_all
  186. // other POSIX bits
  187. set_uid_on_exe = 04000, // S_ISUID, Set-user-ID on execution
  188. set_gid_on_exe = 02000, // S_ISGID, Set-group-ID on execution
  189. sticky_bit = 01000, // S_ISVTX,
  190. // (POSIX XSI) On directories, restricted deletion flag
  191. // (V7) 'sticky bit': save swapped text even after use
  192. // (SunOS) On non-directories: don't cache this file
  193. // (SVID-v4.2) On directories: restricted deletion flag
  194. // Also see http://en.wikipedia.org/wiki/Sticky_bit
  195. perms_mask = 07777, // all_all|set_uid_on_exe|set_gid_on_exe|sticky_bit
  196. perms_not_known = 0xFFFF, // present when directory_entry cache not loaded
  197. // options for permissions() function
  198. add_perms = 0x1000, // adds the given permission bits to the current bits
  199. remove_perms = 0x2000, // removes the given permission bits from the current bits;
  200. // choose add_perms or remove_perms, not both; if neither add_perms
  201. // nor remove_perms is given, replace the current bits with
  202. // the given bits.
  203. symlink_perms = 0x4000, // on POSIX, don't resolve symlinks; implied on Windows
  204. // BOOST_BITMASK op~ casts to int32_least_t, producing invalid enum values
  205. _detail_extend_perms_32_1 = 0x7fffffff,
  206. _detail_extend_perms_32_2 = -0x7fffffff-1
  207. };
  208. BOOST_BITMASK(perms)
  209. //--------------------------------------------------------------------------------------//
  210. // file_status //
  211. //--------------------------------------------------------------------------------------//
  212. class file_status
  213. {
  214. public:
  215. BOOST_CONSTEXPR file_status() BOOST_NOEXCEPT :
  216. m_value(status_error), m_perms(perms_not_known)
  217. {
  218. }
  219. explicit BOOST_CONSTEXPR file_status(file_type v) BOOST_NOEXCEPT :
  220. m_value(v), m_perms(perms_not_known)
  221. {
  222. }
  223. BOOST_CONSTEXPR file_status(file_type v, perms prms) BOOST_NOEXCEPT :
  224. m_value(v), m_perms(prms)
  225. {
  226. }
  227. // As of October 2015 the interaction between noexcept and =default is so troublesome
  228. // for VC++, GCC, and probably other compilers, that =default is not used with noexcept
  229. // functions. GCC is not even consistent for the same release on different platforms.
  230. BOOST_CONSTEXPR file_status(const file_status& rhs) BOOST_NOEXCEPT :
  231. m_value(rhs.m_value), m_perms(rhs.m_perms)
  232. {
  233. }
  234. BOOST_CXX14_CONSTEXPR file_status& operator=(const file_status& rhs) BOOST_NOEXCEPT
  235. {
  236. m_value = rhs.m_value;
  237. m_perms = rhs.m_perms;
  238. return *this;
  239. }
  240. # if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  241. // Note: std::move is not constexpr in C++11, that's why we're not using it here
  242. BOOST_CONSTEXPR file_status(file_status&& rhs) BOOST_NOEXCEPT :
  243. m_value(static_cast< file_type&& >(rhs.m_value)), m_perms(static_cast< enum perms&& >(rhs.m_perms))
  244. {
  245. }
  246. BOOST_CXX14_CONSTEXPR file_status& operator=(file_status&& rhs) BOOST_NOEXCEPT
  247. {
  248. m_value = std::move(rhs.m_value);
  249. m_perms = std::move(rhs.m_perms);
  250. return *this;
  251. }
  252. # endif
  253. // observers
  254. BOOST_CONSTEXPR file_type type() const BOOST_NOEXCEPT { return m_value; }
  255. BOOST_CONSTEXPR perms permissions() const BOOST_NOEXCEPT { return m_perms; }
  256. // modifiers
  257. BOOST_CXX14_CONSTEXPR void type(file_type v) BOOST_NOEXCEPT { m_value = v; }
  258. BOOST_CXX14_CONSTEXPR void permissions(perms prms) BOOST_NOEXCEPT { m_perms = prms; }
  259. BOOST_CONSTEXPR bool operator==(const file_status& rhs) const BOOST_NOEXCEPT
  260. {
  261. return type() == rhs.type() &&
  262. permissions() == rhs.permissions();
  263. }
  264. BOOST_CONSTEXPR bool operator!=(const file_status& rhs) const BOOST_NOEXCEPT
  265. {
  266. return !(*this == rhs);
  267. }
  268. private:
  269. file_type m_value;
  270. enum perms m_perms;
  271. };
  272. inline BOOST_CONSTEXPR bool type_present(file_status f) BOOST_NOEXCEPT
  273. { return f.type() != status_error; }
  274. inline BOOST_CONSTEXPR bool permissions_present(file_status f) BOOST_NOEXCEPT
  275. {return f.permissions() != perms_not_known;}
  276. inline BOOST_CONSTEXPR bool status_known(file_status f) BOOST_NOEXCEPT
  277. { return filesystem::type_present(f) && filesystem::permissions_present(f); }
  278. inline BOOST_CONSTEXPR bool exists(file_status f) BOOST_NOEXCEPT
  279. { return f.type() != status_error
  280. && f.type() != file_not_found; }
  281. inline BOOST_CONSTEXPR bool is_regular_file(file_status f) BOOST_NOEXCEPT
  282. { return f.type() == regular_file; }
  283. inline BOOST_CONSTEXPR bool is_directory(file_status f) BOOST_NOEXCEPT
  284. { return f.type() == directory_file; }
  285. inline BOOST_CONSTEXPR bool is_symlink(file_status f) BOOST_NOEXCEPT
  286. { return f.type() == symlink_file; }
  287. inline BOOST_CONSTEXPR bool is_other(file_status f) BOOST_NOEXCEPT
  288. { return filesystem::exists(f) && !filesystem::is_regular_file(f)
  289. && !filesystem::is_directory(f) && !filesystem::is_symlink(f); }
  290. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  291. inline bool is_regular(file_status f) BOOST_NOEXCEPT { return f.type() == regular_file; }
  292. # endif
  293. struct space_info
  294. {
  295. // all values are byte counts
  296. boost::uintmax_t capacity;
  297. boost::uintmax_t free; // <= capacity
  298. boost::uintmax_t available; // <= free
  299. };
  300. BOOST_SCOPED_ENUM_DECLARE_BEGIN(copy_option)
  301. {none=0, fail_if_exists = none, overwrite_if_exists}
  302. BOOST_SCOPED_ENUM_DECLARE_END(copy_option)
  303. //--------------------------------------------------------------------------------------//
  304. // implementation details //
  305. //--------------------------------------------------------------------------------------//
  306. namespace detail
  307. {
  308. // We cannot pass a BOOST_SCOPED_ENUM to a compled function because it will result
  309. // in an undefined reference if the library is compled with -std=c++0x but the use
  310. // is compiled in C++03 mode, or vice versa. See tickets 6124, 6779, 10038.
  311. enum copy_option {none=0, fail_if_exists = none, overwrite_if_exists};
  312. BOOST_FILESYSTEM_DECL
  313. file_status status(const path&p, system::error_code* ec=0);
  314. BOOST_FILESYSTEM_DECL
  315. file_status symlink_status(const path& p, system::error_code* ec=0);
  316. BOOST_FILESYSTEM_DECL
  317. bool is_empty(const path& p, system::error_code* ec=0);
  318. BOOST_FILESYSTEM_DECL
  319. path initial_path(system::error_code* ec=0);
  320. BOOST_FILESYSTEM_DECL
  321. path canonical(const path& p, const path& base, system::error_code* ec=0);
  322. BOOST_FILESYSTEM_DECL
  323. void copy(const path& from, const path& to, system::error_code* ec=0);
  324. BOOST_FILESYSTEM_DECL
  325. void copy_directory(const path& from, const path& to, system::error_code* ec=0);
  326. BOOST_FILESYSTEM_DECL
  327. void copy_file(const path& from, const path& to, // See ticket #2925
  328. detail::copy_option option, system::error_code* ec=0);
  329. BOOST_FILESYSTEM_DECL
  330. void copy_symlink(const path& existing_symlink, const path& new_symlink, system::error_code* ec=0);
  331. BOOST_FILESYSTEM_DECL
  332. bool create_directories(const path& p, system::error_code* ec=0);
  333. BOOST_FILESYSTEM_DECL
  334. bool create_directory(const path& p, system::error_code* ec=0);
  335. BOOST_FILESYSTEM_DECL
  336. void create_directory_symlink(const path& to, const path& from,
  337. system::error_code* ec=0);
  338. BOOST_FILESYSTEM_DECL
  339. void create_hard_link(const path& to, const path& from, system::error_code* ec=0);
  340. BOOST_FILESYSTEM_DECL
  341. void create_symlink(const path& to, const path& from, system::error_code* ec=0);
  342. BOOST_FILESYSTEM_DECL
  343. path current_path(system::error_code* ec=0);
  344. BOOST_FILESYSTEM_DECL
  345. void current_path(const path& p, system::error_code* ec=0);
  346. BOOST_FILESYSTEM_DECL
  347. bool equivalent(const path& p1, const path& p2, system::error_code* ec=0);
  348. BOOST_FILESYSTEM_DECL
  349. boost::uintmax_t file_size(const path& p, system::error_code* ec=0);
  350. BOOST_FILESYSTEM_DECL
  351. boost::uintmax_t hard_link_count(const path& p, system::error_code* ec=0);
  352. BOOST_FILESYSTEM_DECL
  353. std::time_t last_write_time(const path& p, system::error_code* ec=0);
  354. BOOST_FILESYSTEM_DECL
  355. void last_write_time(const path& p, const std::time_t new_time,
  356. system::error_code* ec=0);
  357. BOOST_FILESYSTEM_DECL
  358. void permissions(const path& p, perms prms, system::error_code* ec=0);
  359. BOOST_FILESYSTEM_DECL
  360. path read_symlink(const path& p, system::error_code* ec=0);
  361. BOOST_FILESYSTEM_DECL
  362. path relative(const path& p, const path& base, system::error_code* ec = 0);
  363. BOOST_FILESYSTEM_DECL
  364. bool remove(const path& p, system::error_code* ec=0);
  365. BOOST_FILESYSTEM_DECL
  366. boost::uintmax_t remove_all(const path& p, system::error_code* ec=0);
  367. BOOST_FILESYSTEM_DECL
  368. void rename(const path& old_p, const path& new_p, system::error_code* ec=0);
  369. BOOST_FILESYSTEM_DECL
  370. void resize_file(const path& p, uintmax_t size, system::error_code* ec=0);
  371. BOOST_FILESYSTEM_DECL
  372. space_info space(const path& p, system::error_code* ec=0);
  373. BOOST_FILESYSTEM_DECL
  374. path system_complete(const path& p, system::error_code* ec=0);
  375. BOOST_FILESYSTEM_DECL
  376. path temp_directory_path(system::error_code* ec=0);
  377. BOOST_FILESYSTEM_DECL
  378. path unique_path(const path& p, system::error_code* ec=0);
  379. BOOST_FILESYSTEM_DECL
  380. path weakly_canonical(const path& p, system::error_code* ec = 0);
  381. } // namespace detail
  382. //--------------------------------------------------------------------------------------//
  383. // //
  384. // status query functions //
  385. // //
  386. //--------------------------------------------------------------------------------------//
  387. inline
  388. file_status status(const path& p) {return detail::status(p);}
  389. inline
  390. file_status status(const path& p, system::error_code& ec)
  391. {return detail::status(p, &ec);}
  392. inline
  393. file_status symlink_status(const path& p) {return detail::symlink_status(p);}
  394. inline
  395. file_status symlink_status(const path& p, system::error_code& ec)
  396. {return detail::symlink_status(p, &ec);}
  397. inline
  398. bool exists(const path& p) {return exists(detail::status(p));}
  399. inline
  400. bool exists(const path& p, system::error_code& ec)
  401. {return exists(detail::status(p, &ec));}
  402. inline
  403. bool is_directory(const path& p) {return is_directory(detail::status(p));}
  404. inline
  405. bool is_directory(const path& p, system::error_code& ec)
  406. {return is_directory(detail::status(p, &ec));}
  407. inline
  408. bool is_regular_file(const path& p) {return is_regular_file(detail::status(p));}
  409. inline
  410. bool is_regular_file(const path& p, system::error_code& ec)
  411. {return is_regular_file(detail::status(p, &ec));}
  412. inline
  413. bool is_other(const path& p) {return is_other(detail::status(p));}
  414. inline
  415. bool is_other(const path& p, system::error_code& ec)
  416. {return is_other(detail::status(p, &ec));}
  417. inline
  418. bool is_symlink(const path& p) {return is_symlink(detail::symlink_status(p));}
  419. inline
  420. bool is_symlink(const path& p, system::error_code& ec)
  421. {return is_symlink(detail::symlink_status(p, &ec));}
  422. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  423. inline
  424. bool is_regular(const path& p) {return is_regular(detail::status(p));}
  425. inline
  426. bool is_regular(const path& p, system::error_code& ec)
  427. {return is_regular(detail::status(p, &ec));}
  428. # endif
  429. inline
  430. bool is_empty(const path& p) {return detail::is_empty(p);}
  431. inline
  432. bool is_empty(const path& p, system::error_code& ec)
  433. {return detail::is_empty(p, &ec);}
  434. //--------------------------------------------------------------------------------------//
  435. // //
  436. // operational functions //
  437. // in alphabetical order, unless otherwise noted //
  438. // //
  439. //--------------------------------------------------------------------------------------//
  440. // forward declarations
  441. path current_path(); // fwd declaration
  442. path initial_path();
  443. BOOST_FILESYSTEM_DECL
  444. path absolute(const path& p, const path& base=current_path());
  445. // If base.is_absolute(), throws nothing. Thus no need for ec argument
  446. inline
  447. path canonical(const path& p, const path& base=current_path())
  448. {return detail::canonical(p, base);}
  449. inline
  450. path canonical(const path& p, system::error_code& ec)
  451. {return detail::canonical(p, current_path(), &ec);}
  452. inline
  453. path canonical(const path& p, const path& base, system::error_code& ec)
  454. {return detail::canonical(p, base, &ec);}
  455. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  456. inline
  457. path complete(const path& p)
  458. {
  459. return absolute(p, initial_path());
  460. }
  461. inline
  462. path complete(const path& p, const path& base)
  463. {
  464. return absolute(p, base);
  465. }
  466. # endif
  467. inline
  468. void copy(const path& from, const path& to) {detail::copy(from, to);}
  469. inline
  470. void copy(const path& from, const path& to, system::error_code& ec) BOOST_NOEXCEPT
  471. {detail::copy(from, to, &ec);}
  472. inline
  473. void copy_directory(const path& from, const path& to)
  474. {detail::copy_directory(from, to);}
  475. inline
  476. void copy_directory(const path& from, const path& to, system::error_code& ec) BOOST_NOEXCEPT
  477. {detail::copy_directory(from, to, &ec);}
  478. inline
  479. void copy_file(const path& from, const path& to, // See ticket #2925
  480. BOOST_SCOPED_ENUM_NATIVE(copy_option) option)
  481. {
  482. detail::copy_file(from, to, static_cast<detail::copy_option>(option));
  483. }
  484. inline
  485. void copy_file(const path& from, const path& to)
  486. {
  487. detail::copy_file(from, to, detail::fail_if_exists);
  488. }
  489. inline
  490. void copy_file(const path& from, const path& to, // See ticket #2925
  491. BOOST_SCOPED_ENUM_NATIVE(copy_option) option, system::error_code& ec) BOOST_NOEXCEPT
  492. {
  493. detail::copy_file(from, to, static_cast<detail::copy_option>(option), &ec);
  494. }
  495. inline
  496. void copy_file(const path& from, const path& to, system::error_code& ec) BOOST_NOEXCEPT
  497. {
  498. detail::copy_file(from, to, detail::fail_if_exists, &ec);
  499. }
  500. inline
  501. void copy_symlink(const path& existing_symlink,
  502. const path& new_symlink) {detail::copy_symlink(existing_symlink, new_symlink);}
  503. inline
  504. void copy_symlink(const path& existing_symlink, const path& new_symlink,
  505. system::error_code& ec) BOOST_NOEXCEPT
  506. {detail::copy_symlink(existing_symlink, new_symlink, &ec);}
  507. inline
  508. bool create_directories(const path& p) {return detail::create_directories(p);}
  509. inline
  510. bool create_directories(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  511. {return detail::create_directories(p, &ec);}
  512. inline
  513. bool create_directory(const path& p) {return detail::create_directory(p);}
  514. inline
  515. bool create_directory(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  516. {return detail::create_directory(p, &ec);}
  517. inline
  518. void create_directory_symlink(const path& to, const path& from)
  519. {detail::create_directory_symlink(to, from);}
  520. inline
  521. void create_directory_symlink(const path& to, const path& from, system::error_code& ec) BOOST_NOEXCEPT
  522. {detail::create_directory_symlink(to, from, &ec);}
  523. inline
  524. void create_hard_link(const path& to, const path& new_hard_link) {detail::create_hard_link(to, new_hard_link);}
  525. inline
  526. void create_hard_link(const path& to, const path& new_hard_link, system::error_code& ec) BOOST_NOEXCEPT
  527. {detail::create_hard_link(to, new_hard_link, &ec);}
  528. inline
  529. void create_symlink(const path& to, const path& new_symlink) {detail::create_symlink(to, new_symlink);}
  530. inline
  531. void create_symlink(const path& to, const path& new_symlink, system::error_code& ec) BOOST_NOEXCEPT
  532. {detail::create_symlink(to, new_symlink, &ec);}
  533. inline
  534. path current_path() {return detail::current_path();}
  535. inline
  536. path current_path(system::error_code& ec) BOOST_NOEXCEPT {return detail::current_path(&ec);}
  537. inline
  538. void current_path(const path& p) {detail::current_path(p);}
  539. inline
  540. void current_path(const path& p, system::error_code& ec) BOOST_NOEXCEPT {detail::current_path(p, &ec);}
  541. inline
  542. bool equivalent(const path& p1, const path& p2) {return detail::equivalent(p1, p2);}
  543. inline
  544. bool equivalent(const path& p1, const path& p2, system::error_code& ec) BOOST_NOEXCEPT
  545. {return detail::equivalent(p1, p2, &ec);}
  546. inline
  547. boost::uintmax_t file_size(const path& p) {return detail::file_size(p);}
  548. inline
  549. boost::uintmax_t file_size(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  550. {return detail::file_size(p, &ec);}
  551. inline
  552. boost::uintmax_t hard_link_count(const path& p) {return detail::hard_link_count(p);}
  553. inline
  554. boost::uintmax_t hard_link_count(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  555. {return detail::hard_link_count(p, &ec);}
  556. inline
  557. path initial_path() {return detail::initial_path();}
  558. inline
  559. path initial_path(system::error_code& ec) {return detail::initial_path(&ec);}
  560. template <class Path>
  561. path initial_path() {return initial_path();}
  562. template <class Path>
  563. path initial_path(system::error_code& ec) {return detail::initial_path(&ec);}
  564. inline
  565. std::time_t last_write_time(const path& p) {return detail::last_write_time(p);}
  566. inline
  567. std::time_t last_write_time(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  568. {return detail::last_write_time(p, &ec);}
  569. inline
  570. void last_write_time(const path& p, const std::time_t new_time)
  571. {detail::last_write_time(p, new_time);}
  572. inline
  573. void last_write_time(const path& p, const std::time_t new_time,
  574. system::error_code& ec) BOOST_NOEXCEPT
  575. {detail::last_write_time(p, new_time, &ec);}
  576. inline
  577. void permissions(const path& p, perms prms)
  578. {detail::permissions(p, prms);}
  579. inline
  580. void permissions(const path& p, perms prms, system::error_code& ec) BOOST_NOEXCEPT
  581. {detail::permissions(p, prms, &ec);}
  582. inline
  583. path read_symlink(const path& p) {return detail::read_symlink(p);}
  584. inline
  585. path read_symlink(const path& p, system::error_code& ec)
  586. {return detail::read_symlink(p, &ec);}
  587. inline
  588. // For standardization, if the committee doesn't like "remove", consider "eliminate"
  589. bool remove(const path& p) {return detail::remove(p);}
  590. inline
  591. bool remove(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  592. {return detail::remove(p, &ec);}
  593. inline
  594. boost::uintmax_t remove_all(const path& p) {return detail::remove_all(p);}
  595. inline
  596. boost::uintmax_t remove_all(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  597. {return detail::remove_all(p, &ec);}
  598. inline
  599. void rename(const path& old_p, const path& new_p) {detail::rename(old_p, new_p);}
  600. inline
  601. void rename(const path& old_p, const path& new_p, system::error_code& ec) BOOST_NOEXCEPT
  602. {detail::rename(old_p, new_p, &ec);}
  603. inline // name suggested by Scott McMurray
  604. void resize_file(const path& p, uintmax_t size) {detail::resize_file(p, size);}
  605. inline
  606. void resize_file(const path& p, uintmax_t size, system::error_code& ec) BOOST_NOEXCEPT
  607. {detail::resize_file(p, size, &ec);}
  608. inline
  609. path relative(const path& p, const path& base=current_path())
  610. {return detail::relative(p, base);}
  611. inline
  612. path relative(const path& p, system::error_code& ec)
  613. {return detail::relative(p, current_path(), &ec);}
  614. inline
  615. path relative(const path& p, const path& base, system::error_code& ec)
  616. {return detail::relative(p, base, &ec);}
  617. inline
  618. space_info space(const path& p) {return detail::space(p);}
  619. inline
  620. space_info space(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  621. {return detail::space(p, &ec);}
  622. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  623. inline bool symbolic_link_exists(const path& p)
  624. { return is_symlink(filesystem::symlink_status(p)); }
  625. # endif
  626. inline
  627. path system_complete(const path& p) {return detail::system_complete(p);}
  628. inline
  629. path system_complete(const path& p, system::error_code& ec)
  630. {return detail::system_complete(p, &ec);}
  631. inline
  632. path temp_directory_path() {return detail::temp_directory_path();}
  633. inline
  634. path temp_directory_path(system::error_code& ec)
  635. {return detail::temp_directory_path(&ec);}
  636. inline
  637. path unique_path(const path& p="%%%%-%%%%-%%%%-%%%%")
  638. {return detail::unique_path(p);}
  639. inline
  640. path unique_path(const path& p, system::error_code& ec)
  641. {return detail::unique_path(p, &ec);}
  642. inline
  643. path weakly_canonical(const path& p) {return detail::weakly_canonical(p);}
  644. inline
  645. path weakly_canonical(const path& p, system::error_code& ec)
  646. {return detail::weakly_canonical(p, &ec);}
  647. //--------------------------------------------------------------------------------------//
  648. // //
  649. // directory_entry //
  650. // //
  651. //--------------------------------------------------------------------------------------//
  652. // GCC has a problem with a member function named path within a namespace or
  653. // sub-namespace that also has a class named path. The workaround is to always
  654. // fully qualify the name path when it refers to the class name.
  655. class directory_entry
  656. {
  657. public:
  658. typedef boost::filesystem::path::value_type value_type; // enables class path ctor taking directory_entry
  659. directory_entry() BOOST_NOEXCEPT {}
  660. explicit directory_entry(const boost::filesystem::path& p) :
  661. m_path(p), m_status(file_status()), m_symlink_status(file_status())
  662. {
  663. }
  664. directory_entry(const boost::filesystem::path& p,
  665. file_status st, file_status symlink_st = file_status()) :
  666. m_path(p), m_status(st), m_symlink_status(symlink_st)
  667. {
  668. }
  669. directory_entry(const directory_entry& rhs) :
  670. m_path(rhs.m_path), m_status(rhs.m_status), m_symlink_status(rhs.m_symlink_status)
  671. {
  672. }
  673. directory_entry& operator=(const directory_entry& rhs)
  674. {
  675. m_path = rhs.m_path;
  676. m_status = rhs.m_status;
  677. m_symlink_status = rhs.m_symlink_status;
  678. return *this;
  679. }
  680. // As of October 2015 the interaction between noexcept and =default is so troublesome
  681. // for VC++, GCC, and probably other compilers, that =default is not used with noexcept
  682. // functions. GCC is not even consistent for the same release on different platforms.
  683. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  684. directory_entry(directory_entry&& rhs) BOOST_NOEXCEPT :
  685. m_path(std::move(rhs.m_path)), m_status(std::move(rhs.m_status)), m_symlink_status(std::move(rhs.m_symlink_status))
  686. {
  687. }
  688. directory_entry& operator=(directory_entry&& rhs) BOOST_NOEXCEPT
  689. {
  690. m_path = std::move(rhs.m_path);
  691. m_status = std::move(rhs.m_status);
  692. m_symlink_status = std::move(rhs.m_symlink_status);
  693. return *this;
  694. }
  695. #endif
  696. void assign(const boost::filesystem::path& p,
  697. file_status st = file_status(), file_status symlink_st = file_status())
  698. {
  699. m_path = p;
  700. m_status = st;
  701. m_symlink_status = symlink_st;
  702. }
  703. void replace_filename(const boost::filesystem::path& p,
  704. file_status st = file_status(), file_status symlink_st = file_status())
  705. {
  706. m_path.remove_filename();
  707. m_path /= p;
  708. m_status = st;
  709. m_symlink_status = symlink_st;
  710. }
  711. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  712. void replace_leaf(const boost::filesystem::path& p,
  713. file_status st, file_status symlink_st)
  714. { replace_filename(p, st, symlink_st); }
  715. # endif
  716. const boost::filesystem::path& path() const BOOST_NOEXCEPT {return m_path;}
  717. operator const boost::filesystem::path&() const BOOST_NOEXCEPT
  718. {return m_path;}
  719. file_status status() const {return m_get_status();}
  720. file_status status(system::error_code& ec) const BOOST_NOEXCEPT
  721. {return m_get_status(&ec); }
  722. file_status symlink_status() const {return m_get_symlink_status();}
  723. file_status symlink_status(system::error_code& ec) const BOOST_NOEXCEPT
  724. {return m_get_symlink_status(&ec); }
  725. bool operator==(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path == rhs.m_path; }
  726. bool operator!=(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path != rhs.m_path;}
  727. bool operator< (const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path < rhs.m_path;}
  728. bool operator<=(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path <= rhs.m_path;}
  729. bool operator> (const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path > rhs.m_path;}
  730. bool operator>=(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path >= rhs.m_path;}
  731. private:
  732. BOOST_FILESYSTEM_DECL file_status m_get_status(system::error_code* ec=0) const;
  733. BOOST_FILESYSTEM_DECL file_status m_get_symlink_status(system::error_code* ec=0) const;
  734. private:
  735. boost::filesystem::path m_path;
  736. mutable file_status m_status; // stat()-like
  737. mutable file_status m_symlink_status; // lstat()-like
  738. }; // directory_entry
  739. //--------------------------------------------------------------------------------------//
  740. // //
  741. // directory_entry overloads //
  742. // //
  743. //--------------------------------------------------------------------------------------//
  744. // Without these functions, calling (for example) 'is_directory' with a 'directory_entry' results in:
  745. // - a conversion to 'path' using 'operator const boost::filesystem::path&()',
  746. // - then a call to 'is_directory(const path& p)' which recomputes the status with 'detail::status(p)'.
  747. //
  748. // These functions avoid a costly recomputation of the status if one calls 'is_directory(e)' instead of 'is_directory(e.status)'
  749. inline file_status status (const directory_entry& e) BOOST_NOEXCEPT { return e.status(); }
  750. inline bool type_present (const directory_entry& e) BOOST_NOEXCEPT { return filesystem::type_present(e.status()); }
  751. inline bool status_known (const directory_entry& e) BOOST_NOEXCEPT { return filesystem::status_known(e.status()); }
  752. inline bool exists (const directory_entry& e) BOOST_NOEXCEPT { return filesystem::exists(e.status()); }
  753. inline bool is_regular_file(const directory_entry& e) BOOST_NOEXCEPT { return filesystem::is_regular_file(e.status()); }
  754. inline bool is_directory (const directory_entry& e) BOOST_NOEXCEPT { return filesystem::is_directory(e.status()); }
  755. inline bool is_symlink (const directory_entry& e) BOOST_NOEXCEPT { return filesystem::is_symlink(e.status()); }
  756. inline bool is_other (const directory_entry& e) BOOST_NOEXCEPT { return filesystem::is_other(e.status()); }
  757. #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  758. inline bool is_regular (const directory_entry& e) BOOST_NOEXCEPT { return filesystem::is_regular(e.status()); }
  759. #endif
  760. //--------------------------------------------------------------------------------------//
  761. // //
  762. // directory_iterator helpers //
  763. // //
  764. //--------------------------------------------------------------------------------------//
  765. class directory_iterator;
  766. namespace detail
  767. {
  768. BOOST_FILESYSTEM_DECL
  769. system::error_code dir_itr_close(// never throws()
  770. void *& handle
  771. # if defined(BOOST_POSIX_API)
  772. , void *& buffer
  773. # endif
  774. );
  775. struct dir_itr_imp :
  776. public boost::intrusive_ref_counter< dir_itr_imp >
  777. {
  778. directory_entry dir_entry;
  779. void* handle;
  780. # ifdef BOOST_POSIX_API
  781. void* buffer; // see dir_itr_increment implementation
  782. # endif
  783. dir_itr_imp() BOOST_NOEXCEPT : handle(0)
  784. # ifdef BOOST_POSIX_API
  785. , buffer(0)
  786. # endif
  787. {}
  788. ~dir_itr_imp() BOOST_NOEXCEPT
  789. {
  790. dir_itr_close(handle
  791. # if defined(BOOST_POSIX_API)
  792. , buffer
  793. # endif
  794. );
  795. }
  796. };
  797. // see path::iterator: comment below
  798. BOOST_FILESYSTEM_DECL void directory_iterator_construct(directory_iterator& it,
  799. const path& p, system::error_code* ec);
  800. BOOST_FILESYSTEM_DECL void directory_iterator_increment(directory_iterator& it,
  801. system::error_code* ec);
  802. } // namespace detail
  803. //--------------------------------------------------------------------------------------//
  804. // //
  805. // directory_iterator //
  806. // //
  807. //--------------------------------------------------------------------------------------//
  808. class directory_iterator :
  809. public boost::iterator_facade<
  810. directory_iterator,
  811. directory_entry,
  812. boost::single_pass_traversal_tag
  813. >
  814. {
  815. public:
  816. directory_iterator() BOOST_NOEXCEPT {} // creates the "end" iterator
  817. // iterator_facade derived classes don't seem to like implementations in
  818. // separate translation unit dll's, so forward to detail functions
  819. explicit directory_iterator(const path& p) :
  820. m_imp(new detail::dir_itr_imp())
  821. {
  822. detail::directory_iterator_construct(*this, p, 0);
  823. }
  824. directory_iterator(const path& p, system::error_code& ec) BOOST_NOEXCEPT :
  825. m_imp(new (std::nothrow) detail::dir_itr_imp())
  826. {
  827. if (BOOST_UNLIKELY(!m_imp))
  828. {
  829. ec = make_error_code(system::errc::not_enough_memory);
  830. return;
  831. }
  832. detail::directory_iterator_construct(*this, p, &ec);
  833. }
  834. BOOST_DEFAULTED_FUNCTION(directory_iterator(directory_iterator const& that), : m_imp(that.m_imp) {})
  835. BOOST_DEFAULTED_FUNCTION(directory_iterator& operator= (directory_iterator const& that), { m_imp = that.m_imp; return *this; })
  836. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  837. directory_iterator(directory_iterator&& that) BOOST_NOEXCEPT :
  838. m_imp(std::move(that.m_imp))
  839. {
  840. }
  841. directory_iterator& operator= (directory_iterator&& that) BOOST_NOEXCEPT
  842. {
  843. m_imp = std::move(that.m_imp);
  844. return *this;
  845. }
  846. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  847. directory_iterator& increment(system::error_code& ec) BOOST_NOEXCEPT
  848. {
  849. detail::directory_iterator_increment(*this, &ec);
  850. return *this;
  851. }
  852. private:
  853. friend struct detail::dir_itr_imp;
  854. friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_construct(directory_iterator& it,
  855. const path& p, system::error_code* ec);
  856. friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_increment(directory_iterator& it,
  857. system::error_code* ec);
  858. // intrusive_ptr provides the shallow-copy semantics required for single pass iterators
  859. // (i.e. InputIterators). The end iterator is indicated by !m_imp || !m_imp->handle
  860. boost::intrusive_ptr< detail::dir_itr_imp > m_imp;
  861. friend class boost::iterator_core_access;
  862. boost::iterator_facade<
  863. directory_iterator,
  864. directory_entry,
  865. boost::single_pass_traversal_tag
  866. >::reference dereference() const
  867. {
  868. BOOST_ASSERT_MSG(m_imp.get(), "attempt to dereference end iterator");
  869. return m_imp->dir_entry;
  870. }
  871. void increment() { detail::directory_iterator_increment(*this, 0); }
  872. bool equal(const directory_iterator& rhs) const
  873. {
  874. return m_imp == rhs.m_imp
  875. || (!m_imp && rhs.m_imp && !rhs.m_imp->handle)
  876. || (!rhs.m_imp && m_imp && !m_imp->handle);
  877. }
  878. }; // directory_iterator
  879. // enable directory_iterator C++11 range-based for statement use --------------------//
  880. // begin() and end() are only used by a range-based for statement in the context of
  881. // auto - thus the top-level const is stripped - so returning const is harmless and
  882. // emphasizes begin() is just a pass through.
  883. inline
  884. const directory_iterator& begin(const directory_iterator& iter) BOOST_NOEXCEPT
  885. {return iter;}
  886. inline
  887. directory_iterator end(const directory_iterator&) BOOST_NOEXCEPT
  888. {return directory_iterator();}
  889. // enable C++14 generic accessors for range const iterators
  890. inline
  891. const directory_iterator& cbegin(const directory_iterator& iter) BOOST_NOEXCEPT
  892. {return iter;}
  893. inline
  894. directory_iterator cend(const directory_iterator&) BOOST_NOEXCEPT
  895. {return directory_iterator();}
  896. // enable directory_iterator BOOST_FOREACH -----------------------------------------//
  897. inline
  898. directory_iterator& range_begin(directory_iterator& iter) BOOST_NOEXCEPT
  899. {return iter;}
  900. inline
  901. directory_iterator range_begin(const directory_iterator& iter) BOOST_NOEXCEPT
  902. {return iter;}
  903. inline
  904. directory_iterator range_end(directory_iterator&) BOOST_NOEXCEPT
  905. {return directory_iterator();}
  906. inline
  907. directory_iterator range_end(const directory_iterator&) BOOST_NOEXCEPT
  908. {return directory_iterator();}
  909. } // namespace filesystem
  910. // namespace boost template specializations
  911. template<typename C, typename Enabler>
  912. struct range_mutable_iterator;
  913. template<>
  914. struct range_mutable_iterator<boost::filesystem::directory_iterator, void>
  915. { typedef boost::filesystem::directory_iterator type; };
  916. template<typename C, typename Enabler>
  917. struct range_const_iterator;
  918. template<>
  919. struct range_const_iterator<boost::filesystem::directory_iterator, void>
  920. { typedef boost::filesystem::directory_iterator type; };
  921. namespace filesystem
  922. {
  923. //--------------------------------------------------------------------------------------//
  924. // //
  925. // recursive_directory_iterator helpers //
  926. // //
  927. //--------------------------------------------------------------------------------------//
  928. BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(symlink_option, unsigned int)
  929. {
  930. none,
  931. no_recurse = none, // don't follow directory symlinks (default behavior)
  932. recurse, // follow directory symlinks
  933. _detail_no_push = recurse << 1 // internal use only
  934. }
  935. BOOST_SCOPED_ENUM_DECLARE_END(symlink_option)
  936. BOOST_BITMASK(BOOST_SCOPED_ENUM_NATIVE(symlink_option))
  937. namespace detail
  938. {
  939. struct recur_dir_itr_imp :
  940. public boost::intrusive_ref_counter< recur_dir_itr_imp >
  941. {
  942. typedef directory_iterator element_type;
  943. std::stack< element_type, std::vector< element_type > > m_stack;
  944. int m_level;
  945. // symlink_option values, declared as unsigned int for ABI compatibility
  946. unsigned int m_options;
  947. recur_dir_itr_imp() BOOST_NOEXCEPT : m_level(0), m_options(static_cast< unsigned int >(symlink_option::none)) {}
  948. explicit recur_dir_itr_imp(BOOST_SCOPED_ENUM_NATIVE(symlink_option) opt) BOOST_NOEXCEPT : m_level(0), m_options(static_cast< unsigned int >(opt)) {}
  949. // ec == 0 means throw on error
  950. //
  951. // Invariant: On return, the top of the iterator stack is the next valid (possibly
  952. // end) iterator, regardless of whether or not an error is reported, and regardless of
  953. // whether any error is reported by exception or error code. In other words, progress
  954. // is always made so a loop on the iterator will always eventually terminate
  955. // regardless of errors.
  956. BOOST_FILESYSTEM_DECL void increment(system::error_code* ec);
  957. // Returns: true if push occurs, otherwise false. Always returns false on error.
  958. BOOST_FILESYSTEM_DECL bool push_directory(system::error_code& ec) BOOST_NOEXCEPT;
  959. // ec == 0 means throw on error
  960. BOOST_FILESYSTEM_DECL void pop(system::error_code* ec);
  961. };
  962. } // namespace detail
  963. //--------------------------------------------------------------------------------------//
  964. // //
  965. // recursive_directory_iterator //
  966. // //
  967. //--------------------------------------------------------------------------------------//
  968. class recursive_directory_iterator :
  969. public boost::iterator_facade<
  970. recursive_directory_iterator,
  971. directory_entry,
  972. boost::single_pass_traversal_tag
  973. >
  974. {
  975. public:
  976. recursive_directory_iterator() BOOST_NOEXCEPT {} // creates the "end" iterator
  977. explicit recursive_directory_iterator(const path& dir_path) : // throws if !exists()
  978. m_imp(new detail::recur_dir_itr_imp())
  979. {
  980. m_imp->m_stack.push(directory_iterator(dir_path));
  981. if (m_imp->m_stack.top() == directory_iterator())
  982. { m_imp.reset(); }
  983. }
  984. recursive_directory_iterator(const path& dir_path,
  985. BOOST_SCOPED_ENUM_NATIVE(symlink_option) opt) : // throws if !exists()
  986. m_imp(new detail::recur_dir_itr_imp(opt))
  987. {
  988. m_imp->m_stack.push(directory_iterator(dir_path));
  989. if (m_imp->m_stack.top() == directory_iterator())
  990. { m_imp.reset (); }
  991. }
  992. recursive_directory_iterator(const path& dir_path,
  993. BOOST_SCOPED_ENUM_NATIVE(symlink_option) opt,
  994. system::error_code & ec) BOOST_NOEXCEPT :
  995. m_imp(new (std::nothrow) detail::recur_dir_itr_imp(opt))
  996. {
  997. if (BOOST_UNLIKELY(!m_imp))
  998. {
  999. ec = make_error_code(system::errc::not_enough_memory);
  1000. return;
  1001. }
  1002. directory_iterator it(dir_path, ec);
  1003. if (ec || it == directory_iterator())
  1004. {
  1005. m_imp.reset();
  1006. return;
  1007. }
  1008. try
  1009. {
  1010. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  1011. m_imp->m_stack.push(std::move(it));
  1012. #else
  1013. m_imp->m_stack.push(it);
  1014. #endif
  1015. }
  1016. catch (std::bad_alloc&)
  1017. {
  1018. ec = make_error_code(system::errc::not_enough_memory);
  1019. m_imp.reset();
  1020. }
  1021. }
  1022. recursive_directory_iterator(const path& dir_path,
  1023. system::error_code & ec) BOOST_NOEXCEPT :
  1024. m_imp(new (std::nothrow) detail::recur_dir_itr_imp())
  1025. {
  1026. if (BOOST_UNLIKELY(!m_imp))
  1027. {
  1028. ec = make_error_code(system::errc::not_enough_memory);
  1029. return;
  1030. }
  1031. directory_iterator it(dir_path, ec);
  1032. if (ec || it == directory_iterator())
  1033. {
  1034. m_imp.reset();
  1035. return;
  1036. }
  1037. try
  1038. {
  1039. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  1040. m_imp->m_stack.push(std::move(it));
  1041. #else
  1042. m_imp->m_stack.push(it);
  1043. #endif
  1044. }
  1045. catch (std::bad_alloc&)
  1046. {
  1047. ec = make_error_code(system::errc::not_enough_memory);
  1048. m_imp.reset();
  1049. }
  1050. }
  1051. BOOST_DEFAULTED_FUNCTION(recursive_directory_iterator(recursive_directory_iterator const& that), : m_imp(that.m_imp) {})
  1052. BOOST_DEFAULTED_FUNCTION(recursive_directory_iterator& operator= (recursive_directory_iterator const& that), { m_imp = that.m_imp; return *this; })
  1053. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  1054. recursive_directory_iterator(recursive_directory_iterator&& that) BOOST_NOEXCEPT :
  1055. m_imp(std::move(that.m_imp))
  1056. {
  1057. }
  1058. recursive_directory_iterator& operator= (recursive_directory_iterator&& that) BOOST_NOEXCEPT
  1059. {
  1060. m_imp = std::move(that.m_imp);
  1061. return *this;
  1062. }
  1063. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  1064. recursive_directory_iterator& increment(system::error_code& ec) BOOST_NOEXCEPT
  1065. {
  1066. BOOST_ASSERT_MSG(m_imp.get(),
  1067. "increment() on end recursive_directory_iterator");
  1068. m_imp->increment(&ec);
  1069. if (m_imp->m_stack.empty())
  1070. m_imp.reset(); // done, so make end iterator
  1071. return *this;
  1072. }
  1073. int depth() const BOOST_NOEXCEPT
  1074. {
  1075. BOOST_ASSERT_MSG(m_imp.get(),
  1076. "depth() on end recursive_directory_iterator");
  1077. return m_imp->m_level;
  1078. }
  1079. int level() const BOOST_NOEXCEPT { return depth(); }
  1080. bool recursion_pending() const BOOST_NOEXCEPT
  1081. {
  1082. BOOST_ASSERT_MSG(m_imp.get(),
  1083. "is_no_push_requested() on end recursive_directory_iterator");
  1084. return (m_imp->m_options & static_cast< unsigned int >(symlink_option::_detail_no_push))
  1085. == static_cast< unsigned int >(symlink_option::_detail_no_push);
  1086. }
  1087. bool no_push_pending() const BOOST_NOEXCEPT { return recursion_pending(); }
  1088. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  1089. bool no_push_request() const BOOST_NOEXCEPT { return no_push_pending(); }
  1090. # endif
  1091. void pop()
  1092. {
  1093. BOOST_ASSERT_MSG(m_imp.get(),
  1094. "pop() on end recursive_directory_iterator");
  1095. m_imp->pop(0);
  1096. if (m_imp->m_stack.empty()) m_imp.reset(); // done, so make end iterator
  1097. }
  1098. void pop(system::error_code& ec) BOOST_NOEXCEPT
  1099. {
  1100. BOOST_ASSERT_MSG(m_imp.get(),
  1101. "pop() on end recursive_directory_iterator");
  1102. m_imp->pop(&ec);
  1103. if (m_imp->m_stack.empty()) m_imp.reset(); // done, so make end iterator
  1104. }
  1105. void disable_recursion_pending(bool value=true) BOOST_NOEXCEPT
  1106. {
  1107. BOOST_ASSERT_MSG(m_imp.get(),
  1108. "no_push() on end recursive_directory_iterator");
  1109. if (value)
  1110. m_imp->m_options |= static_cast< unsigned int >(symlink_option::_detail_no_push);
  1111. else
  1112. m_imp->m_options &= ~static_cast< unsigned int >(symlink_option::_detail_no_push);
  1113. }
  1114. void no_push(bool value=true) BOOST_NOEXCEPT { disable_recursion_pending(value); }
  1115. file_status status() const
  1116. {
  1117. BOOST_ASSERT_MSG(m_imp.get(),
  1118. "status() on end recursive_directory_iterator");
  1119. return m_imp->m_stack.top()->status();
  1120. }
  1121. file_status symlink_status() const
  1122. {
  1123. BOOST_ASSERT_MSG(m_imp.get(),
  1124. "symlink_status() on end recursive_directory_iterator");
  1125. return m_imp->m_stack.top()->symlink_status();
  1126. }
  1127. private:
  1128. // intrusive_ptr provides the shallow-copy semantics required for single pass iterators
  1129. // (i.e. InputIterators).
  1130. // The end iterator is indicated by !m_imp || m_imp->m_stack.empty()
  1131. boost::intrusive_ptr< detail::recur_dir_itr_imp > m_imp;
  1132. friend class boost::iterator_core_access;
  1133. boost::iterator_facade<
  1134. recursive_directory_iterator,
  1135. directory_entry,
  1136. boost::single_pass_traversal_tag >::reference
  1137. dereference() const
  1138. {
  1139. BOOST_ASSERT_MSG(m_imp.get(),
  1140. "dereference of end recursive_directory_iterator");
  1141. return *m_imp->m_stack.top();
  1142. }
  1143. void increment()
  1144. {
  1145. BOOST_ASSERT_MSG(m_imp.get(),
  1146. "increment of end recursive_directory_iterator");
  1147. m_imp->increment(0);
  1148. if (m_imp->m_stack.empty())
  1149. m_imp.reset(); // done, so make end iterator
  1150. }
  1151. bool equal(const recursive_directory_iterator& rhs) const
  1152. {
  1153. return m_imp == rhs.m_imp
  1154. || (!m_imp && rhs.m_imp && rhs.m_imp->m_stack.empty())
  1155. || (!rhs.m_imp && m_imp && m_imp->m_stack.empty()) ;
  1156. }
  1157. }; // recursive directory iterator
  1158. // enable recursive directory iterator C++11 range-base for statement use ----------//
  1159. // begin() and end() are only used by a range-based for statement in the context of
  1160. // auto - thus the top-level const is stripped - so returning const is harmless and
  1161. // emphasizes begin() is just a pass through.
  1162. inline
  1163. const recursive_directory_iterator&
  1164. begin(const recursive_directory_iterator& iter) BOOST_NOEXCEPT
  1165. {return iter;}
  1166. inline
  1167. recursive_directory_iterator end(const recursive_directory_iterator&) BOOST_NOEXCEPT
  1168. {return recursive_directory_iterator();}
  1169. // enable C++14 generic accessors for range const iterators
  1170. inline
  1171. const recursive_directory_iterator& cbegin(const recursive_directory_iterator& iter) BOOST_NOEXCEPT
  1172. {return iter;}
  1173. inline
  1174. recursive_directory_iterator cend(const recursive_directory_iterator&) BOOST_NOEXCEPT
  1175. {return recursive_directory_iterator();}
  1176. // enable recursive directory iterator BOOST_FOREACH -------------------------------//
  1177. inline
  1178. recursive_directory_iterator&
  1179. range_begin(recursive_directory_iterator& iter) BOOST_NOEXCEPT
  1180. {return iter;}
  1181. inline
  1182. recursive_directory_iterator
  1183. range_begin(const recursive_directory_iterator& iter) BOOST_NOEXCEPT
  1184. {return iter;}
  1185. inline
  1186. recursive_directory_iterator range_end(recursive_directory_iterator&) BOOST_NOEXCEPT
  1187. {return recursive_directory_iterator();}
  1188. inline
  1189. recursive_directory_iterator range_end(const recursive_directory_iterator&) BOOST_NOEXCEPT
  1190. {return recursive_directory_iterator();}
  1191. } // namespace filesystem
  1192. // namespace boost template specializations
  1193. template<>
  1194. struct range_mutable_iterator<boost::filesystem::recursive_directory_iterator, void>
  1195. { typedef boost::filesystem::recursive_directory_iterator type; };
  1196. template<>
  1197. struct range_const_iterator<boost::filesystem::recursive_directory_iterator, void>
  1198. { typedef boost::filesystem::recursive_directory_iterator type; };
  1199. namespace filesystem
  1200. {
  1201. # if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
  1202. typedef recursive_directory_iterator wrecursive_directory_iterator;
  1203. # endif
  1204. // test helper -----------------------------------------------------------------------//
  1205. // Not part of the documented interface since false positives are possible;
  1206. // there is no law that says that an OS that has large stat.st_size
  1207. // actually supports large file sizes.
  1208. namespace detail
  1209. {
  1210. BOOST_FILESYSTEM_DECL bool possible_large_file_size_support();
  1211. }
  1212. } // namespace filesystem
  1213. } // namespace boost
  1214. #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
  1215. #endif // BOOST_FILESYSTEM3_OPERATIONS_HPP