QAbstractFileEngine Class

The QAbstractFileEngine class provides an abstraction for accessing the filesystem. More...

Header: #include <QAbstractFileEngine>
Inherited By:

QFSFileEngine

Public Types

class ExtensionOption
class ExtensionReturn
enum Extension { AtEndExtension, FastReadLineExtension, MapExtension, UnMapExtension }
enum FileFlag { ReadOwnerPerm, WriteOwnerPerm, ExeOwnerPerm, ReadUserPerm, WriteUserPerm, …, Refresh }
flags FileFlags
enum FileName { DefaultName, BaseName, PathName, AbsoluteName, AbsolutePathName, …, JunctionName }
enum FileOwner { OwnerUser, OwnerGroup }
enum FileTime { BirthTime, MetadataChangeTime, ModificationTime, AccessTime }
Iterator

Public Functions

virtual ~QAbstractFileEngine()
bool atEnd() const
virtual Iterator *beginEntryList(QDir::Filters filters, const QStringList &filterNames)
virtual bool caseSensitive() const
virtual bool cloneTo(QAbstractFileEngine *target)
virtual bool close()
virtual bool copy(const QString &newName)
virtual Iterator *endEntryList()
virtual QStringList entryList(QDir::Filters filters, const QStringList &filterNames) const
QFile::FileError error() const
QString errorString() const
virtual bool extension(Extension extension, const ExtensionOption *option = nullptr, ExtensionReturn *output = nullptr)
virtual FileFlags fileFlags(FileFlags type = ...) const
virtual QString fileName(FileName file = DefaultName) const
virtual QDateTime fileTime(FileTime time) const
virtual bool flush()
virtual int handle() const
virtual QByteArray id() const
virtual bool isRelativePath() const
virtual bool isSequential() const
virtual bool link(const QString &newName)
uchar *map(qint64 offset, qint64 size, QFile::MemoryMapFlags flags)
virtual bool mkdir(const QString &dirName, bool createParentDirectories, int permissions = ...) const
virtual QString owner(FileOwner owner) const
virtual uint ownerId(FileOwner owner) const
virtual qint64 pos() const
virtual qint64 read(char *data, qint64 maxlen)
virtual qint64 readLine(char *data, qint64 maxlen)
virtual bool remove()
virtual bool rename(const QString &newName)
virtual bool renameOverwrite(const QString &newName)
virtual bool rmdir(const QString &dirName, bool recurseParentDirectories) const
virtual void setFileName(const QString &file)
virtual bool setFileTime(const QDateTime &newDate, FileTime time)
virtual bool setPermissions(uint perms)
virtual bool setSize(qint64 size)
virtual qint64 size() const
virtual bool supportsExtension(Extension extension) const
virtual bool syncToDisk()
bool unmap(uchar *address)
virtual qint64 write(const char *data, qint64 len)

Static Public Members

QAbstractFileEngine *create(const QString &fileName)

Protected Functions

QAbstractFileEngine()
QAbstractFileEngine(QAbstractFileEnginePrivate &dd)
void setError(QFile::FileError error, const QString &errorString)

Detailed Description

\inmoduleQtCore \reentrant \internal

\ingroupio \since 4.1

The QDir, QFile, and QFileInfo classes all make use of a QAbstractFileEngine internally. If you create your own QAbstractFileEngine subclass (and register it with Qt by creating a QAbstractFileEngineHandler subclass), your file engine will be used when the path is one that your file engine handles.

A QAbstractFileEngine refers to one file or one directory. If the referent is a file, the setFileName(), rename(), and remove() functions are applicable. If the referent is a directory the mkdir(), rmdir(), and entryList() functions are applicable. In all cases the caseSensitive(), isRelativePath(), fileFlags(), ownerId(), owner(), and fileTime() functions are applicable.

A QAbstractFileEngine subclass can be created to do synchronous network I/O based file system operations, local file system operations, or to operate as a resource system to access file based resources.

See also QAbstractFileEngineHandler.

Member Type Documentation

enum QAbstractFileEngine::Extension

\since4.3

This enum describes the types of extensions that the file engine can support. Before using these extensions, you must verify that the extension is supported (i.e., call supportsExtension()).

