From cc30434c52d8c96922be8de51b4806273d2ea445 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sun, 9 Jul 2017 09:07:28 -0700 Subject: [PATCH] fix inconsistency in file_priorities and override_resume_data behavior. file_priorities are not subject to the override_resume_data flag --- ChangeLog | 2 ++ include/libtorrent/add_torrent_params.hpp | 7 ++-- src/torrent.cpp | 16 +++++++--- test/test_resume.cpp | 39 +++++++++++++++++++++++ 4 files changed, 56 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index ed815d253..fd7269b6e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,5 @@ + * fix inconsistency in file_priorities and override_resume_data behavior + 1.1.4 release * corrected missing const qualifiers on bdecode_node diff --git a/include/libtorrent/add_torrent_params.hpp b/include/libtorrent/add_torrent_params.hpp index e86636646..99c004857 100644 --- a/include/libtorrent/add_torrent_params.hpp +++ b/include/libtorrent/add_torrent_params.hpp @@ -157,8 +157,7 @@ namespace libtorrent // add_torrent_params configuring the torrent override the corresponding // configuration from the resume file, with the one exception of save // resume data, which has its own flag (for historic reasons). - // If this flag is set, but file_priorities is empty, file priorities - // are still loaded from the resume data, if present. + // "file_priorities" and "save_path" are not affected by this flag. flag_override_resume_data = 0x002, // If ``flag_upload_mode`` is set, the torrent will be initialized in @@ -347,7 +346,9 @@ namespace libtorrent // can be set to control the initial file priorities when adding a // torrent. The semantics are the same as for - // ``torrent_handle::prioritize_files()``. + // ``torrent_handle::prioritize_files()``. The file priorities specified + // in here take precedence over those specified in the resume data, if + // any. std::vector file_priorities; // torrent extension construction functions can be added to this vector diff --git a/src/torrent.cpp b/src/torrent.cpp index fa5f8660b..54a9dfac7 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -5637,7 +5637,7 @@ namespace libtorrent { inc_refcount("file_priority"); m_ses.disk_thread().async_set_file_priority(m_storage.get() - , m_file_priority, boost::bind(&torrent::on_file_priority, this)); + , m_file_priority, boost::bind(&torrent::on_file_priority, shared_from_this())); } update_piece_priorities(); @@ -5676,7 +5676,7 @@ namespace libtorrent { inc_refcount("file_priority"); m_ses.disk_thread().async_set_file_priority(m_storage.get() - , m_file_priority, boost::bind(&torrent::on_file_priority, this)); + , m_file_priority, boost::bind(&torrent::on_file_priority, shared_from_this())); } update_piece_priorities(); } @@ -7041,9 +7041,7 @@ namespace libtorrent if (m_completed_time != 0 && m_completed_time < m_added_time) m_completed_time = m_added_time; - // load file priorities except if the add_torrent_param file was set to - // override resume data - if (!m_override_resume_data || m_file_priority.empty()) + if (m_file_priority.empty()) { bdecode_node file_priority = rd.dict_find_list("file_priority"); if (file_priority) @@ -7071,6 +7069,14 @@ namespace libtorrent m_file_priority[i] = 0; } + // storage may be NULL during shutdown + if (m_storage) + { + inc_refcount("file_priority"); + m_ses.disk_thread().async_set_file_priority(m_storage.get() + , m_file_priority, boost::bind(&torrent::on_file_priority, shared_from_this())); + } + update_piece_priorities(); } } diff --git a/test/test_resume.cpp b/test/test_resume.cpp index de871d33a..d8e1acc7b 100644 --- a/test/test_resume.cpp +++ b/test/test_resume.cpp @@ -308,6 +308,45 @@ TORRENT_TEST(file_priorities_default) TEST_EQUAL(file_priorities[2], 4); } +// As long as the add_torrent_params priorities are empty, the file_priorities +// from the resume data should take effect +TORRENT_TEST(file_priorities_in_resume) +{ + lt::session ses(settings()); + std::vector file_priorities = test_resume_flags(ses, 0, "", "123").file_priorities(); + + TEST_EQUAL(file_priorities.size(), 3); + TEST_EQUAL(file_priorities[0], 1); + TEST_EQUAL(file_priorities[1], 2); + TEST_EQUAL(file_priorities[2], 3); +} + +// if both resume data and add_torrent_params has file_priorities, the +// add_torrent_params one take precedence +TORRENT_TEST(file_priorities_in_resume_and_params) +{ + lt::session ses(settings()); + std::vector file_priorities = test_resume_flags(ses, 0, "456", "123").file_priorities(); + + TEST_EQUAL(file_priorities.size(), 3); + TEST_EQUAL(file_priorities[0], 4); + TEST_EQUAL(file_priorities[1], 5); + TEST_EQUAL(file_priorities[2], 6); +} + +// if we set flag_override_resume_data, it should no affect file priorities +TORRENT_TEST(file_priorities_override_resume) +{ + lt::session ses(settings()); + std::vector file_priorities = test_resume_flags(ses + , add_torrent_params::flag_override_resume_data, "", "123").file_priorities(); + + TEST_EQUAL(file_priorities.size(), 3); + TEST_EQUAL(file_priorities[0], 1); + TEST_EQUAL(file_priorities[1], 2); + TEST_EQUAL(file_priorities[2], 3); +} + TORRENT_TEST(file_priorities_resume_seed_mode) { // in share mode file priorities should always be 0