GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_stat_watcher.cc Lines: 45 46 97.8 %
Date: 2020-09-06 22:14:11 Branches: 10 20 50.0 %

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

13419
}  // namespace node