GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_file.cc Lines: 617 733 84.2 %
Date: 2017-06-14 Branches: 420 665 63.2 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#include "node.h"
23
#include "node_buffer.h"
24
#include "node_internals.h"
25
#include "node_stat_watcher.h"
26
27
#include "env.h"
28
#include "env-inl.h"
29
#include "req-wrap.h"
30
#include "req-wrap-inl.h"
31
#include "string_bytes.h"
32
#include "util.h"
33
34
#include <fcntl.h>
35
#include <sys/types.h>
36
#include <sys/stat.h>
37
#include <string.h>
38
#include <errno.h>
39
#include <limits.h>
40
41
#if defined(__MINGW32__) || defined(_MSC_VER)
42
# include <io.h>
43
#endif
44
45
#include <vector>
46
47
namespace node {
48
namespace {
49
50
using v8::Array;
51
using v8::ArrayBuffer;
52
using v8::Context;
53
using v8::Float64Array;
54
using v8::Function;
55
using v8::FunctionCallbackInfo;
56
using v8::FunctionTemplate;
57
using v8::HandleScope;
58
using v8::Integer;
59
using v8::Local;
60
using v8::MaybeLocal;
61
using v8::Number;
62
using v8::Object;
63
using v8::String;
64
using v8::Value;
65
66
#ifndef MIN
67
# define MIN(a, b) ((a) < (b) ? (a) : (b))
68
#endif
69
70
#define TYPE_ERROR(msg) env->ThrowTypeError(msg)
71
72
#define GET_OFFSET(a) ((a)->IsNumber() ? (a)->IntegerValue() : -1)
73
74
class FSReqWrap: public ReqWrap<uv_fs_t> {
75
 public:
76
  enum Ownership { COPY, MOVE };
77
78
  inline static FSReqWrap* New(Environment* env,
79
                               Local<Object> req,
80
                               const char* syscall,
81
                               const char* data = nullptr,
82
                               enum encoding encoding = UTF8,
83
                               Ownership ownership = COPY);
84
85
  inline void Dispose();
86
87
26656
  void ReleaseEarly() {
88
26656
    if (data_ != inline_data()) {
89
26632
      delete[] data_;
90
26632
      data_ = nullptr;
91
    }
92
26656
  }
93
94
542
  const char* syscall() const { return syscall_; }
95
542
  const char* data() const { return data_; }
96
  const enum encoding encoding_;
97
98
  size_t self_size() const override { return sizeof(*this); }
99
100
 private:
101
13332
  FSReqWrap(Environment* env,
102
            Local<Object> req,
103
            const char* syscall,
104
            const char* data,
105
            enum encoding encoding)
106
      : ReqWrap(env, req, AsyncWrap::PROVIDER_FSREQWRAP),
107
        encoding_(encoding),
108
        syscall_(syscall),
109
13332
        data_(data) {
110
13332
    Wrap(object(), this);
111
13332
  }
112
113
26652
  ~FSReqWrap() {
114
13326
    ReleaseEarly();
115
13326
    ClearWrap(object());
116
13326
  }
117
118
  void* operator new(size_t size) = delete;
119
13332
  void* operator new(size_t size, char* storage) { return storage; }
120
26668
  char* inline_data() { return reinterpret_cast<char*>(this + 1); }
121
122
  const char* syscall_;
123
  const char* data_;
124
125
  DISALLOW_COPY_AND_ASSIGN(FSReqWrap);
126
};
127
128
#define ASSERT_PATH(path)                                                   \
129
  if (*path == nullptr)                                                     \
130
    return TYPE_ERROR( #path " must be a string or Buffer");
131
132
13332
FSReqWrap* FSReqWrap::New(Environment* env,
133
                          Local<Object> req,
134
                          const char* syscall,
135
                          const char* data,
136
                          enum encoding encoding,
137
                          Ownership ownership) {
138

13332
  const bool copy = (data != nullptr && ownership == COPY);
139
13332
  const size_t size = copy ? 1 + strlen(data) : 0;
140
  FSReqWrap* that;
141
13332
  char* const storage = new char[sizeof(*that) + size];
142
13332
  that = new(storage) FSReqWrap(env, req, syscall, data, encoding);
143
13332
  if (copy)
144
12
    that->data_ = static_cast<char*>(memcpy(that->inline_data(), data, size));
145
13332
  return that;
146
}
147
148
149
13326
void FSReqWrap::Dispose() {
150
13326
  this->~FSReqWrap();
151
13326
  delete[] reinterpret_cast<char*>(this);
152
13326
}
153
154
155
13342
void NewFSReqWrap(const FunctionCallbackInfo<Value>& args) {
156
13342
  CHECK(args.IsConstructCall());
157
13342
  ClearWrap(args.This());
158
13342
}
159
160
161
34
inline bool IsInt64(double x) {
162
34
  return x == static_cast<double>(static_cast<int64_t>(x));
163
}
164
165
13330
void After(uv_fs_t *req) {
166
13330
  FSReqWrap* req_wrap = static_cast<FSReqWrap*>(req->data);
167
13330
  CHECK_EQ(req_wrap->req(), req);
168
13330
  req_wrap->ReleaseEarly();  // Free memory that's no longer used now.
169
170
13330
  Environment* env = req_wrap->env();
171
13330
  HandleScope handle_scope(env->isolate());
172
13330
  Context::Scope context_scope(env->context());
173
174
  // there is always at least one argument. "error"
175
13330
  int argc = 1;
176
177
  // Allocate space for two args. We may only use one depending on the case.
178
  // (Feel free to increase this if you need more)
179
39990
  Local<Value> argv[2];
180
  MaybeLocal<Value> link;
181
  Local<Value> error;
182
183
13330
  if (req->result < 0) {
184
    // An error happened.
185
    argv[0] = UVException(env->isolate(),
186
                          req->result,
187
                          req_wrap->syscall(),
188
                          nullptr,
189
                          req->path,
190
542
                          req_wrap->data());
191
  } else {
192
    // error value is empty or null for non-error.
193
25576
    argv[0] = Null(env->isolate());
194
195
    // All have at least two args now.
196
12788
    argc = 2;
197
198


12788
    switch (req->fs_type) {
199
      // These all have no data to pass.
200
      case UV_FS_ACCESS:
201
      case UV_FS_CLOSE:
202
      case UV_FS_RENAME:
203
      case UV_FS_UNLINK:
204
      case UV_FS_RMDIR:
205
      case UV_FS_MKDIR:
206
      case UV_FS_FTRUNCATE:
207
      case UV_FS_FSYNC:
208
      case UV_FS_FDATASYNC:
209
      case UV_FS_LINK:
210
      case UV_FS_SYMLINK:
211
      case UV_FS_CHMOD:
212
      case UV_FS_FCHMOD:
213
      case UV_FS_CHOWN:
214
      case UV_FS_FCHOWN:
215
        // These, however, don't.
216
544
        argc = 1;
217
544
        break;
218
219
      case UV_FS_STAT:
220
      case UV_FS_LSTAT:
221
      case UV_FS_FSTAT:
222
809
        argc = 1;
223
        FillStatsArray(env->fs_stats_field_array(),
224
809
                       static_cast<const uv_stat_t*>(req->ptr));
225
809
        break;
226
227
      case UV_FS_UTIME:
228
      case UV_FS_FUTIME:
229
12
        argc = 0;
230
12
        break;
231
232
      case UV_FS_OPEN:
233
954
        argv[1] = Integer::New(env->isolate(), req->result);
234
477
        break;
235
236
      case UV_FS_WRITE:
237
20814
        argv[1] = Integer::New(env->isolate(), req->result);
238
10407
        break;
239
240
      case UV_FS_MKDTEMP:
241
      {
242
        link = StringBytes::Encode(env->isolate(),
243
                                   static_cast<const char*>(req->path),
244
                                   req_wrap->encoding_,
245
6
                                   &error);
246
6
        if (link.IsEmpty()) {
247
          // TODO(addaleax): Use `error` itself here.
248
          argv[0] = UVException(env->isolate(),
249
                                UV_EINVAL,
250
                                req_wrap->syscall(),
251
                                "Invalid character encoding for filename",
252
                                req->path,
253
                                req_wrap->data());
254
        } else {
255
6
          argv[1] = link.ToLocalChecked();
256
        }
257
6
        break;
258
      }
259
260
      case UV_FS_READLINK:
261
        link = StringBytes::Encode(env->isolate(),
262
                                   static_cast<const char*>(req->ptr),
263
                                   req_wrap->encoding_,
264
27
                                   &error);
265
27
        if (link.IsEmpty()) {
266
          // TODO(addaleax): Use `error` itself here.
267
          argv[0] = UVException(env->isolate(),
268
                                UV_EINVAL,
269
                                req_wrap->syscall(),
270
                                "Invalid character encoding for link",
271
                                req->path,
272
                                req_wrap->data());
273
        } else {
274
27
          argv[1] = link.ToLocalChecked();
275
        }
276
27
        break;
277
278
      case UV_FS_REALPATH:
279
        link = StringBytes::Encode(env->isolate(),
280
                                   static_cast<const char*>(req->ptr),
281
                                   req_wrap->encoding_,
282
                                   &error);
283
        if (link.IsEmpty()) {
284
          // TODO(addaleax): Use `error` itself here.
285
          argv[0] = UVException(env->isolate(),
286
                                UV_EINVAL,
287
                                req_wrap->syscall(),
288
                                "Invalid character encoding for link",
289
                                req->path,
290
                                req_wrap->data());
291
        } else {
292
          argv[1] = link.ToLocalChecked();
293
        }
294
        break;
295
296
      case UV_FS_READ:
297
        // Buffer interface
298
982
        argv[1] = Integer::New(env->isolate(), req->result);
299
491
        break;
300
301
      case UV_FS_SCANDIR:
302
        {
303
          int r;
304
15
          Local<Array> names = Array::New(env->isolate(), 0);
305
15
          Local<Function> fn = env->push_values_to_array_function();
306
135
          Local<Value> name_argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
307
15
          size_t name_idx = 0;
308
309
3127
          for (int i = 0; ; i++) {
310
            uv_dirent_t ent;
311
312
3127
            r = uv_fs_scandir_next(req, &ent);
313
3127
            if (r == UV_EOF)
314
30
              break;
315
3112
            if (r != 0) {
316
              argv[0] = UVException(r,
317
                                    nullptr,
318
                                    req_wrap->syscall(),
319
                                    static_cast<const char*>(req->path));
320
              break;
321
            }
322
323
            MaybeLocal<Value> filename =
324
                StringBytes::Encode(env->isolate(),
325
                                    ent.name,
326
                                    req_wrap->encoding_,
327
3112
                                    &error);
328
3112
            if (filename.IsEmpty()) {
329
              // TODO(addaleax): Use `error` itself here.
330
              argv[0] = UVException(env->isolate(),
331
                                    UV_EINVAL,
332
                                    req_wrap->syscall(),
333
                                    "Invalid character encoding for filename",
334
                                    req->path,
335
                                    req_wrap->data());
336
              break;
337
            }
338
6224
            name_argv[name_idx++] = filename.ToLocalChecked();
339
340
3112
            if (name_idx >= arraysize(name_argv)) {
341
1536
              fn->Call(env->context(), names, name_idx, name_argv)
342
768
                  .ToLocalChecked();
343
384
              name_idx = 0;
344
            }
345
3112
          }
346
347
15
          if (name_idx > 0) {
348
56
            fn->Call(env->context(), names, name_idx, name_argv)
349
28
                .ToLocalChecked();
350
          }
351
352
15
          argv[1] = names;
353
        }
354
15
        break;
355
356
      default:
357
        CHECK(0 && "Unhandled eio response");
358
    }
359
  }
360
361
13330
  req_wrap->MakeCallback(env->oncomplete_string(), argc, argv);
362
363
13326
  uv_fs_req_cleanup(req_wrap->req());
364
26652
  req_wrap->Dispose();
365
13326
}
366
367
// This struct is only used on sync fs calls.
368
// For async calls FSReqWrap is used.
369
class fs_req_wrap {
370
 public:
371
136299
  fs_req_wrap() {}
372
136299
  ~fs_req_wrap() { uv_fs_req_cleanup(&req); }
373
  uv_fs_t req;
374
375
 private:
376
  DISALLOW_COPY_AND_ASSIGN(fs_req_wrap);
377
};
378
379
380
#define ASYNC_DEST_CALL(func, request, dest, encoding, ...)                   \
381
  Environment* env = Environment::GetCurrent(args);                           \
382
  CHECK(request->IsObject());                                                 \
383
  FSReqWrap* req_wrap = FSReqWrap::New(env, request.As<Object>(),             \
384
                                       #func, dest, encoding);                \
385
  int err = uv_fs_ ## func(env->event_loop(),                                 \
386
                           req_wrap->req(),                                   \
387
                           __VA_ARGS__,                                       \
388
                           After);                                            \
389
  req_wrap->Dispatched();                                                     \
390
  if (err < 0) {                                                              \
391
    uv_fs_t* uv_req = req_wrap->req();                                        \
392
    uv_req->result = err;                                                     \
393
    uv_req->path = nullptr;                                                   \
394
    After(uv_req);                                                            \
395
    req_wrap = nullptr;                                                       \
396
  } else {                                                                    \
397
    args.GetReturnValue().Set(req_wrap->persistent());                        \
398
  }
399
400
#define ASYNC_CALL(func, req, encoding, ...)                                  \
401
  ASYNC_DEST_CALL(func, req, nullptr, encoding, __VA_ARGS__)                  \
402
403
#define SYNC_DEST_CALL(func, path, dest, ...)                                 \
404
  fs_req_wrap req_wrap;                                                       \
405
  env->PrintSyncTrace();                                                      \
406
  int err = uv_fs_ ## func(env->event_loop(),                                 \
407
                         &req_wrap.req,                                       \
408
                         __VA_ARGS__,                                         \
409
                         nullptr);                                            \
410
  if (err < 0) {                                                              \
411
    return env->ThrowUVException(err, #func, nullptr, path, dest);            \
412
  }                                                                           \
413
414
#define SYNC_CALL(func, path, ...)                                            \
415
  SYNC_DEST_CALL(func, path, nullptr, __VA_ARGS__)                            \
416
417
#define SYNC_REQ req_wrap.req
418
419
#define SYNC_RESULT err
420
421
108
void Access(const FunctionCallbackInfo<Value>& args) {
422
108
  Environment* env = Environment::GetCurrent(args.GetIsolate());
423
108
  HandleScope scope(env->isolate());
424
425
108
  if (args.Length() < 2)
426
    return TYPE_ERROR("path and mode are required");
427
216
  if (!args[1]->IsInt32())
428
    return TYPE_ERROR("mode must be an integer");
429
430
140
  BufferValue path(env->isolate(), args[0]);
431
108
  ASSERT_PATH(path)
432
433
212
  int mode = static_cast<int>(args[1]->Int32Value());
434
435
212
  if (args[2]->IsObject()) {
436

84
    ASYNC_CALL(access, args[2], UTF8, *path, mode);
437
  } else {
438

94
    SYNC_CALL(access, *path, *path, mode);
439
32
  }
440
}
441
442
443
17117
void Close(const FunctionCallbackInfo<Value>& args) {
444
17117
  Environment* env = Environment::GetCurrent(args);
445
446
17117
  if (args.Length() < 1)
447
    return TYPE_ERROR("fd is required");
448
34234
  if (!args[0]->IsInt32())
449
    return TYPE_ERROR("fd must be a file descriptor");
450
451
34234
  int fd = args[0]->Int32Value();
452
453
34234
  if (args[1]->IsObject()) {
454

2954
    ASYNC_CALL(close, args[1], UTF8, fd)
455
  } else {
456

16695
    SYNC_CALL(close, 0, fd)
457
  }
458
}
459
460
}  // anonymous namespace
461
462
71016
void FillStatsArray(double* fields, const uv_stat_t* s) {
463
71016
  fields[0] = s->st_dev;
464
71016
  fields[1] = s->st_mode;
465
71016
  fields[2] = s->st_nlink;
466
71016
  fields[3] = s->st_uid;
467
71016
  fields[4] = s->st_gid;
468
71016
  fields[5] = s->st_rdev;
469
#if defined(__POSIX__)
470
71016
  fields[6] = s->st_blksize;
471
#else
472
  fields[6] = -1;
473
#endif
474
71016
  fields[7] = s->st_ino;
475
71016
  fields[8] = s->st_size;
476
#if defined(__POSIX__)
477
71016
  fields[9] = s->st_blocks;
478
#else
479
  fields[9] = -1;
480
#endif
481
  // Dates.
482
#define X(idx, name)                          \
483
  fields[idx] = (s->st_##name.tv_sec * 1e3) + \
484
                (s->st_##name.tv_nsec / 1e6); \
485
486
71016
  X(10, atim)
487
71016
  X(11, mtim)
488
71016
  X(12, ctim)
489
71016
  X(13, birthtim)
490
#undef X
491
71016
}
492
493
// Used to speed up module loading.  Returns the contents of the file as
494
// a string or undefined when the file cannot be opened.  The speedup
495
// comes from not creating Error objects on failure.
496
7842
static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
497
7842
  Environment* env = Environment::GetCurrent(args);
498
7842
  uv_loop_t* loop = env->event_loop();
499
500
23526
  CHECK(args[0]->IsString());
501
7842
  node::Utf8Value path(env->isolate(), args[0]);
502
503
  uv_fs_t open_req;
504
7842
  const int fd = uv_fs_open(loop, &open_req, *path, O_RDONLY, 0, nullptr);
505
7842
  uv_fs_req_cleanup(&open_req);
506
507
7842
  if (fd < 0) {
508
11766
    return;
509
  }
510
511
3918
  const size_t kBlockSize = 32 << 10;
512
7836
  std::vector<char> chars;
513
3918
  int64_t offset = 0;
514
  ssize_t numchars;
515
3918
  do {
516
3918
    const size_t start = chars.size();
517
3918
    chars.resize(start + kBlockSize);
518
519
    uv_buf_t buf;
520
3918
    buf.base = &chars[start];
521
3918
    buf.len = kBlockSize;
522
523
    uv_fs_t read_req;
524
3918
    numchars = uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr);
525
3918
    uv_fs_req_cleanup(&read_req);
526
527
3918
    CHECK_GE(numchars, 0);
528
3918
    offset += numchars;
529
  } while (static_cast<size_t>(numchars) == kBlockSize);
530
531
  uv_fs_t close_req;
532
3918
  CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr));
533
3918
  uv_fs_req_cleanup(&close_req);
534
535
3918
  size_t start = 0;
536

3918
  if (offset >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
537
    start = 3;  // Skip UTF-8 BOM.
538
  }
539
540
  Local<String> chars_string =
541
      String::NewFromUtf8(env->isolate(),
542
3918
                          &chars[start],
543
                          String::kNormalString,
544
7836
                          offset - start);
545
11754
  args.GetReturnValue().Set(chars_string);
546
}
547
548
// Used to speed up module loading.  Returns 0 if the path refers to
549
// a file, 1 when it's a directory or < 0 on error (usually -ENOENT.)
550
// The speedup comes from not creating thousands of Stat and Error objects.
551
40243
static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
552
40243
  Environment* env = Environment::GetCurrent(args);
