GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_stat_watcher.cc Lines: 59 61 96.7 %
Date: 2017-11-19 Branches: 10 16 62.5 %

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-inl.h"
25
#include "util-inl.h"
26
27
#include <string.h>
28
#include <stdlib.h>
29
30
namespace node {
31
32
using v8::Context;
33
using v8::FunctionCallbackInfo;
34
using v8::FunctionTemplate;
35
using v8::HandleScope;
36
using v8::Integer;
37
using v8::Local;
38
using v8::Object;
39
using v8::String;
40
using v8::Value;
41
42
43
3287
void StatWatcher::Initialize(Environment* env, Local<Object> target) {
44
3287
  HandleScope scope(env->isolate());
45
46
3287
  Local<FunctionTemplate> t = env->NewFunctionTemplate(StatWatcher::New);
47
6574
  t->InstanceTemplate()->SetInternalFieldCount(1);
48
  Local<String> statWatcherString =
49
3287
      FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher");
50
3287
  t->SetClassName(statWatcherString);
51
52
3287
  AsyncWrap::AddWrapMethods(env, t);
53
3287
  env->SetProtoMethod(t, "start", StatWatcher::Start);
54
3287
  env->SetProtoMethod(t, "stop", StatWatcher::Stop);
55
56
6574
  target->Set(statWatcherString, t->GetFunction());
57
3287
}
58
59
60
static void Delete(uv_handle_t* handle) {
61
  delete reinterpret_cast<uv_fs_poll_t*>(handle);
62
}
63
64
65
15
StatWatcher::StatWatcher(Environment* env, Local<Object> wrap)
66
    : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_STATWATCHER),
67
15
      watcher_(new uv_fs_poll_t) {
68
15
  MakeWeak<StatWatcher>(this);
69
15
  Wrap(wrap, this);
70
15
  uv_fs_poll_init(env->event_loop(), watcher_);
71
15
  watcher_->data = static_cast<void*>(this);
72
15
}
73
74
75
3
StatWatcher::~StatWatcher() {
76
1
  Stop();
77
1
  uv_close(reinterpret_cast<uv_handle_t*>(watcher_), Delete);
78
2
}
79
80
81
3
void StatWatcher::Callback(uv_fs_poll_t* handle,
82
                           int status,
83
                           const uv_stat_t* prev,
84
                           const uv_stat_t* curr) {
85
3
  StatWatcher* wrap = static_cast<StatWatcher*>(handle->data);
86
3
  CHECK_EQ(wrap->watcher_, handle);
87
3
  Environment* env = wrap->env();
88
3
  HandleScope handle_scope(env->isolate());
89
3
  Context::Scope context_scope(env->context());
90
91
3
  FillStatsArray(env->fs_stats_field_array(), curr);
92
3
  FillStatsArray(env->fs_stats_field_array() + 14, prev);
93
3
  Local<Value> arg = Integer::New(env->isolate(), status);
94
6
  wrap->MakeCallback(env->onchange_string(), 1, &arg);
95
3
}
96
97
98
15
void StatWatcher::New(const FunctionCallbackInfo<Value>& args) {
99
15
  CHECK(args.IsConstructCall());
100
15
  Environment* env = Environment::GetCurrent(args);
101
15
  new StatWatcher(env, args.This());
102
15
}
103
104
105
14
void StatWatcher::Start(const FunctionCallbackInfo<Value>& args) {
106
14
  CHECK_EQ(args.Length(), 3);
107
108
  StatWatcher* wrap;
109
28
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
110
14
  node::Utf8Value path(args.GetIsolate(), args[0]);
111
28
  const bool persistent = args[1]->BooleanValue();
112
28
  const uint32_t interval = args[2]->Uint32Value();
113
114
14
  if (!persistent)
115
1
    uv_unref(reinterpret_cast<uv_handle_t*>(wrap->watcher_));
116
14
  uv_fs_poll_start(wrap->watcher_, Callback, *path, interval);
117
14
  wrap->ClearWeak();
118
}
119
120
121
14
void StatWatcher::Stop(const FunctionCallbackInfo<Value>& args) {
122
  StatWatcher* wrap;
123
28
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
124
14
  Environment* env = wrap->env();
125
14
  Context::Scope context_scope(env->context());
126
14
  wrap->MakeCallback(env->onstop_string(), 0, nullptr);
127
14
  wrap->Stop();
128
}
129
130
131
15
void StatWatcher::Stop() {
132
15
  if (!uv_is_active(reinterpret_cast<uv_handle_t*>(watcher_)))
133
16
    return;
134
14
  uv_fs_poll_stop(watcher_);
135
14
  MakeWeak<StatWatcher>(this);
136
}
137
138
139
}  // namespace node