From 1fd3cea6ee1fe245d0aa46188ad430192eb4ae22 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 13 Dec 2023 18:05:22 -0500 Subject: [PATCH 1/2] tmpfiles: Fix error contexts We weren't actually formatting these, so the error looks like: `error: Deduplicate tmpfiles entries: readdir {RPMOSTREE_TMPFILESD}: No such file or directory (os error 2)` It's simpler to just not format. --- rust/src/tmpfiles.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/rust/src/tmpfiles.rs b/rust/src/tmpfiles.rs index bec240d1a8..9ffe695ea0 100644 --- a/rust/src/tmpfiles.rs +++ b/rust/src/tmpfiles.rs @@ -26,13 +26,11 @@ pub fn deduplicate_tmpfiles_entries(tmprootfs_dfd: i32) -> CxxResult<()> { // scan all rpm-ostree auto generated entries and save let tmpfiles_dir = tmprootfs_dfd .open_dir(RPMOSTREE_TMPFILESD) - .context("readdir {RPMOSTREE_TMPFILESD}")?; + .context(RPMOSTREE_TMPFILESD)?; let mut rpmostree_tmpfiles_entries = read_tmpfiles(&tmpfiles_dir)?; // remove autovar.conf first, then scan all system entries and save - let tmpfiles_dir = tmprootfs_dfd - .open_dir(TMPFILESD) - .context("readdir {TMPFILESD}")?; + let tmpfiles_dir = tmprootfs_dfd.open_dir(TMPFILESD).context(TMPFILESD)?; if tmpfiles_dir.try_exists(AUTOVAR_PATH)? { tmpfiles_dir.remove_file(AUTOVAR_PATH)?; From 6b78c7f9e85c1cd953a575252c4c73e59c6099fc Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Wed, 13 Dec 2023 18:11:05 -0500 Subject: [PATCH 2/2] tmpfiles: Handle old caches I actually did get bitten by having an old cache where there's nothing in the `RPMOSTREE_TMPFILESD` dir. Just handle the case where the directory doesn't exist. For good measure, also gracefully no-op if the destination tmpfiles.d directory doesn't exist. --- rust/src/tmpfiles.rs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/rust/src/tmpfiles.rs b/rust/src/tmpfiles.rs index 9ffe695ea0..2f44a48c4c 100644 --- a/rust/src/tmpfiles.rs +++ b/rust/src/tmpfiles.rs @@ -25,12 +25,29 @@ pub fn deduplicate_tmpfiles_entries(tmprootfs_dfd: i32) -> CxxResult<()> { // scan all rpm-ostree auto generated entries and save let tmpfiles_dir = tmprootfs_dfd - .open_dir(RPMOSTREE_TMPFILESD) + .open_dir_optional(RPMOSTREE_TMPFILESD) .context(RPMOSTREE_TMPFILESD)?; - let mut rpmostree_tmpfiles_entries = read_tmpfiles(&tmpfiles_dir)?; + let mut rpmostree_tmpfiles_entries = if let Some(tmpfiles_dir) = tmpfiles_dir { + read_tmpfiles(&tmpfiles_dir)? + } else { + Default::default() + }; // remove autovar.conf first, then scan all system entries and save - let tmpfiles_dir = tmprootfs_dfd.open_dir(TMPFILESD).context(TMPFILESD)?; + let tmpfiles_dir = if let Some(d) = tmprootfs_dfd + .open_dir_optional(TMPFILESD) + .context(TMPFILESD)? + { + d + } else { + if !rpmostree_tmpfiles_entries.is_empty() { + return Err( + format!("No {TMPFILESD} directory found, but have tmpfiles to process").into(), + ); + } + // Nothing to do here + return Ok(()); + }; if tmpfiles_dir.try_exists(AUTOVAR_PATH)? { tmpfiles_dir.remove_file(AUTOVAR_PATH)?; @@ -164,4 +181,12 @@ q /var/tmp 1777 root root 30d assert_eq!(entries[1], "d /var/spool/mail 0775 root mail - -"); Ok(()) } + + #[test] + /// Verify that we no-op if the directories don't exist. + fn test_deduplicate_emptydir() -> Result<()> { + let root = &cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?; + deduplicate_tmpfiles_entries(root.as_raw_fd())?; + Ok(()) + } }