GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_file.cc Lines: 1450 1532 94.6 %
Date: 2021-11-06 03:13:44 Branches: 798 1250 63.8 %

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_external_reference.h"
27
#include "node_process-inl.h"
28
#include "node_stat_watcher.h"
29
#include "util-inl.h"
30
31
#include "tracing/trace_event.h"
32
33
#include "req_wrap-inl.h"
34
#include "stream_base-inl.h"
35
#include "string_bytes.h"
36
37
#include <fcntl.h>
38
#include <sys/types.h>
39
#include <sys/stat.h>
40
#include <cstring>
41
#include <cerrno>
42
#include <climits>
43
44
#if defined(__MINGW32__) || defined(_MSC_VER)
45
# include <io.h>
46
#endif
47
48
#include <memory>
49
50
namespace node {
51
52
namespace fs {
53
54
using v8::Array;
55
using v8::BigInt;
56
using v8::Boolean;
57
using v8::Context;
58
using v8::EscapableHandleScope;
59
using v8::Function;
60
using v8::FunctionCallbackInfo;
61
using v8::FunctionTemplate;
62
using v8::HandleScope;
63
using v8::Int32;
64
using v8::Integer;
65
using v8::Isolate;
66
using v8::Local;
67
using v8::MaybeLocal;
68
using v8::Number;
69
using v8::Object;
70
using v8::ObjectTemplate;
71
using v8::Promise;
72
using v8::String;
73
using v8::Symbol;
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
182116
inline int64_t GetOffset(Local<Value> value) {
111
183206
  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
961
FileHandle::FileHandle(BindingData* binding_data,
149
961
                       Local<Object> obj, int fd)
150
    : AsyncWrap(binding_data->env(), obj, AsyncWrap::PROVIDER_FILEHANDLE),
151
      StreamBase(env()),
152
      fd_(fd),
153
961
      binding_data_(binding_data) {
154
961
  MakeWeak();
155
961
  StreamBase::AttachToObject(GetObject());
156
961
}
157
158
961
FileHandle* FileHandle::New(BindingData* binding_data,
159
                            int fd, Local<Object> obj) {
160
961
  Environment* env = binding_data->env();
161
1907
  if (obj.IsEmpty() && !env->fd_constructor_template()
162
1907
                            ->NewInstance(env->context())
163
946
                            .ToLocal(&obj)) {
164
    return nullptr;
165
  }
166
961
  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
15
  CHECK(args[0]->IsInt32());
174
175
  FileHandle* handle =
176
30
      FileHandle::New(binding_data, args[0].As<Int32>()->Value(), args.This());
177
15
  if (handle == nullptr) return;
178
15
  if (args[1]->IsNumber())
179
30
    handle->read_offset_ = args[1]->IntegerValue(env->context()).FromJust();
180
30
  if (args[2]->IsNumber())
181
30
    handle->read_length_ = args[2]->IntegerValue(env->context()).FromJust();
182
}
183
184
3716
FileHandle::~FileHandle() {
185
1858
  CHECK(!closing_);  // We should not be deleting while explicitly closing!
186
1858
  Close();           // Close synchronously and emit warning
187
1858
  CHECK(closed_);    // We have to be closed at the point
188
3716
}
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
6
  return ret;
211
}
212
213
6
FileHandle::TransferData::TransferData(int fd) : fd_(fd) {}
214
215
24
FileHandle::TransferData::~TransferData() {
216
12
  if (fd_ > 0) {
217
    uv_fs_t close_req;
218
8
    CHECK_NE(fd_, -1);
219
8
    CHECK_EQ(0, uv_fs_close(nullptr, &close_req, fd_, nullptr));
220
8
    uv_fs_req_cleanup(&close_req);
221
  }
222
24
}
223
224
2
BaseObjectPtr<BaseObject> FileHandle::TransferData::Deserialize(
225
    Environment* env,
226
    v8::Local<v8::Context> context,
227
    std::unique_ptr<worker::TransferData> self) {
228
2
  BindingData* bd = Environment::GetBindingData<BindingData>(context);
229
2
  if (bd == nullptr) return {};
230
231
2
  int fd = fd_;
232
2
  fd_ = -1;
233
2
  return BaseObjectPtr<BaseObject> { FileHandle::New(bd, fd) };
234
}
235
236
// Close the file descriptor if it hasn't already been closed. A process
237
// warning will be emitted using a SetImmediate to avoid calling back to
238
// JS during GC. If closing the fd fails at this point, a fatal exception
239
// will crash the process immediately.
240
929
inline void FileHandle::Close() {
241

929
  if (closed_ || closing_) return;
242
  uv_fs_t req;
243
13
  CHECK_NE(fd_, -1);
244
13
  int ret = uv_fs_close(env()->event_loop(), &req, fd_, nullptr);
245
13
  uv_fs_req_cleanup(&req);
246
247
  struct err_detail { int ret; int fd; };
248
249
13
  err_detail detail { ret, fd_ };
250
251
13
  AfterClose();
252
253
13
  if (ret < 0) {
254
    // Do not unref this
255
    env()->SetImmediate([detail](Environment* env) {
256
      char msg[70];
257
      snprintf(msg, arraysize(msg),
258
              "Closing file descriptor %d on garbage collection failed",
259
              detail.fd);
260
      // This exception will end up being fatal for the process because
261
      // it is being thrown from within the SetImmediate handler and
262
      // there is no JS stack to bubble it to. In other words, tearing
263
      // down the process is the only reasonable thing we can do here.
264
      HandleScope handle_scope(env->isolate());
265
      env->ThrowUVException(detail.ret, "close", msg);
266
    });
267
    return;
268
  }
269
270
  // If the close was successful, we still want to emit a process warning
271
  // to notify that the file descriptor was gc'd. We want to be noisy about
272
  // this because not explicitly closing the FileHandle is a bug.
273
274
13
  env()->SetImmediate([detail](Environment* env) {
275
    ProcessEmitWarning(env,
276
                       "Closing file descriptor %d on garbage collection",
277
4
                       detail.fd);
278
4
    if (env->filehandle_close_warning()) {
279
3
      env->set_filehandle_close_warning(false);
280
3
      ProcessEmitDeprecationWarning(
281
          env,
282
          "Closing a FileHandle object on garbage collection is deprecated. "
283
          "Please close FileHandle objects explicitly using "
284
          "FileHandle.prototype.close(). In the future, an error will be "
285
          "thrown if a file descriptor is closed during garbage collection.",
286
3
          "DEP0137").IsNothing();
287
    }
288
4
  }, CallbackFlags::kUnrefed);
289
}
290
291
931
void FileHandle::CloseReq::Resolve() {
292
931
  Isolate* isolate = env()->isolate();
293
1841
  HandleScope scope(isolate);
294
931
  Context::Scope context_scope(env()->context());
295
931
  InternalCallbackScope callback_scope(this);
296
1862
  Local<Promise> promise = promise_.Get(isolate);
297
931
  Local<Promise::Resolver> resolver = promise.As<Promise::Resolver>();
298
2793
  resolver->Resolve(env()->context(), Undefined(isolate)).Check();
299
910
}
300
301
1
void FileHandle::CloseReq::Reject(Local<Value> reason) {
302
1
  Isolate* isolate = env()->isolate();
303
2
  HandleScope scope(isolate);
304
1
  Context::Scope context_scope(env()->context());
305
1
  InternalCallbackScope callback_scope(this);
306
2
  Local<Promise> promise = promise_.Get(isolate);
307
1
  Local<Promise::Resolver> resolver = promise.As<Promise::Resolver>();
308
2
  resolver->Reject(env()->context(), reason).Check();
309
1
}
310
311
932
FileHandle* FileHandle::CloseReq::file_handle() {
312
932
  Isolate* isolate = env()->isolate();
313
932
  HandleScope scope(isolate);
314
1864
  Local<Value> val = ref_.Get(isolate);
315
932
  Local<Object> obj = val.As<Object>();
316
932
  return Unwrap<FileHandle>(obj);
317
}
318
319
932
FileHandle::CloseReq::CloseReq(Environment* env,
320
                               Local<Object> obj,
321
                               Local<Promise> promise,
322
932
                               Local<Value> ref)
323
932
  : ReqWrap(env, obj, AsyncWrap::PROVIDER_FILEHANDLECLOSEREQ) {
324
932
  promise_.Reset(env->isolate(), promise);
325
932
  ref_.Reset(env->isolate(), ref);
326
932
}
327
328
7288
FileHandle::CloseReq::~CloseReq() {
329
1822
  uv_fs_req_cleanup(req());
330
1822
  promise_.Reset();
331
1822
  ref_.Reset();
332
3644
}
333
334
void FileHandle::CloseReq::MemoryInfo(MemoryTracker* tracker) const {
335
  tracker->TrackField("promise", promise_);
336
  tracker->TrackField("ref", ref_);
337
}
338
339
340
341
// Closes this FileHandle asynchronously and returns a Promise that will be
342
// resolved when the callback is invoked, or rejects with a UVException if
343
// there was a problem closing the fd. This is the preferred mechanism for
344
// closing the FD object even tho the object will attempt to close
345
// automatically on gc.
346
932
MaybeLocal<Promise> FileHandle::ClosePromise() {
347
932
  Isolate* isolate = env()->isolate();
348
932
  EscapableHandleScope scope(isolate);
349
932
  Local<Context> context = env()->context();
350
351
  Local<Value> close_resolver =
352
2796
      object()->GetInternalField(FileHandle::kClosingPromiseSlot);
353

2796
  if (!close_resolver.IsEmpty() && !close_resolver->IsUndefined()) {
354
    CHECK(close_resolver->IsPromise());
355
    return close_resolver.As<Promise>();
356
  }
357
358
932
  CHECK(!closed_);
359
932
  CHECK(!closing_);
360
932
  CHECK(!reading_);
361
362
932
  auto maybe_resolver = Promise::Resolver::New(context);
363
932
  CHECK(!maybe_resolver.IsEmpty());
364
932
  Local<Promise::Resolver> resolver = maybe_resolver.ToLocalChecked();
365
932
  Local<Promise> promise = resolver.As<Promise>();
366
367
  Local<Object> close_req_obj;
368
932
  if (!env()->fdclose_constructor_template()
369
1864
          ->NewInstance(env()->context()).ToLocal(&close_req_obj)) {
370
    return MaybeLocal<Promise>();
371
  }
372
932
  closing_ = true;
373
1864
  object()->SetInternalField(FileHandle::kClosingPromiseSlot, promise);
374
375
1864
  CloseReq* req = new CloseReq(env(), close_req_obj, promise, object());
376
1864
  auto AfterClose = uv_fs_callback_t{[](uv_fs_t* req) {
377
1843
    std::unique_ptr<CloseReq> close(CloseReq::from_req(req));
378
932
    CHECK_NOT_NULL(close);
379
932
    close->file_handle()->AfterClose();
380
932
    Isolate* isolate = close->env()->isolate();
381
932
    if (req->result < 0) {
382
2
      HandleScope handle_scope(isolate);
383
2
      close->Reject(
384
1
          UVException(isolate, static_cast<int>(req->result), "close"));
385
    } else {
386
931
      close->Resolve();
387
    }
388
911
  }};
389
932
  CHECK_NE(fd_, -1);
390
932
  int ret = req->Dispatch(uv_fs_close, fd_, AfterClose);
391
932
  if (ret < 0) {
392
    req->Reject(UVException(isolate, ret, "close"));
393
    delete req;
394
  }
395
396
932
  return scope.Escape(promise);
397
}
398
399
932
void FileHandle::Close(const FunctionCallbackInfo<Value>& args) {
400
  FileHandle* fd;
401
932
  ASSIGN_OR_RETURN_UNWRAP(&fd, args.Holder());
402
  Local<Promise> ret;
403
1864
  if (!fd->ClosePromise().ToLocal(&ret)) return;
404
1864
  args.GetReturnValue().Set(ret);
405
}
406
407
408
8
void FileHandle::ReleaseFD(const FunctionCallbackInfo<Value>& args) {
409
  FileHandle* fd;
410
8
  ASSIGN_OR_RETURN_UNWRAP(&fd, args.Holder());
411
  // Just act as if this FileHandle has been closed.
412
8
  fd->AfterClose();
413
}
414
415
416
953
void FileHandle::AfterClose() {
417
953
  closing_ = false;
418
953
  closed_ = true;
419
953
  fd_ = -1;
420

953
  if (reading_ && !persistent().IsEmpty())
421
    EmitRead(UV_EOF);
422
953
}
423
424
void FileHandleReadWrap::MemoryInfo(MemoryTracker* tracker) const {
425
  tracker->TrackField("buffer", buffer_);
426
  tracker->TrackField("file_handle", this->file_handle_);
427
}
428
429
16
FileHandleReadWrap::FileHandleReadWrap(FileHandle* handle, Local<Object> obj)
430
  : ReqWrap(handle->env(), obj, AsyncWrap::PROVIDER_FSREQCALLBACK),
431
16
    file_handle_(handle) {}
432
433
217
int FileHandle::ReadStart() {
434

217
  if (!IsAlive() || IsClosing())
435
    return UV_EOF;
436
437
217
  reading_ = true;
438
439
217
  if (current_read_)
440
3
    return 0;
441
442
428
  BaseObjectPtr<FileHandleReadWrap> read_wrap;
443
444
214
  if (read_length_ == 0) {
445
5
    EmitRead(UV_EOF);
446
5
    return 0;
447
  }
448
449
  {
450
    // Create a new FileHandleReadWrap or re-use one.
451
    // Either way, we need these two scopes for AsyncReset() or otherwise
452
    // for creating the new instance.
453
209
    HandleScope handle_scope(env()->isolate());
454
209
    AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(this);
455
456
209
    auto& freelist = binding_data_->file_handle_read_wrap_freelist;
457
209
    if (freelist.size() > 0) {
458
193
      read_wrap = std::move(freelist.back());
459
193
      freelist.pop_back();
460
      // Use a fresh async resource.
461
      // Lifetime is ensured via AsyncWrap::resource_.
462
193
      Local<Object> resource = Object::New(env()->isolate());
463
193
      USE(resource->Set(
464
579
          env()->context(), env()->handle_string(), read_wrap->object()));
465
193
      read_wrap->AsyncReset(resource);
466
193
      read_wrap->file_handle_ = this;
467
    } else {
468
      Local<Object> wrap_obj;
469
32
      if (!env()
470
16
               ->filehandlereadwrap_template()
471
16
               ->NewInstance(env()->context())
472
16
               .ToLocal(&wrap_obj)) {
473
        return UV_EBUSY;
474
      }
475
16
      read_wrap = MakeDetachedBaseObject<FileHandleReadWrap>(this, wrap_obj);
476
    }
477
  }
478
209
  int64_t recommended_read = 65536;
479

209
  if (read_length_ >= 0 && read_length_ <= recommended_read)
480
7
    recommended_read = read_length_;
481
482
209
  read_wrap->buffer_ = EmitAlloc(recommended_read);
483
484
209
  current_read_ = std::move(read_wrap);
485
486
418
  current_read_->Dispatch(uv_fs_read,
487
                          fd_,
488
209
                          &current_read_->buffer_,
489
                          1,
490
                          read_offset_,
491
209
                          uv_fs_callback_t{[](uv_fs_t* req) {
492
    FileHandle* handle;
493
    {
494
209
      FileHandleReadWrap* req_wrap = FileHandleReadWrap::from_req(req);
495
209
      handle = req_wrap->file_handle_;
496
209
      CHECK_EQ(handle->current_read_.get(), req_wrap);
497
    }
498
499
    // ReadStart() checks whether current_read_ is set to determine whether
500
    // a read is in progress. Moving it into a local variable makes sure that
501
    // the ReadStart() call below doesn't think we're still actively reading.
502
    BaseObjectPtr<FileHandleReadWrap> read_wrap =
503
418
        std::move(handle->current_read_);
504
505
209
    ssize_t result = req->result;
506
209
    uv_buf_t buffer = read_wrap->buffer_;
507
508
209
    uv_fs_req_cleanup(req);
509
510
    // Push the read wrap back to the freelist, or let it be destroyed
511
    // once we’re exiting the current scope.
512
209
    constexpr size_t kWantedFreelistFill = 100;
513
209
    auto& freelist = handle->binding_data_->file_handle_read_wrap_freelist;
514
209
    if (freelist.size() < kWantedFreelistFill) {
515
209
      read_wrap->Reset();
516
209
      freelist.emplace_back(std::move(read_wrap));
517
    }
518
519
209
    if (result >= 0) {
520
      // Read at most as many bytes as we originally planned to.
521

208
      if (handle->read_length_ >= 0 && handle->read_length_ < result)
522
        result = handle->read_length_;
523
524
      // If we read data and we have an expected length, decrease it by
525
      // how much we have read.
526
208
      if (handle->read_length_ >= 0)
527
9
        handle->read_length_ -= result;
528
529
      // If we have an offset, increase it by how much we have read.
530
208
      if (handle->read_offset_ >= 0)
531
204
        handle->read_offset_ += result;
532
    }
533
534
    // Reading 0 bytes from a file always means EOF, or that we reached
535
    // the end of the requested range.
536
209
    if (result == 0)
537
8
      result = UV_EOF;
538
539
209
    handle->EmitRead(result, buffer);
540
541
    // Start over, if EmitRead() didn’t tell us to stop.
542
209
    if (handle->reading_)
543
1
      handle->ReadStart();
544
209
  }});
545
546
209
  return 0;
547
}
548
549
228
int FileHandle::ReadStop() {
550
228
  reading_ = false;
551
228
  return 0;
552
}
553
554
typedef SimpleShutdownWrap<ReqWrap<uv_fs_t>> FileHandleCloseWrap;
555
556
3
ShutdownWrap* FileHandle::CreateShutdownWrap(Local<Object> object) {
557
3
  return new FileHandleCloseWrap(this, object);
558
}
559
560
3
int FileHandle::DoShutdown(ShutdownWrap* req_wrap) {
561

3
  if (closing_ || closed_) {
562
3
    req_wrap->Done(0);
563
3
    return 1;
564
  }
565
  FileHandleCloseWrap* wrap = static_cast<FileHandleCloseWrap*>(req_wrap);
566
  closing_ = true;
567
  CHECK_NE(fd_, -1);
568
  wrap->Dispatch(uv_fs_close, fd_, uv_fs_callback_t{[](uv_fs_t* req) {
569
    FileHandleCloseWrap* wrap = static_cast<FileHandleCloseWrap*>(
570
        FileHandleCloseWrap::from_req(req));
571
    FileHandle* handle = static_cast<FileHandle*>(wrap->stream());
572
    handle->AfterClose();
573
574
    int result = static_cast<int>(req->result);
575
    uv_fs_req_cleanup(req);
576
    wrap->Done(result);
577
  }});
578
579
  return 0;
580
}
581
582
583
4671
void FSReqCallback::Reject(Local<Value> reject) {
584
4671
  MakeCallback(env()->oncomplete_string(), 1, &reject);
585
4671
}
586
587
2431
void FSReqCallback::ResolveStat(const uv_stat_t* stat) {
588
2431
  Resolve(FillGlobalStatsArray(binding_data(), use_bigint(), stat));
589
2431
}
590
591
46188
void FSReqCallback::Resolve(Local<Value> value) {
592
  Local<Value> argv[2] {
593
    Null(env()->isolate()),
594
    value
595
46188
  };
596
  MakeCallback(env()->oncomplete_string(),
597
85506
               value->IsUndefined() ? 1 : arraysize(argv),
598
131694
               argv);
599
46183
}
600
601
50859
void FSReqCallback::SetReturnValue(const FunctionCallbackInfo<Value>& args) {
602
50859
  args.GetReturnValue().SetUndefined();
603
50859
}
604
605
50859
void NewFSReqCallback(const FunctionCallbackInfo<Value>& args) {
606
50859
  CHECK(args.IsConstructCall());
607
50859
  BindingData* binding_data = Environment::GetBindingData<BindingData>(args);
608
101718
  new FSReqCallback(binding_data, args.This(), args[0]->IsTrue());
609
50859
}
610
611
54912
FSReqAfterScope::FSReqAfterScope(FSReqBase* wrap, uv_fs_t* req)
612
    : wrap_(wrap),
613
      req_(req),
614
      handle_scope_(wrap->env()->isolate()),
615
54912
      context_scope_(wrap->env()->context()) {
616
54912
  CHECK_EQ(wrap_->req(), req);
617
54912
}
618
619
109814
FSReqAfterScope::~FSReqAfterScope() {
620
54907
  Clear();
621
54907
}
622
623
59906
void FSReqAfterScope::Clear() {
624
59906
  if (!wrap_) return;
625
626
54907
  uv_fs_req_cleanup(wrap_->req());
627
54907
  wrap_->Detach();
628
54907
  wrap_.reset();
629
}
630
631
// TODO(joyeecheung): create a normal context object, and
632
// construct the actual errors in the JS land using the context.
633
// The context should include fds for some fs APIs, currently they are
634
// missing in the error messages. The path, dest, syscall, fd, .etc
635
// can be put into the context before the binding is even invoked,
636
// the only information that has to come from the C++ layer is the
637
// error number (and possibly the syscall for abstraction),
638
// which is also why the errors should have been constructed
639
// in JS for more flexibility.
640
4988
void FSReqAfterScope::Reject(uv_fs_t* req) {
641
9976
  BaseObjectPtr<FSReqBase> wrap { wrap_ };
642
4988
  Local<Value> exception = UVException(wrap_->env()->isolate(),
643
4988
                                       static_cast<int>(req->result),
644
                                       wrap_->syscall(),
645
                                       nullptr,
646
                                       req->path,
647
4988
                                       wrap_->data());
648
4988
  Clear();
649
4988
  wrap->Reject(exception);
650
4988
}
651
652
54912
bool FSReqAfterScope::Proceed() {
653
54912
  if (req_->result < 0) {
654
4988
    Reject(req_);
655
4988
    return false;
656
  }
657
49924
  return true;
658
}
659
660
7255
void AfterNoArgs(uv_fs_t* req) {
661
7255
  FSReqBase* req_wrap = FSReqBase::from_req(req);
662
14507
  FSReqAfterScope after(req_wrap, req);
663
664
7255
  if (after.Proceed())
665
14190
    req_wrap->Resolve(Undefined(req_wrap->env()->isolate()));
666
7252
}
667
668
8384
void AfterStat(uv_fs_t* req) {
669
8384
  FSReqBase* req_wrap = FSReqBase::from_req(req);
670
16768
  FSReqAfterScope after(req_wrap, req);
671
672
8384
  if (after.Proceed()) {
673
3785
    req_wrap->ResolveStat(&req->statbuf);
674
  }
675
8384
}
676
677
37555
void AfterInteger(uv_fs_t* req) {
678
37555
  FSReqBase* req_wrap = FSReqBase::from_req(req);
679
75109
  FSReqAfterScope after(req_wrap, req);
680
681
37555
  int result = static_cast<int>(req->result);
682

37555
  if (result >= 0 && req_wrap->is_plain_open())
683
5485
    req_wrap->env()->AddUnmanagedFd(result);
684
685
37555
  if (after.Proceed())
686
74748
    req_wrap->Resolve(Integer::New(req_wrap->env()->isolate(), result));
687
37554
}
688
689
968
void AfterOpenFileHandle(uv_fs_t* req) {
690
968
  FSReqBase* req_wrap = FSReqBase::from_req(req);
691
968
  FSReqAfterScope after(req_wrap, req);
692
693
968
  if (after.Proceed()) {
694
943
    FileHandle* fd = FileHandle::New(req_wrap->binding_data(),
695
943
                                     static_cast<int>(req->result));
696
943
    if (fd == nullptr) return;
697
1886
    req_wrap->Resolve(fd->object());
698
  }
699
}
700
701
// Reverse the logic applied by path.toNamespacedPath() to create a
702
// namespace-prefixed path.
703
492
void FromNamespacedPath(std::string* path) {
704
#ifdef _WIN32
705
  if (path->compare(0, 8, "\\\\?\\UNC\\", 8) == 0) {
706
    *path = path->substr(8);
707
    path->insert(0, "\\\\");
708
  } else if (path->compare(0, 4, "\\\\?\\", 4) == 0) {
709
    *path = path->substr(4);
710
  }
711
#endif
712
492
}
713
714
365
void AfterMkdirp(uv_fs_t* req) {
715
365
  FSReqBase* req_wrap = FSReqBase::from_req(req);
716
365
  FSReqAfterScope after(req_wrap, req);
717
365
  if (after.Proceed()) {
718
718
    std::string first_path(req_wrap->continuation_data()->first_path());
719
359
    if (first_path.empty())
720
262
      return req_wrap->Resolve(Undefined(req_wrap->env()->isolate()));
721
228
    FromNamespacedPath(&first_path);
722
    Local<Value> path;
723
    Local<Value> error;
724
228
    if (!StringBytes::Encode(req_wrap->env()->isolate(), first_path.c_str(),
725
                             req_wrap->encoding(),
726
456
                             &error).ToLocal(&path)) {
727
      return req_wrap->Reject(error);
728
    }
729
228
    return req_wrap->Resolve(path);
730
  }
731
}
732
733
6
void AfterStringPath(uv_fs_t* req) {
734
6
  FSReqBase* req_wrap = FSReqBase::from_req(req);
735
12
  FSReqAfterScope after(req_wrap, req);
736
737
  MaybeLocal<Value> link;
738
  Local<Value> error;
739
740
6
  if (after.Proceed()) {
741
    link = StringBytes::Encode(req_wrap->env()->isolate(),
742
                               req->path,
743
                               req_wrap->encoding(),
744
5
                               &error);
745
5
    if (link.IsEmpty())
746
      req_wrap->Reject(error);
747
    else
748
10
      req_wrap->Resolve(link.ToLocalChecked());
749
  }
750
6
}
751
752
64
void AfterStringPtr(uv_fs_t* req) {
753
64
  FSReqBase* req_wrap = FSReqBase::from_req(req);
754
127
  FSReqAfterScope after(req_wrap, req);
755
756
  MaybeLocal<Value> link;
757
  Local<Value> error;
758
759
64
  if (after.Proceed()) {
760
    link = StringBytes::Encode(req_wrap->env()->isolate(),
761
58
                               static_cast<const char*>(req->ptr),
762
                               req_wrap->encoding(),
763
58
                               &error);
764
58
    if (link.IsEmpty())
765
      req_wrap->Reject(error);
766
    else
767
116
      req_wrap->Resolve(link.ToLocalChecked());
768
  }
769
63
}
770
771
152
void AfterScanDir(uv_fs_t* req) {
772
152
  FSReqBase* req_wrap = FSReqBase::from_req(req);
773
152
  FSReqAfterScope after(req_wrap, req);
774
775
152
  if (!after.Proceed()) {
776
8
    return;
777
  }
778
144
  Environment* env = req_wrap->env();
779
  Local<Value> error;
780
  int r;
781
144
  std::vector<Local<Value>> name_v;
782
783
  for (;;) {
784
    uv_dirent_t ent;
785
786
7591
    r = uv_fs_scandir_next(req, &ent);
787
7591
    if (r == UV_EOF)
788
144
      break;
789
7447
    if (r != 0) {
790
      return req_wrap->Reject(UVException(
791
          env->isolate(), r, nullptr, req_wrap->syscall(), req->path));
792
    }
793
794
    MaybeLocal<Value> filename =
795
      StringBytes::Encode(env->isolate(),
796
          ent.name,
797
          req_wrap->encoding(),
798
7447
          &error);
799
7447
    if (filename.IsEmpty())
800
      return req_wrap->Reject(error);
801
802
7447
    name_v.push_back(filename.ToLocalChecked());
803
7447
  }
804
805
288
  req_wrap->Resolve(Array::New(env->isolate(), name_v.data(), name_v.size()));
806
}
807
808
131
void AfterScanDirWithTypes(uv_fs_t* req) {
809
131
  FSReqBase* req_wrap = FSReqBase::from_req(req);
810
131
  FSReqAfterScope after(req_wrap, req);
811
812
131
  if (!after.Proceed()) {
813
1
    return;
814
  }
815
816
130
  Environment* env = req_wrap->env();
817
130
  Isolate* isolate = env->isolate();
818
  Local<Value> error;
819
  int r;
820
821
130
  std::vector<Local<Value>> name_v;
822
130
  std::vector<Local<Value>> type_v;
823
824
  for (;;) {
825
    uv_dirent_t ent;
826
827
147
    r = uv_fs_scandir_next(req, &ent);
828
147
    if (r == UV_EOF)
829
130
      break;
830
17
    if (r != 0) {
831
      return req_wrap->Reject(
832
          UVException(isolate, r, nullptr, req_wrap->syscall(), req->path));
833
    }
834
835
    MaybeLocal<Value> filename =
836
      StringBytes::Encode(isolate,
837
          ent.name,
838
          req_wrap->encoding(),
839
17
          &error);
840
17
    if (filename.IsEmpty())
841
      return req_wrap->Reject(error);
842
843
17
    name_v.push_back(filename.ToLocalChecked());
844
17
    type_v.emplace_back(Integer::New(isolate, ent.type));
845
17
  }
846
847
  Local<Value> result[] = {
848
    Array::New(isolate, name_v.data(), name_v.size()),
849
    Array::New(isolate, type_v.data(), type_v.size())
850
260
  };
851
260
  req_wrap->Resolve(Array::New(isolate, result, arraysize(result)));
852
}
853
854
291
void Access(const FunctionCallbackInfo<Value>& args) {
855
291
  Environment* env = Environment::GetCurrent(args);
856
291
  Isolate* isolate = env->isolate();
857
582
  HandleScope scope(isolate);
858
859
291
  const int argc = args.Length();
860
291
  CHECK_GE(argc, 2);
861
862
291
  CHECK(args[1]->IsInt32());
863
582
  int mode = args[1].As<Int32>()->Value();
864
865
582
  BufferValue path(isolate, args[0]);
866
291
  CHECK_NOT_NULL(*path);
867
868
291
  FSReqBase* req_wrap_async = GetReqWrap(args, 2);
869
291
  if (req_wrap_async != nullptr) {  // access(path, mode, req)
870
44
    AsyncCall(env, req_wrap_async, args, "access", UTF8, AfterNoArgs,
871
              uv_fs_access, *path, mode);
872
  } else {  // access(path, mode, undefined, ctx)
873
247
    CHECK_EQ(argc, 4);
874
494
    FSReqWrapSync req_wrap_sync;
875

249
    FS_SYNC_TRACE_BEGIN(access);
876
494
    SyncCall(env, args[3], &req_wrap_sync, "access", uv_fs_access, *path, mode);
877

249
    FS_SYNC_TRACE_END(access);
878
  }
879
291
}
880
881
882
51755
void Close(const FunctionCallbackInfo<Value>& args) {
883
51755
  Environment* env = Environment::GetCurrent(args);
884
885
51755
  const int argc = args.Length();
886
51755
  CHECK_GE(argc, 2);
887
888
51755
  CHECK(args[0]->IsInt32());
889
103510
  int fd = args[0].As<Int32>()->Value();
890
51755
  env->RemoveUnmanagedFd(fd);
891
892
51755
  FSReqBase* req_wrap_async = GetReqWrap(args, 1);
893
51755
  if (req_wrap_async != nullptr) {  // close(fd, req)
894
5414
    AsyncCall(env, req_wrap_async, args, "close", UTF8, AfterNoArgs,
895
              uv_fs_close, fd);
896
  } else {  // close(fd, undefined, ctx)
897
46341
    CHECK_EQ(argc, 3);
898
92682
    FSReqWrapSync req_wrap_sync;
899

46397
    FS_SYNC_TRACE_BEGIN(close);
900
46341
    SyncCall(env, args[2], &req_wrap_sync, "close", uv_fs_close, fd);
901

46397
    FS_SYNC_TRACE_END(close);
902
  }
903
51755
}
904
905
906
// Used to speed up module loading. Returns an array [string, boolean]
907
50886
static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
908
50886
  Environment* env = Environment::GetCurrent(args);
909
50886
  Isolate* isolate = env->isolate();
910
50886
  uv_loop_t* loop = env->event_loop();
911
912
101772
  CHECK(args[0]->IsString());
913
50886
  node::Utf8Value path(isolate, args[0]);
914
915
50886
  if (strlen(*path) != path.length()) {
916
3
    args.GetReturnValue().Set(Array::New(isolate));
917
3
    return;  // Contains a nul byte.
918
  }
919
  uv_fs_t open_req;
920
50883
  const int fd = uv_fs_open(loop, &open_req, *path, O_RDONLY, 0, nullptr);
921
50883
  uv_fs_req_cleanup(&open_req);
922
923
50883
  if (fd < 0) {
924
45356
    args.GetReturnValue().Set(Array::New(isolate));
925
45356
    return;
926
  }
927
928
5527
  auto defer_close = OnScopeLeave([fd, loop]() {
929
    uv_fs_t close_req;
930
5527
    CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr));
931
5527
    uv_fs_req_cleanup(&close_req);
932
5527
  });
