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