From ddd4dc0b29b9429b4c6a022ecbdca1e3a83f989e Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 13 Dec 2024 11:21:14 -0800 Subject: [PATCH 1/4] Split Linux's and Android's long_os_version into separate functions --- src/unix/linux/system.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/unix/linux/system.rs b/src/unix/linux/system.rs index 3f14e4824..0243bb341 100644 --- a/src/unix/linux/system.rs +++ b/src/unix/linux/system.rs @@ -385,14 +385,26 @@ impl SystemInner { get_system_info_android(InfoType::Name) } + #[cfg(not(target_os = "android"))] pub(crate) fn long_os_version() -> Option { - #[cfg(target_os = "android")] - let system_name = "Android"; + let mut long_name = "Linux".to_owned(); - #[cfg(not(target_os = "android"))] - let system_name = "Linux"; + if let Some(os_version) = Self::os_version() { + long_name.push(' '); + long_name.push_str(&os_version); + } - let mut long_name = system_name.to_owned(); + if let Some(short_name) = Self::name() { + long_name.push(' '); + long_name.push_str(&short_name); + } + + Some(long_name) + } + + #[cfg(target_os = "android")] + pub(crate) fn long_os_version() -> Option { + let mut long_name = "Android".to_owned(); if let Some(os_version) = Self::os_version() { long_name.push(' '); From 8c8874b0aa78cc30dfa5c52737dafe75358fa68f Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 13 Dec 2024 11:25:39 -0800 Subject: [PATCH 2/4] Improve System::long_os_version() on Android --- src/common/system.rs | 2 +- src/unix/linux/system.rs | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/common/system.rs b/src/common/system.rs index b8b5ae774..dabf4e939 100644 --- a/src/common/system.rs +++ b/src/common/system.rs @@ -736,7 +736,7 @@ impl System { /// | example platform | value of `System::long_os_version()` | /// |---|---| /// | linux laptop | "Linux 24.04 Ubuntu" | - /// | android phone | "Android 15 Pixel 9 Pro" | + /// | android phone | "Android 15 on Pixel 9 Pro" | /// | apple laptop | "macOS 15.1.1 Sequoia" | /// | windows server | "Windows Server 2022 Datacenter" | /// diff --git a/src/unix/linux/system.rs b/src/unix/linux/system.rs index 0243bb341..cb5bec377 100644 --- a/src/unix/linux/system.rs +++ b/src/unix/linux/system.rs @@ -411,9 +411,12 @@ impl SystemInner { long_name.push_str(&os_version); } - if let Some(short_name) = Self::name() { - long_name.push(' '); - long_name.push_str(&short_name); + // Android's name() is extracted from the system property "ro.product.model" + // which is documented as "The end-user-visible name for the end product." + // So this produces a long_os_version like "Android 15 on Pixel 9 Pro". + if let Some(product_name) = Self::name() { + long_name.push_str(" on "); + long_name.push_str(&product_name); } Some(long_name) From d1e0c221dd2ff0c252daa2117d992e72df4aec54 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 13 Dec 2024 11:27:12 -0800 Subject: [PATCH 3/4] Improve System::long_os_version() on Linux --- src/common/system.rs | 2 +- src/unix/linux/system.rs | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/common/system.rs b/src/common/system.rs index dabf4e939..dab1ca35a 100644 --- a/src/common/system.rs +++ b/src/common/system.rs @@ -735,7 +735,7 @@ impl System { /// /// | example platform | value of `System::long_os_version()` | /// |---|---| - /// | linux laptop | "Linux 24.04 Ubuntu" | + /// | linux laptop | "Linux (Ubuntu 24.04)" | /// | android phone | "Android 15 on Pixel 9 Pro" | /// | apple laptop | "macOS 15.1.1 Sequoia" | /// | windows server | "Windows Server 2022 Datacenter" | diff --git a/src/unix/linux/system.rs b/src/unix/linux/system.rs index cb5bec377..c53d1ef3f 100644 --- a/src/unix/linux/system.rs +++ b/src/unix/linux/system.rs @@ -389,14 +389,19 @@ impl SystemInner { pub(crate) fn long_os_version() -> Option { let mut long_name = "Linux".to_owned(); - if let Some(os_version) = Self::os_version() { - long_name.push(' '); - long_name.push_str(&os_version); - } - - if let Some(short_name) = Self::name() { + let distro_name = Self::name(); + let distro_version = Self::os_version(); + if let (Some(distro_name), Some(distro_version)) = (&distro_name, &distro_version) { + // "Linux (Ubuntu 24.04)" + long_name.push_str(" ("); + long_name.push_str(distro_name); long_name.push(' '); - long_name.push_str(&short_name); + long_name.push_str(distro_version); + long_name.push(')'); + } else if let Some(distro_or_version) = distro_name.or(distro_version) { + long_name.push_str(" ("); + long_name.push_str(&distro_or_version); + long_name.push(')'); } Some(long_name) From d375fc6ea686bc9d3358235f294f6f7d8d980581 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 13 Dec 2024 16:31:22 -0800 Subject: [PATCH 4/4] Prefix distro version with "unknown" if distro name is not known --- src/unix/linux/system.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/unix/linux/system.rs b/src/unix/linux/system.rs index c53d1ef3f..f285606f1 100644 --- a/src/unix/linux/system.rs +++ b/src/unix/linux/system.rs @@ -391,16 +391,17 @@ impl SystemInner { let distro_name = Self::name(); let distro_version = Self::os_version(); - if let (Some(distro_name), Some(distro_version)) = (&distro_name, &distro_version) { + if let Some(distro_version) = &distro_version { // "Linux (Ubuntu 24.04)" long_name.push_str(" ("); - long_name.push_str(distro_name); + long_name.push_str(distro_name.as_deref().unwrap_or("unknown")); long_name.push(' '); long_name.push_str(distro_version); long_name.push(')'); - } else if let Some(distro_or_version) = distro_name.or(distro_version) { + } else if let Some(distro_name) = &distro_name { + // "Linux (Ubuntu)" long_name.push_str(" ("); - long_name.push_str(&distro_or_version); + long_name.push_str(distro_name); long_name.push(')'); }