diff options
| author | rorka900 <rorka900@gmail.com> | 2026-04-06 21:05:06 +0300 |
|---|---|---|
| committer | rorka900 <rorka900@gmail.com> | 2026-04-06 21:05:06 +0300 |
| commit | 62d9463b3f24516158e9e73cd20ccebfc9eec329 (patch) | |
| tree | 664ee2433618df788e9a3db3118a609329ca6e8d /src/install.rs | |
| parent | 044d414f07b13bfdee5a04bc2484510ecfe0a0f8 (diff) | |
| download | ror-62d9463b3f24516158e9e73cd20ccebfc9eec329.tar.gz ror-62d9463b3f24516158e9e73cd20ccebfc9eec329.zip | |
feat: add sror package type, gpg repo verification, and group command
Diffstat (limited to 'src/install.rs')
| -rw-r--r-- | src/install.rs | 277 |
1 files changed, 268 insertions, 9 deletions
diff --git a/src/install.rs b/src/install.rs index 1801cc0..66ad894 100644 --- a/src/install.rs +++ b/src/install.rs @@ -65,8 +65,6 @@ pub struct BinaryPackage { #[serde(default = "default_install_prefix")] pub install_prefix: String, #[serde(default)] - pub files: Vec<String>, - #[serde(default)] pub libc: Option<String>, #[serde(default)] pub dict: Option<String>, @@ -242,6 +240,84 @@ pub fn find_package_file(pkg_name: &str) -> Option<PathBuf> { None } +pub fn find_package_with_repo(pkg_name: &str) -> Option<(PathBuf, String)> { + let repo = Path::new(REPO_ROOT); + let read_dir = fs::read_dir(repo).ok()?; + + for entry in read_dir.flatten() { + let cat_path = entry.path(); + if !cat_path.is_dir() { + continue; + } + + let pkg_path = cat_path.join(pkg_name).join(format!("{}.yaml", pkg_name)); + if pkg_path.exists() && pkg_path.is_file() { + let repo_name = cat_path.file_name()?.to_string_lossy().to_string(); + return Some((pkg_path, repo_name)); + } + } + None +} + +fn verify_gpg_signature(archive_path: &Path, asc_path: &Path, pubkey_path: &Path) -> Result<(), String> { + if !pubkey_path.exists() { + return Err(format!( + "GPG public key not found: {}", + pubkey_path.display() + )); + } + + if !asc_path.exists() { + return Err(format!( + "GPG signature not found: {} — cannot verify package", + asc_path.display() + )); + } + + let tmp_keyring = Path::new("/tmp/ror-gpg-keyring.gpg"); + + let import = Command::new("gpg") + .args([ + "--batch", + "--no-default-keyring", + "--keyring", tmp_keyring.to_str().unwrap(), + "--import", pubkey_path.to_str().unwrap(), + ]) + .output() + .map_err(|e| format!("Failed to run gpg --import: {}", e))?; + + if !import.status.success() { + return Err(format!( + "Failed to import GPG key {}: {}", + pubkey_path.display(), + String::from_utf8_lossy(&import.stderr) + )); + } + + let verify = Command::new("gpg") + .args([ + "--batch", + "--no-default-keyring", + "--keyring", tmp_keyring.to_str().unwrap(), + "--verify", asc_path.to_str().unwrap(), + archive_path.to_str().unwrap(), + ]) + .output() + .map_err(|e| format!("Failed to run gpg --verify: {}", e))?; + + let _ = fs::remove_file(tmp_keyring); + + if verify.status.success() { + Ok(()) + } else { + Err(format!( + "GPG signature verification failed for {}: {}", + archive_path.display(), + String::from_utf8_lossy(&verify.stderr).lines().next().unwrap_or("unknown error") + )) + } +} + pub fn install_package(pkg_name: &str, cfg: &config::Config) { match install_package_with_result(pkg_name, cfg) { Ok(installed_pkg) => { @@ -264,9 +340,22 @@ pub fn install_package_with_result(pkg_name: &str, cfg: &config::Config) -> Resu let mut spinner = Spinner::new(&format!("Resolving {}", pkg_name)); spinner.tick(&format!("Resolving {}", pkg_name)); - let pkg_path = find_package_file(pkg_name) + let (pkg_path, repo_name) = find_package_with_repo(pkg_name) .ok_or_else(|| format!("Package '{}' not found", pkg_name))?; + 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 + )); + } + } + let content = fs::read_to_string(&pkg_path) .map_err(|e| format!("Failed to read package file: {}", e))?; @@ -295,12 +384,25 @@ pub fn install_package_with_result(pkg_name: &str, cfg: &config::Config) -> Resu binary .mirrors .iter() - .map(|m| m.replace("{filename}", &binary.filename)) + .filter_map(|m| { + if m.starts_with("http://") || m.starts_with("https://") { + Some(m.replace("{filename}", &binary.filename)) + } else { + let repo = cfg.repositories.get(m.as_str())?; + let base = repo.urlrepo.as_deref().or(Some(repo.url.as_str()))?; + let base = base.trim_end_matches('/'); + Some(format!("{}/{}", base, binary.filename)) + } + }) .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)); + } + if !cfg.global.ignore_speed && urls_to_try.len() > 1 { spinner.tick("Testing mirror speeds..."); urls_to_try = sort_mirrors_by_speed(&urls_to_try); @@ -324,6 +426,31 @@ pub fn install_package_with_result(pkg_name: &str, cfg: &config::Config) -> Resu spinner.finish(&format!("Downloaded {}", pkg_name)); + if gpg_enabled { + let asc_url = format!("{}.asc", url); + spinner.tick(&format!("Downloading GPG signature...")); + let asc_path = match download_and_verify(&asc_url, "", cfg) { + Ok(p) => p, + Err(_) => { + let _ = fs::remove_file(&archive_path); + return Err(format!( + "GPG signature not found for '{}' at {} — installation aborted", + pkg_name, asc_url + )); + } + }; + let key_file = gpg_key.unwrap(); + let pubkey_path = Path::new(REPO_ROOT).join(key_file); + spinner.tick(&format!("Verifying GPG signature for {}...", pkg_name)); + if let Err(e) = verify_gpg_signature(&archive_path, &asc_path, &pubkey_path) { + let _ = fs::remove_file(&archive_path); + let _ = fs::remove_file(&asc_path); + return Err(e); + } + let _ = fs::remove_file(&asc_path); + spinner.finish(&format!("GPG signature OK for {}", pkg_name)); + } + let work_dir = Path::new("/tmp/ror-install").join(&pkg.name); let _ = fs::remove_dir_all(&work_dir); fs::create_dir_all(&work_dir).map_err(|e| format!("Failed to create temp dir: {}", e))?; @@ -344,17 +471,22 @@ pub fn install_package_with_result(pkg_name: &str, cfg: &config::Config) -> Resu let files = extract_ror(&archive_path, &dict_path, &work_dir) .map_err(|e| format!("Extraction failed: {}", e))?; let _ = fs::remove_file(&dict_path); - let file_list = if binary.files.is_empty() { files } else { binary.files.clone() }; sp2.tick(&format!("Installing files for {}...", pkg_name)); - let mut installed = install_files(&work_dir, &binary.install_prefix, &file_list) + let mut installed = install_files(&work_dir, &binary.install_prefix, &files) + .map_err(|e| format!("Failed to install files: {}", e))?; + all_installed_files.append(&mut installed); + } else if pkg_type == "sror" { + let files = extract_sror(&archive_path, &work_dir) + .map_err(|e| format!("Extraction failed: {}", e))?; + sp2.tick(&format!("Installing files for {}...", pkg_name)); + let mut installed = install_files(&work_dir, &binary.install_prefix, &files) .map_err(|e| format!("Failed to install files: {}", e))?; all_installed_files.append(&mut installed); } else { let files = extract_native(&archive_path, &work_dir) .map_err(|e| format!("Extraction failed: {}", e))?; - let file_list = if binary.files.is_empty() { files } else { binary.files.clone() }; sp2.tick(&format!("Installing files for {}...", pkg_name)); - let mut installed = install_files(&work_dir, &binary.install_prefix, &file_list) + let mut installed = install_files(&work_dir, &binary.install_prefix, &files) .map_err(|e| format!("Failed to install files: {}", e))?; all_installed_files.append(&mut installed); } @@ -514,7 +646,32 @@ pub fn extract_ror(archive_path: &Path, dict_path: &Path, target_dir: &Path) -> .map_err(|e| format!("Failed to execute tar for .ror: {}", e))?; if !status.success() { - return Err(format!("tar .ror extraction failed with exit code {:?}", status.code())); + return Err(format!("tar .ror failed with exit code {:?}", status.code())); + } + + let mut files = Vec::new(); + for entry in WalkDir::new(target_dir).into_iter().filter_map(|e| e.ok()) { + if entry.file_type().is_file() || entry.file_type().is_symlink() { + let rel_path = entry.path().strip_prefix(target_dir).unwrap(); + files.push(rel_path.to_string_lossy().to_string()); + } + } + Ok(files) +} + +pub fn extract_sror(archive_path: &Path, target_dir: &Path) -> Result<Vec<String>, String> { + let status = Command::new("tar") + .arg("-I") + .arg("zstd") + .arg("-xf") + .arg(archive_path) + .arg("-C") + .arg(target_dir) + .status() + .map_err(|e| format!("Failed to execute tar for .sror: {}", e))?; + + if !status.success() { + return Err(format!("tar .sror failed with exit code {:?}", status.code())); } let mut files = Vec::new(); @@ -624,3 +781,105 @@ pub fn load_package(pkg_name: &str) -> Option<Package> { let content = fs::read_to_string(pkg_path).ok()?; serde_yaml::from_str(&content).ok() } + +pub fn install_local_package(archive_path: &Path, cfg: &config::Config) { + match install_local_package_with_result(archive_path, cfg) { + Ok(installed_pkg) => { + let mut db = InstalledDB::load(); + db.add_package(installed_pkg); + if let Err(e) = db.save() { + eprintln!("{} Failed to save installed DB: {}", "[ror]".red().bold(), e); + } else { + println!("{} Package registered in database", "[ror]".green()); + } + println!("{} {} Package installed successfully.", "[ror]".blue().bold(), ">>>".green()); + } + Err(e) => { + eprintln!("{} {}", "[ror]".red().bold(), e); + } + } +} + +pub fn install_local_package_with_result(archive_path: &Path, _cfg: &config::Config) -> Result<InstalledPackage, String> { + if !archive_path.exists() { + return Err(format!("File not found: {}", archive_path.display())); + } + + let ext = archive_path.extension().and_then(|e| e.to_str()).unwrap_or(""); + let stem = archive_path.file_stem().and_then(|s| s.to_str()).unwrap_or("unknown"); + + let name = stem.to_string(); + let version = "local".to_string(); + + let work_dir = Path::new("/tmp/ror-local").join(&name); + let _ = fs::remove_dir_all(&work_dir); + fs::create_dir_all(&work_dir).map_err(|e| format!("Failed to create temp dir: {}", e))?; + + let mut spinner = Spinner::new(&format!("Installing local package {}", name)); + spinner.tick(&format!("Extracting {}...", name)); + + let installed_files = if ext == "sror" { + let files = extract_sror(archive_path, &work_dir) + .map_err(|e| format!("Extraction failed: {}", e))?; + let installed = install_files_to_root(&work_dir, Path::new("/"), &files) + .map_err(|e| format!("Failed to install files: {}", e))?; + installed + } else if ext == "ror" { + let dict_path = archive_path.with_extension("dict"); + if !dict_path.exists() { + return Err(format!( + "Dictionary file not found: {} (expected alongside .ror)", + dict_path.display() + )); + } + let files = extract_ror(archive_path, &dict_path, &work_dir) + .map_err(|e| format!("Extraction failed: {}", e))?; + let installed = install_files_to_root(&work_dir, Path::new("/"), &files) + .map_err(|e| format!("Failed to install files: {}", e))?; + installed + } else { + return Err(format!("Unsupported local package format: .{}", ext)); + }; + + spinner.finish(&format!("Installed {} files", installed_files.len())); + + let _ = fs::remove_dir_all(&work_dir); + + Ok(InstalledPackage { + name, + version, + libc: None, + files: installed_files, + installed_at: Local::now().to_rfc3339(), + }) +} + +fn install_files_to_root(source_dir: &Path, root: &Path, files: &[String]) -> Result<Vec<String>, String> { + let mut installed = Vec::new(); + for rel_path in files { + let src = source_dir.join(rel_path); + let dst = root.join(rel_path); + + let meta = fs::symlink_metadata(&src) + .map_err(|_| format!("File {} not found in package", rel_path))?; + + if let Some(parent) = dst.parent() { + fs::create_dir_all(parent) + .map_err(|e| format!("Failed to create dir {}: {}", parent.display(), e))?; + } + + if meta.is_symlink() { + let target = fs::read_link(&src) + .map_err(|e| format!("Failed to read link {}: {}", src.display(), e))?; + let _ = fs::remove_file(&dst); + symlink(&target, &dst) + .map_err(|e| format!("Failed to create symlink {}: {}", dst.display(), e))?; + } else { + let _ = fs::remove_file(&dst); + fs::copy(&src, &dst) + .map_err(|e| format!("Failed to copy {}: {}", src.display(), e))?; + } + installed.push(rel_path.clone()); + } + Ok(installed) +} |