diff --git a/include/libs/config/config.h b/include/libs/config/config.h index 51fd83c49f8a47867369684ba9abe6b1c038079a..d603595cbf07c614512f307957438feb21abdde5 100644 --- a/include/libs/config/config.h +++ b/include/libs/config/config.h @@ -23,6 +23,8 @@ extern "C" { #endif +#define CFG_NAME_MAX_LEN 128 + typedef enum { CFG_STYPE_DEFAULT, CFG_STYPE_CFG_FILE, @@ -48,12 +50,12 @@ typedef enum { typedef enum { CFG_UTYPE_NONE, - CFG_UTYPE_PERCENT, CFG_UTYPE_GB, CFG_UTYPE_MB, CFG_UTYPE_BYTE, CFG_UTYPE_SECOND, - CFG_UTYPE_MS + CFG_UTYPE_MS, + CFG_UTYPE_PERCENT } ECfgUnitType; typedef struct SConfigItem { diff --git a/include/os/osDir.h b/include/os/osDir.h index 35c294af39bf49967ab6fa1f15bc3bf35a523911..01ec24f23569e577ead33d9b3400078881b37e97 100644 --- a/include/os/osDir.h +++ b/include/os/osDir.h @@ -24,7 +24,7 @@ void taosRemoveDir(const char *dirname); int32_t taosDirExist(char *dirname); int32_t taosMkDir(const char *dirname); void taosRemoveOldFiles(char *dirname, int32_t keepDays); -int32_t taosExpandDir(char *dirname, char *outname, int32_t maxlen); +int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen); int32_t taosRealPath(char *dirname, int32_t maxlen); #ifdef __cplusplus diff --git a/include/os/osSocket.h b/include/os/osSocket.h index d76b286d651b0124fd5db4fd4018f566f498a0d0..af99e4b47419350e408d8809e8d593d0b2a3996a 100644 --- a/include/os/osSocket.h +++ b/include/os/osSocket.h @@ -55,7 +55,7 @@ void taosSetMaskSIGPIPE(); int32_t taosSetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t optlen); int32_t taosGetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *optval, int32_t *optlen); -uint32_t taosInetAddr(char *ipAddr); +uint32_t taosInetAddr(const char *ipAddr); const char *taosInetNtoa(struct in_addr ipInt); #if (defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) diff --git a/source/libs/config/inc/cfgInt.h b/source/libs/config/inc/cfgInt.h index 6f78b97435e5903847e1c25a16f287237868da98..0b620182220b6f8b16127498139619f1c58fc936 100644 --- a/source/libs/config/inc/cfgInt.h +++ b/source/libs/config/inc/cfgInt.h @@ -20,6 +20,7 @@ #include "config.h" #include "taoserror.h" #include "thash.h" +#include "tutil.h" #include "ulog.h" #ifdef __cplusplus diff --git a/source/libs/config/src/config.c b/source/libs/config/src/config.c index 74c03bfb3df7316f63e3fdd8a7057f7d3a123c9c..41d59d20a5ae4fd57ae2ee66629786860d4da05f 100644 --- a/source/libs/config/src/config.c +++ b/source/libs/config/src/config.c @@ -64,8 +64,7 @@ SConfigItem *cfgIterate(SConfig *pConfig, SConfigItem *pIter) { return taosHashI void cfgCancelIterate(SConfig *pConfig, SConfigItem *pIter) { return taosHashCancelIterate(pConfig->hash, pIter); } - -int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) { +static int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) { bool tmp = false; if (strcasecmp(value, "true") == 0) { tmp = true; @@ -73,12 +72,13 @@ int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) { if (atoi(value) > 0) { tmp = true; } + pItem->boolVal = tmp; pItem->stype = stype; return 0; } -int32_t cfgSetInt8(SConfigItem *pItem, const char *value, ECfgSrcType stype) { +static int32_t cfgSetInt8(SConfigItem *pItem, const char *value, ECfgSrcType stype) { int8_t ival = (int8_t)atoi(value); if (ival < pItem->minIntVal || ival > pItem->maxIntVal) { uError("cfg:%s, type:%s src:%s value:%d out of range[%" PRId64 ", %" PRId64 "], use last src:%s value:%d", @@ -87,12 +87,13 @@ int32_t cfgSetInt8(SConfigItem *pItem, const char *value, ECfgSrcType stype) { terrno = TSDB_CODE_OUT_OF_RANGE; return -1; } + pItem->int8Val = ival; pItem->stype = stype; return 0; } -int32_t cfgSetUInt16(SConfigItem *pItem, const char *value, ECfgSrcType stype) { +static int32_t cfgSetUInt16(SConfigItem *pItem, const char *value, ECfgSrcType stype) { uint16_t ival = (uint16_t)atoi(value); if (ival < pItem->minIntVal || ival > pItem->maxIntVal) { uError("cfg:%s, type:%s src:%s value:%d out of range[%" PRId64 ", %" PRId64 "], use last src:%s value:%d", @@ -101,12 +102,13 @@ int32_t cfgSetUInt16(SConfigItem *pItem, const char *value, ECfgSrcType stype) { terrno = TSDB_CODE_OUT_OF_RANGE; return -1; } + pItem->uint16Val = ival; pItem->stype = stype; return 0; } -int32_t cfgSetInt32(SConfigItem *pItem, const char *value, ECfgSrcType stype) { +static int32_t cfgSetInt32(SConfigItem *pItem, const char *value, ECfgSrcType stype) { int32_t ival = (int32_t)atoi(value); if (ival < pItem->minIntVal || ival > pItem->maxIntVal) { uError("cfg:%s, type:%s src:%s value:%d out of range[%" PRId64 ", %" PRId64 "], use last src:%s value:%d", @@ -115,26 +117,29 @@ int32_t cfgSetInt32(SConfigItem *pItem, const char *value, ECfgSrcType stype) { terrno = TSDB_CODE_OUT_OF_RANGE; return -1; } + pItem->int32Val = ival; pItem->stype = stype; return 0; } -int32_t cfgSetInt64(SConfigItem *pItem, const char *value, ECfgSrcType stype) { +static int32_t cfgSetInt64(SConfigItem *pItem, const char *value, ECfgSrcType stype) { int64_t ival = (int64_t)atoi(value); if (ival < pItem->minIntVal || ival > pItem->maxIntVal) { - uError("cfg:%s, type:%s src:%s value:%d out of range[%" PRId64 ", %" PRId64 "], use last src:%s value:%d", + uError("cfg:%s, type:%s src:%s value:%" PRId64 " out of range[%" PRId64 ", %" PRId64 + "], use last src:%s value:%" PRId64, pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->minIntVal, pItem->maxIntVal, cfgStypeStr(pItem->stype), pItem->int64Val); terrno = TSDB_CODE_OUT_OF_RANGE; return -1; } + pItem->int64Val = ival; pItem->stype = stype; return 0; } -int32_t cfgSetFloat(SConfigItem *pItem, const char *value, ECfgSrcType stype) { +static int32_t cfgSetFloat(SConfigItem *pItem, const char *value, ECfgSrcType stype) { float fval = (float)atof(value); if (fval < pItem->minFloatVal || fval > pItem->maxFloatVal) { uError("cfg:%s, type:%s src:%s value:%f out of range[%f, %f], use last src:%s value:%f", pItem->name, @@ -143,47 +148,51 @@ int32_t cfgSetFloat(SConfigItem *pItem, const char *value, ECfgSrcType stype) { terrno = TSDB_CODE_OUT_OF_RANGE; return -1; } + pItem->floatVal = fval; pItem->stype = stype; return 0; } -int32_t cfgSetString(SConfigItem *pItem, const char *value, ECfgSrcType stype) { +static int32_t cfgSetString(SConfigItem *pItem, const char *value, ECfgSrcType stype) { char *tmp = strdup(value); if (tmp == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s, use last src:%s value:%s", pItem->name, - cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), terrstr(), cfgStypeStr(pItem->stype), pItem->floatVal); + cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->strVal); return -1; } + free(pItem->strVal); pItem->strVal = tmp; pItem->stype = stype; return 0; } -int32_t cfgSetIpStr(SConfigItem *pItem, const char *value, ECfgSrcType stype) { +static int32_t cfgSetIpStr(SConfigItem *pItem, const char *value, ECfgSrcType stype) { char *tmp = strdup(value); if (tmp == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s, use last src:%s value:%s", pItem->name, - cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), terrstr(), cfgStypeStr(pItem->stype), pItem->floatVal); + cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->strVal); return -1; } + free(pItem->strVal); pItem->strVal = tmp; pItem->stype = stype; return 0; } -int32_t cfgSetDir(SConfigItem *pItem, const char *value, ECfgSrcType stype) { +static int32_t cfgSetDir(SConfigItem *pItem, const char *value, ECfgSrcType stype) { char *tmp = strdup(value); if (tmp == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s, use last src:%s value:%s", pItem->name, - cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), terrstr(), cfgStypeStr(pItem->stype), pItem->floatVal); + cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->strVal); return -1; } + free(pItem->strVal); pItem->strVal = tmp; pItem->stype = stype; @@ -214,7 +223,7 @@ int32_t cfgSetItem(SConfig *pConfig, const char *name, const char *value, ECfgSr case CFG_DTYPE_IPSTR: return cfgSetIpStr(pItem, value, stype); case CFG_DTYPE_DIR: - return cfgSetFqdn(pItem, value, stype); + return cfgSetDir(pItem, value, stype); case CFG_DTYPE_NONE: default: break; @@ -225,8 +234,9 @@ int32_t cfgSetItem(SConfig *pConfig, const char *name, const char *value, ECfgSr } SConfigItem *cfgGetItem(SConfig *pConfig, const char *name) { - char lowcaseName[128] = 0; - memcpy(lowcaseName, name, 127); + char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0}; + memcpy(lowcaseName, name, CFG_NAME_MAX_LEN); + strntolower(lowcaseName, name, CFG_NAME_MAX_LEN); SConfigItem *pItem = taosHashGet(pConfig->hash, lowcaseName, strlen(lowcaseName) + 1); if (pItem == NULL) { @@ -245,8 +255,10 @@ static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name return -1; } - char lowcaseName[128] = 0; - memcpy(lowcaseName, name, 127); + char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0}; + memcpy(lowcaseName, name, CFG_NAME_MAX_LEN); + strntolower(lowcaseName, name, CFG_NAME_MAX_LEN); + if (taosHashPut(pConfig->hash, lowcaseName, strlen(lowcaseName) + 1, pItem, sizeof(SConfigItem)) != 0) { if (pItem->dtype == CFG_DTYPE_STRING) { free(pItem->strVal); @@ -412,6 +424,10 @@ const char *cfgStypeStr(ECfgSrcType type) { return "env_var"; case CFG_STYPE_APOLLO_URL: return "apollo_url"; + case CFG_STYPE_ARG_LIST: + return "arg_list"; + case CFG_STYPE_API_OPTION: + return "api_option"; default: return "invalid"; } diff --git a/source/libs/config/test/cfgTest.cpp b/source/libs/config/test/cfgTest.cpp index b14c0bb2018b6db10c36aec5f466616078a65b24..8610fa3643ee032d1d29ba151f10eca9eb752798 100644 --- a/source/libs/config/test/cfgTest.cpp +++ b/source/libs/config/test/cfgTest.cpp @@ -34,12 +34,13 @@ TEST_F(CfgTest, 02_Str) { EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ENV_FILE), "env_file"); EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ENV_VAR), "env_var"); EXPECT_STREQ(cfgStypeStr(CFG_STYPE_APOLLO_URL), "apollo_url"); + EXPECT_STREQ(cfgStypeStr(CFG_STYPE_ARG_LIST), "arg_list"); + EXPECT_STREQ(cfgStypeStr(CFG_STYPE_API_OPTION), "api_option"); EXPECT_STREQ(cfgStypeStr(ECfgSrcType(1024)), "invalid"); EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_NONE), "none"); EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_BOOL), "bool"); EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT8), "int8"); - EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_UINT16), "uint16"); EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT32), "int32"); EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_INT64), "int64"); @@ -50,12 +51,12 @@ TEST_F(CfgTest, 02_Str) { EXPECT_STREQ(cfgDtypeStr(ECfgDataType(1024)), "invalid"); EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_NONE), ""); - EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_PERCENT), "(%)"); EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_GB), "(GB)"); EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_MB), "(Mb)"); EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_BYTE), "(byte)"); EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_SECOND), "(s)"); EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_MS), "(ms)"); + EXPECT_STREQ(cfgUtypeStr(CFG_UTYPE_PERCENT), "(%)"); EXPECT_STREQ(cfgUtypeStr(ECfgUnitType(1024)), "invalid"); } @@ -64,11 +65,11 @@ TEST_F(CfgTest, 02_Basic) { ASSERT_NE(pConfig, nullptr); EXPECT_EQ(cfgAddBool(pConfig, "test_bool", 0, CFG_UTYPE_NONE), 0); - EXPECT_EQ(cfgAddInt8(pConfig, "test_int8", 1, CFG_UTYPE_GB), 0); - EXPECT_EQ(cfgAddUInt16(pConfig, "test_uint16", 2, CFG_UTYPE_SECOND), 0); - EXPECT_EQ(cfgAddInt32(pConfig, "test_int32", 3, CFG_UTYPE_MS), 0); - EXPECT_EQ(cfgAddInt64(pConfig, "test_int64", 4, CFG_UTYPE_NONE), 0); - EXPECT_EQ(cfgAddFloat(pConfig, "test_float", 5, CFG_UTYPE_NONE), 0); + EXPECT_EQ(cfgAddInt8(pConfig, "test_int8", 1, 0, 16, CFG_UTYPE_GB), 0); + EXPECT_EQ(cfgAddUInt16(pConfig, "test_uint16", 2, 0, 16, CFG_UTYPE_MB), 0); + EXPECT_EQ(cfgAddInt32(pConfig, "test_int32", 3, 0, 16, CFG_UTYPE_BYTE), 0); + EXPECT_EQ(cfgAddInt64(pConfig, "test_int64", 4, 0, 16, CFG_UTYPE_SECOND), 0); + EXPECT_EQ(cfgAddFloat(pConfig, "test_float", 5, 0, 16, CFG_UTYPE_MS), 0); EXPECT_EQ(cfgAddString(pConfig, "test_string", "6", CFG_UTYPE_NONE), 0); EXPECT_EQ(cfgAddIpStr(pConfig, "test_ipstr", "192.168.0.1", CFG_UTYPE_NONE), 0); EXPECT_EQ(cfgAddDir(pConfig, "test_dir", "/tmp", CFG_UTYPE_NONE), 0); @@ -83,16 +84,16 @@ TEST_F(CfgTest, 02_Basic) { printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->boolVal); break; case CFG_DTYPE_INT8: - printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->uint8Val); + printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->int8Val); break; case CFG_DTYPE_UINT16: - printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->int16Val); + printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->uint16Val); break; case CFG_DTYPE_INT32: - printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->uint32Val); + printf("index:%d, cfg:%s value:%d\n", size, pItem->name, pItem->int32Val); break; case CFG_DTYPE_INT64: - printf("index:%d, cfg:%s value:%" PRIu64 "\n", size, pItem->name, pItem->uint64Val); + printf("index:%d, cfg:%s value:%" PRId64 "\n", size, pItem->name, pItem->int64Val); break; case CFG_DTYPE_FLOAT: printf("index:%d, cfg:%s value:%f\n", size, pItem->name, pItem->floatVal); @@ -133,28 +134,28 @@ TEST_F(CfgTest, 02_Basic) { pItem = cfgGetItem(pConfig, "test_uint16"); EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT); - EXPECT_EQ(pItem->utype, CFG_UTYPE_SECOND); + EXPECT_EQ(pItem->utype, CFG_UTYPE_MB); EXPECT_EQ(pItem->dtype, CFG_DTYPE_UINT16); EXPECT_STREQ(pItem->name, "test_uint16"); EXPECT_EQ(pItem->uint16Val, 2); pItem = cfgGetItem(pConfig, "test_int32"); EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT); - EXPECT_EQ(pItem->utype, CFG_UTYPE_MS); + EXPECT_EQ(pItem->utype, CFG_UTYPE_BYTE); EXPECT_EQ(pItem->dtype, CFG_DTYPE_INT32); EXPECT_STREQ(pItem->name, "test_int32"); EXPECT_EQ(pItem->int32Val, 3); pItem = cfgGetItem(pConfig, "test_int64"); EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT); - EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE); + EXPECT_EQ(pItem->utype, CFG_UTYPE_SECOND); EXPECT_EQ(pItem->dtype, CFG_DTYPE_INT64); EXPECT_STREQ(pItem->name, "test_int64"); EXPECT_EQ(pItem->int64Val, 4); pItem = cfgGetItem(pConfig, "test_float"); EXPECT_EQ(pItem->stype, CFG_STYPE_DEFAULT); - EXPECT_EQ(pItem->utype, CFG_UTYPE_NONE); + EXPECT_EQ(pItem->utype, CFG_UTYPE_MS); EXPECT_EQ(pItem->dtype, CFG_DTYPE_FLOAT); EXPECT_STREQ(pItem->name, "test_float"); EXPECT_EQ(pItem->floatVal, 5); diff --git a/source/os/src/osDir.c b/source/os/src/osDir.c index 8999646f6a7ad8b61b53f51e67d60610c0ee2028..8fee6a6f7dab26d7a10586b58e67f0762c79b416 100644 --- a/source/os/src/osDir.c +++ b/source/os/src/osDir.c @@ -114,7 +114,7 @@ void taosRemoveOldFiles(char *dirname, int32_t keepDays) { rmdir(dirname); } -int32_t taosExpandDir(char *dirname, char *outname, int32_t maxlen) { +int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen) { wordexp_t full_path; if (0 != wordexp(dirname, &full_path, 0)) { //printf("failed to expand path:%s since %s", dirname, strerror(errno)); diff --git a/source/os/src/osSocket.c b/source/os/src/osSocket.c index e12f9493b3159e85593f1723ab018ff047614851..8afb4ae8b71483172b6505c8179f8038655b66c5 100644 --- a/source/os/src/osSocket.c +++ b/source/os/src/osSocket.c @@ -150,7 +150,7 @@ int32_t taosGetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *op #if !((defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32)) && defined(_MSC_VER)) -uint32_t taosInetAddr(char *ipAddr) { return inet_addr(ipAddr); } +uint32_t taosInetAddr(const char *ipAddr) { return inet_addr(ipAddr); } const char *taosInetNtoa(struct in_addr ipInt) { return inet_ntoa(ipInt); } @@ -240,7 +240,7 @@ int32_t taosSetSockOpt(SOCKET socketfd, int32_t level, int32_t optname, void *op #ifdef _MSC_VER //#if _MSC_VER >= 1900 -uint32_t taosInetAddr(char *ipAddr) { +uint32_t taosInetAddr(const char *ipAddr) { uint32_t value; int32_t ret = inet_pton(AF_INET, ipAddr, &value); if (ret <= 0) { diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 12aa77214f6109e9a33082c428a7bb7722a62835..2579082022d0b2e885aaa1148f428272fbd2d2c0 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -217,7 +217,7 @@ static void *taosThreadToOpenNewFile(void *param) { uInfo(" new log file:%d is opened", tsLogObj.flag); uInfo("=================================="); - taosPrintCfg(); + // taosPrintCfg(); taosKeepOldLog(keepName); return NULL;