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