20#ifndef LIBREPCB_CORE_TRANSACTIONALFILESYSTEM_H
21#define LIBREPCB_CORE_TRANSACTIONALFILESYSTEM_H
26#include "../exceptions.h"
141 QString(
"Autosave backup detected in directory '%1'.")
149 const FilePath& filepath,
bool writable =
false,
152 QObject* parent =
nullptr);
163 virtual QStringList
getDirs(const QString& path = "") const noexcept override;
165 const QString& path = "") const noexcept override;
166 virtual
bool fileExists(const QString& path) const noexcept override;
167 virtual QByteArray
read(const QString& path) const override;
168 virtual QByteArray
readIfExists(const QString& path) const override;
169 virtual
void write(const QString& path, const QByteArray& content) override;
170 virtual
void renameFile(const QString& src, const QString& dst) override;
171 virtual
void removeFile(const QString& path) override;
187 const
FilePath& filepath,
bool writable,
190 QObject* parent =
nullptr) {
191 return std::make_shared<TransactionalFileSystem>(
192 filepath, writable, restoreCallback, lockCallback, parent);
194 static std::shared_ptr<TransactionalFileSystem>
openRO(
197 QObject* parent =
nullptr) {
198 return open(filepath,
false, restoreCallback,
nullptr, parent);
200 static std::shared_ptr<TransactionalFileSystem>
openRW(
204 QObject* parent =
nullptr) {
205 return open(filepath,
true, restoreCallback, lockCallback, parent);
207 static QString
cleanPath(QString path)
noexcept;
210 bool isRemoved(
const QString& path)
const noexcept;
213 void saveDiff(
const QString& type)
const;
222#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
This class can be used to implement file-based directory locks.
Definition: directorylock.h:154
std::function< bool(const FilePath &dir, LockStatus status, const QString &user)> LockHandlerCallback
Callback type used to determine whether a lock should be overridden or not.
Definition: directorylock.h:193
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:129
QString toNative() const noexcept
Get the absolute filepath with native directory separators.
Definition: filepath.cpp:108
Base class / interface for all file system implementations.
Definition: filesystem.h:45
The RuntimeError class.
Definition: exceptions.h:218
Transactional librepcb::FileSystem implementation.
Definition: transactionalfilesystem.h:71
DirectoryLock mLock
Definition: transactionalfilesystem.h:220
virtual void removeDirRecursively(const QString &path="") override
Definition: transactionalfilesystem.cpp:220
TransactionalFileSystem()=delete
QRecursiveMutex mMutex
Definition: transactionalfilesystem.h:223
virtual bool fileExists(const QString &path) const noexcept override
Definition: transactionalfilesystem.cpp:162
void discardChanges() noexcept
Definition: transactionalfilesystem.cpp:322
bool mRestoredFromAutosave
Definition: transactionalfilesystem.h:221
void autosave()
Definition: transactionalfilesystem.cpp:362
virtual QStringList getFiles(const QString &path="") const noexcept override
Definition: transactionalfilesystem.cpp:134
virtual void renameFile(const QString &src, const QString &dst) override
Definition: transactionalfilesystem.cpp:207
void releaseLock()
Definition: transactionalfilesystem.cpp:410
virtual void write(const QString &path, const QByteArray &content) override
Definition: transactionalfilesystem.cpp:199
const FilePath & getPath() const noexcept
Definition: transactionalfilesystem.h:157
virtual QStringList getDirs(const QString &path="") const noexcept override
Definition: transactionalfilesystem.cpp:105
QStringList checkForModifications() const
Definition: transactionalfilesystem.cpp:329
QSet< QString > mRemovedFiles
Definition: transactionalfilesystem.h:230
static std::shared_ptr< TransactionalFileSystem > open(const FilePath &filepath, bool writable, RestoreCallback restoreCallback=&RestoreMode::no, DirectoryLock::LockHandlerCallback lockCallback=nullptr, QObject *parent=nullptr)
Definition: transactionalfilesystem.h:186
QByteArray exportToZip(FilterFunction filter=nullptr) const
Definition: transactionalfilesystem.cpp:280
void exportDirToZip(QuaZipFile &file, const FilePath &zipFp, const QString &dir, FilterFunction filter) const
Definition: transactionalfilesystem.cpp:445
virtual ~TransactionalFileSystem() noexcept
Definition: transactionalfilesystem.cpp:81
TransactionalFileSystem(const TransactionalFileSystem &other)=delete
void saveDiff(const QString &type) const
Definition: transactionalfilesystem.cpp:489
bool isRemoved(const QString &path) const noexcept
Definition: transactionalfilesystem.cpp:431
static std::shared_ptr< TransactionalFileSystem > openRW(const FilePath &filepath, RestoreCallback restoreCallback=&RestoreMode::no, DirectoryLock::LockHandlerCallback lockCallback=nullptr, QObject *parent=nullptr)
Definition: transactionalfilesystem.h:200
bool isRestoredFromAutosave() const noexcept
Definition: transactionalfilesystem.h:159
virtual void removeFile(const QString &path) override
Definition: transactionalfilesystem.cpp:213
virtual QByteArray read(const QString &path) const override
Definition: transactionalfilesystem.cpp:174
void loadDiff(const FilePath &fp)
Definition: transactionalfilesystem.cpp:526
bool mIsWritable
Definition: transactionalfilesystem.h:219
std::function< bool(const FilePath &dir)> RestoreCallback
Callback type used to determine whether a backup should be restored or not.
Definition: transactionalfilesystem.h:96
void save()
Definition: transactionalfilesystem.cpp:367
void removeDiff(const QString &type)
Definition: transactionalfilesystem.cpp:549
static std::shared_ptr< TransactionalFileSystem > openRO(const FilePath &filepath, RestoreCallback restoreCallback=&RestoreMode::no, QObject *parent=nullptr)
Definition: transactionalfilesystem.h:194
static QString cleanPath(QString path) noexcept
Definition: transactionalfilesystem.cpp:419
bool isWritable() const noexcept
Definition: transactionalfilesystem.h:158
std::function< bool(const QString &filePath)> FilterFunction
Function to filter files.
Definition: transactionalfilesystem.h:83
void loadFromZip(QByteArray content)
Definition: transactionalfilesystem.cpp:241
QSet< QString > mRemovedDirs
Definition: transactionalfilesystem.h:231
QHash< QString, QByteArray > mModifiedFiles
Definition: transactionalfilesystem.h:229
const FilePath mFilePath
Definition: transactionalfilesystem.h:218
virtual QByteArray readIfExists(const QString &path) const override
Definition: transactionalfilesystem.cpp:185
virtual FilePath getAbsPath(const QString &path="") const noexcept override
Definition: transactionalfilesystem.cpp:100
Definition: occmodel.cpp:77
Convenience class providing standard implementations for librepcb::TransactionalFileSystem::RestoreCa...
Definition: transactionalfilesystem.h:104
static bool yes(const FilePath &dir)
Always restore the backup, if there is any.
Definition: transactionalfilesystem.h:124
static bool abort(const FilePath &dir)
If there exists a backup, abort opening the directory by raising an exception.
Definition: transactionalfilesystem.h:139
static bool no(const FilePath &dir)
Never restore a backup.
Definition: transactionalfilesystem.h:112