#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
* 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
/**
* 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);
}
/** @} */
endif(UNIX AND NOT APPLE)
if (WIN32)
+ nextcloud_add_test(LongWinPath "")
nextcloud_add_test(SyncCfApi "")
endif()
# 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})
-
+++ /dev/null
-/*
- * libcsync -- a library to sync a directory with another
- *
- * Copyright (c) 2013 by Klaas Freitag <freitag@owncloud.com>
- *
- * 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 <cstdio>
-#include "common/filesystembase.h"
-#include "torture.h"
-
-#ifdef _WIN32
-#include <string.h>
-#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);
-}
-
--- /dev/null
+/*
+ * libcsync -- a library to sync a directory with another
+ *
+ * Copyright (c) 2013 by Klaas Freitag <freitag@owncloud.com>
+ *
+ * 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 <QTest>
+
+
+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"