From a1a5fb065e33caa7ec1e0d430b8413a652a11096 Mon Sep 17 00:00:00 2001 From: sledgehammer999 Date: Sun, 29 Sep 2013 21:32:27 +0300 Subject: [PATCH] Backup/recover torrent persistent data into each individual .fastresume file. This should mitigate the problem of users losing their torrents' settings/savepath/label after qbt wasn't shutdown cleanly. --- src/qtlibtorrent/qbtsession.cpp | 45 ++++++++++++++++++++++++++++++++- src/qtlibtorrent/qbtsession.h | 2 ++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index da41f6bd1..52d146500 100755 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -61,7 +61,7 @@ #include #include //#include -#include +#include #include #include #include @@ -1117,6 +1117,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr } #endif + recoverPersistentData(hash, buf); QString savePath; if (!from_url.isEmpty() && savepathLabel_fromurl.contains(QUrl::fromEncoded(from_url.toUtf8()))) { // Enforcing the save path defined before URL download (from RSS for example) @@ -1684,6 +1685,7 @@ void QBtSession::saveFastResumeData() { if (!h.is_valid()) continue; try { // Remove old fastresume file if it exists + backupPersistentData(h.hash(), rd->resume_data); vector out; bencode(back_inserter(out), *rd->resume_data); const QString filepath = torrentBackup.absoluteFilePath(h.hash()+".fastresume"); @@ -2307,6 +2309,7 @@ void QBtSession::readAlerts() { if (resume_file.exists()) fsutils::forceRemove(filepath); qDebug("Saving fastresume data in %s", qPrintable(filepath)); + backupPersistentData(h.hash(), p->resume_data); vector out; bencode(back_inserter(out), *p->resume_data); if (!out.empty() && resume_file.open(QIODevice::WriteOnly)) { @@ -2870,3 +2873,43 @@ entry QBtSession::generateFilePriorityResumeData(boost::intrusive_ptr &buf) { + if (TorrentPersistentData::isKnownTorrent(hash) || TorrentTempData::hasTempData(hash) || buf.empty()) + return; + + libtorrent::lazy_entry fast; + libtorrent::error_code ec; + + libtorrent::lazy_bdecode(&(buf.front()), &(buf.back()), fast, ec); + if (fast.type() != libtorrent::lazy_entry::dict_t && !ec) + return; + + QString savePath = QString::fromUtf8(fast.dict_find_string_value("qBt-savePath").c_str()); + qreal ratioLimit = QString::fromUtf8(fast.dict_find_string_value("qBt-ratioLimit").c_str()).toDouble(); + QDateTime addedDate = QDateTime::fromTime_t(fast.dict_find_int_value("added_time")); + QString previousSavePath = QString::fromUtf8(fast.dict_find_string_value("qBt-previousSavePath").c_str()); + QDateTime seedDate = QDateTime::fromTime_t(fast.dict_find_int_value("qBt-seedDate")); + QString label = QString::fromUtf8(fast.dict_find_string_value("qBt-label").c_str()); + int priority = fast.dict_find_int_value("qBt-queuePosition"); + bool seedStatus = fast.dict_find_int_value("qBt-seedStatus"); + + TorrentPersistentData::saveSavePath(hash, savePath); + TorrentPersistentData::setRatioLimit(hash, ratioLimit); + TorrentPersistentData::setAddedDate(hash, addedDate); + TorrentPersistentData::setPreviousSavePath(hash, previousSavePath); + TorrentPersistentData::saveSeedDate(hash, seedDate); + TorrentPersistentData::saveLabel(hash, label); + TorrentPersistentData::savePriority(hash, priority); + TorrentPersistentData::saveSeedStatus(hash, seedStatus); +} + +void QBtSession::backupPersistentData(const QString &hash, boost::shared_ptr data) { + (*data)["qBt-savePath"] = TorrentPersistentData::getSavePath(hash).toUtf8().constData(); + (*data)["qBt-ratioLimit"] = QString::number(TorrentPersistentData::getRatioLimit(hash)).toUtf8().constData(); + (*data)["qBt-previousSavePath"] = TorrentPersistentData::getPreviousPath(hash).toUtf8().constData(); + (*data)["qBt-seedDate"] = TorrentPersistentData::getSeedDate(hash).toTime_t(); + (*data)["qBt-label"] = TorrentPersistentData::getLabel(hash).toUtf8().constData(); + (*data)["qBt-queuePosition"] = TorrentPersistentData::getPriority(hash); + (*data)["qBt-seedStatus"] = (int)TorrentPersistentData::isSeed(hash); +} diff --git a/src/qtlibtorrent/qbtsession.h b/src/qtlibtorrent/qbtsession.h index 31eb06999..f7f52142e 100755 --- a/src/qtlibtorrent/qbtsession.h +++ b/src/qtlibtorrent/qbtsession.h @@ -184,6 +184,8 @@ private: libtorrent::add_torrent_params initializeAddTorrentParams(const QString &hash); libtorrent::entry generateFilePriorityResumeData(boost::intrusive_ptr &t, const std::vector &fp); void updateRatioTimer(); + void recoverPersistentData(const QString &hash, const std::vector &buf); + void backupPersistentData(const QString &hash, boost::shared_ptr data); private slots: void addTorrentsFromScanFolder(QStringList&);