GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/node_file.cc Lines: 585 649 90.1 %
Date: 2016-09-05 Branches: 345 585 59.0 %

Line Branch Exec Source
1
#include "node.h"
2
#include "node_file.h"
3
#include "node_buffer.h"
4
#include "node_internals.h"
5
#include "node_stat_watcher.h"
6
7
#include "env.h"
8
#include "env-inl.h"
9
#include "req-wrap.h"
10
#include "req-wrap-inl.h"
11
#include "string_bytes.h"
12
#include "util.h"
13
14
#include <fcntl.h>
15
#include <sys/types.h>
16
#include <sys/stat.h>
17
#include <string.h>
18
#include <errno.h>
19
#include <limits.h>
20
21
#if defined(__MINGW32__) || defined(_MSC_VER)
22
# include <io.h>
23
#endif
24
25
#include <vector>
26
27
namespace node {
28
29
using v8::Array;
30
using v8::Context;
31
using v8::EscapableHandleScope;
32
using v8::Function;
33
using v8::FunctionCallbackInfo;
34
using v8::FunctionTemplate;
35
using v8::HandleScope;
36
using v8::Integer;
37
using v8::Local;
38
using v8::Number;
39
using v8::Object;
40
using v8::String;
41
using v8::Value;
42
43
#ifndef MIN
44
# define MIN(a, b) ((a) < (b) ? (a) : (b))
45
#endif
46
47
#define TYPE_ERROR(msg) env->ThrowTypeError(msg)
48
49
#define GET_OFFSET(a) ((a)->IsNumber() ? (a)->IntegerValue() : -1)
50
51
class FSReqWrap: public ReqWrap<uv_fs_t> {
52
 public:
53
  enum Ownership { COPY, MOVE };
54
55
  inline static FSReqWrap* New(Environment* env,
56
                               Local<Object> req,
57
                               const char* syscall,
58
                               const char* data = nullptr,
59
                               enum encoding encoding = UTF8,
60
                               Ownership ownership = COPY);
61
62
  inline void Dispose();
63
64
  void ReleaseEarly() {
65

54528
    if (data_ != inline_data()) {
66

27236
      delete[] data_;
67
27236
      data_ = nullptr;
68
    }
69
  }
70
71
  const char* syscall() const { return syscall_; }
72
  const char* data() const { return data_; }
73
  const enum encoding encoding_;
74
75
  size_t self_size() const override { return sizeof(*this); }
76
77
 private:
78
13633
  FSReqWrap(Environment* env,
79
            Local<Object> req,
80
            const char* syscall,
81
            const char* data,
82
            enum encoding encoding)
83
      : ReqWrap(env, req, AsyncWrap::PROVIDER_FSREQWRAP),
84
        encoding_(encoding),
85
        syscall_(syscall),
86
13633
        data_(data) {
87
27266
    Wrap(object(), this);
88
13633
  }
89
90
27262
  ~FSReqWrap() { ReleaseEarly(); }
91
92
  void* operator new(size_t size) = delete;
93
  void* operator new(size_t size, char* storage) { return storage; }
94
27278
  char* inline_data() { return reinterpret_cast<char*>(this + 1); }
95
96
  const char* syscall_;
97
  const char* data_;
98
99
  DISALLOW_COPY_AND_ASSIGN(FSReqWrap);
100
};
101
102
#define ASSERT_PATH(path)                                                   \
103
  if (*path == nullptr)                                                     \
104
    return TYPE_ERROR( #path " must be a string or Buffer");
105
106
13633
FSReqWrap* FSReqWrap::New(Environment* env,
107
                          Local<Object> req,
108
                          const char* syscall,
109
                          const char* data,
110
                          enum encoding encoding,
111
                          Ownership ownership) {
112
13633
  const bool copy = (data != nullptr && ownership == COPY);
113
13633
  const size_t size = copy ? 1 + strlen(data) : 0;
114
  FSReqWrap* that;
115
13633
  char* const storage = new char[sizeof(*that) + size];
116
13633
  that = new(storage) FSReqWrap(env, req, syscall, data, encoding);
117
13633
  if (copy)
118
28
    that->data_ = static_cast<char*>(memcpy(that->inline_data(), data, size));
119
13633
  return that;
120
}
121
122
123
13631
void FSReqWrap::Dispose() {
124
13631
  this->~FSReqWrap();
125
13631
  delete[] reinterpret_cast<char*>(this);
126
13631
}
127
128
129
13642
static void NewFSReqWrap(const FunctionCallbackInfo<Value>& args) {
130
13642
  CHECK(args.IsConstructCall());
131
13642
}
132
133
134
static inline bool IsInt64(double x) {
135
15
  return x == static_cast<double>(static_cast<int64_t>(x));
136
}
137
138
13633
static void After(uv_fs_t *req) {
139
13633
  FSReqWrap* req_wrap = static_cast<FSReqWrap*>(req->data);
140
13633
  CHECK_EQ(&req_wrap->req_, req);
141
13633
  req_wrap->ReleaseEarly();  // Free memory that's no longer used now.
142
143
13633
  Environment* env = req_wrap->env();
144
27264
  HandleScope handle_scope(env->isolate());
145
40897
  Context::Scope context_scope(env->context());
146
147
  // there is always at least one argument. "error"
148
13633
  int argc = 1;
149
150
  // Allocate space for two args. We may only use one depending on the case.
151
  // (Feel free to increase this if you need more)
152
68165
  Local<Value> argv[2];
153
13633
  Local<Value> link;
154
155
13633
  if (req->result < 0) {
156
    // An error happened.
157
    argv[0] = UVException(env->isolate(),
158
                          req->result,
159
                          req_wrap->syscall(),
160
                          nullptr,
161
                          req->path,
162
397
                          req_wrap->data());
163
  } else {
164
    // error value is empty or null for non-error.
165
39708
    argv[0] = Null(env->isolate());
166
167
    // All have at least two args now.
168
13236
    argc = 2;
169
170


13236
    switch (req->fs_type) {
171
      // These all have no data to pass.
172
      case UV_FS_ACCESS:
173
      case UV_FS_CLOSE:
174
      case UV_FS_RENAME:
175
      case UV_FS_UNLINK:
176
      case UV_FS_RMDIR:
177
      case UV_FS_MKDIR:
178
      case UV_FS_FTRUNCATE:
179
      case UV_FS_FSYNC:
180
      case UV_FS_FDATASYNC:
181
      case UV_FS_LINK:
182
      case UV_FS_SYMLINK:
183
      case UV_FS_CHMOD:
184
      case UV_FS_FCHMOD:
185
      case UV_FS_CHOWN:
186
      case UV_FS_FCHOWN:
187
        // These, however, don't.
188
        argc = 1;
189
        break;
190
191
      case UV_FS_UTIME:
192
      case UV_FS_FUTIME:
193
14
        argc = 0;
194
14
        break;
195
196
      case UV_FS_OPEN:
197
726
        argv[1] = Integer::New(env->isolate(), req->result);
198
363
        break;
199
200
      case UV_FS_WRITE:
201
20682
        argv[1] = Integer::New(env->isolate(), req->result);
202
10341
        break;
203
204
      case UV_FS_STAT:
205
      case UV_FS_LSTAT:
206
      case UV_FS_FSTAT:
207
        argv[1] = BuildStatsObject(env,
208
987
                                   static_cast<const uv_stat_t*>(req->ptr));
209
987
        break;
210
211
      case UV_FS_MKDTEMP:
212
        link = StringBytes::Encode(env->isolate(),
213
4
                                   static_cast<const char*>(req->path),
214
8
                                   req_wrap->encoding_);
215
4
        if (link.IsEmpty()) {
216
          argv[0] = UVException(env->isolate(),
217
                                UV_EINVAL,
218
                                req_wrap->syscall(),
219
                                "Invalid character encoding for filename",
220
                                req->path,
221
                                req_wrap->data());
222
        } else {
223
4
          argv[1] = link;
224
        }
225
        break;
226
227
      case UV_FS_READLINK:
228
        link = StringBytes::Encode(env->isolate(),
229
23
                                   static_cast<const char*>(req->ptr),
230
46
                                   req_wrap->encoding_);
231
23
        if (link.IsEmpty()) {
232
          argv[0] = UVException(env->isolate(),
233
                                UV_EINVAL,
234
                                req_wrap->syscall(),
235
                                "Invalid character encoding for link",
236
                                req->path,
237
                                req_wrap->data());
238
        } else {
239
23
          argv[1] = link;
240
        }
241
        break;
242
243
      case UV_FS_REALPATH:
244
        link = StringBytes::Encode(env->isolate(),
245
                                   static_cast<const char*>(req->ptr),
246
                                   req_wrap->encoding_);
247
        if (link.IsEmpty()) {
248
          argv[0] = UVException(env->isolate(),
249
                                UV_EINVAL,
250
                                req_wrap->syscall(),
251
                                "Invalid character encoding for link",
252
                                req->path,
253
                                req_wrap->data());
254
        } else {
255
          argv[1] = link;
256
        }
257
        break;
258
259
      case UV_FS_READ:
260
        // Buffer interface
261
908
        argv[1] = Integer::New(env->isolate(), req->result);
262
454
        break;
263
264
      case UV_FS_SCANDIR:
265
        {
266
          int r;
267
199
          Local<Array> names = Array::New(env->isolate(), 0);
268
199
          Local<Function> fn = env->push_values_to_array_function();
269
3383
          Local<Value> name_argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
270
          size_t name_idx = 0;
271
272
1840
          for (int i = 0; ; i++) {
273
            uv_dirent_t ent;
274
275
2039
            r = uv_fs_scandir_next(req, &ent);
276
2039
            if (r == UV_EOF)
277
              break;
278
1840
            if (r != 0) {
279
              argv[0] = UVException(r,
280
                                    nullptr,
281
                                    req_wrap->syscall(),
282
                                    static_cast<const char*>(req->path));
283
              break;
284
            }
285
286
            Local<Value> filename = StringBytes::Encode(env->isolate(),
287
                                                        ent.name,
288
1840
                                                        req_wrap->encoding_);
289
1840
            if (filename.IsEmpty()) {
290
              argv[0] = UVException(env->isolate(),
291
                                    UV_EINVAL,
292
                                    req_wrap->syscall(),
293
                                    "Invalid character encoding for filename",
294
                                    req->path,
295
                                    req_wrap->data());
296
              break;
297
            }
298
1840
            name_argv[name_idx++] = filename;
299
300
1840
            if (name_idx >= arraysize(name_argv)) {
301
700
              fn->Call(env->context(), names, name_idx, name_argv)
302
175
                  .ToLocalChecked();
303
175
              name_idx = 0;
304
            }
305
          }
306
307
199
          if (name_idx > 0) {
308
796
            fn->Call(env->context(), names, name_idx, name_argv)
309
199
                .ToLocalChecked();
310
          }
311
312
199
          argv[1] = names;
313
        }
314
199
        break;
315
316
      default:
317
        CHECK(0 && "Unhandled eio response");
318
    }
319
  }
320
321
13633
  req_wrap->MakeCallback(env->oncomplete_string(), argc, argv);
322
323
13631
  uv_fs_req_cleanup(&req_wrap->req_);
324
13631
  req_wrap->Dispose();
325
13631
}
326
327
// This struct is only used on sync fs calls.
328
// For async calls FSReqWrap is used.
329
class fs_req_wrap {
330
 public:
331
  fs_req_wrap() {}
332
147600
  ~fs_req_wrap() { uv_fs_req_cleanup(&req); }
333
  uv_fs_t req;
334
335
 private:
336
  DISALLOW_COPY_AND_ASSIGN(fs_req_wrap);
337
};
338
339
340
#define ASYNC_DEST_CALL(func, req, dest, encoding, ...)                       \
341
  Environment* env = Environment::GetCurrent(args);                           \
342
  CHECK(req->IsObject());                                                     \
343
  FSReqWrap* req_wrap = FSReqWrap::New(env, req.As<Object>(),                 \
344
                                       #func, dest, encoding);                \
345
  int err = uv_fs_ ## func(env->event_loop(),                                 \
346
                           &req_wrap->req_,                                   \
347
                           __VA_ARGS__,                                       \
348
                           After);                                            \
349
  req_wrap->Dispatched();                                                     \
350
  if (err < 0) {                                                              \
351
    uv_fs_t* uv_req = &req_wrap->req_;                                        \
352
    uv_req->result = err;                                                     \
353
    uv_req->path = nullptr;                                                   \
354
    After(uv_req);                                                            \
355
    req_wrap = nullptr;                                                       \
356
  } else {                                                                    \
357
    args.GetReturnValue().Set(req_wrap->persistent());                        \
358
  }
359
360
#define ASYNC_CALL(func, req, encoding, ...)                                  \
361
  ASYNC_DEST_CALL(func, req, nullptr, encoding, __VA_ARGS__)                  \
362
363
#define SYNC_DEST_CALL(func, path, dest, ...)                                 \
364
  fs_req_wrap req_wrap;                                                       \
365
  env->PrintSyncTrace();                                                      \
366
  int err = uv_fs_ ## func(env->event_loop(),                                 \
367
                         &req_wrap.req,                                       \
368
                         __VA_ARGS__,                                         \
369
                         nullptr);                                            \
370
  if (err < 0) {                                                              \
371
    return env->ThrowUVException(err, #func, nullptr, path, dest);            \
372
  }                                                                           \
373
374
#define SYNC_CALL(func, path, ...)                                            \
375
  SYNC_DEST_CALL(func, path, nullptr, __VA_ARGS__)                            \
376
377
#define SYNC_REQ req_wrap.req
378
379
#define SYNC_RESULT err
380
381
129
static void Access(const FunctionCallbackInfo<Value>& args) {
382
258
  Environment* env = Environment::GetCurrent(args.GetIsolate());
383
236
  HandleScope scope(env->isolate());
384
385
129
  if (args.Length() < 2)
386
22
    return TYPE_ERROR("path and mode are required");
387
129
  if (!args[1]->IsInt32())
388
    return TYPE_ERROR("mode must be an integer");
389
390
236
  BufferValue path(env->isolate(), args[0]);
391
129
  ASSERT_PATH(path)
392
393
127
  int mode = static_cast<int>(args[1]->Int32Value());
394
395
127
  if (args[2]->IsObject()) {
396

574
    ASYNC_CALL(access, args[2], UTF8, *path, mode);
397
  } else {
398
135
    SYNC_CALL(access, *path, *path, mode);
399
  }
400
}
401
402
403
10201
static void Close(const FunctionCallbackInfo<Value>& args) {
404
10201
  Environment* env = Environment::GetCurrent(args);
405
406
10201
  if (args.Length() < 1)
407
    return TYPE_ERROR("fd is required");
408
10201
  if (!args[0]->IsInt32())
409
    return TYPE_ERROR("fd must be a file descriptor");
410
411
10201
  int fd = args[0]->Int32Value();
412
413
10201
  if (args[1]->IsObject()) {
414

2233
    ASYNC_CALL(close, args[1], UTF8, fd)
415
  } else {
416
29646
    SYNC_CALL(close, 0, fd)
417
  }
418
}
419
420
421
98240
Local<Value> BuildStatsObject(Environment* env, const uv_stat_t* s) {
422
196480
  EscapableHandleScope handle_scope(env->isolate());
423
424
  // If you hit this assertion, you forgot to enter the v8::Context first.
425
294720
  CHECK_EQ(env->context(), env->isolate()->GetCurrentContext());
426
427
  // The code below is very nasty-looking but it prevents a segmentation fault
428
  // when people run JS code like the snippet below. It's apparently more
429
  // common than you would expect, several people have reported this crash...
430
  //
431
  //   function crash() {
432
  //     fs.statSync('.');
433
  //     crash();
434
  //   }
435
  //
436
  // We need to check the return value of Integer::New() and Date::New()
437
  // and make sure that we bail out when V8 returns an empty handle.
438
439
  // Integers.
440
#define X(name)                                                               \
441
  Local<Value> name = Integer::New(env->isolate(), s->st_##name);             \
442
  if (name.IsEmpty())                                                         \
443
    return handle_scope.Escape(Local<Object>());                              \
444
445
196480
  X(dev)
446
196480
  X(mode)
447
196480
  X(nlink)
448
196480
  X(uid)
449
196480
  X(gid)
450
196480
  X(rdev)
451
# if defined(__POSIX__)
452
196480
  X(blksize)
453
# else
454
  Local<Value> blksize = Undefined(env->isolate());
455
# endif
456
#undef X
457
458
  // Numbers.
459
#define X(name)                                                               \
460
  Local<Value> name = Number::New(env->isolate(),                             \
461
                                  static_cast<double>(s->st_##name));         \
462
  if (name.IsEmpty())                                                         \
463
    return handle_scope.Escape(Local<Object>());                              \
464
465
196480
  X(ino)
466
196480
  X(size)
467
# if defined(__POSIX__)
468
196480
  X(blocks)
469
# else
470
  Local<Value> blocks = Undefined(env->isolate());
471
# endif
472
#undef X
473
474
  // Dates.
475
#define X(name)                                                               \
476
  Local<Value> name##_msec =                                                  \
477
    Number::New(env->isolate(),                                               \
478
        (static_cast<double>(s->st_##name.tv_sec) * 1000) +                   \
479
        (static_cast<double>(s->st_##name.tv_nsec / 1000000)));               \
480
                                                                              \
481
  if (name##_msec.IsEmpty())                                                  \
482
    return handle_scope.Escape(Local<Object>());                              \
483
484
196480
  X(atim)
485
196480
  X(mtim)
486
196480
  X(ctim)
487
196480
  X(birthtim)
488
#undef X
489
490
  // Pass stats as the first argument, this is the object we are modifying.
491
  Local<Value> argv[] = {
492
    dev,
493
    mode,
494
    nlink,
495
    uid,
496
    gid,
497
    rdev,
498
    blksize,
499
    ino,
500
    size,
501
    blocks,
502
    atim_msec,
503
    mtim_msec,
504
    ctim_msec,
505
    birthtim_msec
506
98240
  };
507
508
  // Call out to JavaScript to create the stats object.
509
  Local<Value> stats =
510
294720
      env->fs_stats_constructor_function()->NewInstance(
511
          env->context(),
512
98240
          arraysize(argv),
513
589440
          argv).FromMaybe(Local<Value>());
514
515
98240
  if (stats.IsEmpty())
516
3
    return handle_scope.Escape(Local<Object>());
517
518
  return handle_scope.Escape(stats);
519
}
520
521
// Used to speed up module loading.  Returns the contents of the file as
522
// a string or undefined when the file cannot be opened.  The speedup
523
// comes from not creating Error objects on failure.
524
2475
static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) {
525
2475
  Environment* env = Environment::GetCurrent(args);
526
2475
  uv_loop_t* loop = env->event_loop();
527
528
4950
  CHECK(args[0]->IsString());
529
4167
  node::Utf8Value path(env->isolate(), args[0]);
530
531
  uv_fs_t open_req;
532
2475
  const int fd = uv_fs_open(loop, &open_req, *path, O_RDONLY, 0, nullptr);
533
2475
  uv_fs_req_cleanup(&open_req);
534
535
2475
  if (fd < 0) {
536
783
    return;
537
  }
538
539
3384
  std::vector<char> chars;
540
1692
  int64_t offset = 0;
541
1692
  for (;;) {
542
3384
    const size_t kBlockSize = 32 << 10;
543
6768
    const size_t start = chars.size();
544
3384
    chars.resize(start + kBlockSize);
545
546
    uv_buf_t buf;
547
6768
    buf.base = &chars[start];
548
3384
    buf.len = kBlockSize;
549
550
    uv_fs_t read_req;
551
    const ssize_t numchars =
552
3384
        uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr);
553
3384
    uv_fs_req_cleanup(&read_req);
554
555
3384
    CHECK_GE(numchars, 0);
556
3384
    if (static_cast<size_t>(numchars) < kBlockSize) {
557
3384
      chars.resize(start + numchars);
558
    }
559
3384
    if (numchars == 0) {
560
      break;
561
    }
562
1692
    offset += numchars;
563
  }
564
565
  uv_fs_t close_req;
566
1692
  CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr));
567
1692
  uv_fs_req_cleanup(&close_req);
568
569
1692
  size_t start = 0;
570

3384
  if (chars.size() >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) {
571
    start = 3;  // Skip UTF-8 BOM.
572
  }
573
574
  Local<String> chars_string =
575
      String::NewFromUtf8(env->isolate(),
576
3384
                          &chars[start],
577
                          String::kNormalString,
578
3384
                          chars.size() - start);
579
3384
  args.GetReturnValue().Set(chars_string);
580
}
581
582
// Used to speed up module loading.  Returns 0 if the path refers to
583
// a file, 1 when it's a directory or < 0 on error (usually -ENOENT.)
584
// The speedup comes from not creating thousands of Stat and Error objects.
585
21581
static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) {
586
21581
  Environment* env = Environment::GetCurrent(args);
587
588
43162
  CHECK(args[0]->IsString());
589
43162
  node::Utf8Value path(env->isolate(), args[0]);
590
591
  uv_fs_t req;
592
43162
  int rc = uv_fs_stat(env->event_loop(), &req, *path, nullptr);
593
21581
  if (rc == 0) {
594
12111
    const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
595
12111
    rc = !!(s->st_mode & S_IFDIR);
596
  }
597
21581
  uv_fs_req_cleanup(&req);
598
599
43162
  args.GetReturnValue().Set(rc);
600
21581
}
601
602
21459
static void Stat(const FunctionCallbackInfo<Value>& args) {
603
21459
  Environment* env = Environment::GetCurrent(args);
604
605
21459
  if (args.Length() < 1)
606
3
    return TYPE_ERROR("path required");
607
608
42915
  BufferValue path(env->isolate(), args[0]);
609
21459
  ASSERT_PATH(path)
610
611
21459
  if (args[1]->IsObject()) {
612

1064
    ASYNC_CALL(stat, args[1], UTF8, *path)
613
  } else {
614
63921
    SYNC_CALL(stat, *path, *path)
615
63912
    args.GetReturnValue().Set(
616
42608
        BuildStatsObject(env, static_cast<const uv_stat_t*>(SYNC_REQ.ptr)));
617
  }
618
}
619
620
68712
static void LStat(const FunctionCallbackInfo<Value>& args) {
621
68712
  Environment* env = Environment::GetCurrent(args);
622
623
68712
  if (args.Length() < 1)
624
1
    return TYPE_ERROR("path required");
625
626
137423
  BufferValue path(env->isolate(), args[0]);
627
68712
  ASSERT_PATH(path)
628
629
68712
  if (args[1]->IsObject()) {
630

6041
    ASYNC_CALL(lstat, args[1], UTF8, *path)
631
  } else {
632
203547
    SYNC_CALL(lstat, *path, *path)
633
203544
    args.GetReturnValue().Set(
634
135696
        BuildStatsObject(env, static_cast<const uv_stat_t*>(SYNC_REQ.ptr)));
635
  }
636
}
637
638
8180
static void FStat(const FunctionCallbackInfo<Value>& args) {
639
8180
  Environment* env = Environment::GetCurrent(args);
640
641
8180
  if (args.Length() < 1)
642
    return TYPE_ERROR("fd is required");
643
8180
  if (!args[0]->IsInt32())
644
    return TYPE_ERROR("fd must be a file descriptor");
645
646
8180
  int fd = args[0]->Int32Value();
647
648
8180
  if (args[1]->IsObject()) {
649

595
    ASYNC_CALL(fstat, args[1], UTF8, fd)
650
  } else {
651
24285
    SYNC_CALL(fstat, 0, fd)
652
24285
    args.GetReturnValue().Set(
653
16190
        BuildStatsObject(env, static_cast<const uv_stat_t*>(SYNC_REQ.ptr)));
654
  }
655
}
656
657
34
static void Symlink(const FunctionCallbackInfo<Value>& args) {
658
34
  Environment* env = Environment::GetCurrent(args);
659
660
34
  int len = args.Length();
661
34
  if (len < 1)
662
1
    return TYPE_ERROR("target path required");
663
34
  if (len < 2)
664
    return TYPE_ERROR("src path required");
665
666
67
  BufferValue target(env->isolate(), args[0]);
667
34
  ASSERT_PATH(target)
668
67
  BufferValue path(env->isolate(), args[1]);
669
34
  ASSERT_PATH(path)
670
671
34
  int flags = 0;
672
673
68
  if (args[2]->IsString()) {
674
38
    node::Utf8Value mode(env->isolate(), args[2]);
675
19
    if (strcmp(*mode, "dir") == 0) {
676
      flags |= UV_FS_SYMLINK_DIR;
677
9
    } else if (strcmp(*mode, "junction") == 0) {
678
      flags |= UV_FS_SYMLINK_JUNCTION;
679
6
    } else if (strcmp(*mode, "file") != 0) {
680
      return env->ThrowError("Unknown symlink type");
681
    }
682
  }
683
684
34
  if (args[3]->IsObject()) {
685

28
    ASYNC_DEST_CALL(symlink, args[3], *path, UTF8, *target, *path, flags)
686
  } else {
687
90
    SYNC_DEST_CALL(symlink, *target, *path, *target, *path, flags)
688
  }
689
}
690
691
7
static void Link(const FunctionCallbackInfo<Value>& args) {
692
7
  Environment* env = Environment::GetCurrent(args);
693
694
7
  int len = args.Length();
695
7
  if (len < 1)
696
4
    return TYPE_ERROR("src path required");
697
7
  if (len < 2)
698
    return TYPE_ERROR("dest path required");
699
700
10
  BufferValue src(env->isolate(), args[0]);
701
7
  ASSERT_PATH(src)
702
703
9
  BufferValue dest(env->isolate(), args[1]);
704
6
  ASSERT_PATH(dest)
705
706
5
  if (args[2]->IsObject()) {
707

21
    ASYNC_DEST_CALL(link, args[2], *dest, UTF8, *src, *dest)
708
  } else {
709
6
    SYNC_DEST_CALL(link, *src, *dest, *src, *dest)
710
  }
711
}
712
713
52
static void ReadLink(const FunctionCallbackInfo<Value>& args) {
714
52
  Environment* env = Environment::GetCurrent(args);
715
716
52
  const int argc = args.Length();
717
718
52
  if (argc < 1)
719
1
    return TYPE_ERROR("path required");
720
721
103
  BufferValue path(env->isolate(), args[0]);
722
52
  ASSERT_PATH(path)
723
724
52
  const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8);
725
726
156
  Local<Value> callback = Null(env->isolate());
727
52
  if (argc == 3)
728
    callback = args[2];
729
730
52
  if (callback->IsObject()) {
731

120
    ASYNC_CALL(readlink, callback, encoding, *path)
732
  } else {
733
84
    SYNC_CALL(readlink, *path, *path)
734
27
    const char* link_path = static_cast<const char*>(SYNC_REQ.ptr);
735
    Local<Value> rc = StringBytes::Encode(env->isolate(),
736
                                          link_path,
737
27
                                          encoding);
738
27
    if (rc.IsEmpty()) {
739
      return env->ThrowUVException(UV_EINVAL,
740
                                   "readlink",
741
                                   "Invalid character encoding for link",
742
                                   *path);
743
    }
744
54
    args.GetReturnValue().Set(rc);
745
  }
746
}
747
748
9
static void Rename(const FunctionCallbackInfo<Value>& args) {
749
9
  Environment* env = Environment::GetCurrent(args);
750
751
9
  int len = args.Length();
752
9
  if (len < 1)
753
2
    return TYPE_ERROR("old path required");
754
9
  if (len < 2)
755
    return TYPE_ERROR("new path required");
756
757
16
  BufferValue old_path(env->isolate(), args[0]);
758
9
  ASSERT_PATH(old_path)
759
16
  BufferValue new_path(env->isolate(), args[1]);
760
9
  ASSERT_PATH(new_path)
761
762
9
  if (args[2]->IsObject()) {
763

49
    ASYNC_DEST_CALL(rename, args[2], *new_path, UTF8, *old_path, *new_path)
764
  } else {
765
6
    SYNC_DEST_CALL(rename, *old_path, *new_path, *old_path, *new_path)
766
  }
767
}
768
769
15
static void FTruncate(const FunctionCallbackInfo<Value>& args) {
770
15
  Environment* env = Environment::GetCurrent(args);
771
772
15
  if (args.Length() < 2)
773
    return TYPE_ERROR("fd and length are required");
774
15
  if (!args[0]->IsInt32())
775
    return TYPE_ERROR("fd must be a file descriptor");
776
777
15
  int fd = args[0]->Int32Value();
778
779
  // FIXME(bnoordhuis) It's questionable to reject non-ints here but still
780
  // allow implicit coercion from null or undefined to zero.  Probably best
781
  // handled in lib/fs.js.
782
15
  Local<Value> len_v(args[1]);
783

60
  if (!len_v->IsUndefined() &&
784

45
      !len_v->IsNull() &&
785
30
      !IsInt64(len_v->NumberValue())) {
786
    return env->ThrowTypeError("Not an integer");
787
  }
788
789
15
  const int64_t len = len_v->IntegerValue();
790
791
15
  if (args[2]->IsObject()) {
792

56
    ASYNC_CALL(ftruncate, args[2], UTF8, fd, len)
793
  } else {
794
21
    SYNC_CALL(ftruncate, 0, fd, len)
795
  }
796
}
797
798
2
static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
799
2
  Environment* env = Environment::GetCurrent(args);
800
801
2
  if (args.Length() < 1)
802
    return TYPE_ERROR("fd is required");
803
2
  if (!args[0]->IsInt32())
804
    return TYPE_ERROR("fd must be a file descriptor");
805
806
2
  int fd = args[0]->Int32Value();
807
808
2
  if (args[1]->IsObject()) {
809

7
    ASYNC_CALL(fdatasync, args[1], UTF8, fd)
810
  } else {
811
3
    SYNC_CALL(fdatasync, 0, fd)
812
  }
813
}
814
815
14
static void Fsync(const FunctionCallbackInfo<Value>& args) {
816
14
  Environment* env = Environment::GetCurrent(args);
817
818
14
  if (args.Length() < 1)
819
    return TYPE_ERROR("fd is required");
820
14
  if (!args[0]->IsInt32())
821
    return TYPE_ERROR("fd must be a file descriptor");
822
823
14
  int fd = args[0]->Int32Value();
824
825
14
  if (args[1]->IsObject()) {
826

7
    ASYNC_CALL(fsync, args[1], UTF8, fd)
827
  } else {
828
39
    SYNC_CALL(fsync, 0, fd)
829
  }
830
}
831
832
414
static void Unlink(const FunctionCallbackInfo<Value>& args) {
833
414
  Environment* env = Environment::GetCurrent(args);
834
835
414
  if (args.Length() < 1)
836
32
    return TYPE_ERROR("path required");
837
838
796
  BufferValue path(env->isolate(), args[0]);
839
414
  ASSERT_PATH(path)
840
841
414
  if (args[1]->IsObject()) {
842

1673
    ASYNC_CALL(unlink, args[1], UTF8, *path)
843
  } else {
844
525
    SYNC_CALL(unlink, *path, *path)
845
  }
846
}
847
848
791
static void RMDir(const FunctionCallbackInfo<Value>& args) {
849
791
  Environment* env = Environment::GetCurrent(args);
850
851
791
  if (args.Length() < 1)
852
182
    return TYPE_ERROR("path required");
853
854
1400
  BufferValue path(env->isolate(), args[0]);
855
791
  ASSERT_PATH(path)
856
857
791
  if (args[1]->IsObject()) {
858

2604
    ASYNC_CALL(rmdir, args[1], UTF8, *path)
859
  } else {
860
1257
    SYNC_CALL(rmdir, *path, *path)
861
  }
862
}
863
864
1924
static void MKDir(const FunctionCallbackInfo<Value>& args) {
865
1924
  Environment* env = Environment::GetCurrent(args);
866
867
1924
  if (args.Length() < 2)
868
1700
    return TYPE_ERROR("path and mode are required");
869
1924
  if (!args[1]->IsInt32())
870
    return TYPE_ERROR("mode must be an integer");
871
872
2148
  BufferValue path(env->isolate(), args[0]);
873
1924
  ASSERT_PATH(path)
874
875
1924
  int mode = static_cast<int>(args[1]->Int32Value());
876
877
1924
  if (args[2]->IsObject()) {
878

294
    ASYNC_CALL(mkdir, args[2], UTF8, *path, mode)
879
  } else {
880
5646
    SYNC_CALL(mkdir, *path, *path, mode)
881
  }
882
}
883
884
static void RealPath(const FunctionCallbackInfo<Value>& args) {
885
  Environment* env = Environment::GetCurrent(args);
886
887
  const int argc = args.Length();
888
889
  if (argc < 1)
890
    return TYPE_ERROR("path required");
891
892
  BufferValue path(env->isolate(), args[0]);
893
  ASSERT_PATH(path)
894
895
  const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8);
896
897
  Local<Value> callback = Null(env->isolate());
898
  if (argc == 3)
899
    callback = args[2];
900
901
  if (callback->IsObject()) {
902
    ASYNC_CALL(realpath, callback, encoding, *path);
903
  } else {
904
    SYNC_CALL(realpath, *path, *path);
905
    const char* link_path = static_cast<const char*>(SYNC_REQ.ptr);
906
    Local<Value> rc = StringBytes::Encode(env->isolate(),
907
                                          link_path,
908
                                          encoding);
909
    if (rc.IsEmpty()) {
910
      return env->ThrowUVException(UV_EINVAL,
911
                                   "realpath",
912
                                   "Invalid character encoding for path",
913
                                   *path);
914
    }
915
    args.GetReturnValue().Set(rc);
916
  }
917
}
918
919
614
static void ReadDir(const FunctionCallbackInfo<Value>& args) {
920
614
  Environment* env = Environment::GetCurrent(args);
921
922
614
  const int argc = args.Length();
923
924
614
  if (argc < 1)
925
20
    return TYPE_ERROR("path required");
926
927
1208
  BufferValue path(env->isolate(), args[0]);
928
614
  ASSERT_PATH(path)
929
930
614
  const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8);
931
932
1842
  Local<Value> callback = Null(env->isolate());
933
614
  if (argc == 3)
934
    callback = args[2];
935
936
614
  if (callback->IsObject()) {
937

1010
    ASYNC_CALL(scandir, callback, encoding, *path, 0 /*flags*/)
938
  } else {
939
1236
    SYNC_CALL(scandir, *path, *path, 0 /*flags*/)
940
941
392
    CHECK_GE(SYNC_REQ.result, 0);
942
    int r;
943
392
    Local<Array> names = Array::New(env->isolate(), 0);
944
392
    Local<Function> fn = env->push_values_to_array_function();
945
6664
    Local<Value> name_v[NODE_PUSH_VAL_TO_ARRAY_MAX];
946
    size_t name_idx = 0;
947
948
3272
    for (int i = 0; ; i++) {
949
      uv_dirent_t ent;
950
951
3664
      r = uv_fs_scandir_next(&SYNC_REQ, &ent);
952
3664
      if (r == UV_EOF)
953
        break;
954
3272
      if (r != 0)
955
        return env->ThrowUVException(r, "readdir", "", *path);
956
957
      Local<Value> filename = StringBytes::Encode(env->isolate(),
958
                                                  ent.name,
959
3272
                                                  encoding);
960
3272
      if (filename.IsEmpty()) {
961
        return env->ThrowUVException(UV_EINVAL,
962
                                     "readdir",
963
                                     "Invalid character encoding for filename",
964
                                     *path);
965
      }
966
967
3272
      name_v[name_idx++] = filename;
968
969
3272
      if (name_idx >= arraysize(name_v)) {
970
1088
        fn->Call(env->context(), names, name_idx, name_v)
971
272
            .ToLocalChecked();
972
272
        name_idx = 0;
973
      }
974
    }
975
976
392
    if (name_idx > 0) {
977
1492
      fn->Call(env->context(), names, name_idx, name_v).ToLocalChecked();
978
    }
979
980
784
    args.GetReturnValue().Set(names);
981
  }
982
}
983
984
10599
static void Open(const FunctionCallbackInfo<Value>& args) {
985
10599
  Environment* env = Environment::GetCurrent(args);
986
987
10599
  int len = args.Length();
988
10599
  if (len < 1)
989
4
    return TYPE_ERROR("path required");
990
10599
  if (len < 2)
991
    return TYPE_ERROR("flags required");
992
10599
  if (len < 3)
993
    return TYPE_ERROR("mode required");
994
10599
  if (!args[1]->IsInt32())
995
    return TYPE_ERROR("flags must be an int");
996
10599
  if (!args[2]->IsInt32())
997
    return TYPE_ERROR("mode must be an int");
998
999
21194
  BufferValue path(env->isolate(), args[0]);
1000
10599
  ASSERT_PATH(path)
1001
1002
10599
  int flags = args[1]->Int32Value();
1003
10599
  int mode = static_cast<int>(args[2]->Int32Value());
1004
1005
10599
  if (args[3]->IsObject()) {
1006

2765
    ASYNC_CALL(open, args[3], UTF8, *path, flags, mode)
1007
  } else {
1008
30612
    SYNC_CALL(open, *path, *path, flags, mode)
1009
20400
    args.GetReturnValue().Set(SYNC_RESULT);
1010
  }
1011
}
1012
1013
1014
// Wrapper for write(2).
1015
//
1016
// bytesWritten = write(fd, buffer, offset, length, position, callback)
1017
// 0 fd        integer. file descriptor
1018
// 1 buffer    the data to write
1019
// 2 offset    where in the buffer to start from
1020
// 3 length    how much to write
1021
// 4 position  if integer, position to write at in the file.
1022
//             if null, write from the current position
1023
28614
static void WriteBuffer(const FunctionCallbackInfo<Value>& args) {
1024
28614
  Environment* env = Environment::GetCurrent(args);
1025
1026
28614
  if (!args[0]->IsInt32())
1027
10316
    return env->ThrowTypeError("First argument must be file descriptor");
1028
1029
28613
  CHECK(Buffer::HasInstance(args[1]));
1030
1031
28613
  int fd = args[0]->Int32Value();
1032
57226
  Local<Object> obj = args[1].As<Object>();
1033
28613
  const char* buf = Buffer::Data(obj);
1034
28613
  size_t buffer_length = Buffer::Length(obj);
1035
28613
  size_t off = args[2]->Uint32Value();
1036
28613
  size_t len = args[3]->Uint32Value();
1037
30321
  int64_t pos = GET_OFFSET(args[4]);
1038
28613
  Local<Value> req = args[5];
1039
1040
28613
  if (off > buffer_length)
1041
    return env->ThrowRangeError("offset out of bounds");
1042
28613
  if (len > buffer_length)
1043
    return env->ThrowRangeError("length out of bounds");
1044
28613
  if (off + len < off)
1045
    return env->ThrowRangeError("off + len overflow");
1046
28613
  if (!Buffer::IsWithinBounds(off, len, buffer_length))
1047
    return env->ThrowRangeError("off + len > buffer.length");
1048
1049
28613
  buf += off;
1050
1051
28613
  uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);
1052
1053
28613
  if (req->IsObject()) {
1054

51575
    ASYNC_CALL(write, req, UTF8, fd, &uvbuf, 1, pos)
1055
    return;
1056
  }
1057
1058
54894
  SYNC_CALL(write, nullptr, fd, &uvbuf, 1, pos)
1059
36596
  args.GetReturnValue().Set(SYNC_RESULT);
1060
}
1061
1062
1063
// Wrapper for writev(2).
1064
//
1065
// bytesWritten = writev(fd, chunks, position, callback)
1066
// 0 fd        integer. file descriptor
1067
// 1 chunks    array of buffers to write
1068
// 2 position  if integer, position to write at in the file.
1069
//             if null, write from the current position
1070
4
static void WriteBuffers(const FunctionCallbackInfo<Value>& args) {
1071
4
  Environment* env = Environment::GetCurrent(args);
1072
1073
4
  CHECK(args[0]->IsInt32());
1074
4
  CHECK(args[1]->IsArray());
1075
1076
4
  int fd = args[0]->Int32Value();
1077
8
  Local<Array> chunks = args[1].As<Array>();
1078
4
  int64_t pos = GET_OFFSET(args[2]);
1079
4
  Local<Value> req = args[3];
1080
1081
8
  MaybeStackBuffer<uv_buf_t> iovs(chunks->Length());
1082
1083
424
  for (uint32_t i = 0; i < iovs.length(); i++) {
1084
420
    Local<Value> chunk = chunks->Get(i);
1085
1086
420
    if (!Buffer::HasInstance(chunk))
1087
      return env->ThrowTypeError("Array elements all need to be buffers");
1088
1089
840
    iovs[i] = uv_buf_init(Buffer::Data(chunk), Buffer::Length(chunk));
1090
  }
1091
1092
4
  if (req->IsObject()) {
1093

20
    ASYNC_CALL(write, req, UTF8, fd, *iovs, iovs.length(), pos)
1094
    return;
1095
  }
1096
1097
  SYNC_CALL(write, nullptr, fd, *iovs, iovs.length(), pos)
1098
  args.GetReturnValue().Set(SYNC_RESULT);
1099
}
1100
1101
1102
// Wrapper for write(2).
1103
//
1104
// bytesWritten = write(fd, string, position, enc, callback)
1105
// 0 fd        integer. file descriptor
1106
// 1 string    non-buffer values are converted to strings
1107
// 2 position  if integer, position to write at in the file.
1108
//             if null, write from the current position
1109
// 3 enc       encoding of string
1110
493
static void WriteString(const FunctionCallbackInfo<Value>& args) {
1111
493
  Environment* env = Environment::GetCurrent(args);
1112
1113
493
  if (!args[0]->IsInt32())
1114
    return env->ThrowTypeError("First argument must be file descriptor");
1115
1116
492
  Local<Value> req;
1117
492
  Local<Value> string = args[1];
1118
492
  int fd = args[0]->Int32Value();
1119
492
  char* buf = nullptr;
1120
  int64_t pos;
1121
  size_t len;
1122
492
  FSReqWrap::Ownership ownership = FSReqWrap::COPY;
1123
1124
  // will assign buf and len if string was external
1125
492
  if (!StringBytes::GetExternalParts(env->isolate(),
1126
                                     string,
1127
                                     const_cast<const char**>(&buf),
1128
                                     &len)) {
1129
492
    enum encoding enc = ParseEncoding(env->isolate(), args[3], UTF8);
1130
492
    len = StringBytes::StorageSize(env->isolate(), string, enc);
1131
492
    buf = new char[len];
1132
    // StorageSize may return too large a char, so correct the actual length
1133
    // by the write size
1134
492
    len = StringBytes::Write(env->isolate(), buf, len, args[1], enc);
1135
492
    ownership = FSReqWrap::MOVE;
1136
  }
1137
514
  pos = GET_OFFSET(args[2]);
1138
492
  req = args[4];
1139
1140
492
  uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);
1141
1142
492
  if (!req->IsObject()) {
1143
    // SYNC_CALL returns on error.  Make sure to always free the memory.
1144
    struct Delete {
1145
470
      inline explicit Delete(char* pointer) : pointer_(pointer) {}
1146
470
      inline ~Delete() { delete[] pointer_; }
1147
      char* const pointer_;
1148
    };
1149
1410
    Delete delete_on_return(ownership == FSReqWrap::MOVE ? buf : nullptr);
1150
1410
    SYNC_CALL(write, nullptr, fd, &uvbuf, 1, pos)
1151
940
    return args.GetReturnValue().Set(SYNC_RESULT);
1152
  }
1153
1154
  FSReqWrap* req_wrap =
1155
44
      FSReqWrap::New(env, req.As<Object>(), "write", buf, UTF8, ownership);
1156
44
  int err = uv_fs_write(env->event_loop(),
1157
                        &req_wrap->req_,
1158
                        fd,
1159
                        &uvbuf,
1160
                        1,
1161
                        pos,
1162
22
                        After);
1163
44
  req_wrap->Dispatched();
1164
22
  if (err < 0) {
1165
    uv_fs_t* uv_req = &req_wrap->req_;
1166
    uv_req->result = err;
1167
    uv_req->path = nullptr;
1168
    After(uv_req);
1169
    return;
1170
  }
1171
1172
44
  return args.GetReturnValue().Set(req_wrap->persistent());
1173
}
1174
1175
1176
/*
1177
 * Wrapper for read(2).
1178
 *
1179
 * bytesRead = fs.read(fd, buffer, offset, length, position)
1180
 *
1181
 * 0 fd        integer. file descriptor
1182
 * 1 buffer    instance of Buffer
1183
 * 2 offset    integer. offset to start reading into inside buffer
1184
 * 3 length    integer. length to read
1185
 * 4 position  file position - null for current position
1186
 *
1187
 */
