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<()> {
for toplevel in &self.generations {
let generation_result = Generation::from_toplevel(toplevel).with_context(|| {
format!("Failed to build generation from toplevel: {toplevel:?}")
});
let generation_result = Generation::from_toplevel(toplevel)
.with_context(|| format!("Failed to build generation from toplevel: {toplevel:?}"));
let generation = match generation_result {
Ok(generation) => generation,

View File

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

View File

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

View File

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