From f0f3f094c5fa05bafe1963d1ea13f1be39a6673b Mon Sep 17 00:00:00 2001 From: B Stack Date: Sun, 17 May 2020 11:17:28 -0400 Subject: add upstream 10.24 --- zen/system.cpp | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) (limited to 'zen/system.cpp') diff --git a/zen/system.cpp b/zen/system.cpp index d9a169c7..aa967f71 100644 --- a/zen/system.cpp +++ b/zen/system.cpp @@ -96,22 +96,31 @@ std::wstring zen::getOsDescription() //throw FileError { try { - std::wstring osName; - std::wstring osVersion; - - if (const auto [exitCode, output] = consoleExecute("lsb_release --id -s", std::nullopt); //throw SysError - exitCode != 0) - throw SysError(formatSystemError("lsb_release --id", replaceCpy(_("Exit code %x"), L"%x", numberTo(exitCode)), output)); - else - osName = trimCpy(output); - - if (const auto [exitCode, output] = consoleExecute("lsb_release --release -s", std::nullopt); //throw SysError - exitCode != 0) - throw SysError(formatSystemError("lsb_release --release", replaceCpy(_("Exit code %x"), L"%x", numberTo(exitCode)), output)); - else - osVersion = trimCpy(output); - - return osName + L' ' + osVersion; //e.g. "CentOS 7.7.1908" + //"lsb_release" not available on some systems: https://freefilesync.org/forum/viewtopic.php?t=7191 + // => use /etc/os-release: https://www.freedesktop.org/software/systemd/man/os-release.html + std::string releaseInfo; + try + { + releaseInfo = loadBinContainer("/etc/os-release", nullptr /*notifyUnbufferedIO*/); //throw FileError + } + catch (const FileError& e) { throw SysError(e.toString()); } //further enrich with context info => SysError + + std::string osName; + std::string osVersion; + for (const std::string& line : split(releaseInfo, '\n', SplitType::SKIP_EMPTY)) //throw FileError + if (startsWith(line, "NAME=")) + osName = afterFirst(line, '=', IF_MISSING_RETURN_NONE); + else if (startsWith(line, "VERSION_ID=")) + osVersion = afterFirst(line, '=', IF_MISSING_RETURN_NONE); + + trim(osName, true, true, [](char c) { return c == '"' || c == '\''; }); + trim(osVersion, true, true, [](char c) { return c == '"' || c == '\''; }); + + if (osName .empty()) throw SysError(formatSystemError("/etc/os-release", L"", L"NAME missing.")); + if (osVersion.empty()) throw SysError(formatSystemError("/etc/os-release", L"", L"VERSION_ID missing.")); + + //PRETTY_NAME? too wordy! e.g. "Fedora 17 (Beefy Miracle)" + return utfTo(osName + ' ' + osVersion); //e.g. "CentOS Linux 7" } catch (const SysError& e) { throw FileError(_("Cannot get process information."), e.toString()); } -- cgit