#![forbid(unsafe_code)]
use std::{
ffi::OsStr,
fmt,
path::{Path, PathBuf},
};
pub use wim_format::Compression;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CaptureOptions {
pub strict_security: bool,
}
#[cfg(feature = "disk-capture")]
#[derive(Debug, Clone, Default)]
pub struct VolumeCaptureOptions {
pub volume_aliases: Vec<String>,
pub preserve_external_links: bool,
pub installation_system_drive: Option<String>,
}
#[cfg(feature = "disk-capture")]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct VolumeCaptureAudit {
pub omitted_kernel_eas: usize,
pub nodes_with_omitted_kernel_eas: usize,
pub storage_class_nodes: usize,
pub sparse_files: usize,
pub sparse_hole_ranges: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
Engine(i32),
InvalidImageIndex(u32),
InteriorNul,
MissingHandle,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Engine(code) => write!(
f,
"WIM operation failed ({code}): {}",
crate::engine::error_message(*code)
),
Self::InvalidImageIndex(index) => {
write!(
f,
"WIM image index {index} must be between 1 and {}",
i32::MAX
)
}
Self::InteriorNul => f.write_str("WIM paths and properties cannot contain NUL"),
Self::MissingHandle => f.write_str("WIM engine returned no handle"),
}
}
}
impl std::error::Error for Error {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ImageIndex(i32);
impl TryFrom<u32> for ImageIndex {
type Error = Error;
fn try_from(value: u32) -> Result<Self, Error> {
match i32::try_from(value) {
Ok(index) if index > 0 => Ok(Self(index)),
_ => Err(Error::InvalidImageIndex(value)),
}
}
}
impl ImageIndex {
pub fn get(self) -> u32 {
self.0 as u32
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct OpenOptions {
pub check_integrity: bool,
pub write_access: bool,
}
fn validate_text(value: &OsStr) -> Result<(), Error> {
#[cfg(not(windows))]
let has_nul = value.as_encoded_bytes().contains(&0);
#[cfg(windows)]
let has_nul = {
use std::os::windows::ffi::OsStrExt;
value.encode_wide().any(|unit| unit == 0)
};
if has_nul {
Err(Error::InteriorNul)
} else {
Ok(())
}
}
fn engine<T>(result: Result<T, wim_format::ParseError>) -> Result<T, Error> {
result.map_err(|error| Error::Engine(error as i32))
}
fn status<T>(result: Result<T, i32>) -> Result<T, Error> {
result.map_err(Error::Engine)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Info {
pub image_count: u32,
pub boot_index: u32,
pub compression: Compression,
pub total_bytes: u64,
}
pub struct Wim(Box<crate::engine::handles::WimHandle>);
impl fmt::Debug for Wim {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Wim")
.field("image_count", &self.0.images.len())
.field("compression", &self.0.output_compression)
.finish_non_exhaustive()
}
}
impl Wim {
#[cfg(feature = "disk-capture")]
pub fn capture_ntfs(
&mut self,
manifest: disk_capture::Manifest,
name: &str,
options: &VolumeCaptureOptions,
) -> Result<ImageIndex, Error> {
self.capture_ntfs_with_audit(manifest, name, options)
.map(|(index, _)| index)
}
#[cfg(feature = "disk-capture")]
pub fn capture_ntfs_with_audit(
&mut self,
manifest: disk_capture::Manifest,
name: &str,
options: &VolumeCaptureOptions,
) -> Result<(ImageIndex, VolumeCaptureAudit), Error> {
let (plan, audit) = crate::engine::capture::offline::plan(manifest, options)
.map_err(|e| Error::Engine(e as i32))?;
validate_text(OsStr::new(name))?;
let index = engine(crate::engine::image_mutation::add_empty(
&mut self.0,
Some(name.as_bytes()),
))?;
if let Err(error) =
crate::engine::capture::offline::attach(&mut self.0, index as usize - 1, plan)
{
engine(crate::engine::image_mutation::delete_images(
&mut self.0,
index as i32,
))?;
return Err(Error::Engine(error as i32));
}
Ok((ImageIndex(index as i32), audit))
}
pub fn open(path: &Path, options: OpenOptions) -> Result<Self, Error> {
validate_text(path.as_os_str())?;
let flags = i32::from(options.check_integrity) | (i32::from(options.write_access) << 2);
status(crate::engine::handles::open_path(
path,
flags,
Default::default(),
))
.map(Self)
}
pub fn open_with_cancel(
path: &Path,
options: OpenOptions,
mut cancelled: impl FnMut() -> bool,
) -> Result<Self, Error> {
if cancelled() {
return Err(Error::Engine(76));
}
validate_text(path.as_os_str())?;
let flags = i32::from(options.check_integrity) | (i32::from(options.write_access) << 2);
let handle = status(crate::engine::handles::open_path_with_cancel(
path,
flags,
&mut cancelled,
))?;
Ok(Self(handle))
}
pub fn new(compression: Compression) -> Result<Self, Error> {
status(crate::engine::handles::create_handle(compression)).map(Self)
}
pub fn info(&self) -> Result<Info, Error> {
let info = crate::engine::info::info(&self.0);
Ok(Info {
image_count: info.image_count,
boot_index: info.boot_index,
compression: Compression::from_i32(info.compression_type)
.map_err(|_| Error::Engine(16))?,
total_bytes: info.total_bytes,
})
}
pub fn reference_resource_files(&mut self, paths: &[PathBuf]) -> Result<(), Error> {
for path in paths {
validate_text(path.as_os_str())?;
}
let paths: Vec<_> = paths.iter().map(PathBuf::as_path).collect();
status(crate::engine::references::reference_files(
&mut self.0,
&paths,
0,
1,
))
}
pub fn verify(&mut self) -> Result<(), Error> {
engine(crate::engine::verify::verify_archive(&mut self.0, 0))
}
fn cancellable<T>(
&mut self,
mut cancelled: impl FnMut() -> bool,
operation: impl FnOnce(&mut crate::engine::handles::WimHandle) -> Result<T, Error>,
) -> Result<T, Error> {
if cancelled() {
return Err(Error::Engine(76));
}
crate::engine::progress::with_cancellation(&mut self.0, &mut cancelled, operation)
}
pub fn verify_with_cancel(&mut self, cancelled: impl FnMut() -> bool) -> Result<(), Error> {
self.cancellable(cancelled, |handle| {
engine(crate::engine::verify::verify_archive(handle, 0))
})
}
pub fn write_with_cancel(
&mut self,
path: &Path,
cancelled: impl FnMut() -> bool,
) -> Result<(), Error> {
validate_text(path.as_os_str())?;
self.cancellable(cancelled, |handle| {
status(crate::engine::write::write_archive(handle, path, -1, 1, 0))
})
}
pub fn write(&mut self, path: &Path) -> Result<(), Error> {
validate_text(path.as_os_str())?;
status(crate::engine::write::write_archive(
&mut self.0,
path,
-1,
1,
0,
))
}
pub fn overwrite(mut self) -> Result<(), Error> {
status(crate::engine::overwrite::overwrite_archive(
&mut self.0,
1,
0,
))
}
pub fn export_image(
&mut self,
index: ImageIndex,
destination: &mut Self,
name: Option<&str>,
boot: bool,
) -> Result<(), Error> {
if let Some(name) = name {
validate_text(OsStr::new(name))?;
}
engine(crate::engine::export::export_archive(
&mut self.0,
index.0,
&mut destination.0,
name,
None,
i32::from(boot),
))
}
pub fn set_image_property(
&mut self,
index: ImageIndex,
property: &str,
value: &str,
) -> Result<(), Error> {
validate_text(OsStr::new(property))?;
validate_text(OsStr::new(value))?;
engine(crate::engine::properties::set_property(
&mut self.0,
index.0,
property.as_bytes(),
Some(value.as_bytes()),
))
}
pub fn set_boot_index(&mut self, index: ImageIndex) -> Result<(), Error> {
let info = crate::engine::info::WimInfo {
boot_index: index.get(),
..Default::default()
};
status(crate::engine::info::set_info(&mut self.0, &info, 4))
}
pub fn add_file(
&mut self,
index: ImageIndex,
source: &Path,
destination: &str,
) -> Result<(), Error> {
validate_text(source.as_os_str())?;
validate_text(OsStr::new(destination))?;
engine(crate::engine::update::add_file(
&mut self.0,
index.0,
source,
OsStr::new(destination),
0,
))
}
pub fn capture_image(&mut self, source: &Path) -> Result<(), Error> {
self.capture_image_with_options(source, CaptureOptions::default())
}
pub fn capture_image_with_options(
&mut self,
source: &Path,
options: CaptureOptions,
) -> Result<(), Error> {
validate_text(source.as_os_str())?;
engine(crate::engine::capture::capture_image(
&mut self.0,
source,
None,
if options.strict_security { 0x40 } else { 0 },
))
.map(|_| ())
}
pub fn extract_image(&mut self, index: ImageIndex, destination: &Path) -> Result<(), Error> {
validate_text(destination.as_os_str())?;
engine(crate::engine::extract::extract_image(
&self.0,
index.0,
destination,
0,
))
}
pub fn extract_path_case_insensitive_with_cancel(
&mut self,
index: ImageIndex,
path: &str,
destination: &Path,
cancelled: impl FnMut() -> bool,
) -> Result<PathBuf, Error> {
self.cancellable(cancelled, |handle| {
Self::extract_case_insensitive(handle, index, path, destination)
})
}
pub fn extract_path_case_insensitive(
&mut self,
index: ImageIndex,
path: &str,
destination: &Path,
) -> Result<PathBuf, Error> {
Self::extract_case_insensitive(&self.0, index, path, destination)
}
fn extract_case_insensitive(
handle: &crate::engine::handles::WimHandle,
index: ImageIndex,
path: &str,
destination: &Path,
) -> Result<PathBuf, Error> {
if path.contains('\0') {
return Err(Error::InteriorNul);
}
let actual = {
let bytes = crate::engine::handles::image_metadata_bytes(handle, index.0 as usize - 1)
.map_err(|e| Error::Engine(e as i32))?;
let tree = wim_format::metadata::Metadata::parse(&bytes)
.map_err(|e| Error::Engine(e as i32))?;
if tree.nodes.is_empty() {
return Err(Error::Engine(49));
}
let mut node = 0;
let mut names = Vec::new();
for component in path.split(['/', '\\']).filter(|s| !s.is_empty()) {
let entry = tree.inode_entry(node).ok_or(Error::Engine(49))?;
if entry.attributes & 0x410 != 0x10 {
return Err(Error::Engine(49));
}
let folded: Vec<_> = component
.encode_utf16()
.map(wim_format::ntfs_upcase::uppercase)
.collect();
let mut matches = tree.nodes[node].children.iter().copied().filter(|&i| {
tree.nodes[i]
.entry
.name
.chunks_exact(2)
.map(|b| {
wim_format::ntfs_upcase::uppercase(u16::from_le_bytes([b[0], b[1]]))
})
.eq(folded.iter().copied())
});
node = matches.next().ok_or(Error::Engine(49))?;
if matches.next().is_some() {
return Err(Error::Engine(46));
}
let units: Vec<_> = tree.nodes[node]
.entry
.name
.chunks_exact(2)
.map(|b| u16::from_le_bytes([b[0], b[1]]))
.collect();
names.push(String::from_utf16(&units).map_err(|_| Error::Engine(49))?);
}
if names.is_empty() {
return Err(Error::Engine(49));
}
names
};
validate_text(destination.as_os_str())?;
let path = format!("/{}", actual.join("/"));
engine(crate::engine::extract::extract_paths(
handle,
index.0,
destination,
&[OsStr::new(&path)],
0x0020_0000,
))?;
Ok(destination.join(actual.last().ok_or(Error::Engine(49))?))
}
pub fn extract_path(
&mut self,
index: ImageIndex,
path: &OsStr,
destination: &Path,
) -> Result<(), Error> {
validate_text(path)?;
validate_text(destination.as_os_str())?;
engine(crate::engine::extract::extract_paths(
&self.0,
index.0,
destination,
&[path],
0x0020_0000,
))
}
}