Skip to content

Commit

Permalink
fs: allow setting Stat date properties
Browse files Browse the repository at this point in the history
PR-URL: nodejs#52708
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
nicolo-ribaudo authored and bmeck committed Jun 22, 2024
1 parent d380103 commit 4a85ae2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 16 deletions.
24 changes: 8 additions & 16 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,51 +461,43 @@ 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: {
__proto__: null,
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: {
__proto__: null,
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: {
__proto__: null,
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 });
},
},
};
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-fs-stat-bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
}
33 changes: 33 additions & 0 deletions test/parallel/test-fs-stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
}

0 comments on commit 4a85ae2

Please sign in to comment.