ConstantValueDescription
QAbstractFileEngine::AtEndExtension0Whether the current file position is at the end of the file or not. This extension allows file engines that implement local buffering to report end-of-file status without having to check the size of the file. It is also useful for sequential files, where the size of the file cannot be used to determine whether or not you have reached the end. This extension returns true if the file is at the end; otherwise it returns false. The input and output arguments to extension() are ignored.
QAbstractFileEngine::FastReadLineExtension1Whether the file engine provides a fast implementation for readLine() or not. If readLine() remains unimplemented in the file engine, QAbstractFileEngine will provide an implementation based on calling read() repeatedly. If supportsExtension() returns false for this extension, however, QIODevice can provide a faster implementation by making use of its internal buffer. For engines that already provide a fast readLine() implementation, returning false for this extension can avoid unnecessary double-buffering in QIODevice.
QAbstractFileEngine::MapExtension2Whether the file engine provides the ability to map a file to memory.
QAbstractFileEngine::UnMapExtension3Whether the file engine provides the ability to unmap memory that was previously mapped.

enum QAbstractFileEngine::FileFlag
flags QAbstractFileEngine::FileFlags

The permissions and types of a file, suitable for OR'ing together.

ConstantValueDescription
QAbstractFileEngine::ReadOwnerPerm0x4000The owner of the file has permission to read it.
QAbstractFileEngine::WriteOwnerPerm0x2000The owner of the file has permission to write to it.
QAbstractFileEngine::ExeOwnerPerm0x1000The owner of the file has permission to execute it.
QAbstractFileEngine::ReadUserPerm0x0400The current user has permission to read the file.
QAbstractFileEngine::WriteUserPerm0x0200The current user has permission to write to the file.
QAbstractFileEngine::ExeUserPerm0x0100The current user has permission to execute the file.
QAbstractFileEngine::ReadGroupPerm0x0040Members of the current user's group have permission to read the file.
QAbstractFileEngine::WriteGroupPerm0x0020Members of the current user's group have permission to write to the file.
QAbstractFileEngine::ExeGroupPerm0x0010Members of the current user's group have permission to execute the file.
QAbstractFileEngine::ReadOtherPerm0x0004All users have permission to read the file.
QAbstractFileEngine::WriteOtherPerm0x0002All users have permission to write to the file.
QAbstractFileEngine::ExeOtherPerm0x0001All users have permission to execute the file.
QAbstractFileEngine::LinkType0x10000The file is a link to another file (or link) in the file system (i.e. not a file or directory).
QAbstractFileEngine::FileType0x20000The file is a regular file to the file system (i.e. not a link or directory)
QAbstractFileEngine::BundleType0x80000macOS and iOS: the file is a bundle; implies DirectoryType
QAbstractFileEngine::DirectoryType0x40000The file is a directory in the file system (i.e. not a link or file).
QAbstractFileEngine::HiddenFlag0x0100000The file is hidden.
QAbstractFileEngine::ExistsFlag0x0400000The file actually exists in the file system.
QAbstractFileEngine::RootFlag0x0800000The file or the file pointed to is the root of the filesystem.
QAbstractFileEngine::LocalDiskFlag0x0200000The file resides on the local disk and can be passed to standard file functions.
QAbstractFileEngine::Refresh0x1000000Passing this flag will force the file engine to refresh all flags.

The FileFlags type is a typedef for QFlags<FileFlag>. It stores an OR combination of FileFlag values.

See also fileFlags() and setFileName().

enum QAbstractFileEngine::FileName

These values are used to request a file name in a particular format.

ConstantValueDescription
QAbstractFileEngine::DefaultName0The same filename that was passed to the QAbstractFileEngine.
QAbstractFileEngine::BaseName1The name of the file excluding the path.
QAbstractFileEngine::PathName2The path to the file excluding the base name.
QAbstractFileEngine::AbsoluteName3The absolute path to the file (including the base name).
QAbstractFileEngine::AbsolutePathName4The absolute path to the file (excluding the base name).
QAbstractFileEngine::AbsoluteLinkTarget5The full file name of the file that this file is a link to. (This will be empty if this file is not a link.)
QAbstractFileEngine::RawLinkPath10The raw link path of the file that this file is a link to. (This will be empty if this file is not a link.)
QAbstractFileEngine::CanonicalName6Often very similar to AbsoluteLinkTarget. Will return the true path to the file.
QAbstractFileEngine::CanonicalPathName7Same as CanonicalName, excluding the base name.
QAbstractFileEngine::BundleName8Returns the name of the bundle implies BundleType is set.
QAbstractFileEngine::JunctionName9The full name of the directory that this NTFS junction is linked to. (This will be empty if this file is not an NTFS junction.)

