Browse Source

Fix wrong behavior when reading text

Also add another 'file read error' status.

Closes #19254.
PR #19262.
adaptive-webui-19844
Chocobo1 1 year ago committed by GitHub
parent
commit
80791e328d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      .gitattributes
  2. 8
      .pre-commit-config.yaml
  3. 35
      src/base/utils/io.cpp
  4. 1
      src/base/utils/io.h
  5. 1
      src/webui/webapplication.cpp
  6. 2
      test/testdata/crlf.txt
  7. 7
      test/testutilsio.cpp

2
.gitattributes vendored

@ -5,3 +5,5 @@ core.eol=lf @@ -5,3 +5,5 @@ core.eol=lf
*.png binary
*.qm binary
*.zip binary
test/testdata/crlf.txt text eol=crlf

8
.pre-commit-config.yaml

@ -33,19 +33,19 @@ repos: @@ -33,19 +33,19 @@ repos:
args: ["--fix=lf"]
exclude: |
(?x)^(
compile_commands.json |
src/webui/www/private/css/lib/.* |
src/webui/www/private/scripts/lib/.*
src/webui/www/private/scripts/lib/.* |
test/testdata/crlf.txt
)$
- id: end-of-file-fixer
name: Check trailing newlines
exclude: |
(?x)^(
compile_commands.json |
configure |
src/webui/www/private/css/lib/.* |
src/webui/www/private/scripts/lib/.*
src/webui/www/private/scripts/lib/.* |
test/testdata/crlf.txt
)$
exclude_types:
- svg

35
src/base/utils/io.cpp

@ -89,16 +89,37 @@ nonstd::expected<QByteArray, Utils::IO::ReadError> Utils::IO::readFile(const Pat @@ -89,16 +89,37 @@ nonstd::expected<QByteArray, Utils::IO::ReadError> Utils::IO::readFile(const Pat
return nonstd::make_unexpected(ReadError {ReadError::ExceedSize, message});
}
// Do not use `QIODevice::readAll()` it won't stop when reading `/dev/zero`
const QByteArray data = file.read(fileSize);
if (const qint64 dataSize = data.size(); dataSize != fileSize)
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
QByteArray ret {fileSize, Qt::Uninitialized};
#else
QByteArray ret {static_cast<int>(fileSize), Qt::Uninitialized};
#endif
const qint64 actualSize = file.read(ret.data(), fileSize);
if (actualSize < 0)
{
const QString message = QCoreApplication::translate("Utils::IO", "File read error. File: \"%1\". Error: \"%2\"")
.arg(file.fileName(), file.errorString());
return nonstd::make_unexpected(ReadError {ReadError::Failed, message});
}
if (actualSize < fileSize)
{
const QString message = QCoreApplication::translate("Utils::IO", "Read size mismatch. File: \"%1\". Expected: %2. Actual: %3")
.arg(file.fileName(), QString::number(fileSize), QString::number(dataSize));
return nonstd::make_unexpected(ReadError {ReadError::SizeMismatch, message});
// `QIODevice::Text` will convert CRLF to LF on-the-fly and affects return value
// of `qint64 QIODevice::read(char *data, qint64 maxSize)`
if (additionalMode.testFlag(QIODevice::Text))
{
ret.truncate(actualSize);
}
else
{
const QString message = QCoreApplication::translate("Utils::IO", "Read size mismatch. File: \"%1\". Expected: %2. Actual: %3")
.arg(file.fileName(), QString::number(fileSize), QString::number(actualSize));
return nonstd::make_unexpected(ReadError {ReadError::SizeMismatch, message});
}
}
return data;
return ret;
}
nonstd::expected<void, QString> Utils::IO::saveToFile(const Path &path, const QByteArray &data)

1
src/base/utils/io.h

@ -89,6 +89,7 @@ namespace Utils::IO @@ -89,6 +89,7 @@ namespace Utils::IO
{
NotExist,
ExceedSize,
Failed, // `read()` operation failed
SizeMismatch
};

1
src/webui/webapplication.cpp

@ -516,6 +516,7 @@ void WebApplication::sendFile(const Path &path) @@ -516,6 +516,7 @@ void WebApplication::sendFile(const Path &path)
LogMsg(message, Log::WARNING);
throw InternalServerErrorHTTPError(readResult.error().message);
case Utils::IO::ReadError::Failed:
case Utils::IO::ReadError::SizeMismatch:
LogMsg(message, Log::WARNING);
throw InternalServerErrorHTTPError(readResult.error().message);

2
test/testdata/crlf.txt vendored

@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@

7
test/testutilsio.cpp

@ -77,6 +77,13 @@ private slots: @@ -77,6 +77,13 @@ private slots:
QCOMPARE(readResult.value(), size10Data);
}
{
const Path crlfFile = testFolder / Path(u"crlf.txt"_s);
const auto readResult = Utils::IO::readFile(crlfFile, -1, QIODevice::Text);
QCOMPARE(readResult.has_value(), true);
QCOMPARE(readResult.value(), "\n\n");
}
{
const Path nonExistFile = testFolder / Path(u".non_existent_file_1234"_s);
const auto readResult = Utils::IO::readFile(nonExistFile, 1);

Loading…
Cancel
Save