Skip to content

Commit 7acff58

Browse files
committed
Split into separate functions and remove anyhow dependency
1 parent 826c666 commit 7acff58

File tree

4 files changed

+75
-62
lines changed

4 files changed

+75
-62
lines changed

Cargo.lock

Lines changed: 1 addition & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "aosp-missing-blobs"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
authors = ["Josh Choo <dev@joshuous.com>"]
55
edition = "2018"
66
description = """
@@ -13,5 +13,4 @@ homepage = "https://github.com/joshuous/aosp-missing-blobs"
1313
repository = "https://github.com/joshuous/aosp-missing-blobs"
1414

1515
[dependencies]
16-
anyhow = "1.0.40"
1716
goblin = "0.4"

src/lib.rs

Lines changed: 71 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,94 @@
1-
use anyhow::Result;
21
use goblin::{self, Object};
3-
use std::collections::{HashMap, HashSet};
2+
use std::collections::HashMap;
43
use std::fs;
5-
use std::path::Path;
4+
use std::path::{Path, PathBuf};
65

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+
}
1012

13+
fn find_blobs_in_paths(paths: &[String]) -> Vec<PathBuf> {
1114
let dirs = paths
1215
.iter()
1316
.map(Path::new)
1417
.filter(|path| path.is_dir())
1518
.collect::<Vec<_>>();
1619

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();
2032

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+
}
2735

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();
2938

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();
3746

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);
4049

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);
5453
}
55-
}
54+
});
5655

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+
});
5875

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);
6284
}
6385
}
6486

65-
for m in missing {
66-
println!("{} required by: {}", m, dependencies[&m].join("; "));
67-
}
87+
missing_blobs
88+
}
6889

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+
}
7094
}

src/main.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
use std::{env, process};
1+
use std::env;
22

33
use aosp_missing_blobs::run;
44

55
fn main() {
66
let args: Vec<String> = env::args().collect();
77
let paths = &args[1..];
88

9-
if let Err(e) = run(&paths) {
10-
println!("An error occurred: {}", e);
11-
process::exit(1)
12-
}
9+
run(paths);
1310
}

0 commit comments

Comments
 (0)