Skip to content

Commit

Permalink
Write and read snapshot file from C++ instead of JS
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Mar 15, 2019
1 parent 653f2a0 commit 0b99729
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
9 changes: 3 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ let dir = process.cwd();

async function run() {
try {
let token = fs.readFileSync(dir + '/token.txt', 'utf8');
let changes = await fschanges.getEventsSince(dir, token);
let changes = await fschanges.getEventsSince(dir, dir + '/token.txt');
console.log(changes);
} catch (err) {
console.log(err)
}

let token = await fschanges.getCurrentToken(dir);
console.log('token', token)
fs.writeFileSync(dir + '/token.txt', token);
await fschanges.writeSnapshot(dir, dir + '/token.txt');
}

run();
run();
28 changes: 14 additions & 14 deletions src/FSChanges.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@

using namespace v8;

std::string getCurrentTokenImpl(std::string *dir);
EventList *getEventsSinceImpl(std::string *dir, std::string *token);
void writeSnapshotImpl(std::string *dir, std::string *snapshotPath);
EventList *getEventsSinceImpl(std::string *dir, std::string *snapshotPath);

struct AsyncRequest {
uv_work_t work;
std::string directory;
std::string token;
std::string snapshotPath;
EventList *events;
Nan::Persistent<Promise::Resolver> *resolver;

AsyncRequest(Local<Value> d, Local<Value> cb, Local<Promise::Resolver> r) {
AsyncRequest(Local<Value> d, Local<Value> s, Local<Promise::Resolver> r) {
work.data = (void *)this;

// copy the string since the JS garbage collector might run before the async request is finished
Nan::Utf8String dir(d);
Nan::Utf8String sp(s);
directory = std::string(*dir);
snapshotPath = std::string(*sp);
events = NULL;

resolver = new Nan::Persistent<Promise::Resolver>(r);
Expand All @@ -45,32 +47,32 @@ void asyncCallback(uv_work_t *work) {
if (req->events) {
result = req->events->toJS();
} else {
result = Nan::New<String>(req->token).ToLocalChecked();
result = Nan::Null();
}

auto resolver = Nan::New(*req->resolver);
resolver->Resolve(result);
delete req;
}

void getCurrentTokenAsync(uv_work_t *work) {
void writeSnapshotAsync(uv_work_t *work) {
AsyncRequest *req = (AsyncRequest *) work->data;
req->token = getCurrentTokenImpl(&req->directory);
writeSnapshotImpl(&req->directory, &req->snapshotPath);
}

void getEventsSinceAsync(uv_work_t *work) {
AsyncRequest *req = (AsyncRequest *) work->data;
req->events = getEventsSinceImpl(&req->directory, &req->token);
req->events = getEventsSinceImpl(&req->directory, &req->snapshotPath);
}

NAN_METHOD(getCurrentToken) {
NAN_METHOD(writeSnapshot) {
if (info.Length() < 1 || !info[0]->IsString()) {
return Nan::ThrowTypeError("Expected a string");
}

auto resolver = Promise::Resolver::New(info.GetIsolate());
AsyncRequest *req = new AsyncRequest(info[0], info[1], resolver);
uv_queue_work(uv_default_loop(), &req->work, getCurrentTokenAsync, (uv_after_work_cb) asyncCallback);
uv_queue_work(uv_default_loop(), &req->work, writeSnapshotAsync, (uv_after_work_cb) asyncCallback);

info.GetReturnValue().Set(resolver->GetPromise());
}
Expand All @@ -85,16 +87,14 @@ NAN_METHOD(getEventsSince) {
}

auto resolver = Promise::Resolver::New(info.GetIsolate());
AsyncRequest *req = new AsyncRequest(info[0], info[2], resolver);
Nan::Utf8String token(info[1]);
req->token = std::string(*token);
AsyncRequest *req = new AsyncRequest(info[0], info[1], resolver);
uv_queue_work(uv_default_loop(), &req->work, getEventsSinceAsync, (uv_after_work_cb) asyncCallback);

info.GetReturnValue().Set(resolver->GetPromise());
}

NAN_MODULE_INIT(Init) {
Nan::Export(target, "getCurrentToken", getCurrentToken);
Nan::Export(target, "writeSnapshot", writeSnapshot);
Nan::Export(target, "getEventsSince", getEventsSince);
}

Expand Down
20 changes: 14 additions & 6 deletions src/macos/FSEvents.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <CoreServices/CoreServices.h>
#include <string>
#include <fstream>
#include "../Event.hh"

std::string getCurrentTokenImpl(std::string *dir) {
void writeSnapshotImpl(std::string *dir, std::string *snapshotPath) {
FSEventStreamEventId id = FSEventsGetCurrentEventId();
return std::to_string(id);
std::ofstream ofs(*snapshotPath);
ofs << id;
}

void FSEventsCallback(
Expand Down Expand Up @@ -55,8 +57,16 @@ void FSEventsCallback(
}
}

EventList *getEventsSinceImpl(std::string *dir, std::string *token) {
FSEventStreamEventId id = std::stoull(*token);
EventList *getEventsSinceImpl(std::string *dir, std::string *snapshotPath) {
EventList *list = new EventList();

std::ifstream ifs(*snapshotPath);
if (ifs.fail()) {
return list;
}

FSEventStreamEventId id;
ifs >> id;

CFAbsoluteTime latency = 0.001;
CFStringRef fileWatchPath = CFStringCreateWithCString(
Expand All @@ -72,8 +82,6 @@ EventList *getEventsSinceImpl(std::string *dir, std::string *token) {
NULL
);

EventList *list = new EventList();

FSEventStreamContext callbackInfo {0, (void *)list, nullptr, nullptr, nullptr};
FSEventStreamRef stream = FSEventStreamCreate(
NULL,
Expand Down
19 changes: 11 additions & 8 deletions src/shared/brute.cc
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#include <string>
#include <sstream>
#include <fstream>
#include "../DirTree.hh"
#include "../Event.hh"

DirTree *getDirTree(std::string *dir);

std::string getCurrentTokenImpl(std::string *dir) {
void writeSnapshotImpl(std::string *dir, std::string *snapshotPath) {
auto tree = getDirTree(dir);
std::ostringstream os;
tree->write(os);
return os.str();
std::ofstream ofs(*snapshotPath);
tree->write(ofs);
}

EventList *getEventsSinceImpl(std::string *dir, std::string *token) {
std::istringstream is(*token);
auto snapshot = new DirTree(is);
EventList *getEventsSinceImpl(std::string *dir, std::string *snapshotPath) {
std::ifstream ifs(*snapshotPath);
if (ifs.fail()) {
return new EventList();
}

auto snapshot = new DirTree(ifs);
auto now = getDirTree(dir);
return now->getChanges(snapshot);
}

0 comments on commit 0b99729

Please sign in to comment.