Skip to content

Commit 0ad09ad

Browse files
committed
Implement recursive search
1 parent 67be228 commit 0ad09ad

File tree

2 files changed

+63
-13
lines changed

2 files changed

+63
-13
lines changed

src/lib.rs

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,78 @@
11
use goblin::{self, Object};
2+
use ignore::WalkBuilder;
23
use std::collections::HashMap;
34
use std::fs;
45
use std::path::{Path, PathBuf};
56

6-
pub fn run(paths: &[&str]) {
7-
let blob_paths = find_blobs_in_paths(&paths);
7+
pub struct Config {
8+
pub recursive: bool,
9+
}
10+
11+
pub fn run(paths: &[&str], config: Config) {
12+
let file_paths: Vec<PathBuf> = if config.recursive {
13+
find_files_recursively(&paths)
14+
} else {
15+
find_files(&paths)
16+
};
17+
18+
let blob_paths: Vec<&PathBuf> = file_paths
19+
.iter()
20+
.filter(|path| match path.extension() {
21+
// Assume that valid blobs have ".so" extension.
22+
Some(ext) => ext == "so",
23+
None => false,
24+
})
25+
.collect();
26+
827
let blobs_to_dependencies = get_dependencies(&blob_paths);
928
let missing_blobs = identify_missing(&blobs_to_dependencies);
1029
display_missing_blobs(&missing_blobs);
1130
}
1231

13-
fn find_blobs_in_paths(paths: &[&str]) -> Vec<PathBuf> {
32+
fn find_files(paths: &[&str]) -> Vec<PathBuf> {
1433
let dirs = paths
1534
.iter()
1635
.map(Path::new)
1736
.filter(|path| path.is_dir())
1837
.collect::<Vec<_>>();
1938

20-
let blob_paths: Vec<PathBuf> = dirs
39+
let file_paths: Vec<PathBuf> = dirs
2140
.iter()
2241
.map(|dir| fs::read_dir(dir).expect("Could not read directory."))
2342
.flat_map(|read_dir| {
2443
read_dir.map(|dir_entry| dir_entry.expect("Could not read directory entry.").path())
2544
})
26-
.filter(|path| match path.extension() {
27-
// Assume that valid blobs have ".so" extension.
28-
Some(ext) => ext == "so",
29-
None => false,
45+
.collect();
46+
47+
file_paths
48+
}
49+
50+
fn find_files_recursively(paths: &[&str]) -> Vec<PathBuf> {
51+
let mut walker = WalkBuilder::new(paths[0]);
52+
for path in &paths[1..] {
53+
walker.add(path);
54+
}
55+
56+
// Don't read from ignore configs
57+
walker
58+
.ignore(false)
59+
.git_ignore(false)
60+
.git_exclude(false)
61+
.git_global(false);
62+
63+
let file_paths = walker
64+
.build()
65+
.map(|dir_entry| {
66+
dir_entry
67+
.expect("Could not read directory entry.")
68+
.into_path()
3069
})
3170
.collect();
3271

33-
blob_paths
72+
file_paths
3473
}
3574

36-
fn get_dependencies(blob_paths: &Vec<PathBuf>) -> HashMap<String, Vec<String>> {
75+
fn get_dependencies(blob_paths: &Vec<&PathBuf>) -> HashMap<String, Vec<String>> {
3776
let mut dependencies: HashMap<String, Vec<String>> = HashMap::new();
3877

3978
blob_paths.iter().for_each(|path| {

src/main.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use aosp_missing_blobs::run;
1+
use aosp_missing_blobs::{run, Config};
22
use clap::{App, Arg};
33

44
fn main() {
55
let matches = App::new("aosp-missing-blobs")
6-
.version("0.3.0")
6+
.version("0.4.0")
77
.author("Josh Choo <dev@joshuous.com>")
88
.about("An AOSP tool to generate a list of required missing blobs.")
99
.arg(
@@ -12,9 +12,20 @@ fn main() {
1212
.required(true)
1313
.multiple(true),
1414
)
15+
.arg(
16+
Arg::with_name("recursive")
17+
.short("r")
18+
.long("recursive")
19+
.help("Read blobs in each path recursively"),
20+
)
1521
.get_matches();
1622

1723
let paths = matches.values_of("PATHS").unwrap().collect::<Vec<_>>();
1824

19-
run(&paths);
25+
run(
26+
&paths,
27+
Config {
28+
recursive: matches.is_present("recursive"),
29+
},
30+
);
2031
}

0 commit comments

Comments
 (0)