src: deduplicate CHECK_EQ/CHECK_NE macros
[platform/upstream/nodejs.git] / src / fs_event_wrap.cc
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.h"
23 #include "async-wrap-inl.h"
24 #include "env.h"
25 #include "env-inl.h"
26 #include "util.h"
27 #include "util-inl.h"
28 #include "node.h"
29 #include "handle_wrap.h"
30
31 #include <stdlib.h>
32
33 namespace node {
34
35 using v8::Context;
36 using v8::FunctionCallbackInfo;
37 using v8::FunctionTemplate;
38 using v8::Handle;
39 using v8::HandleScope;
40 using v8::Integer;
41 using v8::Local;
42 using v8::Object;
43 using v8::String;
44 using v8::Value;
45
46 class FSEventWrap: public HandleWrap {
47  public:
48   static void Initialize(Handle<Object> target,
49                          Handle<Value> unused,
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);
54
55  private:
56   FSEventWrap(Environment* env, Handle<Object> object);
57   virtual ~FSEventWrap();
58
59   static void OnEvent(uv_fs_event_t* handle, const char* filename, int events,
60     int status);
61
62   uv_fs_event_t handle_;
63   bool initialized_;
64 };
65
66
67 FSEventWrap::FSEventWrap(Environment* env, Handle<Object> object)
68     : HandleWrap(env,
69                  object,
70                  reinterpret_cast<uv_handle_t*>(&handle_),
71                  AsyncWrap::PROVIDER_FSEVENTWRAP) {
72   initialized_ = false;
73 }
74
75
76 FSEventWrap::~FSEventWrap() {
77   assert(initialized_ == false);
78 }
79
80
81 void FSEventWrap::Initialize(Handle<Object> target,
82                              Handle<Value> unused,
83                              Handle<Context> context) {
84   Environment* env = Environment::GetCurrent(context);
85
86   Local<FunctionTemplate> t = FunctionTemplate::New(env->isolate(),  New);
87   t->InstanceTemplate()->SetInternalFieldCount(1);
88   t->SetClassName(env->fsevent_string());
89
90   NODE_SET_PROTOTYPE_METHOD(t, "start", Start);
91   NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
92
93   target->Set(env->fsevent_string(), t->GetFunction());
94 }
95
96
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());
102 }
103
104
105 void FSEventWrap::Start(const FunctionCallbackInfo<Value>& args) {
106   Environment* env = Environment::GetCurrent(args.GetIsolate());
107   HandleScope scope(env->isolate());
108
109   FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This());
110
111   if (args.Length() < 1 || !args[0]->IsString()) {
112     return env->ThrowTypeError("Bad arguments");
113   }
114
115   String::Utf8Value path(args[0]);
116
117   unsigned int flags = 0;
118   if (args[2]->IsTrue())
119     flags |= UV_FS_EVENT_RECURSIVE;
120
121   int err = uv_fs_event_init(wrap->env()->event_loop(), &wrap->handle_);
122   if (err == 0) {
123     wrap->initialized_ = true;
124
125     err = uv_fs_event_start(&wrap->handle_, OnEvent, *path, flags);
126
127     if (err == 0) {
128       // Check for persistent argument
129       if (!args[1]->IsTrue()) {
130         uv_unref(reinterpret_cast<uv_handle_t*>(&wrap->handle_));
131       }
132     } else {
133       FSEventWrap::Close(args);
134     }
135   }
136
137   args.GetReturnValue().Set(err);
138 }
139
140
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();
145
146   HandleScope handle_scope(env->isolate());
147   Context::Scope context_scope(env->context());
148
149   assert(wrap->persistent().IsEmpty() == false);
150
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.
153   //
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.
158   //
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;
163   if (status) {
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();
169   } else {
170     assert(0 && "bad fs events flag");
171     abort();
172   }
173
174   Local<Value> argv[] = {
175     Integer::New(env->isolate(), status),
176     event_string,
177     Null(env->isolate())
178   };
179
180   if (filename != NULL) {
181     argv[2] = OneByteString(env->isolate(), filename);
182   }
183
184   wrap->MakeCallback(env->onchange_string(), ARRAY_SIZE(argv), argv);
185 }
186
187
188 void FSEventWrap::Close(const FunctionCallbackInfo<Value>& args) {
189   Environment* env = Environment::GetCurrent(args.GetIsolate());
190   HandleScope scope(env->isolate());
191
192   FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This());
193
194   if (wrap == NULL || wrap->initialized_ == false)
195     return;
196   wrap->initialized_ = false;
197
198   HandleWrap::Close(args);
199 }
200
201 }  // namespace node
202
203 NODE_MODULE_CONTEXT_AWARE_BUILTIN(fs_event_wrap, node::FSEventWrap::Initialize)