Skip to content

Commit 2c2be3e

Browse files
cliffhallclaude
andcommitted
fix(filesystem): open append_file's new-file write with O_APPEND
append_file creates a missing file through the exclusive-create path, whose descriptor was not O_APPEND: another append reaching the new file before the creating write would be overwritten at offset 0. The create now takes an `append` flag, which only append_file sets. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent af8a01b commit 2c2be3e

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,32 @@ describe("append_file", () => {
9393
expect(await fs.readFile(file, "utf-8")).toBe("hello");
9494
});
9595

96+
it("keeps an append that lands on a new file before the creating write", async () => {
97+
const file = path.join(dir, "race.txt");
98+
// The creating call's write is held back until another caller's append
99+
// has reached the new file; without O_APPEND the held write would land at
100+
// offset 0 over it.
101+
const realWriteFile = fsSync.writeFile;
102+
let raced = false;
103+
vi.spyOn(fsSync, "writeFile").mockImplementation(((
104+
target: number,
105+
...rest: unknown[]
106+
) => {
107+
if (!raced && typeof target === "number") {
108+
raced = true;
109+
fsSync.appendFileSync(file, "SECOND");
110+
}
111+
return (realWriteFile as (...args: unknown[]) => void)(target, ...rest);
112+
}) as typeof fsSync.writeFile);
113+
const result = await call(client, "append_file", {
114+
path: file,
115+
content: "FIRST",
116+
});
117+
expect(result.isError).toBeFalsy();
118+
expect(raced).toBe(true);
119+
expect(await fs.readFile(file, "utf-8")).toBe("SECONDFIRST");
120+
});
121+
96122
it("leaves a file unchanged when the content is empty", async () => {
97123
const file = path.join(dir, "same.txt");
98124
await fs.writeFile(file, "kept");

‎src/filesystem/lib.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,9 @@ export async function appendFileContent(
632632
content: string,
633633
): Promise<void> {
634634
try {
635-
await createFileInDirectory(filePath, content);
635+
// O_APPEND here too: another append can reach the file between its
636+
// creation and this write, and must not be overwritten.
637+
await createFileInDirectory(filePath, content, true);
636638
return;
637639
} catch (error) {
638640
if ((error as NodeJS.ErrnoException).code !== "EEXIST") {

‎src/filesystem/pinned-directory.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,23 @@ function openInDirectory(
229229
});
230230
}
231231

232-
/** Create `filePath`, which must not exist, and write `content` to it. */
232+
/**
233+
* Create `filePath`, which must not exist, and write `content` to it. With
234+
* `append`, the descriptor is opened O_APPEND, so an append that lands on the
235+
* new file before this write does is kept rather than overwritten (#1208).
236+
*/
233237
export async function createFileInDirectory(
234238
filePath: string,
235239
content: string,
240+
append: boolean = false,
236241
): Promise<void> {
237242
// O_EXCL: an existing file or symlink fails with EEXIST, never followed.
238243
const { fd } = openInDirectory(
239244
filePath,
240245
fs.constants.O_WRONLY |
241246
fs.constants.O_CREAT |
242247
fs.constants.O_EXCL |
248+
(append ? fs.constants.O_APPEND : 0) |
243249
noFollow(),
244250
false,
245251
);

0 commit comments

Comments
 (0)