933
934
5527
  const size_t kBlockSize = 32 << 10;
935
5527
  std::vector<char> chars;
936
5527
  int64_t offset = 0;
937
  ssize_t numchars;
938
  do {
939
5527
    const size_t start = chars.size();
940
5527
    chars.resize(start + kBlockSize);
941
942
    uv_buf_t buf;
943
5527
    buf.base = &chars[start];
944
5527
    buf.len = kBlockSize;
945
946
    uv_fs_t read_req;
947
5527
    numchars = uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr);
948
5527
    uv_fs_req_cleanup(&read_req);
949
950
5527
    if (numchars < 0) {
951
1
      args.GetReturnValue().Set(Array::New(isolate));
952
1
      return;
953
    }
954
5526
    offset += numchars;
955
5526
  } while (static_cast<size_t>(numchars) == kBlockSize);
956
957
5526
  size_t start = 0;
958

5526
  if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
959
1
    start = 3;  // Skip UTF-8 BOM.
960
  }
961
962
5526
  const size_t size = offset - start;
963
5526
  char* p = &chars[start];
964
5526
  char* pe = &chars[size];
965
  char* pos[2];
966
5526
  char** ppos = &pos[0];
967
968
82463
  while (p < pe) {
969
82451
    char c = *p++;
970

82451
    if (c == '\\' && p < pe && *p == '"') p++;
971
82451
    if (c != '"') continue;
972
12690
    *ppos++ = p;
973
12690
    if (ppos < &pos[2]) continue;
974
6345
    ppos = &pos[0];
975
976
6345
    char* s = &pos[0][0];
977
6345
    char* se = &pos[1][-1];  // Exclude quote.
978
6345
    size_t n = se - s;
979
980
6345
    if (n == 4) {
981
5507
      if (0 == memcmp(s, "main", 4)) break;
982
5466
      if (0 == memcmp(s, "name", 4)) break;
983
27
      if (0 == memcmp(s, "type", 4)) break;
984
838
    } else if (n == 7) {
985
12
      if (0 == memcmp(s, "exports", 7)) break;
986
6
      if (0 == memcmp(s, "imports", 7)) break;
987
    }
988
  }
