summaryrefslogtreecommitdiffstats
path: root/widget/LSBUtils.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-08 15:11:26 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-08 15:11:26 +0000
commitfcea19dfd2c426bac0456da850e7c12258e4b9eb (patch)
treea31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /widget/LSBUtils.cpp
parentAdding upstream version 115.7.0esr. (diff)
downloadfirefox-esr-upstream/115.8.0esr.tar.xz
firefox-esr-upstream/115.8.0esr.zip
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'widget/LSBUtils.cpp')
-rw-r--r--widget/LSBUtils.cpp73
1 files changed, 72 insertions, 1 deletions
diff --git a/widget/LSBUtils.cpp b/widget/LSBUtils.cpp
index bcadddef12..520d50e5cc 100644
--- a/widget/LSBUtils.cpp
+++ b/widget/LSBUtils.cpp
@@ -6,6 +6,9 @@
#include "LSBUtils.h"
+#include <fstream>
+#include <string>
+#include <string_view>
#include <unistd.h>
#include "base/process_util.h"
#include "mozilla/FileUtils.h"
@@ -15,10 +18,78 @@
namespace mozilla::widget::lsb {
-static const char* gLsbReleasePath = "/usr/bin/lsb_release";
+static const char gLsbReleasePath[] = "/usr/bin/lsb_release";
+static const char gEtcOsReleasePath[] = "/etc/os-release";
+static const char gUsrOsReleasePath[] = "/usr/lib/os-release";
+
+// See https://www.freedesktop.org/software/systemd/man/latest/os-release.html
+bool ExtractAndSetValue(nsACString& aContainer, std::string_view& aValue) {
+ // We assume the value is well formed and doesn't contain escape characters.
+ if (aValue.size() > 1 && (aValue.front() == '"' || aValue.front() == '\'')) {
+ // We assume the quote is properly balanced.
+ aValue = aValue.substr(1, aValue.size() - 2);
+ }
+ aContainer.Assign(aValue.data(), aValue.size());
+ return !aValue.empty();
+}
+
+bool GetOSRelease(nsACString& aDistributor, nsACString& aDescription,
+ nsACString& aRelease, nsACString& aCodename) {
+ std::ifstream stream(gEtcOsReleasePath);
+ if (stream.fail()) {
+ stream.open(gUsrOsReleasePath);
+ if (stream.fail()) {
+ return false;
+ }
+ }
+ bool seen_id = false, seen_pretty_name = false, seen_version_id = false;
+ std::string rawline;
+ nsAutoCString name;
+ while (std::getline(stream, rawline)) {
+ std::string_view line(rawline);
+ size_t pos = line.find('=');
+ if (pos != std::string_view::npos) {
+ auto key = line.substr(0, pos);
+ auto value = line.substr(pos + 1);
+ if (key == "ID") {
+ if (ExtractAndSetValue(aDistributor, value)) {
+ // Capitalize the first letter of the id. This mimics what
+ // lsb_release does on Debian and derivatives. On RH derivatives,
+ // ID tends to be capitalized already.
+ char* c = aDistributor.BeginWriting();
+ if (*c >= 'a' && *c <= 'z') {
+ *c -= ('a' - 'A');
+ }
+ seen_id = true;
+ }
+ } else if (key == "NAME") {
+ ExtractAndSetValue(name, value);
+ } else if (key == "PRETTY_NAME") {
+ if (ExtractAndSetValue(aDescription, value)) seen_pretty_name = true;
+ } else if (key == "VERSION_ID") {
+ if (ExtractAndSetValue(aRelease, value)) seen_version_id = true;
+ } else if (key == "VERSION_CODENAME") {
+ ExtractAndSetValue(aCodename, value);
+ }
+ }
+ }
+ // If NAME is set and only differs from ID in case, use NAME.
+ if (seen_id && !name.IsEmpty() && name.EqualsIgnoreCase(aDistributor)) {
+ aDistributor = name;
+ }
+ // 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;
+}
bool GetLSBRelease(nsACString& aDistributor, nsACString& aDescription,
nsACString& aRelease, nsACString& aCodename) {
+ // Nowadays, /etc/os-release is more likely to be available than
+ // /usr/bin/lsb_release. Relying on the former also avoids forking.
+ if (GetOSRelease(aDistributor, aDescription, aRelease, aCodename)) {
+ return true;
+ }
+
if (access(gLsbReleasePath, R_OK) != 0) return false;
int pipefd[2];