2020-05-12 11:07:49 +00:00
|
|
|
pub mod types;
|
2020-05-16 04:32:28 +00:00
|
|
|
pub mod config;
|
2020-05-16 04:53:15 +00:00
|
|
|
|
|
|
|
mod parse_time;
|
|
|
|
pub mod time;
|
2020-06-09 09:52:06 +00:00
|
|
|
|
|
|
|
use anyhow::{bail, Error};
|
|
|
|
|
|
|
|
/// Escape strings for usage in systemd unit names
|
|
|
|
pub fn escape_unit(mut unit: &str, is_path: bool) -> String {
|
|
|
|
|
|
|
|
if is_path {
|
|
|
|
unit = unit.trim_matches('/');
|
|
|
|
if unit.is_empty() {
|
|
|
|
return String::from("-");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let unit = unit.as_bytes();
|
|
|
|
|
|
|
|
let mut escaped = String::new();
|
|
|
|
|
|
|
|
for (i, c) in unit.iter().enumerate() {
|
|
|
|
if *c == b'/' {
|
|
|
|
escaped.push('-');
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-21 08:36:25 +00:00
|
|
|
if (i == 0 && *c == b'.')
|
|
|
|
|| !(*c == b'_' ||
|
|
|
|
*c == b'.' ||
|
|
|
|
(*c >= b'0' && *c <= b'9') ||
|
|
|
|
(*c >= b'A' && *c <= b'Z') ||
|
|
|
|
(*c >= b'a' && *c <= b'z')) {
|
2020-06-09 09:52:06 +00:00
|
|
|
escaped.push_str(&format!("\\x{:0x}", c));
|
|
|
|
} else {
|
|
|
|
escaped.push(*c as char);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
escaped
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_hex_digit(d: u8) -> Result<u8, Error> {
|
|
|
|
if d >= b'0' && d <= b'9' { return Ok(d - b'0'); }
|
|
|
|
if d >= b'A' && d <= b'F' { return Ok(d - b'A' + 10); }
|
|
|
|
if d >= b'a' && d <= b'f' { return Ok(d - b'a' + 10); }
|
|
|
|
bail!("got invalid hex digit");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unescape strings used in systemd unit names
|
|
|
|
pub fn unescape_unit(text: &str) -> Result<String, Error> {
|
|
|
|
|
|
|
|
let mut i = text.as_bytes();
|
|
|
|
|
|
|
|
let mut data: Vec<u8> = Vec::new();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
if i.is_empty() { break; }
|
|
|
|
let next = i[0];
|
|
|
|
if next == b'\\' {
|
|
|
|
if i.len() < 4 { bail!("short input"); }
|
|
|
|
if i[1] != b'x' { bail!("unkwnown escape sequence"); }
|
|
|
|
let h1 = parse_hex_digit(i[2])?;
|
|
|
|
let h0 = parse_hex_digit(i[3])?;
|
|
|
|
data.push(h1<<4|h0);
|
|
|
|
i = &i[4..]
|
|
|
|
} else if next == b'-' {
|
|
|
|
data.push(b'/');
|
|
|
|
i = &i[1..]
|
|
|
|
} else {
|
|
|
|
data.push(next);
|
|
|
|
i = &i[1..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let text = String::from_utf8(data)?;
|
|
|
|
|
|
|
|
Ok(text)
|
|
|
|
}
|
2020-06-10 06:54:41 +00:00
|
|
|
|
|
|
|
pub fn reload_daemon() -> Result<(), Error> {
|
|
|
|
|
2020-06-15 09:16:11 +00:00
|
|
|
let mut command = std::process::Command::new("systemctl");
|
2020-06-10 06:54:41 +00:00
|
|
|
command.arg("daemon-reload");
|
|
|
|
|
|
|
|
crate::tools::run_command(command, None)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-08-13 10:58:52 +00:00
|
|
|
pub fn disable_unit(unit: &str) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut command = std::process::Command::new("systemctl");
|
|
|
|
command.arg("disable");
|
|
|
|
command.arg(unit);
|
|
|
|
|
|
|
|
crate::tools::run_command(command, None)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-06-10 06:54:41 +00:00
|
|
|
pub fn enable_unit(unit: &str) -> Result<(), Error> {
|
|
|
|
|
2020-06-15 09:16:11 +00:00
|
|
|
let mut command = std::process::Command::new("systemctl");
|
2020-06-10 06:54:41 +00:00
|
|
|
command.arg("enable");
|
|
|
|
command.arg(unit);
|
|
|
|
|
|
|
|
crate::tools::run_command(command, None)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn start_unit(unit: &str) -> Result<(), Error> {
|
|
|
|
|
2020-06-15 09:16:11 +00:00
|
|
|
let mut command = std::process::Command::new("systemctl");
|
2020-06-10 06:54:41 +00:00
|
|
|
command.arg("start");
|
|
|
|
command.arg(unit);
|
|
|
|
|
|
|
|
crate::tools::run_command(command, None)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stop_unit(unit: &str) -> Result<(), Error> {
|
|
|
|
|
2020-06-15 09:16:11 +00:00
|
|
|
let mut command = std::process::Command::new("systemctl");
|
2020-06-10 06:54:41 +00:00
|
|
|
command.arg("stop");
|
|
|
|
command.arg(unit);
|
|
|
|
|
|
|
|
crate::tools::run_command(command, None)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|