[njs] Added friends functions to fs.renameSync().

Dmitry Volyntsev xeioex at nginx.com
Tue May 26 18:36:19 UTC 2020


details:   https://hg.nginx.org/njs/rev/b5f4b848c60f
branches:  
changeset: 1400:b5f4b848c60f
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue May 26 16:34:45 2020 +0000
description:
Added friends functions to fs.renameSync().

diffstat:

 src/njs_fs.c               |   48 ++++++++++++++++---
 test/js/fs_promises_006.js |  104 +++++++++++++++++++++++++++++++++++++++++++++
 test/njs_expect_test.exp   |    5 ++
 3 files changed, 148 insertions(+), 9 deletions(-)

diffs (206 lines):

diff -r 2e94c512d5c7 -r b5f4b848c60f src/njs_fs.c
--- a/src/njs_fs.c	Tue May 26 16:21:05 2020 +0000
+++ b/src/njs_fs.c	Tue May 26 16:34:45 2020 +0000
@@ -440,11 +440,22 @@ 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)
+njs_fs_rename(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
+    njs_index_t calltype)
 {
-    njs_int_t   ret;
-    const char  *old_path, *new_path;
+    njs_int_t    ret;
+    const char   *old_path, *new_path;
+    njs_value_t  retval, *callback;
+
+    callback = NULL;
+
+    if (calltype == NJS_FS_CALLBACK) {
+        callback = njs_arg(args, nargs, 3);
+        if (!njs_is_function(callback)) {
+            njs_type_error(vm, "\"callback\" must be a function");
+            return NJS_ERROR;
+        }
+    }
 
     ret = njs_fs_path_arg(vm, &old_path, njs_arg(args, nargs, 1),
                           &njs_str_value("oldPath"));
@@ -458,15 +469,18 @@ njs_fs_rename_sync(njs_vm_t *vm, njs_val
         return ret;
     }
 
-    njs_set_undefined(&vm->retval);
+    njs_set_undefined(&retval);
 
     ret = rename(old_path, new_path);
     if (njs_slow_path(ret != 0)) {
-        ret = njs_fs_error(vm, "rename", strerror(errno), NULL, errno,
-                            &vm->retval);
+        ret = njs_fs_error(vm, "rename", strerror(errno), NULL, errno, &retval);
     }
 
-    return ret;
+    if (ret == NJS_OK) {
+        return njs_fs_result(vm, &retval, calltype, callback, 1);
+    }
+
+    return NJS_ERROR;
 }
 
 
@@ -1263,6 +1277,14 @@ static const njs_object_prop_t  njs_fs_p
 
     {
         .type = NJS_PROPERTY,
+        .name = njs_string("rename"),
+        .value = njs_native_function2(njs_fs_rename, 0, NJS_FS_PROMISE),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
         .name = njs_string("rmdir"),
         .value = njs_native_function2(njs_fs_rmdir, 0, NJS_FS_PROMISE),
         .writable = 1,
@@ -1445,8 +1467,16 @@ static const njs_object_prop_t  njs_fs_o
 
     {
         .type = NJS_PROPERTY,
+        .name = njs_string("rename"),
+        .value = njs_native_function2(njs_fs_rename, 0, NJS_FS_CALLBACK),
+        .writable = 1,
+        .configurable = 1,
+    },
+
+    {
+        .type = NJS_PROPERTY,
         .name = njs_string("renameSync"),
-        .value = njs_native_function(njs_fs_rename_sync, 0),
+        .value = njs_native_function2(njs_fs_rename, 0, NJS_FS_DIRECT),
         .writable = 1,
         .configurable = 1,
     },
diff -r 2e94c512d5c7 -r b5f4b848c60f test/js/fs_promises_006.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/js/fs_promises_006.js	Tue May 26 16:34:45 2020 +0000
@@ -0,0 +1,104 @@
+var fs = require('fs');
+var fsp  = fs.promises;
+var dname = './build/test/';
+var fname = (d) => d + '/fs_promises_006_file';
+var fname_utf8 = (d) => d + '/fs_promises_αβγ_006';
+
+var testSync = new Promise((resolve, reject) => {
+    try {
+        try { fs.unlinkSync(fname(dname)); } catch (e) {}
+        try { fs.unlinkSync(fname_utf8(dname)); } catch (e) {}
+
+        fs.writeFileSync(fname(dname), fname(dname));
+
+        fs.renameSync(fname(dname), fname_utf8(dname));
+
+        fs.accessSync(fname_utf8(dname));
+
+        try {
+            fs.renameSync(fname_utf8(dname), dname);
+
+        } catch (e) {
+            if (e.syscall != 'rename'
+                || (e.code != 'ENOTDIR' && e.code != 'EISDIR'))
+            {
+                throw e;
+            }
+        }
+
+        resolve();
+
+    } catch (e) {
+        reject(e);
+    }
+});
+
+var testCallback = new Promise((resolve, reject) => {
+    try {
+        try { fs.unlinkSync(fname(dname)); } catch (e) {}
+        try { fs.unlinkSync(fname_utf8(dname)); } catch (e) {}
+
+        fs.writeFileSync(fname(dname), fname(dname));
+
+        fs.rename(fname(dname), fname_utf8(dname), err => {
+            if (err) {
+                throw err;
+            }
+        });
+
+        fs.accessSync(fname_utf8(dname));
+
+        fs.rename(fname_utf8(dname), dname, err => {
+            if (err.syscall != 'rename'
+                || (err.code != 'ENOTDIR' && err.code != 'EISDIR'))
+            {
+                throw err;
+            }
+        });
+
+        resolve();
+
+    } catch (e) {
+        reject(e);
+    }
+});
+
+Promise.resolve()
+.then(() => testSync)
+.then(() => {
+    console.log('test fs.renameSync');
+})
+.catch((e) => {
+    console.log('test fs.renameSync failed', JSON.stringify(e));
+})
+
+.then(testCallback)
+.then(() => {
+    console.log('test fs.rename');
+})
+.catch((e) => {
+    console.log('test fs.rename failed', JSON.stringify(e));
+})
+
+.then(() => {
+    try { fs.unlinkSync(fname(dname)); } catch (e) {}
+    try { fs.unlinkSync(fname_utf8(dname)); } catch (e) {}
+
+    fs.writeFileSync(fname(dname), fname(dname));
+})
+.then(() => fsp.rename(fname(dname), fname_utf8(dname)))
+.then(() => fsp.access(fname_utf8(dname)))
+.then(() => fsp.rename(fname_utf8(dname), dname))
+.catch(e => {
+    if (e.syscall != 'rename'
+        || (e.code != 'ENOTDIR' && e.code != 'EISDIR'))
+    {
+        throw e;
+    }
+})
+.then(() => {
+    console.log('test fsp.rename');
+})
+.catch((e) => {
+    console.log('test fsp.rename failed', JSON.stringify(e));
+});
diff -r 2e94c512d5c7 -r b5f4b848c60f test/njs_expect_test.exp
--- a/test/njs_expect_test.exp	Tue May 26 16:21:05 2020 +0000
+++ b/test/njs_expect_test.exp	Tue May 26 16:34:45 2020 +0000
@@ -1135,3 +1135,8 @@ njs_run {"./test/js/fs_promises_005.js"}
 "test fs.mkdirSync
 test fs.mkdir
 test fsp.mkdir"
+
+njs_run {"./test/js/fs_promises_006.js"} \
+"test fs.renameSync
+test fs.rename
+test fsp.rename"


More information about the nginx-devel mailing list