Change "metadata received" stop condition behavior
[qBittorrent.git] / src / base / bittorrent / bencoderesumedatastorage.cpp
blobf6602bebc2f71f74ac2e2afda5c44cbebf59c935
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015-2022 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "bencoderesumedatastorage.h"
31 #include <libtorrent/bdecode.hpp>
32 #include <libtorrent/entry.hpp>
33 #include <libtorrent/read_resume_data.hpp>
34 #include <libtorrent/torrent_info.hpp>
35 #include <libtorrent/write_resume_data.hpp>
37 #include <QByteArray>
38 #include <QDebug>
39 #include <QFile>
40 #include <QRegularExpression>
41 #include <QThread>
43 #include "base/algorithm.h"
44 #include "base/exceptions.h"
45 #include "base/global.h"
46 #include "base/logger.h"
47 #include "base/preferences.h"
48 #include "base/profile.h"
49 #include "base/tagset.h"
50 #include "base/utils/fs.h"
51 #include "base/utils/io.h"
52 #include "base/utils/string.h"
53 #include "infohash.h"
54 #include "loadtorrentparams.h"
56 namespace BitTorrent
58 class BencodeResumeDataStorage::Worker final : public QObject
60 Q_DISABLE_COPY_MOVE(Worker)
62 public:
63 explicit Worker(const Path &resumeDataDir);
65 void store(const TorrentID &id, const LoadTorrentParams &resumeData) const;
66 void remove(const TorrentID &id) const;
67 void storeQueue(const QVector<TorrentID> &queue) const;
69 private:
70 const Path m_resumeDataDir;
74 namespace
76 template <typename LTStr>
77 QString fromLTString(const LTStr &str)
79 return QString::fromUtf8(str.data(), static_cast<int>(str.size()));
82 using ListType = lt::entry::list_type;
84 ListType setToEntryList(const TagSet &input)
86 ListType entryList;
87 entryList.reserve(input.size());
88 for (const Tag &setValue : input)
89 entryList.emplace_back(setValue.toString().toStdString());
90 return entryList;
94 BitTorrent::BencodeResumeDataStorage::BencodeResumeDataStorage(const Path &path, QObject *parent)
95 : ResumeDataStorage(path, parent)
96 , m_ioThread {new QThread}
97 , m_asyncWorker {new Worker(path)}
99 Q_ASSERT(path.isAbsolute());
101 if (!path.exists() && !Utils::Fs::mkpath(path))
103 throw RuntimeError(tr("Cannot create torrent resume folder: \"%1\"")
104 .arg(path.toString()));
107 const QRegularExpression filenamePattern {u"^([A-Fa-f0-9]{40})\\.fastresume$"_s};
108 const QStringList filenames = QDir(path.data()).entryList(QStringList(u"*.fastresume"_s), QDir::Files, QDir::Unsorted);
110 m_registeredTorrents.reserve(filenames.size());
111 for (const QString &filename : filenames)
113 const QRegularExpressionMatch rxMatch = filenamePattern.match(filename);
114 if (rxMatch.hasMatch())
115 m_registeredTorrents.append(TorrentID::fromString(rxMatch.captured(1)));
118 loadQueue(path / Path(u"queue"_s));
120 qDebug() << "Registered torrents count: " << m_registeredTorrents.size();
122 m_asyncWorker->moveToThread(m_ioThread.get());
123 connect(m_ioThread.get(), &QThread::finished, m_asyncWorker, &QObject::deleteLater);
124 m_ioThread->start();
127 QVector<BitTorrent::TorrentID> BitTorrent::BencodeResumeDataStorage::registeredTorrents() const
129 return m_registeredTorrents;
132 BitTorrent::LoadResumeDataResult BitTorrent::BencodeResumeDataStorage::load(const TorrentID &id) const
134 const QString idString = id.toString();
135 const Path fastresumePath = path() / Path(idString + u".fastresume");
136 const Path torrentFilePath = path() / Path(idString + u".torrent");
137 const qint64 torrentSizeLimit = Preferences::instance()->getTorrentFileSizeLimit();
139 const auto resumeDataReadResult = Utils::IO::readFile(fastresumePath, torrentSizeLimit);
140 if (!resumeDataReadResult)
141 return nonstd::make_unexpected(resumeDataReadResult.error().message);
143 const auto metadataReadResult = Utils::IO::readFile(torrentFilePath, torrentSizeLimit);
144 if (!metadataReadResult)
146 if (metadataReadResult.error().status != Utils::IO::ReadError::NotExist)
147 return nonstd::make_unexpected(metadataReadResult.error().message);
150 const QByteArray data = resumeDataReadResult.value();
151 const QByteArray metadata = metadataReadResult.value_or(QByteArray());
152 return loadTorrentResumeData(data, metadata);
155 void BitTorrent::BencodeResumeDataStorage::doLoadAll() const
157 qDebug() << "Loading torrents count: " << m_registeredTorrents.size();
159 emit const_cast<BencodeResumeDataStorage *>(this)->loadStarted(m_registeredTorrents);
161 for (const TorrentID &torrentID : asConst(m_registeredTorrents))
162 onResumeDataLoaded(torrentID, load(torrentID));
164 emit const_cast<BencodeResumeDataStorage *>(this)->loadFinished();
167 void BitTorrent::BencodeResumeDataStorage::loadQueue(const Path &queueFilename)
169 const int lineMaxLength = 48;
171 QFile queueFile {queueFilename.data()};
172 if (!queueFile.exists())
173 return;
175 if (!queueFile.open(QFile::ReadOnly))
177 LogMsg(tr("Couldn't load torrents queue: %1").arg(queueFile.errorString()), Log::WARNING);
178 return;
181 const QRegularExpression hashPattern {u"^([A-Fa-f0-9]{40})$"_s};
182 int start = 0;
183 while (true)
185 const auto line = QString::fromLatin1(queueFile.readLine(lineMaxLength).trimmed());
186 if (line.isEmpty())
187 break;
189 const QRegularExpressionMatch rxMatch = hashPattern.match(line);
190 if (rxMatch.hasMatch())
192 const auto torrentID = BitTorrent::TorrentID::fromString(rxMatch.captured(1));
193 const int pos = m_registeredTorrents.indexOf(torrentID, start);
194 if (pos != -1)
196 std::swap(m_registeredTorrents[start], m_registeredTorrents[pos]);
197 ++start;
203 BitTorrent::LoadResumeDataResult BitTorrent::BencodeResumeDataStorage::loadTorrentResumeData(const QByteArray &data, const QByteArray &metadata) const
205 const auto *pref = Preferences::instance();
207 lt::error_code ec;
208 const lt::bdecode_node resumeDataRoot = lt::bdecode(data, ec
209 , nullptr, pref->getBdecodeDepthLimit(), pref->getBdecodeTokenLimit());
210 if (ec)
211 return nonstd::make_unexpected(tr("Cannot parse resume data: %1").arg(QString::fromStdString(ec.message())));
213 if (resumeDataRoot.type() != lt::bdecode_node::dict_t)
214 return nonstd::make_unexpected(tr("Cannot parse resume data: invalid format"));
216 LoadTorrentParams torrentParams;
217 torrentParams.category = fromLTString(resumeDataRoot.dict_find_string_value("qBt-category"));
218 torrentParams.name = fromLTString(resumeDataRoot.dict_find_string_value("qBt-name"));
219 torrentParams.hasFinishedStatus = resumeDataRoot.dict_find_int_value("qBt-seedStatus");
220 torrentParams.firstLastPiecePriority = resumeDataRoot.dict_find_int_value("qBt-firstLastPiecePriority");
221 torrentParams.seedingTimeLimit = resumeDataRoot.dict_find_int_value("qBt-seedingTimeLimit", Torrent::USE_GLOBAL_SEEDING_TIME);
222 torrentParams.inactiveSeedingTimeLimit = resumeDataRoot.dict_find_int_value("qBt-inactiveSeedingTimeLimit", Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME);
224 torrentParams.savePath = Profile::instance()->fromPortablePath(
225 Path(fromLTString(resumeDataRoot.dict_find_string_value("qBt-savePath"))));
226 torrentParams.useAutoTMM = torrentParams.savePath.isEmpty();
227 if (!torrentParams.useAutoTMM)
229 torrentParams.downloadPath = Profile::instance()->fromPortablePath(
230 Path(fromLTString(resumeDataRoot.dict_find_string_value("qBt-downloadPath"))));
233 // TODO: The following code is deprecated. Replace with the commented one after several releases in 4.4.x.
234 // === BEGIN DEPRECATED CODE === //
235 const lt::bdecode_node contentLayoutNode = resumeDataRoot.dict_find("qBt-contentLayout");
236 if (contentLayoutNode.type() == lt::bdecode_node::string_t)
238 const QString contentLayoutStr = fromLTString(contentLayoutNode.string_value());
239 torrentParams.contentLayout = Utils::String::toEnum(contentLayoutStr, TorrentContentLayout::Original);
241 else
243 const bool hasRootFolder = resumeDataRoot.dict_find_int_value("qBt-hasRootFolder");
244 torrentParams.contentLayout = (hasRootFolder ? TorrentContentLayout::Original : TorrentContentLayout::NoSubfolder);
246 // === END DEPRECATED CODE === //
247 // === BEGIN REPLACEMENT CODE === //
248 // torrentParams.contentLayout = Utils::String::parse(
249 // fromLTString(root.dict_find_string_value("qBt-contentLayout")), TorrentContentLayout::Default);
250 // === END REPLACEMENT CODE === //
252 torrentParams.stopCondition = Utils::String::toEnum(
253 fromLTString(resumeDataRoot.dict_find_string_value("qBt-stopCondition")), Torrent::StopCondition::None);
255 const lt::string_view ratioLimitString = resumeDataRoot.dict_find_string_value("qBt-ratioLimit");
256 if (ratioLimitString.empty())
257 torrentParams.ratioLimit = resumeDataRoot.dict_find_int_value("qBt-ratioLimit", Torrent::USE_GLOBAL_RATIO * 1000) / 1000.0;
258 else
259 torrentParams.ratioLimit = fromLTString(ratioLimitString).toDouble();
261 const lt::bdecode_node tagsNode = resumeDataRoot.dict_find("qBt-tags");
262 if (tagsNode.type() == lt::bdecode_node::list_t)
264 for (int i = 0; i < tagsNode.list_size(); ++i)
266 const Tag tag {fromLTString(tagsNode.list_string_value_at(i))};
267 torrentParams.tags.insert(tag);
271 lt::add_torrent_params &p = torrentParams.ltAddTorrentParams;
273 p = lt::read_resume_data(resumeDataRoot, ec);
275 if (!metadata.isEmpty())
277 const auto *pref = Preferences::instance();
278 const lt::bdecode_node torentInfoRoot = lt::bdecode(metadata, ec
279 , nullptr, pref->getBdecodeDepthLimit(), pref->getBdecodeTokenLimit());
280 if (ec)
281 return nonstd::make_unexpected(tr("Cannot parse torrent info: %1").arg(QString::fromStdString(ec.message())));
283 if (torentInfoRoot.type() != lt::bdecode_node::dict_t)
284 return nonstd::make_unexpected(tr("Cannot parse torrent info: invalid format"));
286 const auto torrentInfo = std::make_shared<lt::torrent_info>(torentInfoRoot, ec);
287 if (ec)
288 return nonstd::make_unexpected(tr("Cannot parse torrent info: %1").arg(QString::fromStdString(ec.message())));
290 p.ti = torrentInfo;
292 #ifdef QBT_USES_LIBTORRENT2
293 if (((p.info_hashes.has_v1() && (p.info_hashes.v1 != p.ti->info_hashes().v1))
294 || (p.info_hashes.has_v2() && (p.info_hashes.v2 != p.ti->info_hashes().v2))))
295 #else
296 if (!p.info_hash.is_all_zeros() && (p.info_hash != p.ti->info_hash()))
297 #endif
299 return nonstd::make_unexpected(tr("Mismatching info-hash detected in resume data"));
303 p.save_path = Profile::instance()->fromPortablePath(
304 Path(fromLTString(p.save_path))).toString().toStdString();
306 torrentParams.stopped = (p.flags & lt::torrent_flags::paused) && !(p.flags & lt::torrent_flags::auto_managed);
307 torrentParams.operatingMode = (p.flags & lt::torrent_flags::paused) || (p.flags & lt::torrent_flags::auto_managed)
308 ? TorrentOperatingMode::AutoManaged : TorrentOperatingMode::Forced;
310 if (p.flags & lt::torrent_flags::stop_when_ready)
312 p.flags &= ~lt::torrent_flags::stop_when_ready;
313 torrentParams.stopCondition = Torrent::StopCondition::FilesChecked;
316 const bool hasMetadata = (p.ti && p.ti->is_valid());
317 if (!hasMetadata && !resumeDataRoot.dict_find("info-hash"))
318 return nonstd::make_unexpected(tr("Resume data is invalid: neither metadata nor info-hash was found"));
320 return torrentParams;
323 void BitTorrent::BencodeResumeDataStorage::store(const TorrentID &id, const LoadTorrentParams &resumeData) const
325 QMetaObject::invokeMethod(m_asyncWorker, [this, id, resumeData]()
327 m_asyncWorker->store(id, resumeData);
331 void BitTorrent::BencodeResumeDataStorage::remove(const TorrentID &id) const
333 QMetaObject::invokeMethod(m_asyncWorker, [this, id]()
335 m_asyncWorker->remove(id);
339 void BitTorrent::BencodeResumeDataStorage::storeQueue(const QVector<TorrentID> &queue) const
341 QMetaObject::invokeMethod(m_asyncWorker, [this, queue]()
343 m_asyncWorker->storeQueue(queue);
347 BitTorrent::BencodeResumeDataStorage::Worker::Worker(const Path &resumeDataDir)
348 : m_resumeDataDir {resumeDataDir}
352 void BitTorrent::BencodeResumeDataStorage::Worker::store(const TorrentID &id, const LoadTorrentParams &resumeData) const
354 // We need to adjust native libtorrent resume data
355 lt::add_torrent_params p = resumeData.ltAddTorrentParams;
356 p.save_path = Profile::instance()->toPortablePath(Path(p.save_path))
357 .toString().toStdString();
358 if (resumeData.stopped)
360 p.flags |= lt::torrent_flags::paused;
361 p.flags &= ~lt::torrent_flags::auto_managed;
363 else
365 // Torrent can be actually "running" but temporarily "paused" to perform some
366 // service jobs behind the scenes so we need to restore it as "running"
367 if (resumeData.operatingMode == BitTorrent::TorrentOperatingMode::AutoManaged)
369 p.flags |= lt::torrent_flags::auto_managed;
371 else
373 p.flags &= ~lt::torrent_flags::paused;
374 p.flags &= ~lt::torrent_flags::auto_managed;
378 lt::entry data = lt::write_resume_data(p);
380 // metadata is stored in separate .torrent file
381 if (p.ti)
383 lt::entry::dictionary_type &dataDict = data.dict();
384 lt::entry metadata {lt::entry::dictionary_t};
385 lt::entry::dictionary_type &metadataDict = metadata.dict();
386 metadataDict.insert(dataDict.extract("info"));
387 metadataDict.insert(dataDict.extract("creation date"));
388 metadataDict.insert(dataDict.extract("created by"));
389 metadataDict.insert(dataDict.extract("comment"));
391 const Path torrentFilepath = m_resumeDataDir / Path(u"%1.torrent"_s.arg(id.toString()));
392 const nonstd::expected<void, QString> result = Utils::IO::saveToFile(torrentFilepath, metadata);
393 if (!result)
395 LogMsg(tr("Couldn't save torrent metadata to '%1'. Error: %2.")
396 .arg(torrentFilepath.toString(), result.error()), Log::CRITICAL);
397 return;
401 data["qBt-ratioLimit"] = static_cast<int>(resumeData.ratioLimit * 1000);
402 data["qBt-seedingTimeLimit"] = resumeData.seedingTimeLimit;
403 data["qBt-inactiveSeedingTimeLimit"] = resumeData.inactiveSeedingTimeLimit;
404 data["qBt-category"] = resumeData.category.toStdString();
405 data["qBt-tags"] = setToEntryList(resumeData.tags);
406 data["qBt-name"] = resumeData.name.toStdString();
407 data["qBt-seedStatus"] = resumeData.hasFinishedStatus;
408 data["qBt-contentLayout"] = Utils::String::fromEnum(resumeData.contentLayout).toStdString();
409 data["qBt-firstLastPiecePriority"] = resumeData.firstLastPiecePriority;
410 data["qBt-stopCondition"] = Utils::String::fromEnum(resumeData.stopCondition).toStdString();
412 if (!resumeData.useAutoTMM)
414 data["qBt-savePath"] = Profile::instance()->toPortablePath(resumeData.savePath).data().toStdString();
415 data["qBt-downloadPath"] = Profile::instance()->toPortablePath(resumeData.downloadPath).data().toStdString();
418 const Path resumeFilepath = m_resumeDataDir / Path(u"%1.fastresume"_s.arg(id.toString()));
419 const nonstd::expected<void, QString> result = Utils::IO::saveToFile(resumeFilepath, data);
420 if (!result)
422 LogMsg(tr("Couldn't save torrent resume data to '%1'. Error: %2.")
423 .arg(resumeFilepath.toString(), result.error()), Log::CRITICAL);
427 void BitTorrent::BencodeResumeDataStorage::Worker::remove(const TorrentID &id) const
429 const Path resumeFilename {u"%1.fastresume"_s.arg(id.toString())};
430 Utils::Fs::removeFile(m_resumeDataDir / resumeFilename);
432 const Path torrentFilename {u"%1.torrent"_s.arg(id.toString())};
433 Utils::Fs::removeFile(m_resumeDataDir / torrentFilename);
436 void BitTorrent::BencodeResumeDataStorage::Worker::storeQueue(const QVector<TorrentID> &queue) const
438 QByteArray data;
439 data.reserve(((BitTorrent::TorrentID::length() * 2) + 1) * queue.size());
440 for (const BitTorrent::TorrentID &torrentID : queue)
441 data += (torrentID.toString().toLatin1() + '\n');
443 const Path filepath = m_resumeDataDir / Path(u"queue"_s);
444 const nonstd::expected<void, QString> result = Utils::IO::saveToFile(filepath, data);
445 if (!result)
447 LogMsg(tr("Couldn't save data to '%1'. Error: %2")
448 .arg(filepath.toString(), result.error()), Log::CRITICAL);