989
990
991
  Local<Value> return_value[] = {
992
5526
    String::NewFromUtf8(isolate,
993
5526
                        &chars[start],
994
                        v8::NewStringType::kNormal,
995
5526
                        size).ToLocalChecked(),
996
    Boolean::New(isolate, p < pe ? true : false)
997
16578
  };
998
11052
  args.GetReturnValue().Set(
999
    Array::New(isolate, return_value, arraysize(return_value)));
1000
}
1001
1002
// Used to speed up module loading.  Returns 0 if the path refers to
1003
// a file, 1 when it's a directory or < 0 on error (usually -ENOENT.)
1004
// The speedup comes from not creating thousands of Stat and Error objects.
1005
132176
static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
1006
132176
  Environment* env = Environment::GetCurrent(args);
1007
1008
264352
  CHECK(args[0]->IsString());
1009
132176
  node::Utf8Value path(env->isolate(), args[0]);
1010
1011
  uv_fs_t req;
1012
132176
  int rc = uv_fs_stat(env->event_loop(), &req, *path, nullptr);
1013
132176
  if (rc == 0) {
1014
63558
    const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
1015
63558
    rc = !!(s->st_mode & S_IFDIR);
1016
  }
1017
132176
  uv_fs_req_cleanup(&req);
1018
1019
264352
  args.GetReturnValue().Set(rc);
1020
132176
}
1021
1022
17332
static void Stat(const FunctionCallbackInfo<Value>& args) {
1023
17332
  BindingData* binding_data = Environment::GetBindingData<BindingData>(args);
1024
17332
  Environment* env = binding_data->env();
1025
1026
17332
  const int argc = args.Length();
1027
17332
  CHECK_GE(argc, 2);
1028
1029
17332
  BufferValue path(env->isolate(), args[0]);
1030
17332
  CHECK_NOT_NULL(*path);
1031
1032
17332
  bool use_bigint = args[1]->IsTrue();
1033
17332
  FSReqBase* req_wrap_async = GetReqWrap(args, 2, use_bigint);
1034
17332
  if (req_wrap_async != nullptr) {  // stat(path, use_bigint, req)
1035
2529
    AsyncCall(env, req_wrap_async, args, "stat", UTF8, AfterStat,
1036
              uv_fs_stat, *path);
1037
  } else {  // stat(path, use_bigint, undefined, ctx)
1038
14803
    CHECK_EQ(argc, 4);
1039
14803
    FSReqWrapSync req_wrap_sync;
1040

14805
    FS_SYNC_TRACE_BEGIN(stat);
1041
29606
    int err = SyncCall(env, args[3], &req_wrap_sync, "stat", uv_fs_stat, *path);
1042

14805
    FS_SYNC_TRACE_END(stat);
1043
14803
    if (err != 0) {
1044
713
      return;  // error info is in ctx
1045
    }
1046
1047
    Local<Value> arr = FillGlobalStatsArray(binding_data, use_bigint,
1048
14090
        static_cast<const uv_stat_t*>(req_wrap_sync.req.ptr));
1049
28180
    args.GetReturnValue().Set(arr);
1050
  }
1051
}
1052
1053
100790
static void LStat(const FunctionCallbackInfo<Value>& args) {
1054
100790
  BindingData* binding_data = Environment::GetBindingData<BindingData>(args);
1055
100790
  Environment* env = binding_data->env();
1056
1057
100790
  const int argc = args.Length();
1058
100790
  CHECK_GE(argc, 3);
1059
1060
100790
  BufferValue path(env->isolate(), args[0]);
1061
100790
  CHECK_NOT_NULL(*path);
1062
1063
100790
  bool use_bigint = args[1]->IsTrue();
1064
100790
  FSReqBase* req_wrap_async = GetReqWrap(args, 2, use_bigint);
1065
100790
  if (req_wrap_async != nullptr) {  // lstat(path, use_bigint, req)
1066
4841
    AsyncCall(env, req_wrap_async, args, "lstat", UTF8, AfterStat,
1067
              uv_fs_lstat, *path);
1068
  } else {  // lstat(path, use_bigint, undefined, ctx)
1069
95949
    CHECK_EQ(argc, 4);
1070
95949
    FSReqWrapSync req_wrap_sync;
1071

95966
    FS_SYNC_TRACE_BEGIN(lstat);
1072
191898
    int err = SyncCall(env, args[3], &req_wrap_sync, "lstat", uv_fs_lstat,
1073
                       *path);
1074

95966
    FS_SYNC_TRACE_END(lstat);
1075
95949
    if (err != 0) {
1076
420
      return;  // error info is in ctx
1077
    }
1078
1079
    Local<Value> arr = FillGlobalStatsArray(binding_data, use_bigint,
1080
95529
        static_cast<const uv_stat_t*>(req_wrap_sync.req.ptr));
1081
191058
    args.GetReturnValue().Set(arr);
1082
  }
1083
}
1084
1085
41854
static void FStat(const FunctionCallbackInfo<Value>& args) {
1086
41854
  BindingData* binding_data = Environment::GetBindingData<BindingData>(args);
1087
41854
  Environment* env = binding_data->env();
1088
1089
41854
  const int argc = args.Length();
1090
41854
  CHECK_GE(argc, 2);
1091
1092
41854
  CHECK(args[0]->IsInt32());
1093
83708
  int fd = args[0].As<Int32>()->Value();
1094
1095
41854
  bool use_bigint = args[1]->IsTrue();
1096
41854
  FSReqBase* req_wrap_async = GetReqWrap(args, 2, use_bigint);
1097
41854
  if (req_wrap_async != nullptr) {  // fstat(fd, use_bigint, req)
1098
1016
    AsyncCall(env, req_wrap_async, args, "fstat", UTF8, AfterStat,
1099
              uv_fs_fstat, fd);
1100
  } else {  // fstat(fd, use_bigint, undefined, ctx)
1101
40838
    CHECK_EQ(argc, 4);
1102
40838
    FSReqWrapSync req_wrap_sync;
1103

40846
    FS_SYNC_TRACE_BEGIN(fstat);
1104
40838
    int err = SyncCall(env, args[3], &req_wrap_sync, "fstat", uv_fs_fstat, fd);
1105

40846
    FS_SYNC_TRACE_END(fstat);
1106
40838
    if (err != 0) {
1107
21
      return;  // error info is in ctx
1108
    }
1109
1110
    Local<Value> arr = FillGlobalStatsArray(binding_data, use_bigint,
1111
40817
        static_cast<const uv_stat_t*>(req_wrap_sync.req.ptr));
1112
81634
    args.GetReturnValue().Set(arr);
1113
  }
1114
}
1115
1116
349
static void Symlink(const FunctionCallbackInfo<Value>& args) {
1117
349
  Environment* env = Environment::GetCurrent(args);
1118
349
  Isolate* isolate = env->isolate();
1119
1120
349
  const int argc = args.Length();
1121
349
  CHECK_GE(argc, 4);
1122
1123
698
  BufferValue target(isolate, args[0]);
1124
349
  CHECK_NOT_NULL(*target);
1125
698
  BufferValue path(isolate, args[1]);
1126
349
  CHECK_NOT_NULL(*path);
1127
1128
349
  CHECK(args[2]->IsInt32());
1129
698
  int flags = args[2].As<Int32>()->Value();
1130
1131
349
  FSReqBase* req_wrap_async = GetReqWrap(args, 3);
1132
349
  if (req_wrap_async != nullptr) {  // symlink(target, path, flags, req)
1133
23
    AsyncDestCall(env, req_wrap_async, args, "symlink", *path, path.length(),
1134
                  UTF8, AfterNoArgs, uv_fs_symlink, *target, *path, flags);
1135
  } else {  // symlink(target, path, flags, undefinec, ctx)
1136
326
    CHECK_EQ(argc, 5);
1137
652
    FSReqWrapSync req_wrap_sync;
1138

330
    FS_SYNC_TRACE_BEGIN(symlink);
1139
652
    SyncCall(env, args[4], &req_wrap_sync, "symlink",
1140
             uv_fs_symlink, *target, *path, flags);
1141

330
    FS_SYNC_TRACE_END(symlink);
1142
  }
1143
349
}
1144
1145
9
static void Link(const FunctionCallbackInfo<Value>& args) {
1146
9
  Environment* env = Environment::GetCurrent(args);
1147
9
  Isolate* isolate = env->isolate();
1148
1149
9
  const int argc = args.Length();
1150
9
  CHECK_GE(argc, 3);
1151
1152
18
  BufferValue src(isolate, args[0]);
1153
9
  CHECK_NOT_NULL(*src);
1154
1155
18
  BufferValue dest(isolate, args[1]);
1156
9
  CHECK_NOT_NULL(*dest);
1157
1158
9
  FSReqBase* req_wrap_async = GetReqWrap(args, 2);
1159
9
  if (req_wrap_async != nullptr) {  // link(src, dest, req)
1160
4
    AsyncDestCall(env, req_wrap_async, args, "link", *dest, dest.length(), UTF8,
1161
                  AfterNoArgs, uv_fs_link, *src, *dest);
1162
  } else {  // link(src, dest)
1163
5
    CHECK_EQ(argc, 4);
1164
10
    FSReqWrapSync req_wrap_sync;
1165

11
    FS_SYNC_TRACE_BEGIN(link);
1166
10
    SyncCall(env, args[3], &req_wrap_sync, "link",
1167
             uv_fs_link, *src, *dest);
1168

11
    FS_SYNC_TRACE_END(link);
1169
  }
1170
9
}
1171
1172
102
static void ReadLink(const FunctionCallbackInfo<Value>& args) {
1173
102
  Environment* env = Environment::GetCurrent(args);
1174
102
  Isolate* isolate = env->isolate();
1175
1176
102
  const int argc = args.Length();
1177
102
  CHECK_GE(argc, 3);
1178
1179
102
  BufferValue path(isolate, args[0]);
1180
102
  CHECK_NOT_NULL(*path);
1181
1182
102
  const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
1183
1184
102
  FSReqBase* req_wrap_async = GetReqWrap(args, 2);
1185
102
  if (req_wrap_async != nullptr) {  // readlink(path, encoding, req)
1186
42
    AsyncCall(env, req_wrap_async, args, "readlink", encoding, AfterStringPtr,
1187
              uv_fs_readlink, *path);
1188
  } else {
1189
60
    CHECK_EQ(argc, 4);
1190
60
    FSReqWrapSync req_wrap_sync;
1191

62
    FS_SYNC_TRACE_BEGIN(readlink);
1192
120
    int err = SyncCall(env, args[3], &req_wrap_sync, "readlink",
1193
                       uv_fs_readlink, *path);
1194

62
    FS_SYNC_TRACE_END(readlink);
1195
60
    if (err < 0) {
1196
2
      return;  // syscall failed, no need to continue, error info is in ctx
1197
    }
1198
58
    const char* link_path = static_cast<const char*>(req_wrap_sync.req.ptr);
1199
1200
    Local<Value> error;
1201
    MaybeLocal<Value> rc = StringBytes::Encode(isolate,
1202
                                               link_path,
1203
                                               encoding,
1204
58
                                               &error);
1205
58
    if (rc.IsEmpty()) {
1206
      Local<Object> ctx = args[3].As<Object>();
1207
      ctx->Set(env->context(), env->error_string(), error).Check();
1208
      return;
1209
    }
1210
1211
116
    args.GetReturnValue().Set(rc.ToLocalChecked());
1212
  }
1213
}
1214
1215
6
static void Rename(const FunctionCallbackInfo<Value>& args) {
1216
6
  Environment* env = Environment::GetCurrent(args);
1217
6
  Isolate* isolate = env->isolate();
1218
1219
6
  const int argc = args.Length();
1220
6
  CHECK_GE(argc, 3);
1221
1222
12
  BufferValue old_path(isolate, args[0]);
1223
6
  CHECK_NOT_NULL(*old_path);
1224
12
  BufferValue new_path(isolate, args[1]);
1225
6
  CHECK_NOT_NULL(*new_path);
1226
1227
6
  FSReqBase* req_wrap_async = GetReqWrap(args, 2);
1228
6
  if (req_wrap_async != nullptr) {
1229
3
    AsyncDestCall(env, req_wrap_async, args, "rename", *new_path,
1230
                  new_path.length(), UTF8, AfterNoArgs, uv_fs_rename,
1231
                  *old_path, *new_path);
1232
  } else {
1233
3
    CHECK_EQ(argc, 4);
1234
6
    FSReqWrapSync req_wrap_sync;
1235

5
    FS_SYNC_TRACE_BEGIN(rename);
1236
6
    SyncCall(env, args[3], &req_wrap_sync, "rename", uv_fs_rename,
1237
             *old_path, *new_path);
1238

5
    FS_SYNC_TRACE_END(rename);
1239
  }
1240
6
}
1241
1242
69
static void FTruncate(const FunctionCallbackInfo<Value>& args) {
1243
69
  Environment* env = Environment::GetCurrent(args);
1244
1245
69
  const int argc = args.Length();
1246
69
  CHECK_GE(argc, 3);
1247
1248
69
  CHECK(args[0]->IsInt32());
1249
138
  const int fd = args[0].As<Int32>()->Value();
1250
1251
69
  CHECK(IsSafeJsInt(args[1]));
1252
138
  const int64_t len = args[1].As<Integer>()->Value();
1253
1254
69
  FSReqBase* req_wrap_async = GetReqWrap(args, 2);
1255
69
  if (req_wrap_async != nullptr) {
1256
54
    AsyncCall(env, req_wrap_async, args, "ftruncate", UTF8, AfterNoArgs,
1257
              uv_fs_ftruncate, fd, len);
1258
  } else {
1259
15
    CHECK_EQ(argc, 4);
1260
30
    FSReqWrapSync req_wrap_sync;
1261

17
    FS_SYNC_TRACE_BEGIN(ftruncate);
1262
15
    SyncCall(env, args[3], &req_wrap_sync, "ftruncate", uv_fs_ftruncate, fd,
1263
             len);
1264

17
    FS_SYNC_TRACE_END(ftruncate);
1265
  }
1266
69
}
1267
1268
7
static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
1269
7
  Environment* env = Environment::GetCurrent(args);
