From 5344761da4357314a239d4f2ce493eec71b460a0 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 1 Jun 2017 04:15:15 -0400 Subject: [PATCH] make flags to move_storage a proper enum class (#2043) --- bindings/python/src/torrent_handle.cpp | 19 ++++++++++--------- include/libtorrent/aux_/storage_utils.hpp | 2 +- include/libtorrent/disk_interface.hpp | 2 +- include/libtorrent/disk_io_job.hpp | 2 ++ include/libtorrent/disk_io_thread.hpp | 2 +- include/libtorrent/storage.hpp | 8 ++++---- include/libtorrent/storage_defs.hpp | 12 +++++++++++- include/libtorrent/torrent.hpp | 2 +- include/libtorrent/torrent_handle.hpp | 16 ++++++++++++---- src/disk_io_thread.cpp | 6 +++--- src/storage.cpp | 8 ++++---- src/storage_utils.cpp | 6 +++--- src/torrent.cpp | 4 ++-- src/torrent_handle.cpp | 13 ++++++++++--- test/test_block_cache.cpp | 2 +- test/test_storage.cpp | 6 +++--- 16 files changed, 69 insertions(+), 41 deletions(-) diff --git a/bindings/python/src/torrent_handle.cpp b/bindings/python/src/torrent_handle.cpp index 277387fef..571530e78 100644 --- a/bindings/python/src/torrent_handle.cpp +++ b/bindings/python/src/torrent_handle.cpp @@ -441,11 +441,11 @@ void bind_torrent_handle() int (torrent_handle::*piece_priority0)(piece_index_t) const = &torrent_handle::piece_priority; void (torrent_handle::*piece_priority1)(piece_index_t, int) const = &torrent_handle::piece_priority; - void (torrent_handle::*move_storage0)(std::string const&, int flags) const = &torrent_handle::move_storage; + void (torrent_handle::*move_storage0)(std::string const&, lt::move_flags_t) const = &torrent_handle::move_storage; void (torrent_handle::*rename_file0)(file_index_t, std::string const&) const = &torrent_handle::rename_file; #if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE - void (torrent_handle::*move_storage1)(std::wstring const&, int flags) const = &torrent_handle::move_storage; + void (torrent_handle::*move_storage1)(std::wstring const&, int) const = &torrent_handle::move_storage; void (torrent_handle::*rename_file1)(file_index_t, std::wstring const&) const = &torrent_handle::rename_file; #endif @@ -453,6 +453,12 @@ void bind_torrent_handle() #define _ allow_threads + enum_("move_flags_t") + .value("always_replace_files", move_flags_t::always_replace_files) + .value("fail_if_exist", move_flags_t::fail_if_exist) + .value("dont_replace", move_flags_t::dont_replace) + ; + class_("torrent_handle") .def(self == self) .def(self != self) @@ -548,13 +554,13 @@ void bind_torrent_handle() #ifndef TORRENT_NO_DEPRECATE .def("set_tracker_login", &torrent_handle::set_tracker_login) #endif - .def("move_storage", _(move_storage0), (arg("path"), arg("flags") = 0)) + .def("move_storage", _(move_storage0), (arg("path"), arg("flags") = move_flags_t::always_replace_files)) .def("info_hash", _(&torrent_handle::info_hash)) .def("force_recheck", _(&torrent_handle::force_recheck)) .def("rename_file", _(rename_file0)) .def("set_ssl_certificate", &torrent_handle::set_ssl_certificate, (arg("cert"), arg("private_key"), arg("dh_params"), arg("passphrase")="")) #if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE - .def("move_storage", _(move_storage1), (arg("path"), arg("flags") = 0)) + .def("move_storage", _(move_storage1), (arg("path"), arg("flags") = always_replace_files)) .def("rename_file", _(rename_file1)) #endif ; @@ -605,11 +611,6 @@ void bind_torrent_handle() .value("query_verified_pieces", torrent_handle::query_verified_pieces) ; - enum_("move_flags_t") - .value("always_replace_files", always_replace_files) - .value("fail_if_exist", fail_if_exist) - .value("dont_replace", dont_replace) - ; } #ifdef _MSC_VER diff --git a/include/libtorrent/aux_/storage_utils.hpp b/include/libtorrent/aux_/storage_utils.hpp index d5ddde061..e84a16097 100644 --- a/include/libtorrent/aux_/storage_utils.hpp +++ b/include/libtorrent/aux_/storage_utils.hpp @@ -77,7 +77,7 @@ namespace libtorrent { , std::string const& save_path , std::string const& destination_save_path , part_file* pf - , int const flags, storage_error& ec); + , move_flags_t flags, storage_error& ec); // deletes the files on fs from save_path according to options. Options may // opt to only delete the partfile diff --git a/include/libtorrent/disk_interface.hpp b/include/libtorrent/disk_interface.hpp index d16a7da94..6dc1394c8 100644 --- a/include/libtorrent/disk_interface.hpp +++ b/include/libtorrent/disk_interface.hpp @@ -145,7 +145,7 @@ namespace libtorrent { , std::uint8_t flags = 0) = 0; virtual void async_hash(storage_index_t storage, piece_index_t piece, std::uint8_t flags , std::function handler, void* requester) = 0; - virtual void async_move_storage(storage_index_t storage, std::string p, std::uint8_t flags + virtual void async_move_storage(storage_index_t storage, std::string p, move_flags_t flags , std::function handler) = 0; virtual void async_release_files(storage_index_t storage , std::function handler = std::function()) = 0; diff --git a/include/libtorrent/disk_io_job.hpp b/include/libtorrent/disk_io_job.hpp index e0a54abb7..41e842194 100644 --- a/include/libtorrent/disk_io_job.hpp +++ b/include/libtorrent/disk_io_job.hpp @@ -204,6 +204,8 @@ namespace libtorrent { // flags controlling this job std::uint8_t flags = 0; + move_flags_t move_flags = move_flags_t::always_replace_files; + #if TORRENT_USE_ASSERTS bool in_use = false; diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index 812b2c8c8..110596f6c 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -306,7 +306,7 @@ namespace aux { , std::uint8_t flags = 0) override; void async_hash(storage_index_t storage, piece_index_t piece, std::uint8_t flags , std::function handler, void* requester) override; - void async_move_storage(storage_index_t storage, std::string p, std::uint8_t flags + void async_move_storage(storage_index_t storage, std::string p, move_flags_t flags , std::function handler) override; void async_release_files(storage_index_t storage , std::function handler = std::function()) override; diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index 9e741c0f8..639e42500 100644 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -234,8 +234,8 @@ namespace libtorrent { // like ``release_files()``. // //If an error occurs, ``storage_error`` should be set to reflect it. - virtual status_t move_storage(std::string const& save_path, int flags - , storage_error& ec) = 0; + virtual status_t move_storage(std::string const& save_path + , move_flags_t flags, storage_error& ec) = 0; // This function should verify the resume data ``rd`` with the files // on disk. If the resume data seems to be up-to-date, return true. If @@ -390,8 +390,8 @@ namespace libtorrent { virtual void release_files(storage_error& ec) override; virtual void delete_files(int options, storage_error& ec) override; virtual void initialize(storage_error& ec) override; - virtual status_t move_storage(std::string const& save_path, int flags - , storage_error& ec) override; + virtual status_t move_storage(std::string const& save_path + , move_flags_t flags, storage_error& ec) override; virtual bool verify_resume_data(add_torrent_params const& rd , aux::vector const& links , storage_error& error) override; diff --git a/include/libtorrent/storage_defs.hpp b/include/libtorrent/storage_defs.hpp index db2250fa8..aab116e16 100644 --- a/include/libtorrent/storage_defs.hpp +++ b/include/libtorrent/storage_defs.hpp @@ -72,7 +72,7 @@ namespace libtorrent { }; // flags for async_move_storage - enum move_flags_t + enum class move_flags_t : std::uint8_t { // replace any files in the destination when copying // or moving the storage @@ -92,6 +92,16 @@ namespace libtorrent { dont_replace }; +#ifndef TORRENT_NO_DEPRECATE + // deprecated in 1.2 + enum deprecated_move_flags_t + { + always_replace_files TORRENT_DEPRECATED_ENUM, + fail_if_exist TORRENT_DEPRECATED_ENUM, + dont_replace TORRENT_DEPRECATED_ENUM + }; +#endif + struct TORRENT_EXPORT storage_params { file_storage const* files = nullptr; diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index ce9bd91dd..adaf986f0 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -1014,7 +1014,7 @@ namespace libtorrent { // RESOURCE MANAGEMENT // flags are defined in storage.hpp - void move_storage(std::string const& save_path, int flags); + void move_storage(std::string const& save_path, move_flags_t flags); // renames the file with the given index to the new name // the name may include a directory path diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index f9cff49fc..ca54fec70 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -53,6 +53,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/sha1_hash.hpp" #include "libtorrent/units.hpp" #include "libtorrent/aux_/vector.hpp" +#include "libtorrent/storage_defs.hpp" namespace libtorrent { namespace aux { @@ -1211,10 +1212,6 @@ namespace libtorrent { namespace aux { // The ``flags`` argument determines the behavior of the copying/moving // of the files in the torrent. see move_flags_t. // - // * always_replace_files = 0 - // * fail_if_exist = 1 - // * dont_replace = 2 - // // ``always_replace_files`` is the default and replaces any file that // exist in both the source directory and the target directory. // @@ -1241,7 +1238,18 @@ namespace libtorrent { namespace aux { // torrent but are stored in the torrent's directory may be moved as // well. This goes for files that have been renamed to absolute paths // that still end up inside the save path. + void move_storage(std::string const& save_path + , move_flags_t flags +#ifdef TORRENT_NO_DEPRECATE + = move_flags_t::always_replace_files +#endif + ) const; + +#ifndef TORRENT_NO_DEPRECATE + // deprecated in 1.2 + TORRENT_DEPRECATED void move_storage(std::string const& save_path, int flags = 0) const; +#endif // Renames the file with the given index asynchronously. The rename // operation is complete when either a file_renamed_alert or diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 776e12177..b56862937 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -1805,14 +1805,14 @@ namespace libtorrent { } void disk_io_thread::async_move_storage(storage_index_t const storage - , std::string p, std::uint8_t const flags + , std::string p, move_flags_t const flags , std::function handler) { disk_io_job* j = allocate_job(disk_io_job::move_storage); j->storage = m_torrents[storage]->shared_from_this(); j->argument = std::move(p); j->callback = std::move(handler); - j->flags = flags; + j->move_flags = flags; add_fence_job(j); } @@ -2447,7 +2447,7 @@ namespace libtorrent { // if files have to be closed, that's the storage's responsibility return j->storage->move_storage(boost::get(j->argument) - , j->flags, j->error); + , j->move_flags, j->error); } status_t disk_io_thread::do_release_files(disk_io_job* j, jobqueue_t& completed_jobs) diff --git a/src/storage.cpp b/src/storage.cpp index fa3793d77..014d55e8c 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -450,8 +450,8 @@ namespace libtorrent { , m_file_priority, m_stat_cache, m_save_path, ec); } - status_t default_storage::move_storage(std::string const& sp, int const flags - , storage_error& ec) + status_t default_storage::move_storage(std::string const& sp + , move_flags_t const flags, storage_error& ec) { m_pool.release(storage_index()); @@ -737,7 +737,7 @@ namespace { void release_files(storage_error&) override {} void delete_files(int, storage_error&) override {} void initialize(storage_error&) override {} - status_t move_storage(std::string const&, int, storage_error&) override { return status_t::no_error; } + status_t move_storage(std::string const&, move_flags_t, storage_error&) override { return status_t::no_error; } int readv(span bufs , piece_index_t, int, open_mode_t, storage_error&) override @@ -796,7 +796,7 @@ namespace { void set_file_priority(aux::vector const& /* prio */ , storage_error&) override {} status_t move_storage(std::string const& /* save_path */ - , int /* flags */, storage_error&) override { return status_t::no_error; } + , move_flags_t, storage_error&) override { return status_t::no_error; } bool verify_resume_data(add_torrent_params const& /* rd */ , aux::vector const& /* links */ , storage_error&) override diff --git a/src/storage_utils.cpp b/src/storage_utils.cpp index 8fd242e01..be0f69ead 100644 --- a/src/storage_utils.cpp +++ b/src/storage_utils.cpp @@ -199,13 +199,13 @@ namespace libtorrent { namespace aux { , std::string const& save_path , std::string const& destination_save_path , part_file* pf - , int const flags, storage_error& ec) + , move_flags_t const flags, storage_error& ec) { status_t ret = status_t::no_error; std::string const new_save_path = complete(destination_save_path); // check to see if any of the files exist - if (flags == fail_if_exist) + if (flags == move_flags_t::fail_if_exist) { file_status s; error_code err; @@ -269,7 +269,7 @@ namespace libtorrent { namespace aux { std::string const old_path = combine_path(save_path, f.file_path(i)); std::string const new_path = combine_path(new_save_path, f.file_path(i)); - if (flags == dont_replace && exists(new_path)) + if (flags == move_flags_t::dont_replace && exists(new_path)) { if (ret == status_t::no_error) ret = status_t::need_full_check; continue; diff --git a/src/torrent.cpp b/src/torrent.cpp index ffd0d7384..8506fbbaa 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -7520,7 +7520,7 @@ namespace libtorrent { return; } - void torrent::move_storage(std::string const& save_path, int const flags) + void torrent::move_storage(std::string const& save_path, move_flags_t const flags) { TORRENT_ASSERT(is_single_thread()); INVARIANT_CHECK; @@ -7551,7 +7551,7 @@ namespace libtorrent { #else std::string path = save_path; #endif - m_ses.disk_thread().async_move_storage(m_storage, std::move(path), std::uint8_t(flags) + m_ses.disk_thread().async_move_storage(m_storage, std::move(path), flags , std::bind(&torrent::on_storage_moved, shared_from_this(), _1, _2, _3)); m_moving_storage = true; } diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index 231d8ea93..0948d99b1 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -212,18 +212,25 @@ namespace libtorrent { return sync_call_ret(0, &torrent::download_limit); } - void torrent_handle::move_storage( - std::string const& save_path, int flags) const + void torrent_handle::move_storage(std::string const& save_path, move_flags_t flags) const { async_call(&torrent::move_storage, save_path, flags); } +#ifndef TORRENT_NO_DEPRECATE + void torrent_handle::move_storage( + std::string const& save_path, int const flags) const + { + async_call(&torrent::move_storage, save_path, static_cast(flags)); + } +#endif + #if TORRENT_USE_WSTRING #ifndef TORRENT_NO_DEPRECATE void torrent_handle::move_storage( std::wstring const& save_path, int flags) const { - async_call(&torrent::move_storage, wchar_utf8(save_path), flags); + async_call(&torrent::move_storage, wchar_utf8(save_path), static_cast(flags)); } void torrent_handle::rename_file(file_index_t index, std::wstring const& new_name) const diff --git a/test/test_block_cache.cpp b/test/test_block_cache.cpp index d6aa7599d..0b21ba7a1 100644 --- a/test/test_block_cache.cpp +++ b/test/test_block_cache.cpp @@ -64,7 +64,7 @@ struct test_storage_impl : storage_interface bool has_any_file(storage_error& ec) override { return false; } void set_file_priority(aux::vector const& prio , storage_error& ec) override {} - status_t move_storage(std::string const& save_path, int flags + status_t move_storage(std::string const& save_path, move_flags_t flags , storage_error& ec) override { return status_t::no_error; } bool verify_resume_data(add_torrent_params const& rd , aux::vector const& links diff --git a/test/test_storage.cpp b/test/test_storage.cpp index 316105958..45f7995e2 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -1367,7 +1367,7 @@ TORRENT_TEST(move_storage_to_self) TEST_CHECK(exists(combine_path(test_path, combine_path("folder2", "test3.tmp")))); TEST_CHECK(exists(combine_path(test_path, combine_path("_folder3", "test4.tmp")))); - s->move_storage(save_path, 0, se); + s->move_storage(save_path, move_flags_t::always_replace_files, se); TEST_EQUAL(se.ec, boost::system::errc::success); TEST_CHECK(exists(test_path)); @@ -1394,7 +1394,7 @@ TORRENT_TEST(move_storage_into_self) s->writev(b, piece_index_t(2), 0, open_mode_t::read_write, se); std::string const test_path = combine_path(save_path, combine_path("temp_storage", "folder1")); - s->move_storage(test_path, 0, se); + s->move_storage(test_path, move_flags_t::always_replace_files, se); TEST_EQUAL(se.ec, boost::system::errc::success); TEST_CHECK(exists(combine_path(test_path, combine_path("temp_storage" @@ -1453,7 +1453,7 @@ TORRENT_TEST(dont_move_intermingled_files) f.close(); TEST_EQUAL(ec, boost::system::errc::success); - s->move_storage(test_path, 0, se); + s->move_storage(test_path, move_flags_t::always_replace_files, se); TEST_EQUAL(se.ec, boost::system::errc::success); // torrent files moved to new place