summaryrefslogtreecommitdiffstats
path: root/src/corelib/io/qrandomaccessasyncfile.cpp
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2025-07-04 17:24:11 +0200
committerIvan Solovev <ivan.solovev@qt.io>2025-09-15 13:22:47 +0200
commit65b7217ae71ad8b0448a0ba60e7e33350e84d97c (patch)
treecc85435ff06fa50aab4aa5dc178661573dd7da21 /src/corelib/io/qrandomaccessasyncfile.cpp
parentcc16e9e4030f310cf344b340a01c99c257f3841b (diff)
Add QRandomAccessAsyncFile and QIOOperation
For now, as a private API. The QRandomAccessAsyncFile class is supposed to handle async read and write operations. Note that some operations (e.g. open() and size()) are synchronous, because the tests showed that they normally would not block even if the file is not actually downloaded (e.g. stored on a MS OneDrive). The implementation for async calls is inspired by QNetworkAccessManager and QNetworkReply. The request for an async read or write returns a pointer to a QIOOperation object. That object will emit a finished() signal when the operation is complete, and an errorOccurred() signal, if there was any error. The user has to connect to these signals to handle the results. The typical usecase would be: QRandomAccessAsyncFile file; file.open(path, mode); auto op = file.read(offset, maxSize); connect(op, &QIOOperation::finished, &file, [op] { if (op->error() != QIOOperation::Error::None) // handle error // handle the data op->deleteLater(); }); Similarly to QNetworkReply, the user has to call deleteLater() in the callback that is connected to the signal. The API provides two types of methods: * owning methods that take ownership of the provided data-to-write or read into a buffer that is allocated internally. These are QRAAF::read() and QRAAF::write(). They exist for simplicity and user convenience. * non-owning methods that rely on the user to keep the provided buffers alive as long as the operation in not completed. These are QRAAF::readInto() and QRAAF::writeFrom(). They have overloads taking span-of-spans, which should allow to implement vectored IO. QIOOperation should become a public class at some point. This means that its APIs should be easy to use, and also extensible. It should not be limited to only Read and Write. The hierarchy of IO operations is represented by QIOOperation and its derived classes. The base class can be used when the user is not interested in the data of the operation, or if the operation should only report success of failure. The derived classes implement data() methods with various return types. The classes that represent Read and Write operations also additionally provide offset() and numBytesProcessed() methods. The patch also introduces QtPrivate::QIOOperationDataStorage that holds a std::variant of all possible values that the operation can contain. If needed, this variant can be extended to hold a QVariant in order to store an arbitrary value. This patch also provides the fallback QThreadpool-based implementation that simply executes the requests on the dedicated threadpool using QFuture. For simplicity, this implementation uses QFSFileEngine to provide all operations. The implementations for various backends should be added in follow-up patches. Task-number: QTBUG-136763 Change-Id: I8f34f9e78d91aa35756352de7fbe6544b58de23e Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/corelib/io/qrandomaccessasyncfile.cpp')
-rw-r--r--src/corelib/io/qrandomaccessasyncfile.cpp183
1 files changed, 183 insertions, 0 deletions
diff --git a/src/corelib/io/qrandomaccessasyncfile.cpp b/src/corelib/io/qrandomaccessasyncfile.cpp
new file mode 100644
index 00000000000..92cf2278b69
--- /dev/null
+++ b/src/corelib/io/qrandomaccessasyncfile.cpp
@@ -0,0 +1,183 @@
+// Copyright (C) 2025 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
+// Qt-Security score:significant reason:default
+
+#include "qrandomaccessasyncfile_p.h"
+#include "qrandomaccessasyncfile_p_p.h"
+
+QT_BEGIN_NAMESPACE
+
+QRandomAccessAsyncFile::QRandomAccessAsyncFile(QObject *parent)
+ : QObject{*new QRandomAccessAsyncFilePrivate, parent}
+{
+ d_func()->init();
+}
+
+QRandomAccessAsyncFile::~QRandomAccessAsyncFile()
+{
+ close();
+}
+
+bool QRandomAccessAsyncFile::open(const QString &filePath, QIODeviceBase::OpenMode mode)
+{
+ Q_D(QRandomAccessAsyncFile);
+ return d->open(filePath, mode);
+}
+
+void QRandomAccessAsyncFile::close()
+{
+ Q_D(QRandomAccessAsyncFile);
+ d->close();
+}
+
+qint64 QRandomAccessAsyncFile::size() const
+{
+ Q_D(const QRandomAccessAsyncFile);
+ return d->size();
+}
+
+/*!
+ \internal
+
+ Reads at maximum \a maxSize bytes, starting from \a offset.
+
+ The data is written to the internal buffer managed by the returned
+ QIOOperation object.
+
+//! [returns-qiooperation]
+ Returns a QIOOperation object that would emit a QIOOperation::finished()
+ signal once the operation is complete.
+//! [returns-qiooperation]
+*/
+QIOReadOperation *QRandomAccessAsyncFile::read(qint64 offset, qint64 maxSize)
+{
+ Q_D(QRandomAccessAsyncFile);
+ return d->read(offset, maxSize);
+}
+
+/*!
+ \internal
+ \overload
+
+ Reads the data from the file, starting from \a offset, and stores it into
+ \a buffer.
+
+ The amount of bytes to be read from the file is determined by the size of
+ the buffer. Note that the actual amount of read bytes can be less than that.
+
+ This operation does not take ownership of the provided buffer, so it is the
+ user's responsibility to make sure that the buffer is valid until the
+ returned QIOOperation completes.
+
+ \note The buffer might be populated from different threads, so the user
+ application should not access it until the returned QIOOperation completes.
+
+ \include qrandomaccessasyncfile.cpp returns-qiooperation
+*/
+QIOVectoredReadOperation *
+QRandomAccessAsyncFile::readInto(qint64 offset, QSpan<std::byte> buffer)
+{
+ Q_D(QRandomAccessAsyncFile);
+ return d->readInto(offset, buffer);
+}
+
+/*!
+ \internal
+
+ Reads the data from the file, starting from \a offset, and stores it into
+ \a buffers.
+
+ The amount of bytes to be read from the file is determined by the sum of
+ sizes of all buffers. Note that the actual amount of read bytes can be less
+ than that.
+
+ This operation does not take ownership of the provided buffers, so it is the
+ user's responsibility to make sure that the buffers are valid until the
+ returned QIOOperation completes.
+
+ \note The buffers might be populated from different threads, so the user
+ application should not access them until the returned QIOOperation completes.
+
+ \include qrandomaccessasyncfile.cpp returns-qiooperation
+*/
+QIOVectoredReadOperation *
+QRandomAccessAsyncFile::readInto(qint64 offset, QSpan<const QSpan<std::byte>> buffers)
+{
+ Q_D(QRandomAccessAsyncFile);
+ return d->readInto(offset, buffers);
+}
+
+/*!
+ \internal
+
+ Writes \a data into the file, starting from \a offset.
+
+ The \a data array is copied into the returned QIOOperation object.
+
+ \include qrandomaccessasyncfile.cpp returns-qiooperation
+*/
+QIOWriteOperation *QRandomAccessAsyncFile::write(qint64 offset, const QByteArray &data)
+{
+ Q_D(QRandomAccessAsyncFile);
+ return d->write(offset, data);
+}
+
+/*!
+ \internal
+ \overload
+
+ Writes \a data into the file, starting from \a offset.
+
+ The \a data array is moved into the returned QIOOperation object.
+
+ \include qrandomaccessasyncfile.cpp returns-qiooperation
+*/
+QIOWriteOperation *QRandomAccessAsyncFile::write(qint64 offset, QByteArray &&data)
+{
+ Q_D(QRandomAccessAsyncFile);
+ return d->write(offset, std::move(data));
+}
+
+/*!
+ \internal
+ \overload
+
+ Writes the content of \a buffer into the file, starting from \a offset.
+
+ This operation does not take ownership of the provided buffer, so it is the
+ user's responsibility to make sure that the buffer is valid until the
+ returned QIOOperation completes.
+
+ \note The buffer might be accessed from different threads, so the user
+ application should not modify it until the returned QIOOperation completes.
+*/
+QIOVectoredWriteOperation *
+QRandomAccessAsyncFile::writeFrom(qint64 offset, QSpan<const std::byte> buffer)
+{
+ Q_D(QRandomAccessAsyncFile);
+ return d->writeFrom(offset, buffer);
+}
+
+/*!
+ \internal
+
+ Writes the content of \a buffers into the file, starting from \a offset.
+
+ This operation does not take ownership of the provided buffers, so it is the
+ user's responsibility to make sure that the buffers are valid until the
+ returned QIOOperation completes.
+
+ \note The buffers might be accessed from different threads, so the user
+ application should not modify them until the returned QIOOperation
+ completes.
+*/
+QIOVectoredWriteOperation *
+QRandomAccessAsyncFile::writeFrom(qint64 offset, QSpan<const QSpan<const std::byte>> buffers)
+{
+ Q_D(QRandomAccessAsyncFile);
+ return d->writeFrom(offset, buffers);
+}
+
+QT_END_NAMESPACE
+
+#include "moc_qrandomaccessasyncfile_p.cpp"