1188
8903
static void Read(const FunctionCallbackInfo<Value>& args) {
1189
8903
  Environment* env = Environment::GetCurrent(args);
1190
1191
8903
  if (args.Length() < 2)
1192
    return TYPE_ERROR("fd and buffer are required");
1193
8903
  if (!args[0]->IsInt32())
1194
    return TYPE_ERROR("fd must be a file descriptor");
1195
8903
  if (!Buffer::HasInstance(args[1]))
1196
    return TYPE_ERROR("Second argument needs to be a buffer");
1197
1198
8903
  int fd = args[0]->Int32Value();
1199
1200
8903
  Local<Value> req;
1201
1202
  size_t len;
1203
  int64_t pos;
1204
1205
8903
  char * buf = nullptr;
1206
1207
17806
  Local<Object> buffer_obj = args[1]->ToObject(env->isolate());
1208
8903
  char *buffer_data = Buffer::Data(buffer_obj);
1209
8903
  size_t buffer_length = Buffer::Length(buffer_obj);
1210
1211
8903
  size_t off = args[2]->Int32Value();
1212
8903
  if (off >= buffer_length) {
1213
    return env->ThrowError("Offset is out of bounds");
1214
  }
1215
1216
8903
  len = args[3]->Int32Value();
1217
8903
  if (!Buffer::IsWithinBounds(off, len, buffer_length))
1218
    return env->ThrowRangeError("Length extends beyond buffer");
1219
1220
9132
  pos = GET_OFFSET(args[4]);
1221
1222
8903
  buf = buffer_data + off;
1223
1224
8903
  uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);
