From 6aa8251b98eb1469255e703ced5a2b1f7d893192 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Thu, 20 May 2021 09:30:15 +0300 Subject: [PATCH 1/2] Provide correct error description in "upload mode" --- src/base/bittorrent/session.cpp | 3 ++- src/base/bittorrent/torrentimpl.cpp | 16 +++++++++++++++- src/base/bittorrent/torrentimpl.h | 8 ++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 26371c8e9..875afba45 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -4580,8 +4580,9 @@ void Session::handleFileErrorAlert(const lt::file_error_alert *p) if (!torrent) return; - const TorrentID id = torrent->id(); + torrent->handleAlert(p); + const TorrentID id = torrent->id(); if (!m_recentErroredTorrents.contains(id)) { m_recentErroredTorrents.insert(id); diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index ecb44432e..04a41dced 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -1005,7 +1005,13 @@ QString TorrentImpl::error() const return QString::fromStdString(m_nativeStatus.errc.message()); if (m_nativeStatus.flags & lt::torrent_flags::upload_mode) - return tr("There's not enough space on disk. Torrent is currently in \"upload only\" mode."); + { + const QString writeErrorStr = tr("Couldn't write to file."); + const QString uploadModeStr = tr("Torrent is currently in \"upload only\" mode."); + const QString errorMessage = QString::fromLocal8Bit(m_lastFileError.error.message().c_str()); + + return writeErrorStr + QLatin1Char(' ') + errorMessage + QLatin1String(". ") + uploadModeStr; + } return {}; } @@ -1922,6 +1928,11 @@ void TorrentImpl::handleFileCompletedAlert(const lt::file_completed_alert *p) } } +void TorrentImpl::handleFileErrorAlert(const lt::file_error_alert *p) +{ + m_lastFileError = {p->error, p->op}; +} + #if (LIBTORRENT_VERSION_NUM >= 20003) void TorrentImpl::handleFilePrioAlert(const lt::file_prio_alert *) { @@ -1981,6 +1992,9 @@ void TorrentImpl::handleAlert(const lt::alert *a) case lt::file_completed_alert::alert_type: handleFileCompletedAlert(static_cast(a)); break; + case lt::file_error_alert::alert_type: + handleFileErrorAlert(static_cast(a)); + break; case lt::torrent_finished_alert::alert_type: handleTorrentFinishedAlert(static_cast(a)); break; diff --git a/src/base/bittorrent/torrentimpl.h b/src/base/bittorrent/torrentimpl.h index 59e0d7ac6..7fd3fad22 100644 --- a/src/base/bittorrent/torrentimpl.h +++ b/src/base/bittorrent/torrentimpl.h @@ -78,6 +78,12 @@ namespace BitTorrent HandleMetadata }; + struct FileErrorInfo + { + lt::error_code error; + lt::operation_t operation; + }; + class TorrentImpl final : public QObject, public Torrent { Q_DISABLE_COPY(TorrentImpl) @@ -255,6 +261,7 @@ namespace BitTorrent void handleFastResumeRejectedAlert(const lt::fastresume_rejected_alert *p); void handleFileCompletedAlert(const lt::file_completed_alert *p); + void handleFileErrorAlert(const lt::file_error_alert *p); #if (LIBTORRENT_VERSION_NUM >= 20003) void handleFilePrioAlert(const lt::file_prio_alert *p); #endif @@ -310,6 +317,7 @@ namespace BitTorrent QHash> m_oldPath; QHash> m_trackerPeerCounts; + FileErrorInfo m_lastFileError; // Persistent data QString m_name; From b6a35e94777f792cb47e737a0c5d6cd569e63d9e Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Sun, 23 May 2021 19:39:10 +0300 Subject: [PATCH 2/2] Don't log any error when "queue" file doesn't exist --- .../bittorrent/bencoderesumedatastorage.cpp | 60 +++++++++++-------- .../bittorrent/bencoderesumedatastorage.h | 1 + 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/base/bittorrent/bencoderesumedatastorage.cpp b/src/base/bittorrent/bencoderesumedatastorage.cpp index e6157caf8..0fd3a8c75 100644 --- a/src/base/bittorrent/bencoderesumedatastorage.cpp +++ b/src/base/bittorrent/bencoderesumedatastorage.cpp @@ -125,32 +125,7 @@ BitTorrent::BencodeResumeDataStorage::BencodeResumeDataStorage(const QString &pa m_registeredTorrents.append(TorrentID::fromString(rxMatch.captured(1))); } - QFile queueFile {m_resumeDataDir.absoluteFilePath(QLatin1String("queue"))}; - if (queueFile.open(QFile::ReadOnly)) - { - const QRegularExpression hashPattern {QLatin1String("^([A-Fa-f0-9]{40})$")}; - QByteArray line; - int start = 0; - while (!(line = queueFile.readLine().trimmed()).isEmpty()) - { - const QRegularExpressionMatch rxMatch = hashPattern.match(line); - if (rxMatch.hasMatch()) - { - const auto torrentID = TorrentID::fromString(rxMatch.captured(1)); - const int pos = m_registeredTorrents.indexOf(torrentID, start); - if (pos != -1) - { - std::swap(m_registeredTorrents[start], m_registeredTorrents[pos]); - ++start; - } - } - } - } - else - { - LogMsg(tr("Couldn't load torrents queue from '%1'. Error: %2") - .arg(queueFile.fileName(), queueFile.errorString()), Log::WARNING); - } + loadQueue(m_resumeDataDir.absoluteFilePath(QLatin1String("queue"))); qDebug("Registered torrents count: %d", m_registeredTorrents.size()); @@ -295,6 +270,39 @@ void BitTorrent::BencodeResumeDataStorage::storeQueue(const QVector & }); } +void BitTorrent::BencodeResumeDataStorage::loadQueue(const QString &queueFilename) +{ + QFile queueFile {queueFilename}; + if (!queueFile.exists()) + return; + + if (queueFile.open(QFile::ReadOnly)) + { + const QRegularExpression hashPattern {QLatin1String("^([A-Fa-f0-9]{40})$")}; + QByteArray line; + int start = 0; + while (!(line = queueFile.readLine().trimmed()).isEmpty()) + { + const QRegularExpressionMatch rxMatch = hashPattern.match(line); + if (rxMatch.hasMatch()) + { + const auto torrentID = TorrentID::fromString(rxMatch.captured(1)); + const int pos = m_registeredTorrents.indexOf(torrentID, start); + if (pos != -1) + { + std::swap(m_registeredTorrents[start], m_registeredTorrents[pos]); + ++start; + } + } + } + } + else + { + LogMsg(tr("Couldn't load torrents queue from '%1'. Error: %2") + .arg(queueFile.fileName(), queueFile.errorString()), Log::WARNING); + } +} + BitTorrent::BencodeResumeDataStorage::Worker::Worker(const QDir &resumeDataDir) : m_resumeDataDir {resumeDataDir} { diff --git a/src/base/bittorrent/bencoderesumedatastorage.h b/src/base/bittorrent/bencoderesumedatastorage.h index 6653fad1f..b631dfaa5 100644 --- a/src/base/bittorrent/bencoderesumedatastorage.h +++ b/src/base/bittorrent/bencoderesumedatastorage.h @@ -56,6 +56,7 @@ namespace BitTorrent void storeQueue(const QVector &queue) const override; private: + void loadQueue(const QString &queueFilename); std::optional loadTorrentResumeData(const QByteArray &data, const TorrentInfo &metadata) const; const QDir m_resumeDataDir;