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 |
|
1 |
SET_NO_MEMORY_INFO() |
60 |
|
1 |
SET_MEMORY_INFO_NAME(FSEventWrap) |
61 |
|
1 |
SET_SELF_SIZE(FSEventWrap) |
62 |
|
|
|
63 |
|
|
private: |
64 |
|
|
static const encoding kDefaultEncoding = UTF8; |
65 |
|
|
|
66 |
|
|
FSEventWrap(Environment* env, Local<Object> object); |
67 |
|
58 |
~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 |
|
|
|
77 |
|
29 |
FSEventWrap::FSEventWrap(Environment* env, Local<Object> object) |
78 |
|
|
: HandleWrap(env, |
79 |
|
|
object, |
80 |
|
29 |
reinterpret_cast<uv_handle_t*>(&handle_), |
81 |
|
29 |
AsyncWrap::PROVIDER_FSEVENTWRAP) { |
82 |
|
29 |
MarkAsUninitialized(); |
83 |
|
29 |
} |
84 |
|
|
|
85 |
|
|
|
86 |
|
40 |
void FSEventWrap::GetInitialized(const FunctionCallbackInfo<Value>& args) { |
87 |
|
40 |
FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This()); |
88 |
✗✓ |
40 |
CHECK_NOT_NULL(wrap); |
89 |
|
120 |
args.GetReturnValue().Set(!wrap->IsHandleClosing()); |
90 |
|
40 |
} |
91 |
|
|
|
92 |
|
22 |
void FSEventWrap::Initialize(Local<Object> target, |
93 |
|
|
Local<Value> unused, |
94 |
|
|
Local<Context> context, |
95 |
|
|
void* priv) { |
96 |
|
22 |
Environment* env = Environment::GetCurrent(context); |
97 |
|
|
|
98 |
|
22 |
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); |
99 |
|
66 |
t->InstanceTemplate()->SetInternalFieldCount( |
100 |
|
22 |
FSEventWrap::kInternalFieldCount); |
101 |
|
|
|
102 |
|
44 |
t->Inherit(HandleWrap::GetConstructorTemplate(env)); |
103 |
|
22 |
env->SetProtoMethod(t, "start", Start); |
104 |
|
|
|
105 |
|
|
Local<FunctionTemplate> get_initialized_templ = |
106 |
|
|
FunctionTemplate::New(env->isolate(), |
107 |
|
|
GetInitialized, |
108 |
|
|
Local<Value>(), |
109 |
|
44 |
Signature::New(env->isolate(), t)); |
110 |
|
|
|
111 |
|
88 |
t->PrototypeTemplate()->SetAccessorProperty( |
112 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "initialized"), |
113 |
|
|
get_initialized_templ, |
114 |
|
|
Local<FunctionTemplate>(), |
115 |
|
22 |
static_cast<PropertyAttribute>(ReadOnly | DontDelete | DontEnum)); |
116 |
|
|
|
117 |
|
22 |
env->SetConstructorFunction(target, "FSEvent", t); |
118 |
|
22 |
} |
119 |
|
|
|
120 |
|
|
|
121 |
|
29 |
void FSEventWrap::New(const FunctionCallbackInfo<Value>& args) { |
122 |
✗✓ |
29 |
CHECK(args.IsConstructCall()); |
123 |
|
29 |
Environment* env = Environment::GetCurrent(args); |
124 |
|
29 |
new FSEventWrap(env, args.This()); |
125 |
|
29 |
} |
126 |
|
|
|
127 |
|
|
// wrap.start(filename, persistent, recursive, encoding) |
128 |
|
19 |
void FSEventWrap::Start(const FunctionCallbackInfo<Value>& args) { |
129 |
|
19 |
Environment* env = Environment::GetCurrent(args); |
130 |
|
|
|
131 |
|
19 |
FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This()); |
132 |
✗✓ |
19 |
CHECK_NOT_NULL(wrap); |
133 |
✗✓ |
19 |
CHECK(wrap->IsHandleClosing()); // Check that Start() has not been called. |
134 |
|
|
|
135 |
|
19 |
const int argc = args.Length(); |
136 |
✗✓ |
19 |
CHECK_GE(argc, 4); |
137 |
|
|
|
138 |
|
37 |
BufferValue path(env->isolate(), args[0]); |
139 |
✗✓ |
19 |
CHECK_NOT_NULL(*path); |
140 |
|
|
|
141 |
|
19 |
unsigned int flags = 0; |
142 |
✗✓ |
38 |
if (args[2]->IsTrue()) |
143 |
|
|
flags |= UV_FS_EVENT_RECURSIVE; |
144 |
|
|
|
145 |
|
38 |
wrap->encoding_ = ParseEncoding(env->isolate(), args[3], kDefaultEncoding); |
146 |
|
|
|
147 |
|
19 |
int err = uv_fs_event_init(wrap->env()->event_loop(), &wrap->handle_); |
148 |
✗✓ |
19 |
if (err != 0) { |
149 |
|
|
return args.GetReturnValue().Set(err); |
150 |
|
|
} |
151 |
|
|
|
152 |
|
19 |
err = uv_fs_event_start(&wrap->handle_, OnEvent, *path, flags); |
153 |
|
19 |
wrap->MarkAsInitialized(); |
154 |
|
|
|
155 |
✓✓ |
19 |
if (err != 0) { |
156 |
|
1 |
FSEventWrap::Close(args); |
157 |
|
2 |
return args.GetReturnValue().Set(err); |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
// Check for persistent argument |
161 |
✓✓ |
36 |
if (!args[1]->IsTrue()) { |
162 |
|
2 |
uv_unref(reinterpret_cast<uv_handle_t*>(&wrap->handle_)); |
163 |
|
|
} |
164 |
|
|
|
165 |
✓✓ |
36 |
args.GetReturnValue().Set(err); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
|
169 |
|
11 |
void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename, |
170 |
|
|
int events, int status) { |
171 |
|
11 |
FSEventWrap* wrap = static_cast<FSEventWrap*>(handle->data); |
172 |
|
11 |
Environment* env = wrap->env(); |
173 |
|
|
|
174 |
|
22 |
HandleScope handle_scope(env->isolate()); |
175 |
|
11 |
Context::Scope context_scope(env->context()); |
176 |
|
|
|
177 |
✗✓ |
22 |
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 |
✗✓ |
11 |
if (status) { |
192 |
|
|
event_string = String::Empty(env->isolate()); |
193 |
✓✓ |
11 |
} else if (events & UV_RENAME) { |
194 |
|
7 |
event_string = env->rename_string(); |
195 |
✓✗ |
4 |
} else if (events & UV_CHANGE) { |
196 |
|
4 |
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 |
|
44 |
}; |
206 |
|
|
|
207 |
✓✗ |
11 |
if (filename != nullptr) { |
208 |
|
|
Local<Value> error; |
209 |
|
|
MaybeLocal<Value> fn = StringBytes::Encode(env->isolate(), |
210 |
|
|
filename, |
211 |
|
|
wrap->encoding_, |
212 |
|
11 |
&error); |
213 |
✗✓ |
11 |
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 |
|
11 |
argv[2] = fn.ToLocalChecked(); |
222 |
|
|
} |
223 |
|
|
} |
224 |
|
|
|
225 |
|
11 |
wrap->MakeCallback(env->onchange_string(), arraysize(argv), argv); |
226 |
|
11 |
} |
227 |
|
|
|
228 |
|
|
} // anonymous namespace |
229 |
|
|
} // namespace node |
230 |
|
|
|
231 |
✓✗✓✗
|
18652 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(fs_event_wrap, node::FSEventWrap::Initialize) |