Skip to content

Commit 8f70dbd

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 8f70dbd

File tree

1 file changed

+71
-53
lines changed

1 file changed

+71
-53
lines changed

src/efi.rs

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

187-
/// Copy from /usr/lib/efi to boot/ESP.
188-
fn package_mode_copy_to_boot_impl(&self) -> Result<()> {
189-
let sysroot = Path::new("/");
190-
let sysroot_path =
191-
Utf8Path::from_path(sysroot).context("Sysroot path is not valid UTF-8")?;
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+
}
192215

193-
// Find components in /usr/lib/efi
194-
let efi_comps = match get_efi_component_from_usr(sysroot_path, EFILIB)? {
195-
Some(comps) if !comps.is_empty() => comps,
196-
_ => {
197-
log::debug!("No EFI components found in /usr/lib/efi");
198-
return Ok(());
199-
}
200-
};
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+
}
201223

224+
/// Shared helper to copy EFI components to all ESP devices
225+
fn copy_efi_components_to_all_esps(
226+
&self,
227+
sysroot: &Path,
228+
sysroot_dir: &openat::Dir,
229+
efi_components: &[EFIComponent],
230+
) -> Result<()> {
202231
// Find all ESP devices
203232
let devices = blockdev::get_devices(sysroot)?;
204233
let Some(esp_devices) = blockdev::find_colocated_esps(&devices)? else {
205234
anyhow::bail!("No ESP found");
206235
};
207236

208-
let sysroot_dir = openat::Dir::open(sysroot).context("Opening sysroot for reading")?;
209-
210237
// Copy to all ESPs
211238
for esp in esp_devices {
212239
let esp_path = self.ensure_mounted_esp(sysroot, Path::new(&esp))?;
@@ -215,37 +242,29 @@ impl Efi {
215242
.with_context(|| format!("Opening ESP at {}", esp_path.display()))?;
216243
validate_esp_fstype(&esp_dir)?;
217244

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-
})?;
245+
self.copy_efi_components_to_esp(sysroot_dir, &esp_path, efi_components)?;
246+
}
247+
248+
Ok(())
249+
}
250+
251+
/// Copy from /usr/lib/efi to boot/ESP.
252+
fn package_mode_copy_to_boot_impl(&self) -> Result<()> {
253+
let sysroot = Path::new("/");
254+
let sysroot_path =
255+
Utf8Path::from_path(sysroot).context("Sysroot path is not valid UTF-8")?;
256+
257+
let efi_comps = match get_efi_component_from_usr(sysroot_path, EFILIB)? {
258+
Some(comps) if !comps.is_empty() => comps,
259+
_ => {
260+
log::debug!("No EFI components found in /usr/lib/efi");
261+
return Ok(());
242262
}
263+
};
243264

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-
}
265+
let sysroot_dir = openat::Dir::open(sysroot).context("Opening sysroot for reading")?;
266+
267+
self.copy_efi_components_to_all_esps(sysroot, &sysroot_dir, &efi_comps)?;
249268

250269
log::info!(
251270
"Successfully copied {} EFI component(s) to all ESPs",
@@ -484,23 +503,22 @@ impl Component for Efi {
484503
} else {
485504
None
486505
};
487-
let dest = destpath.to_str().with_context(|| {
488-
format!(
489-
"Include invalid UTF-8 characters in dest {}",
490-
destpath.display()
491-
)
492-
})?;
493506

494507
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-
}
508+
// Use shared helper to copy components from /usr/lib/efi
509+
self.copy_efi_components_to_esp(&src_dir, &destpath, &efi_components)?;
498510
EFILIB
499511
} else {
500512
let updates = component_updatedirname(self);
501513
let src = updates
502514
.to_str()
503515
.context("Include invalid UTF-8 characters in path")?;
516+
let dest = destpath.to_str().with_context(|| {
517+
format!(
518+
"Include invalid UTF-8 characters in dest {}",
519+
destpath.display()
520+
)
521+
})?;
504522
filetree::copy_dir_with_args(&src_dir, src, dest, OPTIONS)?;
505523
&src.to_owned()
506524
};

0 commit comments

Comments
 (0)