1225
1226
8903
  req = args[5];
1227
1228
8903
  if (req->IsObject()) {
1229

2290
    ASYNC_CALL(read, req, UTF8, fd, &uvbuf, 1, pos);
1230
  } else {
1231
25335
    SYNC_CALL(read, 0, fd, &uvbuf, 1, pos)
1232
16890
    args.GetReturnValue().Set(SYNC_RESULT);
1233
  }
1234
}
1235
1236
1237
/* fs.chmod(path, mode);
1238
 * Wrapper for chmod(1) / EIO_CHMOD
1239
 */
1240
8
static void Chmod(const FunctionCallbackInfo<Value>& args) {
1241
8
  Environment* env = Environment::GetCurrent(args);
1242
1243
8
  if (args.Length() < 2)
1244
1
    return TYPE_ERROR("path and mode are required");
1245
8
  if (!args[1]->IsInt32())
1246
    return TYPE_ERROR("mode must be an integer");
1247
1248
15
  BufferValue path(env->isolate(), args[0]);
1249
8
  ASSERT_PATH(path)
1250
1251
8
  int mode = static_cast<int>(args[1]->Int32Value());
1252
1253
8
  if (args[2]->IsObject()) {
1254

14
    ASYNC_CALL(chmod, args[2], UTF8, *path, mode);
1255
  } else {
1256
18
    SYNC_CALL(chmod, *path, *path, mode);
1257
  }
1258
}
1259
1260
1261
/* fs.fchmod(fd, mode);
1262
 * Wrapper for fchmod(1) / EIO_FCHMOD
1263
 */
