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