Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Normalize file names before using them as keys #12002

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
10 changes: 5 additions & 5 deletions src/common/syncjournaldb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,11 +824,11 @@ QVector<QByteArray> SyncJournalDb::tableColumns(const QByteArray &table)

qint64 SyncJournalDb::getPHash(const QByteArray &file)
{
qint64 h;
int len = file.length();

h = c_jhash64((uint8_t *)file.data(), len, 0);
return h;
QByteArray normalizedFile = file;
if (Utility::isMac()) {
normalizedFile = QString::fromUtf8(file).normalized(QString::NormalizationForm_C).toUtf8();
}
return c_jhash64((uint8_t *)normalizedFile.data(), normalizedFile.length(), 0);
}

Result<void, QString> SyncJournalDb::setFileRecord(const SyncJournalFileRecord &_record)
Expand Down
3 changes: 3 additions & 0 deletions src/gui/folderman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,9 @@ Folder *FolderMan::addFolderInternal(
Folder *FolderMan::folderForPath(const QString &path, QString *relativePath)
{
QString absolutePath = QDir::cleanPath(path) + QLatin1Char('/');
if (!Utility::isLinux()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity: why not use an ifdef? These are all ifs which never change at runtime.

absolutePath = absolutePath.normalized(QString::NormalizationForm_C);
}

for (auto *folder : std::as_const(_folders)) {
const QString folderPath = folder->cleanPath() + QLatin1Char('/');
Expand Down
23 changes: 18 additions & 5 deletions src/libsync/discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,24 @@ void ProcessDirectoryJob::process()
RemoteInfo serverEntry;
LocalInfo localEntry;
};

// The key for the `entries` mapping is the NFC form of the filename. This is done to prevent
// NFC->NFD or NFD->NFC conversions done by `QFile` and/or the underlying local filesystem.
std::map<QString, Entries> entries;
for (auto &e : _serverNormalQueryEntries) {
entries[e.name].serverEntry = std::move(e);
// Keep the name on the server as-is, but use the NFC form as the key
entries[e.name.normalized(QString::NormalizationForm_C)].serverEntry = std::move(e);
}
_serverNormalQueryEntries.clear();

// fetch all the name from the DB
auto pathU8 = _currentFolder._original.toUtf8();
auto pathU8 = Utility::isMac() ? _currentFolder._original.normalized(QString::NormalizationForm_C).toUtf8() : _currentFolder._original.toUtf8(); // XXX
if (!_discoveryData->_statedb->listFilesInPath(pathU8, [&](const SyncJournalFileRecord &rec) {
auto name = pathU8.isEmpty() ? QString::fromUtf8(rec._path) : QString::fromUtf8(rec._path.constData() + (pathU8.size() + 1));
if (rec.isVirtualFile() && isVfsWithSuffix()) {
name = chopVirtualFileSuffix(name);
}
auto &dbEntry = entries[name].dbEntry;
auto &dbEntry = entries[name.normalized(QString::NormalizationForm_C)].dbEntry;
dbEntry = rec;
setupDbPinStateActions(dbEntry);
})) {
Expand All @@ -102,6 +106,7 @@ void ProcessDirectoryJob::process()
}

for (auto &e : _localNormalQueryEntries) {
e.name = e.name.normalized(QString::NormalizationForm_C);
entries[e.name].localEntry = e;
}
if (isVfsWithSuffix()) {
Expand Down Expand Up @@ -140,8 +145,16 @@ void ProcessDirectoryJob::process()
for (const auto &f : entries) {
const auto &e = f.second;

PathTuple path;
path = _currentFolder.addName(e.nameOverride.isEmpty() ? f.first : e.nameOverride);
QString name;
if (!e.nameOverride.isEmpty()) {
name = e.nameOverride;
} else if (!e.serverEntry.name.isEmpty()) {
// IF there is a name on the server, take that, as it hasn't been normalized.
name = e.serverEntry.name;
} else {
name = f.first;
}
PathTuple path = _currentFolder.addName(name);

if (isVfsWithSuffix()) {
// Without suffix vfs the paths would be good. But since the dbEntry and localEntry
Expand Down
Loading