1264
2
static void FChmod(const FunctionCallbackInfo<Value>& args) {
1265
2
  Environment* env = Environment::GetCurrent(args);
1266
1267
2
  if (args.Length() < 2)
1268
    return TYPE_ERROR("fd and mode are required");
1269
2
  if (!args[0]->IsInt32())
1270
    return TYPE_ERROR("fd must be a file descriptor");
1271
2
  if (!args[1]->IsInt32())
1272
    return TYPE_ERROR("mode must be an integer");
1273
1274
2
  int fd = args[0]->Int32Value();
1275
2
  int mode = static_cast<int>(args[1]->Int32Value());
1276
1277
2
  if (args[2]->IsObject()) {
1278

7
    ASYNC_CALL(fchmod, args[2], UTF8, fd, mode);
1279
  } else {
1280
3
    SYNC_CALL(fchmod, 0, fd, mode);
1281
  }
1282
}
1283
1284
1285
/* fs.chown(path, uid, gid);
1286
 * Wrapper for chown(1) / EIO_CHOWN
1287
 */
1288
2
static void Chown(const FunctionCallbackInfo<Value>& args) {
1289
2
  Environment* env = Environment::GetCurrent(args);
1290
1291
2
  int len = args.Length();
1292
2
  if (len < 1)
1293
    return TYPE_ERROR("path required");
1294
2
  if (len < 2)
1295
    return TYPE_ERROR("uid required");
1296
2
  if (len < 3)
1297
    return TYPE_ERROR("gid required");
1298
2
  if (!args[1]->IsUint32())
1299
    return TYPE_ERROR("uid must be an unsigned int");
1300
2
  if (!args[2]->IsUint32())
1301
    return TYPE_ERROR("gid must be an unsigned int");
1302
1303
4
  BufferValue path(env->isolate(), args[0]);
1304
2
  ASSERT_PATH(path)
1305
1306
2
  uv_uid_t uid = static_cast<uv_uid_t>(args[1]->Uint32Value());
1307
2
  uv_gid_t gid = static_cast<uv_gid_t>(args[2]->Uint32Value());
1308
1309
2
  if (args[3]->IsObject()) {
1310

14
    ASYNC_CALL(chown, args[3], UTF8, *path, uid, gid);
1311
  } else {
1312
    SYNC_CALL(chown, *path, *path, uid, gid);
1313
  }
1314
}
1315
1316
1317
/* fs.fchown(fd, uid, gid);
1318
 * Wrapper for fchown(1) / EIO_FCHOWN
1319
 */
