Skip to content

Commit b3a7df7

Browse files
committed
fs: throw errors from fs.ftruncateSync in JS
PR-URL: #18348 Refs: #18106 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 5583981 commit b3a7df7

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

lib/fs.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,11 @@ fs.ftruncateSync = function(fd, len = 0) {
995995
validateUint32(fd, 'fd');
996996
validateLen(len);
997997
len = Math.max(0, len);
998-
return binding.ftruncate(fd, len);
998+
const ctx = {};
999+
binding.ftruncate(fd, len, undefined, ctx);
1000+
if (ctx.errno !== undefined) {
1001+
throw new errors.uvException(ctx);
1002+
}
9991003
};
10001004

10011005
fs.rmdir = function(path, callback) {

src/node_file.cc

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -711,18 +711,24 @@ static void Rename(const FunctionCallbackInfo<Value>& args) {
711711
static void FTruncate(const FunctionCallbackInfo<Value>& args) {
712712
Environment* env = Environment::GetCurrent(args);
713713

714+
const int argc = args.Length();
715+
CHECK_GE(argc, 3);
716+
714717
CHECK(args[0]->IsInt32());
715-
CHECK(args[1]->IsNumber());
718+
const int fd = args[0].As<Int32>()->Value();
716719

717-
int fd = args[0]->Int32Value();
718-
const int64_t len = args[1]->IntegerValue();
720+
CHECK(args[1]->IsNumber());
721+
const int64_t len = args[1].As<Integer>()->Value();
719722

720-
if (args[2]->IsObject()) {
721-
CHECK_EQ(args.Length(), 3);
723+
if (args[2]->IsObject()) { // ftruncate(fd, len, req)
724+
CHECK_EQ(argc, 3);
722725
AsyncCall(env, args, "ftruncate", UTF8, AfterNoArgs,
723726
uv_fs_ftruncate, fd, len);
724-
} else {
725-
SYNC_CALL(ftruncate, 0, fd, len)
727+
} else { // ftruncate(fd, len, undefined, ctx)
728+
CHECK_EQ(argc, 4);
729+
fs_req_wrap req_wrap;
730+
SyncCall(env, args[3], &req_wrap, "ftruncate",
731+
uv_fs_ftruncate, fd, len);
726732
}
727733
}
728734

test/parallel/test-fs-error-messages.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,30 @@ function re(literals, ...values) {
455455
validateError
456456
);
457457
}
458+
459+
// ftruncate
460+
{
461+
const validateError = (err) => {
462+
assert.strictEqual(err.syscall, 'ftruncate');
463+
// Could be EBADF or EINVAL, depending on the platform
464+
if (err.code === 'EBADF') {
465+
assert.strictEqual(err.message, 'EBADF: bad file descriptor, ftruncate');
466+
assert.strictEqual(err.errno, uv.UV_EBADF);
467+
} else {
468+
assert.strictEqual(err.message, 'EINVAL: invalid argument, ftruncate');
469+
assert.strictEqual(err.errno, uv.UV_EINVAL);
470+
assert.strictEqual(err.code, 'EINVAL');
471+
}
472+
return true;
473+
};
474+
475+
const fd = fs.openSync(existingFile, 'r');
476+
fs.closeSync(fd);
477+
478+
fs.ftruncate(fd, 4, common.mustCall(validateError));
479+
480+
assert.throws(
481+
() => fs.ftruncateSync(fd, 4),
482+
validateError
483+
);
484+
}

0 commit comments

Comments
 (0)