From 2213856c81580fbd63dbfa43fad6f236866b038f Mon Sep 17 00:00:00 2001 From: Matteo Settenvini Date: Sun, 26 Jan 2025 15:07:49 +0100 Subject: [PATCH] chore(dso): minor refactoring --- src/cleaners/dso.rs | 81 ++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 42 deletions(-) diff --git a/src/cleaners/dso.rs b/src/cleaners/dso.rs index 49ff7e8..7288663 100644 --- a/src/cleaners/dso.rs +++ b/src/cleaners/dso.rs @@ -60,6 +60,7 @@ impl Cleaner for DsoCleaner { } } + // DEBUGGING stuff you can uncomment for a quick and dirty graph: // println!( // "{:?}", // petgraph::dot::Dot::with_config(&state.graph, &[petgraph::dot::Config::EdgeNoLabel]) @@ -151,19 +152,7 @@ impl DsoCleaner { dst_path.display() ); - state - .paths_map - .entry(src.st_ino) - .or_default() - .insert(path.into()); - - state - .paths_map - .entry(dst.st_ino) - .or_default() - .insert(dst_path); - - state.graph.add_edge(src.st_ino, dst.st_ino, ()); + Self::update_graph(state, path.into(), src.st_ino, dst_path, dst.st_ino); Ok(()) } @@ -192,22 +181,7 @@ impl DsoCleaner { search_paths.append(&mut rpaths); } - let ld_config_path = std::env::var("LD_LIBRARY_PATH"); - let mut env_paths = ld_config_path - .as_ref() - .map(|env| { - env.split(':') - .filter_map(|dir| { - if dir.is_empty() { - None - } else { - Some(dir.to_string()) - } - }) - .collect::>() - }) - .unwrap_or_default(); - search_paths.append(&mut env_paths); + search_paths.append(&mut Self::get_env_library_paths()); } if elf.runpaths != vec![""] { @@ -249,19 +223,7 @@ impl DsoCleaner { continue; // These are not the droids you are looking for. } - state - .paths_map - .entry(src_inode) - .or_default() - .insert(path.into()); - - state - .paths_map - .entry(dst.st_ino) - .or_default() - .insert(tentative_path); - - state.graph.add_edge(src_inode, dst.st_ino, ()); + Self::update_graph(state, path.into(), src_inode, tentative_path, dst.st_ino); continue 'next_lib; } @@ -270,4 +232,39 @@ impl DsoCleaner { Ok(()) } + + fn get_env_library_paths() -> Vec { + let ld_config_path = std::env::var("LD_LIBRARY_PATH"); + ld_config_path + .as_ref() + .map(|env| { + env.split(':') + .filter(|s| s.is_empty()) + .map(|s| s.into()) + .collect() + }) + .unwrap_or_default() + } + + fn update_graph( + state: &mut State, + src_path: PathBuf, + src_inode: ino_t, + dst_path: PathBuf, + dst_inode: ino_t, + ) { + state + .paths_map + .entry(src_inode) + .or_default() + .insert(src_path); + + state + .paths_map + .entry(dst_inode) + .or_default() + .insert(dst_path); + + state.graph.add_edge(src_inode, dst_inode, ()); + } }