[njs] FS: introduced fs.existsSync().
Dmitry Volyntsev
xeioex at nginx.com
Sat Sep 30 05:43:55 UTC 2023
details: https://hg.nginx.org/njs/rev/f076da3c7a6f
branches:
changeset: 2212:f076da3c7a6f
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Fri Sep 29 21:41:34 2023 -0700
description:
FS: introduced fs.existsSync().
diffstat:
external/njs_fs_module.c | 30 ++++++++++++++++++++++++++++++
test/fs/methods.t.js | 31 +++++++++++++++++++++++++++++++
2 files changed, 61 insertions(+), 0 deletions(-)
diffs (102 lines):
diff -r 88318e18dec9 -r f076da3c7a6f external/njs_fs_module.c
--- a/external/njs_fs_module.c Thu Sep 28 19:09:56 2023 -0700
+++ b/external/njs_fs_module.c Fri Sep 29 21:41:34 2023 -0700
@@ -146,6 +146,8 @@ typedef njs_int_t (*njs_file_tree_walk_c
static njs_int_t njs_fs_access(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t calltype, njs_value_t *retval);
+static njs_int_t njs_fs_exists_sync(njs_vm_t *vm, njs_value_t *args,
+ njs_uint_t nargs, njs_index_t calltype, njs_value_t *retval);
static njs_int_t njs_fs_mkdir(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t calltype, njs_value_t *retval);
static njs_int_t njs_fs_open(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
@@ -583,6 +585,16 @@ static njs_external_t njs_ext_fs[] = {
{
.flags = NJS_EXTERN_METHOD,
+ .name.string = njs_str("existsSync"),
+ .writable = 1,
+ .configurable = 1,
+ .u.method = {
+ .native = njs_fs_exists_sync,
+ }
+ },
+
+ {
+ .flags = NJS_EXTERN_METHOD,
.name.string = njs_str("fstatSync"),
.writable = 1,
.configurable = 1,
@@ -1470,6 +1482,24 @@ njs_fs_access(njs_vm_t *vm, njs_value_t
static njs_int_t
+njs_fs_exists_sync(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
+ njs_index_t calltype, njs_value_t *retval)
+{
+ const char *path;
+ char path_buf[NJS_MAX_PATH + 1];
+
+ path = njs_fs_path(vm, path_buf, njs_arg(args, nargs, 1), "path");
+ if (njs_slow_path(path == NULL)) {
+ return NJS_ERROR;
+ }
+
+ njs_value_boolean_set(retval, access(path, F_OK) == 0);
+
+ return NJS_OK;
+}
+
+
+static njs_int_t
njs_fs_open(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t calltype, njs_value_t *retval)
{
diff -r 88318e18dec9 -r f076da3c7a6f test/fs/methods.t.js
--- a/test/fs/methods.t.js Thu Sep 28 19:09:56 2023 -0700
+++ b/test/fs/methods.t.js Fri Sep 29 21:41:34 2023 -0700
@@ -359,6 +359,36 @@ let appendFileP_tsuite = {
get tests() { return append_tests() },
};
+async function exists_test(params) {
+ let res = await method("exists", params);
+
+ if (res !== params.expected) {
+ throw Error(`exists failed check`);
+ }
+
+ return 'SUCCESS';
+}
+
+let exists_tests = () => [
+ { args: ["test/fs/ascii"],
+ expected: true },
+ { args: ["test/fs"],
+ expected: true },
+ { args: ["test/fs_NONEXISTENT/ascii"],
+ expected: false },
+ { args: ["test/fs/ascii_NONEXISTENT"],
+ expected: false },
+];
+
+let existsSync_tsuite = {
+ name: "fs existsSync",
+ skip: () => (!has_fs() || !has_buffer()),
+ T: exists_test,
+ prepare_args: p,
+ opts: { type: "sync" },
+ get tests() { return exists_tests() },
+};
+
async function realpath_test(params) {
let data = await method("realpath", params);
@@ -1153,6 +1183,7 @@ run([
appendFile_tsuite,
appendFileSync_tsuite,
appendFileP_tsuite,
+ existsSync_tsuite,
realpath_tsuite,
realpathSync_tsuite,
realpathP_tsuite,
More information about the nginx-devel
mailing list