From c6e0fe2f22d62e298b9c1dd01e725f2244f1797c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sun, 28 Apr 2024 15:39:35 +0200 Subject: [PATCH] fs: allow setting Stat date properties PR-URL: https://github.com/nodejs/node/pull/52708 Reviewed-By: Yagiz Nizipli Reviewed-By: Joyee Cheung Reviewed-By: LiviaMedeiros Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Raz Luvaton Reviewed-By: Luigi Pinca --- lib/internal/fs/utils.js | 24 +++++++------------- test/parallel/test-fs-stat-bigint.js | 33 ++++++++++++++++++++++++++++ test/parallel/test-fs-stat.js | 33 ++++++++++++++++++++++++++++ 3 files changed, 74 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 }); }, }, }; 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 506c5e5fa97317..46874086d58dc7 100644 --- a/test/parallel/test-fs-stat.js +++ b/test/parallel/test-fs-stat.js @@ -179,3 +179,36 @@ fs.lstat(__filename, undefined, common.mustCall()); ] }); } + +{ + // 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); + })); + + // 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); + })); +}