Skip to content

Commit

Permalink
rhsm: Use atomic_write
Browse files Browse the repository at this point in the history
Just noticed this when reading the code; our cap-std-ext crate
has a handy `atomic_write` API which streamlines exactly this
use case. This avoids having a corrupted/half-written facts
file if our process is interrupted, and also avoids a case
where a reading process may temporarily see a half written file.

Signed-off-by: Colin Walters <[email protected]>
  • Loading branch information
cgwalters committed Dec 20, 2024
1 parent 36854dc commit e6223aa
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/src/rhsm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Integration with Red Hat Subscription Manager
use anyhow::Result;
use cap_std::fs::{Dir, OpenOptions};
use cap_std_ext::cap_std;
use anyhow::{Context, Result};
use cap_std::fs::Dir;
use cap_std_ext::{cap_std, dirext::CapStdExtDirExt};
use fn_error_context::context;
use serde::Serialize;

Expand Down Expand Up @@ -96,11 +96,11 @@ pub(crate) async fn publish_facts(root: &Dir) -> Result<()> {
let (_deployments, host) = crate::status::get_status(&sysroot, booted_deployment.as_ref())?;

let facts = RhsmFacts::from(host.status);
let mut bootc_facts_file = root.open_with(
FACTS_PATH,
OpenOptions::new().write(true).create(true).truncate(true),
)?;
serde_json::to_writer_pretty(&mut bootc_facts_file, &facts)?;
root.atomic_replace_with(FACTS_PATH, |w| {
serde_json::to_writer_pretty(w, &facts)?;
anyhow::Ok(())
})
.with_context(|| format!("Writing {FACTS_PATH}"))?;
Ok(())
}

Expand Down

0 comments on commit e6223aa

Please sign in to comment.