Folder watcher now reports changing paths instead of dirs.

This commit is contained in:
Christian Kamm 2014-11-07 10:53:41 +01:00
parent 1ada20ac7b
commit 6d09f1b6c0
5 changed files with 47 additions and 42 deletions

View file

@ -140,15 +140,15 @@ void FolderMan::registerFolderMonitor( Folder *folder )
fw->addIgnoreListFile( cfg.excludeFile(MirallConfigFile::SystemScope) ); fw->addIgnoreListFile( cfg.excludeFile(MirallConfigFile::SystemScope) );
fw->addIgnoreListFile( cfg.excludeFile(MirallConfigFile::UserScope) ); fw->addIgnoreListFile( cfg.excludeFile(MirallConfigFile::UserScope) );
// Connect the folderChanged signal, which comes with the changed path, // Connect the pathChanged signal, which comes with the changed path,
// to the signal mapper which maps to the folder alias. The changed path // to the signal mapper which maps to the folder alias. The changed path
// is lost this way, but we do not need it for the current implementation. // is lost this way, but we do not need it for the current implementation.
connect(fw, SIGNAL(folderChanged(QString)), _folderWatcherSignalMapper, SLOT(map())); connect(fw, SIGNAL(pathChanged(QString)), _folderWatcherSignalMapper, SLOT(map()));
_folderWatcherSignalMapper->setMapping(fw, folder->alias()); _folderWatcherSignalMapper->setMapping(fw, folder->alias());
_folderWatchers.insert(folder->alias(), fw); _folderWatchers.insert(folder->alias(), fw);
// This is at the moment only for the behaviour of the SocketApi. // This is at the moment only for the behaviour of the SocketApi.
connect(fw, SIGNAL(folderChanged(QString)), folder, SLOT(watcherSlot(QString))); connect(fw, SIGNAL(pathChanged(QString)), folder, SLOT(watcherSlot(QString)));
} }
// register the folder with the socket API // register the folder with the socket API

View file

@ -130,7 +130,7 @@ void FolderWatcher::changeDetected( const QStringList& paths )
_lastPaths = pathsSet; _lastPaths = pathsSet;
_timer.restart(); _timer.restart();
QSet<QString> changedFolders; QSet<QString> changedPaths;
// ------- handle ignores: // ------- handle ignores:
for (int i = 0; i < paths.size(); ++i) { for (int i = 0; i < paths.size(); ++i) {
@ -139,20 +139,15 @@ void FolderWatcher::changeDetected( const QStringList& paths )
continue; continue;
} }
QFileInfo fi(path); changedPaths.insert(path);
if (fi.isDir()) {
changedFolders.insert(path);
} else {
changedFolders.insert(fi.dir().path());
}
} }
if (changedFolders.isEmpty()) { if (changedPaths.isEmpty()) {
return; return;
} }
qDebug() << "detected changes in folders:" << changedFolders; qDebug() << "detected changes in paths:" << changedPaths;
foreach (const QString &path, changedFolders) { foreach (const QString &path, changedPaths) {
emit folderChanged(path); emit pathChanged(path);
} }
} }

View file

@ -34,7 +34,7 @@ class FolderWatcherPrivate;
/* /*
* Folder Watcher monitors a directory and its sub directories * Folder Watcher monitors a directory and its sub directories
* for changes in the local file system. Changes are signalled * for changes in the local file system. Changes are signalled
* through the folderChanged() signal. * through the pathChanged() signal.
* *
* Note that if new folders are created, this folderwatcher class * Note that if new folders are created, this folderwatcher class
* does not automatically adds them to the list of monitored * does not automatically adds them to the list of monitored
@ -74,8 +74,9 @@ public:
bool pathIsIgnored( const QString& path ); bool pathIsIgnored( const QString& path );
signals: signals:
/** Emitted when one of the paths is changed */ /** Emitted when one of the watched directories or one
void folderChanged(const QString &path); * of the contained files is changed. */
void pathChanged(const QString &path);
/** Emitted if an error occurs */ /** Emitted if an error occurs */
void error(const QString& error); void error(const QString& error);

View file

