forked from qt-creator/qt-creator
let QMakeVfs::readFile() report ENOFILE explicitly
when the QFile object is already constructed, querying whether the file exists is actually cheap, so do it right away instead of later on demand. that makes the calling code a bit cleaner. fwiw, that we need to explicitly query the file's existence at all is a result of QFile's completely useless error "codes" (which merely say which function failed, as if the caller would not know). Change-Id: Ifec39d05b1713d8128046f679287e510f10e45dc Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
@@ -242,8 +242,9 @@ bool QMakeParser::read(ProFile *pro, ParseFlags flags)
|
|||||||
{
|
{
|
||||||
QString content;
|
QString content;
|
||||||
QString errStr;
|
QString errStr;
|
||||||
if (!m_vfs->readFile(pro->fileName(), &content, &errStr)) {
|
QMakeVfs::ReadResult result = m_vfs->readFile(pro->fileName(), &content, &errStr);
|
||||||
if (m_handler && ((flags & ParseReportMissing) || m_vfs->exists(pro->fileName())))
|
if (result != QMakeVfs::ReadOk) {
|
||||||
|
if (m_handler && ((flags & ParseReportMissing) || result != QMakeVfs::ReadNotFound))
|
||||||
m_handler->message(QMakeParserHandler::ParserIoError,
|
m_handler->message(QMakeParserHandler::ParserIoError,
|
||||||
fL1S("Cannot read %1: %2").arg(pro->fileName(), errStr));
|
fL1S("Cannot read %1: %2").arg(pro->fileName(), errStr));
|
||||||
return false;
|
return false;
|
||||||
|
@@ -114,7 +114,7 @@ bool QMakeVfs::readVirtualFile(const QString &fn, QString *contents)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool QMakeVfs::readFile(const QString &fn, QString *contents, QString *errStr)
|
QMakeVfs::ReadResult QMakeVfs::readFile(const QString &fn, QString *contents, QString *errStr)
|
||||||
{
|
{
|
||||||
#ifndef PROEVALUATOR_FULL
|
#ifndef PROEVALUATOR_FULL
|
||||||
# ifdef PROEVALUATOR_THREAD_SAFE
|
# ifdef PROEVALUATOR_THREAD_SAFE
|
||||||
@@ -124,25 +124,26 @@ bool QMakeVfs::readFile(const QString &fn, QString *contents, QString *errStr)
|
|||||||
if (it != m_files.constEnd()) {
|
if (it != m_files.constEnd()) {
|
||||||
if (it->constData() == m_magicMissing.constData()) {
|
if (it->constData() == m_magicMissing.constData()) {
|
||||||
*errStr = fL1S("No such file or directory");
|
*errStr = fL1S("No such file or directory");
|
||||||
return false;
|
return ReadNotFound;
|
||||||
}
|
}
|
||||||
if (it->constData() != m_magicExisting.constData()) {
|
if (it->constData() != m_magicExisting.constData()) {
|
||||||
*contents = *it;
|
*contents = *it;
|
||||||
return true;
|
return ReadOk;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
QFile file(fn);
|
QFile file(fn);
|
||||||
if (!file.open(QIODevice::ReadOnly)) {
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
|
if (!file.exists()) {
|
||||||
#ifndef PROEVALUATOR_FULL
|
#ifndef PROEVALUATOR_FULL
|
||||||
if (!IoUtils::exists(fn)) {
|
|
||||||
m_files[fn] = m_magicMissing;
|
m_files[fn] = m_magicMissing;
|
||||||
*errStr = fL1S("No such file or directory");
|
|
||||||
} else
|
|
||||||
#endif
|
#endif
|
||||||
|
*errStr = fL1S("No such file or directory");
|
||||||
|
return ReadNotFound;
|
||||||
|
}
|
||||||
*errStr = file.errorString();
|
*errStr = file.errorString();
|
||||||
return false;
|
return ReadOtherError;
|
||||||
}
|
}
|
||||||
#ifndef PROEVALUATOR_FULL
|
#ifndef PROEVALUATOR_FULL
|
||||||
m_files[fn] = m_magicExisting;
|
m_files[fn] = m_magicExisting;
|
||||||
@@ -152,10 +153,10 @@ bool QMakeVfs::readFile(const QString &fn, QString *contents, QString *errStr)
|
|||||||
if (bcont.startsWith("\xef\xbb\xbf")) {
|
if (bcont.startsWith("\xef\xbb\xbf")) {
|
||||||
// UTF-8 BOM will cause subtle errors
|
// UTF-8 BOM will cause subtle errors
|
||||||
*errStr = fL1S("Unexpected UTF-8 BOM");
|
*errStr = fL1S("Unexpected UTF-8 BOM");
|
||||||
return false;
|
return ReadOtherError;
|
||||||
}
|
}
|
||||||
*contents = QString::fromLocal8Bit(bcont);
|
*contents = QString::fromLocal8Bit(bcont);
|
||||||
return true;
|
return ReadOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QMakeVfs::exists(const QString &fn)
|
bool QMakeVfs::exists(const QString &fn)
|
||||||
|
@@ -41,10 +41,16 @@ QT_BEGIN_NAMESPACE
|
|||||||
class QMAKE_EXPORT QMakeVfs
|
class QMAKE_EXPORT QMakeVfs
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
enum ReadResult {
|
||||||
|
ReadOk,
|
||||||
|
ReadNotFound,
|
||||||
|
ReadOtherError
|
||||||
|
};
|
||||||
|
|
||||||
QMakeVfs();
|
QMakeVfs();
|
||||||
|
|
||||||
bool writeFile(const QString &fn, QIODevice::OpenMode mode, bool exe, const QString &contents, QString *errStr);
|
bool writeFile(const QString &fn, QIODevice::OpenMode mode, bool exe, const QString &contents, QString *errStr);
|
||||||
bool readFile(const QString &fn, QString *contents, QString *errStr);
|
ReadResult readFile(const QString &fn, QString *contents, QString *errStr);
|
||||||
bool exists(const QString &fn);
|
bool exists(const QString &fn);
|
||||||
|
|
||||||
#ifndef PROEVALUATOR_FULL
|
#ifndef PROEVALUATOR_FULL
|
||||||
|
Reference in New Issue
Block a user