QFile: use unbuffered mode for the file-copy data pumps

There's no need to have the QIODevice layer buffer the data and thus
allocate memory. In the case of QFile::copy(), it also improves the
error message output.

Previously:
"Cannot create /tmp/tmp/f for output: No space left on device"

Now:
"Failure to write block: No space left on device"

Change-Id: I537a2a79ead9d1d9ac2efffdd650702a4424bac6
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
(cherry picked from commit 8a720c162d500369810674930884e36cf2494d88)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Thiago Macieira 2025-01-21 10:52:43 -08:00 committed by Qt Cherry-pick Bot
parent 2a734e1971
commit 266653fee4

View File

@ -667,8 +667,8 @@ QFile::rename(const QString &newName)
}
QFile out(newName);
if (open(QIODevice::ReadOnly)) {
if (out.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
if (open(QIODevice::ReadOnly | QIODevice::Unbuffered)) {
if (out.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Unbuffered)) {
bool error = false;
char block[4096];
qint64 bytes;
@ -812,7 +812,7 @@ QFile::copy(const QString &newName)
return true;
} else {
bool error = false;
if (!open(QFile::ReadOnly)) {
if (!open(QFile::ReadOnly | QFile::Unbuffered)) {
error = true;
d->setError(QFile::CopyError, tr("Cannot open %1 for input").arg(d->fileName));
} else {
@ -837,6 +837,7 @@ QFile::copy(const QString &newName)
if (!d->engine()->cloneTo(out.d_func()->engine())) {
char block[4096];
qint64 totalRead = 0;
out.setOpenMode(ReadWrite | Unbuffered);
while (!atEnd()) {
qint64 in = read(block, sizeof(block));
if (in <= 0)