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