use crate::engine::TChar;
use std::ffi::{c_int, c_void};
use std::sync::Mutex;
struct State {
show: bool,
sink: usize,
owned: bool,
}
static STATE: Mutex<State> = Mutex::new(State {
show: false,
sink: 0,
owned: false,
});
unsafe extern "C" {
fn fclose(stream: *mut c_void) -> c_int;
fn fflush(stream: *mut c_void) -> c_int;
fn fwrite(data: *const c_void, size: usize, count: usize, stream: *mut c_void) -> usize;
#[cfg(not(windows))]
fn fopen(path: *const TChar, mode: *const std::ffi::c_char) -> *mut c_void;
#[cfg(unix)]
static mut stderr: *mut c_void;
#[cfg(unix)]
static mut stdout: *mut c_void;
#[cfg(windows)]
fn __acrt_iob_func(index: u32) -> *mut c_void;
#[cfg(windows)]
fn _wfopen(path: *const TChar, mode: *const u16) -> *mut c_void;
}
fn lock() -> std::sync::MutexGuard<'static, State> {
STATE
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
pub(crate) fn ensure_default_sink() {
let mut state = lock();
if state.sink == 0 {
#[cfg(unix)]
{
state.sink = unsafe { stderr } as usize;
}
#[cfg(windows)]
{
state.sink = unsafe { __acrt_iob_func(2) } as usize;
}
}
}
#[unsafe(no_mangle)]
pub extern "C" fn wimlib_set_print_errors(show: bool) -> c_int {
lock().show = show;
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wimlib_set_error_file(stream: *mut c_void) -> c_int {
let mut state = lock();
if state.owned {
unsafe {
fclose(state.sink as *mut c_void);
}
}
state.sink = stream as usize;
state.show = !stream.is_null();
state.owned = false;
0
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wimlib_set_error_file_by_name(path: *const TChar) -> c_int {
if path.is_null() {
return 24;
}
#[cfg(not(windows))]
let stream = unsafe { fopen(path, c"a".as_ptr()) };
#[cfg(windows)]
let stream = unsafe { _wfopen(path, [b'a' as u16, 0].as_ptr()) };
if stream.is_null() {
return 47;
}
let mut state = lock();
if state.owned {
unsafe {
fclose(state.sink as *mut c_void);
}
}
state.sink = stream as usize;
state.show = true;
state.owned = true;
0
}
pub(crate) fn cleanup() {
unsafe {
wimlib_set_error_file(std::ptr::null_mut());
}
}
pub(crate) fn message(warning: bool, text: &[u8], with_errno: bool) {
#[cfg(target_os = "linux")]
let saved_errno = unsafe { *libc::__errno_location() };
let state = lock();
if !state.show || state.sink == 0 {
return;
}
let sink = state.sink as *mut c_void;
#[cfg(unix)]
let output = unsafe { stdout };
#[cfg(windows)]
let output = unsafe { __acrt_iob_func(1) };
let tag = if warning {
b"\r[WARNING] ".as_slice()
} else {
b"\r[ERROR] ".as_slice()
};
unsafe {
fflush(output);
fwrite(tag.as_ptr().cast(), 1, tag.len(), sink);
fwrite(text.as_ptr().cast(), 1, text.len(), sink);
}
#[cfg(target_os = "linux")]
if with_errno && saved_errno != 0 {
let description = unsafe { libc::strerror(saved_errno) };
let fallback;
let bytes = if description.is_null() {
fallback = format!("unknown error (errno={saved_errno})").into_bytes();
fallback.as_slice()
} else {
let raw = unsafe { std::ffi::CStr::from_ptr(description) }.to_bytes();
if raw.len() < 64 {
raw
} else {
fallback = format!("unknown error (errno={saved_errno})").into_bytes();
fallback.as_slice()
}
};
unsafe {
fwrite(b": ".as_ptr().cast(), 1, 2, sink);
fwrite(bytes.as_ptr().cast(), 1, bytes.len(), sink);
}
}
#[cfg(not(target_os = "linux"))]
let _ = with_errno;
unsafe {
fwrite(b"\n".as_ptr().cast(), 1, 1, sink);
fflush(sink);
}
#[cfg(target_os = "linux")]
unsafe {
*libc::__errno_location() = saved_errno;
}
}