Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qabstractfileengine.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "private/qabstractfileengine_p.h"
5#include "private/qfsfileengine_p.h"
6#ifdef QT_BUILD_CORE_LIB
7#include "private/qresource_p.h"
8#endif
9#include "qdatetime.h"
10#include "qreadwritelock.h"
11#include "qvariant.h"
12// built-in handlers
13#include "qdiriterator.h"
14#include "qstringbuilder.h"
15
16#include <QtCore/private/qfilesystementry_p.h>
17#include <QtCore/private/qfilesystemmetadata_p.h>
18#include <QtCore/private/qfilesystemengine_p.h>
19
21
69
70/*
71 All application-wide handlers are stored in this list. The mutex must be
72 acquired to ensure thread safety.
73 */
74Q_GLOBAL_STATIC(QReadWriteLock, fileEngineHandlerMutex, QReadWriteLock::Recursive)
77{
78public:
80 {
81 QWriteLocker locker(fileEngineHandlerMutex());
83 }
84};
86
87
97{
98 QWriteLocker locker(fileEngineHandlerMutex());
100 fileEngineHandlers()->prepend(this);
101}
102
108{
109 QWriteLocker locker(fileEngineHandlerMutex());
110 // Remove this handler from the handler list only if the list is valid.
112 QAbstractFileEngineHandlerList *handlers = fileEngineHandlers();
113 handlers->removeOne(this);
114 if (handlers->isEmpty())
116 }
117}
118
119/*
120 \internal
121
122 Handles calls to custom file engine handlers.
123*/
125{
127 QReadLocker locker(fileEngineHandlerMutex());
128
129 // check for registered handlers that can load the file
130 for (QAbstractFileEngineHandler *handler : std::as_const(*fileEngineHandlers())) {
131 if (QAbstractFileEngine *engine = handler->create(path))
132 return engine;
133 }
134 }
135
136 return nullptr;
137}
138
166{
168 QFileSystemMetaData metaData;
170
171#ifndef QT_NO_FSFILEENGINE
172 if (!engine)
173 // fall back to regular file engine
174 return new QFSFileEngine(entry.filePath());
175#endif
176
177 return engine;
178}
179
320{
321 d_ptr->q_ptr = this;
322}
323
330{
331 d_ptr->q_ptr = this;
332}
333
338{
339}
340
354bool QAbstractFileEngine::open(QIODevice::OpenMode openMode,
355 std::optional<QFile::Permissions> permissions)
356{
357 Q_UNUSED(openMode);
358 Q_UNUSED(permissions);
359 return false;
360}
361
368{
369 return false;
370}
371
381{
382 return false;
383}
384
392{
393 return false;
394}
395
400{
401 return 0;
402}
403
410{
411 return 0;
412}
413
426{
427 Q_UNUSED(pos);
428 return false;
429}
430
439{
440 return false;
441}
442
450{
451 return false;
452}
453
459{
460 Q_UNUSED(newName);
461 return false;
462}
463
472{
473 Q_UNUSED(newName);
474 return false;
475}
476
488{
489 Q_UNUSED(newName);
490 return false;
491}
492
500{
501 Q_UNUSED(newName);
502 return false;
503}
504
518bool QAbstractFileEngine::mkdir(const QString &dirName, bool createParentDirectories,
519 std::optional<QFile::Permissions> permissions) const
520{
521 Q_UNUSED(dirName);
522 Q_UNUSED(createParentDirectories);
523 Q_UNUSED(permissions);
524 return false;
525}
526
538bool QAbstractFileEngine::rmdir(const QString &dirName, bool recurseParentDirectories) const
539{
540 Q_UNUSED(dirName);
541 Q_UNUSED(recurseParentDirectories);
542 return false;
543}
544
554{
555 Q_UNUSED(size);
556 return false;
557}
558
564{
565 return false;
566}
567
575{
576 return false;
577}
578
590QStringList QAbstractFileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
591{
593 QDirIterator it(fileName(), filterNames, filters);
594 while (it.hasNext()) {
595 it.next();
596 ret << it.fileName();
597 }
598 return ret;
599}
600
614QAbstractFileEngine::FileFlags QAbstractFileEngine::fileFlags(FileFlags type) const
615{
616 Q_UNUSED(type);
617 return {};
618}
619
630{
631 Q_UNUSED(perms);
632 return false;
633}
634
642{
643 return QByteArray();
644}
645
657{
658 Q_UNUSED(file);
659 return QString();
660}
661
670{
672 return 0;
673}
674
684{
686 return QString();
687}
688
689
699{
700 Q_UNUSED(newDate);
701 Q_UNUSED(time);
702 return false;
703}
704
716{
717 Q_UNUSED(time);
718 return QDateTime();
719}
720
728{
729 Q_UNUSED(file);
730}
731
738{
739 return -1;
740}
741
755{
756 return const_cast<QAbstractFileEngine *>(this)->extension(AtEndExtension);
757}
758
776{
778 option.offset = offset;
779 option.size = size;
780 option.flags = flags;
782 if (!extension(MapExtension, &option, &r))
783 return nullptr;
784 return r.address;
785}
786
800{
801 UnMapExtensionOption options;
802 options.address = address;
803 return extension(UnMapExtension, &options);
804}
805
815{
817 return false;
818}
819
891{
892public:
894 QDir::Filters filters;
897};
898
904 const QStringList &nameFilters)
906{
908 d->filters = filters;
909}
910
917{
918}
919
927{
928 return d->path;
929}
930
937void QAbstractFileEngineIterator::setPath(const QString &path)
938{
939 d->path = path;
940}
941
948{
949 return d->nameFilters;
950}
951
958{
959 return d->filters;
960}
961
978{
980 if (!name.isNull()) {
981 QString tmp = path();
982 if (!tmp.isEmpty()) {
983 if (!tmp.endsWith(u'/'))
984 tmp.append(u'/');
985 name.prepend(tmp);
986 }
987 }
988 return name;
989}
990
1001{
1003 if (d->fileInfo.filePath() != path)
1004 d->fileInfo.setFile(path);
1005
1006 // return a shallow copy
1007 return d->fileInfo;
1008}
1009
1020{
1021 Q_UNUSED(type);
1022 return QVariant();
1023}
1024
1060{
1062 Q_UNUSED(filterNames);
1063 return nullptr;
1064}
1065
1070{
1071 return nullptr;
1072}
1073
1082{
1083 Q_UNUSED(data);
1084 Q_UNUSED(maxlen);
1085 return -1;
1086}
1087
1093{
1094 Q_UNUSED(data);
1095 Q_UNUSED(len);
1096 return -1;
1097}
1098
1105{
1106 qint64 readSoFar = 0;
1107 while (readSoFar < maxlen) {
1108 char c;
1109 qint64 readResult = read(&c, 1);
1110 if (readResult <= 0)
1111 return (readSoFar > 0) ? readSoFar : -1;
1112 ++readSoFar;
1113 *data++ = c;
1114 if (c == '\n')
1115 return readSoFar;
1116 }
1117 return readSoFar;
1118}
1119
1192{
1196 return false;
1197}
1198
1209{
1211 return false;
1212}
1213
1222{
1223 Q_D(const QAbstractFileEngine);
1224 return d->fileError;
1225}
1226
1235{
1236 Q_D(const QAbstractFileEngine);
1237 return d->errorString;
1238}
1239
1248{
1250 d->fileError = error;
1251 d->errorString = errorString;
1252}
1253
\inmodule QtCore \reentrant
virtual ~QAbstractFileEngineHandler()
Destroys the file handler.
The QAbstractFileEngineIterator class provides an iterator interface for custom file engines.
virtual QVariant entryInfo(EntryInfoType type) const
virtual ~QAbstractFileEngineIterator()
Destroys the QAbstractFileEngineIterator.
QDir::Filters filters() const
Returns the entry filters for this iterator.
virtual QString currentFileName() const =0
This pure virtual function returns the name of the current directory entry, excluding the path.
QString path() const
Returns the path for this iterator.
QStringList nameFilters() const
Returns the name filters for this iterator.
QAbstractFileEngineIterator(QDir::Filters filters, const QStringList &nameFilters)
Constructs a QAbstractFileEngineIterator, using the entry filters filters, and wildcard name filters ...
virtual QFileInfo currentFileInfo() const
The virtual function returns a QFileInfo for the current directory entry.
virtual QString currentFilePath() const
Returns the path to the current directory entry.
\inmodule QtCore \reentrant
virtual bool remove()
Requests that the file is deleted from the file system.
virtual bool renameOverwrite(const QString &newName)
QScopedPointer< QAbstractFileEnginePrivate > d_ptr
FileOwner
\value OwnerUser The user who owns the file.
virtual bool supportsExtension(Extension extension) const
virtual ~QAbstractFileEngine()
Destroys the QAbstractFileEngine.
virtual bool copy(const QString &newName)
Copies the contents of this file to a file with the name newName.
virtual bool setPermissions(uint perms)
Requests that the file's permissions be set to perms.
virtual bool setFileTime(const QDateTime &newDate, FileTime time)
virtual bool isRelativePath() const
Return true if the file referred to by this file engine has a relative path; otherwise return false.
virtual void setFileName(const QString &file)
Sets the file engine's file name to file.
virtual bool rmdir(const QString &dirName, bool recurseParentDirectories) const
Requests that the directory dirName is deleted from the file system.
virtual bool close()
Closes the file, returning true if successful; otherwise returns false.
virtual qint64 write(const char *data, qint64 len)
Writes len bytes from data to the file.
virtual int handle() const
Returns the native file handle for this file engine.
virtual bool flush()
Flushes the open file, returning true if successful; otherwise returns false.
virtual Iterator * endEntryList()
QAbstractFileEngine()
Constructs a new QAbstractFileEngine that does not refer to any file or directory.
virtual bool seek(qint64 pos)
Sets the file position to the given offset.
virtual bool mkdir(const QString &dirName, bool createParentDirectories, std::optional< QFile::Permissions > permissions=std::nullopt) const
Requests that the directory dirName be created with the specified permissions.
virtual qint64 readLine(char *data, qint64 maxlen)
This function reads one line, terminated by a '\n' character, from the file info data.
virtual qint64 read(char *data, qint64 maxlen)
Reads a number of characters from the file into data.
virtual bool rename(const QString &newName)
Requests that the file be renamed to newName in the file system.
virtual QDateTime fileTime(FileTime time) const
If time is BirthTime, return when the file was born (created).
virtual bool link(const QString &newName)
Creates a link from the file currently specified by fileName() to newName.
virtual bool cloneTo(QAbstractFileEngine *target)
FileTime
These are used by the fileTime() function.
void setError(QFile::FileError error, const QString &str)
Sets the error type to error, and the error string to errorString.
virtual qint64 size() const
Returns the size of the file.
virtual uint ownerId(FileOwner) const
If owner is OwnerUser return the ID of the user who owns the file.
virtual QStringList entryList(QDir::Filters filters, const QStringList &filterNames) const
Requests that a list of all the files matching the filters list based on the filterNames in the file ...
uchar * map(qint64 offset, qint64 size, QFile::MemoryMapFlags flags)
virtual QString fileName(FileName file=DefaultName) const
Return the file engine's current file name in the format specified by file.
virtual bool isSequential() const
Returns true if the file is a sequential access device; returns false if the file is a direct access ...
virtual QByteArray id() const
static QAbstractFileEngine * create(const QString &fileName)
Creates and returns a QAbstractFileEngine suitable for processing fileName.
virtual FileFlags fileFlags(FileFlags type=FileInfoAll) const
This function should return the set of OR'd flags that are true for the file engine's file,...
virtual Iterator * beginEntryList(QDir::Filters filters, const QStringList &filterNames)
Returns an instance of a QAbstractFileEngineIterator using filters for entry filtering and filterName...
FileName
These values are used to request a file name in a particular format.
virtual bool extension(Extension extension, const ExtensionOption *option=nullptr, ExtensionReturn *output=nullptr)
virtual qint64 pos() const
Returns the current file position.
virtual QString owner(FileOwner) const
If owner is OwnerUser return the name of the user who owns the file.
QFile::FileError error() const
Returns the QFile::FileError that resulted from the last failed operation.
virtual bool caseSensitive() const
Should return true if the underlying file system is case-sensitive; otherwise return false.
virtual bool setSize(qint64 size)
Requests that the file be set to size size.
QString errorString() const
Returns the human-readable message appropriate to the current error reported by error().
virtual bool open(QIODevice::OpenMode openMode, std::optional< QFile::Permissions > permissions=std::nullopt)
Opens the file in the specified mode.
void storeRelaxed(T newValue) noexcept
T loadRelaxed() const noexcept
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore\reentrant
Definition qdatetime.h:257
The QDirIterator class provides an iterator for directory entrylists.
\inmodule QtCore
FileError
This enum describes the errors that may be returned by the error() function.
Definition qfiledevice.h:23
\inmodule QtCore \reentrant
Definition qfileinfo.h:22
void setFile(const QString &file)
Sets the file that the QFileInfo provides information about to file.
QString filePath() const
Returns the file name, including the path (which may be absolute or relative).
static QAbstractFileEngine * resolveEntryAndCreateLegacyEngine(QFileSystemEntry &entry, QFileSystemMetaData &data)
Definition qlist.h:74
bool isEmpty() const noexcept
Definition qlist.h:390
bool removeOne(const AT &t)
Definition qlist.h:581
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition qstring.cpp:5350
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
QString & append(QChar c)
Definition qstring.cpp:3227
QString & prepend(QChar c)
Definition qstring.h:411
\inmodule QtCore
Definition qvariant.h:64
\inmodule QtCore
void extension()
[6]
Definition dialogs.cpp:230
QSet< QString >::iterator it
Combined button and popup list for selecting options.
QAbstractFileEngine * qt_custom_file_engine_handler_create(const QString &path)
static Q_CONSTINIT QBasicAtomicInt qt_file_engine_handlers_in_use
static Q_CONSTINIT bool qt_abstractfileenginehandlerlist_shutDown
#define Q_BASIC_ATOMIC_INITIALIZER(a)
DBusConnection const char DBusError * error
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
return ret
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLboolean r
[2]
GLenum type
GLenum target
GLbitfield flags
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint GLintptr offset
GLuint name
const GLubyte * c
GLuint entry
GLenum GLsizei len
GLuint GLuint64EXT address
GLsizei const GLchar *const * path
GLuint GLenum option
#define Q_UNUSED(x)
unsigned char uchar
Definition qtypes.h:27
unsigned int uint
Definition qtypes.h:29
long long qint64
Definition qtypes.h:55
QT_BEGIN_NAMESPACE typedef uchar * output
QFile file
[0]
const QStringList filters({"Image files (*.png *.xpm *.jpg)", "Text files (*.txt)", "Any files (*)" })
[6]
QJSEngine engine
[0]