| 347 | } |
| 348 | |
| 349 | fn write_head_map( |
| 350 | root: &Path, |
| 351 | mappings: &BTreeMap<String, HeadMapEntry>, |
| 352 | ) -> Result<(), RepositoryError> { |
| 353 | let path = root.join(HEAD_MAP_RELATIVE); |
| 354 | if let Some(parent) = path.parent() { |
| 355 | fs::create_dir_all(parent).map_err(|error| RepositoryError::InvalidRepository { |
| 356 | reason: format!( |
| 357 | "cannot create shim directory '{}': {error}", |
| 358 | parent.display() |
| 359 | ), |
| 360 | })?; |
| 361 | } |
| 362 | let wire = HeadMapWire { |
| 363 | version: 1, |
| 364 | mappings: mappings.clone(), |
| 365 | }; |
| 366 | let mut bytes = serde_json::to_vec_pretty(&wire) |
| 367 | .map_err(|error| RepositoryError::Serialization(error.to_string()))?; |
| 368 | bytes.push(b'\n'); |
| 369 | atomic_write(&root.join(HEAD_MAP_RELATIVE), &bytes) |
| 370 | } |
| 371 | |
| 372 | fn atomic_write(path: &Path, bytes: &[u8]) -> Result<(), RepositoryError> { |
| 373 | let parent = path |