mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2026-01-07 16:12:30 -06:00
Merge pull request #15419 from Chocobo1/file
Improve error detection when saving files
This commit is contained in:
@@ -29,7 +29,6 @@
|
||||
#include "bencoderesumedatastorage.h"
|
||||
|
||||
#include <libtorrent/bdecode.hpp>
|
||||
#include <libtorrent/bencode.hpp>
|
||||
#include <libtorrent/entry.hpp>
|
||||
#include <libtorrent/read_resume_data.hpp>
|
||||
#include <libtorrent/torrent_info.hpp>
|
||||
@@ -37,7 +36,6 @@
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QRegularExpression>
|
||||
#include <QSaveFile>
|
||||
#include <QThread>
|
||||
|
||||
#include "base/algorithm.h"
|
||||
@@ -88,17 +86,6 @@ namespace
|
||||
entryList.emplace_back(setValue.toStdString());
|
||||
return entryList;
|
||||
}
|
||||
|
||||
void writeEntryToFile(const QString &filepath, const lt::entry &data)
|
||||
{
|
||||
QSaveFile file {filepath};
|
||||
if (!file.open(QIODevice::WriteOnly))
|
||||
throw RuntimeError(file.errorString());
|
||||
|
||||
lt::bencode(Utils::IO::FileDeviceOutputIterator {file}, data);
|
||||
if (file.error() != QFileDevice::NoError || !file.commit())
|
||||
throw RuntimeError(file.errorString());
|
||||
}
|
||||
}
|
||||
|
||||
BitTorrent::BencodeResumeDataStorage::BencodeResumeDataStorage(const QString &path, QObject *parent)
|
||||
@@ -355,14 +342,11 @@ void BitTorrent::BencodeResumeDataStorage::Worker::store(const TorrentID &id, co
|
||||
metadataDict.insert(dataDict.extract("comment"));
|
||||
|
||||
const QString torrentFilepath = m_resumeDataDir.absoluteFilePath(QString::fromLatin1("%1.torrent").arg(id.toString()));
|
||||
try
|
||||
{
|
||||
writeEntryToFile(torrentFilepath, metadata);
|
||||
}
|
||||
catch (const RuntimeError &err)
|
||||
const nonstd::expected<void, QString> result = Utils::IO::saveToFile(torrentFilepath, metadata);
|
||||
if (!result)
|
||||
{
|
||||
LogMsg(tr("Couldn't save torrent metadata to '%1'. Error: %2.")
|
||||
.arg(torrentFilepath, err.message()), Log::CRITICAL);
|
||||
.arg(torrentFilepath, result.error()), Log::CRITICAL);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -378,14 +362,11 @@ void BitTorrent::BencodeResumeDataStorage::Worker::store(const TorrentID &id, co
|
||||
data["qBt-firstLastPiecePriority"] = resumeData.firstLastPiecePriority;
|
||||
|
||||
const QString resumeFilepath = m_resumeDataDir.absoluteFilePath(QString::fromLatin1("%1.fastresume").arg(id.toString()));
|
||||
try
|
||||
{
|
||||
writeEntryToFile(resumeFilepath, data);
|
||||
}
|
||||
catch (const RuntimeError &err)
|
||||
const nonstd::expected<void, QString> result = Utils::IO::saveToFile(resumeFilepath, data);
|
||||
if (!result)
|
||||
{
|
||||
LogMsg(tr("Couldn't save torrent resume data to '%1'. Error: %2.")
|
||||
.arg(resumeFilepath, err.message()), Log::CRITICAL);
|
||||
.arg(resumeFilepath, result.error()), Log::CRITICAL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -406,10 +387,10 @@ void BitTorrent::BencodeResumeDataStorage::Worker::storeQueue(const QVector<Torr
|
||||
data += (torrentID.toString().toLatin1() + '\n');
|
||||
|
||||
const QString filepath = m_resumeDataDir.absoluteFilePath(QLatin1String("queue"));
|
||||
QSaveFile file {filepath};
|
||||
if (!file.open(QIODevice::WriteOnly) || (file.write(data) != data.size()) || !file.commit())
|
||||
const nonstd::expected<void, QString> result = Utils::IO::saveToFile(filepath, data);
|
||||
if (!result)
|
||||
{
|
||||
LogMsg(tr("Couldn't save data to '%1'. Error: %2")
|
||||
.arg(filepath, file.errorString()), Log::CRITICAL);
|
||||
.arg(filepath, result.error()), Log::CRITICAL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,13 +30,11 @@
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include <libtorrent/bencode.hpp>
|
||||
#include <libtorrent/create_torrent.hpp>
|
||||
#include <libtorrent/file_storage.hpp>
|
||||
#include <libtorrent/torrent_info.hpp>
|
||||
|
||||
#include <QDirIterator>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QHash>
|
||||
|
||||
@@ -209,16 +207,9 @@ void TorrentCreatorThread::run()
|
||||
checkInterruptionRequested();
|
||||
|
||||
// create the torrent
|
||||
QFile outfile {m_params.savePath};
|
||||
if (!outfile.open(QIODevice::WriteOnly))
|
||||
throw RuntimeError(outfile.errorString());
|
||||
|
||||
checkInterruptionRequested();
|
||||
|
||||
lt::bencode(Utils::IO::FileDeviceOutputIterator {outfile}, entry);
|
||||
if (outfile.error() != QFileDevice::NoError)
|
||||
throw RuntimeError(outfile.errorString());
|
||||
outfile.close();
|
||||
const nonstd::expected<void, QString> result = Utils::IO::saveToFile(m_params.savePath, entry);
|
||||
if (!result)
|
||||
throw RuntimeError(result.error());
|
||||
|
||||
emit updateProgress(100);
|
||||
emit creationSuccess(m_params.savePath, parentPath);
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
|
||||
#include "torrentinfo.h"
|
||||
|
||||
#include <libtorrent/bencode.hpp>
|
||||
#include <libtorrent/create_torrent.hpp>
|
||||
#include <libtorrent/error_code.hpp>
|
||||
|
||||
@@ -175,14 +174,9 @@ void TorrentInfo::saveToFile(const QString &path) const
|
||||
{
|
||||
const auto torrentCreator = lt::create_torrent(*nativeInfo());
|
||||
const lt::entry torrentEntry = torrentCreator.generate();
|
||||
|
||||
QFile torrentFile {path};
|
||||
if (!torrentFile.open(QIODevice::WriteOnly))
|
||||
throw RuntimeError(torrentFile.errorString());
|
||||
|
||||
lt::bencode(Utils::IO::FileDeviceOutputIterator {torrentFile}, torrentEntry);
|
||||
if (torrentFile.error() != QFileDevice::NoError)
|
||||
throw RuntimeError(torrentFile.errorString());
|
||||
const nonstd::expected<void, QString> result = Utils::IO::saveToFile(path, torrentEntry);
|
||||
if (!result)
|
||||
throw RuntimeError(result.error());
|
||||
}
|
||||
catch (const lt::system_error &err)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user