553
554
120729
  CHECK(args[0]->IsString());
555
40243
  node::Utf8Value path(env->isolate(), args[0]);
556
557
  uv_fs_t req;
558
40243
  int rc = uv_fs_stat(env->event_loop(), &req, *path, nullptr);
559
40243
  if (rc == 0) {
560
22393
    const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
561
22393
    rc = !!(s->st_mode & S_IFDIR);
562
  }
563
40243
  uv_fs_req_cleanup(&req);
564
565
80486
  args.GetReturnValue().Set(rc);
566
40243
}
567
568
10341
static void Stat(const FunctionCallbackInfo<Value>& args) {
569
10341
  Environment* env = Environment::GetCurrent(args);
570
571
10341
  if (args.Length() < 1)
572
4
    return TYPE_ERROR("path required");
573
574
10341
  BufferValue path(env->isolate(), args[0]);
575
10341
  ASSERT_PATH(path)
576
577
20682
  if (args[1]->IsObject()) {
578

3990
    ASYNC_CALL(stat, args[1], UTF8, *path)
579
  } else {
580
9771
    SYNC_CALL(stat, *path, *path)
581
    FillStatsArray(env->fs_stats_field_array(),
582

9767
                   static_cast<const uv_stat_t*>(SYNC_REQ.ptr));
583
10337
  }
584
}
585
586
47111
static void LStat(const FunctionCallbackInfo<Value>& args) {
587
47111
  Environment* env = Environment::GetCurrent(args);
588
589
47111
  if (args.Length() < 1)
590
2
    return TYPE_ERROR("path required");
591
592
47111
  BufferValue path(env->isolate(), args[0]);
593
47111
  ASSERT_PATH(path)
594
595
94222
  if (args[1]->IsObject()) {
596

4088
    ASYNC_CALL(lstat, args[1], UTF8, *path)
597
  } else {
598
46527
    SYNC_CALL(lstat, *path, *path)
599
    FillStatsArray(env->fs_stats_field_array(),
600

46525
                   static_cast<const uv_stat_t*>(SYNC_REQ.ptr));
601
47109
  }
602
}
603
604
14039
static void FStat(const FunctionCallbackInfo<Value>& args) {
605
14039
  Environment* env = Environment::GetCurrent(args);
606
607
14039
  if (args.Length() < 1)
608
    return TYPE_ERROR("fd is required");
609
28078
  if (!args[0]->IsInt32())
610
    return TYPE_ERROR("fd must be a file descriptor");
611
612
28078
  int fd = args[0]->Int32Value();
613
614
28078
  if (args[1]->IsObject()) {
615

896
    ASYNC_CALL(fstat, args[1], UTF8, fd)
616
  } else {
617
13911
    SYNC_CALL(fstat, nullptr, fd)
618
    FillStatsArray(env->fs_stats_field_array(),
619
13909
                   static_cast<const uv_stat_t*>(SYNC_REQ.ptr));
620
  }
621
}
622
623
38
static void Symlink(const FunctionCallbackInfo<Value>& args) {
624
38
  Environment* env = Environment::GetCurrent(args);
625
626
38
  int len = args.Length();
627
38
  if (len < 1)
628
1
    return TYPE_ERROR("target path required");
629
38
  if (len < 2)
630
    return TYPE_ERROR("src path required");
631
632
38
  BufferValue target(env->isolate(), args[0]);
633
38
  ASSERT_PATH(target)
634
75
  BufferValue path(env->isolate(), args[1]);
635
38
  ASSERT_PATH(path)
636
637
38
  int flags = 0;
638
639
114
  if (args[2]->IsString()) {
640
20
    node::Utf8Value mode(env->isolate(), args[2]);
641
20
    if (strcmp(*mode, "dir") == 0) {
642
10
      flags |= UV_FS_SYMLINK_DIR;
643
10
    } else if (strcmp(*mode, "junction") == 0) {
644
4
      flags |= UV_FS_SYMLINK_JUNCTION;
645
6
    } else if (strcmp(*mode, "file") != 0) {
646
      return env->ThrowError("Unknown symlink type");
647
20
    }
648
  }
649
650
76
  if (args[3]->IsObject()) {
651

35
    ASYNC_DEST_CALL(symlink, args[3], *path, UTF8, *target, *path, flags)
652
  } else {
653

33
    SYNC_DEST_CALL(symlink, *target, *path, *target, *path, flags)
654
37
  }
655
}
656
657
7
static void Link(const FunctionCallbackInfo<Value>& args) {
658
7
  Environment* env = Environment::GetCurrent(args);
659
660
7
  int len = args.Length();
661
7
  if (len < 1)
662
4
    return TYPE_ERROR("src path required");
663
7
  if (len < 2)
664
    return TYPE_ERROR("dest path required");
665
666
7
  BufferValue src(env->isolate(), args[0]);
667
7
  ASSERT_PATH(src)
668
669
9
  BufferValue dest(env->isolate(), args[1]);
670
6
  ASSERT_PATH(dest)
671
672
10
  if (args[2]->IsObject()) {
673

21
    ASYNC_DEST_CALL(link, args[2], *dest, UTF8, *src, *dest)
674
  } else {
675

2
    SYNC_DEST_CALL(link, *src, *dest, *src, *dest)
676
3
  }
677
}
678
679
61
static void ReadLink(const FunctionCallbackInfo<Value>& args) {
680
61
  Environment* env = Environment::GetCurrent(args);
681
682
61
  const int argc = args.Length();
683
684
61
  if (argc < 1)
685
1
    return TYPE_ERROR("path required");
686
687
61
  BufferValue path(env->isolate(), args[0]);
688
61
  ASSERT_PATH(path)
689
690
61
  const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8);
