Skip to content

Commit

Permalink
Introduced njs_fs_path_arg().
Browse files Browse the repository at this point in the history
  • Loading branch information
drsm committed Jan 17, 2020
1 parent f62e260 commit 1e9f7f5
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 83 deletions.
123 changes: 52 additions & 71 deletions src/njs_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ static njs_fs_entry_t njs_flags_table[] = {
};


njs_inline njs_int_t
njs_fs_path_arg(njs_vm_t *vm, const char **dst,
const njs_value_t* src, const njs_str_t *prop_name)
{
if (njs_slow_path(!njs_is_string(src))) {
njs_type_error(vm, "\"%V\" must be a string", prop_name);
return NJS_ERROR;
}

*dst = njs_string_to_c_string(vm, njs_value_arg(src));
if (njs_slow_path(*dst == NULL)) {
return NJS_ERROR;
}

return NJS_OK;
}


static njs_int_t
njs_fs_read_file(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused)
Expand All @@ -85,13 +103,14 @@ njs_fs_read_file(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_object_prop_t *prop;
njs_lvlhsh_query_t lhq;

if (njs_slow_path(nargs < 3)) {
njs_type_error(vm, "too few arguments");
return NJS_ERROR;
ret = njs_fs_path_arg(vm, &path, njs_arg(args, nargs, 1),
&njs_str_value("path"));
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}

if (njs_slow_path(!njs_is_string(&args[1]))) {
njs_type_error(vm, "path must be a string");
if (njs_slow_path(nargs < 3)) {
njs_type_error(vm, "too few arguments");
return NJS_ERROR;
}

Expand Down Expand Up @@ -156,11 +175,6 @@ njs_fs_read_file(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
return NJS_ERROR;
}

path = njs_string_to_c_string(vm, &args[1]);
if (njs_slow_path(path == NULL)) {
return NJS_ERROR;
}

if (encoding.length != 0
&& (encoding.length != 4 || memcmp(encoding.start, "utf8", 4) != 0))
{
Expand Down Expand Up @@ -320,13 +334,14 @@ njs_fs_read_file_sync(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_object_prop_t *prop;
njs_lvlhsh_query_t lhq;

if (njs_slow_path(nargs < 2)) {
njs_type_error(vm, "too few arguments");
return NJS_ERROR;
ret = njs_fs_path_arg(vm, &path, njs_arg(args, nargs, 1),
&njs_str_value("path"));
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}

if (njs_slow_path(!njs_is_string(&args[1]))) {
njs_type_error(vm, "path must be a string");
if (njs_slow_path(nargs < 2)) {
njs_type_error(vm, "too few arguments");
return NJS_ERROR;
}

Expand Down Expand Up @@ -557,13 +572,14 @@ static njs_int_t njs_fs_write_file_internal(njs_vm_t *vm, njs_value_t *args,
njs_object_prop_t *prop;
njs_lvlhsh_query_t lhq;

if (njs_slow_path(nargs < 4)) {
njs_type_error(vm, "too few arguments");
return NJS_ERROR;
ret = njs_fs_path_arg(vm, &path, njs_arg(args, nargs, 1),
&njs_str_value("path"));
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}

if (njs_slow_path(!njs_is_string(&args[1]))) {
njs_type_error(vm, "path must be a string");
if (njs_slow_path(nargs < 4)) {
njs_type_error(vm, "too few arguments");
return NJS_ERROR;
}

Expand Down Expand Up @@ -654,11 +670,6 @@ static njs_int_t njs_fs_write_file_internal(njs_vm_t *vm, njs_value_t *args,
md = 0666;
}

path = njs_string_to_c_string(vm, &args[1]);
if (njs_slow_path(path == NULL)) {
return NJS_ERROR;
}

if (encoding.length != 0
&& (encoding.length != 4 || memcmp(encoding.start, "utf8", 4) != 0))
{
Expand Down Expand Up @@ -745,13 +756,14 @@ njs_fs_write_file_sync_internal(njs_vm_t *vm, njs_value_t *args,
njs_object_prop_t *prop;
njs_lvlhsh_query_t lhq;

if (njs_slow_path(nargs < 3)) {
njs_type_error(vm, "too few arguments");
return NJS_ERROR;
ret = njs_fs_path_arg(vm, &path, njs_arg(args, nargs, 1),
&njs_str_value("path"));
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}

if (njs_slow_path(!njs_is_string(&args[1]))) {
njs_type_error(vm, "path must be a string");
if (njs_slow_path(nargs < 3)) {
njs_type_error(vm, "too few arguments");
return NJS_ERROR;
}

Expand Down Expand Up @@ -827,11 +839,6 @@ njs_fs_write_file_sync_internal(njs_vm_t *vm, njs_value_t *args,
md = 0666;
}

path = njs_string_to_c_string(vm, &args[1]);
if (njs_slow_path(path == NULL)) {
return NJS_ERROR;
}

if (encoding.length != 0
&& (encoding.length != 4 || memcmp(encoding.start, "utf8", 4) != 0))
{
Expand Down Expand Up @@ -896,45 +903,19 @@ 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;
njs_value_t *old, *new;

if (njs_slow_path(nargs < 3)) {
if (nargs < 2) {
njs_type_error(vm, "oldPath must be a string");
return NJS_ERROR;
}

njs_type_error(vm, "newPath must be a string");
return NJS_ERROR;
}

old = njs_argument(args, 1);
new = njs_argument(args, 2);
njs_int_t ret;
const char *old_path, *new_path;

if (njs_slow_path(!njs_is_string(old))) {
ret = njs_value_to_string(vm, old, old);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
}

if (njs_slow_path(!njs_is_string(new))) {
ret = njs_value_to_string(vm, new, new);
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}
}

old_path = njs_string_to_c_string(vm, old);
if (njs_slow_path(old_path == NULL)) {
return NJS_ERROR;
ret = njs_fs_path_arg(vm, &old_path, njs_arg(args, nargs, 1),
&njs_str_value("oldPath"));
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}

new_path = njs_string_to_c_string(vm, new);
if (njs_slow_path(new_path == NULL)) {
return NJS_ERROR;
ret = njs_fs_path_arg(vm, &new_path, njs_arg(args, nargs, 2),
&njs_str_value("newPath"));
if (njs_slow_path(ret != NJS_OK)) {
return ret;
}

ret = rename(old_path, new_path);
Expand Down
2 changes: 1 addition & 1 deletion src/test/njs_interactive_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ static njs_interactive_test_t njs_test[] =
" at main (native)\n") },

{ njs_str("var fs = require('fs'); fs.readFile()" ENTER),
njs_str("TypeError: too few arguments\n"
njs_str("TypeError: \"path\" must be a string\n"
" at fs.readFile (native)\n"
" at main (native)\n") },

Expand Down
54 changes: 43 additions & 11 deletions src/test/njs_unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -15520,7 +15520,7 @@ static njs_unit_test_t njs_test[] =

{ njs_str("var fs = require('fs');"
"fs.readFile()"),
njs_str("TypeError: too few arguments") },
njs_str("TypeError: \"path\" must be a string") },

{ njs_str("var fs = require('fs');"
"fs.readFile('/njs_unknown_path')"),
Expand Down Expand Up @@ -15550,11 +15550,11 @@ static njs_unit_test_t njs_test[] =

{ njs_str("var fs = require('fs');"
"fs.readFileSync()"),
njs_str("TypeError: too few arguments") },
njs_str("TypeError: \"path\" must be a string") },

{ njs_str("var fs = require('fs');"
"fs.readFileSync({})"),
njs_str("TypeError: path must be a string") },
njs_str("TypeError: \"path\" must be a string") },

{ njs_str("var fs = require('fs');"
"fs.readFileSync('/njs_unknown_path', {flag:'xx'})"),
Expand All @@ -15577,7 +15577,7 @@ static njs_unit_test_t njs_test[] =

{ njs_str("var fs = require('fs');"
"fs.writeFile()"),
njs_str("TypeError: too few arguments") },
njs_str("TypeError: \"path\" must be a string") },

{ njs_str("var fs = require('fs');"
"fs.writeFile('/njs_unknown_path')"),
Expand All @@ -15589,7 +15589,7 @@ static njs_unit_test_t njs_test[] =

{ njs_str("var fs = require('fs');"
"fs.writeFile({}, '', function () {})"),
njs_str("TypeError: path must be a string") },
njs_str("TypeError: \"path\" must be a string") },

{ njs_str("var fs = require('fs');"
"fs.writeFile('/njs_unknown_path', '', 'utf8')"),
Expand All @@ -15615,15 +15615,15 @@ static njs_unit_test_t njs_test[] =

{ njs_str("var fs = require('fs');"
"fs.writeFileSync()"),
njs_str("TypeError: too few arguments") },
njs_str("TypeError: \"path\" must be a string") },

{ njs_str("var fs = require('fs');"
"fs.writeFileSync('/njs_unknown_path')"),
njs_str("TypeError: too few arguments") },

{ njs_str("var fs = require('fs');"
"fs.writeFileSync({}, '')"),
njs_str("TypeError: path must be a string") },
njs_str("TypeError: \"path\" must be a string") },

{ njs_str("var fs = require('fs');"
"fs.writeFileSync('/njs_unknown_path', '', {flag:'xx'})"),
Expand All @@ -15641,15 +15641,47 @@ 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() */
/* require('fs').renameSync() */

{ njs_str("var fs = require('fs');"
"fs.renameSync()"),
njs_str("TypeError: oldPath must be a string") },
njs_str("TypeError: \"oldPath\" must be a string") },

{ njs_str("var fs = require('fs');"
"fs.renameSync('/njs_unknown_path')"),
njs_str("TypeError: \"newPath\" must be a string") },

{ njs_str("var fs = require('fs');"
"fs.renameSync({toString(){return '/path/1'}})"),
njs_str("TypeError: newPath must be a string") },
"[undefined, null, false, NaN, Symbol(), {}, Object('/njs_unknown_path')]"
".map((x) => { try { fs.renameSync(x, '/njs_unknown_path'); } "
" catch (e) { return (e instanceof TypeError); } })"
".every((x) => x === true)"),
njs_str("true")},

{ njs_str("var fs = require('fs');"
"[undefined, null, false, NaN, Symbol(), {}, Object('/njs_unknown_path')]"
".map((x) => { try { fs.renameSync('/njs_unknown_path', x); } "
" catch (e) { return (e instanceof TypeError); } })"
".every((x) => x === true)"),
njs_str("true")},

{ njs_str("var "
"fs = require('fs'),"
"func = ["
"'readFile',"
"'readFileSync',"
"'writeFile',"
"'writeFileSync',"
"'appendFile',"
"'appendFileSync',"
"],"
"test = (fname) =>"
"[undefined, null, false, NaN, Symbol(), {}, Object('/njs_unknown_path')]"
".map((x) => { try { fs[fname](x); } "
" catch (e) { return (e instanceof TypeError); } })"
".every((x) => x === true);"
"func.map(test).every((x) => x)"),
njs_str("true")},

/* require('crypto').createHash() */

Expand Down

0 comments on commit 1e9f7f5

Please sign in to comment.