1270
1271
7
  const int argc = args.Length();
1272
7
  CHECK_GE(argc, 2);
1273
1274
7
  CHECK(args[0]->IsInt32());
1275
14
  const int fd = args[0].As<Int32>()->Value();
1276
1277
7
  FSReqBase* req_wrap_async = GetReqWrap(args, 1);
1278
7
  if (req_wrap_async != nullptr) {
1279
4
    AsyncCall(env, req_wrap_async, args, "fdatasync", UTF8, AfterNoArgs,
1280
              uv_fs_fdatasync, fd);
1281
  } else {
1282
3
    CHECK_EQ(argc, 3);
1283
6
    FSReqWrapSync req_wrap_sync;
1284

5
    FS_SYNC_TRACE_BEGIN(fdatasync);
1285
3
    SyncCall(env, args[2], &req_wrap_sync, "fdatasync", uv_fs_fdatasync, fd);
1286

5
    FS_SYNC_TRACE_END(fdatasync);
1287
  }
1288
7
}
1289
1290
21
static void Fsync(const FunctionCallbackInfo<Value>& args) {
1291
21
  Environment* env = Environment::GetCurrent(args);
1292
1293
21
  const int argc = args.Length();
1294
21
  CHECK_GE(argc, 2);
1295
1296
21
  CHECK(args[0]->IsInt32());
1297
42
  const int fd = args[0].As<Int32>()->Value();
1298
1299
21
  FSReqBase* req_wrap_async = GetReqWrap(args, 1);
1300
21
  if (req_wrap_async != nullptr) {
1301
4
    AsyncCall(env, req_wrap_async, args, "fsync", UTF8, AfterNoArgs,
1302
              uv_fs_fsync, fd);
1303
  } else {
1304
17
    CHECK_EQ(argc, 3);
1305
34
    FSReqWrapSync req_wrap_sync;
1306

19
    FS_SYNC_TRACE_BEGIN(fsync);
1307
17
    SyncCall(env, args[2], &req_wrap_sync, "fsync", uv_fs_fsync, fd);
1308

19
    FS_SYNC_TRACE_END(fsync);
1309
  }
1310
21
}
1311
1312
2405
static void Unlink(const FunctionCallbackInfo<Value>& args) {
1313
2405
  Environment* env = Environment::GetCurrent(args);
1314
1315
2405
  const int argc = args.Length();
1316
2405
  CHECK_GE(argc, 2);
1317
1318
4810
  BufferValue path(env->isolate(), args[0]);
1319
2405
  CHECK_NOT_NULL(*path);
1320
1321
2405
  FSReqBase* req_wrap_async = GetReqWrap(args, 1);
1322
2405
  if (req_wrap_async != nullptr) {
1323
965
    AsyncCall(env, req_wrap_async, args, "unlink", UTF8, AfterNoArgs,
1324
              uv_fs_unlink, *path);
1325
  } else {
1326
1440
    CHECK_EQ(argc, 3);
1327
2880
    FSReqWrapSync req_wrap_sync;
1328

1495
    FS_SYNC_TRACE_BEGIN(unlink);
1329
2880
    SyncCall(env, args[2], &req_wrap_sync, "unlink", uv_fs_unlink, *path);
1330

1495
    FS_SYNC_TRACE_END(unlink);
1331
  }
1332
2405
}
1333
1334
1662
static void RMDir(const FunctionCallbackInfo<Value>& args) {
1335
1662
  Environment* env = Environment::GetCurrent(args);
1336
1337
1662
  const int argc = args.Length();
1338
1662
  CHECK_GE(argc, 2);
1339
1340
3324
  BufferValue path(env->isolate(), args[0]);
1341
1662
  CHECK_NOT_NULL(*path);
1342
1343
1662
  FSReqBase* req_wrap_async = GetReqWrap(args, 1);  // rmdir(path, req)
1344
1662
  if (req_wrap_async != nullptr) {
1345
217
    AsyncCall(env, req_wrap_async, args, "rmdir", UTF8, AfterNoArgs,
1346
              uv_fs_rmdir, *path);
1347
  } else {  // rmdir(path, undefined, ctx)
1348
1445
    CHECK_EQ(argc, 3);
1349
2890
    FSReqWrapSync req_wrap_sync;
1350

1451
    FS_SYNC_TRACE_BEGIN(rmdir);
1351
2890
    SyncCall(env, args[2], &req_wrap_sync, "rmdir",
1352
             uv_fs_rmdir, *path);
1353

1451
    FS_SYNC_TRACE_END(rmdir);
1354
  }
1355
1662
}
1356
1357
5659
int MKDirpSync(uv_loop_t* loop,
1358
               uv_fs_t* req,
1359
               const std::string& path,
1360
               int mode,
1361
               uv_fs_cb cb) {
1362
5659
  FSReqWrapSync* req_wrap = ContainerOf(&FSReqWrapSync::req, req);
1363
1364
  // on the first iteration of algorithm, stash state information.
1365
5659
  if (req_wrap->continuation_data() == nullptr) {
1366
5659
    req_wrap->set_continuation_data(
1367
11318
        std::make_unique<FSContinuationData>(req, mode, cb));
1368
5659
    req_wrap->continuation_data()->PushPath(std::move(path));
1369
  }
1370
1371
11187
  while (req_wrap->continuation_data()->paths().size() > 0) {
1372
5807
    std::string next_path = req_wrap->continuation_data()->PopPath();
1373
5807
    int err = uv_fs_mkdir(loop, req, next_path.c_str(), mode, nullptr);
1374
    while (true) {
1375

5808
      switch (err) {
1376
        // Note: uv_fs_req_cleanup in terminal paths will be called by
1377
        // ~FSReqWrapSync():
1378
348
        case 0:
1379
348
          req_wrap->continuation_data()->MaybeSetFirstPath(next_path);
1380
348
          if (req_wrap->continuation_data()->paths().size() == 0) {
1381
275
            return 0;
1382
          }
1383
73
          break;
1384
2
        case UV_EACCES:
1385
        case UV_ENOTDIR:
1386
        case UV_EPERM: {
1387
2
          return err;
1388
        }
1389
75
        case UV_ENOENT: {
1390
          std::string dirname = next_path.substr(0,
1391
75
                                        next_path.find_last_of(kPathSeparator));
1392
75
          if (dirname != next_path) {
1393
74
            req_wrap->continuation_data()->PushPath(std::move(next_path));
1394
74
            req_wrap->continuation_data()->PushPath(std::move(dirname));
1395
1
          } else if (req_wrap->continuation_data()->paths().size() == 0) {
1396
1
            err = UV_EEXIST;
1397
1
            continue;
1398
          }
1399
74
          break;
1400
        }
1401
5383
        default:
1402
5383
          uv_fs_req_cleanup(req);
1403
5383
          int orig_err = err;
1404
5383
          err = uv_fs_stat(loop, req, next_path.c_str(), nullptr);
1405

5383
          if (err == 0 && !S_ISDIR(req->statbuf.st_mode)) {
1406
1
            uv_fs_req_cleanup(req);
1407

2
            if (orig_err == UV_EEXIST &&
1408
1
              req_wrap->continuation_data()->paths().size() > 0) {
1409
              return UV_ENOTDIR;
1410
            }
1411
1
            return UV_EEXIST;
1412
          }
1413
5382
          if (err < 0) return err;
1414
5381
          break;
1415
      }
1416
5528
      break;
1417
1
    }
1418
5528
    uv_fs_req_cleanup(req);
1419
  }
1420
1421
5380
  return 0;
1422
}
1423
1424
629
int MKDirpAsync(uv_loop_t* loop,
1425
                uv_fs_t* req,
1426
                const char* path,
1427
                int mode,
1428
                uv_fs_cb cb) {
1429
629
  FSReqBase* req_wrap = FSReqBase::from_req(req);
1430
  // on the first iteration of algorithm, stash state information.
1431
629
  if (req_wrap->continuation_data() == nullptr) {
1432
365
    req_wrap->set_continuation_data(
1433
730
        std::make_unique<FSContinuationData>(req, mode, cb));
1434
365
    req_wrap->continuation_data()->PushPath(std::move(path));
1435
  }
1436
1437
  // on each iteration of algorithm, mkdir directory on top of stack.
1438
629
  std::string next_path = req_wrap->continuation_data()->PopPath();
1439
629
  int err = uv_fs_mkdir(loop, req, next_path.c_str(), mode,
1440
629
                        uv_fs_callback_t{[](uv_fs_t* req) {
1441
629
    FSReqBase* req_wrap = FSReqBase::from_req(req);
1442
629
    Environment* env = req_wrap->env();
1443
629
    uv_loop_t* loop = env->event_loop();
1444
1258
    std::string path = req->path;
1445
629
    int err = static_cast<int>(req->result);
1446
1447
    while (true) {
1448

630
      switch (err) {
1449
        // Note: uv_fs_req_cleanup in terminal paths will be called by
1450
        // FSReqAfterScope::~FSReqAfterScope()
1451
239
        case 0: {
1452
239
          if (req_wrap->continuation_data()->paths().size() == 0) {
1453
228
            req_wrap->continuation_data()->MaybeSetFirstPath(path);
1454
228
            req_wrap->continuation_data()->Done(0);
1455
          } else {
1456
11
            req_wrap->continuation_data()->MaybeSetFirstPath(path);
1457
11
            uv_fs_req_cleanup(req);
1458
11
            MKDirpAsync(loop, req, path.c_str(),
1459
                        req_wrap->continuation_data()->mode(), nullptr);
1460
          }
1461
239
          break;
1462
        }
1463
3
        case UV_EACCES:
1464
        case UV_ENOTDIR:
1465
        case UV_EPERM: {
1466
3
          req_wrap->continuation_data()->Done(err);
1467
3
          break;
1468
        }
1469
133
        case UV_ENOENT: {
1470
          std::string dirname = path.substr(0,
1471
133
                                            path.find_last_of(kPathSeparator));
1472
133
          if (dirname != path) {
1473
132
            req_wrap->continuation_data()->PushPath(std::move(path));
1474
132
            req_wrap->continuation_data()->PushPath(std::move(dirname));
1475
1
          } else if (req_wrap->continuation_data()->paths().size() == 0) {
1476
1
            err = UV_EEXIST;
1477
1
            continue;
1478
          }
1479
132
          uv_fs_req_cleanup(req);
1480
132
          MKDirpAsync(loop, req, path.c_str(),
1481
                      req_wrap->continuation_data()->mode(), nullptr);
1482
132
          break;
1483
        }
1484
255
        default:
1485
255
          uv_fs_req_cleanup(req);
1486
          // Stash err for use in the callback.
1487
255
          req->data = reinterpret_cast<void*>(static_cast<intptr_t>(err));
1488
255
          int err = uv_fs_stat(loop, req, path.c_str(),
1489
255
                               uv_fs_callback_t{[](uv_fs_t* req) {
1490
255
            FSReqBase* req_wrap = FSReqBase::from_req(req);
1491
255
            int err = static_cast<int>(req->result);
1492

510
            if (reinterpret_cast<intptr_t>(req->data) == UV_EEXIST &&
1493
255
                  req_wrap->continuation_data()->paths().size() > 0) {
1494

121
              if (err == 0 && S_ISDIR(req->statbuf.st_mode)) {
1495
121
                Environment* env = req_wrap->env();
1496
121
                uv_loop_t* loop = env->event_loop();
1497
121
                std::string path = req->path;
1498
121
                uv_fs_req_cleanup(req);
1499
121
                MKDirpAsync(loop, req, path.c_str(),
1500
                            req_wrap->continuation_data()->mode(), nullptr);
1501
121
                return;
1502
              }
1503
              err = UV_ENOTDIR;
1504
            }
1505
            // verify that the path pointed to is actually a directory.
1506

134
            if (err == 0 && !S_ISDIR(req->statbuf.st_mode)) err = UV_EEXIST;
1507
134
            req_wrap->continuation_data()->Done(err);
1508
255
          }});
1509
255
          if (err < 0) req_wrap->continuation_data()->Done(err);
1510
255
          break;
1511
      }
1512
629
      break;
1513
1
    }
1514
629
  }});
1515
1516
629
  return err;
1517
}
1518
1519
270
int CallMKDirpSync(Environment* env, const FunctionCallbackInfo<Value>& args,
1520
                   FSReqWrapSync* req_wrap, const char* path, int mode) {
1521
270
  env->PrintSyncTrace();
1522
270
  int err = MKDirpSync(env->event_loop(), &req_wrap->req, path, mode,
1523
                       nullptr);
1524
270
  if (err < 0) {
1525
4
    v8::Local<v8::Context> context = env->context();
1526
4
    v8::Local<v8::Object> ctx_obj = args[4].As<v8::Object>();
1527
4
    v8::Isolate* isolate = env->isolate();
1528
4
    ctx_obj->Set(context,
1529
                 env->errno_string(),
1530
16
                 v8::Integer::New(isolate, err)).Check();
1531
4
    ctx_obj->Set(context,
1532
                 env->syscall_string(),
1533
16
                 OneByteString(isolate, "mkdir")).Check();
1534
  }
1535
270
  return err;
1536
}
1537
1538
1702
static void MKDir(const FunctionCallbackInfo<Value>& args) {
1539
1702
  Environment* env = Environment::GetCurrent(args);
1540
1541
1702
  const int argc = args.Length();
1542
1702
  CHECK_GE(argc, 4);
1543
1544
1702
  BufferValue path(env->isolate(), args[0]);
1545
1702
  CHECK_NOT_NULL(*path);
1546
1547
1702
  CHECK(args[1]->IsInt32());
1548
3404
  const int mode = args[1].As<Int32>()->Value();
1549
1550
1702
  CHECK(args[2]->IsBoolean());
1551
1702
  bool mkdirp = args[2]->IsTrue();
1552
1553
1702
  FSReqBase* req_wrap_async = GetReqWrap(args, 3);
1554
1702
  if (req_wrap_async != nullptr) {  // mkdir(path, mode, req)
1555

588
    AsyncCall(env, req_wrap_async, args, "mkdir", UTF8,
1556
              mkdirp ? AfterMkdirp : AfterNoArgs,
1557
              mkdirp ? MKDirpAsync : uv_fs_mkdir, *path, mode);
1558
  } else {  // mkdir(path, mode, undefined, ctx)
1559
1114
    CHECK_EQ(argc, 5);
1560
1114
    FSReqWrapSync req_wrap_sync;
1561

1118
    FS_SYNC_TRACE_BEGIN(mkdir);
1562
1114
    if (mkdirp) {
1563
270
      int err = CallMKDirpSync(env, args, &req_wrap_sync, *path, mode);
1564

536
      if (err == 0 &&
1565
266
          !req_wrap_sync.continuation_data()->first_path().empty()) {
1566
        Local<Value> error;
1567
264
        std::string first_path(req_wrap_sync.continuation_data()->first_path());
1568
264
        FromNamespacedPath(&first_path);
1569
        MaybeLocal<Value> path = StringBytes::Encode(env->isolate(),
1570
                                                     first_path.c_str(),
1571
264
                                                     UTF8, &error);
1572
264
        if (path.IsEmpty()) {
1573
          Local<Object> ctx = args[4].As<Object>();
1574
          ctx->Set(env->context(), env->error_string(), error).Check();
1575
          return;
1576
        }
1577
528
        args.GetReturnValue().Set(path.ToLocalChecked());
1578
      }
1579
    } else {
1580
1688
      SyncCall(env, args[4], &req_wrap_sync, "mkdir",
1581
               uv_fs_mkdir, *path, mode);
1582
    }
1583

1118
    FS_SYNC_TRACE_END(mkdir);
1584
  }
1585
}
1586
1587
42
static void RealPath(const FunctionCallbackInfo<Value>& args) {
1588
42
  Environment* env = Environment::GetCurrent(args);
1589
42
  Isolate* isolate = env->isolate();
1590
1591
42
  const int argc = args.Length();
1592
42
  CHECK_GE(argc, 3);
1593
1594
42
  BufferValue path(isolate, args[0]);
1595
42
  CHECK_NOT_NULL(*path);
1596
1597
42
  const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
1598
1599
42
  FSReqBase* req_wrap_async = GetReqWrap(args, 2);
1600
42
  if (req_wrap_async != nullptr) {  // realpath(path, encoding, req)
1601
22
    AsyncCall(env, req_wrap_async, args, "realpath", encoding, AfterStringPtr,
1602
              uv_fs_realpath, *path);
1603
  } else {  // realpath(path, encoding, undefined, ctx)
1604
20
    CHECK_EQ(argc, 4);
1605
20
    FSReqWrapSync req_wrap_sync;
1606

22
    FS_SYNC_TRACE_BEGIN(realpath);
1607
40
    int err = SyncCall(env, args[3], &req_wrap_sync, "realpath",
1608
                       uv_fs_realpath, *path);
1609

22
    FS_SYNC_TRACE_END(realpath);
1610
20
    if (err < 0) {
1611
2
      return;  // syscall failed, no need to continue, error info is in ctx
1612
    }
1613
1614
18
    const char* link_path = static_cast<const char*>(req_wrap_sync.req.ptr);
1615
1616
    Local<Value> error;
1617
    MaybeLocal<Value> rc = StringBytes::Encode(isolate,
1618
                                               link_path,
1619
                                               encoding,
1620
18
                                               &error);
1621
18
    if (rc.IsEmpty()) {
1622
      Local<Object> ctx = args[3].As<Object>();
1623
      ctx->Set(env->context(), env->error_string(), error).Check();
1624
      return;
1625
    }
1626
1627
36
    args.GetReturnValue().Set(rc.ToLocalChecked());
1628
  }
1629
}
1630
1631
14001
static void ReadDir(const FunctionCallbackInfo<Value>& args) {
1632
14001
  Environment* env = Environment::GetCurrent(args);
1633
14001
  Isolate* isolate = env->isolate();
1634
1635
14001
  const int argc = args.Length();
1636
14001
  CHECK_GE(argc, 3);
1637
1638
14001
  BufferValue path(isolate, args[0]);
1639
14001
  CHECK_NOT_NULL(*path);
1640
1641
14001
  const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
1642
1643
14001
  bool with_types = args[2]->IsTrue();
1644
1645
14001
  FSReqBase* req_wrap_async = GetReqWrap(args, 3);
1646
14001
  if (req_wrap_async != nullptr) {  // readdir(path, encoding, withTypes, req)
1647
283
    if (with_types) {
1648
131
      AsyncCall(env, req_wrap_async, args, "scandir", encoding,
1649
                AfterScanDirWithTypes, uv_fs_scandir, *path, 0 /*flags*/);
1650
    } else {
1651
152
      AsyncCall(env, req_wrap_async, args, "scandir", encoding,
1652
                AfterScanDir, uv_fs_scandir, *path, 0 /*flags*/);
1653
    }
1654
  } else {  // readdir(path, encoding, withTypes, undefined, ctx)
1655
13718
    CHECK_EQ(argc, 5);
1656
13718
    FSReqWrapSync req_wrap_sync;
1657

13720
    FS_SYNC_TRACE_BEGIN(readdir);
1658
27436
    int err = SyncCall(env, args[4], &req_wrap_sync, "scandir",
1659
                       uv_fs_scandir, *path, 0 /*flags*/);
1660

13720
    FS_SYNC_TRACE_END(readdir);
1661
13718
    if (err < 0) {
1662
133
      return;  // syscall failed, no need to continue, error info is in ctx
1663
    }
1664
1665
13585
    CHECK_GE(req_wrap_sync.req.result, 0);
1666
    int r;
1667
13585
    std::vector<Local<Value>> name_v;
1668
13585
    std::vector<Local<Value>> type_v;
1669
1670
    for (;;) {
1671
      uv_dirent_t ent;
1672
1673
707352
      r = uv_fs_scandir_next(&(req_wrap_sync.req), &ent);
1674
707352
      if (r == UV_EOF)
1675
13585
        break;
1676
693767
      if (r != 0) {
1677
        Local<Object> ctx = args[4].As<Object>();
1678
        ctx->Set(env->context(), env->errno_string(),
1679
                 Integer::New(isolate, r)).Check();
1680
        ctx->Set(env->context(), env->syscall_string(),
1681
                 OneByteString(isolate, "readdir")).Check();
1682
        return;
1683
      }
1684
1685
      Local<Value> error;
1686
      MaybeLocal<Value> filename = StringBytes::Encode(isolate,
1687
                                                       ent.name,
1688
                                                       encoding,
1689
693767
                                                       &error);
1690
1691
693767
      if (filename.IsEmpty()) {
1692
        Local<Object> ctx = args[4].As<Object>();
1693
        ctx->Set(env->context(), env->error_string(), error).Check();
1694
        return;
1695
      }
1696
1697
693767
      name_v.push_back(filename.ToLocalChecked());
1698
1699
693767
      if (with_types) {
1700
47959
        type_v.emplace_back(Integer::New(isolate, ent.type));
1701
      }
1702
693767
    }
1703
1704
1705
13585
    Local<Array> names = Array::New(isolate, name_v.data(), name_v.size());
1706
13585
    if (with_types) {
1707
      Local<Value> result[] = {
1708
        names,
1709
        Array::New(isolate, type_v.data(), type_v.size())
1710
386
      };
1711
386
      args.GetReturnValue().Set(Array::New(isolate, result, arraysize(result)));
1712
    } else {
1713
26784
      args.GetReturnValue().Set(names);
1714
    }
1715
  }
1716
}
1717
1718
52456
static void Open(const FunctionCallbackInfo<Value>& args) {
1719
52456
  Environment* env = Environment::GetCurrent(args);
1720
1721
52456
  const int argc = args.Length();
1722
52456
  CHECK_GE(argc, 3);
1723
1724
104912
  BufferValue path(env->isolate(), args[0]);
1725
52456
  CHECK_NOT_NULL(*path);
1726
1727
52456
  CHECK(args[1]->IsInt32());
1728
104912
  const int flags = args[1].As<Int32>()->Value();
1729
1730
52456
  CHECK(args[2]->IsInt32());
1731
104912
  const int mode = args[2].As<Int32>()->Value();
1732
1733
52456
  FSReqBase* req_wrap_async = GetReqWrap(args, 3);
1734
52456
  if (req_wrap_async != nullptr) {  // open(path, flags, mode, req)
1735
5659
    req_wrap_async->set_is_plain_open(true);
1736
5659
    AsyncCall(env, req_wrap_async, args, "open", UTF8, AfterInteger,
1737
              uv_fs_open, *path, flags, mode);
1738
  } else {  // open(path, flags, mode, undefined, ctx)
1739
46797
    CHECK_EQ(argc, 5);
1740
46797
    FSReqWrapSync req_wrap_sync;
1741

46859
    FS_SYNC_TRACE_BEGIN(open);
1742
93594
    int result = SyncCall(env, args[4], &req_wrap_sync, "open",
1743
                          uv_fs_open, *path, flags, mode);
1744

46859
    FS_SYNC_TRACE_END(open);
1745
46797
    if (result >= 0) env->AddUnmanagedFd(result);
1746
93594
    args.GetReturnValue().Set(result);
1747
  }
1748
52456
}
1749
1750
977
static void OpenFileHandle(const FunctionCallbackInfo<Value>& args) {
1751
977
  BindingData* binding_data = Environment::GetBindingData<BindingData>(args);
1752
977
  Environment* env = binding_data->env();
1753
977
  Isolate* isolate = env->isolate();
1754
1755
977
  const int argc = args.Length();
1756
977
  CHECK_GE(argc, 3);
1757
1758
977
  BufferValue path(isolate, args[0]);
1759
977
  CHECK_NOT_NULL(*path);
1760
1761
977
  CHECK(args[1]->IsInt32());
1762
1954
  const int flags = args[1].As<Int32>()->Value();
1763
1764
977
  CHECK(args[2]->IsInt32());
1765
1954
  const int mode = args[2].As<Int32>()->Value();
1766
1767
977
  FSReqBase* req_wrap_async = GetReqWrap(args, 3);
1768
977
  if (req_wrap_async != nullptr) {  // openFileHandle(path, flags, mode, req)
1769
976
    AsyncCall(env, req_wrap_async, args, "open", UTF8, AfterOpenFileHandle,
1770
              uv_fs_open, *path, flags, mode);
1771
  } else {  // openFileHandle(path, flags, mode, undefined, ctx)
1772
1
    CHECK_EQ(argc, 5);
1773
1
    FSReqWrapSync req_wrap_sync;
1774

1
    FS_SYNC_TRACE_BEGIN(open);
1775
2
    int result = SyncCall(env, args[4], &req_wrap_sync, "open",
1776
                          uv_fs_open, *path, flags, mode);
1777

1
    FS_SYNC_TRACE_END(open);
1778
1
    if (result < 0) {
1779
      return;  // syscall failed, no need to continue, error info is in ctx
1780
    }
1781
1
    FileHandle* fd = FileHandle::New(binding_data, result);
1782
1
    if (fd == nullptr) return;
1783
2
    args.GetReturnValue().Set(fd->object());
1784
  }
1785
}
1786
1787
212
static void CopyFile(const FunctionCallbackInfo<Value>& args) {
1788
212
  Environment* env = Environment::GetCurrent(args);
1789
212
  Isolate* isolate = env->isolate();
1790
1791
212
  const int argc = args.Length();
1792
212
  CHECK_GE(argc, 3);
1793
1794
424
  BufferValue src(isolate, args[0]);
1795
212
  CHECK_NOT_NULL(*src);
1796
1797
424
  BufferValue dest(isolate, args[1]);
1798
212
  CHECK_NOT_NULL(*dest);
1799
1800
212
  CHECK(args[2]->IsInt32());
1801
424
  const int flags = args[2].As<Int32>()->Value();
1802
1803
212
  FSReqBase* req_wrap_async = GetReqWrap(args, 3);
1804
212
  if (req_wrap_async != nullptr) {  // copyFile(src, dest, flags, req)
1805
204
    AsyncDestCall(env, req_wrap_async, args, "copyfile",
1806
102
                  *dest, dest.length(), UTF8, AfterNoArgs,
1807
                  uv_fs_copyfile, *src, *dest, flags);
1808
  } else {  // copyFile(src, dest, flags, undefined, ctx)
1809
110
    CHECK_EQ(argc, 5);
1810
220
    FSReqWrapSync req_wrap_sync;
1811

112
    FS_SYNC_TRACE_BEGIN(copyfile);
1812
220
    SyncCall(env, args[4], &req_wrap_sync, "copyfile",
1813
             uv_fs_copyfile, *src, *dest, flags);
1814

112
    FS_SYNC_TRACE_END(copyfile);
1815
  }
1816
212
}
1817
1818
1819
// Wrapper for write(2).
1820
//
1821
// bytesWritten = write(fd, buffer, offset, length, position, callback)
1822
// 0 fd        integer. file descriptor
1823
// 1 buffer    the data to write
1824
// 2 offset    where in the buffer to start from
1825
// 3 length    how much to write
1826
// 4 position  if integer, position to write at in the file.
1827
//             if null, write from the current position
1828
61617
static void WriteBuffer(const FunctionCallbackInfo<Value>& args) {
1829
61617
  Environment* env = Environment::GetCurrent(args);
1830
1831
61617
  const int argc = args.Length();
1832
61617
  CHECK_GE(argc, 4);
1833
1834
61617
  CHECK(args[0]->IsInt32());
1835
123234
  const int fd = args[0].As<Int32>()->Value();
1836
1837
61617
  CHECK(Buffer::HasInstance(args[1]));
1838
61617
  Local<Object> buffer_obj = args[1].As<Object>();
1839
61617
  char* buffer_data = Buffer::Data(buffer_obj);
1840
61617
  size_t buffer_length = Buffer::Length(buffer_obj);
1841
1842
61617
  CHECK(IsSafeJsInt(args[2]));
1843
123234
  const int64_t off_64 = args[2].As<Integer>()->Value();
1844
61617
  CHECK_GE(off_64, 0);
1845
61617
  CHECK_LE(static_cast<uint64_t>(off_64), buffer_length);
1846
61617
  const size_t off = static_cast<size_t>(off_64);
1847
1848
61617
  CHECK(args[3]->IsInt32());
1849
123234
  const size_t len = static_cast<size_t>(args[3].As<Int32>()->Value());
1850
61617
  CHECK(Buffer::IsWithinBounds(off, len, buffer_length));
1851
61617
  CHECK_LE(len, buffer_length);
1852
61617
  CHECK_GE(off + len, off);
1853
1854
61617
  const int64_t pos = GetOffset(args[4]);
1855
1856
61617
  char* buf = buffer_data + off;
1857
61617
  uv_buf_t uvbuf = uv_buf_init(buf, len);
1858
1859
61617
  FSReqBase* req_wrap_async = GetReqWrap(args, 5);
1860
61617
  if (req_wrap_async != nullptr) {  // write(fd, buffer, off, len, pos, req)
1861
17445
    AsyncCall(env, req_wrap_async, args, "write", UTF8, AfterInteger,
1862
              uv_fs_write, fd, &uvbuf, 1, pos);
1863
  } else {  // write(fd, buffer, off, len, pos, undefined, ctx)
1864
44172
    CHECK_EQ(argc, 7);
1865
44172
    FSReqWrapSync req_wrap_sync;
1866

44222
    FS_SYNC_TRACE_BEGIN(write);
1867
44172
    int bytesWritten = SyncCall(env, args[6], &req_wrap_sync, "write",
1868
44172
                                uv_fs_write, fd, &uvbuf, 1, pos);
1869

44222
    FS_SYNC_TRACE_END(write, "bytesWritten", bytesWritten);
1870
88344
    args.GetReturnValue().Set(bytesWritten);
1871
  }
1872
61617
}
1873
1874
1875
// Wrapper for writev(2).
1876
//
1877
// bytesWritten = writev(fd, chunks, position, callback)
1878
// 0 fd        integer. file descriptor
1879
// 1 chunks    array of buffers to write
1880
// 2 position  if integer, position to write at in the file.
1881
//             if null, write from the current position
1882
18
static void WriteBuffers(const FunctionCallbackInfo<Value>& args) {
1883
18
  Environment* env = Environment::GetCurrent(args);
1884
1885
18
  const int argc = args.Length();
1886
18
  CHECK_GE(argc, 3);
1887
1888
18
  CHECK(args[0]->IsInt32());
1889
36
  const int fd = args[0].As<Int32>()->Value();
1890
1891
18
  CHECK(args[1]->IsArray());
1892
36
  Local<Array> chunks = args[1].As<Array>();
1893
1894
18
  int64_t pos = GetOffset(args[2]);
1895
1896
36
  MaybeStackBuffer<uv_buf_t> iovs(chunks->Length());
1897
1898
71
  for (uint32_t i = 0; i < iovs.length(); i++) {
1899
53
    Local<Value> chunk = chunks->Get(env->context(), i).ToLocalChecked();
1900
53
    CHECK(Buffer::HasInstance(chunk));
1901
53
    iovs[i] = uv_buf_init(Buffer::Data(chunk), Buffer::Length(chunk));
1902
  }
1903
1904
18
  FSReqBase* req_wrap_async = GetReqWrap(args, 3);
1905
18
  if (req_wrap_async != nullptr) {  // writeBuffers(fd, chunks, pos, req)
1906
14
    AsyncCall(env, req_wrap_async, args, "write", UTF8, AfterInteger,
1907
              uv_fs_write, fd, *iovs, iovs.length(), pos);
1908
  } else {  // writeBuffers(fd, chunks, pos, undefined, ctx)
1909
4
    CHECK_EQ(argc, 5);
1910
4
    FSReqWrapSync req_wrap_sync;
1911

4
    FS_SYNC_TRACE_BEGIN(write);
1912
8
    int bytesWritten = SyncCall(env, args[4], &req_wrap_sync, "write",
1913
4
                                uv_fs_write, fd, *iovs, iovs.length(), pos);
1914

4
    FS_SYNC_TRACE_END(write, "bytesWritten", bytesWritten);
1915
8
    args.GetReturnValue().Set(bytesWritten);
1916
  }
1917
18
}
1918
1919
1920
// Wrapper for write(2).
1921
//
1922
// bytesWritten = write(fd, string, position, enc, callback)
1923
// 0 fd        integer. file descriptor
1924
// 1 string    non-buffer values are converted to strings
1925
// 2 position  if integer, position to write at in the file.
1926
//             if null, write from the current position
1927
// 3 enc       encoding of string
1928
120470
static void WriteString(const FunctionCallbackInfo<Value>& args) {
1929
120470
  Environment* env = Environment::GetCurrent(args);
1930
120470
  Isolate* isolate = env->isolate();
1931
1932
120470
  const int argc = args.Length();
1933
120470
  CHECK_GE(argc, 4);
1934
1935
120470
  CHECK(args[0]->IsInt32());
1936
240940
  const int fd = args[0].As<Int32>()->Value();
1937
1938
120470
  const int64_t pos = GetOffset(args[2]);
1939
1940
120470
  const auto enc = ParseEncoding(isolate, args[3], UTF8);
1941
1942
120470
  Local<Value> value = args[1];
1943
120470
  char* buf = nullptr;
1944
  size_t len;
1945
1946
120470
  FSReqBase* req_wrap_async = GetReqWrap(args, 4);
1947
120470
  const bool is_async = req_wrap_async != nullptr;
1948
1949
  // Avoid copying the string when it is externalized but only when:
1950
  // 1. The target encoding is compatible with the string's encoding, and
1951
  // 2. The write is synchronous, otherwise the string might get neutered
1952
  //    while the request is in flight, and
1953
  // 3. For UCS2, when the host system is little-endian.  Big-endian systems
1954
  //    need to call StringBytes::Write() to ensure proper byte swapping.
1955
  // The const_casts are conceptually sound: memory is read but not written.
1956

361254
  if (!is_async && value->IsString()) {
1957
120392
    auto string = value.As<String>();
1958


120393
    if ((enc == ASCII || enc == LATIN1) && string->IsExternalOneByte()) {
1959
1
      auto ext = string->GetExternalOneByteStringResource();
1960
1
      buf = const_cast<char*>(ext->data());
1961
1
      len = ext->length();
1962


120392
    } else if (enc == UCS2 && IsLittleEndian() && string->IsExternalTwoByte()) {
1963
1
      auto ext = string->GetExternalStringResource();
1964
1
      buf = reinterpret_cast<char*>(const_cast<uint16_t*>(ext->data()));
1965
1
      len = ext->length() * sizeof(*ext->data());
1966
    }
1967
  }
1968
1969
120470
  if (is_async) {  // write(fd, string, pos, enc, req)
1970
78
    CHECK_NOT_NULL(req_wrap_async);
1971
156
    if (!StringBytes::StorageSize(isolate, value, enc).To(&len)) return;
1972
    FSReqBase::FSReqBuffer& stack_buffer =
1973
78
        req_wrap_async->Init("write", len, enc);
1974
    // StorageSize may return too large a char, so correct the actual length
1975
    // by the write size
1976
78
    len = StringBytes::Write(isolate, *stack_buffer, len, args[1], enc);
1977
78
    stack_buffer.SetLengthAndZeroTerminate(len);
1978
78
    uv_buf_t uvbuf = uv_buf_init(*stack_buffer, len);
1979
78
    int err = req_wrap_async->Dispatch(uv_fs_write,
1980
                                       fd,
1981
                                       &uvbuf,
1982
                                       1,
1983
                                       pos,
1984
                                       AfterInteger);
1985
78
    if (err < 0) {
1986
      uv_fs_t* uv_req = req_wrap_async->req();
1987
      uv_req->result = err;
1988
      uv_req->path = nullptr;
1989
      AfterInteger(uv_req);  // after may delete req_wrap_async if there is
1990
                             // an error
1991
    } else {
1992
78
      req_wrap_async->SetReturnValue(args);
1993
    }
1994
  } else {  // write(fd, string, pos, enc, undefined, ctx)
1995
120392
    CHECK_EQ(argc, 6);
1996
120392
    FSReqWrapSync req_wrap_sync;
1997
120392
    FSReqBase::FSReqBuffer stack_buffer;
1998
120392
    if (buf == nullptr) {
1999
240780
      if (!StringBytes::StorageSize(isolate, value, enc).To(&len))
2000
        return;
2001
120390
      stack_buffer.AllocateSufficientStorage(len + 1);
2002
      // StorageSize may return too large a char, so correct the actual length
2003
      // by the write size
2004
120390
      len = StringBytes::Write(isolate, *stack_buffer,
2005
                               len, args[1], enc);
2006
120390
      stack_buffer.SetLengthAndZeroTerminate(len);
2007
120390
      buf = *stack_buffer;
2008
    }
2009
120392
    uv_buf_t uvbuf = uv_buf_init(buf, len);
2010

120392
    FS_SYNC_TRACE_BEGIN(write);
2011
120392
    int bytesWritten = SyncCall(env, args[5], &req_wrap_sync, "write",
2012
120392
                                uv_fs_write, fd, &uvbuf, 1, pos);
2013

120392
    FS_SYNC_TRACE_END(write, "bytesWritten", bytesWritten);
2014
240784
    args.GetReturnValue().Set(bytesWritten);
2015
  }
2016
}
2017
2018
2019
/*
2020
 * Wrapper for read(2).
2021
 *
2022
 * bytesRead = fs.read(fd, buffer, offset, length, position)
2023
 *
2024
 * 0 fd        int32. file descriptor
2025
 * 1 buffer    instance of Buffer
2026
 * 2 offset    int64. offset to start reading into inside buffer
2027
 * 3 length    int32. length to read
2028
 * 4 position  int64. file position - -1 for current position
2029
 */
