[njs] Added fs.renameSync().

Dmitry Volyntsev xeioex at nginx.com
Mon Aug 12 18:05:16 UTC 2019


details:   https://hg.nginx.org/njs/rev/e861b6f6bcae
branches:  
changeset: 1122:e861b6f6bcae
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon Aug 12 21:04:49 2019 +0300
description:
Added fs.renameSync().

This closes #198 issue on Github.

diffstat:

 src/njs_fs.c             |  51 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/test/njs_unit_test.c |  10 +++++++++
 test/njs_expect_test.exp |  22 ++++++++++++++++++++
 3 files changed, 83 insertions(+), 0 deletions(-)

diffs (127 lines):

diff -r 355028822e48 -r e861b6f6bcae src/njs_fs.c
--- a/src/njs_fs.c	Mon Aug 12 21:03:24 2019 +0300
+++ b/src/njs_fs.c	Mon Aug 12 21:04:49 2019 +0300
@@ -30,6 +30,8 @@ static njs_int_t njs_fs_write_file_inter
     njs_uint_t nargs, int default_flags);
 static njs_int_t njs_fs_write_file_sync_internal(njs_vm_t *vm,
     njs_value_t *args, njs_uint_t nargs, int default_flags);
+static njs_int_t njs_fs_rename_sync(njs_vm_t *vm, njs_value_t *args,
+    njs_uint_t nargs, njs_index_t unused);
 
 static njs_int_t njs_fs_fd_read(njs_vm_t *vm, njs_value_t *path, int fd,
     njs_str_t *data);
@@ -905,6 +907,46 @@ done:
 
 
 static njs_int_t
+njs_fs_rename_sync(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
+    njs_index_t unused)
+{
+    int         ret;
+    const char  *old_path, *new_path;
+
+    if (njs_slow_path(!njs_is_string(njs_arg(args, nargs, 1)))) {
+        njs_type_error(vm, "oldPath must be a string");
+        return NJS_ERROR;
+    }
+
+    if (njs_slow_path(!njs_is_string(njs_arg(args, nargs, 2)))) {
+        njs_type_error(vm, "newPath must be a string");
+        return NJS_ERROR;
+    }
+
+    old_path = (const char *) njs_string_to_c_string(vm, njs_argument(args, 1));
+    if (njs_slow_path(old_path == NULL)) {
+        return NJS_ERROR;
+    }
+
+    new_path = (const char *) njs_string_to_c_string(vm, njs_argument(args, 2));
+    if (njs_slow_path(new_path == NULL)) {
+        return NJS_ERROR;
+    }
+
+    ret = rename(old_path, new_path);
+    if (njs_slow_path(ret != 0)) {
+        ret = njs_fs_error(vm, "rename", strerror(errno), NULL, errno,
+                           &vm->retval);
+        return NJS_ERROR;
+    }
+
+    njs_set_undefined(&vm->retval);
+
+    return NJS_OK;
+}
+
+
+static njs_int_t
 njs_fs_fd_read(njs_vm_t *vm, njs_value_t *path, int fd, njs_str_t *data)
 {
     u_char   *p, *end, *start;
@@ -1159,6 +1201,15 @@ static const njs_object_prop_t  njs_fs_o
         .configurable = 1,
     },
 
+    {
+        .type = NJS_PROPERTY,
+        .name = njs_string("renameSync"),
+        .value = njs_native_function(njs_fs_rename_sync, NJS_STRING_ARG,
+                                     NJS_STRING_ARG, 0),
+        .writable = 1,
+        .configurable = 1,
+    },
+
 };
 
 
diff -r 355028822e48 -r e861b6f6bcae src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Mon Aug 12 21:03:24 2019 +0300
+++ b/src/test/njs_unit_test.c	Mon Aug 12 21:04:49 2019 +0300
@@ -13156,6 +13156,16 @@ static njs_unit_test_t  njs_test[] =
                  "fs.writeFileSync('/njs_unknown_path', '', true)"),
       njs_str("TypeError: Unknown options type (a string or object required)") },
 
+    /* require('fs').writeFileSync() */
+
+    { njs_str("var fs = require('fs');"
+              "fs.renameSync()"),
+      njs_str("TypeError: oldPath must be a string") },
+
+    { njs_str("var fs = require('fs');"
+              "fs.renameSync({toString(){return '/path/1'}})"),
+      njs_str("TypeError: newPath must be a string") },
+
     /* require('crypto').createHash() */
 
     { njs_str("require('crypto').createHash('sha1')"),
diff -r 355028822e48 -r e861b6f6bcae test/njs_expect_test.exp
--- a/test/njs_expect_test.exp	Mon Aug 12 21:03:24 2019 +0300
+++ b/test/njs_expect_test.exp	Mon Aug 12 21:04:49 2019 +0300
@@ -646,6 +646,28 @@ njs_test {
      "'ABCABC'\r\n>> "}
 }
 
+# require('fs').renameSync()
+
+njs_test {
+    {"var fs = require('fs'), mktemp = ()=> `/tmp/njs_${Math.round(Math.random() * 1000000)}`\r\n"
+     "undefined\r\n>> "}
+    {"var fn1 = mktemp(), fn2 = mktemp();\r\n"
+     "undefined\r\n>> "}
+    {"fs.writeFileSync(fn1, 'ABC')\r\n"
+     "undefined\r\n>> "}
+    {"fs.renameSync(fn1, fn2)\r\n"
+     "undefined\r\n>> "}
+    {"fs.readFileSync(fn2)\r\n"
+     "'ABC'\r\n>> "}
+}
+
+njs_test {
+    {"var fs = require('fs')\r\n"
+     "undefined\r\n>> "}
+    {"fs.renameSync('build/test/file2', 'test/fs/')\r\n"
+     "Error: Not a directory*"}
+}
+
 # Modules
 
 njs_run {"-p" "test/module/libs" "./test/module/normal.js"} \


More information about the nginx-devel mailing list