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