Skip to content

Commit 36d7b43

Browse files
authored
Merge pull request #997 from HuijingHei/improve-step2-prep
Improve `get_efi_vendor()` to use `Path`
2 parents a100398 + 59f98b0 commit 36d7b43

File tree

4 files changed

+17
-24
lines changed

4 files changed

+17
-24
lines changed

src/bios.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl Component for Bios {
267267
Ok(ValidationResult::Skip)
268268
}
269269

270-
fn get_efi_vendor(&self, _: &str) -> Result<Option<String>> {
270+
fn get_efi_vendor(&self, _: &Path) -> Result<Option<String>> {
271271
Ok(None)
272272
}
273273
}

src/bootupd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub(crate) fn install(
9999
log::info!("Installed {} {}", component.name(), meta.meta.version);
100100
state.installed.insert(component.name().into(), meta);
101101
// Yes this is a hack...the Component thing just turns out to be too generic.
102-
if let Some(vendor) = component.get_efi_vendor(&source_root)? {
102+
if let Some(vendor) = component.get_efi_vendor(&Path::new(source_root))? {
103103
assert!(installed_efi_vendor.is_none());
104104
installed_efi_vendor = Some(vendor);
105105
}

src/component.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub(crate) trait Component {
7878
fn validate(&self, current: &InstalledContent) -> Result<ValidationResult>;
7979

8080
/// Locating efi vendor dir
81-
fn get_efi_vendor(&self, sysroot: &str) -> Result<Option<String>>;
81+
fn get_efi_vendor(&self, sysroot: &Path) -> Result<Option<String>>;
8282
}
8383

8484
/// Given a component name, create an implementation.
@@ -204,7 +204,6 @@ mod tests {
204204
let tdp = td.path();
205205
let tdupdates = "usr/lib/bootupd/updates/EFI";
206206
let tdir = openat::Dir::open(tdp)?;
207-
let td = tdp.to_str().unwrap();
208207

209208
tdir.ensure_dir_all(tdupdates, 0o755)?;
210209
let efi = tdir.sub_dir(tdupdates)?;
@@ -227,13 +226,13 @@ mod tests {
227226
let target_components: Vec<_> = all_components.values().collect();
228227
for &component in target_components.iter() {
229228
if component.name() == "BIOS" {
230-
assert_eq!(component.get_efi_vendor(&td)?, None);
229+
assert_eq!(component.get_efi_vendor(tdp)?, None);
231230
}
232231
if component.name() == "EFI" {
233-
let x = component.get_efi_vendor(&td);
232+
let x = component.get_efi_vendor(tdp);
234233
assert_eq!(x.is_err(), true);
235234
efi.remove_all("centos")?;
236-
assert_eq!(component.get_efi_vendor(&td)?, Some("fedora".to_string()));
235+
assert_eq!(component.get_efi_vendor(tdp)?, Some("fedora".to_string()));
237236
{
238237
let td_vendor = "usr/lib/efi/shim/15.8-3/EFI/centos";
239238
tdir.ensure_dir_all(td_vendor, 0o755)?;
@@ -245,16 +244,15 @@ mod tests {
245244
)?;
246245

247246
// usr/lib/efi wins and get 'centos'
248-
assert_eq!(component.get_efi_vendor(&td)?, Some("centos".to_string()));
247+
assert_eq!(component.get_efi_vendor(tdp)?, Some("centos".to_string()));
249248
// find directly from usr/lib/efi and get 'centos'
250-
let td_usr = format!("{td}/usr/lib/efi");
249+
let td_usr = tdp.join("usr/lib/efi");
251250
assert_eq!(
252251
component.get_efi_vendor(&td_usr)?,
253252
Some("centos".to_string())
254253
);
255254
// find directly from updates and get 'fedora'
256-
let td_efi =
257-
format!("{td}/{}", component_updatedirname(&**component).display());
255+
let td_efi = tdp.join(component_updatedirname(&**component));
258256
assert_eq!(
259257
component.get_efi_vendor(&td_efi)?,
260258
Some("fedora".to_string())

src/efi.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl Component for Efi {
257257
fn migrate_static_grub_config(&self, sysroot_path: &str, destdir: &openat::Dir) -> Result<()> {
258258
let sysroot =
259259
openat::Dir::open(sysroot_path).with_context(|| format!("Opening {sysroot_path}"))?;
260-
let Some(vendor) = self.get_efi_vendor(sysroot_path)? else {
260+
let Some(vendor) = self.get_efi_vendor(&Path::new(sysroot_path))? else {
261261
anyhow::bail!("Failed to find efi vendor");
262262
};
263263

@@ -380,7 +380,7 @@ impl Component for Efi {
380380
.current_dir(format!("/proc/self/fd/{}", src_dir.as_raw_fd()))
381381
.run()?;
382382
if update_firmware {
383-
if let Some(vendordir) = self.get_efi_vendor(&src_root)? {
383+
if let Some(vendordir) = self.get_efi_vendor(&Path::new(src_root))? {
384384
self.update_firmware(device, destd, &vendordir)?
385385
}
386386
}
@@ -552,20 +552,15 @@ impl Component for Efi {
552552
}
553553
}
554554

555-
fn get_efi_vendor(&self, sysroot: &str) -> Result<Option<String>> {
556-
let sysroot_path = Path::new(sysroot);
557-
let efi_lib = sysroot_path.join(EFILIB);
558-
let updates = sysroot_path.join(component_updatedirname(self));
555+
fn get_efi_vendor(&self, sysroot: &Path) -> Result<Option<String>> {
556+
let efi_lib = sysroot.join(EFILIB);
557+
let updates = sysroot.join(component_updatedirname(self));
559558

560-
let target = if let Some(p) = [efi_lib.as_path(), updates.as_path(), sysroot_path]
559+
let paths: [&Path; 3] = [&efi_lib, &updates, sysroot];
560+
let target = paths
561561
.into_iter()
562562
.find(|p| p.exists())
563-
{
564-
p
565-
} else {
566-
bail!("Failed to find valid target path");
567-
};
568-
563+
.ok_or_else(|| anyhow::anyhow!("Failed to find valid target path"))?;
569564
let shim_files = find_file_recursive(target, SHIM)?;
570565

571566
// Does not support multiple shim for efi

0 commit comments

Comments
 (0)