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