summaryrefslogtreecommitdiffstats
path: root/src/corelib/io/qiooperation.cpp
blob: b9cdb63e55460914e49514f833c2118e15e22a3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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 "qiooperation_p.h"
#include "qiooperation_p_p.h"

#include <QtCore/private/qobject_p.h>
#include <QtCore/private/qrandomaccessasyncfile_p_p.h>

QT_BEGIN_NAMESPACE

QIOOperationPrivate::QIOOperationPrivate(QtPrivate::QIOOperationDataStorage *storage)
    : dataStorage(storage)
{
    Q_ASSERT(storage);
}

QIOOperationPrivate::~QIOOperationPrivate()
{
}

void QIOOperationPrivate::appendBytesProcessed(qint64 num)
{
    processed += num;
}

void QIOOperationPrivate::operationComplete(QIOOperation::Error err)
{
    Q_Q(QIOOperation);

    error = err;
    state = State::Finished;
    if (err != QIOOperation::Error::None)
        Q_EMIT q->errorOccurred(err);
    Q_EMIT q->finished();
}

void QIOOperationPrivate::setError(QIOOperation::Error err)
{
    Q_Q(QIOOperation);
    error = err;
    if (err != QIOOperation::Error::None) {
        state = State::Finished;
        Q_EMIT q->errorOccurred(err);
        Q_EMIT q->finished();
    }
}

QIOOperation::~QIOOperation()
{
    ensureCompleteOrCanceled();
}

QIOOperation::Type QIOOperation::type() const
{
    Q_D(const QIOOperation);
    return d->type;
}

QIOOperation::Error QIOOperation::error() const
{
    Q_D(const QIOOperation);
    return d->error;
}

bool QIOOperation::isFinished() const
{
    Q_D(const QIOOperation);
    return d->state == QIOOperationPrivate::State::Finished;
}

QIOOperation::QIOOperation(QIOOperationPrivate &dd, QObject *parent)
    : QObject(dd, parent)
{
    if (auto file = qobject_cast<QRandomAccessAsyncFile*>(parent))
        d_func()->file = file;
}

void QIOOperation::ensureCompleteOrCanceled()
{
    // Block until the operation is either complete or canceled.
    // Otherwise the write/read might use removed data
    Q_D(QIOOperation);
    if (d->state != QIOOperationPrivate::State::Finished) {
        if (d->file) {
            auto *filePriv = QRandomAccessAsyncFilePrivate::get(d->file);
            filePriv->cancelAndWait(this);
        }
    }
}

QIOReadWriteOperationBase::~QIOReadWriteOperationBase()
        = default;

qint64 QIOReadWriteOperationBase::offset() const
{
    Q_D(const QIOOperation);
    return d->offset;
}

qint64 QIOReadWriteOperationBase::numBytesProcessed() const
{
    if (!isFinished())
        return -1;
    Q_D(const QIOOperation);
    return d->processed;
}

QIOReadWriteOperationBase::QIOReadWriteOperationBase(QIOOperationPrivate &dd, QObject *parent)
    : QIOOperation(dd, parent)
{
}

QIOReadOperation::~QIOReadOperation() = default;

QByteArray QIOReadOperation::data() const
{
    if (!isFinished())
        return {};
    Q_D(const QIOOperation);
    return d->dataStorage->getValue<QByteArray>();
}

QIOReadOperation::QIOReadOperation(QIOOperationPrivate &dd, QObject *parent)
    : QIOReadWriteOperationBase(dd, parent)
{
    Q_ASSERT(dd.type == QIOOperation::Type::Read);
    Q_ASSERT(dd.dataStorage->containsByteArray());
}

QIOWriteOperation::~QIOWriteOperation() = default;

QByteArray QIOWriteOperation::data() const
{
    if (!isFinished())
        return {};
    Q_D(const QIOOperation);
    return d->dataStorage->getValue<QByteArray>();
}

QIOWriteOperation::QIOWriteOperation(QIOOperationPrivate &dd, QObject *parent)
    : QIOReadWriteOperationBase(dd, parent)
{
    Q_ASSERT(dd.type == QIOOperation::Type::Write);
    Q_ASSERT(dd.dataStorage->containsByteArray());
}

QIOVectoredReadOperation::~QIOVectoredReadOperation() = default;

QSpan<const QSpan<std::byte>> QIOVectoredReadOperation::data() const
{
    if (!isFinished())
        return {};
    Q_D(const QIOOperation);
    return d->dataStorage->getValue<QSpan<const QSpan<std::byte>>>();
}

QIOVectoredReadOperation::QIOVectoredReadOperation(QIOOperationPrivate &dd, QObject *parent)
    : QIOReadWriteOperationBase(dd, parent)
{
    Q_ASSERT(dd.type == QIOOperation::Type::Read);
    Q_ASSERT(dd.dataStorage->containsReadSpans());
}

QIOVectoredWriteOperation::~QIOVectoredWriteOperation() = default;

QSpan<const QSpan<const std::byte>> QIOVectoredWriteOperation::data() const
{
    if (!isFinished())
        return {};
    Q_D(const QIOOperation);
    return d->dataStorage->getValue<QSpan<const QSpan<const std::byte>>>();
}

QIOVectoredWriteOperation::QIOVectoredWriteOperation(QIOOperationPrivate &dd, QObject *parent)
    : QIOReadWriteOperationBase(dd, parent)
{
    Q_ASSERT(dd.type == QIOOperation::Type::Write);
    Q_ASSERT(dd.dataStorage->containsWriteSpans());
}

QT_END_NAMESPACE