directory.hpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. // boost/filesystem/directory.hpp ---------------------------------------------------//
  2. // Copyright Beman Dawes 2002-2009
  3. // Copyright Jan Langer 2002
  4. // Copyright Dietmar Kuehl 2001
  5. // Copyright Vladimir Prus 2002
  6. // Copyright Andrey Semashev 2019, 2022
  7. // Distributed under the Boost Software License, Version 1.0.
  8. // See http://www.boost.org/LICENSE_1_0.txt
  9. // Library home page: http://www.boost.org/libs/filesystem
  10. //--------------------------------------------------------------------------------------//
  11. #ifndef BOOST_FILESYSTEM_DIRECTORY_HPP
  12. #define BOOST_FILESYSTEM_DIRECTORY_HPP
  13. #include <boost/filesystem/config.hpp>
  14. #include <boost/filesystem/path.hpp>
  15. #include <boost/filesystem/file_status.hpp>
  16. #include <boost/filesystem/detail/path_traits.hpp>
  17. #include <cstddef>
  18. #include <string>
  19. #include <vector>
  20. #include <boost/assert.hpp>
  21. #include <boost/detail/bitmask.hpp>
  22. #include <boost/system/error_code.hpp>
  23. #include <boost/smart_ptr/intrusive_ptr.hpp>
  24. #include <boost/smart_ptr/intrusive_ref_counter.hpp>
  25. #include <boost/iterator/iterator_facade.hpp>
  26. #include <boost/iterator/iterator_categories.hpp>
  27. #include <boost/filesystem/detail/header.hpp> // must be the last #include
  28. //--------------------------------------------------------------------------------------//
  29. namespace boost {
  30. namespace filesystem {
  31. enum class directory_options : unsigned int
  32. {
  33. none = 0u,
  34. skip_permission_denied = 1u, // if a directory cannot be opened because of insufficient permissions, pretend that the directory is empty
  35. follow_directory_symlink = 1u << 1u, // recursive_directory_iterator: follow directory symlinks
  36. skip_dangling_symlinks = 1u << 2u, // non-standard extension for recursive_directory_iterator: don't follow dangling directory symlinks,
  37. pop_on_error = 1u << 3u, // non-standard extension for recursive_directory_iterator: instead of producing an end iterator on errors,
  38. // repeatedly invoke pop() until it succeeds or the iterator becomes equal to end iterator
  39. _detail_no_follow = 1u << 4u, // internal use only
  40. _detail_no_push = 1u << 5u // internal use only
  41. };
  42. BOOST_BITMASK(directory_options)
  43. class directory_iterator;
  44. class recursive_directory_iterator;
  45. namespace detail {
  46. struct directory_iterator_params;
  47. BOOST_FILESYSTEM_DECL void directory_iterator_construct(directory_iterator& it, path const& p, directory_options opts, directory_iterator_params* params, system::error_code* ec);
  48. BOOST_FILESYSTEM_DECL void directory_iterator_increment(directory_iterator& it, system::error_code* ec);
  49. struct recur_dir_itr_imp;
  50. BOOST_FILESYSTEM_DECL void recursive_directory_iterator_construct(recursive_directory_iterator& it, path const& dir_path, directory_options opts, system::error_code* ec);
  51. BOOST_FILESYSTEM_DECL void recursive_directory_iterator_increment(recursive_directory_iterator& it, system::error_code* ec);
  52. BOOST_FILESYSTEM_DECL void recursive_directory_iterator_pop(recursive_directory_iterator& it, system::error_code* ec);
  53. } // namespace detail
  54. //--------------------------------------------------------------------------------------//
  55. // //
  56. // directory_entry //
  57. // //
  58. //--------------------------------------------------------------------------------------//
  59. // GCC has a problem with a member function named path within a namespace or
  60. // sub-namespace that also has a class named path. The workaround is to always
  61. // fully qualify the name path when it refers to the class name.
  62. class directory_entry
  63. {
  64. friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_construct(directory_iterator& it, path const& p, directory_options opts, detail::directory_iterator_params* params, system::error_code* ec);
  65. friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_increment(directory_iterator& it, system::error_code* ec);
  66. friend BOOST_FILESYSTEM_DECL void detail::recursive_directory_iterator_increment(recursive_directory_iterator& it, system::error_code* ec);
  67. public:
  68. typedef boost::filesystem::path::value_type value_type; // enables class path ctor taking directory_entry
  69. directory_entry() noexcept {}
  70. explicit directory_entry(boost::filesystem::path const& p);
  71. #if BOOST_FILESYSTEM_VERSION >= 4
  72. directory_entry(boost::filesystem::path const& p, system::error_code& ec) :
  73. m_path(p)
  74. {
  75. refresh_impl(&ec);
  76. if (ec)
  77. m_path.clear();
  78. }
  79. #else
  80. directory_entry(boost::filesystem::path const& p, file_status st, file_status symlink_st = file_status()) :
  81. m_path(p), m_status(st), m_symlink_status(symlink_st)
  82. {
  83. }
  84. #endif
  85. directory_entry(directory_entry const& rhs) :
  86. m_path(rhs.m_path), m_status(rhs.m_status), m_symlink_status(rhs.m_symlink_status)
  87. {
  88. }
  89. directory_entry& operator=(directory_entry const& rhs)
  90. {
  91. m_path = rhs.m_path;
  92. m_status = rhs.m_status;
  93. m_symlink_status = rhs.m_symlink_status;
  94. return *this;
  95. }
  96. directory_entry(directory_entry&& rhs) noexcept :
  97. m_path(static_cast< boost::filesystem::path&& >(rhs.m_path)),
  98. m_status(static_cast< file_status&& >(rhs.m_status)),
  99. m_symlink_status(static_cast< file_status&& >(rhs.m_symlink_status))
  100. {
  101. }
  102. directory_entry& operator=(directory_entry&& rhs) noexcept
  103. {
  104. m_path = static_cast< boost::filesystem::path&& >(rhs.m_path);
  105. m_status = static_cast< file_status&& >(rhs.m_status);
  106. m_symlink_status = static_cast< file_status&& >(rhs.m_symlink_status);
  107. return *this;
  108. }
  109. void assign(boost::filesystem::path&& p);
  110. #if BOOST_FILESYSTEM_VERSION >= 4
  111. void assign(boost::filesystem::path&& p, system::error_code& ec)
  112. {
  113. m_path = static_cast< boost::filesystem::path&& >(p);
  114. refresh_impl(&ec);
  115. }
  116. #else
  117. void assign(boost::filesystem::path&& p, file_status st, file_status symlink_st = file_status())
  118. {
  119. assign_with_status(static_cast< boost::filesystem::path&& >(p), st, symlink_st);
  120. }
  121. #endif
  122. void assign(boost::filesystem::path const& p);
  123. #if BOOST_FILESYSTEM_VERSION >= 4
  124. void assign(boost::filesystem::path const& p, system::error_code& ec)
  125. {
  126. m_path = p;
  127. refresh_impl(&ec);
  128. }
  129. #else
  130. void assign(boost::filesystem::path const& p, file_status st, file_status symlink_st = file_status())
  131. {
  132. assign_with_status(p, st, symlink_st);
  133. }
  134. #endif
  135. void replace_filename(boost::filesystem::path const& p);
  136. #if BOOST_FILESYSTEM_VERSION >= 4
  137. void replace_filename(boost::filesystem::path const& p, system::error_code& ec)
  138. {
  139. m_path.replace_filename(p);
  140. refresh_impl(&ec);
  141. }
  142. #else
  143. void replace_filename(boost::filesystem::path const& p, file_status st, file_status symlink_st = file_status())
  144. {
  145. replace_filename_with_status(p, st, symlink_st);
  146. }
  147. BOOST_FILESYSTEM_DETAIL_DEPRECATED("Use directory_entry::replace_filename() instead")
  148. void replace_leaf(boost::filesystem::path const& p, file_status st, file_status symlink_st)
  149. {
  150. replace_filename_with_status(p, st, symlink_st);
  151. }
  152. #endif
  153. boost::filesystem::path const& path() const noexcept { return m_path; }
  154. operator boost::filesystem::path const&() const noexcept { return m_path; }
  155. void refresh() { refresh_impl(); }
  156. void refresh(system::error_code& ec) noexcept { refresh_impl(&ec); }
  157. file_status status() const
  158. {
  159. if (!filesystem::status_known(m_status))
  160. refresh_impl();
  161. return m_status;
  162. }
  163. file_status status(system::error_code& ec) const noexcept
  164. {
  165. ec.clear();
  166. if (!filesystem::status_known(m_status))
  167. refresh_impl(&ec);
  168. return m_status;
  169. }
  170. file_status symlink_status() const
  171. {
  172. if (!filesystem::status_known(m_symlink_status))
  173. refresh_impl();
  174. return m_symlink_status;
  175. }
  176. file_status symlink_status(system::error_code& ec) const noexcept
  177. {
  178. ec.clear();
  179. if (!filesystem::status_known(m_symlink_status))
  180. refresh_impl(&ec);
  181. return m_symlink_status;
  182. }
  183. filesystem::file_type file_type() const
  184. {
  185. if (!filesystem::type_present(m_status))
  186. refresh_impl();
  187. return m_status.type();
  188. }
  189. filesystem::file_type file_type(system::error_code& ec) const noexcept
  190. {
  191. ec.clear();
  192. if (!filesystem::type_present(m_status))
  193. refresh_impl(&ec);
  194. return m_status.type();
  195. }
  196. filesystem::file_type symlink_file_type() const
  197. {
  198. if (!filesystem::type_present(m_symlink_status))
  199. refresh_impl();
  200. return m_symlink_status.type();
  201. }
  202. filesystem::file_type symlink_file_type(system::error_code& ec) const noexcept
  203. {
  204. ec.clear();
  205. if (!filesystem::type_present(m_symlink_status))
  206. refresh_impl(&ec);
  207. return m_symlink_status.type();
  208. }
  209. bool exists() const
  210. {
  211. filesystem::file_type ft = this->file_type();
  212. return ft != filesystem::status_error && ft != filesystem::file_not_found;
  213. }
  214. bool exists(system::error_code& ec) const noexcept
  215. {
  216. filesystem::file_type ft = this->file_type(ec);
  217. return ft != filesystem::status_error && ft != filesystem::file_not_found;
  218. }
  219. bool is_regular_file() const
  220. {
  221. return this->file_type() == filesystem::regular_file;
  222. }
  223. bool is_regular_file(system::error_code& ec) const noexcept
  224. {
  225. return this->file_type(ec) == filesystem::regular_file;
  226. }
  227. bool is_directory() const
  228. {
  229. return this->file_type() == filesystem::directory_file;
  230. }
  231. bool is_directory(system::error_code& ec) const noexcept
  232. {
  233. return this->file_type(ec) == filesystem::directory_file;
  234. }
  235. bool is_symlink() const
  236. {
  237. return this->symlink_file_type() == filesystem::symlink_file;
  238. }
  239. bool is_symlink(system::error_code& ec) const noexcept
  240. {
  241. return this->symlink_file_type(ec) == filesystem::symlink_file;
  242. }
  243. bool is_block_file() const
  244. {
  245. return this->file_type() == filesystem::block_file;
  246. }
  247. bool is_block_file(system::error_code& ec) const noexcept
  248. {
  249. return this->file_type(ec) == filesystem::block_file;
  250. }
  251. bool is_character_file() const
  252. {
  253. return this->file_type() == filesystem::character_file;
  254. }
  255. bool is_character_file(system::error_code& ec) const noexcept
  256. {
  257. return this->file_type(ec) == filesystem::character_file;
  258. }
  259. bool is_fifo() const
  260. {
  261. return this->file_type() == filesystem::fifo_file;
  262. }
  263. bool is_fifo(system::error_code& ec) const noexcept
  264. {
  265. return this->file_type(ec) == filesystem::fifo_file;
  266. }
  267. bool is_socket() const
  268. {
  269. return this->file_type() == filesystem::socket_file;
  270. }
  271. bool is_socket(system::error_code& ec) const noexcept
  272. {
  273. return this->file_type(ec) == filesystem::socket_file;
  274. }
  275. bool is_reparse_file() const
  276. {
  277. return this->symlink_file_type() == filesystem::reparse_file;
  278. }
  279. bool is_reparse_file(system::error_code& ec) const noexcept
  280. {
  281. return this->symlink_file_type(ec) == filesystem::reparse_file;
  282. }
  283. bool is_other() const
  284. {
  285. filesystem::file_type ft = this->file_type();
  286. return ft != filesystem::status_error && ft != filesystem::file_not_found &&
  287. ft != filesystem::regular_file && ft != filesystem::directory_file;
  288. }
  289. bool is_other(system::error_code& ec) const noexcept
  290. {
  291. filesystem::file_type ft = this->file_type(ec);
  292. return ft != filesystem::status_error && ft != filesystem::file_not_found &&
  293. ft != filesystem::regular_file && ft != filesystem::directory_file;
  294. }
  295. bool operator==(directory_entry const& rhs) const { return m_path == rhs.m_path; }
  296. bool operator!=(directory_entry const& rhs) const { return m_path != rhs.m_path; }
  297. bool operator<(directory_entry const& rhs) const { return m_path < rhs.m_path; }
  298. bool operator<=(directory_entry const& rhs) const { return m_path <= rhs.m_path; }
  299. bool operator>(directory_entry const& rhs) const { return m_path > rhs.m_path; }
  300. bool operator>=(directory_entry const& rhs) const { return m_path >= rhs.m_path; }
  301. private:
  302. BOOST_FILESYSTEM_DECL void refresh_impl(system::error_code* ec = nullptr) const;
  303. void assign_with_status(boost::filesystem::path&& p, file_status st, file_status symlink_st)
  304. {
  305. m_path = static_cast< boost::filesystem::path&& >(p);
  306. m_status = static_cast< file_status&& >(st);
  307. m_symlink_status = static_cast< file_status&& >(symlink_st);
  308. }
  309. void assign_with_status(boost::filesystem::path const& p, file_status st, file_status symlink_st)
  310. {
  311. m_path = p;
  312. m_status = static_cast< file_status&& >(st);
  313. m_symlink_status = static_cast< file_status&& >(symlink_st);
  314. }
  315. void replace_filename_with_status(boost::filesystem::path const& p, file_status st, file_status symlink_st)
  316. {
  317. m_path.replace_filename(p);
  318. m_status = static_cast< file_status&& >(st);
  319. m_symlink_status = static_cast< file_status&& >(symlink_st);
  320. }
  321. private:
  322. boost::filesystem::path m_path;
  323. mutable file_status m_status; // stat()-like
  324. mutable file_status m_symlink_status; // lstat()-like
  325. };
  326. #if !defined(BOOST_FILESYSTEM_SOURCE)
  327. inline directory_entry::directory_entry(boost::filesystem::path const& p) :
  328. m_path(p)
  329. {
  330. #if BOOST_FILESYSTEM_VERSION >= 4
  331. refresh_impl();
  332. #endif
  333. }
  334. inline void directory_entry::assign(boost::filesystem::path&& p)
  335. {
  336. m_path = static_cast< boost::filesystem::path&& >(p);
  337. #if BOOST_FILESYSTEM_VERSION >= 4
  338. refresh_impl();
  339. #else
  340. m_status = file_status();
  341. m_symlink_status = file_status();
  342. #endif
  343. }
  344. inline void directory_entry::assign(boost::filesystem::path const& p)
  345. {
  346. m_path = p;
  347. #if BOOST_FILESYSTEM_VERSION >= 4
  348. refresh_impl();
  349. #else
  350. m_status = file_status();
  351. m_symlink_status = file_status();
  352. #endif
  353. }
  354. inline void directory_entry::replace_filename(boost::filesystem::path const& p)
  355. {
  356. m_path.replace_filename(p);
  357. #if BOOST_FILESYSTEM_VERSION >= 4
  358. refresh_impl();
  359. #else
  360. m_status = file_status();
  361. m_symlink_status = file_status();
  362. #endif
  363. }
  364. #endif // !defined(BOOST_FILESYSTEM_SOURCE)
  365. namespace detail {
  366. namespace path_traits {
  367. // Dispatch function for integration with path class
  368. template< typename Callback >
  369. BOOST_FORCEINLINE typename Callback::result_type dispatch(directory_entry const& de, Callback cb, const codecvt_type* cvt, directory_entry_tag)
  370. {
  371. boost::filesystem::path::string_type const& source = de.path().native();
  372. return cb(source.data(), source.data() + source.size(), cvt);
  373. }
  374. } // namespace path_traits
  375. } // namespace detail
  376. //--------------------------------------------------------------------------------------//
  377. // //
  378. // directory_entry overloads //
  379. // //
  380. //--------------------------------------------------------------------------------------//
  381. // Without these functions, calling (for example) 'is_directory' with a 'directory_entry' results in:
  382. // - a conversion to 'path' using 'operator boost::filesystem::path const&()',
  383. // - then a call to 'is_directory(path const& p)' which recomputes the status with 'detail::status(p)'.
  384. //
  385. // These functions avoid a costly recomputation of the status if one calls 'is_directory(e)' instead of 'is_directory(e.status())'
  386. inline file_status status(directory_entry const& e)
  387. {
  388. return e.status();
  389. }
  390. inline file_status status(directory_entry const& e, system::error_code& ec) noexcept
  391. {
  392. return e.status(ec);
  393. }
  394. inline file_status symlink_status(directory_entry const& e)
  395. {
  396. return e.symlink_status();
  397. }
  398. inline file_status symlink_status(directory_entry const& e, system::error_code& ec) noexcept
  399. {
  400. return e.symlink_status(ec);
  401. }
  402. inline bool type_present(directory_entry const& e)
  403. {
  404. return e.file_type() != filesystem::status_error;
  405. }
  406. inline bool type_present(directory_entry const& e, system::error_code& ec) noexcept
  407. {
  408. return e.file_type(ec) != filesystem::status_error;
  409. }
  410. inline bool status_known(directory_entry const& e)
  411. {
  412. return filesystem::status_known(e.status());
  413. }
  414. inline bool status_known(directory_entry const& e, system::error_code& ec) noexcept
  415. {
  416. return filesystem::status_known(e.status(ec));
  417. }
  418. inline bool exists(directory_entry const& e)
  419. {
  420. return e.exists();
  421. }
  422. inline bool exists(directory_entry const& e, system::error_code& ec) noexcept
  423. {
  424. return e.exists(ec);
  425. }
  426. inline bool is_regular_file(directory_entry const& e)
  427. {
  428. return e.is_regular_file();
  429. }
  430. inline bool is_regular_file(directory_entry const& e, system::error_code& ec) noexcept
  431. {
  432. return e.is_regular_file(ec);
  433. }
  434. inline bool is_directory(directory_entry const& e)
  435. {
  436. return e.is_directory();
  437. }
  438. inline bool is_directory(directory_entry const& e, system::error_code& ec) noexcept
  439. {
  440. return e.is_directory(ec);
  441. }
  442. inline bool is_symlink(directory_entry const& e)
  443. {
  444. return e.is_symlink();
  445. }
  446. inline bool is_symlink(directory_entry const& e, system::error_code& ec) noexcept
  447. {
  448. return e.is_symlink(ec);
  449. }
  450. inline bool is_block_file(directory_entry const& e)
  451. {
  452. return e.is_block_file();
  453. }
  454. inline bool is_block_file(directory_entry const& e, system::error_code& ec) noexcept
  455. {
  456. return e.is_block_file(ec);
  457. }
  458. inline bool is_character_file(directory_entry const& e)
  459. {
  460. return e.is_character_file();
  461. }
  462. inline bool is_character_file(directory_entry const& e, system::error_code& ec) noexcept
  463. {
  464. return e.is_character_file(ec);
  465. }
  466. inline bool is_fifo(directory_entry const& e)
  467. {
  468. return e.is_fifo();
  469. }
  470. inline bool is_fifo(directory_entry const& e, system::error_code& ec) noexcept
  471. {
  472. return e.is_fifo(ec);
  473. }
  474. inline bool is_socket(directory_entry const& e)
  475. {
  476. return e.is_socket();
  477. }
  478. inline bool is_socket(directory_entry const& e, system::error_code& ec) noexcept
  479. {
  480. return e.is_socket(ec);
  481. }
  482. inline bool is_reparse_file(directory_entry const& e)
  483. {
  484. return e.is_reparse_file();
  485. }
  486. inline bool is_reparse_file(directory_entry const& e, system::error_code& ec) noexcept
  487. {
  488. return e.is_reparse_file(ec);
  489. }
  490. inline bool is_other(directory_entry const& e)
  491. {
  492. return e.is_other();
  493. }
  494. inline bool is_other(directory_entry const& e, system::error_code& ec) noexcept
  495. {
  496. return e.is_other(ec);
  497. }
  498. //--------------------------------------------------------------------------------------//
  499. // //
  500. // directory_iterator helpers //
  501. // //
  502. //--------------------------------------------------------------------------------------//
  503. namespace detail {
  504. struct dir_itr_imp :
  505. public boost::intrusive_ref_counter< dir_itr_imp >
  506. {
  507. #ifdef BOOST_WINDOWS_API
  508. bool close_handle;
  509. unsigned char extra_data_format;
  510. std::size_t current_offset;
  511. #endif
  512. directory_entry dir_entry;
  513. void* handle;
  514. dir_itr_imp() noexcept :
  515. #ifdef BOOST_WINDOWS_API
  516. close_handle(false),
  517. extra_data_format(0u),
  518. current_offset(0u),
  519. #endif
  520. handle(nullptr)
  521. {
  522. }
  523. BOOST_FILESYSTEM_DECL ~dir_itr_imp() noexcept;
  524. BOOST_FILESYSTEM_DECL static void* operator new(std::size_t class_size, std::size_t extra_size) noexcept;
  525. BOOST_FILESYSTEM_DECL static void operator delete(void* p, std::size_t extra_size) noexcept;
  526. BOOST_FILESYSTEM_DECL static void operator delete(void* p) noexcept;
  527. };
  528. } // namespace detail
  529. //--------------------------------------------------------------------------------------//
  530. // //
  531. // directory_iterator //
  532. // //
  533. //--------------------------------------------------------------------------------------//
  534. class directory_iterator :
  535. public boost::iterator_facade<
  536. directory_iterator,
  537. directory_entry,
  538. boost::single_pass_traversal_tag
  539. >
  540. {
  541. friend class boost::iterator_core_access;
  542. friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_construct(directory_iterator& it, path const& p, directory_options opts, detail::directory_iterator_params* params, system::error_code* ec);
  543. friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_increment(directory_iterator& it, system::error_code* ec);
  544. friend BOOST_FILESYSTEM_DECL void detail::recursive_directory_iterator_increment(recursive_directory_iterator& it, system::error_code* ec);
  545. public:
  546. directory_iterator() noexcept {} // creates the "end" iterator
  547. // iterator_facade derived classes don't seem to like implementations in
  548. // separate translation unit dll's, so forward to detail functions
  549. explicit directory_iterator(path const& p, directory_options opts = directory_options::none)
  550. {
  551. detail::directory_iterator_construct(*this, p, opts, nullptr, nullptr);
  552. }
  553. directory_iterator(path const& p, system::error_code& ec) noexcept
  554. {
  555. detail::directory_iterator_construct(*this, p, directory_options::none, nullptr, &ec);
  556. }
  557. directory_iterator(path const& p, directory_options opts, system::error_code& ec) noexcept
  558. {
  559. detail::directory_iterator_construct(*this, p, opts, nullptr, &ec);
  560. }
  561. directory_iterator(directory_iterator const&) = default;
  562. directory_iterator& operator=(directory_iterator const&) = default;
  563. directory_iterator(directory_iterator&& that) noexcept :
  564. m_imp(static_cast< boost::intrusive_ptr< detail::dir_itr_imp >&& >(that.m_imp))
  565. {
  566. }
  567. directory_iterator& operator=(directory_iterator&& that) noexcept
  568. {
  569. m_imp = static_cast< boost::intrusive_ptr< detail::dir_itr_imp >&& >(that.m_imp);
  570. return *this;
  571. }
  572. directory_iterator& increment(system::error_code& ec) noexcept
  573. {
  574. detail::directory_iterator_increment(*this, &ec);
  575. return *this;
  576. }
  577. private:
  578. boost::iterator_facade<
  579. directory_iterator,
  580. directory_entry,
  581. boost::single_pass_traversal_tag
  582. >::reference dereference() const
  583. {
  584. BOOST_ASSERT_MSG(!is_end(), "attempt to dereference end directory iterator");
  585. return m_imp->dir_entry;
  586. }
  587. void increment() { detail::directory_iterator_increment(*this, nullptr); }
  588. bool equal(directory_iterator const& rhs) const noexcept
  589. {
  590. return m_imp == rhs.m_imp || (is_end() && rhs.is_end());
  591. }
  592. bool is_end() const noexcept
  593. {
  594. // Note: The check for handle is needed because the iterator can be copied and the copy
  595. // can be incremented to end while the original iterator still refers to the same dir_itr_imp.
  596. return !m_imp || !m_imp->handle;
  597. }
  598. private:
  599. // intrusive_ptr provides the shallow-copy semantics required for single pass iterators
  600. // (i.e. InputIterators). The end iterator is indicated by is_end().
  601. boost::intrusive_ptr< detail::dir_itr_imp > m_imp;
  602. };
  603. // enable directory_iterator C++11 range-based for statement use --------------------//
  604. // begin() and end() are only used by a range-based for statement in the context of
  605. // auto - thus the top-level const is stripped - so returning const is harmless and
  606. // emphasizes begin() is just a pass through.
  607. inline directory_iterator const& begin(directory_iterator const& iter) noexcept
  608. {
  609. return iter;
  610. }
  611. inline directory_iterator end(directory_iterator const&) noexcept
  612. {
  613. return directory_iterator();
  614. }
  615. // enable C++14 generic accessors for range const iterators
  616. inline directory_iterator const& cbegin(directory_iterator const& iter) noexcept
  617. {
  618. return iter;
  619. }
  620. inline directory_iterator cend(directory_iterator const&) noexcept
  621. {
  622. return directory_iterator();
  623. }
  624. // enable directory_iterator BOOST_FOREACH -----------------------------------------//
  625. inline directory_iterator& range_begin(directory_iterator& iter) noexcept
  626. {
  627. return iter;
  628. }
  629. inline directory_iterator range_begin(directory_iterator const& iter) noexcept
  630. {
  631. return iter;
  632. }
  633. inline directory_iterator range_end(directory_iterator&) noexcept
  634. {
  635. return directory_iterator();
  636. }
  637. inline directory_iterator range_end(directory_iterator const&) noexcept
  638. {
  639. return directory_iterator();
  640. }
  641. } // namespace filesystem
  642. // namespace boost template specializations
  643. template< typename C, typename Enabler >
  644. struct range_mutable_iterator;
  645. template<>
  646. struct range_mutable_iterator< boost::filesystem::directory_iterator, void >
  647. {
  648. typedef boost::filesystem::directory_iterator type;
  649. };
  650. template< typename C, typename Enabler >
  651. struct range_const_iterator;
  652. template<>
  653. struct range_const_iterator< boost::filesystem::directory_iterator, void >
  654. {
  655. typedef boost::filesystem::directory_iterator type;
  656. };
  657. namespace filesystem {
  658. //--------------------------------------------------------------------------------------//
  659. // //
  660. // recursive_directory_iterator helpers //
  661. // //
  662. //--------------------------------------------------------------------------------------//
  663. namespace detail {
  664. struct recur_dir_itr_imp :
  665. public boost::intrusive_ref_counter< recur_dir_itr_imp >
  666. {
  667. typedef directory_iterator element_type;
  668. std::vector< element_type > m_stack;
  669. directory_options m_options;
  670. explicit recur_dir_itr_imp(directory_options opts) noexcept : m_options(opts) {}
  671. };
  672. } // namespace detail
  673. //--------------------------------------------------------------------------------------//
  674. // //
  675. // recursive_directory_iterator //
  676. // //
  677. //--------------------------------------------------------------------------------------//
  678. class recursive_directory_iterator :
  679. public boost::iterator_facade<
  680. recursive_directory_iterator,
  681. directory_entry,
  682. boost::single_pass_traversal_tag
  683. >
  684. {
  685. friend class boost::iterator_core_access;
  686. friend BOOST_FILESYSTEM_DECL void detail::recursive_directory_iterator_construct(recursive_directory_iterator& it, path const& dir_path, directory_options opts, system::error_code* ec);
  687. friend BOOST_FILESYSTEM_DECL void detail::recursive_directory_iterator_increment(recursive_directory_iterator& it, system::error_code* ec);
  688. friend BOOST_FILESYSTEM_DECL void detail::recursive_directory_iterator_pop(recursive_directory_iterator& it, system::error_code* ec);
  689. public:
  690. recursive_directory_iterator() noexcept {} // creates the "end" iterator
  691. explicit recursive_directory_iterator(path const& dir_path)
  692. {
  693. detail::recursive_directory_iterator_construct(*this, dir_path, directory_options::none, nullptr);
  694. }
  695. recursive_directory_iterator(path const& dir_path, system::error_code& ec)
  696. {
  697. detail::recursive_directory_iterator_construct(*this, dir_path, directory_options::none, &ec);
  698. }
  699. recursive_directory_iterator(path const& dir_path, directory_options opts)
  700. {
  701. detail::recursive_directory_iterator_construct(*this, dir_path, opts, nullptr);
  702. }
  703. recursive_directory_iterator(path const& dir_path, directory_options opts, system::error_code& ec)
  704. {
  705. detail::recursive_directory_iterator_construct(*this, dir_path, opts, &ec);
  706. }
  707. recursive_directory_iterator(recursive_directory_iterator const&) = default;
  708. recursive_directory_iterator& operator=(recursive_directory_iterator const&) = default;
  709. recursive_directory_iterator(recursive_directory_iterator&& that) noexcept :
  710. m_imp(static_cast< boost::intrusive_ptr< detail::recur_dir_itr_imp >&& >(that.m_imp))
  711. {
  712. }
  713. recursive_directory_iterator& operator=(recursive_directory_iterator&& that) noexcept
  714. {
  715. m_imp = static_cast< boost::intrusive_ptr< detail::recur_dir_itr_imp >&& >(that.m_imp);
  716. return *this;
  717. }
  718. recursive_directory_iterator& increment(system::error_code& ec) noexcept
  719. {
  720. detail::recursive_directory_iterator_increment(*this, &ec);
  721. return *this;
  722. }
  723. int depth() const noexcept
  724. {
  725. BOOST_ASSERT_MSG(!is_end(), "depth() on end recursive_directory_iterator");
  726. return static_cast< int >(m_imp->m_stack.size() - 1u);
  727. }
  728. bool recursion_pending() const noexcept
  729. {
  730. BOOST_ASSERT_MSG(!is_end(), "recursion_pending() on end recursive_directory_iterator");
  731. return (m_imp->m_options & directory_options::_detail_no_push) == directory_options::none;
  732. }
  733. void pop()
  734. {
  735. detail::recursive_directory_iterator_pop(*this, nullptr);
  736. }
  737. void pop(system::error_code& ec) noexcept
  738. {
  739. detail::recursive_directory_iterator_pop(*this, &ec);
  740. }
  741. void disable_recursion_pending(bool value = true) noexcept
  742. {
  743. BOOST_ASSERT_MSG(!is_end(), "disable_recursion_pending() on end recursive_directory_iterator");
  744. if (value)
  745. m_imp->m_options |= directory_options::_detail_no_push;
  746. else
  747. m_imp->m_options &= ~directory_options::_detail_no_push;
  748. }
  749. file_status status() const
  750. {
  751. BOOST_ASSERT_MSG(!is_end(), "status() on end recursive_directory_iterator");
  752. return m_imp->m_stack.back()->status();
  753. }
  754. file_status symlink_status() const
  755. {
  756. BOOST_ASSERT_MSG(!is_end(), "symlink_status() on end recursive_directory_iterator");
  757. return m_imp->m_stack.back()->symlink_status();
  758. }
  759. private:
  760. boost::iterator_facade<
  761. recursive_directory_iterator,
  762. directory_entry,
  763. boost::single_pass_traversal_tag
  764. >::reference dereference() const
  765. {
  766. BOOST_ASSERT_MSG(!is_end(), "dereference of end recursive_directory_iterator");
  767. return *m_imp->m_stack.back();
  768. }
  769. void increment() { detail::recursive_directory_iterator_increment(*this, nullptr); }
  770. bool equal(recursive_directory_iterator const& rhs) const noexcept
  771. {
  772. return m_imp == rhs.m_imp || (is_end() && rhs.is_end());
  773. }
  774. bool is_end() const noexcept
  775. {
  776. // Note: The check for m_stack.empty() is needed because the iterator can be copied and the copy
  777. // can be incremented to end while the original iterator still refers to the same recur_dir_itr_imp.
  778. return !m_imp || m_imp->m_stack.empty();
  779. }
  780. private:
  781. // intrusive_ptr provides the shallow-copy semantics required for single pass iterators
  782. // (i.e. InputIterators). The end iterator is indicated by is_end().
  783. boost::intrusive_ptr< detail::recur_dir_itr_imp > m_imp;
  784. };
  785. // enable recursive directory iterator C++11 range-base for statement use ----------//
  786. // begin() and end() are only used by a range-based for statement in the context of
  787. // auto - thus the top-level const is stripped - so returning const is harmless and
  788. // emphasizes begin() is just a pass through.
  789. inline recursive_directory_iterator const& begin(recursive_directory_iterator const& iter) noexcept
  790. {
  791. return iter;
  792. }
  793. inline recursive_directory_iterator end(recursive_directory_iterator const&) noexcept
  794. {
  795. return recursive_directory_iterator();
  796. }
  797. // enable C++14 generic accessors for range const iterators
  798. inline recursive_directory_iterator const& cbegin(recursive_directory_iterator const& iter) noexcept
  799. {
  800. return iter;
  801. }
  802. inline recursive_directory_iterator cend(recursive_directory_iterator const&) noexcept
  803. {
  804. return recursive_directory_iterator();
  805. }
  806. // enable recursive directory iterator BOOST_FOREACH -------------------------------//
  807. inline recursive_directory_iterator& range_begin(recursive_directory_iterator& iter) noexcept
  808. {
  809. return iter;
  810. }
  811. inline recursive_directory_iterator range_begin(recursive_directory_iterator const& iter) noexcept
  812. {
  813. return iter;
  814. }
  815. inline recursive_directory_iterator range_end(recursive_directory_iterator&) noexcept
  816. {
  817. return recursive_directory_iterator();
  818. }
  819. inline recursive_directory_iterator range_end(recursive_directory_iterator const&) noexcept
  820. {
  821. return recursive_directory_iterator();
  822. }
  823. } // namespace filesystem
  824. // namespace boost template specializations
  825. template<>
  826. struct range_mutable_iterator< boost::filesystem::recursive_directory_iterator, void >
  827. {
  828. typedef boost::filesystem::recursive_directory_iterator type;
  829. };
  830. template<>
  831. struct range_const_iterator< boost::filesystem::recursive_directory_iterator, void >
  832. {
  833. typedef boost::filesystem::recursive_directory_iterator type;
  834. };
  835. } // namespace boost
  836. #include <boost/filesystem/detail/footer.hpp>
  837. #endif // BOOST_FILESYSTEM_DIRECTORY_HPP