directory.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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
  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_FILESYSTEM3_DIRECTORY_HPP
  12. #define BOOST_FILESYSTEM3_DIRECTORY_HPP
  13. #include <boost/config.hpp>
  14. # if defined( BOOST_NO_STD_WSTRING )
  15. # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support
  16. # endif
  17. #include <boost/filesystem/config.hpp>
  18. #include <boost/filesystem/path.hpp>
  19. #include <boost/filesystem/file_status.hpp>
  20. #include <string>
  21. #include <vector>
  22. #include <utility> // std::move
  23. #include <boost/assert.hpp>
  24. #include <boost/core/scoped_enum.hpp>
  25. #include <boost/detail/bitmask.hpp>
  26. #include <boost/system/error_code.hpp>
  27. #include <boost/smart_ptr/intrusive_ptr.hpp>
  28. #include <boost/smart_ptr/intrusive_ref_counter.hpp>
  29. #include <boost/iterator/iterator_facade.hpp>
  30. #include <boost/iterator/iterator_categories.hpp>
  31. #include <boost/config/abi_prefix.hpp> // must be the last #include
  32. //--------------------------------------------------------------------------------------//
  33. namespace boost {
  34. namespace filesystem {
  35. //--------------------------------------------------------------------------------------//
  36. // //
  37. // directory_entry //
  38. // //
  39. //--------------------------------------------------------------------------------------//
  40. // GCC has a problem with a member function named path within a namespace or
  41. // sub-namespace that also has a class named path. The workaround is to always
  42. // fully qualify the name path when it refers to the class name.
  43. class directory_entry
  44. {
  45. public:
  46. typedef boost::filesystem::path::value_type value_type; // enables class path ctor taking directory_entry
  47. directory_entry() BOOST_NOEXCEPT {}
  48. explicit directory_entry(const boost::filesystem::path& p) :
  49. m_path(p), m_status(file_status()), m_symlink_status(file_status())
  50. {
  51. }
  52. directory_entry(const boost::filesystem::path& p,
  53. file_status st, file_status symlink_st = file_status()) :
  54. m_path(p), m_status(st), m_symlink_status(symlink_st)
  55. {
  56. }
  57. directory_entry(const directory_entry& rhs) :
  58. m_path(rhs.m_path), m_status(rhs.m_status), m_symlink_status(rhs.m_symlink_status)
  59. {
  60. }
  61. directory_entry& operator=(const directory_entry& rhs)
  62. {
  63. m_path = rhs.m_path;
  64. m_status = rhs.m_status;
  65. m_symlink_status = rhs.m_symlink_status;
  66. return *this;
  67. }
  68. // As of October 2015 the interaction between noexcept and =default is so troublesome
  69. // for VC++, GCC, and probably other compilers, that =default is not used with noexcept
  70. // functions. GCC is not even consistent for the same release on different platforms.
  71. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  72. directory_entry(directory_entry&& rhs) BOOST_NOEXCEPT :
  73. m_path(std::move(rhs.m_path)), m_status(std::move(rhs.m_status)), m_symlink_status(std::move(rhs.m_symlink_status))
  74. {
  75. }
  76. directory_entry& operator=(directory_entry&& rhs) BOOST_NOEXCEPT
  77. {
  78. m_path = std::move(rhs.m_path);
  79. m_status = std::move(rhs.m_status);
  80. m_symlink_status = std::move(rhs.m_symlink_status);
  81. return *this;
  82. }
  83. #endif
  84. void assign(const boost::filesystem::path& p,
  85. file_status st = file_status(), file_status symlink_st = file_status())
  86. {
  87. m_path = p;
  88. m_status = st;
  89. m_symlink_status = symlink_st;
  90. }
  91. void replace_filename(const boost::filesystem::path& p,
  92. file_status st = file_status(), file_status symlink_st = file_status())
  93. {
  94. m_path.remove_filename();
  95. m_path /= p;
  96. m_status = st;
  97. m_symlink_status = symlink_st;
  98. }
  99. # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  100. void replace_leaf(const boost::filesystem::path& p, file_status st, file_status symlink_st)
  101. {
  102. replace_filename(p, st, symlink_st);
  103. }
  104. # endif
  105. const boost::filesystem::path& path() const BOOST_NOEXCEPT { return m_path; }
  106. operator const boost::filesystem::path&() const BOOST_NOEXCEPT { return m_path; }
  107. file_status status() const { return get_status(); }
  108. file_status status(system::error_code& ec) const BOOST_NOEXCEPT { return get_status(&ec); }
  109. file_status symlink_status() const { return get_symlink_status(); }
  110. file_status symlink_status(system::error_code& ec) const BOOST_NOEXCEPT { return get_symlink_status(&ec); }
  111. bool operator==(const directory_entry& rhs) const BOOST_NOEXCEPT { return m_path == rhs.m_path; }
  112. bool operator!=(const directory_entry& rhs) const BOOST_NOEXCEPT { return m_path != rhs.m_path; }
  113. bool operator< (const directory_entry& rhs) const BOOST_NOEXCEPT { return m_path < rhs.m_path; }
  114. bool operator<=(const directory_entry& rhs) const BOOST_NOEXCEPT { return m_path <= rhs.m_path; }
  115. bool operator> (const directory_entry& rhs) const BOOST_NOEXCEPT { return m_path > rhs.m_path; }
  116. bool operator>=(const directory_entry& rhs) const BOOST_NOEXCEPT { return m_path >= rhs.m_path; }
  117. private:
  118. BOOST_FILESYSTEM_DECL file_status get_status(system::error_code* ec=0) const;
  119. BOOST_FILESYSTEM_DECL file_status get_symlink_status(system::error_code* ec=0) const;
  120. private:
  121. boost::filesystem::path m_path;
  122. mutable file_status m_status; // stat()-like
  123. mutable file_status m_symlink_status; // lstat()-like
  124. }; // directory_entry
  125. //--------------------------------------------------------------------------------------//
  126. // //
  127. // directory_entry overloads //
  128. // //
  129. //--------------------------------------------------------------------------------------//
  130. // Without these functions, calling (for example) 'is_directory' with a 'directory_entry' results in:
  131. // - a conversion to 'path' using 'operator const boost::filesystem::path&()',
  132. // - then a call to 'is_directory(const path& p)' which recomputes the status with 'detail::status(p)'.
  133. //
  134. // These functions avoid a costly recomputation of the status if one calls 'is_directory(e)' instead of 'is_directory(e.status())'
  135. inline file_status status (const directory_entry& e) { return e.status(); }
  136. inline file_status status (const directory_entry& e, system::error_code& ec) BOOST_NOEXCEPT { return e.status(ec); }
  137. inline bool type_present (const directory_entry& e) { return filesystem::type_present(e.status()); }
  138. inline bool type_present (const directory_entry& e, system::error_code& ec) BOOST_NOEXCEPT { return filesystem::type_present(e.status(ec)); }
  139. inline bool status_known (const directory_entry& e) { return filesystem::status_known(e.status()); }
  140. inline bool status_known (const directory_entry& e, system::error_code& ec) BOOST_NOEXCEPT { return filesystem::status_known(e.status(ec)); }
  141. inline bool exists (const directory_entry& e) { return filesystem::exists(e.status()); }
  142. inline bool exists (const directory_entry& e, system::error_code& ec) BOOST_NOEXCEPT { return filesystem::exists(e.status(ec)); }
  143. inline bool is_regular_file(const directory_entry& e) { return filesystem::is_regular_file(e.status()); }
  144. inline bool is_regular_file(const directory_entry& e, system::error_code& ec) BOOST_NOEXCEPT { return filesystem::is_regular_file(e.status(ec)); }
  145. inline bool is_directory (const directory_entry& e) { return filesystem::is_directory(e.status()); }
  146. inline bool is_directory (const directory_entry& e, system::error_code& ec) BOOST_NOEXCEPT { return filesystem::is_directory(e.status(ec)); }
  147. inline bool is_symlink (const directory_entry& e) { return filesystem::is_symlink(e.symlink_status()); }
  148. inline bool is_symlink (const directory_entry& e, system::error_code& ec) BOOST_NOEXCEPT { return filesystem::is_symlink(e.symlink_status(ec)); }
  149. inline bool is_other (const directory_entry& e) { return filesystem::is_other(e.status()); }
  150. inline bool is_other (const directory_entry& e, system::error_code& ec) BOOST_NOEXCEPT { return filesystem::is_other(e.status(ec)); }
  151. #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  152. inline bool is_regular (const directory_entry& e) { return filesystem::is_regular(e.status()); }
  153. #endif
  154. //--------------------------------------------------------------------------------------//
  155. // //
  156. // directory_iterator helpers //
  157. // //
  158. //--------------------------------------------------------------------------------------//
  159. BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(directory_options, unsigned int)
  160. {
  161. none = 0u,
  162. skip_permission_denied = 1u, // if a directory cannot be opened because of insufficient permissions, pretend that the directory is empty
  163. follow_directory_symlink = 1u << 1, // recursive_directory_iterator: follow directory symlinks
  164. skip_dangling_symlinks = 1u << 2, // non-standard extension for recursive_directory_iterator: don't follow dangling directory symlinks,
  165. pop_on_error = 1u << 3, // non-standard extension for recursive_directory_iterator: instead of producing an end iterator on errors,
  166. // repeatedly invoke pop() until it succeeds or the iterator becomes equal to end iterator
  167. _detail_no_push = 1u << 4 // internal use only
  168. }
  169. BOOST_SCOPED_ENUM_DECLARE_END(directory_options)
  170. BOOST_BITMASK(BOOST_SCOPED_ENUM_NATIVE(directory_options))
  171. class directory_iterator;
  172. namespace detail {
  173. BOOST_FILESYSTEM_DECL
  174. system::error_code dir_itr_close(// never throws()
  175. void*& handle
  176. #if defined(BOOST_POSIX_API)
  177. , void*& buffer
  178. #endif
  179. ) BOOST_NOEXCEPT;
  180. struct dir_itr_imp :
  181. public boost::intrusive_ref_counter< dir_itr_imp >
  182. {
  183. directory_entry dir_entry;
  184. void* handle;
  185. #if defined(BOOST_POSIX_API)
  186. void* buffer; // see dir_itr_increment implementation
  187. #endif
  188. dir_itr_imp() BOOST_NOEXCEPT :
  189. handle(0)
  190. #if defined(BOOST_POSIX_API)
  191. , buffer(0)
  192. #endif
  193. {
  194. }
  195. ~dir_itr_imp() BOOST_NOEXCEPT
  196. {
  197. dir_itr_close(handle
  198. #if defined(BOOST_POSIX_API)
  199. , buffer
  200. #endif
  201. );
  202. }
  203. };
  204. // see path::iterator: comment below
  205. BOOST_FILESYSTEM_DECL void directory_iterator_construct(directory_iterator& it, const path& p, unsigned int opts, system::error_code* ec);
  206. BOOST_FILESYSTEM_DECL void directory_iterator_increment(directory_iterator& it, system::error_code* ec);
  207. } // namespace detail
  208. //--------------------------------------------------------------------------------------//
  209. // //
  210. // directory_iterator //
  211. // //
  212. //--------------------------------------------------------------------------------------//
  213. class directory_iterator :
  214. public boost::iterator_facade<
  215. directory_iterator,
  216. directory_entry,
  217. boost::single_pass_traversal_tag
  218. >
  219. {
  220. friend class boost::iterator_core_access;
  221. friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_construct(directory_iterator& it, const path& p, unsigned int opts, system::error_code* ec);
  222. friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_increment(directory_iterator& it, system::error_code* ec);
  223. public:
  224. directory_iterator() BOOST_NOEXCEPT {} // creates the "end" iterator
  225. // iterator_facade derived classes don't seem to like implementations in
  226. // separate translation unit dll's, so forward to detail functions
  227. explicit directory_iterator(const path& p, BOOST_SCOPED_ENUM_NATIVE(directory_options) opts = directory_options::none)
  228. {
  229. detail::directory_iterator_construct(*this, p, static_cast< unsigned int >(opts), 0);
  230. }
  231. directory_iterator(const path& p, system::error_code& ec) BOOST_NOEXCEPT
  232. {
  233. detail::directory_iterator_construct(*this, p, static_cast< unsigned int >(directory_options::none), &ec);
  234. }
  235. directory_iterator(const path& p, BOOST_SCOPED_ENUM_NATIVE(directory_options) opts, system::error_code& ec) BOOST_NOEXCEPT
  236. {
  237. detail::directory_iterator_construct(*this, p, static_cast< unsigned int >(opts), &ec);
  238. }
  239. BOOST_DEFAULTED_FUNCTION(directory_iterator(directory_iterator const& that), : m_imp(that.m_imp) {})
  240. BOOST_DEFAULTED_FUNCTION(directory_iterator& operator= (directory_iterator const& that), { m_imp = that.m_imp; return *this; })
  241. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  242. directory_iterator(directory_iterator&& that) BOOST_NOEXCEPT :
  243. m_imp(std::move(that.m_imp))
  244. {
  245. }
  246. directory_iterator& operator= (directory_iterator&& that) BOOST_NOEXCEPT
  247. {
  248. m_imp = std::move(that.m_imp);
  249. return *this;
  250. }
  251. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  252. directory_iterator& increment(system::error_code& ec) BOOST_NOEXCEPT
  253. {
  254. detail::directory_iterator_increment(*this, &ec);
  255. return *this;
  256. }
  257. private:
  258. boost::iterator_facade<
  259. directory_iterator,
  260. directory_entry,
  261. boost::single_pass_traversal_tag
  262. >::reference dereference() const
  263. {
  264. BOOST_ASSERT_MSG(!is_end(), "attempt to dereference end directory iterator");
  265. return m_imp->dir_entry;
  266. }
  267. void increment() { detail::directory_iterator_increment(*this, 0); }
  268. bool equal(const directory_iterator& rhs) const BOOST_NOEXCEPT
  269. {
  270. return m_imp == rhs.m_imp || (is_end() && rhs.is_end());
  271. }
  272. bool is_end() const BOOST_NOEXCEPT
  273. {
  274. // Note: The check for handle is needed because the iterator can be copied and the copy
  275. // can be incremented to end while the original iterator still refers to the same dir_itr_imp.
  276. return !m_imp || !m_imp->handle;
  277. }
  278. private:
  279. // intrusive_ptr provides the shallow-copy semantics required for single pass iterators
  280. // (i.e. InputIterators). The end iterator is indicated by is_end().
  281. boost::intrusive_ptr< detail::dir_itr_imp > m_imp;
  282. };
  283. // enable directory_iterator C++11 range-based for statement use --------------------//
  284. // begin() and end() are only used by a range-based for statement in the context of
  285. // auto - thus the top-level const is stripped - so returning const is harmless and
  286. // emphasizes begin() is just a pass through.
  287. inline const directory_iterator& begin(const directory_iterator& iter) BOOST_NOEXCEPT { return iter; }
  288. inline directory_iterator end(const directory_iterator&) BOOST_NOEXCEPT { return directory_iterator(); }
  289. // enable C++14 generic accessors for range const iterators
  290. inline const directory_iterator& cbegin(const directory_iterator& iter) BOOST_NOEXCEPT { return iter; }
  291. inline directory_iterator cend(const directory_iterator&) BOOST_NOEXCEPT { return directory_iterator(); }
  292. // enable directory_iterator BOOST_FOREACH -----------------------------------------//
  293. inline directory_iterator& range_begin(directory_iterator& iter) BOOST_NOEXCEPT { return iter; }
  294. inline directory_iterator range_begin(const directory_iterator& iter) BOOST_NOEXCEPT { return iter; }
  295. inline directory_iterator range_end(directory_iterator&) BOOST_NOEXCEPT { return directory_iterator(); }
  296. inline directory_iterator range_end(const directory_iterator&) BOOST_NOEXCEPT { return directory_iterator(); }
  297. } // namespace filesystem
  298. // namespace boost template specializations
  299. template<typename C, typename Enabler>
  300. struct range_mutable_iterator;
  301. template<>
  302. struct range_mutable_iterator<boost::filesystem::directory_iterator, void>
  303. {
  304. typedef boost::filesystem::directory_iterator type;
  305. };
  306. template<typename C, typename Enabler>
  307. struct range_const_iterator;
  308. template<>
  309. struct range_const_iterator<boost::filesystem::directory_iterator, void>
  310. {
  311. typedef boost::filesystem::directory_iterator type;
  312. };
  313. namespace filesystem {
  314. //--------------------------------------------------------------------------------------//
  315. // //
  316. // recursive_directory_iterator helpers //
  317. // //
  318. //--------------------------------------------------------------------------------------//
  319. #if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
  320. // Deprecated enum, use directory_options instead
  321. BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(symlink_option, unsigned int)
  322. {
  323. none = static_cast< unsigned int >(directory_options::none),
  324. no_recurse = none, // don't follow directory symlinks (default behavior)
  325. recurse = static_cast< unsigned int >(directory_options::follow_directory_symlink), // follow directory symlinks
  326. _detail_no_push = static_cast< unsigned int >(directory_options::_detail_no_push) // internal use only
  327. }
  328. BOOST_SCOPED_ENUM_DECLARE_END(symlink_option)
  329. BOOST_BITMASK(BOOST_SCOPED_ENUM_NATIVE(symlink_option))
  330. #endif // BOOST_FILESYSTEM_NO_DEPRECATED
  331. class recursive_directory_iterator;
  332. namespace detail {
  333. struct recur_dir_itr_imp :
  334. public boost::intrusive_ref_counter< recur_dir_itr_imp >
  335. {
  336. typedef directory_iterator element_type;
  337. std::vector< element_type > m_stack;
  338. // directory_options values, declared as unsigned int for ABI compatibility
  339. unsigned int m_options;
  340. explicit recur_dir_itr_imp(unsigned int opts) BOOST_NOEXCEPT : m_options(opts) {}
  341. };
  342. BOOST_FILESYSTEM_DECL void recursive_directory_iterator_construct(recursive_directory_iterator& it, const path& dir_path, unsigned int opts, system::error_code* ec);
  343. BOOST_FILESYSTEM_DECL void recursive_directory_iterator_increment(recursive_directory_iterator& it, system::error_code* ec);
  344. BOOST_FILESYSTEM_DECL void recursive_directory_iterator_pop(recursive_directory_iterator& it, system::error_code* ec);
  345. } // namespace detail
  346. //--------------------------------------------------------------------------------------//
  347. // //
  348. // recursive_directory_iterator //
  349. // //
  350. //--------------------------------------------------------------------------------------//
  351. class recursive_directory_iterator :
  352. public boost::iterator_facade<
  353. recursive_directory_iterator,
  354. directory_entry,
  355. boost::single_pass_traversal_tag
  356. >
  357. {
  358. friend class boost::iterator_core_access;
  359. friend BOOST_FILESYSTEM_DECL void detail::recursive_directory_iterator_construct(recursive_directory_iterator& it, const path& dir_path, unsigned int opts, system::error_code* ec);
  360. friend BOOST_FILESYSTEM_DECL void detail::recursive_directory_iterator_increment(recursive_directory_iterator& it, system::error_code* ec);
  361. friend BOOST_FILESYSTEM_DECL void detail::recursive_directory_iterator_pop(recursive_directory_iterator& it, system::error_code* ec);
  362. public:
  363. recursive_directory_iterator() BOOST_NOEXCEPT {} // creates the "end" iterator
  364. explicit recursive_directory_iterator(const path& dir_path)
  365. {
  366. detail::recursive_directory_iterator_construct(*this, dir_path, static_cast< unsigned int >(directory_options::none), 0);
  367. }
  368. recursive_directory_iterator(const path& dir_path, system::error_code& ec)
  369. {
  370. detail::recursive_directory_iterator_construct(*this, dir_path, static_cast< unsigned int >(directory_options::none), &ec);
  371. }
  372. recursive_directory_iterator(const path& dir_path, BOOST_SCOPED_ENUM_NATIVE(directory_options) opts)
  373. {
  374. detail::recursive_directory_iterator_construct(*this, dir_path, static_cast< unsigned int >(opts), 0);
  375. }
  376. recursive_directory_iterator(const path& dir_path, BOOST_SCOPED_ENUM_NATIVE(directory_options) opts, system::error_code& ec)
  377. {
  378. detail::recursive_directory_iterator_construct(*this, dir_path, static_cast< unsigned int >(opts), &ec);
  379. }
  380. #if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
  381. // Deprecated constructors
  382. recursive_directory_iterator(const path& dir_path, BOOST_SCOPED_ENUM_NATIVE(symlink_option) opts)
  383. {
  384. detail::recursive_directory_iterator_construct(*this, dir_path, static_cast< unsigned int >(opts), 0);
  385. }
  386. recursive_directory_iterator(const path& dir_path, BOOST_SCOPED_ENUM_NATIVE(symlink_option) opts, system::error_code& ec) BOOST_NOEXCEPT
  387. {
  388. detail::recursive_directory_iterator_construct(*this, dir_path, static_cast< unsigned int >(opts), &ec);
  389. }
  390. #endif // BOOST_FILESYSTEM_NO_DEPRECATED
  391. BOOST_DEFAULTED_FUNCTION(recursive_directory_iterator(recursive_directory_iterator const& that), : m_imp(that.m_imp) {})
  392. BOOST_DEFAULTED_FUNCTION(recursive_directory_iterator& operator= (recursive_directory_iterator const& that), { m_imp = that.m_imp; return *this; })
  393. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  394. recursive_directory_iterator(recursive_directory_iterator&& that) BOOST_NOEXCEPT :
  395. m_imp(std::move(that.m_imp))
  396. {
  397. }
  398. recursive_directory_iterator& operator= (recursive_directory_iterator&& that) BOOST_NOEXCEPT
  399. {
  400. m_imp = std::move(that.m_imp);
  401. return *this;
  402. }
  403. #endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  404. recursive_directory_iterator& increment(system::error_code& ec) BOOST_NOEXCEPT
  405. {
  406. detail::recursive_directory_iterator_increment(*this, &ec);
  407. return *this;
  408. }
  409. int depth() const BOOST_NOEXCEPT
  410. {
  411. BOOST_ASSERT_MSG(!is_end(), "depth() on end recursive_directory_iterator");
  412. return static_cast< int >(m_imp->m_stack.size() - 1u);
  413. }
  414. bool recursion_pending() const BOOST_NOEXCEPT
  415. {
  416. BOOST_ASSERT_MSG(!is_end(), "recursion_pending() on end recursive_directory_iterator");
  417. return (m_imp->m_options & static_cast< unsigned int >(directory_options::_detail_no_push)) == 0u;
  418. }
  419. #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  420. int level() const BOOST_NOEXCEPT { return depth(); }
  421. bool no_push_pending() const BOOST_NOEXCEPT { return !recursion_pending(); }
  422. bool no_push_request() const BOOST_NOEXCEPT { return !recursion_pending(); }
  423. #endif
  424. void pop()
  425. {
  426. detail::recursive_directory_iterator_pop(*this, 0);
  427. }
  428. void pop(system::error_code& ec) BOOST_NOEXCEPT
  429. {
  430. detail::recursive_directory_iterator_pop(*this, &ec);
  431. }
  432. void disable_recursion_pending(bool value = true) BOOST_NOEXCEPT
  433. {
  434. BOOST_ASSERT_MSG(!is_end(), "disable_recursion_pending() on end recursive_directory_iterator");
  435. if (value)
  436. m_imp->m_options |= static_cast< unsigned int >(directory_options::_detail_no_push);
  437. else
  438. m_imp->m_options &= ~static_cast< unsigned int >(directory_options::_detail_no_push);
  439. }
  440. #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  441. void no_push(bool value = true) BOOST_NOEXCEPT { disable_recursion_pending(value); }
  442. #endif
  443. file_status status() const
  444. {
  445. BOOST_ASSERT_MSG(!is_end(), "status() on end recursive_directory_iterator");
  446. return m_imp->m_stack.back()->status();
  447. }
  448. file_status symlink_status() const
  449. {
  450. BOOST_ASSERT_MSG(!is_end(), "symlink_status() on end recursive_directory_iterator");
  451. return m_imp->m_stack.back()->symlink_status();
  452. }
  453. private:
  454. boost::iterator_facade<
  455. recursive_directory_iterator,
  456. directory_entry,
  457. boost::single_pass_traversal_tag
  458. >::reference dereference() const
  459. {
  460. BOOST_ASSERT_MSG(!is_end(), "dereference of end recursive_directory_iterator");
  461. return *m_imp->m_stack.back();
  462. }
  463. void increment() { detail::recursive_directory_iterator_increment(*this, 0); }
  464. bool equal(const recursive_directory_iterator& rhs) const BOOST_NOEXCEPT
  465. {
  466. return m_imp == rhs.m_imp || (is_end() && rhs.is_end());
  467. }
  468. bool is_end() const BOOST_NOEXCEPT
  469. {
  470. // Note: The check for m_stack.empty() is needed because the iterator can be copied and the copy
  471. // can be incremented to end while the original iterator still refers to the same recur_dir_itr_imp.
  472. return !m_imp || m_imp->m_stack.empty();
  473. }
  474. private:
  475. // intrusive_ptr provides the shallow-copy semantics required for single pass iterators
  476. // (i.e. InputIterators). The end iterator is indicated by is_end().
  477. boost::intrusive_ptr< detail::recur_dir_itr_imp > m_imp;
  478. };
  479. #if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
  480. typedef recursive_directory_iterator wrecursive_directory_iterator;
  481. #endif
  482. // enable recursive directory iterator C++11 range-base for statement use ----------//
  483. // begin() and end() are only used by a range-based for statement in the context of
  484. // auto - thus the top-level const is stripped - so returning const is harmless and
  485. // emphasizes begin() is just a pass through.
  486. inline const recursive_directory_iterator& begin(const recursive_directory_iterator& iter) BOOST_NOEXCEPT { return iter; }
  487. inline recursive_directory_iterator end(const recursive_directory_iterator&) BOOST_NOEXCEPT { return recursive_directory_iterator(); }
  488. // enable C++14 generic accessors for range const iterators
  489. inline const recursive_directory_iterator& cbegin(const recursive_directory_iterator& iter) BOOST_NOEXCEPT { return iter; }
  490. inline recursive_directory_iterator cend(const recursive_directory_iterator&) BOOST_NOEXCEPT { return recursive_directory_iterator(); }
  491. // enable recursive directory iterator BOOST_FOREACH -------------------------------//
  492. inline recursive_directory_iterator& range_begin(recursive_directory_iterator& iter) BOOST_NOEXCEPT { return iter; }
  493. inline recursive_directory_iterator range_begin(const recursive_directory_iterator& iter) BOOST_NOEXCEPT { return iter; }
  494. inline recursive_directory_iterator range_end(recursive_directory_iterator&) BOOST_NOEXCEPT { return recursive_directory_iterator(); }
  495. inline recursive_directory_iterator range_end(const recursive_directory_iterator&) BOOST_NOEXCEPT { return recursive_directory_iterator(); }
  496. } // namespace filesystem
  497. // namespace boost template specializations
  498. template<>
  499. struct range_mutable_iterator<boost::filesystem::recursive_directory_iterator, void>
  500. {
  501. typedef boost::filesystem::recursive_directory_iterator type;
  502. };
  503. template<>
  504. struct range_const_iterator<boost::filesystem::recursive_directory_iterator, void>
  505. {
  506. typedef boost::filesystem::recursive_directory_iterator type;
  507. };
  508. } // namespace boost
  509. #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
  510. #endif // BOOST_FILESYSTEM3_DIRECTORY_HPP