Utils: "Fix" UNC file path parsing

... by adapting the expectations.

Change-Id: If3a3fe2def370440f1edbbcb5a1c2646f1239f67
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2022-09-13 12:02:15 +02:00
parent 3522097a8b
commit 3dff69979b
2 changed files with 4 additions and 35 deletions

View File

@@ -8,7 +8,6 @@
#include "fileutils.h" #include "fileutils.h"
#include "hostosinfo.h" #include "hostosinfo.h"
#include "qtcassert.h" #include "qtcassert.h"
#include "stringutils.h"
#include <QtGlobal> #include <QtGlobal>
#include <QDateTime> #include <QDateTime>
@@ -131,6 +130,8 @@ inline bool isWindowsDriveLetter(QChar ch);
code from QString based file path to FilePath. An exception here are code from QString based file path to FilePath. An exception here are
fragments of paths of a FilePath that are later used with \c pathAppended() fragments of paths of a FilePath that are later used with \c pathAppended()
or similar which should be kept as QString. or similar which should be kept as QString.
UNC paths will retain their "//" begin, and are recognizable by this.
*/ */
FilePath::FilePath() FilePath::FilePath()
@@ -770,38 +771,6 @@ bool isWindowsDriveLetter(QChar ch)
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'); return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
} }
using RootAndPath = QPair<QStringView, QStringView>;
std::optional<RootAndPath> windowsRootAndPath(const QStringView path)
{
const QChar slash('/');
QStringView workPath = path;
if (workPath.size() < 2)
return {};
if (workPath.startsWith(slash))
workPath = workPath.mid(1);
if (workPath.startsWith(slash)) {
// Its a UNC path ( //server/share/path )
workPath = workPath.mid(1);
const auto firstSlash = workPath.indexOf(slash);
// If the first slash is not found, we either received "//" or "//server"
// If we only received "//" we return an empty root name, otherwise
// we return "//server" as root name.
if (firstSlash == -1)
return RootAndPath{path.length() > 2 ? path : workPath, {}};
workPath = workPath.mid(firstSlash + 1);
return RootAndPath{chopIfEndsWith(path.mid(0, path.length() - workPath.length()), '/'),
workPath};
}
if (workPath.length() > 1 && workPath[1] == ':' && isWindowsDriveLetter(workPath[0]))
return RootAndPath{workPath.mid(0, 2), workPath.mid(3)};
return {};
}
void FilePath::setPath(QStringView path) void FilePath::setPath(QStringView path)
{ {
if (path.startsWith(QStringLiteral("/./"))) if (path.startsWith(QStringLiteral("/./")))

View File

@@ -399,8 +399,8 @@ void tst_fileutils::fromString_data()
QTest::newRow("qrc") << D(":/test.txt", "", "", ":/test.txt"); QTest::newRow("qrc") << D(":/test.txt", "", "", ":/test.txt");
QTest::newRow("qrc-no-slash") << D(":test.txt", "", "", ":test.txt"); QTest::newRow("qrc-no-slash") << D(":test.txt", "", "", ":test.txt");
QTest::newRow("unc-incomplete") << D("//", "", "", "", FailEverywhere); QTest::newRow("unc-incomplete") << D("//", "", "", "//");
QTest::newRow("unc-incomplete-only-server") << D("//server", "", "", "//server/", FailEverywhere); QTest::newRow("unc-incomplete-only-server") << D("//server", "", "", "//server");
QTest::newRow("unc-incomplete-only-server-2") << D("//server/", "", "", "//server/"); QTest::newRow("unc-incomplete-only-server-2") << D("//server/", "", "", "//server/");
QTest::newRow("unc-server-and-share") << D("//server/share", "", "", "//server/share"); QTest::newRow("unc-server-and-share") << D("//server/share", "", "", "//server/share");
QTest::newRow("unc-server-and-share-2") << D("//server/share/", "", "", "//server/share/"); QTest::newRow("unc-server-and-share-2") << D("//server/share/", "", "", "//server/share/");