Convert paths from String to PathBuf

This commit is contained in:
Kienan Stewart 2022-09-24 11:36:08 -04:00
parent 222d4c6c18
commit 52cb639511
2 changed files with 36 additions and 38 deletions

View File

@ -1,10 +1,12 @@
use std::io::Read;
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration;
pub struct Conf {
pub job_dir: String,
pub output_dir: String,
pub job_dir: PathBuf,
pub output_dir: PathBuf,
pub check_interval: Duration,
pub driver_url: String,
}
@ -12,16 +14,15 @@ pub struct Conf {
impl Conf {
pub fn get_default_conf() -> Conf {
return Conf {
job_dir: String::from_str("jobs.d").unwrap(),
output_dir: String::from_str("results.d").unwrap(),
job_dir: PathBuf::from_str("jobs.d").unwrap(),
output_dir: PathBuf::from_str("results.d").unwrap(),
check_interval: Duration::new(15*60, 0),
driver_url: String::from_str("http://localhost:4444").unwrap(),
};
}
pub fn update_from_file(&mut self, f: &str) {
let path = std::path::Path::new(f);
let mut file = match std::fs::File::open(&path) {
pub fn update_from_file(&mut self, path: &Path) {
let mut file = match std::fs::File::open(path) {
Err(why) => {
println!("Could not open file '{}': {}", path.display(), why); return;
},
@ -53,12 +54,14 @@ impl Conf {
match key {
"job_dir" => {
println!("{} changed from '{}' to '{}' by line in '{}'", key, self.job_dir, value, path.display());
self.job_dir = String::from_str(value).unwrap();
println!("{} changed from '{}' to '{}' by line in '{}'",
key, self.job_dir.display(), value, path.display());
self.job_dir = PathBuf::from_str(value).unwrap();
},
"output_dir" => {
println!("{} changed from '{}' to '{}' by line in '{}'", key, self.output_dir, value, path.display());
self.output_dir = String::from_str(value).unwrap();
println!("{} changed from '{}' to '{}' by line in '{}'",
key, self.output_dir.display(), value, path.display());
self.output_dir = PathBuf::from_str(value).unwrap();
},
"check_interval" => {
let converted_value = match value.parse::<u64>() {
@ -86,20 +89,19 @@ mod tests {
use std::io::Write;
use std::str::FromStr;
use tempfile::NamedTempFile;
use std::time::Duration;
use super::*;
fn write_conf_to_temp_file(content: &str) -> NamedTempFile {
let mut f = NamedTempFile::new().unwrap();
f.write_all(content.as_bytes());
f.write_all(content.as_bytes()).expect("Failed to write configuration to file");
return f;
}
#[test]
fn test_not_existing() {
let mut conf = Conf::get_default_conf();
conf.update_from_file("/not/a/real/file");
conf.update_from_file(Path::new("/not/a/real/file"));
}
#[test]
@ -111,10 +113,9 @@ output_dir=/var/lib/output
check_interval=3600
driver_url = https://example.test:6666
"#);
let f = tf.path().to_str().unwrap();
conf.update_from_file(f);
assert!(String::from_str("/val=ue").unwrap().eq(&conf.job_dir));
assert!(String::from_str("/var/lib/output").unwrap().eq(&conf.output_dir));
conf.update_from_file(tf.path());
assert!(Path::new("/val=ue").eq(&conf.job_dir));
assert!(Path::new("/var/lib/output").eq(&conf.output_dir));
assert!(String::from_str("https://example.test:6666").unwrap().eq(&conf.driver_url));
assert_eq!(3600, conf.check_interval.as_secs());
}
@ -125,8 +126,7 @@ driver_url = https://example.test:6666
let tf = write_conf_to_temp_file(r#"
check_interval=d3600
"#);
let f = tf.path().to_str().unwrap();
conf.update_from_file(f);
conf.update_from_file(tf.path());
assert_eq!(15*60, conf.check_interval.as_secs());
}

View File

@ -1,5 +1,6 @@
use std::cmp::Ordering;
use std::env;
use std::path::Path;
use std::thread;
use std::time::Duration;
use std::time::Instant;
@ -43,20 +44,20 @@ impl ThreadJob<'_> {
}
}
fn verify_directory(dir: &String) -> bool {
fn verify_directory(dir: &Path) -> bool {
let md = match std::fs::metadata(&dir) {
Err(why) => {
println!("Cannot stat directory '{}': {}", dir, why);
println!("Cannot stat directory '{}': {}", dir.display(), why);
return false;
},
Ok(val) => val,
};
if !md.is_dir() {
println!("Output directory '{}' is not a directory", dir);
println!("Output directory '{}' is not a directory", dir.display());
return false;
}
if md.permissions().readonly() {
println!("Output directory '{}' is read-only", dir);
println!("Output directory '{}' is read-only", dir.display());
return false;
}
return true;
@ -64,13 +65,13 @@ fn verify_directory(dir: &String) -> bool {
fn main() {
let mut conf = Conf::get_default_conf();
conf.update_from_file("/etc/haunter/haunter.conf");
conf.update_from_file(Path::new("/etc/haunter/haunter.conf"));
let mut args = env::args();
while let Some(arg) = args.next() {
if arg.eq("-f") {
match args.next() {
Some(file) => conf.update_from_file(file.as_str()),
Some(file) => conf.update_from_file(Path::new(&file)),
_ => {println!("Missing argument after '-f'"); std::process::exit(1);},
};
}
@ -189,39 +190,36 @@ fn get_source(driver: &str, url: &str) -> Result<String, &'static str> {
mod tests {
use super::*;
use std::str::FromStr;
use tempfile;
#[test]
fn verify_directory_exists_but_is_file() {
let mut tf = tempfile::NamedTempFile::new().unwrap();
let dir = String::from_str(tf.path().to_str().unwrap()).unwrap();
assert!(!verify_directory(&dir));
let tf = tempfile::NamedTempFile::new().unwrap();
assert!(!verify_directory(tf.path()));
}
#[test]
fn verify_directory_does_not_exist() {
assert!(!verify_directory(&String::from_str("/fake/path/that/does/not/exist").unwrap()));
assert!(!verify_directory(Path::new("/fake/path/that/does/not/exist")));
}
#[test]
fn verify_directory_exists() {
let mut tf = tempfile::tempdir().unwrap();
assert!(verify_directory(&String::from_str(tf.path().to_str().unwrap()).unwrap()));
let tf = tempfile::tempdir().unwrap();
assert!(verify_directory(tf.path()));
}
#[test]
fn verify_directory_exists_but_is_not_writable() {
let mut tf = tempfile::tempdir().unwrap();
let tf = tempfile::tempdir().unwrap();
let md = std::fs::metadata(&tf.path()).unwrap();
let mut perms = md.permissions();
perms.set_readonly(true);
std::fs::set_permissions(&tf.path(), perms);
let result = verify_directory(&String::from_str(tf.path().to_str().unwrap()).unwrap());
std::fs::set_permissions(&tf.path(), perms).expect("Failed to set temp dir read-only");
let result = verify_directory(tf.path());
perms = md.permissions();
perms.set_readonly(false);
std::fs::set_permissions(&tf.path(), perms);
std::fs::set_permissions(&tf.path(), perms).expect("Failed to set temp dir writable");
assert!(!result);
}
}