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