1320
static void FChown(const FunctionCallbackInfo<Value>& args) {
1321
  Environment* env = Environment::GetCurrent(args);
1322
1323
  int len = args.Length();
1324
  if (len < 1)
1325
    return TYPE_ERROR("fd required");
1326
  if (len < 2)
1327
    return TYPE_ERROR("uid required");
1328
  if (len < 3)
1329
    return TYPE_ERROR("gid required");
1330
  if (!args[0]->IsInt32())
1331
    return TYPE_ERROR("fd must be an int");
1332
  if (!args[1]->IsUint32())
1333
    return TYPE_ERROR("uid must be an unsigned int");
1334
  if (!args[2]->IsUint32())
1335
    return TYPE_ERROR("gid must be an unsigned int");
1336
1337
  int fd = args[0]->Int32Value();
1338
  uv_uid_t uid = static_cast<uv_uid_t>(args[1]->Uint32Value());
1339
  uv_gid_t gid = static_cast<uv_gid_t>(args[2]->Uint32Value());
1340
1341
  if (args[3]->IsObject()) {
1342
    ASYNC_CALL(fchown, args[3], UTF8, fd, uid, gid);
1343
  } else {
1344
    SYNC_CALL(fchown, 0, fd, uid, gid);
1345
  }
1346
}
1347
1348
1349
26
static void UTimes(const FunctionCallbackInfo<Value>& args) {
1350
26
  Environment* env = Environment::GetCurrent(args);
1351
1352
26
  int len = args.Length();
1353
26
  if (len < 1)
1354
6
    return TYPE_ERROR("path required");
1355
26
  if (len < 2)
1356
    return TYPE_ERROR("atime required");
1357
26
  if (len < 3)
1358
    return TYPE_ERROR("mtime required");
1359
26
  if (!args[1]->IsNumber())
1360
    return TYPE_ERROR("atime must be a number");
1361
26
  if (!args[2]->IsNumber())
1362
    return TYPE_ERROR("mtime must be a number");
1363
1364
46
  BufferValue path(env->isolate(), args[0]);
1365
26
  ASSERT_PATH(path)
1366
1367
26
  const double atime = static_cast<double>(args[1]->NumberValue());
1368
26
  const double mtime = static_cast<double>(args[2]->NumberValue());
1369
1370
26
  if (args[3]->IsObject()) {
1371

98
    ASYNC_CALL(utime, args[3], UTF8, *path, atime, mtime);
1372
  } else {
1373
36
    SYNC_CALL(utime, *path, *path, atime, mtime);
1374
  }
1375
}
1376
1377
24
static void FUTimes(const FunctionCallbackInfo<Value>& args) {
1378
24
  Environment* env = Environment::GetCurrent(args);
1379
1380
24
  int len = args.Length();
1381
24
  if (len < 1)
1382
    return TYPE_ERROR("fd required");
1383
24
  if (len < 2)
1384
    return TYPE_ERROR("atime required");
1385
24
  if (len < 3)
1386
    return TYPE_ERROR("mtime required");
1387
24
  if (!args[0]->IsInt32())
1388
    return TYPE_ERROR("fd must be an int");
1389
24
  if (!args[1]->IsNumber())
1390
    return TYPE_ERROR("atime must be a number");
1391
24
  if (!args[2]->IsNumber())
1392
    return TYPE_ERROR("mtime must be a number");
1393
1394
24
  const int fd = args[0]->Int32Value();
1395
24
  const double atime = static_cast<double>(args[1]->NumberValue());
1396
24
  const double mtime = static_cast<double>(args[2]->NumberValue());
1397
1398
24
  if (args[3]->IsObject()) {
1399

84
    ASYNC_CALL(futime, args[3], UTF8, fd, atime, mtime);
1400
  } else {
1401
36
    SYNC_CALL(futime, 0, fd, atime, mtime);
1402
  }
1403
}
1404
1405
7
static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
1406
7
  Environment* env = Environment::GetCurrent(args);
