Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / scripts / maps / mamaps_build / src / tilespill_part2.rs

tilespill_part2.rs24 lines · 1.1 KiBRaw
1/// Write all of `buf` at `offset` without moving the file's cursor.
2///
3/// The positional twin of [`tilecodec::pmtiles::read_exact_at`], and the one genuinely new I/O
4/// primitive this change needs: nothing else in the tree writes positionally. Same shape and the
5/// same reason -- the cursor is the only thing that would need `&mut File`, and it is shared between
6/// clones of a handle, so seek-then-write cannot be done concurrently on one file while this can.
7/// Neither platform guarantees a full write, hence the loop.
8fn write_all_at(file: &File, mut buf: &[u8], mut offset: u64) -> std::io::Result<()> {
9 while !buf.is_empty() {
10 #[cfg(windows)]
11 let n = std::os::windows::fs::FileExt::seek_write(file, buf, offset)?;
12 #[cfg(unix)]
13 let n = std::os::unix::fs::FileExt::write_at(file, buf, offset)?;
14 if n == 0 {
15 return Err(std::io::Error::new(
16 std::io::ErrorKind::WriteZero,
17 "the file took none of the bytes offered",
18 ));
19 }
20 buf = &buf[n..];
21 offset += n as u64;
22 }
23 Ok(())
24}