2030
59023
static void Read(const FunctionCallbackInfo<Value>& args) {
2031
59023
  Environment* env = Environment::GetCurrent(args);
2032
2033
59023
  const int argc = args.Length();
2034
59023
  CHECK_GE(argc, 5);
2035
2036
59023
  CHECK(args[0]->IsInt32());
2037
118046
  const int fd = args[0].As<Int32>()->Value();
2038
2039
59023
  CHECK(Buffer::HasInstance(args[1]));
2040
59023
  Local<Object> buffer_obj = args[1].As<Object>();
2041
59023
  char* buffer_data = Buffer::Data(buffer_obj);
2042
59023
  size_t buffer_length = Buffer::Length(buffer_obj);
2043
2044
59023
  CHECK(IsSafeJsInt(args[2]));
2045
118046
  const int64_t off_64 = args[2].As<Integer>()->Value();
2046
59023
  CHECK_GE(off_64, 0);
2047
59023
  CHECK_LT(static_cast<uint64_t>(off_64), buffer_length);
2048
59023
  const size_t off = static_cast<size_t>(off_64);
2049
2050
59023
  CHECK(args[3]->IsInt32());
2051
118046
  const size_t len = static_cast<size_t>(args[3].As<Int32>()->Value());
2052
59023
  CHECK(Buffer::IsWithinBounds(off, len, buffer_length));
2053
2054

59027
  CHECK(IsSafeJsInt(args[4]) || args[4]->IsBigInt());
2055
59023
  const int64_t pos = args[4]->IsNumber() ?
2056
118038
                      args[4].As<Integer>()->Value() :
2057
8
                      args[4].As<BigInt>()->Int64Value();
2058
2059
59023
  char* buf = buffer_data + off;
2060
59023
  uv_buf_t uvbuf = uv_buf_init(buf, len);
2061
2062
59023
  FSReqBase* req_wrap_async = GetReqWrap(args, 5);
2063
59023
  if (req_wrap_async != nullptr) {  // read(fd, buffer, offset, len, pos, req)
2064
14352
    AsyncCall(env, req_wrap_async, args, "read", UTF8, AfterInteger,
2065
              uv_fs_read, fd, &uvbuf, 1, pos);
2066
  } else {  // read(fd, buffer, offset, len, pos, undefined, ctx)
2067
44671
    CHECK_EQ(argc, 7);
2068
44671
    FSReqWrapSync req_wrap_sync;
2069

44679
    FS_SYNC_TRACE_BEGIN(read);
2070
44671
    const int bytesRead = SyncCall(env, args[6], &req_wrap_sync, "read",
2071
44671
                                   uv_fs_read, fd, &uvbuf, 1, pos);
2072

44679
    FS_SYNC_TRACE_END(read, "bytesRead", bytesRead);
2073
89342
    args.GetReturnValue().Set(bytesRead);
2074
  }
2075
59023
}
2076
2077
2078
// Wrapper for readv(2).
2079
//
2080
// bytesRead = fs.readv(fd, buffers[, position], callback)
2081
// 0 fd        integer. file descriptor
2082
// 1 buffers   array of buffers to read
2083
// 2 position  if integer, position to read at in the file.
2084
//             if null, read from the current position
2085
11
static void ReadBuffers(const FunctionCallbackInfo<Value>& args) {
2086
11
  Environment* env = Environment::GetCurrent(args);
2087
2088
11
  const int argc = args.Length();
2089
11
  CHECK_GE(argc, 3);
2090
2091
11
  CHECK(args[0]->IsInt32());
2092
22
  const int fd = args[0].As<Int32>()->Value();
2093
2094
11
  CHECK(args[1]->IsArray());
2095
22
  Local<Array> buffers = args[1].As<Array>();
2096
2097
11
  int64_t pos = GetOffset(args[2]);  // -1 if not a valid JS int
2098
2099
22
  MaybeStackBuffer<uv_buf_t> iovs(buffers->Length());
2100
2101
  // Init uv buffers from ArrayBufferViews
2102
28
  for (uint32_t i = 0; i < iovs.length(); i++) {
2103
17
    Local<Value> buffer = buffers->Get(env->context(), i).ToLocalChecked();
2104
17
    CHECK(Buffer::HasInstance(buffer));
2105
17
    iovs[i] = uv_buf_init(Buffer::Data(buffer), Buffer::Length(buffer));
2106
  }
2107
2108
11
  FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2109
11
  if (req_wrap_async != nullptr) {  // readBuffers(fd, buffers, pos, req)
2110
7
    AsyncCall(env, req_wrap_async, args, "read", UTF8, AfterInteger,
2111
              uv_fs_read, fd, *iovs, iovs.length(), pos);
2112
  } else {  // readBuffers(fd, buffers, undefined, ctx)
2113
4
    CHECK_EQ(argc, 5);
2114
4
    FSReqWrapSync req_wrap_sync;
2115

4
    FS_SYNC_TRACE_BEGIN(read);
2116
8
    int bytesRead = SyncCall(env, /* ctx */ args[4], &req_wrap_sync, "read",
2117
4
                             uv_fs_read, fd, *iovs, iovs.length(), pos);
2118

4
    FS_SYNC_TRACE_END(read, "bytesRead", bytesRead);
2119
8
    args.GetReturnValue().Set(bytesRead);
2120
  }
2121
11
}
2122
2123
2124
/* fs.chmod(path, mode);
2125
 * Wrapper for chmod(1) / EIO_CHMOD
2126
 */
