2014-01-23 16:16:08 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "folder.h"
|
|
|
|
#include "folderwatcher_linux.h"
|
2014-01-23 16:16:08 +04:00
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QObject>
|
2014-03-17 13:04:42 +04:00
|
|
|
#include <QVarLengthArray>
|
2014-01-23 16:16:08 +04:00
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2014-01-23 16:16:08 +04:00
|
|
|
|
|
|
|
FolderWatcherPrivate::FolderWatcherPrivate(FolderWatcher *p, const QString& path)
|
|
|
|
: QObject(),
|
|
|
|
_parent(p),
|
|
|
|
_folder(path)
|
|
|
|
{
|
|
|
|
_fd = inotify_init();
|
|
|
|
if (_fd != -1) {
|
|
|
|
_socket.reset( new QSocketNotifier(_fd, QSocketNotifier::Read) );
|
|
|
|
connect(_socket.data(), SIGNAL(activated(int)), SLOT(slotReceivedNotification(int)));
|
|
|
|
} else {
|
|
|
|
qDebug() << Q_FUNC_INFO << "notify_init() failed: " << strerror(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
QMetaObject::invokeMethod(this, "slotAddFolderRecursive", Q_ARG(QString, path));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
FolderWatcherPrivate::~FolderWatcherPrivate()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// attention: result list passed by reference!
|
|
|
|
bool FolderWatcherPrivate::findFoldersBelow( const QDir& dir, QStringList& fullList )
|
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
if( !(dir.exists() && dir.isReadable()) ) {
|
|
|
|
qDebug() << "Non existing path coming in: " << dir.absolutePath();
|
|
|
|
ok = false;
|
|
|
|
} else {
|
|
|
|
QStringList nameFilter;
|
|
|
|
nameFilter << QLatin1String("*");
|
|
|
|
QDir::Filters filter = QDir::Dirs | QDir::NoDotAndDotDot|QDir::NoSymLinks;
|
|
|
|
const QStringList pathes = dir.entryList(nameFilter, filter);
|
|
|
|
|
|
|
|
QStringList::const_iterator constIterator;
|
|
|
|
for (constIterator = pathes.constBegin(); constIterator != pathes.constEnd();
|
|
|
|
++constIterator) {
|
|
|
|
const QString fullPath(dir.path()+QLatin1String("/")+(*constIterator));
|
|
|
|
fullList.append(fullPath);
|
|
|
|
ok = findFoldersBelow(QDir(fullPath), fullList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FolderWatcherPrivate::inotifyRegisterPath(const QString& path)
|
|
|
|
{
|
|
|
|
if( !path.isEmpty()) {
|
|
|
|
int wd = inotify_add_watch(_fd, path.toUtf8().constData(),
|
|
|
|
IN_CLOSE_WRITE | IN_ATTRIB | IN_MOVE |
|
|
|
|
IN_CREATE |IN_DELETE | IN_DELETE_SELF |
|
2015-12-10 15:05:43 +03:00
|
|
|
IN_MOVE_SELF |IN_UNMOUNT |IN_ONLYDIR);
|
2014-01-23 16:16:08 +04:00
|
|
|
if( wd > -1 ) {
|
|
|
|
_watches.insert(wd, path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FolderWatcherPrivate::slotAddFolderRecursive(const QString &path)
|
|
|
|
{
|
|
|
|
int subdirs = 0;
|
|
|
|
qDebug() << "(+) Watcher:" << path;
|
|
|
|
|
|
|
|
QDir inPath(path);
|
|
|
|
inotifyRegisterPath(inPath.absolutePath());
|
|
|
|
|
|
|
|
const QStringList watchedFolders = _watches.values();
|
|
|
|
|
|
|
|
QStringList allSubfolders;
|
|
|
|
if( !findFoldersBelow(QDir(path), allSubfolders)) {
|
|
|
|
qDebug() << "Could not traverse all sub folders";
|
|
|
|
}
|
|
|
|
// qDebug() << "currently watching " << watchedFolders;
|
|
|
|
QStringListIterator subfoldersIt(allSubfolders);
|
|
|
|
while (subfoldersIt.hasNext()) {
|
|
|
|
QString subfolder = subfoldersIt.next();
|
|
|
|
// qDebug() << " (**) subfolder: " << subfolder;
|
|
|
|
QDir folder (subfolder);
|
|
|
|
if (folder.exists() && !watchedFolders.contains(folder.absolutePath())) {
|
|
|
|
subdirs++;
|
|
|
|
if( _parent->pathIsIgnored(subfolder) ) {
|
|
|
|
qDebug() << "* Not adding" << folder.path();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
inotifyRegisterPath(folder.absolutePath());
|
|
|
|
} else {
|
|
|
|
qDebug() << " `-> discarded:" << folder.path();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subdirs >0) {
|
|
|
|
qDebug() << " `-> and" << subdirs << "subdirectories";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FolderWatcherPrivate::slotReceivedNotification(int fd)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
struct inotify_event* event;
|
|
|
|
int i;
|
|
|
|
int error;
|
2014-03-17 13:04:42 +04:00
|
|
|
QVarLengthArray<char, 2048> buffer(2048);
|
2014-01-23 16:16:08 +04:00
|
|
|
|
|
|
|
do {
|
2014-03-17 13:04:42 +04:00
|
|
|
len = read(fd, buffer.data(), buffer.size());
|
2014-01-23 16:16:08 +04:00
|
|
|
error = errno;
|
|
|
|
/**
|
|
|
|
* From inotify documentation:
|
|
|
|
*
|
|
|
|
* The behavior when the buffer given to read(2) is too
|
|
|
|
* small to return information about the next event
|
|
|
|
* depends on the kernel version: in kernels before 2.6.21,
|
|
|
|
* read(2) returns 0; since kernel 2.6.21, read(2) fails with
|
|
|
|
* the error EINVAL.
|
|
|
|
*/
|
|
|
|
if (len < 0 && error == EINVAL)
|
|
|
|
{
|
|
|
|
// double the buffer size
|
2014-03-17 13:04:42 +04:00
|
|
|
buffer.resize(buffer.size() * 2);
|
2014-01-23 16:16:08 +04:00
|
|
|
/* and try again ... */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} while (false);
|
|
|
|
|
|
|
|
// reset counter
|
|
|
|
i = 0;
|
|
|
|
// while there are enough events in the buffer
|
|
|
|
while(i + sizeof(struct inotify_event) < static_cast<unsigned int>(len)) {
|
|
|
|
// cast an inotify_event
|
|
|
|
event = (struct inotify_event*)&buffer[i];
|
|
|
|
if (event == NULL) {
|
|
|
|
qDebug() << "NULL event";
|
|
|
|
i += sizeof(struct inotify_event);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-07 12:14:09 +03:00
|
|
|
// Fire event for the path that was changed.
|
2014-01-23 16:16:08 +04:00
|
|
|
if (event->len > 0 && event->wd > -1) {
|
2014-11-07 12:14:09 +03:00
|
|
|
QByteArray fileName(event->name);
|
2014-04-08 15:05:51 +04:00
|
|
|
// qDebug() << Q_FUNC_INFO << event->name;
|
2014-11-07 12:14:09 +03:00
|
|
|
if (fileName.startsWith(".csync_journal.db") ||
|
|
|
|
fileName.startsWith(".owncloudsync.log")) {
|
2014-04-08 15:05:51 +04:00
|
|
|
// qDebug() << "ignore journal";
|
2014-02-04 19:45:43 +04:00
|
|
|
} else {
|
2014-11-07 12:14:09 +03:00
|
|
|
const QString p = _watches[event->wd] + '/' + fileName;
|
|
|
|
//qDebug() << "found a change in " << p;
|
2014-02-04 19:45:43 +04:00
|
|
|
_parent->changeDetected(p);
|
|
|
|
}
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// increment counter
|
|
|
|
i += sizeof(struct inotify_event) + event->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FolderWatcherPrivate::addPath(const QString& path)
|
|
|
|
{
|
|
|
|
slotAddFolderRecursive(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FolderWatcherPrivate::removePath(const QString& path)
|
|
|
|
{
|
|
|
|
int wid = -1;
|
|
|
|
// Remove the inotify watch.
|
|
|
|
QHash<int, QString>::const_iterator i = _watches.constBegin();
|
|
|
|
|
|
|
|
while (i != _watches.constEnd()) {
|
|
|
|
if( i.value() == path ) {
|
|
|
|
wid = i.key();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if( wid > -1 ) {
|
|
|
|
inotify_rm_watch(_fd, wid);
|
|
|
|
_watches.remove(wid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // ns mirall
|