See also fileName() and setFileName().

enum QAbstractFileEngine::FileOwner

ConstantValueDescription
QAbstractFileEngine::OwnerUser0The user who owns the file.
QAbstractFileEngine::OwnerGroup1The group who owns the file.

See also owner(), ownerId(), and setFileName().

enum QAbstractFileEngine::FileTime

These are used by the fileTime() function.

ConstantValueDescription
QAbstractFileEngine::BirthTime1When the file was born (created).
QAbstractFileEngine::MetadataChangeTime2When the file's metadata was last changed.
QAbstractFileEngine::ModificationTime3When the file was most recently modified.
QAbstractFileEngine::AccessTime0When the file was most recently accessed (e.g. read or written to).

See also setFileName().

QAbstractFileEngine::Iterator

\since4.3

Synonym for QAbstractFileEngineIterator.

Member Function Documentation

[protected] QAbstractFileEngine::QAbstractFileEngine()

Constructs a new QAbstractFileEngine that does not refer to any file or directory.

See also setFileName().

[protected] QAbstractFileEngine::QAbstractFileEngine(QAbstractFileEnginePrivate &dd)

\internal

Constructs a QAbstractFileEngine.

[virtual noexcept] QAbstractFileEngine::~QAbstractFileEngine()

Destroys the QAbstractFileEngine.

bool QAbstractFileEngine::atEnd() const

\since4.3

Returns true if the current position is at the end of the file; otherwise, returns false.

This function bases its behavior on calling extension() with AtEndExtension. If the engine does not support this extension, false is returned.

See also extension(), supportsExtension(), and QFile::atEnd().

[virtual] Iterator *QAbstractFileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)

Returns an instance of a QAbstractFileEngineIterator using filters for entry filtering and filterNames for name filtering. This function is called by QDirIterator to initiate directory iteration.

QDirIterator takes ownership of the returned instance, and deletes it when it's done.

See also QDirIterator.

[virtual] bool QAbstractFileEngine::caseSensitive() const

Should return true if the underlying file system is case-sensitive; otherwise return false.

[virtual] bool QAbstractFileEngine::cloneTo(QAbstractFileEngine *target)

\since5.10

Duplicates the contents of this file (starting from the current position) to the file specified by the engine target.

Returns true on success; otherwise, false is returned.

[virtual] bool QAbstractFileEngine::close()

Closes the file, returning true if successful; otherwise returns false.

The default implementation always returns false.

[virtual] bool QAbstractFileEngine::copy(const QString &newName)

Copies the contents of this file to a file with the name newName. Returns true on success; otherwise, false is returned.

[static] QAbstractFileEngine *QAbstractFileEngine::create(const QString &fileName)

Creates and returns a QAbstractFileEngine suitable for processing fileName.

You should not need to call this function; use QFile, QFileInfo or QDir directly instead.

If you reimplemnt this function, it should only return file engines that knows how to handle fileName; otherwise, it should return 0.

See also QAbstractFileEngineHandler.

[virtual] Iterator *QAbstractFileEngine::endEntryList()

\internal

[virtual] QStringList QAbstractFileEngine::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 engine's directory are returned.

Should return an empty list if the file engine refers to a file rather than a directory, or if the directory is unreadable or does not exist or if nothing matches the specifications.

See also setFileName().

QFile::FileError QAbstractFileEngine::error() const

Returns the QFile::FileError that resulted from the last failed operation. If QFile::UnspecifiedError is returned, QFile will use its own idea of the error status.

See also setError(), QFile::FileError, and errorString().

QString QAbstractFileEngine::errorString() const

