GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
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 |
#include "node_file.h" // NOLINT(build/include_inline) |
||
22 |
#include "node_file-inl.h" |
||
23 |
#include "aliased_buffer.h" |
||
24 |
#include "memory_tracker-inl.h" |
||
25 |
#include "node_buffer.h" |
||
26 |
#include "node_process.h" |
||
27 |
#include "node_stat_watcher.h" |
||
28 |
#include "util-inl.h" |
||
29 |
|||
30 |
#include "tracing/trace_event.h" |
||
31 |
|||
32 |
#include "req_wrap-inl.h" |
||
33 |
#include "stream_base-inl.h" |
||
34 |
#include "string_bytes.h" |
||
35 |
|||
36 |
#include <fcntl.h> |
||
37 |
#include <sys/types.h> |
||
38 |
#include <sys/stat.h> |
||
39 |
#include <cstring> |
||
40 |
#include <cerrno> |
||
41 |
#include <climits> |
||
42 |
|||
43 |
#if defined(__MINGW32__) || defined(_MSC_VER) |
||
44 |
# include <io.h> |
||
45 |
#endif |
||
46 |
|||
47 |
#include <memory> |
||
48 |
|||
49 |
namespace node { |
||
50 |
|||
51 |
namespace fs { |
||
52 |
|||
53 |
using v8::Array; |
||
54 |
using v8::BigInt; |
||
55 |
using v8::Boolean; |
||
56 |
using v8::Context; |
||
57 |
using v8::EscapableHandleScope; |
||
58 |
using v8::Function; |
||
59 |
using v8::FunctionCallbackInfo; |
||
60 |
using v8::FunctionTemplate; |
||
61 |
using v8::HandleScope; |
||
62 |
using v8::Int32; |
||
63 |
using v8::Integer; |
||
64 |
using v8::Isolate; |
||
65 |
using v8::Local; |
||
66 |
using v8::MaybeLocal; |
||
67 |
using v8::Number; |
||
68 |
using v8::Object; |
||
69 |
using v8::ObjectTemplate; |
||
70 |
using v8::Promise; |
||
71 |
using v8::String; |
||
72 |
using v8::Symbol; |
||
73 |
using v8::Uint32; |
||
74 |
using v8::Undefined; |
||
75 |
using v8::Value; |
||
76 |
|||
77 |
#ifndef S_ISDIR |
||
78 |
# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) |
||
79 |
#endif |
||
80 |
|||
81 |
#ifdef __POSIX__ |
||
82 |
constexpr char kPathSeparator = '/'; |
||
83 |
#else |
||
84 |
const char* const kPathSeparator = "\\/"; |
||
85 |
#endif |
||
86 |
|||
87 |
10 |
std::string Basename(const std::string& str, const std::string& extension) { |
|
88 |
// Remove everything leading up to and including the final path separator. |
||
89 |
10 |
std::string::size_type pos = str.find_last_of(kPathSeparator); |
|
90 |
|||
91 |
// Starting index for the resulting string |
||
92 |
10 |
std::size_t start_pos = 0; |
|
93 |
// String size to return |
||
94 |
10 |
std::size_t str_size = str.size(); |
|
95 |
✓✗ | 10 |
if (pos != std::string::npos) { |
96 |
10 |
start_pos = pos + 1; |
|
97 |
10 |
str_size -= start_pos; |
|
98 |
} |
||
99 |
|||
100 |
// Strip away the extension, if any. |
||
101 |
✓✗✗✓ ✗✓ |
20 |
if (str_size >= extension.size() && |
102 |
10 |
str.compare(str.size() - extension.size(), |
|
103 |
extension.size(), extension) == 0) { |
||
104 |
str_size -= extension.size(); |
||
105 |
} |
||
106 |
|||
107 |
10 |
return str.substr(start_pos, str_size); |
|
108 |
} |
||
109 |
|||
110 |
133948 |
inline int64_t GetOffset(Local<Value> value) { |
|
111 |
✓✓ | 136104 |
return IsSafeJsInt(value) ? value.As<Integer>()->Value() : -1; |
112 |
} |
||
113 |
|||
114 |
#define TRACE_NAME(name) "fs.sync." #name |
||
115 |
#define GET_TRACE_ENABLED \ |
||
116 |
(*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \ |
||
117 |
(TRACING_CATEGORY_NODE2(fs, sync)) != 0) |
||
118 |
#define FS_SYNC_TRACE_BEGIN(syscall, ...) \ |
||
119 |
if (GET_TRACE_ENABLED) \ |
||
120 |
TRACE_EVENT_BEGIN(TRACING_CATEGORY_NODE2(fs, sync), TRACE_NAME(syscall), \ |
||
121 |
##__VA_ARGS__); |
||
122 |
#define FS_SYNC_TRACE_END(syscall, ...) \ |
||
123 |
if (GET_TRACE_ENABLED) \ |
||
124 |
TRACE_EVENT_END(TRACING_CATEGORY_NODE2(fs, sync), TRACE_NAME(syscall), \ |
||
125 |
##__VA_ARGS__); |
||
126 |
|||
127 |
// We sometimes need to convert a C++ lambda function to a raw C-style function. |
||
128 |
// This is helpful, because ReqWrap::Dispatch() does not recognize lambda |
||
129 |
// functions, and thus does not wrap them properly. |
||
130 |
typedef void(*uv_fs_callback_t)(uv_fs_t*); |
||
131 |
|||
132 |
|||
133 |
void FSContinuationData::MemoryInfo(MemoryTracker* tracker) const { |
||
134 |
tracker->TrackField("paths", paths_); |
||
135 |
} |
||
136 |
|||
137 |
FileHandleReadWrap::~FileHandleReadWrap() = default; |
||
138 |
|||
139 |
FSReqBase::~FSReqBase() = default; |
||
140 |
|||
141 |
2 |
void FSReqBase::MemoryInfo(MemoryTracker* tracker) const { |
|
142 |
2 |
tracker->TrackField("continuation_data", continuation_data_); |
|
143 |
2 |
} |
|
144 |
|||
145 |
// The FileHandle object wraps a file descriptor and will close it on garbage |
||
146 |
// collection if necessary. If that happens, a process warning will be |
||
147 |
// emitted (or a fatal exception will occur if the fd cannot be closed.) |
||
148 |
670 |
FileHandle::FileHandle(BindingData* binding_data, |
|
149 |
670 |
Local<Object> obj, int fd) |
|
150 |
: AsyncWrap(binding_data->env(), obj, AsyncWrap::PROVIDER_FILEHANDLE), |
||
151 |
StreamBase(env()), |
||
152 |
fd_(fd), |
||
153 |
670 |
binding_data_(binding_data) { |
|
154 |
670 |
MakeWeak(); |
|
155 |
670 |
StreamBase::AttachToObject(GetObject()); |
|
156 |
670 |
} |
|
157 |
|||
158 |
670 |
FileHandle* FileHandle::New(BindingData* binding_data, |
|
159 |
int fd, Local<Object> obj) { |
||
160 |
670 |
Environment* env = binding_data->env(); |
|
161 |
✓✓✗✓ ✗✓ |
2650 |
if (obj.IsEmpty() && !env->fd_constructor_template() |
162 |
1980 |
->NewInstance(env->context()) |
|
163 |
655 |
.ToLocal(&obj)) { |
|
164 |
return nullptr; |
||
165 |
} |
||
166 |
670 |
return new FileHandle(binding_data, obj, fd); |
|
167 |
} |
||
168 |
|||
169 |
15 |
void FileHandle::New(const FunctionCallbackInfo<Value>& args) { |
|
170 |
15 |
BindingData* binding_data = Environment::GetBindingData<BindingData>(args); |
|
171 |
15 |
Environment* env = binding_data->env(); |
|
172 |
✗✓ | 15 |
CHECK(args.IsConstructCall()); |
173 |
✗✓ | 30 |
CHECK(args[0]->IsInt32()); |
174 |
|||
175 |
FileHandle* handle = |
||
176 |
45 |
FileHandle::New(binding_data, args[0].As<Int32>()->Value(), args.This()); |
|
177 |
✗✓ | 15 |
if (handle == nullptr) return; |
178 |
✓✗ | 30 |
if (args[1]->IsNumber()) |
179 |
60 |
handle->read_offset_ = args[1]->IntegerValue(env->context()).FromJust(); |
|
180 |
✓✗ | 30 |
if (args[2]->IsNumber()) |
181 |
60 |
handle->read_length_ = args[2]->IntegerValue(env->context()).FromJust(); |
|
182 |
} |
||
183 |
|||
184 |
1959 |
FileHandle::~FileHandle() { |
|
185 |
✗✓ | 653 |
CHECK(!closing_); // We should not be deleting while explicitly closing! |
186 |
653 |
Close(); // Close synchronously and emit warning |
|
187 |
✗✓ | 653 |
CHECK(closed_); // We have to be closed at the point |
188 |
1306 |
} |
|
189 |
|||
190 |
int FileHandle::DoWrite(WriteWrap* w, |
||
191 |
uv_buf_t* bufs, |
||
192 |
size_t count, |
||
193 |
uv_stream_t* send_handle) { |
||
194 |
return UV_ENOSYS; // Not implemented (yet). |
||
195 |
} |
||
196 |
|||
197 |
void FileHandle::MemoryInfo(MemoryTracker* tracker) const { |
||
198 |
tracker->TrackField("current_read", current_read_); |
||
199 |
} |
||
200 |
|||
201 |
10 |
FileHandle::TransferMode FileHandle::GetTransferMode() const { |
|
202 |
✓✗✓✗ ✗✓ |
10 |
return reading_ || closing_ || closed_ ? |
203 |
10 |
TransferMode::kUntransferable : TransferMode::kTransferable; |
|
204 |
} |
||
205 |
|||
206 |
6 |
std::unique_ptr<worker::TransferData> FileHandle::TransferForMessaging() { |
|
207 |
✗✓ | 6 |
CHECK_NE(GetTransferMode(), TransferMode::kUntransferable); |
208 |
12 |
auto ret = std::make_unique<TransferData>(fd_); |
|
209 |
6 |
closed_ = true; |
|
210 |
12 |
return ret; |
|
211 |
} |
||
212 |
|||
213 |
6 |
FileHandle::TransferData::TransferData(int fd) : fd_(fd) {} |
|
214 |
|||
215 |
18 |
FileHandle::TransferData::~TransferData() { |
|
216 |
✓✓ | 6 |
if (fd_ > 0) { |
217 |
uv_fs_t close_req; |
||
218 |
✗✓ | 4 |
CHECK_EQ(0, uv_fs_close(nullptr, &close_req, fd_, nullptr)); |
219 |
4 |
uv_fs_req_cleanup(&close_req); |
|
220 |
} |
||
221 |
12 |
} |
|
222 |
|||
223 |
2 |
BaseObjectPtr<BaseObject> FileHandle::TransferData::Deserialize( |
|
224 |
Environment* env, |
||
225 |
v8::Local<v8::Context> context, |
||
226 |
std::unique_ptr<worker::TransferData> self) { |
||
227 |
2 |
BindingData* bd = Environment::GetBindingData<BindingData>(context); |
|
228 |
✗✓ | 2 |
if (bd == nullptr) return {}; |
229 |
|||
230 |
2 |
int fd = fd_; |
|
231 |
2 |
fd_ = -1; |
|
232 |
2 |
return BaseObjectPtr<BaseObject> { FileHandle::New(bd, fd) }; |
|
233 |
} |
||
234 |
|||
235 |
// Close the file descriptor if it hasn't already been closed. A process |
||
236 |
// warning will be emitted using a SetImmediate to avoid calling back to |
||
237 |
// JS during GC. If closing the fd fails at this point, a fatal exception |
||
238 |
// will crash the process immediately. |
||
239 |
653 |
inline void FileHandle::Close() { |
|
240 |
✓✓ | 1288 |
if (closed_) return; |
241 |
uv_fs_t req; |
||
242 |
18 |
int ret = uv_fs_close(env()->event_loop(), &req, fd_, nullptr); |
|
243 |
18 |
uv_fs_req_cleanup(&req); |
|
244 |
|||
245 |
struct err_detail { int ret; int fd; }; |
||
246 |
|||
247 |
18 |
err_detail detail { ret, fd_ }; |
|
248 |
|||
249 |
18 |
AfterClose(); |
|
250 |
|||
251 |
✗✓ | 18 |
if (ret < 0) { |
252 |
// Do not unref this |
||
253 |
env()->SetImmediate([detail](Environment* env) { |
||
254 |
char msg[70]; |
||
255 |
snprintf(msg, arraysize(msg), |
||
256 |
"Closing file descriptor %d on garbage collection failed", |
||
257 |
detail.fd); |
||
258 |
// This exception will end up being fatal for the process because |
||
259 |
// it is being thrown from within the SetImmediate handler and |
||
260 |
// there is no JS stack to bubble it to. In other words, tearing |
||
261 |
// down the process is the only reasonable thing we can do here. |
||
262 |
HandleScope handle_scope(env->isolate()); |
||
263 |
env->ThrowUVException(detail.ret, "close", msg); |
||
264 |
}); |
||
265 |
return; |
||
266 |
} |
||
267 |
|||
268 |
// If the close was successful, we still want to emit a process warning |
||
269 |
// to notify that the file descriptor was gc'd. We want to be noisy about |
||
270 |
// this because not explicitly closing the FileHandle is a bug. |
||
271 |
|||
272 |
38 |
env()->SetImmediate([detail](Environment* env) { |
|
273 |
ProcessEmitWarning(env, |
||
274 |
"Closing file descriptor %d on garbage collection", |
||
275 |
2 |
detail.fd); |
|
276 |
✓✗ | 2 |
if (env->filehandle_close_warning()) { |
277 |
2 |
env->set_filehandle_close_warning(false); |
|
278 |
4 |
ProcessEmitDeprecationWarning( |
|
279 |
env, |
||
280 |
"Closing a FileHandle object on garbage collection is deprecated. " |
||
281 |
"Please close FileHandle objects explicitly using " |
||
282 |
"FileHandle.prototype.close(). In the future, an error will be " |
||
283 |
"thrown if a file descriptor is closed during garbage collection.", |
||
284 |
"DEP0137").IsNothing(); |
||
285 |
} |
||
286 |
20 |
}, CallbackFlags::kUnrefed); |
|
287 |
} |
||
288 |
|||
289 |
637 |
void FileHandle::CloseReq::Resolve() { |
|
290 |
637 |
Isolate* isolate = env()->isolate(); |
|
291 |
1257 |
HandleScope scope(isolate); |
|
292 |
637 |
Context::Scope context_scope(env()->context()); |
|
293 |
1274 |
InternalCallbackScope callback_scope(this); |
|
294 |
1274 |
Local<Promise> promise = promise_.Get(isolate); |
|
295 |
637 |
Local<Promise::Resolver> resolver = promise.As<Promise::Resolver>(); |
|
296 |
1911 |
resolver->Resolve(env()->context(), Undefined(isolate)).Check(); |
|
297 |
620 |
} |
|
298 |
|||
299 |
1 |
void FileHandle::CloseReq::Reject(Local<Value> reason) { |
|
300 |
1 |
Isolate* isolate = env()->isolate(); |
|
301 |
2 |
HandleScope scope(isolate); |
|
302 |
1 |
Context::Scope context_scope(env()->context()); |
|
303 |
2 |
InternalCallbackScope callback_scope(this); |
|
304 |
2 |
Local<Promise> promise = promise_.Get(isolate); |
|
305 |
1 |
Local<Promise::Resolver> resolver = promise.As<Promise::Resolver>(); |
|
306 |
3 |
resolver->Reject(env()->context(), reason).Check(); |
|
307 |
1 |
} |
|
308 |
|||
309 |
638 |
FileHandle* FileHandle::CloseReq::file_handle() { |
|
310 |
638 |
Isolate* isolate = env()->isolate(); |
|
311 |
1276 |
HandleScope scope(isolate); |
|
312 |
1276 |
Local<Value> val = ref_.Get(isolate); |
|
313 |
638 |
Local<Object> obj = val.As<Object>(); |
|
314 |
1276 |
return Unwrap<FileHandle>(obj); |
|
315 |
} |
||
316 |
|||
317 |
638 |
FileHandle::CloseReq::CloseReq(Environment* env, |
|
318 |
Local<Object> obj, |
||
319 |
Local<Promise> promise, |
||
320 |
638 |
Local<Value> ref) |
|
321 |
1276 |
: ReqWrap(env, obj, AsyncWrap::PROVIDER_FILEHANDLECLOSEREQ) { |
|
322 |
638 |
promise_.Reset(env->isolate(), promise); |
|
323 |
638 |
ref_.Reset(env->isolate(), ref); |
|
324 |
638 |
} |
|
325 |
|||
326 |
3105 |
FileHandle::CloseReq::~CloseReq() { |
|
327 |
621 |
uv_fs_req_cleanup(req()); |
|
328 |
621 |
promise_.Reset(); |
|
329 |
621 |
ref_.Reset(); |
|
330 |
1242 |
} |
|
331 |
|||
332 |
void FileHandle::CloseReq::MemoryInfo(MemoryTracker* tracker) const { |
||
333 |
tracker->TrackField("promise", promise_); |
||
334 |
tracker->TrackField("ref", ref_); |
||
335 |
} |
||
336 |
|||
337 |
|||
338 |
|||
339 |
// Closes this FileHandle asynchronously and returns a Promise that will be |
||
340 |
// resolved when the callback is invoked, or rejects with a UVException if |
||
341 |
// there was a problem closing the fd. This is the preferred mechanism for |
||
342 |
// closing the FD object even tho the object will attempt to close |
||
343 |
// automatically on gc. |
||
344 |
638 |
MaybeLocal<Promise> FileHandle::ClosePromise() { |
|
345 |
638 |
Isolate* isolate = env()->isolate(); |
|
346 |
638 |
EscapableHandleScope scope(isolate); |
|
347 |
638 |
Local<Context> context = env()->context(); |
|
348 |
638 |
auto maybe_resolver = Promise::Resolver::New(context); |
|
349 |
✗✓ | 638 |
CHECK(!maybe_resolver.IsEmpty()); |
350 |
638 |
Local<Promise::Resolver> resolver = maybe_resolver.ToLocalChecked(); |
|
351 |
638 |
Local<Promise> promise = resolver.As<Promise>(); |
|
352 |
✗✓ | 638 |
CHECK(!reading_); |
353 |
✓✗✓✗ |
638 |
if (!closed_ && !closing_) { |
354 |
638 |
closing_ = true; |
|
355 |
Local<Object> close_req_obj; |
||
356 |
✗✓ | 1276 |
if (!env() |
357 |
1276 |
->fdclose_constructor_template() |
|
358 |
1914 |
->NewInstance(env()->context()) |
|
359 |
638 |
.ToLocal(&close_req_obj)) { |
|
360 |
return MaybeLocal<Promise>(); |
||
361 |
} |
||
362 |
1276 |
CloseReq* req = new CloseReq(env(), close_req_obj, promise, object()); |
|
363 |
1914 |
auto AfterClose = uv_fs_callback_t{[](uv_fs_t* req) { |
|
364 |
1259 |
std::unique_ptr<CloseReq> close(CloseReq::from_req(req)); |
|
365 |
✗✓ | 638 |
CHECK_NOT_NULL(close); |
366 |
638 |
close->file_handle()->AfterClose(); |
|
367 |
638 |
Isolate* isolate = close->env()->isolate(); |
|
368 |
✓✓ | 638 |
if (req->result < 0) { |
369 |
2 |
HandleScope handle_scope(isolate); |
|
370 |
1 |
close->Reject( |
|
371 |
2 |
UVException(isolate, static_cast<int>(req->result), "close")); |
|
372 |
} else { |
||
373 |
637 |
close->Resolve(); |
|
374 |
} |
||
375 |
2535 |
}}; |
|
376 |
638 |
int ret = req->Dispatch(uv_fs_close, fd_, AfterClose); |
|
377 |
✗✓ | 638 |
if (ret < 0) { |
378 |
req->Reject(UVException(isolate, ret, "close")); |
||
379 |
delete req; |
||
380 |
638 |
} |
|
381 |
} else { |
||
382 |
// Already closed. Just reject the promise immediately |
||
383 |
resolver->Reject(context, UVException(isolate, UV_EBADF, "close")) |
||
384 |
.Check(); |
||
385 |
} |
||
386 |
638 |
return scope.Escape(promise); |
|
387 |
} |
||
388 |
|||
389 |
638 |
void FileHandle::Close(const FunctionCallbackInfo<Value>& args) { |
|
390 |
FileHandle* fd; |
||
391 |
✗✓ | 638 |
ASSIGN_OR_RETURN_UNWRAP(&fd, args.Holder()); |
392 |
Local<Promise> ret; |
||
393 |
✗✓ | 1276 |
if (!fd->ClosePromise().ToLocal(&ret)) return; |
394 |
1276 |
args.GetReturnValue().Set(ret); |
|
395 |
} |
||
396 |
|||
397 |
|||
398 |
8 |
void FileHandle::ReleaseFD(const FunctionCallbackInfo<Value>& args) { |
|
399 |
FileHandle* fd; |
||
400 |
✗✓ | 8 |
ASSIGN_OR_RETURN_UNWRAP(&fd, args.Holder()); |
401 |
// Just act as if this FileHandle has been closed. |
||
402 |
8 |
fd->AfterClose(); |
|
403 |
} |
||
404 |
|||
405 |
|||
406 |
664 |
void FileHandle::AfterClose() { |
|
407 |
664 |
closing_ = false; |
|
408 |
664 |
closed_ = true; |
|
409 |
664 |
fd_ = -1; |
|
410 |
✗✓✗✗ ✗✓ |
664 |
if (reading_ && !persistent().IsEmpty()) |
411 |
EmitRead(UV_EOF); |
||
412 |
664 |
} |
|
413 |
|||
414 |
void FileHandleReadWrap::MemoryInfo(MemoryTracker* tracker) const { |
||
415 |
tracker->TrackField("buffer", buffer_); |
||
416 |
tracker->TrackField("file_handle", this->file_handle_); |
||
417 |
} |
||
418 |
|||
419 |
15 |
FileHandleReadWrap::FileHandleReadWrap(FileHandle* handle, Local<Object> obj) |
|
420 |
: ReqWrap(handle->env(), obj, AsyncWrap::PROVIDER_FSREQCALLBACK), |
||
421 |
15 |
file_handle_(handle) {} |
|
422 |
|||
423 |
212 |
int FileHandle::ReadStart() { |
|
424 |
✓✗✗✓ ✗✓ |
212 |
if (!IsAlive() || IsClosing()) |
425 |
return UV_EOF; |
||
426 |
|||
427 |
212 |
reading_ = true; |
|
428 |
|||
429 |
✗✓ | 212 |
if (current_read_) |
430 |
return 0; |
||
431 |
|||
432 |
424 |
BaseObjectPtr<FileHandleReadWrap> read_wrap; |
|
433 |
|||
434 |
✓✓ | 212 |
if (read_length_ == 0) { |
435 |
5 |
EmitRead(UV_EOF); |
|
436 |
5 |
return 0; |
|
437 |
} |
||
438 |
|||
439 |
{ |
||
440 |
// Create a new FileHandleReadWrap or re-use one. |
||
441 |
// Either way, we need these two scopes for AsyncReset() or otherwise |
||
442 |
// for creating the new instance. |
||
443 |
414 |
HandleScope handle_scope(env()->isolate()); |
|
444 |
✓✗ | 414 |
AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(this); |
445 |
|||
446 |
207 |
auto& freelist = binding_data_->file_handle_read_wrap_freelist; |
|
447 |
✓✓ | 207 |
if (freelist.size() > 0) { |
448 |
192 |
read_wrap = std::move(freelist.back()); |
|
449 |
192 |
freelist.pop_back(); |
|
450 |
// Use a fresh async resource. |
||
451 |
// Lifetime is ensured via AsyncWrap::resource_. |
||
452 |
192 |
Local<Object> resource = Object::New(env()->isolate()); |
|
453 |
384 |
USE(resource->Set( |
|
454 |
1152 |
env()->context(), env()->handle_string(), read_wrap->object())); |
|
455 |
192 |
read_wrap->AsyncReset(resource); |
|
456 |
192 |
read_wrap->file_handle_ = this; |
|
457 |
} else { |
||
458 |
Local<Object> wrap_obj; |
||
459 |
✗✓ | 30 |
if (!env() |
460 |
30 |
->filehandlereadwrap_template() |
|
461 |
45 |
->NewInstance(env()->context()) |
|
462 |
15 |
.ToLocal(&wrap_obj)) { |
|
463 |
return UV_EBUSY; |
||
464 |
} |
||
465 |
✓✗ | 15 |
read_wrap = MakeDetachedBaseObject<FileHandleReadWrap>(this, wrap_obj); |
466 |
} |
||
467 |
} |
||
468 |
207 |
int64_t recommended_read = 65536; |
|
469 |
✓✓✓✓ |
207 |
if (read_length_ >= 0 && read_length_ <= recommended_read) |
470 |
7 |
recommended_read = read_length_; |
|
471 |
|||
472 |
207 |
read_wrap->buffer_ = EmitAlloc(recommended_read); |
|
473 |
|||
474 |
207 |
current_read_ = std::move(read_wrap); |
|
475 |
|||
476 |
414 |
current_read_->Dispatch(uv_fs_read, |
|
477 |
fd_, |
||
478 |
207 |
¤t_read_->buffer_, |
|
479 |
1, |
||
480 |
read_offset_, |
||
481 |
621 |
uv_fs_callback_t{[](uv_fs_t* req) { |
|
482 |
FileHandle* handle; |
||
483 |
{ |
||
484 |
207 |
FileHandleReadWrap* req_wrap = FileHandleReadWrap::from_req(req); |
|
485 |
207 |
handle = req_wrap->file_handle_; |
|
486 |
✗✓ | 207 |
CHECK_EQ(handle->current_read_.get(), req_wrap); |
487 |
} |
||
488 |
|||
489 |
// ReadStart() checks whether current_read_ is set to determine whether |
||
490 |
// a read is in progress. Moving it into a local variable makes sure that |
||
491 |
// the ReadStart() call below doesn't think we're still actively reading. |
||
492 |
BaseObjectPtr<FileHandleReadWrap> read_wrap = |
||
493 |
414 |
std::move(handle->current_read_); |
|
494 |
|||
495 |
207 |
ssize_t result = req->result; |
|
496 |
207 |
uv_buf_t buffer = read_wrap->buffer_; |
|
497 |
|||
498 |
207 |
uv_fs_req_cleanup(req); |
|
499 |
|||
500 |
// Push the read wrap back to the freelist, or let it be destroyed |
||
501 |
// once we’re exiting the current scope. |
||
502 |
207 |
constexpr size_t kWantedFreelistFill = 100; |
|
503 |
207 |
auto& freelist = handle->binding_data_->file_handle_read_wrap_freelist; |
|
504 |
✓✗ | 207 |
if (freelist.size() < kWantedFreelistFill) { |
505 |
207 |
read_wrap->Reset(); |
|
506 |
207 |
freelist.emplace_back(std::move(read_wrap)); |
|
507 |
} |
||
508 |
|||
509 |
✓✓ | 207 |
if (result >= 0) { |
510 |
// Read at most as many bytes as we originally planned to. |
||
511 |
✓✓✗✓ |
206 |
if (handle->read_length_ >= 0 && handle->read_length_ < result) |
512 |
result = handle->read_length_; |
||
513 |
|||
514 |
// If we read data and we have an expected length, decrease it by |
||
515 |
// how much we have read. |
||
516 |
✓✓ | 206 |
if (handle->read_length_ >= 0) |
517 |
9 |
handle->read_length_ -= result; |
|
518 |
|||
519 |
// If we have an offset, increase it by how much we have read. |
||
520 |
✓✓ | 206 |
if (handle->read_offset_ >= 0) |
521 |
204 |
handle->read_offset_ += result; |
|
522 |
} |
||
523 |
|||
524 |
// Reading 0 bytes from a file always means EOF, or that we reached |
||
525 |
// the end of the requested range. |
||
526 |
✓✓ | 207 |
if (result == 0) |
527 |
7 |
result = UV_EOF; |
|
528 |
|||
529 |
207 |
handle->EmitRead(result, buffer); |
|
530 |
|||
531 |
// Start over, if EmitRead() didn’t tell us to stop. |
||
532 |
✗✓ | 207 |
if (handle->reading_) |
533 |
handle->ReadStart(); |
||
534 |
828 |
}}); |
|
535 |
|||
536 |
207 |
return 0; |
|
537 |
} |
||
538 |
|||
539 |
227 |
int FileHandle::ReadStop() { |
|
540 |
227 |
reading_ = false; |
|
541 |
227 |
return 0; |
|
542 |
} |
||
543 |
|||
544 |
typedef SimpleShutdownWrap<ReqWrap<uv_fs_t>> FileHandleCloseWrap; |
||
545 |
|||
546 |
ShutdownWrap* FileHandle::CreateShutdownWrap(Local<Object> object) { |
||
547 |
return new FileHandleCloseWrap(this, object); |
||
548 |
} |
||
549 |
|||
550 |
int FileHandle::DoShutdown(ShutdownWrap* req_wrap) { |
||
551 |
FileHandleCloseWrap* wrap = static_cast<FileHandleCloseWrap*>(req_wrap); |
||
552 |
closing_ = true; |
||
553 |
wrap->Dispatch(uv_fs_close, fd_, uv_fs_callback_t{[](uv_fs_t* req) { |
||
554 |
FileHandleCloseWrap* wrap = static_cast<FileHandleCloseWrap*>( |
||
555 |
FileHandleCloseWrap::from_req(req)); |
||
556 |
FileHandle* handle = static_cast<FileHandle*>(wrap->stream()); |
||
557 |
handle->AfterClose(); |
||
558 |
|||
559 |
int result = static_cast<int>(req->result); |
||
560 |
uv_fs_req_cleanup(req); |
||
561 |
wrap->Done(result); |
||
562 |
}}); |
||
563 |
|||
564 |
return 0; |
||
565 |
} |
||
566 |
|||
567 |
|||
568 |
3936 |
void FSReqCallback::Reject(Local<Value> reject) { |
|
569 |
3936 |
MakeCallback(env()->oncomplete_string(), 1, &reject); |
|
570 |
3936 |
} |
|
571 |
|||
572 |
3133 |
void FSReqCallback::ResolveStat(const uv_stat_t* stat) { |
|
573 |
3133 |
Resolve(FillGlobalStatsArray(binding_data(), use_bigint(), stat)); |
|
574 |
3133 |
} |
|
575 |
|||
576 |
46796 |
void FSReqCallback::Resolve(Local<Value> value) { |
|
577 |
Local<Value> argv[2] { |
||
578 |
Null(env()->isolate()), |
||
579 |
value |
||
580 |
93592 |
}; |
|
581 |
MakeCallback(env()->oncomplete_string(), |
||
582 |
131551 |
value->IsUndefined() ? 1 : arraysize(argv), |
|
583 |
✓✓ | 84755 |
argv); |
584 |
46792 |
} |
|
585 |
|||
586 |
50732 |
void FSReqCallback::SetReturnValue(const FunctionCallbackInfo<Value>& args) { |
|
587 |
101464 |
args.GetReturnValue().SetUndefined(); |
|
588 |
50732 |
} |
|
589 |
|||
590 |
50732 |
void NewFSReqCallback(const FunctionCallbackInfo<Value>& args) { |
|
591 |
✗✓ | 50732 |
CHECK(args.IsConstructCall()); |
592 |
50732 |
BindingData* binding_data = Environment::GetBindingData<BindingData>(args); |
|
593 |
152196 |
new FSReqCallback(binding_data, args.This(), args[0]->IsTrue()); |
|
594 |
50732 |
} |
|
595 |
|||
596 |
53547 |
FSReqAfterScope::FSReqAfterScope(FSReqBase* wrap, uv_fs_t* req) |
|
597 |
: wrap_(wrap), |
||
598 |
req_(req), |
||
599 |
handle_scope_(wrap->env()->isolate()), |
||
600 |
53547 |
context_scope_(wrap->env()->context()) { |
|
601 |
✗✓ | 53547 |
CHECK_EQ(wrap_->req(), req); |
602 |
53547 |
} |
|
603 |
|||
604 |
160629 |
FSReqAfterScope::~FSReqAfterScope() { |
|
605 |
53543 |
Clear(); |
|
606 |
53543 |
} |
|
607 |
|||
608 |
57701 |
void FSReqAfterScope::Clear() { |
|
609 |
✓✓ | 57701 |
if (!wrap_) return; |
610 |
|||
611 |
53543 |
uv_fs_req_cleanup(wrap_->req()); |
|
612 |
53543 |
wrap_->Detach(); |
|
613 |
53543 |
wrap_.reset(); |
|
614 |
} |
||
615 |
|||
616 |
// TODO(joyeecheung): create a normal context object, and |
||
617 |
// construct the actual errors in the JS land using the context. |
||
618 |
// The context should include fds for some fs APIs, currently they are |
||
619 |
// missing in the error messages. The path, dest, syscall, fd, .etc |
||
620 |
// can be put into the context before the binding is even invoked, |
||
621 |
// the only information that has to come from the C++ layer is the |
||
622 |
// error number (and possibly the syscall for abstraction), |
||
623 |
// which is also why the errors should have been constructed |
||
624 |
// in JS for more flexibility. |
||
625 |
4147 |
void FSReqAfterScope::Reject(uv_fs_t* req) { |
|
626 |
8294 |
BaseObjectPtr<FSReqBase> wrap { wrap_ }; |
|
627 |
4147 |
Local<Value> exception = UVException(wrap_->env()->isolate(), |
|
628 |
4147 |
static_cast<int>(req->result), |
|
629 |
wrap_->syscall(), |
||
630 |
nullptr, |
||
631 |
req->path, |
||
632 |
12441 |
wrap_->data()); |
|
633 |
4147 |
Clear(); |
|
634 |
4147 |
wrap->Reject(exception); |
|
635 |
4147 |
} |
|
636 |
|||
637 |
53547 |
bool FSReqAfterScope::Proceed() { |
|
638 |
✓✓ | 53547 |
if (req_->result < 0) { |
639 |
4147 |
Reject(req_); |
|
640 |
4147 |
return false; |
|
641 |
} |
||
642 |
49400 |
return true; |
|
643 |
} |
||
644 |
|||
645 |
8773 |
void AfterNoArgs(uv_fs_t* req) { |
|
646 |
8773 |
FSReqBase* req_wrap = FSReqBase::from_req(req); |
|
647 |
17543 |
FSReqAfterScope after(req_wrap, req); |
|
648 |
|||
649 |
✓✓ | 8773 |
if (after.Proceed()) |
650 |
17088 |
req_wrap->Resolve(Undefined(req_wrap->env()->isolate())); |
|
651 |
8770 |
} |
|
652 |
|||
653 |
7455 |
void AfterStat(uv_fs_t* req) { |
|
654 |
7455 |
FSReqBase* req_wrap = FSReqBase::from_req(req); |
|
655 |
14910 |
FSReqAfterScope after(req_wrap, req); |
|
656 |
|||
657 |
✓✓ | 7455 |
if (after.Proceed()) { |
658 |
3812 |
req_wrap->ResolveStat(&req->statbuf); |
|
659 |
} |
||
660 |
7455 |
} |
|
661 |
|||
662 |
35681 |
void AfterInteger(uv_fs_t* req) { |
|
663 |
35681 |
FSReqBase* req_wrap = FSReqBase::from_req(req); |
|
664 |
71362 |
FSReqAfterScope after(req_wrap, req); |
|
665 |
|||
666 |
35681 |
int result = static_cast<int>(req->result); |
|
667 |
✓✓✓✓ ✓✓ |
35681 |
if (result >= 0 && req_wrap->is_plain_open()) |
668 |
5173 |
req_wrap->env()->AddUnmanagedFd(result); |
|
669 |
|||
670 |
✓✓ | 35681 |
if (after.Proceed()) |
671 |
70918 |
req_wrap->Resolve(Integer::New(req_wrap->env()->isolate(), result)); |
|
672 |
35681 |
} |
|
673 |
|||
674 |
682 |
void AfterOpenFileHandle(uv_fs_t* req) { |
|
675 |
682 |
FSReqBase* req_wrap = FSReqBase::from_req(req); |
|
676 |
1364 |
FSReqAfterScope after(req_wrap, req); |
|
677 |
|||
678 |
✓✓ | 682 |
if (after.Proceed()) { |
679 |
1304 |
FileHandle* fd = FileHandle::New(req_wrap->binding_data(), |
|
680 |
1304 |
static_cast<int>(req->result)); |
|
681 |
✗✓ | 652 |
if (fd == nullptr) return; |
682 |
✓✗ | 1304 |
req_wrap->Resolve(fd->object()); |
683 |
} |
||
684 |
} |
||
685 |
|||
686 |
// Reverse the logic applied by path.toNamespacedPath() to create a |
||
687 |
// namespace-prefixed path. |
||
688 |
485 |
void FromNamespacedPath(std::string* path) { |
|
689 |
#ifdef _WIN32 |
||
690 |
if (path->compare(0, 8, "\\\\?\\UNC\\", 8) == 0) { |
||
691 |
*path = path->substr(8); |
||
692 |
path->insert(0, "\\\\"); |
||
693 |
} else if (path->compare(0, 4, "\\\\?\\", 4) == 0) { |
||
694 |
*path = path->substr(4); |
||
695 |
} |
||
696 |
#endif |
||
697 |
485 |
} |
|
698 |
|||
699 |
613 |
void AfterMkdirp(uv_fs_t* req) { |
|
700 |
613 |
FSReqBase* req_wrap = FSReqBase::from_req(req); |
|
701 |
1226 |
FSReqAfterScope after(req_wrap, req); |
|
702 |
|||
703 |
MaybeLocal<Value> path; |
||
704 |
Local<Value> error; |
||
705 |
|||
706 |
✓✓ | 613 |
if (after.Proceed()) { |
707 |
✓✓ | 607 |
if (!req_wrap->continuation_data()->first_path().empty()) { |
708 |
516 |
std::string first_path(req_wrap->continuation_data()->first_path()); |
|
709 |
258 |
FromNamespacedPath(&first_path); |
|
710 |
path = StringBytes::Encode(req_wrap->env()->isolate(), first_path.c_str(), |
||
711 |
req_wrap->encoding(), |
||
712 |
258 |
&error); |
|
713 |
✗✓ | 258 |
if (path.IsEmpty()) |
714 |
req_wrap->Reject(error); |
||
715 |
else |
||
716 |
516 |
req_wrap->Resolve(path.ToLocalChecked()); |
|
717 |
} else { |
||
718 |
698 |
req_wrap->Resolve(Undefined(req_wrap->env()->isolate())); |
|
719 |
} |
||
720 |
} |
||
721 |
613 |
} |
|
722 |
|||
723 |
6 |
void AfterStringPath(uv_fs_t* req) { |
|
724 |
6 |
FSReqBase* req_wrap = FSReqBase::from_req(req); |
|
725 |
12 |
FSReqAfterScope after(req_wrap, req); |
|
726 |
|||
727 |
MaybeLocal<Value> link; |
||
728 |
Local<Value> error; |
||
729 |
|||
730 |
✓✓ | 6 |
if (after.Proceed()) { |
731 |
link = StringBytes::Encode(req_wrap->env()->isolate(), |
||
732 |
req->path, |
||
733 |
req_wrap->encoding(), |
||
734 |
5 |
&error); |
|
735 |
✗✓ | 5 |
if (link.IsEmpty()) |
736 |
req_wrap->Reject(error); |
||
737 |
else |
||
738 |
10 |
req_wrap->Resolve(link.ToLocalChecked()); |
|
739 |
} |
||
740 |
6 |
} |
|
741 |
|||
742 |
56 |
void AfterStringPtr(uv_fs_t* req) { |
|
743 |
56 |
FSReqBase* req_wrap = FSReqBase::from_req(req); |
|
744 |
111 |
FSReqAfterScope after(req_wrap, req); |
|
745 |
|||
746 |
MaybeLocal<Value> link; |
||
747 |
Local<Value> error; |
||
748 |
|||
749 |
✓✓ | 56 |
if (after.Proceed()) { |
750 |
link = StringBytes::Encode(req_wrap->env()->isolate(), |
||
751 |
51 |
static_cast<const char*>(req->ptr), |
|
752 |
req_wrap->encoding(), |
||
753 |
102 |
&error); |
|
754 |
✗✓ | 51 |
if (link.IsEmpty()) |
755 |
req_wrap->Reject(error); |
||
756 |
else |
||
757 |
102 |
req_wrap->Resolve(link.ToLocalChecked()); |
|
758 |
} |
||
759 |
55 |
} |
|
760 |
|||
761 |
89 |
void AfterScanDir(uv_fs_t* req) { |
|
762 |
89 |
FSReqBase* req_wrap = FSReqBase::from_req(req); |
|
763 |
169 |
FSReqAfterScope after(req_wrap, req); |
|
764 |
|||
765 |
✓✓ | 89 |
if (!after.Proceed()) { |
766 |
9 |
return; |
|
767 |
} |
||
768 |
80 |
Environment* env = req_wrap->env(); |
|
769 |
Local<Value> error; |
||
770 |
int r; |
||
771 |
✓✓ | 160 |
std::vector<Local<Value>> name_v; |
772 |
|||
773 |
for (;;) { |
||
774 |
uv_dirent_t ent; |
||
775 |
|||
776 |
7077 |
r = uv_fs_scandir_next(req, &ent); |
|
777 |
✓✓ | 7077 |
if (r == UV_EOF) |
778 |
80 |
break; |
|
779 |
✗✓ | 6997 |
if (r != 0) { |
780 |
return req_wrap->Reject(UVException( |
||
781 |
env->isolate(), r, nullptr, req_wrap->syscall(), req->path)); |
||
782 |
} |
||
783 |
|||
784 |
MaybeLocal<Value> filename = |
||
785 |
StringBytes::Encode(env->isolate(), |
||
786 |
ent.name, |
||
787 |
req_wrap->encoding(), |
||
788 |
6997 |
&error); |
|
789 |
✗✓ | 6997 |
if (filename.IsEmpty()) |
790 |
return req_wrap->Reject(error); |
||
791 |
|||
792 |
6997 |
name_v.push_back(filename.ToLocalChecked()); |
|
793 |
6997 |
} |
|
794 |
|||
795 |
160 |
req_wrap->Resolve(Array::New(env->isolate(), name_v.data(), name_v.size())); |
|
796 |
} |
||
797 |
|||
798 |
160 |
void AfterScanDirWithTypes(uv_fs_t* req) { |
|
799 |
160 |
FSReqBase* req_wrap = FSReqBase::from_req(req); |
|
800 |
319 |
FSReqAfterScope after(req_wrap, req); |
|
801 |
|||
802 |
✓✓ | 160 |
if (!after.Proceed()) { |
803 |
1 |
return; |
|
804 |
} |
||
805 |
|||
806 |
159 |
Environment* env = req_wrap->env(); |
|
807 |
159 |
Isolate* isolate = env->isolate(); |
|
808 |
Local<Value> error; |
||
809 |
int r; |
||
810 |
|||
811 |
✓✓ | 318 |
std::vector<Local<Value>> name_v; |
812 |
✓✗ | 318 |
std::vector<Local<Value>> type_v; |
813 |
|||
814 |
for (;;) { |
||
815 |
uv_dirent_t ent; |
||
816 |
|||
817 |
175 |
r = uv_fs_scandir_next(req, &ent); |
|
818 |
✓✓ | 175 |
if (r == UV_EOF) |
819 |
159 |
break; |
|
820 |
✗✓ | 16 |
if (r != 0) { |
821 |
return req_wrap->Reject( |
||
822 |
UVException(isolate, r, nullptr, req_wrap->syscall(), req->path)); |
||
823 |
} |
||
824 |
|||
825 |
MaybeLocal<Value> filename = |
||
826 |
StringBytes::Encode(isolate, |
||
827 |
ent.name, |
||
828 |
req_wrap->encoding(), |
||
829 |
16 |
&error); |
|
830 |
✗✓ | 16 |
if (filename.IsEmpty()) |
831 |
return req_wrap->Reject(error); |
||
832 |
|||
833 |
16 |
name_v.push_back(filename.ToLocalChecked()); |
|
834 |
16 |
type_v.emplace_back(Integer::New(isolate, ent.type)); |
|
835 |
16 |
} |
|
836 |
|||
837 |
Local<Value> result[] = { |
||
838 |
Array::New(isolate, name_v.data(), name_v.size()), |
||
839 |
Array::New(isolate, type_v.data(), type_v.size()) |
||
840 |
477 |
}; |
|
841 |
318 |
req_wrap->Resolve(Array::New(isolate, result, arraysize(result))); |
|
842 |
} |
||
843 |
|||
844 |
264 |
void Access(const FunctionCallbackInfo<Value>& args) { |
|
845 |
264 |
Environment* env = Environment::GetCurrent(args); |
|
846 |
264 |
Isolate* isolate = env->isolate(); |
|
847 |
528 |
HandleScope scope(isolate); |
|
848 |
|||
849 |
264 |
const int argc = args.Length(); |
|
850 |
✗✓ | 264 |
CHECK_GE(argc, 2); |
851 |
|||
852 |
✗✓ | 528 |
CHECK(args[1]->IsInt32()); |
853 |
792 |
int mode = args[1].As<Int32>()->Value(); |
|
854 |
|||
855 |
528 |
BufferValue path(isolate, args[0]); |
|
856 |
✗✓ | 264 |
CHECK_NOT_NULL(*path); |
857 |
|||
858 |
264 |
FSReqBase* req_wrap_async = GetReqWrap(args, 2); |
|
859 |
✓✓ | 264 |
if (req_wrap_async != nullptr) { // access(path, mode, req) |
860 |
42 |
AsyncCall(env, req_wrap_async, args, "access", UTF8, AfterNoArgs, |
|
861 |
42 |
uv_fs_access, *path, mode); |
|
862 |
} else { // access(path, mode, undefined, ctx) |
||
863 |
✗✓ | 222 |
CHECK_EQ(argc, 4); |
864 |
444 |
FSReqWrapSync req_wrap_sync; |
|
865 |
✓✓ | 222 |
FS_SYNC_TRACE_BEGIN(access); |
866 |
444 |
SyncCall(env, args[3], &req_wrap_sync, "access", uv_fs_access, *path, mode); |
|
867 |
✓✓ | 223 |
FS_SYNC_TRACE_END(access); |
868 |
✓✗ | 1 |
} |
869 |
266 |
} |
|
870 |
✓✗✓✗ |
3 |
|
871 |
2 |
||
872 |
✓✗ | 51455 |
void Close(const FunctionCallbackInfo<Value>& args) { |
873 |
51455 |
Environment* env = Environment::GetCurrent(args); |
|
874 |
|||
875 |
51454 |
const int argc = args.Length(); |
|
876 |
✗✓ | 51453 |
CHECK_GE(argc, 2); |
877 |
|||
878 |
✗✓ | 102906 |
CHECK(args[0]->IsInt32()); |
879 |
154359 |
int fd = args[0].As<Int32>()->Value(); |
|
880 |
51453 |
env->RemoveUnmanagedFd(fd); |
|
881 |
|||
882 |
51453 |
FSReqBase* req_wrap_async = GetReqWrap(args, 1); |
|
883 |
✓✓ | 51453 |
if (req_wrap_async != nullptr) { // close(fd, req) |
884 |
AsyncCall(env, req_wrap_async, args, "close", UTF8, AfterNoArgs, |
||
885 |
5107 |
uv_fs_close, fd); |
|
886 |
} else { // close(fd, undefined, ctx) |
||
887 |
✗✓ | 46346 |
CHECK_EQ(argc, 3); |
888 |
92692 |
FSReqWrapSync req_wrap_sync; |
|
889 |
✓✓ | 46346 |
FS_SYNC_TRACE_BEGIN(close); |
890 |
46346 |
SyncCall(env, args[2], &req_wrap_sync, "close", uv_fs_close, fd); |
|
891 |
✓✓ | 46376 |
FS_SYNC_TRACE_END(close); |
892 |
✓✓ | 30 |
} |
893 |
51509 |
} |
|
894 |
✓✗✓✓ |
90 |
|
895 |
56 |
||
896 |
✓✗ | 60 |
// Used to speed up module loading. Returns an array [string, boolean] |
897 |
55972 |
static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) { |
|
898 |
55912 |
Environment* env = Environment::GetCurrent(args); |
|
899 |
55942 |
Isolate* isolate = env->isolate(); |
|
900 |
55912 |
uv_loop_t* loop = env->event_loop(); |
|
901 |
|||
902 |
✗✓ | 167736 |
CHECK(args[0]->IsString()); |
903 |
61748 |
node::Utf8Value path(isolate, args[0]); |
|
904 |
|||
905 |
✓✓ | 55911 |
if (strlen(*path) != path.length()) { |
906 |
9 |
args.GetReturnValue().Set(Array::New(isolate)); |
|
907 |
3 |
return; // Contains a nul byte. |
|
908 |
} |
||
909 |
uv_fs_t open_req; |
||
910 |
55908 |
const int fd = uv_fs_open(loop, &open_req, *path, O_RDONLY, 0, nullptr); |
|
911 |
55909 |
uv_fs_req_cleanup(&open_req); |
|
912 |
|||
913 |
✓✓ | 55909 |
if (fd < 0) { |
914 |
150216 |
args.GetReturnValue().Set(Array::New(isolate)); |
|
915 |
50072 |
return; |
|
916 |
} |
||
917 |
|||
918 |
5837 |
auto defer_close = OnScopeLeave([fd, loop]() { |
|
919 |
uv_fs_t close_req; |
||
920 |
✗✓ | 5837 |
CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr)); |
921 |
5837 |
uv_fs_req_cleanup(&close_req); |
|
922 |
✓✓ | 17510 |
}); |
923 |
|||
924 |
5837 |
const size_t kBlockSize = 32 << 10; |
|
925 |
✓✓ | 11673 |
std::vector<char> chars; |
926 |
5837 |
int64_t offset = 0; |
|
927 |
ssize_t numchars; |
||
928 |
do { |
||
929 |
5837 |
const size_t start = chars.size(); |
|
930 |
5837 |
chars.resize(start + kBlockSize); |
|
931 |
|||
932 |
uv_buf_t buf; |
||
933 |
5837 |
buf.base = &chars[start]; |
|
934 |
5837 |
buf.len = kBlockSize; |
|
935 |
|||
936 |
uv_fs_t read_req; |
||
937 |
5837 |
numchars = uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr); |
|
938 |
5837 |
uv_fs_req_cleanup(&read_req); |
|
939 |
|||
940 |
✓✓ | 5837 |
if (numchars < 0) { |
941 |
3 |
args.GetReturnValue().Set(Array::New(isolate)); |
|
942 |
1 |
return; |
|
943 |
} |
||
944 |
5836 |
offset += numchars; |
|
945 |
✗✓ | 5836 |
} while (static_cast<size_t>(numchars) == kBlockSize); |
946 |
|||
947 |
5836 |
size_t start = 0; |
|
948 |
✓✓✓✓ ✓✓ |
5836 |
if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) { |
949 |
1 |
start = 3; // Skip UTF-8 BOM. |
|
950 |
} |
||
951 |
|||
952 |
5836 |
const size_t size = offset - start; |
|
953 |
5836 |
char* p = &chars[start]; |
|
954 |
5836 |
char* pe = &chars[size]; |
|
955 |
char* pos[2]; |
||
956 |
5836 |
char** ppos = &pos[0]; |
|
957 |
|||
958 |
✓✓ | 330894 |
while (p < pe) { |
959 |
168353 |
char c = *p++; |
|
960 |
✗✓✗✗ ✗✗ |
168353 |
if (c == '\\' && p < pe && *p == '"') p++; |
961 |
✓✓ | 168353 |
if (c != '"') continue; |
962 |
22736 |
*ppos++ = p; |
|
963 |
✓✓ | 22736 |
if (ppos < &pos[2]) continue; |
964 |
11368 |
ppos = &pos[0]; |
|
965 |
|||
966 |
11368 |
char* s = &pos[0][0]; |
|
967 |
11368 |
char* se = &pos[1][-1]; // Exclude quote. |
|
968 |
11368 |
size_t n = se - s; |
|
969 |
|||
970 |
✓✓ | 11368 |
if (n == 4) { |
971 |
✓✓ | 6173 |
if (0 == memcmp(s, "main", 4)) break; |
972 |
✓✓ | 6031 |
if (0 == memcmp(s, "name", 4)) break; |
973 |
✓✓ | 383 |
if (0 == memcmp(s, "type", 4)) break; |
974 |
✓✓ | 5195 |
} else if (n == 7) { |
975 |
✓✓ | 524 |
if (0 == memcmp(s, "exports", 7)) break; |
976 |
✓✓ | 517 |
if (0 == memcmp(s, "imports", 7)) break; |
977 |
} |
||
978 |
} |
||
979 |
|||
980 |
|||
981 |
Local<Value> return_value[] = { |
||
982 |
11672 |
String::NewFromUtf8(isolate, |
|
983 |
5836 |
&chars[start], |
|
984 |
v8::NewStringType::kNormal, |
||
985 |
5836 |
size).ToLocalChecked(), |
|
986 |
Boolean::New(isolate, p < pe ? true : false) |
||
987 |
17508 |
}; |
|
988 |
✓✓ | 17508 |
args.GetReturnValue().Set( |
989 |
Array::New(isolate, return_value, arraysize(return_value))); |
||
990 |
} |
||
991 |
|||
992 |
// Used to speed up module loading. Returns 0 if the path refers to |
||
993 |
// a file, 1 when it's a directory or < 0 on error (usually -ENOENT.) |
||
994 |
// The speedup comes from not creating thousands of Stat and Error objects. |
||
995 |
122747 |
static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) { |
|
996 |
122747 |
Environment* env = Environment::GetCurrent(args); |
|
997 |
|||
998 |
✗✓ | 368241 |
CHECK(args[0]->IsString()); |
999 |
245494 |
node::Utf8Value path(env->isolate(), args[0]); |
|
1000 |
|||
1001 |
uv_fs_t req; |
||
1002 |
122747 |
int rc = uv_fs_stat(env->event_loop(), &req, *path, nullptr); |
|
1003 |
✓✓ | 122747 |
if (rc == 0) { |
1004 |
65151 |
const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr); |
|
1005 |
65151 |
rc = !!(s->st_mode & S_IFDIR); |
|
1006 |
} |
||
1007 |
122747 |
uv_fs_req_cleanup(&req); |
|
1008 |
|||
1009 |
245494 |
args.GetReturnValue().Set(rc); |
|
1010 |
122747 |
} |
|
1011 |
|||
1012 |
15831 |
static void Stat(const FunctionCallbackInfo<Value>& args) { |
|
1013 |
15831 |
BindingData* binding_data = Environment::GetBindingData<BindingData>(args); |
|
1014 |
15831 |
Environment* env = binding_data->env(); |
|
1015 |
|||
1016 |
15831 |
const int argc = args.Length(); |
|
1017 |
✗✓ | 15831 |
CHECK_GE(argc, 2); |
1018 |
|||
1019 |
31414 |
BufferValue path(env->isolate(), args[0]); |
|
1020 |
✗✓ | 15831 |
CHECK_NOT_NULL(*path); |
1021 |
|||
1022 |
31662 |
bool use_bigint = args[1]->IsTrue(); |
|
1023 |
15831 |
FSReqBase* req_wrap_async = GetReqWrap(args, 2, use_bigint); |
|
1024 |
✓✓ | 15831 |
if (req_wrap_async != nullptr) { // stat(path, use_bigint, req) |
1025 |
2573 |
AsyncCall(env, req_wrap_async, args, "stat", UTF8, AfterStat, |
|
1026 |
2573 |
uv_fs_stat, *path); |
|
1027 |
} else { // stat(path, use_bigint, undefined, ctx) |
||
1028 |
✗✓ | 13258 |
CHECK_EQ(argc, 4); |
1029 |
✓✓ | 26268 |
FSReqWrapSync req_wrap_sync; |
1030 |
✓✓ | 13258 |
FS_SYNC_TRACE_BEGIN(stat); |
1031 |
26516 |
int err = SyncCall(env, args[3], &req_wrap_sync, "stat", uv_fs_stat, *path); |
|
1032 |
✓✓ | 13259 |
FS_SYNC_TRACE_END(stat); |
1033 |
✓✗✓✓ |
13259 |
if (err != 0) { |
1034 |
250 |
return; // error info is in ctx |
|
1035 |
✓✗✓✗ |
4 |
} |
1036 |
2 |
||
1037 |
✓✗ | 3 |
Local<Value> arr = FillGlobalStatsArray(binding_data, use_bigint, |
1038 |
13012 |
static_cast<const uv_stat_t*>(req_wrap_sync.req.ptr)); |
|
1039 |
✓✓ | 26020 |
args.GetReturnValue().Set(arr); |
1040 |
1 |
} |
|
1041 |
} |
||
1042 |
|||
1043 |
107774 |
static void LStat(const FunctionCallbackInfo<Value>& args) { |
|
1044 |
107774 |
BindingData* binding_data = Environment::GetBindingData<BindingData>(args); |
|
1045 |
107774 |
Environment* env = binding_data->env(); |
|
1046 |
|||
1047 |
107774 |
const int argc = args.Length(); |
|
1048 |
✗✓ | 107774 |
CHECK_GE(argc, 3); |
1049 |
|||
1050 |
215234 |
BufferValue path(env->isolate(), args[0]); |
|
1051 |
✗✓ | 107774 |
CHECK_NOT_NULL(*path); |
1052 |
|||
1053 |
215548 |
bool use_bigint = args[1]->IsTrue(); |
|
1054 |
107774 |
FSReqBase* req_wrap_async = GetReqWrap(args, 2, use_bigint); |
|
1055 |
✓✓ | 107774 |
if (req_wrap_async != nullptr) { // lstat(path, use_bigint, req) |
1056 |
3953 |
AsyncCall(env, req_wrap_async, args, "lstat", UTF8, AfterStat, |
|
1057 |
3953 |
uv_fs_lstat, *path); |
|
1058 |
} else { // lstat(path, use_bigint, undefined, ctx) |
||
1059 |
✗✓ | 103821 |
CHECK_EQ(argc, 4); |
1060 |
✓✓ | 207328 |
FSReqWrapSync req_wrap_sync; |
1061 |
✓✓ | 103821 |
FS_SYNC_TRACE_BEGIN(lstat); |
1062 |
103821 |
int err = SyncCall(env, args[3], &req_wrap_sync, "lstat", uv_fs_lstat, |
|
1063 |
103838 |
*path); |
|
1064 |
✓✓✓✓ |
103838 |
FS_SYNC_TRACE_END(lstat); |
1065 |
✓✓ | 103823 |
if (err != 0) { |
1066 |
✓✗ | 367 |
return; // error info is in ctx |
1067 |
✓✓ | 34 |
} |
1068 |
2 |
||
1069 |
✓✗ | 53 |
Local<Value> arr = FillGlobalStatsArray(binding_data, use_bigint, |
1070 |
103524 |
static_cast<const uv_stat_t*>(req_wrap_sync.req.ptr)); |
|
1071 |
✓✓ | 207014 |
args.GetReturnValue().Set(arr); |
1072 |
17 |
} |
|
1073 |
} |
||
1074 |
|||
1075 |
42105 |
static void FStat(const FunctionCallbackInfo<Value>& args) { |
|
1076 |
42105 |
BindingData* binding_data = Environment::GetBindingData<BindingData>(args); |
|
1077 |
42105 |
Environment* env = binding_data->env(); |
|
1078 |
|||
1079 |
42105 |
const int argc = args.Length(); |
|
1080 |
✗✓ | 42105 |
CHECK_GE(argc, 2); |
1081 |
|||
1082 |
✗✓ | 84210 |
CHECK(args[0]->IsInt32()); |
1083 |
126315 |
int fd = args[0].As<Int32>()->Value(); |
|
1084 |
|||
1085 |
84210 |
bool use_bigint = args[1]->IsTrue(); |
|
1086 |
42105 |
FSReqBase* req_wrap_async = GetReqWrap(args, 2, use_bigint); |
|
1087 |
✓✓ | 42105 |
if (req_wrap_async != nullptr) { // fstat(fd, use_bigint, req) |
1088 |
AsyncCall(env, req_wrap_async, args, "fstat", UTF8, AfterStat, |
||
1089 |
929 |
uv_fs_fstat, fd); |
|
1090 |
} else { // fstat(fd, use_bigint, undefined, ctx) |
||
1091 |
✗✓ | 41176 |
CHECK_EQ(argc, 4); |
1092 |
82331 |
FSReqWrapSync req_wrap_sync; |
|
1093 |
✓✓ | 41176 |
FS_SYNC_TRACE_BEGIN(fstat); |
1094 |
41176 |
int err = SyncCall(env, args[3], &req_wrap_sync, "fstat", uv_fs_fstat, fd); |
|
1095 |
✓✓ | 41181 |
FS_SYNC_TRACE_END(fstat); |
1096 |
✓✓✓✓ |
41181 |
if (err != 0) { |
1097 |
29 |
return; // error info is in ctx |
|
1098 |
✓✗✓✓ |
18 |
} |
1099 |
8 |
||
1100 |
✓✗ | 13 |
Local<Value> arr = FillGlobalStatsArray(binding_data, use_bigint, |
1101 |
41165 |
static_cast<const uv_stat_t*>(req_wrap_sync.req.ptr)); |
|
1102 |
✓✓ | 82310 |
args.GetReturnValue().Set(arr); |
1103 |
5 |
} |
|
1104 |
} |
||
1105 |
|||
1106 |
330 |
static void Symlink(const FunctionCallbackInfo<Value>& args) { |
|
1107 |
330 |
Environment* env = Environment::GetCurrent(args); |
|
1108 |
330 |
Isolate* isolate = env->isolate(); |
|
1109 |
|||
1110 |
330 |
const int argc = args.Length(); |
|
1111 |
✗✓ | 330 |
CHECK_GE(argc, 4); |
1112 |
|||
1113 |
660 |
BufferValue target(isolate, args[0]); |
|
1114 |
✗✓ | 330 |
CHECK_NOT_NULL(*target); |
1115 |
660 |
BufferValue path(isolate, args[1]); |
|
1116 |
✗✓ | 330 |
CHECK_NOT_NULL(*path); |
1117 |
|||
1118 |
✗✓ | 660 |
CHECK(args[2]->IsInt32()); |
1119 |
990 |
int flags = args[2].As<Int32>()->Value(); |
|
1120 |
|||
1121 |
330 |
FSReqBase* req_wrap_async = GetReqWrap(args, 3); |
|
1122 |
✓✓ | 330 |
if (req_wrap_async != nullptr) { // symlink(target, path, flags, req) |
1123 |
24 |
AsyncDestCall(env, req_wrap_async, args, "symlink", *path, path.length(), |
|
1124 |
24 |
UTF8, AfterNoArgs, uv_fs_symlink, *target, *path, flags); |
|
1125 |
} else { // symlink(target, path, flags, undefinec, ctx) |
||
1126 |
✗✓ | 306 |
CHECK_EQ(argc, 5); |
1127 |
612 |
FSReqWrapSync req_wrap_sync; |
|
1128 |
✓✓ | 306 |
FS_SYNC_TRACE_BEGIN(symlink); |
1129 |
306 |
SyncCall(env, args[4], &req_wrap_sync, "symlink", |
|
1130 |
308 |
uv_fs_symlink, *target, *path, flags); |
|
1131 |
✓✗✓✓ |
308 |
FS_SYNC_TRACE_END(symlink); |
1132 |
2 |
} |
|
1133 |
✓✗ | 338 |
} |
1134 |
✓✗ | 4 |
|
1135 |
73 |
static void Link(const FunctionCallbackInfo<Value>& args) { |
|
1136 |
✓✗ | 79 |
Environment* env = Environment::GetCurrent(args); |
1137 |
73 |
Isolate* isolate = env->isolate(); |
|
1138 |
|||
1139 |
73 |
const int argc = args.Length(); |
|
1140 |
✗✓ | 71 |
CHECK_GE(argc, 3); |
1141 |
|||
1142 |
142 |
BufferValue src(isolate, args[0]); |
|
1143 |
✗✓ | 71 |
CHECK_NOT_NULL(*src); |
1144 |
|||
1145 |
142 |
BufferValue dest(isolate, args[1]); |
|
1146 |
✗✓ | 71 |
CHECK_NOT_NULL(*dest); |
1147 |
|||
1148 |
71 |
FSReqBase* req_wrap_async = GetReqWrap(args, 2); |
|
1149 |
✓✓ | 71 |
if (req_wrap_async != nullptr) { // link(src, dest, req) |
1150 |
66 |
AsyncDestCall(env, req_wrap_async, args, "link", *dest, dest.length(), UTF8, |
|
1151 |
66 |
AfterNoArgs, uv_fs_link, *src, *dest); |
|
1152 |
} else { // link(src, dest) |
||
1153 |
✗✓ | 5 |
CHECK_EQ(argc, 4); |
1154 |
10 |
FSReqWrapSync req_wrap_sync; |
|
1155 |
✓✓ | 5 |
FS_SYNC_TRACE_BEGIN(link); |
1156 |
5 |
SyncCall(env, args[3], &req_wrap_sync, "link", |
|
1157 |
8 |
uv_fs_link, *src, *dest); |
|
1158 |
✓✗✓✓ |
8 |
FS_SYNC_TRACE_END(link); |
1159 |
3 |
} |
|
1160 |
✓✗ | 83 |
} |
1161 |
✓✗ | 6 |
|
1162 |
85 |
static void ReadLink(const FunctionCallbackInfo<Value>& args) { |
|
1163 |
✓✗ | 94 |
Environment* env = Environment::GetCurrent(args); |
1164 |
85 |
Isolate* isolate = env->isolate(); |
|
1165 |
|||
1166 |
85 |
const int argc = args.Length(); |
|
1167 |
✗✓ | 82 |
CHECK_GE(argc, 3); |
1168 |
|||
1169 |
163 |
BufferValue path(isolate, args[0]); |
|
1170 |
✗✓ | 82 |
CHECK_NOT_NULL(*path); |
1171 |
|||
1172 |
82 |
const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); |
|
1173 |
|||
1174 |
82 |
FSReqBase* req_wrap_async = GetReqWrap(args, 2); |
|
1175 |
✓✓ | 82 |
if (req_wrap_async != nullptr) { // readlink(path, encoding, req) |
1176 |
34 |
AsyncCall(env, req_wrap_async, args, "readlink", encoding, AfterStringPtr, |
|
1177 |
34 |
uv_fs_readlink, *path); |
|
1178 |
} else { |
||
1179 |
✗✓ | 48 |
CHECK_EQ(argc, 4); |
1180 |
✓✓ | 95 |
FSReqWrapSync req_wrap_sync; |
1181 |
✓✓ | 48 |
FS_SYNC_TRACE_BEGIN(readlink); |
1182 |
48 |
int err = SyncCall(env, args[3], &req_wrap_sync, "readlink", |
|
1183 |
49 |
uv_fs_readlink, *path); |
|
1184 |
✓✗✓✓ |
49 |
FS_SYNC_TRACE_END(readlink); |
1185 |
✓✓ | 49 |
if (err < 0) { |
1186 |
✓✗ | 5 |
return; // syscall failed, no need to continue, error info is in ctx |
1187 |
✓✗ | 2 |
} |
1188 |
48 |
const char* link_path = static_cast<const char*>(req_wrap_sync.req.ptr); |
|
1189 |
✓✗ | 4 |
|
1190 |
1 |
Local<Value> error; |
|
1191 |
MaybeLocal<Value> rc = StringBytes::Encode(isolate, |
||
1192 |
1 |
link_path, |
|
1193 |
encoding, |
||
1194 |
47 |
&error); |
|
1195 |
✗✓ | 47 |
if (rc.IsEmpty()) { |
1196 |
Local<Object> ctx = args[3].As<Object>(); |
||
1197 |
ctx->Set(env->context(), env->error_string(), error).Check(); |
||
1198 |
return; |
||
1199 |
} |
||
1200 |
|||
1201 |
✓✓ | 94 |
args.GetReturnValue().Set(rc.ToLocalChecked()); |
1202 |
} |
||
1203 |
} |
||
1204 |
|||
1205 |
6 |
static void Rename(const FunctionCallbackInfo<Value>& args) { |
|
1206 |
6 |
Environment* env = Environment::GetCurrent(args); |
|
1207 |
6 |
Isolate* isolate = env->isolate(); |
|
1208 |
|||
1209 |
6 |
const int argc = args.Length(); |
|
1210 |
✗✓ | 6 |
CHECK_GE(argc, 3); |
1211 |
|||
1212 |
12 |
BufferValue old_path(isolate, args[0]); |
|
1213 |
✗✓ | 6 |
CHECK_NOT_NULL(*old_path); |
1214 |
12 |
BufferValue new_path(isolate, args[1]); |
|
1215 |
✗✓ | 6 |
CHECK_NOT_NULL(*new_path); |
1216 |
|||
1217 |
6 |
FSReqBase* req_wrap_async = GetReqWrap(args, 2); |
|
1218 |
✓✓ | 6 |
if (req_wrap_async != nullptr) { |
1219 |
3 |
AsyncDestCall(env, req_wrap_async, args, "rename", *new_path, |
|
1220 |
new_path.length(), UTF8, AfterNoArgs, uv_fs_rename, |
||
1221 |
3 |
*old_path, *new_path); |
|
1222 |
} else { |
||
1223 |
✗✓ | 3 |
CHECK_EQ(argc, 4); |
1224 |
6 |
FSReqWrapSync req_wrap_sync; |
|
1225 |
✓✓ | 3 |
FS_SYNC_TRACE_BEGIN(rename); |
1226 |
3 |
SyncCall(env, args[3], &req_wrap_sync, "rename", uv_fs_rename, |
|
1227 |
4 |
*old_path, *new_path); |
|
1228 |
✓✗✓✓ |
4 |
FS_SYNC_TRACE_END(rename); |
1229 |
1 |
} |
|
1230 |
✓✗ | 10 |
} |
1231 |
✓✗ | 2 |
|
1232 |
62 |
static void FTruncate(const FunctionCallbackInfo<Value>& args) { |
|
1233 |
✓✗ | 65 |
Environment* env = Environment::GetCurrent(args); |
1234 |
1 |
||
1235 |
61 |
const int argc = args.Length(); |
|
1236 |
✗✓ | 62 |
CHECK_GE(argc, 3); |
1237 |
|||
1238 |
✗✓ | 122 |
CHECK(args[0]->IsInt32()); |
1239 |
183 |
const int fd = args[0].As<Int32>()->Value(); |
|
1240 |
|||
1241 |
✗✓ | 61 |
CHECK(IsSafeJsInt(args[1])); |
1242 |
183 |
const int64_t len = args[1].As<Integer>()->Value(); |
|
1243 |
|||
1244 |
61 |
FSReqBase* req_wrap_async = GetReqWrap(args, 2); |
|
1245 |
✓✓ | 61 |
if (req_wrap_async != nullptr) { |
1246 |
AsyncCall(env, req_wrap_async, args, "ftruncate", UTF8, AfterNoArgs, |
||
1247 |
48 |
uv_fs_ftruncate, fd, len); |
|
1248 |
} else { |
||
1249 |
✗✓ | 13 |
CHECK_EQ(argc, 4); |
1250 |
26 |
FSReqWrapSync req_wrap_sync; |
|
1251 |
✓✓ | 13 |
FS_SYNC_TRACE_BEGIN(ftruncate); |
1252 |
SyncCall(env, args[3], &req_wrap_sync, "ftruncate", uv_fs_ftruncate, fd, |
||
1253 |
14 |
len); |
|
1254 |
✓✗✓✓ |
14 |
FS_SYNC_TRACE_END(ftruncate); |
1255 |
1 |
} |
|
1256 |
✓✗ | 65 |
} |
1257 |
✓✗ | 2 |
|
1258 |
8 |
static void Fdatasync(const FunctionCallbackInfo<Value>& args) { |
|
1259 |
✓✗ | 11 |
Environment* env = Environment::GetCurrent(args); |
1260 |
1 |
||
1261 |
7 |
const int argc = args.Length(); |
|
1262 |
✗✓ | 8 |
CHECK_GE(argc, 2); |
1263 |
|||
1264 |
✗✓ | 14 |
CHECK(args[0]->IsInt32()); |
1265 |
21 |
const int fd = args[0].As<Int32>()->Value(); |
|
1266 |
|||
1267 |
7 |
FSReqBase* req_wrap_async = GetReqWrap(args, 1); |
|
1268 |
✓✓ | 7 |
if (req_wrap_async != nullptr) { |
1269 |
AsyncCall(env, req_wrap_async, args, "fdatasync", UTF8, AfterNoArgs, |
||
1270 |
4 |
uv_fs_fdatasync, fd); |
|
1271 |
} else { |
||
1272 |
✗✓ | 3 |
CHECK_EQ(argc, 3); |
1273 |
6 |
FSReqWrapSync req_wrap_sync; |
|
1274 |
✓✓ | 3 |
FS_SYNC_TRACE_BEGIN(fdatasync); |
1275 |
3 |
SyncCall(env, args[2], &req_wrap_sync, "fdatasync", uv_fs_fdatasync, fd); |
|
1276 |
✓✓ | 4 |
FS_SYNC_TRACE_END(fdatasync); |
1277 |
✓✗ | 1 |
} |
1278 |
9 |
} |
|
1279 |
✓✗✓✗ |
4 |
|
1280 |
23 |
static void Fsync(const FunctionCallbackInfo<Value>& args) { |
|
1281 |
✓✗ | 24 |
Environment* env = Environment::GetCurrent(args); |
1282 |
2 |
||
1283 |
21 |
const int argc = args.Length(); |
|
1284 |
✗✓ | 22 |
CHECK_GE(argc, 2); |
1285 |
|||
1286 |
✗✓ | 42 |
CHECK(args[0]->IsInt32()); |
1287 |
63 |
const int fd = args[0].As<Int32>()->Value(); |
|
1288 |
|||
1289 |
21 |
FSReqBase* req_wrap_async = GetReqWrap(args, 1); |
|
1290 |
✓✓ | 21 |
if (req_wrap_async != nullptr) { |
1291 |
AsyncCall(env, req_wrap_async, args, "fsync", UTF8, AfterNoArgs, |
||
1292 |
4 |
uv_fs_fsync, fd); |
|
1293 |
} else { |
||
1294 |
✗✓ | 17 |
CHECK_EQ(argc, 3); |
1295 |
34 |
FSReqWrapSync req_wrap_sync; |
|
1296 |
✓✓ | 17 |
FS_SYNC_TRACE_BEGIN(fsync); |
1297 |
17 |
SyncCall(env, args[2], &req_wrap_sync, "fsync", uv_fs_fsync, fd); |
|
1298 |
✓✓ | 18 |
FS_SYNC_TRACE_END(fsync); |
1299 |
✓✗ | 1 |
} |
1300 |
23 |
} |
|
1301 |
✓✗✓✗ |
4 |
|
1302 |
2253 |
static void Unlink(const FunctionCallbackInfo<Value>& args) { |
|
1303 |
✓✗ | 2254 |
Environment* env = Environment::GetCurrent(args); |
1304 |
2 |
||
1305 |
2251 |
const int argc = args.Length(); |
|
1306 |
✗✓ | 2252 |
CHECK_GE(argc, 2); |
1307 |
|||
1308 |
4502 |
BufferValue path(env->isolate(), args[0]); |
|
1309 |
✗✓ | 2251 |
CHECK_NOT_NULL(*path); |
1310 |
|||
1311 |
2251 |
FSReqBase* req_wrap_async = GetReqWrap(args, 1); |
|
1312 |
✓✓ | 2251 |
if (req_wrap_async != nullptr) { |
1313 |
1017 |
AsyncCall(env, req_wrap_async, args, "unlink", UTF8, AfterNoArgs, |
|
1314 |
1017 |
uv_fs_unlink, *path); |
|
1315 |
} else { |
||
1316 |
✗✓ | 1234 |
CHECK_EQ(argc, 3); |
1317 |
2468 |
FSReqWrapSync req_wrap_sync; |
|
1318 |
✓✓ | 1234 |
FS_SYNC_TRACE_BEGIN(unlink); |
1319 |
2468 |
SyncCall(env, args[2], &req_wrap_sync, "unlink", uv_fs_unlink, *path); |
|
1320 |
✓✓ | 1264 |
FS_SYNC_TRACE_END(unlink); |
1321 |
✓✓ | 30 |
} |
1322 |
2306 |
} |
|
1323 |
✓✗✓✓ |
115 |
|
1324 |
1408 |
static void RMDir(const FunctionCallbackInfo<Value>& args) { |
|
1325 |
✓✗ | 1438 |
Environment* env = Environment::GetCurrent(args); |
1326 |
60 |
||
1327 |
1353 |
const int argc = args.Length(); |
|
1328 |
✗✓ | 1383 |
CHECK_GE(argc, 2); |
1329 |
|||
1330 |
2706 |
BufferValue path(env->isolate(), args[0]); |
|
1331 |
✗✓ | 1353 |
CHECK_NOT_NULL(*path); |
1332 |
|||
1333 |
1353 |
FSReqBase* req_wrap_async = GetReqWrap(args, 1); // rmdir(path, req) |
|
1334 |
✓✓ | 1353 |
if (req_wrap_async != nullptr) { |
1335 |
215 |
AsyncCall(env, req_wrap_async, args, "rmdir", UTF8, AfterNoArgs, |
|
1336 |
215 |
uv_fs_rmdir, *path); |
|
1337 |
} else { // rmdir(path, undefined, ctx) |
||
1338 |
✗✓ | 1138 |
CHECK_EQ(argc, 3); |
1339 |
2276 |
FSReqWrapSync req_wrap_sync; |
|
1340 |
✓✓ | 1138 |
FS_SYNC_TRACE_BEGIN(rmdir); |
1341 |
1138 |
SyncCall(env, args[2], &req_wrap_sync, "rmdir", |
|
1342 |
1141 |
uv_fs_rmdir, *path); |
|
1343 |
✓✗✓✓ |
1141 |
FS_SYNC_TRACE_END(rmdir); |
1344 |
3 |
} |
|
1345 |
✓✗ | 1365 |
} |
1346 |
✓✗ | 6 |
|
1347 |
5266 |
int MKDirpSync(uv_loop_t* loop, |
|
1348 |
✓✗ | 12 |
uv_fs_t* req, |
1349 |
3 |
const std::string& path, |
|
1350 |
int mode, |
||
1351 |
3 |
uv_fs_cb cb) { |
|
1352 |
5263 |
FSReqWrapSync* req_wrap = ContainerOf(&FSReqWrapSync::req, req); |
|
1353 |
|||
1354 |
// on the first iteration of algorithm, stash state information. |
||
1355 |
✓✗ | 5263 |
if (req_wrap->continuation_data() == nullptr) { |
1356 |
req_wrap->set_continuation_data( |
||
1357 |
5263 |
std::make_unique<FSContinuationData>(req, mode, cb)); |
|
1358 |
5263 |
req_wrap->continuation_data()->PushPath(std::move(path)); |
|
1359 |
} |
||
1360 |
|||
1361 |
✓✓ | 15483 |
while (req_wrap->continuation_data()->paths().size() > 0) { |
1362 |
10473 |
std::string next_path = req_wrap->continuation_data()->PopPath(); |
|
1363 |
5363 |
int err = uv_fs_mkdir(loop, req, next_path.c_str(), mode, nullptr); |
|
1364 |
while (true) { |
||
1365 |
✓✓✓✓ |
5364 |
switch (err) { |
1366 |
// Note: uv_fs_req_cleanup in terminal paths will be called by |
||
1367 |
// ~FSReqWrapSync(): |
||
1368 |
case 0: |
||
1369 |
298 |
req_wrap->continuation_data()->MaybeSetFirstPath(next_path); |
|
1370 |
✓✓ | 298 |
if (req_wrap->continuation_data()->paths().size() == 0) { |
1371 |
249 |
return 0; |
|
1372 |
} |
||
1373 |
49 |
break; |
|
1374 |
case UV_EACCES: |
||
1375 |
case UV_ENOTDIR: |
||
1376 |
case UV_EPERM: { |
||
1377 |
2 |
return err; |
|
1378 |
} |
||
1379 |
case UV_ENOENT: { |
||
1380 |
std::string dirname = next_path.substr(0, |
||
1381 |
51 |
next_path.find_last_of(kPathSeparator)); |
|
1382 |
✓✓ | 51 |
if (dirname != next_path) { |
1383 |
50 |
req_wrap->continuation_data()->PushPath(std::move(next_path)); |
|
1384 |
50 |
req_wrap->continuation_data()->PushPath(std::move(dirname)); |
|
1385 |
✓✗ | 1 |
} else if (req_wrap->continuation_data()->paths().size() == 0) { |
1386 |
1 |
err = UV_EEXIST; |
|
1387 |
1 |
continue; |
|
1388 |
} |
||
1389 |
✓✓ | 50 |
break; |
1390 |
} |
||
1391 |
default: |
||
1392 |
5013 |
uv_fs_req_cleanup(req); |
|
1393 |
5013 |
int orig_err = err; |
|
1394 |
5013 |
err = uv_fs_stat(loop, req, next_path.c_str(), nullptr); |
|
1395 |
✓✓✓✓ |
5013 |
if (err == 0 && !S_ISDIR(req->statbuf.st_mode)) { |
1396 |
1 |
uv_fs_req_cleanup(req); |
|
1397 |
✓✗✗✓ ✗✓ |
2 |
if (orig_err == UV_EEXIST && |
1398 |
1 |
req_wrap->continuation_data()->paths().size() > 0) { |
|
1399 |
return UV_ENOTDIR; |
||
1400 |
} |
||
1401 |
1 |
return UV_EEXIST; |
|
1402 |
} |
||
1403 |
✓✓ | 5012 |
if (err < 0) return err; |
1404 |
5011 |
break; |
|
1405 |
} |
||
1406 |
5110 |
break; |
|
1407 |
1 |
} |
|
1408 |
✓✓ | 5110 |
uv_fs_req_cleanup(req); |
1409 |
} |
||
1410 |
|||
1411 |
5010 |
return 0; |
|
1412 |
} |
||
1413 |
|||
1414 |
825 |
int MKDirpAsync(uv_loop_t* loop, |
|
1415 |
uv_fs_t* req, |
||
1416 |
const char* path, |
||
1417 |
int mode, |
||
1418 |
uv_fs_cb cb) { |
||
1419 |
825 |
FSReqBase* req_wrap = FSReqBase::from_req(req); |
|
1420 |
// on the first iteration of algorithm, stash state information. |
||
1421 |
✓✓ | 825 |
if (req_wrap->continuation_data() == nullptr) { |
1422 |
req_wrap->set_continuation_data( |
||
1423 |
613 |
std::make_unique<FSContinuationData>(req, mode, cb)); |
|
1424 |
613 |
req_wrap->continuation_data()->PushPath(std::move(path)); |
|
1425 |
} |
||
1426 |
|||
1427 |
// on each iteration of algorithm, mkdir directory on top of stack. |
||
1428 |
1650 |
std::string next_path = req_wrap->continuation_data()->PopPath(); |
|
1429 |
825 |
int err = uv_fs_mkdir(loop, req, next_path.c_str(), mode, |
|
1430 |
2475 |
uv_fs_callback_t{[](uv_fs_t* req) { |
|
1431 |
825 |
FSReqBase* req_wrap = FSReqBase::from_req(req); |
|
1432 |
825 |
Environment* env = req_wrap->env(); |
|
1433 |
825 |
uv_loop_t* loop = env->event_loop(); |
|
1434 |
1650 |
std::string path = req->path; |
|
1435 |
825 |
int err = static_cast<int>(req->result); |
|
1436 |
|||
1437 |
while (true) { |
||
1438 |
✓✓✓✓ |
826 |
switch (err) { |
1439 |
// Note: uv_fs_req_cleanup in terminal paths will be called by |
||
1440 |
// FSReqAfterScope::~FSReqAfterScope() |
||
1441 |
case 0: { |
||
1442 |
✓✓ | 271 |
if (req_wrap->continuation_data()->paths().size() == 0) { |
1443 |
258 |
req_wrap->continuation_data()->MaybeSetFirstPath(path); |
|
1444 |
258 |
req_wrap->continuation_data()->Done(0); |
|
1445 |
} else { |
||
1446 |
13 |
req_wrap->continuation_data()->MaybeSetFirstPath(path); |
|
1447 |
13 |
uv_fs_req_cleanup(req); |
|
1448 |
13 |
MKDirpAsync(loop, req, path.c_str(), |
|
1449 |
13 |
req_wrap->continuation_data()->mode(), nullptr); |
|
1450 |
} |
||
1451 |
271 |
break; |
|
1452 |
} |
||
1453 |
case UV_EACCES: |
||
1454 |
case UV_ENOTDIR: |
||
1455 |
case UV_EPERM: { |
||
1456 |
3 |
req_wrap->continuation_data()->Done(err); |
|
1457 |
3 |
break; |
|
1458 |
} |
||
1459 |
case UV_ENOENT: { |
||
1460 |
std::string dirname = path.substr(0, |
||
1461 |
107 |
path.find_last_of(kPathSeparator)); |
|
1462 |
✓✓ | 107 |
if (dirname != path) { |
1463 |
106 |
req_wrap->continuation_data()->PushPath(std::move(path)); |
|
1464 |
106 |
req_wrap->continuation_data()->PushPath(std::move(dirname)); |
|
1465 |
✓✗ | 1 |
} else if (req_wrap->continuation_data()->paths().size() == 0) { |
1466 |
1 |
err = UV_EEXIST; |
|
1467 |
1 |
continue; |
|
1468 |
} |
||
1469 |
106 |
uv_fs_req_cleanup(req); |
|
1470 |
106 |
MKDirpAsync(loop, req, path.c_str(), |
|
1471 |
106 |
req_wrap->continuation_data()->mode(), nullptr); |
|
1472 |
✓✓ | 106 |
break; |
1473 |
} |
||
1474 |
default: |
||
1475 |
445 |
uv_fs_req_cleanup(req); |
|
1476 |
// Stash err for use in the callback. |
||
1477 |
445 |
req->data = reinterpret_cast<void*>(static_cast<intptr_t>(err)); |
|
1478 |
445 |
int err = uv_fs_stat(loop, req, path.c_str(), |
|
1479 |
1335 |
uv_fs_callback_t{[](uv_fs_t* req) { |
|
1480 |
445 |
FSReqBase* req_wrap = FSReqBase::from_req(req); |
|
1481 |
445 |
int err = static_cast<int>(req->result); |
|
1482 |
✓✗✓✓ ✓✓ |
890 |
if (reinterpret_cast<intptr_t>(req->data) == UV_EEXIST && |
1483 |
445 |
req_wrap->continuation_data()->paths().size() > 0) { |
|
1484 |
✓✗✓✗ |
93 |
if (err == 0 && S_ISDIR(req->statbuf.st_mode)) { |
1485 |
93 |
Environment* env = req_wrap->env(); |
|
1486 |
93 |
uv_loop_t* loop = env->event_loop(); |
|
1487 |
186 |
std::string path = req->path; |
|
1488 |
93 |
uv_fs_req_cleanup(req); |
|
1489 |
93 |
MKDirpAsync(loop, req, path.c_str(), |
|
1490 |
93 |
req_wrap->continuation_data()->mode(), nullptr); |
|
1491 |
93 |
return; |
|
1492 |
} |
||
1493 |
err = UV_ENOTDIR; |
||
1494 |
} |
||
1495 |
// verify that the path pointed to is actually a directory. |
||
1496 |
✓✓✓✓ |
352 |
if (err == 0 && !S_ISDIR(req->statbuf.st_mode)) err = UV_EEXIST; |
1497 |
352 |
req_wrap->continuation_data()->Done(err); |
|
1498 |
1335 |
}}); |
|
1499 |
✗✓ | 445 |
if (err < 0) req_wrap->continuation_data()->Done(err); |
1500 |
445 |
break; |
|
1501 |
} |
||
1502 |
825 |
break; |
|
1503 |
1 |
} |
|
1504 |
3300 |
}}); |
|
1505 |
|||
1506 |
1650 |
return err; |
|
1507 |
} |
||
1508 |
|||
1509 |
233 |
int CallMKDirpSync(Environment* env, const FunctionCallbackInfo<Value>& args, |
|
1510 |
FSReqWrapSync* req_wrap, const char* path, int mode) { |
||
1511 |
233 |
env->PrintSyncTrace(); |
|
1512 |
466 |
int err = MKDirpSync(env->event_loop(), &req_wrap->req, path, mode, |
|
1513 |
233 |
nullptr); |
|
1514 |
✓✓ | 233 |
if (err < 0) { |
1515 |
4 |
v8::Local<v8::Context> context = env->context(); |
|
1516 |
8 |
v8::Local<v8::Object> ctx_obj = args[4].As<v8::Object>(); |
|
1517 |
4 |
v8::Isolate* isolate = env->isolate(); |
|
1518 |
8 |
ctx_obj->Set(context, |
|
1519 |
env->errno_string(), |
||
1520 |
16 |
v8::Integer::New(isolate, err)).Check(); |
|
1521 |
8 |
ctx_obj->Set(context, |
|
1522 |
env->syscall_string(), |
||
1523 |
16 |
OneByteString(isolate, "mkdir")).Check(); |
|
1524 |
} |
||
1525 |
233 |
return err; |
|
1526 |
} |
||
1527 |
|||
1528 |
1802 |
static void MKDir(const FunctionCallbackInfo<Value>& args) { |
|
1529 |
1802 |
Environment* env = Environment::GetCurrent(args); |
|
1530 |
|||
1531 |
1802 |
const int argc = args.Length(); |
|
1532 |
✗✓ | 1802 |
CHECK_GE(argc, 4); |
1533 |
|||
1534 |
3604 |
BufferValue path(env->isolate(), args[0]); |
|
1535 |
✗✓ | 1802 |
CHECK_NOT_NULL(*path); |
1536 |
|||
1537 |
✗✓ | 3604 |
CHECK(args[1]->IsInt32()); |
1538 |
5406 |
const int mode = args[1].As<Int32>()->Value(); |
|
1539 |
|||
1540 |
✗✓ | 3604 |
CHECK(args[2]->IsBoolean()); |
1541 |
3604 |
bool mkdirp = args[2]->IsTrue(); |
|
1542 |
|||
1543 |
1802 |
FSReqBase* req_wrap_async = GetReqWrap(args, 3); |
|
1544 |
✓✓ | 1802 |
if (req_wrap_async != nullptr) { // mkdir(path, mode, req) |
1545 |
✓✓✓✓ |
832 |
AsyncCall(env, req_wrap_async, args, "mkdir", UTF8, |
1546 |
mkdirp ? AfterMkdirp : AfterNoArgs, |
||
1547 |
832 |
mkdirp ? MKDirpAsync : uv_fs_mkdir, *path, mode); |
|
1548 |
} else { // mkdir(path, mode, undefined, ctx) |
||
1549 |
✗✓ | 970 |
CHECK_EQ(argc, 5); |
1550 |
✓✗ | 1940 |
FSReqWrapSync req_wrap_sync; |
1551 |
✓✓ | 970 |
FS_SYNC_TRACE_BEGIN(mkdir); |
1552 |
✓✓ | 970 |
if (mkdirp) { |
1553 |
235 |
int err = CallMKDirpSync(env, args, &req_wrap_sync, *path, mode); |
|
1554 |
✓✗✓✓ ✓✓✓✓ |
464 |
if (err == 0 && |
1555 |
231 |
!req_wrap_sync.continuation_data()->first_path().empty()) { |
|
1556 |
✓✗ | 6 |
Local<Value> error; |
1557 |
456 |
std::string first_path(req_wrap_sync.continuation_data()->first_path()); |
|
1558 |
227 |
FromNamespacedPath(&first_path); |
|
1559 |
2 |
MaybeLocal<Value> path = StringBytes::Encode(env->isolate(), |
|
1560 |
first_path.c_str(), |
||
1561 |
227 |
UTF8, &error); |
|
1562 |
✗✓ | 227 |
if (path.IsEmpty()) { |
1563 |
Local<Object> ctx = args[4].As<Object>(); |
||
1564 |
ctx->Set(env->context(), env->error_string(), error).Check(); |
||
1565 |
return; |
||
1566 |
} |
||
1567 |
✓✗ | 454 |
args.GetReturnValue().Set(path.ToLocalChecked()); |
1568 |
} |
||
1569 |
} else { |
||
1570 |
737 |
SyncCall(env, args[4], &req_wrap_sync, "mkdir", |
|
1571 |
737 |
uv_fs_mkdir, *path, mode); |
|
1572 |
} |
||
1573 |
✓✓ | 970 |
FS_SYNC_TRACE_END(mkdir); |
1574 |
} |
||
1575 |
2 |
} |
|
1576 |
✓✗ | 2 |
|
1577 |
44 |
static void RealPath(const FunctionCallbackInfo<Value>& args) { |
|
1578 |
✓✗ | 48 |
Environment* env = Environment::GetCurrent(args); |
1579 |
44 |
Isolate* isolate = env->isolate(); |
|
1580 |
|||
1581 |
✓✗ | 44 |
const int argc = args.Length(); |
1582 |
✗✓ | 42 |
CHECK_GE(argc, 3); |
1583 |
|||
1584 |
82 |
BufferValue path(isolate, args[0]); |
|
1585 |
✗✓ | 42 |
CHECK_NOT_NULL(*path); |
1586 |
|||
1587 |
42 |
const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); |
|
1588 |
|||
1589 |
42 |
FSReqBase* req_wrap_async = GetReqWrap(args, 2); |
|
1590 |
✓✓ | 42 |
if (req_wrap_async != nullptr) { // realpath(path, encoding, req) |
1591 |
22 |
AsyncCall(env, req_wrap_async, args, "realpath", encoding, AfterStringPtr, |
|
1592 |
22 |
uv_fs_realpath, *path); |
|
1593 |
} else { // realpath(path, encoding, undefined, ctx) |
||
1594 |
✗✓ | 20 |
CHECK_EQ(argc, 4); |
1595 |
✓✓ | 38 |
FSReqWrapSync req_wrap_sync; |
1596 |
✓✓ | 20 |
FS_SYNC_TRACE_BEGIN(realpath); |
1597 |
20 |
int err = SyncCall(env, args[3], &req_wrap_sync, "realpath", |
|
1598 |
21 |
uv_fs_realpath, *path); |
|
1599 |
✓✗✓✓ |
21 |
FS_SYNC_TRACE_END(realpath); |
1600 |
✓✓ | 21 |
if (err < 0) { |
1601 |
✓✗ | 6 |
return; // syscall failed, no need to continue, error info is in ctx |
1602 |
✓✗ | 2 |
} |
1603 |
1 |
||
1604 |
✓✗ | 22 |
const char* link_path = static_cast<const char*>(req_wrap_sync.req.ptr); |
1605 |
1 |
||
1606 |
Local<Value> error; |
||
1607 |
1 |
MaybeLocal<Value> rc = StringBytes::Encode(isolate, |
|
1608 |
link_path, |
||
1609 |
encoding, |
||
1610 |
18 |
&error); |
|
1611 |
✗✓ | 18 |
if (rc.IsEmpty()) { |
1612 |
Local<Object> ctx = args[3].As<Object>(); |
||
1613 |
ctx->Set(env->context(), env->error_string(), error).Check(); |
||
1614 |
return; |
||
1615 |
} |
||
1616 |
|||
1617 |
✓✓ | 36 |
args.GetReturnValue().Set(rc.ToLocalChecked()); |
1618 |
} |
||
1619 |
} |
||
1620 |
|||
1621 |
13676 |
static void ReadDir(const FunctionCallbackInfo<Value>& args) { |
|
1622 |
13676 |
Environment* env = Environment::GetCurrent(args); |
|
1623 |
13676 |
Isolate* isolate = env->isolate(); |
|
1624 |
|||
1625 |
13676 |
const int argc = args.Length(); |
|
1626 |
✗✓ | 13676 |
CHECK_GE(argc, 3); |
1627 |
|||
1628 |
27191 |
BufferValue path(isolate, args[0]); |
|
1629 |
✗✓ | 13676 |
CHECK_NOT_NULL(*path); |
1630 |
|||
1631 |
13676 |
const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); |
|
1632 |
|||
1633 |
27352 |
bool with_types = args[2]->IsTrue(); |
|
1634 |
|||
1635 |
13676 |
FSReqBase* req_wrap_async = GetReqWrap(args, 3); |
|
1636 |
✓✓ | 13676 |
if (req_wrap_async != nullptr) { // readdir(path, encoding, withTypes, req) |
1637 |
✓✓ | 249 |
if (with_types) { |
1638 |
160 |
AsyncCall(env, req_wrap_async, args, "scandir", encoding, |
|
1639 |
160 |
AfterScanDirWithTypes, uv_fs_scandir, *path, 0 /*flags*/); |
|
1640 |
} else { |
||
1641 |
89 |
AsyncCall(env, req_wrap_async, args, "scandir", encoding, |
|
1642 |
89 |
AfterScanDir, uv_fs_scandir, *path, 0 /*flags*/); |
|
1643 |
} |
||
1644 |
} else { // readdir(path, encoding, withTypes, undefined, ctx) |
||
1645 |
✗✓ | 13427 |
CHECK_EQ(argc, 5); |
1646 |
✓✓ | 26693 |
FSReqWrapSync req_wrap_sync; |
1647 |
✓✓ | 13427 |
FS_SYNC_TRACE_BEGIN(readdir); |
1648 |
13427 |
int err = SyncCall(env, args[4], &req_wrap_sync, "scandir", |
|
1649 |
13428 |
uv_fs_scandir, *path, 0 /*flags*/); |
|
1650 |
✓✗✓✓ |
13428 |
FS_SYNC_TRACE_END(readdir); |
1651 |
✓✓ | 13428 |
if (err < 0) { |
1652 |
✓✗ | 165 |
return; // syscall failed, no need to continue, error info is in ctx |
1653 |
✓✗ | 2 |
} |
1654 |
1 |
||
1655 |
✓✗✗✓ |
13270 |
CHECK_GE(req_wrap_sync.req.result, 0); |
1656 |
1 |
int r; |
|
1657 |
✓✓ | 26532 |
std::vector<Local<Value>> name_v; |
1658 |
✓✗ | 26533 |
std::vector<Local<Value>> type_v; |
1659 |
|||
1660 |
for (;;) { |
||
1661 |
uv_dirent_t ent; |
||
1662 |
|||
1663 |
715656 |
r = uv_fs_scandir_next(&(req_wrap_sync.req), &ent); |
|
1664 |
✓✓ | 715656 |
if (r == UV_EOF) |
1665 |
13266 |
break; |
|
1666 |
✗✓ | 702390 |
if (r != 0) { |
1667 |
Local<Object> ctx = args[4].As<Object>(); |
||
1668 |
ctx->Set(env->context(), env->errno_string(), |
||
1669 |
Integer::New(isolate, r)).Check(); |
||
1670 |
ctx->Set(env->context(), env->syscall_string(), |
||
1671 |
OneByteString(isolate, "readdir")).Check(); |
||
1672 |
return; |
||
1673 |
} |
||
1674 |
|||
1675 |
Local<Value> error; |
||
1676 |
MaybeLocal<Value> filename = StringBytes::Encode(isolate, |
||
1677 |
ent.name, |
||
1678 |
encoding, |
||
1679 |
702390 |
&error); |
|
1680 |
|||
1681 |
✗✓ | 702390 |
if (filename.IsEmpty()) { |
1682 |
Local<Object> ctx = args[4].As<Object>(); |
||
1683 |
ctx->Set(env->context(), env->error_string(), error).Check(); |
||
1684 |
return; |
||
1685 |
} |
||
1686 |
|||
1687 |
702390 |
name_v.push_back(filename.ToLocalChecked()); |
|
1688 |
|||
1689 |
✓✓ | 702390 |
if (with_types) { |
1690 |
45422 |
type_v.emplace_back(Integer::New(isolate, ent.type)); |
|
1691 |
} |
||
1692 |
702390 |
} |
|
1693 |
|||
1694 |
|||
1695 |
13266 |
Local<Array> names = Array::New(isolate, name_v.data(), name_v.size()); |
|
1696 |
✓✓ | 13266 |
if (with_types) { |
1697 |
Local<Value> result[] = { |
||
1698 |
names, |
||
1699 |
Array::New(isolate, type_v.data(), type_v.size()) |
||
1700 |
186 |
}; |
|
1701 |
186 |
args.GetReturnValue().Set(Array::New(isolate, result, arraysize(result))); |
|
1702 |
} else { |
||
1703 |
✓✗ | 26408 |
args.GetReturnValue().Set(names); |
1704 |
} |
||
1705 |
} |
||
1706 |
} |
||
1707 |
|||
1708 |
52099 |
static void Open(const FunctionCallbackInfo<Value>& args) { |
|
1709 |
52099 |
Environment* env = Environment::GetCurrent(args); |
|
1710 |
|||
1711 |
52099 |
const int argc = args.Length(); |
|
1712 |
✗✓ | 52099 |
CHECK_GE(argc, 3); |
1713 |
|||
1714 |
104198 |
BufferValue path(env->isolate(), args[0]); |
|
1715 |
✗✓ | 52099 |
CHECK_NOT_NULL(*path); |
1716 |
|||
1717 |
✗✓ | 104198 |
CHECK(args[1]->IsInt32()); |
1718 |
156297 |
const int flags = args[1].As<Int32>()->Value(); |
|
1719 |
|||
1720 |
✗✓ | 104198 |
CHECK(args[2]->IsInt32()); |
1721 |
156297 |
const int mode = args[2].As<Int32>()->Value(); |
|
1722 |
|||
1723 |
52099 |
FSReqBase* req_wrap_async = GetReqWrap(args, 3); |
|
1724 |
✓✓ | 52099 |
if (req_wrap_async != nullptr) { // open(path, flags, mode, req) |
1725 |
5390 |
req_wrap_async->set_is_plain_open(true); |
|
1726 |
5390 |
AsyncCall(env, req_wrap_async, args, "open", UTF8, AfterInteger, |
|
1727 |
5390 |
uv_fs_open, *path, flags, mode); |
|
1728 |
} else { // open(path, flags, mode, undefined, ctx) |
||
1729 |
✗✓ | 46709 |
CHECK_EQ(argc, 5); |
1730 |
93418 |
FSReqWrapSync req_wrap_sync; |
|
1731 |
✓✓✓✓ |
46745 |
FS_SYNC_TRACE_BEGIN(open); |
1732 |
46709 |
int result = SyncCall(env, args[4], &req_wrap_sync, "open", |
|
1733 |
46709 |
uv_fs_open, *path, flags, mode); |
|
1734 |
✓✓ | 46709 |
FS_SYNC_TRACE_END(open); |
1735 |
✓✓ | 46735 |
if (result >= 0) env->AddUnmanagedFd(result); |
1736 |
✓✗ | 93552 |
args.GetReturnValue().Set(result); |
1737 |
✓✓ | 72 |
} |
1738 |
52125 |
} |
|
1739 |
✓✗ | 134 |
|
1740 |
721 |
static void OpenFileHandle(const FunctionCallbackInfo<Value>& args) { |
|
1741 |
685 |
BindingData* binding_data = Environment::GetBindingData<BindingData>(args); |
|
1742 |
721 |
Environment* env = binding_data->env(); |
|
1743 |
685 |
Isolate* isolate = env->isolate(); |
|
1744 |
|||
1745 |
685 |
const int argc = args.Length(); |
|
1746 |
✗✓ | 685 |
CHECK_GE(argc, 3); |
1747 |
|||
1748 |
1370 |
BufferValue path(isolate, args[0]); |
|
1749 |
✗✓ | 685 |
CHECK_NOT_NULL(*path); |
1750 |
|||
1751 |
✗✓ | 1370 |
CHECK(args[1]->IsInt32()); |
1752 |
2055 |
const int flags = args[1].As<Int32>()->Value(); |
|
1753 |
|||
1754 |
✗✓ | 1370 |
CHECK(args[2]->IsInt32()); |
1755 |
2055 |
const int mode = args[2].As<Int32>()->Value(); |
|
1756 |
|||
1757 |
685 |
FSReqBase* req_wrap_async = GetReqWrap(args, 3); |
|
1758 |
✓✓ | 685 |
if (req_wrap_async != nullptr) { // openFileHandle(path, flags, mode, req) |
1759 |
684 |
AsyncCall(env, req_wrap_async, args, "open", UTF8, AfterOpenFileHandle, |
|
1760 |
684 |
uv_fs_open, *path, flags, mode); |
|
1761 |
} else { // openFileHandle(path, flags, mode, undefined, ctx) |
||
1762 |
✗✓ | 1 |
CHECK_EQ(argc, 5); |
1763 |
✓✗ | 2 |
FSReqWrapSync req_wrap_sync; |
1764 |
✗✓ | 1 |
FS_SYNC_TRACE_BEGIN(open); |
1765 |
1 |
int result = SyncCall(env, args[4], &req_wrap_sync, "open", |
|
1766 |
1 |
uv_fs_open, *path, flags, mode); |
|
1767 |
✗✗✗✓ |
1 |
FS_SYNC_TRACE_END(open); |
1768 |
✗✓ | 1 |
if (result < 0) { |
1769 |
return; // syscall failed, no need to continue, error info is in ctx |
||
1770 |
} |
||
1771 |
1 |
FileHandle* fd = FileHandle::New(binding_data, result); |
|
1772 |
✗✗✗✓ |
1 |
if (fd == nullptr) return; |
1773 |
✓✗ | 3 |
args.GetReturnValue().Set(fd->object()); |
1774 |
} |
||
1775 |
} |
||
1776 |
|||
1777 |
34 |
static void CopyFile(const FunctionCallbackInfo<Value>& args) { |
|
1778 |
34 |
Environment* env = Environment::GetCurrent(args); |
|
1779 |
34 |
Isolate* isolate = env->isolate(); |
|
1780 |
|||
1781 |
34 |
const int argc = args.Length(); |
|
1782 |
✗✓ | 34 |
CHECK_GE(argc, 3); |
1783 |
|||
1784 |
68 |
BufferValue src(isolate, args[0]); |
|
1785 |
✗✓ | 34 |
CHECK_NOT_NULL(*src); |
1786 |
|||
1787 |
68 |
BufferValue dest(isolate, args[1]); |
|
1788 |
✗✓ | 34 |
CHECK_NOT_NULL(*dest); |
1789 |
|||
1790 |
✗✓ | 68 |
CHECK(args[2]->IsInt32()); |
1791 |
102 |
const int flags = args[2].As<Int32>()->Value(); |
|
1792 |
|||
1793 |
34 |
FSReqBase* req_wrap_async = GetReqWrap(args, 3); |
|
1794 |
✓✓ | 34 |
if (req_wrap_async != nullptr) { // copyFile(src, dest, flags, req) |
1795 |
15 |
AsyncDestCall(env, req_wrap_async, args, "copyfile", |
|
1796 |
15 |
*dest, dest.length(), UTF8, AfterNoArgs, |
|
1797 |
15 |
uv_fs_copyfile, *src, *dest, flags); |
|
1798 |
} else { // copyFile(src, dest, flags, undefined, ctx) |
||
1799 |
✗✓ | 19 |
CHECK_EQ(argc, 5); |
1800 |
38 |
FSReqWrapSync req_wrap_sync; |
|
1801 |
✓✓ | 19 |
FS_SYNC_TRACE_BEGIN(copyfile); |
1802 |
19 |
SyncCall(env, args[4], &req_wrap_sync, "copyfile", |
|
1803 |
20 |
uv_fs_copyfile, *src, *dest, flags); |
|
1804 |
✓✗✓✓ |
20 |
FS_SYNC_TRACE_END(copyfile); |
1805 |
1 |
} |
|
1806 |
✓✗ | 38 |
} |
1807 |
✓✗ | 2 |
|
1808 |
1 |
||
1809 |
✓✗ | 4 |
// Wrapper for write(2). |
1810 |
1 |
// |
|
1811 |
// bytesWritten = write(fd, buffer, offset, length, position, callback) |
||
1812 |
1 |
// 0 fd integer. file descriptor |
|
1813 |
// 1 buffer the data to write |
||
1814 |
// 2 offset where in the buffer to start from |
||
1815 |
// 3 length how much to write |
||
1816 |
// 4 position if integer, position to write at in the file. |
||
1817 |
// if null, write from the current position |
||
1818 |
35761 |
static void WriteBuffer(const FunctionCallbackInfo<Value>& args) { |
|
1819 |
35761 |
Environment* env = Environment::GetCurrent(args); |
|
1820 |
|||
1821 |
35761 |
const int argc = args.Length(); |
|
1822 |
✗✓ | 35761 |
CHECK_GE(argc, 4); |
1823 |
|||
1824 |
✗✓ | 71522 |
CHECK(args[0]->IsInt32()); |
1825 |
107283 |
const int fd = args[0].As<Int32>()->Value(); |
|
1826 |
|||
1827 |
✗✓ | 35761 |
CHECK(Buffer::HasInstance(args[1])); |
1828 |
71522 |
Local<Object> buffer_obj = args[1].As<Object>(); |
|
1829 |
35761 |
char* buffer_data = Buffer::Data(buffer_obj); |
|
1830 |
35761 |
size_t buffer_length = Buffer::Length(buffer_obj); |
|
1831 |
|||
1832 |
✗✓ | 35761 |
CHECK(IsSafeJsInt(args[2])); |
1833 |
107283 |
const int64_t off_64 = args[2].As<Integer>()->Value(); |
|
1834 |
✗✓ | 35761 |
CHECK_GE(off_64, 0); |
1835 |
✗✓ | 35761 |
CHECK_LE(static_cast<uint64_t>(off_64), buffer_length); |
1836 |
35761 |
const size_t off = static_cast<size_t>(off_64); |
|
1837 |
|||
1838 |
✗✓ | 35761 |
CHECK(IsSafeJsInt(args[3])); |
1839 |
107283 |
const size_t len = static_cast<size_t>(args[3].As<Integer>()->Value()); |
|
1840 |
✗✓ | 35761 |
CHECK(Buffer::IsWithinBounds(off, len, buffer_length)); |
1841 |
✗✓ | 35761 |
CHECK_LE(len, buffer_length); |
1842 |
✗✓ | 35761 |
CHECK_GE(off + len, off); |
1843 |
|||
1844 |
35761 |
const int64_t pos = GetOffset(args[4]); |
|
1845 |
|||
1846 |
35761 |
char* buf = buffer_data + off; |
|
1847 |
35761 |
uv_buf_t uvbuf = uv_buf_init(buf, len); |
|
1848 |
|||
1849 |
35761 |
FSReqBase* req_wrap_async = GetReqWrap(args, 5); |
|
1850 |
✓✓ | 35761 |
if (req_wrap_async != nullptr) { // write(fd, buffer, off, len, pos, req) |
1851 |
AsyncCall(env, req_wrap_async, args, "write", UTF8, AfterInteger, |
||
1852 |
16359 |
uv_fs_write, fd, &uvbuf, 1, pos); |
|
1853 |
} else { // write(fd, buffer, off, len, pos, undefined, ctx) |
||
1854 |
✗✓ | 19402 |
CHECK_EQ(argc, 7); |
1855 |
38804 |
FSReqWrapSync req_wrap_sync; |
|
1856 |
✓✓✓✗ |
19427 |
FS_SYNC_TRACE_BEGIN(write); |
1857 |
19402 |
int bytesWritten = SyncCall(env, args[6], &req_wrap_sync, "write", |
|
1858 |
19402 |
uv_fs_write, fd, &uvbuf, 1, pos); |
|
1859 |
✓✓ | 19402 |
FS_SYNC_TRACE_END(write, "bytesWritten", bytesWritten); |
1860 |
58231 |
args.GetReturnValue().Set(bytesWritten); |
|
1861 |
✓✗ | 100 |
} |
1862 |
✓✗ | 35811 |
} |
1863 |
25 |
||
1864 |
✓✗ | 100 |
|
1865 |
25 |
// Wrapper for writev(2). |
|
1866 |
// |
||
1867 |
25 |
// bytesWritten = writev(fd, chunks, position, callback) |
|
1868 |
// 0 fd integer. file descriptor |
||
1869 |
// 1 chunks array of buffers to write |
||
1870 |
// 2 position if integer, position to write at in the file. |
||
1871 |
// if null, write from the current position |
||
1872 |
18 |
static void WriteBuffers(const FunctionCallbackInfo<Value>& args) { |
|
1873 |
18 |
Environment* env = Environment::GetCurrent(args); |
|
1874 |
|||
1875 |
18 |
const int argc = args.Length(); |
|
1876 |
✗✓ | 18 |
CHECK_GE(argc, 3); |
1877 |
|||
1878 |
✗✓ | 36 |
CHECK(args[0]->IsInt32()); |
1879 |
54 |
const int fd = args[0].As<Int32>()->Value(); |
|
1880 |
|||
1881 |
✗✓ | 36 |
CHECK(args[1]->IsArray()); |
1882 |
36 |
Local<Array> chunks = args[1].As<Array>(); |
|
1883 |
|||
1884 |
18 |
int64_t pos = GetOffset(args[2]); |
|
1885 |
|||
1886 |
36 |
MaybeStackBuffer<uv_buf_t> iovs(chunks->Length()); |
|
1887 |
|||
1888 |
✓✓ | 71 |
for (uint32_t i = 0; i < iovs.length(); i++) { |
1889 |
159 |
Local<Value> chunk = chunks->Get(env->context(), i).ToLocalChecked(); |
|
1890 |
✗✓ | 53 |
CHECK(Buffer::HasInstance(chunk)); |
1891 |
53 |
iovs[i] = uv_buf_init(Buffer::Data(chunk), Buffer::Length(chunk)); |
|
1892 |
} |
||
1893 |
|||
1894 |
18 |
FSReqBase* req_wrap_async = GetReqWrap(args, 3); |
|
1895 |
✓✓ | 18 |
if (req_wrap_async != nullptr) { // writeBuffers(fd, chunks, pos, req) |
1896 |
14 |
AsyncCall(env, req_wrap_async, args, "write", UTF8, AfterInteger, |
|
1897 |
14 |
uv_fs_write, fd, *iovs, iovs.length(), pos); |
|
1898 |
} else { // writeBuffers(fd, chunks, pos, undefined, ctx) |
||
1899 |
✗✓ | 4 |
CHECK_EQ(argc, 5); |
1900 |
8 |
FSReqWrapSync req_wrap_sync; |
|
1901 |
✗✓ | 4 |
FS_SYNC_TRACE_BEGIN(write); |
1902 |
8 |
int bytesWritten = SyncCall(env, args[4], &req_wrap_sync, "write", |
|
1903 |
4 |
uv_fs_write, fd, *iovs, iovs.length(), pos); |
|
1904 |
✗✗✗✓ |
4 |
FS_SYNC_TRACE_END(write, "bytesWritten", bytesWritten); |
1905 |
12 |
args.GetReturnValue().Set(bytesWritten); |
|
1906 |
} |
||
1907 |
✗✗ | 18 |
} |
1908 |
|||
1909 |
|||
1910 |
// Wrapper for write(2). |
||
1911 |
// |
||
1912 |
// bytesWritten = write(fd, string, position, enc, callback) |
||
1913 |
// 0 fd integer. file descriptor |
||
1914 |
// 1 string non-buffer values are converted to strings |
||
1915 |
// 2 position if integer, position to write at in the file. |
||
1916 |
// if null, write from the current position |
||
1917 |
// 3 enc encoding of string |
||
1918 |
98158 |
static void WriteString(const FunctionCallbackInfo<Value>& args) { |
|
1919 |
98158 |
Environment* env = Environment::GetCurrent(args); |
|
1920 |
98158 |
Isolate* isolate = env->isolate(); |
|
1921 |
|||
1922 |
98158 |
const int argc = args.Length(); |
|
1923 |
✗✓ | 98158 |
CHECK_GE(argc, 4); |
1924 |
|||
1925 |
✗✓ | 196316 |
CHECK(args[0]->IsInt32()); |
1926 |
294474 |
const int fd = args[0].As<Int32>()->Value(); |
|
1927 |
|||
1928 |
98158 |
const int64_t pos = GetOffset(args[2]); |
|
1929 |
|||
1930 |
98158 |
const auto enc = ParseEncoding(isolate, args[3], UTF8); |
|
1931 |
|||
1932 |
98158 |
Local<Value> value = args[1]; |
|
1933 |
98158 |
char* buf = nullptr; |
|
1934 |
size_t len; |
||
1935 |
|||
1936 |
98158 |
FSReqBase* req_wrap_async = GetReqWrap(args, 4); |
|
1937 |
98158 |
const bool is_async = req_wrap_async != nullptr; |
|
1938 |
|||
1939 |
// Avoid copying the string when it is externalized but only when: |
||
1940 |
// 1. The target encoding is compatible with the string's encoding, and |
||
1941 |
// 2. The write is synchronous, otherwise the string might get neutered |
||
1942 |
// while the request is in flight, and |
||
1943 |
// 3. For UCS2, when the host system is little-endian. Big-endian systems |
||
1944 |
// need to call StringBytes::Write() to ensure proper byte swapping. |
||
1945 |
// The const_casts are conceptually sound: memory is read but not written. |
||
1946 |
✓✓✓✗ ✓✓ |
294340 |
if (!is_async && value->IsString()) { |
1947 |
98091 |
auto string = value.As<String>(); |
|
1948 |
✓✗✓✓ ✓✗✓✓ |
98092 |
if ((enc == ASCII || enc == LATIN1) && string->IsExternalOneByte()) { |
1949 |
1 |
auto ext = string->GetExternalOneByteStringResource(); |
|
1950 |
1 |
buf = const_cast<char*>(ext->data()); |
|
1951 |
1 |
len = ext->length(); |
|
1952 |
✓✓✓✗ ✓✗✓✓ |
98091 |
} else if (enc == UCS2 && IsLittleEndian() && string->IsExternalTwoByte()) { |
1953 |
2 |
auto ext = string->GetExternalStringResource(); |
|
1954 |
1 |
buf = reinterpret_cast<char*>(const_cast<uint16_t*>(ext->data())); |
|
1955 |
1 |
len = ext->length() * sizeof(*ext->data()); |
|
1956 |
} |
||
1957 |
} |
||
1958 |
|||
1959 |
✓✓ | 98158 |
if (is_async) { // write(fd, string, pos, enc, req) |
1960 |
✗✓ | 67 |
CHECK_NOT_NULL(req_wrap_async); |
1961 |
✗✓ | 134 |
if (!StringBytes::StorageSize(isolate, value, enc).To(&len)) return; |
1962 |
FSReqBase::FSReqBuffer& stack_buffer = |
||
1963 |
67 |
req_wrap_async->Init("write", len, enc); |
|
1964 |
// StorageSize may return too large a char, so correct the actual length |
||
1965 |
// by the write size |
||
1966 |
67 |
len = StringBytes::Write(isolate, *stack_buffer, len, args[1], enc); |
|
1967 |
67 |
stack_buffer.SetLengthAndZeroTerminate(len); |
|
1968 |
67 |
uv_buf_t uvbuf = uv_buf_init(*stack_buffer, len); |
|
1969 |
67 |
int err = req_wrap_async->Dispatch(uv_fs_write, |
|
1970 |
fd, |
||
1971 |
&uvbuf, |
||
1972 |
1, |
||
1973 |
pos, |
||
1974 |
67 |
AfterInteger); |
|
1975 |
✗✓ | 67 |
if (err < 0) { |
1976 |
uv_fs_t* uv_req = req_wrap_async->req(); |
||
1977 |
uv_req->result = err; |
||
1978 |
uv_req->path = nullptr; |
||
1979 |
AfterInteger(uv_req); // after may delete req_wrap_async if there is |
||
1980 |
// an error |
||
1981 |
} else { |
||
1982 |
67 |
req_wrap_async->SetReturnValue(args); |
|
1983 |
} |
||
1984 |
} else { // write(fd, string, pos, enc, undefined, ctx) |
||
1985 |
✗✓ | 98091 |
CHECK_EQ(argc, 6); |
1986 |
196182 |
FSReqWrapSync req_wrap_sync; |
|
1987 |
✓✗ | 196182 |
FSReqBase::FSReqBuffer stack_buffer; |
1988 |
✓✓ | 98091 |
if (buf == nullptr) { |
1989 |
✗✓ | 196178 |
if (!StringBytes::StorageSize(isolate, value, enc).To(&len)) |
1990 |
return; |
||
1991 |
98089 |
stack_buffer.AllocateSufficientStorage(len + 1); |
|
1992 |
// StorageSize may return too large a char, so correct the actual length |
||
1993 |
// by the write size |
||
1994 |
98089 |
len = StringBytes::Write(isolate, *stack_buffer, |
|
1995 |
len, args[1], enc); |
||
1996 |
98089 |
stack_buffer.SetLengthAndZeroTerminate(len); |
|
1997 |
98089 |
buf = *stack_buffer; |
|
1998 |
} |
||
1999 |
98091 |
uv_buf_t uvbuf = uv_buf_init(buf, len); |
|
2000 |
✗✓ | 98091 |
FS_SYNC_TRACE_BEGIN(write); |
2001 |
98091 |
int bytesWritten = SyncCall(env, args[5], &req_wrap_sync, "write", |
|
2002 |
98091 |
uv_fs_write, fd, &uvbuf, 1, pos); |
|
2003 |
✗✗✗✓ |
98091 |
FS_SYNC_TRACE_END(write, "bytesWritten", bytesWritten); |
2004 |
✓✗ | 294273 |
args.GetReturnValue().Set(bytesWritten); |
2005 |
} |
||
2006 |
} |
||
2007 |
|||
2008 |
|||
2009 |
/* |
||
2010 |
* Wrapper for read(2). |
||
2011 |
* |
||
2012 |
* bytesRead = fs.read(fd, buffer, offset, length, position) |
||
2013 |
* |
||
2014 |
* 0 fd int32. file descriptor |
||
2015 |
* 1 buffer instance of Buffer |
||
2016 |
* 2 offset int64. offset to start reading into inside buffer |
||
2017 |
* 3 length int32. length to read |
||
2018 |
* 4 position int64. file position - -1 for current position |
||
2019 |
*/ |
||
2020 |
58370 |
static void Read(const FunctionCallbackInfo<Value>& args) { |
|
2021 |
58370 |
Environment* env = Environment::GetCurrent(args); |
|
2022 |
|||
2023 |
58370 |
const int argc = args.Length(); |
|
2024 |
✗✓ | 58370 |
CHECK_GE(argc, 5); |
2025 |
|||
2026 |
✗✓ | 116740 |
CHECK(args[0]->IsInt32()); |
2027 |
175110 |
const int fd = args[0].As<Int32>()->Value(); |
|
2028 |
|||
2029 |
✗✓ | 58370 |
CHECK(Buffer::HasInstance(args[1])); |
2030 |
116740 |
Local<Object> buffer_obj = args[1].As<Object>(); |
|
2031 |
58370 |
char* buffer_data = Buffer::Data(buffer_obj); |
|
2032 |
58370 |
size_t buffer_length = Buffer::Length(buffer_obj); |
|
2033 |
|||
2034 |
✗✓ | 58370 |
CHECK(IsSafeJsInt(args[2])); |
2035 |
175110 |
const int64_t off_64 = args[2].As<Integer>()->Value(); |
|
2036 |
✗✓ | 58370 |
CHECK_GE(off_64, 0); |
2037 |
✗✓ | 58370 |
CHECK_LT(static_cast<uint64_t>(off_64), buffer_length); |
2038 |
58370 |
const size_t off = static_cast<size_t>(off_64); |
|
2039 |
|||
2040 |
✗✓ | 116740 |
CHECK(args[3]->IsInt32()); |
2041 |
175110 |
const size_t len = static_cast<size_t>(args[3].As<Int32>()->Value()); |
|
2042 |
✗✓ | 58370 |
CHECK(Buffer::IsWithinBounds(off, len, buffer_length)); |
2043 |
|||
2044 |
✓✓✗✓ ✗✓ |
58378 |
CHECK(IsSafeJsInt(args[4]) || args[4]->IsBigInt()); |
2045 |
✓✓ | 233480 |
const int64_t pos = args[4]->IsNumber() ? |
2046 |
233468 |
args[4].As<Integer>()->Value() : |
|
2047 |
58382 |
args[4].As<BigInt>()->Int64Value(); |
|
2048 |
|||
2049 |
58370 |
char* buf = buffer_data + off; |
|
2050 |
58370 |
uv_buf_t uvbuf = uv_buf_init(buf, len); |
|
2051 |
|||
2052 |
58370 |
FSReqBase* req_wrap_async = GetReqWrap(args, 5); |
|
2053 |
✓✓ | 58370 |
if (req_wrap_async != nullptr) { // read(fd, buffer, offset, len, pos, req) |
2054 |
AsyncCall(env, req_wrap_async, args, "read", UTF8, AfterInteger, |
||
2055 |
13844 |
uv_fs_read, fd, &uvbuf, 1, pos); |
|
2056 |
} else { // read(fd, buffer, offset, len, pos, undefined, ctx) |
||
2057 |
✗✓ | 44526 |
CHECK_EQ(argc, 7); |
2058 |
89052 |
FSReqWrapSync req_wrap_sync; |
|
2059 |
✓✓✓✓ |
44531 |
FS_SYNC_TRACE_BEGIN(read); |
2060 |
44526 |
const int bytesRead = SyncCall(env, args[6], &req_wrap_sync, "read", |
|
2061 |
44526 |
uv_fs_read, fd, &uvbuf, 1, pos); |
|
2062 |
✓✓ | 44526 |
FS_SYNC_TRACE_END(read, "bytesRead", bytesRead); |
2063 |
133581 |
args.GetReturnValue().Set(bytesRead); |
|
2064 |
✓✗ | 18 |
} |
2065 |
✓✓ | 58380 |
} |
2066 |
3 |
||
2067 |
✓✗ | 18 |
|
2068 |
5 |
// Wrapper for readv(2). |
|
2069 |
// |
||
2070 |
5 |
// bytesRead = fs.readv(fd, buffers[, position], callback) |
|
2071 |
// 0 fd integer. file descriptor |
||
2072 |
// 1 buffers array of buffers to read |
||
2073 |
// 2 position if integer, position to read at in the file. |
||
2074 |
// if null, read from the current position |
||
2075 |
11 |
static void ReadBuffers(const FunctionCallbackInfo<Value>& args) { |
|
2076 |
11 |
Environment* env = Environment::GetCurrent(args); |
|
2077 |
|||
2078 |
11 |
const int argc = args.Length(); |
|
2079 |
✗✓ | 11 |
CHECK_GE(argc, 3); |
2080 |
|||
2081 |
✗✓ | 22 |
CHECK(args[0]->IsInt32()); |
2082 |
33 |
const int fd = args[0].As<Int32>()->Value(); |
|
2083 |
|||
2084 |
✗✓ | 22 |
CHECK(args[1]->IsArray()); |
2085 |
22 |
Local<Array> buffers = args[1].As<Array>(); |
|
2086 |
|||
2087 |
11 |
int64_t pos = GetOffset(args[2]); // -1 if not a valid JS int |
|
2088 |
|||
2089 |
22 |
MaybeStackBuffer<uv_buf_t> iovs(buffers->Length()); |
|
2090 |
|||
2091 |
// Init uv buffers from ArrayBufferViews |
||
2092 |
✓✓ | 28 |
for (uint32_t i = 0; i < iovs.length(); i++) { |
2093 |
51 |
Local<Value> buffer = buffers->Get(env->context(), i).ToLocalChecked(); |
|
2094 |
✗✓ | 17 |
CHECK(Buffer::HasInstance(buffer)); |
2095 |
17 |
iovs[i] = uv_buf_init(Buffer::Data(buffer), Buffer::Length(buffer)); |
|
2096 |
} |
||
2097 |
|||
2098 |
11 |
FSReqBase* req_wrap_async = GetReqWrap(args, 3); |
|
2099 |
✓✓ | 11 |
if (req_wrap_async != nullptr) { // readBuffers(fd, buffers, pos, req) |
2100 |
7 |
AsyncCall(env, req_wrap_async, args, "read", UTF8, AfterInteger, |
|
2101 |
7 |
uv_fs_read, fd, *iovs, iovs.length(), pos); |
|
2102 |
} else { // readBuffers(fd, buffers, undefined, ctx) |
||
2103 |
✗✓ | 4 |
CHECK_EQ(argc, 5); |
2104 |
8 |
FSReqWrapSync req_wrap_sync; |
|
2105 |
✗✓ | 4 |
FS_SYNC_TRACE_BEGIN(read); |
2106 |
8 |
int bytesRead = SyncCall(env, /* ctx */ args[4], &req_wrap_sync, "read", |
|
2107 |
4 |
uv_fs_read, fd, *iovs, iovs.length(), pos); |
|
2108 |
✗✗✗✓ |
4 |
FS_SYNC_TRACE_END(read, "bytesRead", bytesRead); |
2109 |
12 |
args.GetReturnValue().Set(bytesRead); |
|
2110 |
} |
||
2111 |
✗✗ | 11 |
} |
2112 |
|||
2113 |
|||
2114 |
/* fs.chmod(path, mode); |
||
2115 |
* Wrapper for chmod(1) / EIO_CHMOD |
||
2116 |
*/ |
||
2117 |
88 |
static void Chmod(const FunctionCallbackInfo<Value>& args) { |
|
2118 |
88 |
Environment* env = Environment::GetCurrent(args); |
|
2119 |
|||
2120 |
88 |
const int argc = args.Length(); |
|
2121 |
✗✓ | 88 |
CHECK_GE(argc, 2); |
2122 |
|||
2123 |
176 |
BufferValue path(env->isolate(), args[0]); |
|
2124 |
✗✓ | 88 |
CHECK_NOT_NULL(*path); |
2125 |
|||
2126 |
✗✓ | 176 |
CHECK(args[1]->IsInt32()); |
2127 |
264 |
int mode = args[1].As<Int32>()->Value(); |
|
2128 |
|||
2129 |
88 |
FSReqBase* req_wrap_async = GetReqWrap(args, 2); |
|
2130 |
✓✓ | 88 |
if (req_wrap_async != nullptr) { // chmod(path, mode, req) |
2131 |
73 |
AsyncCall(env, req_wrap_async, args, "chmod", UTF8, AfterNoArgs, |
|
2132 |
73 |
uv_fs_chmod, *path, mode); |
|
2133 |
} else { // chmod(path, mode, undefined, ctx) |
||
2134 |
✗✓ | 15 |
CHECK_EQ(argc, 4); |
2135 |
30 |
FSReqWrapSync req_wrap_sync; |
|
2136 |
✓✓ | 15 |
FS_SYNC_TRACE_BEGIN(chmod); |
2137 |
15 |
SyncCall(env, args[3], &req_wrap_sync, "chmod", |
|
2138 |
16 |
uv_fs_chmod, *path, mode); |
|
2139 |
✓✗✓✓ |
16 |
FS_SYNC_TRACE_END(chmod); |
2140 |
1 |
} |
|
2141 |
✓✗ | 92 |
} |
2142 |
✓✗ | 2 |
|
2143 |
1 |
||
2144 |
✓✗ | 4 |
/* fs.fchmod(fd, mode); |
2145 |
1 |
* Wrapper for fchmod(1) / EIO_FCHMOD |
|
2146 |
*/ |
||
2147 |
13 |
static void FChmod(const FunctionCallbackInfo<Value>& args) { |
|
2148 |
12 |
Environment* env = Environment::GetCurrent(args); |
|
2149 |
|||
2150 |
12 |
const int argc = args.Length(); |
|
2151 |
✗✓ | 12 |
CHECK_GE(argc, 2); |
2152 |
|||
2153 |
✗✓ | 24 |
CHECK(args[0]->IsInt32()); |
2154 |
36 |
const int fd = args[0].As<Int32>()->Value(); |
|
2155 |
|||
2156 |
✗✓ | 24 |
CHECK(args[1]->IsInt32()); |
2157 |
36 |
const int mode = args[1].As<Int32>()->Value(); |
|
2158 |
|||
2159 |
12 |
FSReqBase* req_wrap_async = GetReqWrap(args, 2); |
|
2160 |
✓✓ | 12 |
if (req_wrap_async != nullptr) { // fchmod(fd, mode, req) |
2161 |
AsyncCall(env, req_wrap_async, args, "fchmod", UTF8, AfterNoArgs, |
||
2162 |
7 |
uv_fs_fchmod, fd, mode); |
|
2163 |
} else { // fchmod(fd, mode, undefined, ctx) |
||
2164 |
✗✓ | 5 |
CHECK_EQ(argc, 4); |
2165 |
10 |
FSReqWrapSync req_wrap_sync; |
|
2166 |
✓✓ | 5 |
FS_SYNC_TRACE_BEGIN(fchmod); |
2167 |
SyncCall(env, args[3], &req_wrap_sync, "fchmod", |
||
2168 |
6 |
uv_fs_fchmod, fd, mode); |
|
2169 |
✓✗✓✓ |
6 |
FS_SYNC_TRACE_END(fchmod); |
2170 |
1 |
} |
|
2171 |
✓✗ | 16 |
} |
2172 |
✓✗ | 2 |
|
2173 |
1 |
||
2174 |
✓✗ | 4 |
/* fs.chown(path, uid, gid); |
2175 |
1 |
* Wrapper for chown(1) / EIO_CHOWN |
|
2176 |
*/ |
||
2177 |
5 |
static void Chown(const FunctionCallbackInfo<Value>& args) { |
|
2178 |
4 |
Environment* env = Environment::GetCurrent(args); |
|
2179 |
|||
2180 |
4 |
const int argc = args.Length(); |
|
2181 |
✗✓ | 4 |
CHECK_GE(argc, 3); |
2182 |
|||
2183 |
8 |
BufferValue path(env->isolate(), args[0]); |
|
2184 |
✗✓ | 4 |
CHECK_NOT_NULL(*path); |
2185 |
|||
2186 |
✗✓ | 8 |
CHECK(args[1]->IsUint32()); |
2187 |
12 |
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Uint32>()->Value()); |
|
2188 |
|||
2189 |
✗✓ | 8 |
CHECK(args[2]->IsUint32()); |
2190 |
12 |
const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Uint32>()->Value()); |
|
2191 |
|||
2192 |
4 |
FSReqBase* req_wrap_async = GetReqWrap(args, 3); |
|
2193 |
✓✓ | 4 |
if (req_wrap_async != nullptr) { // chown(path, uid, gid, req) |
2194 |
2 |
AsyncCall(env, req_wrap_async, args, "chown", UTF8, AfterNoArgs, |
|
2195 |
2 |
uv_fs_chown, *path, uid, gid); |
|
2196 |
} else { // chown(path, uid, gid, undefined, ctx) |
||
2197 |
✗✓ | 2 |
CHECK_EQ(argc, 5); |
2198 |
4 |
FSReqWrapSync req_wrap_sync; |
|
2199 |
✓✓ | 2 |
FS_SYNC_TRACE_BEGIN(chown); |
2200 |
2 |
SyncCall(env, args[4], &req_wrap_sync, "chown", |
|
2201 |
3 |
uv_fs_chown, *path, uid, gid); |
|
2202 |
✓✗✓✓ |
3 |
FS_SYNC_TRACE_END(chown); |
2203 |
1 |
} |
|
2204 |
✓✗ | 8 |
} |
2205 |
✓✗ | 2 |
|
2206 |
1 |
||
2207 |
✓✗ | 4 |
/* fs.fchown(fd, uid, gid); |
2208 |
1 |
* Wrapper for fchown(1) / EIO_FCHOWN |
|
2209 |
*/ |
||
2210 |
5 |
static void FChown(const FunctionCallbackInfo<Value>& args) { |
|
2211 |
4 |
Environment* env = Environment::GetCurrent(args); |
|
2212 |
|||
2213 |
4 |
const int argc = args.Length(); |
|
2214 |
✗✓ | 4 |
CHECK_GE(argc, 3); |
2215 |
|||
2216 |
✗✓ | 8 |
CHECK(args[0]->IsInt32()); |
2217 |
12 |
const int fd = args[0].As<Int32>()->Value(); |
|
2218 |
|||
2219 |
✗✓ | 8 |
CHECK(args[1]->IsUint32()); |
2220 |
12 |
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Uint32>()->Value()); |
|
2221 |
|||
2222 |
✗✓ | 8 |
CHECK(args[2]->IsUint32()); |
2223 |
12 |
const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Uint32>()->Value()); |
|
2224 |
|||
2225 |
4 |
FSReqBase* req_wrap_async = GetReqWrap(args, 3); |
|
2226 |
✓✓ | 4 |
if (req_wrap_async != nullptr) { // fchown(fd, uid, gid, req) |
2227 |
AsyncCall(env, req_wrap_async, args, "fchown", UTF8, AfterNoArgs, |
||
2228 |
2 |
uv_fs_fchown, fd, uid, gid); |
|
2229 |
} else { // fchown(fd, uid, gid, undefined, ctx) |
||
2230 |
✗✓ | 2 |
CHECK_EQ(argc, 5); |
2231 |
4 |
FSReqWrapSync req_wrap_sync; |
|
2232 |
✓✓ | 2 |
FS_SYNC_TRACE_BEGIN(fchown); |
2233 |
SyncCall(env, args[4], &req_wrap_sync, "fchown", |
||
2234 |
3 |
uv_fs_fchown, fd, uid, gid); |
|
2235 |
✓✗✓✓ |
3 |
FS_SYNC_TRACE_END(fchown); |
2236 |
1 |
} |
|
2237 |
✓✗ | 8 |
} |
2238 |
✓✗ | 2 |
|
2239 |
1 |
||
2240 |
✓✗ | 9 |
static void LChown(const FunctionCallbackInfo<Value>& args) { |
2241 |
6 |
Environment* env = Environment::GetCurrent(args); |
|
2242 |
|||
2243 |
6 |
const int argc = args.Length(); |
|
2244 |
✗✓ | 5 |
CHECK_GE(argc, 3); |
2245 |
|||
2246 |
10 |
BufferValue path(env->isolate(), args[0]); |
|
2247 |
✗✓ | 5 |
CHECK_NOT_NULL(*path); |
2248 |
|||
2249 |
✗✓ | 10 |
CHECK(args[1]->IsUint32()); |
2250 |
15 |
const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Uint32>()->Value()); |
|
2251 |
|||
2252 |
✗✓ | 10 |
CHECK(args[2]->IsUint32()); |
2253 |
15 |
const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Uint32>()->Value()); |
|
2254 |
|||
2255 |
5 |
FSReqBase* req_wrap_async = GetReqWrap(args, 3); |
|
2256 |
✓✓ | 5 |
if (req_wrap_async != nullptr) { // lchown(path, uid, gid, req) |
2257 |
3 |
AsyncCall(env, req_wrap_async, args, "lchown", UTF8, AfterNoArgs, |
|
2258 |
3 |
uv_fs_lchown, *path, uid, gid); |
|
2259 |
} else { // lchown(path, uid, gid, undefined, ctx) |
||
2260 |
✗✓ | 2 |
CHECK_EQ(argc, 5); |
2261 |
4 |
FSReqWrapSync req_wrap_sync; |
|
2262 |
✓✓ | 2 |
FS_SYNC_TRACE_BEGIN(lchown); |
2263 |
2 |
SyncCall(env, args[4], &req_wrap_sync, "lchown", |
|
2264 |
3 |
uv_fs_lchown, *path, uid, gid); |
|
2265 |
✓✗✓✓ |
3 |
FS_SYNC_TRACE_END(lchown); |
2266 |
1 |
} |
|
2267 |
✓✗ | 9 |
} |
2268 |
✓✗ | 2 |
|
2269 |
1 |
||
2270 |
✓✗ | 37 |
static void UTimes(const FunctionCallbackInfo<Value>& args) { |
2271 |
34 |
Environment* env = Environment::GetCurrent(args); |
|
2272 |
|||
2273 |
34 |
const int argc = args.Length(); |
|
2274 |
✗✓ | 33 |
CHECK_GE(argc, 3); |
2275 |
|||
2276 |
66 |
BufferValue path(env->isolate(), args[0]); |
|
2277 |
✗✓ | 33 |
CHECK_NOT_NULL(*path); |
2278 |
|||
2279 |
✗✓ | 66 |
CHECK(args[1]->IsNumber()); |
2280 |
99 |
const double atime = args[1].As<Number>()->Value(); |
|
2281 |
|||
2282 |
✗✓ | 66 |
CHECK(args[2]->IsNumber()); |
2283 |
99 |
const double mtime = args[2].As<Number>()->Value(); |
|
2284 |
|||
2285 |
33 |
FSReqBase* req_wrap_async = GetReqWrap(args, 3); |
|
2286 |
✓✓ | 33 |
if (req_wrap_async != nullptr) { // utimes(path, atime, mtime, req) |
2287 |
16 |
AsyncCall(env, req_wrap_async, args, "utime", UTF8, AfterNoArgs, |
|
2288 |
16 |
uv_fs_utime, *path, atime, mtime); |
|
2289 |
} else { // utimes(path, atime, mtime, undefined, ctx) |
||
2290 |
✗✓ | 17 |
CHECK_EQ(argc, 5); |
2291 |
34 |
FSReqWrapSync req_wrap_sync; |
|
2292 |
✓✓ | 17 |
FS_SYNC_TRACE_BEGIN(utimes); |
2293 |
17 |
SyncCall(env, args[4], &req_wrap_sync, "utime", |
|
2294 |
18 |
uv_fs_utime, *path, atime, mtime); |
|
2295 |
✓✗✓✓ |
18 |
FS_SYNC_TRACE_END(utimes); |
2296 |
1 |
} |
|
2297 |
✓✗ | 37 |
} |
2298 |
✓✗ | 2 |
|
2299 |
1908 |
static void FUTimes(const FunctionCallbackInfo<Value>& args) { |
|
2300 |
✓✗ | 1911 |
Environment* env = Environment::GetCurrent(args); |
2301 |
1 |
||
2302 |
1907 |
const int argc = args.Length(); |
|
2303 |
✗✓ | 1908 |
CHECK_GE(argc, 3); |
2304 |
|||
2305 |
✗✓ | 3814 |
CHECK(args[0]->IsInt32()); |
2306 |
5721 |
const int fd = args[0].As<Int32>()->Value(); |
|
2307 |
|||
2308 |
✗✓ | 3814 |
CHECK(args[1]->IsNumber()); |
2309 |
5721 |
const double atime = args[1].As<Number>()->Value(); |
|
2310 |
|||
2311 |
✗✓ | 3814 |
CHECK(args[2]->IsNumber()); |
2312 |
5721 |
const double mtime = args[2].As<Number>()->Value(); |
|
2313 |
|||
2314 |
1907 |
FSReqBase* req_wrap_async = GetReqWrap(args, 3); |
|
2315 |
✓✓ | 1907 |
if (req_wrap_async != nullptr) { // futimes(fd, atime, mtime, req) |
2316 |
AsyncCall(env, req_wrap_async, args, "futime", UTF8, AfterNoArgs, |
||
2317 |
1898 |
uv_fs_futime, fd, atime, mtime); |
|
2318 |
} else { // futimes(fd, atime, mtime, undefined, ctx) |
||
2319 |
✗✓ | 9 |
CHECK_EQ(argc, 5); |
2320 |
18 |
FSReqWrapSync req_wrap_sync; |
|
2321 |
✓✓ | 9 |
FS_SYNC_TRACE_BEGIN(futimes); |
2322 |
SyncCall(env, args[4], &req_wrap_sync, "futime", |
||
2323 |
10 |
uv_fs_futime, fd, atime, mtime); |
|
2324 |
✓✗✓✓ |
10 |
FS_SYNC_TRACE_END(futimes); |
2325 |
1 |
} |
|
2326 |
✓✗ | 1911 |
} |
2327 |
✓✗ | 2 |
|
2328 |
16 |
static void LUTimes(const FunctionCallbackInfo<Value>& args) { |
|
2329 |
✓✗ | 19 |
Environment* env = Environment::GetCurrent(args); |
2330 |
1 |
||
2331 |
15 |
const int argc = args.Length(); |
|
2332 |
✗✓ | 16 |
CHECK_GE(argc, 3); |
2333 |
|||
2334 |
30 |
BufferValue path(env->isolate(), args[0]); |
|
2335 |
✗✓ | 15 |
CHECK_NOT_NULL(*path); |
2336 |
|||
2337 |
✗✓ | 30 |
CHECK(args[1]->IsNumber()); |
2338 |
45 |
const double atime = args[1].As<Number>()->Value(); |
|
2339 |
|||
2340 |
✗✓ | 30 |
CHECK(args[2]->IsNumber()); |
2341 |
45 |
const double mtime = args[2].As<Number>()->Value(); |
|
2342 |
|||
2343 |
15 |
FSReqBase* req_wrap_async = GetReqWrap(args, 3); |
|
2344 |
✓✓ | 15 |
if (req_wrap_async != nullptr) { // lutimes(path, atime, mtime, req) |
2345 |
8 |
AsyncCall(env, req_wrap_async, args, "lutime", UTF8, AfterNoArgs, |
|
2346 |
8 |
uv_fs_lutime, *path, atime, mtime); |
|
2347 |
} else { // lutimes(path, atime, mtime, undefined, ctx) |
||
2348 |
✗✓ | 7 |
CHECK_EQ(argc, 5); |
2349 |
14 |
FSReqWrapSync req_wrap_sync; |
|
2350 |
✗✓ | 7 |
FS_SYNC_TRACE_BEGIN(lutimes); |
2351 |
7 |
SyncCall(env, args[4], &req_wrap_sync, "lutime", |
|
2352 |
7 |
uv_fs_lutime, *path, atime, mtime); |
|
2353 |
✗✗✗✓ |
7 |
FS_SYNC_TRACE_END(lutimes); |
2354 |
} |
||
2355 |
✗✗ | 15 |
} |
2356 |
|||
2357 |
14 |
static void Mkdtemp(const FunctionCallbackInfo<Value>& args) { |
|
2358 |
✗✗ | 14 |
Environment* env = Environment::GetCurrent(args); |
2359 |
14 |
Isolate* isolate = env->isolate(); |
|
2360 |
|||
2361 |
14 |
const int argc = args.Length(); |
|
2362 |
✗✓ | 14 |
CHECK_GE(argc, 2); |
2363 |
|||
2364 |
28 |
BufferValue tmpl(isolate, args[0]); |
|
2365 |
✗✓ | 14 |
CHECK_NOT_NULL(*tmpl); |
2366 |
|||
2367 |
14 |
const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); |
|
2368 |
|||
2369 |
14 |
FSReqBase* req_wrap_async = GetReqWrap(args, 2); |
|
2370 |
✓✓ | 14 |
if (req_wrap_async != nullptr) { // mkdtemp(tmpl, encoding, req) |
2371 |
6 |
AsyncCall(env, req_wrap_async, args, "mkdtemp", encoding, AfterStringPath, |
|
2372 |
6 |
uv_fs_mkdtemp, *tmpl); |
|
2373 |
} else { // mkdtemp(tmpl, encoding, undefined, ctx) |
||
2374 |
✗✓ | 8 |
CHECK_EQ(argc, 4); |
2375 |
✓✗ | 16 |
FSReqWrapSync req_wrap_sync; |
2376 |
✓✓ | 8 |
FS_SYNC_TRACE_BEGIN(mkdtemp); |
2377 |
8 |
SyncCall(env, args[3], &req_wrap_sync, "mkdtemp", |
|
2378 |
9 |
uv_fs_mkdtemp, *tmpl); |
|
2379 |
✓✗✓✓ |
9 |
FS_SYNC_TRACE_END(mkdtemp); |
2380 |
9 |
const char* path = req_wrap_sync.req.path; |
|
2381 |
✓✗ | 4 |
|
2382 |
✓✗ | 2 |
Local<Value> error; |
2383 |
1 |
MaybeLocal<Value> rc = |
|
2384 |
✓✗ | 12 |
StringBytes::Encode(isolate, path, encoding, &error); |
2385 |
✗✓ | 9 |
if (rc.IsEmpty()) { |
2386 |
Local<Object> ctx = args[3].As<Object>(); |
||
2387 |
1 |
ctx->Set(env->context(), env->error_string(), error).Check(); |
|
2388 |
return; |
||
2389 |
} |
||
2390 |
✓✗ | 16 |
args.GetReturnValue().Set(rc.ToLocalChecked()); |
2391 |
} |
||
2392 |
} |
||
2393 |
|||
2394 |
22 |
void BindingData::MemoryInfo(MemoryTracker* tracker) const { |
|
2395 |
22 |
tracker->TrackField("stats_field_array", stats_field_array); |
|
2396 |
22 |
tracker->TrackField("stats_field_bigint_array", stats_field_bigint_array); |
|
2397 |
22 |
tracker->TrackField("file_handle_read_wrap_freelist", |
|
2398 |
22 |
file_handle_read_wrap_freelist); |
|
2399 |
22 |
} |
|
2400 |
|||
2401 |
// TODO(addaleax): Remove once we're on C++17. |
||
2402 |
constexpr FastStringKey BindingData::type_name; |
||
2403 |
|||
2404 |
5024 |
void Initialize(Local<Object> target, |
|
2405 |
Local<Value> unused, |
||
2406 |
Local<Context> context, |
||
2407 |
void* priv) { |
||
2408 |
5024 |
Environment* env = Environment::GetCurrent(context); |
|
2409 |
5024 |
Isolate* isolate = env->isolate(); |
|
2410 |
BindingData* const binding_data = |
||
2411 |
5024 |
env->AddBindingData<BindingData>(context, target); |
|
2412 |
✗✓ | 5024 |
if (binding_data == nullptr) return; |
2413 |
|||
2414 |
5024 |
env->SetMethod(target, "access", Access); |
|
2415 |
5024 |
env->SetMethod(target, "close", Close); |
|
2416 |
5024 |
env->SetMethod(target, "open", Open); |
|
2417 |
5024 |
env->SetMethod(target, "openFileHandle", OpenFileHandle); |
|
2418 |
5024 |
env->SetMethod(target, "read", Read); |
|
2419 |
5024 |
env->SetMethod(target, "readBuffers", ReadBuffers); |
|
2420 |
5024 |
env->SetMethod(target, "fdatasync", Fdatasync); |
|
2421 |
5024 |
env->SetMethod(target, "fsync", Fsync); |
|
2422 |
5024 |
env->SetMethod(target, "rename", Rename); |
|
2423 |
5024 |
env->SetMethod(target, "ftruncate", FTruncate); |
|
2424 |
5024 |
env->SetMethod(target, "rmdir", RMDir); |
|
2425 |
5024 |
env->SetMethod(target, "mkdir", MKDir); |
|
2426 |
5024 |
env->SetMethod(target, "readdir", ReadDir); |
|
2427 |
5024 |
env->SetMethod(target, "internalModuleReadJSON", InternalModuleReadJSON); |
|
2428 |
5024 |
env->SetMethod(target, "internalModuleStat", InternalModuleStat); |
|
2429 |
5024 |
env->SetMethod(target, "stat", Stat); |
|
2430 |
5024 |
env->SetMethod(target, "lstat", LStat); |
|
2431 |
5024 |
env->SetMethod(target, "fstat", FStat); |
|
2432 |
5024 |
env->SetMethod(target, "link", Link); |
|
2433 |
5024 |
env->SetMethod(target, "symlink", Symlink); |
|
2434 |
5024 |
env->SetMethod(target, "readlink", ReadLink); |
|
2435 |
5024 |
env->SetMethod(target, "unlink", Unlink); |
|
2436 |
5024 |
env->SetMethod(target, "writeBuffer", WriteBuffer); |
|
2437 |
5024 |
env->SetMethod(target, "writeBuffers", WriteBuffers); |
|
2438 |
5024 |
env->SetMethod(target, "writeString", WriteString); |
|
2439 |
5024 |
env->SetMethod(target, "realpath", RealPath); |
|
2440 |
5024 |
env->SetMethod(target, "copyFile", CopyFile); |
|
2441 |
|||
2442 |
5024 |
env->SetMethod(target, "chmod", Chmod); |
|
2443 |
5024 |
env->SetMethod(target, "fchmod", FChmod); |
|
2444 |
// env->SetMethod(target, "lchmod", LChmod); |
||
2445 |
|||
2446 |
5024 |
env->SetMethod(target, "chown", Chown); |
|
2447 |
5024 |
env->SetMethod(target, "fchown", FChown); |
|
2448 |
5024 |
env->SetMethod(target, "lchown", LChown); |
|
2449 |
|||
2450 |
5024 |
env->SetMethod(target, "utimes", UTimes); |
|
2451 |
5024 |
env->SetMethod(target, "futimes", FUTimes); |
|
2452 |
5024 |
env->SetMethod(target, "lutimes", LUTimes); |
|
2453 |
|||
2454 |
5024 |
env->SetMethod(target, "mkdtemp", Mkdtemp); |
|
2455 |
|||
2456 |
target |
||
2457 |
10048 |
->Set(context, |
|
2458 |
FIXED_ONE_BYTE_STRING(isolate, "kFsStatsFieldsNumber"), |
||
2459 |
Integer::New( |
||
2460 |
isolate, |
||
2461 |
20096 |
static_cast<int32_t>(FsStatsOffset::kFsStatsFieldsNumber))) |
|
2462 |
.Check(); |
||
2463 |
|||
2464 |
10048 |
target->Set(context, |
|
2465 |
FIXED_ONE_BYTE_STRING(isolate, "statValues"), |
||
2466 |
20096 |
binding_data->stats_field_array.GetJSArray()).Check(); |
|
2467 |
|||
2468 |
10048 |
target->Set(context, |
|
2469 |
FIXED_ONE_BYTE_STRING(isolate, "bigintStatValues"), |
||
2470 |
20096 |
binding_data->stats_field_bigint_array.GetJSArray()).Check(); |
|
2471 |
|||
2472 |
5024 |
StatWatcher::Initialize(env, target); |
|
2473 |
|||
2474 |
// Create FunctionTemplate for FSReqCallback |
||
2475 |
5024 |
Local<FunctionTemplate> fst = env->NewFunctionTemplate(NewFSReqCallback); |
|
2476 |
15072 |
fst->InstanceTemplate()->SetInternalFieldCount( |
|
2477 |
5024 |
FSReqBase::kInternalFieldCount); |
|
2478 |
10048 |
fst->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
|
2479 |
5024 |
env->SetConstructorFunction(target, "FSReqCallback", fst); |
|
2480 |
|||
2481 |
// Create FunctionTemplate for FileHandleReadWrap. There’s no need |
||
2482 |
// to do anything in the constructor, so we only store the instance template. |
||
2483 |
5024 |
Local<FunctionTemplate> fh_rw = FunctionTemplate::New(isolate); |
|
2484 |
15072 |
fh_rw->InstanceTemplate()->SetInternalFieldCount( |
|
2485 |
5024 |
FSReqBase::kInternalFieldCount); |
|
2486 |
10048 |
fh_rw->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
|
2487 |
Local<String> fhWrapString = |
||
2488 |
5024 |
FIXED_ONE_BYTE_STRING(isolate, "FileHandleReqWrap"); |
|
2489 |
5024 |
fh_rw->SetClassName(fhWrapString); |
|
2490 |
5024 |
env->set_filehandlereadwrap_template( |
|
2491 |
5024 |
fst->InstanceTemplate()); |
|
2492 |
|||
2493 |
// Create Function Template for FSReqPromise |
||
2494 |
5024 |
Local<FunctionTemplate> fpt = FunctionTemplate::New(isolate); |
|
2495 |
10048 |
fpt->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
|
2496 |
Local<String> promiseString = |
||
2497 |
5024 |
FIXED_ONE_BYTE_STRING(isolate, "FSReqPromise"); |
|
2498 |
5024 |
fpt->SetClassName(promiseString); |
|
2499 |
5024 |
Local<ObjectTemplate> fpo = fpt->InstanceTemplate(); |
|
2500 |
5024 |
fpo->SetInternalFieldCount(FSReqBase::kInternalFieldCount); |
|
2501 |
5024 |
env->set_fsreqpromise_constructor_template(fpo); |
|
2502 |
|||
2503 |
// Create FunctionTemplate for FileHandle |
||
2504 |
5024 |
Local<FunctionTemplate> fd = env->NewFunctionTemplate(FileHandle::New); |
|
2505 |
10048 |
fd->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
|
2506 |
5024 |
env->SetProtoMethod(fd, "close", FileHandle::Close); |
|
2507 |
5024 |
env->SetProtoMethod(fd, "releaseFD", FileHandle::ReleaseFD); |
|
2508 |
5024 |
Local<ObjectTemplate> fdt = fd->InstanceTemplate(); |
|
2509 |
5024 |
fdt->SetInternalFieldCount(StreamBase::kInternalFieldCount); |
|
2510 |
5024 |
StreamBase::AddMethods(env, fd); |
|
2511 |
5024 |
env->SetConstructorFunction(target, "FileHandle", fd); |
|
2512 |
5024 |
env->set_fd_constructor_template(fdt); |
|
2513 |
|||
2514 |
// Create FunctionTemplate for FileHandle::CloseReq |
||
2515 |
5024 |
Local<FunctionTemplate> fdclose = FunctionTemplate::New(isolate); |
|
2516 |
10048 |
fdclose->SetClassName(FIXED_ONE_BYTE_STRING(isolate, |
|
2517 |
5024 |
"FileHandleCloseReq")); |
|
2518 |
10048 |
fdclose->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
|
2519 |
5024 |
Local<ObjectTemplate> fdcloset = fdclose->InstanceTemplate(); |
|
2520 |
5024 |
fdcloset->SetInternalFieldCount(FSReqBase::kInternalFieldCount); |
|
2521 |
5024 |
env->set_fdclose_constructor_template(fdcloset); |
|
2522 |
|||
2523 |
Local<Symbol> use_promises_symbol = |
||
2524 |
Symbol::New(isolate, |
||
2525 |
5024 |
FIXED_ONE_BYTE_STRING(isolate, "use promises")); |
|
2526 |
5024 |
env->set_fs_use_promises_symbol(use_promises_symbol); |
|
2527 |
10048 |
target->Set(context, |
|
2528 |
FIXED_ONE_BYTE_STRING(isolate, "kUsePromises"), |
||
2529 |
15072 |
use_promises_symbol).Check(); |
|
2530 |
} |
||
2531 |
|||
2532 |
3785 |
BindingData* FSReqBase::binding_data() { |
|
2533 |
3785 |
return binding_data_.get(); |
|
2534 |
} |
||
2535 |
} // namespace fs |
||
2536 |
|||
2537 |
} // end namespace node |
||
2538 |
|||
2539 |
✓✗✓✗ |
18788 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(fs, node::fs::Initialize) |
Generated by: GCOVR (Version 3.4) |