use std::{
ffi::OsString,
fs,
path::{Path, PathBuf},
process::ExitCode,
};
use anyhow::{Context, Result};
pub const TOOL_SUBCOMMAND: &str = "tool";
pub fn dispatch(args: &[OsString]) -> Option<ExitCode> {
let invoked_as = args
.first()
.and_then(|program| Path::new(program).file_stem())
.and_then(|stem| stem.to_str())
.filter(|stem| is_tool(stem));
if let Some(name) = invoked_as {
return Some(run_tool(name, args.iter().skip(1).cloned()));
}
if args.get(1).is_some_and(|arg| arg == TOOL_SUBCOMMAND) {
let Some(name) = args.get(2).and_then(|name| name.to_str()) else {
eprintln!(
"error: `gdsuite tool` needs a tool name: gdtest, gdhealth, gdtypecheck or gdtidy"
);
return Some(ExitCode::from(2));
};
if !is_tool(name) {
eprintln!(
"error: no tool named {name}; the tools are gdtest, gdhealth, gdtypecheck and gdtidy"
);
return Some(ExitCode::from(2));
}
return Some(run_tool(name, args.iter().skip(3).cloned()));
}
None
}
fn is_tool(name: &str) -> bool {
matches!(name, "gdtest" | "gdhealth" | "gdtypecheck" | "gdtidy")
}
fn run_tool(name: &str, args: impl Iterator<Item = OsString>) -> ExitCode {
let argv = std::iter::once(OsString::from(name)).chain(args);
match name {
"gdtidy" => gdtidy::run(argv),
"gdtypecheck" => gdtypecheck::run(argv),
"gdtest" => match gdtest::run(argv) {
Ok(()) => ExitCode::SUCCESS,
Err(error) => {
eprintln!("Error: {error:?}");
ExitCode::FAILURE
}
},
"gdhealth" => match gdhealth::run_from(argv) {
Ok(()) => ExitCode::SUCCESS,
Err(error) => {
eprintln!("{error}");
ExitCode::FAILURE
}
},
_ => unreachable!("dispatch checks the tool name"),
}
}
pub fn write_gdtest_addons(dir: &Path) -> Result<PathBuf> {
if dir.exists() {
fs::remove_dir_all(dir).with_context(|| format!("failed to clear {}", dir.display()))?;
}
for (relative, contents) in gdtest::addon_files() {
let path = dir.join(relative);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)
.with_context(|| format!("failed to create {}", parent.display()))?;
}
fs::write(&path, contents)
.with_context(|| format!("failed to write {}", path.display()))?;
}
Ok(dir.to_path_buf())
}
pub fn refresh_link(launcher: &Path) -> Result<()> {
let gdsuite = std::env::current_exe().context("failed to find the gdsuite program")?;
refresh_link_to(launcher, &gdsuite)
}
fn refresh_link_to(launcher: &Path, gdsuite: &Path) -> Result<()> {
static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
if is_current(launcher, gdsuite) {
return Ok(());
}
let parent = launcher.parent().unwrap_or(Path::new("."));
fs::create_dir_all(parent).with_context(|| format!("failed to create {}", parent.display()))?;
let name = launcher.file_name().unwrap_or_default().to_string_lossy();
let next = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let temp = parent.join(format!(".{name}.{}.{next}.tmp", std::process::id()));
let _ = fs::remove_file(&temp);
if fs::hard_link(gdsuite, &temp).is_err() {
fs::copy(gdsuite, &temp).with_context(|| format!("failed to write {}", temp.display()))?;
}
let renamed = fs::rename(&temp, launcher);
let _ = fs::remove_file(&temp);
renamed.with_context(|| format!("failed to replace {}", launcher.display()))
}
fn is_current(launcher: &Path, gdsuite: &Path) -> bool {
let (Ok(launcher), Ok(gdsuite)) = (fs::metadata(launcher), fs::metadata(gdsuite)) else {
return false;
};
let not_older = match (launcher.modified(), gdsuite.modified()) {
(Ok(launcher), Ok(gdsuite)) => launcher >= gdsuite,
_ => false,
};
launcher.len() == gdsuite.len() && not_older
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn launchers_refreshed_at_once_stay_whole() {
let dir = tempfile::tempdir().unwrap();
let gdsuite = dir.path().join("gdsuite-new");
fs::write(&gdsuite, vec![7u8; 64 * 1024]).unwrap();
let launcher = dir.path().join("bin").join("gdsuite");
fs::create_dir_all(launcher.parent().unwrap()).unwrap();
for _ in 0..20 {
let _ = fs::remove_file(&launcher);
fs::write(&launcher, b"old").unwrap();
let threads: Vec<_> = (0..8)
.map(|_| {
let (launcher, gdsuite) = (launcher.clone(), gdsuite.clone());
std::thread::spawn(move || refresh_link_to(&launcher, &gdsuite))
})
.collect();
for thread in threads {
thread.join().unwrap().unwrap();
}
assert_eq!(fs::read(&launcher).unwrap().len(), 64 * 1024);
assert_eq!(fs::read(&gdsuite).unwrap().len(), 64 * 1024);
}
let leftovers: Vec<_> = fs::read_dir(launcher.parent().unwrap())
.unwrap()
.map(|e| e.unwrap().file_name())
.collect();
assert_eq!(leftovers, vec![std::ffi::OsString::from("gdsuite")]);
}
}