1 // boost/filesystem/operations.hpp ---------------------------------------------------//
3 // Copyright Beman Dawes 2002-2009
4 // Copyright Jan Langer 2002
5 // Copyright Dietmar Kuehl 2001
6 // Copyright Vladimir Prus 2002
8 // Distributed under the Boost Software License, Version 1.0.
9 // See http://www.boost.org/LICENSE_1_0.txt
11 // Library home page: http://www.boost.org/libs/filesystem
13 //--------------------------------------------------------------------------------------//
15 #ifndef BOOST_FILESYSTEM3_OPERATIONS_HPP
16 #define BOOST_FILESYSTEM3_OPERATIONS_HPP
18 #include <boost/config.hpp>
20 # if defined( BOOST_NO_STD_WSTRING )
21 # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support
24 #include <boost/filesystem/config.hpp>
25 #include <boost/filesystem/path.hpp>
27 #include <boost/detail/scoped_enum_emulation.hpp>
28 #include <boost/detail/bitmask.hpp>
29 #include <boost/system/error_code.hpp>
30 #include <boost/system/system_error.hpp>
31 #include <boost/shared_ptr.hpp>
32 #include <boost/utility/enable_if.hpp>
33 #include <boost/type_traits/is_same.hpp>
34 #include <boost/iterator.hpp>
35 #include <boost/cstdint.hpp>
36 #include <boost/range/mutable_iterator.hpp>
37 #include <boost/range/const_iterator.hpp>
38 #include <boost/assert.hpp>
40 #include <utility> // for pair
45 #ifdef BOOST_WINDOWS_API
49 #include <boost/config/abi_prefix.hpp> // must be the last #include
51 //--------------------------------------------------------------------------------------//
58 //--------------------------------------------------------------------------------------//
60 // class filesystem_error //
62 //--------------------------------------------------------------------------------------//
64 class BOOST_SYMBOL_VISIBLE filesystem_error : public system::system_error
66 // see http://www.boost.org/more/error_handling.html for design rationale
68 // all functions are inline to avoid issues with crossing dll boundaries
70 // functions previously throw() are now BOOST_NOEXCEPT_OR_NOTHROW
71 // functions previously without throw() are now BOOST_NOEXCEPT
74 // compiler generates copy constructor and copy assignment
77 const std::string & what_arg, system::error_code ec) BOOST_NOEXCEPT
78 : system::system_error(ec, what_arg)
82 m_imp_ptr.reset(new m_imp);
84 catch (...) { m_imp_ptr.reset(); }
88 const std::string & what_arg, const path& path1_arg,
89 system::error_code ec) BOOST_NOEXCEPT
90 : system::system_error(ec, what_arg)
94 m_imp_ptr.reset(new m_imp);
95 m_imp_ptr->m_path1 = path1_arg;
97 catch (...) { m_imp_ptr.reset(); }
101 const std::string & what_arg, const path& path1_arg,
102 const path& path2_arg, system::error_code ec) BOOST_NOEXCEPT
103 : system::system_error(ec, what_arg)
107 m_imp_ptr.reset(new m_imp);
108 m_imp_ptr->m_path1 = path1_arg;
109 m_imp_ptr->m_path2 = path2_arg;
111 catch (...) { m_imp_ptr.reset(); }
114 ~filesystem_error() BOOST_NOEXCEPT_OR_NOTHROW{}
116 const path& path1() const BOOST_NOEXCEPT
118 static const path empty_path;
119 return m_imp_ptr.get() ? m_imp_ptr->m_path1 : empty_path;
121 const path& path2() const BOOST_NOEXCEPT
123 static const path empty_path;
124 return m_imp_ptr.get() ? m_imp_ptr->m_path2 : empty_path;
127 const char* what() const BOOST_NOEXCEPT_OR_NOTHROW
129 if (!m_imp_ptr.get())
130 return system::system_error::what();
134 if (m_imp_ptr->m_what.empty())
136 m_imp_ptr->m_what = system::system_error::what();
137 if (!m_imp_ptr->m_path1.empty())
139 m_imp_ptr->m_what += ": \"";
140 m_imp_ptr->m_what += m_imp_ptr->m_path1.string();
141 m_imp_ptr->m_what += "\"";
143 if (!m_imp_ptr->m_path2.empty())
145 m_imp_ptr->m_what += ", \"";
146 m_imp_ptr->m_what += m_imp_ptr->m_path2.string();
147 m_imp_ptr->m_what += "\"";
150 return m_imp_ptr->m_what.c_str();
154 return system::system_error::what();
161 path m_path1; // may be empty()
162 path m_path2; // may be empty()
163 std::string m_what; // not built until needed
165 boost::shared_ptr<m_imp> m_imp_ptr;
168 //--------------------------------------------------------------------------------------//
170 //--------------------------------------------------------------------------------------//
175 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
176 status_unknown = status_error,
181 // the following may not apply to some operating systems or file systems
187 reparse_file, // Windows: FILE_ATTRIBUTE_REPARSE_POINT that is not a symlink
188 type_unknown, // file does exist, but isn't one of the above types or
189 // we don't have strong enough permission to find its type
191 _detail_directory_symlink // internal use only; never exposed to users
194 //--------------------------------------------------------------------------------------//
196 //--------------------------------------------------------------------------------------//
200 no_perms = 0, // file_not_found is no_perms rather than perms_not_known
202 // POSIX equivalent macros given in comments.
203 // Values are from POSIX and are given in octal per the POSIX standard.
207 owner_read = 0400, // S_IRUSR, Read permission, owner
208 owner_write = 0200, // S_IWUSR, Write permission, owner
209 owner_exe = 0100, // S_IXUSR, Execute/search permission, owner
210 owner_all = 0700, // S_IRWXU, Read, write, execute/search by owner
212 group_read = 040, // S_IRGRP, Read permission, group
213 group_write = 020, // S_IWGRP, Write permission, group
214 group_exe = 010, // S_IXGRP, Execute/search permission, group
215 group_all = 070, // S_IRWXG, Read, write, execute/search by group
217 others_read = 04, // S_IROTH, Read permission, others
218 others_write = 02, // S_IWOTH, Write permission, others
219 others_exe = 01, // S_IXOTH, Execute/search permission, others
220 others_all = 07, // S_IRWXO, Read, write, execute/search by others
222 all_all = 0777, // owner_all|group_all|others_all
226 set_uid_on_exe = 04000, // S_ISUID, Set-user-ID on execution
227 set_gid_on_exe = 02000, // S_ISGID, Set-group-ID on execution
228 sticky_bit = 01000, // S_ISVTX,
229 // (POSIX XSI) On directories, restricted deletion flag
230 // (V7) 'sticky bit': save swapped text even after use
231 // (SunOS) On non-directories: don't cache this file
232 // (SVID-v4.2) On directories: restricted deletion flag
233 // Also see http://en.wikipedia.org/wiki/Sticky_bit
235 perms_mask = 07777, // all_all|set_uid_on_exe|set_gid_on_exe|sticky_bit
237 perms_not_known = 0xFFFF, // present when directory_entry cache not loaded
239 // options for permissions() function
241 add_perms = 0x1000, // adds the given permission bits to the current bits
242 remove_perms = 0x2000, // removes the given permission bits from the current bits;
243 // choose add_perms or remove_perms, not both; if neither add_perms
244 // nor remove_perms is given, replace the current bits with
247 symlink_perms = 0x4000 // on POSIX, don't resolve symlinks; implied on Windows
252 //--------------------------------------------------------------------------------------//
254 //--------------------------------------------------------------------------------------//
256 class BOOST_FILESYSTEM_DECL file_status
259 file_status() : m_value(status_error), m_perms(perms_not_known) {}
260 explicit file_status(file_type v, perms prms = perms_not_known)
261 : m_value(v), m_perms(prms) {}
264 file_type type() const { return m_value; }
265 perms permissions() const { return m_perms; }
268 void type(file_type v) { m_value = v; }
269 void permissions(perms prms) { m_perms = prms; }
271 bool operator==(const file_status& rhs) const { return type() == rhs.type() &&
272 permissions() == rhs.permissions(); }
273 bool operator!=(const file_status& rhs) const { return !(*this == rhs); }
280 inline bool type_present(file_status f) { return f.type() != status_error; }
281 inline bool permissions_present(file_status f)
282 {return f.permissions() != perms_not_known;}
283 inline bool status_known(file_status f) { return type_present(f) && permissions_present(f); }
284 inline bool exists(file_status f) { return f.type() != status_error
285 && f.type() != file_not_found; }
286 inline bool is_regular_file(file_status f){ return f.type() == regular_file; }
287 inline bool is_directory(file_status f) { return f.type() == directory_file; }
288 inline bool is_symlink(file_status f) { return f.type() == symlink_file; }
289 inline bool is_other(file_status f) { return exists(f) && !is_regular_file(f)
290 && !is_directory(f) && !is_symlink(f); }
292 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
293 inline bool is_regular(file_status f) { return f.type() == regular_file; }
298 // all values are byte counts
299 boost::uintmax_t capacity;
300 boost::uintmax_t free; // <= capacity
301 boost::uintmax_t available; // <= free
304 BOOST_SCOPED_ENUM_START(copy_option)
305 {none=0, fail_if_exists = none, overwrite_if_exists};
306 BOOST_SCOPED_ENUM_END
308 //--------------------------------------------------------------------------------------//
309 // implementation details //
310 //--------------------------------------------------------------------------------------//
314 // We cannot pass a BOOST_SCOPED_ENUM to a compled function because it will result
315 // in an undefined reference if the library is compled with -std=c++0x but the use
316 // is compiled in C++03 mode, or visa versa. See tickets 6124, 6779, 10038.
317 enum copy_option {none=0, fail_if_exists = none, overwrite_if_exists};
319 BOOST_FILESYSTEM_DECL
320 file_status status(const path&p, system::error_code* ec=0);
321 BOOST_FILESYSTEM_DECL
322 file_status symlink_status(const path& p, system::error_code* ec=0);
323 BOOST_FILESYSTEM_DECL
324 bool is_empty(const path& p, system::error_code* ec=0);
325 BOOST_FILESYSTEM_DECL
326 path initial_path(system::error_code* ec=0);
327 BOOST_FILESYSTEM_DECL
328 path canonical(const path& p, const path& base, system::error_code* ec=0);
329 BOOST_FILESYSTEM_DECL
330 void copy(const path& from, const path& to, system::error_code* ec=0);
331 BOOST_FILESYSTEM_DECL
332 void copy_directory(const path& from, const path& to, system::error_code* ec=0);
333 BOOST_FILESYSTEM_DECL
334 void copy_file(const path& from, const path& to, // See ticket #2925
335 detail::copy_option option, system::error_code* ec=0);
336 BOOST_FILESYSTEM_DECL
337 void copy_symlink(const path& existing_symlink, const path& new_symlink, system::error_code* ec=0);
338 BOOST_FILESYSTEM_DECL
339 bool create_directories(const path& p, system::error_code* ec=0);
340 BOOST_FILESYSTEM_DECL
341 bool create_directory(const path& p, system::error_code* ec=0);
342 BOOST_FILESYSTEM_DECL
343 void create_directory_symlink(const path& to, const path& from,
344 system::error_code* ec=0);
345 BOOST_FILESYSTEM_DECL
346 void create_hard_link(const path& to, const path& from, system::error_code* ec=0);
347 BOOST_FILESYSTEM_DECL
348 void create_symlink(const path& to, const path& from, system::error_code* ec=0);
349 BOOST_FILESYSTEM_DECL
350 path current_path(system::error_code* ec=0);
351 BOOST_FILESYSTEM_DECL
352 void current_path(const path& p, system::error_code* ec=0);
353 BOOST_FILESYSTEM_DECL
354 bool equivalent(const path& p1, const path& p2, system::error_code* ec=0);
355 BOOST_FILESYSTEM_DECL
356 boost::uintmax_t file_size(const path& p, system::error_code* ec=0);
357 BOOST_FILESYSTEM_DECL
358 boost::uintmax_t hard_link_count(const path& p, system::error_code* ec=0);
359 BOOST_FILESYSTEM_DECL
360 std::time_t last_write_time(const path& p, system::error_code* ec=0);
361 BOOST_FILESYSTEM_DECL
362 void last_write_time(const path& p, const std::time_t new_time,
363 system::error_code* ec=0);
364 BOOST_FILESYSTEM_DECL
365 void permissions(const path& p, perms prms, system::error_code* ec=0);
366 BOOST_FILESYSTEM_DECL
367 path read_symlink(const path& p, system::error_code* ec=0);
368 BOOST_FILESYSTEM_DECL
369 // For standardization, if the committee doesn't like "remove", consider "eliminate"
370 bool remove(const path& p, system::error_code* ec=0);
371 BOOST_FILESYSTEM_DECL
372 boost::uintmax_t remove_all(const path& p, system::error_code* ec=0);
373 BOOST_FILESYSTEM_DECL
374 void rename(const path& old_p, const path& new_p, system::error_code* ec=0);
375 BOOST_FILESYSTEM_DECL
376 void resize_file(const path& p, uintmax_t size, system::error_code* ec=0);
377 BOOST_FILESYSTEM_DECL
378 space_info space(const path& p, system::error_code* ec=0);
379 BOOST_FILESYSTEM_DECL
380 path system_complete(const path& p, system::error_code* ec=0);
381 BOOST_FILESYSTEM_DECL
382 path temp_directory_path(system::error_code* ec=0);
383 BOOST_FILESYSTEM_DECL
384 path unique_path(const path& p, system::error_code* ec=0);
385 } // namespace detail
387 //--------------------------------------------------------------------------------------//
389 // status query functions //
391 //--------------------------------------------------------------------------------------//
394 file_status status(const path& p) {return detail::status(p);}
396 file_status status(const path& p, system::error_code& ec)
397 {return detail::status(p, &ec);}
399 file_status symlink_status(const path& p) {return detail::symlink_status(p);}
401 file_status symlink_status(const path& p, system::error_code& ec)
402 {return detail::symlink_status(p, &ec);}
404 bool exists(const path& p) {return exists(detail::status(p));}
406 bool exists(const path& p, system::error_code& ec)
407 {return exists(detail::status(p, &ec));}
409 bool is_directory(const path& p) {return is_directory(detail::status(p));}
411 bool is_directory(const path& p, system::error_code& ec)
412 {return is_directory(detail::status(p, &ec));}
414 bool is_regular_file(const path& p) {return is_regular_file(detail::status(p));}
416 bool is_regular_file(const path& p, system::error_code& ec)
417 {return is_regular_file(detail::status(p, &ec));}
419 bool is_other(const path& p) {return is_other(detail::status(p));}
421 bool is_other(const path& p, system::error_code& ec)
422 {return is_other(detail::status(p, &ec));}
424 bool is_symlink(const path& p) {return is_symlink(detail::symlink_status(p));}
426 bool is_symlink(const path& p, system::error_code& ec)
427 {return is_symlink(detail::symlink_status(p, &ec));}
428 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
430 bool is_regular(const path& p) {return is_regular(detail::status(p));}
432 bool is_regular(const path& p, system::error_code& ec)
433 {return is_regular(detail::status(p, &ec));}
437 bool is_empty(const path& p) {return detail::is_empty(p);}
439 bool is_empty(const path& p, system::error_code& ec)
440 {return detail::is_empty(p, &ec);}
442 //--------------------------------------------------------------------------------------//
444 // operational functions //
445 // in alphabetical order, unless otherwise noted //
447 //--------------------------------------------------------------------------------------//
449 // forward declarations
450 path current_path(); // fwd declaration
453 BOOST_FILESYSTEM_DECL
454 path absolute(const path& p, const path& base=current_path());
455 // If base.is_absolute(), throws nothing. Thus no need for ec argument
458 path canonical(const path& p, const path& base=current_path())
459 {return detail::canonical(p, base);}
461 path canonical(const path& p, system::error_code& ec)
462 {return detail::canonical(p, current_path(), &ec);}
464 path canonical(const path& p, const path& base, system::error_code& ec)
465 {return detail::canonical(p, base, &ec);}
467 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
469 path complete(const path& p)
471 return absolute(p, initial_path());
475 path complete(const path& p, const path& base)
477 return absolute(p, base);
482 void copy(const path& from, const path& to) {detail::copy(from, to);}
485 void copy(const path& from, const path& to, system::error_code& ec)
486 {detail::copy(from, to, &ec);}
488 void copy_directory(const path& from, const path& to)
489 {detail::copy_directory(from, to);}
491 void copy_directory(const path& from, const path& to, system::error_code& ec)
492 {detail::copy_directory(from, to, &ec);}
494 void copy_file(const path& from, const path& to, // See ticket #2925
495 BOOST_SCOPED_ENUM(copy_option) option)
497 detail::copy_file(from, to, static_cast<detail::copy_option>(option));
500 void copy_file(const path& from, const path& to)
502 detail::copy_file(from, to, detail::fail_if_exists);
505 void copy_file(const path& from, const path& to, // See ticket #2925
506 BOOST_SCOPED_ENUM(copy_option) option, system::error_code& ec)
508 detail::copy_file(from, to, static_cast<detail::copy_option>(option), &ec);
511 void copy_file(const path& from, const path& to, system::error_code& ec)
513 detail::copy_file(from, to, detail::fail_if_exists, &ec);
516 void copy_symlink(const path& existing_symlink,
517 const path& new_symlink) {detail::copy_symlink(existing_symlink, new_symlink);}
520 void copy_symlink(const path& existing_symlink, const path& new_symlink, system::error_code& ec)
521 {detail::copy_symlink(existing_symlink, new_symlink, &ec);}
523 bool create_directories(const path& p) {return detail::create_directories(p);}
526 bool create_directories(const path& p, system::error_code& ec)
527 {return detail::create_directories(p, &ec);}
529 bool create_directory(const path& p) {return detail::create_directory(p);}
532 bool create_directory(const path& p, system::error_code& ec)
533 {return detail::create_directory(p, &ec);}
535 void create_directory_symlink(const path& to, const path& from)
536 {detail::create_directory_symlink(to, from);}
538 void create_directory_symlink(const path& to, const path& from, system::error_code& ec)
539 {detail::create_directory_symlink(to, from, &ec);}
541 void create_hard_link(const path& to, const path& new_hard_link) {detail::create_hard_link(to, new_hard_link);}
544 void create_hard_link(const path& to, const path& new_hard_link, system::error_code& ec)
545 {detail::create_hard_link(to, new_hard_link, &ec);}
547 void create_symlink(const path& to, const path& new_symlink) {detail::create_symlink(to, new_symlink);}
550 void create_symlink(const path& to, const path& new_symlink, system::error_code& ec)
551 {detail::create_symlink(to, new_symlink, &ec);}
553 path current_path() {return detail::current_path();}
556 path current_path(system::error_code& ec) {return detail::current_path(&ec);}
559 void current_path(const path& p) {detail::current_path(p);}
562 void current_path(const path& p, system::error_code& ec) {detail::current_path(p, &ec);}
565 bool equivalent(const path& p1, const path& p2) {return detail::equivalent(p1, p2);}
568 bool equivalent(const path& p1, const path& p2, system::error_code& ec)
569 {return detail::equivalent(p1, p2, &ec);}
571 boost::uintmax_t file_size(const path& p) {return detail::file_size(p);}
574 boost::uintmax_t file_size(const path& p, system::error_code& ec)
575 {return detail::file_size(p, &ec);}
577 boost::uintmax_t hard_link_count(const path& p) {return detail::hard_link_count(p);}
580 boost::uintmax_t hard_link_count(const path& p, system::error_code& ec)
581 {return detail::hard_link_count(p, &ec);}
583 path initial_path() {return detail::initial_path();}
586 path initial_path(system::error_code& ec) {return detail::initial_path(&ec);}
588 template <class Path>
589 path initial_path() {return initial_path();}
590 template <class Path>
591 path initial_path(system::error_code& ec) {return detail::initial_path(&ec);}
594 std::time_t last_write_time(const path& p) {return detail::last_write_time(p);}
597 std::time_t last_write_time(const path& p, system::error_code& ec)
598 {return detail::last_write_time(p, &ec);}
600 void last_write_time(const path& p, const std::time_t new_time)
601 {detail::last_write_time(p, new_time);}
603 void last_write_time(const path& p, const std::time_t new_time, system::error_code& ec)
604 {detail::last_write_time(p, new_time, &ec);}
606 void permissions(const path& p, perms prms)
607 {detail::permissions(p, prms);}
609 void permissions(const path& p, perms prms, system::error_code& ec)
610 {detail::permissions(p, prms, &ec);}
613 path read_symlink(const path& p) {return detail::read_symlink(p);}
616 path read_symlink(const path& p, system::error_code& ec)
617 {return detail::read_symlink(p, &ec);}
619 // For standardization, if the committee doesn't like "remove", consider "eliminate"
620 bool remove(const path& p) {return detail::remove(p);}
623 bool remove(const path& p, system::error_code& ec) {return detail::remove(p, &ec);}
626 boost::uintmax_t remove_all(const path& p) {return detail::remove_all(p);}
629 boost::uintmax_t remove_all(const path& p, system::error_code& ec)
630 {return detail::remove_all(p, &ec);}
632 void rename(const path& old_p, const path& new_p) {detail::rename(old_p, new_p);}
635 void rename(const path& old_p, const path& new_p, system::error_code& ec)
636 {detail::rename(old_p, new_p, &ec);}
637 inline // name suggested by Scott McMurray
638 void resize_file(const path& p, uintmax_t size) {detail::resize_file(p, size);}
641 void resize_file(const path& p, uintmax_t size, system::error_code& ec)
642 {detail::resize_file(p, size, &ec);}
644 space_info space(const path& p) {return detail::space(p);}
647 space_info space(const path& p, system::error_code& ec) {return detail::space(p, &ec);}
649 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
650 inline bool symbolic_link_exists(const path& p)
651 { return is_symlink(symlink_status(p)); }
655 path system_complete(const path& p) {return detail::system_complete(p);}
658 path system_complete(const path& p, system::error_code& ec)
659 {return detail::system_complete(p, &ec);}
661 path temp_directory_path() {return detail::temp_directory_path();}
664 path temp_directory_path(system::error_code& ec)
665 {return detail::temp_directory_path(&ec);}
667 path unique_path(const path& p="%%%%-%%%%-%%%%-%%%%")
668 { return detail::unique_path(p); }
670 path unique_path(const path& p, system::error_code& ec)
671 { return detail::unique_path(p, &ec); }
673 //--------------------------------------------------------------------------------------//
675 // directory_entry //
677 //--------------------------------------------------------------------------------------//
679 // GCC has a problem with a member function named path within a namespace or
680 // sub-namespace that also has a class named path. The workaround is to always
681 // fully qualify the name path when it refers to the class name.
683 class BOOST_FILESYSTEM_DECL directory_entry
687 // compiler generated copy constructor, copy assignment, and destructor apply
690 explicit directory_entry(const boost::filesystem::path& p,
691 file_status st = file_status(), file_status symlink_st=file_status())
692 : m_path(p), m_status(st), m_symlink_status(symlink_st)
695 void assign(const boost::filesystem::path& p,
696 file_status st = file_status(), file_status symlink_st = file_status())
697 { m_path = p; m_status = st; m_symlink_status = symlink_st; }
699 void replace_filename(const boost::filesystem::path& p,
700 file_status st = file_status(), file_status symlink_st = file_status())
702 m_path.remove_filename();
705 m_symlink_status = symlink_st;
708 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
709 void replace_leaf(const boost::filesystem::path& p,
710 file_status st, file_status symlink_st)
711 { replace_filename(p, st, symlink_st); }
714 const boost::filesystem::path& path() const {return m_path;}
715 file_status status() const {return m_get_status();}
716 file_status status(system::error_code& ec) const {return m_get_status(&ec);}
717 file_status symlink_status() const {return m_get_symlink_status();}
718 file_status symlink_status(system::error_code& ec) const {return m_get_symlink_status(&ec);}
720 bool operator==(const directory_entry& rhs) {return m_path == rhs.m_path;}
721 bool operator!=(const directory_entry& rhs) {return m_path != rhs.m_path;}
722 bool operator< (const directory_entry& rhs) {return m_path < rhs.m_path;}
723 bool operator<=(const directory_entry& rhs) {return m_path <= rhs.m_path;}
724 bool operator> (const directory_entry& rhs) {return m_path > rhs.m_path;}
725 bool operator>=(const directory_entry& rhs) {return m_path >= rhs.m_path;}
728 boost::filesystem::path m_path;
729 mutable file_status m_status; // stat()-like
730 mutable file_status m_symlink_status; // lstat()-like
732 file_status m_get_status(system::error_code* ec=0) const;
733 file_status m_get_symlink_status(system::error_code* ec=0) const;
734 }; // directory_entry
736 //--------------------------------------------------------------------------------------//
738 // directory_iterator helpers //
740 //--------------------------------------------------------------------------------------//
742 class directory_iterator;
746 BOOST_FILESYSTEM_DECL
747 system::error_code dir_itr_close(// never throws()
749 # if defined(BOOST_POSIX_API)
756 directory_entry dir_entry;
759 # ifdef BOOST_POSIX_API
760 void* buffer; // see dir_itr_increment implementation
763 dir_itr_imp() : handle(0)
764 # ifdef BOOST_POSIX_API
769 ~dir_itr_imp() // never throws
772 # if defined(BOOST_POSIX_API)
779 // see path::iterator: comment below
780 BOOST_FILESYSTEM_DECL void directory_iterator_construct(directory_iterator& it,
781 const path& p, system::error_code* ec);
782 BOOST_FILESYSTEM_DECL void directory_iterator_increment(directory_iterator& it,
783 system::error_code* ec);
785 } // namespace detail
787 //--------------------------------------------------------------------------------------//
789 // directory_iterator //
791 //--------------------------------------------------------------------------------------//
793 class directory_iterator
794 : public boost::iterator_facade< directory_iterator,
796 boost::single_pass_traversal_tag >
800 directory_iterator(){} // creates the "end" iterator
802 // iterator_facade derived classes don't seem to like implementations in
803 // separate translation unit dll's, so forward to detail functions
804 explicit directory_iterator(const path& p)
805 : m_imp(new detail::dir_itr_imp)
806 { detail::directory_iterator_construct(*this, p, 0); }
808 directory_iterator(const path& p, system::error_code& ec)
809 : m_imp(new detail::dir_itr_imp)
810 { detail::directory_iterator_construct(*this, p, &ec); }
812 ~directory_iterator() {} // never throws
814 directory_iterator& increment(system::error_code& ec)
816 detail::directory_iterator_increment(*this, &ec);
821 friend struct detail::dir_itr_imp;
822 friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_construct(directory_iterator& it,
823 const path& p, system::error_code* ec);
824 friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_increment(directory_iterator& it,
825 system::error_code* ec);
827 // shared_ptr provides shallow-copy semantics required for InputIterators.
828 // m_imp.get()==0 indicates the end iterator.
829 boost::shared_ptr< detail::dir_itr_imp > m_imp;
831 friend class boost::iterator_core_access;
833 boost::iterator_facade<
836 boost::single_pass_traversal_tag >::reference dereference() const
838 BOOST_ASSERT_MSG(m_imp.get(), "attempt to dereference end iterator");
839 return m_imp->dir_entry;
842 void increment() { detail::directory_iterator_increment(*this, 0); }
844 bool equal(const directory_iterator& rhs) const
845 { return m_imp == rhs.m_imp; }
848 // enable C++11 range-base for statement use ---------------------------------------//
850 // begin() and end() are only used by a range-based for statement in the context of
851 // auto - thus the top-level const is stripped - so returning const is harmless and
852 // emphasizes begin() is just a pass through.
854 const directory_iterator& begin(const directory_iterator& iter) {return iter;}
856 directory_iterator end(const directory_iterator&) {return directory_iterator();}
858 // enable BOOST_FOREACH ------------------------------------------------------------//
861 directory_iterator& range_begin(directory_iterator& iter) {return iter;}
863 directory_iterator range_begin(const directory_iterator& iter) {return iter;}
865 directory_iterator range_end(const directory_iterator&) {return directory_iterator();}
866 } // namespace filesystem
868 // namespace boost template specializations
870 struct range_mutable_iterator<boost::filesystem::directory_iterator>
871 { typedef boost::filesystem::directory_iterator type; };
873 struct range_const_iterator <boost::filesystem::directory_iterator>
874 { typedef boost::filesystem::directory_iterator type; };
879 //--------------------------------------------------------------------------------------//
881 // recursive_directory_iterator helpers //
883 //--------------------------------------------------------------------------------------//
885 BOOST_SCOPED_ENUM_START(symlink_option)
888 no_recurse = none, // don't follow directory symlinks (default behavior)
889 recurse, // follow directory symlinks
890 _detail_no_push = recurse << 1 // internal use only
892 BOOST_SCOPED_ENUM_END
894 BOOST_BITMASK(BOOST_SCOPED_ENUM(symlink_option))
898 struct recur_dir_itr_imp
900 typedef directory_iterator element_type;
901 std::stack< element_type, std::vector< element_type > > m_stack;
903 BOOST_SCOPED_ENUM(symlink_option) m_options;
905 recur_dir_itr_imp() : m_level(0), m_options(symlink_option::none) {}
907 void increment(system::error_code* ec); // ec == 0 means throw on error
909 bool push_directory(system::error_code& ec) BOOST_NOEXCEPT;
915 // Implementation is inline to avoid dynamic linking difficulties with m_stack:
916 // Microsoft warning C4251, m_stack needs to have dll-interface to be used by
917 // clients of struct 'boost::filesystem::detail::recur_dir_itr_imp'
920 bool recur_dir_itr_imp::push_directory(system::error_code& ec) BOOST_NOEXCEPT
921 // Returns: true if push occurs, otherwise false. Always returns false on error.
925 // Discover if the iterator is for a directory that needs to be recursed into,
926 // taking symlinks and options into account.
928 if ((m_options & symlink_option::_detail_no_push) == symlink_option::_detail_no_push)
929 m_options &= ~symlink_option::_detail_no_push;
933 // Logic for following predicate was contributed by Daniel Aarno to handle cyclic
934 // symlinks correctly and efficiently, fixing ticket #5652.
935 // if (((m_options & symlink_option::recurse) == symlink_option::recurse
936 // || !is_symlink(m_stack.top()->symlink_status()))
937 // && is_directory(m_stack.top()->status())) ...
938 // The predicate code has since been rewritten to pass error_code arguments,
941 file_status symlink_stat;
943 if ((m_options & symlink_option::recurse) != symlink_option::recurse)
945 symlink_stat = m_stack.top()->symlink_status(ec);
950 if ((m_options & symlink_option::recurse) == symlink_option::recurse
951 || !is_symlink(symlink_stat))
953 file_status stat = m_stack.top()->status(ec);
954 if (ec || !is_directory(stat))
957 directory_iterator next(m_stack.top()->path(), ec);
958 if (!ec && next != directory_iterator())
970 void recur_dir_itr_imp::increment(system::error_code* ec)
971 // ec == 0 means throw on error
973 // Invariant: On return, the top of the iterator stack is the next valid (possibly
974 // end) iterator, regardless of whether or not an error is reported, and regardless of
975 // whether any error is reported by exception or error code. In other words, progress
976 // is always made so a loop on the iterator will always eventually terminate
977 // regardless of errors.
979 system::error_code ec_push_directory;
981 // if various conditions are met, push a directory_iterator into the iterator stack
982 if (push_directory(ec_push_directory))
989 // Do the actual increment operation on the top iterator in the iterator
990 // stack, popping the stack if necessary, until either the stack is empty or a
991 // non-end iterator is reached.
992 while (!m_stack.empty() && ++m_stack.top() == directory_iterator())
998 // report errors if any
999 if (ec_push_directory)
1002 *ec = ec_push_directory;
1005 BOOST_FILESYSTEM_THROW(filesystem_error(
1006 "filesystem::recursive_directory_iterator directory error",
1007 ec_push_directory));
1015 void recur_dir_itr_imp::pop()
1017 BOOST_ASSERT_MSG(m_level > 0,
1018 "pop() on recursive_directory_iterator with level < 1");
1025 while (!m_stack.empty() && ++m_stack.top() == directory_iterator());
1027 } // namespace detail
1029 //--------------------------------------------------------------------------------------//
1031 // recursive_directory_iterator //
1033 //--------------------------------------------------------------------------------------//
1035 class recursive_directory_iterator
1036 : public boost::iterator_facade<
1037 recursive_directory_iterator,
1039 boost::single_pass_traversal_tag >
1043 recursive_directory_iterator(){} // creates the "end" iterator
1045 explicit recursive_directory_iterator(const path& dir_path,
1046 BOOST_SCOPED_ENUM(symlink_option) opt = symlink_option::none)
1047 : m_imp(new detail::recur_dir_itr_imp)
1049 m_imp->m_options = opt;
1050 m_imp->m_stack.push(directory_iterator(dir_path));
1051 if (m_imp->m_stack.top() == directory_iterator())
1055 recursive_directory_iterator(const path& dir_path,
1056 BOOST_SCOPED_ENUM(symlink_option) opt,
1057 system::error_code & ec)
1058 : m_imp(new detail::recur_dir_itr_imp)
1060 m_imp->m_options = opt;
1061 m_imp->m_stack.push(directory_iterator(dir_path, ec));
1062 if (m_imp->m_stack.top() == directory_iterator())
1066 recursive_directory_iterator(const path& dir_path,
1067 system::error_code & ec)
1068 : m_imp(new detail::recur_dir_itr_imp)
1070 m_imp->m_options = symlink_option::none;
1071 m_imp->m_stack.push(directory_iterator(dir_path, ec));
1072 if (m_imp->m_stack.top() == directory_iterator())
1076 recursive_directory_iterator& increment(system::error_code& ec)
1078 BOOST_ASSERT_MSG(m_imp.get(),
1079 "increment() on end recursive_directory_iterator");
1080 m_imp->increment(&ec);
1081 if (m_imp->m_stack.empty())
1082 m_imp.reset(); // done, so make end iterator
1088 BOOST_ASSERT_MSG(m_imp.get(),
1089 "level() on end recursive_directory_iterator");
1090 return m_imp->m_level;
1093 bool no_push_pending() const
1095 BOOST_ASSERT_MSG(m_imp.get(),
1096 "is_no_push_requested() on end recursive_directory_iterator");
1097 return (m_imp->m_options & symlink_option::_detail_no_push)
1098 == symlink_option::_detail_no_push;
1101 # ifndef BOOST_FILESYSTEM_NO_DEPRECATED
1102 bool no_push_request() const { return no_push_pending(); }
1107 BOOST_ASSERT_MSG(m_imp.get(),
1108 "pop() on end recursive_directory_iterator");
1110 if (m_imp->m_stack.empty()) m_imp.reset(); // done, so make end iterator
1113 void no_push(bool value=true)
1115 BOOST_ASSERT_MSG(m_imp.get(),
1116 "no_push() on end recursive_directory_iterator");
1118 m_imp->m_options |= symlink_option::_detail_no_push;
1120 m_imp->m_options &= ~symlink_option::_detail_no_push;
1123 file_status status() const
1125 BOOST_ASSERT_MSG(m_imp.get(),
1126 "status() on end recursive_directory_iterator");
1127 return m_imp->m_stack.top()->status();
1130 file_status symlink_status() const
1132 BOOST_ASSERT_MSG(m_imp.get(),
1133 "symlink_status() on end recursive_directory_iterator");
1134 return m_imp->m_stack.top()->symlink_status();
1139 // shared_ptr provides shallow-copy semantics required for InputIterators.
1140 // m_imp.get()==0 indicates the end iterator.
1141 boost::shared_ptr< detail::recur_dir_itr_imp > m_imp;
1143 friend class boost::iterator_core_access;
1145 boost::iterator_facade<
1146 recursive_directory_iterator,
1148 boost::single_pass_traversal_tag >::reference
1151 BOOST_ASSERT_MSG(m_imp.get(),
1152 "dereference of end recursive_directory_iterator");
1153 return *m_imp->m_stack.top();
1158 BOOST_ASSERT_MSG(m_imp.get(),
1159 "increment of end recursive_directory_iterator");
1160 m_imp->increment(0);
1161 if (m_imp->m_stack.empty())
1162 m_imp.reset(); // done, so make end iterator
1165 bool equal(const recursive_directory_iterator& rhs) const
1166 { return m_imp == rhs.m_imp; }
1170 // enable C++11 range-base for statement use ---------------------------------------//
1172 // begin() and end() are only used by a range-based for statement in the context of
1173 // auto - thus the top-level const is stripped - so returning const is harmless and
1174 // emphasizes begin() is just a pass through.
1176 const recursive_directory_iterator& begin(const recursive_directory_iterator& iter)
1179 recursive_directory_iterator end(const recursive_directory_iterator&)
1180 {return recursive_directory_iterator();}
1182 // enable BOOST_FOREACH ------------------------------------------------------------//
1185 recursive_directory_iterator& range_begin(recursive_directory_iterator& iter)
1188 recursive_directory_iterator range_begin(const recursive_directory_iterator& iter)
1191 recursive_directory_iterator range_end(const recursive_directory_iterator&)
1192 {return recursive_directory_iterator();}
1193 } // namespace filesystem
1195 // namespace boost template specializations
1197 struct range_mutable_iterator<boost::filesystem::recursive_directory_iterator>
1198 { typedef boost::filesystem::recursive_directory_iterator type; };
1200 struct range_const_iterator <boost::filesystem::recursive_directory_iterator>
1201 { typedef boost::filesystem::recursive_directory_iterator type; };
1203 namespace filesystem
1206 # if !defined(BOOST_FILESYSTEM_NO_DEPRECATED)
1207 typedef recursive_directory_iterator wrecursive_directory_iterator;
1210 // test helper -----------------------------------------------------------------------//
1212 // Not part of the documented interface since false positives are possible;
1213 // there is no law that says that an OS that has large stat.st_size
1214 // actually supports large file sizes.
1218 BOOST_FILESYSTEM_DECL bool possible_large_file_size_support();
1221 } // namespace filesystem
1222 } // namespace boost
1224 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
1225 #endif // BOOST_FILESYSTEM3_OPERATIONS_HPP