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.h" |
26 |
|
|
#include "node_file.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::String; |
42 |
|
|
using v8::Uint32; |
43 |
|
|
using v8::Value; |
44 |
|
|
|
45 |
|
|
|
46 |
|
5193 |
void StatWatcher::Initialize(Environment* env, Local<Object> target) { |
47 |
|
5193 |
HandleScope scope(env->isolate()); |
48 |
|
|
|
49 |
|
5193 |
Local<FunctionTemplate> t = env->NewFunctionTemplate(StatWatcher::New); |
50 |
|
10386 |
t->InstanceTemplate()->SetInternalFieldCount(1); |
51 |
|
|
Local<String> statWatcherString = |
52 |
|
5193 |
FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"); |
53 |
|
5193 |
t->SetClassName(statWatcherString); |
54 |
|
10386 |
t->Inherit(HandleWrap::GetConstructorTemplate(env)); |
55 |
|
|
|
56 |
|
5193 |
env->SetProtoMethod(t, "start", StatWatcher::Start); |
57 |
|
|
|
58 |
|
|
target->Set(env->context(), statWatcherString, |
59 |
|
25965 |
t->GetFunction(env->context()).ToLocalChecked()).Check(); |
60 |
|
5193 |
} |
61 |
|
|
|
62 |
|
|
|
63 |
|
20 |
StatWatcher::StatWatcher(Environment* env, |
64 |
|
|
Local<Object> wrap, |
65 |
|
|
bool use_bigint) |
66 |
|
|
: HandleWrap(env, |
67 |
|
|
wrap, |
68 |
|
|
reinterpret_cast<uv_handle_t*>(&watcher_), |
69 |
|
|
AsyncWrap::PROVIDER_STATWATCHER), |
70 |
|
20 |
use_bigint_(use_bigint) { |
71 |
✗✓ |
20 |
CHECK_EQ(0, uv_fs_poll_init(env->event_loop(), &watcher_)); |
72 |
|
20 |
} |
73 |
|
|
|
74 |
|
|
|
75 |
|
16 |
void StatWatcher::Callback(uv_fs_poll_t* handle, |
76 |
|
|
int status, |
77 |
|
|
const uv_stat_t* prev, |
78 |
|
|
const uv_stat_t* curr) { |
79 |
|
16 |
StatWatcher* wrap = ContainerOf(&StatWatcher::watcher_, handle); |
80 |
|
16 |
Environment* env = wrap->env(); |
81 |
|
16 |
HandleScope handle_scope(env->isolate()); |
82 |
|
16 |
Context::Scope context_scope(env->context()); |
83 |
|
|
|
84 |
|
16 |
Local<Value> arr = fs::FillGlobalStatsArray(env, wrap->use_bigint_, curr); |
85 |
|
16 |
USE(fs::FillGlobalStatsArray(env, wrap->use_bigint_, prev, true)); |
86 |
|
|
|
87 |
|
32 |
Local<Value> argv[2] = { Integer::New(env->isolate(), status), arr }; |
88 |
|
32 |
wrap->MakeCallback(env->onchange_string(), arraysize(argv), argv); |
89 |
|
16 |
} |
90 |
|
|
|
91 |
|
|
|
92 |
|
20 |
void StatWatcher::New(const FunctionCallbackInfo<Value>& args) { |
93 |
✗✓ |
20 |
CHECK(args.IsConstructCall()); |
94 |
|
20 |
Environment* env = Environment::GetCurrent(args); |
95 |
|
60 |
new StatWatcher(env, args.This(), args[0]->IsTrue()); |
96 |
|
20 |
} |
97 |
|
|
|
98 |
|
|
// wrap.start(filename, interval) |
99 |
|
19 |
void StatWatcher::Start(const FunctionCallbackInfo<Value>& args) { |
100 |
✗✓ |
19 |
CHECK_EQ(args.Length(), 2); |
101 |
|
|
|
102 |
|
|
StatWatcher* wrap; |
103 |
✗✓ |
38 |
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder()); |
104 |
✗✓ |
19 |
CHECK(!uv_is_active(wrap->GetHandle())); |
105 |
|
|
|
106 |
|
19 |
node::Utf8Value path(args.GetIsolate(), args[0]); |
107 |
✗✓ |
19 |
CHECK_NOT_NULL(*path); |
108 |
|
|
|
109 |
✗✓ |
38 |
CHECK(args[1]->IsUint32()); |
110 |
|
57 |
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 |
|
19 |
const int err = uv_fs_poll_start(&wrap->watcher_, Callback, *path, interval); |
115 |
✗✓ |
19 |
if (err != 0) { |
116 |
|
|
args.GetReturnValue().Set(err); |
117 |
|
19 |
} |
118 |
|
|
} |
119 |
|
|
|
120 |
✓✗✓✗
|
15144 |
} // namespace node |