7 use anyhow
::{bail, Error}
;
9 /// Escape strings for usage in systemd unit names
10 pub fn escape_unit(mut unit
: &str, is_path
: bool
) -> String
{
13 unit
= unit
.trim_matches('
/'
);
15 return String
::from("-");
19 let unit
= unit
.as_bytes();
21 let mut escaped
= String
::new();
23 for (i
, c
) in unit
.iter().enumerate() {
28 if (i
== 0 && *c
== b'
.'
)
31 (*c
>= b'
0'
&& *c
<= b'
9'
) ||
32 (*c
>= b'A'
&& *c
<= b'Z'
) ||
33 (*c
>= b'a'
&& *c
<= b'z'
)) {
34 escaped
.push_str(&format
!("\\x{:0x}", c
));
36 escaped
.push(*c
as char);
42 fn parse_hex_digit(d
: u8) -> Result
<u8, Error
> {
43 if d
>= b'
0'
&& d
<= b'
9' { return Ok(d - b'0'); }
44 if d
>= b'A'
&& d
<= b'F' { return Ok(d - b'A' + 10); }
45 if d
>= b'a'
&& d
<= b'f' { return Ok(d - b'a' + 10); }
46 bail
!("got invalid hex digit");
49 /// Unescape strings used in systemd unit names
50 pub fn unescape_unit(text
: &str) -> Result
<String
, Error
> {
52 let mut i
= text
.as_bytes();
54 let mut data
: Vec
<u8> = Vec
::new();
57 if i
.is_empty() { break; }
60 if i
.len() < 4 { bail!("short input"); }
61 if i
[1] != b'x' { bail!("unkwnown escape sequence"); }
62 let h1
= parse_hex_digit(i
[2])?
;
63 let h0
= parse_hex_digit(i
[3])?
;
66 } else if next
== b'
-'
{
75 let text
= String
::from_utf8(data
)?
;
80 pub fn reload_daemon() -> Result
<(), Error
> {
82 let mut command
= std
::process
::Command
::new("systemctl");
83 command
.arg("daemon-reload");
85 crate::tools
::run_command(command
, None
)?
;
90 pub fn disable_unit(unit
: &str) -> Result
<(), Error
> {
92 let mut command
= std
::process
::Command
::new("systemctl");
93 command
.arg("disable");
96 crate::tools
::run_command(command
, None
)?
;
101 pub fn enable_unit(unit
: &str) -> Result
<(), Error
> {
103 let mut command
= std
::process
::Command
::new("systemctl");
104 command
.arg("enable");
107 crate::tools
::run_command(command
, None
)?
;
112 pub fn start_unit(unit
: &str) -> Result
<(), Error
> {
114 let mut command
= std
::process
::Command
::new("systemctl");
115 command
.arg("start");
118 crate::tools
::run_command(command
, None
)?
;
123 pub fn stop_unit(unit
: &str) -> Result
<(), Error
> {
125 let mut command
= std
::process
::Command
::new("systemctl");
129 crate::tools
::run_command(command
, None
)?
;