diff options
Diffstat (limited to 'src/install.rs')
| -rw-r--r-- | src/install.rs | 170 |
1 files changed, 107 insertions, 63 deletions
diff --git a/src/install.rs b/src/install.rs index 8a66118..a0cf8f2 100644 --- a/src/install.rs +++ b/src/install.rs @@ -65,8 +65,6 @@ 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, } @@ -76,7 +74,7 @@ pub fn parse_dep(dep: &str) -> (String, Option<VersionConstraint>) { 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 }; + let c = VersionConstraint { op: op.clone(), version: ver }; return (name, Some(c)); } } @@ -463,64 +461,38 @@ pub fn parse_package_spec(s: &str) -> (String, Option<String>) { (s.to_string(), None) } -fn find_case_insensitive_dir(parent: &Path, target: &str) -> Option<PathBuf> { - let exact = parent.join(target); - if exact.exists() && exact.is_dir() { - return Some(exact); - } - let target_lower = target.to_lowercase(); - for entry in fs::read_dir(parent).ok()? { - let entry = entry.ok()?; - if entry.file_type().ok()?.is_dir() && entry.file_name().to_string_lossy().to_lowercase() == target_lower { - return Some(entry.path()); - } - } - None -} - -pub fn find_package_file(pkg_name: &str) -> Option<PathBuf> { - let (pkg_name, _) = parse_package_spec(pkg_name); - debug!("Looking for package {} in repository root", pkg_name); - 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; - } - if let Some(pkg_dir) = find_case_insensitive_dir(&cat_path, &pkg_name) { - let dir_name = pkg_dir.file_name()?.to_string_lossy().to_string(); - let pkg_path = pkg_dir.join(format!("{}.yaml", dir_name)); - if pkg_path.exists() && pkg_path.is_file() { - debug!("Found package at {:?}", pkg_path); - return Some(pkg_path); - } - } - } - None +pub fn find_package_file(pkg_name: &str) -> Option<PathBuf> { + find_package_with_repo(pkg_name).map(|(p, _)| p) } pub fn find_package_with_repo(pkg_name: &str) -> Option<(PathBuf, String)> { let (pkg_name, _) = parse_package_spec(pkg_name); debug!("Searching for package {} with repository info", pkg_name); let repo = Path::new(REPO_ROOT); - let read_dir = fs::read_dir(repo).ok()?; + let Ok(read_dir) = fs::read_dir(repo) else { + eprintln!("[ror-debug] Cannot read REPO_ROOT: {:?}", repo); + return None; + }; for entry in read_dir.flatten() { - let cat_path = entry.path(); - if !cat_path.is_dir() { + let repo_dir = entry.path(); + if !repo_dir.is_dir() || repo_dir.file_name().unwrap_or_default() == "groups" { continue; } - - if let Some(pkg_dir) = find_case_insensitive_dir(&cat_path, &pkg_name) { - let dir_name = pkg_dir.file_name()?.to_string_lossy().to_string(); - let pkg_path = pkg_dir.join(format!("{}.yaml", dir_name)); - if pkg_path.exists() && pkg_path.is_file() { - let repo_name = cat_path.file_name()?.to_string_lossy().to_string(); - debug!("Found package in repo {}: {:?}", repo_name, pkg_path); - return Some((pkg_path, repo_name)); + let repo_name = repo_dir.file_name()?.to_string_lossy().to_string(); + + for walk_entry in WalkDir::new(&repo_dir).min_depth(1).max_depth(5).into_iter().flatten() { + if walk_entry.file_type().is_dir() { + let dir_path = walk_entry.path(); + if dir_path.file_name()?.to_string_lossy().eq_ignore_ascii_case(&pkg_name) { + let dir_name = dir_path.file_name()?.to_string_lossy().to_string(); + let pkg_path = dir_path.join(format!("{}.yaml", dir_name)); + if pkg_path.exists() && pkg_path.is_file() { + return Some((pkg_path, repo_name)); + } + } } } } @@ -528,6 +500,7 @@ pub fn find_package_with_repo(pkg_name: &str) -> Option<(PathBuf, String)> { } + pub fn resolve_package_name(pkg_name: &str) -> Option<String> { let path = find_package_file(pkg_name)?; let content = fs::read_to_string(path).ok()?; @@ -584,7 +557,6 @@ pub fn resolve_filename(pkg: &Package, binary: &BinaryPackage) -> String { format!("{}-{}-pkg-{}-{}-rorkos.{}", pkg.name, pkg.version, binary.arch, libc, ext) } -#[allow(dead_code)] pub fn generate_dict_url(filename: &str, mirrors: &[String], cfg: &config::Config) -> Result<String, String> { let dict_filename = { let stem = if let Some(s) = filename.strip_suffix(".ror") { @@ -684,7 +656,7 @@ pub fn install_services(pkg: &Package) { "[ror]".yellow().bold(), pkg.name ); } - Some(init) if init != &service.init => { + Some(init) if !service.init.is_empty() && service.init != "all" && init != &service.init => { eprintln!( "{} Package '{}' has a service for '{}', but your init is '{}' — not supported", "[ror]".yellow().bold(), pkg.name, service.init, init @@ -731,7 +703,7 @@ pub fn install_services_to_root(pkg: &Package, root_dir: &Path) { "[ror]".yellow().bold(), pkg.name ); } - Some(init) if init != &service.init => { + Some(init) if !service.init.is_empty() && service.init != "all" && init != &service.init => { eprintln!( "{} Package '{}' has a service for '{}', but rootfs init is '{}' — not supported", "[ror]".yellow().bold(), pkg.name, service.init, init @@ -749,10 +721,16 @@ pub fn install_services_to_root(pkg: &Package, root_dir: &Path) { } } match std::fs::write(&full_path, &service.file) { - Ok(_) => println!( - "{} Created service for '{}': {}", - "[ror]".green().bold(), pkg.name, service.filepath - ), + Ok(_) => { + println!( + "{} Created service for '{}': {}", + "[ror]".green().bold(), pkg.name, service.filepath + ); + if service.file.starts_with("#!") { + use std::os::unix::fs::PermissionsExt; + let _ = std::fs::set_permissions(&full_path, std::fs::Permissions::from_mode(0o755)); + } + } Err(e) => eprintln!( "{} Failed to write service file '{}': {}", "[ror]".red().bold(), service.filepath, e @@ -946,9 +924,20 @@ pub fn install_package_with_result(pkg_name: &str, cfg: &config::Config) -> Resu let mut files = extract_deb(&archive_path, Path::new("/"))?; all_installed_files.append(&mut files); } else if pkg_type == "ror" { + if let Ok(dict_url) = generate_dict_url(&effective_filename, &binary.mirrors, cfg) { + if let Ok(dict_path) = download_and_verify(&dict_url, "", cfg) { + let target_dict = archive_path.with_extension("dict"); + let _ = fs::copy(&dict_path, &target_dict); + let _ = fs::remove_file(&dict_path); + } + } sp2.tick(&format!("Extracting {}...", pkg_name)); let files = extract_ror(&archive_path, &work_dir) .map_err(|e| format!("Extraction failed: {}", e))?; + let target_dict = archive_path.with_extension("dict"); + if target_dict.exists() { + let _ = fs::remove_file(&target_dict); + } 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))?; @@ -1178,9 +1167,34 @@ fn install_files(source_dir: &Path, prefix: &str, files: &[String]) -> Result<Ve let target = fs::read_link(&src) .map_err(|e| format!("Failed to read link {}: {}", src.display(), e))?; validate_symlink_target(&target, &dst, prefix_path)?; - let _ = fs::remove_file(&dst); - symlink(&target, &dst) - .map_err(|e| format!("Failed to create symlink {}: {}", dst.display(), e))?; + + let target_in_root = if target.is_absolute() { + prefix_path.join(target.strip_prefix("/").unwrap_or(&target)) + } else { + dst.parent().unwrap_or(prefix_path).join(&target) + }; + + let is_self_or_canonical = if let Some(parent) = dst.parent() { + if let (Ok(parent_canonical), Ok(target_canonical)) = (fs::canonicalize(parent), fs::canonicalize(&target_in_root)) { + if let Some(file_name) = dst.file_name() { + parent_canonical.join(file_name) == target_canonical + } else { + false + } + } else { + false + } + } else { + false + }; + + if is_self_or_canonical { + debug!("Skipping symlink {:?} -> {:?} because target already exists as canonical file", dst, target); + } else { + 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) @@ -1210,9 +1224,33 @@ pub fn install_files_to_root(source_dir: &Path, root: &Path, files: &[String]) - let target = fs::read_link(&src) .map_err(|e| format!("Failed to read link {}: {}", src.display(), e))?; validate_symlink_target(&target, &dst, root)?; - let _ = fs::remove_file(&dst); - symlink(&target, &dst) - .map_err(|e| format!("Failed to create symlink {}: {}", dst.display(), e))?; + let target_in_root = if target.is_absolute() { + root.join(target.strip_prefix("/").unwrap_or(&target)) + } else { + dst.parent().unwrap_or(root).join(&target) + }; + + let is_self_or_canonical = if let Some(parent) = dst.parent() { + if let (Ok(parent_canonical), Ok(target_canonical)) = (fs::canonicalize(parent), fs::canonicalize(&target_in_root)) { + if let Some(file_name) = dst.file_name() { + parent_canonical.join(file_name) == target_canonical + } else { + false + } + } else { + false + } + } else { + false + }; + + if is_self_or_canonical { + debug!("Skipping symlink {:?} -> {:?} because target already exists as canonical file", dst, target); + } else { + 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) @@ -1263,7 +1301,13 @@ pub fn load_package(pkg_name: &str) -> Option<Package> { debug!("Loading full package metadata for {}", pkg_name); let pkg_path = find_package_file(&pkg_name)?; let content = fs::read_to_string(pkg_path).ok()?; - serde_yaml::from_str(&content).ok() + match serde_yaml::from_str(&content) { + Ok(pkg) => Some(pkg), + Err(e) => { + eprintln!("[ror-debug] Failed to parse YAML for {}: {}", pkg_name, e); + None + } + } } fn check_oss_policy(pkg: &Package) -> Result<(), String> { |