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
|
2016-10-25 12:00:07 +03:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-01-23 16:16:08 +04:00
|
|
|
*
|
|
|
|
* 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 <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));
|
2017-09-20 11:14:48 +03:00
|
|
|
connect(_socket.data(), &QSocketNotifier::activated, this, &FolderWatcherPrivate::slotReceivedNotification);
|
2014-01-23 16:16:08 +04:00
|
|
|
} else {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcFolderWatcher) << "notify_init() failed: " << strerror(errno);
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QMetaObject::invokeMethod(this, "slotAddFolderRecursive", Q_ARG(QString, path));
|
|
|
|
}
|
|
|
|
|
2020-05-25 22:33:24 +03:00
|
|
|
FolderWatcherPrivate::~FolderWatcherPrivate() = default;
|
2014-01-23 16:16:08 +04:00
|
|
|
|
|
|
|
// attention: result list passed by reference!
|
|
|
|
bool FolderWatcherPrivate::findFoldersBelow(const QDir &dir, QStringList &fullList)
|
|
|
|
{
|
|
|
|
bool ok = true;
|
|
|
|
if (!(dir.exists() && dir.isReadable())) {
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcFolderWatcher) << "Non existing path coming in: " << dir.absolutePath();
|
2014-01-23 16:16:08 +04:00
|
|
|
ok = false;
|
|
|
|
} else {
|
|
|
|
QStringList nameFilter;
|
|
|
|
nameFilter << QLatin1String("*");
|
2016-04-12 12:49:52 +03:00
|
|
|
QDir::Filters filter = QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::Hidden;
|
2023-06-02 15:05:33 +03:00
|
|
|
const QStringList paths = dir.entryList(nameFilter, filter);
|
2014-01-23 16:16:08 +04:00
|
|
|
|
|
|
|
QStringList::const_iterator constIterator;
|
2023-06-02 15:05:33 +03:00
|
|
|
for (constIterator = paths.constBegin(); constIterator != paths.constEnd();
|
2014-01-23 16:16:08 +04:00
|
|
|
++constIterator) {
|
|
|
|
const QString fullPath(dir.path() + QLatin1String("/") + (*constIterator));
|
|
|
|
fullList.append(fullPath);
|
|
|
|
ok = findFoldersBelow(QDir(fullPath), fullList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FolderWatcherPrivate::inotifyRegisterPath(const QString &path)
|
|
|
|
{
|
2019-03-05 15:20:09 +03:00
|
|
|
if (path.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
int wd = inotify_add_watch(_fd, path.toUtf8().constData(),
|
|
|
|
IN_CLOSE_WRITE | IN_ATTRIB | IN_MOVE | IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF | IN_UNMOUNT | IN_ONLYDIR);
|
|
|
|
if (wd > -1) {
|
|
|
|
_watchToPath.insert(wd, path);
|
|
|
|
_pathToWatch.insert(path, wd);
|
|
|
|
} else {
|
|
|
|
// If we're running out of memory or inotify watches, become
|
|
|
|
// unreliable.
|
|
|
|
if (_parent->_isReliable && (errno == ENOMEM || errno == ENOSPC)) {
|
|
|
|
_parent->_isReliable = false;
|
|
|
|
emit _parent->becameUnreliable(
|
|
|
|
tr("This problem usually happens when the inotify watches are exhausted. "
|
|
|
|
"Check the FAQ for details."));
|
2018-10-12 12:03:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-23 16:16:08 +04:00
|
|
|
void FolderWatcherPrivate::slotAddFolderRecursive(const QString &path)
|
|
|
|
{
|
2019-03-05 15:20:09 +03:00
|
|
|
if (_pathToWatch.contains(path))
|
|
|
|
return;
|
|
|
|
|
2014-01-23 16:16:08 +04:00
|
|
|
int subdirs = 0;
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcFolderWatcher) << "(+) Watcher:" << path;
|
2014-01-23 16:16:08 +04:00
|
|
|
|
|
|
|
QDir inPath(path);
|
|
|
|
inotifyRegisterPath(inPath.absolutePath());
|
|
|
|
|
|
|
|
QStringList allSubfolders;
|
|
|
|
if (!findFoldersBelow(QDir(path), allSubfolders)) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcFolderWatcher) << "Could not traverse all sub folders";
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
QStringListIterator subfoldersIt(allSubfolders);
|
|
|
|
while (subfoldersIt.hasNext()) {
|
|
|
|
QString subfolder = subfoldersIt.next();
|
|
|
|
QDir folder(subfolder);
|
2019-03-05 15:20:09 +03:00
|
|
|
if (folder.exists() && !_pathToWatch.contains(folder.absolutePath())) {
|
2014-01-23 16:16:08 +04:00
|
|
|
subdirs++;
|
|
|
|
if (_parent->pathIsIgnored(subfolder)) {
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcFolderWatcher) << "* Not adding" << folder.path();
|
2014-01-23 16:16:08 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
inotifyRegisterPath(folder.absolutePath());
|
|
|
|
} else {
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcFolderWatcher) << " `-> discarded:" << folder.path();
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subdirs > 0) {
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcFolderWatcher) << " `-> and" << subdirs << "subdirectories";
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FolderWatcherPrivate::slotReceivedNotification(int fd)
|
|
|
|
{
|
2020-05-29 16:07:05 +03:00
|
|
|
int len = 0;
|
|
|
|
struct inotify_event *event = nullptr;
|
2020-08-18 22:46:30 +03:00
|
|
|
size_t i = 0;
|
2020-05-29 16:07:05 +03:00
|
|
|
int error = 0;
|
2014-03-17 13:04:42 +04:00
|
|
|
QVarLengthArray<char, 2048> buffer(2048);
|
2014-01-23 16:16:08 +04:00
|
|
|
|
2020-08-18 22:58:51 +03:00
|
|
|
len = read(fd, buffer.data(), buffer.size());
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
while (len < 0 && error == EINVAL) {
|
|
|
|
// double the buffer size
|
|
|
|
buffer.resize(buffer.size() * 2);
|
|
|
|
|
|
|
|
/* and try again ... */
|
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;
|
2020-08-18 22:58:51 +03:00
|
|
|
}
|
2014-01-23 16:16:08 +04:00
|
|
|
|
2018-10-12 12:03:10 +03:00
|
|
|
// iterate events in buffer
|
|
|
|
unsigned int ulen = len;
|
|
|
|
for (i = 0; i + sizeof(inotify_event) < ulen; i += sizeof(inotify_event) + (event ? event->len : 0)) {
|
2014-01-23 16:16:08 +04:00
|
|
|
// cast an inotify_event
|
|
|
|
event = (struct inotify_event *)&buffer[i];
|
2020-06-10 04:47:49 +03:00
|
|
|
if (!event) {
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcFolderWatcher) << "NULL event";
|
2014-01-23 16:16:08 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-07 12:14:09 +03:00
|
|
|
// Fire event for the path that was changed.
|
2018-10-12 12:03:10 +03:00
|
|
|
if (event->len == 0 || event->wd <= -1)
|
|
|
|
continue;
|
|
|
|
QByteArray fileName(event->name);
|
2019-06-13 10:59:01 +03:00
|
|
|
// Filter out journal changes - redundant with filtering in
|
|
|
|
// FolderWatcher::pathIsIgnored.
|
2018-10-12 12:03:10 +03:00
|
|
|
if (fileName.startsWith("._sync_")
|
|
|
|
|| fileName.startsWith(".csync_journal.db")
|
|
|
|
|| fileName.startsWith(".sync_")) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-05 15:20:09 +03:00
|
|
|
const QString p = _watchToPath[event->wd] + '/' + fileName;
|
2018-10-12 12:03:10 +03:00
|
|
|
_parent->changeDetected(p);
|
|
|
|
|
2019-03-05 15:20:09 +03:00
|
|
|
if ((event->mask & (IN_MOVED_TO | IN_CREATE))
|
|
|
|
&& QFileInfo(p).isDir()
|
|
|
|
&& !_parent->pathIsIgnored(p)) {
|
|
|
|
slotAddFolderRecursive(p);
|
|
|
|
}
|
|
|
|
if (event->mask & (IN_MOVED_FROM | IN_DELETE)) {
|
|
|
|
removeFoldersBelow(p);
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 15:20:09 +03:00
|
|
|
void FolderWatcherPrivate::removeFoldersBelow(const QString &path)
|
2014-01-23 16:16:08 +04:00
|
|
|
{
|
2019-03-05 15:20:09 +03:00
|
|
|
auto it = _pathToWatch.find(path);
|
|
|
|
if (it == _pathToWatch.end())
|
|
|
|
return;
|
2014-01-23 16:16:08 +04:00
|
|
|
|
2019-03-05 15:20:09 +03:00
|
|
|
QString pathSlash = path + '/';
|
2014-01-23 16:16:08 +04:00
|
|
|
|
2019-03-05 15:20:09 +03:00
|
|
|
// Remove the entry and all subentries
|
|
|
|
while (it != _pathToWatch.end()) {
|
|
|
|
auto itPath = it.key();
|
|
|
|
if (!itPath.startsWith(path))
|
2014-01-23 16:16:08 +04:00
|
|
|
break;
|
2019-03-05 15:20:09 +03:00
|
|
|
if (itPath != path && !itPath.startsWith(pathSlash)) {
|
|
|
|
// order is 'foo', 'foo bar', 'foo/bar'
|
|
|
|
++it;
|
|
|
|
continue;
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
2019-03-05 15:20:09 +03:00
|
|
|
|
|
|
|
auto wid = it.value();
|
2014-01-23 16:16:08 +04:00
|
|
|
inotify_rm_watch(_fd, wid);
|
2019-03-05 15:20:09 +03:00
|
|
|
_watchToPath.remove(wid);
|
|
|
|
it = _pathToWatch.erase(it);
|
|
|
|
qCDebug(lcFolderWatcher) << "Removed watch for" << itPath;
|
2014-01-23 16:16:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // ns mirall
|