5#include <QtCore/QDebug>
10using namespace Qt::StringLiterals;
40 if (!QWasmFileEngineHandler::isWasmFileName(fileName))
44 if (singleton->m_files.contains(fileName)) {
45 qstdweb::File file = singleton->m_files.value(fileName);
46 return std::make_unique<QWasmFileEngine>(fileName, file);
47 }
else if (singleton->m_fileSystemFiles.contains(fileName)) {
48 qstdweb::FileSystemFileHandle file = singleton->m_fileSystemFiles.value(fileName);
49 return std::make_unique<QWasmFileEngine>(fileName, file);
59 return QUrl(fileName).scheme() == wasmlocalfileScheme;
65 static std::atomic<uint64_t> sid = 0;
66 const uint64_t id = ++sid;
67 return wasmlocalfileScheme + QStringLiteral(
":/%1/%2").arg(id).arg(nativeFileName);
73 QUrl url(wasmFileName);
74 if (url.scheme() == wasmlocalfileScheme) {
75 QString path = url.path();
77 const qsizetype idx = path.indexOf(u'/', 1);
79 return path.mid(idx + 1);
87 QString nativeFileName = QString::fromStdString(file.name());
88 QString wasmFileName = makeWasmFileName(nativeFileName);
89 singleton->m_files.insert(wasmFileName, file);
96 QString nativeFileName = QString::fromStdString(file.name());
97 QString wasmFileName = makeWasmFileName(nativeFileName);
98 singleton->m_fileSystemFiles.insert(wasmFileName, file);
105 singleton->m_files.remove(fileName);
106 singleton->m_fileSystemFiles.remove(fileName);
111 return singleton->m_files.value(fileName);
116 return singleton->m_fileSystemFiles.value(fileName);
120
121
122
123
146bool QWasmFileEngine::open(QIODevice::OpenMode openMode, std::optional<QFile::Permissions> permissions)
148 Q_UNUSED(permissions);
149 m_openMode = openMode;
152 return m_fileDevice->open(openMode);
153 else if (m_blobDevice)
154 return m_blobDevice->open(openMode);
161 if (m_openMode == QIODevice::NotOpen)
166 m_fileDevice->close();
169 m_blobDevice->close();
172 m_openMode = QIODevice::NotOpen;
189 return m_fileDevice->size();
191 return m_blobDevice->size();
198 return m_fileDevice->pos();
200 return m_blobDevice->pos();
207 return m_fileDevice->seek(pos);
209 return m_blobDevice->seek(pos);
248 std::optional<QFile::Permissions> permissions)
const
251 Q_UNUSED(createParentDirectories);
252 Q_UNUSED(permissions);
259 Q_UNUSED(recurseParentDirectories);
282 return type & (QAbstractFileEngine::FileFlag::ExistsFlag |
283 QAbstractFileEngine::FileFlag::FileType |
284 QAbstractFileEngine::FileFlag::ReadOwnerPerm |
285 QAbstractFileEngine::FileFlag::WriteOwnerPerm);
307 QString native = QWasmFileEngineHandler::nativeFileName(m_fileName);
308 QFileInfo info(native);
309 return info.fileName();
312 case AbsolutePathName:
313 case CanonicalPathName: {
314 QString native = QWasmFileEngineHandler::nativeFileName(m_fileName);
315 QFileInfo info(native);
316 QString path = info.path();
317 return path.isEmpty() ?
"."_L1 : path;
349 if (m_fileName == file)
363 return QAbstractFileEngine::TriStateResult::NotSupported;
367 const QStringList &filterNames)
371 Q_UNUSED(filterNames);
377 if (!(m_openMode & QIODevice::ReadOnly))
381 return m_fileDevice->read(data, maxlen);
383 return m_blobDevice->read(data, maxlen);
389 if (!(m_openMode & QIODevice::ReadOnly))
393 return m_fileDevice->readLine(data, maxlen);
395 return m_blobDevice->readLine(data, maxlen);
401 if (!(m_openMode & QIODevice::WriteOnly))
405 return m_fileDevice->write(data, len);
qstdweb::FileSystemFileHandle getFileSystemFile(const QString fileName)
virtual ~QWasmFileEngineHandler() override
virtual std::unique_ptr< QAbstractFileEngine > create(const QString &fileName) const override
If this file handler can handle fileName, this method creates a file engine and returns it wrapped in...
qstdweb::File getFile(const QString fileName)
The QWasmFileEngine class provides a QAbstractFileEngine for files that has the prefix ':weblocalfile...
virtual bool link(const QString &newName) override
Creates a link from the file currently specified by fileName() to newName.
virtual bool flush() override
Flushes the open file, returning true if successful; otherwise returns false.
virtual qint64 read(char *data, qint64 maxlen) override
Reads a number of characters from the file into data.
virtual void setFileName(const QString &file) override
Sets the file engine's file name to file.
virtual bool rmdir(const QString &dirName, bool recurseParentDirectories) const override
Requests that the directory dirName is deleted from the file system.
virtual bool setSize(qint64 size) override
Requests that the file be set to size size.
virtual bool copy(const QString &newName) override
Copies the contents of this file to a file with the name newName.
virtual uint ownerId(FileOwner) const override
If owner is OwnerUser return the ID of the user who owns the file.
virtual bool caseSensitive() const override
Should return true if the underlying file system is case-sensitive; otherwise return false.
virtual QString fileName(FileName file=DefaultName) const override
Return the file engine's current file name in the format specified by file.
virtual qint64 pos() const override
Returns the current file position.
virtual bool supportsExtension(Extension extension) const override
virtual bool extension(Extension extension, const ExtensionOption *option=nullptr, ExtensionReturn *output=nullptr) override
virtual bool close() override
Closes the file, returning true if successful; otherwise returns false.
virtual bool isSequential() const override
Returns true if the file is a sequential access device; returns false if the file is a direct access ...
virtual bool syncToDisk() override
virtual bool seek(qint64 pos) override
Sets the file position to the given offset.
virtual qint64 write(const char *data, qint64 len) override
Writes len bytes from data to the file.
virtual TriStateResult cloneTo(QAbstractFileEngine *target) override
virtual bool rename(const QString &newName) override
Requests that the file be renamed to newName in the file system.
virtual QByteArray id() const override
virtual QString owner(FileOwner) const override
If owner is OwnerUser return the name of the user who owns the file.
virtual qint64 readLine(char *data, qint64 maxlen) override
This function reads one line, terminated by a '\n' character, from the file info data.
virtual bool setPermissions(uint perms) override
Requests that the file's permissions be set to perms.
virtual int handle() const override
Returns the native file handle for this file engine.
virtual bool remove() override
Requests that the file is deleted from the file system.
virtual IteratorUniquePtr beginEntryList(const QString &path, QDirListing::IteratorFlags filters, const QStringList &filterNames) override
Returns a QAbstractFileEngine::IteratorUniquePtr, that can be used to iterate over the entries in pat...
virtual FileFlags fileFlags(FileFlags type=FileInfoAll) const override
This function should return the set of OR'd flags that are true for the file engine's file,...
virtual bool mkdir(const QString &dirName, bool createParentDirectories, std::optional< QFile::Permissions > permissions=std::nullopt) const override
Requests that the directory dirName be created with the specified permissions.
~QWasmFileEngine() override
virtual qint64 size() const override
Returns the size of the file.
QWasmFileEngine(const QString &fileName, qstdweb::File file)
virtual bool renameOverwrite(const QString &newName) override
virtual bool isRelativePath() const override
Return true if the file referred to by this file engine has a relative path; otherwise return false.
virtual QDateTime fileTime(QFile::FileTime time) const override
If time is BirthTime, return when the file was born (created).
virtual bool setFileTime(const QDateTime &newDate, QFile::FileTime time) override
Combined button and popup list for selecting options.
static constexpr QLatin1StringView wasmlocalfileScheme