From: Hannah von Reth Date: Mon, 14 Dec 2020 12:32:28 +0000 (+0100) Subject: Cleanup pathtoUNC and its test X-Git-Tag: archive/raspbian/3.16.7-1_deb13u1+rpi1~1^2~12^2~21^2~446^2~9 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=1144473f5dab7e9e1c9b97fa39d27094283e4167;p=nextcloud-desktop.git Cleanup pathtoUNC and its test --- diff --git a/src/common/filesystembase.cpp b/src/common/filesystembase.cpp index 6b2271777..9914c1a7e 100644 --- a/src/common/filesystembase.cpp +++ b/src/common/filesystembase.cpp @@ -497,33 +497,26 @@ bool FileSystem::isJunction(const QString &filename) #endif } -QString FileSystem::pathtoUNC(const QString &str) +#ifdef Q_OS_WIN +QString FileSystem::pathtoUNC(const QString &_str) { - int len = 0; - QString longStr; - - len = str.length(); - longStr.reserve(len + 4); - - // prepend \\?\ and convert '/' => '\' to support long names - if (str[0] == QLatin1Char('/') || str[0] == QLatin1Char('\\')) { - // Don't prepend if already UNC - if (!(len > 1 && (str[1] == QLatin1Char('/') || str[1] == QLatin1Char('\\')))) { - longStr.append(QStringLiteral("\\\\?")); - } - } else { - longStr.append(QStringLiteral("\\\\?\\")); // prepend string by this four magic chars. + if (_str.isEmpty()) { + return _str; } - longStr += str; + const QString str = QDir::toNativeSeparators(_str); + const QLatin1Char sep('\\'); - /* replace all occurences of / with the windows native \ */ + // we already have a unc path + if (str.startsWith(sep + sep)) { + return str; + } + // prepend \\?\ and to support long names - for (auto &c : longStr) { - if (c == QLatin1Char('/')) { - c = QLatin1Char('\\'); - } + if (str.at(0) == sep) { + return QStringLiteral(R"(\\?)") + str; } - return longStr; + return QStringLiteral(R"(\\?\)") + str; } +#endif } // namespace OCC diff --git a/src/common/filesystembase.h b/src/common/filesystembase.h index 4e3573058..0259fb6c7 100644 --- a/src/common/filesystembase.h +++ b/src/common/filesystembase.h @@ -128,6 +128,19 @@ namespace FileSystem { * Returns the file system used at the given path. */ QString fileSystemForPath(const QString &path); + + /* + * This function takes a path and converts it to a UNC representation of the + * string. That means that it prepends a \\?\ (unless already UNC) and converts + * all slashes to backslashes. + * + * Note the following: + * - The string must be absolute. + * - it needs to contain a drive character to be a valid UNC + * - A conversion is only done if the path len is larger than 245. Otherwise + * the windows API functions work with the normal "unixoid" representation too. + */ + QString OCSYNC_EXPORT pathtoUNC(const QString &str); #endif /** @@ -144,19 +157,6 @@ namespace FileSystem { * Returns whether the file is a junction (windows only) */ bool OCSYNC_EXPORT isJunction(const QString &filename); - - /* - * This function takes a path and converts it to a UNC representation of the - * string. That means that it prepends a \\?\ (unless already UNC) and converts - * all slashes to backslashes. - * - * Note the following: - * - The string must be absolute. - * - it needs to contain a drive character to be a valid UNC - * - A conversion is only done if the path len is larger than 245. Otherwise - * the windows API functions work with the normal "unixoid" representation too. - */ - QString OCSYNC_EXPORT pathtoUNC(const QString &str); } /** @} */ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b13f6bff1..0d510c51a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -74,6 +74,7 @@ if( UNIX AND NOT APPLE ) endif(UNIX AND NOT APPLE) if (WIN32) + nextcloud_add_test(LongWinPath "") nextcloud_add_test(SyncCfApi "") endif() diff --git a/test/csync/CMakeLists.txt b/test/csync/CMakeLists.txt index 224ecb606..27e6d3857 100644 --- a/test/csync/CMakeLists.txt +++ b/test/csync/CMakeLists.txt @@ -24,6 +24,3 @@ add_cmocka_test(check_std_c_jhash std_tests/check_std_c_jhash.c ${TEST_TARGET_LI # vio add_cmocka_test(check_vio_ext vio_tests/check_vio_ext.cpp ${TEST_TARGET_LIBRARIES}) -# encoding -add_cmocka_test(check_encoding_functions encoding_tests/check_encoding.cpp ${TEST_TARGET_LIBRARIES}) - diff --git a/test/csync/encoding_tests/check_encoding.cpp b/test/csync/encoding_tests/check_encoding.cpp deleted file mode 100644 index 98ac0fa22..000000000 --- a/test/csync/encoding_tests/check_encoding.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* - * libcsync -- a library to sync a directory with another - * - * Copyright (c) 2013 by Klaas Freitag - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ -#include -#include "common/filesystembase.h" -#include "torture.h" - -#ifdef _WIN32 -#include -#endif - -#include "torture.h" - -static void check_long_win_path(void **state) -{ - (void) state; /* unused */ - - { - const auto path = QStringLiteral("C://DATA/FILES/MUSIC/MY_MUSIC.mp3"); // check a short path - const auto exp_path = QStringLiteral(R"(\\?\C:\\DATA\FILES\MUSIC\MY_MUSIC.mp3)"); - QString new_short = OCC::FileSystem::pathtoUNC(path); - assert_string_equal(new_short.constData(), exp_path.constData()); - } - - { - const auto path = QStringLiteral(R"(\\foo\bar/MY_MUSIC.mp3)"); - const auto exp_path = QStringLiteral(R"(\\foo\bar\MY_MUSIC.mp3)"); - QString new_short = OCC::FileSystem::pathtoUNC(path); - assert_string_equal(new_short.constData(), exp_path.constData()); - } - - { - const auto path = QStringLiteral(R"(//foo\bar/MY_MUSIC.mp3)"); - const auto exp_path = QStringLiteral(R"(\\foo\bar\MY_MUSIC.mp3)"); - QString new_short = OCC::FileSystem::pathtoUNC(path); - assert_string_equal(new_short.constData(), exp_path.constData()); - } - - { - const auto path = QStringLiteral(R"(\foo\bar)"); - const auto exp_path = QStringLiteral(R"(\\?\foo\bar)"); - QString new_short = OCC::FileSystem::pathtoUNC(path); - assert_string_equal(new_short.constData(), exp_path.constData()); - } - - { - const auto path = QStringLiteral("/foo/bar"); - const auto exp_path = QStringLiteral(R"(\\?\foo\bar)"); - QString new_short = OCC::FileSystem::pathtoUNC(path); - assert_string_equal(new_short.constData(), exp_path.constData()); - } - - const auto longPath = QStringLiteral("D://alonglonglonglong/blonglonglonglong/clonglonglonglong/dlonglonglonglong/" - "elonglonglonglong/flonglonglonglong/glonglonglonglong/hlonglonglonglong/ilonglonglonglong/" - "jlonglonglonglong/klonglonglonglong/llonglonglonglong/mlonglonglonglong/nlonglonglonglong/" - "olonglonglonglong/file.txt"); - const auto longPathConv = QStringLiteral(R"(\\?\D:\\alonglonglonglong\blonglonglonglong\clonglonglonglong\dlonglonglonglong\)" - R"(elonglonglonglong\flonglonglonglong\glonglonglonglong\hlonglonglonglong\ilonglonglonglong\)" - R"(jlonglonglonglong\klonglonglonglong\llonglonglonglong\mlonglonglonglong\nlonglonglonglong\)" - R"(olonglonglonglong\file.txt)"); - - QString new_long = OCC::FileSystem::pathtoUNC(longPath); - // printf( "XXXXXXXXXXXX %s %d\n", new_long, mem_reserved); - - assert_string_equal(new_long.constData(), longPathConv.constData()); - - // printf( "YYYYYYYYYYYY %ld\n", strlen(new_long)); - assert_int_equal(new_long.length(), 286); -} - -int torture_run_tests(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(check_long_win_path), - - }; - - return cmocka_run_group_tests(tests, nullptr, nullptr); -} - diff --git a/test/testlongwinpath.cpp b/test/testlongwinpath.cpp new file mode 100644 index 000000000..16933813c --- /dev/null +++ b/test/testlongwinpath.cpp @@ -0,0 +1,87 @@ +/* + * libcsync -- a library to sync a directory with another + * + * Copyright (c) 2013 by Klaas Freitag + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#include "common/filesystembase.h" + +#include + + +class TestLongWindowsPath : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void check_long_win_path() + { + { + const auto path = QStringLiteral("C://DATA/FILES/MUSIC/MY_MUSIC.mp3"); // check a short path + const auto exp_path = QStringLiteral("\\\\?\\C:\\\\DATA\\FILES\\MUSIC\\MY_MUSIC.mp3"); + QString new_short = OCC::FileSystem::pathtoUNC(path); + QCOMPARE(new_short, exp_path); + } + + { + const auto path = QStringLiteral("\\\\foo\\bar/MY_MUSIC.mp3"); + const auto exp_path = QStringLiteral("\\\\foo\\bar\\MY_MUSIC.mp3"); + QString new_short = OCC::FileSystem::pathtoUNC(path); + QCOMPARE(new_short, exp_path); + } + + { + const auto path = QStringLiteral("//foo\\bar/MY_MUSIC.mp3"); + const auto exp_path = QStringLiteral("\\\\foo\\bar\\MY_MUSIC.mp3"); + QString new_short = OCC::FileSystem::pathtoUNC(path); + QCOMPARE(new_short, exp_path); + } + + { + const auto path = QStringLiteral("\\foo\\bar"); + const auto exp_path = QStringLiteral("\\\\?\\foo\\bar"); + QString new_short = OCC::FileSystem::pathtoUNC(path); + QCOMPARE(new_short, exp_path); + } + + { + const auto path = QStringLiteral("/foo/bar"); + const auto exp_path = QStringLiteral("\\\\?\\foo\\bar"); + QString new_short = OCC::FileSystem::pathtoUNC(path); + QCOMPARE(new_short, exp_path); + } + + const auto longPath = QStringLiteral("D://alonglonglonglong/blonglonglonglong/clonglonglonglong/dlonglonglonglong/" + "elonglonglonglong/flonglonglonglong/glonglonglonglong/hlonglonglonglong/ilonglonglonglong/" + "jlonglonglonglong/klonglonglonglong/llonglonglonglong/mlonglonglonglong/nlonglonglonglong/" + "olonglonglonglong/file.txt"); + const auto longPathConv = QStringLiteral("\\\\?\\D:\\\\alonglonglonglong\\blonglonglonglong\\clonglonglonglong\\dlonglonglonglong\\" + "elonglonglonglong\\flonglonglonglong\\glonglonglonglong\\hlonglonglonglong\\ilonglonglonglong\\" + "jlonglonglonglong\\klonglonglonglong\\llonglonglonglong\\mlonglonglonglong\\nlonglonglonglong\\" + "olonglonglonglong\\file.txt"); + + QString new_long = OCC::FileSystem::pathtoUNC(longPath); + // printf( "XXXXXXXXXXXX %s %d\n", new_long, mem_reserved); + + QCOMPARE(new_long, longPathConv); + + // printf( "YYYYYYYYYYYY %ld\n", strlen(new_long)); + QCOMPARE(new_long.length(), 286); + } +}; + +QTEST_GUILESS_MAIN(TestLongWindowsPath) +#include "testlongwinpath.moc"