Returns the human-readable message appropriate to the current error reported by error(). If no suitable string is available, an empty string is returned.

See also error().

[virtual] bool QAbstractFileEngine::extension(Extension extension, const ExtensionOption *option = nullptr, ExtensionReturn *output = nullptr)

\since4.3

This virtual function can be reimplemented in a QAbstractFileEngine subclass to provide support for extensions. The option argument is provided as input to the extension, and this function can store output results in output.

The behavior of this function is determined by extension; see the Extension documentation for details.

You can call supportsExtension() to check if an extension is supported by the file engine.

By default, no extensions are supported, and this function returns false.

See also supportsExtension() and Extension.

[virtual] FileFlags QAbstractFileEngine::fileFlags(FileFlags type = ...) const

This function should return the set of OR'd flags that are true for the file engine's file, and that are in the type's OR'd members.

In your reimplementation you can use the type argument as an optimization hint and only return the OR'd set of members that are true and that match those in type; in other words you can ignore any members not mentioned in type, thus avoiding some potentially expensive lookups or system calls.

See also setFileName().

[virtual] QString QAbstractFileEngine::fileName(FileName file = DefaultName) const

Return the file engine's current file name in the format specified by file.

If you don't handle some FileName possibilities, return the file name set in setFileName() when an unhandled format is requested.

See also setFileName() and FileName.

[virtual] QDateTime QAbstractFileEngine::fileTime(FileTime time) const

If time is BirthTime, return when the file was born (created). If time is MetadataChangeTime, return when the file's metadata was last changed. If time is ModificationTime, return when the file was most recently modified. If time is AccessTime, return when the file was most recently accessed (e.g. read or written). If the time cannot be determined return QDateTime() (an invalid date time).

See also setFileTime(), setFileName(), QDateTime, QDateTime::isValid(), and FileTime.

[virtual] bool QAbstractFileEngine::flush()

Flushes the open file, returning true if successful; otherwise returns false.

The default implementation always returns false.

[virtual] int QAbstractFileEngine::handle() const

Returns the native file handle for this file engine. This handle must be used with care; its value and type are platform specific, and using it will most likely lead to non-portable code.

[virtual] QByteArray QAbstractFileEngine::id() const

\since5.9

Return an identifier that (hopefully) uniquely identifies this file in the system. Returns an invalid QByteArray() if that cannot be calculated.

[virtual] bool QAbstractFileEngine::isRelativePath() const

Return true if the file referred to by this file engine has a relative path; otherwise return false.

See also setFileName().

[virtual] bool QAbstractFileEngine::isSequential() const

Returns true if the file is a sequential access device; returns false if the file is a direct access device.

Operations involving size() and seek(qint64) are not valid on sequential devices.

Creates a link from the file currently specified by fileName() to newName. What a link is depends on the underlying filesystem (be it a shortcut on Windows or a symbolic link on Unix). Returns true if successful; otherwise returns false.

uchar *QAbstractFileEngine::map(qint64 offset, qint64 size, QFile::MemoryMapFlags flags)

\since4.4

Maps size bytes of the file into memory starting at offset. Returns a pointer to the memory if successful; otherwise returns false if, for example, an error occurs.

This function bases its behavior on calling extension() with MapExtensionOption. If the engine does not support this extension, 0 is returned.

flags is currently not used, but could be used in the future.

See also unmap() and supportsExtension().

[virtual] bool QAbstractFileEngine::mkdir(const QString &dirName, bool createParentDirectories, int permissions = ...) const

Requests that the directory dirName be created with the specified permissions. If createParentDirectories is true, then any sub-directories in dirName that don't exist must be created. If createParentDirectories is false then any sub-directories in dirName must already exist for the function to succeed. If the operation succeeds return true; otherwise return false.

If permissions is null then implementation-specific default permissions are used.

See also setFileName(), rmdir(), and isRelativePath().

[virtual] QString QAbstractFileEngine::owner(FileOwner owner) const

If owner is OwnerUser return the name of the user who owns the file. If owner is OwnerGroup return the name of the group that own the file. If you can't determine the owner return QString().

See also ownerId(), setFileName(), and FileOwner.

