use crate::engine::TChar;
use crate::engine::collections::FallibleCollections as _;
use std::collections::HashMap;
use std::ffi::c_int;
use std::path::{Path, PathBuf};
use wim_format::xml::XmlInfo;
#[derive(Debug, Clone)]
pub enum HandleImage {
Source(u32),
Empty(EmptyImage),
Owned(OwnedImage),
}
#[derive(Debug)]
pub struct RetainedPath(Vec<u8>);
impl RetainedPath {
pub fn new(path: &std::path::Path) -> Result<Self, ParseError> {
Ok(Self(retain_archive_bytes(
path.as_os_str().as_encoded_bytes(),
)?))
}
pub fn as_path(&self) -> &std::path::Path {
self
}
pub fn try_clone(&self) -> Result<Self, ParseError> {
Self::new(self)
}
}
impl std::ops::Deref for RetainedPath {
type Target = std::path::Path;
fn deref(&self) -> &Self::Target {
let value = unsafe { std::ffi::OsStr::from_encoded_bytes_unchecked(&self.0) };
std::path::Path::new(value)
}
}
impl AsRef<std::path::Path> for RetainedPath {
fn as_ref(&self) -> &std::path::Path {
self
}
}
#[derive(Debug, Clone)]
pub struct OwnedResource {
pub blob: wim_format::lookup::LookupBlob,
pub resource: wim_format::lookup::LookupResource,
pub part: u16,
}
#[derive(Debug, Clone)]
pub struct OwnedImage {
pub metadata: Vec<u8>,
pub hash: [u8; 20],
pub pending: Option<std::sync::Arc<std::sync::Mutex<PendingMetadata>>>,
pub descriptor: Option<OwnedResource>,
pub identity: (u64, u32),
}
#[derive(Debug)]
pub enum OwnedBlobData {
Decoded(Vec<u8>),
Memory(std::sync::Arc<Vec<u8>>),
}
impl OwnedBlobData {
pub fn as_slice(&self) -> &[u8] {
match self {
Self::Decoded(bytes) => bytes,
Self::Memory(bytes) => bytes.as_slice(),
}
}
pub fn is_memory(&self) -> bool {
matches!(self, Self::Memory(_))
}
pub fn try_clone(&self) -> Result<Self, ParseError> {
match self {
Self::Memory(bytes) => Ok(Self::Memory(bytes.clone())),
Self::Decoded(bytes) => {
let mut copied = Vec::new();
copied
.try_reserve_exact(bytes.len())
.map_err(|_| ParseError::Nomem)?;
copied.extend_from_slice(bytes);
Ok(Self::Decoded(copied))
}
}
}
pub fn memory(bytes: &[u8]) -> Result<Self, ParseError> {
let mut retained = Vec::new();
retained
.try_extend_from_slice(bytes)
.map_err(|_| ParseError::Nomem)?;
Ok(Self::Memory(std::sync::Arc::new(retained)))
}
}
impl AsRef<[u8]> for OwnedBlobData {
fn as_ref(&self) -> &[u8] {
self.as_slice()
}
}
impl std::ops::Deref for OwnedBlobData {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.as_slice()
}
}
#[derive(Debug)]
pub struct OwnedBlob {
pub bytes: OwnedBlobData,
pub backing: Option<std::sync::Arc<crate::engine::backing::Backing>>,
pub captured: Option<std::sync::Arc<crate::engine::capture::CapturedStream>>,
pub descriptor: OwnedResource,
}
pub(crate) fn new_identity() -> u64 {
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}
pub(crate) fn image_identity(handle: &WimHandle, image: &HandleImage) -> (u64, u32) {
match image {
HandleImage::Source(index) => (handle.identity, *index),
HandleImage::Empty(empty) => (empty.identity, 0),
HandleImage::Owned(owned) => owned.identity,
}
}
#[derive(Debug)]
pub struct PendingMetadata {
pub capture: Option<crate::engine::capture::CapturePlan>,
pub metadata: Vec<u8>,
pub hash: [u8; 20],
}
pub(crate) fn pending_metadata(
image: &HandleImage,
) -> Option<&std::sync::Arc<std::sync::Mutex<PendingMetadata>>> {
match image {
HandleImage::Empty(empty) => Some(&empty.shared),
HandleImage::Owned(owned) => owned.pending.as_ref(),
HandleImage::Source(_) => None,
}
}
pub(crate) fn image_metadata_hash(
handle: &WimHandle,
image: &HandleImage,
) -> Result<[u8; 20], ParseError> {
if let Some(pending) = pending_metadata(image) {
return pending
.lock()
.map(|metadata| metadata.hash)
.map_err(|_| ParseError::InvalidParam);
}
match image {
HandleImage::Source(index) => handle
.lookup
.as_ref()
.and_then(|lookup| lookup.metadata.get((*index - 1) as usize))
.map(|blob| blob.hash)
.ok_or(ParseError::MetadataNotFound),
HandleImage::Owned(owned) => Ok(owned.hash),
HandleImage::Empty(_) => Err(ParseError::InvalidParam),
}
}
pub(crate) fn image_metadata_bytes(
handle: &WimHandle,
image_index: usize,
) -> Result<std::borrow::Cow<'_, [u8]>, ParseError> {
if image_index >= handle.header.image_count as usize {
return Err(ParseError::InvalidImage);
}
let image = handle
.images
.get(image_index)
.ok_or(ParseError::MetadataNotFound)?;
if let Some(pending) = pending_metadata(image) {
let pending = pending.lock().map_err(|_| ParseError::InvalidParam)?;
let mut bytes = Vec::new();
bytes
.try_reserve_exact(pending.metadata.len())
.map_err(|_| ParseError::Nomem)?;
bytes.extend_from_slice(&pending.metadata);
return Ok(std::borrow::Cow::Owned(bytes));
}
match image {
HandleImage::Source(index) => {
let bytes = handle
.backing
.as_deref()
.ok_or(ParseError::MetadataNotFound)?;
let metadata = bytes.archive()?.read_metadata(*index).map_err(|error| {
if error == ParseError::InvalidResourceHash {
ParseError::InvalidMetadataResource
} else {
error
}
})?;
Ok(std::borrow::Cow::Owned(metadata))
}
HandleImage::Owned(owned) => Ok(std::borrow::Cow::Borrowed(&owned.metadata)),
HandleImage::Empty(_) => Err(ParseError::InvalidParam),
}
}
pub(crate) fn image_capture_plan(
image: &HandleImage,
) -> Result<Option<crate::engine::capture::CapturePlan>, ParseError> {
pending_metadata(image)
.map(|pending| {
pending
.lock()
.map(|p| p.capture.clone())
.map_err(|_| ParseError::InvalidParam)
})
.transpose()
.map(Option::flatten)
}
#[derive(Debug, Clone)]
pub struct EmptyImage {
pub identity: u64,
pub shared: std::sync::Arc<std::sync::Mutex<PendingMetadata>>,
}
use wim_format::{Compression, Header, ParseError, ResourceHeader, WIM_MAGIC};
pub struct WimHandle {
pub progress: std::cell::Cell<crate::engine::progress::ProgressRegistration>,
pub identity: u64,
pub header: Header,
pub xml: Box<XmlInfo>,
pub backing: Option<std::sync::Arc<crate::engine::backing::Backing>>,
pub lookup: Option<wim_format::lookup::LookupTable>,
pub(crate) blob_index: crate::engine::blob_index::BlobIndex,
pub filename: Option<RetainedPath>,
pub xml_strings: HashMap<Vec<u8>, Vec<TChar>>,
pub images: Vec<HandleImage>,
pub image_owners: Vec<std::sync::Arc<()>>,
pub dirty_images: hashbrown::HashSet<(u64, u32)>,
pub image_deletion_occurred: bool,
pub removed_blobs: hashbrown::HashSet<[u8; 20]>,
pub owned_blobs: Box<hashbrown::HashMap<[u8; 20], OwnedBlob>>,
pub output_compression: Compression,
pub output_chunk_size: u32,
pub output_solid_compression: Compression,
pub output_solid_chunk_size: u32,
}
pub(crate) unsafe fn handle_ref<'a>(pointer: *const WimHandle) -> Option<&'a WimHandle> {
unsafe { pointer.as_ref() }
}
pub(crate) unsafe fn handle_mut<'a>(pointer: *mut WimHandle) -> Option<&'a mut WimHandle> {
unsafe { pointer.as_mut() }
}
pub(crate) fn can_modify(handle: &WimHandle) -> bool {
handle.header.total_parts == 1
&& handle.header.flags & 4 == 0
&& handle.filename.as_ref().is_none_or(|path| writable(path))
}
#[cfg(unix)]
fn writable(path: &Path) -> bool {
use std::os::unix::ffi::OsStrExt;
unsafe extern "C" {
fn access(path: *const std::ffi::c_char, mode: c_int) -> c_int;
}
let Ok(path) = std::ffi::CString::new(path.as_os_str().as_bytes()) else {
return false;
};
unsafe { access(path.as_ptr(), 2) == 0 }
}
#[cfg(not(unix))]
fn writable(path: &Path) -> bool {
std::fs::OpenOptions::new().write(true).open(path).is_ok()
}
pub(crate) struct HandleStorage(Box<std::mem::MaybeUninit<WimHandle>>);
impl HandleStorage {
pub(crate) fn new() -> Option<Self> {
Some(Self(Box::new(std::mem::MaybeUninit::uninit())))
}
pub(crate) unsafe fn publish(
mut self,
handle: WimHandle,
output: *mut *mut WimHandle,
) -> c_int {
self.0.write(handle);
let pointer = Box::into_raw(self.0).cast::<WimHandle>();
unsafe {
output.write(pointer);
}
0
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wimlib_create_new_wim(
compression: c_int,
output: *mut *mut WimHandle,
) -> c_int {
let initialization = crate::engine::runtime::initialize(0);
if initialization != 0 {
return initialization;
}
if output.is_null() {
return 24;
}
let compression = match Compression::from_i32(compression) {
Ok(c) => c,
Err(error) => return error as c_int,
};
match create_handle(compression) {
Ok(handle) => {
unsafe {
output.write(Box::into_raw(handle));
}
0
}
Err(error) => error,
}
}
pub(crate) fn create_handle(compression: Compression) -> Result<Box<WimHandle>, c_int> {
let initialization = crate::engine::runtime::initialize(0);
if initialization != 0 {
return Err(initialization);
}
let empty = ResourceHeader {
size_in_wim: 0,
flags: 0,
offset_in_wim: 0,
uncompressed_size: 0,
};
let header = Header {
magic: WIM_MAGIC,
version: 0x10d00,
flags: 0,
chunk_size: 0,
guid: [0; 16],
part_number: 1,
total_parts: 1,
image_count: 0,
blob_table: empty,
xml_data: empty,
boot_metadata: empty,
boot_index: 0,
integrity_table: empty,
reserved: [0; 60],
};
let xml = match XmlInfo::parse_bytes(b"<WIM/>") {
Ok(xml) => Box::new(xml),
Err(error) => return Err(error as c_int),
};
let chunk_size = match compression {
Compression::None => 0,
Compression::Lzms => 131072,
_ => 32768,
};
let owned_blobs = match new_owned_blob_table() {
Ok(table) => table,
Err(_) => return Err(39),
};
let blob_index = match crate::engine::blob_index::BlobIndex::new(64) {
Ok(index) => index,
Err(_) => return Err(39),
};
let handle = WimHandle {
progress: std::cell::Cell::new(crate::engine::progress::ProgressRegistration::default()),
identity: new_identity(),
header,
xml,
backing: None,
lookup: None,
blob_index,
filename: None,
xml_strings: HashMap::new(),
images: Vec::new(),
image_owners: Vec::new(),
dirty_images: hashbrown::HashSet::new(),
image_deletion_occurred: false,
removed_blobs: hashbrown::HashSet::new(),
owned_blobs,
output_compression: compression,
output_chunk_size: chunk_size,
output_solid_compression: Compression::Lzms,
output_solid_chunk_size: 67108864,
};
Ok(Box::new(handle))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wimlib_free(handle: *mut WimHandle) {
if !handle.is_null() {
unsafe {
drop(Box::from_raw(handle));
}
}
}
#[cfg(unix)]
pub(crate) unsafe fn borrowed_path_from_pointer<'a>(
pointer: *const TChar,
) -> Result<&'a std::path::Path, c_int> {
use std::os::unix::ffi::OsStrExt;
if pointer.is_null() {
return Err(24);
}
let bytes = unsafe { std::ffi::CStr::from_ptr(pointer) }.to_bytes();
if bytes.is_empty() {
return Err(24);
}
Ok(std::path::Path::new(std::ffi::OsStr::from_bytes(bytes)))
}
pub(crate) unsafe fn path_from_pointer(pointer: *const TChar) -> Result<PathBuf, c_int> {
if pointer.is_null() {
return Err(24);
}
#[cfg(not(windows))]
{
use std::os::unix::ffi::OsStrExt;
let path = unsafe { std::ffi::CStr::from_ptr(pointer) }.to_bytes();
if path.is_empty() {
return Err(24);
}
Ok(PathBuf::from(std::ffi::OsStr::from_bytes(path)))
}
#[cfg(windows)]
{
use std::os::windows::ffi::OsStringExt;
let mut length = 0;
while unsafe { *pointer.add(length) } != 0 {
length += 1;
}
if length == 0 {
return Err(24);
}
Ok(
std::ffi::OsString::from_wide(unsafe { std::slice::from_raw_parts(pointer, length) })
.into(),
)
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wimlib_open_wim(
filename: *const TChar,
flags: c_int,
output: *mut *mut WimHandle,
) -> c_int {
unsafe { wimlib_open_wim_with_progress(filename, flags, output, None, std::ptr::null_mut()) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wimlib_open_wim_with_progress(
filename: *const TChar,
flags: c_int,
output: *mut *mut WimHandle,
callback: Option<crate::engine::progress::ProgressCallback>,
context: *mut std::ffi::c_void,
) -> c_int {
if flags & !7 != 0 || output.is_null() {
return 24;
}
let path = match unsafe { path_from_pointer(filename) } {
Ok(path) => path,
Err(error) => return error,
};
let registration = crate::engine::progress::ProgressRegistration::new(callback, context);
match open_path(&path, flags, registration) {
Ok(handle) => {
unsafe {
output.write(Box::into_raw(handle));
}
0
}
Err(error) => error,
}
}
pub(crate) fn open_path(
path: &Path,
flags: c_int,
registration: crate::engine::progress::ProgressRegistration,
) -> Result<Box<WimHandle>, c_int> {
if flags & !7 != 0 || path.as_os_str().is_empty() {
return Err(24);
}
let initialization = crate::engine::runtime::initialize(0);
if initialization != 0 {
return Err(initialization);
}
let file = match std::fs::File::open(path) {
Ok(file) => file,
Err(error) => {
let mut message = b"Can't open \"".to_vec();
message.extend_from_slice(path.as_os_str().as_encoded_bytes());
message.extend_from_slice(b"\" read-only");
#[cfg(target_os = "linux")]
if let Some(errno) = error.raw_os_error() {
unsafe {
*libc::__errno_location() = errno;
}
}
#[cfg(not(target_os = "linux"))]
let _ = error;
crate::engine::diagnostics::message(false, &message, true);
return Err(47);
}
};
let backing = match crate::engine::backing::Backing::new(file) {
Ok(backing) => backing,
Err(error) => return Err(error as c_int),
};
let canonical = match std::fs::canonicalize(path) {
Ok(path) => path,
Err(_) => return Err(45),
};
open_handle(backing, canonical, flags, registration)
.map(Box::new)
.map_err(|error| error as c_int)
}
pub(crate) fn open_path_with_cancel(
path: &Path,
flags: c_int,
cancelled: &mut impl FnMut() -> bool,
) -> Result<Box<WimHandle>, c_int> {
let registration =
unsafe { crate::engine::progress::ProgressRegistration::cancellation(cancelled) };
let handle = open_path(path, flags, registration)?;
handle.progress.set(Default::default());
Ok(handle)
}
fn open_handle(
backing: crate::engine::backing::Backing,
filename: PathBuf,
flags: c_int,
registration: crate::engine::progress::ProgressRegistration,
) -> Result<WimHandle, ParseError> {
let mut header = backing.header()?;
if flags & 4 != 0 && (header.total_parts != 1 || header.flags & 4 != 0 || !writable(&filename))
{
return Err(ParseError::WimIsReadonly);
}
if flags & 2 != 0 && header.total_parts != 1 {
return Err(ParseError::IsSplitWim);
}
if header.boot_index > header.image_count {
header.boot_index = 0;
}
let compression = header.validate_compression()?;
if flags & 1 != 0 {
let progress_filename = crate::engine::progress::filename_buffer(Some(&filename))?;
let status = wim_format::file_archive::check_integrity_with_progress(
&mut backing.reader(),
&header,
|state| {
let mut info = crate::engine::progress::ProgressInfo::zeroed();
info.integrity = crate::engine::progress::IntegrityProgress {
total_bytes: state.total_bytes,
completed_bytes: state.completed_bytes,
total_chunks: state.total_chunks,
completed_chunks: state.completed_chunks,
chunk_size: state.chunk_size,
filename: progress_filename
.as_ref()
.map_or(std::ptr::null(), |s| s.as_ptr()),
};
unsafe { registration.call(16, &mut info) }
},
)?;
if status == wim_format::integrity::IntegrityStatus::Mismatch {
return Err(ParseError::Integrity);
}
}
if header.blob_table.uncompressed_size == 0 && header.xml_data.uncompressed_size == 0 {
return Err(ParseError::WimIsIncomplete);
}
let xml_bytes =
wim_format::file_archive::read_resource(&mut backing.reader(), &header, &header.xml_data)?;
let xml = Box::new(XmlInfo::parse_utf16le(&xml_bytes)?);
if xml.image_count() != header.image_count as usize {
return Err(ParseError::ImageCount);
}
let lookup = backing.archive()?.lookup.try_clone()?;
let backing = std::sync::Arc::new(backing);
let chunk_size = header.chunk_size;
let mut images = Vec::new();
if header.part_number == 1 {
images
.try_reserve(header.image_count as usize)
.map_err(|_| ParseError::Nomem)?;
images
.try_extend((1..=header.image_count).map(HandleImage::Source))
.map_err(|_| ParseError::Nomem)?;
}
let mut image_owners = Vec::new();
image_owners
.try_reserve(images.len())
.map_err(|_| ParseError::Nomem)?;
for _ in &images {
image_owners
.try_push(new_image_owner()?)
.map_err(|_| ParseError::Nomem)?;
}
let filename = RetainedPath::new(&filename)?;
let owned_blobs = new_owned_blob_table()?;
let raw_entries =
usize::try_from(header.blob_table.uncompressed_size / 50).map_err(|_| ParseError::Nomem)?;
let blob_index = crate::engine::blob_index::BlobIndex::new(raw_entries)?;
for blob in &lookup.blobs {
blob_index.insert(blob.hash, crate::engine::blob_index::BlobOwner::Stored)?;
}
Ok(WimHandle {
progress: std::cell::Cell::new(registration),
identity: new_identity(),
header,
xml,
backing: Some(backing),
lookup: Some(lookup),
blob_index,
filename: Some(filename),
xml_strings: HashMap::new(),
images,
image_owners,
dirty_images: hashbrown::HashSet::new(),
image_deletion_occurred: false,
removed_blobs: hashbrown::HashSet::new(),
owned_blobs,
output_compression: compression,
output_chunk_size: chunk_size,
output_solid_compression: Compression::Lzms,
output_solid_chunk_size: 67108864,
})
}
pub(crate) fn own_xml(xml: XmlInfo) -> Result<Box<XmlInfo>, ParseError> {
Ok(Box::new(xml))
}
pub(crate) fn new_owned_blob_table()
-> Result<Box<hashbrown::HashMap<[u8; 20], OwnedBlob>>, ParseError> {
let mut table = hashbrown::HashMap::new();
table.try_reserve(64).map_err(|_| ParseError::Nomem)?;
Ok(Box::new(table))
}
pub(crate) fn retain_archive_bytes(bytes: &[u8]) -> Result<Vec<u8>, ParseError> {
let mut retained = Vec::new();
retained
.try_extend_from_slice(bytes)
.map_err(|_| ParseError::Nomem)?;
Ok(retained)
}
pub(crate) fn new_pending_metadata(
metadata: PendingMetadata,
) -> Result<std::sync::Arc<std::sync::Mutex<PendingMetadata>>, ParseError> {
Ok(std::sync::Arc::new(std::sync::Mutex::new(metadata)))
}
pub(crate) fn new_image_owner() -> Result<std::sync::Arc<()>, ParseError> {
Ok(std::sync::Arc::new(()))
}
pub(crate) fn image_owner_count(handle: &WimHandle, index: usize) -> usize {
std::sync::Arc::strong_count(&handle.image_owners[index])
}
#[cfg(test)]
mod tests {
use super::create_handle;
use crate::engine::{image_mutation, info, properties};
use wim_format::Compression;
#[test]
fn typed_archive_mutations_preserve_boot_validation_and_owned_metadata() {
let mut archive = create_handle(Compression::None).unwrap();
let image = image_mutation::add_empty(&mut archive, Some(b"Initial")).unwrap();
properties::set_property(&mut archive, image as i32, b"NAME", Some(b"Renamed")).unwrap();
let mut state = info::info(&archive);
state.boot_index = image + 1;
assert_eq!(info::set_info(&mut archive, &state, 4), Err(18));
assert_eq!(info::info(&archive).boot_index, 0);
state.boot_index = image;
info::set_info(&mut archive, &state, 4).unwrap();
assert_eq!(
archive.xml.name_bytes(image as i32),
Some(b"Renamed".as_slice())
);
image_mutation::delete_images(&mut archive, image as i32).unwrap();
let state = info::info(&archive);
assert_eq!(state.image_count, 0);
assert_eq!(state.boot_index, 0);
assert!(archive.images.is_empty());
}
}