forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_event_wrap.cc
240 lines (197 loc) · 7.81 KB
/
fs_event_wrap.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
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "async_wrap-inl.h"
#include "env-inl.h"
#include "handle_wrap.h"
#include "node.h"
#include "node_external_reference.h"
#include "string_bytes.h"
namespace node {
using v8::Context;
using v8::DontDelete;
using v8::DontEnum;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Integer;
using v8::Local;
using v8::MaybeLocal;
using v8::Object;
using v8::PropertyAttribute;
using v8::ReadOnly;
using v8::Signature;
using v8::String;
using v8::Value;
namespace {
class FSEventWrap: public HandleWrap {
public:
static void Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
static void New(const FunctionCallbackInfo<Value>& args);
static void Start(const FunctionCallbackInfo<Value>& args);
static void GetInitialized(const FunctionCallbackInfo<Value>& args);
SET_NO_MEMORY_INFO()
SET_MEMORY_INFO_NAME(FSEventWrap)
SET_SELF_SIZE(FSEventWrap)
private:
static const encoding kDefaultEncoding = UTF8;
FSEventWrap(Environment* env, Local<Object> object);
~FSEventWrap() override = default;
static void OnEvent(uv_fs_event_t* handle, const char* filename, int events,
int status);
uv_fs_event_t handle_;
enum encoding encoding_ = kDefaultEncoding;
};
FSEventWrap::FSEventWrap(Environment* env, Local<Object> object)
: HandleWrap(env,
object,
reinterpret_cast<uv_handle_t*>(&handle_),
AsyncWrap::PROVIDER_FSEVENTWRAP) {
MarkAsUninitialized();
}
void FSEventWrap::GetInitialized(const FunctionCallbackInfo<Value>& args) {
FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This());
CHECK_NOT_NULL(wrap);
args.GetReturnValue().Set(!wrap->IsHandleClosing());
}
void FSEventWrap::Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Environment* env = Environment::GetCurrent(context);
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
t->InstanceTemplate()->SetInternalFieldCount(
FSEventWrap::kInternalFieldCount);
t->Inherit(HandleWrap::GetConstructorTemplate(env));
env->SetProtoMethod(t, "start", Start);
Local<FunctionTemplate> get_initialized_templ =
FunctionTemplate::New(env->isolate(),
GetInitialized,
Local<Value>(),
Signature::New(env->isolate(), t));
t->PrototypeTemplate()->SetAccessorProperty(
FIXED_ONE_BYTE_STRING(env->isolate(), "initialized"),
get_initialized_templ,
Local<FunctionTemplate>(),
static_cast<PropertyAttribute>(ReadOnly | DontDelete | DontEnum));
env->SetConstructorFunction(target, "FSEvent", t);
}
void FSEventWrap::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(New);
registry->Register(Start);
registry->Register(GetInitialized);
}
void FSEventWrap::New(const FunctionCallbackInfo<Value>& args) {
CHECK(args.IsConstructCall());
Environment* env = Environment::GetCurrent(args);
new FSEventWrap(env, args.This());
}
// wrap.start(filename, persistent, recursive, encoding)
void FSEventWrap::Start(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This());
CHECK_NOT_NULL(wrap);
CHECK(wrap->IsHandleClosing()); // Check that Start() has not been called.
const int argc = args.Length();
CHECK_GE(argc, 4);
BufferValue path(env->isolate(), args[0]);
CHECK_NOT_NULL(*path);
unsigned int flags = 0;
if (args[2]->IsTrue())
flags |= UV_FS_EVENT_RECURSIVE;
wrap->encoding_ = ParseEncoding(env->isolate(), args[3], kDefaultEncoding);
int err = uv_fs_event_init(wrap->env()->event_loop(), &wrap->handle_);
if (err != 0) {
return args.GetReturnValue().Set(err);
}
err = uv_fs_event_start(&wrap->handle_, OnEvent, *path, flags);
wrap->MarkAsInitialized();
if (err != 0) {
FSEventWrap::Close(args);
return args.GetReturnValue().Set(err);
}
// Check for persistent argument
if (!args[1]->IsTrue()) {
uv_unref(reinterpret_cast<uv_handle_t*>(&wrap->handle_));
}
args.GetReturnValue().Set(err);
}
void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename,
int events, int status) {
FSEventWrap* wrap = static_cast<FSEventWrap*>(handle->data);
Environment* env = wrap->env();
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());
CHECK_EQ(wrap->persistent().IsEmpty(), false);
// We're in a bind here. libuv can set both UV_RENAME and UV_CHANGE but
// the Node API only lets us pass a single event to JS land.
//
// The obvious solution is to run the callback twice, once for each event.
// However, since the second event is not allowed to fire if the handle is
// closed after the first event, and since there is no good way to detect
// closed handles, that option is out.
//
// For now, ignore the UV_CHANGE event if UV_RENAME is also set. Make the
// assumption that a rename implicitly means an attribute change. Not too
// unreasonable, right? Still, we should revisit this before v1.0.
Local<String> event_string;
if (status) {
event_string = String::Empty(env->isolate());
} else if (events & UV_RENAME) {
event_string = env->rename_string();
} else if (events & UV_CHANGE) {
event_string = env->change_string();
} else {
CHECK(0 && "bad fs events flag");
}
Local<Value> argv[] = {
Integer::New(env->isolate(), status),
event_string,
Null(env->isolate())
};
if (filename != nullptr) {
Local<Value> error;
MaybeLocal<Value> fn = StringBytes::Encode(env->isolate(),
filename,
wrap->encoding_,
&error);
if (fn.IsEmpty()) {
argv[0] = Integer::New(env->isolate(), UV_EINVAL);
argv[2] = StringBytes::Encode(env->isolate(),
filename,
strlen(filename),
BUFFER,
&error).ToLocalChecked();
} else {
argv[2] = fn.ToLocalChecked();
}
}
wrap->MakeCallback(env->onchange_string(), arraysize(argv), argv);
}
} // anonymous namespace
} // namespace node
NODE_MODULE_CONTEXT_AWARE_INTERNAL(fs_event_wrap, node::FSEventWrap::Initialize)
NODE_MODULE_EXTERNAL_REFERENCE(fs_event_wrap,
node::FSEventWrap::RegisterExternalReferences)