Bump to 4.6.7
[qBittorrent.git] / src / base / torrentfileswatcher.cpp
blob66ec9a56622ac9638a98502b613acd9a4001a2bc
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2021 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/magneturi.h"
48 #include "base/bittorrent/torrentcontentlayout.h"
49 #include "base/bittorrent/session.h"
50 #include "base/bittorrent/torrent.h"
51 #include "base/bittorrent/torrentinfo.h"
52 #include "base/exceptions.h"
53 #include "base/global.h"
54 #include "base/logger.h"
55 #include "base/profile.h"
56 #include "base/settingsstorage.h"
57 #include "base/tagset.h"
58 #include "base/utils/fs.h"
59 #include "base/utils/io.h"
60 #include "base/utils/string.h"
62 using namespace std::chrono_literals;
64 const std::chrono::seconds WATCH_INTERVAL {10};
65 const int MAX_FAILED_RETRIES = 5;
66 const QString CONF_FILE_NAME = u"watched_folders.json"_s;
68 const QString OPTION_ADDTORRENTPARAMS = u"add_torrent_params"_s;
69 const QString OPTION_RECURSIVE = u"recursive"_s;
71 namespace
73 TorrentFilesWatcher::WatchedFolderOptions parseWatchedFolderOptions(const QJsonObject &jsonObj)
75 TorrentFilesWatcher::WatchedFolderOptions options;
76 options.addTorrentParams = BitTorrent::parseAddTorrentParams(jsonObj.value(OPTION_ADDTORRENTPARAMS).toObject());
77 options.recursive = jsonObj.value(OPTION_RECURSIVE).toBool();
79 return options;
82 QJsonObject serializeWatchedFolderOptions(const TorrentFilesWatcher::WatchedFolderOptions &options)
84 return {{OPTION_ADDTORRENTPARAMS, BitTorrent::serializeAddTorrentParams(options.addTorrentParams)},
85 {OPTION_RECURSIVE, options.recursive}};
89 class TorrentFilesWatcher::Worker final : public QObject
91 Q_OBJECT
92 Q_DISABLE_COPY_MOVE(Worker)
94 public:
95 Worker(QFileSystemWatcher *watcher);
97 public slots:
98 void setWatchedFolder(const Path &path, const TorrentFilesWatcher::WatchedFolderOptions &options);
99 void removeWatchedFolder(const Path &path);
101 signals:
102 void magnetFound(const BitTorrent::MagnetUri &magnetURI, const BitTorrent::AddTorrentParams &addTorrentParams);
103 void torrentFound(const BitTorrent::TorrentInfo &torrentInfo, const BitTorrent::AddTorrentParams &addTorrentParams);
105 private:
106 void onTimeout();
107 void scheduleWatchedFolderProcessing(const Path &path);
108 void processWatchedFolder(const Path &path);
109 void processFolder(const Path &path, const Path &watchedFolderPath, const TorrentFilesWatcher::WatchedFolderOptions &options);
110 void processFailedTorrents();
111 void addWatchedFolder(const Path &path, const TorrentFilesWatcher::WatchedFolderOptions &options);
112 void updateWatchedFolder(const Path &path, const TorrentFilesWatcher::WatchedFolderOptions &options);
114 QFileSystemWatcher *m_watcher = nullptr;
115 QTimer *m_watchTimer = nullptr;
116 QHash<Path, TorrentFilesWatcher::WatchedFolderOptions> m_watchedFolders;
117 QSet<Path> m_watchedByTimeoutFolders;
119 // Failed torrents
120 QTimer *m_retryTorrentTimer = nullptr;
121 QHash<Path, QHash<Path, int>> m_failedTorrents;
124 TorrentFilesWatcher *TorrentFilesWatcher::m_instance = nullptr;
126 void TorrentFilesWatcher::initInstance()
128 if (!m_instance)
129 m_instance = new TorrentFilesWatcher;
132 void TorrentFilesWatcher::freeInstance()
134 delete m_instance;
135 m_instance = nullptr;
138 TorrentFilesWatcher *TorrentFilesWatcher::instance()
140 return m_instance;
143 TorrentFilesWatcher::TorrentFilesWatcher(QObject *parent)
144 : QObject(parent)
145 , m_ioThread {new QThread}
146 , m_asyncWorker {new TorrentFilesWatcher::Worker(new QFileSystemWatcher(this))}
148 connect(m_asyncWorker, &TorrentFilesWatcher::Worker::magnetFound, this, &TorrentFilesWatcher::onMagnetFound);
149 connect(m_asyncWorker, &TorrentFilesWatcher::Worker::torrentFound, this, &TorrentFilesWatcher::onTorrentFound);
151 m_asyncWorker->moveToThread(m_ioThread.get());
152 connect(m_ioThread.get(), &QThread::finished, m_asyncWorker, &QObject::deleteLater);
153 m_ioThread->start();
155 load();
158 void TorrentFilesWatcher::load()
160 const int fileMaxSize = 10 * 1024 * 1024;
161 const Path path = specialFolderLocation(SpecialFolder::Config) / Path(CONF_FILE_NAME);
163 const auto readResult = Utils::IO::readFile(path, fileMaxSize);
164 if (!readResult)
166 if (readResult.error().status == Utils::IO::ReadError::NotExist)
168 loadLegacy();
169 return;
172 LogMsg(tr("Failed to load Watched Folders configuration. %1").arg(readResult.error().message), Log::WARNING);
173 return;
176 QJsonParseError jsonError;
177 const QJsonDocument jsonDoc = QJsonDocument::fromJson(readResult.value(), &jsonError);
178 if (jsonError.error != QJsonParseError::NoError)
180 LogMsg(tr("Failed to parse Watched Folders configuration from %1. Error: \"%2\"")
181 .arg(path.toString(), jsonError.errorString()), Log::WARNING);
182 return;
185 if (!jsonDoc.isObject())
187 LogMsg(tr("Failed to load Watched Folders configuration from %1. Error: \"Invalid data format.\"")
188 .arg(path.toString()), Log::WARNING);
189 return;
192 const QJsonObject jsonObj = jsonDoc.object();
193 for (auto it = jsonObj.constBegin(); it != jsonObj.constEnd(); ++it)
195 const Path watchedFolder {it.key()};
196 const WatchedFolderOptions options = parseWatchedFolderOptions(it.value().toObject());
199 doSetWatchedFolder(watchedFolder, options);
201 catch (const InvalidArgument &err)
203 LogMsg(err.message(), Log::WARNING);
208 void TorrentFilesWatcher::loadLegacy()
210 const auto dirs = SettingsStorage::instance()->loadValue<QVariantHash>(u"Preferences/Downloads/ScanDirsV2"_s);
212 for (auto it = dirs.cbegin(); it != dirs.cend(); ++it)
214 const Path watchedFolder {it.key()};
215 BitTorrent::AddTorrentParams params;
216 if (it.value().type() == QVariant::Int)
218 if (it.value().toInt() == 0)
220 params.savePath = watchedFolder;
221 params.useAutoTMM = false;
224 else
226 const Path customSavePath {it.value().toString()};
227 params.savePath = customSavePath;
228 params.useAutoTMM = false;
233 doSetWatchedFolder(watchedFolder, {params, false});
235 catch (const InvalidArgument &err)
237 LogMsg(err.message(), Log::WARNING);
241 store();
242 SettingsStorage::instance()->removeValue(u"Preferences/Downloads/ScanDirsV2"_s);
245 void TorrentFilesWatcher::store() const
247 QJsonObject jsonObj;
248 for (auto it = m_watchedFolders.cbegin(); it != m_watchedFolders.cend(); ++it)
250 const Path &watchedFolder = it.key();
251 const WatchedFolderOptions &options = it.value();
252 jsonObj[watchedFolder.data()] = serializeWatchedFolderOptions(options);
255 const Path path = specialFolderLocation(SpecialFolder::Config) / Path(CONF_FILE_NAME);
256 const QByteArray data = QJsonDocument(jsonObj).toJson();
257 const nonstd::expected<void, QString> result = Utils::IO::saveToFile(path, data);
258 if (!result)
260 LogMsg(tr("Couldn't store Watched Folders configuration to %1. Error: %2")
261 .arg(path.toString(), result.error()), Log::WARNING);
265 QHash<Path, TorrentFilesWatcher::WatchedFolderOptions> TorrentFilesWatcher::folders() const
267 return m_watchedFolders;
270 void TorrentFilesWatcher::setWatchedFolder(const Path &path, const WatchedFolderOptions &options)
272 doSetWatchedFolder(path, options);
273 store();
276 void TorrentFilesWatcher::doSetWatchedFolder(const Path &path, const WatchedFolderOptions &options)
278 if (path.isEmpty())
279 throw InvalidArgument(tr("Watched folder Path cannot be empty."));
281 if (path.isRelative())
282 throw InvalidArgument(tr("Watched folder Path cannot be relative."));
284 m_watchedFolders[path] = options;
286 QMetaObject::invokeMethod(m_asyncWorker, [this, path, options]
288 m_asyncWorker->setWatchedFolder(path, options);
291 emit watchedFolderSet(path, options);
294 void TorrentFilesWatcher::removeWatchedFolder(const Path &path)
296 if (m_watchedFolders.remove(path))
298 if (m_asyncWorker)
300 QMetaObject::invokeMethod(m_asyncWorker, [this, path]()
302 m_asyncWorker->removeWatchedFolder(path);
306 emit watchedFolderRemoved(path);
308 store();
312 void TorrentFilesWatcher::onMagnetFound(const BitTorrent::MagnetUri &magnetURI
313 , const BitTorrent::AddTorrentParams &addTorrentParams)
315 BitTorrent::Session::instance()->addTorrent(magnetURI, addTorrentParams);
318 void TorrentFilesWatcher::onTorrentFound(const BitTorrent::TorrentInfo &torrentInfo
319 , const BitTorrent::AddTorrentParams &addTorrentParams)
321 BitTorrent::Session::instance()->addTorrent(torrentInfo, addTorrentParams);
324 TorrentFilesWatcher::Worker::Worker(QFileSystemWatcher *watcher)
325 : m_watcher {watcher}
326 , m_watchTimer {new QTimer(this)}
327 , m_retryTorrentTimer {new QTimer(this)}
329 connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, [this](const QString &path)
331 scheduleWatchedFolderProcessing(Path(path));
333 connect(m_watchTimer, &QTimer::timeout, this, &Worker::onTimeout);
335 connect(m_retryTorrentTimer, &QTimer::timeout, this, &Worker::processFailedTorrents);
338 void TorrentFilesWatcher::Worker::onTimeout()
340 for (const Path &path : asConst(m_watchedByTimeoutFolders))
341 processWatchedFolder(path);
344 void TorrentFilesWatcher::Worker::setWatchedFolder(const Path &path, const TorrentFilesWatcher::WatchedFolderOptions &options)
346 if (m_watchedFolders.contains(path))
347 updateWatchedFolder(path, options);
348 else
349 addWatchedFolder(path, options);
352 void TorrentFilesWatcher::Worker::removeWatchedFolder(const Path &path)
354 m_watchedFolders.remove(path);
356 m_watcher->removePath(path.data());
357 m_watchedByTimeoutFolders.remove(path);
358 if (m_watchedByTimeoutFolders.isEmpty())
359 m_watchTimer->stop();
361 m_failedTorrents.remove(path);
362 if (m_failedTorrents.isEmpty())
363 m_retryTorrentTimer->stop();
366 void TorrentFilesWatcher::Worker::scheduleWatchedFolderProcessing(const Path &path)
368 QTimer::singleShot(2s, Qt::CoarseTimer, this, [this, path]
370 processWatchedFolder(path);
374 void TorrentFilesWatcher::Worker::processWatchedFolder(const Path &path)
376 const TorrentFilesWatcher::WatchedFolderOptions options = m_watchedFolders.value(path);
377 processFolder(path, path, options);
379 if (!m_failedTorrents.empty() && !m_retryTorrentTimer->isActive())
380 m_retryTorrentTimer->start(WATCH_INTERVAL);
383 void TorrentFilesWatcher::Worker::processFolder(const Path &path, const Path &watchedFolderPath
384 , const TorrentFilesWatcher::WatchedFolderOptions &options)
386 QDirIterator dirIter {path.data(), {u"*.torrent"_s, u"*.magnet"_s}, QDir::Files};
387 while (dirIter.hasNext())
389 const Path filePath {dirIter.next()};
390 BitTorrent::AddTorrentParams addTorrentParams = options.addTorrentParams;
391 if (path != watchedFolderPath)
393 const Path subdirPath = watchedFolderPath.relativePathOf(path);
394 const bool useAutoTMM = addTorrentParams.useAutoTMM.value_or(!BitTorrent::Session::instance()->isAutoTMMDisabledByDefault());
395 if (useAutoTMM)
397 addTorrentParams.category = addTorrentParams.category.isEmpty()
398 ? subdirPath.data() : (addTorrentParams.category + u'/' + subdirPath.data());
400 else
402 addTorrentParams.savePath = addTorrentParams.savePath / subdirPath;
406 if (filePath.hasExtension(u".magnet"_s))
408 const int fileMaxSize = 100 * 1024 * 1024;
410 QFile file {filePath.data()};
411 if (file.open(QIODevice::ReadOnly | QIODevice::Text))
413 if (file.size() <= fileMaxSize)
415 while (!file.atEnd())
417 const auto line = QString::fromLatin1(file.readLine()).trimmed();
418 emit magnetFound(BitTorrent::MagnetUri(line), addTorrentParams);
421 file.close();
422 Utils::Fs::removeFile(filePath);
424 else
426 LogMsg(tr("Magnet file too big. File: %1").arg(file.errorString()));
429 else
431 LogMsg(tr("Failed to open magnet file: %1").arg(file.errorString()));
434 else
436 const nonstd::expected<BitTorrent::TorrentInfo, QString> result = BitTorrent::TorrentInfo::loadFromFile(filePath);
437 if (result)
439 emit torrentFound(result.value(), addTorrentParams);
440 Utils::Fs::removeFile(filePath);
442 else
444 if (!m_failedTorrents.value(path).contains(filePath))
446 m_failedTorrents[path][filePath] = 0;
452 if (options.recursive)
454 QDirIterator dirIter {path.data(), (QDir::Dirs | QDir::NoDot | QDir::NoDotDot)};
455 while (dirIter.hasNext())
457 const Path folderPath {dirIter.next()};
458 // Skip processing of subdirectory that is explicitly set as watched folder
459 if (!m_watchedFolders.contains(folderPath))
460 processFolder(folderPath, watchedFolderPath, options);
465 void TorrentFilesWatcher::Worker::processFailedTorrents()
467 // Check which torrents are still partial
468 Algorithm::removeIf(m_failedTorrents, [this](const Path &watchedFolderPath, QHash<Path, int> &partialTorrents)
470 const TorrentFilesWatcher::WatchedFolderOptions options = m_watchedFolders.value(watchedFolderPath);
471 Algorithm::removeIf(partialTorrents, [this, &watchedFolderPath, &options](const Path &torrentPath, int &value)
473 if (!torrentPath.exists())
474 return true;
476 const nonstd::expected<BitTorrent::TorrentInfo, QString> result = BitTorrent::TorrentInfo::loadFromFile(torrentPath);
477 if (result)
479 BitTorrent::AddTorrentParams addTorrentParams = options.addTorrentParams;
480 if (torrentPath != watchedFolderPath)
482 const Path subdirPath = watchedFolderPath.relativePathOf(torrentPath);
483 const bool useAutoTMM = addTorrentParams.useAutoTMM.value_or(!BitTorrent::Session::instance()->isAutoTMMDisabledByDefault());
484 if (useAutoTMM)
486 addTorrentParams.category = addTorrentParams.category.isEmpty()
487 ? subdirPath.data() : (addTorrentParams.category + u'/' + subdirPath.data());
489 else
491 addTorrentParams.savePath = addTorrentParams.savePath / subdirPath;
495 emit torrentFound(result.value(), addTorrentParams);
496 Utils::Fs::removeFile(torrentPath);
498 return true;
501 if (value >= MAX_FAILED_RETRIES)
503 LogMsg(tr("Rejecting failed torrent file: %1").arg(torrentPath.toString()));
504 Utils::Fs::renameFile(torrentPath, (torrentPath + u".qbt_rejected"));
505 return true;
508 ++value;
509 return false;
512 if (partialTorrents.isEmpty())
513 return true;
515 return false;
518 // Stop the partial timer if necessary
519 if (m_failedTorrents.empty())
520 m_retryTorrentTimer->stop();
521 else
522 m_retryTorrentTimer->start(WATCH_INTERVAL);
525 void TorrentFilesWatcher::Worker::addWatchedFolder(const Path &path, const TorrentFilesWatcher::WatchedFolderOptions &options)
527 // Check if the `path` points to a network file system or not
528 if (Utils::Fs::isNetworkFileSystem(path) || options.recursive)
530 m_watchedByTimeoutFolders.insert(path);
531 if (!m_watchTimer->isActive())
532 m_watchTimer->start(WATCH_INTERVAL);
534 else
536 m_watcher->addPath(path.data());
537 scheduleWatchedFolderProcessing(path);
540 m_watchedFolders[path] = options;
542 LogMsg(tr("Watching folder: \"%1\"").arg(path.toString()));
545 void TorrentFilesWatcher::Worker::updateWatchedFolder(const Path &path, const TorrentFilesWatcher::WatchedFolderOptions &options)
547 const bool recursiveModeChanged = (m_watchedFolders[path].recursive != options.recursive);
548 if (recursiveModeChanged && !Utils::Fs::isNetworkFileSystem(path))
550 if (options.recursive)
552 m_watcher->removePath(path.data());
554 m_watchedByTimeoutFolders.insert(path);
555 if (!m_watchTimer->isActive())
556 m_watchTimer->start(WATCH_INTERVAL);
558 else
560 m_watchedByTimeoutFolders.remove(path);
561 if (m_watchedByTimeoutFolders.isEmpty())
562 m_watchTimer->stop();
564 m_watcher->addPath(path.data());
565 scheduleWatchedFolderProcessing(path);
569 m_watchedFolders[path] = options;
572 #include "torrentfileswatcher.moc"