ostime and ossocket

This commit is contained in:
xsren 2024-09-13 15:20:23 +08:00
parent e3842b8fdc
commit 92171e080e
6 changed files with 59 additions and 52 deletions

View File

@ -54,7 +54,7 @@ enum TdTimezone {
TdEastZone12
};
void taosGetSystemTimezone(char *outTimezone, enum TdTimezone *tsTimezone);
int32_t taosGetSystemTimezone(char *outTimezone, enum TdTimezone *tsTimezone);
int32_t taosSetSystemTimezone(const char *inTimezone, char *outTimezone, int8_t *outDaylight, enum TdTimezone *tsTimezone);
#ifdef __cplusplus

View File

@ -157,6 +157,7 @@ int32_t taosGetErrSize();
#define TSDB_CODE_INTERNAL_ERROR TAOS_DEF_ERROR_CODE(0, 0x0138)
#define TSDB_CODE_SOCKET_ERROR TAOS_DEF_ERROR_CODE(0, 0x0139)
#define TSDB_CODE_UNSUPPORT_OS TAOS_DEF_ERROR_CODE(0, 0x013A)
#define TSDB_CODE_TIME_ERROR TAOS_DEF_ERROR_CODE(0, 0x013B)
//client
#define TSDB_CODE_TSC_INVALID_OPERATION TAOS_DEF_ERROR_CODE(0, 0x0200)

View File

