-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OSDescription.Linux: return a user-friendly name based on /etc/os-rel…
…ease.
- Loading branch information
Showing
7 changed files
with
176 additions
and
2 deletions.
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 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. | ||
string? prettyName = null, name = null, version = null; | ||
foreach (var line in lines) | ||
{ | ||
if (line.StartsWith("PRETTY_NAME=", StringComparison.Ordinal)) | ||
{ | ||
prettyName = line.Substring("PRETTY_NAME=".Length); | ||
} | ||
else if (line.StartsWith("NAME=", StringComparison.Ordinal)) | ||
{ | ||
name = line.Substring("NAME=".Length); | ||
} | ||
else if (line.StartsWith("VERSION=", StringComparison.Ordinal)) | ||
{ | ||
version = line.Substring("VERSION=".Length); | ||
} | ||
} | ||
|
||
// Prefer PRETTY_NAME. | ||
if (prettyName is not null) | ||
{ | ||
return GetValue(prettyName); | ||
} | ||
|
||
// Fall back to: NAME[ VERSION]. | ||
if (name is not null) | ||
{ | ||
if (version is not null) | ||
{ | ||
return $"{GetValue(name)} {GetValue(version)}"; | ||
} | ||
return GetValue(name); | ||
} | ||
|
||
static string GetValue(string fieldValue) | ||
{ | ||
// Remove enclosing quotes. | ||
if ((fieldValue.StartsWith('"') && fieldValue.EndsWith('"')) || | ||
(fieldValue.StartsWith('\'') && fieldValue.EndsWith('\''))) | ||
{ | ||
fieldValue = fieldValue.Substring(1, fieldValue.Length - 2); | ||
} | ||
|
||
return fieldValue; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
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
69 changes: 69 additions & 0 deletions
69
src/libraries/Common/tests/Tests/Interop/osReleaseTests.cs
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// 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 | ||
{ | ||
[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 static void GetPrettyName_Success( | ||
string content, | ||
string expectedName) | ||
{ | ||
string path = Path.GetTempFileName(); | ||
try | ||
{ | ||
File.WriteAllText(path, content); | ||
|
||
string? name = Interop.OSReleaseFile.GetPrettyName(path); | ||
Assert.Equal(expectedName, name); | ||
} | ||
finally { File.Delete(path); } | ||
} | ||
|
||
[Fact] | ||
public static 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 static void GetPrettyName_CannotRead_ReturnsNull() | ||
{ | ||
string path = Path.GetTempFileName(); | ||
try | ||
{ | ||
File.SetUnixFileMode(path, UnixFileMode.None); | ||
Assert.ThrowsAny<Exception>(() => File.ReadAllText(path)); | ||
|
||
string? name = Interop.OSReleaseFile.GetPrettyName(path); | ||
Assert.Null(name); | ||
} | ||
finally { File.Delete(path); } | ||
} | ||
} | ||
} |
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
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