Skip to content

Commit 4504d25

Browse files
committed
WIP: mkpath test
1 parent 10a5b75 commit 4504d25

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

src/common/filesystembase.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include <QSettings>
2727
#include <QStorageInfo>
2828

29+
#include <filesystem>
30+
2931
#include <sys/stat.h>
3032
#include <sys/types.h>
3133

@@ -382,18 +384,14 @@ bool FileSystem::mkpath(const QString &parent, const QString &newDir)
382384
#ifdef Q_OS_WIN
383385
return QDir(parent).mkpath(newDir);
384386
#else // POSIX
385-
auto parts = newDir.split(u'/');
386-
QString parentIt = parent;
387-
while (!parts.isEmpty()) {
388-
auto part = parts.takeFirst();
389-
parentIt.append(u'/' + part);
390-
if (::mkdir(encodeFileName(QDir::toNativeSeparators(parentIt)).constData(), 0777) != 0) {
391-
if (errno != EEXIST) {
392-
return false;
393-
}
394-
}
395-
}
396-
return true;
387+
std::error_code err;
388+
QString fullPath = parent;
389+
if (!fullPath.endsWith(u'/')) {
390+
fullPath += u'/';
391+
}
392+
fullPath += newDir;
393+
std::filesystem::create_directories(newDir.toStdString(), err);
394+
return err.value() == 0;
397395
#endif
398396
}
399397

test/testfilesystem.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,35 @@ private Q_SLOTS:
137137

138138
QVERIFY(tmp.remove());
139139
}
140+
141+
void testMkdir_data()
142+
{
143+
QTest::addColumn<QString>("name");
144+
145+
const unsigned char a_umlaut_composed_bytes[] = {0xc3, 0xa4, 0x00};
146+
const QString a_umlaut_composed = QString::fromUtf8(reinterpret_cast<const char *>(a_umlaut_composed_bytes));
147+
const QString a_umlaut_decomposed = a_umlaut_composed.normalized(QString::NormalizationForm_D);
148+
149+
QTest::newRow("a-umlaut composed") << a_umlaut_composed;
150+
QTest::newRow("a-umlaut decomposed") << a_umlaut_decomposed;
151+
}
152+
153+
// This is not a full test, it is meant to verify that no nomalization changes are done.
154+
void testMkdir()
155+
{
156+
QFETCH(QString, name);
157+
158+
auto tmp = OCC::TestUtils::createTempDir();
159+
QVERIFY(OCC::FileSystem::mkpath(tmp.path(), name));
160+
csync_file_stat_t buf;
161+
auto dh = csync_vio_local_opendir(tmp.path());
162+
QVERIFY(dh);
163+
while (auto fs = csync_vio_local_readdir(dh, nullptr)) {
164+
QCOMPARE(fs->path, name);
165+
QCOMPARE(fs->type, ItemTypeDirectory);
166+
}
167+
csync_vio_local_closedir(dh);
168+
}
140169
};
141170

142171
QTEST_GUILESS_MAIN(TestFileSystem)

0 commit comments

Comments
 (0)