@ -1696,7 +1696,10 @@ int32_t taosReadDataFolder(const char *cfgDir, const char **envCmd, const char *
SArray *pArgs) {
int32_t code = TSDB_CODE_SUCCESS;
if (tsCfg == NULL) osDefaultInit();
if (tsCfg == NULL) code = osDefaultInit();
if (code != 0) {
(void)printf("failed to init os since %s\n", tstrerror(code));
}
SConfig *pCfg = NULL;
TAOS_CHECK_RETURN(cfgInit(&pCfg));

View File

@ -50,7 +50,10 @@ int32_t osDefaultInit() {
taosSeedRand(taosSafeRand());
taosGetSystemLocale(tsLocale, tsCharset);
taosGetSystemTimezone(tsTimezoneStr, &tsTimezone);
code = taosGetSystemTimezone(tsTimezoneStr, &tsTimezone);
if(code != 0) {
return code;
}
if (strlen(tsTimezoneStr) > 0) { // ignore empty timezone
if ((code = taosSetSystemTimezone(tsTimezoneStr, tsTimezoneStr, &tsDaylight, &tsTimezone)) != TSDB_CODE_SUCCESS)
return code;

View File

@ -69,7 +69,7 @@ uint32_t taosSafeRand(void) {
if (len < 0) {
seed = (int)taosGetTimestampSec();
}
(void)taosCloseFile(&pFile);
TAOS_SKIP_ERROR(taosCloseFile(&pFile));
}
return (uint32_t)seed;

View File

@ -811,7 +811,11 @@ int32_t taosSetSystemTimezone(const char *inTimezoneStr, char *outTimezoneStr, i
#elif defined(_TD_DARWIN_64)
setenv("TZ", buf, 1);
code = setenv("TZ", buf, 1);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
tzset();
int32_t tz = (int32_t)((-timezone * MILLISECOND_PER_SECOND) / MILLISECOND_PER_HOUR);
*tsTimezone = tz;
@ -839,13 +843,17 @@ int32_t taosSetSystemTimezone(const char *inTimezoneStr, char *outTimezoneStr, i
return code;
}
void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
int32_t taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
int32_t code = 0;
#ifdef WINDOWS
char value[100];
char keyPath[100];
DWORD bufferSize = sizeof(value);
RegGetValue(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation", "TimeZoneKeyName",
LONG result = RegGetValue(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation", "TimeZoneKeyName",
RRF_RT_ANY, NULL, (PVOID)&value, &bufferSize);
if (result != ERROR_SUCCESS) {
return TAOS_SYSTEM_WINAPI_ERROR(result);
}
strcpy(outTimezoneStr, "not configured");
*tsTimezone = 0;
if (bufferSize > 0) {
@ -854,7 +862,10 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
strcpy(outTimezoneStr, win_tz[i][1]);
bufferSize = sizeof(value);
sprintf(keyPath, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", value);
RegGetValue(HKEY_LOCAL_MACHINE, keyPath, "Display", RRF_RT_ANY, NULL, (PVOID)&value, &bufferSize);
result = RegGetValue(HKEY_LOCAL_MACHINE, keyPath, "Display", RRF_RT_ANY, NULL, (PVOID)&value, &bufferSize);
if (result != ERROR_SUCCESS) {
return TAOS_SYSTEM_WINAPI_ERROR(result);
}
if (bufferSize > 0) {
// value[4] = (value[4] == '+' ? '-' : '+');
sprintf(outTimezoneStr, "%s (UTC, %c%c%c%c%c)", outTimezoneStr, value[4], value[5], value[6], value[8],
@ -865,6 +876,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
}
}
}
return 0;
#elif defined(_TD_DARWIN_64)
char buf[4096] = {0};
char *tz = NULL;
@ -883,21 +895,11 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
}
tz = zi + strlen("zoneinfo") + 1;
// for (int i = n - 1; i >= 0; --i) {
// if (buf[i] == '/') {
// if (tz) {
// tz = buf + i + 1;
// break;
// }
// tz = buf + i + 1;
// }
// }
// if (!tz || 0 == strchr(tz, '/')) {
// printf("parsing /etc/localtime failed\n");
// return;
// }
setenv("TZ", tz, 1);
code = setenv("TZ", tz, 1);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
tzset();
}
@ -908,7 +910,9 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
*/
time_t tx1 = taosGetTimestampSec();
struct tm tm1;
taosLocalTime(&tx1, &tm1, NULL);
if (taosLocalTime(&tx1, &tm1, NULL) == NULL) {
return TSDB_CODE_TIME_ERROR;
}
daylight = tm1.tm_isdst;
isdst_now = tm1.tm_isdst;
@ -920,6 +924,7 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
*/
snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (%s, %+03ld00)", tz, tm1.tm_isdst ? tzname[daylight] : tzname[0],
-timezone / 3600);
return 0;
#else
char buf[4096] = {0};
@ -927,8 +932,6 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
{
int n = readlink("/etc/localtime", buf, sizeof(buf)-1);
if (n < 0) {
(void)printf("read /etc/localtime error, reason:%s\n", strerror(errno));
if (taosCheckExistFile("/etc/timezone")) {
/*
* NOTE: do not remove it.
@ -937,7 +940,9 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
*/
time_t tx1 = taosGetTimestampSec();
struct tm tm1;
(void)taosLocalTime(&tx1, &tm1, NULL);
if(taosLocalTime(&tx1, &tm1, NULL) == NULL) {
return TSDB_CODE_TIME_ERROR;
}
/* load time zone string from /etc/timezone */
// FILE *f = fopen("/etc/timezone", "r");
errno = 0;
@ -946,12 +951,11 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
if (pFile != NULL) {
int len = taosReadFile(pFile, buf, 64);
if (len < 0) {
(void)taosCloseFile(&pFile);
(void)printf("read /etc/timezone error, reason:%s\n", strerror(errno));
return;
TAOS_UNUSED(taosCloseFile(&pFile));
return TSDB_CODE_TIME_ERROR;
}
(void)taosCloseFile(&pFile);
TAOS_UNUSED(taosCloseFile(&pFile));
buf[sizeof(buf) - 1] = 0;
char *lineEnd = strstr(buf, "\n");
@ -961,7 +965,11 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
// for CentOS system, /etc/timezone does not exist. Ignore the TZ environment variables
if (strlen(buf) > 0) {
(void)setenv("TZ", buf, 1);
code = setenv("TZ", buf, 1);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
}
}
// get and set default timezone
@ -986,34 +994,23 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
(void)snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (%s, %s%02d00)", buf, tzname[daylight], tz >= 0 ? "+" : "-",
abs(tz));
} else {
(void)printf("There is not /etc/timezone.\n");
return TSDB_CODE_TIME_ERROR;
}
return;
return 0;
}
buf[n] = '\0';
char *zi = strstr(buf, "zoneinfo");
if (!zi) {
(void)printf("parsing /etc/localtime failed\n");
return;
return TSDB_CODE_TIME_ERROR;
}
tz = zi + strlen("zoneinfo") + 1;
// for (int i = n - 1; i >= 0; --i) {
// if (buf[i] == '/') {
// if (tz) {
// tz = buf + i + 1;
// break;
// }
// tz = buf + i + 1;
// }
// }
// if (!tz || 0 == strchr(tz, '/')) {
// printf("parsing /etc/localtime failed");
// return;
// }
(void)setenv("TZ", tz, 1);
code = setenv("TZ", tz, 1);
if (-1 == code) {
terrno = TAOS_SYSTEM_ERROR(errno);
return terrno;
}
tzset();
}
@ -1024,7 +1021,9 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
*/
time_t tx1 = taosGetTimestampSec();
struct tm tm1;
(void)taosLocalTime(&tx1, &tm1, NULL);
if(taosLocalTime(&tx1, &tm1, NULL) == NULL) {
return TSDB_CODE_TIME_ERROR;
}
isdst_now = tm1.tm_isdst;
/*
@ -1035,5 +1034,6 @@ void taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) {
*/
(void)snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (%s, %+03ld00)", tz, tm1.tm_isdst ? tzname[daylight] : tzname[0],
-timezone / 3600);
return 0;
#endif
}