summaryrefslogtreecommitdiff
path: root/src/check.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/check.rs')
-rw-r--r--src/check.rs198
1 files changed, 198 insertions, 0 deletions
diff --git a/src/check.rs b/src/check.rs
new file mode 100644
index 0000000..a98afcb
--- /dev/null
+++ b/src/check.rs
@@ -0,0 +1,198 @@
+use colored::Colorize;
+use sha2::{Sha256, Digest};
+use std::collections::HashMap;
+use std::fs;
+use std::io::Write;
+use std::path::{Path};
+use tempfile::tempdir;
+
+use crate::config::Config;
+use crate::install::{
+ InstalledDB, load_package, select_binary_for_arch, BinaryPackage,
+ download_and_verify, extract_native, extract_ror, extract_sror, extract_deb,
+};
+
+fn compute_sha256(path: &Path) -> Result<String, std::io::Error> {
+ let mut file = fs::File::open(path)?;
+ let mut hasher = Sha256::new();
+ std::io::copy(&mut file, &mut hasher)?;
+ Ok(format!("{:x}", hasher.finalize()))
+}
+
+fn load_expected_hashes(
+ _pkg_name: &str,
+ binary: &BinaryPackage,
+ cfg: &Config,
+) -> Result<HashMap<String, String>, String> {
+ let urls = if !binary.url.is_empty() {
+ vec![binary.url.clone()]
+ } else {
+ binary
+ .mirrors
+ .iter()
+ .map(|m| m.replace("{filename}", &binary.filename))
+ .collect()
+ };
+
+ if urls.is_empty() {
+ return Err("No download URLs".to_string());
+ }
+
+ let archive_path = download_and_verify(&urls[0], &binary.sha256, cfg)?;
+
+ let temp_dir = tempdir().map_err(|e| e.to_string())?;
+ let work_dir = temp_dir.path();
+
+ let pkg_type = binary.pkg_type.as_deref().unwrap_or("tar.xz");
+ let files = if pkg_type == "ror" {
+ let dict_url = binary
+ .dict
+ .as_ref()
+ .ok_or_else(|| "Missing dict URL for .ror".to_string())?;
+ let dict_path = download_and_verify(dict_url, "", cfg)?;
+ let res = extract_ror(&archive_path, &dict_path, work_dir);
+ let _ = fs::remove_file(&dict_path);
+ res
+ } else if pkg_type == "sror" {
+ extract_sror(&archive_path, work_dir)
+ } else if pkg_type == "deb" {
+ extract_deb(&archive_path, work_dir)
+ } else {
+ extract_native(&archive_path, work_dir)
+ }
+ .map_err(|e| format!("Extraction failed: {}", e))?;
+
+ let mut hashes = HashMap::new();
+ for rel_path in files {
+ let full = work_dir.join(&rel_path);
+ if full.is_file() {
+ if let Ok(h) = compute_sha256(&full) {
+ hashes.insert(rel_path, h);
+ }
+ }
+ }
+
+ let _ = fs::remove_file(&archive_path);
+ Ok(hashes)
+}
+
+pub fn check_integrity(cfg: &Config) {
+ println!("{} Checking package integrity...", "[ror]".blue().bold());
+
+ let db = InstalledDB::load();
+ if db.packages.is_empty() {
+ println!("{} No packages installed.", "[ror]".yellow());
+ return;
+ }
+
+ let mut total_ok = 0;
+ let mut total_missing = 0;
+ let mut total_modified = 0;
+ let mut broken_packages = Vec::new();
+
+ for (name, installed) in &db.packages {
+ if installed.files.is_empty() {
+ continue;
+ }
+
+ print!(" {} ... ", name);
+ std::io::stdout().flush().ok();
+
+ let pkg = match load_package(name) {
+ Some(p) => p,
+ None => {
+ println!("{}", "SKIP (not in repo)".yellow());
+ continue;
+ }
+ };
+
+ let binary = match select_binary_for_arch(&pkg, &cfg.global.arch, &cfg.global.libc) {
+ Ok(b) => b,
+ Err(_) => {
+ println!("{}", "SKIP (no binary)".yellow());
+ continue;
+ }
+ };
+
+ let expected_hashes = match load_expected_hashes(name, binary, cfg) {
+ Ok(map) => map,
+ Err(e) => {
+ println!("{} ({})", "ERROR".red(), e);
+ broken_packages.push(name.clone());
+ continue;
+ }
+ };
+
+ let mut missing = Vec::new();
+ let mut modified = Vec::new();
+ let mut ok = 0;
+
+ for rel_path in &installed.files {
+ let full_path = Path::new("/").join(rel_path);
+ match fs::metadata(&full_path) {
+ Ok(meta) if meta.is_file() => {
+ match compute_sha256(&full_path) {
+ Ok(hash) => {
+ if let Some(expected) = expected_hashes.get(rel_path) {
+ if &hash == expected {
+ ok += 1;
+ } else {
+ modified.push(rel_path.clone());
+ }
+ }
+ }
+ Err(_) => {
+ modified.push(rel_path.clone());
+ }
+ }
+ }
+ Ok(_) => {} // не файл (симлинк, папка) — пока не проверяем
+ Err(_) => {
+ missing.push(rel_path.clone());
+ }
+ }
+ }
+
+ if missing.is_empty() && modified.is_empty() {
+ println!("{}", "OK".green());
+ } else {
+ println!(
+ "{} (missing: {}, modified: {})",
+ "WARN".yellow(),
+ missing.len(),
+ modified.len()
+ );
+ broken_packages.push(name.clone());
+ }
+
+ total_ok += ok;
+ total_missing += missing.len();
+ total_modified += modified.len();
+ }
+
+ println!(
+ "\n{} Summary: {} files OK, {} missing, {} modified",
+ "[ror]".blue().bold(),
+ total_ok,
+ total_missing,
+ total_modified
+ );
+
+ if !broken_packages.is_empty() {
+ println!(
+ "\n{} Broken packages: {}",
+ "[ror]".yellow().bold(),
+ broken_packages.join(", ")
+ );
+ print!("{} Reinstall them? [y/N]: ", "[ror]".blue().bold());
+ std::io::stdout().flush().ok();
+ let mut answer = String::new();
+ std::io::stdin().read_line(&mut answer).ok();
+ if answer.trim().eq_ignore_ascii_case("y") {
+ for pkg in broken_packages {
+ println!("{} Reinstalling {} ...", ">>>".cyan(), pkg);
+ crate::install::install_package(&pkg, cfg);
+ }
+ }
+ }
+}