Skip to content

Commit

Permalink
fix: systemd rc version parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
desbma-s1n committed Dec 1, 2023
1 parent e549755 commit 5c8ec20
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions src/systemd/version.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! Systemd & kernel version
use std::fmt;
use std::io::BufRead;
use std::process::Command;
use std::str;

#[derive(Ord, PartialOrd, Eq, PartialEq)]
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
pub struct SystemdVersion {
pub major: u16,
pub minor: u16,
Expand All @@ -20,20 +21,40 @@ impl SystemdVersion {
if !output.status.success() {
anyhow::bail!("systemctl invocation failed with code {:?}", output.status);
}
let (major, rest) = str::from_utf8(&output.stdout)?
let line = output
.stdout
.lines()
.next()
.ok_or_else(|| anyhow::anyhow!("Unable to get systemd version"))??;
Self::parse_version_line(&line)
}

fn parse_version_line(s: &str) -> anyhow::Result<Self> {
let version = s
.split_once('(')
.ok_or_else(|| anyhow::anyhow!("Unable to get systemd major version"))?
.ok_or_else(|| anyhow::anyhow!("Unable to parse systemd version"))?
.1
.split_once('.')
.ok_or_else(|| anyhow::anyhow!("Unable to get systemd minor version"))?;
let minor = rest
.split_once(')')
.ok_or_else(|| anyhow::anyhow!("Unable to parse systemd version"))?
.0;
let major_str = version
.chars()
.take_while(|c| c.is_ascii_digit())
.collect::<String>();
Ok(Self {
major: major.parse()?,
minor: minor.parse()?,
})
let major = major_str.parse()?;
let minor = if let Some('.') = version.chars().nth(major_str.len()) {
// Actual minor version
version
.chars()
.skip(major_str.len() + 1)
.take_while(|c| c.is_ascii_digit())
.collect::<String>()
.parse()?
} else {
// RC or distro suffix
0
};
Ok(Self { major, minor })
}
}

Expand Down Expand Up @@ -90,3 +111,20 @@ impl fmt::Display for KernelVersion {
write!(f, "{}.{}.{}", self.major, self.minor, self.release)
}
}

#[cfg(test)]
mod tests {
use crate::systemd::SystemdVersion;

#[test]
fn test_parse_version() {
assert_eq!(
SystemdVersion::parse_version_line("systemd 254 (254.1)").unwrap(),
SystemdVersion::new(254, 1)
);
assert_eq!(
SystemdVersion::parse_version_line("systemd 255 (255~rc3-2)").unwrap(),
SystemdVersion::new(255, 0)
);
}
}

0 comments on commit 5c8ec20

Please sign in to comment.