Skip to content

Commit bb4c446

Browse files
committed
Update batch script and add fs module
1 parent f1e4c01 commit bb4c446

26 files changed

Lines changed: 4360 additions & 28 deletions

src/plugins/batchprocess/core/Script.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace BatchProcess {
1818
}
1919

2020
class ScriptAction;
21+
class FileSystemAccessInterface;
22+
class FileSystemAccessInterfacePrivate;
2123
class ScriptPrivate;
2224

2325
class BATCH_PROCESS_EXPORT Script : public QObject {
@@ -34,6 +36,8 @@ namespace BatchProcess {
3436
private:
3537
friend class Internal::BatchProcessRuntime;
3638
friend class Internal::RuntimeModuleExtension;
39+
friend class FileSystemAccessInterface;
40+
friend class FileSystemAccessInterfacePrivate;
3741
explicit Script(const QString &filePath, const QString &rootPath, QObject *parent = nullptr);
3842

3943
QScopedPointer<ScriptPrivate> d_ptr;

src/plugins/batchprocess/fsaccess/FileSystemAccessInterface.cpp

Lines changed: 1746 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// SPDX-FileCopyrightText: Team OpenVPI
2+
// SPDX-License-Identifier: LGPL-3.0-or-later
3+
4+
#ifndef DIFFSCOPE_BATCHPROCESS_FILESYSTEMACCESSINTERFACE_H
5+
#define DIFFSCOPE_BATCHPROCESS_FILESYSTEMACCESSINTERFACE_H
6+
7+
#include <QByteArray>
8+
#include <QByteArrayView>
9+
#include <QJSValue>
10+
#include <QList>
11+
#include <QObject>
12+
#include <QScopedPointer>
13+
#include <QStringList>
14+
15+
#include <batchprocess/FileSystemHandle.h>
16+
#include <batchprocess/FileSystemTypes.h>
17+
#include <batchprocess/batchprocessglobal.h>
18+
19+
namespace BatchProcess {
20+
21+
namespace Internal {
22+
class BatchProcessPlugin;
23+
class FileSystemModuleBridge;
24+
class FileSystemModuleExtension;
25+
}
26+
27+
class FileSystemAccessInterfacePrivate;
28+
class ScriptExecutionContext;
29+
30+
class BATCH_PROCESS_EXPORT FileSystemAccessInterface : public QObject {
31+
Q_OBJECT
32+
Q_DECLARE_PRIVATE(FileSystemAccessInterface)
33+
public:
34+
~FileSystemAccessInterface() override;
35+
36+
static FileSystemAccessInterface *instance();
37+
38+
FileSystemPermissionDecision requestPermission(ScriptExecutionContext *context, const FileSystemPermissionRequest &request, FileSystemError *error = nullptr);
39+
bool hasPermission(ScriptExecutionContext *context, const QString &path, FileAccessMode access, bool recursive = false) const;
40+
41+
FileSystemHandle getFile(ScriptExecutionContext *context, const QString &path, FileAccessMode access = FileAccessMode::Read, FileSystemError *error = nullptr);
42+
FileSystemHandle getDirectory(ScriptExecutionContext *context, const QString &path, FileAccessMode access = FileAccessMode::Read, bool recursive = false, FileSystemError *error = nullptr);
43+
FileSystemHandle scriptPackage(ScriptExecutionContext *context, FileSystemError *error = nullptr);
44+
FileSystemHandle dataDirectory(ScriptExecutionContext *context, FileSystemError *error = nullptr);
45+
FileSystemHandle temporaryDirectory(ScriptExecutionContext *context, FileSystemError *error = nullptr);
46+
47+
/** Creates a capability after a trusted host component has independently obtained user authorization. */
48+
FileSystemHandle createAuthorizedFileHandle(ScriptExecutionContext *context, const QString &path, FileAccessMode access, FileSystemError *error = nullptr);
49+
/** Creates a capability after a trusted host component has independently obtained user authorization. */
50+
FileSystemHandle createAuthorizedDirectoryHandle(ScriptExecutionContext *context, const QString &path, FileAccessMode access, bool recursive, FileSystemError *error = nullptr);
51+
bool validateHandle(ScriptExecutionContext *context, const FileSystemHandle &handle, FileAccessMode access, FileSystemHandle::Kind expectedKind = FileSystemHandle::Invalid, FileSystemError *error = nullptr) const;
52+
53+
bool stat(ScriptExecutionContext *context, const QString &path, const FileSystemStatOptions &options, FileSystemStat *result, FileSystemError *error = nullptr) const;
54+
bool stat(ScriptExecutionContext *context, const FileSystemHandle &handle, const FileSystemStatOptions &options, FileSystemStat *result, FileSystemError *error = nullptr) const;
55+
bool readText(ScriptExecutionContext *context, const FileSystemHandle &handle, const FileTextReadOptions &options, QString *result, FileSystemError *error = nullptr) const;
56+
bool readBytes(ScriptExecutionContext *context, const FileSystemHandle &handle, const FileBinaryReadOptions &options, QByteArray *result, FileSystemError *error = nullptr) const;
57+
bool writeText(ScriptExecutionContext *context, const FileSystemHandle &handle, const QString &text, const FileTextWriteOptions &options, FileSystemError *error = nullptr);
58+
bool writeBytes(ScriptExecutionContext *context, const FileSystemHandle &handle, QByteArrayView data, const FileBinaryWriteOptions &options, FileSystemError *error = nullptr);
59+
bool entries(ScriptExecutionContext *context, const FileSystemHandle &handle, const FileSystemDirectoryListOptions &options, QList<FileSystemDirectoryEntry> *result, FileSystemError *error = nullptr) const;
60+
FileSystemHandle childFile(ScriptExecutionContext *context, const FileSystemHandle &directory, const QString &relativePath, FileAccessMode access = FileAccessMode::Read, FileSystemError *error = nullptr);
61+
FileSystemHandle childDirectory(ScriptExecutionContext *context, const FileSystemHandle &directory, const QString &relativePath, FileSystemError *error = nullptr);
62+
63+
bool createDirectory(ScriptExecutionContext *context, const QString &path, const FileSystemCreateDirectoryOptions &options, FileSystemError *error = nullptr);
64+
bool copy(ScriptExecutionContext *context, const QString &source, const QString &destination, const FileSystemCopyOptions &options, FileSystemError *error = nullptr);
65+
bool move(ScriptExecutionContext *context, const QString &source, const QString &destination, const FileSystemMoveOptions &options, FileSystemError *error = nullptr);
66+
bool remove(ScriptExecutionContext *context, const QString &path, const FileSystemRemoveOptions &options, FileSystemError *error = nullptr);
67+
bool realPath(ScriptExecutionContext *context, const QString &path, QString *result, FileSystemError *error = nullptr) const;
68+
69+
QString normalize(const QString &path) const;
70+
bool resolve(ScriptExecutionContext *context, const QStringList &parts, QString *result, FileSystemError *error = nullptr) const;
71+
bool relative(ScriptExecutionContext *context, const QString &from, const QString &to, QString *result, FileSystemError *error = nullptr) const;
72+
bool isAbsolute(const QString &path) const;
73+
QString join(const QStringList &parts) const;
74+
QString baseName(const QString &path) const;
75+
QString directoryName(const QString &path) const;
76+
QString extension(const QString &path) const;
77+
FileSystemParsedPath parse(const QString &path) const;
78+
bool format(const FileSystemParsedPath &path, QString *result, FileSystemError *error = nullptr) const;
79+
bool matchesPattern(ScriptExecutionContext *context, const QString &path, const QString &pattern, bool *result, FileSystemError *error = nullptr) const;
80+
81+
QJSValue toJavaScriptValue(ScriptExecutionContext *context, const FileSystemHandle &handle, FileSystemError *error = nullptr);
82+
bool fromJavaScriptValue(ScriptExecutionContext *context, const QJSValue &value, FileSystemHandle *handle, FileSystemHandle::Kind expectedKind = FileSystemHandle::Invalid, FileSystemError *error = nullptr) const;
83+
84+
private:
85+
friend class Internal::BatchProcessPlugin;
86+
friend class Internal::FileSystemModuleBridge;
87+
friend class Internal::FileSystemModuleExtension;
88+
89+
explicit FileSystemAccessInterface(QObject *parent = nullptr);
90+
91+
QScopedPointer<FileSystemAccessInterfacePrivate> d_ptr;
92+
};
93+
94+
}
95+
96+
#endif // DIFFSCOPE_BATCHPROCESS_FILESYSTEMACCESSINTERFACE_H
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// SPDX-FileCopyrightText: Team OpenVPI
2+
// SPDX-License-Identifier: LGPL-3.0-or-later
3+
4+
#ifndef DIFFSCOPE_BATCHPROCESS_FILESYSTEMACCESSINTERFACE_P_H
5+
#define DIFFSCOPE_BATCHPROCESS_FILESYSTEMACCESSINTERFACE_P_H
6+
7+
#include <batchprocess/FileSystemAccessInterface.h>
8+
9+
#include <memory>
10+
11+
#include <QHash>
12+
#include <QPointer>
13+
#include <QSharedPointer>
14+
#include <QTemporaryDir>
15+
#include <QVector>
16+
17+
#include <batchprocess/private/FileSystemHandle_p.h>
18+
19+
class QJSEngine;
20+
21+
namespace BatchProcess {
22+
23+
namespace Internal {
24+
class FileSystemModuleExtension;
25+
}
26+
27+
class FileSystemAccessInterfacePrivate {
28+
Q_DECLARE_PUBLIC(FileSystemAccessInterface)
29+
public:
30+
struct PermissionGrant {
31+
QString pattern;
32+
FileAccessMode access{FileAccessMode::Read};
33+
};
34+
35+
struct SessionPermissions {
36+
QVector<PermissionGrant> grants;
37+
bool fullAccess{};
38+
};
39+
40+
struct ExecutionState {
41+
QSharedPointer<Internal::FileSystemExecutionToken> token;
42+
QString scriptId;
43+
QString scriptRoot;
44+
QVector<PermissionGrant> grants;
45+
std::unique_ptr<QTemporaryDir> temporaryDirectory;
46+
QString temporaryDirectoryPath;
47+
QHash<QString, quint64> invalidatedPaths;
48+
quint64 mutationSerial{};
49+
bool cleanupScheduled{};
50+
};
51+
52+
explicit FileSystemAccessInterfacePrivate(FileSystemAccessInterface *q);
53+
~FileSystemAccessInterfacePrivate();
54+
55+
void beginExecution(ScriptExecutionContext *context);
56+
void scheduleEndExecution(ScriptExecutionContext *context);
57+
void finalizeExecution(quint64 serial);
58+
std::shared_ptr<ExecutionState> executionForContext(ScriptExecutionContext *context) const;
59+
60+
bool resolvePath(ScriptExecutionContext *context, const QString &path, QString *result, FileSystemError *error, const QString &operation) const;
61+
bool normalizePattern(ScriptExecutionContext *context, const QString &pattern, QString *result, FileSystemError *error) const;
62+
bool permissionCoversPath(const ExecutionState &execution, const QString &path, FileAccessMode access) const;
63+
bool permissionCoversPattern(const ExecutionState &execution, const QString &pattern, FileAccessMode access) const;
64+
bool canonicalPermissionCheck(const ExecutionState &execution, const QString &path, FileAccessMode access, bool followTarget, FileSystemError *error, const QString &operation) const;
65+
bool handleCoversPath(const FileSystemHandle &handle, const QString &path) const;
66+
67+
FileSystemHandle createHandle(const std::shared_ptr<ExecutionState> &execution, FileSystemHandle::Kind kind, const QString &path, FileAccessMode access, bool recursive, bool intrinsicCapability, const QString &scopePath = {}, bool scopeRecursive = false) const;
68+
69+
static bool accessCovers(FileAccessMode available, FileAccessMode requested);
70+
static void setPermissionError(FileSystemError *error, const QString &path, const QString &message);
71+
static void setFileSystemError(FileSystemError *error, FileSystemErrorCode code, const QString &operation, const QString &path, const QString &message, const QString &destinationPath = {});
72+
73+
FileSystemAccessInterface *q_ptr;
74+
QHash<QString, SessionPermissions> sessionPermissions;
75+
std::shared_ptr<ExecutionState> currentExecution;
76+
quint64 nextExecutionSerial{};
77+
QPointer<Internal::FileSystemModuleExtension> moduleExtension;
78+
};
79+
80+
}
81+
82+
#endif // DIFFSCOPE_BATCHPROCESS_FILESYSTEMACCESSINTERFACE_P_H
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-FileCopyrightText: Team OpenVPI
2+
// SPDX-License-Identifier: LGPL-3.0-or-later
3+
4+
#include "FileSystemHandle.h"
5+
#include "FileSystemHandle_p.h"
6+
7+
#include <QDir>
8+
9+
#include <batchprocess/ScriptAction.h>
10+
#include <batchprocess/ScriptExecutionContext.h>
11+
12+
namespace BatchProcess {
13+
14+
FileSystemHandle::FileSystemHandle() = default;
15+
16+
FileSystemHandle::FileSystemHandle(FileSystemHandlePrivate *d) : d(d) {
17+
}
18+
19+
FileSystemHandle::FileSystemHandle(const FileSystemHandle &other) = default;
20+
21+
FileSystemHandle::FileSystemHandle(FileSystemHandle &&other) noexcept = default;
22+
23+
FileSystemHandle::~FileSystemHandle() = default;
24+
25+
FileSystemHandle &FileSystemHandle::operator=(const FileSystemHandle &other) = default;
26+
27+
FileSystemHandle &FileSystemHandle::operator=(FileSystemHandle &&other) noexcept = default;
28+
29+
bool FileSystemHandle::isValid() const {
30+
return d && d->kind != Invalid && d->token && d->token->active && d->token->engine && d->token->context && d->token->action && d->token->context->engine() == d->token->engine && d->token->context->action() == d->token->action;
31+
}
32+
33+
FileSystemHandle::Kind FileSystemHandle::kind() const {
34+
return d ? d->kind : Invalid;
35+
}
36+
37+
QString FileSystemHandle::path() const {
38+
return d ? QDir::toNativeSeparators(d->path) : QString();
39+
}
40+
41+
FileAccessMode FileSystemHandle::access() const {
42+
return d ? d->access : FileAccessMode::Read;
43+
}
44+
45+
bool FileSystemHandle::recursive() const {
46+
return d && d->recursive;
47+
}
48+
49+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// SPDX-FileCopyrightText: Team OpenVPI
2+
// SPDX-License-Identifier: LGPL-3.0-or-later
3+
4+
#ifndef DIFFSCOPE_BATCHPROCESS_FILESYSTEMHANDLE_H
5+
#define DIFFSCOPE_BATCHPROCESS_FILESYSTEMHANDLE_H
6+
7+
#include <QExplicitlySharedDataPointer>
8+
#include <QString>
9+
10+
#include <batchprocess/FileSystemTypes.h>
11+
#include <batchprocess/batchprocessglobal.h>
12+
13+
namespace BatchProcess {
14+
15+
namespace Internal {
16+
class FileSystemHandleBridge;
17+
}
18+
19+
class FileSystemAccessInterface;
20+
class FileSystemAccessInterfacePrivate;
21+
class FileSystemHandlePrivate;
22+
23+
class BATCH_PROCESS_EXPORT FileSystemHandle {
24+
public:
25+
enum Kind {
26+
Invalid,
27+
File,
28+
Directory,
29+
};
30+
31+
FileSystemHandle();
32+
FileSystemHandle(const FileSystemHandle &other);
33+
FileSystemHandle(FileSystemHandle &&other) noexcept;
34+
~FileSystemHandle();
35+
36+
FileSystemHandle &operator=(const FileSystemHandle &other);
37+
FileSystemHandle &operator=(FileSystemHandle &&other) noexcept;
38+
39+
bool isValid() const;
40+
Kind kind() const;
41+
QString path() const;
42+
FileAccessMode access() const;
43+
bool recursive() const;
44+
45+
private:
46+
friend class FileSystemAccessInterface;
47+
friend class FileSystemAccessInterfacePrivate;
48+
friend class Internal::FileSystemHandleBridge;
49+
50+
explicit FileSystemHandle(FileSystemHandlePrivate *d);
51+
52+
QExplicitlySharedDataPointer<FileSystemHandlePrivate> d;
53+
};
54+
55+
}
56+
57+
#endif // DIFFSCOPE_BATCHPROCESS_FILESYSTEMHANDLE_H
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-FileCopyrightText: Team OpenVPI
2+
// SPDX-License-Identifier: LGPL-3.0-or-later
3+
4+
#ifndef DIFFSCOPE_BATCHPROCESS_FILESYSTEMHANDLE_P_H
5+
#define DIFFSCOPE_BATCHPROCESS_FILESYSTEMHANDLE_P_H
6+
7+
#include <batchprocess/FileSystemHandle.h>
8+
9+
#include <QJSEngine>
10+
#include <QPointer>
11+
#include <QSharedData>
12+
#include <QSharedPointer>
13+
14+
namespace BatchProcess {
15+
16+
class ScriptAction;
17+
class ScriptExecutionContext;
18+
19+
namespace Internal {
20+
21+
struct FileSystemExecutionToken {
22+
bool active{};
23+
quint64 serial{};
24+
QPointer<QJSEngine> engine;
25+
QPointer<ScriptExecutionContext> context;
26+
QPointer<ScriptAction> action;
27+
};
28+
29+
}
30+
31+
class FileSystemHandlePrivate : public QSharedData {
32+
public:
33+
FileSystemHandle::Kind kind{FileSystemHandle::Invalid};
34+
QString path;
35+
FileAccessMode access{FileAccessMode::Read};
36+
bool recursive{};
37+
bool intrinsicCapability{};
38+
QString scopePath;
39+
bool scopeRecursive{};
40+
quint64 createdAtMutationSerial{};
41+
QSharedPointer<Internal::FileSystemExecutionToken> token;
42+
};
43+
44+
}
45+
46+
#endif // DIFFSCOPE_BATCHPROCESS_FILESYSTEMHANDLE_P_H
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-FileCopyrightText: Team OpenVPI
2+
// SPDX-License-Identifier: LGPL-3.0-or-later
3+
4+
#include "FileSystemTypes.h"
5+
6+
namespace BatchProcess {
7+
8+
bool FileSystemError::hasError() const {
9+
return kind != FileSystemErrorKind::None;
10+
}
11+
12+
void FileSystemError::clear() {
13+
*this = {};
14+
}
15+
16+
}

0 commit comments

Comments
 (0)