track watched files and directories only in QFileSystemWatcherEngineUnix

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-12-21 15:14:10 +02:00
parent 668ccbb7c4
commit fa121a8f02
5 changed files with 20 additions and 34 deletions

View file

@ -159,7 +159,7 @@ void QFileSystemWatcher::addPaths(const QStringList &paths)
QStringList p = paths;
if (Q_LIKELY(d->watcher))
p = d->watcher->addPaths(paths, &d->files, &d->directories);
p = d->watcher->addPaths(paths);
if (Q_UNLIKELY(!p.isEmpty())) {
qWarning("QFileSystemWatcher: failed to add paths: %s",
@ -194,7 +194,7 @@ void QFileSystemWatcher::removePaths(const QStringList &paths)
}
Q_D(QFileSystemWatcher);
if (Q_LIKELY(d->watcher))
d->watcher->removePaths(paths, &d->files, &d->directories);
d->watcher->removePaths(paths);
}
/*!
@ -238,13 +238,13 @@ void QFileSystemWatcher::removePaths(const QStringList &paths)
QStringList QFileSystemWatcher::directories() const
{
Q_D(const QFileSystemWatcher);
return d->directories;
return d->watcher->directories.keys();
}
QStringList QFileSystemWatcher::files() const
{
Q_D(const QFileSystemWatcher);
return d->files;
return d->watcher->files.keys();
}
QT_END_NAMESPACE

View file

@ -52,7 +52,6 @@ public:
QFileSystemWatcherPrivate();
QFileSystemWatcherEngineUnix *watcher;
QStringList files, directories;
};
QT_END_NAMESPACE

View file

@ -36,50 +36,37 @@ QFileSystemWatcherEngineUnix::QFileSystemWatcherEngineUnix()
connect(&timer, SIGNAL(timeout()), this, SLOT(timeout()));
}
QStringList QFileSystemWatcherEngineUnix::addPaths(const QStringList &paths,
QStringList *files,
QStringList *directories)
QStringList QFileSystemWatcherEngineUnix::addPaths(const QStringList &paths)
{
QStringList p = paths;
foreach (const QString &path, paths) {
QStatInfo fi(path, true);
if (fi.isDir() || path.endsWith(QLatin1Char('/'))) {
if (!directories->contains(path))
directories->append(path);
if (!path.endsWith(QLatin1Char('/')))
fi = QStatInfo(path + QLatin1Char('/'), true);
this->directories.insert(path, fi);
directories.insert(path, fi);
} else {
if (!files->contains(path))
files->append(path);
this->files.insert(path, fi);
files.insert(path, fi);
}
p.removeAll(path);
}
if ((!this->files.isEmpty() ||
!this->directories.isEmpty()) &&
!timer.isActive()) {
if ((!files.isEmpty() || !directories.isEmpty()) && !timer.isActive()) {
timer.start(PollingInterval);
}
return p;
}
QStringList QFileSystemWatcherEngineUnix::removePaths(const QStringList &paths,
QStringList *files,
QStringList *directories)
QStringList QFileSystemWatcherEngineUnix::removePaths(const QStringList &paths)
{
QStringList p = paths;
foreach (const QString &path, paths) {
if (this->directories.remove(path)) {
directories->removeAll(path);
if (directories.remove(path)) {
p.removeAll(path);
} else if (this->files.remove(path)) {
files->removeAll(path);
} else if (files.remove(path)) {
p.removeAll(path);
}
}
if (this->files.isEmpty() &&
this->directories.isEmpty()) {
if (files.isEmpty() && directories.isEmpty()) {
timer.stop();
}
return p;

View file

@ -52,13 +52,13 @@ class QFileSystemWatcherEngineUnix : public QObject
{
Q_OBJECT
QHash<QString, QStatInfo> files, directories;
public:
QFileSystemWatcherEngineUnix();
QStringList addPaths(const QStringList &paths, QStringList *files, QStringList *directories);
QStringList removePaths(const QStringList &paths, QStringList *files, QStringList *directories);
QStringList addPaths(const QStringList &paths);
QStringList removePaths(const QStringList &paths);
QHash<QString, QStatInfo> files, directories;
Q_SIGNALS:
void fileChanged(const QString &path);

View file

@ -446,11 +446,11 @@ void tst_QFileSystemWatcher::nonExistingFileAndDirectory()
{
// Don't crash and watch for its creation
const QStringList nonexistingfiles = QStringList()
<< "file_that_does_not_exist.txt"
<< "foo/bar.txt";
<< "foo/bar.txt"
<< "file_that_does_not_exist.txt";
const QStringList nonexistingdirs = QStringList()
<< "dir_that_does_not_exist/"
<< "dir_foo/dir_bar/";
<< "dir_foo/dir_bar/"
<< "dir_that_does_not_exist/";
QFileSystemWatcher watcher;
watcher.addPaths(nonexistingfiles);
watcher.addPaths(nonexistingdirs);