1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 #include "async_wrap-inl.h"
23 #include "env-inl.h"
24 #include "node.h"
25 #include "handle_wrap.h"
26 #include "string_bytes.h"
27
28
29 namespace node {
30
31 using v8::Context;
32 using v8::DontDelete;
33 using v8::DontEnum;
34 using v8::FunctionCallbackInfo;
35 using v8::FunctionTemplate;
36 using v8::HandleScope;
37 using v8::Integer;
38 using v8::Local;
39 using v8::MaybeLocal;
40 using v8::Object;
41 using v8::PropertyAttribute;
42 using v8::ReadOnly;
43 using v8::Signature;
44 using v8::String;
45 using v8::Value;
46
47 namespace {
48
49 class FSEventWrap: public HandleWrap {
50 public:
51 static void Initialize(Local<Object> target,
52 Local<Value> unused,
53 Local<Context> context,
54 void* priv);
55 static void New(const FunctionCallbackInfo<Value>& args);
56 static void Start(const FunctionCallbackInfo<Value>& args);
57 static void GetInitialized(const FunctionCallbackInfo<Value>& args);
58
59 SET_NO_MEMORY_INFO()
60 SET_MEMORY_INFO_NAME(FSEventWrap)
61 SET_SELF_SIZE(FSEventWrap)
62
63 private:
64 static const encoding kDefaultEncoding = UTF8;
65
66 FSEventWrap(Environment* env, Local<Object> object);
67 ~FSEventWrap() override = default;
68
69 static void OnEvent(uv_fs_event_t* handle, const char* filename, int events,
70 int status);
71
72 uv_fs_event_t handle_;
73 enum encoding encoding_ = kDefaultEncoding;
74 };
75
76
FSEventWrap(Environment * env,Local<Object> object)77 FSEventWrap::FSEventWrap(Environment* env, Local<Object> object)
78 : HandleWrap(env,
79 object,
80 reinterpret_cast<uv_handle_t*>(&handle_),
81 AsyncWrap::PROVIDER_FSEVENTWRAP) {
82 MarkAsUninitialized();
83 }
84
85
GetInitialized(const FunctionCallbackInfo<Value> & args)86 void FSEventWrap::GetInitialized(const FunctionCallbackInfo<Value>& args) {
87 FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This());
88 CHECK_NOT_NULL(wrap);
89 args.GetReturnValue().Set(!wrap->IsHandleClosing());
90 }
91
Initialize(Local<Object> target,Local<Value> unused,Local<Context> context,void * priv)92 void FSEventWrap::Initialize(Local<Object> target,
93 Local<Value> unused,
94 Local<Context> context,
95 void* priv) {
96 Environment* env = Environment::GetCurrent(context);
97
98 Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
99 t->InstanceTemplate()->SetInternalFieldCount(
100 FSEventWrap::kInternalFieldCount);
101
102 t->Inherit(HandleWrap::GetConstructorTemplate(env));
103 env->SetProtoMethod(t, "start", Start);
104
105 Local<FunctionTemplate> get_initialized_templ =
106 FunctionTemplate::New(env->isolate(),
107 GetInitialized,
108 Local<Value>(),
109 Signature::New(env->isolate(), t));
110
111 t->PrototypeTemplate()->SetAccessorProperty(
112 FIXED_ONE_BYTE_STRING(env->isolate(), "initialized"),
113 get_initialized_templ,
114 Local<FunctionTemplate>(),
115 static_cast<PropertyAttribute>(ReadOnly | DontDelete | DontEnum));
116
117 env->SetConstructorFunction(target, "FSEvent", t);
118 }
119
120
New(const FunctionCallbackInfo<Value> & args)121 void FSEventWrap::New(const FunctionCallbackInfo<Value>& args) {
122 CHECK(args.IsConstructCall());
123 Environment* env = Environment::GetCurrent(args);
124 new FSEventWrap(env, args.This());
125 }
126
127 // wrap.start(filename, persistent, recursive, encoding)
Start(const FunctionCallbackInfo<Value> & args)128 void FSEventWrap::Start(const FunctionCallbackInfo<Value>& args) {
129 Environment* env = Environment::GetCurrent(args);
130
131 FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This());
132 CHECK_NOT_NULL(wrap);
133 CHECK(wrap->IsHandleClosing()); // Check that Start() has not been called.
134
135 const int argc = args.Length();
136 CHECK_GE(argc, 4);
137
138 BufferValue path(env->isolate(), args[0]);
139 CHECK_NOT_NULL(*path);
140
141 unsigned int flags = 0;
142 if (args[2]->IsTrue())
143 flags |= UV_FS_EVENT_RECURSIVE;
144
145 wrap->encoding_ = ParseEncoding(env->isolate(), args[3], kDefaultEncoding);
146
147 int err = uv_fs_event_init(wrap->env()->event_loop(), &wrap->handle_);
148 if (err != 0) {
149 return args.GetReturnValue().Set(err);
150 }
151
152 err = uv_fs_event_start(&wrap->handle_, OnEvent, *path, flags);
153 wrap->MarkAsInitialized();
154
155 if (err != 0) {
156 FSEventWrap::Close(args);
157 return args.GetReturnValue().Set(err);
158 }
159
160 // Check for persistent argument
161 if (!args[1]->IsTrue()) {
162 uv_unref(reinterpret_cast<uv_handle_t*>(&wrap->handle_));
163 }
164
165 args.GetReturnValue().Set(err);
166 }
167
168
OnEvent(uv_fs_event_t * handle,const char * filename,int events,int status)169 void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename,
170 int events, int status) {
171 FSEventWrap* wrap = static_cast<FSEventWrap*>(handle->data);
172 Environment* env = wrap->env();
173
174 HandleScope handle_scope(env->isolate());
175 Context::Scope context_scope(env->context());
176
177 CHECK_EQ(wrap->persistent().IsEmpty(), false);
178
179 // We're in a bind here. libuv can set both UV_RENAME and UV_CHANGE but
180 // the Node API only lets us pass a single event to JS land.
181 //
182 // The obvious solution is to run the callback twice, once for each event.
183 // However, since the second event is not allowed to fire if the handle is
184 // closed after the first event, and since there is no good way to detect
185 // closed handles, that option is out.
186 //
187 // For now, ignore the UV_CHANGE event if UV_RENAME is also set. Make the
188 // assumption that a rename implicitly means an attribute change. Not too
189 // unreasonable, right? Still, we should revisit this before v1.0.
190 Local<String> event_string;
191 if (status) {
192 event_string = String::Empty(env->isolate());
193 } else if (events & UV_RENAME) {
194 event_string = env->rename_string();
195 } else if (events & UV_CHANGE) {
196 event_string = env->change_string();
197 } else {
198 CHECK(0 && "bad fs events flag");
199 }
200
201 Local<Value> argv[] = {
202 Integer::New(env->isolate(), status),
203 event_string,
204 Null(env->isolate())
205 };
206
207 if (filename != nullptr) {
208 Local<Value> error;
209 MaybeLocal<Value> fn = StringBytes::Encode(env->isolate(),
210 filename,
211 wrap->encoding_,
212 &error);
213 if (fn.IsEmpty()) {
214 argv[0] = Integer::New(env->isolate(), UV_EINVAL);
215 argv[2] = StringBytes::Encode(env->isolate(),
216 filename,
217 strlen(filename),
218 BUFFER,
219 &error).ToLocalChecked();
220 } else {
221 argv[2] = fn.ToLocalChecked();
222 }
223 }
224
225 wrap->MakeCallback(env->onchange_string(), arraysize(argv), argv);
226 }
227
228 } // anonymous namespace
229 } // namespace node
230
231 NODE_MODULE_CONTEXT_AWARE_INTERNAL(fs_event_wrap, node::FSEventWrap::Initialize)
232