From 3ccb2fddff52850a76f29d98f86cfe9c0a42fd2b Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 8 Jan 2011 13:31:57 +0000 Subject: [PATCH] BUGFIX: Do not allocate space for unwanted files (preallocation mode) --- Changelog | 1 + src/qtlibtorrent/qbtsession.cpp | 57 ++++++++++++++++++++++++++++- src/qtlibtorrent/qbtsession.h | 1 + src/qtlibtorrent/qtorrenthandle.cpp | 7 ++-- 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/Changelog b/Changelog index ecce3b01c..8868bafd8 100644 --- a/Changelog +++ b/Changelog @@ -12,6 +12,7 @@ - FEATURE: Added a search engine plugin for kickasstorrents.com - FEATURE: Added auto-suspend upon downloads completion feature - BUGFIX: Hide unwanted files that have to be partly downloaded + - BUGFIX: Do not allocate space for unwanted files (preallocation mode) - I18N: Added Galician translation - COSMETIC: Same deletion confirmation dialog in the GUI and Web UI - COSMETIC: Simplified the top toolbar diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index d034a8d40..3de89e878 100644 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -932,7 +932,20 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr p.resume_data = &buf; qDebug("Successfully loaded fast resume data"); } + } else { + // Generate fake resume data to make sure unwanted files + // are not allocated + if(preAllocateAll) { + vector fp; + TorrentTempData::getFilesPriority(hash, fp); + if((int)fp.size() == t->num_files()) { + entry rd = generateFilePriorityResumeData(t, fp); + bencode(std::back_inserter(buf), rd); + p.resume_data = &buf; + } + } } + QString savePath; if(!from_url.isEmpty() && savepathLabel_fromurl.contains(QUrl::fromEncoded(from_url.toLocal8Bit()))) { // Enforcing the save path defined before URL download (from RSS for example) @@ -1953,7 +1966,7 @@ void QBtSession::readAlerts() { bool will_shutdown = (pref.shutdownWhenDownloadsComplete() || pref.shutdownqBTWhenDownloadsComplete() || pref.suspendWhenDownloadsComplete()) - && !hasDownloadingTorrents(); + && !hasDownloadingTorrents(); // AutoRun program if(pref.isAutoRunEnabled()) autoRunExternalProgram(h, will_shutdown); @@ -2567,3 +2580,45 @@ void QBtSession::handleIPFilterError() addConsoleMessage(tr("Error: Failed to parse the provided IP filter."), "red"); emit ipFilterParsed(true, 0); } + +entry QBtSession::generateFilePriorityResumeData(boost::intrusive_ptr t, const std::vector &fp) +{ + entry::dictionary_type rd; + rd["file-format"] = "libtorrent resume file"; + rd["file-version"] = 1; + rd["libtorrent-version"] = LIBTORRENT_VERSION; + rd["allocation"] = "full"; + sha1_hash info_hash = t->info_hash(); + rd["info-hash"] = std::string((char*)info_hash.begin(), (char*)info_hash.end()); + // Priorities + entry::list_type priorities; + priorities.resize(fp.size()); + for(uint i=0; inum_files(); ++i) { + entry::list_type p; + p.push_back(entry(0)); + p.push_back(entry(0)); + sizes.push_back(entry(p)); + } + rd["file sizes"] = entry(sizes); + // Slots + entry::list_type tslots; + + for(int i=0; inum_pieces(); ++i) { + tslots.push_back(-1); + } + rd["slots"] = entry(tslots); + + entry::string_type pieces; + std::memset(&pieces[0], 0, t->num_pieces()); + rd["pieces"] = entry(pieces); + + entry ret(rd); + Q_ASSERT(ret.type() == entry::dictionary_t); + return ret; +} diff --git a/src/qtlibtorrent/qbtsession.h b/src/qtlibtorrent/qbtsession.h index da9db1ee9..333b57687 100644 --- a/src/qtlibtorrent/qbtsession.h +++ b/src/qtlibtorrent/qbtsession.h @@ -167,6 +167,7 @@ private: void loadTorrentSettings(QTorrentHandle h); void loadTorrentTempData(QTorrentHandle h, QString savePath, bool magnet); libtorrent::add_torrent_params initializeAddTorrentParams(QString hash); + libtorrent::entry generateFilePriorityResumeData(boost::intrusive_ptr t, const std::vector &fp); private slots: void addTorrentsFromScanFolder(QStringList&); diff --git a/src/qtlibtorrent/qtorrenthandle.cpp b/src/qtlibtorrent/qtorrenthandle.cpp index 9aa7ad0e7..7053d2236 100644 --- a/src/qtlibtorrent/qtorrenthandle.cpp +++ b/src/qtlibtorrent/qtorrenthandle.cpp @@ -500,15 +500,14 @@ void QTorrentHandle::file_priority(int index, int priority) const { } } -// TODO: Also hide the folder on Windows void QTorrentHandle::prioritize_files(const vector &files) const { if((int)files.size() != torrent_handle::get_torrent_info().num_files()) return; - const vector prev_priorities = torrent_handle::file_priorities(); + qDebug() << Q_FUNC_INFO; bool was_seed = is_seed(); torrent_handle::prioritize_files(files); for(uint i=0; i 0 && files[i] == 0) { + if(files[i] == 0) { QString old_path = filepath_at(i); QString old_name = filename_at(i); QString parent_path = misc::branchPath(old_path); @@ -535,7 +534,7 @@ void QTorrentHandle::prioritize_files(const vector &files) const { } } // Move wanted files back to their original folder - if(prev_priorities[i] == 0 && files[i] > 0) { + if(files[i] > 0) { QString old_path = filepath_at(i); QString old_name = filename_at(i); QDir parent_path(misc::branchPath(old_path));