Skip to content

Commit e6223aa

Browse files
committed
rhsm: Use atomic_write
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]>
1 parent 36854dc commit e6223aa

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

lib/src/rhsm.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Integration with Red Hat Subscription Manager
22
3-
use anyhow::Result;
4-
use cap_std::fs::{Dir, OpenOptions};
5-
use cap_std_ext::cap_std;
3+
use anyhow::{Context, Result};
4+
use cap_std::fs::Dir;
5+
use cap_std_ext::{cap_std, dirext::CapStdExtDirExt};
66
use fn_error_context::context;
77
use serde::Serialize;
88

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

9898
let facts = RhsmFacts::from(host.status);
99-
let mut bootc_facts_file = root.open_with(
100-
FACTS_PATH,
101-
OpenOptions::new().write(true).create(true).truncate(true),
102-
)?;
103-
serde_json::to_writer_pretty(&mut bootc_facts_file, &facts)?;
99+
root.atomic_replace_with(FACTS_PATH, |w| {
100+
serde_json::to_writer_pretty(w, &facts)?;
101+
anyhow::Ok(())
102+
})
103+
.with_context(|| format!("Writing {FACTS_PATH}"))?;
104104
Ok(())
105105
}
106106

0 commit comments

Comments
 (0)