diff options
| author | rorka900 <rorka900@gmail.com> | 2026-04-14 16:16:59 +0300 |
|---|---|---|
| committer | rorka900 <rorka900@gmail.com> | 2026-04-14 16:16:59 +0300 |
| commit | 025cb091fd9518df7249f42c626d52e1868b3c41 (patch) | |
| tree | 3d8ab02f6f63d65b9df70e9d90a53d937492e152 /src/install.rs | |
| parent | 7f0a8d633b8c9e3ecb81a625fea97ccf34ac3276 (diff) | |
| download | ror-025cb091fd9518df7249f42c626d52e1868b3c41.tar.gz ror-025cb091fd9518df7249f42c626d52e1868b3c41.zip | |
feat: add version constraints to resolver and debug logging
Diffstat (limited to 'src/install.rs')
| -rw-r--r-- | src/install.rs | 96 |
1 files changed, 82 insertions, 14 deletions
diff --git a/src/install.rs b/src/install.rs index 8749f2c..a3bd400 100644 --- a/src/install.rs +++ b/src/install.rs @@ -11,9 +11,53 @@ use std::collections::HashMap; use std::os::unix::fs::symlink; use crate::config; use crate::progress::{Spinner, format_bytes}; +use crate::debug; const REPO_ROOT: &str = "/var/ror/packages"; +#[derive(Debug, Clone, PartialEq)] +pub enum VerOp { Ge, Gt, Le, Lt, Eq } + +#[derive(Debug, Clone)] +pub struct VersionConstraint { + #[allow(dead_code)] + pub name: String, + pub op: VerOp, + pub version: String, +} + +pub fn parse_dep(dep: &str) -> (String, Option<VersionConstraint>) { + for (op_str, op) in &[(">=", VerOp::Ge), ("<=", VerOp::Le), (">", VerOp::Gt), ("<", VerOp::Lt), ("==", VerOp::Eq)] { + if let Some(idx) = dep.find(op_str) { + let name = dep[..idx].trim().to_string(); + let ver = dep[idx + op_str.len()..].trim().to_string(); + let c = VersionConstraint { name: name.clone(), op: op.clone(), version: ver }; + return (name, Some(c)); + } + } + (dep.trim().to_string(), None) +} + +pub fn version_satisfies(installed: &str, constraint: &VersionConstraint) -> bool { + use semver::Version; + let a = Version::parse(installed); + let b = Version::parse(&constraint.version); + match (a, b) { + (Ok(iv), Ok(cv)) => match constraint.op { + VerOp::Ge => iv >= cv, + VerOp::Gt => iv > cv, + VerOp::Le => iv <= cv, + VerOp::Lt => iv < cv, + VerOp::Eq => iv == cv, + }, + _ => match constraint.op { + VerOp::Ge | VerOp::Gt => installed >= constraint.version.as_str(), + VerOp::Le | VerOp::Lt => installed <= constraint.version.as_str(), + VerOp::Eq => installed == constraint.version.as_str(), + }, + } +} + #[derive(Debug, Serialize, Deserialize)] #[serde(untagged)] pub enum Dependency { @@ -339,23 +383,26 @@ pub fn install_package(pkg_name: &str, cfg: &config::Config) { } pub fn install_package_with_result(pkg_name: &str, cfg: &config::Config) -> Result<InstalledPackage, String> { + debug!("install_package_with_result: start '{}'", pkg_name); let mut spinner = Spinner::new(&format!("Resolving {}", pkg_name)); spinner.tick(&format!("Resolving {}", pkg_name)); let (pkg_path, repo_name) = find_package_with_repo(pkg_name) .ok_or_else(|| format!("Package '{}' not found", pkg_name))?; + debug!("install_package_with_result: '{}' found in repo '{}' at {}", pkg_name, repo_name, pkg_path.display()); + let repo_cfg = cfg.repositories.get(&repo_name); let gpg_enabled = repo_cfg.map(|r| r.gpg_support).unwrap_or(false); let gpg_key = repo_cfg.and_then(|r| r.gpg_key.as_deref()); - if gpg_enabled { - if gpg_key.is_none() { - return Err(format!( - "Repository '{}' has gpg_support = true but no gpg_key configured", - repo_name - )); - } + debug!("install_package_with_result: repo='{}' gpg_enabled={} gpg_key={:?}", repo_name, gpg_enabled, gpg_key); + + if gpg_enabled && gpg_key.is_none() { + return Err(format!( + "Repository '{}' has gpg_support = true but no gpg_key configured", + repo_name + )); } let content = fs::read_to_string(&pkg_path) @@ -364,15 +411,20 @@ pub fn install_package_with_result(pkg_name: &str, cfg: &config::Config) -> Resu let pkg: Package = serde_yaml::from_str(&content) .map_err(|e| format!("YAML structure error: {}", e))?; + debug!("install_package_with_result: '{}' version={} binaries={}", pkg_name, pkg.version, pkg.binaries.len()); + if pkg.binaries.is_empty() { return Err("No binary packages defined".to_string()); } let binary = select_binary_for_arch(&pkg, &cfg.global.arch, &cfg.global.libc)?; + debug!("install_package_with_result: selected binary arch={} type={:?} libc={:?}", binary.arch, binary.pkg_type, binary.libc); + let db = InstalledDB::load(); for conflict in &pkg.conflicts { if db.is_installed(conflict) { + debug!("install_package_with_result: '{}' conflicts with '{}'", pkg_name, conflict); return Err(format!( "Package '{}' conflicts with installed package '{}'", pkg_name, conflict @@ -380,10 +432,13 @@ pub fn install_package_with_result(pkg_name: &str, cfg: &config::Config) -> Resu } } - let mut urls_to_try: Vec<String> = if !binary.url.is_empty() { - vec![binary.url.clone()] + let mut urls_to_try = Vec::new(); + if !binary.url.is_empty() { + debug!("install_package_with_result: using direct url={}", binary.url); + urls_to_try.push(binary.url.clone()); } else if !binary.mirrors.is_empty() { - binary + debug!("install_package_with_result: building urls from mirrors"); + urls_to_try = binary .mirrors .iter() .filter_map(|m| { @@ -396,19 +451,22 @@ pub fn install_package_with_result(pkg_name: &str, cfg: &config::Config) -> Resu Some(format!("{}/{}", base, binary.filename)) } }) - .collect() + .collect(); } else { return Err(format!("No URL or mirrors provided for package {}", pkg_name)); - }; - + } if urls_to_try.is_empty() { return Err(format!("Could not resolve any download URL for package {}", pkg_name)); } + debug!("install_package_with_result: '{}' urls={:?}", pkg_name, urls_to_try); + if !cfg.global.ignore_speed && urls_to_try.len() > 1 { + debug!("install_package_with_result: testing mirror speeds for '{}'", pkg_name); spinner.tick("Testing mirror speeds..."); urls_to_try = sort_mirrors_by_speed(&urls_to_try); spinner.finish(&format!("Fastest mirror selected: {}", urls_to_try[0])); + debug!("install_package_with_result: fastest mirror={}", urls_to_try[0]); } let mut last_err = None; @@ -416,11 +474,16 @@ pub fn install_package_with_result(pkg_name: &str, cfg: &config::Config) -> Resu let mut downloaded = false; for url in urls_to_try { + debug!("install_package_with_result: trying url={}", url); spinner.tick(&format!("Downloading {} ...", url.split('/').last().unwrap_or(&url))); let archive_path = match download_and_verify(&url, &binary.sha256, cfg) { - Ok(p) => p, + Ok(p) => { + debug!("install_package_with_result: downloaded to {}", p.display()); + p + } Err(e) => { + debug!("install_package_with_result: download failed: {}", e); last_err = Some(e); continue; } @@ -498,20 +561,25 @@ pub fn install_package_with_result(pkg_name: &str, cfg: &config::Config) -> Resu let _ = fs::remove_dir_all(&work_dir); let _ = fs::remove_file(&archive_path); downloaded = true; + debug!("install_package_with_result: '{}' extracted {} files", pkg_name, all_installed_files.len()); break; } if !downloaded { + debug!("install_package_with_result: '{}' all mirrors failed", pkg_name); return Err(last_err.unwrap_or_else(|| "All mirrors/URLs failed".to_string())); } if !pkg.install_steps.is_empty() { + debug!("install_package_with_result: '{}' running install_steps", pkg_name); let mut sp3 = Spinner::new(&format!("Running install steps for {}", pkg_name)); sp3.tick(&format!("Running install steps for {}...", pkg_name)); run_commands(&pkg.install_steps, Path::new("/"))?; sp3.finish("Install steps done"); + debug!("install_package_with_result: '{}' install_steps done", pkg_name); } + debug!("install_package_with_result: '{}' complete v={} files={}", pkg_name, pkg.version, all_installed_files.len()); Ok(InstalledPackage { name: pkg.name.clone(), version: pkg.version.clone(), |