Fix crash on application exit (Qt 6.5)
[qBittorrent.git] / src / base / torrentfileswatcher.cpp
blobf50768767ef264c5bf18d1682ab62ee918836d2c
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2021-2023 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2010 Christian Kandeler, Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "torrentfileswatcher.h"
32 #include <chrono>
34 #include <QtGlobal>
35 #include <QDir>
36 #include <QDirIterator>
37 #include <QFile>
38 #include <QFileSystemWatcher>
39 #include <QJsonDocument>
40 #include <QJsonObject>
41 #include <QSet>
42 #include <QThread>
43 #include <QTimer>
44 #include <QVariant>
46 #include "base/algorithm.h"
47 #include "base/bittorrent/torrentcontentlayout.h"
48 #include "base/bittorrent/session.h"
49 #include "base/bittorrent/torrent.h"
50 #include "base/exceptions.h"
51 #include "base/global.h"
52 #include "base/logger.h"
53 #include "base/profile.h"
54 #include "base/settingsstorage.h"
55 #include "base/tagset.h"
56 #include "base/utils/fs.h"
57 #include "base/utils/io.h"
58 #include "base/utils/string.h"
60 using namespace std::chrono_literals;
62 const std::chrono::seconds WATCH_INTERVAL {10};
63 const int MAX_FAILED_RETRIES = 5;
64 const QString CONF_FILE_NAME = u"watched_folders.json"_s;
66 const QString OPTION_ADDTORRENTPARAMS = u"add_torrent_params"_s;
67 const QString OPTION_RECURSIVE = u"recursive"_s;
69 namespace
71 TorrentFilesWatcher::WatchedFolderOptions parseWatchedFolderOptions(const QJsonObject &jsonObj)
73 TorrentFilesWatcher::WatchedFolderOptions options;
74 options.addTorrentParams = BitTorrent::parseAddTorrentParams(jsonObj.value(OPTION_ADDTORRENTPARAMS).toObject());
75 options.recursive = jsonObj.value(OPTION_RECURSIVE).toBool();
77 return options;
80 QJsonObject serializeWatchedFolderOptions(const TorrentFilesWatcher::WatchedFolderOptions &options)
82 return {{OPTION_ADDTORRENTPARAMS, BitTorrent::serializeAddTorrentParams(options.addTorrentParams)},
83 {OPTION_RECURSIVE, options.recursive}};
87 class TorrentFilesWatcher::Worker final : public QObject
89 Q_OBJECT
90 Q_DISABLE_COPY_MOVE(Worker)
92 public:
93 Worker();
95 public slots:
96 void setWatchedFolder(const Path &path, const TorrentFilesWatcher::WatchedFolderOptions &options);
97 void removeWatchedFolder(const Path &path);
99 signals:
100 void torrentFound(const BitTorrent::TorrentDescriptor &torrentDescr, const BitTorrent::AddTorrentParams &addTorrentParams);
102 private:
103 void onTimeout();
104 void scheduleWatchedFolderProcessing(const Path &path);
105 void processWatchedFolder(const Path &path);
106 void processFolder(const Path &path, const Path &watchedFolderPath, const TorrentFilesWatcher::WatchedFolderOptions &options);
107 void processFailedTorrents();
108 void addWatchedFolder(const Path &path, const TorrentFilesWatcher::WatchedFolderOptions &options);
109 void updateWatchedFolder(const Path &path, const TorrentFilesWatcher::WatchedFolderOptions &options);
111 QFileSystemWatcher *m_watcher = nullptr;
112 QTimer *m_watchTimer = nullptr;
113 QHash<Path, TorrentFilesWatcher::WatchedFolderOptions> m_watchedFolders;
114 QSet<Path> m_watchedByTimeoutFolders;
116 // Failed torrents
117 QTimer *m_retryTorrentTimer = nullptr;
118 QHash<Path, QHash<Path, int>> m_failedTorrents;
121 TorrentFilesWatcher *TorrentFilesWatcher::m_instance = nullptr;
123 void TorrentFilesWatcher::initInstance()
125 if (!m_instance)
126 m_instance = new TorrentFilesWatcher;
129 void TorrentFilesWatcher::freeInstance()
131 delete m_instance;
132 m_instance = nullptr;
135 TorrentFilesWatcher *TorrentFilesWatcher::instance()
137 return m_instance;
140 TorrentFilesWatcher::TorrentFilesWatcher(QObject *parent)
141 : QObject(parent)
142 , m_ioThread {new QThread}
144 const auto *btSession = BitTorrent::Session::instance();
145 if (btSession->isRestored())
146 initWorker();
147 else
148 connect(btSession, &BitTorrent::Session::restored, this, &TorrentFilesWatcher::initWorker);
150 load();
153 void TorrentFilesWatcher::initWorker()
155 Q_ASSERT(!m_asyncWorker);
157 m_asyncWorker = new TorrentFilesWatcher::Worker;
159 connect(m_asyncWorker, &TorrentFilesWatcher::Worker::torrentFound, this, &TorrentFilesWatcher::onTorrentFound);
161 m_asyncWorker->moveToThread(m_ioThread.get());
162 connect(m_ioThread.get(), &QThread::finished, this, [this] { delete m_asyncWorker; });
163 m_ioThread->start();
165 for (auto it = m_watchedFolders.cbegin(); it != m_watchedFolders.cend(); ++it)
167 QMetaObject::invokeMethod(m_asyncWorker, [this, path = it.key(), options = it.value()]()
169 m_asyncWorker->setWatchedFolder(path, options);
174 void TorrentFilesWatcher::load()
176 const int fileMaxSize = 10 * 1024 * 1024;
177 const Path path = specialFolderLocation(SpecialFolder::Config) / Path(CONF_FILE_NAME);
179 const auto readResult = Utils::IO::readFile(path, fileMaxSize);
180 if (!readResult)
182 if (readResult.error().status == Utils::IO::ReadError::NotExist)
184 loadLegacy();
185 return;
188 LogMsg(tr("Failed to load Watched Folders configuration. %1").arg(readResult.error().message), Log::WARNING);
189 return;
192 QJsonParseError jsonError;
193 const QJsonDocument jsonDoc = QJsonDocument::fromJson(readResult.value(), &jsonError);
194 if (jsonError.error != QJsonParseError::NoError)
196 LogMsg(tr("Failed to parse Watched Folders configuration from %1. Error: \"%2\"")
197 .arg(path.toString(), jsonError.errorString()), Log::WARNING);
198 return;
201 if (!jsonDoc.isObject())
203 LogMsg(tr("Failed to load Watched Folders configuration from %1. Error: \"Invalid data format.\"")
204 .arg(path.toString()), Log::WARNING);
205 return;
208 const QJsonObject jsonObj = jsonDoc.object();
209 for (auto it = jsonObj.constBegin(); it != jsonObj.constEnd(); ++it)
211 const Path watchedFolder {it.key()};
212 const WatchedFolderOptions options = parseWatchedFolderOptions(it.value().toObject());
215 doSetWatchedFolder(watchedFolder, options);
217 catch (const InvalidArgument &err)
219 LogMsg(err.message(), Log::WARNING);
224 void TorrentFilesWatcher::loadLegacy()
226 const auto dirs = SettingsStorage::instance()->loadValue<QVariantHash>(u"Preferences/Downloads/ScanDirsV2"_s);
228 for (auto it = dirs.cbegin(); it != dirs.cend(); ++it)
230 const Path watchedFolder {it.key()};
231 BitTorrent::AddTorrentParams params;
232 if (it.value().userType() == QMetaType::Int)
234 if (it.value().toInt() == 0)
236 params.savePath = watchedFolder;
237 params.useAutoTMM = false;
240 else
242 const Path customSavePath {it.value().toString()};
243 params.savePath = customSavePath;
244 params.useAutoTMM = false;
249 doSetWatchedFolder(watchedFolder, {params, false});
251 catch (const InvalidArgument &err)
253 LogMsg(err.message(), Log::WARNING);
257 store();
258 SettingsStorage::instance()->removeValue(u"Preferences/Downloads/ScanDirsV2"_s);
261 void TorrentFilesWatcher::store() const
263 QJsonObject jsonObj;
264 for (auto it = m_watchedFolders.cbegin(); it != m_watchedFolders.cend(); ++it)
266 const Path &watchedFolder = it.key();
267 const WatchedFolderOptions &options = it.value();
268 jsonObj[watchedFolder.data()] = serializeWatchedFolderOptions(options);
271 const Path path = specialFolderLocation(SpecialFolder::Config) / Path(CONF_FILE_NAME);
272 const QByteArray data = QJsonDocument(jsonObj).toJson();
273 const nonstd::expected<void, QString> result = Utils::IO::saveToFile(path, data);
274 if (!result)
276 LogMsg(tr("Couldn't store Watched Folders configuration to %1. Error: %2")
277 .arg(path.toString(), result.error()), Log::WARNING);
281 QHash<Path, TorrentFilesWatcher::WatchedFolderOptions> TorrentFilesWatcher::folders() const
283 return m_watchedFolders;
286 void TorrentFilesWatcher::setWatchedFolder(const Path &path, const WatchedFolderOptions &options)
288 doSetWatchedFolder(path, options);
289 store();
292 void TorrentFilesWatcher::doSetWatchedFolder(const Path &path, const WatchedFolderOptions &options)
294 if (path.isEmpty())
295 throw InvalidArgument(tr("Watched folder Path cannot be empty."));
297 if (path.isRelative())
298 throw InvalidArgument(tr("Watched folder Path cannot be relative."));
300 m_watchedFolders[path] = options;
302 if (m_asyncWorker)
304 QMetaObject::invokeMethod(m_asyncWorker, [this, path, options]()
306 m_asyncWorker->setWatchedFolder(path, options);
310 emit watchedFolderSet(path, options);
313 void TorrentFilesWatcher::removeWatchedFolder(const Path &path)
315 if (m_watchedFolders.remove(path))
317 if (m_asyncWorker)
319 QMetaObject::invokeMethod(m_asyncWorker, [this, path]()
321 m_asyncWorker->removeWatchedFolder(path);
325 emit watchedFolderRemoved(path);
327 store();
331 void TorrentFilesWatcher::onTorrentFound(const BitTorrent::TorrentDescriptor &torrentDescr
332 , const BitTorrent::AddTorrentParams &addTorrentParams)
334 BitTorrent::Session::instance()->addTorrent(torrentDescr, addTorrentParams);
337 TorrentFilesWatcher::Worker::Worker()
338 : m_watcher {new QFileSystemWatcher(this)}
339 , m_watchTimer {new QTimer(this)}
340 , m_retryTorrentTimer {new QTimer(this)}
342 connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, [this](const QString &path)
344 scheduleWatchedFolderProcessing(Path(path));
346 connect(m_watchTimer, &QTimer::timeout, this, &Worker::onTimeout);
348 connect(m_retryTorrentTimer, &QTimer::timeout, this, &Worker::processFailedTorrents);
351 void TorrentFilesWatcher::Worker::onTimeout()
353 for (const Path &path : asConst(m_watchedByTimeoutFolders))
354 processWatchedFolder(path);
357 void TorrentFilesWatcher::Worker::setWatchedFolder(const Path &path, const TorrentFilesWatcher::WatchedFolderOptions &options)
359 if (m_watchedFolders.contains(path))
360 updateWatchedFolder(path, options);
361 else
362 addWatchedFolder(path, options);
365 void TorrentFilesWatcher::Worker::removeWatchedFolder(const Path &path)
367 m_watchedFolders.remove(path);
369 m_watcher->removePath(path.data());
370 m_watchedByTimeoutFolders.remove(path);
371 if (m_watchedByTimeoutFolders.isEmpty())
372 m_watchTimer->stop();
374 m_failedTorrents.remove(path);
375 if (m_failedTorrents.isEmpty())
376 m_retryTorrentTimer->stop();
379 void TorrentFilesWatcher::Worker::scheduleWatchedFolderProcessing(const Path &path)
381 QTimer::singleShot(2s, Qt::CoarseTimer, this, [this, path]
383 processWatchedFolder(path);
387 void TorrentFilesWatcher::Worker::processWatchedFolder(const Path &path)
389 const TorrentFilesWatcher::WatchedFolderOptions options = m_watchedFolders.value(path);
390 processFolder(path, path, options);
392 if (!m_failedTorrents.empty() && !m_retryTorrentTimer->isActive())
393 m_retryTorrentTimer->start(WATCH_INTERVAL);
396 void TorrentFilesWatcher::Worker::processFolder(const Path &path, const Path &watchedFolderPath
397 , const TorrentFilesWatcher::WatchedFolderOptions &options)
399 QDirIterator dirIter {path.data(), {u"*.torrent"_s, u"*.magnet"_s}, QDir::Files};
400 while (dirIter.hasNext())
402 const Path filePath {dirIter.next()};
403 BitTorrent::AddTorrentParams addTorrentParams = options.addTorrentParams;
404 if (path != watchedFolderPath)
406 const Path subdirPath = watchedFolderPath.relativePathOf(path);
407 const bool useAutoTMM = addTorrentParams.useAutoTMM.value_or(!BitTorrent::Session::instance()->isAutoTMMDisabledByDefault());
408 if (useAutoTMM)
410 addTorrentParams.category = addTorrentParams.category.isEmpty()
411 ? subdirPath.data() : (addTorrentParams.category + u'/' + subdirPath.data());
413 else
415 addTorrentParams.savePath = addTorrentParams.savePath / subdirPath;
419 if (filePath.hasExtension(u".magnet"_s))
421 const int fileMaxSize = 100 * 1024 * 1024;
423 QFile file {filePath.data()};
424 if (file.open(QIODevice::ReadOnly | QIODevice::Text))
426 if (file.size() <= fileMaxSize)
428 while (!file.atEnd())
430 const auto line = QString::fromLatin1(file.readLine()).trimmed();
431 if (const auto parseResult = BitTorrent::TorrentDescriptor::parse(line))
432 emit torrentFound(parseResult.value(), addTorrentParams);
433 else
434 LogMsg(tr("Invalid Magnet URI. URI: %1. Reason: %2").arg(line, parseResult.error()), Log::WARNING);
437 file.close();
438 Utils::Fs::removeFile(filePath);
440 else
442 LogMsg(tr("Magnet file too big. File: %1").arg(file.errorString()), Log::WARNING);
445 else
447 LogMsg(tr("Failed to open magnet file: %1").arg(file.errorString()));
450 else
452 if (const auto loadResult = BitTorrent::TorrentDescriptor::loadFromFile(filePath))
454 emit torrentFound(loadResult.value(), addTorrentParams);
455 Utils::Fs::removeFile(filePath);
457 else
459 if (!m_failedTorrents.value(path).contains(filePath))
461 m_failedTorrents[path][filePath] = 0;
467 if (options.recursive)
469 QDirIterator dirIter {path.data(), (QDir::Dirs | QDir::NoDot | QDir::NoDotDot)};
470 while (dirIter.hasNext())
472 const Path folderPath {dirIter.next()};
473 // Skip processing of subdirectory that is explicitly set as watched folder
474 if (!m_watchedFolders.contains(folderPath))
475 processFolder(folderPath, watchedFolderPath, options);
480 void TorrentFilesWatcher::Worker::processFailedTorrents()
482 // Check which torrents are still partial
483 Algorithm::removeIf(m_failedTorrents, [this](const Path &watchedFolderPath, QHash<Path, int> &partialTorrents)
485 const TorrentFilesWatcher::WatchedFolderOptions options = m_watchedFolders.value(watchedFolderPath);
486 Algorithm::removeIf(partialTorrents, [this, &watchedFolderPath, &options](const Path &torrentPath, int &value)
488 if (!torrentPath.exists())
489 return true;
491 if (const auto loadResult = BitTorrent::TorrentDescriptor::loadFromFile(torrentPath))
493 BitTorrent::AddTorrentParams addTorrentParams = options.addTorrentParams;
494 if (torrentPath != watchedFolderPath)
496 const Path subdirPath = watchedFolderPath.relativePathOf(torrentPath);
497 const bool useAutoTMM = addTorrentParams.useAutoTMM.value_or(!BitTorrent::Session::instance()->isAutoTMMDisabledByDefault());
498 if (useAutoTMM)
500 addTorrentParams.category = addTorrentParams.category.isEmpty()
501 ? subdirPath.data() : (addTorrentParams.category + u'/' + subdirPath.data());
503 else
505 addTorrentParams.savePath = addTorrentParams.savePath / subdirPath;
509 emit torrentFound(loadResult.value(), addTorrentParams);
510 Utils::Fs::removeFile(torrentPath);
512 return true;
515 if (value >= MAX_FAILED_RETRIES)
517 LogMsg(tr("Rejecting failed torrent file: %1").arg(torrentPath.toString()));
518 Utils::Fs::renameFile(torrentPath, (torrentPath + u".qbt_rejected"));
519 return true;
522 ++value;
523 return false;
526 if (partialTorrents.isEmpty())
527 return true;
529 return false;
532 // Stop the partial timer if necessary
533 if (m_failedTorrents.empty())
534 m_retryTorrentTimer->stop();
535 else
536 m_retryTorrentTimer->start(WATCH_INTERVAL);
539 void TorrentFilesWatcher::Worker::addWatchedFolder(const Path &path, const TorrentFilesWatcher::WatchedFolderOptions &options)
541 // Check if the `path` points to a network file system or not
542 if (Utils::Fs::isNetworkFileSystem(path) || options.recursive)
544 m_watchedByTimeoutFolders.insert(path);
545 if (!m_watchTimer->isActive())
546 m_watchTimer->start(WATCH_INTERVAL);
548 else
550 m_watcher->addPath(path.data());
551 scheduleWatchedFolderProcessing(path);
554 m_watchedFolders[path] = options;
556 LogMsg(tr("Watching folder: \"%1\"").arg(path.toString()));
559 void TorrentFilesWatcher::Worker::updateWatchedFolder(const Path &path, const TorrentFilesWatcher::WatchedFolderOptions &options)
561 const bool recursiveModeChanged = (m_watchedFolders[path].recursive != options.recursive);
562 if (recursiveModeChanged && !Utils::Fs::isNetworkFileSystem(path))
564 if (options.recursive)
566 m_watcher->removePath(path.data());
568 m_watchedByTimeoutFolders.insert(path);
569 if (!m_watchTimer->isActive())
570 m_watchTimer->start(WATCH_INTERVAL);
572 else
574 m_watchedByTimeoutFolders.remove(path);
575 if (m_watchedByTimeoutFolders.isEmpty())
576 m_watchTimer->stop();
578 m_watcher->addPath(path.data());
579 scheduleWatchedFolderProcessing(path);
583 m_watchedFolders[path] = options;
586 #include "torrentfileswatcher.moc"