691
692
61
  Local<Value> callback = Null(env->isolate());
693
61
  if (argc == 3)
694
28
    callback = args[2];
695
696
61
  if (callback->IsObject()) {
697

140
    ASYNC_CALL(readlink, callback, encoding, *path)
698
  } else {
699
33
    SYNC_CALL(readlink, *path, *path)
700
32
    const char* link_path = static_cast<const char*>(SYNC_REQ.ptr);
701
702
    Local<Value> error;
703
    MaybeLocal<Value> rc = StringBytes::Encode(env->isolate(),
704
                                               link_path,
705
                                               encoding,
706
32
                                               &error);
707
32
    if (rc.IsEmpty()) {
708
      // TODO(addaleax): Use `error` itself here.
709
      return env->ThrowUVException(UV_EINVAL,
710
                                   "readlink",
711
                                   "Invalid character encoding for link",
712
                                   *path);
713
    }
714

64
    args.GetReturnValue().Set(rc.ToLocalChecked());
715
60
  }
716
}
717
718
7
static void Rename(const FunctionCallbackInfo<Value>& args) {
719
7
  Environment* env = Environment::GetCurrent(args);
720
721
7
  int len = args.Length();
722
7
  if (len < 1)
723
2
    return TYPE_ERROR("old path required");
724
7
  if (len < 2)
725
    return TYPE_ERROR("new path required");
726
727
7
  BufferValue old_path(env->isolate(), args[0]);
728
7
  ASSERT_PATH(old_path)
729
12
  BufferValue new_path(env->isolate(), args[1]);
730
7
  ASSERT_PATH(new_path)
731
732
14
  if (args[2]->IsObject()) {
733

28
    ASYNC_DEST_CALL(rename, args[2], *new_path, UTF8, *old_path, *new_path)
734
  } else {
735

3
    SYNC_DEST_CALL(rename, *old_path, *new_path, *old_path, *new_path)
736
5
  }
737
}
738
739
34
static void FTruncate(const FunctionCallbackInfo<Value>& args) {
740
34
  Environment* env = Environment::GetCurrent(args);
741
742
34
  if (args.Length() < 2)
743
    return TYPE_ERROR("fd and length are required");
744
68
  if (!args[0]->IsInt32())
745
    return TYPE_ERROR("fd must be a file descriptor");
746
747
68
  int fd = args[0]->Int32Value();
748
749
  // FIXME(bnoordhuis) It's questionable to reject non-ints here but still
750
  // allow implicit coercion from null or undefined to zero.  Probably best
751
  // handled in lib/fs.js.
752
34
  Local<Value> len_v(args[1]);
753

136
  if (!len_v->IsUndefined() &&
754

136
      !len_v->IsNull() &&
755
34
      !IsInt64(len_v->NumberValue())) {
756
    return env->ThrowTypeError("Not an integer");
757
  }
758
759
34
  const int64_t len = len_v->IntegerValue();
760
761
68
  if (args[2]->IsObject()) {
762

182
    ASYNC_CALL(ftruncate, args[2], UTF8, fd, len)
763
  } else {
764

8
    SYNC_CALL(ftruncate, 0, fd, len)
765
  }
766
}
767
768
2
static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
769
2
  Environment* env = Environment::GetCurrent(args);
