Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
sha1 = "0.10.6"
tar = { git = "https://github.com/jamcleod/tar-rs" }
tempfile = "3.19.0"
tempfile = "3.27.0"
thiserror = "2.0.12"
wait-timeout = "0.2.1"
walkdir = "2.5.0"
Expand Down
38 changes: 27 additions & 11 deletions src/analysis/find_linux_filesystems.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cmp::Reverse;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

Expand All @@ -14,7 +15,7 @@ const MIN_REQUIRED: usize = (KEY_DIRS.len() + CRITICAL_FILES.len()) / 2;

#[derive(Debug, Clone)]
pub struct PrimaryFilesystem {
pub path: PathBuf,
pub paths: BTreeMap<PathBuf,PathBuf>,
pub size: u64,
pub num_files: usize,
pub key_file_count: usize,
Expand All @@ -25,34 +26,41 @@ pub fn find_linux_filesystems(
start_dir: &Path,
min_executables: Option<usize>,
extractor_name: &str,
internal_paths: Option<&BTreeMap<PathBuf,String>>,
) -> Vec<PrimaryFilesystem> {
let mut filesystems = Vec::new();
let min_executables = min_executables.unwrap_or(DEFAULT_MIN_EXECUTABLES);

log::info!("Searching {start_dir:?}");

let mut paths: BTreeMap<PathBuf,PathBuf> = BTreeMap::new();
for entry in WalkDir::new(start_dir)
.max_depth(MAX_EXPLORE_DEPTH)
.into_iter()
.filter_entry(|entry| entry.file_type().is_dir())
{
let Ok(entry) = entry else { continue };

//log::trace!("looking at {:?}", entry.path());

let mut total_matches = 0;
let root = entry.path();

for dir in KEY_DIRS {
if root.join(dir).exists() {
//log::trace!("{dir} found in {root:?}");
//log::debug!("{dir} found in {root:?}");
total_matches += 1;
}
}

if let Some(internals) = internal_paths{
if let Some(result ) = internals.iter().find(|(_,v)| {entry.path().to_str().unwrap_or_default().contains(*v)}){
if ! paths.contains_key(result.0) {
paths.insert(result.0.clone(), entry.path().to_path_buf());
} else {
continue; //don't double add
}
}
}
for file in CRITICAL_FILES {
if root.join(file).exists() {
//log::trace!("{file} found in {root:?}");
//log::debug!("{file} found in {root:?}");
total_matches += 1;
}
}
Expand All @@ -66,9 +74,10 @@ pub fn find_linux_filesystems(

if total_executables >= min_executables {
log::info!("{root:?}: {total_executables}, {total_size}, {total_files}");

let mut paths: BTreeMap<PathBuf,PathBuf> = BTreeMap::new();
paths.insert(PathBuf::from("/"),root.to_owned());
filesystems.push(PrimaryFilesystem {
path: root.to_owned(),
paths: paths,
size: total_size,
num_files: total_files,
key_file_count: total_matches,
Expand All @@ -81,9 +90,16 @@ pub fn find_linux_filesystems(
log::info!("Directory {} had {total_matches}", root.display());
}
}

filesystems = filesystems.iter().map(|file_system| -> PrimaryFilesystem{
let root = file_system.paths.get(&PathBuf::from("/")).unwrap();
let mut new_filesystem = file_system.clone();
let mut new_paths = paths.clone();
new_paths.insert(PathBuf::from("/"), root.to_owned());
new_filesystem.paths = new_paths;
new_filesystem
}).collect();
filesystems.sort_by_key(|fs| Reverse((fs.executables, fs.size, fs.key_file_count)));

//log::debug!("filesystems: {:?}", filesystems);
filesystems
}

Expand Down
23 changes: 14 additions & 9 deletions src/analysis/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashSet;
use std::collections::{BTreeMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::time::Instant;
Expand Down Expand Up @@ -51,17 +51,18 @@ pub fn extract_and_process(
results: &Mutex<Vec<ExtractionResult>>,
metadata: &Metadata,
removed_devices: Option<&Mutex<HashSet<PathBuf>>>,
external_paths: Option<&BTreeMap<PathBuf, PathBuf>>,
internal_paths: Option<&BTreeMap<PathBuf,String>>,
) -> Result<(), ExtractProcessError> {
let extractor_name = extractor.name();

let scratch_dir = scratch_dir
let origin_dir = scratch_dir
.map(Path::to_path_buf)
.unwrap_or_else(env::temp_dir);

let temp_dir_prefix = format!("fw2tar_{extractor_name}");
let temp_dir = TempDir::with_prefix_in(temp_dir_prefix, &scratch_dir)
let mut temp_dir = TempDir::with_prefix_in(temp_dir_prefix, &origin_dir)
.map_err(ExtractProcessError::TempDirFail)?;

temp_dir.disable_cleanup(scratch_dir.is_some());
let extract_dir = temp_dir.path();

let log_file = {
Expand All @@ -84,7 +85,7 @@ pub fn extract_and_process(
log::info!("{extractor_name} took {elapsed:.2} seconds");
}

let rootfs_choices = find_linux_filesystems(extract_dir, None, extractor_name);
let rootfs_choices = find_linux_filesystems(extract_dir, None, extractor_name, internal_paths);

if rootfs_choices.is_empty() {
log::error!("No Linux filesystems found extracting {in_file:?} with {extractor_name}");
Expand All @@ -105,9 +106,13 @@ pub fn extract_and_process(
let file_name = out_file_base.file_name().unwrap().to_string_lossy();
out_file_base.with_file_name(format!("{}.{extractor_name}.{i}.tar.gz", file_name))
};

let combined_paths = if let Some(external) = external_paths {
fs.paths.iter().chain(external.iter()).map(|(k,v)| (k.clone(),v.clone())).collect()
}else {
fs.paths.clone()
};
// XXX: improve error handling here
let file_node_count = tar_fs(&fs.path, &tar_path, metadata, removed_devices).unwrap();
let file_node_count = tar_fs(&combined_paths, &tar_path, metadata, removed_devices, ).unwrap();
let archive_hash = sha1_file(&tar_path).unwrap();

results.lock().unwrap().push(ExtractionResult {
Expand All @@ -123,7 +128,7 @@ pub fn extract_and_process(
}

drop(temp_dir);

Ok(())
}

Expand Down
Loading
Loading