-
Notifications
You must be signed in to change notification settings - Fork 5.3k
OSDescription.Linux: return a user-friendly name based on /etc/os-release. #83976
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8451453
OSDescription.Linux: return a user-friendly name based on /etc/os-rel…
tmds e39f829
Use FileCleanupTestBase.
tmds 16099c8
Avoid some allocations by using Span.
tmds 14ecc5c
expectedName can be null.
tmds 64f2c9e
Apply suggestions from code review
tmds e9a0fc5
Break when we encounter PRETTY_NAME.
tmds 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
82 changes: 82 additions & 0 deletions
82
src/libraries/Common/src/Interop/Linux/os-release/Interop.OSReleaseFile.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| // Parse information from '/etc/os-release'. | ||
| internal static class OSReleaseFile | ||
| { | ||
| private const string EtcOsReleasePath = "/etc/os-release"; | ||
|
|
||
| /// <summary> | ||
| /// Returns a user-friendly distribution name. | ||
| /// </summary> | ||
| internal static string? GetPrettyName(string filename = EtcOsReleasePath) | ||
| { | ||
| if (File.Exists(filename)) | ||
| { | ||
| string[] lines; | ||
| try | ||
| { | ||
| lines = File.ReadAllLines(filename); | ||
| } | ||
| catch | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| // Parse the NAME, PRETTY_NAME, and VERSION fields. | ||
| // These fields are suitable for presentation to the user. | ||
| ReadOnlySpan<char> prettyName = default, name = default, version = default; | ||
| foreach (var line in lines) | ||
tmds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| ReadOnlySpan<char> lineSpan = line.AsSpan(); | ||
|
|
||
| _ = TryGetFieldValue(lineSpan, "PRETTY_NAME=", ref prettyName) || | ||
| TryGetFieldValue(lineSpan, "NAME=", ref name) || | ||
| TryGetFieldValue(lineSpan, "VERSION=", ref version); | ||
| } | ||
tmds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Prefer "PRETTY_NAME". | ||
| if (!prettyName.IsEmpty) | ||
| { | ||
| return new string(prettyName); | ||
| } | ||
|
|
||
| // Fall back to "NAME[ VERSION]". | ||
| if (!name.IsEmpty) | ||
| { | ||
| if (!version.IsEmpty) | ||
| { | ||
| return string.Concat(name, " ", version); | ||
| } | ||
| return new string(name); | ||
| } | ||
|
|
||
| static bool TryGetFieldValue(ReadOnlySpan<char> line, ReadOnlySpan<char> prefix, ref ReadOnlySpan<char> value) | ||
| { | ||
| if (!line.StartsWith(prefix)) | ||
| { | ||
| return false; | ||
| } | ||
| ReadOnlySpan<char> fieldValue = line.Slice(prefix.Length); | ||
|
|
||
| // Remove enclosing quotes. | ||
| if ((fieldValue.StartsWith("\"") && fieldValue.EndsWith("\"")) || | ||
| (fieldValue.StartsWith("'") && fieldValue.EndsWith("'"))) | ||
tmds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| fieldValue = fieldValue.Slice(1, fieldValue.Length - 2); | ||
tmds marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| value = fieldValue; | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
|
Member
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. Nit: I'd find this easier to read if this were moved up to above the static local function. |
||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
61 changes: 61 additions & 0 deletions
61
src/libraries/Common/tests/Tests/Interop/OSReleaseTests.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Text; | ||
| using Xunit; | ||
|
|
||
| namespace Common.Tests | ||
| { | ||
| public class OSReleaseTests : FileCleanupTestBase | ||
| { | ||
| [Theory] | ||
| // Double quotes: | ||
| [InlineData("NAME=\"Fedora\"\nVERSION=\"37\"\nPRETTY_NAME=\"Fedora Linux 37\"", "Fedora Linux 37")] | ||
| [InlineData("NAME=\"Fedora\"\nVERSION=\"37\"", "Fedora 37")] | ||
| [InlineData("NAME=\"Fedora\"", "Fedora")] | ||
| // Single quotes: | ||
| [InlineData("NAME='Ubuntu'\nVERSION='22.04'\nPRETTY_NAME='Ubuntu Linux 22.04'", "Ubuntu Linux 22.04")] | ||
| [InlineData("NAME='Ubuntu'\nVERSION='22.04'", "Ubuntu 22.04")] | ||
| [InlineData("NAME='Ubuntu'", "Ubuntu")] | ||
| // No quotes: | ||
| [InlineData("NAME=Alpine\nVERSION=3.14\nPRETTY_NAME=Alpine_Linux_3.14", "Alpine_Linux_3.14")] | ||
| [InlineData("NAME=Alpine\nVERSION=3.14", "Alpine 3.14")] | ||
| [InlineData("NAME=Alpine", "Alpine")] | ||
| // No pretty name fields: | ||
| [InlineData("ID=fedora\nVERSION_ID=37", null)] | ||
| [InlineData("", null)] | ||
| public void GetPrettyName_Success( | ||
| string content, | ||
| string? expectedName) | ||
| { | ||
| string path = GetTestFilePath(); | ||
| File.WriteAllText(path, content); | ||
|
|
||
| string? name = Interop.OSReleaseFile.GetPrettyName(path); | ||
| Assert.Equal(expectedName, name); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetPrettyName_NoFile_ReturnsNull() | ||
| { | ||
| string path = Path.GetRandomFileName(); | ||
| Assert.False(File.Exists(path)); | ||
|
|
||
| string? name = Interop.OSReleaseFile.GetPrettyName(path); | ||
| Assert.Null(name); | ||
| } | ||
|
|
||
| [Fact, PlatformSpecific(TestPlatforms.Linux)] | ||
| public void GetPrettyName_CannotRead_ReturnsNull() | ||
| { | ||
| string path = CreateTestFile(); | ||
| File.SetUnixFileMode(path, UnixFileMode.None); | ||
| Assert.ThrowsAny<Exception>(() => File.ReadAllText(path)); | ||
|
|
||
| string? name = Interop.OSReleaseFile.GetPrettyName(path); | ||
| Assert.Null(name); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.