Skip to content

Commit d3b5f7b

Browse files
committed
Add refactor extracting common ESP copying logic into shared helpers
Extract duplicated ESP mounting, validation, and copying logic from install() and package_mode_copy_to_boot_impl() into shared helper function to eliminate dupe code.
1 parent 7734d00 commit d3b5f7b

File tree

1 file changed

+70
-56
lines changed

1 file changed

+70
-56
lines changed

src/efi.rs

Lines changed: 70 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,51 @@ impl Efi {
184184
create_efi_boot_entry(device, esp_part_num.trim(), &loader, &product_name)
185185
}
186186

187+
/// Shared helper to copy EFI components to a single ESP
188+
fn copy_efi_components_to_esp(
189+
&self,
190+
sysroot_dir: &openat::Dir,
191+
esp_path: &Path,
192+
efi_components: &[EFIComponent],
193+
) -> Result<()> {
194+
let dest_str = esp_path
195+
.to_str()
196+
.context("ESP path contains invalid UTF-8")?;
197+
198+
// Copy each component
199+
for efi_comp in efi_components {
200+
log::info!(
201+
"Copying EFI component {} version {} to ESP at {}",
202+
efi_comp.name,
203+
efi_comp.version,
204+
esp_path.display()
205+
);
206+
207+
filetree::copy_dir_with_args(sysroot_dir, efi_comp.path.as_str(), dest_str, OPTIONS)
208+
.with_context(|| {
209+
format!(
210+
"Failed to copy {} from {} to {}",
211+
efi_comp.name, efi_comp.path, dest_str
212+
)
213+
})?;
214+
}
215+
216+
// Sync filesystem
217+
let efidir =
218+
openat::Dir::open(&esp_path.join("EFI")).context("Opening EFI directory for sync")?;
219+
fsfreeze_thaw_cycle(efidir.open_file(".")?)?;
220+
221+
Ok(())
222+
}
223+
224+
225+
187226
/// Copy from /usr/lib/efi to boot/ESP.
188227
fn package_mode_copy_to_boot_impl(&self) -> Result<()> {
189228
let sysroot = Path::new("/");
190229
let sysroot_path =
191230
Utf8Path::from_path(sysroot).context("Sysroot path is not valid UTF-8")?;
192231

193-
// Find components in /usr/lib/efi
194232
let efi_comps = match get_efi_component_from_usr(sysroot_path, EFILIB)? {
195233
Some(comps) if !comps.is_empty() => comps,
196234
_ => {
@@ -199,57 +237,34 @@ impl Efi {
199237
}
200238
};
201239

202-
// Find all ESP devices
203-
let devices = blockdev::get_devices(sysroot)?;
204-
let Some(esp_devices) = blockdev::find_colocated_esps(&devices)? else {
205-
anyhow::bail!("No ESP found");
206-
};
207-
208240
let sysroot_dir = openat::Dir::open(sysroot).context("Opening sysroot for reading")?;
209241

210-
// Copy to all ESPs
211-
for esp in esp_devices {
212-
let esp_path = self.ensure_mounted_esp(sysroot, Path::new(&esp))?;
213-
214-
let esp_dir = openat::Dir::open(&esp_path)
215-
.with_context(|| format!("Opening ESP at {}", esp_path.display()))?;
216-
validate_esp_fstype(&esp_dir)?;
217-
218-
// Copy each component
219-
for efi_comp in &efi_comps {
220-
log::info!(
221-
"Copying EFI component {} version {} to ESP at {}",
222-
efi_comp.name,
223-
efi_comp.version,
224-
esp_path.display()
225-
);
226-
227-
let dest_str = esp_path
228-
.to_str()
229-
.context("ESP path contains invalid UTF-8")?;
230-
filetree::copy_dir_with_args(
231-
&sysroot_dir,
232-
efi_comp.path.as_str(),
233-
dest_str,
234-
OPTIONS,
235-
)
236-
.with_context(|| {
237-
format!(
238-
"Failed to copy {} from {} to {}",
239-
efi_comp.name, efi_comp.path, dest_str
240-
)
241-
})?;
242-
}
242+
// First try to use an already mounted ESP
243+
let esp_path = if let Some(mounted_esp) = self.get_mounted_esp(sysroot)? {
244+
mounted_esp
245+
} else {
246+
// If not mounted, find ESP from devices
247+
let devices = blockdev::get_devices(sysroot)?;
248+
let Some(esp_devices) = blockdev::find_colocated_esps(&devices)? else {
249+
anyhow::bail!("No ESP found");
250+
};
243251

244-
// Sync filesystem
245-
let efidir = openat::Dir::open(&esp_path.join("EFI"))
246-
.context("Opening EFI directory for sync")?;
247-
fsfreeze_thaw_cycle(efidir.open_file(".")?)?;
248-
}
252+
let esp_device = esp_devices
253+
.first()
254+
.ok_or_else(|| anyhow::anyhow!("No ESP device found"))?;
255+
self.ensure_mounted_esp(sysroot, Path::new(esp_device))?
256+
};
257+
258+
let esp_dir = openat::Dir::open(&esp_path)
259+
.with_context(|| format!("Opening ESP at {}", esp_path.display()))?;
260+
validate_esp_fstype(&esp_dir)?;
261+
262+
self.copy_efi_components_to_esp(&sysroot_dir, &esp_path, &efi_comps)?;
249263

250264
log::info!(
251-
"Successfully copied {} EFI component(s) to all ESPs",
252-
efi_comps.len()
265+
"Successfully copied {} EFI component(s) to ESP at {}",
266+
efi_comps.len(),
267+
esp_path.display()
253268
);
254269
Ok(())
255270
}
@@ -484,23 +499,22 @@ impl Component for Efi {
484499
} else {
485500
None
486501
};
487-
let dest = destpath.to_str().with_context(|| {
488-
format!(
489-
"Include invalid UTF-8 characters in dest {}",
490-
destpath.display()
491-
)
492-
})?;
493502

494503
let efi_path = if let Some(efi_components) = efi_comps {
495-
for efi in efi_components {
496-
filetree::copy_dir_with_args(&src_dir, efi.path.as_str(), dest, OPTIONS)?;
497-
}
504+
// Use shared helper to copy components from /usr/lib/efi
505+
self.copy_efi_components_to_esp(&src_dir, &destpath, &efi_components)?;
498506
EFILIB
499507
} else {
500508
let updates = component_updatedirname(self);
501509
let src = updates
502510
.to_str()
503511
.context("Include invalid UTF-8 characters in path")?;
512+
let dest = destpath.to_str().with_context(|| {
513+
format!(
514+
"Include invalid UTF-8 characters in dest {}",
515+
destpath.display()
516+
)
517+
})?;
504518
filetree::copy_dir_with_args(&src_dir, src, dest, OPTIONS)?;
505519
&src.to_owned()
506520
};

0 commit comments

Comments
 (0)