770
771
2
  if (args.Length() < 1)
772
    return TYPE_ERROR("fd is required");
773
4
  if (!args[0]->IsInt32())
774
    return TYPE_ERROR("fd must be a file descriptor");
775
776
4
  int fd = args[0]->Int32Value();
777
778
4
  if (args[1]->IsObject()) {
779

7
    ASYNC_CALL(fdatasync, args[1], UTF8, fd)
780
  } else {
781

1
    SYNC_CALL(fdatasync, 0, fd)
782
  }
783
}
784
785
17
static void Fsync(const FunctionCallbackInfo<Value>& args) {
786
17
  Environment* env = Environment::GetCurrent(args);
787
788
17
  if (args.Length() < 1)
789
    return TYPE_ERROR("fd is required");
790
34
  if (!args[0]->IsInt32())
791
    return TYPE_ERROR("fd must be a file descriptor");
792
793
34
  int fd = args[0]->Int32Value();
794
795
34
  if (args[1]->IsObject()) {
796

21
    ASYNC_CALL(fsync, args[1], UTF8, fd)
797
  } else {
798

14
    SYNC_CALL(fsync, 0, fd)
799
  }
800
}
801
802
212
static void Unlink(const FunctionCallbackInfo<Value>& args) {
803
212
  Environment* env = Environment::GetCurrent(args);
804
805
212
  if (args.Length() < 1)
806
25
    return TYPE_ERROR("path required");
807
808
212
  BufferValue path(env->isolate(), args[0]);
809
212
  ASSERT_PATH(path)
810
811
424
  if (args[1]->IsObject()) {
812

21
    ASYNC_CALL(unlink, args[1], UTF8, *path)
813
  } else {
814

209
    SYNC_CALL(unlink, *path, *path)
815
187
  }
816
}
817
818
411
static void RMDir(const FunctionCallbackInfo<Value>& args) {
819
411
  Environment* env = Environment::GetCurrent(args);
820
821
411
  if (args.Length() < 1)
822
174
    return TYPE_ERROR("path required");
823
824
411
  BufferValue path(env->isolate(), args[0]);
825
411
  ASSERT_PATH(path)
826
827
822
  if (args[1]->IsObject()) {
828

21
    ASYNC_CALL(rmdir, args[1], UTF8, *path)
829
  } else {
830

408
    SYNC_CALL(rmdir, *path, *path)
831
237
  }
832
}
833
834
2984
static void MKDir(const FunctionCallbackInfo<Value>& args) {
835
2984
  Environment* env = Environment::GetCurrent(args);
836
837
2984
  if (args.Length() < 2)
838
2685
    return TYPE_ERROR("path and mode are required");
839
5968
  if (!args[1]->IsInt32())
840
    return TYPE_ERROR("mode must be an integer");
841
842
2984
  BufferValue path(env->isolate(), args[0]);
843
2984
  ASSERT_PATH(path)
844
845
5968
  int mode = static_cast<int>(args[1]->Int32Value());
846
847
5968
  if (args[2]->IsObject()) {
848

539
    ASYNC_CALL(mkdir, args[2], UTF8, *path, mode)
849
  } else {
850

2907
    SYNC_CALL(mkdir, *path, *path, mode)
851
299
  }
852
}
853
854
static void RealPath(const FunctionCallbackInfo<Value>& args) {
855
  Environment* env = Environment::GetCurrent(args);
856
857
  const int argc = args.Length();
858
859
  if (argc < 1)
860
    return TYPE_ERROR("path required");
861
862
  BufferValue path(env->isolate(), args[0]);
863
  ASSERT_PATH(path)
864
865
  const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8);
