qmake: require a drive in a DOS path for it to be absolute

For Q_OS_WIN, a path is only truly absolute if it includes a drive
letter; merely starting with a slash is not enough.  (We can't support
UNC paths, so don't even try: qmake runs various commands in the
source directory using CMD.exe, which doesn't support UNC as PWD.)
This requires, when resolving a path relative to a root, transcribing
the root's drive to such not-quite-absolute paths.

Changed QMakeGlobals, $$absolute_path() and $$relative_path() to now
use IoUtils::resolvePath() rather than delegating to QDir's absolute
path method, since that doesn't correctly recognize the need for a
drive letter (and qmake did run into problems with some paths, from
splitPathList and a failing test, as a result).

Moved existing ioUtils tests for handling of relative / absolute paths
out into separate functions and expanded significantly.  Fixed some
existing tests to use an absolute path where one is needed; added two
tests involving driveless (but rooted) paths; and fixed the test init
to set a value for QT_HOST_DATA/src property (the lack of which lead
to an assertion failure with this fix).

Task-number: QTBUG-50839
Change-Id: I2bfc13c1bfbe1ae09997274622ea55cb3de31b43
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@qt.io>
This commit is contained in:
Edward Welbourne 2017-07-28 13:25:27 +02:00 committed by Oswald Buddenhagen
parent 3149d0fb13
commit e86f3c0188
6 changed files with 157 additions and 42 deletions

View File

@ -66,21 +66,22 @@ IoUtils::FileType IoUtils::fileType(const QString &fileName)
bool IoUtils::isRelativePath(const QString &path) bool IoUtils::isRelativePath(const QString &path)
{ {
if (path.startsWith(QLatin1Char('/')))
return false;
#ifdef QMAKE_BUILTIN_PRFS #ifdef QMAKE_BUILTIN_PRFS
if (path.startsWith(QLatin1String(":/"))) if (path.startsWith(QLatin1String(":/")))
return false; return false;
#endif #endif
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
if (path.startsWith(QLatin1Char('\\'))) // Unlike QFileInfo, this considers only paths with both a drive prefix and
return false; // a subsequent (back-)slash absolute:
// Unlike QFileInfo, this won't accept a relative path with a drive letter.
// Such paths result in a royal mess anyway ...
if (path.length() >= 3 && path.at(1) == QLatin1Char(':') && path.at(0).isLetter() if (path.length() >= 3 && path.at(1) == QLatin1Char(':') && path.at(0).isLetter()
&& (path.at(2) == QLatin1Char('/') || path.at(2) == QLatin1Char('\\'))) && (path.at(2) == QLatin1Char('/') || path.at(2) == QLatin1Char('\\'))) {
return false; return false;
#endif }
// (... unless, of course, they're UNC, which qmake fails on anyway)
#else
if (path.startsWith(QLatin1Char('/')))
return false;
#endif // Q_OS_WIN
return true; return true;
} }
@ -100,6 +101,12 @@ QString IoUtils::resolvePath(const QString &baseDir, const QString &fileName)
return QString(); return QString();
if (isAbsolutePath(fileName)) if (isAbsolutePath(fileName))
return QDir::cleanPath(fileName); return QDir::cleanPath(fileName);
#ifdef Q_OS_WIN // Add drive to otherwise-absolute path:
if (fileName.at(0).unicode() == '/' || fileName.at(0).unicode() == '\\') {
Q_ASSERT(isAbsolutePath(baseDir));
return QDir::cleanPath(baseDir.left(2) + fileName);
}
#endif // Q_OS_WIN
return QDir::cleanPath(baseDir + QLatin1Char('/') + fileName); return QDir::cleanPath(baseDir + QLatin1Char('/') + fileName);
} }

View File

@ -1173,9 +1173,9 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinExpand(
if (args.count() > 2) { if (args.count() > 2) {
evalError(fL1S("absolute_path(path[, base]) requires one or two arguments.")); evalError(fL1S("absolute_path(path[, base]) requires one or two arguments."));
} else { } else {
QString rstr = QDir::cleanPath( QString arg = args.at(0).toQString(m_tmp1);
QDir(args.count() > 1 ? args.at(1).toQString(m_tmp2) : currentDirectory()) QString baseDir = args.count() > 1 ? args.at(1).toQString(m_tmp2) : currentDirectory();
.absoluteFilePath(args.at(0).toQString(m_tmp1))); QString rstr = arg.isEmpty() ? baseDir : IoUtils::resolvePath(baseDir, arg);
ret << (rstr.isSharedWith(m_tmp1) ret << (rstr.isSharedWith(m_tmp1)
? args.at(0) ? args.at(0)
: args.count() > 1 && rstr.isSharedWith(m_tmp2) : args.count() > 1 && rstr.isSharedWith(m_tmp2)
@ -1187,9 +1187,10 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateBuiltinExpand(
if (args.count() > 2) { if (args.count() > 2) {
evalError(fL1S("relative_path(path[, base]) requires one or two arguments.")); evalError(fL1S("relative_path(path[, base]) requires one or two arguments."));
} else { } else {
QDir baseDir(args.count() > 1 ? args.at(1).toQString(m_tmp2) : currentDirectory()); QString arg = args.at(0).toQString(m_tmp1);
QString rstr = baseDir.relativeFilePath(baseDir.absoluteFilePath( QString baseDir = args.count() > 1 ? args.at(1).toQString(m_tmp2) : currentDirectory();
args.at(0).toQString(m_tmp1))); QString absArg = arg.isEmpty() ? baseDir : IoUtils::resolvePath(baseDir, arg);
QString rstr = QDir(baseDir).relativeFilePath(absArg);
ret << (rstr.isSharedWith(m_tmp1) ? args.at(0) : ProString(rstr).setSource(args.at(0))); ret << (rstr.isSharedWith(m_tmp1) ? args.at(0) : ProString(rstr).setSource(args.at(0)));
} }
break; break;

View File

@ -68,6 +68,7 @@
#endif #endif
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
using namespace QMakeInternal; // for IoUtils
#define fL1S(s) QString::fromLatin1(s) #define fL1S(s) QString::fromLatin1(s)
@ -96,9 +97,9 @@ QString QMakeGlobals::cleanSpec(QMakeCmdLineParserState &state, const QString &s
{ {
QString ret = QDir::cleanPath(spec); QString ret = QDir::cleanPath(spec);
if (ret.contains(QLatin1Char('/'))) { if (ret.contains(QLatin1Char('/'))) {
QString absRet = QDir(state.pwd).absoluteFilePath(ret); QString absRet = IoUtils::resolvePath(state.pwd, ret);
if (QFile::exists(absRet)) if (QFile::exists(absRet))
ret = QDir::cleanPath(absRet); ret = absRet;
} }
return ret; return ret;
} }
@ -126,10 +127,10 @@ QMakeGlobals::ArgumentReturn QMakeGlobals::addCommandLineArguments(
user_template_prefix = arg; user_template_prefix = arg;
break; break;
case ArgCache: case ArgCache:
cachefile = args[*pos] = QDir::cleanPath(QDir(state.pwd).absoluteFilePath(arg)); cachefile = args[*pos] = IoUtils::resolvePath(state.pwd, arg);
break; break;
case ArgQtConf: case ArgQtConf:
qtconf = args[*pos] = QDir::cleanPath(QDir(state.pwd).absoluteFilePath(arg)); qtconf = args[*pos] = IoUtils::resolvePath(state.pwd, arg);
break; break;
default: default:
if (arg.startsWith(QLatin1Char('-'))) { if (arg.startsWith(QLatin1Char('-'))) {
@ -259,11 +260,11 @@ QStringList QMakeGlobals::splitPathList(const QString &val) const
{ {
QStringList ret; QStringList ret;
if (!val.isEmpty()) { if (!val.isEmpty()) {
QDir bdir; QString cwd(QDir::currentPath());
const QStringList vals = val.split(dirlist_sep); const QStringList vals = val.split(dirlist_sep);
ret.reserve(vals.length()); ret.reserve(vals.length());
for (const QString &it : vals) for (const QString &it : vals)
ret << QDir::cleanPath(bdir.absoluteFilePath(it)); ret << IoUtils::resolvePath(cwd, it);
} }
return ret; return ret;
} }
@ -318,7 +319,7 @@ bool QMakeGlobals::initProperties()
return false; return false;
data = proc.readAll(); data = proc.readAll();
#else #else
if (FILE *proc = QT_POPEN(QString(QMakeInternal::IoUtils::shellQuote(qmake_abslocation) if (FILE *proc = QT_POPEN(QString(IoUtils::shellQuote(qmake_abslocation)
+ QLatin1String(" -query")).toLocal8Bit(), QT_POPEN_READ)) { + QLatin1String(" -query")).toLocal8Bit(), QT_POPEN_READ)) {
char buff[1024]; char buff[1024];
while (!feof(proc)) while (!feof(proc))

View File

@ -34,6 +34,12 @@
#include <qmakeglobals.h> #include <qmakeglobals.h>
#include <qmakeevaluator.h> #include <qmakeevaluator.h>
#ifdef Q_OS_WIN
# define EVAL_DRIVE "R:"
#else
# define EVAL_DRIVE
#endif
void tst_qmakelib::addAssignments() void tst_qmakelib::addAssignments()
{ {
QTest::newRow("assignment") QTest::newRow("assignment")
@ -1599,20 +1605,28 @@ void tst_qmakelib::addReplaceFunctions(const QString &qindir)
<< true; << true;
QTest::newRow("$$absolute_path(): file & path") QTest::newRow("$$absolute_path(): file & path")
<< "VAR = $$absolute_path(dir/file.ext, /root/sub)" << "VAR = $$absolute_path(dir/file.ext, " EVAL_DRIVE "/root/sub)"
<< "VAR = /root/sub/dir/file.ext" << "VAR = " EVAL_DRIVE "/root/sub/dir/file.ext"
<< "" << ""
<< true; << true;
#ifdef Q_OS_WIN
QTest::newRow("$$absolute_path(): driveless file & absolute path")
<< "VAR = $$absolute_path(/root/sub/dir/file.ext, " EVAL_DRIVE "/other)"
<< "VAR = " EVAL_DRIVE "/root/sub/dir/file.ext"
<< ""
<< true;
#endif
QTest::newRow("$$absolute_path(): absolute file & path") QTest::newRow("$$absolute_path(): absolute file & path")
<< "VAR = $$absolute_path(/root/sub/dir/file.ext, /other)" << "VAR = $$absolute_path(" EVAL_DRIVE "/root/sub/dir/file.ext, " EVAL_DRIVE "/other)"
<< "VAR = /root/sub/dir/file.ext" << "VAR = " EVAL_DRIVE "/root/sub/dir/file.ext"
<< "" << ""
<< true; << true;
QTest::newRow("$$absolute_path(): empty file & path") QTest::newRow("$$absolute_path(): empty file & path")
<< "VAR = $$absolute_path('', /root/sub)" << "VAR = $$absolute_path('', " EVAL_DRIVE "/root/sub)"
<< "VAR = /root/sub" << "VAR = " EVAL_DRIVE "/root/sub"
<< "" << ""
<< true; << true;
@ -1634,14 +1648,22 @@ void tst_qmakelib::addReplaceFunctions(const QString &qindir)
<< "" << ""
<< true; << true;
#ifdef Q_OS_WIN
QTest::newRow("$$relative_path(): driveless file & absolute path")
<< "VAR = $$relative_path(/root/sub/dir/file.ext, " EVAL_DRIVE "/root/sub)"
<< "VAR = dir/file.ext"
<< ""
<< true;
#endif
QTest::newRow("$$relative_path(): absolute file & path") QTest::newRow("$$relative_path(): absolute file & path")
<< "VAR = $$relative_path(/root/sub/dir/file.ext, /root/sub)" << "VAR = $$relative_path(" EVAL_DRIVE "/root/sub/dir/file.ext, " EVAL_DRIVE "/root/sub)"
<< "VAR = dir/file.ext" << "VAR = dir/file.ext"
<< "" << ""
<< true; << true;
QTest::newRow("$$relative_path(): empty file & path") QTest::newRow("$$relative_path(): empty file & path")
<< "VAR = $$relative_path('', /root/sub)" << "VAR = $$relative_path('', " EVAL_DRIVE "/root/sub)"
<< "VAR = ." << "VAR = ."
<< "" << ""
<< true; << true;
@ -2555,20 +2577,20 @@ void tst_qmakelib::addTestFunctions(const QString &qindir)
<< true; << true;
QTest::newRow("touch(): missing target") QTest::newRow("touch(): missing target")
<< "touch(/does/not/exist, files/other.txt): OK = 1" << "touch(" EVAL_DRIVE "/does/not/exist, files/other.txt): OK = 1"
<< "OK = UNDEF" << "OK = UNDEF"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
<< "##:1: Cannot open /does/not/exist: The system cannot find the path specified." << "##:1: Cannot open " EVAL_DRIVE "/does/not/exist: The system cannot find the path specified."
#else #else
<< "##:1: Cannot touch /does/not/exist: No such file or directory." << "##:1: Cannot touch /does/not/exist: No such file or directory."
#endif #endif
<< true; << true;
QTest::newRow("touch(): missing reference") QTest::newRow("touch(): missing reference")
<< "touch(" + wpath + ", /does/not/exist): OK = 1" << "touch(" + wpath + ", " EVAL_DRIVE "/does/not/exist): OK = 1"
<< "OK = UNDEF" << "OK = UNDEF"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
<< "##:1: Cannot open reference file /does/not/exist: The system cannot find the path specified." << "##:1: Cannot open reference file " EVAL_DRIVE "/does/not/exist: The system cannot find the path specified."
#else #else
<< "##:1: Cannot stat() reference file /does/not/exist: No such file or directory." << "##:1: Cannot stat() reference file /does/not/exist: No such file or directory."
#endif #endif

View File

@ -42,6 +42,7 @@ void tst_qmakelib::initTestCase()
#endif #endif
m_prop.insert(ProKey("P1"), ProString("prop val")); m_prop.insert(ProKey("P1"), ProString("prop val"));
m_prop.insert(ProKey("QT_HOST_DATA/get"), ProString(m_indir)); m_prop.insert(ProKey("QT_HOST_DATA/get"), ProString(m_indir));
m_prop.insert(ProKey("QT_HOST_DATA/src"), ProString(m_indir));
QVERIFY(!m_indir.isEmpty()); QVERIFY(!m_indir.isEmpty());
QVERIFY(QDir(m_outdir).removeRecursively()); QVERIFY(QDir(m_outdir).removeRecursively());
@ -224,21 +225,99 @@ void tst_qmakelib::pathUtils()
QVERIFY(IoUtils::isRelativePath(fn0)); QVERIFY(IoUtils::isRelativePath(fn0));
QString fn1 = "/a/unix/file/path"; QString fn1 = "/a/unix/file/path";
QVERIFY(IoUtils::isAbsolutePath(fn1));
QCOMPARE(IoUtils::pathName(fn1).toString(), QStringLiteral("/a/unix/file/")); QCOMPARE(IoUtils::pathName(fn1).toString(), QStringLiteral("/a/unix/file/"));
QCOMPARE(IoUtils::fileName(fn1).toString(), QStringLiteral("path")); QCOMPARE(IoUtils::fileName(fn1).toString(), QStringLiteral("path"));
}
#ifdef Q_OS_WIN void tst_qmakelib::ioUtilRelativity_data()
QString fn0a = "c:file/path"; {
QVERIFY(IoUtils::isRelativePath(fn0a)); QTest::addColumn<QString>("path");
QTest::addColumn<bool>("relative");
QString fn1a = "c:\\file\\path"; static const struct {
QVERIFY(IoUtils::isAbsolutePath(fn1a)); const char *name;
const char *path;
bool relative;
} rows[] = {
{ "resource", ":/resource",
#ifdef QMAKE_BUILTIN_PRFS
false
#else
true
#endif #endif
},
#ifdef Q_OS_WIN // all the complications:
// (except UNC: unsupported)
{ "drive-abs", "c:/path/to/file", false },
{ "drive-abs-bs", "c:\\path\\to\\file", false },
{ "drive-path", "c:path/to/file.txt", true },
{ "drive-path-bs", "c:path\\to\\file.txt", true },
{ "rooted", "/Users/qt/bin/true", true },
{ "rooted-bs", "\\Users\\qt\\bin\\true", true },
{ "drive-rel", "c:file.txt", true },
{ "subdir-bs", "path\\to\\file", true },
#else
{ "rooted", "/usr/bin/false", false },
#endif // Q_OS_WIN
{ "subdir", "path/to/file", true },
{ "simple", "file.name", true },
{ "empty", "", true }
};
QString fnbase = "/another/dir"; for (unsigned int i = sizeof(rows) / sizeof(rows[0]); i-- > 0; )
QCOMPARE(IoUtils::resolvePath(fnbase, fn0), QStringLiteral("/another/dir/file/path")); QTest::newRow(rows[i].name) << QString::fromLatin1(rows[i].path)
QCOMPARE(IoUtils::resolvePath(fnbase, fn1), QStringLiteral("/a/unix/file/path")); << rows[i].relative;
}
void tst_qmakelib::ioUtilRelativity()
{
QFETCH(QString, path);
QFETCH(bool, relative);
QCOMPARE(IoUtils::isRelativePath(path), relative);
}
void tst_qmakelib::ioUtilResolve_data()
{
QTest::addColumn<QString>("base");
QTest::addColumn<QString>("path");
QTest::addColumn<QString>("expect");
static const struct {
const char *name;
const char *base;
const char *path;
const char *expect;
} data[] = {
#ifdef Q_OS_WIN // all the complications:
{ "drive-drive", "a:/ms/dir", "z:/root/file", "z:/root/file" },
{ "drive-drive-bs", "a:\\ms\\dir", "z:\\root\\file", "z:/root/file" },
{ "drive-root", "a:/ms/dir", "/root/file", "a:/root/file" },
{ "drive-root-bs", "a:\\ms\\dir", "\\root\\file", "a:/root/file" },
{ "drive-sub", "a:/ms/dir", "sub/file", "a:/ms/dir/sub/file" },
{ "drive-sub-bs", "a:\\ms\\dir", "sub\\file", "a:/ms/dir/sub/file" },
{ "drive-rel", "a:/ms/dir", "file.txt", "a:/ms/dir/file.txt" },
{ "drive-rel-bs", "a:\\ms\\dir", "file.txt", "a:/ms/dir/file.txt" },
#else
{ "abs-abs", "/a/unix/dir", "/root/file", "/root/file" },
{ "abs-sub", "/a/unix/dir", "sub/file", "/a/unix/dir/sub/file" },
{ "abs-rel", "/a/unix/dir", "file.txt", "/a/unix/dir/file.txt" },
#endif // Q_OS_WIN
};
for (unsigned i = sizeof(data) / sizeof(data[0]); i-- > 0; )
QTest::newRow(data[i].name) << QString::fromLatin1(data[i].base)
<< QString::fromLatin1(data[i].path)
<< QString::fromLatin1(data[i].expect);
}
void tst_qmakelib::ioUtilResolve()
{
QFETCH(QString, base);
QFETCH(QString, path);
QFETCH(QString, expect);
QCOMPARE(IoUtils::resolvePath(base, path), expect);
} }
void QMakeTestHandler::print(const QString &fileName, int lineNo, int type, const QString &msg) void QMakeTestHandler::print(const QString &fileName, int lineNo, int type, const QString &msg)

View File

@ -48,7 +48,12 @@ private slots:
void quoteArgUnix(); void quoteArgUnix();
void quoteArgWin_data(); void quoteArgWin_data();
void quoteArgWin(); void quoteArgWin();
void pathUtils(); void pathUtils();
void ioUtilRelativity_data();
void ioUtilRelativity();
void ioUtilResolve_data();
void ioUtilResolve();
void proString(); void proString();
void proStringList(); void proStringList();