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

18788
NODE_MODULE_CONTEXT_AWARE_INTERNAL(fs_event_wrap, node::FSEventWrap::Initialize)