1407
1408
7
  CHECK_GE(args.Length(), 2);
1409
1410
14
  BufferValue tmpl(env->isolate(), args[0]);
1411
7
  if (*tmpl == nullptr)
1412
    return TYPE_ERROR("template must be a string or Buffer");
1413
1414
7
  const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8);
1415
1416
7
  if (args[2]->IsObject()) {
1417

28
    ASYNC_CALL(mkdtemp, args[2], encoding, *tmpl);
1418
  } else {
1419
9
    SYNC_CALL(mkdtemp, *tmpl, *tmpl);
1420
3
    const char* path = static_cast<const char*>(SYNC_REQ.path);
1421
3
    Local<Value> rc = StringBytes::Encode(env->isolate(), path, encoding);
1422
3
    if (rc.IsEmpty()) {
1423
      return env->ThrowUVException(UV_EINVAL,
1424
                                   "mkdtemp",
1425
                                   "Invalid character encoding for filename",
1426
                                   *tmpl);
1427
    }
1428
6
    args.GetReturnValue().Set(rc);
1429
  }
1430
}
1431
1432
1626
void FSInitialize(const FunctionCallbackInfo<Value>& args) {
1433
3252
  Local<Function> stats_constructor = args[0].As<Function>();
1434
1626
  CHECK(stats_constructor->IsFunction());
1435
1436
1626
  Environment* env = Environment::GetCurrent(args);
1437
1626
  env->set_fs_stats_constructor_function(stats_constructor);
1438
1626
}
1439
1440
1626
void InitFs(Local<Object> target,
1441
            Local<Value> unused,
1442
            Local<Context> context,
1443
            void* priv) {
1444
1626
  Environment* env = Environment::GetCurrent(context);
1445
1446
  // Function which creates a new Stats object.
1447
6504
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "FSInitialize"),
1448
4878
              env->NewFunctionTemplate(FSInitialize)->GetFunction());
