Skip to content

Commit 3ff06fe

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 3ff06fe

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/efi.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,26 @@ 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-
});
82+
let found_mount = ESP_MOUNTS
83+
.iter()
84+
.try_fold(None, |mntpoint, &mnt| -> anyhow::Result<_> {
85+
// return for the first match
86+
if mntpoint.is_some() {
87+
return Ok(mntpoint);
88+
}
89+
let path = root.join(mnt);
90+
if !path.exists() {
91+
return Ok(None);
92+
}
93+
94+
let st = rustix::fs::statfs(&path)?;
95+
if st.f_type == libc::MSDOS_SUPER_MAGIC {
96+
util::ensure_writable_mount(&path)?;
97+
Ok(Some(path))
98+
} else {
99+
Ok(None)
100+
}
101+
})?;
87102

88103
// Only borrow mutably if we found a mount point
89104
if let Some(mnt) = found_mount {
@@ -266,8 +281,7 @@ impl Component for Efi {
266281
anyhow::bail!("Failed to find adoptable system")
267282
};
268283

269-
// Confirm that esp_devices is Some(value)
270-
let esp_devices = esp_devices.unwrap();
284+
let esp_devices = esp_devices.unwrap_or_default();
271285
let mut devices = esp_devices.iter();
272286
let Some(esp) = devices.next() else {
273287
anyhow::bail!("Failed to find esp device");
@@ -445,8 +459,7 @@ impl Component for Efi {
445459
.as_ref()
446460
.ok_or_else(|| anyhow::anyhow!("No filetree for installed EFI found!"))?;
447461

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

0 commit comments

Comments
 (0)