Skip to content

Commit

Permalink
Enable recursive directory traversal in casc
Browse files Browse the repository at this point in the history
This enables recursively discovering all policy files in the specified
path(s), which is expected to be the most common use case.

Additional ergonomics, like defaulting to CWD if no path is specified is
future work.
  • Loading branch information
dburgener committed May 27, 2022
1 parent e03b2dc commit 442937f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -19,3 +19,4 @@ lalrpop-util = "0.19"
regex = "1"
sexp = "1.1"
thiserror = "1.0"
walkdir = "2"
34 changes: 32 additions & 2 deletions src/bin/casc.rs
Expand Up @@ -6,6 +6,7 @@ use selinux_cascade::error::ErrorItem;
use clap::Parser;
use std::fs::File;
use std::io::{Error, ErrorKind, Write};
use walkdir::WalkDir;

#[derive(Parser, Debug)]
#[clap(author, version, name = "casc")]
Expand All @@ -16,9 +17,9 @@ struct Args {

fn main() -> std::io::Result<()> {
let args = Args::parse();
let policies: Vec<&str> = args.input_file.iter().map(|s| s as &str).collect();
let policies: Vec<String> = get_policy_files(args.input_file);
let mut out_file = File::create("out.cil")?;
let res = compile_system_policy(policies);
let res = compile_system_policy(policies.iter().map(|s| s as &str).collect());
match res {
Err(error_list) => {
for e in error_list {
Expand All @@ -35,3 +36,32 @@ fn main() -> std::io::Result<()> {
Ok(s) => out_file.write_all(s.as_bytes()),
}
}

// Create a list of policy files
// Display info and proceed on errors. As long as we have at least one file, errors should be
// considered non-fatal
fn get_policy_files(filenames: Vec<String>) -> Vec<String> {
let mut policy_files = Vec::new();
for file in filenames {
for entry in WalkDir::new(file) {
match entry {
Ok(entry) => {
if entry.file_type().is_file()
&& entry.path().extension().unwrap_or_default() == "cas"
{
let filename = entry.path().display().to_string();
policy_files.push(filename);
}
}
Err(e) => {
if let Some(path) = e.path() {
eprintln!("{}: {}", path.display(), e);
} else {
eprintln!("{}", e);
}
}
}
}
}
policy_files
}

0 comments on commit 442937f

Please sign in to comment.