Skip to content

Commit c5fda36

Browse files
cliffhallclaude
andcommitted
fix(filesystem): remove copy_file's copy when closing the source fails
A failure closing the source after the copy completed failed the call but left the destination behind, so a retry hit "Destination already exists". The copy is now discarded on that error too, as the cross-device move already does. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 65e68d3 commit c5fda36

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

‎src/filesystem/__tests__/copy-file.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,35 @@ describe("copy_file", () => {
164164
},
165165
);
166166

167+
it("removes the copy when closing the source fails after it", async () => {
168+
const source = path.join(dir, "a.txt");
169+
const destination = path.join(dir, "b.txt");
170+
await fs.writeFile(source, "data");
171+
// The copy closes the destination first, then the source: fail the
172+
// second close, after really closing the descriptor.
173+
const realClose = fsSync.close;
174+
let closes = 0;
175+
vi.spyOn(fsSync, "close").mockImplementation(((
176+
fd: number,
177+
callback: (error: NodeJS.ErrnoException | null) => void,
178+
) =>
179+
realClose(fd, (error) =>
180+
++closes === 2
181+
? callback(
182+
Object.assign(new Error("EIO: i/o error, close"), {
183+
code: "EIO",
184+
}),
185+
)
186+
: callback(error),
187+
)) as typeof fsSync.close);
188+
const result = await call(client, "copy_file", { source, destination });
189+
expect(closes).toBe(2);
190+
expect(result.isError).toBe(true);
191+
expect(textOf(result)).toBe("EIO: i/o error, close");
192+
expect(fsSync.existsSync(destination)).toBe(false);
193+
expect(await fs.readFile(source, "utf-8")).toBe("data");
194+
});
195+
167196
it("reports a missing source", async () => {
168197
const result = await call(client, "copy_file", {
169198
source: path.join(dir, "missing.txt"),

‎src/filesystem/pinned-directory.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,10 +557,21 @@ export async function copyFileInDirectory(
557557
destinationPath: string,
558558
): Promise<void> {
559559
const { fd, opened: source } = openSourceInDirectory(sourcePath, "copy");
560+
// Set once the copy is complete; until then copyIntoNewFile cleans up.
561+
let created: EntryIdentity | undefined;
560562
try {
561-
await copyIntoNewFile(fd, source, destinationPath);
562-
} finally {
563-
await close(fd);
563+
try {
564+
created = await copyIntoNewFile(fd, source, destinationPath);
565+
} finally {
566+
await close(fd);
567+
}
568+
} catch (error) {
569+
// A failure after the copy completed (closing the source) still fails
570+
// the call, so the copy goes too, as the move's does.
571+
if (created) {
572+
discardCopy(destinationPath, created);
573+
}
574+
throw error;
564575
}
565576
}
566577

0 commit comments

Comments
 (0)