Browse Source

- BUGFIX: Added safety mecanism which adds the torrents back to the list in case qbittorrent-resume.conf gets deleted or corrupted.

adaptive-webui-19844
Christophe Dumez 15 years ago
parent
commit
5d1ed7a564
  1. 154
      src/bittorrent.cpp

154
src/bittorrent.cpp

@ -1529,14 +1529,31 @@ void bittorrent::applyEncryptionSettings(pe_settings se) {
// backup directory // backup directory
void bittorrent::startUpTorrents() { void bittorrent::startUpTorrents() {
qDebug("Resuming unfinished torrents"); qDebug("Resuming unfinished torrents");
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
QDir torrentBackup(misc::qBittorrentPath() + "BT_backup"); QDir torrentBackup(misc::qBittorrentPath() + "BT_backup");
QStringList fileNames; QStringList fileNames;
QStringList known_torrents = TorrentPersistentData::knownTorrents(); QStringList known_torrents = TorrentPersistentData::knownTorrents();
if(known_torrents.empty()) {
if(known_torrents.empty() && !settings.value("v1_4_x_torrent_imported", false).toBool()) {
qDebug("No known torrent, importing old torrents"); qDebug("No known torrent, importing old torrents");
importOldTorrents(); importOldTorrents();
return; return;
} }
// Safety measure because some people reported torrent loss since
// we switch the v1.5 way of resuming torrents on startup
QStringList filters;
filters << "*.torrent";
QStringList torrents_on_hd = torrentBackup.entryList(filters, QDir::Files, QDir::Unsorted);
foreach(QString hash, torrents_on_hd) {
hash.chop(8); // remove trailing .torrent
if(!known_torrents.contains(hash)) {
std::cerr << "ERROR Detected!!! Adding back torrent " << hash.toLocal8Bit().data() << " which got lost for some reason." << std::endl;
addTorrent(torrentBackup.path()+QDir::separator()+hash+".torrent", false, QString(), true);
}
}
// End of safety measure
qDebug("Starting up torrents"); qDebug("Starting up torrents");
if(isQueueingEnabled()) { if(isQueueingEnabled()) {
QList<QPair<int, QString> > hashes; QList<QPair<int, QString> > hashes;
@ -1697,79 +1714,78 @@ void bittorrent::applyFormerAttributeFiles(QTorrentHandle h) {
// TODO: Remove in qBittorrent v1.6.0 // TODO: Remove in qBittorrent v1.6.0
void bittorrent::importOldTorrents() { void bittorrent::importOldTorrents() {
QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
if(!settings.value("v1_4_x_torrent_imported", false).toBool()) { Q_ASSERT(!settings.value("v1_4_x_torrent_imported", false).toBool());
// Import old torrent // Import old torrent
QDir torrentBackup(misc::qBittorrentPath() + "BT_backup"); QDir torrentBackup(misc::qBittorrentPath() + "BT_backup");
QStringList fileNames; QStringList fileNames;
QStringList filters; QStringList filters;
filters << "*.torrent"; filters << "*.torrent";
fileNames = torrentBackup.entryList(filters, QDir::Files, QDir::Unsorted); fileNames = torrentBackup.entryList(filters, QDir::Files, QDir::Unsorted);
if(isQueueingEnabled()) { if(isQueueingEnabled()) {
QList<QPair<int, QString> > filePaths; QList<QPair<int, QString> > filePaths;
foreach(const QString &fileName, fileNames) { foreach(const QString &fileName, fileNames) {
QString filePath = torrentBackup.path()+QDir::separator()+fileName; QString filePath = torrentBackup.path()+QDir::separator()+fileName;
int prio = 99999; int prio = 99999;
// Get priority // Get priority
QString prioPath = filePath; QString prioPath = filePath;
prioPath.replace(".torrent", ".prio"); prioPath.replace(".torrent", ".prio");
if(QFile::exists(prioPath)) { if(QFile::exists(prioPath)) {
QFile prio_file(prioPath); QFile prio_file(prioPath);
if(prio_file.open(QIODevice::ReadOnly | QIODevice::Text)) { if(prio_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
bool ok = false; bool ok = false;
prio = prio_file.readAll().toInt(&ok); prio = prio_file.readAll().toInt(&ok);
if(!ok) if(!ok)
prio = 99999; prio = 99999;
prio_file.close(); prio_file.close();
}
} }
misc::insertSort2<QString>(filePaths, qMakePair(prio, filePath));
} }
// Resume downloads misc::insertSort2<QString>(filePaths, qMakePair(prio, filePath));
QPair<int, QString> fileName; }
foreach(fileName, filePaths) { // Resume downloads
importOldTempData(fileName.second); QPair<int, QString> fileName;
QTorrentHandle h = addTorrent(fileName.second, false, QString(), true); foreach(fileName, filePaths) {
// Sequential download importOldTempData(fileName.second);
if(TorrentTempData::hasTempData(h.hash())) { QTorrentHandle h = addTorrent(fileName.second, false, QString(), true);
qDebug("addTorrent: Setting download as sequential (from tmp data)"); // Sequential download
h.set_sequential_download(TorrentTempData::isSequential(h.hash())); if(TorrentTempData::hasTempData(h.hash())) {
} qDebug("addTorrent: Setting download as sequential (from tmp data)");
applyFormerAttributeFiles(h); h.set_sequential_download(TorrentTempData::isSequential(h.hash()));
QString savePath = TorrentTempData::getSavePath(h.hash());
// Save persistent data for new torrent
TorrentPersistentData::saveTorrentPersistentData(h);
// Save save_path
if(!defaultTempPath.isEmpty() && !savePath.isNull()) {
qDebug("addTorrent: Saving save_path in persistent data: %s", savePath.toLocal8Bit().data());
TorrentPersistentData::saveSavePath(h.hash(), savePath);
}
} }
} else { applyFormerAttributeFiles(h);
QStringList filePaths; QString savePath = TorrentTempData::getSavePath(h.hash());
foreach(const QString &fileName, fileNames) { // Save persistent data for new torrent
filePaths.append(torrentBackup.path()+QDir::separator()+fileName); TorrentPersistentData::saveTorrentPersistentData(h);
// Save save_path
if(!defaultTempPath.isEmpty() && !savePath.isNull()) {
qDebug("addTorrent: Saving save_path in persistent data: %s", savePath.toLocal8Bit().data());
TorrentPersistentData::saveSavePath(h.hash(), savePath);
} }
// Resume downloads }
foreach(const QString &fileName, filePaths) { } else {
importOldTempData(fileName); QStringList filePaths;
QTorrentHandle h = addTorrent(fileName, false, QString(), true); foreach(const QString &fileName, fileNames) {
// Sequential download filePaths.append(torrentBackup.path()+QDir::separator()+fileName);
if(TorrentTempData::hasTempData(h.hash())) { }
qDebug("addTorrent: Setting download as sequential (from tmp data)"); // Resume downloads
h.set_sequential_download(TorrentTempData::isSequential(h.hash())); foreach(const QString &fileName, filePaths) {
} importOldTempData(fileName);
applyFormerAttributeFiles(h); QTorrentHandle h = addTorrent(fileName, false, QString(), true);
QString savePath = TorrentTempData::getSavePath(h.hash()); // Sequential download
// Save persistent data for new torrent if(TorrentTempData::hasTempData(h.hash())) {
TorrentPersistentData::saveTorrentPersistentData(h); qDebug("addTorrent: Setting download as sequential (from tmp data)");
// Save save_path h.set_sequential_download(TorrentTempData::isSequential(h.hash()));
if(!defaultTempPath.isEmpty() && !savePath.isNull()) { }
qDebug("addTorrent: Saving save_path in persistent data: %s", savePath.toLocal8Bit().data()); applyFormerAttributeFiles(h);
TorrentPersistentData::saveSavePath(h.hash(), savePath); QString savePath = TorrentTempData::getSavePath(h.hash());
} // Save persistent data for new torrent
TorrentPersistentData::saveTorrentPersistentData(h);
// Save save_path
if(!defaultTempPath.isEmpty() && !savePath.isNull()) {
qDebug("addTorrent: Saving save_path in persistent data: %s", savePath.toLocal8Bit().data());
TorrentPersistentData::saveSavePath(h.hash(), savePath);
} }
} }
settings.setValue("v1_4_x_torrent_imported", true);
std::cout << "Successfully imported torrents from v1.4.x (or previous) instance" << std::endl;
} }
settings.setValue("v1_4_x_torrent_imported", true);
std::cout << "Successfully imported torrents from v1.4.x (or previous) instance" << std::endl;
} }

Loading…
Cancel
Save