diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-11-11 09:49:53 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-11-11 17:05:26 +0100 |
commit | 7af96e33717573c1acbbcd4146b65ef73ce33cc9 (patch) | |
tree | c136e7eee4b3504ba8398c7667a492047259bc55 | |
parent | b7699fac8d077cba2b30dffd311834ea2a4c8891 (diff) |
Add command: verify-sources
This adds a command to hash-check all sources.
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/cli.rs | 10 | ||||
-rw-r--r-- | src/commands/mod.rs | 3 | ||||
-rw-r--r-- | src/commands/verify_sources.rs | 42 | ||||
-rw-r--r-- | src/main.rs | 5 |
4 files changed, 60 insertions, 0 deletions
@@ -278,6 +278,16 @@ pub fn cli<'a>() -> App<'a> { .help("Do not use the fancy format, but simply <name> <version>") ) ) + .subcommand(App::new("verify-sources") + .about("Hash-check all source files") + .arg(Arg::with_name("package_name") + .required(false) + .multiple(false) + .index(1) + .value_name("PKG") + .help("Verify the sources of this package (optional, if left out, all packages are checked)") + ) + ) } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 7118157..6c8c0db 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -16,6 +16,9 @@ pub use dependencies_of::dependencies_of; mod what_depends; pub use what_depends::what_depends; +mod verify_sources; +pub use verify_sources::verify_sources; + mod versions_of; pub use versions_of::versions_of; diff --git a/src/commands/verify_sources.rs b/src/commands/verify_sources.rs new file mode 100644 index 0000000..3f7b9c8 --- /dev/null +++ b/src/commands/verify_sources.rs @@ -0,0 +1,42 @@ +use std::io::Write; +use std::path::PathBuf; + +use anyhow::Result; +use clap_v3::ArgMatches; + +use crate::config::*; +use crate::package::PackageName; +use crate::repository::Repository; +use crate::source::*; + +pub async fn verify_sources<'a>(matches: &ArgMatches, config: &Configuration<'a>, repo: Repository) -> Result<()> { + use tokio::stream::StreamExt; + + let source_cache_root = PathBuf::from(config.source_cache_root()); + let sc = SourceCache::new(source_cache_root); + let pname = matches.value_of("package_name").map(String::from).map(PackageName::from); + + repo.packages() + .filter(|p| pname.as_ref().map(|n| p.name() == n).unwrap_or(true)) + .map(|p| { + let source = sc.source_for(p); + async move { + let out = std::io::stdout(); + if source.exists() { + if source.verify_hash().await? { + writeln!(out.lock(), "Ok: {}", source.path().display())?; + } else { + writeln!(out.lock(), "Hash Mismatch: {}", source.path().display())?; + } + } else { + writeln!(out.lock(), "Source missing: {}", source.path().display())?; + } + + Ok(()) + } + }) + .collect::<futures::stream::FuturesUnordered<_>>() + .collect::<Result<()>>() + .await +} + diff --git a/src/main.rs b/src/main.rs index 9eadec5..cef9b43 100644 --- a/src/main.rs +++ b/src/main.rs @@ -98,6 +98,11 @@ async fn main() -> Result<()> { ("find-pkg", Some(matches)) => { let repo = load_repo()?; crate::commands::find_pkg(matches, &config, repo).await? + }, + + ("verify-sources", Some(matches)) => { + let repo = load_repo()?; + crate::commands::verify_sources(matches, &config, repo).await? } (other, _) => return Err(anyhow!("Unknown subcommand: {}", other)), |