@@ -25,6 +25,7 @@ mod fat;
25
25
mod file_data_source;
26
26
27
27
use std:: {
28
+ borrow:: Cow ,
28
29
collections:: BTreeMap ,
29
30
fs,
30
31
path:: { Path , PathBuf } ,
@@ -45,12 +46,12 @@ const CONFIG_FILE_NAME: &str = "boot.json";
45
46
///
46
47
/// It can currently create `MBR` (BIOS), `GPT` (UEFI), and `TFTP` (UEFI) images.
47
48
pub struct DiskImageBuilder {
48
- files : BTreeMap < String , FileDataSource > ,
49
+ files : BTreeMap < Cow < ' static , str > , FileDataSource > ,
49
50
}
50
51
51
52
impl DiskImageBuilder {
52
53
/// Create a new instance of DiskImageBuilder, with the specified kernel.
53
- pub fn new ( kernel : & Path ) -> Self {
54
+ pub fn new ( kernel : PathBuf ) -> Self {
54
55
let mut obj = Self :: empty ( ) ;
55
56
obj. set_kernel ( kernel) ;
56
57
obj
@@ -64,13 +65,19 @@ impl DiskImageBuilder {
64
65
}
65
66
66
67
/// Add or replace a kernel to be included in the final image.
67
- pub fn set_kernel ( & mut self , path : & Path ) -> & mut Self {
68
- self . set_file_source ( KERNEL_FILE_NAME , FileDataSource :: File ( path. to_path_buf ( ) ) )
68
+ pub fn set_kernel ( & mut self , path : PathBuf ) -> & mut Self {
69
+ self . set_file_source (
70
+ KERNEL_FILE_NAME . into ( ) ,
71
+ FileDataSource :: File ( path. to_path_buf ( ) ) ,
72
+ )
69
73
}
70
74
71
75
/// Add or replace a ramdisk to be included in the final image.
72
- pub fn set_ramdisk ( & mut self , path : & Path ) -> & mut Self {
73
- self . set_file_source ( RAMDISK_FILE_NAME , FileDataSource :: File ( path. to_path_buf ( ) ) )
76
+ pub fn set_ramdisk ( & mut self , path : PathBuf ) -> & mut Self {
77
+ self . set_file_source (
78
+ RAMDISK_FILE_NAME . into ( ) ,
79
+ FileDataSource :: File ( path. to_path_buf ( ) ) ,
80
+ )
74
81
}
75
82
76
83
/// Configures the runtime behavior of the bootloader.
@@ -83,16 +90,16 @@ impl DiskImageBuilder {
83
90
///
84
91
/// Note that the bootloader only loads the kernel and ramdisk files into memory on boot.
85
92
/// Other files need to be loaded manually by the kernel.
86
- pub fn set_file_contents ( & mut self , destination : & str , data : Vec < u8 > ) -> & mut Self {
87
- self . set_file_source ( destination, FileDataSource :: Data ( data) )
93
+ pub fn set_file_contents ( & mut self , destination : String , data : Vec < u8 > ) -> & mut Self {
94
+ self . set_file_source ( destination. into ( ) , FileDataSource :: Data ( data) )
88
95
}
89
96
90
97
/// Add a file with the specified source file to the disk image
91
98
///
92
99
/// Note that the bootloader only loads the kernel and ramdisk files into memory on boot.
93
100
/// Other files need to be loaded manually by the kernel.
94
101
pub fn set_file ( & mut self , destination : & str , file_path : PathBuf ) -> & mut Self {
95
- self . set_file_source ( destination, FileDataSource :: File ( file_path) )
102
+ self . set_file_source ( destination. into ( ) , FileDataSource :: File ( file_path) )
96
103
}
97
104
98
105
#[ cfg( feature = "bios" ) ]
@@ -156,6 +163,8 @@ impl DiskImageBuilder {
156
163
#[ cfg( feature = "uefi" ) ]
157
164
/// Create a folder containing the needed files for UEFI TFTP/PXE booting.
158
165
pub fn create_uefi_tftp_folder ( & self , tftp_path : & Path ) -> anyhow:: Result < ( ) > {
166
+ use std:: ops:: Deref ;
167
+
159
168
const UEFI_TFTP_BOOT_FILENAME : & str = "bootloader" ;
160
169
let bootloader_path = Path :: new ( env ! ( "UEFI_BOOTLOADER_PATH" ) ) ;
161
170
fs:: create_dir_all ( tftp_path)
@@ -171,7 +180,7 @@ impl DiskImageBuilder {
171
180
} ) ?;
172
181
173
182
for f in & self . files {
174
- let to = tftp_path. join ( f. 0 . clone ( ) ) ;
183
+ let to = tftp_path. join ( f. 0 . deref ( ) ) ;
175
184
176
185
let mut new_file = fs:: OpenOptions :: new ( )
177
186
. read ( true )
@@ -187,8 +196,11 @@ impl DiskImageBuilder {
187
196
}
188
197
189
198
/// Add a file source to the disk image
190
- fn set_file_source ( & mut self , destination : & str , source : FileDataSource ) -> & mut Self {
191
- let destination = destination. to_string ( ) ;
199
+ fn set_file_source (
200
+ & mut self ,
201
+ destination : Cow < ' static , str > ,
202
+ source : FileDataSource ,
203
+ ) -> & mut Self {
192
204
self . files . insert ( destination, source) ;
193
205
self
194
206
}
0 commit comments