From 8344340f779d6ee852687626ee6d081cbe7de827 Mon Sep 17 00:00:00 2001 From: Gabriele Musco Date: Sat, 3 Jun 2023 17:07:55 +0200 Subject: [PATCH] feat: dependency check module --- src/depcheck.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + 2 files changed, 64 insertions(+) create mode 100644 src/depcheck.rs diff --git a/src/depcheck.rs b/src/depcheck.rs new file mode 100644 index 0000000..f9e7bd8 --- /dev/null +++ b/src/depcheck.rs @@ -0,0 +1,63 @@ +use std::path::Path; + +pub enum DepType { + SharedObject(String), +} + +pub struct Dependency { + pub name: String, + pub dep_type: DepType, +} + +fn shared_obj_paths() -> Vec { + vec![ + "/lib".to_string(), + "/usr/lib".to_string(), + "/usr/local/lib".to_string(), + "/usr/lib/x86_64-linux-gnu".to_string(), + "/usr/lib/aarch64-linux-gnu".to_string(), + "/lib/x86_64-linux-gnu".to_string(), + "/lib/aarch64-linux-gnu".to_string(), + "/app/lib".to_string(), + ] +} + +pub fn check_dependency(dep: Dependency) -> bool { + match dep.dep_type { + DepType::SharedObject(filename) => { + for lib_path in shared_obj_paths() { + let path_s = &format!("{lp}/{fn}", lp=lib_path, fn=filename); + let path = Path::new(&path_s); + if path.is_file() { + return true; + } + } + return false; + } + } +} + +#[cfg(test)] +mod tests { + use super::{Dependency, DepType, check_dependency}; + + #[test] + fn can_find_libc() { + let libc_dep = Dependency { + name: "libc".into(), + dep_type: DepType::SharedObject("libc.so".into()), + }; + + assert!(check_dependency(libc_dep)); + } + + #[test] + fn cannot_find_fake_lib() { + let fake_dep = Dependency { + name: "fakedep".into(), + dep_type: DepType::SharedObject("fakedep.so".into()), + }; + + assert!(!check_dependency(fake_dep)); + } +} diff --git a/src/main.rs b/src/main.rs index 291d503..7134386 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ pub mod config; pub mod constants; pub mod file_utils; pub mod profiles; +pub mod depcheck; fn main() { MainWin::run(Settings::default());