866
867
  Local<Value> callback = Null(env->isolate());
868
  if (argc == 3)
869
    callback = args[2];
870
871
  if (callback->IsObject()) {
872
    ASYNC_CALL(realpath, callback, encoding, *path);
873
  } else {
874
    SYNC_CALL(realpath, *path, *path);
875
    const char* link_path = static_cast<const char*>(SYNC_REQ.ptr);
876
877
    Local<Value> error;
878
    MaybeLocal<Value> rc = StringBytes::Encode(env->isolate(),
879
                                               link_path,
880
                                               encoding,
881
                                               &error);
882
    if (rc.IsEmpty()) {
883
      // TODO(addaleax): Use `error` itself here.
884
      return env->ThrowUVException(UV_EINVAL,
885
                                   "realpath",
886
                                   "Invalid character encoding for path",
887
                                   *path);
888
    }
889
    args.GetReturnValue().Set(rc.ToLocalChecked());
890
  }
891
}
892
893
587
static void ReadDir(const FunctionCallbackInfo<Value>& args) {
894
587
  Environment* env = Environment::GetCurrent(args);
895
896
587
  const int argc = args.Length();
897
898
587
  if (argc < 1)
899
38
    return TYPE_ERROR("path required");
900
901
587
  BufferValue path(env->isolate(), args[0]);
902
587
  ASSERT_PATH(path)
903
904
587
  const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8);
905
906
587
  Local<Value> callback = Null(env->isolate());
907
587
  if (argc == 3)
908
17
    callback = args[2];
909
910
587
  if (callback->IsObject()) {
911

85
    ASYNC_CALL(scandir, callback, encoding, *path, 0 /*flags*/)
912
  } else {
913
570
    SYNC_CALL(scandir, *path, *path, 0 /*flags*/)
914
915
532
    CHECK_GE(SYNC_REQ.result, 0);
916
    int r;
917
532
    Local<Array> names = Array::New(env->isolate(), 0);
918
532
    Local<Function> fn = env->push_values_to_array_function();
919
4788
    Local<Value> name_v[NODE_PUSH_VAL_TO_ARRAY_MAX];
920
532
    size_t name_idx = 0;
921
922
7099
    for (int i = 0; ; i++) {
923
      uv_dirent_t ent;
924
925
7099
      r = uv_fs_scandir_next(&SYNC_REQ, &ent);
926
7099
      if (r == UV_EOF)
927
532
        break;
928
6567
      if (r != 0)
929
        return env->ThrowUVException(r, "readdir", "", *path);
930
931
      Local<Value> error;
932
      MaybeLocal<Value> filename = StringBytes::Encode(env->isolate(),
933
                                                       ent.name,
934
                                                       encoding,
935
6567
                                                       &error);
936
6567
      if (filename.IsEmpty()) {
937
        // TODO(addaleax): Use `error` itself here.
938
        return env->ThrowUVException(UV_EINVAL,
939
                                     "readdir",
940
                                     "Invalid character encoding for filename",
941
                                     *path);
942
      }
943
944
13134
      name_v[name_idx++] = filename.ToLocalChecked();
945
946
6567
      if (name_idx >= arraysize(name_v)) {
947
2464
        fn->Call(env->context(), names, name_idx, name_v)
948
1232
            .ToLocalChecked();
949
616
        name_idx = 0;
950
      }
951
6567
    }
952
953
532
    if (name_idx > 0) {
954
2052
      fn->Call(env->context(), names, name_idx, name_v).ToLocalChecked();
955
    }
956
957

1064
    args.GetReturnValue().Set(names);
958
549
  }
959
}
960
961
17530
static void Open(const FunctionCallbackInfo<Value>& args) {
962
17530
  Environment* env = Environment::GetCurrent(args);
963
964
17530
  int len = args.Length();
965
17530
  if (len < 1)
966
6
    return TYPE_ERROR("path required");
967
17530
  if (len < 2)
968
    return TYPE_ERROR("flags required");
969
17530
  if (len < 3)
970
    return TYPE_ERROR("mode required");
971
35060
  if (!args[1]->IsInt32())
972
    return TYPE_ERROR("flags must be an int");
973
35060
  if (!args[2]->IsInt32())
974
    return TYPE_ERROR("mode must be an int");
975
976
17530
  BufferValue path(env->isolate(), args[0]);
977
17530
  ASSERT_PATH(path)
978
979
35060
  int flags = args[1]->Int32Value();
980
35060
  int mode = static_cast<int>(args[2]->Int32Value());
981
982
35060
  if (args[3]->IsObject()) {
983

3549
    ASYNC_CALL(open, args[3], UTF8, *path, flags, mode)
984
  } else {
985
17023
    SYNC_CALL(open, *path, *path, flags, mode)
986

34034
    args.GetReturnValue().Set(SYNC_RESULT);
987
17524
  }
988
}
989
990
991
// Wrapper for write(2).
992
//
993
// bytesWritten = write(fd, buffer, offset, length, position, callback)
994
// 0 fd        integer. file descriptor
995
// 1 buffer    the data to write
996
// 2 offset    where in the buffer to start from
997
// 3 length    how much to write
998
// 4 position  if integer, position to write at in the file.
999
//             if null, write from the current position
1000
23874
static void WriteBuffer(const FunctionCallbackInfo<Value>& args) {
1001
23874
  Environment* env = Environment::GetCurrent(args);
1002
1003
47748
  if (!args[0]->IsInt32())
1004
10380
    return env->ThrowTypeError("First argument must be file descriptor");
1005
1006
23873
  CHECK(Buffer::HasInstance(args[1]));
1007
1008
47746
  int fd = args[0]->Int32Value();
1009
47746
  Local<Object> obj = args[1].As<Object>();
1010
23873
  const char* buf = Buffer::Data(obj);
1011
23873
  size_t buffer_length = Buffer::Length(obj);
1012
47746
  size_t off = args[2]->Uint32Value();
1013
47746
  size_t len = args[3]->Uint32Value();
1014

53248
  int64_t pos = GET_OFFSET(args[4]);
1015
23873
  Local<Value> req = args[5];
1016
1017
23873
  if (off > buffer_length)
1018
    return env->ThrowRangeError("offset out of bounds");
1019
23873
  if (len > buffer_length)
1020
    return env->ThrowRangeError("length out of bounds");
1021
23873
  if (off + len < off)
1022
    return env->ThrowRangeError("off + len overflow");
1023
23873
  if (!Buffer::IsWithinBounds(off, len, buffer_length))
1024
    return env->ThrowRangeError("off + len > buffer.length");
1025
1026
23873
  buf += off;
1027
1028
23873
  uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);