2127
305
static void Chmod(const FunctionCallbackInfo<Value>& args) {
2128
305
  Environment* env = Environment::GetCurrent(args);
2129
2130
305
  const int argc = args.Length();
2131
305
  CHECK_GE(argc, 2);
2132
2133
610
  BufferValue path(env->isolate(), args[0]);
2134
305
  CHECK_NOT_NULL(*path);
2135
2136
305
  CHECK(args[1]->IsInt32());
2137
610
  int mode = args[1].As<Int32>()->Value();
2138
2139
305
  FSReqBase* req_wrap_async = GetReqWrap(args, 2);
2140
305
  if (req_wrap_async != nullptr) {  // chmod(path, mode, req)
2141
141
    AsyncCall(env, req_wrap_async, args, "chmod", UTF8, AfterNoArgs,
2142
              uv_fs_chmod, *path, mode);
2143
  } else {  // chmod(path, mode, undefined, ctx)
2144
164
    CHECK_EQ(argc, 4);
2145
328
    FSReqWrapSync req_wrap_sync;
2146

166
    FS_SYNC_TRACE_BEGIN(chmod);
2147
328
    SyncCall(env, args[3], &req_wrap_sync, "chmod",
2148
             uv_fs_chmod, *path, mode);
2149

166
    FS_SYNC_TRACE_END(chmod);
2150
  }
2151
305
}
2152
2153
2154
/* fs.fchmod(fd, mode);
2155
 * Wrapper for fchmod(1) / EIO_FCHMOD
2156
 */
