File indexing completed on 2024-09-15 04:36:24

0001 /*
0002  * SPDX-FileCopyrightText: 2015 Daniel Vrátil <dvratil@redhat.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  *
0006  */
0007 
0008 // krazy:excludeall=dpointer
0009 
0010 #pragma once
0011 
0012 #include "akonadiprivate_export.h"
0013 
0014 #include <QHash>
0015 #include <QList>
0016 #include <QMutex>
0017 
0018 class QString;
0019 class QByteArray;
0020 class QThread;
0021 
0022 namespace Akonadi
0023 {
0024 class AKONADIPRIVATE_EXPORT ExternalPartStorageTransaction
0025 {
0026 public:
0027     explicit ExternalPartStorageTransaction();
0028     ~ExternalPartStorageTransaction();
0029 
0030     bool commit();
0031     bool rollback();
0032 
0033 private:
0034     Q_DISABLE_COPY(ExternalPartStorageTransaction)
0035 };
0036 
0037 /**
0038  * Provides access to external payload part file storage
0039  *
0040  * Use ExternalPartStorageTransaction to delay deletion of part files until
0041  * commit. Files created during the transaction will be deleted when transaction
0042  * is rolled back to keep the storage clean.
0043  */
0044 class AKONADIPRIVATE_EXPORT ExternalPartStorage
0045 {
0046 public:
0047     static ExternalPartStorage *self();
0048 
0049     static QString resolveAbsolutePath(const QByteArray &filename, bool *exists = nullptr, bool legacyFallback = true);
0050     static QString resolveAbsolutePath(const QString &filename, bool *exists = nullptr, bool legacyFallback = true);
0051     static QByteArray updateFileNameRevision(const QByteArray &filename);
0052     static QByteArray nameForPartId(qint64 partId);
0053     static QString akonadiStoragePath();
0054 
0055     bool updatePartFile(const QByteArray &newData, const QByteArray &partFile, QByteArray &newPartFile);
0056     bool createPartFile(const QByteArray &newData, qint64 partId, QByteArray &partFileName);
0057     bool removePartFile(const QString &partFile);
0058 
0059     bool inTransaction() const;
0060 
0061 private:
0062     friend class ExternalPartStorageTransaction;
0063 
0064     struct Operation {
0065         enum Type {
0066             Create,
0067             Delete
0068             // We never update files, we always create a new one with increased
0069             // revision number, hence no "Update"
0070         };
0071 
0072         Type type;
0073         QString filename;
0074     };
0075 
0076     ExternalPartStorage();
0077 
0078     bool beginTransaction();
0079     bool commitTransaction();
0080     bool rollbackTransaction();
0081 
0082     bool replayTransaction(const QList<Operation> &trx, bool commit);
0083     void addToTransaction(const QList<Operation> &ops);
0084 
0085     mutable QMutex mTransactionLock;
0086     QHash<QThread *, QList<Operation>> mTransactions;
0087 };
0088 
0089 }