MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / atomic_write

Function atomic_write

atomic-repository/src/repository/anchor.rs:372–404  ·  view source on GitHub ↗
(path: &Path, bytes: &[u8])

Source from the content-addressed store, hash-verified

370}
371
372fn atomic_write(path: &Path, bytes: &[u8]) -> Result<(), RepositoryError> {
373 let parent = path
374 .parent()
375 .ok_or_else(|| RepositoryError::InvalidRepository {
376 reason: format!("target '{}' has no parent directory", path.display()),
377 })?;
378 let temporary = parent.join(format!(
379 ".{}.{}.tmp",
380 path.file_name()
381 .and_then(|name| name.to_str())
382 .unwrap_or("atomic"),
383 std::process::id()
384 ));
385 let result = (|| -> std::io::Result<()> {
386 {
387 use std::io::Write;
388 let mut file = fs::OpenOptions::new()
389 .write(true)
390 .create_new(true)
391 .open(&temporary)?;
392 file.write_all(bytes)?;
393 file.sync_all()?;
394 }
395 fs::rename(&temporary, path)?;
396 Ok(())
397 })();
398 if result.is_err() {
399 let _ = fs::remove_file(&temporary);
400 }
401 result.map_err(|error| RepositoryError::InvalidRepository {
402 reason: format!("cannot write '{}': {error}", path.display()),
403 })
404}
405
406// ── Policy and helpers ───────────────────────────────────────────────────
407

Callers 1

write_head_mapFunction · 0.85

Calls 3

parentMethod · 0.80
openMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected