Fix resolution of relative symlinks from relative path on unix

Consider the following:
/root/target - a file
/root/path/link -> ../target
/root/path/other/exe - executable

Running from /root/path/other.

exe is:

#include <QDebug>
#include <QFileInfo>

int main()
{
    qDebug() << QFileInfo("../link").symLinkTarget()
    return 0;
}

The link references /root/target, but the current output is
/root/path/target.

The link doesn't depend on the PWD. It depends on its own directory.

Change-Id: I61e95018154a75e0e0d795ee801068e18870a5df
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Orgad Shaneh 2017-08-26 23:02:07 +03:00 committed by Orgad Shaneh
parent e51dbda067
commit 0f11fab6f7
2 changed files with 12 additions and 7 deletions

View File

@ -187,13 +187,11 @@ QFileSystemEntry QFileSystemEngine::getLinkTarget(const QFileSystemEntry &link,
#endif
if (!ret.startsWith(QLatin1Char('/'))) {
const QString linkFilePath = link.filePath();
if (linkFilePath.startsWith(QLatin1Char('/'))) {
ret.prepend(linkFilePath.leftRef(linkFilePath.lastIndexOf(QLatin1Char('/')))
+ QLatin1Char('/'));
} else {
ret.prepend(QDir::currentPath() + QLatin1Char('/'));
}
const QString linkPath = link.path();
if (linkPath.startsWith(QLatin1Char('/')))
ret.prepend(linkPath + QLatin1Char('/'));
else
ret.prepend(QDir::currentPath() + QLatin1Char('/') + linkPath + QLatin1Char('/'));
}
ret = QDir::cleanPath(ret);
if (ret.size() > 1 && ret.endsWith(QLatin1Char('/')))

View File

@ -1261,6 +1261,7 @@ void tst_QFileInfo::isSymLink_data()
QFile::remove("link.lnk");
QFile::remove("brokenlink.lnk");
QFile::remove("dummyfile");
QFile::remove("relative/link.lnk");
QFile file1(m_sourceFile);
QVERIFY(file1.link("link.lnk"));
@ -1277,6 +1278,12 @@ void tst_QFileInfo::isSymLink_data()
QTest::newRow("existent file") << m_sourceFile << false << "";
QTest::newRow("link") << "link.lnk" << true << QFileInfo(m_sourceFile).absoluteFilePath();
QTest::newRow("broken link") << "brokenlink.lnk" << true << QFileInfo("dummyfile").absoluteFilePath();
#ifndef Q_OS_WIN
QDir::current().mkdir("relative");
QFile::link("../dummyfile", "relative/link.lnk");
QTest::newRow("relative link") << "relative/link.lnk" << true << QFileInfo("dummyfile").absoluteFilePath();
#endif
#endif
}