From 266653fee440e2780bbe5db571c94dd1ab22220d Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 21 Jan 2025 10:52:43 -0800 Subject: [PATCH] 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 (cherry picked from commit 8a720c162d500369810674930884e36cf2494d88) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/io/qfile.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/corelib/io/qfile.cpp b/src/corelib/io/qfile.cpp index 7e7c16c4660..3a15b4ac7f6 100644 --- a/src/corelib/io/qfile.cpp +++ b/src/corelib/io/qfile.cpp @@ -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)