GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/src/node_stat_watcher.cc Lines: 50 57 87.7 %
Date: 2016-07-12 Branches: 8 14 57.1 %

Line Exec Source
1
#include "node_stat_watcher.h"
2
#include "async-wrap.h"
3
#include "async-wrap-inl.h"
4
#include "env.h"
5
#include "env-inl.h"
6
#include "util.h"
7
#include "util-inl.h"
8
9
#include <string.h>
10
#include <stdlib.h>
11
12
namespace node {
13
14
using v8::Context;
15
using v8::FunctionCallbackInfo;
16
using v8::FunctionTemplate;
17
using v8::HandleScope;
18
using v8::Integer;
19
using v8::Local;
20
using v8::Object;
21
using v8::Value;
22
23
24
1554
void StatWatcher::Initialize(Environment* env, Local<Object> target) {
25
3108
  HandleScope scope(env->isolate());
26
27
3108
  Local<FunctionTemplate> t = env->NewFunctionTemplate(StatWatcher::New);
28
3108
  t->InstanceTemplate()->SetInternalFieldCount(1);
29
1554
  t->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"));
30
31
1554
  env->SetProtoMethod(t, "start", StatWatcher::Start);
32
1554
  env->SetProtoMethod(t, "stop", StatWatcher::Stop);
33
34
7770
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "StatWatcher"),
35
1554
              t->GetFunction());
36
1554
}
37
38
39
static void Delete(uv_handle_t* handle) {
40
  delete reinterpret_cast<uv_fs_poll_t*>(handle);
41
}
42
43
44
4
StatWatcher::StatWatcher(Environment* env, Local<Object> wrap)
45
    : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_STATWATCHER),
46
4
      watcher_(new uv_fs_poll_t) {
47
4
  MakeWeak<StatWatcher>(this);
48
8
  uv_fs_poll_init(env->event_loop(), watcher_);
49
4
  watcher_->data = static_cast<void*>(this);
50
4
}
51
52
53
StatWatcher::~StatWatcher() {
54
  Stop();
55
  uv_close(reinterpret_cast<uv_handle_t*>(watcher_), Delete);
56
}
57
58
59
3
void StatWatcher::Callback(uv_fs_poll_t* handle,
60
                           int status,
61
                           const uv_stat_t* prev,
62
                           const uv_stat_t* curr) {
63
3
  StatWatcher* wrap = static_cast<StatWatcher*>(handle->data);
64
3
  CHECK_EQ(wrap->watcher_, handle);
65
3
  Environment* env = wrap->env();
66
6
  HandleScope handle_scope(env->isolate());
67
9
  Context::Scope context_scope(env->context());
68
  Local<Value> argv[] = {
69
    BuildStatsObject(env, curr),
70
    BuildStatsObject(env, prev),
71
    Integer::New(env->isolate(), status)
72
6
  };
73
6
  wrap->MakeCallback(env->onchange_string(), arraysize(argv), argv);
74
3
}
75
76
77
4
void StatWatcher::New(const FunctionCallbackInfo<Value>& args) {
78
4
  CHECK(args.IsConstructCall());
79
4
  Environment* env = Environment::GetCurrent(args);
80
4
  new StatWatcher(env, args.This());
81
4
}
82
83
84
4
void StatWatcher::Start(const FunctionCallbackInfo<Value>& args) {
85
4
  CHECK_EQ(args.Length(), 3);
86
87
  StatWatcher* wrap;
88
4
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
89
12
  node::Utf8Value path(args.GetIsolate(), args[0]);
90
4
  const bool persistent = args[1]->BooleanValue();
91
4
  const uint32_t interval = args[2]->Uint32Value();
92
93
4
  if (!persistent)
94
1
    uv_unref(reinterpret_cast<uv_handle_t*>(wrap->watcher_));
95
4
  uv_fs_poll_start(wrap->watcher_, Callback, *path, interval);
96
8
  wrap->ClearWeak();
97
}
98
99
100
4
void StatWatcher::Stop(const FunctionCallbackInfo<Value>& args) {
101
  StatWatcher* wrap;
102
4
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
103
4
  Environment* env = wrap->env();
104
12
  Context::Scope context_scope(env->context());
105
4
  wrap->MakeCallback(env->onstop_string(), 0, nullptr);
106
4
  wrap->Stop();
107
}
108
109
110
4
void StatWatcher::Stop() {
111
4
  if (!uv_is_active(reinterpret_cast<uv_handle_t*>(watcher_)))
112
    return;
113
4
  uv_fs_poll_stop(watcher_);
114
4
  MakeWeak<StatWatcher>(this);
115
}
116
117
118
}  // namespace node