diff options
Diffstat (limited to 'boost/filesystem/operations.hpp')
-rw-r--r-- | boost/filesystem/operations.hpp | 682 |
1 files changed, 366 insertions, 316 deletions
diff --git a/boost/filesystem/operations.hpp b/boost/filesystem/operations.hpp index 640d288882..74e47edbb8 100644 --- a/boost/filesystem/operations.hpp +++ b/boost/filesystem/operations.hpp @@ -2,9 +2,9 @@ // Copyright Beman Dawes 2002-2009 // Copyright Jan Langer 2002 -// Copyright Dietmar Kuehl 2001 +// Copyright Dietmar Kuehl 2001 // Copyright Vladimir Prus 2002 - + // Distributed under the Boost Software License, Version 1.0. // See http://www.boost.org/LICENSE_1_0.txt @@ -24,20 +24,21 @@ #include <boost/filesystem/config.hpp> #include <boost/filesystem/path.hpp> -#include <boost/detail/scoped_enum_emulation.hpp> +#include <boost/core/scoped_enum.hpp> #include <boost/detail/bitmask.hpp> #include <boost/system/error_code.hpp> #include <boost/system/system_error.hpp> -#include <boost/shared_ptr.hpp> -#include <boost/utility/enable_if.hpp> +#include <boost/smart_ptr/intrusive_ptr.hpp> +#include <boost/smart_ptr/intrusive_ref_counter.hpp> #include <boost/type_traits/is_same.hpp> #include <boost/cstdint.hpp> #include <boost/assert.hpp> #include <string> -#include <utility> // for pair +#include <utility> // std::move #include <ctime> #include <vector> #include <stack> +#include <new> // std::nothrow, std::bad_alloc #ifdef BOOST_WINDOWS_API # include <fstream> @@ -58,7 +59,8 @@ namespace boost // // //--------------------------------------------------------------------------------------// - class BOOST_SYMBOL_VISIBLE filesystem_error : public system::system_error + class BOOST_SYMBOL_VISIBLE filesystem_error : + public system::system_error { // see http://www.boost.org/more/error_handling.html for design rationale @@ -71,60 +73,57 @@ namespace boost // compiler generates copy constructor and copy assignment filesystem_error( - const std::string & what_arg, system::error_code ec) BOOST_NOEXCEPT - : system::system_error(ec, what_arg) + const std::string & what_arg, system::error_code ec) BOOST_NOEXCEPT : + system::system_error(ec, what_arg) { try { - m_imp_ptr.reset(new m_imp); + m_imp_ptr.reset(new impl()); } catch (...) { m_imp_ptr.reset(); } } filesystem_error( const std::string & what_arg, const path& path1_arg, - system::error_code ec) BOOST_NOEXCEPT - : system::system_error(ec, what_arg) + system::error_code ec) BOOST_NOEXCEPT : + system::system_error(ec, what_arg) { try { - m_imp_ptr.reset(new m_imp); - m_imp_ptr->m_path1 = path1_arg; + m_imp_ptr.reset(new impl(path1_arg)); } catch (...) { m_imp_ptr.reset(); } } filesystem_error( const std::string & what_arg, const path& path1_arg, - const path& path2_arg, system::error_code ec) BOOST_NOEXCEPT - : system::system_error(ec, what_arg) + const path& path2_arg, system::error_code ec) BOOST_NOEXCEPT : + system::system_error(ec, what_arg) { try { - m_imp_ptr.reset(new m_imp); - m_imp_ptr->m_path1 = path1_arg; - m_imp_ptr->m_path2 = path2_arg; + m_imp_ptr.reset(new impl(path1_arg, path2_arg)); } catch (...) { m_imp_ptr.reset(); } } - ~filesystem_error() BOOST_NOEXCEPT_OR_NOTHROW{} + ~filesystem_error() BOOST_NOEXCEPT_OR_NOTHROW {} - const path& path1() const BOOST_NOEXCEPT + const path& path1() const BOOST_NOEXCEPT { static const path empty_path; return m_imp_ptr.get() ? m_imp_ptr->m_path1 : empty_path; } - const path& path2() const BOOST_NOEXCEPT + const path& path2() const BOOST_NOEXCEPT { static const path empty_path; return m_imp_ptr.get() ? m_imp_ptr->m_path2 : empty_path; } - const char* what() const BOOST_NOEXCEPT_OR_NOTHROW + const char* what() const BOOST_NOEXCEPT_OR_NOTHROW { if (!m_imp_ptr.get()) - return system::system_error::what(); + return system::system_error::what(); try { @@ -153,13 +152,18 @@ namespace boost } private: - struct m_imp + struct impl : + public boost::intrusive_ref_counter< impl > { path m_path1; // may be empty() path m_path2; // may be empty() std::string m_what; // not built until needed + + BOOST_DEFAULTED_FUNCTION(impl(), {}) + explicit impl(path const& path1) : m_path1(path1) {} + impl(path const& path1, path const& path2) : m_path1(path1), m_path2(path2) {} }; - boost::shared_ptr<m_imp> m_imp_ptr; + boost::intrusive_ptr< impl > m_imp_ptr; }; //--------------------------------------------------------------------------------------// @@ -167,7 +171,7 @@ namespace boost //--------------------------------------------------------------------------------------// enum file_type - { + { status_error, # ifndef BOOST_FILESYSTEM_NO_DEPRECATED status_unknown = status_error, @@ -200,7 +204,7 @@ namespace boost // Values are from POSIX and are given in octal per the POSIX standard. // permission bits - + owner_read = 0400, // S_IRUSR, Read permission, owner owner_write = 0200, // S_IWUSR, Write permission, owner owner_exe = 0100, // S_IXUSR, Execute/search permission, owner @@ -223,8 +227,8 @@ namespace boost set_uid_on_exe = 04000, // S_ISUID, Set-user-ID on execution set_gid_on_exe = 02000, // S_ISGID, Set-group-ID on execution sticky_bit = 01000, // S_ISVTX, - // (POSIX XSI) On directories, restricted deletion flag - // (V7) 'sticky bit': save swapped text even after use + // (POSIX XSI) On directories, restricted deletion flag + // (V7) 'sticky bit': save swapped text even after use // (SunOS) On non-directories: don't cache this file // (SVID-v4.2) On directories: restricted deletion flag // Also see http://en.wikipedia.org/wiki/Sticky_bit @@ -254,23 +258,31 @@ namespace boost // file_status // //--------------------------------------------------------------------------------------// - class BOOST_FILESYSTEM_DECL file_status + class file_status { public: - file_status() BOOST_NOEXCEPT - : m_value(status_error), m_perms(perms_not_known) {} - explicit file_status(file_type v) BOOST_NOEXCEPT - : m_value(v), m_perms(perms_not_known) {} - file_status(file_type v, perms prms) BOOST_NOEXCEPT - : m_value(v), m_perms(prms) {} + BOOST_CONSTEXPR file_status() BOOST_NOEXCEPT : + m_value(status_error), m_perms(perms_not_known) + { + } + explicit BOOST_CONSTEXPR file_status(file_type v) BOOST_NOEXCEPT : + m_value(v), m_perms(perms_not_known) + { + } + BOOST_CONSTEXPR file_status(file_type v, perms prms) BOOST_NOEXCEPT : + m_value(v), m_perms(prms) + { + } - // As of October 2015 the interaction between noexcept and =default is so troublesome - // for VC++, GCC, and probably other compilers, that =default is not used with noexcept - // functions. GCC is not even consistent for the same release on different platforms. + // As of October 2015 the interaction between noexcept and =default is so troublesome + // for VC++, GCC, and probably other compilers, that =default is not used with noexcept + // functions. GCC is not even consistent for the same release on different platforms. - file_status(const file_status& rhs) BOOST_NOEXCEPT - : m_value(rhs.m_value), m_perms(rhs.m_perms) {} - file_status& operator=(const file_status& rhs) BOOST_NOEXCEPT + BOOST_CONSTEXPR file_status(const file_status& rhs) BOOST_NOEXCEPT : + m_value(rhs.m_value), m_perms(rhs.m_perms) + { + } + BOOST_CXX14_CONSTEXPR file_status& operator=(const file_status& rhs) BOOST_NOEXCEPT { m_value = rhs.m_value; m_perms = rhs.m_perms; @@ -278,57 +290,60 @@ namespace boost } # if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) - file_status(file_status&& rhs) BOOST_NOEXCEPT + // Note: std::move is not constexpr in C++11, that's why we're not using it here + BOOST_CONSTEXPR file_status(file_status&& rhs) BOOST_NOEXCEPT : + m_value(static_cast< file_type&& >(rhs.m_value)), m_perms(static_cast< enum perms&& >(rhs.m_perms)) { - m_value = std::move(rhs.m_value); - m_perms = std::move(rhs.m_perms); } - file_status& operator=(file_status&& rhs) BOOST_NOEXCEPT - { + BOOST_CXX14_CONSTEXPR file_status& operator=(file_status&& rhs) BOOST_NOEXCEPT + { m_value = std::move(rhs.m_value); m_perms = std::move(rhs.m_perms); return *this; } # endif - // observers - file_type type() const BOOST_NOEXCEPT { return m_value; } - perms permissions() const BOOST_NOEXCEPT { return m_perms; } + BOOST_CONSTEXPR file_type type() const BOOST_NOEXCEPT { return m_value; } + BOOST_CONSTEXPR perms permissions() const BOOST_NOEXCEPT { return m_perms; } // modifiers - void type(file_type v) BOOST_NOEXCEPT { m_value = v; } - void permissions(perms prms) BOOST_NOEXCEPT { m_perms = prms; } + BOOST_CXX14_CONSTEXPR void type(file_type v) BOOST_NOEXCEPT { m_value = v; } + BOOST_CXX14_CONSTEXPR void permissions(perms prms) BOOST_NOEXCEPT { m_perms = prms; } - bool operator==(const file_status& rhs) const BOOST_NOEXCEPT - { return type() == rhs.type() && - permissions() == rhs.permissions(); } - bool operator!=(const file_status& rhs) const BOOST_NOEXCEPT - { return !(*this == rhs); } + BOOST_CONSTEXPR bool operator==(const file_status& rhs) const BOOST_NOEXCEPT + { + return type() == rhs.type() && + permissions() == rhs.permissions(); + } + BOOST_CONSTEXPR bool operator!=(const file_status& rhs) const BOOST_NOEXCEPT + { + return !(*this == rhs); + } private: file_type m_value; enum perms m_perms; }; - inline bool type_present(file_status f) BOOST_NOEXCEPT + inline BOOST_CONSTEXPR bool type_present(file_status f) BOOST_NOEXCEPT { return f.type() != status_error; } - inline bool permissions_present(file_status f) BOOST_NOEXCEPT + inline BOOST_CONSTEXPR bool permissions_present(file_status f) BOOST_NOEXCEPT {return f.permissions() != perms_not_known;} - inline bool status_known(file_status f) BOOST_NOEXCEPT - { return type_present(f) && permissions_present(f); } - inline bool exists(file_status f) BOOST_NOEXCEPT + inline BOOST_CONSTEXPR bool status_known(file_status f) BOOST_NOEXCEPT + { return filesystem::type_present(f) && filesystem::permissions_present(f); } + inline BOOST_CONSTEXPR bool exists(file_status f) BOOST_NOEXCEPT { return f.type() != status_error && f.type() != file_not_found; } - inline bool is_regular_file(file_status f) BOOST_NOEXCEPT + inline BOOST_CONSTEXPR bool is_regular_file(file_status f) BOOST_NOEXCEPT { return f.type() == regular_file; } - inline bool is_directory(file_status f) BOOST_NOEXCEPT + inline BOOST_CONSTEXPR bool is_directory(file_status f) BOOST_NOEXCEPT { return f.type() == directory_file; } - inline bool is_symlink(file_status f) BOOST_NOEXCEPT + inline BOOST_CONSTEXPR bool is_symlink(file_status f) BOOST_NOEXCEPT { return f.type() == symlink_file; } - inline bool is_other(file_status f) BOOST_NOEXCEPT - { return exists(f) && !is_regular_file(f) - && !is_directory(f) && !is_symlink(f); } + inline BOOST_CONSTEXPR bool is_other(file_status f) BOOST_NOEXCEPT + { return filesystem::exists(f) && !filesystem::is_regular_file(f) + && !filesystem::is_directory(f) && !filesystem::is_symlink(f); } # ifndef BOOST_FILESYSTEM_NO_DEPRECATED inline bool is_regular(file_status f) BOOST_NOEXCEPT { return f.type() == regular_file; } @@ -342,9 +357,9 @@ namespace boost boost::uintmax_t available; // <= free }; - BOOST_SCOPED_ENUM_START(copy_option) - {none=0, fail_if_exists = none, overwrite_if_exists}; - BOOST_SCOPED_ENUM_END + BOOST_SCOPED_ENUM_DECLARE_BEGIN(copy_option) + {none=0, fail_if_exists = none, overwrite_if_exists} + BOOST_SCOPED_ENUM_DECLARE_END(copy_option) //--------------------------------------------------------------------------------------// // implementation details // @@ -354,7 +369,7 @@ namespace boost { // We cannot pass a BOOST_SCOPED_ENUM to a compled function because it will result // in an undefined reference if the library is compled with -std=c++0x but the use - // is compiled in C++03 mode, or visa versa. See tickets 6124, 6779, 10038. + // is compiled in C++03 mode, or vice versa. See tickets 6124, 6779, 10038. enum copy_option {none=0, fail_if_exists = none, overwrite_if_exists}; BOOST_FILESYSTEM_DECL @@ -417,7 +432,7 @@ namespace boost BOOST_FILESYSTEM_DECL void resize_file(const path& p, uintmax_t size, system::error_code* ec=0); BOOST_FILESYSTEM_DECL - space_info space(const path& p, system::error_code* ec=0); + space_info space(const path& p, system::error_code* ec=0); BOOST_FILESYSTEM_DECL path system_complete(const path& p, system::error_code* ec=0); BOOST_FILESYSTEM_DECL @@ -426,7 +441,7 @@ namespace boost path unique_path(const path& p, system::error_code* ec=0); BOOST_FILESYSTEM_DECL path weakly_canonical(const path& p, system::error_code* ec = 0); - } // namespace detail + } // namespace detail //--------------------------------------------------------------------------------------// // // @@ -436,37 +451,37 @@ namespace boost inline file_status status(const path& p) {return detail::status(p);} - inline + inline file_status status(const path& p, system::error_code& ec) {return detail::status(p, &ec);} - inline + inline file_status symlink_status(const path& p) {return detail::symlink_status(p);} inline file_status symlink_status(const path& p, system::error_code& ec) {return detail::symlink_status(p, &ec);} - inline + inline bool exists(const path& p) {return exists(detail::status(p));} - inline + inline bool exists(const path& p, system::error_code& ec) {return exists(detail::status(p, &ec));} - inline + inline bool is_directory(const path& p) {return is_directory(detail::status(p));} - inline + inline bool is_directory(const path& p, system::error_code& ec) {return is_directory(detail::status(p, &ec));} - inline + inline bool is_regular_file(const path& p) {return is_regular_file(detail::status(p));} - inline + inline bool is_regular_file(const path& p, system::error_code& ec) {return is_regular_file(detail::status(p, &ec));} - inline + inline bool is_other(const path& p) {return is_other(detail::status(p));} - inline + inline bool is_other(const path& p, system::error_code& ec) {return is_other(detail::status(p, &ec));} inline bool is_symlink(const path& p) {return is_symlink(detail::symlink_status(p));} - inline + inline bool is_symlink(const path& p, system::error_code& ec) {return is_symlink(detail::symlink_status(p, &ec));} # ifndef BOOST_FILESYSTEM_NO_DEPRECATED @@ -489,7 +504,7 @@ namespace boost // in alphabetical order, unless otherwise noted // // // //--------------------------------------------------------------------------------------// - + // forward declarations path current_path(); // fwd declaration path initial_path(); @@ -526,7 +541,7 @@ namespace boost void copy(const path& from, const path& to) {detail::copy(from, to);} inline - void copy(const path& from, const path& to, system::error_code& ec) BOOST_NOEXCEPT + void copy(const path& from, const path& to, system::error_code& ec) BOOST_NOEXCEPT {detail::copy(from, to, &ec);} inline void copy_directory(const path& from, const path& to) @@ -536,7 +551,7 @@ namespace boost {detail::copy_directory(from, to, &ec);} inline void copy_file(const path& from, const path& to, // See ticket #2925 - BOOST_SCOPED_ENUM(copy_option) option) + BOOST_SCOPED_ENUM_NATIVE(copy_option) option) { detail::copy_file(from, to, static_cast<detail::copy_option>(option)); } @@ -547,7 +562,7 @@ namespace boost } inline void copy_file(const path& from, const path& to, // See ticket #2925 - BOOST_SCOPED_ENUM(copy_option) option, system::error_code& ec) BOOST_NOEXCEPT + BOOST_SCOPED_ENUM_NATIVE(copy_option) option, system::error_code& ec) BOOST_NOEXCEPT { detail::copy_file(from, to, static_cast<detail::copy_option>(option), &ec); } @@ -671,7 +686,7 @@ namespace boost inline boost::uintmax_t remove_all(const path& p) {return detail::remove_all(p);} - + inline boost::uintmax_t remove_all(const path& p, system::error_code& ec) BOOST_NOEXCEPT {return detail::remove_all(p, &ec);} @@ -697,15 +712,15 @@ namespace boost path relative(const path& p, const path& base, system::error_code& ec) {return detail::relative(p, base, &ec);} inline - space_info space(const path& p) {return detail::space(p);} + space_info space(const path& p) {return detail::space(p);} inline space_info space(const path& p, system::error_code& ec) BOOST_NOEXCEPT - {return detail::space(p, &ec);} + {return detail::space(p, &ec);} # ifndef BOOST_FILESYSTEM_NO_DEPRECATED inline bool symbolic_link_exists(const path& p) - { return is_symlink(symlink_status(p)); } + { return is_symlink(filesystem::symlink_status(p)); } # endif inline @@ -718,7 +733,7 @@ namespace boost path temp_directory_path() {return detail::temp_directory_path();} inline - path temp_directory_path(system::error_code& ec) + path temp_directory_path(system::error_code& ec) {return detail::temp_directory_path(&ec);} inline path unique_path(const path& p="%%%%-%%%%-%%%%-%%%%") @@ -728,7 +743,7 @@ namespace boost {return detail::unique_path(p, &ec);} inline path weakly_canonical(const path& p) {return detail::weakly_canonical(p);} - + inline path weakly_canonical(const path& p, system::error_code& ec) {return detail::weakly_canonical(p, &ec);} @@ -739,25 +754,30 @@ namespace boost // // //--------------------------------------------------------------------------------------// -// GCC has a problem with a member function named path within a namespace or +// GCC has a problem with a member function named path within a namespace or // sub-namespace that also has a class named path. The workaround is to always // fully qualify the name path when it refers to the class name. -class BOOST_FILESYSTEM_DECL directory_entry +class directory_entry { public: typedef boost::filesystem::path::value_type value_type; // enables class path ctor taking directory_entry directory_entry() BOOST_NOEXCEPT {} - explicit directory_entry(const boost::filesystem::path& p) - : m_path(p), m_status(file_status()), m_symlink_status(file_status()) - {} + explicit directory_entry(const boost::filesystem::path& p) : + m_path(p), m_status(file_status()), m_symlink_status(file_status()) + { + } directory_entry(const boost::filesystem::path& p, - file_status st, file_status symlink_st = file_status()) - : m_path(p), m_status(st), m_symlink_status(symlink_st) {} + file_status st, file_status symlink_st = file_status()) : + m_path(p), m_status(st), m_symlink_status(symlink_st) + { + } - directory_entry(const directory_entry& rhs) - : m_path(rhs.m_path), m_status(rhs.m_status), m_symlink_status(rhs.m_symlink_status){} + directory_entry(const directory_entry& rhs) : + m_path(rhs.m_path), m_status(rhs.m_status), m_symlink_status(rhs.m_symlink_status) + { + } directory_entry& operator=(const directory_entry& rhs) { @@ -772,14 +792,12 @@ public: // functions. GCC is not even consistent for the same release on different platforms. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) - directory_entry(directory_entry&& rhs) BOOST_NOEXCEPT + directory_entry(directory_entry&& rhs) BOOST_NOEXCEPT : + m_path(std::move(rhs.m_path)), m_status(std::move(rhs.m_status)), m_symlink_status(std::move(rhs.m_symlink_status)) { - m_path = std::move(rhs.m_path); - m_status = std::move(rhs.m_status); - m_symlink_status = std::move(rhs.m_symlink_status); } directory_entry& operator=(directory_entry&& rhs) BOOST_NOEXCEPT - { + { m_path = std::move(rhs.m_path); m_status = std::move(rhs.m_status); m_symlink_status = std::move(rhs.m_symlink_status); @@ -789,7 +807,11 @@ public: void assign(const boost::filesystem::path& p, file_status st = file_status(), file_status symlink_st = file_status()) - { m_path = p; m_status = st; m_symlink_status = symlink_st; } + { + m_path = p; + m_status = st; + m_symlink_status = symlink_st; + } void replace_filename(const boost::filesystem::path& p, file_status st = file_status(), file_status symlink_st = file_status()) @@ -817,21 +839,47 @@ public: {return m_get_symlink_status(&ec); } bool operator==(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path == rhs.m_path; } - bool operator!=(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path != rhs.m_path;} - bool operator< (const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path < rhs.m_path;} - bool operator<=(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path <= rhs.m_path;} - bool operator> (const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path > rhs.m_path;} - bool operator>=(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path >= rhs.m_path;} + bool operator!=(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path != rhs.m_path;} + bool operator< (const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path < rhs.m_path;} + bool operator<=(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path <= rhs.m_path;} + bool operator> (const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path > rhs.m_path;} + bool operator>=(const directory_entry& rhs) const BOOST_NOEXCEPT {return m_path >= rhs.m_path;} + +private: + BOOST_FILESYSTEM_DECL file_status m_get_status(system::error_code* ec=0) const; + BOOST_FILESYSTEM_DECL file_status m_get_symlink_status(system::error_code* ec=0) const; private: boost::filesystem::path m_path; mutable file_status m_status; // stat()-like mutable file_status m_symlink_status; // lstat()-like - - file_status m_get_status(system::error_code* ec=0) const; - file_status m_get_symlink_status(system::error_code* ec=0) const; }; // directory_entry + +//--------------------------------------------------------------------------------------// +// // +// directory_entry overloads // +// // +//--------------------------------------------------------------------------------------// + +// Without these functions, calling (for example) 'is_directory' with a 'directory_entry' results in: +// - a conversion to 'path' using 'operator const boost::filesystem::path&()', +// - then a call to 'is_directory(const path& p)' which recomputes the status with 'detail::status(p)'. +// +// These functions avoid a costly recomputation of the status if one calls 'is_directory(e)' instead of 'is_directory(e.status)' + +inline file_status status (const directory_entry& e) BOOST_NOEXCEPT { return e.status(); } +inline bool type_present (const directory_entry& e) BOOST_NOEXCEPT { return filesystem::type_present(e.status()); } +inline bool status_known (const directory_entry& e) BOOST_NOEXCEPT { return filesystem::status_known(e.status()); } +inline bool exists (const directory_entry& e) BOOST_NOEXCEPT { return filesystem::exists(e.status()); } +inline bool is_regular_file(const directory_entry& e) BOOST_NOEXCEPT { return filesystem::is_regular_file(e.status()); } +inline bool is_directory (const directory_entry& e) BOOST_NOEXCEPT { return filesystem::is_directory(e.status()); } +inline bool is_symlink (const directory_entry& e) BOOST_NOEXCEPT { return filesystem::is_symlink(e.status()); } +inline bool is_other (const directory_entry& e) BOOST_NOEXCEPT { return filesystem::is_other(e.status()); } +#ifndef BOOST_FILESYSTEM_NO_DEPRECATED +inline bool is_regular (const directory_entry& e) BOOST_NOEXCEPT { return filesystem::is_regular(e.status()); } +#endif + //--------------------------------------------------------------------------------------// // // // directory_iterator helpers // @@ -843,14 +891,15 @@ class directory_iterator; namespace detail { BOOST_FILESYSTEM_DECL - system::error_code dir_itr_close(// never throws() + system::error_code dir_itr_close(// never throws() void *& handle # if defined(BOOST_POSIX_API) , void *& buffer # endif - ); + ); - struct dir_itr_imp + struct dir_itr_imp : + public boost::intrusive_ref_counter< dir_itr_imp > { directory_entry dir_entry; void* handle; @@ -859,19 +908,19 @@ namespace detail void* buffer; // see dir_itr_increment implementation # endif - dir_itr_imp() : handle(0) + dir_itr_imp() BOOST_NOEXCEPT : handle(0) # ifdef BOOST_POSIX_API , buffer(0) # endif {} - ~dir_itr_imp() // never throws + ~dir_itr_imp() BOOST_NOEXCEPT { dir_itr_close(handle # if defined(BOOST_POSIX_API) , buffer # endif - ); + ); } }; @@ -889,10 +938,12 @@ namespace detail // // //--------------------------------------------------------------------------------------// - class directory_iterator - : public boost::iterator_facade< directory_iterator, - directory_entry, - boost::single_pass_traversal_tag > + class directory_iterator : + public boost::iterator_facade< + directory_iterator, + directory_entry, + boost::single_pass_traversal_tag + > { public: @@ -900,18 +951,42 @@ namespace detail // iterator_facade derived classes don't seem to like implementations in // separate translation unit dll's, so forward to detail functions - explicit directory_iterator(const path& p) - : m_imp(new detail::dir_itr_imp) - { detail::directory_iterator_construct(*this, p, 0); } + explicit directory_iterator(const path& p) : + m_imp(new detail::dir_itr_imp()) + { + detail::directory_iterator_construct(*this, p, 0); + } + + directory_iterator(const path& p, system::error_code& ec) BOOST_NOEXCEPT : + m_imp(new (std::nothrow) detail::dir_itr_imp()) + { + if (BOOST_UNLIKELY(!m_imp)) + { + ec = make_error_code(system::errc::not_enough_memory); + return; + } - directory_iterator(const path& p, system::error_code& ec) BOOST_NOEXCEPT - : m_imp(new detail::dir_itr_imp) - { detail::directory_iterator_construct(*this, p, &ec); } + detail::directory_iterator_construct(*this, p, &ec); + } + + BOOST_DEFAULTED_FUNCTION(directory_iterator(directory_iterator const& that), : m_imp(that.m_imp) {}) + BOOST_DEFAULTED_FUNCTION(directory_iterator& operator= (directory_iterator const& that), { m_imp = that.m_imp; return *this; }) + +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + directory_iterator(directory_iterator&& that) BOOST_NOEXCEPT : + m_imp(std::move(that.m_imp)) + { + } - ~directory_iterator() {} + directory_iterator& operator= (directory_iterator&& that) BOOST_NOEXCEPT + { + m_imp = std::move(that.m_imp); + return *this; + } +#endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) directory_iterator& increment(system::error_code& ec) BOOST_NOEXCEPT - { + { detail::directory_iterator_increment(*this, &ec); return *this; } @@ -923,16 +998,17 @@ namespace detail friend BOOST_FILESYSTEM_DECL void detail::directory_iterator_increment(directory_iterator& it, system::error_code* ec); - // shared_ptr provides the shallow-copy semantics required for single pass iterators + // intrusive_ptr provides the shallow-copy semantics required for single pass iterators // (i.e. InputIterators). The end iterator is indicated by !m_imp || !m_imp->handle - boost::shared_ptr< detail::dir_itr_imp > m_imp; + boost::intrusive_ptr< detail::dir_itr_imp > m_imp; friend class boost::iterator_core_access; boost::iterator_facade< directory_iterator, directory_entry, - boost::single_pass_traversal_tag >::reference dereference() const + boost::single_pass_traversal_tag + >::reference dereference() const { BOOST_ASSERT_MSG(m_imp.get(), "attempt to dereference end iterator"); return m_imp->dir_entry; @@ -941,15 +1017,14 @@ namespace detail void increment() { detail::directory_iterator_increment(*this, 0); } bool equal(const directory_iterator& rhs) const - { + { return m_imp == rhs.m_imp || (!m_imp && rhs.m_imp && !rhs.m_imp->handle) || (!rhs.m_imp && m_imp && !m_imp->handle); } - }; // directory_iterator - // enable directory_iterator C++11 range-base for statement use --------------------// + // enable directory_iterator C++11 range-based for statement use --------------------// // begin() and end() are only used by a range-based for statement in the context of // auto - thus the top-level const is stripped - so returning const is harmless and @@ -961,6 +1036,14 @@ namespace detail directory_iterator end(const directory_iterator&) BOOST_NOEXCEPT {return directory_iterator();} + // enable C++14 generic accessors for range const iterators + inline + const directory_iterator& cbegin(const directory_iterator& iter) BOOST_NOEXCEPT + {return iter;} + inline + directory_iterator cend(const directory_iterator&) BOOST_NOEXCEPT + {return directory_iterator();} + // enable directory_iterator BOOST_FOREACH -----------------------------------------// inline @@ -1001,211 +1084,163 @@ namespace filesystem // // //--------------------------------------------------------------------------------------// - BOOST_SCOPED_ENUM_START(symlink_option) + BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(symlink_option, unsigned int) { none, no_recurse = none, // don't follow directory symlinks (default behavior) recurse, // follow directory symlinks - _detail_no_push = recurse << 1, // internal use only - - // BOOST_BITMASK op~ casts to int32_least_t, producing invalid enum values - _detail_extend_symlink_option_32_1 = 0x7fffffff, - _detail_extend_symlink_option_32_2 = -0x7fffffff-1 - }; - BOOST_SCOPED_ENUM_END + _detail_no_push = recurse << 1 // internal use only + } + BOOST_SCOPED_ENUM_DECLARE_END(symlink_option) - BOOST_BITMASK(BOOST_SCOPED_ENUM(symlink_option)) + BOOST_BITMASK(BOOST_SCOPED_ENUM_NATIVE(symlink_option)) namespace detail { - struct recur_dir_itr_imp + struct recur_dir_itr_imp : + public boost::intrusive_ref_counter< recur_dir_itr_imp > { typedef directory_iterator element_type; std::stack< element_type, std::vector< element_type > > m_stack; - int m_level; - BOOST_SCOPED_ENUM(symlink_option) m_options; - - recur_dir_itr_imp() : m_level(0), m_options(symlink_option::none) {} - - void increment(system::error_code* ec); // ec == 0 means throw on error + int m_level; + // symlink_option values, declared as unsigned int for ABI compatibility + unsigned int m_options; + + recur_dir_itr_imp() BOOST_NOEXCEPT : m_level(0), m_options(static_cast< unsigned int >(symlink_option::none)) {} + explicit recur_dir_itr_imp(BOOST_SCOPED_ENUM_NATIVE(symlink_option) opt) BOOST_NOEXCEPT : m_level(0), m_options(static_cast< unsigned int >(opt)) {} + + // ec == 0 means throw on error + // + // Invariant: On return, the top of the iterator stack is the next valid (possibly + // end) iterator, regardless of whether or not an error is reported, and regardless of + // whether any error is reported by exception or error code. In other words, progress + // is always made so a loop on the iterator will always eventually terminate + // regardless of errors. + BOOST_FILESYSTEM_DECL void increment(system::error_code* ec); + + // Returns: true if push occurs, otherwise false. Always returns false on error. + BOOST_FILESYSTEM_DECL bool push_directory(system::error_code& ec) BOOST_NOEXCEPT; + + // ec == 0 means throw on error + BOOST_FILESYSTEM_DECL void pop(system::error_code* ec); + }; - bool push_directory(system::error_code& ec) BOOST_NOEXCEPT; + } // namespace detail - void pop(); +//--------------------------------------------------------------------------------------// +// // +// recursive_directory_iterator // +// // +//--------------------------------------------------------------------------------------// - }; + class recursive_directory_iterator : + public boost::iterator_facade< + recursive_directory_iterator, + directory_entry, + boost::single_pass_traversal_tag + > + { + public: - // Implementation is inline to avoid dynamic linking difficulties with m_stack: - // Microsoft warning C4251, m_stack needs to have dll-interface to be used by - // clients of struct 'boost::filesystem::detail::recur_dir_itr_imp' + recursive_directory_iterator() BOOST_NOEXCEPT {} // creates the "end" iterator - inline - bool recur_dir_itr_imp::push_directory(system::error_code& ec) BOOST_NOEXCEPT - // Returns: true if push occurs, otherwise false. Always returns false on error. + explicit recursive_directory_iterator(const path& dir_path) : // throws if !exists() + m_imp(new detail::recur_dir_itr_imp()) { - ec.clear(); + m_imp->m_stack.push(directory_iterator(dir_path)); + if (m_imp->m_stack.top() == directory_iterator()) + { m_imp.reset(); } + } - // Discover if the iterator is for a directory that needs to be recursed into, - // taking symlinks and options into account. + recursive_directory_iterator(const path& dir_path, + BOOST_SCOPED_ENUM_NATIVE(symlink_option) opt) : // throws if !exists() + m_imp(new detail::recur_dir_itr_imp(opt)) + { + m_imp->m_stack.push(directory_iterator(dir_path)); + if (m_imp->m_stack.top() == directory_iterator()) + { m_imp.reset (); } + } - if ((m_options & symlink_option::_detail_no_push) == symlink_option::_detail_no_push) + recursive_directory_iterator(const path& dir_path, + BOOST_SCOPED_ENUM_NATIVE(symlink_option) opt, + system::error_code & ec) BOOST_NOEXCEPT : + m_imp(new (std::nothrow) detail::recur_dir_itr_imp(opt)) + { + if (BOOST_UNLIKELY(!m_imp)) { - m_options &= ~symlink_option::_detail_no_push; - return false; + ec = make_error_code(system::errc::not_enough_memory); + return; } - file_status symlink_stat; - - // if we are not recursing into symlinks, we are going to have to know if the - // stack top is a symlink, so get symlink_status and verify no error occurred - if ((m_options & symlink_option::recurse) != symlink_option::recurse) + directory_iterator it(dir_path, ec); + if (ec || it == directory_iterator()) { - symlink_stat = m_stack.top()->symlink_status(ec); - if (ec) - return false; + m_imp.reset(); + return; } - // Logic for following predicate was contributed by Daniel Aarno to handle cyclic - // symlinks correctly and efficiently, fixing ticket #5652. - // if (((m_options & symlink_option::recurse) == symlink_option::recurse - // || !is_symlink(m_stack.top()->symlink_status())) - // && is_directory(m_stack.top()->status())) ... - // The predicate code has since been rewritten to pass error_code arguments, - // per ticket #5653. - - if ((m_options & symlink_option::recurse) == symlink_option::recurse - || !is_symlink(symlink_stat)) + try { - file_status stat = m_stack.top()->status(ec); - if (ec || !is_directory(stat)) - return false; - - directory_iterator next(m_stack.top()->path(), ec); - if (!ec && next != directory_iterator()) - { - m_stack.push(next); - ++m_level; - return true; - } +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + m_imp->m_stack.push(std::move(it)); +#else + m_imp->m_stack.push(it); +#endif + } + catch (std::bad_alloc&) + { + ec = make_error_code(system::errc::not_enough_memory); + m_imp.reset(); } - return false; } - inline - void recur_dir_itr_imp::increment(system::error_code* ec) - // ec == 0 means throw on error - // - // Invariant: On return, the top of the iterator stack is the next valid (possibly - // end) iterator, regardless of whether or not an error is reported, and regardless of - // whether any error is reported by exception or error code. In other words, progress - // is always made so a loop on the iterator will always eventually terminate - // regardless of errors. + recursive_directory_iterator(const path& dir_path, + system::error_code & ec) BOOST_NOEXCEPT : + m_imp(new (std::nothrow) detail::recur_dir_itr_imp()) { - system::error_code ec_push_directory; - - // if various conditions are met, push a directory_iterator into the iterator stack - if (push_directory(ec_push_directory)) + if (BOOST_UNLIKELY(!m_imp)) { - if (ec) - ec->clear(); + ec = make_error_code(system::errc::not_enough_memory); return; } - // Do the actual increment operation on the top iterator in the iterator - // stack, popping the stack if necessary, until either the stack is empty or a - // non-end iterator is reached. - while (!m_stack.empty() && ++m_stack.top() == directory_iterator()) + directory_iterator it(dir_path, ec); + if (ec || it == directory_iterator()) { - m_stack.pop(); - --m_level; + m_imp.reset(); + return; } - // report errors if any - if (ec_push_directory) + try { - if (ec) - *ec = ec_push_directory; - else - { - BOOST_FILESYSTEM_THROW(filesystem_error( - "filesystem::recursive_directory_iterator directory error", - ec_push_directory)); - } +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + m_imp->m_stack.push(std::move(it)); +#else + m_imp->m_stack.push(it); +#endif } - else if (ec) - ec->clear(); - } - - inline - void recur_dir_itr_imp::pop() - { - BOOST_ASSERT_MSG(m_level > 0, - "pop() on recursive_directory_iterator with level < 1"); - - do + catch (std::bad_alloc&) { - m_stack.pop(); - --m_level; + ec = make_error_code(system::errc::not_enough_memory); + m_imp.reset(); } - while (!m_stack.empty() && ++m_stack.top() == directory_iterator()); - } - } // namespace detail - -//--------------------------------------------------------------------------------------// -// // -// recursive_directory_iterator // -// // -//--------------------------------------------------------------------------------------// - - class recursive_directory_iterator - : public boost::iterator_facade< - recursive_directory_iterator, - directory_entry, - boost::single_pass_traversal_tag > - { - public: - - recursive_directory_iterator() BOOST_NOEXCEPT {} // creates the "end" iterator - - explicit recursive_directory_iterator(const path& dir_path) // throws if !exists() - : m_imp(new detail::recur_dir_itr_imp) - { - m_imp->m_options = symlink_option::none; - m_imp->m_stack.push(directory_iterator(dir_path)); - if (m_imp->m_stack.top() == directory_iterator()) - { m_imp.reset(); } } - recursive_directory_iterator(const path& dir_path, - BOOST_SCOPED_ENUM(symlink_option) opt) // throws if !exists() - : m_imp(new detail::recur_dir_itr_imp) - { - m_imp->m_options = opt; - m_imp->m_stack.push(directory_iterator(dir_path)); - if (m_imp->m_stack.top() == directory_iterator()) - { m_imp.reset (); } - } + BOOST_DEFAULTED_FUNCTION(recursive_directory_iterator(recursive_directory_iterator const& that), : m_imp(that.m_imp) {}) + BOOST_DEFAULTED_FUNCTION(recursive_directory_iterator& operator= (recursive_directory_iterator const& that), { m_imp = that.m_imp; return *this; }) - recursive_directory_iterator(const path& dir_path, - BOOST_SCOPED_ENUM(symlink_option) opt, - system::error_code & ec) BOOST_NOEXCEPT - : m_imp(new detail::recur_dir_itr_imp) +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + recursive_directory_iterator(recursive_directory_iterator&& that) BOOST_NOEXCEPT : + m_imp(std::move(that.m_imp)) { - m_imp->m_options = opt; - m_imp->m_stack.push(directory_iterator(dir_path, ec)); - if (m_imp->m_stack.top() == directory_iterator()) - { m_imp.reset (); } } - recursive_directory_iterator(const path& dir_path, - system::error_code & ec) BOOST_NOEXCEPT - : m_imp(new detail::recur_dir_itr_imp) + recursive_directory_iterator& operator= (recursive_directory_iterator&& that) BOOST_NOEXCEPT { - m_imp->m_options = symlink_option::none; - m_imp->m_stack.push(directory_iterator(dir_path, ec)); - if (m_imp->m_stack.top() == directory_iterator()) - { m_imp.reset (); } + m_imp = std::move(that.m_imp); + return *this; } +#endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) recursive_directory_iterator& increment(system::error_code& ec) BOOST_NOEXCEPT { @@ -1218,22 +1253,22 @@ namespace filesystem } int depth() const BOOST_NOEXCEPT - { + { BOOST_ASSERT_MSG(m_imp.get(), "depth() on end recursive_directory_iterator"); return m_imp->m_level; } - + int level() const BOOST_NOEXCEPT { return depth(); } bool recursion_pending() const BOOST_NOEXCEPT { BOOST_ASSERT_MSG(m_imp.get(), "is_no_push_requested() on end recursive_directory_iterator"); - return (m_imp->m_options & symlink_option::_detail_no_push) - == symlink_option::_detail_no_push; + return (m_imp->m_options & static_cast< unsigned int >(symlink_option::_detail_no_push)) + == static_cast< unsigned int >(symlink_option::_detail_no_push); } - + bool no_push_pending() const BOOST_NOEXCEPT { return recursion_pending(); } # ifndef BOOST_FILESYSTEM_NO_DEPRECATED @@ -1241,10 +1276,18 @@ namespace filesystem # endif void pop() - { + { + BOOST_ASSERT_MSG(m_imp.get(), + "pop() on end recursive_directory_iterator"); + m_imp->pop(0); + if (m_imp->m_stack.empty()) m_imp.reset(); // done, so make end iterator + } + + void pop(system::error_code& ec) BOOST_NOEXCEPT + { BOOST_ASSERT_MSG(m_imp.get(), "pop() on end recursive_directory_iterator"); - m_imp->pop(); + m_imp->pop(&ec); if (m_imp->m_stack.empty()) m_imp.reset(); // done, so make end iterator } @@ -1253,11 +1296,11 @@ namespace filesystem BOOST_ASSERT_MSG(m_imp.get(), "no_push() on end recursive_directory_iterator"); if (value) - m_imp->m_options |= symlink_option::_detail_no_push; + m_imp->m_options |= static_cast< unsigned int >(symlink_option::_detail_no_push); else - m_imp->m_options &= ~symlink_option::_detail_no_push; + m_imp->m_options &= ~static_cast< unsigned int >(symlink_option::_detail_no_push); } - + void no_push(bool value=true) BOOST_NOEXCEPT { disable_recursion_pending(value); } file_status status() const @@ -1276,18 +1319,18 @@ namespace filesystem private: - // shared_ptr provides the shallow-copy semantics required for single pass iterators + // intrusive_ptr provides the shallow-copy semantics required for single pass iterators // (i.e. InputIterators). // The end iterator is indicated by !m_imp || m_imp->m_stack.empty() - boost::shared_ptr< detail::recur_dir_itr_imp > m_imp; + boost::intrusive_ptr< detail::recur_dir_itr_imp > m_imp; friend class boost::iterator_core_access; - boost::iterator_facade< + boost::iterator_facade< recursive_directory_iterator, directory_entry, boost::single_pass_traversal_tag >::reference - dereference() const + dereference() const { BOOST_ASSERT_MSG(m_imp.get(), "dereference of end recursive_directory_iterator"); @@ -1295,7 +1338,7 @@ namespace filesystem } void increment() - { + { BOOST_ASSERT_MSG(m_imp.get(), "increment of end recursive_directory_iterator"); m_imp->increment(0); @@ -1309,7 +1352,6 @@ namespace filesystem || (!m_imp && rhs.m_imp && rhs.m_imp->m_stack.empty()) || (!rhs.m_imp && m_imp && m_imp->m_stack.empty()) ; } - }; // recursive directory iterator // enable recursive directory iterator C++11 range-base for statement use ----------// @@ -1325,10 +1367,18 @@ namespace filesystem recursive_directory_iterator end(const recursive_directory_iterator&) BOOST_NOEXCEPT {return recursive_directory_iterator();} + // enable C++14 generic accessors for range const iterators + inline + const recursive_directory_iterator& cbegin(const recursive_directory_iterator& iter) BOOST_NOEXCEPT + {return iter;} + inline + recursive_directory_iterator cend(const recursive_directory_iterator&) BOOST_NOEXCEPT + {return recursive_directory_iterator();} + // enable recursive directory iterator BOOST_FOREACH -------------------------------// inline - recursive_directory_iterator& + recursive_directory_iterator& range_begin(recursive_directory_iterator& iter) BOOST_NOEXCEPT {return iter;} inline |