Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / gtfs_ingest / src / index_part4.rs
index_part4.rs37 lines · 1.2 KiBRaw
| 1 | /// Write header + section directory + 8-byte-aligned payloads to `out`, and |
| 2 | /// return the total byte length. |
| 3 | /// |
| 4 | /// Every section's length is known by the time this runs, so the directory is |
| 5 | /// computed up front: nothing is reserved and seeked back to, which is what lets |
| 6 | /// the pack go straight to a `BufWriter` instead of being concatenated into one |
| 7 | /// multi-gigabyte `Vec` first. |
| 8 | fn write_pack( |
| 9 | out: &mut impl Write, |
| 10 | header: &[u8], |
| 11 | sections: &[&[u8]; SECTION_COUNT], |
| 12 | ) -> std::io::Result<usize> { |
| 13 | const PAD: [u8; 8] = [0; 8]; |
| 14 | let align = |o: usize| (o + 7) & !7; |
| 15 | |
| 16 | let mut off = HEADER_LEN + SECTION_COUNT * 16; |
| 17 | let mut dir = Vec::with_capacity(SECTION_COUNT * 16); |
| 18 | for s in sections { |
| 19 | off = align(off); |
| 20 | dir.extend_from_slice(&(off as u64).to_le_bytes()); |
| 21 | dir.extend_from_slice(&(s.len() as u64).to_le_bytes()); |
| 22 | off += s.len(); |
| 23 | } |
| 24 | let total = off; |
| 25 | |
| 26 | out.write_all(header)?; |
| 27 | out.write_all(&dir)?; |
| 28 | let mut pos = header.len() + dir.len(); |
| 29 | for s in sections { |
| 30 | let pad = align(pos) - pos; |
| 31 | out.write_all(&PAD[..pad])?; |
| 32 | out.write_all(s)?; |
| 33 | pos += pad + s.len(); |
| 34 | } |
| 35 | debug_assert_eq!(pos, total); |
| 36 | Ok(total) |
| 37 | } |