Browse Source

- Should fix fastresume data code

adaptive-webui-19844
Christophe Dumez 16 years ago
parent
commit
453dd93abf
  1. 75
      src/bittorrent.cpp

75
src/bittorrent.cpp

@ -54,6 +54,7 @@ bittorrent::bittorrent() : timerScan(0), DHTEnabled(false), preAllocateAll(false
s = new session(fingerprint("qB", VERSION_MAJOR, VERSION_MINOR, VERSION_BUGFIX, 0)); s = new session(fingerprint("qB", VERSION_MAJOR, VERSION_MINOR, VERSION_BUGFIX, 0));
} }
// Set severity level of libtorrent session // Set severity level of libtorrent session
//s->set_alert_mask(alert::all_categories & ~alert::progress_notification);
s->set_alert_mask(alert::error_notification | alert::peer_notification | alert::port_mapping_notification | alert::storage_notification | alert::tracker_notification | alert::status_notification | alert::ip_block_notification); s->set_alert_mask(alert::error_notification | alert::peer_notification | alert::port_mapping_notification | alert::storage_notification | alert::tracker_notification | alert::status_notification | alert::ip_block_notification);
// Enabling metadata plugin // Enabling metadata plugin
s->add_extension(&create_metadata_plugin); s->add_extension(&create_metadata_plugin);
@ -739,8 +740,6 @@ void bittorrent::setFinishedTorrent(QString hash) {
updateUploadQueue(); updateUploadQueue();
} }
} }
// Save fast resume data
saveFastResumeData(hash);
//emit torrentSwitchedtoFinished(hash); //emit torrentSwitchedtoFinished(hash);
} }
@ -943,9 +942,11 @@ void bittorrent::addTorrent(QString path, bool fromScanDir, QString from_url, bo
add_torrent_params p; add_torrent_params p;
//Getting fast resume data if existing //Getting fast resume data if existing
std::vector<char> buf; std::vector<char> buf;
if (load_file((torrentBackup.path()+hash+QString(".fastresume")).toUtf8().data(), buf) == 0) { qDebug("Trying to load fastresume data: %s", (torrentBackup.path()+QDir::separator()+hash+QString(".fastresume")).toUtf8().data());
if (load_file((torrentBackup.path()+QDir::separator()+hash+QString(".fastresume")).toUtf8().data(), buf) == 0) {
fastResume = true; fastResume = true;
p.resume_data = &buf; p.resume_data = &buf;
qDebug("Successfuly loaded");
} }
QString savePath = getSavePath(hash); QString savePath = getSavePath(hash);
// Save save_path to hard drive // Save save_path to hard drive
@ -1299,31 +1300,46 @@ float bittorrent::getRealRatio(QString hash) const{
// Only save fast resume data for unfinished and unpaused torrents (Optimization) // Only save fast resume data for unfinished and unpaused torrents (Optimization)
// Called periodically and on exit // Called periodically and on exit
void bittorrent::saveFastResumeData() { void bittorrent::saveFastResumeData() {
QString hash; // Stop listening for alerts
QStringList hashes = getUnfinishedTorrents(); timerAlerts->stop();
foreach(hash, hashes) { int num_resume_data = 0;
QTorrentHandle h = getTorrentHandle(hash); s->pause();
if(!h.is_valid()) { std::vector<torrent_handle> torrents = s->get_torrents();
qDebug("/!\\ Error: Invalid handle"); for(unsigned int i=0; i<torrents.size(); ++i) {
continue; QTorrentHandle h(torrents[i]);
} if(!h.is_valid()) continue;
if(h.is_paused()) { if(h.is_paused()) continue;
// Do not need to save fast resume data for paused torrents if (!h.has_metadata()) continue;
continue; h.save_resume_data();
} ++num_resume_data;
saveFastResumeData(hash);
} }
hashes = getFinishedTorrents(); while (num_resume_data > 0)
foreach(hash, hashes) { {
QTorrentHandle h = getTorrentHandle(hash); alert const* a = s->wait_for_alert(seconds(30));
if(!h.is_valid()) { if (a == 0)
qDebug("/!\\ Error: Invalid handle"); {
continue; std::cerr << " aborting with " << num_resume_data << " outstanding "
"torrents to save resume data for" << std::endl;
break;
} }
if(h.is_paused()) { qDebug("Received an alert...");
// Do not need to save ratio data for paused torrents save_resume_data_alert const* rd = dynamic_cast<save_resume_data_alert const*>(a);
if (!rd) {
s->pop_alert();
continue; continue;
} }
qDebug("Alert is for resume data");
--num_resume_data;
if (!rd->resume_data) continue;
qDebug("saving resume data: %d", num_resume_data);
QDir torrentBackup(misc::qBittorrentPath() + "BT_backup");
QTorrentHandle h(rd->handle);
QFile::remove(torrentBackup.path()+QDir::separator()+ h.hash() + ".fastresume");
QString file = h.hash()+".fastresume";
boost::filesystem::ofstream out(fs::path(torrentBackup.path().toUtf8().data()) / file.toUtf8().data(), std::ios_base::binary);
out.unsetf(std::ios_base::skipws);
bencode(std::ostream_iterator<char>(out), *rd->resume_data);
s->pop_alert();
} }
} }
@ -1355,11 +1371,6 @@ void bittorrent::addPeerBanMessage(QString ip, bool from_ipfilter) {
void bittorrent::saveFastResumeData(QString hash) { void bittorrent::saveFastResumeData(QString hash) {
QString file; QString file;
QDir torrentBackup(misc::qBittorrentPath() + "BT_backup"); QDir torrentBackup(misc::qBittorrentPath() + "BT_backup");
// Checking if torrentBackup Dir exists
// create it if it is not
if(! torrentBackup.exists()) {
torrentBackup.mkpath(torrentBackup.path());
}
// Extracting resume data // Extracting resume data
QTorrentHandle h = getTorrentHandle(hash); QTorrentHandle h = getTorrentHandle(hash);
if(!h.is_valid()) { if(!h.is_valid()) {
@ -1367,14 +1378,12 @@ void bittorrent::saveFastResumeData(QString hash) {
return; return;
} }
if (h.has_metadata() && h.state() != torrent_status::checking_files && h.state() != torrent_status::queued_for_checking) { if (h.has_metadata() && h.state() != torrent_status::checking_files && h.state() != torrent_status::queued_for_checking) {
if(QFile::exists(torrentBackup.path()+QDir::separator()+hash+".torrent")) {
// Remove old .fastresume data in case it exists // Remove old .fastresume data in case it exists
QFile::remove(torrentBackup.path()+QDir::separator()+hash + ".fastresume"); QFile::remove(torrentBackup.path()+QDir::separator()+hash + ".fastresume");
// Write fast resume data // Write fast resume data
h.save_resume_data(); h.save_resume_data();
} }
} }
}
bool bittorrent::isFilePreviewPossible(QString hash) const{ bool bittorrent::isFilePreviewPossible(QString hash) const{
// See if there are supported files in the torrent // See if there are supported files in the torrent
@ -1620,6 +1629,7 @@ void bittorrent::readAlerts() {
QTorrentHandle h(p->handle); QTorrentHandle h(p->handle);
if(h.is_valid()){ if(h.is_valid()){
QString hash = h.hash(); QString hash = h.hash();
h.save_resume_data();
qDebug("Received finished alert for %s", h.name().toUtf8().data()); qDebug("Received finished alert for %s", h.name().toUtf8().data());
setFinishedTorrent(hash); setFinishedTorrent(hash);
emit finishedTorrent(h); emit finishedTorrent(h);
@ -1628,7 +1638,8 @@ void bittorrent::readAlerts() {
else if (save_resume_data_alert* p = dynamic_cast<save_resume_data_alert*>(a.get())) { else if (save_resume_data_alert* p = dynamic_cast<save_resume_data_alert*>(a.get())) {
QDir torrentBackup(misc::qBittorrentPath() + "BT_backup"); QDir torrentBackup(misc::qBittorrentPath() + "BT_backup");
QTorrentHandle h(p->handle); QTorrentHandle h(p->handle);
QString file = torrentBackup.path()+QDir::separator()+h.hash()+".torrent"; QString file = h.hash()+".fastresume";
qDebug("Saving fastresume data in %s", file.toUtf8().data());
TORRENT_ASSERT(p->resume_data); TORRENT_ASSERT(p->resume_data);
if (p->resume_data) if (p->resume_data)
{ {

Loading…
Cancel
Save