mirror of
https://github.com/textmate/textmate.git
synced 2026-04-28 03:00:34 -04:00
Move fs-events code to own file
This commit is contained in:
64
Frameworks/scm/src/fs_events.cc
Normal file
64
Frameworks/scm/src/fs_events.cc
Normal 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, "can’t 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);
|
||||
}
|
||||
}
|
||||
26
Frameworks/scm/src/fs_events.h
Normal file
26
Frameworks/scm/src/fs_events.h
Normal 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 */
|
||||
@@ -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, "can’t 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; }
|
||||
|
||||
Reference in New Issue
Block a user