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 "node_stat_watcher.h"
23 #include "async-wrap.h"
24 #include "async-wrap-inl.h"
37 using v8::FunctionCallbackInfo;
38 using v8::FunctionTemplate;
40 using v8::HandleScope;
48 void StatWatcher::Initialize(Environment* env, Handle<Object> target) {
49 HandleScope scope(env->isolate());
51 Local<FunctionTemplate> t = FunctionTemplate::New(env->isolate(),
53 t->InstanceTemplate()->SetInternalFieldCount(1);
54 t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"));
56 NODE_SET_PROTOTYPE_METHOD(t, "start", StatWatcher::Start);
57 NODE_SET_PROTOTYPE_METHOD(t, "stop", StatWatcher::Stop);
59 target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"),
64 static void Delete(uv_handle_t* handle) {
65 delete reinterpret_cast<uv_fs_poll_t*>(handle);
69 StatWatcher::StatWatcher(Environment* env, Local<Object> wrap)
70 : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_STATWATCHER),
71 watcher_(new uv_fs_poll_t) {
72 MakeWeak<StatWatcher>(this);
73 uv_fs_poll_init(env->event_loop(), watcher_);
74 watcher_->data = static_cast<void*>(this);
78 StatWatcher::~StatWatcher() {
80 uv_close(reinterpret_cast<uv_handle_t*>(watcher_), Delete);
84 void StatWatcher::Callback(uv_fs_poll_t* handle,
86 const uv_stat_t* prev,
87 const uv_stat_t* curr) {
88 StatWatcher* wrap = static_cast<StatWatcher*>(handle->data);
89 assert(wrap->watcher_ == handle);
90 Environment* env = wrap->env();
91 HandleScope handle_scope(env->isolate());
92 Context::Scope context_scope(env->context());
93 Local<Value> argv[] = {
94 BuildStatsObject(env, curr),
95 BuildStatsObject(env, prev),
96 Integer::New(env->isolate(), status)
98 wrap->MakeCallback(env->onchange_string(), ARRAY_SIZE(argv), argv);
102 void StatWatcher::New(const FunctionCallbackInfo<Value>& args) {
103 assert(args.IsConstructCall());
104 HandleScope handle_scope(args.GetIsolate());
105 Environment* env = Environment::GetCurrent(args.GetIsolate());
106 new StatWatcher(env, args.This());
110 void StatWatcher::Start(const FunctionCallbackInfo<Value>& args) {
111 assert(args.Length() == 3);
112 Environment* env = Environment::GetCurrent(args.GetIsolate());
113 HandleScope scope(env->isolate());
115 StatWatcher* wrap = Unwrap<StatWatcher>(args.This());
116 String::Utf8Value path(args[0]);
117 const bool persistent = args[1]->BooleanValue();
118 const uint32_t interval = args[2]->Uint32Value();
121 uv_unref(reinterpret_cast<uv_handle_t*>(wrap->watcher_));
122 uv_fs_poll_start(wrap->watcher_, Callback, *path, interval);
127 void StatWatcher::Stop(const FunctionCallbackInfo<Value>& args) {
128 StatWatcher* wrap = Unwrap<StatWatcher>(args.This());
129 Environment* env = wrap->env();
130 HandleScope handle_scope(env->isolate());
131 Context::Scope context_scope(env->context());
132 wrap->MakeCallback(env->onstop_string(), 0, NULL);
137 void StatWatcher::Stop() {
138 if (!uv_is_active(reinterpret_cast<uv_handle_t*>(watcher_)))
140 uv_fs_poll_stop(watcher_);
141 MakeWeak<StatWatcher>(this);