1 // Copyright Joyent, Inc. and other Node contributors.
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:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
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.
22 #include "async-wrap.h"
23 #include "async-wrap-inl.h"
29 #include "handle_wrap.h"
36 using v8::FunctionCallbackInfo;
37 using v8::FunctionTemplate;
39 using v8::HandleScope;
46 class FSEventWrap: public HandleWrap {
48 static void Initialize(Handle<Object> target,
50 Handle<Context> context);
51 static void New(const FunctionCallbackInfo<Value>& args);
52 static void Start(const FunctionCallbackInfo<Value>& args);
53 static void Close(const FunctionCallbackInfo<Value>& args);
56 FSEventWrap(Environment* env, Handle<Object> object);
57 virtual ~FSEventWrap();
59 static void OnEvent(uv_fs_event_t* handle, const char* filename, int events,
62 uv_fs_event_t handle_;
67 FSEventWrap::FSEventWrap(Environment* env, Handle<Object> object)
70 reinterpret_cast<uv_handle_t*>(&handle_),
71 AsyncWrap::PROVIDER_FSEVENTWRAP) {
76 FSEventWrap::~FSEventWrap() {
77 assert(initialized_ == false);
81 void FSEventWrap::Initialize(Handle<Object> target,
83 Handle<Context> context) {
84 Environment* env = Environment::GetCurrent(context);
86 Local<FunctionTemplate> t = FunctionTemplate::New(env->isolate(), New);
87 t->InstanceTemplate()->SetInternalFieldCount(1);
88 t->SetClassName(env->fsevent_string());
90 NODE_SET_PROTOTYPE_METHOD(t, "start", Start);
91 NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
93 target->Set(env->fsevent_string(), t->GetFunction());
97 void FSEventWrap::New(const FunctionCallbackInfo<Value>& args) {
98 assert(args.IsConstructCall());
99 HandleScope handle_scope(args.GetIsolate());
100 Environment* env = Environment::GetCurrent(args.GetIsolate());
101 new FSEventWrap(env, args.This());
105 void FSEventWrap::Start(const FunctionCallbackInfo<Value>& args) {
106 Environment* env = Environment::GetCurrent(args.GetIsolate());
107 HandleScope scope(env->isolate());
109 FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This());
111 if (args.Length() < 1 || !args[0]->IsString()) {
112 return env->ThrowTypeError("Bad arguments");
115 String::Utf8Value path(args[0]);
117 unsigned int flags = 0;
118 if (args[2]->IsTrue())
119 flags |= UV_FS_EVENT_RECURSIVE;
121 int err = uv_fs_event_init(wrap->env()->event_loop(), &wrap->handle_);
123 wrap->initialized_ = true;
125 err = uv_fs_event_start(&wrap->handle_, OnEvent, *path, flags);
128 // Check for persistent argument
129 if (!args[1]->IsTrue()) {
130 uv_unref(reinterpret_cast<uv_handle_t*>(&wrap->handle_));
133 FSEventWrap::Close(args);
137 args.GetReturnValue().Set(err);
141 void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename,
142 int events, int status) {
143 FSEventWrap* wrap = static_cast<FSEventWrap*>(handle->data);
144 Environment* env = wrap->env();
146 HandleScope handle_scope(env->isolate());
147 Context::Scope context_scope(env->context());
149 assert(wrap->persistent().IsEmpty() == false);
151 // We're in a bind here. libuv can set both UV_RENAME and UV_CHANGE but
152 // the Node API only lets us pass a single event to JS land.
154 // The obvious solution is to run the callback twice, once for each event.
155 // However, since the second event is not allowed to fire if the handle is
156 // closed after the first event, and since there is no good way to detect
157 // closed handles, that option is out.
159 // For now, ignore the UV_CHANGE event if UV_RENAME is also set. Make the
160 // assumption that a rename implicitly means an attribute change. Not too
161 // unreasonable, right? Still, we should revisit this before v1.0.
162 Local<String> event_string;
164 event_string = String::Empty(env->isolate());
165 } else if (events & UV_RENAME) {
166 event_string = env->rename_string();
167 } else if (events & UV_CHANGE) {
168 event_string = env->change_string();
170 assert(0 && "bad fs events flag");
174 Local<Value> argv[] = {
175 Integer::New(env->isolate(), status),
180 if (filename != NULL) {
181 argv[2] = OneByteString(env->isolate(), filename);
184 wrap->MakeCallback(env->onchange_string(), ARRAY_SIZE(argv), argv);
188 void FSEventWrap::Close(const FunctionCallbackInfo<Value>& args) {
189 Environment* env = Environment::GetCurrent(args.GetIsolate());
190 HandleScope scope(env->isolate());
192 FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This());
194 if (wrap == NULL || wrap->initialized_ == false)
196 wrap->initialized_ = false;
198 HandleWrap::Close(args);
203 NODE_MODULE_CONTEXT_AWARE_BUILTIN(fs_event_wrap, node::FSEventWrap::Initialize)