2157
12
static void FChmod(const FunctionCallbackInfo<Value>& args) {
2158
12
  Environment* env = Environment::GetCurrent(args);
2159
2160
12
  const int argc = args.Length();
2161
12
  CHECK_GE(argc, 2);
2162
2163
12
  CHECK(args[0]->IsInt32());
2164
24
  const int fd = args[0].As<Int32>()->Value();
2165
2166
12
  CHECK(args[1]->IsInt32());
2167
24
  const int mode = args[1].As<Int32>()->Value();
2168
2169
12
  FSReqBase* req_wrap_async = GetReqWrap(args, 2);
2170
12
  if (req_wrap_async != nullptr) {  // fchmod(fd, mode, req)
2171
7
    AsyncCall(env, req_wrap_async, args, "fchmod", UTF8, AfterNoArgs,
2172
              uv_fs_fchmod, fd, mode);
2173
  } else {  // fchmod(fd, mode, undefined, ctx)
2174
5
    CHECK_EQ(argc, 4);
2175
10
    FSReqWrapSync req_wrap_sync;
2176

7
    FS_SYNC_TRACE_BEGIN(fchmod);
2177
5
    SyncCall(env, args[3], &req_wrap_sync, "fchmod",
2178
             uv_fs_fchmod, fd, mode);
2179

7
    FS_SYNC_TRACE_END(fchmod);
2180
  }
2181
12
}
2182
2183
2184
/* fs.chown(path, uid, gid);
2185
 * Wrapper for chown(1) / EIO_CHOWN
2186
 */
2187
4
static void Chown(const FunctionCallbackInfo<Value>& args) {
2188
4
  Environment* env = Environment::GetCurrent(args);
2189
2190
4
  const int argc = args.Length();
2191
4
  CHECK_GE(argc, 3);
2192
2193
8
  BufferValue path(env->isolate(), args[0]);
2194
4
  CHECK_NOT_NULL(*path);
2195
2196
4
  CHECK(IsSafeJsInt(args[1]));
2197
8
  const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Integer>()->Value());
2198
2199
4
  CHECK(IsSafeJsInt(args[2]));
2200
8
  const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Integer>()->Value());
2201
2202
4
  FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2203
4
  if (req_wrap_async != nullptr) {  // chown(path, uid, gid, req)
2204
2
    AsyncCall(env, req_wrap_async, args, "chown", UTF8, AfterNoArgs,
2205
              uv_fs_chown, *path, uid, gid);
2206
  } else {  // chown(path, uid, gid, undefined, ctx)
2207
2
    CHECK_EQ(argc, 5);
2208
4
    FSReqWrapSync req_wrap_sync;
2209

4
    FS_SYNC_TRACE_BEGIN(chown);
2210
4
    SyncCall(env, args[4], &req_wrap_sync, "chown",
2211
             uv_fs_chown, *path, uid, gid);
2212

4
    FS_SYNC_TRACE_END(chown);
2213
  }
2214
4
}
2215
2216
2217
/* fs.fchown(fd, uid, gid);
2218
 * Wrapper for fchown(1) / EIO_FCHOWN
2219
 */
2220
4
static void FChown(const FunctionCallbackInfo<Value>& args) {
2221
4
  Environment* env = Environment::GetCurrent(args);
2222
2223
4
  const int argc = args.Length();
2224
4
  CHECK_GE(argc, 3);
2225
2226
4
  CHECK(args[0]->IsInt32());
2227
8
  const int fd = args[0].As<Int32>()->Value();
2228
2229
4
  CHECK(IsSafeJsInt(args[1]));
2230
8
  const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Integer>()->Value());
2231
2232
4
  CHECK(IsSafeJsInt(args[2]));
2233
8
  const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Integer>()->Value());
2234
2235
4
  FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2236
4
  if (req_wrap_async != nullptr) {  // fchown(fd, uid, gid, req)
2237
2
    AsyncCall(env, req_wrap_async, args, "fchown", UTF8, AfterNoArgs,
2238
              uv_fs_fchown, fd, uid, gid);
2239
  } else {  // fchown(fd, uid, gid, undefined, ctx)
2240
2
    CHECK_EQ(argc, 5);
2241
4
    FSReqWrapSync req_wrap_sync;
2242

4
    FS_SYNC_TRACE_BEGIN(fchown);
2243
2
    SyncCall(env, args[4], &req_wrap_sync, "fchown",
2244
             uv_fs_fchown, fd, uid, gid);
2245

4
    FS_SYNC_TRACE_END(fchown);
2246
  }
2247
4
}
2248
2249
2250
5
static void LChown(const FunctionCallbackInfo<Value>& args) {
2251
5
  Environment* env = Environment::GetCurrent(args);
2252
2253
5
  const int argc = args.Length();
2254
5
  CHECK_GE(argc, 3);
2255
2256
10
  BufferValue path(env->isolate(), args[0]);
2257
5
  CHECK_NOT_NULL(*path);
2258
2259
5
  CHECK(IsSafeJsInt(args[1]));
2260
10
  const uv_uid_t uid = static_cast<uv_uid_t>(args[1].As<Integer>()->Value());
2261
2262
5
  CHECK(IsSafeJsInt(args[2]));
2263
10
  const uv_gid_t gid = static_cast<uv_gid_t>(args[2].As<Integer>()->Value());
2264
2265
5
  FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2266
5
  if (req_wrap_async != nullptr) {  // lchown(path, uid, gid, req)
2267
3
    AsyncCall(env, req_wrap_async, args, "lchown", UTF8, AfterNoArgs,
2268
              uv_fs_lchown, *path, uid, gid);
2269
  } else {  // lchown(path, uid, gid, undefined, ctx)
2270
2
    CHECK_EQ(argc, 5);
2271
4
    FSReqWrapSync req_wrap_sync;
2272

4
    FS_SYNC_TRACE_BEGIN(lchown);
2273
4
    SyncCall(env, args[4], &req_wrap_sync, "lchown",
2274
             uv_fs_lchown, *path, uid, gid);
2275

4
    FS_SYNC_TRACE_END(lchown);
2276
  }
2277
5
}
2278
2279
2280
53
static void UTimes(const FunctionCallbackInfo<Value>& args) {
2281
53
  Environment* env = Environment::GetCurrent(args);
2282
2283
53
  const int argc = args.Length();
2284
53
  CHECK_GE(argc, 3);
2285
2286
106
  BufferValue path(env->isolate(), args[0]);
2287
53
  CHECK_NOT_NULL(*path);
2288
2289
53
  CHECK(args[1]->IsNumber());
2290
106
  const double atime = args[1].As<Number>()->Value();
2291
2292
53
  CHECK(args[2]->IsNumber());
2293
106
  const double mtime = args[2].As<Number>()->Value();
2294
2295
53
  FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2296
53
  if (req_wrap_async != nullptr) {  // utimes(path, atime, mtime, req)
2297
26
    AsyncCall(env, req_wrap_async, args, "utime", UTF8, AfterNoArgs,
2298
              uv_fs_utime, *path, atime, mtime);
2299
  } else {  // utimes(path, atime, mtime, undefined, ctx)
2300
27
    CHECK_EQ(argc, 5);
2301
54
    FSReqWrapSync req_wrap_sync;
2302

29
    FS_SYNC_TRACE_BEGIN(utimes);
2303
54
    SyncCall(env, args[4], &req_wrap_sync, "utime",
2304
             uv_fs_utime, *path, atime, mtime);
2305

29
    FS_SYNC_TRACE_END(utimes);
2306
  }
2307
53
}
2308
2309
18
static void FUTimes(const FunctionCallbackInfo<Value>& args) {
2310
18
  Environment* env = Environment::GetCurrent(args);
2311
2312
18
  const int argc = args.Length();
2313
18
  CHECK_GE(argc, 3);
2314
2315
18
  CHECK(args[0]->IsInt32());
2316
36
  const int fd = args[0].As<Int32>()->Value();
2317
2318
18
  CHECK(args[1]->IsNumber());
2319
36
  const double atime = args[1].As<Number>()->Value();
2320
2321
18
  CHECK(args[2]->IsNumber());
2322
36
  const double mtime = args[2].As<Number>()->Value();
2323
2324
18
  FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2325
18
  if (req_wrap_async != nullptr) {  // futimes(fd, atime, mtime, req)
2326
9
    AsyncCall(env, req_wrap_async, args, "futime", UTF8, AfterNoArgs,
2327
              uv_fs_futime, fd, atime, mtime);
2328
  } else {  // futimes(fd, atime, mtime, undefined, ctx)
2329
9
    CHECK_EQ(argc, 5);
2330
18
    FSReqWrapSync req_wrap_sync;
2331

11
    FS_SYNC_TRACE_BEGIN(futimes);
2332
9
    SyncCall(env, args[4], &req_wrap_sync, "futime",
2333
             uv_fs_futime, fd, atime, mtime);
2334

11
    FS_SYNC_TRACE_END(futimes);
2335
  }
2336
18
}
2337
2338
15
static void LUTimes(const FunctionCallbackInfo<Value>& args) {
2339
15
  Environment* env = Environment::GetCurrent(args);
2340
2341
15
  const int argc = args.Length();
2342
15
  CHECK_GE(argc, 3);
2343
2344
30
  BufferValue path(env->isolate(), args[0]);
2345
15
  CHECK_NOT_NULL(*path);
2346
2347
15
  CHECK(args[1]->IsNumber());
2348
30
  const double atime = args[1].As<Number>()->Value();
2349
2350
15
  CHECK(args[2]->IsNumber());
2351
30
  const double mtime = args[2].As<Number>()->Value();
2352
2353
15
  FSReqBase* req_wrap_async = GetReqWrap(args, 3);
2354
15
  if (req_wrap_async != nullptr) {  // lutimes(path, atime, mtime, req)
2355
8
    AsyncCall(env, req_wrap_async, args, "lutime", UTF8, AfterNoArgs,
2356
              uv_fs_lutime, *path, atime, mtime);
2357
  } else {  // lutimes(path, atime, mtime, undefined, ctx)
2358
7
    CHECK_EQ(argc, 5);
2359
14
    FSReqWrapSync req_wrap_sync;
2360

7
    FS_SYNC_TRACE_BEGIN(lutimes);
2361
14
    SyncCall(env, args[4], &req_wrap_sync, "lutime",
2362
             uv_fs_lutime, *path, atime, mtime);
2363

7
    FS_SYNC_TRACE_END(lutimes);
2364
  }
2365
15
}
2366
2367
15
static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
2368
15
  Environment* env = Environment::GetCurrent(args);
