Browse Source

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.

adaptive-webui-19844
sledgehammer999 11 years ago
parent
commit
a1a5fb065e
  1. 45
      src/qtlibtorrent/qbtsession.cpp
  2. 2
      src/qtlibtorrent/qbtsession.h

45
src/qtlibtorrent/qbtsession.cpp

@ -61,7 +61,7 @@
#include <libtorrent/extensions/ut_pex.hpp> #include <libtorrent/extensions/ut_pex.hpp>
#include <libtorrent/extensions/smart_ban.hpp> #include <libtorrent/extensions/smart_ban.hpp>
//#include <libtorrent/extensions/metadata_transfer.hpp> //#include <libtorrent/extensions/metadata_transfer.hpp>
#include <libtorrent/entry.hpp> #include <libtorrent/lazy_entry.hpp>
#include <libtorrent/bencode.hpp> #include <libtorrent/bencode.hpp>
#include <libtorrent/error_code.hpp> #include <libtorrent/error_code.hpp>
#include <libtorrent/identify_client.hpp> #include <libtorrent/identify_client.hpp>
@ -1117,6 +1117,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr
} }
#endif #endif
recoverPersistentData(hash, buf);
QString savePath; QString savePath;
if (!from_url.isEmpty() && savepathLabel_fromurl.contains(QUrl::fromEncoded(from_url.toUtf8()))) { if (!from_url.isEmpty() && savepathLabel_fromurl.contains(QUrl::fromEncoded(from_url.toUtf8()))) {
// Enforcing the save path defined before URL download (from RSS for example) // Enforcing the save path defined before URL download (from RSS for example)
@ -1684,6 +1685,7 @@ void QBtSession::saveFastResumeData() {
if (!h.is_valid()) continue; if (!h.is_valid()) continue;
try { try {
// Remove old fastresume file if it exists // Remove old fastresume file if it exists
backupPersistentData(h.hash(), rd->resume_data);
vector<char> out; vector<char> out;
bencode(back_inserter(out), *rd->resume_data); bencode(back_inserter(out), *rd->resume_data);
const QString filepath = torrentBackup.absoluteFilePath(h.hash()+".fastresume"); const QString filepath = torrentBackup.absoluteFilePath(h.hash()+".fastresume");
@ -2307,6 +2309,7 @@ void QBtSession::readAlerts() {
if (resume_file.exists()) if (resume_file.exists())
fsutils::forceRemove(filepath); fsutils::forceRemove(filepath);
qDebug("Saving fastresume data in %s", qPrintable(filepath)); qDebug("Saving fastresume data in %s", qPrintable(filepath));
backupPersistentData(h.hash(), p->resume_data);
vector<char> out; vector<char> out;
bencode(back_inserter(out), *p->resume_data); bencode(back_inserter(out), *p->resume_data);
if (!out.empty() && resume_file.open(QIODevice::WriteOnly)) { if (!out.empty() && resume_file.open(QIODevice::WriteOnly)) {
@ -2870,3 +2873,43 @@ entry QBtSession::generateFilePriorityResumeData(boost::intrusive_ptr<torrent_in
Q_ASSERT(ret.type() == entry::dictionary_t); Q_ASSERT(ret.type() == entry::dictionary_t);
return ret; return ret;
} }
void QBtSession::recoverPersistentData(const QString &hash, const std::vector<char> &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<libtorrent::entry> 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);
}

2
src/qtlibtorrent/qbtsession.h

@ -184,6 +184,8 @@ private:
libtorrent::add_torrent_params initializeAddTorrentParams(const QString &hash); libtorrent::add_torrent_params initializeAddTorrentParams(const QString &hash);
libtorrent::entry generateFilePriorityResumeData(boost::intrusive_ptr<libtorrent::torrent_info> &t, const std::vector<int> &fp); libtorrent::entry generateFilePriorityResumeData(boost::intrusive_ptr<libtorrent::torrent_info> &t, const std::vector<int> &fp);
void updateRatioTimer(); void updateRatioTimer();
void recoverPersistentData(const QString &hash, const std::vector<char> &buf);
void backupPersistentData(const QString &hash, boost::shared_ptr<libtorrent::entry> data);
private slots: private slots:
void addTorrentsFromScanFolder(QStringList&); void addTorrentsFromScanFolder(QStringList&);

Loading…
Cancel
Save