From aa5863683221d176b42df03743b9fc560f1f5b30 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sun, 25 Jul 2010 14:55:30 +0000 Subject: [PATCH] When resuming a torrent with error, checking if the previous data is present before redownloading it. (closes #609748) --- src/bittorrent.cpp | 8 ++++++-- src/qtorrenthandle.cpp | 17 +++++++++++++++++ src/torrentpersistentdata.h | 16 ++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index 358b75b46..949402dab 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -2195,7 +2195,9 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { #endif else if (torrent_paused_alert* p = dynamic_cast(a.get())) { if(p->handle.is_valid()) { - p->handle.save_resume_data(); + QTorrentHandle h(p->handle); + if(!h.has_error()) + h.save_resume_data(); } } else if (tracker_error_alert* p = dynamic_cast(a.get())) { @@ -2281,9 +2283,11 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { #else if(p->error.value() == 134) { #endif + const QString hash = h.hash(); // Mismatching file size (files were probably moved addConsoleMessage(tr("File sizes mismatch for torrent %1, pausing it.").arg(h.name())); - pauseTorrent(h.hash()); + pauseTorrent(hash); + TorrentPersistentData::setErrorState(hash, true); } else { addConsoleMessage(tr("Fast resume data was rejected for torrent %1, checking again...").arg(h.name()), QString::fromUtf8("red")); addConsoleMessage(tr("Reason: %1").arg(misc::toQString(p->message()))); diff --git a/src/qtorrenthandle.cpp b/src/qtorrenthandle.cpp index 62ee6bdab..e3cee0965 100644 --- a/src/qtorrenthandle.cpp +++ b/src/qtorrenthandle.cpp @@ -35,6 +35,7 @@ #include #include #include "misc.h" +#include "preferences.h" #include "qtorrenthandle.h" #include "torrentpersistentdata.h" #include @@ -510,8 +511,24 @@ void QTorrentHandle::pause() { void QTorrentHandle::resume() { Q_ASSERT(h.is_valid()); if(has_error()) h.clear_error(); + const QString torrent_hash = hash(); + bool has_persistant_error = TorrentPersistentData::hasError(torrent_hash); + TorrentPersistentData::setErrorState(torrent_hash, false); + bool temp_path_enabled = Preferences::isTempPathEnabled(); + if(has_persistant_error && temp_path_enabled) { + // Torrent was supposed to be seeding, checking again in final destination + qDebug("Resuming a torrent with error..."); + const QString final_save_path = TorrentPersistentData::getSavePath(torrent_hash); + qDebug("Torrent final path is: %s", qPrintable(final_save_path)); + if(!final_save_path.isEmpty()) + move_storage(final_save_path); + } h.auto_managed(true); h.resume(); + if(has_persistant_error && temp_path_enabled) { + // Force recheck + h.force_recheck(); + } } void QTorrentHandle::remove_url_seed(QString seed) { diff --git a/src/torrentpersistentdata.h b/src/torrentpersistentdata.h index f53e1e769..fe8e22b56 100644 --- a/src/torrentpersistentdata.h +++ b/src/torrentpersistentdata.h @@ -227,6 +227,22 @@ public: return dt; } + static void setErrorState(QString hash, bool has_error) { + QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); + QHash all_data = settings.value("torrents", QHash()).toHash(); + QHash data = all_data[hash].toHash(); + data.insert("has_error", has_error); + all_data[hash] = data; + settings.setValue("torrents", all_data); + } + + static bool hasError(QString hash) { + QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); + QHash all_data = settings.value("torrents", QHash()).toHash(); + QHash data = all_data[hash].toHash(); + return data.value("has_error", false).toBool(); + } + static void setRootFolder(QString hash, QString root_folder) { QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); QHash all_data = settings.value("torrents", QHash()).toHash();