Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/runtime/webcore/blob/copy_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,13 @@ impl CopyFile {
return self.fallback_read_write(remain, unknown_size, &mut total_written);
}

// EBADF: copy_file_range(2) fails with it when the destination was opened with
// O_APPEND (e.g. stdout redirected with `>>`). A read/write loop appends fine.
// (Any other bad fd fails there with its own error.)
bun_sys::E::EBADF if total_written == 0 => {
return self.fallback_read_write(remain, unknown_size, &mut total_written);
}

// EINVAL: eCryptfs and other filesystems may not support copy_file_range.
// Also returned when the file descriptor is incompatible with the syscall.
bun_sys::E::EINVAL => {
Expand Down
25 changes: 25 additions & 0 deletions test/js/bun/io/bun-write.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1784,3 +1784,28 @@ int posix_fadvise(int fd, off_t offset, off_t len, int advice) {
expect(exitCode).toBe(0);
});
});

describe.skipIf(isWindows)("Bun.write into a descriptor opened for appending", () => {
it("appends the file when stdout is redirected with >>", async () => {
using dir = tempDir("bun-write-append-fd", {
"src.txt": "hello world\n",
"log.txt": "before\n",
});
const src = join(String(dir), "src.txt");
const log = join(String(dir), "log.txt");
const script = `process.stderr.write(String(await Bun.write(Bun.stdout, Bun.file(${JSON.stringify(src)}))))`;
await using proc = Bun.spawn({
cmd: ["sh", "-c", `"$BUN" -e ${JSON.stringify(script)} >> ${JSON.stringify(log)}`],
env: { ...bunEnv, BUN: bunExe() },
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect({ stdout, stderr, content: fs.readFileSync(log, "utf8") }).toEqual({
stdout: "",
stderr: "12",
content: "before\nhello world\n",
});
expect(exitCode).toBe(0);
});
});