Bug 1894412 - When reading /etc/os-release, fall back to BUILD_ID when VERSION_ID is not set. a=dmeehan

Arch Linux doesn't come with a VERSION_ID at all, but it has a BUILD_ID.
Semantically, falling back to BUILD_ID when VERSION_ID is not present
would seem fair play:

Per https://www.freedesktop.org/software/systemd/man/latest/os-release.html:

BUILD_ID=
    A string uniquely identifying the system image originally used as the
    installation base. In most cases, VERSION_ID or IMAGE_ID+IMAGE_VERSION
    are updated when the entire system image is replaced during an update.
    BUILD_ID may be used in distributions where the original installation
    image version is important: VERSION_ID would change during incremental
    system updates, but BUILD_ID would not. This field is optional.

Original Revision: https://phabricator.services.mozilla.com/D209089

Differential Revision: https://phabricator.services.mozilla.com/D209192
This commit is contained in:
Mike Hommey 2024-05-02 23:27:03 +00:00
parent ebd36e4ead
commit f84a160d5b

View File

@ -45,6 +45,7 @@ bool GetOSRelease(nsACString& aDistributor, nsACString& aDescription,
bool seen_id = false, seen_pretty_name = false, seen_version_id = false;
std::string rawline;
nsAutoCString name;
nsAutoCString build_id;
while (std::getline(stream, rawline)) {
std::string_view line(rawline);
size_t pos = line.find('=');
@ -68,6 +69,8 @@ bool GetOSRelease(nsACString& aDistributor, nsACString& aDescription,
if (ExtractAndSetValue(aDescription, value)) seen_pretty_name = true;
} else if (key == "VERSION_ID") {
if (ExtractAndSetValue(aRelease, value)) seen_version_id = true;
} else if (key == "BUILD_ID") {
ExtractAndSetValue(build_id, value);
} else if (key == "VERSION_CODENAME") {
ExtractAndSetValue(aCodename, value);
}
@ -77,6 +80,11 @@ bool GetOSRelease(nsACString& aDistributor, nsACString& aDescription,
if (seen_id && !name.IsEmpty() && name.EqualsIgnoreCase(aDistributor)) {
aDistributor = name;
}
// If VERSION_ID is not set but BUILD_ID is, use BUILD_ID.
if (!seen_version_id && !build_id.IsEmpty()) {
aRelease = build_id;
seen_version_id = true;
}
// Only consider our work done if we've seen at least ID, PRETTY_NAME and
// VERSION_ID.
return seen_id && seen_pretty_name && seen_version_id;