Fix possible crash on torrent removal

This commit is contained in:
Christophe Dumez
2010-10-31 14:11:11 +00:00
parent ae5693544d
commit ccb2ed9a69

View File

@@ -1571,13 +1571,15 @@ void Bittorrent::saveFastResumeData() {
for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) {
QTorrentHandle h = QTorrentHandle(*torrentIT); QTorrentHandle h = QTorrentHandle(*torrentIT);
if(!h.is_valid() || !h.has_metadata()) continue; if(!h.is_valid() || !h.has_metadata()) continue;
if(isQueueingEnabled()) try {
TorrentPersistentData::savePriority(h); if(isQueueingEnabled())
// Actually with should save fast resume data for paused files too TorrentPersistentData::savePriority(h);
//if(h.is_paused()) continue; // Actually with should save fast resume data for paused files too
if(h.state() == torrent_status::checking_files || h.state() == torrent_status::queued_for_checking) continue; //if(h.is_paused()) continue;
h.save_resume_data(); if(h.state() == torrent_status::checking_files || h.state() == torrent_status::queued_for_checking) continue;
++num_resume_data; h.save_resume_data();
++num_resume_data;
} catch(invalid_handle&) {}
} }
while (num_resume_data > 0) { while (num_resume_data > 0) {
alert const* a = s->wait_for_alert(seconds(30)); alert const* a = s->wait_for_alert(seconds(30));
@@ -1593,8 +1595,9 @@ void Bittorrent::saveFastResumeData() {
s->pop_alert(); s->pop_alert();
try { try {
// Remove torrent from session // Remove torrent from session
s->remove_torrent(rda->handle); if(rda->handle.is_valid())
}catch(libtorrent::libtorrent_exception){} s->remove_torrent(rda->handle);
}catch(libtorrent::libtorrent_exception&){}
continue; continue;
} }
save_resume_data_alert const* rd = dynamic_cast<save_resume_data_alert const*>(a); save_resume_data_alert const* rd = dynamic_cast<save_resume_data_alert const*>(a);
@@ -1608,16 +1611,18 @@ void Bittorrent::saveFastResumeData() {
QDir torrentBackup(misc::BTBackupLocation()); QDir torrentBackup(misc::BTBackupLocation());
const QTorrentHandle h(rd->handle); const QTorrentHandle h(rd->handle);
if(!h.is_valid()) continue; if(!h.is_valid()) continue;
// Remove old fastresume file if it exists try {
const QString file = torrentBackup.absoluteFilePath(h.hash()+".fastresume"); // Remove old fastresume file if it exists
if(QFile::exists(file)) const QString file = torrentBackup.absoluteFilePath(h.hash()+".fastresume");
misc::safeRemove(file); if(QFile::exists(file))
boost::filesystem::ofstream out(boost::filesystem::path(file.toLocal8Bit().constData()), std::ios_base::binary); misc::safeRemove(file);
out.unsetf(std::ios_base::skipws); boost::filesystem::ofstream out(boost::filesystem::path(file.toLocal8Bit().constData()), std::ios_base::binary);
bencode(std::ostream_iterator<char>(out), *rd->resume_data); out.unsetf(std::ios_base::skipws);
// Remove torrent from session bencode(std::ostream_iterator<char>(out), *rd->resume_data);
s->remove_torrent(rd->handle); // Remove torrent from session
s->pop_alert(); s->remove_torrent(rd->handle);
s->pop_alert();
}catch(libtorrent::libtorrent_exception&){}
} }
} }