diff --git a/src/common/utility.h b/src/common/utility.h index e19f0da6ae1ee..300d1294d0515 100644 --- a/src/common/utility.h +++ b/src/common/utility.h @@ -275,6 +275,8 @@ namespace Utility { OCSYNC_EXPORT QString formatWinError(long error); + OCSYNC_EXPORT bool canCreateFile(const QString &filePath); + class OCSYNC_EXPORT NtfsPermissionLookupRAII { public: diff --git a/src/common/utility_win.cpp b/src/common/utility_win.cpp index 37b348660c0de..5279c61375c70 100644 --- a/src/common/utility_win.cpp +++ b/src/common/utility_win.cpp @@ -430,6 +430,19 @@ void Utility::UnixTimeToLargeIntegerFiletime(time_t t, LARGE_INTEGER *hundredNSe hundredNSecs->HighPart = ll >>32; } +bool Utility::canCreateFile(const QString &filePath) +{ + Q_ASSERT(!filePath.isEmpty()); + QFile testFile(filePath); + if (!testFile.open(QIODevice::WriteOnly)) { + return false; + } + testFile.close(); + if (!testFile.remove()) { + return false; + } + return true; +} QString Utility::formatWinError(long errorCode) { diff --git a/src/gui/folderman.cpp b/src/gui/folderman.cpp index a7d65c30d5399..3fe657e809e55 100644 --- a/src/gui/folderman.cpp +++ b/src/gui/folderman.cpp @@ -1704,17 +1704,11 @@ static QString checkPathValidityRecursive(const QString &path) #ifdef Q_OS_WIN if (!selFile.isWritable()) { // isWritable() doesn't cover all NTFS permissions - // Try write - const auto testPath = selFile.dir().filePath("nextcloud-write-test-file.txt"); - const auto fp = fopen(testPath.toStdWString().c_str(), "w"); - if (!fp) { - return FolderMan::tr("You have no permission to write to the selected folder!"); - } - fclose(fp); - - // Try delete - const auto rc = remove(testPath.toStdWString().c_str()); - if (rc) { + // try creating and removing a test file, and make sure it is excluded from sync + const QString pathWithSlash = !path.endsWith(QLatin1Char('/')) ? path + QLatin1Char('/') : path; + const QString testFileName = pathWithSlash + QStringLiteral("~$%1-write-test-file-%2").arg(OCC::Theme::instance()->appNameGUI()) + .arg(QDateTime::currentDateTime().toMSecsSinceEpoch()); + if (!Utility::canCreateFile(testFileName)) { return FolderMan::tr("You have no permission to write to the selected folder!"); } } @@ -1723,8 +1717,7 @@ static QString checkPathValidityRecursive(const QString &path) return FolderMan::tr("You have no permission to write to the selected folder!"); } #endif - - return QString(); + return {}; } // QFileInfo::canonicalPath returns an empty string if the file does not exist.