/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// 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()) { returnfalse;
}
} 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('='); 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;
}
} elseif (key == "NAME") {
ExtractAndSetValue(name, value);
} elseif (key == "PRETTY_NAME") { if (ExtractAndSetValue(aDescription, value)) seen_pretty_name = true;
} elseif (key == "VERSION_ID") { if (ExtractAndSetValue(aRelease, value)) seen_version_id = true;
} elseif (key == "BUILD_ID") {
ExtractAndSetValue(build_id, value);
} elseif (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;
} // 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;
}
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)) { returntrue;
}
if (access(gLsbReleasePath, R_OK) != 0) returnfalse;
int pipefd[2]; if (pipe(pipefd) == -1) {
NS_WARNING("pipe() failed!"); returnfalse;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.