Census: report directory traversal failures
Fail when a requested scan root cannot be opened. Report traversal errors below a readable root alongside the other skipped inputs. (cherry picked from commit b1bdd8994a456755255b1e096f641ef47ff719c9)
This commit is contained in:
1 parent
bbd06c317d
commit
7bc655a5c3
2 files changed
+49
-8
No files matched your search
@@ -4,7 +4,7 @@ use std::collections::{BTreeMap, BTreeSet};
|
|||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::{Context, Result};
|
||||||
use victory_hle::{HleResolver, HostPolicy};
|
use victory_hle::{HleResolver, HostPolicy};
|
||||||
use victory_loader::Provenance;
|
use victory_loader::Provenance;
|
||||||
use victory_pe::{ImageInfo, ImportSymbol};
|
use victory_pe::{ImageInfo, ImportSymbol};
|
||||||
@@ -32,10 +32,17 @@ pub fn run(paths: Vec<PathBuf>, strict: bool, list_missing: bool) -> Result<()>
|
|||||||
let mut missing: BTreeMap<(String, String), BTreeSet<PathBuf>> = BTreeMap::new();
|
let mut missing: BTreeMap<(String, String), BTreeSet<PathBuf>> = BTreeMap::new();
|
||||||
|
|
||||||
for root in &paths {
|
for root in &paths {
|
||||||
for entry in walkdir::WalkDir::new(root)
|
for entry in walkdir::WalkDir::new(root) {
|
||||||
.into_iter()
|
let entry = match entry {
|
||||||
.filter_map(Result::ok)
|
Ok(entry) => entry,
|
||||||
{
|
Err(error) if error.depth() == 0 => {
|
||||||
|
return Err(error).context("cannot scan census input");
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
failed.push(error.to_string());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
if !entry.file_type().is_file() || !looks_like_pe(path) {
|
if !entry.file_type().is_file() || !looks_like_pe(path) {
|
||||||
continue;
|
continue;
|
||||||
@@ -46,7 +53,7 @@ pub fn run(paths: Vec<PathBuf>, strict: bool, list_missing: bool) -> Result<()>
|
|||||||
{
|
{
|
||||||
Ok(info) => info,
|
Ok(info) => info,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
failed.push((path.to_path_buf(), e.to_string()));
|
failed.push(format!("{}: {e}", path.display()));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -78,8 +85,8 @@ pub fn run(paths: Vec<PathBuf>, strict: bool, list_missing: bool) -> Result<()>
|
|||||||
}
|
}
|
||||||
|
|
||||||
println!("{images} images scanned, {} unreadable", failed.len());
|
println!("{images} images scanned, {} unreadable", failed.len());
|
||||||
for (p, e) in &failed {
|
for error in &failed {
|
||||||
println!(" skipped {}: {e}", p.display());
|
println!(" skipped {error}");
|
||||||
}
|
}
|
||||||
println!(
|
println!(
|
||||||
"\n{:<56} {:>6} {:>6} {:>6} {:>8} {:>7}",
|
"\n{:<56} {:>6} {:>6} {:>6} {:>8} {:>7}",
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn census_fails_when_a_requested_path_is_missing() {
|
||||||
|
let valid = Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
|
||||||
|
let missing = valid.with_extension("missing-census-input");
|
||||||
|
assert!(!missing.exists());
|
||||||
|
|
||||||
|
let output = Command::new(env!("CARGO_BIN_EXE_victory"))
|
||||||
|
.arg("census")
|
||||||
|
.args([&valid, &missing])
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||||
|
assert!(
|
||||||
|
!output.status.success(),
|
||||||
|
"census succeeded: {}",
|
||||||
|
String::from_utf8_lossy(&output.stdout)
|
||||||
|
);
|
||||||
|
assert!(stderr.contains("Cargo.missing-census-input"), "{stderr}");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn census_accepts_a_valid_path_with_no_pe_images() {
|
||||||
|
let valid = Path::new(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml");
|
||||||
|
let output = Command::new(env!("CARGO_BIN_EXE_victory"))
|
||||||
|
.arg("census")
|
||||||
|
.arg(valid)
|
||||||
|
.output()
|
||||||
|
.unwrap();
|
||||||
|
assert!(output.status.success(), "{output:?}");
|
||||||
|
assert!(String::from_utf8_lossy(&output.stdout).contains("0 images scanned, 0 unreadable"));
|
||||||
|
}
|
||||||
Reference in new issue
Block a user