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; 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(); diff --git a/src/base/utils/random.cpp b/src/base/utils/random.cpp index b0b1e2d96..77d4a2dec 100644 --- a/src/base/utils/random.cpp +++ b/src/base/utils/random.cpp @@ -33,13 +33,12 @@ #include #ifdef Q_OS_WIN -#ifndef NOMINMAX -#define NOMINMAX -#endif #include #include #else // Q_OS_WIN +#include #include +#include #endif #include @@ -97,7 +96,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 +118,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; }