Skip to content

Commit 9c9c6c7

Browse files
committed
Use try_fold() for proper error propagation
Inspired by Colin's comment https://github.com/coreos/bootupd/pull/932/files#r2103628222
1 parent ea09938 commit 9c9c6c7

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/efi.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,22 @@ impl Efi {
7979
// Get mounted point for esp
8080
pub(crate) fn get_mounted_esp(&self, root: &Path) -> Result<Option<PathBuf>> {
8181
// First check all potential mount points without holding the borrow
82-
let found_mount = ESP_MOUNTS.iter().map(|&mnt| root.join(mnt)).find(|mnt| {
83-
mnt.exists()
84-
&& rustix::fs::statfs(mnt).map_or(false, |st| st.f_type == libc::MSDOS_SUPER_MAGIC)
85-
&& util::ensure_writable_mount(mnt).is_ok()
86-
});
87-
82+
let found_mount = ESP_MOUNTS
83+
.iter()
84+
.try_fold(None, |_, &mnt| -> anyhow::Result<_> {
85+
let path = root.join(mnt);
86+
if !path.exists() {
87+
return Ok(None);
88+
}
89+
90+
let st = rustix::fs::statfs(&path)?;
91+
if st.f_type == libc::MSDOS_SUPER_MAGIC {
92+
util::ensure_writable_mount(&path)?;
93+
Ok(Some(path))
94+
} else {
95+
Ok(None)
96+
}
97+
})?;
8898
// Only borrow mutably if we found a mount point
8999
if let Some(mnt) = found_mount {
90100
log::debug!("Reusing existing mount point {mnt:?}");
@@ -266,8 +276,7 @@ impl Component for Efi {
266276
anyhow::bail!("Failed to find adoptable system")
267277
};
268278

269-
// Confirm that esp_devices is Some(value)
270-
let esp_devices = esp_devices.unwrap();
279+
let esp_devices = esp_devices.unwrap_or_default();
271280
let mut devices = esp_devices.iter();
272281
let Some(esp) = devices.next() else {
273282
anyhow::bail!("Failed to find esp device");
@@ -445,8 +454,7 @@ impl Component for Efi {
445454
.as_ref()
446455
.ok_or_else(|| anyhow::anyhow!("No filetree for installed EFI found!"))?;
447456

448-
// Confirm that esp_devices is Some(value)
449-
let esp_devices = esp_devices.unwrap();
457+
let esp_devices = esp_devices.unwrap_or_default();
450458
let mut devices = esp_devices.iter();
451459
let Some(esp) = devices.next() else {
452460
anyhow::bail!("Failed to find esp device");

0 commit comments

Comments
 (0)