config.c 18.6 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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"
S
Shengliang Guan 已提交
18 19 20 21
#include "tmsg.h"
#include "tep.h"
#include "tlocale.h"
#include "ttimezone.h"
S
Shengliang Guan 已提交
22 23

SConfig *cfgInit() {
S
Shengliang Guan 已提交
24 25
  SConfig *pCfg = calloc(1, sizeof(SConfig));
  if (pCfg == NULL) {
S
cfgtest  
Shengliang Guan 已提交
26 27 28 29
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
30 31 32
  pCfg->hash = taosHashInit(16, MurmurHash3_32, false, HASH_NO_LOCK);
  if (pCfg->hash == NULL) {
    free(pCfg);
S
cfgtest  
Shengliang Guan 已提交
33 34 35 36
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
37
  return pCfg;
S
Shengliang Guan 已提交
38 39
}

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

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

S
Shengliang Guan 已提交
65
int32_t cfgGetSize(SConfig *pCfg) { return taosHashGetSize(pCfg->hash); }
S
Shengliang Guan 已提交
66

S
Shengliang Guan 已提交
67
SConfigItem *cfgIterate(SConfig *pCfg, SConfigItem *pIter) { return taosHashIterate(pCfg->hash, pIter); }
S
Shengliang Guan 已提交
68

S
Shengliang Guan 已提交
69
void cfgCancelIterate(SConfig *pCfg, SConfigItem *pIter) { return taosHashCancelIterate(pCfg->hash, pIter); }
S
Shengliang Guan 已提交
70

S
Shengliang Guan 已提交
71 72 73 74 75 76
static int32_t cfgCheckAndSetTimezone(SConfigItem *pItem, const char *timezone) {
  tfree(pItem->str);
  pItem->str = strdup(timezone);
  if (pItem->str == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
S
config  
Shengliang Guan 已提交
77
  }
S
Shengliang Guan 已提交
78 79 80 81 82 83 84 85 86 87

  return 0;
}

static int32_t cfgCheckAndSetCharset(SConfigItem *pItem, const char *charset) {
  tfree(pItem->str);
  pItem->str = strdup(charset);
  if (pItem->str == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
S
config  
Shengliang Guan 已提交
88
  }
S
config  
Shengliang Guan 已提交
89

S
config  
Shengliang Guan 已提交
90 91 92
  return 0;
}

S
Shengliang Guan 已提交
93 94 95 96 97
static int32_t cfgCheckAndSetLocale(SConfigItem *pItem, const char *locale) {
  tfree(pItem->str);
  pItem->str = strdup(locale);
  if (pItem->str == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
config  
Shengliang Guan 已提交
98 99
    return -1;
  }
S
config  
Shengliang Guan 已提交
100

S
config  
Shengliang Guan 已提交
101 102 103
  return 0;
}

S
Shengliang Guan 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
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;
  }

  tfree(pItem->str);
  pItem->str = strdup(fullDir);
  if (pItem->str == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

static int32_t cfgCheckAndSetIpStr(SConfigItem *pItem, const char *ip) {
  uint32_t value = taosInetAddr(ip);
  if (value == INADDR_NONE) {
    uError("ip:%s is not a valid ip address", ip);
    return -1;
  }

  tfree(pItem->str);
  pItem->str = strdup(ip);
  if (pItem->str == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
config  
Shengliang Guan 已提交
144 145
    return -1;
  }
S
config  
Shengliang Guan 已提交
146

S
Shengliang Guan 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159
  return 0;
}

static int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
  bool tmp = false;
  if (strcasecmp(value, "true") == 0) {
    tmp = true;
  }
  if (atoi(value) > 0) {
    tmp = true;
  }

  pItem->bval = tmp;
S
config  
Shengliang Guan 已提交
160 161 162 163
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
164
static int32_t cfgSetInt32(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
165
  int32_t ival = (int32_t)atoi(value);
S
Shengliang Guan 已提交
166
  if (ival < pItem->imin || ival > pItem->imax) {
S
config  
Shengliang Guan 已提交
167
    uError("cfg:%s, type:%s src:%s value:%d out of range[%" PRId64 ", %" PRId64 "], use last src:%s value:%d",
S
Shengliang Guan 已提交
168 169
           pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->imin, pItem->imax,
           cfgStypeStr(pItem->stype), pItem->i32);
S
config  
Shengliang Guan 已提交
170 171 172
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
173

S
Shengliang Guan 已提交
174
  pItem->i32 = ival;
S
config  
Shengliang Guan 已提交
175 176 177 178
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
179
static int32_t cfgSetInt64(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
180
  int64_t ival = (int64_t)atoi(value);
S
Shengliang Guan 已提交
181
  if (ival < pItem->imin || ival > pItem->imax) {
S
config  
Shengliang Guan 已提交
182 183
    uError("cfg:%s, type:%s src:%s value:%" PRId64 " out of range[%" PRId64 ", %" PRId64
           "], use last src:%s value:%" PRId64,
S
Shengliang Guan 已提交
184 185
           pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->imin, pItem->imax,
           cfgStypeStr(pItem->stype), pItem->i64);
S
config  
Shengliang Guan 已提交
186 187 188
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
189

S
Shengliang Guan 已提交
190
  pItem->i64 = ival;
S
config  
Shengliang Guan 已提交
191 192 193 194
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
195
static int32_t cfgSetFloat(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
196
  float fval = (float)atof(value);
S
Shengliang Guan 已提交
197
  if (fval < pItem->fmin || fval > pItem->fmax) {
S
config  
Shengliang Guan 已提交
198
    uError("cfg:%s, type:%s src:%s value:%f out of range[%f, %f], use last src:%s value:%f", pItem->name,
S
Shengliang Guan 已提交
199 200
           cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), fval, pItem->fmin, pItem->fmax, cfgStypeStr(pItem->stype),
           pItem->fval);
S
config  
Shengliang Guan 已提交
201 202 203
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
204

S
Shengliang Guan 已提交
205
  pItem->fval = fval;
S
config  
Shengliang Guan 已提交
206 207 208 209
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
210
static int32_t cfgSetString(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
211 212 213 214
  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
Shengliang Guan 已提交
215
           cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->str);
S
config  
Shengliang Guan 已提交
216 217
    return -1;
  }
S
config  
Shengliang Guan 已提交
218

S
Shengliang Guan 已提交
219 220
  free(pItem->str);
  pItem->str = tmp;
S
config  
Shengliang Guan 已提交
221 222 223 224
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
225
static int32_t cfgSetIpStr(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
226
  char *tmp = strdup(value);
S
Shengliang Guan 已提交
227 228
  if (tmp == NULL || cfgCheckAndSetIpStr(pItem, value) != 0) {
    free(tmp);
S
config  
Shengliang Guan 已提交
229 230
    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
Shengliang Guan 已提交
231
           cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->str);
S
config  
Shengliang Guan 已提交
232 233
    return -1;
  }
S
config  
Shengliang Guan 已提交
234

S
config  
Shengliang Guan 已提交
235 236 237 238
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
239
static int32_t cfgSetDir(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
240
  char *tmp = strdup(value);
S
Shengliang Guan 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
  if (tmp == NULL || cfgCheckAndSetDir(pItem, value) != 0) {
    free(tmp);
    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), value, terrstr(), cfgStypeStr(pItem->stype), pItem->str);
    return -1;
  }

  pItem->stype = stype;
  return 0;
}

static int32_t cfgSetLocale(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
  char *tmp = strdup(value);
  if (tmp == NULL || cfgCheckAndSetLocale(pItem, value) != 0) {
    free(tmp);
    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), value, terrstr(), cfgStypeStr(pItem->stype), pItem->str);
    return -1;
  }

  pItem->stype = stype;
  return 0;
}

static int32_t cfgSetCharset(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
  char *tmp = strdup(value);
  if (tmp == NULL || cfgCheckAndSetCharset(pItem, value) != 0) {
    free(tmp);
    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), value, terrstr(), cfgStypeStr(pItem->stype), pItem->str);
    return -1;
  }

  pItem->stype = stype;
  return 0;
}

