LibrePCB Developers Documentation
Loading...
Searching...
No Matches
transactionalfilesystem.h
Go to the documentation of this file.
1/*
2 * LibrePCB - Professional EDA for everyone!
3 * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4 * https://librepcb.org/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef LIBREPCB_CORE_TRANSACTIONALFILESYSTEM_H
21#define LIBREPCB_CORE_TRANSACTIONALFILESYSTEM_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "../exceptions.h"
27#include "directorylock.h"
28#include "filesystem.h"
29
30#include <QtCore>
31
32#include <memory>
33
34/*******************************************************************************
35 * Namespace / Forward Declarations
36 ******************************************************************************/
37namespace librepcb {
38
39class ZipWriter;
40
41/*******************************************************************************
42 * Class TransactionalFileSystem
43 ******************************************************************************/
44
70class TransactionalFileSystem final : public FileSystem {
71 Q_OBJECT
72
73public:
82 typedef std::function<bool(const QString& filePath)> FilterFunction;
83
95 typedef std::function<bool(const FilePath& dir)> RestoreCallback;
96
103 struct RestoreMode {
111 static bool no(const FilePath& dir) {
112 Q_UNUSED(dir);
113 return false;
114 }
115
123 static bool yes(const FilePath& dir) {
124 Q_UNUSED(dir);
125 return true;
126 }
127
138 static bool abort(const FilePath& dir) {
139 throw RuntimeError(__FILE__, __LINE__,
140 QString("Autosave backup detected in directory '%1'.")
141 .arg(dir.toNative()));
142 }
143 };
144
145 // Constructors / Destructor
148 const FilePath& filepath, bool writable = false,
149 RestoreCallback restoreCallback = RestoreCallback(),
150 DirectoryLock::LockHandlerCallback lockCallback = nullptr,
151 QObject* parent = nullptr);
153 virtual ~TransactionalFileSystem() noexcept;
154
155 // Getters
156 const FilePath& getPath() const noexcept { return mFilePath; }
157 bool isWritable() const noexcept { return mIsWritable; }
158 bool isRestoredFromAutosave() const noexcept { return mRestoredFromAutosave; }
159
160 // Inherited from FileSystem
161 virtual FilePath getAbsPath(const QString& path = "") const noexcept override;
162 virtual QStringList getDirs(const QString& path = "") const noexcept override;
163 virtual QStringList getFiles(
164 const QString& path = "") const noexcept override;
165 virtual bool fileExists(const QString& path) const noexcept override;
166 virtual QByteArray read(const QString& path) const override;
167 virtual QByteArray readIfExists(const QString& path) const override;
168 virtual void write(const QString& path, const QByteArray& content) override;
169 virtual void renameFile(const QString& src, const QString& dst) override;
170 virtual void removeFile(const QString& path) override;
171 virtual void removeDirRecursively(const QString& path = "") override;
172
173 // General Methods
174 void loadFromZip(QByteArray content);
175 void loadFromZip(const FilePath& fp);
176 QByteArray exportToZip(FilterFunction filter = nullptr) const;
177 void exportToZip(const FilePath& fp, FilterFunction filter = nullptr) const;
178 void discardChanges() noexcept;
179 QStringList checkForModifications() const;
180 void autosave();
181 void save();
182 void releaseLock();
183
184 // Static Methods
185 static std::shared_ptr<TransactionalFileSystem> open(
186 const FilePath& filepath, bool writable,
187 RestoreCallback restoreCallback = &RestoreMode::no,
188 DirectoryLock::LockHandlerCallback lockCallback = nullptr,
189 QObject* parent = nullptr) {
190 return std::make_shared<TransactionalFileSystem>(
191 filepath, writable, restoreCallback, lockCallback, parent);
192 }
193 static std::shared_ptr<TransactionalFileSystem> openRO(
194 const FilePath& filepath,
195 RestoreCallback restoreCallback = &RestoreMode::no,
196 QObject* parent = nullptr) {
197 return open(filepath, false, restoreCallback, nullptr, parent);
198 }
199 static std::shared_ptr<TransactionalFileSystem> openRW(
200 const FilePath& filepath,
201 RestoreCallback restoreCallback = &RestoreMode::no,
202 DirectoryLock::LockHandlerCallback lockCallback = nullptr,
203 QObject* parent = nullptr) {
204 return open(filepath, true, restoreCallback, lockCallback, parent);
205 }
206 static QString cleanPath(QString path) noexcept;
207
208private: // Methods
209 bool isRemoved(const QString& path) const noexcept;
210 void exportDirToZip(ZipWriter& zip, const FilePath& zipFp, const QString& dir,
211 FilterFunction filter) const;
212 void saveDiff(const QString& type) const;
213 void loadDiff(const FilePath& fp);
214 void removeDiff(const QString& type);
215
216private: // Data
221 mutable QRecursiveMutex mMutex;
222
223 // File system modifications
224 QHash<QString, QByteArray> mModifiedFiles;
225 QSet<QString> mRemovedFiles;
226 QSet<QString> mRemovedDirs;
227};
228
229/*******************************************************************************
230 * End of File
231 ******************************************************************************/
232
233} // namespace librepcb
234
235#endif
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:127
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:70
DirectoryLock mLock
Definition transactionalfilesystem.h:219
virtual void removeDirRecursively(const QString &path="") override
Definition transactionalfilesystem.cpp:213
QRecursiveMutex mMutex
Definition transactionalfilesystem.h:221
virtual bool fileExists(const QString &path) const noexcept override
Definition transactionalfilesystem.cpp:155
void discardChanges() noexcept
Definition transactionalfilesystem.cpp:280
bool mRestoredFromAutosave
Definition transactionalfilesystem.h:220
void autosave()
Definition transactionalfilesystem.cpp:320
virtual QStringList getFiles(const QString &path="") const noexcept override
Definition transactionalfilesystem.cpp:127
virtual void renameFile(const QString &src, const QString &dst) override
Definition transactionalfilesystem.cpp:200
void releaseLock()
Definition transactionalfilesystem.cpp:368
virtual void write(const QString &path, const QByteArray &content) override
Definition transactionalfilesystem.cpp:192
const FilePath & getPath() const noexcept
Definition transactionalfilesystem.h:156
virtual QStringList getDirs(const QString &path="") const noexcept override
Definition transactionalfilesystem.cpp:98
QStringList checkForModifications() const
Definition transactionalfilesystem.cpp:287
QSet< QString > mRemovedFiles
Definition transactionalfilesystem.h:225
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:185
QByteArray exportToZip(FilterFunction filter=nullptr) const
Definition transactionalfilesystem.cpp:258
virtual ~TransactionalFileSystem() noexcept
Definition transactionalfilesystem.cpp:74
TransactionalFileSystem(const TransactionalFileSystem &other)=delete
void saveDiff(const QString &type) const
Definition transactionalfilesystem.cpp:435
bool isRemoved(const QString &path) const noexcept
Definition transactionalfilesystem.cpp:389
static std::shared_ptr< TransactionalFileSystem > openRW(const FilePath &filepath, RestoreCallback restoreCallback=&RestoreMode::no, DirectoryLock::LockHandlerCallback lockCallback=nullptr, QObject *parent=nullptr)
Definition transactionalfilesystem.h:199
bool isRestoredFromAutosave() const noexcept
Definition transactionalfilesystem.h:158
virtual void removeFile(const QString &path) override
Definition transactionalfilesystem.cpp:206
virtual QByteArray read(const QString &path) const override
Definition transactionalfilesystem.cpp:167
void exportDirToZip(ZipWriter &zip, const FilePath &zipFp, const QString &dir, FilterFunction filter) const
Definition transactionalfilesystem.cpp:403
void loadDiff(const FilePath &fp)
Definition transactionalfilesystem.cpp:472
bool mIsWritable
Definition transactionalfilesystem.h:218
std::function< bool(const FilePath &dir)> RestoreCallback
Callback type used to determine whether a backup should be restored or not.
Definition transactionalfilesystem.h:95
void save()
Definition transactionalfilesystem.cpp:325
void removeDiff(const QString &type)
Definition transactionalfilesystem.cpp:495
static std::shared_ptr< TransactionalFileSystem > openRO(const FilePath &filepath, RestoreCallback restoreCallback=&RestoreMode::no, QObject *parent=nullptr)
Definition transactionalfilesystem.h:193
static QString cleanPath(QString path) noexcept
Definition transactionalfilesystem.cpp:377
bool isWritable() const noexcept
Definition transactionalfilesystem.h:157
std::function< bool(const QString &filePath)> FilterFunction
Function to filter files.
Definition transactionalfilesystem.h:82
void loadFromZip(QByteArray content)
Definition transactionalfilesystem.cpp:234
QSet< QString > mRemovedDirs
Definition transactionalfilesystem.h:226
QHash< QString, QByteArray > mModifiedFiles
Definition transactionalfilesystem.h:224
const FilePath mFilePath
Definition transactionalfilesystem.h:217
virtual QByteArray readIfExists(const QString &path) const override
Definition transactionalfilesystem.cpp:178
virtual FilePath getAbsPath(const QString &path="") const noexcept override
Definition transactionalfilesystem.cpp:93
Zip file writer.
Definition zipwriter.h:50
Definition occmodel.cpp:76
Definition uuid.h:186
Convenience class providing standard implementations for librepcb::TransactionalFileSystem::RestoreCa...
Definition transactionalfilesystem.h:103
static bool yes(const FilePath &dir)
Always restore the backup, if there is any.
Definition transactionalfilesystem.h:123
static bool abort(const FilePath &dir)
If there exists a backup, abort opening the directory by raising an exception.
Definition transactionalfilesystem.h:138
static bool no(const FilePath &dir)
Never restore a backup.
Definition transactionalfilesystem.h:111