Skip to content

Commit

Permalink
79e52ea Check path validity on Windows will validate NTFS permissions…
Browse files Browse the repository at this point in the history
… for network drives.

Signed-off-by: Alkl58 <[email protected]>
  • Loading branch information
Alkl58 authored and allexzander committed Nov 8, 2023
1 parent ab3e4e7 commit 38156c0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/common/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ namespace Utility {

OCSYNC_EXPORT QString formatWinError(long error);

OCSYNC_EXPORT bool canCreateFile(const QString &filePath);

class OCSYNC_EXPORT NtfsPermissionLookupRAII
{
public:
Expand Down
13 changes: 13 additions & 0 deletions src/common/utility_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
16 changes: 15 additions & 1 deletion src/gui/folderman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1701,10 +1701,24 @@ static QString checkPathValidityRecursive(const QString &path)
return FolderMan::tr("The selected path is not a folder!");
}

#ifdef Q_OS_WIN
if (!selFile.isWritable()) {
// isWritable() doesn't cover all NTFS permissions
// 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!");
}
}
#else
if (!selFile.isWritable()) {
return FolderMan::tr("You have no permission to write to the selected folder!");
}
return QString();
#endif

return {};
}

// QFileInfo::canonicalPath returns an empty string if the file does not exist.
Expand Down

0 comments on commit 38156c0

Please sign in to comment.