config.c 16.1 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

#define _DEFAULT_SOURCE
#include "cfgInt.h"

SConfig *cfgInit() {
  SConfig *pConfig = calloc(1, sizeof(SConfig));
S
cfgtest  
Shengliang Guan 已提交
21 22 23 24 25 26 27 28 29 30 31 32
  if (pConfig == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  pConfig->hash = taosHashInit(64, MurmurHash3_32, false, HASH_NO_LOCK);
  if (pConfig->hash == NULL) {
    free(pConfig);
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
33 34 35
  return pConfig;
}

S
Shengliang Guan 已提交
36
int32_t cfgLoad(SConfig *pConfig, ECfgSrcType cfgType, const char *sourceStr) {
S
Shengliang Guan 已提交
37
  switch (cfgType) {
S
config  
Shengliang Guan 已提交
38
    case CFG_STYPE_CFG_FILE:
S
Shengliang Guan 已提交
39
      return cfgLoadFromCfgFile(pConfig, sourceStr);
S
config  
Shengliang Guan 已提交
40
    case CFG_STYPE_ENV_FILE:
S
Shengliang Guan 已提交
41
      return cfgLoadFromEnvFile(pConfig, sourceStr);
S
config  
Shengliang Guan 已提交
42
    case CFG_STYPE_ENV_VAR:
S
Shengliang Guan 已提交
43
      return cfgLoadFromEnvVar(pConfig);
S
config  
Shengliang Guan 已提交
44
    case CFG_STYPE_APOLLO_URL:
S
Shengliang Guan 已提交
45 46 47 48 49 50
      return cfgLoadFromApollUrl(pConfig, sourceStr);
    default:
      return -1;
  }
}

S
Shengliang Guan 已提交
51
void cfgCleanup(SConfig *pConfig) {
S
Shengliang Guan 已提交
52 53 54 55 56 57
  if (pConfig != NULL) {
    if (pConfig->hash != NULL) {
      taosHashCleanup(pConfig->hash);
      pConfig->hash == NULL;
    }
    free(pConfig);
S
Shengliang Guan 已提交
58 59 60 61 62 63 64 65 66
  }
}

int32_t cfgGetSize(SConfig *pConfig) { return taosHashGetSize(pConfig->hash); }

SConfigItem *cfgIterate(SConfig *pConfig, SConfigItem *pIter) { return taosHashIterate(pConfig->hash, pIter); }

void cfgCancelIterate(SConfig *pConfig, SConfigItem *pIter) { return taosHashCancelIterate(pConfig->hash, pIter); }

S
config  
Shengliang Guan 已提交
67
static int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
68 69 70 71 72 73 74
  bool tmp = false;
  if (strcasecmp(value, "true") == 0) {
    tmp = true;
  }
  if (atoi(value) > 0) {
    tmp = true;
  }
S
config  
Shengliang Guan 已提交
75

S
config  
Shengliang Guan 已提交
76 77 78 79 80
  pItem->boolVal = tmp;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
81
static int32_t cfgSetInt8(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
82 83 84 85 86 87 88 89
  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",
           pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->minIntVal, pItem->maxIntVal,
           cfgStypeStr(pItem->stype), pItem->int8Val);
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
90

S
config  
Shengliang Guan 已提交
91 92 93 94 95
  pItem->int8Val = ival;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
96
static int32_t cfgSetUInt16(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
97 98 99 100 101 102 103 104
  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",
           pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->minIntVal, pItem->maxIntVal,
           cfgStypeStr(pItem->stype), pItem->uint16Val);
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
105

S
config  
Shengliang Guan 已提交
106 107 108 109 110
  pItem->uint16Val = ival;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
111
static int32_t cfgSetInt32(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
112 113 114 115 116 117 118 119
  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",
           pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->minIntVal, pItem->maxIntVal,
           cfgStypeStr(pItem->stype), pItem->int32Val);
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
120

S
config  
Shengliang Guan 已提交
121 122 123 124 125
  pItem->int32Val = ival;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
126
static int32_t cfgSetInt64(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
127 128
  int64_t ival = (int64_t)atoi(value);
  if (ival < pItem->minIntVal || ival > pItem->maxIntVal) {
S
config  
Shengliang Guan 已提交
129 130
    uError("cfg:%s, type:%s src:%s value:%" PRId64 " out of range[%" PRId64 ", %" PRId64
           "], use last src:%s value:%" PRId64,
S
config  
Shengliang Guan 已提交
131 132 133 134 135
           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;
  }
S
config  
Shengliang Guan 已提交
136

S
config  
Shengliang Guan 已提交
137 138 139 140 141
  pItem->int64Val = ival;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
142
static int32_t cfgSetFloat(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
143 144 145 146 147 148 149 150
  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,
           cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), fval, pItem->minFloatVal, pItem->maxFloatVal,
           cfgStypeStr(pItem->stype), pItem->floatVal);
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
151

S
config  
Shengliang Guan 已提交
152 153 154 155 156
  pItem->floatVal = fval;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
157
static int32_t cfgSetString(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
158 159 160 161
  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,
S
config  
Shengliang Guan 已提交
162
           cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->strVal);
S
config  
Shengliang Guan 已提交
163 164
    return -1;
  }
S
config  
Shengliang Guan 已提交
165

S
config  
Shengliang Guan 已提交
166 167 168 169 170 171
  free(pItem->strVal);
  pItem->strVal = tmp;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
172
static int32_t cfgSetIpStr(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
173 174 175 176
  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,
S
config  
Shengliang Guan 已提交
177
           cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->strVal);
S
config  
Shengliang Guan 已提交
178 179
    return -1;
  }
S
config  
Shengliang Guan 已提交
180

S
config  
Shengliang Guan 已提交
181 182 183 184 185 186
  free(pItem->strVal);
  pItem->strVal = tmp;
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
187
static int32_t cfgSetDir(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
188 189 190 191
  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,
S
config  
Shengliang Guan 已提交
192
           cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->strVal);
S
config  
Shengliang Guan 已提交
193 194
    return -1;
  }
