use super::{MAX_INPUT_BYTES, bib_error, private_environment, read_limited, stage}; use sha2::{Digest, Sha256}; use std::{ env, fs, os::windows::process::CommandExt, path::{Path, PathBuf}, process::Command, sync::Arc, }; const CREATE_NO_WINDOW: u32 = 0x0800_0000; pub(super) fn bundled_directory() -> Option { let backend = PathBuf::from(env::var_os("TEXRRA_WINDOWS_BACKEND")?); if !backend.is_absolute() || env::current_exe().ok()?.canonicalize().ok()? != backend.join("bin/native_editor.exe") { return None; } Some(backend.join("tools/bibtex")) } pub(super) fn tool_identity() -> Option<[u8; 32]> { let directory = bundled_directory()?; let mut hash = Sha256::new(); hash.update(b"texrra Windows BibTeX 1"); for name in [ "bibtex.exe", "kpsewhich.exe", "kpathsealibw64.dll", "msvcr100.dll", "texmf.cnf", ] { let file = directory.join(name); if !fs::symlink_metadata(&file).ok()?.is_file() { return None; } let bytes = read_limited(&file, 16 * 1024 * 1024).ok()?; super::cache::field(&mut hash, name.as_bytes()); super::cache::field(&mut hash, &bytes); } Some(hash.finalize().into()) } pub(super) fn bundled_command(directory: &Path, scratch: &Path) -> Result { // Keep Kpathsea's search relative to its working directory. The tool // never needs to interpret a canonical Windows path or a user search list. let configuration = read_limited(&directory.join("texmf.cnf"), 64 * 1024).map_err(bib_error)?; stage(scratch, Path::new("texmf.cnf"), &configuration).map_err(bib_error)?; let executable = win32_path(&directory.join("bibtex.exe"))?; let working_directory = win32_path(scratch)?; let mut command = Command::new(executable); private_environment(&mut command, &working_directory) .creation_flags(CREATE_NO_WINDOW) .env_remove("PATH") .env("HOME", ".") .env("TEXMFHOME", ".") .env("TEXMFVAR", ".") .env("TEXMFCONFIG", ".") .env("TEXMFCNF", "."); if let Some(root) = env::var_os("SystemRoot") { command.env("SystemRoot", root); } Ok(command) } // The packaged Kpathsea rejects the verbatim prefix in the executable // spelling it receives from the Windows process launcher. fn win32_path(path: &Path) -> Result { let value = path.to_str().ok_or("BibTeX needs Unicode Windows paths")?; if let Some(rest) = value.strip_prefix(r"\\?\UNC\") { return Ok(format!(r"\\{rest}").into()); } Ok(value.strip_prefix(r"\\?\").unwrap_or(value).into()) } pub(super) fn find_style(style: &Path, roots: &[PathBuf]) -> Result, String> { for root in roots { let style_root = root.join("bibtex/bst"); let mut directories = vec![style_root]; while let Some(directory) = directories.pop() { let candidate = directory.join(style); if candidate.is_file() { let path = candidate.canonicalize().map_err(bib_error)?; if path.starts_with(root) { return read_limited(&path, MAX_INPUT_BYTES) .map(Into::into) .map_err(bib_error); } } // Bundled resources are ordinary files. Keep root priority and // a stable first match, without following directory links. if let Ok(entries) = fs::read_dir(directory) { let mut children = entries .filter_map(Result::ok) .filter(|entry| entry.file_type().is_ok_and(|kind| kind.is_dir())) .map(|entry| entry.path()) .collect::>(); children.sort_unstable(); directories.extend(children.into_iter().rev()); } } } Err(format!("BibTeX style missing: {}", style.display())) }