use crate::error::{KopiError, Result};
use std::fs;
use std::path::Path;
#[cfg(unix)]
pub fn verify_shim(shim_path: &Path) -> Result<()> {
use crate::platform::symlink;
if !symlink::is_symlink(shim_path)? {
return Err(KopiError::SystemError("Not a symlink".to_string()));
}
let target = fs::read_link(shim_path)?;
if !target.ends_with("kopi-shim") {
return Err(KopiError::SystemError("Invalid symlink target".to_string()));
}
let target_path = if target.is_relative() {
shim_path.parent().unwrap().join(&target)
} else {
target.clone()
};
if !target_path.exists() {
return Err(KopiError::SystemError("Broken symlink".to_string()));
}
Ok(())
}
#[cfg(windows)]
pub fn verify_shim(shim_path: &Path) -> Result<()> {
if !shim_path.exists() {
return Err(KopiError::SystemError("Shim file missing".to_string()));
}
let metadata = fs::metadata(shim_path)?;
if !metadata.is_file() {
return Err(KopiError::SystemError("Not a regular file".to_string()));
}
if metadata.len() < 1024 {
return Err(KopiError::SystemError(
"Shim file too small - likely corrupted".to_string(),
));
}
let mut file = fs::File::open(shim_path)?;
let mut header = [0u8; 2];
use std::io::Read;
if file.read_exact(&mut header).is_ok() && header != [0x4D, 0x5A] {
return Err(KopiError::SystemError(
"Invalid executable format - not a PE file".to_string(),
));
}
Ok(())
}