S
config  
Shengliang Guan 已提交
195

S
config  
Shengliang Guan 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  free(pItem->strVal);
  pItem->strVal = tmp;
  pItem->stype = stype;
  return 0;
}

int32_t cfgSetItem(SConfig *pConfig, const char *name, const char *value, ECfgSrcType stype) {
  SConfigItem *pItem = cfgGetItem(pConfig, name);
  if (pItem == NULL) {
    return -1;
  }

  switch (pItem->dtype) {
    case CFG_DTYPE_BOOL:
      return cfgSetBool(pItem, value, stype);
    case CFG_DTYPE_INT8:
      return cfgSetInt8(pItem, value, stype);
    case CFG_DTYPE_UINT16:
      return cfgSetUInt16(pItem, value, stype);
    case CFG_DTYPE_INT32:
      return cfgSetInt32(pItem, value, stype);
    case CFG_DTYPE_INT64:
      return cfgSetInt64(pItem, value, stype);
    case CFG_DTYPE_FLOAT:
      return cfgSetFloat(pItem, value, stype);
    case CFG_DTYPE_STRING:
      return cfgSetString(pItem, value, stype);
    case CFG_DTYPE_IPSTR:
      return cfgSetIpStr(pItem, value, stype);
    case CFG_DTYPE_DIR:
S
config  
Shengliang Guan 已提交
226
      return cfgSetDir(pItem, value, stype);
S
config  
Shengliang Guan 已提交
227 228 229 230 231 232 233 234 235 236
    case CFG_DTYPE_NONE:
    default:
      break;
  }

  terrno = TSDB_CODE_INVALID_CFG;
  return -1;
}

SConfigItem *cfgGetItem(SConfig *pConfig, const char *name) {
S
config  
Shengliang Guan 已提交
237 238 239
  char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0};
  memcpy(lowcaseName, name, CFG_NAME_MAX_LEN);
  strntolower(lowcaseName, name, CFG_NAME_MAX_LEN);
