Skip to content

Commit 1860bb1

Browse files
cliffhallclaude
andcommitted
fix(filesystem): keep a source written to in place during a cross-device move
Review round 3 on #5322: the source's device and inode still match after another writer edits it in place, so the move would delete the edited source while the destination held the content read before the edit. Before the source is removed, its size, mtime and ctime (ns) must also match the stat taken when it was opened; otherwise the copy is removed and the source kept. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 790d7cd commit 1860bb1

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,29 @@ describe("moveFileAcrossDevices", () => {
700700
expect(fs.existsSync(source)).toBe(false);
701701
});
702702

703+
it("removes the copy, and keeps the source, when the source was written to in place during the move", async () => {
704+
const source = path.join(dir, "a.txt");
705+
const destination = path.join(outside, "b.txt");
706+
fs.writeFileSync(source, "a");
707+
const before = fs.lstatSync(source).ino;
708+
const realFchmod = fs.fchmod;
709+
// fchmod runs once the copy has read to the end of the source.
710+
vi.spyOn(fs, "fchmod").mockImplementationOnce(((
711+
fd: number,
712+
mode: number,
713+
callback: (error: NodeJS.ErrnoException | null) => void,
714+
) => {
715+
fs.appendFileSync(source, " and more");
716+
realFchmod(fd, mode, callback);
717+
}) as typeof fs.fchmod);
718+
await expect(moveFileAcrossDevices(source, destination)).rejects.toThrow(
719+
`Refusing to remove: ${source} changed during the move`,
720+
);
721+
expect(fs.lstatSync(source).ino).toBe(before);
722+
expect(fs.readFileSync(source, "utf-8")).toBe("a and more");
723+
expect(fs.readdirSync(outside)).toEqual([]);
724+
});
725+
703726
it("reports the copy's failure when removing the copy fails too", async () => {
704727
const source = path.join(dir, "a.txt");
705728
fs.writeFileSync(source, "a");

‎src/filesystem/pinned-directory.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,14 +401,31 @@ function confirmInDirectory(
401401
* Remove `filePath`, relative to its anchored directory, only while it is
402402
* still the entry `expected` describes.
403403
*/
404-
function removeFromDirectory(filePath: string, expected: EntryIdentity): void {
404+
function removeFromDirectory(
405+
filePath: string,
406+
expected: EntryIdentity,
407+
snapshot?: Pick<fs.BigIntStats, "size" | "mtimeNs" | "ctimeNs">,
408+
): void {
405409
const name = path.basename(filePath);
406410
inDirectory(path.dirname(filePath), () => {
407-
if (!isSameEntry(fs.lstatSync(name, { bigint: true }), expected)) {
411+
const found = fs.lstatSync(name, { bigint: true });
412+
if (!isSameEntry(found, expected)) {
408413
throw new Error(
409414
`Refusing to remove: ${filePath} was replaced during the move`,
410415
);
411416
}
417+
// The same file, written to in place after it was copied: removing it
418+
// would lose what was written, so the copy is the one that goes.
419+
if (
420+
snapshot &&
421+
(found.size !== snapshot.size ||
422+
found.mtimeNs !== snapshot.mtimeNs ||
423+
found.ctimeNs !== snapshot.ctimeNs)
424+
) {
425+
throw new Error(
426+
`Refusing to remove: ${filePath} changed during the move`,
427+
);
428+
}
412429
fs.unlinkSync(name);
413430
});
414431
}
@@ -498,7 +515,7 @@ export async function moveFileAcrossDevices(
498515
created,
499516
`Refusing to remove the source: ${destinationPath} was removed or replaced during the move`,
500517
);
501-
removeFromDirectory(sourcePath, source);
518+
removeFromDirectory(sourcePath, source, source);
502519
} catch (error) {
503520
if (created) {
504521
discardCopy(destinationPath, created);

0 commit comments

Comments
 (0)