diff --git a/benchmark/fs/bench-chownSync.js b/benchmark/fs/bench-chownSync.js new file mode 100644 index 00000000000000..d07f3b65949979 --- /dev/null +++ b/benchmark/fs/bench-chownSync.js @@ -0,0 +1,54 @@ +'use strict'; + +const common = require('../common'); +const fs = require('fs'); +const assert = require('assert'); +const tmpdir = require('../../test/common/tmpdir'); + +if (process.platform === 'win32') { + console.log('Skipping: Windows does not have `getuid` or `getgid`'); + process.exit(0); +} + +const bench = common.createBenchmark(main, { + type: ['existing', 'non-existing'], + method: ['chownSync', 'lchownSync'], + n: [1e4], +}); + +function main({ n, type, method }) { + const uid = process.getuid(); + const gid = process.getgid(); + const fsMethod = fs[method]; + + switch (type) { + case 'existing': { + tmpdir.refresh(); + const tmpfile = tmpdir.resolve(`.existing-file-${process.pid}`); + fs.writeFileSync(tmpfile, 'this-is-for-a-benchmark', 'utf8'); + bench.start(); + for (let i = 0; i < n; i++) { + fsMethod(tmpfile, uid, gid); + } + bench.end(n); + break; + } + case 'non-existing': { + const path = tmpdir.resolve(`.non-existing-file-${Date.now()}`); + let hasError = false; + bench.start(); + for (let i = 0; i < n; i++) { + try { + fs[method](path, uid, gid); + } catch { + hasError = true; + } + } + bench.end(n); + assert(hasError); + break; + } + default: + new Error('Invalid type'); + } +} diff --git a/benchmark/fs/bench-linkSync.js b/benchmark/fs/bench-linkSync.js new file mode 100644 index 00000000000000..7c3ef29291913e --- /dev/null +++ b/benchmark/fs/bench-linkSync.js @@ -0,0 +1,50 @@ +'use strict'; + +const common = require('../common'); +const fs = require('fs'); +const assert = require('assert'); +const tmpdir = require('../../test/common/tmpdir'); + +tmpdir.refresh(); +const tmpfile = tmpdir.resolve(`.bench-file-data-${Date.now()}`); +fs.writeFileSync(tmpfile, 'bench-file', 'utf-8'); + +const bench = common.createBenchmark(main, { + type: ['valid', 'invalid'], + n: [1e3], +}); + +function main({ n, type }) { + switch (type) { + case 'valid': { + bench.start(); + for (let i = 0; i < n; i++) { + fs.linkSync(tmpfile, tmpdir.resolve(`.valid-${i}`), 'file'); + } + bench.end(n); + + break; + } + + case 'invalid': { + let hasError = false; + bench.start(); + for (let i = 0; i < n; i++) { + try { + fs.linkSync( + tmpdir.resolve(`.non-existing-file-for-linkSync-${i}`), + __filename, + 'file', + ); + } catch { + hasError = true; + } + } + bench.end(n); + assert(hasError); + break; + } + default: + new Error('Invalid type'); + } +} diff --git a/benchmark/fs/bench-readlinkSync.js b/benchmark/fs/bench-readlinkSync.js new file mode 100644 index 00000000000000..15c22273e557b6 --- /dev/null +++ b/benchmark/fs/bench-readlinkSync.js @@ -0,0 +1,54 @@ +'use strict'; + +const common = require('../common'); +const fs = require('fs'); +const assert = require('assert'); +const tmpdir = require('../../test/common/tmpdir'); + +if (process.platform === 'win32') { + console.log('Skipping: Windows does not play well with `symlinkSync`'); + process.exit(0); +} + +const bench = common.createBenchmark(main, { + type: ['valid', 'invalid'], + n: [1e3], +}); + +function main({ n, type }) { + switch (type) { + case 'valid': { + tmpdir.refresh(); + const tmpfile = tmpdir.resolve(`.readlink-file-${process.pid}`); + fs.writeFileSync(tmpfile, 'data', 'utf8'); + let returnValue; + for (let i = 0; i < n; i++) { + fs.symlinkSync(tmpfile, tmpdir.resolve(`.readlink-sync-${i}`), 'file'); + } + bench.start(); + for (let i = 0; i < n; i++) { + returnValue = fs.readlinkSync(tmpdir.resolve(`.readlink-sync-${i}`), { encoding: 'utf8' }); + } + bench.end(n); + assert(returnValue); + break; + } + + case 'invalid': { + let hasError = false; + bench.start(); + for (let i = 0; i < n; i++) { + try { + fs.readlinkSync(tmpdir.resolve('.non-existing-file-for-readlinkSync')); + } catch { + hasError = true; + } + } + bench.end(n); + assert(hasError); + break; + } + default: + new Error('Invalid type'); + } +} diff --git a/benchmark/fs/bench-renameSync.js b/benchmark/fs/bench-renameSync.js new file mode 100644 index 00000000000000..9f9f5e4e84cda0 --- /dev/null +++ b/benchmark/fs/bench-renameSync.js @@ -0,0 +1,49 @@ +'use strict'; + +const common = require('../common'); +const fs = require('fs'); +const assert = require('assert'); +const tmpdir = require('../../test/common/tmpdir'); + +const bench = common.createBenchmark(main, { + type: ['invalid', 'valid'], + n: [2e3], +}); + +function main({ n, type }) { + switch (type) { + case 'invalid': { + let hasError = false; + bench.start(); + for (let i = 0; i < n; i++) { + try { + fs.renameSync(tmpdir.resolve(`.non-existing-file-${i}`), tmpdir.resolve(`.new-file-${i}`)); + } catch { + hasError = true; + } + } + bench.end(n); + assert(hasError); + break; + } + case 'valid': { + tmpdir.refresh(); + for (let i = 0; i < n; i++) { + fs.writeFileSync(tmpdir.resolve(`.existing-file-${i}`), 'bench', 'utf8'); + } + + bench.start(); + for (let i = 0; i < n; i++) { + fs.renameSync( + tmpdir.resolve(`.existing-file-${i}`), + tmpdir.resolve(`.new-existing-file-${i}`), + ); + } + + bench.end(n); + break; + } + default: + throw new Error('Invalid type'); + } +} diff --git a/benchmark/fs/bench-symlinkSync.js b/benchmark/fs/bench-symlinkSync.js new file mode 100644 index 00000000000000..5bf4e0e50779d9 --- /dev/null +++ b/benchmark/fs/bench-symlinkSync.js @@ -0,0 +1,51 @@ +'use strict'; + +const common = require('../common'); +const fs = require('fs'); +const assert = require('assert'); +const tmpdir = require('../../test/common/tmpdir'); + +if (process.platform === 'win32') { + console.log('Skipping: Windows does not play well with `symlink`'); + process.exit(0); +} + +const bench = common.createBenchmark(main, { + type: ['valid', 'invalid'], + n: [1e3], +}); + +function main({ n, type }) { + switch (type) { + case 'valid': { + tmpdir.refresh(); + bench.start(); + for (let i = 0; i < n; i++) { + fs.symlinkSync(tmpdir.resolve('.non-existent-symlink-file'), tmpdir.resolve(`.valid-${i}`), 'file'); + } + bench.end(n); + break; + } + + case 'invalid': { + let hasError = false; + bench.start(); + for (let i = 0; i < n; i++) { + try { + fs.symlinkSync( + tmpdir.resolve('.non-existent-symlink-file'), + __filename, + 'file', + ); + } catch { + hasError = true; + } + } + bench.end(n); + assert(hasError); + break; + } + default: + new Error('Invalid type'); + } +} diff --git a/lib/fs.js b/lib/fs.js index 2d5170cd2f44b0..7324c2c2036a08 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1034,10 +1034,10 @@ function rename(oldPath, newPath, callback) { function renameSync(oldPath, newPath) { oldPath = getValidatedPath(oldPath, 'oldPath'); newPath = getValidatedPath(newPath, 'newPath'); - const ctx = { path: oldPath, dest: newPath }; - binding.rename(pathModule.toNamespacedPath(oldPath), - pathModule.toNamespacedPath(newPath), undefined, ctx); - handleErrorFromBinding(ctx); + binding.rename( + pathModule.toNamespacedPath(oldPath), + pathModule.toNamespacedPath(newPath), + ); } /** @@ -1728,11 +1728,10 @@ function readlink(path, options, callback) { function readlinkSync(path, options) { options = getOptions(options); path = getValidatedPath(path, 'oldPath'); - const ctx = { path }; - const result = binding.readlink(pathModule.toNamespacedPath(path), - options.encoding, undefined, ctx); - handleErrorFromBinding(ctx); - return result; + return binding.readlink( + pathModule.toNamespacedPath(path), + options.encoding, + ); } /** @@ -1805,13 +1804,12 @@ function symlinkSync(target, path, type) { } target = getValidatedPath(target, 'target'); path = getValidatedPath(path); - const flags = stringToSymlinkType(type); - - const ctx = { path: target, dest: path }; - binding.symlink(preprocessSymlinkDestination(target, type, path), - pathModule.toNamespacedPath(path), flags, undefined, ctx); - handleErrorFromBinding(ctx); + binding.symlink( + preprocessSymlinkDestination(target, type, path), + pathModule.toNamespacedPath(path), + stringToSymlinkType(type), + ); } /** @@ -1847,12 +1845,10 @@ function linkSync(existingPath, newPath) { existingPath = getValidatedPath(existingPath, 'existingPath'); newPath = getValidatedPath(newPath, 'newPath'); - const ctx = { path: existingPath, dest: newPath }; - const result = binding.link(pathModule.toNamespacedPath(existingPath), - pathModule.toNamespacedPath(newPath), - undefined, ctx); - handleErrorFromBinding(ctx); - return result; + binding.link( + pathModule.toNamespacedPath(existingPath), + pathModule.toNamespacedPath(newPath), + ); } /** @@ -2016,9 +2012,11 @@ function lchownSync(path, uid, gid) { path = getValidatedPath(path); validateInteger(uid, 'uid', -1, kMaxUserId); validateInteger(gid, 'gid', -1, kMaxUserId); - const ctx = { path }; - binding.lchown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx); - handleErrorFromBinding(ctx); + binding.lchown( + pathModule.toNamespacedPath(path), + uid, + gid, + ); } /** @@ -2089,9 +2087,11 @@ function chownSync(path, uid, gid) { path = getValidatedPath(path); validateInteger(uid, 'uid', -1, kMaxUserId); validateInteger(gid, 'gid', -1, kMaxUserId); - const ctx = { path }; - binding.chown(pathModule.toNamespacedPath(path), uid, gid, undefined, ctx); - handleErrorFromBinding(ctx); + binding.chown( + pathModule.toNamespacedPath(path), + uid, + gid, + ); } /** @@ -2714,10 +2714,8 @@ function realpathSync(p, options) { } } if (linkTarget === null) { - const ctx = { path: base }; binding.stat(baseLong, false, undefined, true); - linkTarget = binding.readlink(baseLong, undefined, undefined, ctx); - handleErrorFromBinding(ctx); + linkTarget = binding.readlink(baseLong, undefined); } resolvedLink = pathModule.resolve(previous, linkTarget); @@ -2754,10 +2752,10 @@ function realpathSync(p, options) { realpathSync.native = (path, options) => { options = getOptions(options); path = getValidatedPath(path); - const ctx = { path }; - const result = binding.realpath(pathModule.toNamespacedPath(path), options.encoding, undefined, ctx); - handleErrorFromBinding(ctx); - return result; + return binding.realpath( + pathModule.toNamespacedPath(path), + options.encoding, + ); }; /** @@ -2966,11 +2964,7 @@ function mkdtempSync(prefix, options) { path = Buffer.concat([prefix, Buffer.from('XXXXXX')]); } - const ctx = { path }; - const result = binding.mkdtemp(path, options.encoding, - undefined, ctx); - handleErrorFromBinding(ctx); - return result; + return binding.mkdtemp(path, options.encoding); } /** diff --git a/src/node_file.cc b/src/node_file.cc index afaa028784e710..a9b9231009c144 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1298,7 +1298,7 @@ static void Symlink(const FunctionCallbackInfo& args) { Isolate* isolate = env->isolate(); const int argc = args.Length(); - CHECK_GE(argc, 4); + CHECK_GE(argc, 3); BufferValue target(isolate, args[0]); CHECK_NOT_NULL(*target); @@ -1317,8 +1317,8 @@ static void Symlink(const FunctionCallbackInfo& args) { CHECK(args[2]->IsInt32()); int flags = args[2].As()->Value(); - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - if (req_wrap_async != nullptr) { // symlink(target, path, flags, req) + if (argc > 3) { // symlink(target, path, flags, req) + FSReqBase* req_wrap_async = GetReqWrap(args, 3); FS_ASYNC_TRACE_BEGIN2(UV_FS_SYMLINK, req_wrap_async, "target", @@ -1328,11 +1328,10 @@ static void Symlink(const FunctionCallbackInfo& args) { AsyncDestCall(env, req_wrap_async, args, "symlink", *path, path.length(), UTF8, AfterNoArgs, uv_fs_symlink, *target, *path, flags); } else { // symlink(target, path, flags, undefined, ctx) - CHECK_EQ(argc, 5); - FSReqWrapSync req_wrap_sync; + FSReqWrapSync req_wrap_sync("symlink", *target, *path); FS_SYNC_TRACE_BEGIN(symlink); - SyncCall(env, args[4], &req_wrap_sync, "symlink", - uv_fs_symlink, *target, *path, flags); + SyncCallAndThrowOnError( + env, &req_wrap_sync, uv_fs_symlink, *target, *path, flags); FS_SYNC_TRACE_END(symlink); } } @@ -1342,7 +1341,7 @@ static void Link(const FunctionCallbackInfo& args) { Isolate* isolate = env->isolate(); const int argc = args.Length(); - CHECK_GE(argc, 3); + CHECK_GE(argc, 2); BufferValue src(isolate, args[0]); CHECK_NOT_NULL(*src); @@ -1360,8 +1359,8 @@ static void Link(const FunctionCallbackInfo& args) { THROW_IF_INSUFFICIENT_PERMISSIONS( env, permission::PermissionScope::kFileSystemWrite, dest_view); - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - if (req_wrap_async != nullptr) { // link(src, dest, req) + if (argc > 2) { // link(src, dest, req) + FSReqBase* req_wrap_async = GetReqWrap(args, 2); FS_ASYNC_TRACE_BEGIN2(UV_FS_LINK, req_wrap_async, "src", @@ -1371,11 +1370,9 @@ static void Link(const FunctionCallbackInfo& args) { AsyncDestCall(env, req_wrap_async, args, "link", *dest, dest.length(), UTF8, AfterNoArgs, uv_fs_link, *src, *dest); } else { // link(src, dest) - CHECK_EQ(argc, 4); - FSReqWrapSync req_wrap_sync; + FSReqWrapSync req_wrap_sync("link", *src, *dest); FS_SYNC_TRACE_BEGIN(link); - SyncCall(env, args[3], &req_wrap_sync, "link", - uv_fs_link, *src, *dest); + SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_link, *src, *dest); FS_SYNC_TRACE_END(link); } } @@ -1385,7 +1382,7 @@ static void ReadLink(const FunctionCallbackInfo& args) { Isolate* isolate = env->isolate(); const int argc = args.Length(); - CHECK_GE(argc, 3); + CHECK_GE(argc, 2); BufferValue path(isolate, args[0]); CHECK_NOT_NULL(*path); @@ -1394,21 +1391,20 @@ static void ReadLink(const FunctionCallbackInfo& args) { const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - if (req_wrap_async != nullptr) { // readlink(path, encoding, req) + if (argc > 2) { // readlink(path, encoding, req) + FSReqBase* req_wrap_async = GetReqWrap(args, 2); FS_ASYNC_TRACE_BEGIN1( UV_FS_READLINK, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, req_wrap_async, args, "readlink", encoding, AfterStringPtr, uv_fs_readlink, *path); - } else { - CHECK_EQ(argc, 4); - FSReqWrapSync req_wrap_sync; + } else { // readlink(path, encoding) + FSReqWrapSync req_wrap_sync("readlink", *path); FS_SYNC_TRACE_BEGIN(readlink); - int err = SyncCall(env, args[3], &req_wrap_sync, "readlink", - uv_fs_readlink, *path); + int err = + SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_readlink, *path); FS_SYNC_TRACE_END(readlink); if (err < 0) { - return; // syscall failed, no need to continue, error info is in ctx + return; } const char* link_path = static_cast(req_wrap_sync.req.ptr); @@ -1418,8 +1414,7 @@ static void ReadLink(const FunctionCallbackInfo& args) { encoding, &error); if (rc.IsEmpty()) { - Local ctx = args[3].As(); - ctx->Set(env->context(), env->error_string(), error).Check(); + env->isolate()->ThrowException(error); return; } @@ -1432,7 +1427,7 @@ static void Rename(const FunctionCallbackInfo& args) { Isolate* isolate = env->isolate(); const int argc = args.Length(); - CHECK_GE(argc, 3); + CHECK_GE(argc, 2); BufferValue old_path(isolate, args[0]); CHECK_NOT_NULL(*old_path); @@ -1449,8 +1444,8 @@ static void Rename(const FunctionCallbackInfo& args) { permission::PermissionScope::kFileSystemWrite, new_path.ToStringView()); - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - if (req_wrap_async != nullptr) { + if (argc > 2) { // rename(old_path, new_path, req) + FSReqBase* req_wrap_async = GetReqWrap(args, 2); FS_ASYNC_TRACE_BEGIN2(UV_FS_RENAME, req_wrap_async, "old_path", @@ -1460,12 +1455,11 @@ static void Rename(const FunctionCallbackInfo& args) { AsyncDestCall(env, req_wrap_async, args, "rename", *new_path, new_path.length(), UTF8, AfterNoArgs, uv_fs_rename, *old_path, *new_path); - } else { - CHECK_EQ(argc, 4); - FSReqWrapSync req_wrap_sync; + } else { // rename(old_path, new_path) + FSReqWrapSync req_wrap_sync("rename", *old_path, *new_path); FS_SYNC_TRACE_BEGIN(rename); - SyncCall(env, args[3], &req_wrap_sync, "rename", uv_fs_rename, - *old_path, *new_path); + SyncCallAndThrowOnError( + env, &req_wrap_sync, uv_fs_rename, *old_path, *new_path); FS_SYNC_TRACE_END(rename); } } @@ -1834,28 +1828,27 @@ static void RealPath(const FunctionCallbackInfo& args) { Isolate* isolate = env->isolate(); const int argc = args.Length(); - CHECK_GE(argc, 3); + CHECK_GE(argc, 2); BufferValue path(isolate, args[0]); CHECK_NOT_NULL(*path); const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - if (req_wrap_async != nullptr) { // realpath(path, encoding, req) + if (argc > 2) { // realpath(path, encoding, req) + FSReqBase* req_wrap_async = GetReqWrap(args, 2); FS_ASYNC_TRACE_BEGIN1( UV_FS_REALPATH, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, req_wrap_async, args, "realpath", encoding, AfterStringPtr, uv_fs_realpath, *path); } else { // realpath(path, encoding, undefined, ctx) - CHECK_EQ(argc, 4); - FSReqWrapSync req_wrap_sync; + FSReqWrapSync req_wrap_sync("realpath", *path); FS_SYNC_TRACE_BEGIN(realpath); - int err = SyncCall(env, args[3], &req_wrap_sync, "realpath", - uv_fs_realpath, *path); + int err = + SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_realpath, *path); FS_SYNC_TRACE_END(realpath); if (err < 0) { - return; // syscall failed, no need to continue, error info is in ctx + return; } const char* link_path = static_cast(req_wrap_sync.req.ptr); @@ -1866,8 +1859,7 @@ static void RealPath(const FunctionCallbackInfo& args) { encoding, &error); if (rc.IsEmpty()) { - Local ctx = args[3].As(); - ctx->Set(env->context(), env->error_string(), error).Check(); + env->isolate()->ThrowException(error); return; } @@ -2573,18 +2565,16 @@ static void Chown(const FunctionCallbackInfo& args) { CHECK(IsSafeJsInt(args[2])); const uv_gid_t gid = static_cast(args[2].As()->Value()); - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - if (req_wrap_async != nullptr) { // chown(path, uid, gid, req) + if (argc > 3) { // chown(path, uid, gid, req) + FSReqBase* req_wrap_async = GetReqWrap(args, 3); FS_ASYNC_TRACE_BEGIN1( UV_FS_CHOWN, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, req_wrap_async, args, "chown", UTF8, AfterNoArgs, uv_fs_chown, *path, uid, gid); - } else { // chown(path, uid, gid, undefined, ctx) - CHECK_EQ(argc, 5); - FSReqWrapSync req_wrap_sync; + } else { // chown(path, uid, gid) + FSReqWrapSync req_wrap_sync("chown", *path); FS_SYNC_TRACE_BEGIN(chown); - SyncCall(env, args[4], &req_wrap_sync, "chown", - uv_fs_chown, *path, uid, gid); + SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_chown, *path, uid, gid); FS_SYNC_TRACE_END(chown); } } @@ -2641,18 +2631,16 @@ static void LChown(const FunctionCallbackInfo& args) { CHECK(IsSafeJsInt(args[2])); const uv_gid_t gid = static_cast(args[2].As()->Value()); - FSReqBase* req_wrap_async = GetReqWrap(args, 3); - if (req_wrap_async != nullptr) { // lchown(path, uid, gid, req) + if (argc > 3) { // lchown(path, uid, gid, req) + FSReqBase* req_wrap_async = GetReqWrap(args, 3); FS_ASYNC_TRACE_BEGIN1( UV_FS_LCHOWN, req_wrap_async, "path", TRACE_STR_COPY(*path)) AsyncCall(env, req_wrap_async, args, "lchown", UTF8, AfterNoArgs, uv_fs_lchown, *path, uid, gid); - } else { // lchown(path, uid, gid, undefined, ctx) - CHECK_EQ(argc, 5); - FSReqWrapSync req_wrap_sync; + } else { // lchown(path, uid, gid) + FSReqWrapSync req_wrap_sync("lchown", *path); FS_SYNC_TRACE_BEGIN(lchown); - SyncCall(env, args[4], &req_wrap_sync, "lchown", - uv_fs_lchown, *path, uid, gid); + SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_lchown, *path, uid, gid); FS_SYNC_TRACE_END(lchown); } } @@ -2765,27 +2753,26 @@ static void Mkdtemp(const FunctionCallbackInfo& args) { const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8); - FSReqBase* req_wrap_async = GetReqWrap(args, 2); - if (req_wrap_async != nullptr) { // mkdtemp(tmpl, encoding, req) + if (argc > 2) { // mkdtemp(tmpl, encoding, req) + FSReqBase* req_wrap_async = GetReqWrap(args, 2); FS_ASYNC_TRACE_BEGIN1( UV_FS_MKDTEMP, req_wrap_async, "path", TRACE_STR_COPY(*tmpl)) AsyncCall(env, req_wrap_async, args, "mkdtemp", encoding, AfterStringPath, uv_fs_mkdtemp, *tmpl); - } else { // mkdtemp(tmpl, encoding, undefined, ctx) - CHECK_EQ(argc, 4); - FSReqWrapSync req_wrap_sync; + } else { // mkdtemp(tmpl, encoding) + FSReqWrapSync req_wrap_sync("mkdtemp", *tmpl); FS_SYNC_TRACE_BEGIN(mkdtemp); - SyncCall(env, args[3], &req_wrap_sync, "mkdtemp", - uv_fs_mkdtemp, *tmpl); + int result = + SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_mkdtemp, *tmpl); FS_SYNC_TRACE_END(mkdtemp); - const char* path = req_wrap_sync.req.path; - + if (is_uv_error(result)) { + return; + } Local error; MaybeLocal rc = - StringBytes::Encode(isolate, path, encoding, &error); + StringBytes::Encode(isolate, req_wrap_sync.req.path, encoding, &error); if (rc.IsEmpty()) { - Local ctx = args[3].As(); - ctx->Set(env->context(), env->error_string(), error).Check(); + env->isolate()->ThrowException(error); return; } args.GetReturnValue().Set(rc.ToLocalChecked()); diff --git a/typings/internalBinding/fs.d.ts b/typings/internalBinding/fs.d.ts index c4dd973293ab22..6bed01519fa2b0 100644 --- a/typings/internalBinding/fs.d.ts +++ b/typings/internalBinding/fs.d.ts @@ -67,6 +67,7 @@ declare namespace InternalFSBinding { function chown(path: string, uid: number, gid: number, req: FSReqCallback): void; function chown(path: string, uid: number, gid: number, req: undefined, ctx: FSSyncContext): void; function chown(path: string, uid: number, gid: number, usePromises: typeof kUsePromises): Promise; + function chown(path: string, uid: number, gid: number): void; function close(fd: number, req: FSReqCallback): void; function close(fd: number, req: undefined, ctx: FSSyncContext): void; @@ -116,10 +117,12 @@ declare namespace InternalFSBinding { function lchown(path: string, uid: number, gid: number, req: FSReqCallback): void; function lchown(path: string, uid: number, gid: number, req: undefined, ctx: FSSyncContext): void; function lchown(path: string, uid: number, gid: number, usePromises: typeof kUsePromises): Promise; + function lchown(path: string, uid: number, gid: number): void; function link(existingPath: string, newPath: string, req: FSReqCallback): void; function link(existingPath: string, newPath: string, req: undefined, ctx: FSSyncContext): void; function link(existingPath: string, newPath: string, usePromises: typeof kUsePromises): Promise; + function link(existingPath: string, newPath: string): void; function lstat(path: StringOrBuffer, useBigint: boolean, req: FSReqCallback): void; function lstat(path: StringOrBuffer, useBigint: true, req: FSReqCallback): void; @@ -138,6 +141,7 @@ declare namespace InternalFSBinding { function mkdtemp(prefix: string, encoding: unknown, req: FSReqCallback): void; function mkdtemp(prefix: string, encoding: unknown, req: undefined, ctx: FSSyncContext): string; function mkdtemp(prefix: string, encoding: unknown, usePromises: typeof kUsePromises): Promise; + function mkdtemp(prefix: string, encoding: unknown): string; function mkdir(path: string, mode: number, recursive: boolean, req: FSReqCallback): void; function mkdir(path: string, mode: number, recursive: true, req: FSReqCallback): void; @@ -175,14 +179,17 @@ declare namespace InternalFSBinding { function readlink(path: StringOrBuffer, encoding: unknown, req: FSReqCallback): void; function readlink(path: StringOrBuffer, encoding: unknown, req: undefined, ctx: FSSyncContext): string | Buffer; function readlink(path: StringOrBuffer, encoding: unknown, usePromises: typeof kUsePromises): Promise; + function readlink(path: StringOrBuffer, encoding: unknown): StringOrBuffer; function realpath(path: StringOrBuffer, encoding: unknown, req: FSReqCallback): void; function realpath(path: StringOrBuffer, encoding: unknown, req: undefined, ctx: FSSyncContext): string | Buffer; function realpath(path: StringOrBuffer, encoding: unknown, usePromises: typeof kUsePromises): Promise; + function realpath(path: StringOrBuffer, encoding: unknown): StringOrBuffer; function rename(oldPath: string, newPath: string, req: FSReqCallback): void; function rename(oldPath: string, newPath: string, req: undefined, ctx: FSSyncContext): void; function rename(oldPath: string, newPath: string, usePromises: typeof kUsePromises): Promise; + function rename(oldPath: string, newPath: string): void; function rmdir(path: string, req: FSReqCallback): void; function rmdir(path: string, req: undefined, ctx: FSSyncContext): void; @@ -201,6 +208,7 @@ declare namespace InternalFSBinding { function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number, req: FSReqCallback): void; function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number, req: undefined, ctx: FSSyncContext): void; function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number, usePromises: typeof kUsePromises): Promise; + function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number): void; function unlink(path: string, req: FSReqCallback): void; function unlink(path: string): void;