1029
1030
23873
  if (req->IsObject()) {
1031

51890
    ASYNC_CALL(write, req, UTF8, fd, &uvbuf, 1, pos)
1032
10378
    return;
1033
  }
1034
1035
13495
  SYNC_CALL(write, nullptr, fd, &uvbuf, 1, pos)
1036
26990
  args.GetReturnValue().Set(SYNC_RESULT);
1037
}
1038
1039
1040
// Wrapper for writev(2).
1041
//
1042
// bytesWritten = writev(fd, chunks, position, callback)
1043
// 0 fd        integer. file descriptor
1044
// 1 chunks    array of buffers to write
1045
// 2 position  if integer, position to write at in the file.
1046
//             if null, write from the current position
1047
3
static void WriteBuffers(const FunctionCallbackInfo<Value>& args) {
1048
3
  Environment* env = Environment::GetCurrent(args);
1049
1050
6
  CHECK(args[0]->IsInt32());
1051
6
  CHECK(args[1]->IsArray());
1052
1053
6
  int fd = args[0]->Int32Value();
1054
6
  Local<Array> chunks = args[1].As<Array>();
1055

6
  int64_t pos = GET_OFFSET(args[2]);
1056
3
  Local<Value> req = args[3];
1057
1058
3
  MaybeStackBuffer<uv_buf_t> iovs(chunks->Length());
1059
1060
223
  for (uint32_t i = 0; i < iovs.length(); i++) {
1061
220
    Local<Value> chunk = chunks->Get(i);
1062
1063
220
    if (!Buffer::HasInstance(chunk))
1064
      return env->ThrowTypeError("Array elements all need to be buffers");
1065
1066
220
    iovs[i] = uv_buf_init(Buffer::Data(chunk), Buffer::Length(chunk));
1067
  }
1068
1069
3
  if (req->IsObject()) {
1070

15
    ASYNC_CALL(write, req, UTF8, fd, *iovs, iovs.length(), pos)
1071
3
    return;
1072
  }
1073
1074
  SYNC_CALL(write, nullptr, fd, *iovs, iovs.length(), pos)
1075
  args.GetReturnValue().Set(SYNC_RESULT);
1076
}
1077
1078
1079
// Wrapper for write(2).
1080
//
1081
// bytesWritten = write(fd, string, position, enc, callback)
1082
// 0 fd        integer. file descriptor
1083
// 1 string    non-buffer values are converted to strings
1084
// 2 position  if integer, position to write at in the file.
1085
//             if null, write from the current position
1086
// 3 enc       encoding of string
1087
582
static void WriteString(const FunctionCallbackInfo<Value>& args) {
1088
582
  Environment* env = Environment::GetCurrent(args);
1089
1090
1164
  if (!args[0]->IsInt32())
1091
1
    return env->ThrowTypeError("First argument must be file descriptor");
1092
1093
  Local<Value> req;
1094
581
  Local<Value> string = args[1];
1095
1162
  int fd = args[0]->Int32Value();
1096
581
  char* buf = nullptr;
1097
  int64_t pos;
1098
  size_t len;
1099
581
  FSReqWrap::Ownership ownership = FSReqWrap::COPY;
1100
1101
  // will assign buf and len if string was external
1102
581
  if (!StringBytes::GetExternalParts(string,
1103
                                     const_cast<const char**>(&buf),
1104
581
                                     &len)) {
1105
581
    enum encoding enc = ParseEncoding(env->isolate(), args[3], UTF8);
1106
581
    len = StringBytes::StorageSize(env->isolate(), string, enc);
1107
581
    buf = new char[len];
1108
    // StorageSize may return too large a char, so correct the actual length
1109
    // by the write size
1110
581
    len = StringBytes::Write(env->isolate(), buf, len, args[1], enc);
1111
581
    ownership = FSReqWrap::MOVE;
1112
  }
1113

1216
  pos = GET_OFFSET(args[2]);
1114
581
  req = args[4];
1115
1116
581
  uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);
1117
1118
581
  if (!req->IsObject()) {
1119
    // SYNC_CALL returns on error.  Make sure to always free the memory.
1120
    struct Delete {
1121
555
      inline explicit Delete(char* pointer) : pointer_(pointer) {}
1122
555
      inline ~Delete() { delete[] pointer_; }
1123
      char* const pointer_;
1124
    };
1125
555
    Delete delete_on_return(ownership == FSReqWrap::MOVE ? buf : nullptr);
1126
1110
    SYNC_CALL(write, nullptr, fd, &uvbuf, 1, pos)
1127
1665
    return args.GetReturnValue().Set(SYNC_RESULT);
1128
  }
1129
1130
  FSReqWrap* req_wrap =
1131
52
      FSReqWrap::New(env, req.As<Object>(), "write", buf, UTF8, ownership);
1132
  int err = uv_fs_write(env->event_loop(),
1133
                        req_wrap->req(),
1134
                        fd,
1135
                        &uvbuf,
1136
                        1,
1137
                        pos,
1138
26
                        After);
1139
26
  req_wrap->Dispatched();
1140
26
  if (err < 0) {
1141
    uv_fs_t* uv_req = req_wrap->req();
1142
    uv_req->result = err;
1143
    uv_req->path = nullptr;
1144
    After(uv_req);
1145
    return;
1146
  }
1147
1148
78
  return args.GetReturnValue().Set(req_wrap->persistent());
1149
}
1150
1151
1152
/*
1153
 * Wrapper for read(2).
1154
 *
1155
 * bytesRead = fs.read(fd, buffer, offset, length, position)
1156
 *
1157
 * 0 fd        integer. file descriptor
1158
 * 1 buffer    instance of Buffer
1159
 * 2 offset    integer. offset to start reading into inside buffer
1160
 * 3 length    integer. length to read
1161
 * 4 position  file position - null for current position
1162
 *
1163
 */
