Merge pull request #41 from nix-community/path_are_not_strings

Avoid Path -> String Conversions
This commit is contained in:
nikstur 2022-12-29 00:28:36 +01:00 committed by GitHub
commit f4e4ad9c3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 39 deletions

View File

@ -36,9 +36,8 @@ impl Installer {
pub fn install(&self) -> Result<()> { pub fn install(&self) -> Result<()> {
for toplevel in &self.generations { for toplevel in &self.generations {
let generation_result = Generation::from_toplevel(toplevel).with_context(|| { let generation_result = Generation::from_toplevel(toplevel)
format!("Failed to build generation from toplevel: {toplevel:?}") .with_context(|| format!("Failed to build generation from toplevel: {toplevel:?}"));
});
let generation = match generation_result { let generation = match generation_result {
Ok(generation) => generation, Ok(generation) => generation,
@ -47,7 +46,7 @@ impl Installer {
continue; continue;
} }
}; };
println!("Installing generation {generation}"); println!("Installing generation {generation}");
self.install_generation(&generation) self.install_generation(&generation)

View File

@ -4,7 +4,6 @@ mod generation;
mod install; mod install;
mod pe; mod pe;
mod signature; mod signature;
mod utils;
use anyhow::Result; use anyhow::Result;
use clap::Parser; use clap::Parser;

View File

@ -1,3 +1,4 @@
use std::ffi::OsString;
use std::fs; use std::fs;
use std::io::Write; use std::io::Write;
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
@ -8,8 +9,6 @@ use std::process::Command;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use goblin::pe::PE; use goblin::pe::PE;
use crate::utils;
use tempfile::TempDir; use tempfile::TempDir;
/// Attach all information that lanzaboote needs into the PE binary. /// Attach all information that lanzaboote needs into the PE binary.
@ -94,12 +93,11 @@ fn wrap_in_pe(
.open(&image_path) .open(&image_path)
.context("Failed to generate named temp file")?; .context("Failed to generate named temp file")?;
let mut args: Vec<String> = sections.iter().flat_map(Section::to_objcopy).collect(); let mut args: Vec<OsString> = sections.iter().flat_map(Section::to_objcopy).collect();
let extra_args = vec![
utils::path_to_string(stub), [stub.as_os_str(), image_path.as_os_str()]
utils::path_to_string(&image_path), .iter()
]; .for_each(|a| args.push(a.into()));
args.extend(extra_args);
let status = Command::new("objcopy") let status = Command::new("objcopy")
.args(&args) .args(&args)
@ -122,12 +120,19 @@ struct Section {
} }
impl Section { impl Section {
fn to_objcopy(&self) -> Vec<String> { /// Create objcopy `-add-section` command line parameters that
/// attach the section to a PE file.
fn to_objcopy(&self) -> Vec<OsString> {
// There is unfortunately no format! for OsString, so we cannot
// just format a path.
let mut map_str: OsString = format!("{}=", self.name).into();
map_str.push(&self.file_path);
vec![ vec![
String::from("--add-section"), OsString::from("--add-section"),
format!("{}={}", self.name, utils::path_to_string(&self.file_path)), map_str,
String::from("--change-section-vma"), OsString::from("--change-section-vma"),
format!("{}={:#x}", self.name, self.offset), format!("{}={:#x}", self.name, self.offset).into(),
] ]
} }
} }

View File

@ -1,10 +1,9 @@
use std::ffi::OsString;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::Command; use std::process::Command;
use anyhow::Result; use anyhow::Result;
use crate::utils;
pub struct KeyPair { pub struct KeyPair {
pub private_key: PathBuf, pub private_key: PathBuf,
pub public_key: PathBuf, pub public_key: PathBuf,
@ -19,14 +18,14 @@ impl KeyPair {
} }
pub fn sign_and_copy(&self, from: &Path, to: &Path) -> Result<()> { pub fn sign_and_copy(&self, from: &Path, to: &Path) -> Result<()> {
let args = vec![ let args: Vec<OsString> = vec![
String::from("--key"), OsString::from("--key"),
utils::path_to_string(&self.private_key), self.private_key.clone().into(),
String::from("--cert"), OsString::from("--cert"),
utils::path_to_string(&self.public_key), self.public_key.clone().into(),
utils::path_to_string(from), from.as_os_str().to_owned(),
String::from("--output"), OsString::from("--output"),
utils::path_to_string(to), to.as_os_str().to_owned(),
]; ];
let output = Command::new("sbsign").args(&args).output()?; let output = Command::new("sbsign").args(&args).output()?;

View File

@ -1,11 +0,0 @@
use std::path::Path;
// All Linux file paths should be convertable to strings
pub fn path_to_string(path: impl AsRef<Path>) -> String {
String::from(path.as_ref().to_str().unwrap_or_else(|| {
panic!(
"Failed to convert path '{}' to a string",
path.as_ref().display()
)
}))
}