@ -159,7 +159,6 @@ void FolderWatcherPrivate::slotReceivedNotification(int fd)
while(i + sizeof(struct inotify_event) < static_cast<unsigned int>(len)) { while(i + sizeof(struct inotify_event) < static_cast<unsigned int>(len)) {
// cast an inotify_event // cast an inotify_event
event = (struct inotify_event*)&buffer[i]; event = (struct inotify_event*)&buffer[i];
// with the help of watch descriptor, retrieve, corresponding INotify
if (event == NULL) { if (event == NULL) {
qDebug() << "NULL event"; qDebug() << "NULL event";
i += sizeof(struct inotify_event); i += sizeof(struct inotify_event);

View file

@ -73,7 +73,7 @@ private slots:
Utility::writeRandomFile( _root+"/a1/movefile"); Utility::writeRandomFile( _root+"/a1/movefile");
_watcher = new FolderWatcher(_root); _watcher = new FolderWatcher(_root);
QObject::connect(_watcher, SIGNAL(folderChanged(QString)), this, SLOT(slotFolderChanged(QString))); QObject::connect(_watcher, SIGNAL(pathChanged(QString)), this, SLOT(slotFolderChanged(QString)));
_timer.singleShot(5000, this, SLOT(slotEnd())); _timer.singleShot(5000, this, SLOT(slotEnd()));
} }
@ -91,9 +91,10 @@ private slots:
} }
void testACreate() { // create a new file void testACreate() { // create a new file
QString file(_root + "/foo.txt");
QString cmd; QString cmd;
_requiredNotifications.insert(_root); _requiredNotifications.insert(file);
cmd = QString("echo \"xyz\" > %1/foo.txt").arg(_root); cmd = QString("echo \"xyz\" > %1").arg(file);
qDebug() << "Command: " << cmd; qDebug() << "Command: " << cmd;
system(cmd.toLocal8Bit()); system(cmd.toLocal8Bit());
@ -101,12 +102,13 @@ private slots:
} }
void testATouch() { // touch an existing file. void testATouch() { // touch an existing file.
_requiredNotifications.insert(_root+"/a1"); QString file(_root + "/a1/random.bin");
_requiredNotifications.insert(file);
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
Utility::writeRandomFile(QString("%1/a1/random.bin").arg(_root)); Utility::writeRandomFile(QString("%1/a1/random.bin").arg(_root));
#else #else
QString cmd; QString cmd;
cmd = QString("/usr/bin/touch %1/a1/random.bin").arg(_root); cmd = QString("/usr/bin/touch %1").arg(file);
qDebug() << "Command: " << cmd; qDebug() << "Command: " << cmd;
system(cmd.toLocal8Bit()); system(cmd.toLocal8Bit());
#endif #endif
@ -115,47 +117,55 @@ private slots:
} }
void testCreateADir() { void testCreateADir() {
_requiredNotifications.insert(_root+"/a1/b1"); QString file(_root+"/a1/b1/new_dir");
_skipNotifications.insert(_root + "/a1/b1/new_dir"); _requiredNotifications.insert(file);
//_skipNotifications.insert(_root + "/a1/b1/new_dir");
QDir dir; QDir dir;
dir.mkdir( _root + "/a1/b1/new_dir"); dir.mkdir(file);
QVERIFY(QFile::exists(_root + "/a1/b1/new_dir")); QVERIFY(QFile::exists(file));
checkNotifications(); checkNotifications();
} }
void testRemoveADir() { void testRemoveADir() {
_requiredNotifications.insert(_root+"/a1/b3"); QString file(_root+"/a1/b3/c3");
_requiredNotifications.insert(file);
QDir dir; QDir dir;
QVERIFY(dir.rmdir(_root+"/a1/b3/c3")); QVERIFY(dir.rmdir(file));
checkNotifications(); checkNotifications();
} }
void testRemoveAFile() { void testRemoveAFile() {
_requiredNotifications.insert(_root+"/a1/b2"); QString file(_root+"/a1/b2/todelete.bin");
QVERIFY(QFile::exists(_root+"/a1/b2/todelete.bin")); _requiredNotifications.insert(file);
QFile::remove(_root+"/a1/b2/todelete.bin"); QVERIFY(QFile::exists(file));
QVERIFY(!QFile::exists(_root+"/a1/b2/todelete.bin")); QFile::remove(file);
QVERIFY(!QFile::exists(file));
checkNotifications(); checkNotifications();
} }
void testRenameAFile() { void testRenameAFile() {
_requiredNotifications.insert(_root+"/a2"); QString file1(_root+"/a2/renamefile");
QVERIFY(QFile::exists(_root+"/a2/renamefile")); QString file2(_root+"/a2/renamefile.renamed");
QFile::rename(_root+"/a2/renamefile", _root+"/a2/renamefile.renamed" ); _requiredNotifications.insert(file1);
QVERIFY(QFile::exists(_root+"/a2/renamefile.renamed")); _requiredNotifications.insert(file2);
QVERIFY(QFile::exists(file1));
QFile::rename(file1, file2);
QVERIFY(QFile::exists(file2));
checkNotifications(); checkNotifications();
} }
void testMoveAFile() { void testMoveAFile() {
_requiredNotifications.insert(_root+"/a1"); QString old_file(_root+"/a1/movefile");
_requiredNotifications.insert(_root+"/a2"); QString new_file(_root+"/a2/movefile.renamed");
QVERIFY(QFile::exists(_root+"/a1/movefile")); _requiredNotifications.insert(old_file);
QFile::rename(_root+"/a1/movefile", _root+"/a2/movefile.renamed" ); _requiredNotifications.insert(new_file);
QVERIFY(QFile::exists(_root+"/a2/movefile.renamed")); QVERIFY(QFile::exists(old_file));
QFile::rename(old_file, new_file);
QVERIFY(QFile::exists(new_file));
checkNotifications(); checkNotifications();
} }