Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 "qdirlisting.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
22using namespace Qt::StringLiterals;
23
25{
26 if (!path.isEmpty() && !path.endsWith(u'/')
27#ifdef Q_OS_ANDROID
28 && !path.startsWith("content:/"_L1)
29#endif
30 )
31 return QString{path + u'/'};
32 return path;
33}
34
82
83/*
84 All application-wide handlers are stored in this list. The mutex must be
85 acquired to ensure thread safety.
86 */
87Q_GLOBAL_STATIC(QReadWriteLock, fileEngineHandlerMutex, QReadWriteLock::Recursive)
90{
91 Q_DISABLE_COPY_MOVE(QAbstractFileEngineHandlerList)
92public:
94
96 {
97 QWriteLocker locker(fileEngineHandlerMutex());
99 }
100};
102
103
113{
114 QWriteLocker locker(fileEngineHandlerMutex());
115 qt_file_engine_handlers_in_use.storeRelaxed(true);
116 fileEngineHandlers()->prepend(this);
117}
118
124{
125 QWriteLocker locker(fileEngineHandlerMutex());
126 // Remove this handler from the handler list only if the list is valid.
128 QAbstractFileEngineHandlerList *handlers = fileEngineHandlers();
129 handlers->removeOne(this);
130 if (handlers->isEmpty())
131 qt_file_engine_handlers_in_use.storeRelaxed(false);
132 }
133}
134
135/*
136 \internal
137
138 Handles calls to custom file engine handlers.
139*/
140std::unique_ptr<QAbstractFileEngine> qt_custom_file_engine_handler_create(const QString &path)
141{
142 if (qt_file_engine_handlers_in_use.loadRelaxed()) {
143 QReadLocker locker(fileEngineHandlerMutex());
144
145 // check for registered handlers that can load the file
146 for (QAbstractFileEngineHandler *handler : std::as_const(*fileEngineHandlers())) {
147 if (auto engine = handler->create(path))
148 return engine;
149 }
150 }
151
152 return nullptr;
153}
154
182std::unique_ptr<QAbstractFileEngine> QAbstractFileEngine::create(const QString &fileName)
183{
185 QFileSystemMetaData metaData;
187
188#ifndef QT_NO_FSFILEENGINE
189 if (!engine) // fall back to regular file engine
190 engine = std::make_unique<QFSFileEngine>(entry.filePath());
191#endif
192
193 return engine;
194}
195
325
335
342
356bool QAbstractFileEngine::open(QIODevice::OpenMode openMode,
357 std::optional<QFile::Permissions> permissions)
358{
359 Q_UNUSED(openMode);
360 Q_UNUSED(permissions);
361 return false;
362}
363
370{
371 return false;
372}
373
383{
384 return false;
385}
386
394{
395 return false;
396}
397
402{
403 return 0;
404}
405
412{
413 return 0;
414}
415
428{
429 Q_UNUSED(pos);
430 return false;
431}
432
441{
442 return false;
443}
444
452{
453 return false;
454}
455
461{
462 Q_UNUSED(newName);
463 return false;
464}
465
474{
475 Q_UNUSED(newName);
476 return false;
477}
478
490{
491 Q_UNUSED(newName);
492 return false;
493}
494
502{
503 Q_UNUSED(newName);
504 return false;
505}
506
520bool QAbstractFileEngine::mkdir(const QString &dirName, bool createParentDirectories,
521 std::optional<QFile::Permissions> permissions) const
522{
523 Q_UNUSED(dirName);
524 Q_UNUSED(createParentDirectories);
525 Q_UNUSED(permissions);
526 return false;
527}
528
540bool QAbstractFileEngine::rmdir(const QString &dirName, bool recurseParentDirectories) const
541{
542 Q_UNUSED(dirName);
543 Q_UNUSED(recurseParentDirectories);
544 return false;
545}
546
556{
557 Q_UNUSED(size);
558 return false;
559}
560
566{
567 return false;
568}
569
577{
578 return false;
579}
580
592QStringList QAbstractFileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
593{
595#ifdef QT_BOOTSTRAPPED
597 Q_UNUSED(filterNames);
598 Q_UNREACHABLE_RETURN(ret);
599#else
600 for (const auto &dirEntry : QDirListing(fileName(), filterNames, filters))
601 ret.emplace_back(dirEntry.fileName());
602 return ret;
603#endif
604}
605
619QAbstractFileEngine::FileFlags QAbstractFileEngine::fileFlags(FileFlags type) const
620{
621 Q_UNUSED(type);
622 return {};
623}
624
635{
636 Q_UNUSED(perms);
637 return false;
638}
639
647{
648 return QByteArray();
649}
650
666
675{
677 return 0;
678}
679
689{
691 return QString();
692}
693
694
704{
705 Q_UNUSED(newDate);
706 Q_UNUSED(time);
707 return false;
708}
709
725
736
743{
744 return -1;
745}
746
760{
761 return const_cast<QAbstractFileEngine *>(this)->extension(AtEndExtension);
762}
763
781{
784 if (!extension(MapExtension, &option, &r))
785 return nullptr;
786 return r.address;
787}
788
802{
803 const UnMapExtensionOption options(address);
804 return extension(UnMapExtension, &options);
805}
806
820
898 const QStringList &nameFilters)
899 : m_filters(filters),
900 m_nameFilters(nameFilters),
902{
903}
904
913
922{
923 return m_path;
924}
925
932{
933 return m_nameFilters;
934}
935
942{
943 return m_filters;
944}
945
962{
964 if (name.isNull())
965 return name;
966
967 return path() + name;
968}
969
980{
982 if (m_fileInfo.filePath() != path)
984
985 // return a shallow copy
986 return m_fileInfo;
987}
988
1012 const QStringList &filterNames)
1013{
1014 Q_UNUSED(path);
1016 Q_UNUSED(filterNames);
1017 return {};
1018}
1019
1028{
1029 Q_UNUSED(data);
1030 Q_UNUSED(maxlen);
1031 return -1;
1032}
1033
1039{
1040 Q_UNUSED(data);
1041 Q_UNUSED(len);
1042 return -1;
1043}
1044
1051{
1052 qint64 readSoFar = 0;
1053 while (readSoFar < maxlen) {
1054 char c;
1055 qint64 readResult = read(&c, 1);
1056 if (readResult <= 0)
1057 return (readSoFar > 0) ? readSoFar : -1;
1058 ++readSoFar;
1059 *data++ = c;
1060 if (c == '\n')
1061 return readSoFar;
1062 }
1063 return readSoFar;
1064}
1065
1144
1155{
1157 return false;
1158}
1159
1168{
1169 Q_D(const QAbstractFileEngine);
1170 return d->fileError;
1171}
1172
1181{
1182 Q_D(const QAbstractFileEngine);
1183 return d->errorString;
1184}
1185
1194{
1196 d->fileError = error;
1197 d->errorString = errorString;
1198}
1199
\inmodule QtCore \reentrant
virtual ~QAbstractFileEngineHandler()
Destroys the file handler.
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.
QAbstractFileEngineIterator(const QString &path, QDir::Filters filters, const QStringList &nameFilters)
Constructs a QAbstractFileEngineIterator, using the entry filters filters, and wildcard name filters ...
QString path() const
Returns the path for this iterator.
QStringList nameFilters() const
Returns the name filters for this iterator.
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 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.
static std::unique_ptr< QAbstractFileEngine > create(const QString &fileName)
Creates and returns a QAbstractFileEngine suitable for processing fileName.
virtual bool flush()
Flushes the open file, returning true if successful; otherwise returns false.
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 bool link(const QString &newName)
Creates a link from the file currently specified by fileName() to newName.
virtual bool cloneTo(QAbstractFileEngine *target)
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 bool setFileTime(const QDateTime &newDate, QFile::FileTime time)
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)
std::unique_ptr< Iterator > IteratorUniquePtr
virtual IteratorUniquePtr beginEntryList(const QString &path, QDir::Filters filters, const QStringList &filterNames)
Returns a QAbstractFileEngine::IteratorUniquePtr, that can be used to iterate over the entries in pat...
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
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 QDateTime fileTime(QFile::FileTime time) const
If time is BirthTime, return when the file was born (created).
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.
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore\reentrant
Definition qdatetime.h:283
The QDirListing class provides an STL-style iterator for directory entries.
Definition qdirlisting.h:18
FileError
This enum describes the errors that may be returned by the error() function.
Definition qfiledevice.h:39
void setFile(const QString &file)
QString filePath() const
Returns the path of the file system entry this QFileInfo refers to; the path may be absolute or relat...
static std::unique_ptr< QAbstractFileEngine > createLegacyEngine(QFileSystemEntry &entry, QFileSystemMetaData &data)
Definition qlist.h:75
bool isEmpty() const noexcept
Definition qlist.h:401
bool removeOne(const AT &t)
Definition qlist.h:598
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
void extension()
[6]
Definition dialogs.cpp:230
Combined button and popup list for selecting options.
static Q_CONSTINIT QBasicAtomicInt qt_file_engine_handlers_in_use
std::unique_ptr< QAbstractFileEngine > qt_custom_file_engine_handler_create(const QString &path)
static QString appendSlashIfNeeded(const QString &path)
static Q_CONSTINIT bool qt_abstractfileenginehandlerlist_shutDown
#define Q_BASIC_ATOMIC_INITIALIZER(a)
DBusConnection const char DBusError * error
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
return ret
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLboolean r
[2]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
GLenum target
GLbitfield flags
GLenum GLuint GLintptr offset
GLuint name
const GLubyte * c
GLuint entry
GLuint GLuint64EXT address
GLsizei const GLchar *const * path
GLuint GLenum option
GLenum GLsizei len
#define Q_UNUSED(x)
unsigned char uchar
Definition qtypes.h:32
unsigned int uint
Definition qtypes.h:34
long long qint64
Definition qtypes.h:60
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]