From b5efe1c92af5ed374aa9cbefa270743aefcde23f Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 12 Apr 2010 00:36:31 +0000 Subject: [PATCH] added feature to ask a torrent if it needs to save its resume data or not --- ChangeLog | 1 + bindings/python/src/torrent_handle.cpp | 1 + docs/manual.rst | 12 ++++++++++++ include/libtorrent/torrent.hpp | 7 +++++-- include/libtorrent/torrent_handle.hpp | 1 + src/torrent.cpp | 8 +++++++- src/torrent_handle.cpp | 6 ++++++ 7 files changed, 33 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index c780deb36..fa034441d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * added feature to ask a torrent if it needs to save its resume data or not * added setting to ignore file modification time when loading resume files * support more fine-grained torrent states between which peer sources it announces to diff --git a/bindings/python/src/torrent_handle.cpp b/bindings/python/src/torrent_handle.cpp index 69bef1041..a06825cb5 100644 --- a/bindings/python/src/torrent_handle.cpp +++ b/bindings/python/src/torrent_handle.cpp @@ -335,6 +335,7 @@ void bind_torrent_handle() .def("file_priorities", &file_priorities) .def("use_interface", &torrent_handle::use_interface) .def("save_resume_data", _(&torrent_handle::save_resume_data)) + .def("need_save_resume_data", _(&torrent_handle::need_save_resume_data)) .def("force_reannounce", _(force_reannounce0)) .def("force_reannounce", &force_reannounce) .def("force_dht_announce", _(&torrent_handle::force_dht_announce)) diff --git a/docs/manual.rst b/docs/manual.rst index 058bafcc6..54958b93a 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -1979,6 +1979,7 @@ Its declaration looks like this:: std::string name() const; void save_resume_data() const; + bool need_save_resume_data() const; void force_reannounce() const; void force_dht_announce() const; void force_reannounce(boost::posix_time::time_duration) const; @@ -2857,6 +2858,17 @@ Example code to pause and save resume data for all torrents and wait for the ale } +need_save_resume_data() +----------------------- + + :: + + bool need_save_resume_data() const; + +This function returns true if any whole chunk has been downloaded since the +torrent was first loaded or since the last time the resume data was saved. When +saving resume data periodically, it makes sense to skip any torrent which hasn't +downloaded anything since the last time. status() -------- diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 2c83b2fed..1ead7ea52 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -266,6 +266,7 @@ namespace libtorrent bool is_torrent_paused() const { return !m_allow_peers; } void force_recheck(); void save_resume_data(); + bool need_save_resume_data() const { return m_need_save_resume_data; } bool is_auto_managed() const { return m_auto_managed; } void auto_managed(bool a); @@ -1070,8 +1071,10 @@ namespace libtorrent #else unsigned int m_dummy_padding_bits_to_align:2; #endif - // TODO: Add new bools here! - unsigned int m_dummy_padding_bit_to_alignt:1; + + // set to false when saving resume data. Set to true + // whenever something is downloaded + bool m_need_save_resume_data:1; // total time we've been available as a seed on this torrent // does not count when the torrent is stopped or paused diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index ec8cae96c..b77718e07 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -466,6 +466,7 @@ namespace libtorrent void force_recheck() const; void save_resume_data() const; + bool need_save_resume_data() const; bool is_auto_managed() const; void auto_managed(bool m) const; diff --git a/src/torrent.cpp b/src/torrent.cpp index c28bbd52f..ed39a22c1 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -354,6 +354,7 @@ namespace libtorrent , m_resolving_country(false) , m_resolve_countries(false) #endif + , m_need_save_resume_data(false) , m_seeding_time(0) , m_time_scaler(0) , m_max_uploads(~0) @@ -713,7 +714,7 @@ namespace libtorrent mutex::scoped_lock l(m_ses.m_mutex); INVARIANT_CHECK; - + if (is_seed()) return; if (m_abort) @@ -735,6 +736,8 @@ namespace libtorrent // add_piece() multiple times if (picker().is_finished(block_finished)) return; + m_need_save_resume_data = true; + picker().mark_as_finished(block_finished, 0); // did we just finish the piece? @@ -2557,6 +2560,7 @@ namespace libtorrent } else { + m_need_save_resume_data = false; write_resume_data(*j.resume_data); alerts().post_alert(save_resume_data_alert(j.resume_data , get_handle())); @@ -5227,6 +5231,8 @@ namespace libtorrent return; } + m_need_save_resume_data = false; + TORRENT_ASSERT(m_storage); if (m_state == torrent_status::queued_for_checking || m_state == torrent_status::checking_files diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index 230270845..c099cae30 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -304,6 +304,12 @@ namespace libtorrent TORRENT_FORWARD(save_resume_data()); } + bool torrent_handle::need_save_resume_data() const + { + INVARIANT_CHECK; + TORRENT_FORWARD_RETURN(need_save_resume_data(), false); + } + void torrent_handle::force_recheck() const { INVARIANT_CHECK;