/
bindings.cc
325 lines (252 loc) · 10.7 KB
/
bindings.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2015, Camilo Aguilar
#include "bindings.h"
/* size of the event structure, not counting name */
#define EVENT_SIZE (sizeof (struct inotify_event))
/* reasonable guess as to size of 1024 events */
#define BUF_LEN (1024 * (EVENT_SIZE + 16))
namespace NodeInotify {
void Inotify::Initialize(Local<Object> exports) {
Local<FunctionTemplate> t = Nan::New<FunctionTemplate>(New);
t->SetClassName(Nan::New<String>("Inotify").ToLocalChecked());
t->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(t, "addWatch", Inotify::AddWatch);
Nan::SetPrototypeMethod(t, "removeWatch", Inotify::RemoveWatch);
Nan::SetPrototypeMethod(t, "close", Inotify::Close);
Local<ObjectTemplate> object_tmpl = t->InstanceTemplate();
Nan::SetAccessor(object_tmpl, Nan::New<String>("persistent").ToLocalChecked(), Inotify::GetPersistent);
auto ctx = v8::Isolate::GetCurrent()->GetCurrentContext();
Local<Function> fn = t->GetFunction(ctx).ToLocalChecked();
exports->Set(ctx, Nan::New<String>("Inotify").ToLocalChecked(), fn);
// Constants initialization
NODE_DEFINE_CONSTANT(fn, IN_ACCESS); //File was accessed (read)
NODE_DEFINE_CONSTANT(fn, IN_ATTRIB); //Metadata changed, e.g., permissions, timestamps,
//extended attributes, link count (since Linux 2.6.25),
//UID, GID, etc.
NODE_DEFINE_CONSTANT(fn, IN_CLOSE_WRITE); //File opened for writing was closed
NODE_DEFINE_CONSTANT(fn, IN_CLOSE_NOWRITE); //File not opened for writing was closed
NODE_DEFINE_CONSTANT(fn, IN_CREATE); //File/directory created in watched directory
NODE_DEFINE_CONSTANT(fn, IN_DELETE); //File/directory deleted from watched directory
NODE_DEFINE_CONSTANT(fn, IN_DELETE_SELF); //Watched file/directory was itself deleted
NODE_DEFINE_CONSTANT(fn, IN_MODIFY); //File was modified
NODE_DEFINE_CONSTANT(fn, IN_MOVE_SELF); //Watched file/directory was itself moved
NODE_DEFINE_CONSTANT(fn, IN_MOVED_FROM); //File moved out of watched directory
NODE_DEFINE_CONSTANT(fn, IN_MOVED_TO); //File moved into watched directory
NODE_DEFINE_CONSTANT(fn, IN_OPEN); //File was opened
NODE_DEFINE_CONSTANT(fn, IN_IGNORED);// Watch was removed explicitly (inotify.watch.rm) or
// automatically (file was deleted, or file system was
// unmounted)
NODE_DEFINE_CONSTANT(fn, IN_ISDIR); //Subject of this event is a directory
NODE_DEFINE_CONSTANT(fn, IN_Q_OVERFLOW); //Event queue overflowed (wd is -1 for this event)
NODE_DEFINE_CONSTANT(fn, IN_UNMOUNT); //File system containing watched object was unmounted
NODE_DEFINE_CONSTANT(fn, IN_ALL_EVENTS);
NODE_DEFINE_CONSTANT(fn, IN_ONLYDIR); // Only watch the path if it is a directory.
NODE_DEFINE_CONSTANT(fn, IN_DONT_FOLLOW); // Do not follow a sym link
NODE_DEFINE_CONSTANT(fn, IN_ONESHOT); // Only send event once
NODE_DEFINE_CONSTANT(fn, IN_MASK_ADD); //Add (OR) events to watch mask for this pathname if it
// already exists (instead of replacing mask).
NODE_DEFINE_CONSTANT(fn, IN_CLOSE); // (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) Close
NODE_DEFINE_CONSTANT(fn, IN_MOVE); // (IN_MOVED_FROM | IN_MOVED_TO) Moves
}
Inotify::Inotify() : Nan::ObjectWrap() {
//ev_init(&read_watcher, Inotify::Callback);
read_watcher = new uv_poll_t;
read_watcher->data = this; //preserving my reference to use it inside Inotify::Callback
//uv_poll_init(uv_default_loop(), &read_watcher, Inotify::Callback);
persistent = true;
poll_stopped = 0;
}
Inotify::Inotify(bool nonpersistent) : Nan::ObjectWrap() {
read_watcher = new uv_poll_t;
read_watcher->data = this; //preserving my reference so that we can use it inside Inotify::Callback
//ev_init(&read_watcher, Inotify::Callback);
persistent = nonpersistent;
poll_stopped = 0;
}
Inotify::~Inotify() {
if(!persistent) {
//ev_ref(EV_DEFAULT_UC);
uv_ref((uv_handle_t *) read_watcher);
}
//ev_io_stop(EV_DEFAULT_UC_ &read_watcher);
// if Inotify::Close() was already called we do not need to
// stop polling again thus it causes fail of assertion test
StopPolling();
//assert(!uv_is_pending(&read_watcher));
}
NAN_METHOD(Inotify::New) {
Nan::HandleScope scope;
Inotify *inotify = NULL;
if(info.Length() == 1 && info[0]->IsBoolean()) {
inotify = new Inotify(info[0]->IsTrue());
} else {
inotify = new Inotify();
}
inotify->fd = inotify_init();
if(inotify->fd == -1) {
Nan::ThrowError(Nan::New<String>(strerror(errno)).ToLocalChecked());
info.GetReturnValue().Set(Nan::Null());
}
int flags = fcntl(inotify->fd, F_GETFL);
if(flags == -1) {
flags = 0;
}
fcntl(inotify->fd, F_SETFL, flags | O_NONBLOCK);
//ev_io_set(&inotify->read_watcher, inotify->fd, EV_READ);
//ev_io_start(EV_DEFAULT_UC_ &inotify->read_watcher);
uv_poll_init(uv_default_loop(), inotify->read_watcher, inotify->fd);
uv_poll_start(inotify->read_watcher, UV_READABLE, Inotify::Callback);
Local<Object> obj = info.This();
inotify->Wrap(obj);
if(!inotify->persistent) {
//ev_unref(EV_DEFAULT_UC);
uv_unref((uv_handle_t *) inotify->read_watcher);
}
/**
* Increment object references to avoid be GCed while
* we are waiting for events in the inotify file descriptor.
* Also, the object is not weak anymore.
*/
inotify->Ref();
info.GetReturnValue().Set(obj);
}
NAN_METHOD(Inotify::AddWatch) {
Nan::HandleScope scope;
uint32_t mask = 0;
int watch_descriptor = 0;
if(info.Length() < 1 || !info[0]->IsObject()) {
return Nan::ThrowTypeError("You must specify an object as first argument");
}
auto i = v8::Isolate::GetCurrent();
auto ctx = i->GetCurrentContext();
Local<Object> args_ = info[0]->ToObject(ctx).ToLocalChecked();
Local<String> path_sym = Nan::New<String>("path").ToLocalChecked();
if (!args_->Has(ctx, path_sym).FromJust()) {
return Nan::ThrowTypeError("You must specify a path to watch for events");
}
Local<String> callback_sym = Nan::New<String>("callback").ToLocalChecked();
if (!args_->Has(ctx, callback_sym).FromJust() ||
!args_->Get(ctx, callback_sym).ToLocalChecked()->IsFunction()) {
return Nan::ThrowTypeError("You must specify a callback function");
}
Local<String> watch_for_sym = Nan::New<String>("watch_for").ToLocalChecked();
if (!args_->Has(ctx, watch_for_sym).FromJust()) {
mask |= IN_ALL_EVENTS;
} else {
if (!args_->Get(ctx, watch_for_sym).ToLocalChecked()->IsInt32()) {
return Nan::ThrowTypeError("You must specify OR'ed set of events");
}
mask |= args_->Get(ctx, watch_for_sym).ToLocalChecked()->Int32Value(ctx).FromJust();
if (mask == 0) {
return Nan::ThrowTypeError("You must specify OR'ed set of events");
}
}
String::Utf8Value path(i, args_->Get(ctx, path_sym).ToLocalChecked());
Inotify *inotify = Nan::ObjectWrap::Unwrap<Inotify>(info.This());
/* add watch */
watch_descriptor = inotify_add_watch(inotify->fd, (const char *) *path, mask);
Local<Integer> descriptor = Nan::New<Integer>(watch_descriptor);
//Local<Function> callback = Local<Function>::Cast(args_->Get(callback_sym));
inotify->handle()->Set(ctx, descriptor, args_->Get(ctx, callback_sym).ToLocalChecked());
info.GetReturnValue().Set(descriptor);
}
NAN_METHOD(Inotify::RemoveWatch) {
Nan::HandleScope scope;
auto i = v8::Isolate::GetCurrent();
auto ctx = i->GetCurrentContext();
uint32_t watch = 0;
int ret = -1;
if(info.Length() == 0 || !info[0]->IsInt32()) {
return Nan::ThrowTypeError("You must specify a valid watcher descriptor as argument");
}
watch = info[0]->Int32Value(ctx).FromJust();
Inotify *inotify = Nan::ObjectWrap::Unwrap<Inotify>(info.This());
ret = inotify_rm_watch(inotify->fd, watch);
if(ret == -1) {
Nan::ThrowError(Nan::New<String>(strerror(errno)).ToLocalChecked());
info.GetReturnValue().Set(Nan::False());
}
info.GetReturnValue().Set(Nan::True());
}
void Inotify::on_handle_close(uv_handle_t* handle) {
assert(!uv_is_active(handle));
delete handle;
}
NAN_METHOD(Inotify::Close) {
Nan::HandleScope scope;
int ret = -1;
Inotify *inotify = Nan::ObjectWrap::Unwrap<Inotify>(info.This());
ret = close(inotify->fd);
if (ret == -1) {
Nan::ThrowError(Nan::New<String>(strerror(errno)).ToLocalChecked());
info.GetReturnValue().Set(Nan::False());
}
if (!inotify->persistent) {
//ev_ref(EV_DEFAULT_UC);
uv_ref((uv_handle_t *) inotify->read_watcher);
}
//ev_io_stop(EV_DEFAULT_UC_ &inotify->read_watcher);
inotify->StopPolling();
/*Eliminating reference created inside of Inotify::New.
The object is also weak again.
Now v8 can do its stuff and GC the object.
*/
inotify->Unref();
info.GetReturnValue().Set(Nan::True());
}
void Inotify::Callback(uv_poll_t *watcher, int status, int revents) {
Nan::HandleScope scope;
auto i = v8::Isolate::GetCurrent();
auto ctx = i->GetCurrentContext();
Inotify *inotify = static_cast<Inotify*>(watcher->data);
assert(watcher == inotify->read_watcher);
char buffer[BUF_LEN];
//int length = read(inotify->fd, buffer, BUF_LEN);
Local<Value> argv[1];
Nan::TryCatch try_catch;
int sz = 0;
while ((sz = read(inotify->fd, buffer, BUF_LEN)) > 0) {
struct inotify_event *event;
for (unsigned int i = 0; i <= (sz-EVENT_SIZE); i += (EVENT_SIZE + event->len)) {
event = (struct inotify_event *) &buffer[i];
Local<Object> obj = Nan::New<Object>();
obj->Set(ctx, Nan::New<String>("watch").ToLocalChecked(), Nan::New<Integer>(event->wd));
obj->Set(ctx, Nan::New<String>("mask").ToLocalChecked(), Nan::New<Integer>(event->mask));
obj->Set(ctx, Nan::New<String>("cookie").ToLocalChecked(), Nan::New<Integer>(event->cookie));
if(event->len) {
obj->Set(ctx, Nan::New<String>("name").ToLocalChecked(), Nan::New<String>(event->name).ToLocalChecked());
}
argv[0] = obj;
inotify->Ref();
Local<Object> handle = inotify->handle();
Local<Value> value = handle->Get(ctx, Nan::New<Integer>(event->wd)).ToLocalChecked();
Local<Function> func = Local<Function>::Cast(value);
Nan::Callback callback(func);
Nan::Call(callback, handle, 1, argv);
inotify->Unref();
if(event->mask & IN_IGNORED) {
//deleting callback because the watch was removed
Local<Value> wd = Nan::New<Integer>(event->wd);
handle->Delete(ctx, wd->ToString(ctx).ToLocalChecked());
}
if (try_catch.HasCaught()) {
Nan::FatalException(try_catch);
}
} // for
} // while
}
NAN_GETTER(Inotify::GetPersistent) {
Nan::HandleScope scope;
Inotify *inotify = Nan::ObjectWrap::Unwrap<Inotify>(info.This());
if (inotify->persistent) {
info.GetReturnValue().Set(Nan::True());
}
info.GetReturnValue().Set(Nan::False());
}
void Inotify::StopPolling() {
if (!poll_stopped) {
uv_poll_stop(read_watcher);
uv_close((uv_handle_t *) read_watcher, Inotify::on_handle_close);
poll_stopped = 1;
}
}
}//namespace NodeInotify