Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/libsync/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@
}

// Code inspired from Qt5's QDir::removeRecursively
bool FileSystem::removeRecursively(const QString &path,

Check warning on line 255 in src/libsync/filesystem.cpp

View workflow job for this annotation

GitHub Actions / build

src/libsync/filesystem.cpp:255:18 [readability-function-cognitive-complexity]

function 'removeRecursively' has cognitive complexity of 41 (threshold 25)

Check failure on line 255 in src/libsync/filesystem.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 50 to the 25 allowed.

See more on https://sonarcloud.io/project/issues?id=nextcloud_desktop&issues=AZz7ikT-PCIP36ncNiOx&open=AZz7ikT-PCIP36ncNiOx&pullRequest=9639
const std::function<void(const QString &path, bool isDir)> &onDeleted,
QStringList *errors,
const std::function<void(const QString &path, bool isDir)> &onError,
Expand Down Expand Up @@ -312,7 +312,13 @@
auto folderDeleteError = QString{};

try {
if (!std::filesystem::remove(std::filesystem::path{fileInfo.filePath().toStdWString()})) {
#ifdef Q_OS_WIN
// std::filesystem::remove does not handle paths greater than MAX_PATH, but it seems to be fine with the prefixed UNC paths ...
const auto fspath = std::filesystem::path{FileSystem::longWinPath(fileInfo.filePath()).toStdWString()};
#else
const auto fspath = std::filesystem::path{fileInfo.filePath().toStdWString()};
#endif
if (!std::filesystem::remove(fspath)) {
qCWarning(lcFileSystem()) << "File is already deleted" << fileInfo.filePath();
}
}
Expand Down
22 changes: 22 additions & 0 deletions test/testfilesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* any purpose.
*/

#include <QtTest>

Check failure on line 10 in test/testfilesystem.cpp

View workflow job for this annotation

GitHub Actions / build

test/testfilesystem.cpp:10:10 [clang-diagnostic-error]

'QtTest' file not found
#include <QTemporaryDir>
#include <QTemporaryFile>

Expand Down Expand Up @@ -320,6 +320,28 @@
QCOMPARE_EQ(aclSizeAfterReadWrite.AclBytesInUse, aclSizeInitial.AclBytesInUse);
}
#endif

void testRecursiveDeletionLongPaths()
{
QTemporaryDir tempDir;
QDir tempQDir{tempDir.path()};

const auto makeLongPathSegment = [](const QString &base) -> QString {
QString baseSegment = base + " ";
return baseSegment + QString("0").repeated(200 - baseSegment.size());
};

tempQDir.mkpath("Folder 1 - short name but long single subfolder/" + makeLongPathSegment("Subfolder without accentued characters - padding"));
tempQDir.mkpath(makeLongPathSegment("Folder 2 - directly a very long name"));
auto folder3Path = u"Folder 3 - short name but long multiples subfolder"_s;
for (auto i = 0; i < 5; i++) {
folder3Path.append("/subfolder %1 - total length 40 characters"_L1.arg(QString::number(i + 1)));
}
tempQDir.mkpath(folder3Path);
tempQDir.mkpath("Folder 4 - short name");

QVERIFY(FileSystem::removeRecursively(tempDir.path()));
}
};

QTEST_GUILESS_MAIN(TestFileSystem)
Expand Down
Loading