S
config  
Shengliang Guan 已提交
240 241 242 243 244 245 246 247

  SConfigItem *pItem = taosHashGet(pConfig->hash, lowcaseName, strlen(lowcaseName) + 1);
  if (pItem == NULL) {
    terrno = TSDB_CODE_CFG_NOT_FOUND;
  }

  return pItem;
}
S
Shengliang Guan 已提交
248 249

static int32_t cfgAddItem(SConfig *pConfig, SConfigItem *pItem, const char *name, ECfgUnitType utype) {
S
config  
Shengliang Guan 已提交
250
  pItem->stype = CFG_STYPE_DEFAULT;
S
Shengliang Guan 已提交
251 252
  pItem->utype = utype;
  pItem->name = strdup(name);
S
Shengliang Guan 已提交
253
  if (pItem->name == NULL) {
S
Shengliang Guan 已提交
254 255 256 257
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
config  
Shengliang Guan 已提交
258 259 260 261
  char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0};
  memcpy(lowcaseName, name, CFG_NAME_MAX_LEN);
  strntolower(lowcaseName, name, CFG_NAME_MAX_LEN);

S
config  
Shengliang Guan 已提交
262
  if (taosHashPut(pConfig->hash, lowcaseName, strlen(lowcaseName) + 1, pItem, sizeof(SConfigItem)) != 0) {
S
cfgtest  
Shengliang Guan 已提交
263 264 265 266
    if (pItem->dtype == CFG_DTYPE_STRING) {
      free(pItem->strVal);
    }
    free(pItem->name);
S
Shengliang Guan 已提交
267 268 269 270 271 272 273 274 275 276 277 278
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

int32_t cfgAddBool(SConfig *pConfig, const char *name, bool defaultVal, ECfgUnitType utype) {
  SConfigItem item = {.dtype = CFG_DTYPE_BOOL, .boolVal = defaultVal};
  return cfgAddItem(pConfig, &item, name, utype);
}

S
config  
Shengliang Guan 已提交
279 280 281 282 283 284
int32_t cfgAddInt8(SConfig *pConfig, const char *name, int8_t defaultVal, int64_t minval, int64_t maxval,
                   ECfgUnitType utype) {
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
285

S
config  
Shengliang Guan 已提交
286
  SConfigItem item = {.dtype = CFG_DTYPE_INT8, .int8Val = defaultVal, .minIntVal = minval, .maxIntVal = maxval};
S
Shengliang Guan 已提交
287 288 289
  return cfgAddItem(pConfig, &item, name, utype);
}

S
config  
Shengliang Guan 已提交
290 291 292 293 294 295
int32_t cfgAddUInt16(SConfig *pConfig, const char *name, uint16_t defaultVal, int64_t minval, int64_t maxval,
                     ECfgUnitType utype) {
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
296

S
config  
Shengliang Guan 已提交
297
  SConfigItem item = {.dtype = CFG_DTYPE_UINT16, .uint16Val = defaultVal, .minIntVal = minval, .maxIntVal = maxval};
S
Shengliang Guan 已提交
298 299 300
  return cfgAddItem(pConfig, &item, name, utype);
}

S
config  
Shengliang Guan 已提交
301 302 303 304 305 306
int32_t cfgAddInt32(SConfig *pConfig, const char *name, int32_t defaultVal, int64_t minval, int64_t maxval,
                    ECfgUnitType utype) {
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
307

S
config  
Shengliang Guan 已提交
308
  SConfigItem item = {.dtype = CFG_DTYPE_INT32, .int32Val = defaultVal, .minIntVal = minval, .maxIntVal = maxval};
S
Shengliang Guan 已提交
309 310 311
  return cfgAddItem(pConfig, &item, name, utype);
}

S
config  
Shengliang Guan 已提交
312 313 314 315 316 317
int32_t cfgAddInt64(SConfig *pConfig, const char *name, int64_t defaultVal, int64_t minval, int64_t maxval,
                    ECfgUnitType utype) {
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
318

S
config  
Shengliang Guan 已提交
319
  SConfigItem item = {.dtype = CFG_DTYPE_INT64, .int64Val = defaultVal, .minIntVal = minval, .maxIntVal = maxval};
S
Shengliang Guan 已提交
320 321 322
  return cfgAddItem(pConfig, &item, name, utype);
}

S
config  
Shengliang Guan 已提交
323 324 325 326 327 328
int32_t cfgAddFloat(SConfig *pConfig, const char *name, float defaultVal, double minval, double maxval,
                    ECfgUnitType utype) {
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
329

S
config  
Shengliang Guan 已提交
330
  SConfigItem item = {.dtype = CFG_DTYPE_FLOAT, .floatVal = defaultVal, .minFloatVal = minval, .maxFloatVal = maxval};
S
Shengliang Guan 已提交
331 332
  return cfgAddItem(pConfig, &item, name, utype);
}
S
Shengliang Guan 已提交
333

S
Shengliang Guan 已提交
334
int32_t cfgAddString(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
S
config  
Shengliang Guan 已提交
335 336 337 338 339
  if (defaultVal == NULL) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }

S
Shengliang Guan 已提交
340 341
  SConfigItem item = {.dtype = CFG_DTYPE_STRING};
  item.strVal = strdup(defaultVal);
S
Shengliang Guan 已提交
342
  if (item.strVal == NULL) {
S
Shengliang Guan 已提交
343 344 345 346 347
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  return cfgAddItem(pConfig, &item, name, utype);
}
S
Shengliang Guan 已提交
348

S
config  
Shengliang Guan 已提交
349 350 351 352
static int32_t cfgCheckIpStr(const char *ip) {
  uint32_t value = taosInetAddr(ip);
  if (value == INADDR_NONE) {
    uError("ip:%s is not a valid ip address", ip);
S
Shengliang Guan 已提交
353 354
    return -1;
  }
S
config  
Shengliang Guan 已提交
355 356

  return 0;
S
Shengliang Guan 已提交
357 358 359
}

int32_t cfgAddIpStr(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
S
config  
Shengliang Guan 已提交
360 361 362 363 364
  if (cfgCheckIpStr(defaultVal) != 0) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }

S
Shengliang Guan 已提交
365
  SConfigItem item = {.dtype = CFG_DTYPE_IPSTR};
S
config  
Shengliang Guan 已提交
366 367
  item.strVal = strdup(defaultVal);
  if (item.strVal == NULL) {
S
Shengliang Guan 已提交
368 369 370 371 372 373
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
  return cfgAddItem(pConfig, &item, name, utype);
}

S
config  
Shengliang Guan 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
static int32_t cfgCheckAndSetDir(SConfigItem *pItem, const char *inputDir) {
  char fullDir[PATH_MAX] = {0};
  if (taosExpandDir(inputDir, fullDir, PATH_MAX) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to expand dir:%s since %s", inputDir, terrstr());
    return -1;
  }

  if (taosRealPath(fullDir, PATH_MAX) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    uError("failed to get realpath of dir:%s since %s", inputDir, terrstr());
    return -1;
  }

  if (taosMkDir(fullDir) != 0) {
    uError("failed to create dir:%s realpath:%s since %s", inputDir, fullDir, terrstr());
    return -1;
  }

S
config  
Shengliang Guan 已提交
393 394 395
  tfree(pItem->strVal);
  pItem->strVal = strdup(fullDir);
  if (pItem->strVal == NULL) {
S
Shengliang Guan 已提交
396 397 398
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
config  
Shengliang Guan 已提交
399 400

  return 0;
S
Shengliang Guan 已提交
401 402
}

S
config  
Shengliang Guan 已提交
403 404 405
int32_t cfgAddDir(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
  SConfigItem item = {.dtype = CFG_DTYPE_DIR};
  if (cfgCheckAndSetDir(&item, defaultVal) != 0) {
S
Shengliang Guan 已提交
406 407
    return -1;
  }
S
config  
Shengliang Guan 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

  return cfgAddItem(pConfig, &item, name, utype);
}

static int32_t cfgCheckAndSetLocale(SConfigItem *pItem, const char *locale) {
  tfree(pItem->strVal);
  pItem->strVal = strdup(locale);
  if (pItem->strVal == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

int32_t cfgAddLocale(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
  SConfigItem item = {.dtype = CFG_DTYPE_LOCALE};
  if (cfgCheckAndSetLocale(&item, defaultVal) != 0) {
    return -1;
  }

  return cfgAddItem(pConfig, &item, name, utype);
}

static int32_t cfgCheckAndSetCharset(SConfigItem *pItem, const char *charset) {
  tfree(pItem->strVal);
  pItem->strVal = strdup(charset);
  if (pItem->strVal == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

int32_t cfgAddCharset(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
  SConfigItem item = {.dtype = CFG_DTYPE_CHARSET};
  if (cfgCheckAndSetCharset(&item, defaultVal) != 0) {
    return -1;
  }

  return cfgAddItem(pConfig, &item, name, utype);
}

static int32_t cfgCheckAndSetTimezone(SConfigItem *pItem, const char *timezone) {
  tfree(pItem->strVal);
  pItem->strVal = strdup(timezone);
  if (pItem->strVal == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

int32_t cfgAddTimezone(SConfig *pConfig, const char *name, const char *defaultVal, ECfgUnitType utype) {
  SConfigItem item = {.dtype = CFG_DTYPE_TIMEZONE};
  if (cfgCheckAndSetTimezone(&item, defaultVal) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
469 470
  return cfgAddItem(pConfig, &item, name, utype);
}
S
Shengliang Guan 已提交
471 472 473

const char *cfgStypeStr(ECfgSrcType type) {
  switch (type) {
S
config  
Shengliang Guan 已提交
474
    case CFG_STYPE_DEFAULT:
S
Shengliang Guan 已提交
475
      return "default";
S
config  
Shengliang Guan 已提交
476
    case CFG_STYPE_CFG_FILE:
S
Shengliang Guan 已提交
477
      return "cfg_file";
S
config  
Shengliang Guan 已提交
478
    case CFG_STYPE_ENV_FILE:
S
Shengliang Guan 已提交
479
      return "env_file";
S
config  
Shengliang Guan 已提交
480
    case CFG_STYPE_ENV_VAR:
S
Shengliang Guan 已提交
481
      return "env_var";
S
config  
Shengliang Guan 已提交
482
    case CFG_STYPE_APOLLO_URL:
S
Shengliang Guan 已提交
483
      return "apollo_url";
S
config  
Shengliang Guan 已提交
484 485 486 487
    case CFG_STYPE_ARG_LIST:
      return "arg_list";
    case CFG_STYPE_API_OPTION:
      return "api_option";
S
Shengliang Guan 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
    default:
      return "invalid";
  }
}

const char *cfgDtypeStr(ECfgDataType type) {
  switch (type) {
    case CFG_DTYPE_NONE:
      return "none";
    case CFG_DTYPE_BOOL:
      return "bool";
    case CFG_DTYPE_INT8:
      return "int8";
    case CFG_DTYPE_UINT16:
      return "uint16";
    case CFG_DTYPE_INT32:
      return "int32";
    case CFG_DTYPE_INT64:
      return "int64";
    case CFG_DTYPE_FLOAT:
      return "float";
    case CFG_DTYPE_STRING:
      return "string";
    case CFG_DTYPE_IPSTR:
      return "ipstr";
    case CFG_DTYPE_DIR:
      return "dir";
S
config  
Shengliang Guan 已提交
515 516 517 518 519 520
    case CFG_DTYPE_LOCALE:
      return "locale";
    case CFG_DTYPE_CHARSET:
      return "charset";
    case CFG_DTYPE_TIMEZONE:
      return "timezone";
S
Shengliang Guan 已提交
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
    default:
      return "invalid";
  }
}

const char *cfgUtypeStr(ECfgUnitType type) {
  switch (type) {
    case CFG_UTYPE_NONE:
      return "";
    case CFG_UTYPE_PERCENT:
      return "(%)";
    case CFG_UTYPE_GB:
      return "(GB)";
    case CFG_UTYPE_MB:
      return "(Mb)";
    case CFG_UTYPE_BYTE:
      return "(byte)";
    case CFG_UTYPE_SECOND:
      return "(s)";
    case CFG_UTYPE_MS:
      return "(ms)";
    default:
      return "invalid";
  }
}