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

View File

@ -1,5 +1,6 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use std::env; use std::env;
use std::path::Path;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use std::time::Instant; 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) { let md = match std::fs::metadata(&dir) {
Err(why) => { Err(why) => {
println!("Cannot stat directory '{}': {}", dir, why); println!("Cannot stat directory '{}': {}", dir.display(), why);
return false; return false;
}, },
Ok(val) => val, Ok(val) => val,
}; };
if !md.is_dir() { if !md.is_dir() {
println!("Output directory '{}' is not a directory", dir); println!("Output directory '{}' is not a directory", dir.display());
return false; return false;
} }
if md.permissions().readonly() { if md.permissions().readonly() {
println!("Output directory '{}' is read-only", dir); println!("Output directory '{}' is read-only", dir.display());
return false; return false;
} }
return true; return true;
@ -64,13 +65,13 @@ fn verify_directory(dir: &String) -> bool {
fn main() { fn main() {
let mut conf = Conf::get_default_conf(); 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(); let mut args = env::args();
while let Some(arg) = args.next() { while let Some(arg) = args.next() {
if arg.eq("-f") { if arg.eq("-f") {
match args.next() { 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);}, _ => {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 { mod tests {
use super::*; use super::*;
use std::str::FromStr;
use tempfile; use tempfile;
#[test] #[test]
fn verify_directory_exists_but_is_file() { fn verify_directory_exists_but_is_file() {
let mut tf = tempfile::NamedTempFile::new().unwrap(); let tf = tempfile::NamedTempFile::new().unwrap();
let dir = String::from_str(tf.path().to_str().unwrap()).unwrap(); assert!(!verify_directory(tf.path()));
assert!(!verify_directory(&dir));
} }
#[test] #[test]
fn verify_directory_does_not_exist() { 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] #[test]
fn verify_directory_exists() { fn verify_directory_exists() {
let mut tf = tempfile::tempdir().unwrap(); let tf = tempfile::tempdir().unwrap();
assert!(verify_directory(&String::from_str(tf.path().to_str().unwrap()).unwrap())); assert!(verify_directory(tf.path()));
} }
#[test] #[test]
fn verify_directory_exists_but_is_not_writable() { 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 md = std::fs::metadata(&tf.path()).unwrap();
let mut perms = md.permissions(); let mut perms = md.permissions();
perms.set_readonly(true); perms.set_readonly(true);
std::fs::set_permissions(&tf.path(), perms); std::fs::set_permissions(&tf.path(), perms).expect("Failed to set temp dir read-only");
let result = verify_directory(&String::from_str(tf.path().to_str().unwrap()).unwrap()); let result = verify_directory(tf.path());
perms = md.permissions(); perms = md.permissions();
perms.set_readonly(false); 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); assert!(!result);
} }
} }