]>
Commit | Line | Data |
---|---|---|
f486e9e5 | 1 | pub mod types; |
f3a96b2c | 2 | pub mod config; |
e05b637c DM |
3 | |
4 | mod parse_time; | |
5 | pub mod time; | |
da84cc52 DM |
6 | |
7 | use anyhow::{bail, Error}; | |
8 | ||
9 | /// Escape strings for usage in systemd unit names | |
10 | pub fn escape_unit(mut unit: &str, is_path: bool) -> String { | |
11 | ||
12 | if is_path { | |
13 | unit = unit.trim_matches('/'); | |
14 | if unit.is_empty() { | |
15 | return String::from("-"); | |
16 | } | |
17 | } | |
18 | ||
19 | let unit = unit.as_bytes(); | |
20 | ||
21 | let mut escaped = String::new(); | |
22 | ||
23 | for (i, c) in unit.iter().enumerate() { | |
24 | if *c == b'/' { | |
25 | escaped.push('-'); | |
26 | continue; | |
27 | } | |
28 | if (i == 0 && *c == b'.') || !((*c >= b'0' && *c <= b'9') || (*c >= b'A' && *c <= b'Z') || (*c >= b'a' && *c <= b'z')) { | |
29 | escaped.push_str(&format!("\\x{:0x}", c)); | |
30 | } else { | |
31 | escaped.push(*c as char); | |
32 | } | |
33 | } | |
34 | escaped | |
35 | } | |
36 | ||
37 | fn parse_hex_digit(d: u8) -> Result<u8, Error> { | |
38 | if d >= b'0' && d <= b'9' { return Ok(d - b'0'); } | |
39 | if d >= b'A' && d <= b'F' { return Ok(d - b'A' + 10); } | |
40 | if d >= b'a' && d <= b'f' { return Ok(d - b'a' + 10); } | |
41 | bail!("got invalid hex digit"); | |
42 | } | |
43 | ||
44 | /// Unescape strings used in systemd unit names | |
45 | pub fn unescape_unit(text: &str) -> Result<String, Error> { | |
46 | ||
47 | let mut i = text.as_bytes(); | |
48 | ||
49 | let mut data: Vec<u8> = Vec::new(); | |
50 | ||
51 | loop { | |
52 | if i.is_empty() { break; } | |
53 | let next = i[0]; | |
54 | if next == b'\\' { | |
55 | if i.len() < 4 { bail!("short input"); } | |
56 | if i[1] != b'x' { bail!("unkwnown escape sequence"); } | |
57 | let h1 = parse_hex_digit(i[2])?; | |
58 | let h0 = parse_hex_digit(i[3])?; | |
59 | data.push(h1<<4|h0); | |
60 | i = &i[4..] | |
61 | } else if next == b'-' { | |
62 | data.push(b'/'); | |
63 | i = &i[1..] | |
64 | } else { | |
65 | data.push(next); | |
66 | i = &i[1..] | |
67 | } | |
68 | } | |
69 | ||
70 | let text = String::from_utf8(data)?; | |
71 | ||
72 | Ok(text) | |
73 | } | |
669c137f DM |
74 | |
75 | pub fn reload_daemon() -> Result<(), Error> { | |
76 | ||
cbef49bf | 77 | let mut command = std::process::Command::new("systemctl"); |
669c137f DM |
78 | command.arg("daemon-reload"); |
79 | ||
80 | crate::tools::run_command(command, None)?; | |
81 | ||
82 | Ok(()) | |
83 | } | |
84 | ||
be614c62 HL |
85 | pub fn disable_unit(unit: &str) -> Result<(), Error> { |
86 | ||
87 | let mut command = std::process::Command::new("systemctl"); | |
88 | command.arg("disable"); | |
89 | command.arg(unit); | |
90 | ||
91 | crate::tools::run_command(command, None)?; | |
92 | ||
93 | Ok(()) | |
94 | } | |
95 | ||
669c137f DM |
96 | pub fn enable_unit(unit: &str) -> Result<(), Error> { |
97 | ||
cbef49bf | 98 | let mut command = std::process::Command::new("systemctl"); |
669c137f DM |
99 | command.arg("enable"); |
100 | command.arg(unit); | |
101 | ||
102 | crate::tools::run_command(command, None)?; | |
103 | ||
104 | Ok(()) | |
105 | } | |
106 | ||
107 | pub fn start_unit(unit: &str) -> Result<(), Error> { | |
108 | ||
cbef49bf | 109 | let mut command = std::process::Command::new("systemctl"); |
669c137f DM |
110 | command.arg("start"); |
111 | command.arg(unit); | |
112 | ||
113 | crate::tools::run_command(command, None)?; | |
114 | ||
115 | Ok(()) | |
116 | } | |
117 | ||
118 | pub fn stop_unit(unit: &str) -> Result<(), Error> { | |
119 | ||
cbef49bf | 120 | let mut command = std::process::Command::new("systemctl"); |
669c137f DM |
121 | command.arg("stop"); |
122 | command.arg(unit); | |
123 | ||
124 | crate::tools::run_command(command, None)?; | |
125 | ||
126 | Ok(()) | |
127 | } |