-
-
Notifications
You must be signed in to change notification settings - Fork 333
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
Improve System::long_os_version() on Linux and Android #1427
Merged
GuillaumeGomez
merged 4 commits into
GuillaumeGomez:master
from
dtolnay-contrib:longosversion
Dec 14, 2024
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ddd4dc0
Split Linux's and Android's long_os_version into separate functions
dtolnay 8c8874b
Improve System::long_os_version() on Android
dtolnay d1e0c22
Improve System::long_os_version() on Linux
dtolnay d375fc6
Prefix distro version with "unknown" if distro name is not known
dtolnay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -385,23 +385,43 @@ impl SystemInner { | |
get_system_info_android(InfoType::Name) | ||
} | ||
|
||
#[cfg(not(target_os = "android"))] | ||
pub(crate) fn long_os_version() -> Option<String> { | ||
#[cfg(target_os = "android")] | ||
let system_name = "Android"; | ||
let mut long_name = "Linux".to_owned(); | ||
|
||
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(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(')'); | ||
} | ||
|
||
#[cfg(not(target_os = "android"))] | ||
let system_name = "Linux"; | ||
Some(long_name) | ||
} | ||
|
||
let mut long_name = system_name.to_owned(); | ||
#[cfg(target_os = "android")] | ||
pub(crate) fn long_os_version() -> Option<String> { | ||
let mut long_name = "Android".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() { | ||
long_name.push(' '); | ||
long_name.push_str(&short_name); | ||
// Android's name() is extracted from the system property "ro.product.model" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the extra comment, always very appreciated! :) |
||
// 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) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part might be weird — I am not sure what circumstances would lead to
System::name()
being Some andSystem::os_version()
being None, or vice versa. The former wouldn't be an issue: "Linux (Ubuntu)" is perfectly fine. The latter is worse: "Linux (24.04)".I considered leaving out os_version() if name() is None, but then we have a situation that the value of long_os_version() specifically does not include the os_version().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think adding "unknown" in case the OS name is not known would likely improve the situation? If the version is unknown then just "Linux (Ubuntu)" sounds fine I think.
So with the three cases it gives:
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good call. I like that.
Speaking of "unknown", I forgot about checking for "unknown" in the Android case which is something I mentioned in #1419 (comment). Because
getString
in Build.java can return "unknown":But looking at this more, I think "unknown" is specific to the Java API. They have
public static final String MODEL
where they have chosen to use the string "unknown" instead ofnull
when a particular property hasn't been set. Whereas in Rust, we deal withlibc::__system_property_get
returning 0 by turning it into None (System::name()
returnsOption<String>
notString
). So I think we do not need to check for "unknown" after all.