2369
15
  Isolate* isolate = env->isolate();
2370
2371
15
  const int argc = args.Length();
2372
15
  CHECK_GE(argc, 2);
2373
2374
15
  BufferValue tmpl(isolate, args[0]);
2375
15
  CHECK_NOT_NULL(*tmpl);
2376
2377
15
  const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
2378
2379
15
  FSReqBase* req_wrap_async = GetReqWrap(args, 2);
2380
15
  if (req_wrap_async != nullptr) {  // mkdtemp(tmpl, encoding, req)
2381
6
    AsyncCall(env, req_wrap_async, args, "mkdtemp", encoding, AfterStringPath,
2382
              uv_fs_mkdtemp, *tmpl);
2383
  } else {  // mkdtemp(tmpl, encoding, undefined, ctx)
2384
9
    CHECK_EQ(argc, 4);
2385
9
    FSReqWrapSync req_wrap_sync;
2386

11
    FS_SYNC_TRACE_BEGIN(mkdtemp);
2387
18
    SyncCall(env, args[3], &req_wrap_sync, "mkdtemp",
2388
             uv_fs_mkdtemp, *tmpl);
2389

11
    FS_SYNC_TRACE_END(mkdtemp);
2390
9
    const char* path = req_wrap_sync.req.path;
2391
2392
    Local<Value> error;
2393
    MaybeLocal<Value> rc =
2394
9
        StringBytes::Encode(isolate, path, encoding, &error);
2395
9
    if (rc.IsEmpty()) {
2396
      Local<Object> ctx = args[3].As<Object>();
2397
      ctx->Set(env->context(), env->error_string(), error).Check();
2398
      return;
2399
    }
2400
18
    args.GetReturnValue().Set(rc.ToLocalChecked());
2401
  }
2402
}
2403
2404
22
void BindingData::MemoryInfo(MemoryTracker* tracker) const {
2405
22
  tracker->TrackField("stats_field_array", stats_field_array);
2406
22
  tracker->TrackField("stats_field_bigint_array", stats_field_bigint_array);
2407
22
  tracker->TrackField("file_handle_read_wrap_freelist",
2408
22
                      file_handle_read_wrap_freelist);
2409
22
}
2410
2411
5429
BindingData::BindingData(Environment* env, v8::Local<v8::Object> wrap)
2412
    : SnapshotableObject(env, wrap, type_int),
2413
      stats_field_array(env->isolate(), kFsStatsBufferLength),
2414
5429
      stats_field_bigint_array(env->isolate(), kFsStatsBufferLength) {
2415
5429
  wrap->Set(env->context(),
2416
            FIXED_ONE_BYTE_STRING(env->isolate(), "statValues"),
2417
21716
            stats_field_array.GetJSArray())
2418
      .Check();
2419
2420
5429
  wrap->Set(env->context(),
2421
            FIXED_ONE_BYTE_STRING(env->isolate(), "bigintStatValues"),
2422
16287
            stats_field_bigint_array.GetJSArray())
2423
      .Check();
2424
5429
}
2425
2426
4807
void BindingData::Deserialize(Local<Context> context,
2427
                              Local<Object> holder,
2428
                              int index,
2429
                              InternalFieldInfo* info) {
2430
  DCHECK_EQ(index, BaseObject::kSlot);
2431
9614
  HandleScope scope(context->GetIsolate());
2432
4807
  Environment* env = Environment::GetCurrent(context);
2433
4807
  BindingData* binding = env->AddBindingData<BindingData>(context, holder);
2434
4807
  CHECK_NOT_NULL(binding);
2435
4807
}
2436
2437
6
void BindingData::PrepareForSerialization(Local<Context> context,
2438
                                          v8::SnapshotCreator* creator) {
2439
6
  CHECK(file_handle_read_wrap_freelist.empty());
2440
  // We'll just re-initialize the buffers in the constructor since their
2441
  // contents can be thrown away once consumed in the previous call.
2442
6
  stats_field_array.Release();
2443
6
  stats_field_bigint_array.Release();
2444
6
}
2445
2446
6
InternalFieldInfo* BindingData::Serialize(int index) {
2447
  DCHECK_EQ(index, BaseObject::kSlot);
2448
6
  InternalFieldInfo* info = InternalFieldInfo::New(type());
2449
6
  return info;
2450
}
2451
2452
// TODO(addaleax): Remove once we're on C++17.
2453
constexpr FastStringKey BindingData::type_name;
2454
2455
622
void Initialize(Local<Object> target,
2456
                Local<Value> unused,
2457
                Local<Context> context,
2458
                void* priv) {
2459
622
  Environment* env = Environment::GetCurrent(context);
2460
622
  Isolate* isolate = env->isolate();
2461
  BindingData* const binding_data =
2462
622
      env->AddBindingData<BindingData>(context, target);
2463
622
  if (binding_data == nullptr) return;
2464
2465
622
  env->SetMethod(target, "access", Access);
2466
622
  env->SetMethod(target, "close", Close);
2467
622
  env->SetMethod(target, "open", Open);
2468
622
  env->SetMethod(target, "openFileHandle", OpenFileHandle);
2469
622
  env->SetMethod(target, "read", Read);
2470
622
  env->SetMethod(target, "readBuffers", ReadBuffers);
2471
622
  env->SetMethod(target, "fdatasync", Fdatasync);
2472
622
  env->SetMethod(target, "fsync", Fsync);
2473
622
  env->SetMethod(target, "rename", Rename);
2474
622
  env->SetMethod(target, "ftruncate", FTruncate);
2475
622
  env->SetMethod(target, "rmdir", RMDir);
2476
622
  env->SetMethod(target, "mkdir", MKDir);
2477
622
  env->SetMethod(target, "readdir", ReadDir);
2478
622
  env->SetMethod(target, "internalModuleReadJSON", InternalModuleReadJSON);
2479
622
  env->SetMethod(target, "internalModuleStat", InternalModuleStat);
2480
622
  env->SetMethod(target, "stat", Stat);
2481
622
  env->SetMethod(target, "lstat", LStat);
2482
622
  env->SetMethod(target, "fstat", FStat);
2483
622
  env->SetMethod(target, "link", Link);
2484
622
  env->SetMethod(target, "symlink", Symlink);
2485
622
  env->SetMethod(target, "readlink", ReadLink);
2486
622
  env->SetMethod(target, "unlink", Unlink);
2487
622
  env->SetMethod(target, "writeBuffer", WriteBuffer);
2488
622
  env->SetMethod(target, "writeBuffers", WriteBuffers);
2489
622
  env->SetMethod(target, "writeString", WriteString);
2490
622
  env->SetMethod(target, "realpath", RealPath);
2491
622
  env->SetMethod(target, "copyFile", CopyFile);
2492
2493
622
  env->SetMethod(target, "chmod", Chmod);
2494
622
  env->SetMethod(target, "fchmod", FChmod);
2495
2496
622
  env->SetMethod(target, "chown", Chown);
2497
622
  env->SetMethod(target, "fchown", FChown);
2498
622
  env->SetMethod(target, "lchown", LChown);
2499
2500
622
  env->SetMethod(target, "utimes", UTimes);
2501
622
  env->SetMethod(target, "futimes", FUTimes);
2502
622
  env->SetMethod(target, "lutimes", LUTimes);
2503
2504
622
  env->SetMethod(target, "mkdtemp", Mkdtemp);
2505
2506
  target
2507
622
      ->Set(context,
2508
            FIXED_ONE_BYTE_STRING(isolate, "kFsStatsFieldsNumber"),
2509
            Integer::New(
2510
                isolate,
2511
1866
                static_cast<int32_t>(FsStatsOffset::kFsStatsFieldsNumber)))
2512
      .Check();
2513
2514
622
  StatWatcher::Initialize(env, target);
2515
2516
  // Create FunctionTemplate for FSReqCallback
2517
622
  Local<FunctionTemplate> fst = env->NewFunctionTemplate(NewFSReqCallback);
2518
1244
  fst->InstanceTemplate()->SetInternalFieldCount(
2519
      FSReqBase::kInternalFieldCount);
2520
622
  fst->Inherit(AsyncWrap::GetConstructorTemplate(env));
2521
622
  env->SetConstructorFunction(target, "FSReqCallback", fst);
2522
2523
  // Create FunctionTemplate for FileHandleReadWrap. There’s no need
2524
  // to do anything in the constructor, so we only store the instance template.
2525
622
  Local<FunctionTemplate> fh_rw = FunctionTemplate::New(isolate);
2526
1244
  fh_rw->InstanceTemplate()->SetInternalFieldCount(
2527
      FSReqBase::kInternalFieldCount);
2528
622
  fh_rw->Inherit(AsyncWrap::GetConstructorTemplate(env));
2529
  Local<String> fhWrapString =
2530
622
      FIXED_ONE_BYTE_STRING(isolate, "FileHandleReqWrap");
2531
622
  fh_rw->SetClassName(fhWrapString);
2532
622
  env->set_filehandlereadwrap_template(
2533
      fst->InstanceTemplate());
2534
2535
  // Create Function Template for FSReqPromise
2536
622
  Local<FunctionTemplate> fpt = FunctionTemplate::New(isolate);
2537
622
  fpt->Inherit(AsyncWrap::GetConstructorTemplate(env));
2538
  Local<String> promiseString =
2539
622
      FIXED_ONE_BYTE_STRING(isolate, "FSReqPromise");
2540
622
  fpt->SetClassName(promiseString);
2541
622
  Local<ObjectTemplate> fpo = fpt->InstanceTemplate();
2542
622
  fpo->SetInternalFieldCount(FSReqBase::kInternalFieldCount);
2543
622
  env->set_fsreqpromise_constructor_template(fpo);
2544
2545
  // Create FunctionTemplate for FileHandle
2546
622
  Local<FunctionTemplate> fd = env->NewFunctionTemplate(FileHandle::New);
2547
622
  fd->Inherit(AsyncWrap::GetConstructorTemplate(env));
2548
622
  env->SetProtoMethod(fd, "close", FileHandle::Close);
2549
622
  env->SetProtoMethod(fd, "releaseFD", FileHandle::ReleaseFD);
2550
622
  Local<ObjectTemplate> fdt = fd->InstanceTemplate();
2551
622
  fdt->SetInternalFieldCount(FileHandle::kInternalFieldCount);
2552
622
  StreamBase::AddMethods(env, fd);
2553
622
  env->SetConstructorFunction(target, "FileHandle", fd);
2554
622
  env->set_fd_constructor_template(fdt);
2555
2556
  // Create FunctionTemplate for FileHandle::CloseReq
2557
622
  Local<FunctionTemplate> fdclose = FunctionTemplate::New(isolate);
2558
622
  fdclose->SetClassName(FIXED_ONE_BYTE_STRING(isolate,
2559
                        "FileHandleCloseReq"));
2560
622
  fdclose->Inherit(AsyncWrap::GetConstructorTemplate(env));
2561
622
  Local<ObjectTemplate> fdcloset = fdclose->InstanceTemplate();
2562
622
  fdcloset->SetInternalFieldCount(FSReqBase::kInternalFieldCount);
2563
622
  env->set_fdclose_constructor_template(fdcloset);
2564
2565
  Local<Symbol> use_promises_symbol =
2566
    Symbol::New(isolate,
2567
622
                FIXED_ONE_BYTE_STRING(isolate, "use promises"));
2568
622
  env->set_fs_use_promises_symbol(use_promises_symbol);
2569
622
  target->Set(context,
2570
              FIXED_ONE_BYTE_STRING(isolate, "kUsePromises"),
2571
1866
              use_promises_symbol).Check();
2572
}
2573
2574
3374
BindingData* FSReqBase::binding_data() {
2575
3374
  return binding_data_.get();
2576
}
2577
2578
4813
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
2579
4813
  registry->Register(Access);
2580
4813
  StatWatcher::RegisterExternalReferences(registry);
2581
2582
4813
  registry->Register(Close);
2583
4813
  registry->Register(Open);
2584
4813
  registry->Register(OpenFileHandle);
2585
4813
  registry->Register(Read);
2586
4813
  registry->Register(ReadBuffers);
2587
4813
  registry->Register(Fdatasync);
2588
4813
  registry->Register(Fsync);
2589
4813
  registry->Register(Rename);
2590
4813
  registry->Register(FTruncate);
2591
4813
  registry->Register(RMDir);
2592
4813
  registry->Register(MKDir);
2593
4813
  registry->Register(ReadDir);
2594
4813
  registry->Register(InternalModuleReadJSON);
2595
4813
  registry->Register(InternalModuleStat);
2596
4813
  registry->Register(Stat);
2597
4813
  registry->Register(LStat);
2598
4813
  registry->Register(FStat);
2599
4813
  registry->Register(Link);
2600
4813
  registry->Register(Symlink);
2601
4813
  registry->Register(ReadLink);
2602
4813
  registry->Register(Unlink);
2603
4813
  registry->Register(WriteBuffer);
2604
4813
  registry->Register(WriteBuffers);
2605
4813
  registry->Register(WriteString);
2606
4813
  registry->Register(RealPath);
2607
4813
  registry->Register(CopyFile);
2608
2609
4813
  registry->Register(Chmod);
2610
4813
  registry->Register(FChmod);
2611
2612
4813
  registry->Register(Chown);
2613
4813
  registry->Register(FChown);
2614
4813
  registry->Register(LChown);
2615
2616
4813
  registry->Register(UTimes);
2617
4813
  registry->Register(FUTimes);
2618
4813
  registry->Register(LUTimes);
2619
2620
4813
  registry->Register(Mkdtemp);
2621
4813
  registry->Register(NewFSReqCallback);
2622
2623
4813
  registry->Register(FileHandle::New);
2624
4813
  registry->Register(FileHandle::Close);
2625
4813
  registry->Register(FileHandle::ReleaseFD);
2626
4813
  StreamBase::RegisterExternalReferences(registry);
2627
4813
}
2628
2629
}  // namespace fs
2630
2631
}  // end namespace node
2632
2633
4870
NODE_MODULE_CONTEXT_AWARE_INTERNAL(fs, node::fs::Initialize)
2634
4813
NODE_MODULE_EXTERNAL_REFERENCE(fs, node::fs::RegisterExternalReferences)