From b591ff41dc31271b7c2bbe658ca9a08f274b530c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Fri, 26 Apr 2024 15:36:30 +0200 Subject: [PATCH 1/5] fs: allow setting Stat date properties --- lib/internal/fs/utils.js | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index ca15c986d3a4fb..808c0e1fb0b5fa 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -461,12 +461,10 @@ const lazyDateFields = { enumerable: true, configurable: true, get() { - const value = dateFromMs(this.atimeMs); - ObjectDefineProperty(this, 'atime', { __proto__: null, value }); - return this.atime; + return this.atime = dateFromMs(this.atimeMs); }, set(value) { - this.atime = value; + ObjectDefineProperty(this, 'atime', { __proto__: null, value, writable: true }); }, }, mtime: { @@ -474,12 +472,10 @@ const lazyDateFields = { enumerable: true, configurable: true, get() { - const value = dateFromMs(this.mtimeMs); - ObjectDefineProperty(this, 'mtime', { __proto__: null, value }); - return this.mtime; + return this.mtime = dateFromMs(this.mtimeMs); }, set(value) { - this.mtime = value; + ObjectDefineProperty(this, 'mtime', { __proto__: null, value, writable: true }); }, }, ctime: { @@ -487,12 +483,10 @@ const lazyDateFields = { enumerable: true, configurable: true, get() { - const value = dateFromMs(this.ctimeMs); - ObjectDefineProperty(this, 'ctime', { __proto__: null, value }); - return this.ctime; + return this.ctime = dateFromMs(this.ctimeMs); }, set(value) { - this.ctime = value; + ObjectDefineProperty(this, 'ctime', { __proto__: null, value, writable: true }); }, }, birthtime: { @@ -500,12 +494,10 @@ const lazyDateFields = { enumerable: true, configurable: true, get() { - const value = dateFromMs(this.birthtimeMs); - ObjectDefineProperty(this, 'birthtime', { __proto__: null, value }); - return this.birthtime; + return this.birthtime = dateFromMs(this.birthtimeMs); }, set(value) { - this.birthtime = value; + ObjectDefineProperty(this, 'birthtime', { __proto__: null, value, writable: true }); }, }, }; From cac9a301b920f0296621d6165dba2b36b0b2706d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Fri, 26 Apr 2024 15:42:04 +0200 Subject: [PATCH 2/5] Add test --- test/parallel/test-fs-stat.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/parallel/test-fs-stat.js b/test/parallel/test-fs-stat.js index 506c5e5fa97317..48589b49504658 100644 --- a/test/parallel/test-fs-stat.js +++ b/test/parallel/test-fs-stat.js @@ -179,3 +179,34 @@ fs.lstat(__filename, undefined, common.mustCall()); ] }); } + +// Stat Date properties can be set before reading them +fs.stat(__filename, common.mustSucceed((s) => { + s.atime = 2; + s.mtime = 3; + s.ctime = 4; + s.birthtime = 5; + + assert.strictEqual(s.atime, 2); + assert.strictEqual(s.mtime, 3); + assert.strictEqual(s.ctime, 4); + assert.strictEqual(s.birthtime, 5); +})); + +// Stat Date properties can be set after reading them +fs.stat(__filename, common.mustSucceed((s) => { + s.atime; + s.mtime; + s.ctime; + s.birthtime; + + s.atime = 2; + s.mtime = 3; + s.ctime = 4; + s.birthtime = 5; + + assert.strictEqual(s.atime, 2); + assert.strictEqual(s.mtime, 3); + assert.strictEqual(s.ctime, 4); + assert.strictEqual(s.birthtime, 5); +})); From 5b552c91bf1deadfdb77a3d6ab60650f5c4a0704 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Fri, 26 Apr 2024 15:47:17 +0200 Subject: [PATCH 3/5] Make linter happy --- test/parallel/test-fs-stat.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-fs-stat.js b/test/parallel/test-fs-stat.js index 48589b49504658..94dadcd9173e29 100644 --- a/test/parallel/test-fs-stat.js +++ b/test/parallel/test-fs-stat.js @@ -195,10 +195,11 @@ fs.stat(__filename, common.mustSucceed((s) => { // Stat Date properties can be set after reading them fs.stat(__filename, common.mustSucceed((s) => { - s.atime; - s.mtime; - s.ctime; - s.birthtime; + var _; + _ = s.atime; + _ = s.mtime; + _ = s.ctime; + _ = s.birthtime; s.atime = 2; s.mtime = 3; From 960cbef3c625872948edbcb63ca4a1ef4110bfab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Fri, 26 Apr 2024 15:53:22 +0200 Subject: [PATCH 4/5] Make linter happier --- test/parallel/test-fs-stat.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/test/parallel/test-fs-stat.js b/test/parallel/test-fs-stat.js index 94dadcd9173e29..4479f8cda85394 100644 --- a/test/parallel/test-fs-stat.js +++ b/test/parallel/test-fs-stat.js @@ -195,11 +195,8 @@ fs.stat(__filename, common.mustSucceed((s) => { // Stat Date properties can be set after reading them fs.stat(__filename, common.mustSucceed((s) => { - var _; - _ = s.atime; - _ = s.mtime; - _ = s.ctime; - _ = s.birthtime; + // eslint-disable-next-line no-unused-expressions + s.atime, s.mtime, s.ctime, s.birthtime; s.atime = 2; s.mtime = 3; From c5107f2f7f573e71e1779cb438bcb0f1148ece09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Fri, 26 Apr 2024 16:45:56 +0200 Subject: [PATCH 5/5] Add test for BigIntStats --- test/parallel/test-fs-stat-bigint.js | 33 ++++++++++++++++ test/parallel/test-fs-stat.js | 58 +++++++++++++++------------- 2 files changed, 64 insertions(+), 27 deletions(-) diff --git a/test/parallel/test-fs-stat-bigint.js b/test/parallel/test-fs-stat-bigint.js index d1bddaa48c66f8..0a2bea92e5018c 100644 --- a/test/parallel/test-fs-stat-bigint.js +++ b/test/parallel/test-fs-stat-bigint.js @@ -212,3 +212,36 @@ if (!common.isWindows) { verifyStats(bigintStats, numStats, allowableDelta); await handle.close(); })().then(common.mustCall()); + +{ + // These two tests have an equivalent in ./test-fs-stat.js + + // BigIntStats Date properties can be set before reading them + fs.stat(__filename, { bigint: true }, common.mustSucceed((s) => { + s.atime = 2; + s.mtime = 3; + s.ctime = 4; + s.birthtime = 5; + + assert.strictEqual(s.atime, 2); + assert.strictEqual(s.mtime, 3); + assert.strictEqual(s.ctime, 4); + assert.strictEqual(s.birthtime, 5); + })); + + // BigIntStats Date properties can be set after reading them + fs.stat(__filename, { bigint: true }, common.mustSucceed((s) => { + // eslint-disable-next-line no-unused-expressions + s.atime, s.mtime, s.ctime, s.birthtime; + + s.atime = 2; + s.mtime = 3; + s.ctime = 4; + s.birthtime = 5; + + assert.strictEqual(s.atime, 2); + assert.strictEqual(s.mtime, 3); + assert.strictEqual(s.ctime, 4); + assert.strictEqual(s.birthtime, 5); + })); +} diff --git a/test/parallel/test-fs-stat.js b/test/parallel/test-fs-stat.js index 4479f8cda85394..46874086d58dc7 100644 --- a/test/parallel/test-fs-stat.js +++ b/test/parallel/test-fs-stat.js @@ -180,31 +180,35 @@ fs.lstat(__filename, undefined, common.mustCall()); }); } -// Stat Date properties can be set before reading them -fs.stat(__filename, common.mustSucceed((s) => { - s.atime = 2; - s.mtime = 3; - s.ctime = 4; - s.birthtime = 5; - - assert.strictEqual(s.atime, 2); - assert.strictEqual(s.mtime, 3); - assert.strictEqual(s.ctime, 4); - assert.strictEqual(s.birthtime, 5); -})); +{ + // These two tests have an equivalent in ./test-fs-stat-bigint.js + + // Stats Date properties can be set before reading them + fs.stat(__filename, common.mustSucceed((s) => { + s.atime = 2; + s.mtime = 3; + s.ctime = 4; + s.birthtime = 5; + + assert.strictEqual(s.atime, 2); + assert.strictEqual(s.mtime, 3); + assert.strictEqual(s.ctime, 4); + assert.strictEqual(s.birthtime, 5); + })); -// Stat Date properties can be set after reading them -fs.stat(__filename, common.mustSucceed((s) => { - // eslint-disable-next-line no-unused-expressions - s.atime, s.mtime, s.ctime, s.birthtime; - - s.atime = 2; - s.mtime = 3; - s.ctime = 4; - s.birthtime = 5; - - assert.strictEqual(s.atime, 2); - assert.strictEqual(s.mtime, 3); - assert.strictEqual(s.ctime, 4); - assert.strictEqual(s.birthtime, 5); -})); + // Stats Date properties can be set after reading them + fs.stat(__filename, common.mustSucceed((s) => { + // eslint-disable-next-line no-unused-expressions + s.atime, s.mtime, s.ctime, s.birthtime; + + s.atime = 2; + s.mtime = 3; + s.ctime = 4; + s.birthtime = 5; + + assert.strictEqual(s.atime, 2); + assert.strictEqual(s.mtime, 3); + assert.strictEqual(s.ctime, 4); + assert.strictEqual(s.birthtime, 5); + })); +}