From 936cae6b53e8982fb087458745e06721d35341ef Mon Sep 17 00:00:00 2001 From: Wang Fei Date: Tue, 16 Nov 2021 13:17:39 +0800 Subject: Add QFileInfo::readSymLink() to read the raw link path The existing symLinkTarget() always resolves the symlink target to an absolute path; readSymLink() provides access to the relative path when that is how the symlink references its target. [ChangeLog][QtCore][QFileInfo] Added readSymLink() to read the symlink's raw target, without resolving to an absolute path. Fixes: QTBUG-96761 Change-Id: I360e55f1a3bdb00e2966229ea8de78cf29a29417 Reviewed-by: Thiago Macieira --- src/corelib/io/qfileinfo.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/corelib/io/qfileinfo.cpp') diff --git a/src/corelib/io/qfileinfo.cpp b/src/corelib/io/qfileinfo.cpp index 351fbc3d86..a9b35b78d4 100644 --- a/src/corelib/io/qfileinfo.cpp +++ b/src/corelib/io/qfileinfo.cpp @@ -38,6 +38,9 @@ QString QFileInfoPrivate::getFileName(QAbstractFileEngine::FileName name) const case QAbstractFileEngine::AbsoluteLinkTarget: ret = QFileSystemEngine::getLinkTarget(fileEntry, metaData).filePath(); break; + case QAbstractFileEngine::RawLinkPath: + ret = QFileSystemEngine::getRawLinkPath(fileEntry, metaData).filePath(); + break; case QAbstractFileEngine::JunctionName: ret = QFileSystemEngine::getJunctionTarget(fileEntry, metaData).filePath(); break; @@ -1221,6 +1224,25 @@ QString QFileInfo::symLinkTarget() const return d->getFileName(QAbstractFileEngine::AbsoluteLinkTarget); } +/*! + \since 6.6 + Read the path the symlink references. + + Returns the raw path referenced by the symbolic link, without resolving a relative + path relative to the directory containing the symbolic link. The returned string will + only be an absolute path if the symbolic link actually references it as such. Returns + an empty string if the object is not a symbolic link. + + \sa symLinkTarget(), exists(), isSymLink(), isDir(), isFile() +*/ +QString QFileInfo::readSymLink() const +{ + Q_D(const QFileInfo); + if (d->isDefaultConstructed) + return {}; + return d->getFileName(QAbstractFileEngine::RawLinkPath); +} + /*! \since 6.2 -- cgit v1.2.3