Skip to content

Commit c6e0fe2

Browse files
nicolo-ribaudoaduh95
authored andcommitted
fs: allow setting Stat date properties
PR-URL: #52708 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: LiviaMedeiros <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Raz Luvaton <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 27a5f94 commit c6e0fe2

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
lines changed

lib/internal/fs/utils.js

+8-16
Original file line numberDiff line numberDiff line change
@@ -461,51 +461,43 @@ const lazyDateFields = {
461461
enumerable: true,
462462
configurable: true,
463463
get() {
464-
const value = dateFromMs(this.atimeMs);
465-
ObjectDefineProperty(this, 'atime', { __proto__: null, value });
466-
return this.atime;
464+
return this.atime = dateFromMs(this.atimeMs);
467465
},
468466
set(value) {
469-
this.atime = value;
467+
ObjectDefineProperty(this, 'atime', { __proto__: null, value, writable: true });
470468
},
471469
},
472470
mtime: {
473471
__proto__: null,
474472
enumerable: true,
475473
configurable: true,
476474
get() {
477-
const value = dateFromMs(this.mtimeMs);
478-
ObjectDefineProperty(this, 'mtime', { __proto__: null, value });
479-
return this.mtime;
475+
return this.mtime = dateFromMs(this.mtimeMs);
480476
},
481477
set(value) {
482-
this.mtime = value;
478+
ObjectDefineProperty(this, 'mtime', { __proto__: null, value, writable: true });
483479
},
484480
},
485481
ctime: {
486482
__proto__: null,
487483
enumerable: true,
488484
configurable: true,
489485
get() {
490-
const value = dateFromMs(this.ctimeMs);
491-
ObjectDefineProperty(this, 'ctime', { __proto__: null, value });
492-
return this.ctime;
486+
return this.ctime = dateFromMs(this.ctimeMs);
493487
},
494488
set(value) {
495-
this.ctime = value;
489+
ObjectDefineProperty(this, 'ctime', { __proto__: null, value, writable: true });
496490
},
497491
},
498492
birthtime: {
499493
__proto__: null,
500494
enumerable: true,
501495
configurable: true,
502496
get() {
503-
const value = dateFromMs(this.birthtimeMs);
504-
ObjectDefineProperty(this, 'birthtime', { __proto__: null, value });
505-
return this.birthtime;
497+
return this.birthtime = dateFromMs(this.birthtimeMs);
506498
},
507499
set(value) {
508-
this.birthtime = value;
500+
ObjectDefineProperty(this, 'birthtime', { __proto__: null, value, writable: true });
509501
},
510502
},
511503
};

test/parallel/test-fs-stat-bigint.js

+33
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,36 @@ if (!common.isWindows) {
212212
verifyStats(bigintStats, numStats, allowableDelta);
213213
await handle.close();
214214
})().then(common.mustCall());
215+
216+
{
217+
// These two tests have an equivalent in ./test-fs-stat.js
218+
219+
// BigIntStats Date properties can be set before reading them
220+
fs.stat(__filename, { bigint: true }, common.mustSucceed((s) => {
221+
s.atime = 2;
222+
s.mtime = 3;
223+
s.ctime = 4;
224+
s.birthtime = 5;
225+
226+
assert.strictEqual(s.atime, 2);
227+
assert.strictEqual(s.mtime, 3);
228+
assert.strictEqual(s.ctime, 4);
229+
assert.strictEqual(s.birthtime, 5);
230+
}));
231+
232+
// BigIntStats Date properties can be set after reading them
233+
fs.stat(__filename, { bigint: true }, common.mustSucceed((s) => {
234+
// eslint-disable-next-line no-unused-expressions
235+
s.atime, s.mtime, s.ctime, s.birthtime;
236+
237+
s.atime = 2;
238+
s.mtime = 3;
239+
s.ctime = 4;
240+
s.birthtime = 5;
241+
242+
assert.strictEqual(s.atime, 2);
243+
assert.strictEqual(s.mtime, 3);
244+
assert.strictEqual(s.ctime, 4);
245+
assert.strictEqual(s.birthtime, 5);
246+
}));
247+
}

test/parallel/test-fs-stat.js

+33
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,36 @@ fs.lstat(__filename, undefined, common.mustCall());
179179
]
180180
});
181181
}
182+
183+
{
184+
// These two tests have an equivalent in ./test-fs-stat-bigint.js
185+
186+
// Stats Date properties can be set before reading them
187+
fs.stat(__filename, common.mustSucceed((s) => {
188+
s.atime = 2;
189+
s.mtime = 3;
190+
s.ctime = 4;
191+
s.birthtime = 5;
192+
193+
assert.strictEqual(s.atime, 2);
194+
assert.strictEqual(s.mtime, 3);
195+
assert.strictEqual(s.ctime, 4);
196+
assert.strictEqual(s.birthtime, 5);
197+
}));
198+
199+
// Stats Date properties can be set after reading them
200+
fs.stat(__filename, common.mustSucceed((s) => {
201+
// eslint-disable-next-line no-unused-expressions
202+
s.atime, s.mtime, s.ctime, s.birthtime;
203+
204+
s.atime = 2;
205+
s.mtime = 3;
206+
s.ctime = 4;
207+
s.birthtime = 5;
208+
209+
assert.strictEqual(s.atime, 2);
210+
assert.strictEqual(s.mtime, 3);
211+
assert.strictEqual(s.ctime, 4);
212+
assert.strictEqual(s.birthtime, 5);
213+
}));
214+
}

0 commit comments

Comments
 (0)