Skip to content

Commit

Permalink
Merge pull request qbittorrent#14993 from glassez/file-error
Browse files Browse the repository at this point in the history
Provide correct error description in "upload mode"
  • Loading branch information
glassez committed May 28, 2021
2 parents 3b4bf90 + b6a35e9 commit 1c34635
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 28 deletions.
60 changes: 34 additions & 26 deletions src/base/bittorrent/bencoderesumedatastorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -295,6 +270,39 @@ void BitTorrent::BencodeResumeDataStorage::storeQueue(const QVector<TorrentID> &
});
}

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}
{
Expand Down
1 change: 1 addition & 0 deletions src/base/bittorrent/bencoderesumedatastorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace BitTorrent
void storeQueue(const QVector<TorrentID> &queue) const override;

private:
void loadQueue(const QString &queueFilename);
std::optional<LoadTorrentParams> loadTorrentResumeData(const QByteArray &data, const TorrentInfo &metadata) const;

const QDir m_resumeDataDir;
Expand Down
3 changes: 2 additions & 1 deletion src/base/bittorrent/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 15 additions & 1 deletion src/base/bittorrent/torrentimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
}
Expand Down Expand Up @@ -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 *)
{
Expand Down Expand Up @@ -1981,6 +1992,9 @@ void TorrentImpl::handleAlert(const lt::alert *a)
case lt::file_completed_alert::alert_type:
handleFileCompletedAlert(static_cast<const lt::file_completed_alert*>(a));
break;
case lt::file_error_alert::alert_type:
handleFileErrorAlert(static_cast<const lt::file_error_alert*>(a));
break;
case lt::torrent_finished_alert::alert_type:
handleTorrentFinishedAlert(static_cast<const lt::torrent_finished_alert*>(a));
break;
Expand Down
8 changes: 8 additions & 0 deletions src/base/bittorrent/torrentimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -310,6 +317,7 @@ namespace BitTorrent
QHash<lt::file_index_t, QVector<QString>> m_oldPath;

QHash<QString, QMap<lt::tcp::endpoint, int>> m_trackerPeerCounts;
FileErrorInfo m_lastFileError;

// Persistent data
QString m_name;
Expand Down

0 comments on commit 1c34635

Please sign in to comment.