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 "util-inl.h" |
25 |
|
|
#include "node.h" |
26 |
|
|
#include "handle_wrap.h" |
27 |
|
|
#include "string_bytes.h" |
28 |
|
|
|
29 |
|
|
|
30 |
|
|
namespace node { |
31 |
|
|
|
32 |
|
|
using v8::Context; |
33 |
|
|
using v8::DontDelete; |
34 |
|
|
using v8::DontEnum; |
35 |
|
|
using v8::FunctionCallbackInfo; |
36 |
|
|
using v8::FunctionTemplate; |
37 |
|
|
using v8::HandleScope; |
38 |
|
|
using v8::Integer; |
39 |
|
|
using v8::Local; |
40 |
|
|
using v8::MaybeLocal; |
41 |
|
|
using v8::Object; |
42 |
|
|
using v8::PropertyAttribute; |
43 |
|
|
using v8::ReadOnly; |
44 |
|
|
using v8::Signature; |
45 |
|
|
using v8::String; |
46 |
|
|
using v8::Value; |
47 |
|
|
|
48 |
|
|
namespace { |
49 |
|
|
|
50 |
|
|
class FSEventWrap: public HandleWrap { |
51 |
|
|
public: |
52 |
|
|
static void Initialize(Local<Object> target, |
53 |
|
|
Local<Value> unused, |
54 |
|
|
Local<Context> context, |
55 |
|
|
void* priv); |
56 |
|
|
static void New(const FunctionCallbackInfo<Value>& args); |
57 |
|
|
static void Start(const FunctionCallbackInfo<Value>& args); |
58 |
|
|
static void GetInitialized(const FunctionCallbackInfo<Value>& args); |
59 |
|
|
|
60 |
|
1 |
SET_NO_MEMORY_INFO() |
61 |
|
1 |
SET_MEMORY_INFO_NAME(FSEventWrap) |
62 |
|
1 |
SET_SELF_SIZE(FSEventWrap) |
63 |
|
|
|
64 |
|
|
private: |
65 |
|
|
static const encoding kDefaultEncoding = UTF8; |
66 |
|
|
|
67 |
|
|
FSEventWrap(Environment* env, Local<Object> object); |
68 |
✗✓ |
56 |
~FSEventWrap() = default; |
69 |
|
|
|
70 |
|
|
static void OnEvent(uv_fs_event_t* handle, const char* filename, int events, |
71 |
|
|
int status); |
72 |
|
|
|
73 |
|
|
uv_fs_event_t handle_; |
74 |
|
|
enum encoding encoding_ = kDefaultEncoding; |
75 |
|
|
}; |
76 |
|
|
|
77 |
|
|
|
78 |
|
28 |
FSEventWrap::FSEventWrap(Environment* env, Local<Object> object) |
79 |
|
|
: HandleWrap(env, |
80 |
|
|
object, |
81 |
|
|
reinterpret_cast<uv_handle_t*>(&handle_), |
82 |
|
28 |
AsyncWrap::PROVIDER_FSEVENTWRAP) { |
83 |
|
28 |
MarkAsUninitialized(); |
84 |
|
28 |
} |
85 |
|
|
|
86 |
|
|
|
87 |
|
44 |
void FSEventWrap::GetInitialized(const FunctionCallbackInfo<Value>& args) { |
88 |
|
44 |
FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This()); |
89 |
✗✓ |
44 |
CHECK_NOT_NULL(wrap); |
90 |
|
132 |
args.GetReturnValue().Set(!wrap->IsHandleClosing()); |
91 |
|
44 |
} |
92 |
|
|
|
93 |
|
24 |
void FSEventWrap::Initialize(Local<Object> target, |
94 |
|
|
Local<Value> unused, |
95 |
|
|
Local<Context> context, |
96 |
|
|
void* priv) { |
97 |
|
24 |
Environment* env = Environment::GetCurrent(context); |
98 |
|
|
|
99 |
|
24 |
auto fsevent_string = FIXED_ONE_BYTE_STRING(env->isolate(), "FSEvent"); |
100 |
|
24 |
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); |
101 |
|
48 |
t->InstanceTemplate()->SetInternalFieldCount(1); |
102 |
|
24 |
t->SetClassName(fsevent_string); |
103 |
|
|
|
104 |
|
48 |
t->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
105 |
|
24 |
env->SetProtoMethod(t, "start", Start); |
106 |
|
24 |
env->SetProtoMethod(t, "close", Close); |
107 |
|
|
|
108 |
|
|
Local<FunctionTemplate> get_initialized_templ = |
109 |
|
|
FunctionTemplate::New(env->isolate(), |
110 |
|
|
GetInitialized, |
111 |
|
|
env->as_callback_data(), |
112 |
|
48 |
Signature::New(env->isolate(), t)); |
113 |
|
|
|
114 |
|
72 |
t->PrototypeTemplate()->SetAccessorProperty( |
115 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "initialized"), |
116 |
|
|
get_initialized_templ, |
117 |
|
|
Local<FunctionTemplate>(), |
118 |
|
72 |
static_cast<PropertyAttribute>(ReadOnly | DontDelete | DontEnum)); |
119 |
|
|
|
120 |
|
|
target->Set(env->context(), |
121 |
|
|
fsevent_string, |
122 |
|
96 |
t->GetFunction(context).ToLocalChecked()).Check(); |
123 |
|
24 |
} |
124 |
|
|
|
125 |
|
|
|
126 |
|
28 |
void FSEventWrap::New(const FunctionCallbackInfo<Value>& args) { |
127 |
✗✓ |
28 |
CHECK(args.IsConstructCall()); |
128 |
|
28 |
Environment* env = Environment::GetCurrent(args); |
129 |
|
28 |
new FSEventWrap(env, args.This()); |
130 |
|
28 |
} |
131 |
|
|
|
132 |
|
|
// wrap.start(filename, persistent, recursive, encoding) |
133 |
|
18 |
void FSEventWrap::Start(const FunctionCallbackInfo<Value>& args) { |
134 |
|
18 |
Environment* env = Environment::GetCurrent(args); |
135 |
|
|
|
136 |
|
18 |
FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This()); |
137 |
✗✓ |
18 |
CHECK_NOT_NULL(wrap); |
138 |
✗✓ |
18 |
CHECK(wrap->IsHandleClosing()); // Check that Start() has not been called. |
139 |
|
|
|
140 |
|
18 |
const int argc = args.Length(); |
141 |
✗✓ |
18 |
CHECK_GE(argc, 4); |
142 |
|
|
|
143 |
|
18 |
BufferValue path(env->isolate(), args[0]); |
144 |
✗✓ |
18 |
CHECK_NOT_NULL(*path); |
145 |
|
|
|
146 |
|
18 |
unsigned int flags = 0; |
147 |
✗✓ |
36 |
if (args[2]->IsTrue()) |
148 |
|
|
flags |= UV_FS_EVENT_RECURSIVE; |
149 |
|
|
|
150 |
|
36 |
wrap->encoding_ = ParseEncoding(env->isolate(), args[3], kDefaultEncoding); |
151 |
|
|
|
152 |
|
18 |
int err = uv_fs_event_init(wrap->env()->event_loop(), &wrap->handle_); |
153 |
✗✓ |
18 |
if (err != 0) { |
154 |
|
|
return args.GetReturnValue().Set(err); |
155 |
|
|
} |
156 |
|
|
|
157 |
|
18 |
err = uv_fs_event_start(&wrap->handle_, OnEvent, *path, flags); |
158 |
|
18 |
wrap->MarkAsInitialized(); |
159 |
|
|
|
160 |
✓✓ |
18 |
if (err != 0) { |
161 |
|
1 |
FSEventWrap::Close(args); |
162 |
|
2 |
return args.GetReturnValue().Set(err); |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
// Check for persistent argument |
166 |
✓✓ |
34 |
if (!args[1]->IsTrue()) { |
167 |
|
2 |
uv_unref(reinterpret_cast<uv_handle_t*>(&wrap->handle_)); |
168 |
|
|
} |
169 |
|
|
|
170 |
✓✓ |
34 |
args.GetReturnValue().Set(err); |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
|
174 |
|
11 |
void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename, |
175 |
|
|
int events, int status) { |
176 |
|
11 |
FSEventWrap* wrap = static_cast<FSEventWrap*>(handle->data); |
177 |
|
11 |
Environment* env = wrap->env(); |
178 |
|
|
|
179 |
|
11 |
HandleScope handle_scope(env->isolate()); |
180 |
|
11 |
Context::Scope context_scope(env->context()); |
181 |
|
|
|
182 |
✗✓ |
22 |
CHECK_EQ(wrap->persistent().IsEmpty(), false); |
183 |
|
|
|
184 |
|
|
// We're in a bind here. libuv can set both UV_RENAME and UV_CHANGE but |
185 |
|
|
// the Node API only lets us pass a single event to JS land. |
186 |
|
|
// |
187 |
|
|
// The obvious solution is to run the callback twice, once for each event. |
188 |
|
|
// However, since the second event is not allowed to fire if the handle is |
189 |
|
|
// closed after the first event, and since there is no good way to detect |
190 |
|
|
// closed handles, that option is out. |
191 |
|
|
// |
192 |
|
|
// For now, ignore the UV_CHANGE event if UV_RENAME is also set. Make the |
193 |
|
|
// assumption that a rename implicitly means an attribute change. Not too |
194 |
|
|
// unreasonable, right? Still, we should revisit this before v1.0. |
195 |
|
|
Local<String> event_string; |
196 |
✗✓ |
11 |
if (status) { |
197 |
|
|
event_string = String::Empty(env->isolate()); |
198 |
✓✓ |
11 |
} else if (events & UV_RENAME) { |
199 |
|
7 |
event_string = env->rename_string(); |
200 |
✓✗ |
4 |
} else if (events & UV_CHANGE) { |
201 |
|
4 |
event_string = env->change_string(); |
202 |
|
|
} else { |
203 |
|
|
CHECK(0 && "bad fs events flag"); |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
Local<Value> argv[] = { |
207 |
|
|
Integer::New(env->isolate(), status), |
208 |
|
|
event_string, |
209 |
|
|
Null(env->isolate()) |
210 |
|
44 |
}; |
211 |
|
|
|
212 |
✓✗ |
11 |
if (filename != nullptr) { |
213 |
|
|
Local<Value> error; |
214 |
|
|
MaybeLocal<Value> fn = StringBytes::Encode(env->isolate(), |
215 |
|
|
filename, |
216 |
|
|
wrap->encoding_, |
217 |
|
11 |
&error); |
218 |
✗✓ |
11 |
if (fn.IsEmpty()) { |
219 |
|
|
argv[0] = Integer::New(env->isolate(), UV_EINVAL); |
220 |
|
|
argv[2] = StringBytes::Encode(env->isolate(), |
221 |
|
|
filename, |
222 |
|
|
strlen(filename), |
223 |
|
|
BUFFER, |
224 |
|
|
&error).ToLocalChecked(); |
225 |
|
|
} else { |
226 |
|
11 |
argv[2] = fn.ToLocalChecked(); |
227 |
|
|
} |
228 |
|
|
} |
229 |
|
|
|
230 |
|
22 |
wrap->MakeCallback(env->onchange_string(), arraysize(argv), argv); |
231 |
|
11 |
} |
232 |
|
|
|
233 |
|
|
} // anonymous namespace |
234 |
|
|
} // namespace node |
235 |
|
|
|
236 |
|
4950 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(fs_event_wrap, node::FSEventWrap::Initialize) |