2012-12-05 21:30:40 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Markus Goetz <markus@woboq.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.
|
2012-12-05 21:30:40 +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"
|
|
|
|
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "folder.h"
|
|
|
|
#include "folderwatcher.h"
|
|
|
|
#include "folderwatcher_mac.h"
|
2012-12-05 21:30:40 +04:00
|
|
|
|
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
|
#include <QStringList>
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2012-12-05 21:30:40 +04:00
|
|
|
|
2014-01-13 19:00:41 +04:00
|
|
|
FolderWatcherPrivate::FolderWatcherPrivate(FolderWatcher *p, const QString& path)
|
|
|
|
: _parent(p),
|
|
|
|
_folder(path)
|
2012-12-05 21:30:40 +04:00
|
|
|
{
|
|
|
|
this->startWatching();
|
|
|
|
}
|
|
|
|
|
|
|
|
FolderWatcherPrivate::~FolderWatcherPrivate()
|
|
|
|
{
|
2014-02-19 03:46:48 +04:00
|
|
|
FSEventStreamStop(_stream);
|
|
|
|
FSEventStreamInvalidate(_stream);
|
|
|
|
FSEventStreamRelease(_stream);
|
2012-12-05 21:30:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void callback(
|
|
|
|
ConstFSEventStreamRef streamRef,
|
|
|
|
void *clientCallBackInfo,
|
|
|
|
size_t numEvents,
|
2014-02-18 03:36:41 +04:00
|
|
|
void *eventPathsVoid,
|
2012-12-05 21:30:40 +04:00
|
|
|
const FSEventStreamEventFlags eventFlags[],
|
|
|
|
const FSEventStreamEventId eventIds[])
|
|
|
|
{
|
2015-06-16 17:37:04 +03:00
|
|
|
Q_UNUSED(streamRef)
|
|
|
|
Q_UNUSED(eventFlags)
|
|
|
|
Q_UNUSED(eventIds)
|
|
|
|
|
2016-06-02 19:12:47 +03:00
|
|
|
const FSEventStreamEventFlags c_interestingFlags
|
|
|
|
= kFSEventStreamEventFlagItemCreated // for new folder/file
|
|
|
|
| kFSEventStreamEventFlagItemRemoved // for rm
|
|
|
|
| kFSEventStreamEventFlagItemInodeMetaMod // for mtime change
|
|
|
|
| kFSEventStreamEventFlagItemRenamed // also coming for moves to trash in finder
|
|
|
|
| kFSEventStreamEventFlagItemModified; // for content change
|
|
|
|
//We ignore other flags, e.g. for owner change, xattr change, Finder label change etc
|
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcFolderWatcher) << "FolderWatcherPrivate::callback by OS X";
|
2014-02-18 03:36:41 +04:00
|
|
|
|
|
|
|
QStringList paths;
|
|
|
|
CFArrayRef eventPaths = (CFArrayRef)eventPathsVoid;
|
2015-06-16 17:37:04 +03:00
|
|
|
for (int i = 0; i < static_cast<int>(numEvents); ++i) {
|
2014-02-18 03:36:41 +04:00
|
|
|
CFStringRef path = reinterpret_cast<CFStringRef>(CFArrayGetValueAtIndex(eventPaths, i));
|
|
|
|
|
|
|
|
QString qstring;
|
|
|
|
CFIndex pathLength = CFStringGetLength(path);
|
|
|
|
qstring.resize(pathLength);
|
|
|
|
CFStringGetCharacters(path, CFRangeMake(0, pathLength), reinterpret_cast<UniChar *>(qstring.data()));
|
2016-06-02 19:12:47 +03:00
|
|
|
QString fn = qstring.normalized(QString::NormalizationForm_C);
|
|
|
|
|
|
|
|
if (!(eventFlags[i] & c_interestingFlags)) {
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcFolderWatcher) << "Ignoring non-content changes for" << fn;
|
2016-06-02 19:12:47 +03:00
|
|
|
continue;
|
|
|
|
}
|
2014-02-18 03:36:41 +04:00
|
|
|
|
2016-06-02 19:12:47 +03:00
|
|
|
paths.append(fn);
|
2014-02-18 03:36:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
reinterpret_cast<FolderWatcherPrivate*>(clientCallBackInfo)->doNotifyParent(paths);
|
2012-12-05 21:30:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FolderWatcherPrivate::startWatching()
|
|
|
|
{
|
2017-05-09 15:24:11 +03:00
|
|
|
qCDebug(lcFolderWatcher) << "FolderWatcherPrivate::startWatching()" << _folder;
|
2014-01-13 19:00:41 +04:00
|
|
|
CFStringRef folderCF = CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar *>(_folder.unicode()),
|
|
|
|
_folder.length());
|
2012-12-05 21:30:40 +04:00
|
|
|
CFArrayRef pathsToWatch = CFStringCreateArrayBySeparatingStrings (NULL, folderCF, CFSTR(":"));
|
|
|
|
|
|
|
|
FSEventStreamContext ctx = {0, this, NULL, NULL, NULL};
|
|
|
|
|
|
|
|
// TODO: Add kFSEventStreamCreateFlagFileEvents ?
|
|
|
|
|
2014-02-19 03:46:48 +04:00
|
|
|
_stream = FSEventStreamCreate(NULL,
|
2012-12-05 21:30:40 +04:00
|
|
|
&callback,
|
|
|
|
&ctx,
|
|
|
|
pathsToWatch,
|
|
|
|
kFSEventStreamEventIdSinceNow,
|
|
|
|
0, // latency
|
2014-02-18 03:36:41 +04:00
|
|
|
kFSEventStreamCreateFlagUseCFTypes|kFSEventStreamCreateFlagFileEvents|kFSEventStreamCreateFlagIgnoreSelf
|
2012-12-05 21:30:40 +04:00
|
|
|
);
|
|
|
|
|
2014-02-18 01:35:54 +04:00
|
|
|
CFRelease(pathsToWatch);
|
2014-02-19 03:46:48 +04:00
|
|
|
FSEventStreamScheduleWithRunLoop(_stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
|
|
|
|
FSEventStreamStart(_stream);
|
2012-12-05 21:30:40 +04:00
|
|
|
}
|
|
|
|
|
2014-02-18 03:36:41 +04:00
|
|
|
void FolderWatcherPrivate::doNotifyParent(const QStringList &paths) {
|
|
|
|
|
|
|
|
_parent->changeDetected(paths);
|
2012-12-05 21:30:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // ns mirall
|