use std::path::Path;
use crate::backend::image_probe::WindowsPlan;
use crate::backend::iso_reader::IsoReader;
const BOOT_CHAIN: &[&str] = &[
"EFI/Microsoft/Boot/bootmgfw.efi",
"EFI/Microsoft/Boot/BCD",
"EFI/Microsoft/Boot/boot.sdi",
"EFI/Microsoft/Boot/bootfix.bin",
"EFI/Boot/bootx64.efi",
"bootmgr.efi",
"bootmgr",
"boot/bcd",
"boot/boot.sdi",
"boot/bootfix.bin",
"sources/boot.wim",
];
const INSTALL_SOURCES: &[&str] = &["sources/install.wim", "sources/install.esd"];
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct WindowsExtraction {
pub files_written: usize,
pub bytes_written: u64,
pub wim_split: bool,
}
pub fn extract_windows_to_mount(
reader: &mut IsoReader,
mount: &Path,
plan: WindowsPlan,
mut on_progress: impl FnMut(u64, u64),
) -> anyhow::Result<WindowsExtraction> {
let mut result = WindowsExtraction {
wim_split: matches!(plan, WindowsPlan::SplitWim { .. }),
..Default::default()
};
for rel in BOOT_CHAIN {
extract_one(reader, mount, rel, &mut result, &mut on_progress)?;
}
if plan == WindowsPlan::ExtractAllToFat32 {
for rel in INSTALL_SOURCES {
extract_one(reader, mount, rel, &mut result, &mut on_progress)?;
}
}
Ok(result)
}
fn extract_one(
reader: &mut IsoReader,
mount: &Path,
rel: &str,
result: &mut WindowsExtraction,
on_progress: &mut impl FnMut(u64, u64),
) -> anyhow::Result<()> {
let Some(entry) = reader.find(rel)? else {
return Ok(());
};
if entry.is_dir {
return Ok(());
}
let dest = mount.join(rel);
if let Some(parent) = dest.parent() {
std::fs::create_dir_all(parent)?;
}
let mut out = std::fs::File::create(&dest)?;
let total = entry.len as u64;
reader.extract(&entry, &mut out, |written, total| {
on_progress(written, total)
})?;
out.sync_all()?;
result.files_written += 1;
result.bytes_written += total;
Ok(())
}