Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SystemExt::distribution_id() #844

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/apple/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,10 @@ impl SystemExt for System {
}
}
}

fn distribution_id(&self) -> String {
std::env::consts::OS.to_owned()
}
}

impl Default for System {
Expand Down
4 changes: 4 additions & 0 deletions src/freebsd/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,10 @@ impl SystemExt for System {
fn os_version(&self) -> Option<String> {
self.system_info.get_os_release()
}

fn distribution_id(&self) -> String {
std::env::consts::OS.to_owned()
}
}

impl Default for System {
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,10 @@ mod test {

#[test]
fn check_system_info() {
let s = System::new();

// We don't want to test on unsupported systems.
if System::IS_SUPPORTED {
let s = System::new();
assert!(!s.name().expect("Failed to get system name").is_empty());

assert!(!s
Expand All @@ -345,6 +346,8 @@ mod test {
.expect("Failed to get long OS version")
.is_empty());
}

assert!(!s.distribution_id().is_empty());
}

#[test]
Expand Down
40 changes: 40 additions & 0 deletions src/linux/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,25 @@ impl SystemExt for System {
fn os_version(&self) -> Option<String> {
get_system_info_android(InfoType::OsVersion)
}

#[cfg(not(target_os = "android"))]
fn distribution_id(&self) -> String {
get_system_info_linux(
InfoType::DistributionID,
Path::new("/etc/os-release"),
Path::new(""),
)
.unwrap_or_else(|| std::env::consts::OS.to_owned())
}

#[cfg(target_os = "android")]
fn distribution_id(&self) -> String {
// Currently get_system_info_android doesn't support InfoType::DistributionID and always
// returns None. This call is done anyway for consistency with non-Android implementation
// and to suppress dead-code warning for DistributionID on Android.
get_system_info_android(InfoType::DistributionID)
.unwrap_or_else(|| std::env::consts::OS.to_owned())
}
}

impl Default for System {
Expand All @@ -722,6 +741,9 @@ enum InfoType {
/// - Linux: The distributions name
Name,
OsVersion,
/// Machine-parseable ID of a distribution, see
/// https://www.freedesktop.org/software/systemd/man/os-release.html#ID=
DistributionID,
}

#[cfg(not(target_os = "android"))]
Expand All @@ -732,6 +754,7 @@ fn get_system_info_linux(info: InfoType, path: &Path, fallback_path: &Path) -> O
let info_str = match info {
InfoType::Name => "NAME=",
InfoType::OsVersion => "VERSION_ID=",
InfoType::DistributionID => "ID=",
};

for line in reader.lines().flatten() {
Expand All @@ -750,6 +773,10 @@ fn get_system_info_linux(info: InfoType, path: &Path, fallback_path: &Path) -> O
let info_str = match info {
InfoType::OsVersion => "DISTRIB_RELEASE=",
InfoType::Name => "DISTRIB_ID=",
InfoType::DistributionID => {
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
// lsb-release is inconsistent with os-release and unsupported.
return None;
}
};
for line in reader.lines().flatten() {
if let Some(stripped) = line.strip_prefix(info_str) {
Expand All @@ -765,6 +792,10 @@ fn get_system_info_android(info: InfoType) -> Option<String> {
let name: &'static [u8] = match info {
InfoType::Name => b"ro.product.model\0",
InfoType::OsVersion => b"ro.build.version.release\0",
InfoType::DistributionID => {
// Not supported.
return None;
}
};

let mut value_buffer = vec![0u8; libc::PROP_VALUE_MAX as usize];
Expand Down Expand Up @@ -798,6 +829,7 @@ mod test {
fn lsb_release_fallback_android() {
assert!(get_system_info_android(InfoType::OsVersion).is_some());
assert!(get_system_info_android(InfoType::Name).is_some());
assert!(get_system_info_android(InfoType::DistributionID).is_none());
}

#[test]
Expand Down Expand Up @@ -844,6 +876,10 @@ DISTRIB_DESCRIPTION="Ubuntu 20.10"
get_system_info_linux(InfoType::Name, &tmp1, Path::new("")),
Some("Ubuntu".to_owned())
);
assert_eq!(
get_system_info_linux(InfoType::DistributionID, &tmp1, Path::new("")),
Some("ubuntu".to_owned())
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
);

// Check for the "fallback" path: "/etc/lsb-release"
assert_eq!(
Expand All @@ -854,5 +890,9 @@ DISTRIB_DESCRIPTION="Ubuntu 20.10"
get_system_info_linux(InfoType::Name, Path::new(""), &tmp2),
Some("Ubuntu".to_owned())
);
assert_eq!(
get_system_info_linux(InfoType::DistributionID, Path::new(""), &tmp2),
None
);
}
}
17 changes: 17 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,23 @@ pub trait SystemExt: Sized + Debug + Default + Send + Sync {
/// ```
fn long_os_version(&self) -> Option<String>;

/// Returns the distribution id as defined by os-release,
/// or [`std::env::consts::OS`].
///
/// See also
/// - https://www.freedesktop.org/software/systemd/man/os-release.html#ID=
/// - https://doc.rust-lang.org/std/env/consts/constant.OS.html
///
/// **Important**: this information is computed every time this function is called.
///
/// ```no_run
/// use sysinfo::{System, SystemExt};
///
/// let s = System::new();
/// println!("Distribution ID: {:?}", s.distribution_id());
/// ```
fn distribution_id(&self) -> String;

/// Returns the system hostname based off DNS
///
/// **Important**: this information is computed every time this function is called.
Expand Down
4 changes: 4 additions & 0 deletions src/unknown/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ impl SystemExt for System {
None
}

fn distribution_id(&self) -> String {
std::env::consts::OS.to_owned()
}

fn host_name(&self) -> Option<String> {
None
}
Expand Down
4 changes: 4 additions & 0 deletions src/windows/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ impl SystemExt for System {
};
Some(format!("{} ({})", major, build_number))
}

fn distribution_id(&self) -> String {
std::env::consts::OS.to_owned()
}
}

impl Default for System {
Expand Down