From beae4899f299b7bfe8c6804da3ca9fee276f890d Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 24 Feb 2022 11:35:22 +0800 Subject: [PATCH 1/7] os env --- include/common/tglobal.h | 1 - include/os/osEnv.h | 34 ++++--- source/client/src/clientCfg.c | 4 +- source/common/src/tglobal.c | 4 +- source/common/src/ttszip.c | 2 +- source/dnode/mgmt/daemon/src/dmnCfg.c | 13 +-- source/dnode/mgmt/daemon/src/dmnLog.c | 6 +- source/dnode/mgmt/impl/test/sut/src/sut.cpp | 2 +- source/libs/catalog/test/catalogTests.cpp | 2 +- source/libs/config/src/config.c | 8 +- source/libs/executor/src/executorimpl.c | 2 +- source/libs/function/src/tpercentile.c | 2 +- source/libs/function/src/tudf.c | 2 +- source/libs/index/test/fstUT.cc | 2 +- source/libs/qworker/test/qworkerTests.cpp | 2 +- .../libs/scalar/test/filter/filterTests.cpp | 2 +- .../libs/scalar/test/scalar/scalarTests.cpp | 2 +- source/libs/scheduler/test/schedulerTests.cpp | 2 +- source/libs/transport/test/transUT.cc | 2 +- source/os/src/osEnv.c | 92 +++++++++---------- source/os/src/osSysinfo.c | 4 +- source/util/src/tlog.c | 11 +-- tools/shell/src/shellEngine.c | 6 +- tools/shell/src/shellLinux.c | 4 +- 24 files changed, 108 insertions(+), 103 deletions(-) diff --git a/include/common/tglobal.h b/include/common/tglobal.h index 5e245f3761..8d73ea005d 100644 --- a/include/common/tglobal.h +++ b/include/common/tglobal.h @@ -27,7 +27,6 @@ extern int8_t tsDaylight; extern int32_t tsCompressMsgSize; extern int32_t tsCompressColData; extern int32_t tsMaxNumOfDistinctResults; -extern char tsTempDir[]; extern int tsCompatibleModel; // 2.0 compatible model extern int8_t tsEnableSlaveQuery; extern int8_t tsEnableAdjustMaster; diff --git a/include/os/osEnv.h b/include/os/osEnv.h index 2745520ae3..ba3dbcbc83 100644 --- a/include/os/osEnv.h +++ b/include/os/osEnv.h @@ -16,28 +16,32 @@ #ifndef _TD_OS_ENV_H_ #define _TD_OS_ENV_H_ +#include "osSysinfo.h" + #ifdef __cplusplus extern "C" { #endif -extern char tsOsName[]; +typedef struct SEnvVar { + char dataDir[PATH_MAX]; + char logDir[PATH_MAX]; + char tempDir[PATH_MAX]; + char osName[16]; + SDiskSpace dataSpace; + SDiskSpace logSpace; + SDiskSpace tempSpace; +} SEnvVar; -extern char tsDataDir[]; -extern char tsLogDir[]; -extern char tsTempDir[]; extern char configDir[]; -extern struct SDiskSpace tsLogSpace; -extern struct SDiskSpace tsTempSpace; -extern struct SDiskSpace tsDataSpace; - -void taosUpdateLogSpace(); -void taosUpdateTempSpace(); -void taosUpdateDataSpace(); -bool taosLogSpaceAvailable(); -bool taosTmpSpaceAvailable(); -bool taosDataSpaceAvailable(); -void taosUpdateAllSpace(); +void osInit(); +SEnvVar *osEnv(); +void osUpdate(); +bool osLogSpaceAvailable(); +char * osLogDir(); +char * osTempDir(); +char * osDataDir(); +char * osName(); #ifdef __cplusplus } diff --git a/source/client/src/clientCfg.c b/source/client/src/clientCfg.c index 826626fd77..f2020ddf95 100644 --- a/source/client/src/clientCfg.c +++ b/source/client/src/clientCfg.c @@ -67,7 +67,7 @@ static int32_t tscAddLogCfg(SConfig *pCfg) { } static int32_t tscSetLogCfg(SConfig *pCfg) { - tstrncpy(tsLogDir, cfgGetItem(pCfg, "logDir")->str, PATH_MAX); + tstrncpy(osLogDir(), cfgGetItem(pCfg, "logDir")->str, PATH_MAX); tsAsyncLog = cfgGetItem(pCfg, "asyncLog")->bval; tsNumOfLogLines = cfgGetItem(pCfg, "numOfLogLines")->i32; tsLogKeepDays = cfgGetItem(pCfg, "logKeepDays")->i32; @@ -146,7 +146,7 @@ static int32_t tscAddCfg(SConfig *pCfg) { // if (cfgAddString(pCfg, "gitinfo", gitinfo) != 0) return -1; // if (cfgAddString(pCfg, "version", version) != 0) return -1; - // if (cfgAddDir(pCfg, "dataDir", tsDataDir) != 0) return -1; + // if (cfgAddDir(pCfg, "dataDir", osDataDir()) != 0) return -1; if (cfgAddTimezone(pCfg, "timezone", "") != 0) return -1; if (cfgAddLocale(pCfg, "locale", "") != 0) return -1; if (cfgAddCharset(pCfg, "charset", "") != 0) return -1; diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index ce8be66e82..25da197b87 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -235,7 +235,7 @@ static void doInitGlobalConfig(void) { cfg.option = "dataDir"; - cfg.ptr = tsDataDir; + cfg.ptr = osDataDir(); cfg.valType = TAOS_CFG_VTYPE_DATA_DIRCTORY; cfg.cfgType = TSDB_CFG_CTYPE_B_CONFIG; cfg.minValue = 0; @@ -419,7 +419,7 @@ static void doInitGlobalConfig(void) { taosAddConfigOption(cfg); cfg.option = "tempDir"; - cfg.ptr = tsTempDir; + cfg.ptr = osTempDir(); cfg.valType = TAOS_CFG_VTYPE_STRING; cfg.cfgType = TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_CLIENT; cfg.minValue = 0; diff --git a/source/common/src/ttszip.c b/source/common/src/ttszip.c index 6d57992c35..3265ea5547 100644 --- a/source/common/src/ttszip.c +++ b/source/common/src/ttszip.c @@ -23,7 +23,7 @@ STSBuf* tsBufCreate(bool autoDelete, int32_t order) { pTSBuf->autoDelete = autoDelete; - taosGetTmpfilePath(tsTempDir, "join", pTSBuf->path); + taosGetTmpfilePath(osTempDir(), "join", pTSBuf->path); pTSBuf->f = fopen(pTSBuf->path, "wb+"); if (pTSBuf->f == NULL) { free(pTSBuf); diff --git a/source/dnode/mgmt/daemon/src/dmnCfg.c b/source/dnode/mgmt/daemon/src/dmnCfg.c index 52aa22a533..83bc66ab84 100644 --- a/source/dnode/mgmt/daemon/src/dmnCfg.c +++ b/source/dnode/mgmt/daemon/src/dmnCfg.c @@ -40,18 +40,19 @@ static int32_t dmnAddEpCfg(SConfig *pCfg) { } static int32_t dmnAddDirCfg(SConfig *pCfg) { - if (cfgAddDir(pCfg, "dataDir", tsDataDir) != 0) return -1; - if (cfgAddDir(pCfg, "tempDir", tsTempDir) != 0) return -1; + if (cfgAddDir(pCfg, "dataDir", osDataDir()) != 0) return -1; + if (cfgAddDir(pCfg, "tempDir", osTempDir()) != 0) return -1; if (cfgAddFloat(pCfg, "minimalDataDirGB", 2.0f, 0.001f, 10000000) != 0) return -1; if (cfgAddFloat(pCfg, "minimalTempDirGB", 1.0f, 0.001f, 10000000) != 0) return -1; return 0; } static int32_t dmnCheckDirCfg(SConfig *pCfg) { - tstrncpy(tsDataDir, cfgGetItem(pCfg, "dataDir")->str, PATH_MAX); - tstrncpy(tsTempDir, cfgGetItem(pCfg, "tempDir")->str, PATH_MAX); - tsDataSpace.reserved = cfgGetItem(pCfg, "minimalDataDirGB")->fval; - tsTempSpace.reserved = cfgGetItem(pCfg, "minimalTempDirGB")->fval; + SEnvVar *pEnv = osEnv(); + tstrncpy(pEnv->dataDir, cfgGetItem(pCfg, "dataDir")->str, PATH_MAX); + tstrncpy(pEnv->tempDir, cfgGetItem(pCfg, "tempDir")->str, PATH_MAX); + pEnv->dataSpace.reserved = cfgGetItem(pCfg, "minimalDataDirGB")->fval; + pEnv->tempSpace.reserved = cfgGetItem(pCfg, "minimalTempDirGB")->fval; return 0; } diff --git a/source/dnode/mgmt/daemon/src/dmnLog.c b/source/dnode/mgmt/daemon/src/dmnLog.c index 22a7d7b080..809e27fbf8 100644 --- a/source/dnode/mgmt/daemon/src/dmnLog.c +++ b/source/dnode/mgmt/daemon/src/dmnLog.c @@ -41,8 +41,10 @@ int32_t dmnAddLogCfg(SConfig *pCfg) { } int32_t dmnSetLogCfg(SConfig *pCfg) { - tstrncpy(tsLogDir, cfgGetItem(pCfg, "logDir")->str, PATH_MAX); - tsLogSpace.reserved = cfgGetItem(pCfg, "minimalLogDirGB")->fval; + SEnvVar *pEnv = osEnv(); + + tstrncpy(pEnv->logDir, cfgGetItem(pCfg, "logDir")->str, PATH_MAX); + pEnv->logSpace.reserved = cfgGetItem(pCfg, "minimalLogDirGB")->fval; tsAsyncLog = cfgGetItem(pCfg, "asyncLog")->bval; tsNumOfLogLines = cfgGetItem(pCfg, "numOfLogLines")->i32; tsLogKeepDays = cfgGetItem(pCfg, "logKeepDays")->i32; diff --git a/source/dnode/mgmt/impl/test/sut/src/sut.cpp b/source/dnode/mgmt/impl/test/sut/src/sut.cpp index 9863fd9f54..53b27ffaff 100644 --- a/source/dnode/mgmt/impl/test/sut/src/sut.cpp +++ b/source/dnode/mgmt/impl/test/sut/src/sut.cpp @@ -33,7 +33,7 @@ void Testbase::InitLog(const char* path) { taosRemoveDir(path); taosMkDir(path); - tstrncpy(tsLogDir, path, PATH_MAX); + tstrncpy(osLogDir(), path, PATH_MAX); if (taosInitLog("taosdlog", 1) != 0) { printf("failed to init log file\n"); } diff --git a/source/libs/catalog/test/catalogTests.cpp b/source/libs/catalog/test/catalogTests.cpp index ebe20fbb7f..9dfe6eac3f 100644 --- a/source/libs/catalog/test/catalogTests.cpp +++ b/source/libs/catalog/test/catalogTests.cpp @@ -132,7 +132,7 @@ void ctgTestInitLogFile() { ctgDbgEnableDebug("api"); if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { - printf("failed to open log file in directory:%s\n", tsLogDir); + printf("failed to open log file in directory:%s\n", osLogDir()); } } diff --git a/source/libs/config/src/config.c b/source/libs/config/src/config.c index 17a652d2b5..0b2680f08e 100644 --- a/source/libs/config/src/config.c +++ b/source/libs/config/src/config.c @@ -593,16 +593,16 @@ void cfgDumpCfg(SConfig *pCfg) { // pItem = cfgGetItem(pCfg, "dataDir"); // if (pItem != NULL) { -// tstrncpy(tsDataDir, pItem->str, PATH_MAX); +// tstrncpy(osDataDir(), pItem->str, PATH_MAX); // } // if (tsDiskCfgNum <= 0) { -// taosAddDataDir(0, tsDataDir, 0, 1); +// taosAddDataDir(0, osDataDir(), 0, 1); // tsDiskCfgNum = 1; -// uTrace("dataDir:%s, level:0 primary:1 is configured by default", tsDataDir); +// uTrace("dataDir:%s, level:0 primary:1 is configured by default", osDataDir()); // } -// if (taosDirExist(tsTempDir) != 0) { +// if (taosDirExist(osTempDir()) != 0) { // return -1; // } diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 62c0e3623f..cc05d433eb 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -4629,7 +4629,7 @@ int32_t doInitQInfo(SQInfo* pQInfo, STSBuf* pTsBuf, void* tsdb, void* sourceOptr getIntermediateBufInfo(pRuntimeEnv, &ps, &pQueryAttr->intermediateResultRowSize); int32_t TENMB = 1024*1024*10; - int32_t code = createDiskbasedBuffer(&pRuntimeEnv->pResultBuf, ps, TENMB, pQInfo->qId, tsTempDir); + int32_t code = createDiskbasedBuffer(&pRuntimeEnv->pResultBuf, ps, TENMB, pQInfo->qId, osTempDir()); if (code != TSDB_CODE_SUCCESS) { return code; } diff --git a/source/libs/function/src/tpercentile.c b/source/libs/function/src/tpercentile.c index 40731adc58..e58cdf8802 100644 --- a/source/libs/function/src/tpercentile.c +++ b/source/libs/function/src/tpercentile.c @@ -254,7 +254,7 @@ tMemBucket *tMemBucketCreate(int16_t nElemSize, int16_t dataType, double minval, resetSlotInfo(pBucket); - int32_t ret = createDiskbasedBuffer(&pBucket->pBuffer, pBucket->bufPageSize, pBucket->bufPageSize * 512, 1, tsTempDir); + int32_t ret = createDiskbasedBuffer(&pBucket->pBuffer, pBucket->bufPageSize, pBucket->bufPageSize * 512, 1, osTempDir()); if (ret != 0) { tMemBucketDestroy(pBucket); return NULL; diff --git a/source/libs/function/src/tudf.c b/source/libs/function/src/tudf.c index b65e637a57..98bcf189b0 100644 --- a/source/libs/function/src/tudf.c +++ b/source/libs/function/src/tudf.c @@ -55,7 +55,7 @@ int32_t initUdfInfo(SUdfInfo* pUdfInfo) { } else { char path[PATH_MAX] = {0}; - taosGetTmpfilePath("script", path, tsTempDir); + taosGetTmpfilePath("script", path, osTempDir()); FILE* file = fopen(path, "w+"); diff --git a/source/libs/index/test/fstUT.cc b/source/libs/index/test/fstUT.cc index d59a3428da..af8ee747bf 100644 --- a/source/libs/index/test/fstUT.cc +++ b/source/libs/index/test/fstUT.cc @@ -30,7 +30,7 @@ static void EnvInit() { taosRemoveDir(path.c_str()); taosMkDir(path.c_str()); // init log file - tstrncpy(tsLogDir, path.c_str(), PATH_MAX); + tstrncpy(osLogDir(), path.c_str(), PATH_MAX); if (taosInitLog("tindex.idx", 1) != 0) { printf("failed to init log"); } diff --git a/source/libs/qworker/test/qworkerTests.cpp b/source/libs/qworker/test/qworkerTests.cpp index 8ad5a76388..d8df9a81c4 100644 --- a/source/libs/qworker/test/qworkerTests.cpp +++ b/source/libs/qworker/test/qworkerTests.cpp @@ -102,7 +102,7 @@ void qwtInitLogFile() { qDebugFlag = 159; if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { - printf("failed to open log file in directory:%s\n", tsLogDir); + printf("failed to open log file in directory:%s\n", osLogDir()); } } diff --git a/source/libs/scalar/test/filter/filterTests.cpp b/source/libs/scalar/test/filter/filterTests.cpp index 420371fa04..44918da89d 100644 --- a/source/libs/scalar/test/filter/filterTests.cpp +++ b/source/libs/scalar/test/filter/filterTests.cpp @@ -53,7 +53,7 @@ void flttInitLogFile() { qDebugFlag = 159; if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { - printf("failed to open log file in directory:%s\n", tsLogDir); + printf("failed to open log file in directory:%s\n", osLogDir()); } } diff --git a/source/libs/scalar/test/scalar/scalarTests.cpp b/source/libs/scalar/test/scalar/scalarTests.cpp index 24bc8eaf40..de2c7f5874 100644 --- a/source/libs/scalar/test/scalar/scalarTests.cpp +++ b/source/libs/scalar/test/scalar/scalarTests.cpp @@ -52,7 +52,7 @@ void scltInitLogFile() { qDebugFlag = 159; if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { - printf("failed to open log file in directory:%s\n", tsLogDir); + printf("failed to open log file in directory:%s\n", osLogDir()); } } diff --git a/source/libs/scheduler/test/schedulerTests.cpp b/source/libs/scheduler/test/schedulerTests.cpp index 89d365a7e7..70684412ee 100644 --- a/source/libs/scheduler/test/schedulerTests.cpp +++ b/source/libs/scheduler/test/schedulerTests.cpp @@ -67,7 +67,7 @@ void schtInitLogFile() { qDebugFlag = 159; if (taosInitLog(defaultLogFileNamePrefix, maxLogFileNum) < 0) { - printf("failed to open log file in directory:%s\n", tsLogDir); + printf("failed to open log file in directory:%s\n", osLogDir()); } } diff --git a/source/libs/transport/test/transUT.cc b/source/libs/transport/test/transUT.cc index f5b3ed4c32..f2b1764dc6 100644 --- a/source/libs/transport/test/transUT.cc +++ b/source/libs/transport/test/transUT.cc @@ -155,7 +155,7 @@ class TransObj { taosRemoveDir(path.c_str()); taosMkDir(path.c_str()); - tstrncpy(tsLogDir, path.c_str(), PATH_MAX); + tstrncpy(osLogDir(), path.c_str(), PATH_MAX); if (taosInitLog("taosdlog", 1) != 0) { printf("failed to init log file\n"); } diff --git a/source/os/src/osEnv.c b/source/os/src/osEnv.c index 7417c6ff84..7949eb053e 100644 --- a/source/os/src/osEnv.c +++ b/source/os/src/osEnv.c @@ -13,77 +13,77 @@ * along with this program. If not, see . */ - #define _DEFAULT_SOURCE -#include "os.h" #include "osEnv.h" +#include "os.h" #include "osSysinfo.h" -SDiskSpace tsLogSpace; -SDiskSpace tsTempSpace; -SDiskSpace tsDataSpace; - -void taosUpdateLogSpace() { taosGetDiskSize(tsLogDir, &tsLogSpace.size); } - -void taosUpdateTempSpace() { taosGetDiskSize(tsTempDir, &tsTempSpace.size); } - -void taosUpdateDataSpace() { taosGetDiskSize(tsDataDir, &tsDataSpace.size); } - -bool taosLogSpaceAvailable() { return tsLogSpace.reserved < tsLogSpace.size.avail; } - -bool taosTempSpaceAvailable() { return tsTempSpace.reserved < tsTempSpace.size.avail; } - -bool taosDataSpaceAvailable() { return tsDataSpace.reserved < tsDataSpace.size.avail; } - -void taosUpdateAllSpace() { - taosUpdateLogSpace(); - taosUpdateTempSpace(); - taosUpdateDataSpace(); -} +SEnvVar env = {0}; +char configDir[PATH_MAX] = {0}; #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) -char tsOsName[10] = "Windows"; -char configDir[PATH_MAX] = "C:/TDengine/cfg"; -char tsDataDir[PATH_MAX] = "C:/TDengine/data"; -char tsLogDir[PATH_MAX] = "C:/TDengine/log"; -char tsTempDir[PATH_MAX] = "C:\\Windows\\Temp"; - extern taosWinSocketInit(); void osInit() { + srand(taosSafeRand()); taosWinSocketInit(); const char *tmpDir = getenv("tmp"); if (tmpDir == NULL) { tmpDir = getenv("temp"); } - if (tmpDir != NULL) { - strcpy(tsTempDir, tmpDir); + strcpy(env.tempDir, tmpDir); } + + strcpy(configDir, "C:\\TDengine\\cfg"); + strcpy(env.dataDir, "C:\\TDengine\\data"); + strcpy(env.logDir, "C:\\TDengine\\log"); + strcpy(env.tempDir, "C:\\Windows\\Temp"); + strcpy(env.osName, "Windows"); } #elif defined(_TD_DARWIN_64) -char tsOsName[10] = "Darwin"; -char configDir[PATH_MAX] = "/usr/local/etc/taos"; -char tsDataDir[PATH_MAX] = "/usr/local/var/lib/taos"; -char tsLogDir[PATH_MAX] = "/usr/local/var/log/taos"; -char tsTempDir[PATH_MAX] = "/tmp/taosd"; - -void osInit() {} - +void osInit() { + srand(taosSafeRand()); + strcpy(configDir, "/tmp/taosd"); + strcpy(env.dataDir, "/usr/local/var/lib/taos"); + strcpy(env.logDir, "/usr/local/var/log/taos"); + strcpy(env.tempDir, "/usr/local/etc/taos"); + strcpy(env.osName, "Darwin"); +} #else -char tsOsName[10] = "Linux"; -char configDir[PATH_MAX] = "/etc/taos"; -char tsDataDir[PATH_MAX] = "/var/lib/taos"; -char tsLogDir[PATH_MAX] = "/var/log/taos"; -char tsTempDir[PATH_MAX] = "/tmp/"; - void osInit() { - srand(taosSafeRand()); + srand(taosSafeRand()); + strcpy(configDir, "/etc/taos"); + strcpy(env.dataDir, "/var/lib/taos"); + strcpy(env.logDir, "/var/log/taos"); + strcpy(env.tempDir, "/tmp"); + strcpy(env.osName, "Linux"); } #endif + +SEnvVar *osEnv() { return &env; } + +void osUpdate() { + if (env.logDir[0] != 0) { + taosGetDiskSize(env.logDir, &env.logSpace.size); + } + if (env.dataDir[0] != 0) { + taosGetDiskSize(env.dataDir, &env.dataSpace.size); + } + if (env.tempDir[0] != 0) { + taosGetDiskSize(env.tempDir, &env.tempSpace.size); + } +} + +bool osLogSpaceAvailable() { return env.logSpace.reserved < env.logSpace.size.avail; } + +char *osLogDir() { return env.logDir; } +char *osTempDir() { return env.tempDir; } +char *osDataDir() { return env.dataDir; } +char *osName() { return env.osName; } diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index cae1b18b3c..d223a3d5a6 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -134,7 +134,7 @@ int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { diskSize->used = (int64_t)(i64TotalBytes - i64FreeBytes); return 0; } else { - //printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); + //printf("failed to get disk size, dataDir:%s errno:%s", osDataDir(), strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno); return -1; } @@ -441,7 +441,7 @@ void taosSetCoreDump() {} int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { struct statvfs info; if (statvfs(dataDir, &info)) { - //printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); + //printf("failed to get disk size, dataDir:%s errno:%s", osDataDir(), strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno); return -1; } else { diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index f479203fce..9b01aea0e4 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -118,10 +118,9 @@ static int32_t taosStartLog() { int32_t taosInitLog(const char *logName, int maxFiles) { if (tsLogInited) return 0; - taosUpdateLogSpace(); char fullName[PATH_MAX] = {0}; - snprintf(fullName, PATH_MAX, "%s" TD_DIRSEP "%s", tsLogDir, logName); + snprintf(fullName, PATH_MAX, "%s" TD_DIRSEP "%s", osLogDir(), logName); tsLogObj.logHandle = taosLogBuffNew(TSDB_DEFAULT_LOG_BUF_SIZE); if (tsLogObj.logHandle == NULL) return -1; @@ -187,7 +186,7 @@ static void taosKeepOldLog(char *oldName) { } } - taosRemoveOldFiles(tsLogDir, TABS(tsLogKeepDays)); + taosRemoveOldFiles(osLogDir(), TABS(tsLogKeepDays)); } static void *taosThreadToOpenNewFile(void *param) { @@ -380,7 +379,7 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum) { } void taosPrintLog(const char *flags, int32_t dflag, const char *format, ...) { - if (!taosLogSpaceAvailable()) return; + if (!osLogSpaceAvailable()) return; va_list argpointer; char buffer[MAX_LOGLINE_BUFFER_SIZE] = {0}; @@ -434,7 +433,7 @@ void taosPrintLog(const char *flags, int32_t dflag, const char *format, ...) { } void taosDumpData(unsigned char *msg, int32_t len) { - if (!taosLogSpaceAvailable()) return; + if (!osLogSpaceAvailable()) return; char temp[256]; int32_t i, pos = 0, c = 0; @@ -457,7 +456,7 @@ void taosDumpData(unsigned char *msg, int32_t len) { } void taosPrintLongString(const char *flags, int32_t dflag, const char *format, ...) { - if (!taosLogSpaceAvailable()) return; + if (!osLogSpaceAvailable()) return; va_list argpointer; char buffer[MAX_LOGLINE_DUMP_BUFFER_SIZE]; diff --git a/tools/shell/src/shellEngine.c b/tools/shell/src/shellEngine.c index 4186f0dae1..33774bdd05 100644 --- a/tools/shell/src/shellEngine.c +++ b/tools/shell/src/shellEngine.c @@ -68,11 +68,11 @@ TAOS *shellInit(SShellArguments *_args) { printf("\n"); if (!_args->is_use_passwd) { #ifdef TD_WINDOWS - strcpy(tsOsName, "Windows"); + strcpy(osName(), "Windows"); #elif defined(TD_DARWIN) - strcpy(tsOsName, "Darwin"); + strcpy(osName(), "Darwin"); #endif - printf(CLIENT_VERSION, tsOsName, taos_get_client_info()); + printf(CLIENT_VERSION, osName(), taos_get_client_info()); } fflush(stdout); diff --git a/tools/shell/src/shellLinux.c b/tools/shell/src/shellLinux.c index 89b477f08a..0526ebb442 100644 --- a/tools/shell/src/shellLinux.c +++ b/tools/shell/src/shellLinux.c @@ -184,8 +184,8 @@ static void parse_args( for (int i = 1; i < argc; i++) { if ((strncmp(argv[i], "-p", 2) == 0) || (strncmp(argv[i], "--password", 10) == 0)) { - strcpy(tsOsName, "Linux"); - printf(LINUXCLIENT_VERSION, tsOsName, taos_get_client_info()); + strcpy(osName(), "Linux"); + printf(LINUXCLIENT_VERSION, osName(), taos_get_client_info()); if ((strlen(argv[i]) == 2) || (strncmp(argv[i], "--password", 10) == 0)) { printf("Enter password: "); From 2331525a328eef63d1f31f69ccf2daa4ac6bcde2 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 24 Feb 2022 12:19:51 +0800 Subject: [PATCH 2/7] timezone --- include/common/tglobal.h | 1 - include/common/tmsg.h | 6 +- include/dnode/mgmt/dnode.h | 6 +- include/libs/config/config.h | 1 + include/os/os.h | 1 + include/os/osEnv.h | 18 ++- include/os/osSysinfo.h | 8 +- include/os/osTimezone.h | 30 +++++ source/client/src/clientEnv.c | 14 +- source/common/src/tglobal.c | 1 - source/dnode/mgmt/daemon/src/dmnCfg.c | 10 +- source/dnode/mgmt/impl/src/dndMgmt.c | 6 +- source/libs/parser/src/insertParser.c | 2 +- source/libs/parser/src/parserImpl.c | 2 +- source/libs/parser/src/parserUtil.c | 2 +- source/os/src/osEnv.c | 56 ++++---- source/os/src/osSysinfo.c | 138 ++----------------- source/os/src/osTimeZone.c | 187 ++++++++++++++++++++++---- 18 files changed, 275 insertions(+), 214 deletions(-) create mode 100644 include/os/osTimezone.h diff --git a/include/common/tglobal.h b/include/common/tglobal.h index 8d73ea005d..bb704f1536 100644 --- a/include/common/tglobal.h +++ b/include/common/tglobal.h @@ -23,7 +23,6 @@ extern "C" { #include "tdef.h" // common -extern int8_t tsDaylight; extern int32_t tsCompressMsgSize; extern int32_t tsCompressColData; extern int32_t tsMaxNumOfDistinctResults; diff --git a/include/common/tmsg.h b/include/common/tmsg.h index ae3586e735..9ab78f3c96 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -656,9 +656,9 @@ int32_t tDeserializeSRetrieveFuncRsp(void* buf, int32_t bufLen, SRetrieveFuncRsp typedef struct { int32_t statusInterval; int64_t checkTime; // 1970-01-01 00:00:00.000 - char timezone[TSDB_TIMEZONE_LEN]; // tsTimezone - char locale[TSDB_LOCALE_LEN]; // tsLocale - char charset[TSDB_LOCALE_LEN]; // tsCharset + char timezone[TD_TIMEZONE_LEN]; // tsTimezone + char locale[TD_LOCALE_LEN]; // tsLocale + char charset[TD_LOCALE_LEN]; // tsCharset } SClusterCfg; typedef struct { diff --git a/include/dnode/mgmt/dnode.h b/include/dnode/mgmt/dnode.h index 5d6c869b4d..d630fd0b1a 100644 --- a/include/dnode/mgmt/dnode.h +++ b/include/dnode/mgmt/dnode.h @@ -34,9 +34,9 @@ typedef struct { bool printAuth; int32_t rpcTimer; int32_t rpcMaxTime; - char timezone[TSDB_TIMEZONE_LEN]; - char locale[TSDB_LOCALE_LEN]; - char charset[TSDB_LOCALE_LEN]; + char timezone[TD_TIMEZONE_LEN]; + char locale[TD_LOCALE_LEN]; + char charset[TD_LOCALE_LEN]; char buildinfo[64]; char gitinfo[48]; } SDnodeEnvCfg; diff --git a/include/libs/config/config.h b/include/libs/config/config.h index 3c9ec1e3bf..594c258648 100644 --- a/include/libs/config/config.h +++ b/include/libs/config/config.h @@ -80,6 +80,7 @@ int32_t cfgGetSize(SConfig *pCfg); SConfigItem *cfgIterate(SConfig *pCfg, SConfigItem *pIter); void cfgCancelIterate(SConfig *pCfg, SConfigItem *pIter); SConfigItem *cfgGetItem(SConfig *pCfg, const char *name); +int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcType stype); int32_t cfgAddBool(SConfig *pCfg, const char *name, bool defaultVal); int32_t cfgAddInt32(SConfig *pCfg, const char *name, int32_t defaultVal, int64_t minval, int64_t maxval); diff --git a/include/os/os.h b/include/os/os.h index 023d2b0470..7b478c3137 100644 --- a/include/os/os.h +++ b/include/os/os.h @@ -73,6 +73,7 @@ extern "C" { #include "osThread.h" #include "osTime.h" #include "osTimer.h" +#include "osTimezone.h" void osInit(); diff --git a/include/os/osEnv.h b/include/os/osEnv.h index ba3dbcbc83..ca38ca19fb 100644 --- a/include/os/osEnv.h +++ b/include/os/osEnv.h @@ -26,10 +26,14 @@ typedef struct SEnvVar { char dataDir[PATH_MAX]; char logDir[PATH_MAX]; char tempDir[PATH_MAX]; - char osName[16]; SDiskSpace dataSpace; SDiskSpace logSpace; SDiskSpace tempSpace; + char osName[16]; + char timezone[TD_TIMEZONE_LEN]; + char locale[TD_LOCALE_LEN]; + char charset[TD_CHARSET_LEN]; + int8_t daylight; } SEnvVar; extern char configDir[]; @@ -38,10 +42,14 @@ void osInit(); SEnvVar *osEnv(); void osUpdate(); bool osLogSpaceAvailable(); -char * osLogDir(); -char * osTempDir(); -char * osDataDir(); -char * osName(); +char *osLogDir(); +char *osTempDir(); +char *osDataDir(); +char *osName(); +char *osTimezone(); +int8_t osDaylight(); + +void osSetTimezone(const char*timezone); #ifdef __cplusplus } diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index 6ab2a104df..3780131cda 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -21,9 +21,10 @@ extern "C" { #endif #include "os.h" - -#define TSDB_LOCALE_LEN 64 -#define TSDB_TIMEZONE_LEN 96 + +#define TD_LOCALE_LEN 64 +#define TD_CHARSET_LEN 64 +#define TD_TIMEZONE_LEN 96 typedef struct { int64_t total; @@ -41,7 +42,6 @@ extern int64_t tsOpenMax; extern int64_t tsStreamMax; extern int32_t tsNumOfCores; extern int32_t tsTotalMemoryMB; -extern char tsTimezone[]; extern char tsLocale[]; extern char tsCharset[]; // default encode string diff --git a/include/os/osTimezone.h b/include/os/osTimezone.h new file mode 100644 index 0000000000..6bf52a4108 --- /dev/null +++ b/include/os/osTimezone.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef _TD_OS_TIMEZONE_H_ +#define _TD_OS_TIMEZONE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +void osGetSystemTimezone(char *outTimezone); +void osSetSystemTimezone(const char *inTimezone, char *outTimezone, int8_t *outDaylight); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_OS_SOCKET_H_*/ diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 1ab05f53f3..eeb9b110c5 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -300,7 +300,7 @@ int taos_options_imp(TSDB_OPTION option, const char *str) { assert(cfg != NULL); size_t len = strlen(str); - if (len == 0 || len > TSDB_LOCALE_LEN) { + if (len == 0 || len > TD_LOCALE_LEN) { tscInfo("Invalid locale:%s, use default", str); return -1; } @@ -318,7 +318,7 @@ int taos_options_imp(TSDB_OPTION option, const char *str) { return -1; } - tstrncpy(tsLocale, defaultLocale, TSDB_LOCALE_LEN); + tstrncpy(tsLocale, defaultLocale, TD_LOCALE_LEN); } // set the user specified locale @@ -332,7 +332,7 @@ int taos_options_imp(TSDB_OPTION option, const char *str) { tscInfo("failed to set locale:%s, current locale:%s", str, tsLocale); } - tstrncpy(tsLocale, locale, TSDB_LOCALE_LEN); + tstrncpy(tsLocale, locale, TD_LOCALE_LEN); char *charset = strrchr(tsLocale, sep); if (charset != NULL) { @@ -347,7 +347,7 @@ int taos_options_imp(TSDB_OPTION option, const char *str) { tscInfo("charset changed from %s to %s", tsCharset, charset); } - tstrncpy(tsCharset, charset, TSDB_LOCALE_LEN); + tstrncpy(tsCharset, charset, TD_LOCALE_LEN); cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION; } else { @@ -371,7 +371,7 @@ int taos_options_imp(TSDB_OPTION option, const char *str) { assert(cfg != NULL); size_t len = strlen(str); - if (len == 0 || len > TSDB_LOCALE_LEN) { + if (len == 0 || len > TD_LOCALE_LEN) { tscInfo("failed to set charset:%s", str); return -1; } @@ -384,7 +384,7 @@ int taos_options_imp(TSDB_OPTION option, const char *str) { tscInfo("charset changed from %s to %s", tsCharset, str); } - tstrncpy(tsCharset, str, TSDB_LOCALE_LEN); + tstrncpy(tsCharset, str, TD_LOCALE_LEN); cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION; } else { tscInfo("charset:%s not valid", str); @@ -402,7 +402,7 @@ int taos_options_imp(TSDB_OPTION option, const char *str) { assert(cfg != NULL); if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) { - tstrncpy(tsTimezone, str, TSDB_TIMEZONE_LEN); + tstrncpy(tsTimezone, str, TD_TIMEZONE_LEN); tsSetTimeZone(); cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION; tscDebug("timezone set:%s, input:%s by taos_options", tsTimezone, str); diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index 25da197b87..5946232c85 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -29,7 +29,6 @@ // common -int8_t tsDaylight = 0; int32_t tsMaxBinaryDisplayWidth = 30; int8_t tsEnableSlaveQuery = 1; int8_t tsEnableAdjustMaster = 1; diff --git a/source/dnode/mgmt/daemon/src/dmnCfg.c b/source/dnode/mgmt/daemon/src/dmnCfg.c index 83bc66ab84..b09866fa7b 100644 --- a/source/dnode/mgmt/daemon/src/dmnCfg.c +++ b/source/dnode/mgmt/daemon/src/dmnCfg.c @@ -87,17 +87,25 @@ static int32_t dmnAddDnodeCfg(SConfig *pCfg) { return 0; } +static void dmnSetDnodeCfg(SConfig *pCfg) { + SConfigItem *pItem = cfgGetItem(pCfg, "timezone"); + osSetTimezone(pItem->str); + uDebug("timezone format changed from %s to %s", pItem->str, osTimezone()); + cfgSetItem(pCfg, "timezone", osTimezone(), pItem->stype); +} + static int32_t dmnCheckCfg(SConfig *pCfg) { bool enableCore = cfgGetItem(pCfg, "enableCoreFile")->bval; taosSetCoreDump(enableCore); + dmnSetDnodeCfg(pCfg); + if (dmnCheckDirCfg(pCfg) != 0) { return -1; } taosGetSystemInfo(); - tsSetTimeZone(); tsSetLocale(); if (tsNumOfCores <= 0) { diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 1748c52c66..b7f42fb465 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -371,9 +371,9 @@ void dndSendStatusReq(SDnode *pDnode) { req.clusterCfg.checkTime = 0; char timestr[32] = "1970-01-01 00:00:00.00"; (void)taosParseTime(timestr, &req.clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, 0); - memcpy(req.clusterCfg.timezone, pDnode->env.timezone, TSDB_TIMEZONE_LEN); - memcpy(req.clusterCfg.locale, pDnode->env.locale, TSDB_LOCALE_LEN); - memcpy(req.clusterCfg.charset, pDnode->env.charset, TSDB_LOCALE_LEN); + memcpy(req.clusterCfg.timezone, pDnode->env.timezone, TD_TIMEZONE_LEN); + memcpy(req.clusterCfg.locale, pDnode->env.locale, TD_LOCALE_LEN); + memcpy(req.clusterCfg.charset, pDnode->env.charset, TD_LOCALE_LEN); taosRUnLockLatch(&pMgmt->latch); req.pVloads = taosArrayInit(TSDB_MAX_VNODES, sizeof(SVnodeLoad)); diff --git a/source/libs/parser/src/insertParser.c b/source/libs/parser/src/insertParser.c index 745982e869..a88f679655 100644 --- a/source/libs/parser/src/insertParser.c +++ b/source/libs/parser/src/insertParser.c @@ -204,7 +204,7 @@ static int parseTime(char **end, SToken *pToken, int16_t timePrec, int64_t *time bool isSigned = false; toInteger(pToken->z, pToken->n, 10, &ts, &isSigned); } else { // parse the RFC-3339/ISO-8601 timestamp format string - if (taosParseTime(pToken->z, time, pToken->n, timePrec, tsDaylight) != TSDB_CODE_SUCCESS) { + if (taosParseTime(pToken->z, time, pToken->n, timePrec, osDaylight()) != TSDB_CODE_SUCCESS) { return buildSyntaxErrMsg(pMsgBuf, "invalid timestamp format", pToken->z); } diff --git a/source/libs/parser/src/parserImpl.c b/source/libs/parser/src/parserImpl.c index ef040fdff4..8e29f66c57 100644 --- a/source/libs/parser/src/parserImpl.c +++ b/source/libs/parser/src/parserImpl.c @@ -578,7 +578,7 @@ static EDealRes translateValue(STranslateContext* pCxt, SValueNode* pVal) { return DEAL_RES_ERROR; } int32_t len = trimStringCopy(pVal->literal, n, tmp); - if (taosParseTime(tmp, &pVal->datum.i, len, pVal->node.resType.precision, tsDaylight) != TSDB_CODE_SUCCESS) { + if (taosParseTime(tmp, &pVal->datum.i, len, pVal->node.resType.precision, osDaylight()) != TSDB_CODE_SUCCESS) { tfree(tmp); generateSyntaxErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, pVal->literal); return DEAL_RES_ERROR; diff --git a/source/libs/parser/src/parserUtil.c b/source/libs/parser/src/parserUtil.c index ec68980c44..2647806e03 100644 --- a/source/libs/parser/src/parserUtil.c +++ b/source/libs/parser/src/parserUtil.c @@ -1639,7 +1639,7 @@ static int parseTime(char **end, SToken *pToken, int16_t timePrec, int64_t *time bool isSigned = false; toInteger(pToken->z, pToken->n, 10, &ts, &isSigned); } else { // parse the RFC-3339/ISO-8601 timestamp format string - if (taosParseTime(pToken->z, time, pToken->n, timePrec, tsDaylight) != TSDB_CODE_SUCCESS) { + if (taosParseTime(pToken->z, time, pToken->n, timePrec, osDaylight()) != TSDB_CODE_SUCCESS) { return buildSyntaxErrMsg(pMsgBuf, "invalid timestamp format", pToken->z); } diff --git a/source/os/src/osEnv.c b/source/os/src/osEnv.c index 7949eb053e..1c85a6aac2 100644 --- a/source/os/src/osEnv.c +++ b/source/os/src/osEnv.c @@ -15,12 +15,41 @@ #define _DEFAULT_SOURCE #include "osEnv.h" -#include "os.h" -#include "osSysinfo.h" SEnvVar env = {0}; char configDir[PATH_MAX] = {0}; +SEnvVar *osEnv() { return &env; } + +void osInitImp() { + osGetSystemTimezone(env.timezone); + osSetTimezone(env.timezone); +} + +void osUpdate() { + if (env.logDir[0] != 0) { + taosGetDiskSize(env.logDir, &env.logSpace.size); + } + if (env.dataDir[0] != 0) { + taosGetDiskSize(env.dataDir, &env.dataSpace.size); + } + if (env.tempDir[0] != 0) { + taosGetDiskSize(env.tempDir, &env.tempSpace.size); + } +} + +bool osLogSpaceAvailable() { return env.logSpace.reserved < env.logSpace.size.avail; } + +char *osLogDir() { return env.logDir; } +char *osTempDir() { return env.tempDir; } +char *osDataDir() { return env.dataDir; } +char *osName() { return env.osName; } +char *osTimezone() { return env.timezone; } + +int8_t osDaylight() { return env.daylight; } + +void osSetTimezone(const char *timezone) { osSetSystemTimezone(timezone, env.timezone, &env.daylight); } + #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) extern taosWinSocketInit(); @@ -65,25 +94,4 @@ void osInit() { strcpy(env.osName, "Linux"); } -#endif - -SEnvVar *osEnv() { return &env; } - -void osUpdate() { - if (env.logDir[0] != 0) { - taosGetDiskSize(env.logDir, &env.logSpace.size); - } - if (env.dataDir[0] != 0) { - taosGetDiskSize(env.dataDir, &env.dataSpace.size); - } - if (env.tempDir[0] != 0) { - taosGetDiskSize(env.tempDir, &env.tempSpace.size); - } -} - -bool osLogSpaceAvailable() { return env.logSpace.reserved < env.logSpace.size.avail; } - -char *osLogDir() { return env.logDir; } -char *osTempDir() { return env.tempDir; } -char *osDataDir() { return env.dataDir; } -char *osName() { return env.osName; } +#endif \ No newline at end of file diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index d223a3d5a6..439a8146ab 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -21,9 +21,8 @@ int64_t tsPageSize = 0; int64_t tsOpenMax = 0; int64_t tsStreamMax = 0; int32_t tsNumOfCores = 1; -char tsTimezone[TSDB_TIMEZONE_LEN] = {0}; -char tsLocale[TSDB_LOCALE_LEN] = {0}; -char tsCharset[TSDB_LOCALE_LEN] = {0}; // default encode string +char tsLocale[TD_LOCALE_LEN] = {0}; +char tsCharset[TD_LOCALE_LEN] = {0}; // default encode string #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) @@ -89,21 +88,13 @@ bool taosGetProcMemory(float *memoryUsedMB) { return true; } -static void taosGetSystemTimezone() { - // get and set default timezone - char *tz = getenv("TZ"); - if (tz == NULL || strlen(tz) == 0) { - strcpy(tsTimezone, "not configured"); - } else { - strcpy(tsTimezone, tz); - } -} + static void taosGetSystemLocale() { // get and set default locale char *locale = setlocale(LC_CTYPE, "chs"); if (locale != NULL) { - tstrncpy(tsLocale, locale, TSDB_LOCALE_LEN); + tstrncpy(tsLocale, locale, TD_LOCALE_LEN); } strcpy(tsCharset, "cp936"); @@ -200,7 +191,6 @@ void taosGetSystemInfo() { taosGetCpuUsage(&tmp1, &tmp2); taosGetProcIO(&tmp1, &tmp2); - taosGetSystemTimezone(); taosGetSystemLocale(); } @@ -273,57 +263,6 @@ char *taosGetCmdlineByPID(int pid) { return ""; } #include #include -static void taosGetSystemTimezone() { - /* load time zone string from /etc/localtime */ - char buf[4096]; - char *tz = NULL; - { - int n = readlink("/etc/localtime", buf, sizeof(buf)); - if (n < 0) { - //printf("read /etc/localtime error, reason:%s", strerror(errno)); - return; - } - buf[n] = '\0'; - 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; - } - - setenv("TZ", tz, 1); - tzset(); - } - - /* - * NOTE: do not remove it. - * Enforce set the correct daylight saving time(DST) flag according - * to current time - */ - time_t tx1 = time(NULL); - struct tm tm1; - localtime_r(&tx1, &tm1); - - /* - * format example: - * - * Asia/Shanghai (CST, +0800) - * Europe/London (BST, +0100) - */ - snprintf(tsTimezone, TSDB_TIMEZONE_LEN, "%s (%s, %+03ld00)", tz, tm1.tm_isdst ? tzname[daylight] : tzname[0], - -timezone / 3600); - - // cfg_timezone->cfgStatus = TAOS_CFG_CSTATUS_DEFAULT; - //printf("timezone not configured, set to system default:%s", tsTimezone); -} - /* * originally from src/os/src/detail/osSysinfo.c * POSIX format locale string: @@ -351,7 +290,7 @@ static void taosGetSystemLocale() { // get and set default locale //printf("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno)); strcpy(tsLocale, "en_US.UTF-8"); } else { - tstrncpy(tsLocale, locale, TSDB_LOCALE_LEN); + tstrncpy(tsLocale, locale, TD_LOCALE_LEN); //printf("locale not configured, set to system default:%s", tsLocale); } @@ -361,7 +300,7 @@ static void taosGetSystemLocale() { // get and set default locale str++; char *revisedCharset = taosCharsetReplace(str); - tstrncpy(tsCharset, revisedCharset, TSDB_LOCALE_LEN); + tstrncpy(tsCharset, revisedCharset, TD_LOCALE_LEN); free(revisedCharset); //printf("charset not configured, set to system default:%s", tsCharset); @@ -387,7 +326,6 @@ void taosGetSystemInfo() { tsTotalMemoryMB = physical_pages * page_size / (1024 * 1024); tsPageSize = page_size; - taosGetSystemTimezone(); taosGetSystemLocale(); } @@ -625,65 +563,6 @@ static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { return true; } -static void taosGetSystemTimezone() { - /* - * NOTE: do not remove it. - * Enforce set the correct daylight saving time(DST) flag according - * to current time - */ - time_t tx1 = time(NULL); - struct tm tm1; - localtime_r(&tx1, &tm1); - - /* load time zone string from /etc/timezone */ - FILE *f = fopen("/etc/timezone", "r"); - char buf[68] = {0}; - if (f != NULL) { - int len = fread(buf, 64, 1, f); - if (len < 64 && ferror(f)) { - fclose(f); - //printf("read /etc/timezone error, reason:%s", strerror(errno)); - return; - } - - fclose(f); - - buf[sizeof(buf) - 1] = 0; - char *lineEnd = strstr(buf, "\n"); - if (lineEnd != NULL) { - *lineEnd = 0; - } - - // for CentOS system, /etc/timezone does not exist. Ignore the TZ environment variables - if (strlen(buf) > 0) { - setenv("TZ", buf, 1); - } - } - // get and set default timezone - tzset(); - - /* - * get CURRENT time zone. - * system current time zone is affected by daylight saving time(DST) - * - * e.g., the local time zone of London in DST is GMT+01:00, - * otherwise is GMT+00:00 - */ - int32_t tz = (-timezone * MILLISECOND_PER_SECOND) / MILLISECOND_PER_HOUR; - tz += daylight; - - /* - * format example: - * - * Asia/Shanghai (CST, +0800) - * Europe/London (BST, +0100) - */ - snprintf(tsTimezone, TSDB_TIMEZONE_LEN, "%s (%s, %s%02d00)", buf, tzname[daylight], tz >= 0 ? "+" : "-", abs(tz)); - - // cfg_timezone->cfgStatus = TAOS_CFG_CSTATUS_DEFAULT; - //printf("timezone not configured, set to system default:%s", tsTimezone); -} - /* * POSIX format locale string: * (Language Strings)_(Country/Region Strings).(code_page) @@ -710,7 +589,7 @@ static void taosGetSystemLocale() { // get and set default locale //printf("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno)); strcpy(tsLocale, "en_US.UTF-8"); } else { - tstrncpy(tsLocale, locale, TSDB_LOCALE_LEN); + tstrncpy(tsLocale, locale, TD_LOCALE_LEN); //printf("locale not configured, set to system default:%s", tsLocale); } @@ -720,7 +599,7 @@ static void taosGetSystemLocale() { // get and set default locale str++; char *revisedCharset = taosCharsetReplace(str); - tstrncpy(tsCharset, revisedCharset, TSDB_LOCALE_LEN); + tstrncpy(tsCharset, revisedCharset, TD_LOCALE_LEN); free(revisedCharset); //printf("charset not configured, set to system default:%s", tsCharset); @@ -957,7 +836,6 @@ void taosGetSystemInfo() { taosGetCpuUsage(&tmp1, &tmp2); taosGetProcIO(&tmp1, &tmp2); - taosGetSystemTimezone(); taosGetSystemLocale(); } diff --git a/source/os/src/osTimeZone.c b/source/os/src/osTimeZone.c index da40e14389..84e86662d3 100644 --- a/source/os/src/osTimeZone.c +++ b/source/os/src/osTimeZone.c @@ -15,40 +15,59 @@ #define _DEFAULT_SOURCE #include "os.h" -// #include "ulog.h" -// #include "tglobal.h" -// #include "tutil.h" -// TODO refactor to set the tz value through parameter -void tsSetTimeZone() { -#if 0 - SGlobalCfg *cfg_timezone = taosGetConfigOption("timezone"); - if (cfg_timezone != NULL) { - uInfo("timezone is set to %s by %s", tsTimezone, tsCfgStatusStr[cfg_timezone->cfgStatus]); - } +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#if (_WIN64) +#include +#include +#include +#include +#include +#include +#pragma comment(lib, "Mswsock.lib ") +#endif +#include +#pragma warning(push) +#pragma warning(disable : 4091) +#include +#pragma warning(pop) +#elif defined(_TD_DARWIN_64) +#include +#include +#else +#include +#include +#include +#include +#include +#include +#include +#include +#endif +void osSetSystemTimezone(const char *inTimezone, char *outTimezone, int8_t *outDaylight) { #ifdef WINDOWS - char winStr[TSDB_LOCALE_LEN * 2]; - sprintf(winStr, "TZ=%s", tsTimezone); + char winStr[TD_LOCALE_LEN * 2]; + sprintf(winStr, "TZ=%s", inTimezone); putenv(winStr); #else - setenv("TZ", tsTimezone, 1); + setenv("TZ", inTimezone, 1); #endif tzset(); /* - * get CURRENT time zone. - * system current time zone is affected by daylight saving time(DST) - * - * e.g., the local time zone of London in DST is GMT+01:00, - * otherwise is GMT+00:00 - */ + * get CURRENT time zone. + * system current time zone is affected by daylight saving time(DST) + * + * e.g., the local time zone of London in DST is GMT+01:00, + * otherwise is GMT+00:00 + */ #ifdef _MSC_VER #if _MSC_VER >= 1900 // see https://docs.microsoft.com/en-us/cpp/c-runtime-library/daylight-dstbias-timezone-and-tzname?view=vs-2019 int64_t timezone = _timezone; int32_t daylight = _daylight; - char **tzname = _tzname; + char **tzname = _tzname; #endif #endif @@ -56,14 +75,124 @@ void tsSetTimeZone() { tz += daylight; /* - * format: - * (CST, +0800) - * (BST, +0100) - */ - sprintf(tsTimezone, "(%s, %s%02d00)", tzname[daylight], tz >= 0 ? "+" : "-", abs(tz)); - tsDaylight = daylight; + * format: + * (CST, +0800) + * (BST, +0100) + */ - uInfo("timezone format changed to %s", tsTimezone); - -#endif + sprintf(outTimezone, "(%s, %s%02d00)", tzname[daylight], tz >= 0 ? "+" : "-", abs(tz)); + *outDaylight = daylight; +} + +void osGetSystemTimezone(char *outTimezone) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + char *tz = getenv("TZ"); + if (tz == NULL || strlen(tz) == 0) { + strcpy(outTimezone, "not configured"); + } else { + strcpy(outTimezone, tz); + } + +#elif defined(_TD_DARWIN_64) + char buf[4096] = {0}; + char *tz = NULL; + { + int n = readlink("/etc/localtime", buf, sizeof(buf)); + if (n < 0) { + printf("read /etc/localtime error, reason:%s", strerror(errno)); + return; + } + buf[n] = '\0'; + 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; + } + + setenv("TZ", tz, 1); + tzset(); + } + + /* + * NOTE: do not remove it. + * Enforce set the correct daylight saving time(DST) flag according + * to current time + */ + time_t tx1 = time(NULL); + struct tm tm1; + localtime_r(&tx1, &tm1); + + /* + * format example: + * + * Asia/Shanghai (CST, +0800) + * Europe/London (BST, +0100) + */ + snprintf(outTimezone, TD_TIMEZONE_LEN, "%s (%s, %+03ld00)", tz, tm1.tm_isdst ? tzname[daylight] : tzname[0], + -timezone / 3600); + +#else + /* + * NOTE: do not remove it. + * Enforce set the correct daylight saving time(DST) flag according + * to current time + */ + time_t tx1 = time(NULL); + struct tm tm1; + localtime_r(&tx1, &tm1); + + /* load time zone string from /etc/timezone */ + FILE *f = fopen("/etc/timezone", "r"); + char buf[68] = {0}; + if (f != NULL) { + int len = fread(buf, 64, 1, f); + if (len < 64 && ferror(f)) { + fclose(f); + // printf("read /etc/timezone error, reason:%s", strerror(errno)); + return; + } + + fclose(f); + + buf[sizeof(buf) - 1] = 0; + char *lineEnd = strstr(buf, "\n"); + if (lineEnd != NULL) { + *lineEnd = 0; + } + + // for CentOS system, /etc/timezone does not exist. Ignore the TZ environment variables + if (strlen(buf) > 0) { + setenv("TZ", buf, 1); + } + } + // get and set default timezone + tzset(); + + /* + * get CURRENT time zone. + * system current time zone is affected by daylight saving time(DST) + * + * e.g., the local time zone of London in DST is GMT+01:00, + * otherwise is GMT+00:00 + */ + int32_t tz = (-timezone * MILLISECOND_PER_SECOND) / MILLISECOND_PER_HOUR; + tz += daylight; + + /* + * format example: + * + * Asia/Shanghai (CST, +0800) + * Europe/London (BST, +0100) + */ + snprintf(outTimezone, TD_TIMEZONE_LEN, "%s (%s, %s%02d00)", buf, tzname[daylight], tz >= 0 ? "+" : "-", abs(tz)); + +#endif } From f0b75b96cec106c03ba707032e088de20bee351f Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 24 Feb 2022 12:20:48 +0800 Subject: [PATCH 3/7] timezone --- include/common/ttimezone.h | 29 --------------------------- source/client/src/clientEnv.c | 1 - source/common/src/tglobal.c | 1 - source/dnode/mgmt/daemon/src/dmnCfg.c | 1 - source/libs/config/src/config.c | 1 - 5 files changed, 33 deletions(-) delete mode 100644 include/common/ttimezone.h diff --git a/include/common/ttimezone.h b/include/common/ttimezone.h deleted file mode 100644 index cc50a29d57..0000000000 --- a/include/common/ttimezone.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#ifndef _TD_COMMON_TIMEZONE_H_ -#define _TD_COMMON_TIMEZONE_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -void tsSetTimeZone(); - -#ifdef __cplusplus -} -#endif - -#endif /*_TD_COMMON_TIMEZONE_H_*/ diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index eeb9b110c5..f07048efbc 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -25,7 +25,6 @@ #include "tref.h" #include "trpc.h" #include "ttime.h" -#include "ttimezone.h" #define TSC_VAR_NOT_RELEASE 1 #define TSC_VAR_RELEASED 0 diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index 5946232c85..e26dbb639a 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -23,7 +23,6 @@ #include "tglobal.h" #include "tlocale.h" #include "tlog.h" -#include "ttimezone.h" #include "tutil.h" #include "ulog.h" diff --git a/source/dnode/mgmt/daemon/src/dmnCfg.c b/source/dnode/mgmt/daemon/src/dmnCfg.c index b09866fa7b..9215fb6699 100644 --- a/source/dnode/mgmt/daemon/src/dmnCfg.c +++ b/source/dnode/mgmt/daemon/src/dmnCfg.c @@ -16,7 +16,6 @@ #define _DEFAULT_SOURCE #include "dmnInt.h" #include "tlocale.h" -#include "ttimezone.h" static int32_t dmnAddEpCfg(SConfig *pCfg) { char defaultFqdn[TSDB_FQDN_LEN] = {0}; diff --git a/source/libs/config/src/config.c b/source/libs/config/src/config.c index 0b2680f08e..f877b22046 100644 --- a/source/libs/config/src/config.c +++ b/source/libs/config/src/config.c @@ -18,7 +18,6 @@ #include "tep.h" #include "tlocale.h" #include "tmsg.h" -#include "ttimezone.h" #define CFG_NAME_PRINT_LEN 22 #define CFG_SRC_PRINT_LEN 12 From 942d0b69b1f59dfdbae9304e80cbb3bc3767c60d Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 24 Feb 2022 13:20:52 +0800 Subject: [PATCH 4/7] osenv --- include/os/osEnv.h | 24 ++++++++++++------------ source/dnode/mgmt/daemon/src/dmnCfg.c | 2 +- source/dnode/mgmt/daemon/src/dmnLog.c | 5 ++--- source/dnode/mgmt/daemon/src/dmnMain.c | 2 ++ source/os/src/osEnv.c | 6 +++--- source/util/src/tlog.c | 1 + 6 files changed, 21 insertions(+), 19 deletions(-) diff --git a/include/os/osEnv.h b/include/os/osEnv.h index ca38ca19fb..63ccdb1a64 100644 --- a/include/os/osEnv.h +++ b/include/os/osEnv.h @@ -22,7 +22,7 @@ extern "C" { #endif -typedef struct SEnvVar { +typedef struct SOsEnv { char dataDir[PATH_MAX]; char logDir[PATH_MAX]; char tempDir[PATH_MAX]; @@ -34,20 +34,20 @@ typedef struct SEnvVar { char locale[TD_LOCALE_LEN]; char charset[TD_CHARSET_LEN]; int8_t daylight; -} SEnvVar; +} SOsEnv; extern char configDir[]; -void osInit(); -SEnvVar *osEnv(); -void osUpdate(); -bool osLogSpaceAvailable(); -char *osLogDir(); -char *osTempDir(); -char *osDataDir(); -char *osName(); -char *osTimezone(); -int8_t osDaylight(); +void osInit(); +SOsEnv *osEnv(); +void osUpdate(); +bool osLogSpaceAvailable(); +char *osLogDir(); +char *osTempDir(); +char *osDataDir(); +char *osName(); +char *osTimezone(); +int8_t osDaylight(); void osSetTimezone(const char*timezone); diff --git a/source/dnode/mgmt/daemon/src/dmnCfg.c b/source/dnode/mgmt/daemon/src/dmnCfg.c index 9215fb6699..5da3233b17 100644 --- a/source/dnode/mgmt/daemon/src/dmnCfg.c +++ b/source/dnode/mgmt/daemon/src/dmnCfg.c @@ -47,7 +47,7 @@ static int32_t dmnAddDirCfg(SConfig *pCfg) { } static int32_t dmnCheckDirCfg(SConfig *pCfg) { - SEnvVar *pEnv = osEnv(); + SOsEnv *pEnv = osEnv(); tstrncpy(pEnv->dataDir, cfgGetItem(pCfg, "dataDir")->str, PATH_MAX); tstrncpy(pEnv->tempDir, cfgGetItem(pCfg, "tempDir")->str, PATH_MAX); pEnv->dataSpace.reserved = cfgGetItem(pCfg, "minimalDataDirGB")->fval; diff --git a/source/dnode/mgmt/daemon/src/dmnLog.c b/source/dnode/mgmt/daemon/src/dmnLog.c index 809e27fbf8..5079cd907a 100644 --- a/source/dnode/mgmt/daemon/src/dmnLog.c +++ b/source/dnode/mgmt/daemon/src/dmnLog.c @@ -17,7 +17,7 @@ #include "dmnInt.h" int32_t dmnAddLogCfg(SConfig *pCfg) { - if (cfgAddDir(pCfg, "logDir", "/var/log/taos") != 0) return -1; + if (cfgAddDir(pCfg, "logDir", osLogDir()) != 0) return -1; if (cfgAddFloat(pCfg, "minimalLogDirGB", 1.0f, 0.001f, 10000000) != 0) return -1; if (cfgAddBool(pCfg, "asyncLog", 1) != 0) return -1; if (cfgAddInt32(pCfg, "numOfLogLines", 10000000, 1000, 2000000000) != 0) return -1; @@ -41,8 +41,7 @@ int32_t dmnAddLogCfg(SConfig *pCfg) { } int32_t dmnSetLogCfg(SConfig *pCfg) { - SEnvVar *pEnv = osEnv(); - + SOsEnv *pEnv = osEnv(); tstrncpy(pEnv->logDir, cfgGetItem(pCfg, "logDir")->str, PATH_MAX); pEnv->logSpace.reserved = cfgGetItem(pCfg, "minimalLogDirGB")->fval; tsAsyncLog = cfgGetItem(pCfg, "asyncLog")->bval; diff --git a/source/dnode/mgmt/daemon/src/dmnMain.c b/source/dnode/mgmt/daemon/src/dmnMain.c index 1f1d2d8d05..62f0db5fc5 100644 --- a/source/dnode/mgmt/daemon/src/dmnMain.c +++ b/source/dnode/mgmt/daemon/src/dmnMain.c @@ -97,6 +97,8 @@ int32_t dmnRunDnode(SConfig *pCfg) { } int main(int argc, char const *argv[]) { + osInit(); + if (dmnParseOption(argc, argv) != 0) { return -1; } diff --git a/source/os/src/osEnv.c b/source/os/src/osEnv.c index 1c85a6aac2..462f7044e0 100644 --- a/source/os/src/osEnv.c +++ b/source/os/src/osEnv.c @@ -16,10 +16,10 @@ #define _DEFAULT_SOURCE #include "osEnv.h" -SEnvVar env = {0}; -char configDir[PATH_MAX] = {0}; +SOsEnv env = {0}; +char configDir[PATH_MAX] = {0}; -SEnvVar *osEnv() { return &env; } +SOsEnv *osEnv() { return &env; } void osInitImp() { osGetSystemTimezone(env.timezone); diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 9b01aea0e4..ed55ef91b0 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -118,6 +118,7 @@ static int32_t taosStartLog() { int32_t taosInitLog(const char *logName, int maxFiles) { if (tsLogInited) return 0; + osUpdate(); char fullName[PATH_MAX] = {0}; snprintf(fullName, PATH_MAX, "%s" TD_DIRSEP "%s", osLogDir(), logName); From 9aaa9d205b638848b52ab7d5d338880f494ffea3 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 24 Feb 2022 13:22:24 +0800 Subject: [PATCH 5/7] rename --- source/os/src/{osTimeZone.c => osTimeZone1.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename source/os/src/{osTimeZone.c => osTimeZone1.c} (100%) diff --git a/source/os/src/osTimeZone.c b/source/os/src/osTimeZone1.c similarity index 100% rename from source/os/src/osTimeZone.c rename to source/os/src/osTimeZone1.c From 77788309663b3e41127c5afb7a7160e599414ee0 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 24 Feb 2022 13:51:12 +0800 Subject: [PATCH 6/7] locale --- include/os/os.h | 1 + include/os/osEnv.h | 2 + include/{common/tlocale.h => os/osLocale.h} | 15 +- include/os/osString.h | 1 - include/os/osSysinfo.h | 2 - include/os/osTimezone.h | 6 +- source/common/src/tglobal.c | 1 - source/common/src/tlocale.c | 45 ---- source/dnode/mgmt/daemon/src/dmnCfg.c | 2 - source/libs/config/src/config.c | 1 - source/os/src/osEnv.c | 7 +- source/os/src/osLocale.c | 194 ++++++++++++++++++ source/os/src/osString.c | 18 -- source/os/src/osSysinfo.c | 109 ---------- source/os/src/{osTimeZone1.c => osTimezone.c} | 4 +- 15 files changed, 217 insertions(+), 191 deletions(-) rename include/{common/tlocale.h => os/osLocale.h} (70%) delete mode 100644 source/common/src/tlocale.c create mode 100644 source/os/src/osLocale.c rename source/os/src/{osTimeZone1.c => osTimezone.c} (97%) diff --git a/include/os/os.h b/include/os/os.h index 7b478c3137..d96f7efb51 100644 --- a/include/os/os.h +++ b/include/os/os.h @@ -59,6 +59,7 @@ extern "C" { #include "osEndian.h" #include "osEnv.h" #include "osFile.h" +#include "osLocale.h" #include "osLz4.h" #include "osMath.h" #include "osMemory.h" diff --git a/include/os/osEnv.h b/include/os/osEnv.h index 63ccdb1a64..c7c8ba9053 100644 --- a/include/os/osEnv.h +++ b/include/os/osEnv.h @@ -48,6 +48,8 @@ char *osDataDir(); char *osName(); char *osTimezone(); int8_t osDaylight(); +char *osLocale(); +char *osCharset(); void osSetTimezone(const char*timezone); diff --git a/include/common/tlocale.h b/include/os/osLocale.h similarity index 70% rename from include/common/tlocale.h rename to include/os/osLocale.h index d809c75d67..6e313eb8cd 100644 --- a/include/common/tlocale.h +++ b/include/os/osLocale.h @@ -13,17 +13,22 @@ * along with this program. If not, see . */ -#ifndef _TD_COMMON_LOCALE_H_ -#define _TD_COMMON_LOCALE_H_ +#ifndef _TD_OS_LOCALE_H_ +#define _TD_OS_LOCALE_H_ + +#include "os.h" +#include "osString.h" #ifdef __cplusplus extern "C" { #endif -void tsSetLocale(); +char *taosCharsetReplace(char *charsetstr); +void taosGetSystemLocale(char *outLocale, char *outCharset); +void taosSetSystemLocale(const char *inLocale, const char *inCharSet); #ifdef __cplusplus } -#endif /*_TD_COMMON_LOCALE_H_*/ - #endif + +#endif /*_TD_OS_LOCALE_H_*/ diff --git a/include/os/osString.h b/include/os/osString.h index 582411d444..88160dd69e 100644 --- a/include/os/osString.h +++ b/include/os/osString.h @@ -45,7 +45,6 @@ int32_t taosUcs4ToMbs(void *ucs4, int32_t ucs4_max_len, char *mbs); bool taosMbsToUcs4(const char *mbs, size_t mbs_len, char *ucs4, int32_t ucs4_max_len, int32_t *len); int32_t tasoUcs4Compare(void *f1_ucs4, void *f2_ucs4, int32_t bytes, int8_t ncharSize); bool taosValidateEncodec(const char *encodec); -char * taosCharsetReplace(char *charsetstr); #ifdef __cplusplus } diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index 3780131cda..64362603fe 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -42,8 +42,6 @@ extern int64_t tsOpenMax; extern int64_t tsStreamMax; extern int32_t tsNumOfCores; extern int32_t tsTotalMemoryMB; -extern char tsLocale[]; -extern char tsCharset[]; // default encode string diff --git a/include/os/osTimezone.h b/include/os/osTimezone.h index 6bf52a4108..ff015ef0b1 100644 --- a/include/os/osTimezone.h +++ b/include/os/osTimezone.h @@ -20,11 +20,11 @@ extern "C" { #endif -void osGetSystemTimezone(char *outTimezone); -void osSetSystemTimezone(const char *inTimezone, char *outTimezone, int8_t *outDaylight); +void taosGetSystemTimezone(char *outTimezone); +void taosSetSystemTimezone(const char *inTimezone, char *outTimezone, int8_t *outDaylight); #ifdef __cplusplus } #endif -#endif /*_TD_OS_SOCKET_H_*/ +#endif /*_TD_OS_TIMEZONE_H_*/ diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index e26dbb639a..b4c9efd77d 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -21,7 +21,6 @@ #include "tcompare.h" #include "tep.h" #include "tglobal.h" -#include "tlocale.h" #include "tlog.h" #include "tutil.h" #include "ulog.h" diff --git a/source/common/src/tlocale.c b/source/common/src/tlocale.c deleted file mode 100644 index 1f2b9cd4b1..0000000000 --- a/source/common/src/tlocale.c +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * This program is free software: you can use, redistribute, and/or modify - * it under the terms of the GNU Affero General Public License, version 3 - * or later ("AGPL"), as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -#define _DEFAULT_SOURCE -#include "os.h" -#include "ulog.h" -#include "tglobal.h" -#include "tutil.h" - -/** - * In some Linux systems, setLocale(LC_CTYPE, "") may return NULL, in which case the launch of - * both the TDengine Server and the Client may be interrupted. - * - * In case that the setLocale failed to be executed, the right charset needs to be set. - */ -void tsSetLocale() { - char *locale = setlocale(LC_CTYPE, tsLocale); - - // default locale or user specified locale is not valid, abort launch - if (locale == NULL) { - uError("Invalid locale:%s, please set the valid locale in config file", tsLocale); - } - - if (strlen(tsCharset) == 0) { - uError("failed to get charset, please set the valid charset in config file"); - exit(-1); - } - - if (!taosValidateEncodec(tsCharset)) { - uError("Invalid charset:%s, please set the valid charset in config file", tsCharset); - exit(-1); - } -} \ No newline at end of file diff --git a/source/dnode/mgmt/daemon/src/dmnCfg.c b/source/dnode/mgmt/daemon/src/dmnCfg.c index 5da3233b17..05edf83638 100644 --- a/source/dnode/mgmt/daemon/src/dmnCfg.c +++ b/source/dnode/mgmt/daemon/src/dmnCfg.c @@ -15,7 +15,6 @@ #define _DEFAULT_SOURCE #include "dmnInt.h" -#include "tlocale.h" static int32_t dmnAddEpCfg(SConfig *pCfg) { char defaultFqdn[TSDB_FQDN_LEN] = {0}; @@ -105,7 +104,6 @@ static int32_t dmnCheckCfg(SConfig *pCfg) { taosGetSystemInfo(); - tsSetLocale(); if (tsNumOfCores <= 0) { tsNumOfCores = 1; diff --git a/source/libs/config/src/config.c b/source/libs/config/src/config.c index f877b22046..b23826ceac 100644 --- a/source/libs/config/src/config.c +++ b/source/libs/config/src/config.c @@ -16,7 +16,6 @@ #define _DEFAULT_SOURCE #include "cfgInt.h" #include "tep.h" -#include "tlocale.h" #include "tmsg.h" #define CFG_NAME_PRINT_LEN 22 diff --git a/source/os/src/osEnv.c b/source/os/src/osEnv.c index 462f7044e0..8dd65fde14 100644 --- a/source/os/src/osEnv.c +++ b/source/os/src/osEnv.c @@ -22,7 +22,8 @@ char configDir[PATH_MAX] = {0}; SOsEnv *osEnv() { return &env; } void osInitImp() { - osGetSystemTimezone(env.timezone); + taosGetSystemLocale(env.locale, env.charset); + taosGetSystemTimezone(env.timezone); osSetTimezone(env.timezone); } @@ -45,10 +46,12 @@ char *osTempDir() { return env.tempDir; } char *osDataDir() { return env.dataDir; } char *osName() { return env.osName; } char *osTimezone() { return env.timezone; } +char *osLocale() { return env.locale; } +char *osCharset() { return env.charset; } int8_t osDaylight() { return env.daylight; } -void osSetTimezone(const char *timezone) { osSetSystemTimezone(timezone, env.timezone, &env.daylight); } +void osSetTimezone(const char *timezone) { taosSetSystemTimezone(timezone, env.timezone, &env.daylight); } #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) diff --git a/source/os/src/osLocale.c b/source/os/src/osLocale.c new file mode 100644 index 0000000000..e006a337a0 --- /dev/null +++ b/source/os/src/osLocale.c @@ -0,0 +1,194 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#define _DEFAULT_SOURCE +#include "osLocale.h" + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) +#if (_WIN64) +#include +#include +#include +#include +#include +#include +#pragma comment(lib, "Mswsock.lib ") +#endif +#include +#pragma warning(push) +#pragma warning(disable : 4091) +#include +#pragma warning(pop) +#elif defined(_TD_DARWIN_64) +#include +#include +#else +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +typedef struct CharsetPair { + char *oldCharset; + char *newCharset; +} CharsetPair; + +char *taosCharsetReplace(char *charsetstr) { + CharsetPair charsetRep[] = { + {"utf8", "UTF-8"}, + {"936", "CP936"}, + }; + + for (int32_t i = 0; i < tListLen(charsetRep); ++i) { + if (strcasecmp(charsetRep[i].oldCharset, charsetstr) == 0) { + return strdup(charsetRep[i].newCharset); + } + } + + return strdup(charsetstr); +} + +/** + * In some Linux systems, setLocale(LC_CTYPE, "") may return NULL, in which case the launch of + * both the TDengine Server and the Client may be interrupted. + * + * In case that the setLocale failed to be executed, the right charset needs to be set. + */ +void taosSetSystemLocale(const char *inLocale, const char *inCharSet) { + char *locale = setlocale(LC_CTYPE, inLocale); + + // default locale or user specified locale is not valid, abort launch + if (inLocale == NULL) { + printf("Invalid locale:%s, please set the valid locale in config file\n", inLocale); + } + + if (strlen(inCharSet) == 0) { + printf("failed to get charset, please set the valid charset in config file\n"); + exit(-1); + } + + if (!taosValidateEncodec(inCharSet)) { + printf("Invalid charset:%s, please set the valid charset in config file", inCharSet); + exit(-1); + } +} + +void taosGetSystemLocale(char *outLocale, char *outCharset) { +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) + char *locale = setlocale(LC_CTYPE, "chs"); + if (locale != NULL) { + tstrncpy(outLocale, locale, TD_LOCALE_LEN); + } + strcpy(outCharset, "cp936"); + +#elif defined(_TD_DARWIN_64) + /* + * originally from src/os/src/detail/osSysinfo.c + * POSIX format locale string: + * (Language Strings)_(Country/Region Strings).(code_page) + * + * example: en_US.UTF-8, zh_CN.GB18030, zh_CN.UTF-8, + * + * if user does not specify the locale in taos.cfg the program use default LC_CTYPE as system locale. + * + * In case of some CentOS systems, their default locale is "en_US.utf8", which is not valid code_page + * for libiconv that is employed to convert string in this system. This program will automatically use + * UTF-8 instead as the charset. + * + * In case of windows client, the locale string is not valid POSIX format, user needs to set the + * correct code_page for libiconv. Usually, the code_page of windows system with simple chinese is + * CP936, CP437 for English charset. + * + */ + + char sep = '.'; + char *locale = NULL; + + locale = setlocale(LC_CTYPE, ""); + if (locale == NULL) { + // printf("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno)); + strcpy(outLocale, "en_US.UTF-8"); + } else { + tstrncpy(outLocale, locale, TD_LOCALE_LEN); + // printf("locale not configured, set to system default:%s", outLocale); + } + + /* if user does not specify the charset, extract it from locale */ + char *str = strrchr(outLocale, sep); + if (str != NULL) { + str++; + + char *revisedCharset = taosCharsetReplace(str); + tstrncpy(outCharset, revisedCharset, TD_CHARSET_LEN); + + free(revisedCharset); + // printf("charset not configured, set to system default:%s", outCharset); + } else { + strcpy(outCharset, "UTF-8"); + // printf("can't get locale and charset from system, set it to UTF-8"); + } + +#else + /* + * POSIX format locale string: + * (Language Strings)_(Country/Region Strings).(code_page) + * + * example: en_US.UTF-8, zh_CN.GB18030, zh_CN.UTF-8, + * + * if user does not specify the locale in taos.cfg the program use default LC_CTYPE as system locale. + * + * In case of some CentOS systems, their default locale is "en_US.utf8", which is not valid code_page + * for libiconv that is employed to convert string in this system. This program will automatically use + * UTF-8 instead as the charset. + * + * In case of windows client, the locale string is not valid POSIX format, user needs to set the + * correct code_page for libiconv. Usually, the code_page of windows system with simple chinese is + * CP936, CP437 for English charset. + * + */ + char sep = '.'; + char *locale = NULL; + + locale = setlocale(LC_CTYPE, ""); + if (locale == NULL) { + // printf("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno)); + strcpy(outLocale, "en_US.UTF-8"); + } else { + tstrncpy(outLocale, locale, TD_LOCALE_LEN); + // printf("locale not configured, set to system default:%s", outLocale); + } + + // if user does not specify the charset, extract it from locale + char *str = strrchr(outLocale, sep); + if (str != NULL) { + str++; + + char *revisedCharset = taosCharsetReplace(str); + tstrncpy(outCharset, revisedCharset, TD_LOCALE_LEN); + + free(revisedCharset); + // printf("charset not configured, set to system default:%s", outCharset); + } else { + strcpy(outCharset, "UTF-8"); + // printf("can't get locale and charset from system, set it to UTF-8"); + } + +#endif +} diff --git a/source/os/src/osString.c b/source/os/src/osString.c index f714fd0dc4..88ea4b3e15 100644 --- a/source/os/src/osString.c +++ b/source/os/src/osString.c @@ -18,24 +18,6 @@ #include #include -typedef struct CharsetPair { - char *oldCharset; - char *newCharset; -} CharsetPair; - -char *taosCharsetReplace(char *charsetstr) { - CharsetPair charsetRep[] = { - { "utf8", "UTF-8" }, { "936", "CP936" }, - }; - - for (int32_t i = 0; i < tListLen(charsetRep); ++i) { - if (strcasecmp(charsetRep[i].oldCharset, charsetstr) == 0) { - return strdup(charsetRep[i].newCharset); - } - } - - return strdup(charsetstr); -} int64_t taosStr2int64(const char *str) { char *endptr = NULL; diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 439a8146ab..4a8cc5ecbc 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -21,9 +21,6 @@ int64_t tsPageSize = 0; int64_t tsOpenMax = 0; int64_t tsStreamMax = 0; int32_t tsNumOfCores = 1; -char tsLocale[TD_LOCALE_LEN] = {0}; -char tsCharset[TD_LOCALE_LEN] = {0}; // default encode string - #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) /* @@ -89,17 +86,6 @@ bool taosGetProcMemory(float *memoryUsedMB) { } - -static void taosGetSystemLocale() { - // get and set default locale - char *locale = setlocale(LC_CTYPE, "chs"); - if (locale != NULL) { - tstrncpy(tsLocale, locale, TD_LOCALE_LEN); - } - - strcpy(tsCharset, "cp936"); -} - int32_t taosGetCpuCores() { SYSTEM_INFO info; GetSystemInfo(&info); @@ -191,7 +177,6 @@ void taosGetSystemInfo() { taosGetCpuUsage(&tmp1, &tmp2); taosGetProcIO(&tmp1, &tmp2); - taosGetSystemLocale(); } void taosKillSystem() { @@ -263,52 +248,6 @@ char *taosGetCmdlineByPID(int pid) { return ""; } #include #include -/* - * originally from src/os/src/detail/osSysinfo.c - * POSIX format locale string: - * (Language Strings)_(Country/Region Strings).(code_page) - * - * example: en_US.UTF-8, zh_CN.GB18030, zh_CN.UTF-8, - * - * if user does not specify the locale in taos.cfg the program use default LC_CTYPE as system locale. - * - * In case of some CentOS systems, their default locale is "en_US.utf8", which is not valid code_page - * for libiconv that is employed to convert string in this system. This program will automatically use - * UTF-8 instead as the charset. - * - * In case of windows client, the locale string is not valid POSIX format, user needs to set the - * correct code_page for libiconv. Usually, the code_page of windows system with simple chinese is - * CP936, CP437 for English charset. - * - */ -static void taosGetSystemLocale() { // get and set default locale - char sep = '.'; - char *locale = NULL; - - locale = setlocale(LC_CTYPE, ""); - if (locale == NULL) { - //printf("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno)); - strcpy(tsLocale, "en_US.UTF-8"); - } else { - tstrncpy(tsLocale, locale, TD_LOCALE_LEN); - //printf("locale not configured, set to system default:%s", tsLocale); - } - - /* if user does not specify the charset, extract it from locale */ - char *str = strrchr(tsLocale, sep); - if (str != NULL) { - str++; - - char *revisedCharset = taosCharsetReplace(str); - tstrncpy(tsCharset, revisedCharset, TD_LOCALE_LEN); - - free(revisedCharset); - //printf("charset not configured, set to system default:%s", tsCharset); - } else { - strcpy(tsCharset, "UTF-8"); - //printf("can't get locale and charset from system, set it to UTF-8"); - } -} void taosKillSystem() { //printf("function taosKillSystem, exit!"); @@ -325,8 +264,6 @@ void taosGetSystemInfo() { long page_size = sysconf(_SC_PAGESIZE); tsTotalMemoryMB = physical_pages * page_size / (1024 * 1024); tsPageSize = page_size; - - taosGetSystemLocale(); } bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { @@ -563,51 +500,6 @@ static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { return true; } -/* - * POSIX format locale string: - * (Language Strings)_(Country/Region Strings).(code_page) - * - * example: en_US.UTF-8, zh_CN.GB18030, zh_CN.UTF-8, - * - * if user does not specify the locale in taos.cfg the program use default LC_CTYPE as system locale. - * - * In case of some CentOS systems, their default locale is "en_US.utf8", which is not valid code_page - * for libiconv that is employed to convert string in this system. This program will automatically use - * UTF-8 instead as the charset. - * - * In case of windows client, the locale string is not valid POSIX format, user needs to set the - * correct code_page for libiconv. Usually, the code_page of windows system with simple chinese is - * CP936, CP437 for English charset. - * - */ -static void taosGetSystemLocale() { // get and set default locale - char sep = '.'; - char *locale = NULL; - - locale = setlocale(LC_CTYPE, ""); - if (locale == NULL) { - //printf("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno)); - strcpy(tsLocale, "en_US.UTF-8"); - } else { - tstrncpy(tsLocale, locale, TD_LOCALE_LEN); - //printf("locale not configured, set to system default:%s", tsLocale); - } - - // if user does not specify the charset, extract it from locale - char *str = strrchr(tsLocale, sep); - if (str != NULL) { - str++; - - char *revisedCharset = taosCharsetReplace(str); - tstrncpy(tsCharset, revisedCharset, TD_LOCALE_LEN); - - free(revisedCharset); - //printf("charset not configured, set to system default:%s", tsCharset); - } else { - strcpy(tsCharset, "UTF-8"); - //printf("can't get locale and charset from system, set it to UTF-8"); - } -} int32_t taosGetCpuCores() { return (int32_t)sysconf(_SC_NPROCESSORS_ONLN); } @@ -836,7 +728,6 @@ void taosGetSystemInfo() { taosGetCpuUsage(&tmp1, &tmp2); taosGetProcIO(&tmp1, &tmp2); - taosGetSystemLocale(); } void taosKillSystem() { diff --git a/source/os/src/osTimeZone1.c b/source/os/src/osTimezone.c similarity index 97% rename from source/os/src/osTimeZone1.c rename to source/os/src/osTimezone.c index 84e86662d3..a0ea01596f 100644 --- a/source/os/src/osTimeZone1.c +++ b/source/os/src/osTimezone.c @@ -45,7 +45,7 @@ #include #endif -void osSetSystemTimezone(const char *inTimezone, char *outTimezone, int8_t *outDaylight) { +void taosSetSystemTimezone(const char *inTimezone, char *outTimezone, int8_t *outDaylight) { #ifdef WINDOWS char winStr[TD_LOCALE_LEN * 2]; sprintf(winStr, "TZ=%s", inTimezone); @@ -84,7 +84,7 @@ void osSetSystemTimezone(const char *inTimezone, char *outTimezone, int8_t *outD *outDaylight = daylight; } -void osGetSystemTimezone(char *outTimezone) { +void taosGetSystemTimezone(char *outTimezone) { #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) char *tz = getenv("TZ"); if (tz == NULL || strlen(tz) == 0) { From 0d0841d103e7cabc289c67e37b2d9dbb29f02b0c Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 24 Feb 2022 14:16:42 +0800 Subject: [PATCH 7/7] config --- include/os/osDir.h | 2 +- include/os/osEnv.h | 47 +++++++-------- source/client/src/clientCfg.c | 2 +- source/dnode/mgmt/daemon/src/dmnCfg.c | 9 ++- source/dnode/mgmt/daemon/src/dmnLog.c | 5 +- source/dnode/mgmt/impl/test/sut/src/sut.cpp | 2 +- source/libs/index/test/fstUT.cc | 2 +- source/libs/transport/test/transUT.cc | 2 +- source/os/src/osDir.c | 2 +- source/os/src/osEnv.c | 64 ++++++++++++--------- tools/shell/src/shellLinux.c | 1 - 11 files changed, 71 insertions(+), 67 deletions(-) diff --git a/include/os/osDir.h b/include/os/osDir.h index 01ec24f235..e328c42063 100644 --- a/include/os/osDir.h +++ b/include/os/osDir.h @@ -23,7 +23,7 @@ extern "C" { void taosRemoveDir(const char *dirname); int32_t taosDirExist(char *dirname); int32_t taosMkDir(const char *dirname); -void taosRemoveOldFiles(char *dirname, int32_t keepDays); +void taosRemoveOldFiles(const char *dirname, int32_t keepDays); int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen); int32_t taosRealPath(char *dirname, int32_t maxlen); diff --git a/include/os/osEnv.h b/include/os/osEnv.h index c7c8ba9053..29898953e6 100644 --- a/include/os/osEnv.h +++ b/include/os/osEnv.h @@ -22,36 +22,31 @@ extern "C" { #endif -typedef struct SOsEnv { - char dataDir[PATH_MAX]; - char logDir[PATH_MAX]; - char tempDir[PATH_MAX]; - SDiskSpace dataSpace; - SDiskSpace logSpace; - SDiskSpace tempSpace; - char osName[16]; - char timezone[TD_TIMEZONE_LEN]; - char locale[TD_LOCALE_LEN]; - char charset[TD_CHARSET_LEN]; - int8_t daylight; -} SOsEnv; +typedef struct SOsEnv SOsEnv; extern char configDir[]; -void osInit(); -SOsEnv *osEnv(); -void osUpdate(); -bool osLogSpaceAvailable(); -char *osLogDir(); -char *osTempDir(); -char *osDataDir(); -char *osName(); -char *osTimezone(); -int8_t osDaylight(); -char *osLocale(); -char *osCharset(); +void osInit(); +void osUpdate(); -void osSetTimezone(const char*timezone); +bool osLogSpaceAvailable(); +int8_t osDaylight(); + +const char *osLogDir(); +const char *osTempDir(); +const char *osDataDir(); +const char *osName(); +const char *osTimezone(); +const char *osLocale(); +const char *osCharset(); + +void osSetLogDir(const char *logDir); +void osSetTempDir(const char *tempDir); +void osSetDataDir(const char *dataDir); +void osSetLogReservedSpace(float sizeInGB); +void osSetTempReservedSpace(float sizeInGB); +void osSetDataReservedSpace(float sizeInGB); +void osSetTimezone(const char *timezone); #ifdef __cplusplus } diff --git a/source/client/src/clientCfg.c b/source/client/src/clientCfg.c index f2020ddf95..d1dafbed5c 100644 --- a/source/client/src/clientCfg.c +++ b/source/client/src/clientCfg.c @@ -67,7 +67,7 @@ static int32_t tscAddLogCfg(SConfig *pCfg) { } static int32_t tscSetLogCfg(SConfig *pCfg) { - tstrncpy(osLogDir(), cfgGetItem(pCfg, "logDir")->str, PATH_MAX); + osSetLogDir(cfgGetItem(pCfg, "logDir")->str); tsAsyncLog = cfgGetItem(pCfg, "asyncLog")->bval; tsNumOfLogLines = cfgGetItem(pCfg, "numOfLogLines")->i32; tsLogKeepDays = cfgGetItem(pCfg, "logKeepDays")->i32; diff --git a/source/dnode/mgmt/daemon/src/dmnCfg.c b/source/dnode/mgmt/daemon/src/dmnCfg.c index 05edf83638..65c3e67d97 100644 --- a/source/dnode/mgmt/daemon/src/dmnCfg.c +++ b/source/dnode/mgmt/daemon/src/dmnCfg.c @@ -46,11 +46,10 @@ static int32_t dmnAddDirCfg(SConfig *pCfg) { } static int32_t dmnCheckDirCfg(SConfig *pCfg) { - SOsEnv *pEnv = osEnv(); - tstrncpy(pEnv->dataDir, cfgGetItem(pCfg, "dataDir")->str, PATH_MAX); - tstrncpy(pEnv->tempDir, cfgGetItem(pCfg, "tempDir")->str, PATH_MAX); - pEnv->dataSpace.reserved = cfgGetItem(pCfg, "minimalDataDirGB")->fval; - pEnv->tempSpace.reserved = cfgGetItem(pCfg, "minimalTempDirGB")->fval; + osSetDataDir(cfgGetItem(pCfg, "dataDir")->str); + osSetTempDir(cfgGetItem(pCfg, "tempDir")->str); + osSetTempReservedSpace(cfgGetItem(pCfg, "minimalDataDirGB")->fval); + osSetDataReservedSpace(cfgGetItem(pCfg, "minimalTempDirGB")->fval); return 0; } diff --git a/source/dnode/mgmt/daemon/src/dmnLog.c b/source/dnode/mgmt/daemon/src/dmnLog.c index 5079cd907a..147357c845 100644 --- a/source/dnode/mgmt/daemon/src/dmnLog.c +++ b/source/dnode/mgmt/daemon/src/dmnLog.c @@ -41,9 +41,8 @@ int32_t dmnAddLogCfg(SConfig *pCfg) { } int32_t dmnSetLogCfg(SConfig *pCfg) { - SOsEnv *pEnv = osEnv(); - tstrncpy(pEnv->logDir, cfgGetItem(pCfg, "logDir")->str, PATH_MAX); - pEnv->logSpace.reserved = cfgGetItem(pCfg, "minimalLogDirGB")->fval; + osSetLogDir(cfgGetItem(pCfg, "logDir")->str); + osSetLogReservedSpace(cfgGetItem(pCfg, "minimalLogDirGB")->fval); tsAsyncLog = cfgGetItem(pCfg, "asyncLog")->bval; tsNumOfLogLines = cfgGetItem(pCfg, "numOfLogLines")->i32; tsLogKeepDays = cfgGetItem(pCfg, "logKeepDays")->i32; diff --git a/source/dnode/mgmt/impl/test/sut/src/sut.cpp b/source/dnode/mgmt/impl/test/sut/src/sut.cpp index 53b27ffaff..5d2abd86c3 100644 --- a/source/dnode/mgmt/impl/test/sut/src/sut.cpp +++ b/source/dnode/mgmt/impl/test/sut/src/sut.cpp @@ -33,7 +33,7 @@ void Testbase::InitLog(const char* path) { taosRemoveDir(path); taosMkDir(path); - tstrncpy(osLogDir(), path, PATH_MAX); + osSetLogDir(path); if (taosInitLog("taosdlog", 1) != 0) { printf("failed to init log file\n"); } diff --git a/source/libs/index/test/fstUT.cc b/source/libs/index/test/fstUT.cc index af8ee747bf..0e16f57f58 100644 --- a/source/libs/index/test/fstUT.cc +++ b/source/libs/index/test/fstUT.cc @@ -30,7 +30,7 @@ static void EnvInit() { taosRemoveDir(path.c_str()); taosMkDir(path.c_str()); // init log file - tstrncpy(osLogDir(), path.c_str(), PATH_MAX); + osSetLogDir( path.c_str()); if (taosInitLog("tindex.idx", 1) != 0) { printf("failed to init log"); } diff --git a/source/libs/transport/test/transUT.cc b/source/libs/transport/test/transUT.cc index f2b1764dc6..8a8a6de5e1 100644 --- a/source/libs/transport/test/transUT.cc +++ b/source/libs/transport/test/transUT.cc @@ -155,7 +155,7 @@ class TransObj { taosRemoveDir(path.c_str()); taosMkDir(path.c_str()); - tstrncpy(osLogDir(), path.c_str(), PATH_MAX); + osSetLogDir(path.c_str()); if (taosInitLog("taosdlog", 1) != 0) { printf("failed to init log file\n"); } diff --git a/source/os/src/osDir.c b/source/os/src/osDir.c index 8fee6a6f7d..c464073e5f 100644 --- a/source/os/src/osDir.c +++ b/source/os/src/osDir.c @@ -71,7 +71,7 @@ int32_t taosMkDir(const char *dirname) { return code; } -void taosRemoveOldFiles(char *dirname, int32_t keepDays) { +void taosRemoveOldFiles(const char *dirname, int32_t keepDays) { DIR *dir = opendir(dirname); if (dir == NULL) return; diff --git a/source/os/src/osEnv.c b/source/os/src/osEnv.c index 8dd65fde14..6fad7e4f32 100644 --- a/source/os/src/osEnv.c +++ b/source/os/src/osEnv.c @@ -16,8 +16,24 @@ #define _DEFAULT_SOURCE #include "osEnv.h" -SOsEnv env = {0}; -char configDir[PATH_MAX] = {0}; +extern void taosWinSocketInit(); +char configDir[PATH_MAX] = {0}; + +typedef struct SOsEnv { + char dataDir[PATH_MAX]; + char logDir[PATH_MAX]; + char tempDir[PATH_MAX]; + SDiskSpace dataSpace; + SDiskSpace logSpace; + SDiskSpace tempSpace; + char osName[16]; + char timezone[TD_TIMEZONE_LEN]; + char locale[TD_LOCALE_LEN]; + char charset[TD_CHARSET_LEN]; + int8_t daylight; +} SOsEnv; + +static SOsEnv env = {0}; SOsEnv *osEnv() { return &env; } @@ -39,26 +55,29 @@ void osUpdate() { } } -bool osLogSpaceAvailable() { return env.logSpace.reserved < env.logSpace.size.avail; } - -char *osLogDir() { return env.logDir; } -char *osTempDir() { return env.tempDir; } -char *osDataDir() { return env.dataDir; } -char *osName() { return env.osName; } -char *osTimezone() { return env.timezone; } -char *osLocale() { return env.locale; } -char *osCharset() { return env.charset; } - +bool osLogSpaceAvailable() { return env.logSpace.reserved <= env.logSpace.size.avail; } int8_t osDaylight() { return env.daylight; } +const char *osLogDir() { return env.logDir; } +const char *osTempDir() { return env.tempDir; } +const char *osDataDir() { return env.dataDir; } +const char *osName() { return env.osName; } +const char *osTimezone() { return env.timezone; } +const char *osLocale() { return env.locale; } +const char *osCharset() { return env.charset; } + +void osSetLogDir(const char *logDir) { tstrncpy(env.logDir, logDir, PATH_MAX); } +void osSetTempDir(const char *tempDir) { tstrncpy(env.tempDir, tempDir, PATH_MAX); } +void osSetDataDir(const char *dataDir) { tstrncpy(env.dataDir, dataDir, PATH_MAX); } +void osSetLogReservedSpace(float sizeInGB) { env.logSpace.reserved = sizeInGB; } +void osSetTempReservedSpace(float sizeInGB) { env.tempSpace.reserved = sizeInGB; } +void osSetDataReservedSpace(float sizeInGB) { env.dataSpace.reserved = sizeInGB; } void osSetTimezone(const char *timezone) { taosSetSystemTimezone(timezone, env.timezone, &env.daylight); } -#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) - -extern taosWinSocketInit(); - void osInit() { srand(taosSafeRand()); + +#if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) taosWinSocketInit(); const char *tmpDir = getenv("tmp"); @@ -74,27 +93,20 @@ void osInit() { strcpy(env.logDir, "C:\\TDengine\\log"); strcpy(env.tempDir, "C:\\Windows\\Temp"); strcpy(env.osName, "Windows"); -} #elif defined(_TD_DARWIN_64) - -void osInit() { - srand(taosSafeRand()); strcpy(configDir, "/tmp/taosd"); strcpy(env.dataDir, "/usr/local/var/lib/taos"); strcpy(env.logDir, "/usr/local/var/log/taos"); strcpy(env.tempDir, "/usr/local/etc/taos"); strcpy(env.osName, "Darwin"); -} -#else -void osInit() { - srand(taosSafeRand()); +#else strcpy(configDir, "/etc/taos"); strcpy(env.dataDir, "/var/lib/taos"); strcpy(env.logDir, "/var/log/taos"); strcpy(env.tempDir, "/tmp"); strcpy(env.osName, "Linux"); -} -#endif \ No newline at end of file +#endif +} \ No newline at end of file diff --git a/tools/shell/src/shellLinux.c b/tools/shell/src/shellLinux.c index 0526ebb442..caba986ce7 100644 --- a/tools/shell/src/shellLinux.c +++ b/tools/shell/src/shellLinux.c @@ -184,7 +184,6 @@ static void parse_args( for (int i = 1; i < argc; i++) { if ((strncmp(argv[i], "-p", 2) == 0) || (strncmp(argv[i], "--password", 10) == 0)) { - strcpy(osName(), "Linux"); printf(LINUXCLIENT_VERSION, osName(), taos_get_client_info()); if ((strlen(argv[i]) == 2) || (strncmp(argv[i], "--password", 10) == 0)) {