1164
14499
static void Read(const FunctionCallbackInfo<Value>& args) {
1165
14499
  Environment* env = Environment::GetCurrent(args);
1166
1167
14499
  if (args.Length() < 2)
1168
2
    return TYPE_ERROR("fd and buffer are required");
1169
28998
  if (!args[0]->IsInt32())
1170
    return TYPE_ERROR("fd must be a file descriptor");
1171
14499
  if (!Buffer::HasInstance(args[1]))
1172
2
    return TYPE_ERROR("Second argument needs to be a buffer");
1173
1174
28994
  int fd = args[0]->Int32Value();
1175
1176
  Local<Value> req;
1177
1178
  size_t len;
1179
  int64_t pos;
1180
1181
14497
  char * buf = nullptr;
1182
1183
43491
  Local<Object> buffer_obj = args[1]->ToObject(env->isolate());
1184
14497
  char *buffer_data = Buffer::Data(buffer_obj);
1185
14497
  size_t buffer_length = Buffer::Length(buffer_obj);
1186
1187
28994
  size_t off = args[2]->Int32Value();
1188
14497
  if (off >= buffer_length) {
1189
    return env->ThrowError("Offset is out of bounds");
1190
  }
1191
1192
28994
  len = args[3]->Int32Value();
1193
14497
  if (!Buffer::IsWithinBounds(off, len, buffer_length))
1194
    return env->ThrowRangeError("Length extends beyond buffer");
1195
1196

29538
  pos = GET_OFFSET(args[4]);
1197
1198
14497
  buf = buffer_data + off;
1199
1200
14497
  uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);
1201
1202
14497
  req = args[5];
1203
1204
14497
  if (req->IsObject()) {
1205

2475
    ASYNC_CALL(read, req, UTF8, fd, &uvbuf, 1, pos);
1206
  } else {
1207
14002
    SYNC_CALL(read, 0, fd, &uvbuf, 1, pos)
1208
28004
    args.GetReturnValue().Set(SYNC_RESULT);
1209
  }
1210
}
1211
1212
1213
/* fs.chmod(path, mode);
1214
 * Wrapper for chmod(1) / EIO_CHMOD
1215
 */
1216
11
static void Chmod(const FunctionCallbackInfo<Value>& args) {
1217
11
  Environment* env = Environment::GetCurrent(args);
1218
1219
11
  if (args.Length() < 2)
1220
1
    return TYPE_ERROR("path and mode are required");
1221
22
  if (!args[1]->IsInt32())
1222
    return TYPE_ERROR("mode must be an integer");
1223
1224
11
  BufferValue path(env->isolate(), args[0]);
1225
11
  ASSERT_PATH(path)
1226
1227
22
  int mode = static_cast<int>(args[1]->Int32Value());
1228
1229
22
  if (args[2]->IsObject()) {
1230

21
    ASYNC_CALL(chmod, args[2], UTF8, *path, mode);
1231
  } else {
1232

8
    SYNC_CALL(chmod, *path, *path, mode);
1233
10
  }
1234
}
1235
1236
1237
/* fs.fchmod(fd, mode);
1238
 * Wrapper for fchmod(1) / EIO_FCHMOD
1239
 */
1240
2
static void FChmod(const FunctionCallbackInfo<Value>& args) {
1241
2
  Environment* env = Environment::GetCurrent(args);
1242
1243
2
  if (args.Length() < 2)
1244
    return TYPE_ERROR("fd and mode are required");
1245
4
  if (!args[0]->IsInt32())
1246
    return TYPE_ERROR("fd must be a file descriptor");
1247
4
  if (!args[1]->IsInt32())
1248
    return TYPE_ERROR("mode must be an integer");
1249
1250
4
  int fd = args[0]->Int32Value();
1251
4
  int mode = static_cast<int>(args[1]->Int32Value());
1252
1253
4
  if (args[2]->IsObject()) {
1254

7
    ASYNC_CALL(fchmod, args[2], UTF8, fd, mode);
1255
  } else {
1256

1
    SYNC_CALL(fchmod, 0, fd, mode);
1257
  }
1258
}
1259
1260
1261
/* fs.chown(path, uid, gid);
1262
 * Wrapper for chown(1) / EIO_CHOWN
1263
 */
1264
3
static void Chown(const FunctionCallbackInfo<Value>& args) {
1265
3
  Environment* env = Environment::GetCurrent(args);
1266
1267
3
  int len = args.Length();
1268
3
  if (len < 1)
1269
    return TYPE_ERROR("path required");
1270
3
  if (len < 2)
1271
    return TYPE_ERROR("uid required");
1272
3
  if (len < 3)
1273
    return TYPE_ERROR("gid required");
1274
6
  if (!args[1]->IsUint32())
1275
    return TYPE_ERROR("uid must be an unsigned int");
1276
6
  if (!args[2]->IsUint32())
1277
    return TYPE_ERROR("gid must be an unsigned int");
1278
1279
3
  BufferValue path(env->isolate(), args[0]);
1280
3
  ASSERT_PATH(path)
1281
1282
6
  uv_uid_t uid = static_cast<uv_uid_t>(args[1]->Uint32Value());
1283
6
  uv_gid_t gid = static_cast<uv_gid_t>(args[2]->Uint32Value());
1284
1285
6
  if (args[3]->IsObject()) {
1286

21
    ASYNC_CALL(chown, args[3], UTF8, *path, uid, gid);
1287
  } else {
1288
    SYNC_CALL(chown, *path, *path, uid, gid);
1289
3
  }
1290
}
1291
1292
1293
/* fs.fchown(fd, uid, gid);
1294
 * Wrapper for fchown(1) / EIO_FCHOWN
1295
 */
1296
static void FChown(const FunctionCallbackInfo<Value>& args) {
1297
  Environment* env = Environment::GetCurrent(args);
1298
1299
  int len = args.Length();
1300
  if (len < 1)
1301
    return TYPE_ERROR("fd required");
1302
  if (len < 2)
1303
    return TYPE_ERROR("uid required");
1304
  if (len < 3)
1305
    return TYPE_ERROR("gid required");
1306
  if (!args[0]->IsInt32())
1307
    return TYPE_ERROR("fd must be an int");
1308
  if (!args[1]->IsUint32())
1309
    return TYPE_ERROR("uid must be an unsigned int");
1310
  if (!args[2]->IsUint32())
1311
    return TYPE_ERROR("gid must be an unsigned int");
1312
1313
  int fd = args[0]->Int32Value();
1314
  uv_uid_t uid = static_cast<uv_uid_t>(args[1]->Uint32Value());
1315
  uv_gid_t gid = static_cast<uv_gid_t>(args[2]->Uint32Value());
1316
1317
  if (args[3]->IsObject()) {
1318
    ASYNC_CALL(fchown, args[3], UTF8, fd, uid, gid);
1319
  } else {
1320
    SYNC_CALL(fchown, 0, fd, uid, gid);
1321
  }
1322
}
1323
1324
1325
24
static void UTimes(const FunctionCallbackInfo<Value>& args) {
1326
24
  Environment* env = Environment::GetCurrent(args);
1327
1328
24
  int len = args.Length();
1329
24
  if (len < 1)
1330
6
    return TYPE_ERROR("path required");
1331
24
  if (len < 2)
1332
    return TYPE_ERROR("atime required");
1333
24
  if (len < 3)
1334
    return TYPE_ERROR("mtime required");
1335
48
  if (!args[1]->IsNumber())
1336
    return TYPE_ERROR("atime must be a number");
1337
48
  if (!args[2]->IsNumber())
1338
    return TYPE_ERROR("mtime must be a number");
1339
1340
24
  BufferValue path(env->isolate(), args[0]);
1341
24
  ASSERT_PATH(path)
1342
1343
48
  const double atime = static_cast<double>(args[1]->NumberValue());
1344
48
  const double mtime = static_cast<double>(args[2]->NumberValue());
1345
1346
48
  if (args[3]->IsObject()) {
1347

84
    ASYNC_CALL(utime, args[3], UTF8, *path, atime, mtime);
1348
  } else {
1349

12
    SYNC_CALL(utime, *path, *path, atime, mtime);
1350
18
  }
1351
}
1352
1353
24
static void FUTimes(const FunctionCallbackInfo<Value>& args) {
1354
24
  Environment* env = Environment::GetCurrent(args);
1355
1356
24
  int len = args.Length();
1357
24
  if (len < 1)
1358
    return TYPE_ERROR("fd required");
1359
24
  if (len < 2)
1360
    return TYPE_ERROR("atime required");
1361
24
  if (len < 3)
1362
    return TYPE_ERROR("mtime required");
1363
48
  if (!args[0]->IsInt32())
1364
    return TYPE_ERROR("fd must be an int");
1365
48
  if (!args[1]->IsNumber())
1366
    return TYPE_ERROR("atime must be a number");
1367
48
  if (!args[2]->IsNumber())
1368
    return TYPE_ERROR("mtime must be a number");
1369
1370
48
  const int fd = args[0]->Int32Value();
1371
48
  const double atime = static_cast<double>(args[1]->NumberValue());
1372
48
  const double mtime = static_cast<double>(args[2]->NumberValue());
1373
1374
48
  if (args[3]->IsObject()) {
1375

84
    ASYNC_CALL(futime, args[3], UTF8, fd, atime, mtime);
1376
  } else {
1377

12
    SYNC_CALL(futime, 0, fd, atime, mtime);
1378
  }
1379
}
1380
1381
11
static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
1382
11
  Environment* env = Environment::GetCurrent(args);
