wip: parsing cron

This commit is contained in:
Suyono 2023-11-23 17:23:53 +11:00
parent 4049e4a953
commit f40b8677ab
4 changed files with 114 additions and 20 deletions

46
Cargo.lock generated
View File

@ -17,6 +17,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@ -95,6 +104,12 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.150" version = "0.2.150"
@ -223,6 +238,35 @@ dependencies = [
"bitflags 1.3.2", "bitflags 1.3.2",
] ]
[[package]]
name = "regex"
version = "1.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.23" version = "0.1.23"
@ -396,7 +440,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
name = "wingmate-rs" name = "wingmate-rs"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"lazy_static",
"nix", "nix",
"regex",
"tokio", "tokio",
"tokio-util", "tokio-util",
] ]

View File

@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
lazy_static = "1.4.0"
nix = {version = "0.27.1", features = ["process", "signal", "fs"]} nix = {version = "0.27.1", features = ["process", "signal", "fs"]}
regex = "1.10.2"
tokio = { version = "1.34.0", features = ["full"] } tokio = { version = "1.34.0", features = ["full"] }
tokio-util = "0.7.10" tokio-util = "0.7.10"

View File

@ -1,9 +1,11 @@
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use std::io::{self, BufReader, BufRead}; use std::io::{BufReader, BufRead};
use std::error as std_error; use std::error as std_error;
use crate::init::error as wingmate_error; use crate::init::error as wingmate_error;
use nix::unistd::{access, AccessFlags}; use nix::unistd::{access, AccessFlags};
use lazy_static::lazy_static;
use regex::Regex;
#[derive(Debug)] #[derive(Debug)]
pub enum Command { pub enum Command {
@ -15,6 +17,7 @@ pub enum Command {
pub enum CronTimeFieldSpec { pub enum CronTimeFieldSpec {
Any, Any,
Exact(u8), Exact(u8),
MultiOccurrence(Vec<u8>),
Every(u8) Every(u8)
} }
@ -61,16 +64,8 @@ impl Config {
} }
} }
// let cron = buf.join("cron"); if let Ok(_crontab) = Self::read_crontab(&mut buf) {
// if let Ok(cron_iter) = fs::read_dir(cron.as_path()) { //TODO: fix me! empty branch
// for entry in cron_iter {
// if let Ok(_dirent) = entry {
// // read the cron file
// }
// }
// }
if let Ok(_crontab) = read_crontab(&mut buf) {
} }
} else { } else {
// reserve for future use; when we have a centralized config file // reserve for future use; when we have a centralized config file
@ -85,16 +80,56 @@ impl Config {
let config = Config { services: svc_commands }; let config = Config { services: svc_commands };
Ok(config) Ok(config)
} }
}
fn read_crontab(path: &mut PathBuf) -> Result<Vec<Crontab>, Box<dyn std_error::Error>> { fn read_crontab(path: &mut PathBuf) -> Result<Vec<Crontab>, Box<dyn std_error::Error>> {
let cron_path = path.join("crontab"); lazy_static! {
static ref CRON_REGEX: Regex = Regex::new(
r"^\s*(?P<minute>\S+)\s+(?P<hour>\S+)\s+(?P<dom>\S+)\s+(?P<month>\S+)\s+(?P<dow>\S+)\s+(?P<command>\S.*\S)\s*$"
).unwrap();
}
{ let cron_path = path.join("crontab");
let f = fs::File::open(cron_path.as_path())?;
let _line_iter = BufReader::new(f).lines(); {
let f = fs::File::open(cron_path.as_path())?;
for line in BufReader::new(f).lines() {
if let Ok(l) = line {
let cap = CRON_REGEX.captures(&l).ok_or::<Box<dyn std_error::Error>>(wingmate_error::CronSyntaxError(String::from(&l)).into())?;
let mut match_str = cap.name("minute").ok_or::<Box<dyn std_error::Error>>(wingmate_error::CronSyntaxError(String::from("cannot capture minute")).into())?;
let _minute = Self::to_cron_time_field_spec(&match_str)?;
match_str = cap.name("hour").ok_or::<Box<dyn std_error::Error>>(wingmate_error::CronSyntaxError(String::from("cannot capture hour")).into())?;
let _hour = Self::to_cron_time_field_spec(&match_str)?;
}
}
}
Err(wingmate_error::NoServiceOrCronFoundError.into())
} }
fn to_cron_time_field_spec(match_str: &regex::Match) -> Result<CronTimeFieldSpec, Box<dyn std_error::Error>> {
let field = match_str.as_str();
Err(wingmate_error::NoServiceOrCronFoundError.into()) if field == "*" {
return Ok(CronTimeFieldSpec::Any);
} else if field.starts_with("*/") {
let every = field[2..].parse::<u8>()?;
return Ok(CronTimeFieldSpec::Every(every));
} else if field.contains(",") {
let multi: Vec<&str> = field.split(",").collect();
let mut multi_occurrence: Vec<u8> = Vec::new();
for m in multi {
let ur = m.parse::<u8>()?;
multi_occurrence.push(ur);
}
return Ok(CronTimeFieldSpec::MultiOccurrence(multi_occurrence));
} else {
let n = field.parse::<u8>()?;
return Ok(CronTimeFieldSpec::Exact(n));
}
}
} }

View File

@ -23,3 +23,14 @@ impl fmt::Display for NoServiceOrCronFoundError {
} }
impl error::Error for NoServiceOrCronFoundError {} impl error::Error for NoServiceOrCronFoundError {}
#[derive(Debug)]
pub struct CronSyntaxError(pub String);
impl fmt::Display for CronSyntaxError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "cron syntax error at: {}", self.0)
}
}
impl error::Error for CronSyntaxError {}