Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
node_stat_watcher.cc
Go to the documentation of this file.
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 "node_stat_watcher.h"
23 #include "async-wrap.h"
24 #include "async-wrap-inl.h"
25 #include "env.h"
26 #include "env-inl.h"
27 #include "util.h"
28 #include "util-inl.h"
29 
30 #include <string.h>
31 #include <stdlib.h>
32 
33 namespace node {
34 
35 using v8::Context;
36 using v8::FunctionCallbackInfo;
37 using v8::FunctionTemplate;
38 using v8::HandleScope;
39 using v8::Integer;
40 using v8::Local;
41 using v8::Object;
42 using v8::String;
43 using v8::Value;
44 
45 
46 void StatWatcher::Initialize(Environment* env, Local<Object> target) {
47  HandleScope scope(env->isolate());
48 
49  Local<FunctionTemplate> t = env->NewFunctionTemplate(StatWatcher::New);
50  t->InstanceTemplate()->SetInternalFieldCount(1);
51  Local<String> statWatcherString =
52  FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher");
53  t->SetClassName(statWatcherString);
54 
55  AsyncWrap::AddWrapMethods(env, t);
56  env->SetProtoMethod(t, "start", StatWatcher::Start);
57  env->SetProtoMethod(t, "stop", StatWatcher::Stop);
58 
59  target->Set(statWatcherString, t->GetFunction());
60 }
61 
62 
63 static void Delete(uv_handle_t* handle) {
64  delete reinterpret_cast<uv_fs_poll_t*>(handle);
65 }
66 
67 
68 StatWatcher::StatWatcher(Environment* env, Local<Object> wrap)
69  : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_STATWATCHER),
70  watcher_(new uv_fs_poll_t) {
71  MakeWeak<StatWatcher>(this);
72  Wrap(wrap, this);
73  uv_fs_poll_init(env->event_loop(), watcher_);
74  watcher_->data = static_cast<void*>(this);
75 }
76 
77 
78 StatWatcher::~StatWatcher() {
79  Stop();
80  uv_close(reinterpret_cast<uv_handle_t*>(watcher_), Delete);
81 }
82 
83 
84 void StatWatcher::Callback(uv_fs_poll_t* handle,
85  int status,
86  const uv_stat_t* prev,
87  const uv_stat_t* curr) {
88  StatWatcher* wrap = static_cast<StatWatcher*>(handle->data);
89  CHECK_EQ(wrap->watcher_, handle);
90  Environment* env = wrap->env();
91  HandleScope handle_scope(env->isolate());
92  Context::Scope context_scope(env->context());
93 
94  FillStatsArray(env->fs_stats_field_array(), curr);
95  FillStatsArray(env->fs_stats_field_array() + 14, prev);
96  Local<Value> arg = Integer::New(env->isolate(), status);
97  wrap->MakeCallback(env->onchange_string(), 1, &arg);
98 }
99 
100 
101 void StatWatcher::New(const FunctionCallbackInfo<Value>& args) {
102  CHECK(args.IsConstructCall());
103  Environment* env = Environment::GetCurrent(args);
104  new StatWatcher(env, args.This());
105 }
106 
107 
108 void StatWatcher::Start(const FunctionCallbackInfo<Value>& args) {
109  CHECK_EQ(args.Length(), 3);
110 
111  StatWatcher* wrap;
112  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
113  node::Utf8Value path(args.GetIsolate(), args[0]);
114  const bool persistent = args[1]->BooleanValue();
115  const uint32_t interval = args[2]->Uint32Value();
116 
117  if (!persistent)
118  uv_unref(reinterpret_cast<uv_handle_t*>(wrap->watcher_));
119  uv_fs_poll_start(wrap->watcher_, Callback, *path, interval);
120  wrap->ClearWeak();
121 }
122 
123 
124 void StatWatcher::Stop(const FunctionCallbackInfo<Value>& args) {
125  StatWatcher* wrap;
126  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
127  Environment* env = wrap->env();
128  Context::Scope context_scope(env->context());
129  wrap->MakeCallback(env->onstop_string(), 0, nullptr);
130  wrap->Stop();
131 }
132 
133 
134 void StatWatcher::Stop() {
135  if (!uv_is_active(reinterpret_cast<uv_handle_t*>(watcher_)))
136  return;
137  uv_fs_poll_stop(watcher_);
138  MakeWeak<StatWatcher>(this);
139 }
140 
141 
142 } // namespace node
QueryWrap * wrap
Definition: cares_wrap.cc:478
int status
Definition: cares_wrap.cc:479
void Initialize(Local< Object > target, Local< Value > unused, Local< Context > context, void *priv)
Definition: node_http2.cc:1172
void FillStatsArray(double *fields, const uv_stat_t *s)
Definition: node_file.cc:463
dtrace t
Definition: v8ustack.d:582
MaybeLocal< Object > New(Isolate *isolate, Local< String > string, enum encoding enc)
Definition: node_buffer.cc:241
int Start(Isolate *isolate, IsolateData *isolate_data, int argc, const char *const *argv, int exec_argc, const char *const *exec_argv)
Definition: node.cc:4536