1449
1450
1626
  env->SetMethod(target, "access", Access);
1451
1626
  env->SetMethod(target, "close", Close);
1452
1626
  env->SetMethod(target, "open", Open);
1453
1626
  env->SetMethod(target, "read", Read);
1454
1626
  env->SetMethod(target, "fdatasync", Fdatasync);
1455
1626
  env->SetMethod(target, "fsync", Fsync);
1456
1626
  env->SetMethod(target, "rename", Rename);
1457
1626
  env->SetMethod(target, "ftruncate", FTruncate);
1458
1626
  env->SetMethod(target, "rmdir", RMDir);
1459
1626
  env->SetMethod(target, "mkdir", MKDir);
1460
1626
  env->SetMethod(target, "readdir", ReadDir);
1461
1626
  env->SetMethod(target, "internalModuleReadFile", InternalModuleReadFile);
1462
1626
  env->SetMethod(target, "internalModuleStat", InternalModuleStat);
1463
1626
  env->SetMethod(target, "stat", Stat);
1464
1626
  env->SetMethod(target, "lstat", LStat);
1465
1626
  env->SetMethod(target, "fstat", FStat);
1466
1626
  env->SetMethod(target, "link", Link);
1467
1626
  env->SetMethod(target, "symlink", Symlink);
1468
1626
  env->SetMethod(target, "readlink", ReadLink);
