From 322007bb6afd89fb23320fe0328378b4ea99bba3 Mon Sep 17 00:00:00 2001 From: arvidn Date: Thu, 27 Jul 2017 13:26:12 -0700 Subject: [PATCH] make save_state_flags a strong type --- bindings/python/src/converters.cpp | 7 ++ bindings/python/src/session.cpp | 62 +++++++------ docs/hunspell/libtorrent.dic | 3 + include/libtorrent/Makefile.am | 1 + include/libtorrent/aux_/session_impl.hpp | 8 +- include/libtorrent/aux_/session_interface.hpp | 5 +- include/libtorrent/aux_/storage_utils.hpp | 3 +- include/libtorrent/disk_interface.hpp | 3 +- include/libtorrent/disk_io_job.hpp | 4 +- include/libtorrent/disk_io_thread.hpp | 2 +- include/libtorrent/session.hpp | 20 ++--- include/libtorrent/session_handle.hpp | 88 ++++++++----------- include/libtorrent/session_types.hpp | 55 ++++++++++++ include/libtorrent/storage.hpp | 4 +- include/libtorrent/torrent.hpp | 2 +- src/disk_io_job.cpp | 2 +- src/disk_io_thread.cpp | 6 +- src/session.cpp | 6 +- src/session_handle.cpp | 38 ++++++-- src/session_impl.cpp | 9 +- src/storage.cpp | 6 +- src/storage_utils.cpp | 2 +- src/torrent.cpp | 4 +- test/test_block_cache.cpp | 14 +-- test/test_direct_dht.cpp | 4 +- test/test_remove_torrent.cpp | 8 +- test/test_ssl.cpp | 6 +- test/web_seed_suite.cpp | 2 +- 28 files changed, 226 insertions(+), 148 deletions(-) create mode 100644 include/libtorrent/session_types.hpp diff --git a/bindings/python/src/converters.cpp b/bindings/python/src/converters.cpp index b1bf1c592..b225754ff 100644 --- a/bindings/python/src/converters.cpp +++ b/bindings/python/src/converters.cpp @@ -15,6 +15,7 @@ #include "libtorrent/aux_/noexcept_movable.hpp" #include "libtorrent/peer_info.hpp" #include "libtorrent/alert_types.hpp" // for picker_flags_t +#include "libtorrent/session_types.hpp" // for save_state_flags_t #include "libtorrent/alert.hpp" #include @@ -304,6 +305,9 @@ void bind_converters() to_python_converter>(); to_python_converter>(); to_python_converter>(); + to_python_converter>(); + to_python_converter>(); + to_python_converter>(); // work-around types to_python_converter, address_to_tuple< @@ -358,4 +362,7 @@ void bind_converters() to_bitfield_flag(); to_bitfield_flag(); to_bitfield_flag(); + to_bitfield_flag(); + to_bitfield_flag(); + to_bitfield_flag(); } diff --git a/bindings/python/src/session.cpp b/bindings/python/src/session.cpp index 41938fcb2..00d0e0c4c 100644 --- a/bindings/python/src/session.cpp +++ b/bindings/python/src/session.cpp @@ -163,7 +163,7 @@ namespace return ret; } - std::shared_ptr make_session(boost::python::dict sett, int flags) + std::shared_ptr make_session(boost::python::dict sett, session_flags_t flags) { settings_pack p; make_settings_pack(p, sett); @@ -471,11 +471,11 @@ namespace } #endif - entry save_state(lt::session const& s, std::uint32_t flags) + entry save_state(lt::session const& s, std::uint32_t const flags) { allow_threading_guard guard; entry e; - s.save_state(e, flags); + s.save_state(e, save_state_flags_t(flags)); return e; } @@ -495,7 +495,7 @@ namespace return ret; } - void load_state(lt::session& ses, entry const& st, std::uint32_t flags) + void load_state(lt::session& ses, entry const& st, std::uint32_t const flags) { allow_threading_guard guard; @@ -505,7 +505,7 @@ namespace error_code ec; bdecode(&buf[0], &buf[0] + buf.size(), e, ec); TORRENT_ASSERT(!ec); - ses.load_state(e, flags); + ses.load_state(e, save_state_flags_t(flags)); } #ifndef TORRENT_DISABLE_DHT @@ -564,6 +564,9 @@ struct dummy1 {}; #ifndef TORRENT_NO_DEPRECATE struct dummy2 {}; #endif +struct dummy9 {}; +struct dummy10 {}; +struct dummy11 {}; void bind_session() { @@ -695,21 +698,21 @@ void bind_session() #endif ; - enum_("storage_mode_t") .value("storage_mode_allocate", storage_mode_allocate) .value("storage_mode_sparse", storage_mode_sparse) ; - enum_("options_t") - .value("delete_files", lt::session::delete_files) - ; - - enum_("session_flags_t") - .value("add_default_plugins", lt::session::add_default_plugins) - .value("start_default_features", lt::session::start_default_features) - ; + { + scope s = class_("options_t"); + s.attr("delete_files") = lt::session::delete_files; + } + { + scope s = class_("session_flags_t"); + s.attr("add_default_plugins") = lt::session::add_default_plugins; + s.attr("start_default_features") = lt::session::start_default_features; + } { scope s = class_("torrent_flags"); @@ -799,10 +802,10 @@ void bind_session() ) #ifndef TORRENT_NO_DEPRECATE .def( - init(( + init(( arg("fingerprint")=fingerprint("LT",0,1,0,0) , arg("flags")=lt::session::start_default_features | lt::session::add_default_plugins - , arg("alert_mask")=int(alert::error_notification))) + , arg("alert_mask")=alert::error_notification)) ) .def("outgoing_ports", &outgoing_ports) #endif @@ -933,21 +936,22 @@ void bind_session() .value("tcp", lt::session::tcp) ; - enum_("save_state_flags_t") - .value("save_settings", lt::session::save_settings) - .value("save_dht_settings", lt::session::save_dht_settings) - .value("save_dht_state", lt::session::save_dht_state) - .value("save_encryption_settings", lt::session:: save_encryption_settings) + { + scope s = class_("save_state_flags_t"); + s.attr("save_settings") = lt::session::save_settings; + s.attr("save_dht_settings") = lt::session::save_dht_settings; + s.attr("save_dht_state") = lt::session::save_dht_state; + s.attr("save_encryption_settings") = lt::session:: save_encryption_settings; #ifndef TORRENT_NO_DEPRECATE - .value("save_as_map", lt::session::save_as_map) - .value("save_i2p_proxy", lt::session::save_i2p_proxy) - .value("save_proxy", lt::session::save_proxy) - .value("save_dht_proxy", lt::session::save_dht_proxy) - .value("save_peer_proxy", lt::session::save_peer_proxy) - .value("save_web_proxy", lt::session::save_web_proxy) - .value("save_tracker_proxy", lt::session::save_tracker_proxy) + s.attr("save_as_map") = lt::session::save_as_map; + s.attr("save_i2p_proxy") = lt::session::save_i2p_proxy; + s.attr("save_proxy") = lt::session::save_proxy; + s.attr("save_dht_proxy") = lt::session::save_dht_proxy; + s.attr("save_peer_proxy") = lt::session::save_peer_proxy; + s.attr("save_web_proxy") = lt::session::save_web_proxy; + s.attr("save_tracker_proxy") = lt::session::save_tracker_proxy; #endif - ; + } #ifndef TORRENT_NO_DEPRECATE enum_("listen_on_flags_t") diff --git a/docs/hunspell/libtorrent.dic b/docs/hunspell/libtorrent.dic index 3ad80010f..ea83d6555 100644 --- a/docs/hunspell/libtorrent.dic +++ b/docs/hunspell/libtorrent.dic @@ -208,3 +208,6 @@ Hellman html namespace dn +pe +lt +tex diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index e257da83f..04fe635f5 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -116,6 +116,7 @@ nobase_include_HEADERS = \ session_settings.hpp \ session_stats.hpp \ session_status.hpp \ + session_types.hpp \ settings_pack.hpp \ sha1.hpp \ sha512.hpp \ diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 7a3884ad9..a88320a42 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -499,8 +499,8 @@ namespace aux { void on_async_load_torrent(add_torrent_params* params, error_code ec); #endif - void remove_torrent(torrent_handle const& h, int options) override; - void remove_torrent_impl(std::shared_ptr tptr, int options) override; + void remove_torrent(torrent_handle const& h, remove_flags_t options) override; + void remove_torrent_impl(std::shared_ptr tptr, remove_flags_t options) override; void get_torrent_status(std::vector* ret , std::function const& pred @@ -600,8 +600,8 @@ namespace aux { void announce_lsd(sha1_hash const& ih, int port, bool broadcast = false) override; - void save_state(entry* e, std::uint32_t flags) const; - void load_state(bdecode_node const* e, std::uint32_t flags); + void save_state(entry* e, save_state_flags_t flags) const; + void load_state(bdecode_node const* e, save_state_flags_t flags); bool has_connection(peer_connection* p) const override; void insert_peer(std::shared_ptr const& c) override; diff --git a/include/libtorrent/aux_/session_interface.hpp b/include/libtorrent/aux_/session_interface.hpp index 5210a888a..902f30a39 100644 --- a/include/libtorrent/aux_/session_interface.hpp +++ b/include/libtorrent/aux_/session_interface.hpp @@ -43,6 +43,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/socket.hpp" // for tcp::endpoint #include "libtorrent/aux_/vector.hpp" #include "libtorrent/aux_/listen_socket_handle.hpp" +#include "libtorrent/session_types.hpp" #include #include @@ -155,8 +156,8 @@ namespace libtorrent { namespace aux { virtual bool has_connection(peer_connection* p) const = 0; virtual void insert_peer(std::shared_ptr const& c) = 0; - virtual void remove_torrent(torrent_handle const& h, int options = 0) = 0; - virtual void remove_torrent_impl(std::shared_ptr tptr, int options) = 0; + virtual void remove_torrent(torrent_handle const& h, remove_flags_t options = {}) = 0; + virtual void remove_torrent_impl(std::shared_ptr tptr, remove_flags_t options) = 0; // port filter virtual port_filter const& get_port_filter() const = 0; diff --git a/include/libtorrent/aux_/storage_utils.hpp b/include/libtorrent/aux_/storage_utils.hpp index ba6b71e6e..25432e514 100644 --- a/include/libtorrent/aux_/storage_utils.hpp +++ b/include/libtorrent/aux_/storage_utils.hpp @@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/typed_span.hpp" #include "libtorrent/units.hpp" #include "libtorrent/storage_defs.hpp" // for status_t +#include "libtorrent/session_types.hpp" namespace libtorrent { @@ -85,7 +86,7 @@ namespace aux { // opt to only delete the partfile TORRENT_EXTRA_EXPORT void delete_files(file_storage const& fs, std::string const& save_path - , std::string const& part_file_name, int const options, storage_error& ec); + , std::string const& part_file_name, remove_flags_t const options, storage_error& ec); TORRENT_EXTRA_EXPORT bool verify_resume_data(add_torrent_params const& rd diff --git a/include/libtorrent/disk_interface.hpp b/include/libtorrent/disk_interface.hpp index 66d0ca5a0..d8383cacf 100644 --- a/include/libtorrent/disk_interface.hpp +++ b/include/libtorrent/disk_interface.hpp @@ -46,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/time.hpp" #include "libtorrent/sha1_hash.hpp" #include "libtorrent/flags.hpp" +#include "libtorrent/session_types.hpp" namespace libtorrent { @@ -164,7 +165,7 @@ namespace libtorrent { virtual void async_rename_file(storage_index_t storage , file_index_t index, std::string name , std::function handler) = 0; - virtual void async_delete_files(storage_index_t storage, int options + virtual void async_delete_files(storage_index_t storage, remove_flags_t options , std::function handler) = 0; virtual void async_set_file_priority(storage_index_t storage , aux::vector prio diff --git a/include/libtorrent/disk_io_job.hpp b/include/libtorrent/disk_io_job.hpp index ac1cdd624..a524b302a 100644 --- a/include/libtorrent/disk_io_job.hpp +++ b/include/libtorrent/disk_io_job.hpp @@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/disk_interface.hpp" #include "libtorrent/aux_/vector.hpp" #include "libtorrent/units.hpp" +#include "libtorrent/session_types.hpp" #include "libtorrent/aux_/disable_warnings_push.hpp" #include @@ -127,7 +128,8 @@ namespace libtorrent { , std::string , add_torrent_params const* , aux::vector - , int> argument; + , remove_flags_t + > argument; // the disk storage this job applies to (if applicable) std::shared_ptr storage; diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index 3977c6384..28eabd2fa 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -311,7 +311,7 @@ namespace aux { , std::function handler) override; void async_release_files(storage_index_t storage , std::function handler = std::function()) override; - void async_delete_files(storage_index_t storage, int options + void async_delete_files(storage_index_t storage, remove_flags_t options , std::function handler) override; void async_check_files(storage_index_t storage , add_torrent_params const* resume_data diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index 16a778267..2e2ef3d6e 100644 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -163,7 +163,7 @@ namespace aux { // This function helps to construct a ``session_params`` from a // bencoded data generated by ``session_handle::save_state`` TORRENT_EXPORT session_params read_session_params(bdecode_node const& e - , std::uint32_t flags = 0xffffffff); + , save_state_flags_t flags = save_state_flags_t{0xffffffff}); // The session holds all state that spans multiple torrents. Among other // things it runs the network loop and manages all torrents. Once it's @@ -222,7 +222,7 @@ namespace aux { // default is to start those features. If you do not want them to start, // pass 0 as the flags parameter. session(settings_pack pack - , int flags = start_default_features | add_default_plugins) + , session_flags_t const flags = start_default_features | add_default_plugins) { TORRENT_CFG(); start(flags, std::move(pack), nullptr); @@ -251,7 +251,7 @@ namespace aux { // destruct the session_proxy object. session(settings_pack pack , io_service& ios - , int flags = start_default_features | add_default_plugins) + , session_flags_t const flags = start_default_features | add_default_plugins) { TORRENT_CFG(); start(flags, std::move(pack), &ios); @@ -272,14 +272,14 @@ namespace aux { #endif TORRENT_DEPRECATED session(fingerprint const& print - , int flags = start_default_features | add_default_plugins - , std::uint32_t alert_mask = alert::error_notification) + , session_flags_t const flags = start_default_features | add_default_plugins + , alert_category_t const alert_mask = alert::error_notification) { TORRENT_CFG(); settings_pack pack; pack.set_int(settings_pack::alert_mask, int(alert_mask)); pack.set_str(settings_pack::peer_fingerprint, print.to_string()); - if ((flags & start_default_features) == 0) + if (!(flags & start_default_features)) { pack.set_bool(settings_pack::enable_upnp, false); pack.set_bool(settings_pack::enable_natpmp, false); @@ -294,8 +294,8 @@ namespace aux { session(fingerprint const& print , std::pair listen_port_range , char const* listen_interface = "0.0.0.0" - , int flags = start_default_features | add_default_plugins - , int alert_mask = alert::error_notification) + , session_flags_t const flags = start_default_features | add_default_plugins + , alert_category_t const alert_mask = alert::error_notification) { TORRENT_CFG(); TORRENT_ASSERT(listen_port_range.first > 0); @@ -311,7 +311,7 @@ namespace aux { std::snprintf(if_string, sizeof(if_string), "%s:%d", listen_interface, listen_port_range.first); pack.set_str(settings_pack::listen_interfaces, if_string); - if ((flags & start_default_features) == 0) + if (!(flags & start_default_features)) { pack.set_bool(settings_pack::enable_upnp, false); pack.set_bool(settings_pack::enable_natpmp, false); @@ -362,7 +362,7 @@ namespace aux { private: void start(session_params params, io_service* ios); - void start(int flags, settings_pack sp, io_service* ios); + void start(session_flags_t flags, settings_pack sp, io_service* ios); // data shared between the main thread // and the working thread diff --git a/include/libtorrent/session_handle.hpp b/include/libtorrent/session_handle.hpp index aa74dda4a..ce5ea5cca 100644 --- a/include/libtorrent/session_handle.hpp +++ b/include/libtorrent/session_handle.hpp @@ -43,6 +43,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/peer_class_type_filter.hpp" #include "libtorrent/peer_id.hpp" #include "libtorrent/io_service.hpp" +#include "libtorrent/session_types.hpp" #include "libtorrent/kademlia/dht_storage.hpp" @@ -81,41 +82,30 @@ namespace libtorrent { bool is_valid() const { return !m_impl.expired(); } - // TODO: 2 the ip filter should probably be saved here too + // saves settings (i.e. the settings_pack) + static constexpr save_state_flags_t save_settings{0x001}; -#include "libtorrent/aux_/disable_warnings_push.hpp" + // saves dht_settings + static constexpr save_state_flags_t save_dht_settings{0x002}; - // flags that determines which aspects of the session should be - // saved when calling save_state(). - enum save_state_flags_t - { - // saves settings (i.e. the settings_pack) - save_settings = 0x001, + // saves dht state such as nodes and node-id, possibly accelerating + // joining the DHT if provided at next session startup. + static constexpr save_state_flags_t save_dht_state{0x004}; - // saves dht_settings - save_dht_settings = 0x002, - - // saves dht state such as nodes and node-id, possibly accelerating - // joining the DHT if provided at next session startup. - save_dht_state = 0x004, - - // save pe_settings - save_encryption_settings = 0x020 + // save pe_settings + static constexpr save_state_flags_t save_encryption_settings{0x020}; #ifndef TORRENT_NO_DEPRECATE - , - save_as_map TORRENT_DEPRECATED_ENUM = 0x040, - save_proxy TORRENT_DEPRECATED_ENUM = 0x008, - save_i2p_proxy TORRENT_DEPRECATED_ENUM = 0x010, - save_dht_proxy TORRENT_DEPRECATED_ENUM = 0x008, // save_proxy - save_peer_proxy TORRENT_DEPRECATED_ENUM = 0x008, // save_proxy - save_web_proxy TORRENT_DEPRECATED_ENUM = 0x008, // save_proxy - save_tracker_proxy TORRENT_DEPRECATED_ENUM = 0x008 // save_proxy + static constexpr save_state_flags_t TORRENT_DEPRECATED_MEMBER save_as_map{0x040}; + static constexpr save_state_flags_t TORRENT_DEPRECATED_MEMBER save_proxy{0x008}; + static constexpr save_state_flags_t TORRENT_DEPRECATED_MEMBER save_i2p_proxy{0x010}; + static constexpr save_state_flags_t TORRENT_DEPRECATED_MEMBER save_dht_proxy{0x008}; + static constexpr save_state_flags_t TORRENT_DEPRECATED_MEMBER save_peer_proxy{0x008}; + static constexpr save_state_flags_t TORRENT_DEPRECATED_MEMBER save_web_proxy{0x008}; + static constexpr save_state_flags_t TORRENT_DEPRECATED_MEMBER save_tracker_proxy{0x008}; #endif - }; - -#include "libtorrent/aux_/disable_warnings_pop.hpp" + // TODO: 2 the ip filter should probably be saved here too // loads and saves all session settings, including dht_settings, // encryption settings and proxy settings. ``save_state`` writes all keys // to the ``entry`` that's passed in, which needs to either not be @@ -126,14 +116,14 @@ namespace libtorrent { // // The ``flags`` argument is used to filter which parts of the session // state to save or load. By default, all state is saved/restored (except - // for the individual torrents). see save_state_flags_t + // for the individual torrents). // // When saving settings, there are two fields that are *not* loaded. // ``peer_fingerprint`` and ``user_agent``. Those are left as configured // by the ``session_settings`` passed to the session constructor or // subsequently set via apply_settings(). - void save_state(entry& e, std::uint32_t flags = 0xffffffff) const; - void load_state(bdecode_node const& e, std::uint32_t flags = 0xffffffff); + void save_state(entry& e, save_state_flags_t flags = save_state_flags_t{0xffffffff}) const; + void load_state(bdecode_node const& e, save_state_flags_t flags = save_state_flags_t{0xffffffff}); // .. note:: // these calls are potentially expensive and won't scale well with @@ -551,13 +541,13 @@ namespace libtorrent { // use load_state and save_state instead TORRENT_DEPRECATED void load_state(entry const& ses_state - , std::uint32_t flags = 0xffffffff); + , save_state_flags_t flags = save_state_flags_t(0xffffffff)); TORRENT_DEPRECATED entry state() const; // deprecated in 1.1 TORRENT_DEPRECATED void load_state(lazy_entry const& ses_state - , std::uint32_t flags = 0xffffffff); + , save_state_flags_t flags = save_state_flags_t{0xffffffff}); #endif // TORRENT_NO_DEPRECATE // Sets a filter that will be used to reject and accept incoming as well @@ -741,28 +731,20 @@ namespace libtorrent { , int flags = 0); #endif - // flags to be passed in to remove_torrent(). - enum options_t - { - // delete the files belonging to the torrent from disk. - // including the part-file, if there is one - delete_files = 1, + // delete the files belonging to the torrent from disk. + // including the part-file, if there is one + static constexpr remove_flags_t delete_files{1}; - // delete just the part-file associated with this torrent - delete_partfile = 2 - }; + // delete just the part-file associated with this torrent + static constexpr remove_flags_t delete_partfile{2}; - // flags to be passed in to the session constructor - enum session_flags_t - { - // this will add common extensions like ut_pex, ut_metadata, lt_tex - // smart_ban and possibly others. - add_default_plugins = 1, + // this will add common extensions like ut_pex, ut_metadata, lt_tex + // smart_ban and possibly others. + static constexpr session_flags_t add_default_plugins{1}; - // this will start features like DHT, local service discovery, UPnP - // and NAT-PMP. - start_default_features = 2 - }; + // this will start features like DHT, local service discovery, UPnP + // and NAT-PMP. + static constexpr session_flags_t start_default_features{2}; // ``remove_torrent()`` will close all peer connections associated with // the torrent and tell the tracker that we've stopped participating in @@ -782,7 +764,7 @@ namespace libtorrent { // large state_update to be posted. When removing all torrents, it is // advised to remove them from the back of the queue, to minimize the // shifting. - void remove_torrent(const torrent_handle& h, int options = 0); + void remove_torrent(const torrent_handle& h, remove_flags_t options = {}); #ifndef TORRENT_NO_DEPRECATE // deprecated in libtorrent 1.1. use settings_pack instead diff --git a/include/libtorrent/session_types.hpp b/include/libtorrent/session_types.hpp new file mode 100644 index 000000000..c003da14e --- /dev/null +++ b/include/libtorrent/session_types.hpp @@ -0,0 +1,55 @@ +/* + +Copyright (c) 2017, Arvid Norberg +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef TORRENT_SESSION_TYPES_HPP_INCLUDED +#define TORRENT_SESSION_TYPES_HPP_INCLUDED + +#include +#include "libtorrent/flags.hpp" + +namespace libtorrent { + + // hidden + struct save_state_flags_tag; + using save_state_flags_t = flags::bitfield_flag; + + // hidden + struct session_flags_tag; + using session_flags_t = flags::bitfield_flag; + + // hidden + struct remove_flags_tag; + using remove_flags_t = flags::bitfield_flag; +} + +#endif + diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index db2e6c64a..305774c88 100644 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -295,7 +295,7 @@ namespace libtorrent { // // void release_memory(); // }; - virtual void delete_files(int options, storage_error& ec) = 0; + virtual void delete_files(remove_flags_t options, storage_error& ec) = 0; // called periodically (useful for deferred flushing). When returning // false, it means no more ticks are necessary. Any disk job submitted @@ -387,7 +387,7 @@ namespace libtorrent { virtual void rename_file(file_index_t index, std::string const& new_filename , storage_error& ec) override; virtual void release_files(storage_error& ec) override; - virtual void delete_files(int options, storage_error& ec) override; + virtual void delete_files(remove_flags_t options, storage_error& ec) override; virtual void initialize(storage_error& ec) override; virtual status_t move_storage(std::string const& save_path , move_flags_t flags, storage_error& ec) override; diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 53d0f7616..4221901cd 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -542,7 +542,7 @@ namespace libtorrent { bool should_check_files() const; - bool delete_files(int options); + bool delete_files(remove_flags_t options); void peers_erased(std::vector const& peers); #ifndef TORRENT_NO_DEPRECATE diff --git a/src/disk_io_job.cpp b/src/disk_io_job.cpp index 410d6efeb..b15d3fdc6 100644 --- a/src/disk_io_job.cpp +++ b/src/disk_io_job.cpp @@ -101,7 +101,7 @@ namespace libtorrent { } disk_io_job::disk_io_job() - : argument(0) + : argument(remove_flags_t{0}) , piece(0) { d.io.offset = 0; diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 4f678f48e..b6552ba5d 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -1826,7 +1826,7 @@ namespace libtorrent { } void disk_io_thread::async_delete_files(storage_index_t const storage - , int const options + , remove_flags_t const options , std::function handler) { // remove cache blocks belonging to this torrent @@ -2465,7 +2465,7 @@ namespace libtorrent { status_t disk_io_thread::do_delete_files(disk_io_job* j, jobqueue_t& completed_jobs) { - TORRENT_ASSERT(boost::get(j->argument) != 0); + TORRENT_ASSERT(boost::get(j->argument)); // if this assert fails, something's wrong with the fence logic TORRENT_ASSERT(j->storage->num_outstanding_jobs() == 1); @@ -2477,7 +2477,7 @@ namespace libtorrent { , completed_jobs, l); l.unlock(); - j->storage->delete_files(boost::get(j->argument), j->error); + j->storage->delete_files(boost::get(j->argument), j->error); return j->error ? status_t::fatal_disk_error : status_t::no_error; } diff --git a/src/session.cpp b/src/session.cpp index 20f1276bf..345470b35 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -255,7 +255,7 @@ namespace libtorrent { // configurations this will give a link error void TORRENT_CFG() {} - session_params read_session_params(bdecode_node const& e, std::uint32_t const flags) + session_params read_session_params(bdecode_node const& e, save_state_flags_t const flags) { session_params params; @@ -351,10 +351,10 @@ namespace { } } - void session::start(int flags, settings_pack sp, io_service* ios) + void session::start(session_flags_t const flags, settings_pack sp, io_service* ios) { start({std::move(sp), - default_plugins((flags & add_default_plugins) == 0)}, ios); + default_plugins(!(flags & add_default_plugins))}, ios); } session::~session() diff --git a/src/session_handle.cpp b/src/session_handle.cpp index bc3ba63f4..f7fe12db5 100644 --- a/src/session_handle.cpp +++ b/src/session_handle.cpp @@ -47,9 +47,29 @@ using libtorrent::aux::session_impl; namespace libtorrent { - peer_class_t constexpr session_handle::global_peer_class_id; - peer_class_t constexpr session_handle::tcp_peer_class_id; - peer_class_t constexpr session_handle::local_peer_class_id; + constexpr peer_class_t session_handle::global_peer_class_id; + constexpr peer_class_t session_handle::tcp_peer_class_id; + constexpr peer_class_t session_handle::local_peer_class_id; + + constexpr save_state_flags_t session_handle::save_settings; + constexpr save_state_flags_t session_handle::save_dht_settings; + constexpr save_state_flags_t session_handle::save_dht_state; + constexpr save_state_flags_t session_handle::save_encryption_settings; +#ifndef TORRENT_NO_DEPRECATE + constexpr save_state_flags_t session_handle::save_as_map TORRENT_DEPRECATED_ENUM; + constexpr save_state_flags_t session_handle::save_proxy TORRENT_DEPRECATED_ENUM; + constexpr save_state_flags_t session_handle::save_i2p_proxy TORRENT_DEPRECATED_ENUM; + constexpr save_state_flags_t session_handle::save_dht_proxy TORRENT_DEPRECATED_ENUM; + constexpr save_state_flags_t session_handle::save_peer_proxy TORRENT_DEPRECATED_ENUM; + constexpr save_state_flags_t session_handle::save_web_proxy TORRENT_DEPRECATED_ENUM; + constexpr save_state_flags_t session_handle::save_tracker_proxy TORRENT_DEPRECATED_ENUM; +#endif + + constexpr session_flags_t session_handle::add_default_plugins; + constexpr session_flags_t session_handle::start_default_features; + + constexpr remove_flags_t session_handle::delete_files; + constexpr remove_flags_t session_handle::delete_partfile; template void session_handle::async_call(Fun f, Args&&... a) const @@ -139,14 +159,14 @@ namespace libtorrent { return r; } - void session_handle::save_state(entry& e, std::uint32_t const flags) const + void session_handle::save_state(entry& e, save_state_flags_t const flags) const { entry* ep = &e; sync_call(&session_impl::save_state, ep, flags); } void session_handle::load_state(bdecode_node const& e - , std::uint32_t const flags) + , save_state_flags_t const flags) { // this needs to be synchronized since the lifespan // of e is tied to the caller @@ -701,7 +721,7 @@ namespace { void session_handle::load_country_db(wchar_t const*) {} void session_handle::load_state(entry const& ses_state - , std::uint32_t const flags) + , save_state_flags_t const flags) { if (ses_state.type() == entry::undefined_t) return; std::vector buf; @@ -724,12 +744,12 @@ namespace { { entry ret; auto retp = &ret; - sync_call(&session_impl::save_state, retp, 0xffffffff); + sync_call(&session_impl::save_state, retp, save_state_flags_t{0xffffffff}); return ret; } void session_handle::load_state(lazy_entry const& ses_state - , std::uint32_t const flags) + , save_state_flags_t const flags) { if (ses_state.type() == lazy_entry::none_t) return; std::pair buf = ses_state.data_section(); @@ -858,7 +878,7 @@ namespace { } #endif - void session_handle::remove_torrent(const torrent_handle& h, int options) + void session_handle::remove_torrent(const torrent_handle& h, remove_flags_t const options) { if (!h.is_valid()) #ifdef BOOST_NO_EXCEPTIONS diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 3642527e0..6ef90fbcf 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -715,7 +715,7 @@ namespace aux { #endif } - void session_impl::save_state(entry* eptr, std::uint32_t const flags) const + void session_impl::save_state(entry* eptr, save_state_flags_t const flags) const { TORRENT_ASSERT(is_single_thread()); @@ -755,7 +755,7 @@ namespace aux { } void session_impl::load_state(bdecode_node const* e - , std::uint32_t const flags) + , save_state_flags_t const flags) { TORRENT_ASSERT(is_single_thread()); @@ -5111,7 +5111,8 @@ namespace { return false; } - void session_impl::remove_torrent(const torrent_handle& h, int options) + void session_impl::remove_torrent(const torrent_handle& h + , remove_flags_t const options) { INVARIANT_CHECK; @@ -5127,7 +5128,7 @@ namespace { } void session_impl::remove_torrent_impl(std::shared_ptr tptr - , int options) + , remove_flags_t const options) { #ifndef TORRENT_NO_DEPRECATE // deprecated in 1.2 diff --git a/src/storage.cpp b/src/storage.cpp index 02a75589f..982f60c0e 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -390,7 +390,7 @@ namespace libtorrent { m_stat_cache.clear(); } - void default_storage::delete_files(int const options, storage_error& ec) + void default_storage::delete_files(remove_flags_t const options, storage_error& ec) { #if TORRENT_USE_ASSERTS // this is a fence job, we expect no other @@ -707,7 +707,7 @@ namespace { , storage_error&) override {} void rename_file(file_index_t, std::string const&, storage_error&) override {} void release_files(storage_error&) override {} - void delete_files(int, storage_error&) override {} + void delete_files(remove_flags_t, storage_error&) override {} void initialize(storage_error&) override {} status_t move_storage(std::string const&, move_flags_t, storage_error&) override { return status_t::no_error; } @@ -776,7 +776,7 @@ namespace { void release_files(storage_error&) override {} void rename_file(file_index_t , std::string const& /* new_filename */, storage_error&) override {} - void delete_files(int, storage_error&) override {} + void delete_files(remove_flags_t, storage_error&) override {} }; } diff --git a/src/storage_utils.cpp b/src/storage_utils.cpp index 3ab12c3a5..5d205d544 100644 --- a/src/storage_utils.cpp +++ b/src/storage_utils.cpp @@ -401,7 +401,7 @@ namespace libtorrent { namespace aux { } void delete_files(file_storage const& fs, std::string const& save_path - , std::string const& part_file_name, int const options, storage_error& ec) + , std::string const& part_file_name, remove_flags_t const options, storage_error& ec) { if (options == session::delete_files) { diff --git a/src/torrent.cpp b/src/torrent.cpp index 63f5d411a..1d78b82f8 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -420,7 +420,7 @@ namespace libtorrent { // torrent, create another reference auto me = shared_from_this(); - m_ses.remove_torrent_impl(me, 0); + m_ses.remove_torrent_impl(me, {}); if (alerts().should_post()) alerts().emplace_alert(get_handle(), info_hash(), tf->info_hash()); @@ -8096,7 +8096,7 @@ namespace libtorrent { return limit_impl(peer_connection::download_channel); } - bool torrent::delete_files(int const options) + bool torrent::delete_files(remove_flags_t const options) { TORRENT_ASSERT(is_single_thread()); diff --git a/test/test_block_cache.cpp b/test/test_block_cache.cpp index 1bb111e36..f312e6f31 100644 --- a/test/test_block_cache.cpp +++ b/test/test_block_cache.cpp @@ -72,7 +72,7 @@ struct test_storage_impl : storage_interface void release_files(storage_error& ec) override {} void rename_file(file_index_t index, std::string const& new_filename , storage_error& ec) override {} - void delete_files(int, storage_error& ec) override {} + void delete_files(remove_flags_t, storage_error& ec) override {} }; struct allocator : buffer_allocator_interface @@ -196,7 +196,7 @@ void test_write() TEST_CHECK(ret >= 0); // return the reference to the buffer we just read - rj.argument = 0; + rj.argument = remove_flags_t{}; TEST_EQUAL(bc.pinned_blocks(), 0); bc.update_stats_counters(c); @@ -211,7 +211,7 @@ void test_write() bc.update_stats_counters(c); TEST_EQUAL(c[counters::pinned_blocks], 0); - rj.argument = 0; + rj.argument = remove_flags_t{}; tailqueue jobs; bc.clear(jobs); @@ -334,7 +334,7 @@ void test_arc_promote() // it's supposed to be a cache hit TEST_CHECK(ret >= 0); // return the reference to the buffer we just read - rj.argument = 0; + rj.argument = remove_flags_t{}; bc.update_stats_counters(c); TEST_EQUAL(c[counters::write_cache_blocks], 0); @@ -355,7 +355,7 @@ void test_arc_promote() // it's supposed to be a cache hit TEST_CHECK(ret >= 0); // return the reference to the buffer we just read - rj.argument = 0; + rj.argument = remove_flags_t{}; bc.update_stats_counters(c); TEST_EQUAL(c[counters::write_cache_blocks], 0); @@ -457,7 +457,7 @@ void test_unaligned_read() // it's supposed to be a cache hit TEST_CHECK(ret >= 0); // return the reference to the buffer we just read - rj.argument = 0; + rj.argument = remove_flags_t{}; tailqueue jobs; bc.clear(jobs); @@ -497,7 +497,7 @@ TORRENT_TEST(delete_piece) rj.d.io.buffer_size = 0x4000; rj.piece = piece_index_t(0); rj.storage = pm; - rj.argument = 0; + rj.argument = remove_flags_t{}; ret = bc.try_read(&rj, alloc); cached_piece_entry* pe_ = bc.find_piece(pm.get(), piece_index_t(0)); diff --git a/test/test_direct_dht.cpp b/test/test_direct_dht.cpp index d13c9cfec..e0fcd44bf 100644 --- a/test/test_direct_dht.cpp +++ b/test/test_direct_dht.cpp @@ -98,9 +98,9 @@ TORRENT_TEST(direct_dht_request) sp.set_str(settings_pack::dht_bootstrap_nodes, ""); sp.set_int(settings_pack::max_retry_port_bind, 800); sp.set_str(settings_pack::listen_interfaces, "127.0.0.1:42434"); - lt::session responder(sp, 0); + lt::session responder(sp, {}); sp.set_str(settings_pack::listen_interfaces, "127.0.0.1:45434"); - lt::session requester(sp, 0); + lt::session requester(sp, {}); responder.add_extension(std::make_shared()); diff --git a/test/test_remove_torrent.cpp b/test/test_remove_torrent.cpp index e03f24df9..91973aae5 100644 --- a/test/test_remove_torrent.cpp +++ b/test/test_remove_torrent.cpp @@ -55,7 +55,7 @@ enum test_case { mid_download }; -void test_remove_torrent(int const remove_options +void test_remove_torrent(remove_flags_t const remove_options , test_case const test = complete_download) { // this allows shutting down the sessions in parallel @@ -168,7 +168,7 @@ void test_remove_torrent(int const remove_options TORRENT_TEST(remove_torrent) { - test_remove_torrent(0); + test_remove_torrent({}); } TORRENT_TEST(remove_torrent_and_files) @@ -178,7 +178,7 @@ TORRENT_TEST(remove_torrent_and_files) TORRENT_TEST(remove_torrent_partial) { - test_remove_torrent(0, partial_download); + test_remove_torrent({}, partial_download); } TORRENT_TEST(remove_torrent_and_files_partial) @@ -188,7 +188,7 @@ TORRENT_TEST(remove_torrent_and_files_partial) TORRENT_TEST(remove_torrent_mid_download) { - test_remove_torrent(0, mid_download); + test_remove_torrent({}, mid_download); } TORRENT_TEST(remove_torrent_and_files_mid_download) diff --git a/test/test_ssl.cpp b/test/test_ssl.cpp index e1c95f367..5f532d360 100644 --- a/test/test_ssl.cpp +++ b/test/test_ssl.cpp @@ -159,7 +159,7 @@ void test_ssl(int test_idx, bool use_utp) // if a peer fails once, don't try it again sett.set_int(settings_pack::max_failcount, 1); - lt::session ses1(sett, 0); + lt::session ses1(sett, {}); // this +20 is here to use a different port as ses1 port += 20; @@ -172,7 +172,7 @@ void test_ssl(int test_idx, bool use_utp) sett.set_str(settings_pack::listen_interfaces, listen_iface); - lt::session ses2(sett, 0); + lt::session ses2(sett, {}); wait_for_listen(ses1, "ses1"); wait_for_listen(ses2, "ses2"); @@ -559,7 +559,7 @@ void test_malicious_peer() sett.set_bool(settings_pack::enable_upnp, false); sett.set_bool(settings_pack::enable_natpmp, false); - lt::session ses1(sett, 0); + lt::session ses1(sett, {}); wait_for_listen(ses1, "ses1"); // create torrent diff --git a/test/web_seed_suite.cpp b/test/web_seed_suite.cpp index bc849c9fe..2036f881a 100644 --- a/test/web_seed_suite.cpp +++ b/test/web_seed_suite.cpp @@ -407,7 +407,7 @@ int EXPORT run_http_suite(int proxy, char const* protocol, bool test_url_seed pack.set_bool(settings_pack::enable_natpmp, false); pack.set_bool(settings_pack::enable_upnp, false); pack.set_bool(settings_pack::enable_dht, false); - lt::session ses(pack, 0); + lt::session ses(pack, {}); test_transfer(ses, torrent_file, proxy, protocol, test_url_seed , chunked_encoding, test_ban, keepalive, proxy_peers);