Skip to content

Commit 215815d

Browse files
cliffhallclaude
andcommitted
fix(filesystem): keep sub-millisecond times on a cross-device copy
Review round 4 on #5322: futimes was given the source's Date times, which keep only milliseconds. Pass seconds derived from atimeNs and mtimeNs instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 1860bb1 commit 215815d

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎src/filesystem/__tests__/pinned-directory.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,19 @@ describe("moveFileAcrossDevices", () => {
457457
expect(process.cwd()).toBe(cwd);
458458
});
459459

460+
it("keeps the modification time below a millisecond", async () => {
461+
const source = path.join(dir, "a.txt");
462+
const destination = path.join(outside, "b.txt");
463+
fs.writeFileSync(source, "a");
464+
// 0.5 ms past a whole millisecond: a Date would drop it.
465+
fs.utimesSync(source, 1_000_000_000.1235, 1_000_000_000.1235);
466+
const before = fs.statSync(source, { bigint: true }).mtimeNs;
467+
await moveFileAcrossDevices(source, destination);
468+
const after = fs.statSync(destination, { bigint: true }).mtimeNs;
469+
const drift = after > before ? after - before : before - after;
470+
expect(drift < 1000n).toBe(true);
471+
});
472+
460473
it("moves an empty file", async () => {
461474
const source = path.join(dir, "empty");
462475
fs.writeFileSync(source, "");

‎src/filesystem/pinned-directory.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,22 @@ function writeChunk(fd: number, chunk: Buffer): Promise<void> {
320320
);
321321
}
322322

323+
function seconds(nanoseconds: bigint): number {
324+
return Number(nanoseconds) / 1e9;
325+
}
326+
323327
function keepModeAndTimes(fd: number, source: fs.BigIntStats): Promise<void> {
324328
return new Promise((resolve, reject) =>
325329
fs.fchmod(fd, Number(source.mode & 0o777n), (chmodError) =>
326330
chmodError
327331
? reject(chmodError)
328-
: fs.futimes(fd, source.atime, source.mtime, (timesError) =>
329-
timesError ? reject(timesError) : resolve(),
332+
: // In seconds from the nanosecond fields: a Date keeps only
333+
// milliseconds.
334+
fs.futimes(
335+
fd,
336+
seconds(source.atimeNs),
337+
seconds(source.mtimeNs),
338+
(timesError) => (timesError ? reject(timesError) : resolve()),
330339
),
331340
),
332341
);

0 commit comments

Comments
 (0)