static int32_t cfgSetTimezone(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
  char *tmp = strdup(value);
  if (tmp == NULL || cfgCheckAndSetTimezone(pItem, value) != 0) {
    free(tmp);
S
config  
Shengliang Guan 已提交
285 286
    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
Shengliang Guan 已提交
287
           cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), value, terrstr(), cfgStypeStr(pItem->stype), pItem->str);
S
config  
Shengliang Guan 已提交
288 289
    return -1;
  }
S
config  
Shengliang Guan 已提交
290

S
config  
Shengliang Guan 已提交
291 292 293 294
  pItem->stype = stype;
  return 0;
}

S
Shengliang Guan 已提交
295 296
int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcType stype) {
  SConfigItem *pItem = cfgGetItem(pCfg, name);
S
config  
Shengliang Guan 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
  if (pItem == NULL) {
    return -1;
  }

  switch (pItem->dtype) {
    case CFG_DTYPE_BOOL:
      return cfgSetBool(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 已提交
315
      return cfgSetDir(pItem, value, stype);
S
Shengliang Guan 已提交
316 317 318 319 320 321
    case CFG_DTYPE_TIMEZONE:
      return cfgSetTimezone(pItem, value, stype);
    case CFG_DTYPE_CHARSET:
      return cfgSetCharset(pItem, value, stype);
    case CFG_DTYPE_LOCALE:
      return cfgSetLocale(pItem, value, stype);
S
config  
Shengliang Guan 已提交
322 323 324 325 326 327 328 329 330
    case CFG_DTYPE_NONE:
    default:
      break;
  }

  terrno = TSDB_CODE_INVALID_CFG;
  return -1;
}

S
Shengliang Guan 已提交
331
SConfigItem *cfgGetItem(SConfig *pCfg, const char *name) {
S
config  
Shengliang Guan 已提交
332 333 334
  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 已提交
335

S
Shengliang Guan 已提交
336
  SConfigItem *pItem = taosHashGet(pCfg->hash, lowcaseName, strlen(lowcaseName) + 1);
S
config  
Shengliang Guan 已提交
337 338 339 340 341 342
  if (pItem == NULL) {
    terrno = TSDB_CODE_CFG_NOT_FOUND;
  }

  return pItem;
}
S
Shengliang Guan 已提交
343

S
Shengliang Guan 已提交
344
static int32_t cfgAddItem(SConfig *pCfg, SConfigItem *pItem, const char *name) {
S
config  
Shengliang Guan 已提交
345
  pItem->stype = CFG_STYPE_DEFAULT;
S
Shengliang Guan 已提交
346
  pItem->name = strdup(name);
S
Shengliang Guan 已提交
347
  if (pItem->name == NULL) {
S
Shengliang Guan 已提交
348 349 350 351
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
config  
Shengliang Guan 已提交
352 353 354 355
  char lowcaseName[CFG_NAME_MAX_LEN + 1] = {0};
  memcpy(lowcaseName, name, CFG_NAME_MAX_LEN);
  strntolower(lowcaseName, name, CFG_NAME_MAX_LEN);

S
Shengliang Guan 已提交
356
  if (taosHashPut(pCfg->hash, lowcaseName, strlen(lowcaseName) + 1, pItem, sizeof(SConfigItem)) != 0) {
S
cfgtest  
Shengliang Guan 已提交
357
    if (pItem->dtype == CFG_DTYPE_STRING) {
S
Shengliang Guan 已提交
358
      free(pItem->str);
S
cfgtest  
Shengliang Guan 已提交
359 360
    }
    free(pItem->name);
S
Shengliang Guan 已提交
361 362 363 364 365 366 367
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
368
int32_t cfgAddBool(SConfig *pCfg, const char *name, bool defaultVal) {
S
Shengliang Guan 已提交
369
  SConfigItem item = {.dtype = CFG_DTYPE_BOOL, .bval = defaultVal};
S
Shengliang Guan 已提交
370
  return cfgAddItem(pCfg, &item, name);
S
Shengliang Guan 已提交
371 372
}

S
Shengliang Guan 已提交
373
int32_t cfgAddInt32(SConfig *pCfg, const char *name, int32_t defaultVal, int64_t minval, int64_t maxval) {
S
config  
Shengliang Guan 已提交
374 375 376 377
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
378

S
Shengliang Guan 已提交
379
  SConfigItem item = {.dtype = CFG_DTYPE_INT32, .i32 = defaultVal, .imin = minval, .imax = maxval};
S
Shengliang Guan 已提交
380
  return cfgAddItem(pCfg, &item, name);
S
Shengliang Guan 已提交
381 382
}

S
Shengliang Guan 已提交
383
int32_t cfgAddInt64(SConfig *pCfg, const char *name, int64_t defaultVal, int64_t minval, int64_t maxval) {
S
config  
Shengliang Guan 已提交
384 385 386 387
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
388

S
Shengliang Guan 已提交
389
  SConfigItem item = {.dtype = CFG_DTYPE_INT64, .i64 = defaultVal, .imin = minval, .imax = maxval};
S
Shengliang Guan 已提交
390
  return cfgAddItem(pCfg, &item, name);
S
Shengliang Guan 已提交
391 392
}

S
Shengliang Guan 已提交
393
int32_t cfgAddFloat(SConfig *pCfg, const char *name, float defaultVal, double minval, double maxval) {
S
config  
Shengliang Guan 已提交
394 395 396 397
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
398

S
Shengliang Guan 已提交
399
  SConfigItem item = {.dtype = CFG_DTYPE_FLOAT, .fval = defaultVal, .fmin = minval, .fmax = maxval};
S
Shengliang Guan 已提交
400
  return cfgAddItem(pCfg, &item, name);
S
Shengliang Guan 已提交
401 402
}

S
Shengliang Guan 已提交
403
int32_t cfgAddString(SConfig *pCfg, const char *name, const char *defaultVal) {
S
Shengliang Guan 已提交
404
  SConfigItem item = {.dtype = CFG_DTYPE_STRING};
S
Shengliang Guan 已提交
405 406
  item.str = strdup(defaultVal);
  if (item.str == NULL) {
S
Shengliang Guan 已提交
407 408 409
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
410
  return cfgAddItem(pCfg, &item, name);
S
Shengliang Guan 已提交
411 412
}

S
Shengliang Guan 已提交
413
int32_t cfgAddIpStr(SConfig *pCfg, const char *name, const char *defaultVal) {
S
Shengliang Guan 已提交
414
  SConfigItem item = {.dtype = CFG_DTYPE_IPSTR};
S
Shengliang Guan 已提交
415
  if (cfgCheckAndSetIpStr(&item, defaultVal) != 0) {
S
config  
Shengliang Guan 已提交
416 417 418
    return -1;
  }

S
Shengliang Guan 已提交
419
  return cfgAddItem(pCfg, &item, name);
S
Shengliang Guan 已提交
420 421
}

S
Shengliang Guan 已提交
422
int32_t cfgAddDir(SConfig *pCfg, const char *name, const char *defaultVal) {
S
config  
Shengliang Guan 已提交
423 424
  SConfigItem item = {.dtype = CFG_DTYPE_DIR};
  if (cfgCheckAndSetDir(&item, defaultVal) != 0) {
S
Shengliang Guan 已提交
425 426
    return -1;
  }
S
config  
Shengliang Guan 已提交
427

S
Shengliang Guan 已提交
428
  return cfgAddItem(pCfg, &item, name);
S
config  
Shengliang Guan 已提交
429 430
}

S
Shengliang Guan 已提交
431
int32_t cfgAddLocale(SConfig *pCfg, const char *name, const char *defaultVal) {
S
config  
Shengliang Guan 已提交
432 433 434 435 436
  SConfigItem item = {.dtype = CFG_DTYPE_LOCALE};
  if (cfgCheckAndSetLocale(&item, defaultVal) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
437
  return cfgAddItem(pCfg, &item, name);
S
config  
Shengliang Guan 已提交
438 439
}

S
Shengliang Guan 已提交
440
int32_t cfgAddCharset(SConfig *pCfg, const char *name, const char *defaultVal) {
S
config  
Shengliang Guan 已提交
441 442 443 444 445
  SConfigItem item = {.dtype = CFG_DTYPE_CHARSET};
  if (cfgCheckAndSetCharset(&item, defaultVal) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
446
  return cfgAddItem(pCfg, &item, name);
S
config  
Shengliang Guan 已提交
447 448
}

S
Shengliang Guan 已提交
449
int32_t cfgAddTimezone(SConfig *pCfg, const char *name, const char *defaultVal) {
S
config  
Shengliang Guan 已提交
450 451 452 453 454
  SConfigItem item = {.dtype = CFG_DTYPE_TIMEZONE};
  if (cfgCheckAndSetTimezone(&item, defaultVal) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
455
  return cfgAddItem(pCfg, &item, name);
S
Shengliang Guan 已提交
456
}
S
Shengliang Guan 已提交
457 458 459

const char *cfgStypeStr(ECfgSrcType type) {
  switch (type) {
S
config  
Shengliang Guan 已提交
460
    case CFG_STYPE_DEFAULT:
S
Shengliang Guan 已提交
461
      return "default";
S
config  
Shengliang Guan 已提交
462
    case CFG_STYPE_CFG_FILE:
S
Shengliang Guan 已提交
463
      return "cfg_file";
S
config  
Shengliang Guan 已提交
464
    case CFG_STYPE_ENV_FILE:
S
Shengliang Guan 已提交
465
      return "env_file";
S
config  
Shengliang Guan 已提交
466
    case CFG_STYPE_ENV_VAR:
S
Shengliang Guan 已提交
467
      return "env_var";
S
config  
Shengliang Guan 已提交
468
    case CFG_STYPE_APOLLO_URL:
S
Shengliang Guan 已提交
469
      return "apollo_url";
S
config  
Shengliang Guan 已提交
470 471 472 473
    case CFG_STYPE_ARG_LIST:
      return "arg_list";
    case CFG_STYPE_API_OPTION:
      return "api_option";
S
Shengliang Guan 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
    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_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 已提交
497 498 499 500 501 502
    case CFG_DTYPE_LOCALE:
      return "locale";
    case CFG_DTYPE_CHARSET:
      return "charset";
    case CFG_DTYPE_TIMEZONE:
      return "timezone";
S
Shengliang Guan 已提交
503 504 505 506 507
    default:
      return "invalid";
  }
}

S
Shengliang Guan 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
void cfgPrintCfg(SConfig *pCfg) {
  uInfo("taos global config");
  uInfo("==================================");

  SConfigItem *pItem = cfgIterate(pCfg, NULL);
  while (pItem != NULL) {
    switch (pItem->dtype) {
      case CFG_DTYPE_BOOL:
        uInfo("cfg:%s, value:%u src:%s", pItem->name, pItem->bval, cfgStypeStr(pItem->stype));
        break;
      case CFG_DTYPE_INT32:
        uInfo("cfg:%s, value:%d src:%s", pItem->name, pItem->i32, cfgStypeStr(pItem->stype));
        break;
      case CFG_DTYPE_INT64:
        uInfo("cfg:%s, value:%" PRId64 " src:%s", pItem->name, pItem->i64, cfgStypeStr(pItem->stype));
        break;
      case CFG_DTYPE_FLOAT:
        uInfo("cfg:%s, value:%f src:%s", pItem->name, pItem->fval, cfgStypeStr(pItem->stype));
        break;
      case CFG_DTYPE_STRING:
      case CFG_DTYPE_IPSTR:
      case CFG_DTYPE_DIR:
      case CFG_DTYPE_LOCALE:
      case CFG_DTYPE_CHARSET:
      case CFG_DTYPE_TIMEZONE:
        uInfo("cfg:%s, value:%s src:%s", pItem->name, pItem->str, cfgStypeStr(pItem->stype));
        break;
    }
    pItem = cfgIterate(pCfg, pItem);
  }
}

S
Shengliang Guan 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
void cfgDumpCfg(SConfig *pCfg) {
  uInfo("global config");
  uInfo("==================================");

  SConfigItem *pItem = cfgIterate(pCfg, NULL);
  while (pItem != NULL) {
    switch (pItem->dtype) {
      case CFG_DTYPE_BOOL:
        uInfo("cfg:%s, value:%u src:%s", pItem->name, pItem->bval, cfgStypeStr(pItem->stype));
        break;
      case CFG_DTYPE_INT32:
        uInfo("cfg:%s, value:%d src:%s", pItem->name, pItem->i32, cfgStypeStr(pItem->stype));
        break;
      case CFG_DTYPE_INT64:
        uInfo("cfg:%s, value:%" PRId64 " src:%s", pItem->name, pItem->i64, cfgStypeStr(pItem->stype));
        break;
      case CFG_DTYPE_FLOAT:
        uInfo("cfg:%s, value:%f src:%s", pItem->name, pItem->fval, cfgStypeStr(pItem->stype));
        break;
      case CFG_DTYPE_STRING:
      case CFG_DTYPE_IPSTR:
      case CFG_DTYPE_DIR:
      case CFG_DTYPE_LOCALE:
      case CFG_DTYPE_CHARSET:
      case CFG_DTYPE_TIMEZONE:
        uInfo("cfg:%s, value:%s src:%s", pItem->name, pItem->str, cfgStypeStr(pItem->stype));
        break;
    }
    pItem = cfgIterate(pCfg, pItem);
  }

  uInfo("==================================");
}
S
Shengliang Guan 已提交
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
#if 0
// int32_t cfgCheck(SConfig *pCfg) {
//   SConfigItem *pItem = NULL;

//   pItem = cfgGetItem(pCfg, "serverPort");
//   if (pItem != NULL) {
//     tsServerPort = (uint16_t)pItem->i32;
//   }

//   pItem = cfgGetItem(pCfg, "firstEp");
//   if (pItem != NULL) {
//     tstrncpy(tsFirst, pItem->str, TSDB_EP_LEN);
//   }

//   snprintf(tsLocalEp, TSDB_EP_LEN, "%s:%u", tsLocalFqdn, tsServerPort);
//   uInfo("localEp is: %s", tsLocalEp);

//   SEp ep = {0};
//   if (tsFirst[0] == 0) {
//     strcpy(tsFirst, tsLocalEp);
//   } else {
//     taosGetFqdnPortFromEp(tsFirst, &ep);
//     snprintf(tsFirst, TSDB_EP_LEN, "%s:%u", ep.fqdn, ep.port);
//   }

//   pItem = cfgGetItem(pCfg, "secondEp");
//   if (pItem != NULL) {
//     tstrncpy(tsSecond, pItem->str, TSDB_EP_LEN);
//   }

//   if (tsSecond[0] == 0) {
//     strcpy(tsSecond, tsLocalEp);
//   } else {
//     taosGetFqdnPortFromEp(tsSecond, &ep);
//     snprintf(tsSecond, TSDB_EP_LEN, "%s:%u", ep.fqdn, ep.port);
//   }

//   pItem = cfgGetItem(pCfg, "dataDir");
//   if (pItem != NULL) {
//     tstrncpy(tsDataDir, pItem->str, PATH_MAX);
//   }

//   if (tsDiskCfgNum <= 0) {
//     taosAddDataDir(0, tsDataDir, 0, 1);
//     tsDiskCfgNum = 1;
//     uTrace("dataDir:%s, level:0 primary:1 is configured by default", tsDataDir);
//   }

//   if (taosDirExist(tsTempDir) != 0) {
//     return -1;
//   }

//   taosGetSystemInfo();

//   tsSetLocale();

//   // SGlobalCfg *cfg_timezone = taosGetConfigOption("timezone");
//   // if (cfg_timezone && cfg_timezone->cfgStatus == TAOS_CFG_CSTATUS_FILE) {
//   tsSetTimeZone();
//   // }

//   pItem = cfgGetItem(pCfg, "numOfCores");
//   if (pItem != NULL) {
//     tsNumOfCores = pItem->i32;
//   }

//   if (tsNumOfCores <= 0) {
//     tsNumOfCores = 1;
//   }

//   if (tsQueryBufferSize >= 0) {
//     tsQueryBufferSizeBytes = tsQueryBufferSize * 1048576UL;
//   }

//   cfgPrintCfg(pCfg);

//   return 0;
// }
#endif