QFileDevice Class
The QFileDevice class is the base class of devices that read and write files. More…
| Header | #include <QFileDevice> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Inherits | QIODevice |
| Inherited by | QFile, QSaveFile |
List of all members, including inherited members
Public Types
| typedef | Permission |
| typedef | Permissions |
| enum | FileTime |
| enum | FileError |
| enum | FileHandleFlag |
| typedef | FileHandleFlags |
| enum | MemoryMapFlag |
| typedef | MemoryMapFlags |
Public Functions
| ~QFileDevice() | |
| FileError | error() const |
| void | unsetError() |
| QString | fileName() const |
| int | handle() const |
| bool | flush() |
| bool | resize(qint64 sz) |
| virtual Permissions | permissions() const |
| virtual bool | setPermissions(Permissions permissions) |
| QDateTime | fileTime(FileTime time) const |
| bool | setFileTime(const QDateTime &newDate, FileTime fileTime) |
| uchar * | map(qint64 offset, qint64 size, MemoryMapFlags flags = NoOptions) |
| bool | unmap(uchar *address) |
Reimplemented Public Functions
| void | close() |
| qint64 | pos() const |
| qint64 | size() const |
| bool | seek(qint64 offset) |
| bool | atEnd() const |
| qint64 | bytesAvailable() const |
| bool | canReadLine() const |
Static Public Members
| Permission | ReadOwner |
| Permission | WriteOwner |
| Permission | ExeOwner |
| Permission | ReadUser |
| Permission | WriteUser |
| Permission | ExeUser |
| Permission | ReadGroup |
| Permission | WriteGroup |
| Permission | ExeGroup |
| Permission | ReadOther |
| Permission | WriteOther |
| Permission | ExeOther |
Protected Functions
| QFileDevice() | |
| QFileDevice(QObject *parent) | |
| bool | openPath(const QString &path, OpenMode mode) |
| bool | openPath(const QString &path, OpenMode mode, Permissions createPermissions) |
| bool | openHandle(int fd, std::FILE *fh, OpenMode mode, FileHandleFlags handleFlags) |
| void | setFileError(FileError e, const QString &text) |
Reimplemented Protected Functions
| qint64 | readData(char *data, qint64 maxSize) |
| qint64 | writeData(const char *data, qint64 maxSize) |
| qint64 | readLineData(char *data, qint64 maxSize) |
Detailed Description
QFileDevice implements the file-descriptor based reading, writing and seeking shared by QFile, QSaveFile and QTemporaryFile. It is not instantiated directly. Data is read and written straight through the operating system, without an additional buffer.
The last failure is available from error() and errorString().
See also QFile, QSaveFile, QTemporaryFile and QIODevice.
Differences from Qt
map() and unmap() use mmap(2), and a mapping is removed only by unmap() or the destructor. flush() calls fsync() on the descriptor instead of flushing a user-space buffer. The class is POSIX-only. Permission and Permissions are the QFileInfo types. setFileTime() can change only the access and modification times, and fileTime() reports the birth time only where the kernel and file system provide it (statx() on Linux).
Member Type Documentation
enum QFileDevice::FileError
Identifies the kind of error that last occurred on a file device.
| Constant | Description |
|---|---|
QFileDevice::NoError | No error occurred. |
QFileDevice::ReadError | An error occurred when reading from the file. |
QFileDevice::WriteError | An error occurred when writing to the file. |
QFileDevice::FatalError | A fatal error occurred. |
QFileDevice::ResourceError | Out of resources (for example, too many open files). |
QFileDevice::OpenError | The file could not be opened. |
QFileDevice::AbortError | The operation was aborted. |
QFileDevice::TimeOutError | A timeout occurred. |
QFileDevice::UnspecifiedError | An unspecified error occurred. |
QFileDevice::RemoveError | The file could not be removed. |
QFileDevice::RenameError | The file could not be renamed. |
QFileDevice::PositionError | The position in the file could not be changed. |
QFileDevice::ResizeError | The file could not be resized. |
QFileDevice::PermissionsError | The file could not be accessed. |
QFileDevice::CopyError | The file could not be copied. |
enum QFileDevice::FileHandleFlag
How QFile::open() treats a file handle or descriptor it is given.
| Constant | Description |
|---|---|
QFileDevice::AutoCloseHandle | close() closes the handle too. |
QFileDevice::DontCloseHandle | The handle stays open after close(); this is the default. |
typedef QFileDevice::FileHandleFlags
A QFlags of FileHandleFlag values.
enum QFileDevice::FileTime
Selects one of the time stamps of a file.
| Constant | Description |
|---|---|
QFileDevice::FileAccessTime | When the file was last read. |
QFileDevice::FileBirthTime | When the file was created. |
QFileDevice::FileMetadataChangeTime | When the metadata of the file, such as its permissions, last changed. |
QFileDevice::FileModificationTime | When the contents of the file last changed. |
This function was introduced in 5.10.
See also fileTime() and setFileTime().
enum QFileDevice::MemoryMapFlag
Options of map().
| Constant | Description |
|---|---|
QFileDevice::NoOptions | Changes to the mapped memory are written to the file. |
QFileDevice::MapPrivateOption | Changes to the mapped memory stay private to the mapping and are not written to the file (copy-on-write). |
This function was introduced in 4.4.
typedef QFileDevice::MemoryMapFlags
A QFlags of MemoryMapFlag values.
typedef QFileDevice::Permission
The permission bits of a file, the same type as QFileInfo::Permission, so that values can be passed between the two classes.
typedef QFileDevice::Permissions
A QFlags combination of Permission values, the same type as QFileInfo::Permissions.
Member Function Documentation
QFileDevice::QFileDevice()
Constructs a file device with no parent. The device is not open.
QFileDevice::QFileDevice(QObject *parent)
Constructs a file device with the given parent. The device is not open.
QFileDevice::~QFileDevice()
Destroys the file device, removing the mappings that map() created. Subclasses close the file in their own destructors; a descriptor that is still open here is closed unless it was opened with DontCloseHandle.
bool QFileDevice::atEnd() const
Reimplements QIODevice::atEnd().
Returns true if the file is open and the position is at the end of the file with no buffered data left; otherwise returns false.
qint64 QFileDevice::bytesAvailable() const
Reimplements QIODevice::bytesAvailable().
Returns the number of bytes between the current position and the end of the file, plus any buffered look-ahead data.
bool QFileDevice::canReadLine() const
Reimplements QIODevice::canReadLine().
Returns true if the look-ahead buffer holds a complete line or the file is not at its end; otherwise returns false.
void QFileDevice::close()
Reimplements QIODevice::close().
Closes the file and its descriptor. Does nothing if the file is not open.
FileError QFileDevice::error() const
Returns the file error status. The returned error is NoError if no error has occurred since the file was opened or unsetError() was called.
See also unsetError() and errorString().
QString QFileDevice::fileName() const
Returns the name set by QFile::setFileName() or to the QFile constructors.
See also QFile::setFileName().
QDateTime QFileDevice::fileTime(FileTime time) const
Returns the time stamp time of the file, of the open descriptor or of the file named fileName(). Returns an invalid QDateTime if the file does not exist or the time is not available, such as a birth time on a file system that does not record it.
This function was introduced in 5.10.
See also setFileTime() and QFileInfo::lastModified().
bool QFileDevice::flush()
Flushes the file's pending data to disk by calling fsync() on the descriptor. Returns true if successful, or if the file is not open or does not support synchronizing; otherwise returns false.
See also close().
int QFileDevice::handle() const
Returns the native file descriptor of the open file, or -1 if the file is not open.
uchar * QFileDevice::map(qint64 offset, qint64 size, MemoryMapFlags flags = NoOptions)
Maps size bytes of the file, starting at offset, into memory and returns the address, or nullptr if it fails. The file must be open, and the range must lie within it. The memory is writable if the file is open for writing or flags contains MapPrivateOption. The mapping stays valid after close() until unmap() is called or the object is destroyed.
This function was introduced in 4.4.
See also unmap().
bool QFileDevice::openHandle(int fd, std::FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
Adopts the open descriptor fd, or the stream fh if it is not nullptr, in mode. With AutoCloseHandle in handleFlags, close() closes it too. Subclasses call this from their open() overloads.
See also openPath().
bool QFileDevice::openPath(const QString &path, OpenMode mode)
Opens the file at path in the given mode and stores its descriptor. Returns true on success. Otherwise sets error() and errorString() and returns false.
Append and Truncate imply WriteOnly. The file is created if it does not exist, unless ExistingOnly is given. NewOnly fails if the file already exists. Opening a device that is already open fails with OpenError.
Subclasses call this function from their open() reimplementations.
See also close().
bool QFileDevice::openPath(const QString &path, OpenMode mode, Permissions createPermissions)
This is an overloaded function.
Opens the file at path in the given mode like the other overload. If this creates the file, it is created with the access permissions createPermissions, less what the umask of the process takes away; the permissions of a file that exists already are left alone.
This function was introduced in 6.3.
Permissions QFileDevice::permissions() const
Returns the permissions of the file: of the open descriptor, or of the file named fileName() if the device is not open. Returns no permissions if the file does not exist.
See also setPermissions() and QFileInfo::permissions().
qint64 QFileDevice::pos() const
Reimplements QIODevice::pos().
Returns the current file position, or 0 if the file is not open.
qint64 QFileDevice::readData(char *data, qint64 maxSize)
Reimplements QIODevice::readData().
Reads up to maxSize bytes from the file descriptor into data, retrying when interrupted. Returns the number of bytes read, or -1 on error, in which case error() is set to ReadError.
qint64 QFileDevice::readLineData(char *data, qint64 maxSize)
Reimplements QIODevice::readLineData().
Reads a chunk of up to maxSize bytes into data, ending at the first newline, and leaves the file position just after it. Returns the number of bytes read.
bool QFileDevice::resize(qint64 sz)
Sets the file size to sz bytes. Returns true if the file was resized successfully; returns false if the file is not open or the operation fails. If sz is larger than the file, the extra bytes read as zero; if it is smaller, the file is cut.
See also size().
bool QFileDevice::seek(qint64 offset)
Reimplements QIODevice::seek().
For random-access devices, this function sets the current position to offset, measured from the start of the file. Returns true on success. Returns false if the file is not open, offset is negative, or the position cannot be changed, in which case error() is set to PositionError.
void QFileDevice::setFileError(FileError e, const QString &text)
Sets the file error to e and the error string to text.
See also error() and setErrorString().
bool QFileDevice::setFileTime(const QDateTime &newDate, FileTime fileTime)
Sets the time stamp fileTime of the file to newDate. Returns true if successful; otherwise sets error() to UnspecifiedError and returns false. Only FileAccessTime and FileModificationTime can be changed, and newDate must be valid.
This function was introduced in 5.10.
See also fileTime().
bool QFileDevice::setPermissions(Permissions permissions)
Sets the permissions of the file to permissions. Returns true if successful; otherwise sets error() to PermissionsError and returns false. The owner bits are set if either the Owner or the User flag is given.
See also permissions().
qint64 QFileDevice::size() const
Reimplements QIODevice::size().
Returns the size of the file in bytes. If the file is not open, the size of the file at fileName() is returned. Returns 0 if the size cannot be determined.
bool QFileDevice::unmap(uchar *address)
Removes the mapping at address, which map() returned. Returns true if successful.
This function was introduced in 4.4.
See also map().
void QFileDevice::unsetError()
Sets the file's error to NoError. The error string is not cleared.
See also error().
qint64 QFileDevice::writeData(const char *data, qint64 maxSize)
Reimplements QIODevice::writeData().
Writes up to maxSize bytes from data to the file descriptor, retrying on partial writes. Returns the number of bytes written, or -1 if nothing could be written, in which case error() is set to WriteError.
Member Variable Documentation
Permission QFileDevice::ExeGroup
The file is executable by its group.
Permission QFileDevice::ExeOther
The file is executable by anyone.
Permission QFileDevice::ExeOwner
The file is executable by its owner.
Permission QFileDevice::ExeUser
The file is executable by the user. On POSIX systems this is the owner.
Permission QFileDevice::ReadGroup
The file is readable by its group.
Permission QFileDevice::ReadOther
The file is readable by anyone.
Permission QFileDevice::ReadOwner
The file is readable by its owner.
Permission QFileDevice::ReadUser
The file is readable by the user. On POSIX systems this is the owner.
Permission QFileDevice::WriteGroup
The file is writable by its group.
Permission QFileDevice::WriteOther
The file is writable by anyone.
Permission QFileDevice::WriteOwner
The file is writable by its owner.
Permission QFileDevice::WriteUser
The file is writable by the user. On POSIX systems this is the owner.