|
1 |
| -use anyhow::Result; |
2 | 1 | use goblin::{self, Object};
|
3 |
| -use std::collections::{HashMap, HashSet}; |
| 2 | +use std::collections::HashMap; |
4 | 3 | use std::fs;
|
5 |
| -use std::path::Path; |
| 4 | +use std::path::{Path, PathBuf}; |
6 | 5 |
|
7 |
| -pub fn run(paths: &[String]) -> Result<()> { |
8 |
| - let mut dependencies: HashMap<String, Vec<String>> = HashMap::new(); |
9 |
| - let mut present: HashSet<String> = HashSet::new(); |
| 6 | +pub fn run(paths: &[String]) { |
| 7 | + let blob_paths = find_blobs_in_paths(&paths); |
| 8 | + let blobs_to_dependencies = get_dependencies(&blob_paths); |
| 9 | + let missing_blobs = identify_missing(&blobs_to_dependencies); |
| 10 | + display_missing_blobs(&missing_blobs); |
| 11 | +} |
10 | 12 |
|
| 13 | +fn find_blobs_in_paths(paths: &[String]) -> Vec<PathBuf> { |
11 | 14 | let dirs = paths
|
12 | 15 | .iter()
|
13 | 16 | .map(Path::new)
|
14 | 17 | .filter(|path| path.is_dir())
|
15 | 18 | .collect::<Vec<_>>();
|
16 | 19 |
|
17 |
| - for dir in dirs.iter() { |
18 |
| - for entry in fs::read_dir(dir)? { |
19 |
| - let path = entry?.path(); |
| 20 | + let blob_paths: Vec<PathBuf> = dirs |
| 21 | + .iter() |
| 22 | + .map(|dir| fs::read_dir(dir).expect("Could not read directory.")) |
| 23 | + .flat_map(|read_dir| { |
| 24 | + read_dir.map(|dir_entry| dir_entry.expect("Could not read directory entry.").path()) |
| 25 | + }) |
| 26 | + .filter(|path| match path.extension() { |
| 27 | + // Assume that valid blobs have ".so" extension. |
| 28 | + Some(ext) => ext == "so", |
| 29 | + None => false, |
| 30 | + }) |
| 31 | + .collect(); |
20 | 32 |
|
21 |
| - let filename = path |
22 |
| - .file_name() |
23 |
| - .expect("Could not get file name.") |
24 |
| - .to_str() |
25 |
| - .expect("Could not convert to string.") |
26 |
| - .to_string(); |
| 33 | + blob_paths |
| 34 | +} |
27 | 35 |
|
28 |
| - present.insert(filename.clone()); |
| 36 | +fn get_dependencies(blob_paths: &Vec<PathBuf>) -> HashMap<String, Vec<String>> { |
| 37 | + let mut dependencies: HashMap<String, Vec<String>> = HashMap::new(); |
29 | 38 |
|
30 |
| - if let Some(ext) = path.extension() { |
31 |
| - if ext != "so" { |
32 |
| - continue; |
33 |
| - } |
34 |
| - } else { |
35 |
| - continue; |
36 |
| - } |
| 39 | + blob_paths.iter().for_each(|path| { |
| 40 | + let filename = path |
| 41 | + .file_name() |
| 42 | + .expect("Could not get file name.") |
| 43 | + .to_str() |
| 44 | + .expect("Could not convert to string.") |
| 45 | + .to_owned(); |
37 | 46 |
|
38 |
| - let buffer = fs::read(&path)?; |
39 |
| - let obj = goblin::Object::parse(&buffer); |
| 47 | + let buffer = fs::read(&path).expect("Could not read path."); |
| 48 | + let obj = goblin::Object::parse(&buffer); |
40 | 49 |
|
41 |
| - if let Ok(Object::Elf(elf)) = obj { |
42 |
| - for dep_str in elf.libraries { |
43 |
| - let dep = dep_str.to_string(); |
44 |
| - match dependencies.get_mut(&dep) { |
45 |
| - Some(dependants) => { |
46 |
| - dependants.push(filename.clone()); |
47 |
| - } |
48 |
| - None => { |
49 |
| - dependencies.insert(dep, vec![filename.clone()]); |
50 |
| - } |
51 |
| - } |
52 |
| - } |
53 |
| - } |
| 50 | + if let Ok(Object::Elf(elf)) = obj { |
| 51 | + let deps: Vec<String> = elf.libraries.iter().map(|dep| dep.to_string()).collect(); |
| 52 | + dependencies.insert(filename, deps); |
54 | 53 | }
|
55 |
| - } |
| 54 | + }); |
56 | 55 |
|
57 |
| - let mut missing: Vec<String> = vec![]; |
| 56 | + dependencies |
| 57 | +} |
| 58 | + |
| 59 | +fn identify_missing( |
| 60 | + blobs_to_dependencies: &HashMap<String, Vec<String>>, |
| 61 | +) -> HashMap<String, Vec<String>> { |
| 62 | + let mut dependencies_to_blobs: HashMap<String, Vec<String>> = HashMap::new(); |
| 63 | + blobs_to_dependencies.iter().for_each(|(blob, deps)| { |
| 64 | + deps.iter().for_each( |
| 65 | + |dependency| match dependencies_to_blobs.get_mut(dependency) { |
| 66 | + Some(dependants) => { |
| 67 | + dependants.push(blob.to_owned()); |
| 68 | + } |
| 69 | + None => { |
| 70 | + dependencies_to_blobs.insert(dependency.to_owned(), vec![blob.to_owned()]); |
| 71 | + } |
| 72 | + }, |
| 73 | + ) |
| 74 | + }); |
58 | 75 |
|
59 |
| - for dep in dependencies.keys() { |
60 |
| - if !present.contains(dep) { |
61 |
| - missing.push(dep.to_string()); |
| 76 | + let mut missing_blobs: HashMap<String, Vec<String>> = HashMap::new(); |
| 77 | + |
| 78 | + for dep in dependencies_to_blobs.keys() { |
| 79 | + if !blobs_to_dependencies.contains_key(dep) { |
| 80 | + // Dependency is not present. |
| 81 | + let missing_dep = dep.to_owned(); |
| 82 | + let blobs_requiring_missing_dep = dependencies_to_blobs[dep].to_owned(); |
| 83 | + missing_blobs.insert(missing_dep, blobs_requiring_missing_dep); |
62 | 84 | }
|
63 | 85 | }
|
64 | 86 |
|
65 |
| - for m in missing { |
66 |
| - println!("{} required by: {}", m, dependencies[&m].join("; ")); |
67 |
| - } |
| 87 | + missing_blobs |
| 88 | +} |
68 | 89 |
|
69 |
| - Ok(()) |
| 90 | +fn display_missing_blobs(missing_blobs: &HashMap<String, Vec<String>>) { |
| 91 | + for blob in missing_blobs.keys() { |
| 92 | + println!("{} required by: {}", blob, missing_blobs[blob].join("; ")); |
| 93 | + } |
70 | 94 | }
|
0 commit comments