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

15
  if (wrap == nullptr || wrap->initialized_ == false)
218
1
    return;
219
14
  wrap->initialized_ = false;
220
221
14
  HandleWrap::Close(args);
222
}
223
224
}  // anonymous namespace
225
}  // namespace node
226
227
2441
NODE_MODULE_CONTEXT_AWARE_BUILTIN(fs_event_wrap, node::FSEventWrap::Initialize)