void FilesWatcher::HandleNotification (int descriptor)
{
char buffer [BufferLength_];
ssize_t length = read (descriptor, buffer, BufferLength_);
if (length < 0)
{
qDebug () << "read error";
return;
}
int i = 0;
QList<inotify_event*> eventsBuffer;
while (i < length)
{
struct inotify_event *event = reinterpret_cast<struct inotify_event*> (&buffer [i]);
QString path = WatchedPathes2Descriptors_.right.at (event->wd);
QString fullPath = path + "/" + QString (event->name);
//TODO if in exception list - ignore
if (event->mask & IN_CREATE)
{
if (event->mask & IN_ISDIR)
AddPathWithNotify (path);
else
emit fileWasCreated (fullPath);
}
else if (event->mask & IN_DELETE)
{
if (!(event->mask & IN_ISDIR))
emit fileWasRemoved (fullPath);
}
else if (event->mask & IN_MODIFY)
{
if (!(event->mask & IN_ISDIR))
emit fileWasUpdated (fullPath);
}
else if (event->mask & IN_MOVED_FROM)
{
eventsBuffer << event;
}
else if (event->mask & IN_MOVED_TO)
{
if (!eventsBuffer.isEmpty ())
{
for (auto e : eventsBuffer)
if (e->cookie == event->cookie &&
e->wd == event->wd)
{
emit entryWasRenamed (path + "/" + QString (e->name),
fullPath);
eventsBuffer.removeAll (e);
break;
}
else if (e->cookie == event->cookie)
{
QString oldPrePath = WatchedPathes2Descriptors_.right.at (e->wd);
emit entryWasMoved (oldPrePath + "/" + QString (e->name),
fullPath);
eventsBuffer.removeAll (e);
break;
}
}
else
AddPathWithNotify (fullPath);
}
else if (event->mask & IN_DELETE_SELF)
{
emit dirWasRemoved (path);
}
if (event->mask & IN_IGNORED)
{
inotify_rm_watch (INotifyDescriptor_, event->wd);
WatchedPathes2Descriptors_.right.erase (event->wd);
}
i += EventSize_ + event->len;
}
for (auto e : eventsBuffer)
{
QString path = WatchedPathes2Descriptors_.right.at (e->wd);
QString fullPath = path + "/" + QString (e->name);
if (e->mask & IN_ISDIR)
emit dirWasRemoved (path);
else
emit fileWasRemoved (fullPath);
}
}