1469
1626
  env->SetMethod(target, "unlink", Unlink);
1470
1626
  env->SetMethod(target, "writeBuffer", WriteBuffer);
1471
1626
  env->SetMethod(target, "writeBuffers", WriteBuffers);
1472
1626
  env->SetMethod(target, "writeString", WriteString);
1473
1626
  env->SetMethod(target, "realpath", RealPath);
1474
1475
1626
  env->SetMethod(target, "chmod", Chmod);
1476
1626
  env->SetMethod(target, "fchmod", FChmod);
1477
  // env->SetMethod(target, "lchmod", LChmod);
1478
1479
1626
  env->SetMethod(target, "chown", Chown);
1480
1626
  env->SetMethod(target, "fchown", FChown);
1481
  // env->SetMethod(target, "lchown", LChown);
1482
1483
1626
  env->SetMethod(target, "utimes", UTimes);
1484
1626
  env->SetMethod(target, "futimes", FUTimes);
1485
1486
1626
  env->SetMethod(target, "mkdtemp", Mkdtemp);
1487
1488
1626
  StatWatcher::Initialize(env, target);
1489
1490
  // Create FunctionTemplate for FSReqWrap
1491
  Local<FunctionTemplate> fst =
1492
3252
      FunctionTemplate::New(env->isolate(), NewFSReqWrap);
1493
3252
  fst->InstanceTemplate()->SetInternalFieldCount(1);
1494
1626
  fst->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap"));
1495
6504
  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqWrap"),
1496
1626
              fst->GetFunction());
1497
1626
}
1498
1499
}  // end namespace node
1500
1501
1631
NODE_MODULE_CONTEXT_AWARE_BUILTIN(fs, node::InitFs)