From 4a11fab2b18ef8585bcfc6e914021e8587843cbf Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 5 Oct 2021 13:20:10 +0800 Subject: [PATCH 1/4] Add constexpr to Sample class functions --- src/base/bittorrent/speedmonitor.h | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/base/bittorrent/speedmonitor.h b/src/base/bittorrent/speedmonitor.h index 86ececd0b..6bb7c97c9 100644 --- a/src/base/bittorrent/speedmonitor.h +++ b/src/base/bittorrent/speedmonitor.h @@ -38,34 +38,30 @@ template struct Sample { - Sample() - : download() - , upload() + constexpr Sample() = default; + + constexpr Sample(const T dl, const T ul) + : download {dl} + , upload {ul} { } - Sample(T dl, T ul) - : download(dl) - , upload(ul) - { - } - - Sample &operator+=(const Sample &other) + constexpr Sample &operator+=(const Sample &other) { download += other.download; - upload += other.upload; + upload += other.upload; return *this; } - Sample &operator-=(const Sample &other) + constexpr Sample &operator-=(const Sample &other) { download -= other.download; - upload -= other.upload; + upload -= other.upload; return *this; } - T download; - T upload; + T download {}; + T upload {}; }; typedef Sample SpeedSample; From 6b49323f058b462ca27d2b9f451eff36b02190d1 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 8 Oct 2021 01:44:01 +0800 Subject: [PATCH 2/4] Improve error message reporting --- src/base/utils/random.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/base/utils/random.cpp b/src/base/utils/random.cpp index b0b1e2d96..9c66508ea 100644 --- a/src/base/utils/random.cpp +++ b/src/base/utils/random.cpp @@ -39,7 +39,9 @@ #include #include #else // Q_OS_WIN +#include #include +#include #endif #include @@ -97,7 +99,7 @@ namespace : m_randDev {fopen("/dev/urandom", "rb")} { if (!m_randDev) - qFatal("Failed to open /dev/urandom"); + qFatal("Failed to open /dev/urandom. Reason: %s. Error code: %d.\n", std::strerror(errno), errno); } ~RandomLayer() @@ -119,7 +121,7 @@ namespace { result_type buf = 0; if (fread(&buf, sizeof(buf), 1, m_randDev) != 1) - qFatal("Read /dev/urandom error"); + qFatal("Read /dev/urandom error. Reason: %s. Error code: %d.\n", std::strerror(errno), errno); return buf; } From 6b06cc9216b9cd338e73af154fee84a1fea53491 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 8 Oct 2021 11:01:54 +0800 Subject: [PATCH 3/4] Log error message in DownloadHandlerImpl class --- src/base/net/downloadhandlerimpl.cpp | 33 ++++++++++++++++++---------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/base/net/downloadhandlerimpl.cpp b/src/base/net/downloadhandlerimpl.cpp index 93a2c33eb..75a0318bd 100644 --- a/src/base/net/downloadhandlerimpl.cpp +++ b/src/base/net/downloadhandlerimpl.cpp @@ -32,6 +32,7 @@ #include #include +#include "base/3rdparty/expected.hpp" #include "base/utils/fs.h" #include "base/utils/gzip.h" #include "base/utils/io.h" @@ -41,18 +42,14 @@ const int MAX_REDIRECTIONS = 20; // the common value for web browsers namespace { - bool saveToFile(const QByteArray &replyData, QString &filePath) + nonstd::expected saveToTempFile(const QByteArray &data) { - if (!filePath.isEmpty()) - return Utils::IO::saveToFile(filePath, replyData).has_value(); - QTemporaryFile file {Utils::Fs::tempPath()}; - if (!file.open() || (file.write(replyData) != replyData.length()) || !file.flush()) - return false; + if (!file.open() || (file.write(data) != data.length()) || !file.flush()) + return nonstd::make_unexpected(file.errorString()); file.setAutoRemove(false); - filePath = file.fileName(); - return true; + return file.fileName(); } } @@ -130,11 +127,23 @@ void DownloadHandlerImpl::processFinishedDownload() if (m_downloadRequest.saveToFile()) { - QString filePath {m_downloadRequest.destFileName()}; - if (saveToFile(m_result.data, filePath)) - m_result.filePath = filePath; + const QString destinationPath = m_downloadRequest.destFileName(); + if (destinationPath.isEmpty()) + { + const nonstd::expected result = saveToTempFile(m_result.data); + if (result) + m_result.filePath = result.value(); + else + setError(tr("I/O Error: %1").arg(result.error())); + } else - setError(tr("I/O Error")); + { + const nonstd::expected result = Utils::IO::saveToFile(destinationPath, m_result.data); + if (result) + m_result.filePath = destinationPath; + else + setError(tr("I/O Error: %1").arg(result.error())); + } } finish(); From 03cb51844bbf61119733a7a3d5d133cab8bc7b3f Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 8 Oct 2021 11:11:02 +0800 Subject: [PATCH 4/4] Remove redundant define NOMINMAX is already defined in build scripts. --- src/base/utils/random.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/base/utils/random.cpp b/src/base/utils/random.cpp index 9c66508ea..77d4a2dec 100644 --- a/src/base/utils/random.cpp +++ b/src/base/utils/random.cpp @@ -33,9 +33,6 @@ #include #ifdef Q_OS_WIN -#ifndef NOMINMAX -#define NOMINMAX -#endif #include #include #else // Q_OS_WIN