Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/owncloud/client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKlaas Freitag <freitag@owncloud.com>2016-10-12 15:48:00 +0300
committerKlaas Freitag <freitag@owncloud.com>2016-10-12 15:48:00 +0300
commit27d23edaccbf239881e23824cfd9e1de7d04e545 (patch)
treee4e7bb50db7ba1496ca9f770cd69582323211b7a
parente1a48e3c33161d47ad7f30b0dea30bd350c98b1b (diff)
Utility: Add a function to check if two filenames are equal plus test.
It calls canonical path always and works with the correct case preserving depending on the platform.
-rw-r--r--src/libsync/utility.cpp16
-rw-r--r--src/libsync/utility.h5
-rw-r--r--test/testutility.cpp47
3 files changed, 67 insertions, 1 deletions
diff --git a/src/libsync/utility.cpp b/src/libsync/utility.cpp
index 58c4815c7..880064256 100644
--- a/src/libsync/utility.cpp
+++ b/src/libsync/utility.cpp
@@ -285,12 +285,26 @@ bool Utility::fsCasePreserving()
if( isWindows() || isMac() ) {
re = true;
} else {
- static bool isTest = qgetenv("OWNCLOUD_TEST_CASE_PRESERVING").toInt();
+ bool isTest = qgetenv("OWNCLOUD_TEST_CASE_PRESERVING").toInt();
re = isTest;
}
return re;
}
+bool Utility::fileNamesEqual( const QString& fn1, const QString& fn2)
+{
+ const QDir fd1(fn1);
+ const QDir fd2(fn2);
+
+ // Attention: If the path does not exist, canonicalPath returns ""
+ // ONLY use this function with existing pathes.
+ const QString a = fd1.canonicalPath();
+ const QString b = fd2.canonicalPath();
+ bool re = !a.isEmpty() && QString::compare( a, b,
+ fsCasePreserving() ? Qt::CaseInsensitive : Qt::CaseSensitive) == 0;
+ return re;
+}
+
QDateTime Utility::qDateTimeFromTime_t(qint64 t)
{
return QDateTime::fromMSecsSinceEpoch(t * 1000);
diff --git a/src/libsync/utility.h b/src/libsync/utility.h
index 151c63546..57ae61536 100644
--- a/src/libsync/utility.h
+++ b/src/libsync/utility.h
@@ -100,6 +100,11 @@ namespace Utility
// if false, the two cases are two different files.
OWNCLOUDSYNC_EXPORT bool fsCasePreserving();
+ // Check if two pathes that MUST exist are equal. This function
+ // uses QDir::canonicalPath() to judge and cares for the systems
+ // case sensitivity.
+ OWNCLOUDSYNC_EXPORT bool fileNamesEqual( const QString& fn1, const QString& fn2);
+
// Call the given command with the switch --version and rerun the first line
// of the output.
// If command is empty, the function calls the running application which, on
diff --git a/test/testutility.cpp b/test/testutility.cpp
index 16fd3066b..63015f053 100644
--- a/test/testutility.cpp
+++ b/test/testutility.cpp
@@ -5,6 +5,9 @@
*/
#include <QtTest>
+#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
+#include <QTemporaryDir>
+#endif
#include "utility.h"
@@ -158,6 +161,50 @@ private slots:
s = timeAgoInWords(earlyTS, laterTS );
QCOMPARE(s, QLatin1String("Less than a minute ago"));
}
+
+ void testFsCasePreserving()
+ {
+ qputenv("OWNCLOUD_TEST_CASE_PRESERVING", "1");
+ QVERIFY(fsCasePreserving());
+ qputenv("OWNCLOUD_TEST_CASE_PRESERVING", "0");
+ QVERIFY(! fsCasePreserving());
+ qunsetenv("OWNCLOUD_TEST_CASE_PRESERVING");
+ QVERIFY(isMac() || isWindows() ? fsCasePreserving() : ! fsCasePreserving());
+ }
+
+ void testFileNamesEqual()
+ {
+#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
+ qDebug() << "*** checking fileNamesEqual function";
+ QTemporaryDir dir;
+ QVERIFY(dir.isValid());
+ QDir dir2(dir.path());
+ QVERIFY(dir2.mkpath("test"));
+ if( !fsCasePreserving() ) {
+ QVERIFY(dir2.mkpath("TEST"));
+ }
+ QVERIFY(dir2.mkpath("test/TESTI"));
+ QVERIFY(dir2.mkpath("TESTI"));
+
+ QString a = dir.path();
+ QString b = dir.path();
+
+ QVERIFY(fileNamesEqual(a, b));
+
+ QVERIFY(fileNamesEqual(a+"/test", b+"/test")); // both exist
+ QVERIFY(fileNamesEqual(a+"/test/TESTI", b+"/test/../test/TESTI")); // both exist
+
+ qputenv("OWNCLOUD_TEST_CASE_PRESERVING", "1");
+ QVERIFY(fileNamesEqual(a+"/test", b+"/TEST")); // both exist
+
+ QVERIFY(!fileNamesEqual(a+"/test", b+"/test/TESTI")); // both are different
+
+ dir.remove();
+ qunsetenv("OWNCLOUD_TEST_CASE_PRESERVING");
+#endif
+ }
+
+
};
QTEST_APPLESS_MAIN(TestUtility)