Implement class for handling filesystem paths

PR #15915.
This commit is contained in:
Vladimir Golovnev 2022-02-08 06:03:48 +03:00 committed by GitHub
parent facfa26eed
commit dd1bd8ad10
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
131 changed files with 2252 additions and 1868 deletions

View file

@ -31,21 +31,22 @@
#include <QDebug>
#include <QMetaObject>
#include "base/utils/fs.h"
#include "base/utils/io.h"
AsyncFileStorage::AsyncFileStorage(const QString &storageFolderPath, QObject *parent)
AsyncFileStorage::AsyncFileStorage(const Path &storageFolderPath, QObject *parent)
: QObject(parent)
, m_storageDir(storageFolderPath)
, m_lockFile(m_storageDir.absoluteFilePath(QStringLiteral("storage.lock")))
, m_lockFile((m_storageDir / Path(QStringLiteral("storage.lock"))).data())
{
if (!m_storageDir.mkpath(m_storageDir.absolutePath()))
throw AsyncFileStorageError
{tr("Could not create directory '%1'.")
.arg(m_storageDir.absolutePath())};
Q_ASSERT(m_storageDir.isAbsolute());
if (!Utils::Fs::mkpath(m_storageDir))
throw AsyncFileStorageError(tr("Could not create directory '%1'.").arg(m_storageDir.toString()));
// TODO: This folder locking approach does not work for UNIX systems. Implement it.
if (!m_lockFile.open(QFile::WriteOnly))
throw AsyncFileStorageError {m_lockFile.errorString()};
throw AsyncFileStorageError(m_lockFile.errorString());
}
AsyncFileStorage::~AsyncFileStorage()
@ -54,21 +55,21 @@ AsyncFileStorage::~AsyncFileStorage()
m_lockFile.remove();
}
void AsyncFileStorage::store(const QString &fileName, const QByteArray &data)
void AsyncFileStorage::store(const Path &filePath, const QByteArray &data)
{
QMetaObject::invokeMethod(this, [this, data, fileName]() { store_impl(fileName, data); }
QMetaObject::invokeMethod(this, [this, data, filePath]() { store_impl(filePath, data); }
, Qt::QueuedConnection);
}
QDir AsyncFileStorage::storageDir() const
Path AsyncFileStorage::storageDir() const
{
return m_storageDir;
}
void AsyncFileStorage::store_impl(const QString &fileName, const QByteArray &data)
void AsyncFileStorage::store_impl(const Path &fileName, const QByteArray &data)
{
const QString filePath = m_storageDir.absoluteFilePath(fileName);
qDebug() << "AsyncFileStorage: Saving data to" << filePath;
const Path filePath = m_storageDir / fileName;
qDebug() << "AsyncFileStorage: Saving data to" << filePath.toString();
const nonstd::expected<void, QString> result = Utils::IO::saveToFile(filePath, data);
if (!result)