Skip to content

Commit

Permalink
tmpfiles: Handle old caches
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cgwalters committed Dec 13, 2023
1 parent 1fd3cea commit 6b78c7f
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions rust/src/tmpfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -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(())
}
}

0 comments on commit 6b78c7f

Please sign in to comment.