Read Linux distribution info from os-release file

The Linux distribution community has somewhat deprecated the lsb_release
utility and has standardized on a new file, os-release, that can be
simply parsed to get the same information.  Attempt to read this file in
/etc/os-release, then /usr/lib/os-release, and finally, fall back to
using the lsb_release utility if neither of those files are found.

See: https://www.freedesktop.org/software/systemd/man/os-release.html
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2184391

Closes #23712.
This commit is contained in:
Scott Talbert 2023-07-14 11:45:19 -04:00 committed by Vadim Zeitlin
parent ed7f5a671c
commit aef7df6c9f

View file

@ -60,6 +60,8 @@
#include "wx/evtloop.h"
#include "wx/mstream.h"
#include "wx/private/fdioeventloopsourcehandler.h"
#include "wx/config.h"
#include "wx/filename.h"
#include <memory>
@ -1139,6 +1141,23 @@ wxString wxGetNativeCpuArchitectureName()
#ifdef __LINUX__
static bool
wxGetValuesFromOSRelease(const wxString& filename, wxLinuxDistributionInfo& ret)
{
if ( !wxFileName::Exists(filename) )
{
return false;
}
wxFileConfig fc(wxEmptyString, wxEmptyString, wxEmptyString, filename);
ret.Id = fc.Read(wxS("ID"), wxEmptyString).Capitalize();
ret.Description = fc.Read(wxS("PRETTY_NAME"), wxEmptyString);
ret.Release = fc.Read(wxS("VERSION_ID"), wxEmptyString);
ret.CodeName = fc.Read(wxS("VERSION_CODENAME"), wxEmptyString);
return true;
}
static bool
wxGetValueFromLSBRelease(const wxString& arg, const wxString& lhs, wxString* rhs)
{
@ -1153,6 +1172,17 @@ wxLinuxDistributionInfo wxGetLinuxDistributionInfo()
{
wxLinuxDistributionInfo ret;
// Read /etc/os-release and fall back to /usr/lib/os-release per below
// https://www.freedesktop.org/software/systemd/man/os-release.html
if ( wxGetValuesFromOSRelease(wxS("/etc/os-release"), ret) )
{
return ret;
}
if ( wxGetValuesFromOSRelease(wxS("/usr/lib/os-release"), ret) )
{
return ret;
}
if ( !wxGetValueFromLSBRelease(wxS("--id"), wxS("Distributor ID:\t"),
&ret.Id) )
{