Utils: avoid assert in FileWatcher

Remove a file from the watcher while handling the fileChanged signal can
lead into untracking the directory of the file before it got added to
the watcher, so make sure it is tracked before emitting fileChanged.

Change-Id: I63e80c20856af3d58ee23763180859f44f07b73d
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2023-10-12 15:10:29 +02:00
parent c324ef6321
commit f77635883b

View File

@@ -428,13 +428,11 @@ QStringList FileSystemWatcher::directories() const
void FileSystemWatcher::slotFileChanged(const QString &path)
{
const auto it = d->m_files.find(path);
QStringList toAdd;
if (it != d->m_files.end() && it.value().trigger(path)) {
qCDebug(fileSystemWatcherLog)
<< this << "triggers on file" << it.key()
<< it.value().watchMode
<< it.value().modifiedTime.toString(Qt::ISODate);
d->fileChanged(path);
QFileInfo fi(path);
if (!fi.exists()) {
@@ -443,12 +441,10 @@ void FileSystemWatcher::slotFileChanged(const QString &path)
Q_ASSERT(dirCount > 0);
if (dirCount == 1)
toAdd << directory;
d->m_staticData->m_watcher->addPath(directory);
}
d->fileChanged(path);
}
if (!toAdd.isEmpty())
d->m_staticData->m_watcher->addPaths(toAdd);
}
void FileSystemWatcher::slotDirectoryChanged(const QString &path)