Open
Description
write_all_at
doesn't write the buffer at the given index. The content is written at the end of the file.
I created a repo on my github https://github.com/allevo/rust-fs-write-all-bug to show the error.
Locally on Mac M2, the test on that repo passes, but on CI it doesn't.
NB:
write_all_at
andread_exact_at
doesn't change the cursor positionflush
or/andsync_all
doesn't fix
I tried this code:
use std::{io::Write, os::unix::fs::FileExt, path::PathBuf};
pub fn run(file_path: PathBuf) {
let mut page_file = std::fs::File::options()
.create(true)
.append(true)
.read(true)
.open(&file_path)
.unwrap();
let n: u32 = 10;
let buf: [u8; 4] = n.to_be_bytes();
page_file
.write_all_at(&buf, 0).unwrap();
page_file.flush().unwrap();
page_file
.sync_all().unwrap();
let mut buf: [u8; 4] = [0; 4];
page_file
.read_exact_at(&mut buf, 0).unwrap();
let n = u32::from_be_bytes(buf);
page_file.seek(SeekFrom::Start(0)).unwrap();
let buf: [u8; 4] = (n + 1).to_be_bytes();
page_file
.write_all_at(&buf, 0).unwrap();
}
I expected to see this happen: The file content will be [0, 0, 0, 11]
Instead, this happened: [0, 0, 0, 10, 0, 0, 0, 11]
Meta
rustup --version && rustc --version --verbose
:
info: This is the version for the rustup toolchain manager, not the rustc compiler.
rustup 1.2[8](https://github.com/allevo/rust-fs-write-all-bug/actions/runs/14934973609/job/41960164587#step:3:9).1 (f9edccde0 2025-03-05)
info: The currently active `rustc` version is `rustc 1.86.0 (05f[9](https://github.com/allevo/rust-fs-write-all-bug/actions/runs/14934973609/job/41960164587#step:3:10)846f8 2025-03-31)`
rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7
Backtrace
In this case it is not important, the assertion fails