GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/fs_event_wrap.cc Lines: 71 80 88.8 %
Date: 2019-01-07 12:15:22 Branches: 22 36 61.1 %

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 "async_wrap-inl.h"
23
#include "env-inl.h"
24
#include "util-inl.h"
25
#include "node.h"
26
#include "node_internals.h"
27
#include "handle_wrap.h"
28
#include "string_bytes.h"
29
30
31
namespace node {
32
33
using v8::Context;
34
using v8::DontDelete;
35
using v8::DontEnum;
36
using v8::FunctionCallbackInfo;
37
using v8::FunctionTemplate;
38
using v8::HandleScope;
39
using v8::Integer;
40
using v8::Local;
41
using v8::MaybeLocal;
42
using v8::Object;
43
using v8::PropertyAttribute;
44
using v8::ReadOnly;
45
using v8::Signature;
46
using v8::String;
47
using v8::Value;
48
49
namespace {
50
51
class FSEventWrap: public HandleWrap {
52
 public:
53
  static void Initialize(Local<Object> target,
54
                         Local<Value> unused,
55
                         Local<Context> context,
56
                         void* priv);
57
  static void New(const FunctionCallbackInfo<Value>& args);
58
  static void Start(const FunctionCallbackInfo<Value>& args);
59
  static void GetInitialized(const FunctionCallbackInfo<Value>& args);
60
61
  SET_NO_MEMORY_INFO()
62
  SET_MEMORY_INFO_NAME(FSEventWrap)
63
  SET_SELF_SIZE(FSEventWrap)
64
65
 private:
66
  static const encoding kDefaultEncoding = UTF8;
67
68
  FSEventWrap(Environment* env, Local<Object> object);
69
  ~FSEventWrap() override;
70
71
  static void OnEvent(uv_fs_event_t* handle, const char* filename, int events,
72
    int status);
73
74
  uv_fs_event_t handle_;
75
  enum encoding encoding_ = kDefaultEncoding;
76
};
77
78
79
26
FSEventWrap::FSEventWrap(Environment* env, Local<Object> object)
80
    : HandleWrap(env,
81
                 object,
82
                 reinterpret_cast<uv_handle_t*>(&handle_),
83
26
                 AsyncWrap::PROVIDER_FSEVENTWRAP) {
84
26
  MarkAsUninitialized();
85
26
}
86
87
88
52
FSEventWrap::~FSEventWrap() {
89
52
}
90
91
40
void FSEventWrap::GetInitialized(const FunctionCallbackInfo<Value>& args) {
92
40
  FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This());
93
40
  CHECK_NOT_NULL(wrap);
94
120
  args.GetReturnValue().Set(!wrap->IsHandleClosing());
95
40
}
96
97
17
void FSEventWrap::Initialize(Local<Object> target,
98
                             Local<Value> unused,
99
                             Local<Context> context,
100
                             void* priv) {
101
17
  Environment* env = Environment::GetCurrent(context);
102
103
17
  auto fsevent_string = FIXED_ONE_BYTE_STRING(env->isolate(), "FSEvent");
104
17
  Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
105
34
  t->InstanceTemplate()->SetInternalFieldCount(1);
106
17
  t->SetClassName(fsevent_string);
107
108
34
  t->Inherit(AsyncWrap::GetConstructorTemplate(env));
109
17
  env->SetProtoMethod(t, "start", Start);
110
17
  env->SetProtoMethod(t, "close", Close);
111
112
  Local<FunctionTemplate> get_initialized_templ =
113
      FunctionTemplate::New(env->isolate(),
114
                            GetInitialized,
115
                            env->as_external(),
116
34
                            Signature::New(env->isolate(), t));
117
118
51
  t->PrototypeTemplate()->SetAccessorProperty(
119
      FIXED_ONE_BYTE_STRING(env->isolate(), "initialized"),
120
      get_initialized_templ,
121
      Local<FunctionTemplate>(),
122
51
      static_cast<PropertyAttribute>(ReadOnly | DontDelete | v8::DontEnum));
123
124
  target->Set(env->context(),
125
              fsevent_string,
126
68
              t->GetFunction(context).ToLocalChecked()).FromJust();
127
17
}
128
129
130
26
void FSEventWrap::New(const FunctionCallbackInfo<Value>& args) {
131
26
  CHECK(args.IsConstructCall());
132
26
  Environment* env = Environment::GetCurrent(args);
133
26
  new FSEventWrap(env, args.This());
134
26
}
135
136
// wrap.start(filename, persistent, recursive, encoding)
137
16
void FSEventWrap::Start(const FunctionCallbackInfo<Value>& args) {
138
16
  Environment* env = Environment::GetCurrent(args);
139
140
16
  FSEventWrap* wrap = Unwrap<FSEventWrap>(args.This());
141
16
  CHECK_NOT_NULL(wrap);
142
16
  CHECK(wrap->IsHandleClosing());  // Check that Start() has not been called.
143
144
16
  const int argc = args.Length();
145
16
  CHECK_GE(argc, 4);
146
147
16
  BufferValue path(env->isolate(), args[0]);
148
16
  CHECK_NOT_NULL(*path);
149
150
16
  unsigned int flags = 0;
151
32
  if (args[2]->IsTrue())
152
    flags |= UV_FS_EVENT_RECURSIVE;
153
154
32
  wrap->encoding_ = ParseEncoding(env->isolate(), args[3], kDefaultEncoding);
155
156
16
  int err = uv_fs_event_init(wrap->env()->event_loop(), &wrap->handle_);
157
16
  if (err != 0) {
158
    return args.GetReturnValue().Set(err);
159
  }
160
161
16
  err = uv_fs_event_start(&wrap->handle_, OnEvent, *path, flags);
162
16
  wrap->MarkAsInitialized();
163
164
16
  if (err != 0) {
165
1
    FSEventWrap::Close(args);
166
2
    return args.GetReturnValue().Set(err);
167
  }
168
169
  // Check for persistent argument
170
30
  if (!args[1]->IsTrue()) {
171
2
    uv_unref(reinterpret_cast<uv_handle_t*>(&wrap->handle_));
172
  }
173
174
30
  args.GetReturnValue().Set(err);
175
}
176
177
178
11
void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename,
179
    int events, int status) {
180
11
  FSEventWrap* wrap = static_cast<FSEventWrap*>(handle->data);
181
11
  Environment* env = wrap->env();
182
183
11
  HandleScope handle_scope(env->isolate());
184
11
  Context::Scope context_scope(env->context());
185
186
22
  CHECK_EQ(wrap->persistent().IsEmpty(), false);
187
188
  // We're in a bind here. libuv can set both UV_RENAME and UV_CHANGE but
189
  // the Node API only lets us pass a single event to JS land.
190
  //
191
  // The obvious solution is to run the callback twice, once for each event.
192
  // However, since the second event is not allowed to fire if the handle is
193
  // closed after the first event, and since there is no good way to detect
194
  // closed handles, that option is out.
195
  //
196
  // For now, ignore the UV_CHANGE event if UV_RENAME is also set. Make the
197
  // assumption that a rename implicitly means an attribute change. Not too
198
  // unreasonable, right? Still, we should revisit this before v1.0.
199
  Local<String> event_string;
200
11
  if (status) {
201
    event_string = String::Empty(env->isolate());
202
11
  } else if (events & UV_RENAME) {
203
7
    event_string = env->rename_string();
204
4
  } else if (events & UV_CHANGE) {
205
4
    event_string = env->change_string();
206
  } else {
207
    CHECK(0 && "bad fs events flag");
208
  }
209
210
  Local<Value> argv[] = {
211
    Integer::New(env->isolate(), status),
212
    event_string,
213
    Null(env->isolate())
214
44
  };
215
216
11
  if (filename != nullptr) {
217
    Local<Value> error;
218
    MaybeLocal<Value> fn = StringBytes::Encode(env->isolate(),
219
                                               filename,
220
                                               wrap->encoding_,
221
11
                                               &error);
222
11
    if (fn.IsEmpty()) {
223
      argv[0] = Integer::New(env->isolate(), UV_EINVAL);
224
      argv[2] = StringBytes::Encode(env->isolate(),
225
                                    filename,
226
                                    strlen(filename),
227
                                    BUFFER,
228
                                    &error).ToLocalChecked();
229
    } else {
230
11
      argv[2] = fn.ToLocalChecked();
231
    }
232
  }
233
234
22
  wrap->MakeCallback(env->onchange_string(), arraysize(argv), argv);
235
11
}
236
237
}  // anonymous namespace
238
}  // namespace node
239
240
3596
NODE_MODULE_CONTEXT_AWARE_INTERNAL(fs_event_wrap, node::FSEventWrap::Initialize)