GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
#include "node_dir.h" |
||
2 |
#include "node_external_reference.h" |
||
3 |
#include "node_file-inl.h" |
||
4 |
#include "node_process-inl.h" |
||
5 |
#include "memory_tracker-inl.h" |
||
6 |
#include "util.h" |
||
7 |
|||
8 |
#include "tracing/trace_event.h" |
||
9 |
|||
10 |
#include "string_bytes.h" |
||
11 |
|||
12 |
#include <fcntl.h> |
||
13 |
#include <sys/types.h> |
||
14 |
#include <sys/stat.h> |
||
15 |
#include <cstring> |
||
16 |
#include <cerrno> |
||
17 |
#include <climits> |
||
18 |
|||
19 |
#include <memory> |
||
20 |
|||
21 |
namespace node { |
||
22 |
|||
23 |
namespace fs_dir { |
||
24 |
|||
25 |
using fs::FSReqAfterScope; |
||
26 |
using fs::FSReqBase; |
||
27 |
using fs::FSReqWrapSync; |
||
28 |
using fs::GetReqWrap; |
||
29 |
|||
30 |
using v8::Array; |
||
31 |
using v8::Context; |
||
32 |
using v8::FunctionCallbackInfo; |
||
33 |
using v8::FunctionTemplate; |
||
34 |
using v8::HandleScope; |
||
35 |
using v8::Integer; |
||
36 |
using v8::Isolate; |
||
37 |
using v8::Local; |
||
38 |
using v8::MaybeLocal; |
||
39 |
using v8::Null; |
||
40 |
using v8::Number; |
||
41 |
using v8::Object; |
||
42 |
using v8::ObjectTemplate; |
||
43 |
using v8::Value; |
||
44 |
|||
45 |
2 |
static const char* get_dir_func_name_by_type(uv_fs_type req_type) { |
|
46 |
✓✗✗✗ |
2 |
switch (req_type) { |
47 |
#define FS_TYPE_TO_NAME(type, name) \ |
||
48 |
case UV_FS_##type: \ |
||
49 |
return name; |
||
50 |
2 |
FS_TYPE_TO_NAME(OPENDIR, "opendir") |
|
51 |
FS_TYPE_TO_NAME(READDIR, "readdir") |
||
52 |
FS_TYPE_TO_NAME(CLOSEDIR, "closedir") |
||
53 |
#undef FS_TYPE_TO_NAME |
||
54 |
default: |
||
55 |
return "unknow"; |
||
56 |
} |
||
57 |
} |
||
58 |
|||
59 |
#define TRACE_NAME(name) "fs_dir.sync." #name |
||
60 |
#define GET_TRACE_ENABLED \ |
||
61 |
(*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( \ |
||
62 |
TRACING_CATEGORY_NODE2(fs_dir, sync)) != 0) |
||
63 |
#define FS_DIR_SYNC_TRACE_BEGIN(syscall, ...) \ |
||
64 |
if (GET_TRACE_ENABLED) \ |
||
65 |
TRACE_EVENT_BEGIN(TRACING_CATEGORY_NODE2(fs_dir, sync), \ |
||
66 |
TRACE_NAME(syscall), \ |
||
67 |
##__VA_ARGS__); |
||
68 |
#define FS_DIR_SYNC_TRACE_END(syscall, ...) \ |
||
69 |
if (GET_TRACE_ENABLED) \ |
||
70 |
TRACE_EVENT_END(TRACING_CATEGORY_NODE2(fs_dir, sync), \ |
||
71 |
TRACE_NAME(syscall), \ |
||
72 |
##__VA_ARGS__); |
||
73 |
|||
74 |
#define FS_DIR_ASYNC_TRACE_BEGIN0(fs_type, id) \ |
||
75 |
TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(TRACING_CATEGORY_NODE2(fs_dir, async), \ |
||
76 |
get_dir_func_name_by_type(fs_type), \ |
||
77 |
id); |
||
78 |
#define FS_DIR_ASYNC_TRACE_END0(fs_type, id) \ |
||
79 |
TRACE_EVENT_NESTABLE_ASYNC_END0(TRACING_CATEGORY_NODE2(fs_dir, async), \ |
||
80 |
get_dir_func_name_by_type(fs_type), \ |
||
81 |
id); |
||
82 |
|||
83 |
#define FS_DIR_ASYNC_TRACE_BEGIN1(fs_type, id, name, value) \ |
||
84 |
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE2(fs_dir, async), \ |
||
85 |
get_dir_func_name_by_type(fs_type), \ |
||
86 |
id, \ |
||
87 |
name, \ |
||
88 |
value); |
||
89 |
|||
90 |
#define FS_DIR_ASYNC_TRACE_END1(fs_type, id, name, value) \ |
||
91 |
TRACE_EVENT_NESTABLE_ASYNC_END1(TRACING_CATEGORY_NODE2(fs_dir, async), \ |
||
92 |
get_dir_func_name_by_type(fs_type), \ |
||
93 |
id, \ |
||
94 |
name, \ |
||
95 |
value); |
||
96 |
|||
97 |
179 |
DirHandle::DirHandle(Environment* env, Local<Object> obj, uv_dir_t* dir) |
|
98 |
: AsyncWrap(env, obj, AsyncWrap::PROVIDER_DIRHANDLE), |
||
99 |
179 |
dir_(dir) { |
|
100 |
179 |
MakeWeak(); |
|
101 |
|||
102 |
179 |
dir_->nentries = 0; |
|
103 |
179 |
dir_->dirents = nullptr; |
|
104 |
179 |
} |
|
105 |
|||
106 |
179 |
DirHandle* DirHandle::New(Environment* env, uv_dir_t* dir) { |
|
107 |
Local<Object> obj; |
||
108 |
179 |
if (!env->dir_instance_template() |
|
109 |
179 |
->NewInstance(env->context()) |
|
110 |
✗✓ | 179 |
.ToLocal(&obj)) { |
111 |
return nullptr; |
||
112 |
} |
||
113 |
|||
114 |
179 |
return new DirHandle(env, obj, dir); |
|
115 |
} |
||
116 |
|||
117 |
void DirHandle::New(const FunctionCallbackInfo<Value>& args) { |
||
118 |
CHECK(args.IsConstructCall()); |
||
119 |
} |
||
120 |
|||
121 |
716 |
DirHandle::~DirHandle() { |
|
122 |
✗✓ | 358 |
CHECK(!closing_); // We should not be deleting while explicitly closing! |
123 |
358 |
GCClose(); // Close synchronously and emit warning |
|
124 |
✗✓ | 358 |
CHECK(closed_); // We have to be closed at the point |
125 |
716 |
} |
|
126 |
|||
127 |
void DirHandle::MemoryInfo(MemoryTracker* tracker) const { |
||
128 |
tracker->TrackFieldWithSize("dir", sizeof(*dir_)); |
||
129 |
} |
||
130 |
|||
131 |
// Close the directory handle if it hasn't already been closed. A process |
||
132 |
// warning will be emitted using a SetImmediate to avoid calling back to |
||
133 |
// JS during GC. If closing the fd fails at this point, a fatal exception |
||
134 |
// will crash the process immediately. |
||
135 |
179 |
inline void DirHandle::GCClose() { |
|
136 |
✓✓ | 179 |
if (closed_) return; |
137 |
uv_fs_t req; |
||
138 |
13 |
int ret = uv_fs_closedir(nullptr, &req, dir_, nullptr); |
|
139 |
13 |
uv_fs_req_cleanup(&req); |
|
140 |
13 |
closing_ = false; |
|
141 |
13 |
closed_ = true; |
|
142 |
|||
143 |
struct err_detail { int ret; }; |
||
144 |
|||
145 |
13 |
err_detail detail { ret }; |
|
146 |
|||
147 |
✗✓ | 13 |
if (ret < 0) { |
148 |
// Do not unref this |
||
149 |
env()->SetImmediate([detail](Environment* env) { |
||
150 |
const char* msg = "Closing directory handle on garbage collection failed"; |
||
151 |
// This exception will end up being fatal for the process because |
||
152 |
// it is being thrown from within the SetImmediate handler and |
||
153 |
// there is no JS stack to bubble it to. In other words, tearing |
||
154 |
// down the process is the only reasonable thing we can do here. |
||
155 |
HandleScope handle_scope(env->isolate()); |
||
156 |
env->ThrowUVException(detail.ret, "close", msg); |
||
157 |
}); |
||
158 |
return; |
||
159 |
} |
||
160 |
|||
161 |
// If the close was successful, we still want to emit a process warning |
||
162 |
// to notify that the file descriptor was gc'd. We want to be noisy about |
||
163 |
// this because not explicitly closing the DirHandle is a bug. |
||
164 |
|||
165 |
13 |
env()->SetImmediate([](Environment* env) { |
|
166 |
ProcessEmitWarning(env, |
||
167 |
1 |
"Closing directory handle on garbage collection"); |
|
168 |
1 |
}, CallbackFlags::kUnrefed); |
|
169 |
} |
||
170 |
|||
171 |
82 |
void AfterClose(uv_fs_t* req) { |
|
172 |
82 |
FSReqBase* req_wrap = FSReqBase::from_req(req); |
|
173 |
164 |
FSReqAfterScope after(req_wrap, req); |
|
174 |
✓✓✗✓ |
87 |
FS_DIR_ASYNC_TRACE_END1( |
175 |
req->fs_type, req_wrap, "result", static_cast<int>(req->result)) |
||
176 |
✓✗ | 82 |
if (after.Proceed()) |
177 |
164 |
req_wrap->Resolve(Undefined(req_wrap->env()->isolate())); |
|
178 |
82 |
} |
|
179 |
|||
180 |
166 |
void DirHandle::Close(const FunctionCallbackInfo<Value>& args) { |
|
181 |
166 |
Environment* env = Environment::GetCurrent(args); |
|
182 |
|||
183 |
166 |
const int argc = args.Length(); |
|
184 |
✗✓ | 166 |
CHECK_GE(argc, 1); |
185 |
|||
186 |
DirHandle* dir; |
||
187 |
✗✓ | 166 |
ASSIGN_OR_RETURN_UNWRAP(&dir, args.Holder()); |
188 |
|||
189 |
166 |
dir->closing_ = false; |
|
190 |
166 |
dir->closed_ = true; |
|
191 |
|||
192 |
166 |
FSReqBase* req_wrap_async = GetReqWrap(args, 0); |
|
193 |
✓✓ | 166 |
if (req_wrap_async != nullptr) { // close(req) |
194 |
✓✓✗✓ |
87 |
FS_DIR_ASYNC_TRACE_BEGIN0(UV_FS_CLOSEDIR, req_wrap_async) |
195 |
82 |
AsyncCall(env, req_wrap_async, args, "closedir", UTF8, AfterClose, |
|
196 |
uv_fs_closedir, dir->dir()); |
||
197 |
} else { // close(undefined, ctx) |
||
198 |
✗✓ | 84 |
CHECK_EQ(argc, 2); |
199 |
168 |
FSReqWrapSync req_wrap_sync; |
|
200 |
✗✓✗✗ ✗✗ |
84 |
FS_DIR_SYNC_TRACE_BEGIN(closedir); |
201 |
168 |
SyncCall(env, args[1], &req_wrap_sync, "closedir", uv_fs_closedir, |
|
202 |
dir->dir()); |
||
203 |
✗✓✗✗ ✗✗ |
84 |
FS_DIR_SYNC_TRACE_END(closedir); |
204 |
} |
||
205 |
} |
||
206 |
|||
207 |
155 |
static MaybeLocal<Array> DirentListToArray( |
|
208 |
Environment* env, |
||
209 |
uv_dirent_t* ents, |
||
210 |
int num, |
||
211 |
enum encoding encoding, |
||
212 |
Local<Value>* err_out) { |
||
213 |
310 |
MaybeStackBuffer<Local<Value>, 64> entries(num * 2); |
|
214 |
|||
215 |
// Return an array of all read filenames. |
||
216 |
155 |
int j = 0; |
|
217 |
✓✓ | 650 |
for (int i = 0; i < num; i++) { |
218 |
Local<Value> filename; |
||
219 |
Local<Value> error; |
||
220 |
495 |
const size_t namelen = strlen(ents[i].name); |
|
221 |
495 |
if (!StringBytes::Encode(env->isolate(), |
|
222 |
495 |
ents[i].name, |
|
223 |
namelen, |
||
224 |
encoding, |
||
225 |
✗✓ | 990 |
&error).ToLocal(&filename)) { |
226 |
*err_out = error; |
||
227 |
return MaybeLocal<Array>(); |
||
228 |
} |
||
229 |
|||
230 |
495 |
entries[j++] = filename; |
|
231 |
990 |
entries[j++] = Integer::New(env->isolate(), ents[i].type); |
|
232 |
} |
||
233 |
|||
234 |
310 |
return Array::New(env->isolate(), entries.out(), j); |
|
235 |
} |
||
236 |
|||
237 |
144 |
static void AfterDirRead(uv_fs_t* req) { |
|
238 |
144 |
BaseObjectPtr<FSReqBase> req_wrap { FSReqBase::from_req(req) }; |
|
239 |
144 |
FSReqAfterScope after(req_wrap.get(), req); |
|
240 |
✓✓✗✓ |
149 |
FS_DIR_ASYNC_TRACE_END1( |
241 |
req->fs_type, req_wrap, "result", static_cast<int>(req->result)) |
||
242 |
✗✓ | 144 |
if (!after.Proceed()) { |
243 |
return; |
||
244 |
} |
||
245 |
|||
246 |
144 |
Environment* env = req_wrap->env(); |
|
247 |
144 |
Isolate* isolate = env->isolate(); |
|
248 |
|||
249 |
✓✓ | 144 |
if (req->result == 0) { |
250 |
// Done |
||
251 |
64 |
Local<Value> done = Null(isolate); |
|
252 |
64 |
after.Clear(); |
|
253 |
64 |
req_wrap->Resolve(done); |
|
254 |
64 |
return; |
|
255 |
} |
||
256 |
|||
257 |
80 |
uv_dir_t* dir = static_cast<uv_dir_t*>(req->ptr); |
|
258 |
|||
259 |
Local<Value> error; |
||
260 |
Local<Array> js_array; |
||
261 |
80 |
if (!DirentListToArray(env, |
|
262 |
dir->dirents, |
||
263 |
80 |
static_cast<int>(req->result), |
|
264 |
req_wrap->encoding(), |
||
265 |
80 |
&error) |
|
266 |
✗✓ | 80 |
.ToLocal(&js_array)) { |
267 |
// Clear libuv resources *before* delivering results to JS land because |
||
268 |
// that can schedule another operation on the same uv_dir_t. Ditto below. |
||
269 |
after.Clear(); |
||
270 |
return req_wrap->Reject(error); |
||
271 |
} |
||
272 |
|||
273 |
80 |
after.Clear(); |
|
274 |
80 |
req_wrap->Resolve(js_array); |
|
275 |
} |
||
276 |
|||
277 |
|||
278 |
291 |
void DirHandle::Read(const FunctionCallbackInfo<Value>& args) { |
|
279 |
291 |
Environment* env = Environment::GetCurrent(args); |
|
280 |
291 |
Isolate* isolate = env->isolate(); |
|
281 |
|||
282 |
291 |
const int argc = args.Length(); |
|
283 |
✗✓ | 291 |
CHECK_GE(argc, 3); |
284 |
|||
285 |
291 |
const enum encoding encoding = ParseEncoding(isolate, args[0], UTF8); |
|
286 |
|||
287 |
DirHandle* dir; |
||
288 |
✗✓ | 363 |
ASSIGN_OR_RETURN_UNWRAP(&dir, args.Holder()); |
289 |
|||
290 |
✗✓ | 291 |
CHECK(args[1]->IsNumber()); |
291 |
582 |
uint64_t buffer_size = static_cast<uint64_t>(args[1].As<Number>()->Value()); |
|
292 |
|||
293 |
✓✓ | 291 |
if (buffer_size != dir->dirents_.size()) { |
294 |
163 |
dir->dirents_.resize(buffer_size); |
|
295 |
163 |
dir->dir_->nentries = buffer_size; |
|
296 |
163 |
dir->dir_->dirents = dir->dirents_.data(); |
|
297 |
} |
||
298 |
|||
299 |
291 |
FSReqBase* req_wrap_async = GetReqWrap(args, 2); |
|
300 |
✓✓ | 291 |
if (req_wrap_async != nullptr) { // dir.read(encoding, bufferSize, req) |
301 |
✓✓✗✓ |
149 |
FS_DIR_ASYNC_TRACE_BEGIN0(UV_FS_READDIR, req_wrap_async) |
302 |
144 |
AsyncCall(env, req_wrap_async, args, "readdir", encoding, |
|
303 |
AfterDirRead, uv_fs_readdir, dir->dir()); |
||
304 |
} else { // dir.read(encoding, bufferSize, undefined, ctx) |
||
305 |
✗✓ | 147 |
CHECK_EQ(argc, 4); |
306 |
147 |
FSReqWrapSync req_wrap_sync; |
|
307 |
✗✓✗✗ ✗✗ |
147 |
FS_DIR_SYNC_TRACE_BEGIN(readdir); |
308 |
294 |
int err = SyncCall(env, args[3], &req_wrap_sync, "readdir", uv_fs_readdir, |
|
309 |
dir->dir()); |
||
310 |
✗✓✗✗ ✗✗ |
147 |
FS_DIR_SYNC_TRACE_END(readdir); |
311 |
✗✓ | 147 |
if (err < 0) { |
312 |
return; // syscall failed, no need to continue, error info is in ctx |
||
313 |
} |
||
314 |
|||
315 |
✓✓ | 147 |
if (req_wrap_sync.req.result == 0) { |
316 |
// Done |
||
317 |
72 |
Local<Value> done = Null(isolate); |
|
318 |
72 |
args.GetReturnValue().Set(done); |
|
319 |
72 |
return; |
|
320 |
} |
||
321 |
|||
322 |
✗✓ | 75 |
CHECK_GE(req_wrap_sync.req.result, 0); |
323 |
|||
324 |
Local<Value> error; |
||
325 |
Local<Array> js_array; |
||
326 |
75 |
if (!DirentListToArray(env, |
|
327 |
75 |
dir->dir()->dirents, |
|
328 |
75 |
static_cast<int>(req_wrap_sync.req.result), |
|
329 |
encoding, |
||
330 |
75 |
&error) |
|
331 |
✗✓ | 75 |
.ToLocal(&js_array)) { |
332 |
Local<Object> ctx = args[2].As<Object>(); |
||
333 |
USE(ctx->Set(env->context(), env->error_string(), error)); |
||
334 |
return; |
||
335 |
} |
||
336 |
|||
337 |
150 |
args.GetReturnValue().Set(js_array); |
|
338 |
} |
||
339 |
} |
||
340 |
|||
341 |
86 |
void AfterOpenDir(uv_fs_t* req) { |
|
342 |
86 |
FSReqBase* req_wrap = FSReqBase::from_req(req); |
|
343 |
86 |
FSReqAfterScope after(req_wrap, req); |
|
344 |
✓✓✓✓ |
92 |
FS_DIR_ASYNC_TRACE_END1( |
345 |
req->fs_type, req_wrap, "result", static_cast<int>(req->result)) |
||
346 |
✓✓ | 86 |
if (!after.Proceed()) { |
347 |
1 |
return; |
|
348 |
} |
||
349 |
|||
350 |
85 |
Environment* env = req_wrap->env(); |
|
351 |
|||
352 |
85 |
uv_dir_t* dir = static_cast<uv_dir_t*>(req->ptr); |
|
353 |
85 |
DirHandle* handle = DirHandle::New(env, dir); |
|
354 |
|||
355 |
170 |
req_wrap->Resolve(handle->object().As<Value>()); |
|
356 |
} |
||
357 |
|||
358 |
181 |
static void OpenDir(const FunctionCallbackInfo<Value>& args) { |
|
359 |
181 |
Environment* env = Environment::GetCurrent(args); |
|
360 |
181 |
Isolate* isolate = env->isolate(); |
|
361 |
|||
362 |
181 |
const int argc = args.Length(); |
|
363 |
✗✓ | 181 |
CHECK_GE(argc, 3); |
364 |
|||
365 |
181 |
BufferValue path(isolate, args[0]); |
|
366 |
✗✓ | 181 |
CHECK_NOT_NULL(*path); |
367 |
|||
368 |
181 |
const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); |
|
369 |
|||
370 |
181 |
FSReqBase* req_wrap_async = GetReqWrap(args, 2); |
|
371 |
✓✓ | 181 |
if (req_wrap_async != nullptr) { // openDir(path, encoding, req) |
372 |
✓✓✓✓ |
92 |
FS_DIR_ASYNC_TRACE_BEGIN1( |
373 |
UV_FS_OPENDIR, req_wrap_async, "path", TRACE_STR_COPY(*path)) |
||
374 |
86 |
AsyncCall(env, req_wrap_async, args, "opendir", encoding, AfterOpenDir, |
|
375 |
uv_fs_opendir, *path); |
||
376 |
} else { // openDir(path, encoding, undefined, ctx) |
||
377 |
✗✓ | 95 |
CHECK_EQ(argc, 4); |
378 |
95 |
FSReqWrapSync req_wrap_sync; |
|
379 |
✗✓✗✗ ✗✗ |
95 |
FS_DIR_SYNC_TRACE_BEGIN(opendir); |
380 |
190 |
int result = SyncCall(env, args[3], &req_wrap_sync, "opendir", |
|
381 |
uv_fs_opendir, *path); |
||
382 |
✗✓✗✗ ✗✗ |
95 |
FS_DIR_SYNC_TRACE_END(opendir); |
383 |
✓✓ | 95 |
if (result < 0) { |
384 |
1 |
return; // syscall failed, no need to continue, error info is in ctx |
|
385 |
} |
||
386 |
|||
387 |
94 |
uv_fs_t* req = &req_wrap_sync.req; |
|
388 |
94 |
uv_dir_t* dir = static_cast<uv_dir_t*>(req->ptr); |
|
389 |
94 |
DirHandle* handle = DirHandle::New(env, dir); |
|
390 |
|||
391 |
188 |
args.GetReturnValue().Set(handle->object().As<Value>()); |
|
392 |
} |
||
393 |
} |
||
394 |
|||
395 |
789 |
void Initialize(Local<Object> target, |
|
396 |
Local<Value> unused, |
||
397 |
Local<Context> context, |
||
398 |
void* priv) { |
||
399 |
789 |
Environment* env = Environment::GetCurrent(context); |
|
400 |
789 |
Isolate* isolate = env->isolate(); |
|
401 |
|||
402 |
789 |
SetMethod(context, target, "opendir", OpenDir); |
|
403 |
|||
404 |
// Create FunctionTemplate for DirHandle |
||
405 |
789 |
Local<FunctionTemplate> dir = NewFunctionTemplate(isolate, DirHandle::New); |
|
406 |
789 |
dir->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
|
407 |
789 |
SetProtoMethod(isolate, dir, "read", DirHandle::Read); |
|
408 |
789 |
SetProtoMethod(isolate, dir, "close", DirHandle::Close); |
|
409 |
789 |
Local<ObjectTemplate> dirt = dir->InstanceTemplate(); |
|
410 |
789 |
dirt->SetInternalFieldCount(DirHandle::kInternalFieldCount); |
|
411 |
789 |
SetConstructorFunction(context, target, "DirHandle", dir); |
|
412 |
789 |
env->set_dir_instance_template(dirt); |
|
413 |
789 |
} |
|
414 |
|||
415 |
5527 |
void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
|
416 |
5527 |
registry->Register(OpenDir); |
|
417 |
5527 |
registry->Register(DirHandle::New); |
|
418 |
5527 |
registry->Register(DirHandle::Read); |
|
419 |
5527 |
registry->Register(DirHandle::Close); |
|
420 |
5527 |
} |
|
421 |
|||
422 |
} // namespace fs_dir |
||
423 |
|||
424 |
} // end namespace node |
||
425 |
|||
426 |
5597 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(fs_dir, node::fs_dir::Initialize) |
|
427 |
5527 |
NODE_MODULE_EXTERNAL_REFERENCE(fs_dir, node::fs_dir::RegisterExternalReferences) |
Generated by: GCOVR (Version 4.2) |