Skip to content

Commit 7b9cac1

Browse files
cliffhallclaude
andcommitted
fix(filesystem): create the copy owner-only, and keep the source unless the copy is in place
Review round 1 on #5322: - the copy is created 0600 and takes the source's mode only once it is complete (openInDirectory gains an optional creation mode, default unchanged), so a private file is never readable at the destination - before removing the source, the copy must still be at the destination (anchored lstat identity); if it was removed or replaced, the source stays and the move fails - a failure closing the source after a complete copy now removes the copy too, so a retry is not blocked by a leftover destination Rebased onto #5325 (#5154): the cross-device move's identity checks take BigInt stats in isSameEntry's (pathStat, handleStat) order, and the copy's identity is read again from its descriptor after the write, since isSameEntry's Windows dev-0 fallback compares size and mtime. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent e965a6f commit 7b9cac1

2 files changed

Lines changed: 217 additions & 31 deletions

File tree

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

Lines changed: 149 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,17 +519,22 @@ describe("moveFileAcrossDevices", () => {
519519
);
520520

521521
it.each([
522-
["another file", { ino: -1 }],
522+
["another file", { ino: -1n }],
523523
["not a regular file", { isFile: () => false }],
524524
])(
525525
"refuses, and closes the source, when it was replaced while being opened (%s)",
526526
async (_, replaced) => {
527527
const source = path.join(dir, "a.txt");
528528
fs.writeFileSync(source, "a");
529529
const realFstat = fs.fstatSync;
530-
vi.spyOn(fs, "fstatSync").mockImplementationOnce((fd) =>
531-
Object.assign(realFstat(fd), replaced),
532-
);
530+
vi.spyOn(fs, "fstatSync").mockImplementationOnce(((
531+
fd: number,
532+
options?: fs.StatOptions,
533+
) =>
534+
Object.assign(
535+
realFstat(fd, options),
536+
replaced,
537+
)) as typeof fs.fstatSync);
533538
const closeSync = vi.spyOn(fs, "closeSync");
534539
await expect(
535540
moveFileAcrossDevices(source, path.join(outside, "b.txt")),
@@ -555,6 +560,146 @@ describe("moveFileAcrossDevices", () => {
555560
},
556561
);
557562

563+
it.skipIf(process.platform === "win32")(
564+
"creates the copy owner-only while it is being written",
565+
async () => {
566+
const source = path.join(dir, "a.txt");
567+
const destination = path.join(outside, "b.txt");
568+
fs.writeFileSync(source, "a");
569+
fs.chmodSync(source, 0o644);
570+
const modes: number[] = [];
571+
const realWriteFile = fs.writeFile;
572+
vi.spyOn(fs, "writeFile").mockImplementationOnce(((
573+
...args: unknown[]
574+
) => {
575+
modes.push(fs.statSync(destination).mode & 0o777);
576+
return (realWriteFile as (...a: unknown[]) => void)(...args);
577+
}) as typeof fs.writeFile);
578+
await moveFileAcrossDevices(source, destination);
579+
expect(modes).toEqual([0o600]);
580+
expect(fs.statSync(destination).mode & 0o777).toBe(0o644);
581+
},
582+
);
583+
584+
it.each([
585+
["removed", (destination: string) => fs.unlinkSync(destination)],
586+
[
587+
"replaced",
588+
(destination: string) => {
589+
fs.unlinkSync(destination);
590+
fs.writeFileSync(destination, "someone else's");
591+
},
592+
],
593+
])(
594+
"keeps the source when the copy was %s during the move",
595+
async (_, interfere) => {
596+
const source = path.join(dir, "a.txt");
597+
const destination = path.join(outside, "b.txt");
598+
fs.writeFileSync(source, "a");
599+
const realFchmod = fs.fchmod;
600+
vi.spyOn(fs, "fchmod").mockImplementationOnce(((
601+
fd: number,
602+
mode: number,
603+
callback: (error: NodeJS.ErrnoException | null) => void,
604+
) => {
605+
interfere(destination);
606+
realFchmod(fd, mode, callback);
607+
}) as typeof fs.fchmod);
608+
await expect(moveFileAcrossDevices(source, destination)).rejects.toThrow(
609+
`Refusing to remove the source: ${destination} was removed or replaced during the move`,
610+
);
611+
expect(fs.readFileSync(source, "utf-8")).toBe("a");
612+
},
613+
);
614+
615+
it("reports a failure to check the copy, and keeps the source", async () => {
616+
const source = path.join(dir, "a.txt");
617+
const destination = path.join(outside, "b.txt");
618+
fs.writeFileSync(source, "a");
619+
const realLstat = fs.lstatSync;
620+
// The first lstat of the copy is the create's own check; fail the second.
621+
let checks = 0;
622+
vi.spyOn(fs, "lstatSync").mockImplementation(((
623+
p: fs.PathLike,
624+
options?: fs.StatSyncOptions,
625+
) => {
626+
if (process.cwd() === outside && p === "b.txt" && ++checks === 2) {
627+
throw errno("EIO");
628+
}
629+
return realLstat(p, options);
630+
}) as typeof fs.lstatSync);
631+
await expect(moveFileAcrossDevices(source, destination)).rejects.toThrow(
632+
"EIO",
633+
);
634+
// The third is discarding the copy, which goes ahead.
635+
expect(checks).toBe(3);
636+
expect(fs.readdirSync(outside)).toEqual([]);
637+
expect(fs.readFileSync(source, "utf-8")).toBe("a");
638+
});
639+
640+
it("removes the copy, and keeps the source, when closing the source fails", async () => {
641+
const source = path.join(dir, "a.txt");
642+
fs.writeFileSync(source, "a");
643+
const realClose = fs.close;
644+
let closes = 0;
645+
vi.spyOn(fs, "close").mockImplementation(((
646+
fd: number,
647+
callback: (error: NodeJS.ErrnoException | null) => void,
648+
) => {
649+
// The copy's descriptor closes first, then the source's.
650+
closes += 1;
651+
realClose(fd, (error) => callback(closes === 2 ? errno("EIO") : error));
652+
}) as typeof fs.close);
653+
await expect(
654+
moveFileAcrossDevices(source, path.join(outside, "b.txt")),
655+
).rejects.toThrow("EIO");
656+
expect(fs.readdirSync(outside)).toEqual([]);
657+
expect(fs.readFileSync(source, "utf-8")).toBe("a");
658+
});
659+
660+
it("checks the copy against its identity after the write, where Windows compares size and time", async () => {
661+
// libuv < 1.51 on Windows reports dev 0 on a path stat, so isSameEntry
662+
// compares size and mtime there: the identity from the create (empty)
663+
// would no longer match the finished copy.
664+
Object.defineProperty(process, "platform", { value: "win32" });
665+
const realLstat = fs.lstatSync;
666+
vi.spyOn(fs, "lstatSync").mockImplementation(((
667+
p: fs.PathLike,
668+
options?: fs.StatSyncOptions,
669+
) => {
670+
const stats = realLstat(p, options);
671+
return options?.bigint ? Object.assign(stats!, { dev: 0n }) : stats;
672+
}) as typeof fs.lstatSync);
673+
const source = path.join(dir, "a.txt");
674+
const destination = path.join(outside, "b.txt");
675+
fs.writeFileSync(source, "content");
676+
await moveFileAcrossDevices(source, destination);
677+
expect(fs.readFileSync(destination, "utf-8")).toBe("content");
678+
expect(fs.existsSync(source)).toBe(false);
679+
});
680+
681+
it("falls back to the create's identity when the copy cannot be stat'd before closing", async () => {
682+
const source = path.join(dir, "a.txt");
683+
const destination = path.join(outside, "b.txt");
684+
fs.writeFileSync(source, "a");
685+
const realFstat = fs.fstatSync;
686+
// The source's open and the copy's create stat first; fail the third.
687+
let calls = 0;
688+
vi.spyOn(fs, "fstatSync").mockImplementation(((
689+
fd: number,
690+
options?: fs.StatOptions,
691+
) => {
692+
if (++calls === 3) {
693+
throw errno("EIO");
694+
}
695+
return realFstat(fd, options);
696+
}) as typeof fs.fstatSync);
697+
await moveFileAcrossDevices(source, destination);
698+
expect(calls).toBe(3);
699+
expect(fs.readFileSync(destination, "utf-8")).toBe("a");
700+
expect(fs.existsSync(source)).toBe(false);
701+
});
702+
558703
it("reports the copy's failure when removing the copy fails too", async () => {
559704
const source = path.join(dir, "a.txt");
560705
fs.writeFileSync(source, "a");

‎src/filesystem/pinned-directory.ts‎

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,14 @@ function openInDirectory(
208208
filePath: string,
209209
flags: number,
210210
refuseLink: boolean,
211+
mode: number = 0o666,
211212
): { fd: number; opened: fs.BigIntStats } {
212213
const name = path.basename(filePath);
213214
return inDirectory(path.dirname(filePath), () => {
214215
if (refuseLink && fs.lstatSync(name).isSymbolicLink()) {
215216
throw new Error(`Refusing to write: not a regular file: ${filePath}`);
216217
}
217-
const fd = fs.openSync(name, flags, 0o666);
218+
const fd = fs.openSync(name, flags, mode);
218219
try {
219220
const opened = fs.fstatSync(fd, { bigint: true });
220221
if (!isSameEntry(fs.lstatSync(name, { bigint: true }), opened)) {
@@ -319,9 +320,9 @@ function writeChunk(fd: number, chunk: Buffer): Promise<void> {
319320
);
320321
}
321322

322-
function keepModeAndTimes(fd: number, source: fs.Stats): Promise<void> {
323+
function keepModeAndTimes(fd: number, source: fs.BigIntStats): Promise<void> {
323324
return new Promise((resolve, reject) =>
324-
fs.fchmod(fd, source.mode & 0o777, (chmodError) =>
325+
fs.fchmod(fd, Number(source.mode & 0o777n), (chmodError) =>
325326
chmodError
326327
? reject(chmodError)
327328
: fs.futimes(fd, source.atime, source.mtime, (timesError) =>
@@ -339,11 +340,11 @@ function keepModeAndTimes(fd: number, source: fs.Stats): Promise<void> {
339340
*/
340341
function openSourceInDirectory(sourcePath: string): {
341342
fd: number;
342-
opened: fs.Stats;
343+
opened: fs.BigIntStats;
343344
} {
344345
const name = path.basename(sourcePath);
345346
return inDirectory(path.dirname(sourcePath), () => {
346-
const found = fs.lstatSync(name);
347+
const found = fs.lstatSync(name, { bigint: true });
347348
if (found.isDirectory()) {
348349
throw new Error(
349350
`Cannot move a directory across filesystems: ${sourcePath}. ` +
@@ -357,8 +358,8 @@ function openSourceInDirectory(sourcePath: string): {
357358
}
358359
const fd = fs.openSync(name, readFlags());
359360
try {
360-
const opened = fs.fstatSync(fd);
361-
if (!opened.isFile() || !isSameEntry(opened, found)) {
361+
const opened = fs.fstatSync(fd, { bigint: true });
362+
if (!opened.isFile() || !isSameEntry(found, opened)) {
362363
throw new Error(
363364
`Refusing to move: ${sourcePath} was replaced while it was being opened`,
364365
);
@@ -372,16 +373,38 @@ function openSourceInDirectory(sourcePath: string): {
372373
}
373374

374375
/**
375-
* Remove `filePath`, relative to its anchored directory, only while it is
376-
* still the entry `expected` describes.
376+
* Check that `filePath`, relative to its anchored directory, is still the
377+
* entry `expected` describes, and fail with `message` if it is not.
377378
*/
378-
function removeFromDirectory(
379+
function confirmInDirectory(
379380
filePath: string,
380-
expected: { dev: number; ino: number },
381+
expected: EntryIdentity,
382+
message: string,
381383
): void {
382384
const name = path.basename(filePath);
383385
inDirectory(path.dirname(filePath), () => {
384-
if (!isSameEntry(fs.lstatSync(name), expected)) {
386+
let found: fs.BigIntStats | undefined;
387+
try {
388+
found = fs.lstatSync(name, { bigint: true });
389+
} catch (error) {
390+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
391+
throw error;
392+
}
393+
}
394+
if (!found || !isSameEntry(found, expected)) {
395+
throw new Error(message);
396+
}
397+
});
398+
}
399+
400+
/**
401+
* Remove `filePath`, relative to its anchored directory, only while it is
402+
* still the entry `expected` describes.
403+
*/
404+
function removeFromDirectory(filePath: string, expected: EntryIdentity): void {
405+
const name = path.basename(filePath);
406+
inDirectory(path.dirname(filePath), () => {
407+
if (!isSameEntry(fs.lstatSync(name, { bigint: true }), expected)) {
385408
throw new Error(
386409
`Refusing to remove: ${filePath} was replaced during the move`,
387410
);
@@ -391,10 +414,7 @@ function removeFromDirectory(
391414
}
392415

393416
/** Remove a copy this move created, keeping the error that made it necessary. */
394-
function discardCopy(
395-
destinationPath: string,
396-
created: { dev: number; ino: number },
397-
): void {
417+
function discardCopy(destinationPath: string, created: EntryIdentity): void {
398418
try {
399419
removeFromDirectory(destinationPath, created);
400420
} catch {
@@ -408,9 +428,9 @@ function discardCopy(
408428
*/
409429
async function copyIntoNewFile(
410430
sourceFd: number,
411-
source: fs.Stats,
431+
source: fs.BigIntStats,
412432
destinationPath: string,
413-
): Promise<fs.Stats> {
433+
): Promise<EntryIdentity> {
414434
// O_EXCL: an existing file or symlink fails with EEXIST, never followed.
415435
const { fd, opened } = openInDirectory(
416436
destinationPath,
@@ -419,7 +439,14 @@ async function copyIntoNewFile(
419439
fs.constants.O_EXCL |
420440
noFollow(),
421441
false,
442+
// Owner-only until the copy is complete and takes the source's mode, so
443+
// a private file is never readable at the destination in between.
444+
0o600,
422445
);
446+
// Where isSameEntry falls back to size and modification time (see there),
447+
// the identity from the create goes stale as the copy is written, so it is
448+
// read again from the descriptor before that closes.
449+
let identity: EntryIdentity = opened;
423450
try {
424451
try {
425452
const buffer = Buffer.alloc(64 * 1024);
@@ -432,13 +459,18 @@ async function copyIntoNewFile(
432459
}
433460
await keepModeAndTimes(fd, source);
434461
} finally {
462+
try {
463+
identity = fs.fstatSync(fd, { bigint: true });
464+
} catch {
465+
// Keep the identity from the create; the close reports the fault.
466+
}
435467
await close(fd);
436468
}
437469
} catch (error) {
438-
discardCopy(destinationPath, opened);
470+
discardCopy(destinationPath, identity);
439471
throw error;
440472
}
441-
return opened;
473+
return identity;
442474
}
443475

444476
/**
@@ -452,16 +484,25 @@ export async function moveFileAcrossDevices(
452484
destinationPath: string,
453485
): Promise<void> {
454486
const { fd, opened: source } = openSourceInDirectory(sourcePath);
455-
let created: fs.Stats;
456-
try {
457-
created = await copyIntoNewFile(fd, source, destinationPath);
458-
} finally {
459-
await close(fd);
460-
}
487+
// Set once the copy is complete; until then copyIntoNewFile cleans up.
488+
let created: EntryIdentity | undefined;
461489
try {
490+
try {
491+
created = await copyIntoNewFile(fd, source, destinationPath);
492+
} finally {
493+
await close(fd);
494+
}
495+
// The source goes only while the copy is still where the move put it.
496+
confirmInDirectory(
497+
destinationPath,
498+
created,
499+
`Refusing to remove the source: ${destinationPath} was removed or replaced during the move`,
500+
);
462501
removeFromDirectory(sourcePath, source);
463502
} catch (error) {
464-
discardCopy(destinationPath, created);
503+
if (created) {
504+
discardCopy(destinationPath, created);
505+
}
465506
throw error;
466507
}
467508
}

0 commit comments

Comments
 (0)