[virtual] uint QAbstractFileEngine::ownerId(FileOwner owner) const

If owner is OwnerUser return the ID of the user who owns the file. If owner is OwnerGroup return the ID of the group that own the file. If you can't determine the owner return -2.

See also owner(), setFileName(), and FileOwner.

[virtual] qint64 QAbstractFileEngine::pos() const

Returns the current file position.

This is the position of the data read/write head of the file.

[virtual] qint64 QAbstractFileEngine::read(char *data, qint64 maxlen)

Reads a number of characters from the file into data. At most maxlen characters will be read.

Returns -1 if a fatal error occurs, or 0 if there are no bytes to read.

[virtual] qint64 QAbstractFileEngine::readLine(char *data, qint64 maxlen)

This function reads one line, terminated by a '\n' character, from the file info data. At most maxlen characters will be read. The end-of-line character is included.

[virtual] bool QAbstractFileEngine::remove()

Requests that the file is deleted from the file system. If the operation succeeds return true; otherwise return false.

See also setFileName() and rmdir().

[virtual] bool QAbstractFileEngine::rename(const QString &newName)

Requests that the file be renamed to newName in the file system. If the operation succeeds return true; otherwise return false.

See also setFileName().

[virtual] bool QAbstractFileEngine::renameOverwrite(const QString &newName)

\since5.1

Requests that the file be renamed to newName in the file system. If the new name already exists, it must be overwritten. If the operation succeeds, returns true; otherwise returns false.

See also setFileName().

[virtual] bool QAbstractFileEngine::rmdir(const QString &dirName, bool recurseParentDirectories) const

Requests that the directory dirName is deleted from the file system. When recurseParentDirectories is true, then any empty parent-directories in dirName must also be deleted. If recurseParentDirectories is false, only the dirName leaf-node should be deleted. In most file systems a directory cannot be deleted using this function if it is non-empty. If the operation succeeds return true; otherwise return false.

See also setFileName(), remove(), mkdir(), and isRelativePath().

[protected] void QAbstractFileEngine::setError(QFile::FileError error, const QString &errorString)

Sets the error type to error, and the error string to errorString. Call this function to set the error values returned by the higher-level classes.

See also QFile::error(), QIODevice::errorString(), and QIODevice::setErrorString().

[virtual] void QAbstractFileEngine::setFileName(const QString &file)

Sets the file engine's file name to file. This file name is the file that the rest of the virtual functions will operate on.

See also fileName() and rename().

[virtual] bool QAbstractFileEngine::setFileTime(const QDateTime &newDate, FileTime time)

\since5.10

Sets the file time to newDate, returning true if successful; otherwise returns false.

See also fileTime().

[virtual] bool QAbstractFileEngine::setPermissions(uint perms)

Requests that the file's permissions be set to perms. The argument perms will be set to the OR-ed together combination of QAbstractFileEngine::FileInfo, with only the QAbstractFileEngine::PermsMask being honored. If the operations succceeds return true; otherwise return false;

See also size().

[virtual] bool QAbstractFileEngine::setSize(qint64 size)

Requests that the file be set to size size. If size is larger than the current file then it is filled with 0's, if smaller it is simply truncated. If the operations succceeds return true; otherwise return false;

See also size().

[virtual] qint64 QAbstractFileEngine::size() const

Returns the size of the file.

See also setSize().

[virtual] bool QAbstractFileEngine::supportsExtension(Extension extension) const

\since4.3

This virtual function returns true if the file engine supports extension; otherwise, false is returned. By default, no extensions are supported.

See also extension().

[virtual] bool QAbstractFileEngine::syncToDisk()

\since5.1

Flushes and syncs the file to disk.

Returns true if successful; otherwise returns false. The default implementation always returns false.

bool QAbstractFileEngine::unmap(uchar *address)

\since4.4

Unmaps the memory address. Returns true if the unmap succeeds; otherwise returns false.

This function bases its behavior on calling extension() with UnMapExtensionOption. If the engine does not support this extension, false is returned.

See also map() and supportsExtension().

[virtual] qint64 QAbstractFileEngine::write(const char *data, qint64 len)

Writes len bytes from data to the file. Returns the number of characters written on success; otherwise returns -1.