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 "memory_tracker-inl.h" |
23 |
|
|
#include "node_stat_watcher.h" |
24 |
|
|
#include "async_wrap-inl.h" |
25 |
|
|
#include "env-inl.h" |
26 |
|
|
#include "node_file-inl.h" |
27 |
|
|
#include "util-inl.h" |
28 |
|
|
|
29 |
|
|
#include <cstring> |
30 |
|
|
#include <cstdlib> |
31 |
|
|
|
32 |
|
|
namespace node { |
33 |
|
|
|
34 |
|
|
using v8::Context; |
35 |
|
|
using v8::FunctionCallbackInfo; |
36 |
|
|
using v8::FunctionTemplate; |
37 |
|
|
using v8::HandleScope; |
38 |
|
|
using v8::Integer; |
39 |
|
|
using v8::Local; |
40 |
|
|
using v8::Object; |
41 |
|
|
using v8::Uint32; |
42 |
|
|
using v8::Value; |
43 |
|
|
|
44 |
|
|
|
45 |
|
120 |
void StatWatcher::Initialize(Environment* env, Local<Object> target) { |
46 |
|
240 |
HandleScope scope(env->isolate()); |
47 |
|
|
|
48 |
|
120 |
Local<FunctionTemplate> t = env->NewFunctionTemplate(StatWatcher::New); |
49 |
|
360 |
t->InstanceTemplate()->SetInternalFieldCount( |
50 |
|
120 |
StatWatcher::kInternalFieldCount); |
51 |
|
240 |
t->Inherit(HandleWrap::GetConstructorTemplate(env)); |
52 |
|
|
|
53 |
|
120 |
env->SetProtoMethod(t, "start", StatWatcher::Start); |
54 |
|
|
|
55 |
|
120 |
env->SetConstructorFunction(target, "StatWatcher", t); |
56 |
|
120 |
} |
57 |
|
|
|
58 |
|
|
|
59 |
|
|
StatWatcher::StatWatcher(fs::BindingData* binding_data, |
60 |
|
|
Local<Object> wrap, |
61 |
|
|
bool use_bigint) |
62 |
|
|
: HandleWrap(binding_data->env(), |
63 |
|
|
wrap, |
64 |
|
|
reinterpret_cast<uv_handle_t*>(&watcher_), |
65 |
|
|
AsyncWrap::PROVIDER_STATWATCHER), |
66 |
|
|
use_bigint_(use_bigint), |
67 |
|
|
binding_data_(binding_data) { |
68 |
|
|
CHECK_EQ(0, uv_fs_poll_init(env()->event_loop(), &watcher_)); |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
|
72 |
|
|
void StatWatcher::Callback(uv_fs_poll_t* handle, |
73 |
|
|
int status, |
74 |
|
|
const uv_stat_t* prev, |
75 |
|
|
const uv_stat_t* curr) { |
76 |
|
|
StatWatcher* wrap = ContainerOf(&StatWatcher::watcher_, handle); |
77 |
|
|
Environment* env = wrap->env(); |
78 |
|
|
HandleScope handle_scope(env->isolate()); |
79 |
|
|
Context::Scope context_scope(env->context()); |
80 |
|
|
|
81 |
|
|
Local<Value> arr = fs::FillGlobalStatsArray( |
82 |
|
|
wrap->binding_data_.get(), wrap->use_bigint_, curr); |
83 |
|
|
USE(fs::FillGlobalStatsArray( |
84 |
|
|
wrap->binding_data_.get(), wrap->use_bigint_, prev, true)); |
85 |
|
|
|
86 |
|
|
Local<Value> argv[2] = { Integer::New(env->isolate(), status), arr }; |
87 |
|
|
wrap->MakeCallback(env->onchange_string(), arraysize(argv), argv); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
|
91 |
|
|
void StatWatcher::New(const FunctionCallbackInfo<Value>& args) { |
92 |
|
|
CHECK(args.IsConstructCall()); |
93 |
|
|
fs::BindingData* binding_data = |
94 |
|
|
Environment::GetBindingData<fs::BindingData>(args); |
95 |
|
|
new StatWatcher(binding_data, args.This(), args[0]->IsTrue()); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
// wrap.start(filename, interval) |
99 |
|
|
void StatWatcher::Start(const FunctionCallbackInfo<Value>& args) { |
100 |
|
|
CHECK_EQ(args.Length(), 2); |
101 |
|
|
|
102 |
|
|
StatWatcher* wrap; |
103 |
|
|
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder()); |
104 |
|
|
CHECK(!uv_is_active(wrap->GetHandle())); |
105 |
|
|
|
106 |
|
|
node::Utf8Value path(args.GetIsolate(), args[0]); |
107 |
|
|
CHECK_NOT_NULL(*path); |
108 |
|
|
|
109 |
|
|
CHECK(args[1]->IsUint32()); |
110 |
|
|
const uint32_t interval = args[1].As<Uint32>()->Value(); |
111 |
|
|
|
112 |
|
|
// Note that uv_fs_poll_start does not return ENOENT, we are handling |
113 |
|
|
// mostly memory errors here. |
114 |
|
|
const int err = uv_fs_poll_start(&wrap->watcher_, Callback, *path, interval); |
115 |
|
|
if (err != 0) { |
116 |
|
|
args.GetReturnValue().Set(err); |
117 |
|
|
} |
118 |
|
|
} |
119 |
|
|
|
120 |
✓✗✓✗
|
366 |
} // namespace node |