Skip to content

Commit 6c2079f

Browse files
committed
Update DiskImageBuilder to remove lifetime requirement by cloning, and convert PathBuf parameters to Path
1 parent a13925d commit 6c2079f

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/lib.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,21 @@ use tempfile::NamedTempFile;
2323
const KERNEL_FILE_NAME: &str = "kernel-x86_64";
2424
const RAMDISK_FILE_NAME: &str = "ramdisk";
2525

26-
struct DiskImageFile<'a> {
27-
source: &'a PathBuf,
28-
destination: &'a str,
26+
#[derive(Clone)]
27+
struct DiskImageFile {
28+
source: PathBuf,
29+
destination: String,
2930
}
3031

3132
/// DiskImageBuilder helps create disk images for a specified set of files.
3233
/// It can currently create MBR (BIOS), GPT (UEFI), and TFTP (UEFI) images.
33-
pub struct DiskImageBuilder<'a> {
34-
files: Vec<DiskImageFile<'a>>,
34+
pub struct DiskImageBuilder {
35+
files: Vec<DiskImageFile>,
3536
}
3637

37-
impl<'a> DiskImageBuilder<'a> {
38+
impl DiskImageBuilder {
3839
/// Create a new instance of DiskImageBuilder, with the specified kernel.
39-
pub fn new(kernel: &'a PathBuf) -> Self {
40+
pub fn new(kernel: &Path) -> Self {
4041
let mut obj = Self::empty();
4142
obj.set_kernel(kernel);
4243
obj
@@ -48,31 +49,31 @@ impl<'a> DiskImageBuilder<'a> {
4849
}
4950

5051
/// Add or replace a kernel to be included in the final image.
51-
pub fn set_kernel(&mut self, path: &'a PathBuf) -> &mut Self {
52+
pub fn set_kernel(&mut self, path: &Path) -> &mut Self {
5253
self.add_or_replace_file(path, KERNEL_FILE_NAME)
5354
}
5455

5556
/// Add or replace a ramdisk to be included in the final image.
56-
pub fn set_ramdisk(&mut self, path: &'a PathBuf) -> &mut Self {
57+
pub fn set_ramdisk(&mut self, path: &Path) -> &mut Self {
5758
self.add_or_replace_file(&path, RAMDISK_FILE_NAME)
5859
}
5960

6061
/// Add or replace arbitrary files.
6162
/// NOTE: You can overwrite internal files if you choose, such as EFI/BOOT/BOOTX64.EFI
6263
/// This can be useful in situations where you want to generate an image, but not use the provided bootloader.
63-
pub fn add_or_replace_file(&mut self, path: &'a PathBuf, target: &'a str) -> &mut Self {
64+
pub fn add_or_replace_file(&mut self, path: &Path, target: &str) -> &mut Self {
6465
self.files.insert(
6566
0,
66-
DiskImageFile::<'a> {
67-
source: &path,
68-
destination: &target,
67+
DiskImageFile {
68+
source: path.clone().to_path_buf(),
69+
destination: target.to_string(),
6970
},
7071
);
7172
self
7273
}
7374
fn create_fat_filesystem_image(
7475
&self,
75-
internal_files: BTreeMap<&'a str, &'a Path>,
76+
internal_files: BTreeMap<&str, &Path>,
7677
) -> anyhow::Result<NamedTempFile> {
7778
let mut local_map = BTreeMap::new();
7879

@@ -81,7 +82,7 @@ impl<'a> DiskImageBuilder<'a> {
8182
}
8283

8384
for f in self.files.as_slice() {
84-
local_map.insert(f.destination, &f.source.as_path());
85+
local_map.insert(&f.destination, &f.source.as_path());
8586
}
8687

8788
let out_file = NamedTempFile::new().context("failed to create temp file")?;
@@ -157,8 +158,8 @@ impl<'a> DiskImageBuilder<'a> {
157158
})?;
158159

159160
for f in self.files.as_slice() {
160-
let to = tftp_path.join(f.destination);
161-
let result = std::fs::copy(f.source, to);
161+
let to = tftp_path.join(f.destination.clone());
162+
let result = std::fs::copy(f.source.clone(), to);
162163
if result.is_err() {
163164
return Err(anyhow::Error::from(result.unwrap_err()));
164165
}

0 commit comments

Comments
 (0)