Skip to content

Commit

Permalink
Merge pull request #19 from mrl5/define-pkg-dir
Browse files Browse the repository at this point in the history
feat(cli): allow defining pkg dir for scan [#20]
  • Loading branch information
mrl5 authored Feb 18, 2022
2 parents 21ad016 + 057eab8 commit 339fd54
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 12 deletions.
9 changes: 8 additions & 1 deletion crates/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ pub async fn execute(cmd: Command) -> Result<(), Box<dyn Error>> {
cpe_feed,
} => cpe::execute(get_input(packages_batch)?, cpe_feed.feed_dir).await,
Command::Cve { cpe_batch, summary } => cve::execute(get_input(cpe_batch)?, summary).await,
Command::Scan { cpe_feed, out_dir } => scan::execute(cpe_feed.feed_dir, out_dir).await,
Command::Scan {
cpe_feed,
out_dir,
pkg_dir,
} => scan::execute(cpe_feed.feed_dir, out_dir, pkg_dir).await,
}
}

Expand Down Expand Up @@ -58,6 +62,9 @@ pub enum Command {

#[structopt(short = "o", long = "out-dir", env = "VULNER_OUT_DIR")]
out_dir: PathBuf,

#[structopt(short = "p", long = "pkg-dir", env = "VULNER_PKG_DIR")]
pkg_dir: Option<PathBuf>,
},
}

Expand Down
8 changes: 6 additions & 2 deletions crates/cli/src/command/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};

pub async fn execute(feed_dir: PathBuf, out_dir: PathBuf) -> Result<(), Box<dyn Error>> {
pub async fn execute(
feed_dir: PathBuf,
out_dir: PathBuf,
pkg_dir: Option<PathBuf>,
) -> Result<(), Box<dyn Error>> {
// todo: progress bar
log::debug!("getting os adapter ...");
let os = get_adapter()?;
let os = get_adapter(pkg_dir)?;

let now = Utc::now();
let [date, time] = [
Expand Down
11 changes: 9 additions & 2 deletions crates/os-adapter/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::collections::HashMap;
use std::env;
use std::error::Error;
use std::io;
use std::path::PathBuf;
use std::str::FromStr;
use std::vec::Vec;
mod linux_funtoo;
Expand All @@ -24,16 +25,22 @@ pub trait OsInfo {

pub trait OsPackages {
fn get_all_catpkgs(&self) -> Result<HashMap<String, Vec<Package>>, Box<dyn Error>>;
fn set_pkg_dir(&mut self, pkg_dir: PathBuf);
}

pub trait OsAdapter: OsInfo + OsPackages {}

pub fn get_adapter() -> Result<Box<dyn OsAdapter>, Box<dyn Error>> {
pub fn get_adapter(pkg_dir: Option<PathBuf>) -> Result<Box<dyn OsAdapter>, Box<dyn Error>> {
let os = Os::from_str(env::consts::OS)?;

if os == Os::GnuLinux {
let id = LinuxDistro::from_str(&get_distro_id()?)?;
Ok(get_linux_adapter(id))
let mut adapter = get_linux_adapter(id);

if let Some(pkg_dir) = pkg_dir {
adapter.set_pkg_dir(pkg_dir);
}
Ok(adapter)
} else {
// placeholder
Err(Box::new(io::Error::from(io::ErrorKind::Unsupported)))
Expand Down
10 changes: 8 additions & 2 deletions crates/os-adapter/src/adapter/linux_funtoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
*/

use super::portage::Portage;
use std::path::Path;
use std::path::{Path, PathBuf};

pub struct Funtoo {
os: super::Os,
flavor: super::LinuxDistro,
pkg_dir: PathBuf,
}

impl Funtoo {
pub fn new() -> Self {
Self {
os: super::Os::GnuLinux,
flavor: super::LinuxDistro::Funtoo,
pkg_dir: Path::new("/var/db/pkg/").to_path_buf(),
}
}
}
Expand All @@ -36,6 +38,10 @@ impl super::OsInfo for Funtoo {

impl Portage for Funtoo {
fn get_pkg_dir(&self) -> &Path {
Path::new("/var/db/pkg/")
&self.pkg_dir
}

fn set_pkg_dir(&mut self, pkg_dir: PathBuf) {
self.pkg_dir = pkg_dir;
}
}
10 changes: 8 additions & 2 deletions crates/os-adapter/src/adapter/linux_gentoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
*/

use super::portage::Portage;
use std::path::Path;
use std::path::{Path, PathBuf};

pub struct Gentoo {
os: super::Os,
flavor: super::LinuxDistro,
pkg_dir: PathBuf,
}

impl Gentoo {
pub fn new() -> Self {
Self {
os: super::Os::GnuLinux,
flavor: super::LinuxDistro::Gentoo,
pkg_dir: Path::new("/var/db/pkg/").to_path_buf(),
}
}
}
Expand All @@ -36,6 +38,10 @@ impl super::OsInfo for Gentoo {

impl Portage for Gentoo {
fn get_pkg_dir(&self) -> &Path {
Path::new("/var/db/pkg/")
&self.pkg_dir
}

fn set_pkg_dir(&mut self, pkg_dir: PathBuf) {
self.pkg_dir = pkg_dir;
}
}
16 changes: 13 additions & 3 deletions crates/os-adapter/src/adapter/portage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,34 @@ use cpe_tag::package::{convert_to_pkg, Package};
use std::collections::HashMap;
use std::error::Error;
use std::fs::read_dir;
use std::path::Path;
use std::path::{Path, PathBuf};

pub trait Portage {
fn get_pkg_dir(&self) -> &Path;
fn set_pkg_dir(&mut self, pkg_dir: PathBuf);
}

// https://riptutorial.com/rust/example/22917/inheritance-with-traits
impl<T> super::OsPackages for T
where
T: Portage,
{
// https://riptutorial.com/rust/example/22917/inheritance-with-traits
fn set_pkg_dir(&mut self, pkg_dir: PathBuf) {
self.set_pkg_dir(pkg_dir)
}

fn get_all_catpkgs(&self) -> Result<HashMap<String, Vec<Package>>, Box<dyn Error>> {
let pkg_prefix_adapter: HashMap<&str, String> =
HashMap::from([("dev-libs", "lib".to_owned())]);
let skipped_dirs = vec!["virtual"];
let mut all_catpkgs = HashMap::new();

log::info!("walking {:?} ...", &self.get_pkg_dir().as_os_str());
if !&self.get_pkg_dir().exists() {
log::error!("{:?} doesn't exist", &self.get_pkg_dir().as_os_str());
} else {
log::info!("walking {:?} ...", &self.get_pkg_dir().as_os_str());
}

for category in read_dir(&self.get_pkg_dir())? {
let category = category?;
let path = &category.path();
Expand Down

0 comments on commit 339fd54

Please sign in to comment.