diff options
author | Joerg Bornemann <joerg.bornemann@digia.com> | 2013-04-10 13:31:49 +0200 |
---|---|---|
committer | Christian Kandeler <christian.kandeler@digia.com> | 2013-04-12 14:48:39 +0200 |
commit | b4b068526d989a7b90162237eef96d7bd20e5462 (patch) | |
tree | bd1a9266d019add48982eaca3b950cc8cb43bb07 | |
parent | a7f8f79dad4f25656f43159728c48d3aa6905b4d (diff) |
introduce FileInfo::isFileCaseCorrect
This function checks if the file name of a given path has the same
case that's stored in the file system. Only implemented for Windows
and Darwin derivatives, no-op on all other systems.
Change-Id: I3f0aa8ab89c57b08fbb7f20b42b0bb873bd8448b
Reviewed-by: Christian Kandeler <christian.kandeler@digia.com>
-rw-r--r-- | src/lib/tools/fileinfo.cpp | 26 | ||||
-rw-r--r-- | src/lib/tools/fileinfo.h | 1 | ||||
-rw-r--r-- | src/lib/tools/tst_tools.cpp | 18 | ||||
-rw-r--r-- | src/lib/tools/tst_tools.h | 1 |
4 files changed, 44 insertions, 2 deletions
diff --git a/src/lib/tools/fileinfo.cpp b/src/lib/tools/fileinfo.cpp index 6c043bc93..a12250684 100644 --- a/src/lib/tools/fileinfo.cpp +++ b/src/lib/tools/fileinfo.cpp @@ -37,8 +37,10 @@ #include <QDir> #include <QFileInfo> -#ifdef Q_OS_UNIX +#if defined(Q_OS_UNIX) #include <sys/stat.h> +#elif defined(Q_OS_WIN) +#include <qt_windows.h> #endif namespace qbs { @@ -193,9 +195,29 @@ bool FileInfo::globMatches(const QRegExp ®exp, const QString &fileName) return regexp.exactMatch(fileName); } +bool FileInfo::isFileCaseCorrect(const QString &filePath) +{ #if defined(Q_OS_WIN) + // QFileInfo::canonicalFilePath() does not return the real case of the file path on Windows. + QFileInfo fi(filePath); + const QString absolute = fi.absoluteFilePath(); + WIN32_FIND_DATA fd; + HANDLE hFindFile = ::FindFirstFile((wchar_t*)absolute.utf16(), &fd); + if (hFindFile == INVALID_HANDLE_VALUE) + return false; + const QString actualFileName = QString::fromWCharArray(fd.cFileName); + FindClose(hFindFile); + return actualFileName == fi.fileName(); +#elif defined(Q_OS_DARWIN) + QFileInfo fi(filePath); + return fi.absoluteFilePath() == fi.canonicalFilePath(); +#else + Q_UNUSED(filePath) + return true; +#endif +} -#include <qt_windows.h> +#if defined(Q_OS_WIN) #define z(x) reinterpret_cast<WIN32_FILE_ATTRIBUTE_DATA*>(const_cast<FileInfo::InternalStatType*>(&x)) diff --git a/src/lib/tools/fileinfo.h b/src/lib/tools/fileinfo.h index 455eee991..d8ac2356c 100644 --- a/src/lib/tools/fileinfo.h +++ b/src/lib/tools/fileinfo.h @@ -66,6 +66,7 @@ public: static bool isPattern(const QString &str); static QString resolvePath(const QString &base, const QString &rel); static bool globMatches(const QRegExp &pattern, const QString &subject); + static bool isFileCaseCorrect(const QString &filePath); private: #if defined(Q_OS_WIN) diff --git a/src/lib/tools/tst_tools.cpp b/src/lib/tools/tst_tools.cpp index 89680ea68..be63e64b4 100644 --- a/src/lib/tools/tst_tools.cpp +++ b/src/lib/tools/tst_tools.cpp @@ -34,6 +34,8 @@ #include <tools/hostosinfo.h> #include <tools/profile.h> #include <tools/settings.h> +#include <QFileInfo> +#include <QTemporaryFile> #include <QTest> namespace qbs { @@ -61,6 +63,22 @@ void TestTools::testFileInfo() QCOMPARE(FileInfo("/does/not/exist").lastModified(), FileTime()); } +void TestTools::fileCaseCheck() +{ + QTemporaryFile tempFile(QLatin1String("CamelCase")); + QVERIFY(tempFile.open()); + QFileInfo tempFileInfo(tempFile.fileName()); + const QString lowerFilePath = tempFileInfo.absolutePath() + QLatin1Char('/') + + tempFileInfo.fileName().toLower(); + const QString upperFilePath = tempFileInfo.absolutePath() + QLatin1Char('/') + + tempFileInfo.fileName().toUpper(); + QVERIFY(FileInfo::isFileCaseCorrect(tempFileInfo.absoluteFilePath())); + if (QFile::exists(lowerFilePath)) + QVERIFY(!FileInfo::isFileCaseCorrect(lowerFilePath)); + if (QFile::exists(upperFilePath)) + QVERIFY(!FileInfo::isFileCaseCorrect(upperFilePath)); +} + void TestTools::testProfiles() { bool exceptionCaught; diff --git a/src/lib/tools/tst_tools.h b/src/lib/tools/tst_tools.h index c5d10a7d5..9957c4f2f 100644 --- a/src/lib/tools/tst_tools.h +++ b/src/lib/tools/tst_tools.h @@ -44,6 +44,7 @@ public: private slots: void testFileInfo(); + void fileCaseCheck(); void testProfiles(); private: |