9#include "qplatformdefs.h"
11#include <QtCore/qdir.h>
12#include <QtCore/qfile.h>
13#include <QtCore/private/qfilesystemengine_p.h>
21 return type == QIOOperation::Type::Flush || type == QIOOperation::Type::Open;
28template <
typename Operation,
typename ...Args>
34 priv->offset = offset;
37 Operation *op =
new Operation(*priv, q_ptr);
38 auto opId = getNextId();
39 m_operations.push_back(OperationInfo(opId, op));
40 startOperationsUntilBarrier();
59 auto it = std::find_if(m_operations.cbegin(), m_operations.cend(),
60 [op](
const auto &opInfo) {
61 return opInfo.operation.get() == op;
64 if (it == m_operations.cend())
67 const auto opInfo = m_operations.takeAt(std::distance(m_operations.cbegin(), it));
69 if (opInfo.state == OpState::Running) {
72 if (m_runningOps.contains(opInfo.opId)) {
73 m_opToCancel = opInfo.opId;
74 closeIoChannel(opInfo.channel);
75 m_cancellationCondition.wait(&m_mutex);
76 m_opToCancel = kInvalidOperationId;
82 releaseIoChannel(opInfo.channel);
83 auto *priv = QIOOperationPrivate::get(opInfo.operation);
84 priv->setError(QIOOperation::Error::Aborted);
87 startOperationsUntilBarrier();
92 if (m_fileState == FileState::Closed)
97 m_opToCancel = kAllOperationIds;
98 for (
const auto &op : m_operations)
99 closeIoChannel(op.channel);
100 closeIoChannel(m_ioChannel);
102 if (!m_runningOps.isEmpty() || m_ioChannel)
103 m_cancellationCondition.wait(&m_mutex);
104 m_opToCancel = kInvalidOperationId;
108 for (
auto &opInfo : m_operations) {
109 releaseIoChannel(opInfo.channel);
110 auto *priv = QIOOperationPrivate::get(opInfo.operation);
111 priv->setError(QIOOperation::Error::Aborted);
113 m_operations.clear();
115 releaseIoChannel(m_ioChannel);
122 m_fileState = FileState::Closed;
127 if (m_fileState != FileState::Opened)
130 QFileSystemMetaData metaData;
131 if (QFileSystemEngine::fillMetaData(m_fd, metaData))
132 return metaData.size();
140 if (m_fileState == FileState::Closed) {
145 m_fileState = FileState::OpenPending;
148 return addOperation<QIOOperation>(QIOOperation::Type::Open, 0);
153 return addOperation<QIOOperation>(QIOOperation::Type::Flush, 0);
158 QByteArray array(maxSize, Qt::Uninitialized);
159 return addOperation<QIOReadOperation>(QIOOperation::Type::Read, offset, std::move(array));
164 QByteArray copy = data;
165 return write(offset, std::move(copy));
170 return addOperation<QIOWriteOperation>(QIOOperation::Type::Write, offset, std::move(data));
176 return addOperation<QIOVectoredReadOperation>(QIOOperation::Type::Read, offset,
177 QSpan<
const QSpan<std::byte>>{buffer});
183 return addOperation<QIOVectoredWriteOperation>(QIOOperation::Type::Write, offset,
184 QSpan<
const QSpan<
const std::byte>>{buffer});
198 return addOperation<QIOVectoredReadOperation>(QIOOperation::Type::Read, offset, buffers);
204 return addOperation<QIOVectoredWriteOperation>(QIOOperation::Type::Write, offset, buffers);
209 auto sharedThis =
this;
210 return dispatch_io_create(DISPATCH_IO_RANDOM, fd,
211 dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0),
214 QMutexLocker locker(&sharedThis->m_mutex);
215 sharedThis->m_cancellationCondition.wakeOne();
229 dispatch_io_create_with_io(DISPATCH_IO_RANDOM, m_ioChannel,
230 dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0),
234 QMutexLocker locker(&m_mutex);
235 m_runningOps.insert(opId);
243 dispatch_io_close(channel, DISPATCH_IO_STOP);
249 dispatch_release(channel);
257 auto onReturn = qScopeGuard([
this] {
258 startOperationsUntilBarrier();
261 auto it = std::find_if(m_operations.cbegin(), m_operations.cend(),
262 [opId = opResult.opId](
const auto &opInfo) {
263 return opInfo.opId == opId;
265 if (it == m_operations.cend())
267 qsizetype idx = std::distance(m_operations.cbegin(), it);
269 const OperationInfo info = m_operations.takeAt(idx);
270 closeIoChannel(info.channel);
271 releaseIoChannel(info.channel);
276 auto convertError = [](
int error, QIOOperation::Type type) {
278 return QIOOperation::Error::None;
279 }
else if (error == ECANCELED) {
280 return QIOOperation::Error::Aborted;
281 }
else if (error == EBADF) {
282 return QIOOperation::Error::FileNotOpen;
283 }
else if (error == EINVAL) {
285 case QIOOperation::Type::Read:
286 case QIOOperation::Type::Write:
287 return QIOOperation::Error::IncorrectOffset;
288 case QIOOperation::Type::Flush:
289 return QIOOperation::Error::Flush;
290 case QIOOperation::Type::Open:
291 return QIOOperation::Error::Open;
292 case QIOOperation::Type::Unknown:
293 Q_UNREACHABLE_RETURN(QIOOperation::Error::FileNotOpen);
297 case QIOOperation::Type::Read:
298 return QIOOperation::Error::Read;
299 case QIOOperation::Type::Write:
300 return QIOOperation::Error::Write;
301 case QIOOperation::Type::Flush:
302 return QIOOperation::Error::Flush;
303 case QIOOperation::Type::Open:
304 return QIOOperation::Error::Open;
305 case QIOOperation::Type::Unknown:
306 Q_UNREACHABLE_RETURN(QIOOperation::Error::FileNotOpen);
311 auto *priv = QIOOperationPrivate::get(info.operation);
312 switch (priv->type) {
313 case QIOOperation::Type::Read:
314 case QIOOperation::Type::Write:
315 priv->appendBytesProcessed(opResult.result);
318 if (priv->type == QIOOperation::Type::Read) {
319 auto dataStorage = priv->dataStorage.get();
320 auto processed = priv->processed;
321 if (dataStorage->containsByteArray()) {
322 QByteArray &array = dataStorage->getByteArray();
323 array.truncate(processed);
324 }
else if (dataStorage->containsReadSpans()) {
325 qint64 left = processed;
326 auto &readBuffers = dataStorage->getReadSpans();
327 for (
auto &s : readBuffers) {
328 const qint64 spanSize = qint64(s.size_bytes());
329 const qint64 newSize = (std::min)(left, spanSize);
330 if (newSize < spanSize)
331 s.chop(spanSize - newSize);
336 priv->operationComplete(convertError(opResult.error, priv->type));
338 case QIOOperation::Type::Flush: {
339 const QIOOperation::Error error = convertError(opResult.error, priv->type);
340 priv->operationComplete(error);
343 case QIOOperation::Type::Open: {
344 const QIOOperation::Error error = convertError(opResult.error, priv->type);
345 if (opResult.result >= 0 && error == QIOOperation::Error::None) {
346 m_fd = (
int)opResult.result;
347 m_ioChannel = createMainChannel(m_fd);
348 m_fileState = FileState::Opened;
350 m_fileState = FileState::Closed;
352 priv->operationComplete(error);
355 case QIOOperation::Type::Unknown:
363 const OperationResult res = { opId, 0LL, error };
364 QMetaObject::invokeMethod(q_ptr, [
this, res] {
365 handleOperationComplete(res);
366 }, Qt::QueuedConnection);
374 for (
auto &opInfo : m_operations) {
375 const bool isBarrier = isBarrierOperation(opInfo.operation->type());
376 const bool shouldExecute = (opInfo.state == OpState::Pending) && (!isBarrier || first);
379 opInfo.state = OpState::Running;
380 switch (opInfo.operation->type()) {
381 case QIOOperation::Type::Read:
384 case QIOOperation::Type::Write:
385 executeWrite(opInfo);
387 case QIOOperation::Type::Flush:
388 executeFlush(opInfo);
390 case QIOOperation::Type::Open:
393 case QIOOperation::Type::Unknown:
405 opInfo.channel = duplicateIoChannel(opInfo.opId);
406 if (!opInfo.channel) {
407 queueCompletion(opInfo.opId, EBADF);
410 auto priv = QIOOperationPrivate::get(opInfo.operation);
411 auto dataStorage = priv->dataStorage.get();
412 if (dataStorage->containsByteArray()) {
413 auto &array = dataStorage->getByteArray();
414 char *bytesPtr = array.data();
415 qint64 maxSize = array.size();
416 readOneBufferHelper(opInfo.opId, opInfo.channel, priv->offset,
420 Q_ASSERT(dataStorage->containsReadSpans());
421 auto &readBuffers = dataStorage->getReadSpans();
422 const auto totalBuffers = readBuffers.size();
423 if (totalBuffers == 0) {
424 queueCompletion(opInfo.opId, 0);
427 auto buf = readBuffers[0];
428 readOneBufferHelper(opInfo.opId, opInfo.channel, priv->offset,
429 buf.data(), buf.size(),
436 opInfo.channel = duplicateIoChannel(opInfo.opId);
437 if (!opInfo.channel) {
438 queueCompletion(opInfo.opId, EBADF);
441 auto priv = QIOOperationPrivate::get(opInfo.operation);
442 auto dataStorage = priv->dataStorage.get();
443 if (dataStorage->containsByteArray()) {
444 const auto &array = dataStorage->getByteArray();
445 const char *dataPtr = array.constData();
446 const qint64 dataSize = array.size();
448 dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0);
453 dispatch_data_t dataToWrite = dispatch_data_create(dataPtr, dataSize, queue, ^{});
455 writeHelper(opInfo.opId, opInfo.channel, priv->offset, dataToWrite, dataSize);
457 Q_ASSERT(dataStorage->containsWriteSpans());
459 const auto &writeBuffers = dataStorage->getWriteSpans();
460 const auto totalBuffers = writeBuffers.size();
461 if (totalBuffers == 0) {
462 queueCompletion(opInfo.opId, 0);
466 dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0);
468 dispatch_data_t dataToWrite = dispatch_data_empty;
469 qint64 totalSize = 0;
471 const std::byte *dataPtr = writeBuffers[idx].data();
472 const qint64 dataSize = writeBuffers[idx].size();
473 dispatch_data_t data = dispatch_data_create(dataPtr, dataSize, queue, ^{});
474 dataToWrite = dispatch_data_create_concat(dataToWrite, data);
476 totalSize += dataSize;
477 }
while (++idx < totalBuffers);
479 writeHelper(opInfo.opId, opInfo.channel, priv->offset, dataToWrite, totalSize);
485 opInfo.channel = duplicateIoChannel(opInfo.opId);
486 if (!opInfo.channel) {
487 queueCompletion(opInfo.opId, EBADF);
493 auto sharedThis =
this;
495 const OperationId opId = opInfo.opId;
496 dispatch_io_barrier(opInfo.channel, ^{
497 const int err = fsync(fd);
499 QMutexLocker locker(&sharedThis->m_mutex);
500 sharedThis->m_runningOps.remove(opId);
501 const auto cancelId = sharedThis->m_opToCancel;
502 if (cancelId == kAllOperationIds || cancelId == opId) {
503 if (cancelId == opId)
504 sharedThis->m_cancellationCondition.wakeOne();
506 auto context = sharedThis->q_ptr;
507 const OperationResult res = { opId, 0LL, err };
508 QMetaObject::invokeMethod(context, [sharedThis](
const OperationResult &r) {
509 sharedThis->handleOperationComplete(r);
510 }, Qt::QueuedConnection, res);
518 int oflags = QT_OPEN_RDONLY;
519#ifdef QT_LARGEFILE_SUPPORT
520 oflags |= QT_OPEN_LARGEFILE;
522 if ((mode & QIODevice::ReadWrite) == QIODevice::ReadWrite)
523 oflags = QT_OPEN_RDWR;
524 else if (mode & QIODevice::WriteOnly)
525 oflags = QT_OPEN_WRONLY;
526 if ((mode & QIODevice::WriteOnly)
527 && !(mode & QIODevice::ExistingOnly))
528 oflags |= QT_OPEN_CREAT;
529 if (mode & QIODevice::Truncate)
530 oflags |= QT_OPEN_TRUNC;
531 if (mode & QIODevice::Append)
532 oflags |= QT_OPEN_APPEND;
533 if (mode & QIODevice::NewOnly)
534 oflags |= QT_OPEN_EXCL;
540 if (m_fileState != FileState::OpenPending) {
541 queueCompletion(opInfo.opId, EINVAL);
545 const QByteArray nativeName = QFile::encodeName(QDir::toNativeSeparators(m_filePath));
547 int openFlags = openModeToOpenFlags(m_openMode);
548 openFlags |= O_NONBLOCK;
550 auto sharedThis =
this;
551 const OperationId opId = opInfo.opId;
556 m_runningOps.insert(opId);
559 dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0),
562 const int fd = ::open(nativeName.data(), openFlags);
566 QMutexLocker locker(&sharedThis->m_mutex);
567 sharedThis->m_runningOps.remove(opId);
568 const auto cancelId = sharedThis->m_opToCancel;
569 if (cancelId == kAllOperationIds || cancelId == opId) {
575 Q_ASSERT(sharedThis->m_runningOps.isEmpty());
576 sharedThis->m_cancellationCondition.wakeOne();
578 auto context = sharedThis->q_ptr;
579 const OperationResult res = { opId, qint64(fd), err };
580 QMetaObject::invokeMethod(context, [sharedThis](
const OperationResult &r) {
581 sharedThis->handleOperationComplete(r);
582 }, Qt::QueuedConnection, res);
593 auto it = std::find_if(m_operations.cbegin(), m_operations.cend(),
594 [opId](
const auto &opInfo) {
595 return opId == opInfo.opId;
597 if (it == m_operations.cend())
600 auto op = it->operation;
602 closeIoChannel(it->channel);
606 auto *priv = QIOOperationPrivate::get(op);
607 Q_ASSERT(priv->type == QIOOperation::Type::Read);
608 Q_ASSERT(priv->dataStorage->containsReadSpans());
610 auto &readBuffers = priv->dataStorage->getReadSpans();
611 Q_ASSERT(readBuffers.size() > bufferIdx);
613 qint64 newOffset = priv->offset;
614 for (qsizetype idx = 0; idx < bufferIdx; ++idx)
615 newOffset += readBuffers[idx].size();
617 std::byte *bytesPtr = readBuffers[bufferIdx].data();
618 qint64 maxSize = readBuffers[bufferIdx].size();
620 readOneBufferHelper(opId, it->channel, newOffset, bytesPtr, maxSize, bufferIdx,
621 readBuffers.size(), alreadyRead);
625 qint64 offset,
void *bytesPtr,
626 qint64 maxSize, qsizetype currentBufferIdx,
627 qsizetype totalBuffers, qint64 alreadyRead)
629 auto sharedThis =
this;
630 __block size_t readFromBuffer = 0;
631 dispatch_io_read(channel, offset, maxSize,
632 dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0),
633 ^(
bool done, dispatch_data_t data,
int error) {
637 dispatch_data_apply(data, ^(dispatch_data_t , size_t offset,
638 const void *buffer, size_t size) {
639 const char *startPtr =
640 reinterpret_cast<
const char *>(buffer) + offset;
643 std::memcpy((std::byte *)bytesPtr + readFromBuffer,
645 readFromBuffer += size;
650 QMutexLocker locker(&sharedThis->m_mutex);
651 const auto cancelId = sharedThis->m_opToCancel;
652 if (cancelId == kAllOperationIds || cancelId == opId) {
653 sharedThis->m_runningOps.remove(opId);
654 if (cancelId == opId)
655 sharedThis->m_cancellationCondition.wakeOne();
657 sharedThis->m_runningOps.remove(opId);
658 auto context = sharedThis->q_ptr;
661 qint64 totalRead = qint64(readFromBuffer) + alreadyRead;
662 qsizetype nextBufferIdx = currentBufferIdx + 1;
663 if (error || nextBufferIdx == totalBuffers
664 || qint64(readFromBuffer) != maxSize) {
665 const OperationResult res = { opId, totalRead, error };
666 QMetaObject::invokeMethod(context,
667 [sharedThis](
const OperationResult &r) {
668 sharedThis->handleOperationComplete(r);
669 }, Qt::QueuedConnection, res);
672 QMetaObject::invokeMethod(context,
673 [sharedThis, opId, nextBufferIdx, totalRead] {
674 sharedThis->readOneBuffer(opId, nextBufferIdx, totalRead);
675 }, Qt::QueuedConnection);
682 qint64 offset, dispatch_data_t dataToWrite,
685 auto sharedThis =
this;
686 dispatch_io_write(channel, offset, dataToWrite,
687 dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0),
688 ^(
bool done, dispatch_data_t data,
int error) {
692 QMutexLocker locker(&sharedThis->m_mutex);
693 const auto cancelId = sharedThis->m_opToCancel;
694 if (cancelId == kAllOperationIds || cancelId == opId) {
696 sharedThis->m_runningOps.remove(opId);
697 if (cancelId == opId)
698 sharedThis->m_cancellationCondition.wakeOne();
700 sharedThis->m_runningOps.remove(opId);
704 const size_t toBeWritten =
705 (error == 0) ? 0 : dispatch_data_get_size(data);
706 const size_t written = dataSize - toBeWritten;
707 [dataToWrite release];
709 auto context = sharedThis->q_ptr;
710 const OperationResult res = { opId, qint64(written), error };
711 QMetaObject::invokeMethod(context,
712 [sharedThis](
const OperationResult &r) {
713 sharedThis->handleOperationComplete(r);
714 }, Qt::QueuedConnection, res);
722 static OperationId opId = kInvalidOperationId;
723 if (++opId == kAllOperationIds)
724 opId = kInvalidOperationId + 1;
QIOOperationPrivate(QtPrivate::QIOOperationDataStorage *storage)
QIOOperation * open(const QString &path, QIODeviceBase::OpenMode mode)
~QRandomAccessAsyncFilePrivate() override
QIOReadOperation * read(qint64 offset, qint64 maxSize)
QIOVectoredWriteOperation * writeFrom(qint64 offset, QSpan< const std::byte > buffer)
QIOWriteOperation * write(qint64 offset, QByteArray &&data)
QIOWriteOperation * write(qint64 offset, const QByteArray &data)
QIOVectoredReadOperation * readInto(qint64 offset, QSpan< std::byte > buffer)
Combined button and popup list for selecting options.
static bool isBarrierOperation(QIOOperation::Type type)
static int openModeToOpenFlags(QIODevice::OpenMode mode)