Move fs-events code to own file

This commit is contained in:
Allan Odgaard
2013-02-01 12:10:15 +01:00
parent 32fcb52c68
commit 97eb13f4c9
3 changed files with 91 additions and 67 deletions

View File

@@ -0,0 +1,64 @@
#include "fs_events.h"
#include "scm.h"
#include <io/path.h>
#include <cf/cf.h>
namespace scm
{
// =============
// = watcher_t =
// =============
watcher_t::watcher_t (std::string const& path, info_t* info) : path(path), info(info), stream(NULL)
{
struct statfs buf;
if(statfs(path.c_str(), &buf) != 0)
return;
mount_point = buf.f_mntonname;
dev_t device = buf.f_fsid.val[0];
std::string devicePath = path::relative_to(path, mount_point);
FSEventStreamContext contextInfo = { 0, this, NULL, NULL, NULL };
if(stream = FSEventStreamCreateRelativeToDevice(kCFAllocatorDefault, &callback_function, &contextInfo, device, cf::wrap(std::vector<std::string>(1, devicePath)), kFSEventStreamEventIdSinceNow, 1.0, kFSEventStreamCreateFlagNone))
{
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
FSEventStreamFlushSync(stream);
}
else
{
fprintf(stderr, "cant observe %s\n", path.c_str());
}
}
watcher_t::~watcher_t ()
{
if(!stream)
return;
FSEventStreamStop(stream);
FSEventStreamInvalidate(stream);
FSEventStreamRelease(stream);
}
void watcher_t::callback (std::set<std::string> const& changedPaths)
{
info->callback(changedPaths);
}
void watcher_t::callback_function (ConstFSEventStreamRef streamRef, void* clientCallBackInfo, size_t numEvents, void* eventPaths, FSEventStreamEventFlags const eventFlags[], FSEventStreamEventId const eventIds[])
{
watcher_t& watcher = *(watcher_t*)clientCallBackInfo;
std::set<std::string> changedPaths;
for(size_t i = 0; i < numEvents; ++i)
{
std::string const& file = ((char const* const*)eventPaths)[i];
std::string const& path = path::join(watcher.mount_point, "./" + file);
changedPaths.insert(path);
}
watcher.callback(changedPaths);
}
}

View File

@@ -0,0 +1,26 @@
#ifndef FS_EVENTS_H_QDH73MIO
#define FS_EVENTS_H_QDH73MIO
namespace scm
{
struct info_t;
struct watcher_t
{
watcher_t (std::string const& path, info_t* info);
~watcher_t ();
private:
static void callback_function (ConstFSEventStreamRef streamRef, void* clientCallBackInfo, size_t numEvents, void* eventPaths, FSEventStreamEventFlags const eventFlags[], FSEventStreamEventId const eventIds[]);
void callback (std::set<std::string> const& changedPaths);
std::string path;
info_t* info;
std::string mount_point;
FSEventStreamRef stream;
};
} /* scm */
#endif /* end of include guard: FS_EVENTS_H_QDH73MIO */

View File

@@ -1,5 +1,6 @@
#include "drivers/api.h"
#include "scm.h"
#include "fs_events.h"
#include <io/path.h>
#include <cf/cf.h>
#include <oak/oak.h>
@@ -8,73 +9,6 @@
OAK_DEBUG_VAR(SCM);
namespace scm
{
static void callback_function (ConstFSEventStreamRef streamRef, void* clientCallBackInfo, size_t numEvents, void* eventPaths, FSEventStreamEventFlags const eventFlags[], FSEventStreamEventId const eventIds[]);
struct watcher_t
{
watcher_t (std::string const& path, info_t* info) : path(path), info(info), stream(NULL)
{
struct statfs buf;
if(statfs(path.c_str(), &buf) != 0)
return;
mount_point = buf.f_mntonname;
dev_t device = buf.f_fsid.val[0];
std::string devicePath = path::relative_to(path, mount_point);
FSEventStreamContext contextInfo = { 0, this, NULL, NULL, NULL };
if(stream = FSEventStreamCreateRelativeToDevice(kCFAllocatorDefault, &callback_function, &contextInfo, device, cf::wrap(std::vector<std::string>(1, devicePath)), kFSEventStreamEventIdSinceNow, 1.0, kFSEventStreamCreateFlagNone))
{
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
FSEventStreamFlushSync(stream);
}
else
{
fprintf(stderr, "cant observe %s\n", path.c_str());
}
}
void callback (std::set<std::string> const& changedPaths)
{
info->callback(changedPaths);
}
~watcher_t ()
{
if(!stream)
return;
FSEventStreamStop(stream);
FSEventStreamInvalidate(stream);
FSEventStreamRelease(stream);
}
std::string path;
info_t* info;
std::string mount_point;
FSEventStreamRef stream;
};
static void callback_function (ConstFSEventStreamRef streamRef, void* clientCallBackInfo, size_t numEvents, void* eventPaths, FSEventStreamEventFlags const eventFlags[], FSEventStreamEventId const eventIds[])
{
watcher_t& watcher = *(watcher_t*)clientCallBackInfo;
std::set<std::string> changedPaths;
for(size_t i = 0; i < numEvents; ++i)
{
std::string const& file = ((char const* const*)eventPaths)[i];
std::string const& path = path::join(watcher.mount_point, "./" + file);
changedPaths.insert(path);
}
watcher.callback(changedPaths);
}
}
namespace scm
{
static std::map<std::string, info_ptr>& cache () { static std::map<std::string, info_ptr> cache; return cache; }