Skip to content

Commit 7fabe2a

Browse files
cliffhallclaude
andcommitted
fix(filesystem): keep a pre-1970 time on a cross-device copy
Review round 5 on #5322: fs.futimes takes a negative number to mean "now", so the seconds passed since round 4 lost any time before 1970. Pass them as a numeric string, which it parses as given. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 215815d commit 7fabe2a

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

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

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

460-
it("keeps the modification time below a millisecond", async () => {
460+
it.each([
461+
// 0.5 ms past a whole millisecond: a Date would drop it.
462+
["below a millisecond", 1_000_000_000.1235],
463+
// Before 1970: futimes takes a negative number to mean "now".
464+
["before 1970", new Date(-157_766_400_250)],
465+
])("keeps the modification time %s", async (_, time) => {
461466
const source = path.join(dir, "a.txt");
462467
const destination = path.join(outside, "b.txt");
463468
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);
469+
fs.utimesSync(source, time, time);
466470
const before = fs.statSync(source, { bigint: true }).mtimeNs;
467471
await moveFileAcrossDevices(source, destination);
468472
const after = fs.statSync(destination, { bigint: true }).mtimeNs;

‎src/filesystem/pinned-directory.ts‎

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

323-
function seconds(nanoseconds: bigint): number {
324-
return Number(nanoseconds) / 1e9;
323+
// A numeric string, since fs.futimes takes a negative number to mean "now",
324+
// which would lose a time before 1970; a string it parses as given.
325+
function seconds(nanoseconds: bigint): string {
326+
return String(Number(nanoseconds) / 1e9);
325327
}
326328

327329
function keepModeAndTimes(fd: number, source: fs.BigIntStats): Promise<void> {

0 commit comments

Comments
 (0)