Add two ways for OS version detection on Darwin.

GitOrigin-RevId: cddea795e5d289b1fe15d88fc99db51e42c7138e
This commit is contained in:
levlam 2020-06-20 02:39:33 +03:00
parent 79808f6dd2
commit 4ae7bc23dc
2 changed files with 61 additions and 30 deletions

View File

@ -4678,6 +4678,7 @@ Status Td::set_parameters(td_api::object_ptr<td_api::tdlibParameters> parameters
} }
if (options_.system_version.empty()) { if (options_.system_version.empty()) {
options_.system_version = get_operating_system_version().str(); options_.system_version = get_operating_system_version().str();
VLOG(td_init) << "Set system version to " << options_.system_version;
} }
if (options_.application_version.empty()) { if (options_.application_version.empty()) {
return Status::Error(400, "Application version must be non-empty"); return Status::Error(400, "Application version must be non-empty");

View File

@ -15,18 +15,73 @@
#include "td/utils/port/Stat.h" #include "td/utils/port/Stat.h"
#if TD_PORT_POSIX #if TD_PORT_POSIX
#if TD_ANDROID #if TD_ANDROID
#include <sys/system_properties.h> #include <sys/system_properties.h>
#else #else
#if TD_DARWIN
#include <sys/sysctl.h>
#include <sys/types.h>
#endif
#include <sys/utsname.h> #include <sys/utsname.h>
#endif #endif
#endif #endif
namespace td { namespace td {
#if TD_DARWIN || TD_LINUX
static string read_os_name(CSlice os_version_file_path, CSlice prefix, CSlice suffix) {
auto r_stat = stat(os_version_file_path);
if (r_stat.is_ok() && r_stat.ok().is_reg_ && r_stat.ok().size_ < (1 << 16)) {
auto r_file = read_file_str(os_version_file_path, r_stat.ok().size_);
if (r_file.is_ok()) {
auto begin_pos = r_file.ok().find(prefix.c_str());
if (begin_pos != string::npos) {
begin_pos += prefix.size();
auto end_pos = r_file.ok().find(suffix.c_str(), begin_pos);
if (end_pos != string::npos) {
auto os_version = trim(r_file.ok().substr(begin_pos, end_pos - begin_pos));
if (os_version.find("\n") == string::npos) {
return os_version;
}
}
}
}
}
return string();
}
#endif
Slice get_operating_system_version() { Slice get_operating_system_version() {
static string result = []() -> string { static string result = []() -> string {
#if TD_PORT_POSIX #if TD_DARWIN
char version[256];
size_t size = sizeof(version);
string os_version;
if (sysctlbyname("kern.osproductversion", version, &size, nullptr, 0) == 0) {
os_version = trim(string(version, size));
}
if (os_version.empty()) {
os_version = read_os_name("/System/Library/CoreServices/SystemVersion.plist",
"<key>ProductUserVisibleVersion</key>\n\t<string>", "</string>\n");
}
if (!os_version.empty()) {
os_version = " " + os_version;
}
#if TD_DARWIN_IOS
return "iOS" + os_version;
#elif TD_DARWIN_TV_OS
return "tvOS" + os_version;
#elif TD_DARWIN_WATCH_OS
return "watchOS" + os_version;
#elif TD_DARWIN_MAC
return "macOS" + os_version;
#else
return "Darwin" + os_version;
#endif
#elif TD_PORT_POSIX
#if TD_ANDROID #if TD_ANDROID
char version[PROP_VALUE_MAX + 1]; char version[PROP_VALUE_MAX + 1];
int length = __system_property_get("ro.build.version.release", version); int length = __system_property_get("ro.build.version.release", version);
@ -35,24 +90,9 @@ Slice get_operating_system_version() {
} }
#else #else
#if TD_LINUX #if TD_LINUX
CSlice os_release_path = "/etc/os-release"; auto os_name = read_os_name("/etc/os-release", "PRETTY_NAME=\"", "\"\n");
auto r_stat = stat(os_release_path); if (!os_name.empty()) {
if (r_stat.is_ok() && r_stat.ok().is_reg_ && r_stat.ok().size_ < (1 << 16)) { return os_name;
auto r_file = read_file_str(os_release_path, r_stat.ok().size_);
if (r_file.is_ok()) {
CSlice prefix = "PRETTY_NAME=\"";
auto begin_pos = r_file.ok().find(prefix.c_str());
if (begin_pos != string::npos) {
begin_pos += prefix.size();
auto end_pos = r_file.ok().find("\"\n", begin_pos);
if (end_pos != string::npos) {
auto os_name = trim(r_file.ok().substr(begin_pos, end_pos - begin_pos));
if (!os_name.empty()) {
return os_name;
}
}
}
}
} }
#endif #endif
@ -67,17 +107,7 @@ Slice get_operating_system_version() {
#endif #endif
LOG(ERROR) << "Failed to identify OS name; use generic one"; LOG(ERROR) << "Failed to identify OS name; use generic one";
#if TD_DARWIN_IOS #if TD_ANDROID
return "iOS";
#elif TD_DARWIN_TV_OS
return "tvOS";
#elif TD_DARWIN_WATCH_OS
return "watchOS";
#elif TD_DARWIN_MAC
return "macOS";
#elif TD_DARWIN
return "Darwin";
#elif TD_ANDROID
return "Android"; return "Android";
#elif TD_TIZEN #elif TD_TIZEN
return "Tizen"; return "Tizen";