1383
1384
11
  CHECK_GE(args.Length(), 2);
1385
1386
11
  BufferValue tmpl(env->isolate(), args[0]);
1387
11
  if (*tmpl == nullptr)
1388
    return TYPE_ERROR("template must be a string or Buffer");
1389
1390
11
  const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8);
1391
1392
22
  if (args[2]->IsObject()) {
1393

42
    ASYNC_CALL(mkdtemp, args[2], encoding, *tmpl);
1394
  } else {
1395
5
    SYNC_CALL(mkdtemp, *tmpl, *tmpl);
1396
5
    const char* path = static_cast<const char*>(SYNC_REQ.path);
1397
1398
    Local<Value> error;
1399
    MaybeLocal<Value> rc =
1400
5
        StringBytes::Encode(env->isolate(), path, encoding, &error);
1401
5
    if (rc.IsEmpty()) {
1402
      // TODO(addaleax): Use `error` itself here.
1403
      return env->ThrowUVException(UV_EINVAL,
1404
                                   "mkdtemp",
1405
                                   "Invalid character encoding for filename",
1406
                                   *tmpl);
1407
    }
1408

10
    args.GetReturnValue().Set(rc.ToLocalChecked());
1409
11
  }
1410
}
1411
1412
2401
void GetStatValues(const FunctionCallbackInfo<Value>& args) {
1413
2401
  Environment* env = Environment::GetCurrent(args);
1414
2401
  double* fields = env->fs_stats_field_array();
1415
2401
  if (fields == nullptr) {
1416
    // stat fields contains twice the number of entries because `fs.StatWatcher`
1417
    // needs room to store data for *two* `fs.Stats` instances.
1418
2401
    fields = new double[2 * 14];
1419
2401
    env->set_fs_stats_field_array(fields);
1420
  }
1421
  Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(),
1422
                                           fields,
1423
2401
                                           sizeof(double) * 2 * 14);
1424
2401
  Local<Float64Array> fields_array = Float64Array::New(ab, 0, 2 * 14);
1425
4802
  args.GetReturnValue().Set(fields_array);
1426
2401
}
1427
1428
2401
void InitFs(Local<Object> target,
1429
            Local<Value> unused,
1430
            Local<Context> context,
1431
            void* priv) {
1432
2401
  Environment* env = Environment::GetCurrent(context);
1433
1434
2401
  env->SetMethod(target, "access", Access);
1435
2401
  env->SetMethod(target, "close", Close);
1436
2401
  env->SetMethod(target, "open", Open);
1437
2401
  env->SetMethod(target, "read", Read);
1438
2401
  env->SetMethod(target, "fdatasync", Fdatasync);
1439
2401
  env->SetMethod(target, "fsync", Fsync);
1440
2401
  env->SetMethod(target, "rename", Rename);
1441
2401
  env->SetMethod(target, "ftruncate", FTruncate);
1442
2401
  env->SetMethod(target, "rmdir", RMDir);
1443
2401
  env->SetMethod(target, "mkdir", MKDir);
1444
2401
  env->SetMethod(target, "readdir", ReadDir);
1445
2401
  env->SetMethod(target, "internalModuleReadFile", InternalModuleReadFile);
1446
2401
  env->SetMethod(target, "internalModuleStat", InternalModuleStat);
1447
2401
  env->SetMethod(target, "stat", Stat);
1448
2401
  env->SetMethod(target, "lstat", LStat);
1449
2401
  env->SetMethod(target, "fstat", FStat);
1450
2401
  env->SetMethod(target, "link", Link);
1451
2401
  env->SetMethod(target, "symlink", Symlink);
1452
2401
  env->SetMethod(target, "readlink", ReadLink);
1453
2401
  env->SetMethod(target, "unlink", Unlink);
1454
2401
  env->SetMethod(target, "writeBuffer", WriteBuffer);
1455
2401
  env->SetMethod(target, "writeBuffers", WriteBuffers);
1456
2401
  env->SetMethod(target, "writeString", WriteString);
1457
2401
  env->SetMethod(target, "realpath", RealPath);
1458
1459
2401
  env->SetMethod(target, "chmod", Chmod);
1460
2401
  env->SetMethod(target, "fchmod", FChmod);
1461
  // env->SetMethod(target, "lchmod", LChmod);
1462
1463
2401
  env->SetMethod(target, "chown", Chown);
1464
2401
  env->SetMethod(target, "fchown", FChown);
1465
  // env->SetMethod(target, "lchown", LChown);
1466
1467
2401
  env->SetMethod(target, "utimes", UTimes);
1468
2401
  env->SetMethod(target, "futimes", FUTimes);
1469
1470
2401
  env->SetMethod(target, "mkdtemp", Mkdtemp);
1471
1472
2401
  env->SetMethod(target, "getStatValues", GetStatValues);
1473
1474
2401
  StatWatcher::Initialize(env, target);
1475
1476
  // Create FunctionTemplate for FSReqWrap
1477
  Local<FunctionTemplate> fst =
1478
2401
      FunctionTemplate::New(env->isolate(), NewFSReqWrap);
1479
4802
  fst->InstanceTemplate()->SetInternalFieldCount(1);
1480
2401
  env->SetProtoMethod(fst, "getAsyncId", AsyncWrap::GetAsyncId);
1481
4802
  fst->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap"));
1482
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap"),
1483
7203
              fst->GetFunction());
1484
2401
}
1485
1486
}  // end namespace node
1487
1488
2441
NODE_MODULE_CONTEXT_AWARE_BUILTIN(fs, node::InitFs)