Add flush_disk_cache parameter to save_resume_data

Using this flag avoids potential issue with file timestamps and
is only needed when stopping the session.
This commit is contained in:
Calum Lind 2014-02-24 15:14:34 +00:00
parent f2535e196d
commit bc7380c5d7
2 changed files with 20 additions and 10 deletions

View File

@ -1034,12 +1034,18 @@ class Torrent(object):
return False
return True
def save_resume_data(self):
def save_resume_data(self, flush_disk_cache=False):
"""Signals libtorrent to build resume data for this torrent.
The response is returned in a libtorrent alert.
Params:
flush_disk_cache (bool): Avoids potential issue with file timestamps
and is only needed when stopping the session.
Returns:
None: The response with resume data is returned in a libtorrent save_resume_data_alert.
"""
self.handle.save_resume_data()
flags = lt.save_resume_flags_t.flush_disk_cache if flush_disk_cache else 0
self.handle.save_resume_data(flags)
def write_torrentfile(self):
"""Writes the torrent file to the state directory in config"""

View File

@ -268,7 +268,7 @@ class TorrentManager(component.Component):
if result and os.path.isfile(self.temp_file):
os.remove(self.temp_file)
d = self.save_resume_data()
d = self.save_resume_data(flush_disk_cache=True)
d.addCallback(remove_temp_file)
return d
@ -753,29 +753,33 @@ class TorrentManager(component.Component):
# We return True so that the timer thread will continue
return True
def save_resume_data(self, torrent_ids=None):
def save_resume_data(self, torrent_ids=None, flush_disk_cache=False):
"""Saves resume data for list of torrent_ids or for all torrents
needing resume data updated if torrent_ids is None
:returns: A Deferred whose callback will be invoked when save is complete
:rtype: twisted.internet.defer.Deferred
Params:
torrent_ids (list of str) (
flush_disk_cache (bool): Avoids potential issue with file timestamps
and is only needed when stopping the session.
Returns:
t.i.d.DeferredList: A list of twisted Deferred callbacks that will
be invoked when save is complete.
"""
if torrent_ids is None:
torrent_ids = (t[0] for t in self.torrents.iteritems() if t[1].handle.need_save_resume_data())
deferreds = []
def on_torrent_resume_save(dummy_result, torrent_id):
self.waiting_on_resume_data.pop(torrent_id, None)
deferreds = []
for torrent_id in torrent_ids:
d = self.waiting_on_resume_data.get(torrent_id)
if not d:
d = Deferred().addBoth(on_torrent_resume_save, torrent_id)
self.waiting_on_resume_data[torrent_id] = d
deferreds.append(d)
self.torrents[torrent_id].save_resume_data()
self.torrents[torrent_id].save_resume_data(flush_disk_cache)
def on_all_resume_data_finished(result):
if result: