tconfig.c 31.4 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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
S
Shengliang Guan 已提交
17
#include "tconfig.h"
H
Hongze Cheng 已提交
18
#include "cJSON.h"
S
Shengliang Guan 已提交
19
#include "taoserror.h"
wafwerar's avatar
wafwerar 已提交
20
#include "tenv.h"
wafwerar's avatar
wafwerar 已提交
21
#include "tgrant.h"
H
Hongze Cheng 已提交
22 23 24
#include "tjson.h"
#include "tlog.h"
#include "tutil.h"
S
Shengliang Guan 已提交
25

S
Shengliang Guan 已提交
26
#define CFG_NAME_PRINT_LEN 24
S
Shengliang Guan 已提交
27 28 29 30 31
#define CFG_SRC_PRINT_LEN  12

int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath);
int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *filepath);
int32_t cfgLoadFromEnvVar(SConfig *pConfig);
wafwerar's avatar
wafwerar 已提交
32
int32_t cfgLoadFromEnvCmd(SConfig *pConfig, const char **envCmd);
S
Shengliang Guan 已提交
33 34
int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url);
int32_t cfgSetItem(SConfig *pConfig, const char *name, const char *value, ECfgSrcType stype);
S
Shengliang Guan 已提交
35

wafwerar's avatar
wafwerar 已提交
36 37
extern char **environ;

S
Shengliang Guan 已提交
38
SConfig *cfgInit() {
wafwerar's avatar
wafwerar 已提交
39
  SConfig *pCfg = taosMemoryCalloc(1, sizeof(SConfig));
S
Shengliang Guan 已提交
40
  if (pCfg == NULL) {
S
cfgtest  
Shengliang Guan 已提交
41 42 43 44
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
45 46
  pCfg->array = taosArrayInit(32, sizeof(SConfigItem));
  if (pCfg->array == NULL) {
wafwerar's avatar
wafwerar 已提交
47
    taosMemoryFree(pCfg);
S
cfgtest  
Shengliang Guan 已提交
48 49 50 51
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

S
Shengliang Guan 已提交
52
  return pCfg;
S
Shengliang Guan 已提交
53 54
}

wafwerar's avatar
wafwerar 已提交
55
int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const void *sourceStr) {
S
Shengliang Guan 已提交
56
  switch (cfgType) {
S
config  
Shengliang Guan 已提交
57
    case CFG_STYPE_CFG_FILE:
S
Shengliang Guan 已提交
58
      return cfgLoadFromCfgFile(pCfg, sourceStr);
S
config  
Shengliang Guan 已提交
59
    case CFG_STYPE_ENV_FILE:
S
Shengliang Guan 已提交
60
      return cfgLoadFromEnvFile(pCfg, sourceStr);
S
config  
Shengliang Guan 已提交
61
    case CFG_STYPE_ENV_VAR:
S
Shengliang Guan 已提交
62
      return cfgLoadFromEnvVar(pCfg);
S
config  
Shengliang Guan 已提交
63
    case CFG_STYPE_APOLLO_URL:
S
Shengliang Guan 已提交
64
      return cfgLoadFromApollUrl(pCfg, sourceStr);
wafwerar's avatar
wafwerar 已提交
65 66
    case CFG_STYPE_ENV_CMD:
      return cfgLoadFromEnvCmd(pCfg, (const char **)sourceStr);
S
Shengliang Guan 已提交
67 68 69 70 71
    default:
      return -1;
  }
}

72
int32_t cfgLoadFromArray(SConfig *pCfg, SArray *pArgs) {
S
Shengliang Guan 已提交
73 74 75
  int32_t size = taosArrayGetSize(pArgs);
  for (int32_t i = 0; i < size; ++i) {
    SConfigPair *pPair = taosArrayGet(pArgs, i);
S
Shengliang Guan 已提交
76 77 78
    if (cfgSetItem(pCfg, pPair->name, pPair->value, CFG_STYPE_ARG_LIST) != 0) {
      return -1;
    }
S
Shengliang Guan 已提交
79
  }
S
Shengliang Guan 已提交
80 81

  return 0;
S
Shengliang Guan 已提交
82 83
}

S
tfs cfg  
Shengliang Guan 已提交
84 85 86
static void cfgFreeItem(SConfigItem *pItem) {
  if (pItem->dtype == CFG_DTYPE_STRING || pItem->dtype == CFG_DTYPE_DIR || pItem->dtype == CFG_DTYPE_LOCALE ||
      pItem->dtype == CFG_DTYPE_CHARSET || pItem->dtype == CFG_DTYPE_TIMEZONE) {
wafwerar's avatar
wafwerar 已提交
87
    taosMemoryFreeClear(pItem->str);
S
tfs cfg  
Shengliang Guan 已提交
88 89 90 91 92 93 94
  }
  if (pItem->array) {
    taosArrayDestroy(pItem->array);
    pItem->array = NULL;
  }
}

S
Shengliang Guan 已提交
95 96
void cfgCleanup(SConfig *pCfg) {
  if (pCfg != NULL) {
S
Shengliang Guan 已提交
97 98 99 100
    int32_t size = taosArrayGetSize(pCfg->array);
    for (int32_t i = 0; i < size; ++i) {
      SConfigItem *pItem = taosArrayGet(pCfg->array, i);
      cfgFreeItem(pItem);
wafwerar's avatar
wafwerar 已提交
101
      taosMemoryFreeClear(pItem->name);
S
Shengliang Guan 已提交
102
    }
S
Shengliang Guan 已提交
103
    taosArrayDestroy(pCfg->array);
wafwerar's avatar
wafwerar 已提交
104
    taosMemoryFree(pCfg);
S
Shengliang Guan 已提交
105 106 107
  }
}

S
Shengliang Guan 已提交
108
int32_t cfgGetSize(SConfig *pCfg) { return taosArrayGetSize(pCfg->array); }
S
Shengliang Guan 已提交
109

S
Shengliang Guan 已提交
110
static int32_t cfgCheckAndSetTimezone(SConfigItem *pItem, const char *timezone) {
S
tfs cfg  
Shengliang Guan 已提交
111
  cfgFreeItem(pItem);
112
  pItem->str = taosStrdup(timezone);
S
Shengliang Guan 已提交
113 114 115
  if (pItem->str == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
S
config  
Shengliang Guan 已提交
116
  }
S
Shengliang Guan 已提交
117 118 119 120 121

  return 0;
}

static int32_t cfgCheckAndSetCharset(SConfigItem *pItem, const char *charset) {
S
tfs cfg  
Shengliang Guan 已提交
122
  cfgFreeItem(pItem);
123
  pItem->str = taosStrdup(charset);
S
Shengliang Guan 已提交
124 125 126
  if (pItem->str == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
S
config  
Shengliang Guan 已提交
127
  }
S
config  
Shengliang Guan 已提交
128

S
config  
Shengliang Guan 已提交
129 130 131
  return 0;
}

S
Shengliang Guan 已提交
132
static int32_t cfgCheckAndSetLocale(SConfigItem *pItem, const char *locale) {
S
tfs cfg  
Shengliang Guan 已提交
133
  cfgFreeItem(pItem);
134
  pItem->str = taosStrdup(locale);
S
Shengliang Guan 已提交
135 136
  if (pItem->str == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
config  
Shengliang Guan 已提交
137 138
    return -1;
  }
S
config  
Shengliang Guan 已提交
139

S
config  
Shengliang Guan 已提交
140 141 142
  return 0;
}

S
Shengliang Guan 已提交
143 144 145 146 147 148 149 150
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;
  }

wafwerar's avatar
wafwerar 已提交
151
  taosMemoryFreeClear(pItem->str);
152
  pItem->str = taosStrdup(fullDir);
S
Shengliang Guan 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
  if (pItem->str == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  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 已提交
171 172 173 174
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
175
static int32_t cfgSetInt32(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
176
  int32_t ival = (int32_t)atoi(value);
S
Shengliang Guan 已提交
177
  if (ival < pItem->imin || ival > pItem->imax) {
178 179
    uError("cfg:%s, type:%s src:%s value:%d out of range[%" PRId64 ", %" PRId64 "]", pItem->name,
           cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->imin, pItem->imax);
S
config  
Shengliang Guan 已提交
180 181 182
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
183

S
Shengliang Guan 已提交
184
  pItem->i32 = ival;
S
config  
Shengliang Guan 已提交
185 186 187 188
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
189
static int32_t cfgSetInt64(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
190
  int64_t ival = (int64_t)atoll(value);
S
Shengliang Guan 已提交
191
  if (ival < pItem->imin || ival > pItem->imax) {
192 193
    uError("cfg:%s, type:%s src:%s value:%" PRId64 " out of range[%" PRId64 ", %" PRId64 "]", pItem->name,
           cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->imin, pItem->imax);
S
config  
Shengliang Guan 已提交
194 195 196
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
197

S
Shengliang Guan 已提交
198
  pItem->i64 = ival;
S
config  
Shengliang Guan 已提交
199 200 201 202
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
203
static int32_t cfgSetFloat(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
config  
Shengliang Guan 已提交
204
  float fval = (float)atof(value);
S
Shengliang Guan 已提交
205
  if (fval < pItem->fmin || fval > pItem->fmax) {
206 207
    uError("cfg:%s, type:%s src:%s value:%f out of range[%f, %f]", pItem->name, cfgDtypeStr(pItem->dtype),
           cfgStypeStr(stype), fval, pItem->fmin, pItem->fmax);
S
config  
Shengliang Guan 已提交
208 209 210
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
config  
Shengliang Guan 已提交
211

S
Shengliang Guan 已提交
212
  pItem->fval = fval;
S
config  
Shengliang Guan 已提交
213 214 215 216
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
217
static int32_t cfgSetString(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
218
  char *tmp = taosStrdup(value);
S
config  
Shengliang Guan 已提交
219 220
  if (tmp == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
221 222
    uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s", pItem->name, cfgDtypeStr(pItem->dtype),
           cfgStypeStr(stype), value, terrstr());
S
config  
Shengliang Guan 已提交
223 224
    return -1;
  }
S
config  
Shengliang Guan 已提交
225

wafwerar's avatar
wafwerar 已提交
226
  taosMemoryFree(pItem->str);
S
Shengliang Guan 已提交
227
  pItem->str = tmp;
S
config  
Shengliang Guan 已提交
228 229 230 231
  pItem->stype = stype;
  return 0;
}

S
config  
Shengliang Guan 已提交
232
static int32_t cfgSetDir(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
Shengliang Guan 已提交
233
  if (cfgCheckAndSetDir(pItem, value) != 0) {
234 235
    uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s", pItem->name, cfgDtypeStr(pItem->dtype),
           cfgStypeStr(stype), value, terrstr());
S
Shengliang Guan 已提交
236 237 238 239 240 241 242 243
    return -1;
  }

  pItem->stype = stype;
  return 0;
}

static int32_t cfgSetLocale(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
Shengliang Guan 已提交
244
  if (cfgCheckAndSetLocale(pItem, value) != 0) {
S
Shengliang Guan 已提交
245
    terrno = TSDB_CODE_OUT_OF_MEMORY;
246 247
    uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s", pItem->name, cfgDtypeStr(pItem->dtype),
           cfgStypeStr(stype), value, terrstr());
S
Shengliang Guan 已提交
248 249 250 251 252 253 254 255
    return -1;
  }

  pItem->stype = stype;
  return 0;
}

static int32_t cfgSetCharset(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
Shengliang Guan 已提交
256
  if (cfgCheckAndSetCharset(pItem, value) != 0) {
S
Shengliang Guan 已提交
257
    terrno = TSDB_CODE_OUT_OF_MEMORY;
258 259
    uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s", pItem->name, cfgDtypeStr(pItem->dtype),
           cfgStypeStr(stype), value, terrstr());
S
Shengliang Guan 已提交
260 261 262 263 264 265 266 267
    return -1;
  }

  pItem->stype = stype;
  return 0;
}

static int32_t cfgSetTimezone(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
S
Shengliang Guan 已提交
268
  if (cfgCheckAndSetTimezone(pItem, value) != 0) {
S
config  
Shengliang Guan 已提交
269
    terrno = TSDB_CODE_OUT_OF_MEMORY;
270 271
    uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s", pItem->name, cfgDtypeStr(pItem->dtype),
           cfgStypeStr(stype), value, terrstr());
S
config  
Shengliang Guan 已提交
272 273 274
    return -1;
  }
  pItem->stype = stype;
275 276

  // apply new timezone
277
  osSetTimezone(value);
278

S
config  
Shengliang Guan 已提交
279 280 281
  return 0;
}

S
tfs cfg  
Shengliang Guan 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295
static int32_t cfgSetTfsItem(SConfig *pCfg, const char *name, const char *value, const char *level, const char *primary,
                             ECfgSrcType stype) {
  SConfigItem *pItem = cfgGetItem(pCfg, name);
  if (pItem == NULL) return -1;

  if (pItem->array == NULL) {
    pItem->array = taosArrayInit(16, sizeof(SDiskCfg));
    if (pItem->array == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }
  }

  SDiskCfg cfg = {0};
296
  tstrncpy(cfg.dir, pItem->str, sizeof(cfg.dir));
297 298
  cfg.level = level ? atoi(level) : 0;
  cfg.primary = primary ? atoi(primary) : 1;
S
tfs cfg  
Shengliang Guan 已提交
299 300 301 302 303 304 305 306 307 308
  void *ret = taosArrayPush(pItem->array, &cfg);
  if (ret == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  pItem->stype = stype;
  return 0;
}

S
Shengliang Guan 已提交
309
int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcType stype) {
wafwerar's avatar
wafwerar 已提交
310
  GRANT_CFG_SET;
S
Shengliang Guan 已提交
311
  SConfigItem *pItem = cfgGetItem(pCfg, name);
S
config  
Shengliang Guan 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  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_DIR:
S
config  
Shengliang Guan 已提交
328
      return cfgSetDir(pItem, value, stype);
S
Shengliang Guan 已提交
329 330 331 332 333 334
    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 已提交
335 336 337 338 339 340 341 342 343
    case CFG_DTYPE_NONE:
    default:
      break;
  }

  terrno = TSDB_CODE_INVALID_CFG;
  return -1;
}

S
Shengliang Guan 已提交
344
SConfigItem *cfgGetItem(SConfig *pCfg, const char *name) {
S
Shengliang Guan 已提交
345
  if (pCfg == NULL) return NULL;
S
Shengliang Guan 已提交
346 347 348 349 350 351
  int32_t size = taosArrayGetSize(pCfg->array);
  for (int32_t i = 0; i < size; ++i) {
    SConfigItem *pItem = taosArrayGet(pCfg->array, i);
    if (strcasecmp(pItem->name, name) == 0) {
      return pItem;
    }
S
config  
Shengliang Guan 已提交
352 353
  }

354
  // uError("name:%s, cfg not found", name);
S
Shengliang Guan 已提交
355 356
  terrno = TSDB_CODE_CFG_NOT_FOUND;
  return NULL;
S
config  
Shengliang Guan 已提交
357
}
S
Shengliang Guan 已提交
358

S
Shengliang Guan 已提交
359
static int32_t cfgAddItem(SConfig *pCfg, SConfigItem *pItem, const char *name) {
S
config  
Shengliang Guan 已提交
360
  pItem->stype = CFG_STYPE_DEFAULT;
361
  pItem->name = taosStrdup(name);
S
Shengliang Guan 已提交
362
  if (pItem->name == NULL) {
S
Shengliang Guan 已提交
363 364 365 366
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

S
Shengliang Guan 已提交
367 368 369
  int32_t len = strlen(name);
  char    lowcaseName[CFG_NAME_MAX_LEN + 1] = {0};
  strntolower(lowcaseName, name, TMIN(CFG_NAME_MAX_LEN, len));
S
config  
Shengliang Guan 已提交
370

S
Shengliang Guan 已提交
371
  if (taosArrayPush(pCfg->array, pItem) == NULL) {
S
cfgtest  
Shengliang Guan 已提交
372
    if (pItem->dtype == CFG_DTYPE_STRING) {
wafwerar's avatar
wafwerar 已提交
373
      taosMemoryFree(pItem->str);
S
cfgtest  
Shengliang Guan 已提交
374
    }
wafwerar's avatar
wafwerar 已提交
375
    taosMemoryFree(pItem->name);
S
Shengliang Guan 已提交
376 377 378 379 380 381 382
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
383 384
int32_t cfgAddBool(SConfig *pCfg, const char *name, bool defaultVal, bool tsc) {
  SConfigItem item = {.dtype = CFG_DTYPE_BOOL, .bval = defaultVal, .tsc = tsc};
S
Shengliang Guan 已提交
385
  return cfgAddItem(pCfg, &item, name);
S
Shengliang Guan 已提交
386 387
}

S
Shengliang Guan 已提交
388
int32_t cfgAddInt32(SConfig *pCfg, const char *name, int32_t defaultVal, int64_t minval, int64_t maxval, bool tsc) {
S
config  
Shengliang Guan 已提交
389 390 391 392
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
393

S
Shengliang Guan 已提交
394
  SConfigItem item = {.dtype = CFG_DTYPE_INT32, .i32 = defaultVal, .imin = minval, .imax = maxval, .tsc = tsc};
S
Shengliang Guan 已提交
395
  return cfgAddItem(pCfg, &item, name);
S
Shengliang Guan 已提交
396 397
}

S
Shengliang Guan 已提交
398
int32_t cfgAddInt64(SConfig *pCfg, const char *name, int64_t defaultVal, int64_t minval, int64_t maxval, bool tsc) {
S
config  
Shengliang Guan 已提交
399 400 401 402
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
403

S
Shengliang Guan 已提交
404
  SConfigItem item = {.dtype = CFG_DTYPE_INT64, .i64 = defaultVal, .imin = minval, .imax = maxval, .tsc = tsc};
S
Shengliang Guan 已提交
405
  return cfgAddItem(pCfg, &item, name);
S
Shengliang Guan 已提交
406 407
}

S
Shengliang Guan 已提交
408
int32_t cfgAddFloat(SConfig *pCfg, const char *name, float defaultVal, double minval, double maxval, bool tsc) {
S
config  
Shengliang Guan 已提交
409 410 411 412
  if (defaultVal < minval || defaultVal > maxval) {
    terrno = TSDB_CODE_OUT_OF_RANGE;
    return -1;
  }
S
Shengliang Guan 已提交
413

S
Shengliang Guan 已提交
414
  SConfigItem item = {.dtype = CFG_DTYPE_FLOAT, .fval = defaultVal, .fmin = minval, .fmax = maxval, .tsc = tsc};
S
Shengliang Guan 已提交
415
  return cfgAddItem(pCfg, &item, name);
S
Shengliang Guan 已提交
416 417
}

S
Shengliang Guan 已提交
418 419
int32_t cfgAddString(SConfig *pCfg, const char *name, const char *defaultVal, bool tsc) {
  SConfigItem item = {.dtype = CFG_DTYPE_STRING, .tsc = tsc};
420
  item.str = taosStrdup(defaultVal);
S
Shengliang Guan 已提交
421
  if (item.str == NULL) {
S
Shengliang Guan 已提交
422 423 424
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }
S
Shengliang Guan 已提交
425
  return cfgAddItem(pCfg, &item, name);
S
Shengliang Guan 已提交
426 427
}

S
Shengliang Guan 已提交
428 429
int32_t cfgAddDir(SConfig *pCfg, const char *name, const char *defaultVal, bool tsc) {
  SConfigItem item = {.dtype = CFG_DTYPE_DIR, .tsc = tsc};
S
config  
Shengliang Guan 已提交
430
  if (cfgCheckAndSetDir(&item, defaultVal) != 0) {
S
Shengliang Guan 已提交
431 432
    return -1;
  }
S
config  
Shengliang Guan 已提交
433

S
Shengliang Guan 已提交
434
  return cfgAddItem(pCfg, &item, name);
S
config  
Shengliang Guan 已提交
435 436
}

S
Shengliang Guan 已提交
437
int32_t cfgAddLocale(SConfig *pCfg, const char *name, const char *defaultVal) {
S
Shengliang Guan 已提交
438
  SConfigItem item = {.dtype = CFG_DTYPE_LOCALE, .tsc = 1};
S
config  
Shengliang Guan 已提交
439 440 441 442
  if (cfgCheckAndSetLocale(&item, defaultVal) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
443
  return cfgAddItem(pCfg, &item, name);
S
config  
Shengliang Guan 已提交
444 445
}

S
Shengliang Guan 已提交
446
int32_t cfgAddCharset(SConfig *pCfg, const char *name, const char *defaultVal) {
S
Shengliang Guan 已提交
447
  SConfigItem item = {.dtype = CFG_DTYPE_CHARSET, .tsc = 1};
S
config  
Shengliang Guan 已提交
448 449 450 451
  if (cfgCheckAndSetCharset(&item, defaultVal) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
452
  return cfgAddItem(pCfg, &item, name);
S
config  
Shengliang Guan 已提交
453 454
}

S
Shengliang Guan 已提交
455
int32_t cfgAddTimezone(SConfig *pCfg, const char *name, const char *defaultVal) {
S
Shengliang Guan 已提交
456
  SConfigItem item = {.dtype = CFG_DTYPE_TIMEZONE, .tsc = 1};
S
config  
Shengliang Guan 已提交
457 458 459 460
  if (cfgCheckAndSetTimezone(&item, defaultVal) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
461
  return cfgAddItem(pCfg, &item, name);
S
Shengliang Guan 已提交
462
}
S
Shengliang Guan 已提交
463 464 465

const char *cfgStypeStr(ECfgSrcType type) {
  switch (type) {
S
config  
Shengliang Guan 已提交
466
    case CFG_STYPE_DEFAULT:
S
Shengliang Guan 已提交
467
      return "default";
S
config  
Shengliang Guan 已提交
468
    case CFG_STYPE_CFG_FILE:
S
Shengliang Guan 已提交
469
      return "cfg_file";
S
config  
Shengliang Guan 已提交
470
    case CFG_STYPE_ENV_FILE:
S
Shengliang Guan 已提交
471
      return "env_file";
S
config  
Shengliang Guan 已提交
472
    case CFG_STYPE_ENV_VAR:
S
Shengliang Guan 已提交
473
      return "env_var";
S
config  
Shengliang Guan 已提交
474
    case CFG_STYPE_APOLLO_URL:
S
Shengliang Guan 已提交
475
      return "apollo_url";
S
config  
Shengliang Guan 已提交
476 477
    case CFG_STYPE_ARG_LIST:
      return "arg_list";
S
Shengliang Guan 已提交
478 479
    case CFG_STYPE_TAOS_OPTIONS:
      return "taos_options";
wafwerar's avatar
wafwerar 已提交
480 481
    case CFG_STYPE_ENV_CMD:
      return "env_cmd";
S
Shengliang Guan 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
    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_DIR:
      return "dir";
S
config  
Shengliang Guan 已提交
503 504 505 506 507 508
    case CFG_DTYPE_LOCALE:
      return "locale";
    case CFG_DTYPE_CHARSET:
      return "charset";
    case CFG_DTYPE_TIMEZONE:
      return "timezone";
S
Shengliang Guan 已提交
509 510 511 512 513
    default:
      return "invalid";
  }
}

H
Hongze Cheng 已提交
514
void cfgDumpItemValue(SConfigItem *pItem, char *buf, int32_t bufSize, int32_t *pLen) {
D
dapan1121 已提交
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 540 541 542 543 544 545
  int32_t len = 0;
  switch (pItem->dtype) {
    case CFG_DTYPE_BOOL:
      len = snprintf(buf, bufSize, "%u", pItem->bval);
      break;
    case CFG_DTYPE_INT32:
      len = snprintf(buf, bufSize, "%d", pItem->i32);
      break;
    case CFG_DTYPE_INT64:
      len = snprintf(buf, bufSize, "%" PRId64, pItem->i64);
      break;
    case CFG_DTYPE_FLOAT:
      len = snprintf(buf, bufSize, "%f", pItem->fval);
      break;
    case CFG_DTYPE_STRING:
    case CFG_DTYPE_DIR:
    case CFG_DTYPE_LOCALE:
    case CFG_DTYPE_CHARSET:
    case CFG_DTYPE_TIMEZONE:
    case CFG_DTYPE_NONE:
      len = snprintf(buf, bufSize, "%s", pItem->str);
      break;
  }

  if (len > bufSize) {
    len = bufSize;
  }

  *pLen = len;
}

S
Shengliang Guan 已提交
546 547
void cfgDumpCfg(SConfig *pCfg, bool tsc, bool dump) {
  if (dump) {
S
Shengliang Guan 已提交
548
    printf("                     global config");
S
Shengliang Guan 已提交
549 550 551 552
    printf("\n");
    printf("=================================================================");
    printf("\n");
  } else {
S
Shengliang Guan 已提交
553
    uInfo("                     global config");
S
Shengliang Guan 已提交
554 555
    uInfo("=================================================================");
  }
S
Shengliang Guan 已提交
556 557 558

  char src[CFG_SRC_PRINT_LEN + 1] = {0};
  char name[CFG_NAME_PRINT_LEN + 1] = {0};
S
Shengliang Guan 已提交
559

S
Shengliang Guan 已提交
560 561 562
  int32_t size = taosArrayGetSize(pCfg->array);
  for (int32_t i = 0; i < size; ++i) {
    SConfigItem *pItem = taosArrayGet(pCfg->array, i);
S
Shengliang Guan 已提交
563
    if (tsc && !pItem->tsc) continue;
564 565
    if (dump && strcmp(pItem->name, "scriptDir") == 0) continue;
    if (dump && strcmp(pItem->name, "simDebugFlag") == 0) continue;
S
Shengliang Guan 已提交
566
    tstrncpy(src, cfgStypeStr(pItem->stype), CFG_SRC_PRINT_LEN);
H
Haojun Liao 已提交
567 568
    for (int32_t j = 0; j < CFG_SRC_PRINT_LEN; ++j) {
      if (src[j] == 0) src[j] = ' ';
S
Shengliang Guan 已提交
569 570
    }

S
Shengliang Guan 已提交
571
    tstrncpy(name, pItem->name, CFG_NAME_PRINT_LEN);
H
Haojun Liao 已提交
572 573
    for (int32_t j = 0; j < CFG_NAME_PRINT_LEN; ++j) {
      if (name[j] == 0) name[j] = ' ';
S
Shengliang Guan 已提交
574
    }
S
Shengliang Guan 已提交
575 576 577

    switch (pItem->dtype) {
      case CFG_DTYPE_BOOL:
S
Shengliang Guan 已提交
578 579 580 581 582 583 584
        if (dump) {
          printf("%s %s %u", src, name, pItem->bval);
          printf("\n");
        } else {
          uInfo("%s %s %u", src, name, pItem->bval);
        }

S
Shengliang Guan 已提交
585 586
        break;
      case CFG_DTYPE_INT32:
S
Shengliang Guan 已提交
587 588 589 590 591 592
        if (dump) {
          printf("%s %s %d", src, name, pItem->i32);
          printf("\n");
        } else {
          uInfo("%s %s %d", src, name, pItem->i32);
        }
S
Shengliang Guan 已提交
593 594
        break;
      case CFG_DTYPE_INT64:
S
Shengliang Guan 已提交
595 596 597 598 599 600
        if (dump) {
          printf("%s %s %" PRId64, src, name, pItem->i64);
          printf("\n");
        } else {
          uInfo("%s %s %" PRId64, src, name, pItem->i64);
        }
S
Shengliang Guan 已提交
601 602
        break;
      case CFG_DTYPE_FLOAT:
S
Shengliang Guan 已提交
603
        if (dump) {
604
          printf("%s %s %.2f", src, name, pItem->fval);
S
Shengliang Guan 已提交
605 606
          printf("\n");
        } else {
607
          uInfo("%s %s %.2f", src, name, pItem->fval);
S
Shengliang Guan 已提交
608
        }
S
Shengliang Guan 已提交
609 610 611 612 613 614
        break;
      case CFG_DTYPE_STRING:
      case CFG_DTYPE_DIR:
      case CFG_DTYPE_LOCALE:
      case CFG_DTYPE_CHARSET:
      case CFG_DTYPE_TIMEZONE:
615
      case CFG_DTYPE_NONE:
S
Shengliang Guan 已提交
616 617 618 619 620 621
        if (dump) {
          printf("%s %s %s", src, name, pItem->str);
          printf("\n");
        } else {
          uInfo("%s %s %s", src, name, pItem->str);
        }
S
Shengliang Guan 已提交
622 623 624 625
        break;
    }
  }

S
Shengliang Guan 已提交
626 627 628 629 630 631
  if (dump) {
    printf("=================================================================");
    printf("\n");
  } else {
    uInfo("=================================================================");
  }
S
Shengliang Guan 已提交
632
}
S
Shengliang Guan 已提交
633 634

int32_t cfgLoadFromEnvVar(SConfig *pConfig) {
H
Hongze Cheng 已提交
635
  char    line[1024], *name, *value, *value2, *value3;
wafwerar's avatar
wafwerar 已提交
636
  int32_t olen, vlen, vlen2, vlen3;
637
  int32_t code = 0;
H
Hongze Cheng 已提交
638
  char  **pEnv = environ;
wafwerar's avatar
wafwerar 已提交
639
  line[1023] = 0;
640 641

  if (pEnv == NULL) return 0;
H
Hongze Cheng 已提交
642
  while (*pEnv != NULL) {
wafwerar's avatar
wafwerar 已提交
643 644 645
    name = value = value2 = value3 = NULL;
    olen = vlen = vlen2 = vlen3 = 0;

H
Hongze Cheng 已提交
646
    strncpy(line, *pEnv, sizeof(line) - 1);
wafwerar's avatar
wafwerar 已提交
647
    pEnv++;
wafwerar's avatar
wafwerar 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
    taosEnvToCfg(line, line);

    paGetToken(line, &name, &olen);
    if (olen == 0) continue;
    name[olen] = 0;

    paGetToken(name + olen + 1, &value, &vlen);
    if (vlen == 0) continue;
    value[vlen] = 0;

    paGetToken(value + vlen + 1, &value2, &vlen2);
    if (vlen2 != 0) {
      value2[vlen2] = 0;
      paGetToken(value2 + vlen2 + 1, &value3, &vlen3);
      if (vlen3 != 0) value3[vlen3] = 0;
    }

665 666 667
    code = cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_VAR);
    if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break;

668
    if (strcasecmp(name, "dataDir") == 0) {
669 670
      code = cfgSetTfsItem(pConfig, name, value, value2, value3, CFG_STYPE_ENV_VAR);
      if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break;
wafwerar's avatar
wafwerar 已提交
671 672 673 674
    }
  }

  uInfo("load from env variables cfg success");
S
Shengliang Guan 已提交
675 676 677
  return 0;
}

wafwerar's avatar
wafwerar 已提交
678
int32_t cfgLoadFromEnvCmd(SConfig *pConfig, const char **envCmd) {
679
  char    buf[1024], *name, *value, *value2, *value3;
wafwerar's avatar
wafwerar 已提交
680
  int32_t olen, vlen, vlen2, vlen3;
681
  int32_t code = 0;
wafwerar's avatar
wafwerar 已提交
682 683
  int32_t index = 0;
  if (envCmd == NULL) return 0;
H
Hongze Cheng 已提交
684 685 686
  while (envCmd[index] != NULL) {
    strncpy(buf, envCmd[index], sizeof(buf) - 1);
    buf[sizeof(buf) - 1] = 0;
687
    taosEnvToCfg(buf, buf);
wafwerar's avatar
wafwerar 已提交
688
    index++;
H
Hongze Cheng 已提交
689

wafwerar's avatar
wafwerar 已提交
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
    name = value = value2 = value3 = NULL;
    olen = vlen = vlen2 = vlen3 = 0;

    paGetToken(buf, &name, &olen);
    if (olen == 0) continue;
    name[olen] = 0;

    paGetToken(name + olen + 1, &value, &vlen);
    if (vlen == 0) continue;
    value[vlen] = 0;

    paGetToken(value + vlen + 1, &value2, &vlen2);
    if (vlen2 != 0) {
      value2[vlen2] = 0;
      paGetToken(value2 + vlen2 + 1, &value3, &vlen3);
      if (vlen3 != 0) value3[vlen3] = 0;
    }

708 709 710
    code = cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_CMD);
    if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break;

711
    if (strcasecmp(name, "dataDir") == 0) {
712 713
      code = cfgSetTfsItem(pConfig, name, value, value2, value3, CFG_STYPE_ENV_CMD);
      if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break;
wafwerar's avatar
wafwerar 已提交
714 715 716 717 718 719 720 721
    }
  }

  uInfo("load from env cmd cfg success");
  return 0;
}

int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *envFile) {
wafwerar's avatar
wafwerar 已提交
722
  char    line[1024], *name, *value, *value2, *value3;
wafwerar's avatar
wafwerar 已提交
723
  int32_t olen, vlen, vlen2, vlen3;
724
  int32_t code = 0;
wafwerar's avatar
wafwerar 已提交
725 726 727
  ssize_t _bytes = 0;

  const char *filepath = ".env";
H
Hongze Cheng 已提交
728
  if (envFile != NULL && strlen(envFile) > 0) {
wafwerar's avatar
wafwerar 已提交
729
    if (!taosCheckExistFile(envFile)) {
S
Shengliang Guan 已提交
730
      uError("failed to load env file:%s", envFile);
wafwerar's avatar
wafwerar 已提交
731 732 733
      return -1;
    }
    filepath = envFile;
H
Hongze Cheng 已提交
734
  } else {
wafwerar's avatar
wafwerar 已提交
735
    if (!taosCheckExistFile(filepath)) {
S
Shengliang Guan 已提交
736
      uInfo("env file:%s not load", filepath);
wafwerar's avatar
wafwerar 已提交
737 738 739 740 741 742 743 744 745 746 747 748 749 750
      return 0;
    }
  }

  TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ | TD_FILE_STREAM);
  if (pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  while (!taosEOFFile(pFile)) {
    name = value = value2 = value3 = NULL;
    olen = vlen = vlen2 = vlen3 = 0;

wafwerar's avatar
wafwerar 已提交
751
    _bytes = taosGetsFile(pFile, sizeof(line), line);
wafwerar's avatar
wafwerar 已提交
752 753 754
    if (_bytes <= 0) {
      break;
    }
H
Hongze Cheng 已提交
755
    if (line[_bytes - 1] == '\n') line[_bytes - 1] = 0;
wafwerar's avatar
wafwerar 已提交
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    taosEnvToCfg(line, line);

    paGetToken(line, &name, &olen);
    if (olen == 0) continue;
    name[olen] = 0;

    paGetToken(name + olen + 1, &value, &vlen);
    if (vlen == 0) continue;
    value[vlen] = 0;

    paGetToken(value + vlen + 1, &value2, &vlen2);
    if (vlen2 != 0) {
      value2[vlen2] = 0;
      paGetToken(value2 + vlen2 + 1, &value3, &vlen3);
      if (vlen3 != 0) value3[vlen3] = 0;
    }

773 774 775
    code = cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_FILE);
    if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break;

776
    if (strcasecmp(name, "dataDir") == 0) {
777 778
      code = cfgSetTfsItem(pConfig, name, value, value2, value3, CFG_STYPE_ENV_FILE);
      if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break;
wafwerar's avatar
wafwerar 已提交
779 780 781 782 783 784
    }
  }

  taosCloseFile(&pFile);

  uInfo("load from env cfg file %s success", filepath);
S
Shengliang Guan 已提交
785 786 787 788
  return 0;
}

int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) {
wafwerar's avatar
wafwerar 已提交
789
  char    line[1024], *name, *value, *value2, *value3;
S
Shengliang Guan 已提交
790
  int32_t olen, vlen, vlen2, vlen3;
S
Shengliang Guan 已提交
791
  ssize_t _bytes = 0;
792
  int32_t code = 0;
793

794
  TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ | TD_FILE_STREAM);
795
  if (pFile == NULL) {
796 797 798 799 800 801 802 803 804
    // success when the file does not exist
    if (errno == ENOENT) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      uInfo("failed to load from cfg file %s since %s, use default parameters", filepath, terrstr());
      return 0;
    } else {
      uError("failed to load from cfg file %s since %s", filepath, terrstr());
      return -1;
    }
S
Shengliang Guan 已提交
805 806
  }

807
  while (!taosEOFFile(pFile)) {
S
Shengliang Guan 已提交
808 809 810
    name = value = value2 = value3 = NULL;
    olen = vlen = vlen2 = vlen3 = 0;

wafwerar's avatar
wafwerar 已提交
811
    _bytes = taosGetsFile(pFile, sizeof(line), line);
wafwerar's avatar
wafwerar 已提交
812
    if (_bytes <= 0) {
S
Shengliang Guan 已提交
813 814 815
      break;
    }

H
Hongze Cheng 已提交
816
    if (line[_bytes - 1] == '\n') line[_bytes - 1] = 0;
S
Shengliang Guan 已提交
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832

    paGetToken(line, &name, &olen);
    if (olen == 0) continue;
    name[olen] = 0;

    paGetToken(name + olen + 1, &value, &vlen);
    if (vlen == 0) continue;
    value[vlen] = 0;

    paGetToken(value + vlen + 1, &value2, &vlen2);
    if (vlen2 != 0) {
      value2[vlen2] = 0;
      paGetToken(value2 + vlen2 + 1, &value3, &vlen3);
      if (vlen3 != 0) value3[vlen3] = 0;
    }

833 834 835
    code = cfgSetItem(pConfig, name, value, CFG_STYPE_CFG_FILE);
    if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break;

836
    if (strcasecmp(name, "dataDir") == 0) {
837 838
      code = cfgSetTfsItem(pConfig, name, value, value2, value3, CFG_STYPE_CFG_FILE);
      if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break;
S
Shengliang Guan 已提交
839
    }
S
Shengliang Guan 已提交
840 841
  }

842
  taosCloseFile(&pFile);
S
Shengliang Guan 已提交
843

844
  if (code == 0 || (code != 0 && terrno == TSDB_CODE_CFG_NOT_FOUND)) {
845
    uInfo("load from cfg file %s success", filepath);
846
    return 0;
847 848
  } else {
    uError("failed to load from cfg file %s since %s", filepath, terrstr());
849
    return -1;
850
  }
S
Shengliang Guan 已提交
851 852
}

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
// int32_t cfgLoadFromCfgText(SConfig *pConfig, const char *configText) {
//   char   *line = NULL, *name, *value, *value2, *value3;
//   int32_t olen, vlen, vlen2, vlen3;
//   ssize_t _bytes = 0;
//   int32_t code = 0;

//   TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ | TD_FILE_STREAM);
//   if (pFile == NULL) {
//     // success when the file does not exist
//     if (errno == ENOENT) {
//       terrno = TAOS_SYSTEM_ERROR(errno);
//       uInfo("failed to load from cfg file %s since %s, use default parameters", filepath, terrstr());
//       return 0;
//     } else {
//       uError("failed to load from cfg file %s since %s", filepath, terrstr());
//       return -1;
//     }
//   }

//   while (!taosEOFFile(pFile)) {
//     name = value = value2 = value3 = NULL;
//     olen = vlen = vlen2 = vlen3 = 0;

//     _bytes = taosGetLineFile(pFile, &line);
//     if (_bytes <= 0) {
//       break;
//     }

//     if(line[_bytes - 1] == '\n') line[_bytes - 1] = 0;

//     paGetToken(line, &name, &olen);
//     if (olen == 0) continue;
//     name[olen] = 0;

//     paGetToken(name + olen + 1, &value, &vlen);
//     if (vlen == 0) continue;
//     value[vlen] = 0;

//     paGetToken(value + vlen + 1, &value2, &vlen2);
//     if (vlen2 != 0) {
//       value2[vlen2] = 0;
//       paGetToken(value2 + vlen2 + 1, &value3, &vlen3);
//       if (vlen3 != 0) value3[vlen3] = 0;
//     }

//     code = cfgSetItem(pConfig, name, value, CFG_STYPE_CFG_FILE);
//     if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break;
900
//     if (strcasecmp(name, "dataDir") == 0) {
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
//       code = cfgSetTfsItem(pConfig, name, value, value2, value3, CFG_STYPE_CFG_FILE);
//       if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break;
//     }
//   }

//   taosCloseFile(&pFile);
//   if (line != NULL) taosMemoryFreeClear(line);

//   if (code == 0 || (code != 0 && terrno == TSDB_CODE_CFG_NOT_FOUND)) {
//     uInfo("load from cfg file %s success", filepath);
//     return 0;
//   } else {
//     uError("failed to load from cfg file %s since %s", filepath, terrstr());
//     return -1;
//   }
// }

S
Shengliang Guan 已提交
918
int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) {
wafwerar's avatar
wafwerar 已提交
919 920
  char   *cfgLineBuf = NULL, *name, *value, *value2, *value3;
  int32_t olen, vlen, vlen2, vlen3;
921
  int32_t code = 0;
wafwerar's avatar
wafwerar 已提交
922
  if (url == NULL || strlen(url) == 0) {
S
Shengliang Guan 已提交
923
    uInfo("apoll url not load");
wafwerar's avatar
wafwerar 已提交
924 925
    return 0;
  }
H
Hongze Cheng 已提交
926

wafwerar's avatar
wafwerar 已提交
927 928 929 930 931 932 933
  char *p = strchr(url, ':');
  if (p == NULL) {
    uError("fail to load apoll url: %s, unknown format", url);
    return -1;
  }
  p++;

wafwerar's avatar
wafwerar 已提交
934
  if (strncmp(url, "jsonFile", 8) == 0) {
wafwerar's avatar
wafwerar 已提交
935 936
    char *filepath = p;
    if (!taosCheckExistFile(filepath)) {
S
Shengliang Guan 已提交
937
      uError("failed to load json file:%s", filepath);
wafwerar's avatar
wafwerar 已提交
938 939 940 941 942 943 944 945 946
      return -1;
    }

    TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ);
    if (pFile == NULL) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      return -1;
    }
    size_t fileSize = taosLSeekFile(pFile, 0, SEEK_END);
H
Hongze Cheng 已提交
947
    char  *buf = taosMemoryMalloc(fileSize);
wafwerar's avatar
wafwerar 已提交
948
    taosLSeekFile(pFile, 0, SEEK_SET);
H
Hongze Cheng 已提交
949
    if (taosReadFile(pFile, buf, fileSize) <= 0) {
wafwerar's avatar
wafwerar 已提交
950 951
      taosCloseFile(&pFile);
      uError("load json file error: %s", filepath);
A
Alex Duan 已提交
952
      taosMemoryFreeClear(buf);
wafwerar's avatar
wafwerar 已提交
953 954 955
      return -1;
    }
    taosCloseFile(&pFile);
H
Hongze Cheng 已提交
956
    SJson *pJson = tjsonParse(buf);
wafwerar's avatar
wafwerar 已提交
957 958 959 960 961
    if (NULL == pJson) {
      const char *jsonParseError = tjsonGetError();
      if (jsonParseError != NULL) {
        uError("load json file parse error: %s", jsonParseError);
      }
A
Alex Duan 已提交
962
      taosMemoryFreeClear(buf);
wafwerar's avatar
wafwerar 已提交
963 964 965 966 967
      return -1;
    }
    taosMemoryFreeClear(buf);

    int32_t jsonArraySize = tjsonGetArraySize(pJson);
H
Hongze Cheng 已提交
968 969
    for (int32_t i = 0; i < jsonArraySize; i++) {
      cJSON *item = tjsonGetArrayItem(pJson, i);
wafwerar's avatar
wafwerar 已提交
970 971 972 973 974 975 976 977 978 979 980
      if (item == NULL) break;
      char *itemName = NULL, *itemValueString = NULL;
      tjsonGetObjectName(item, &itemName);
      tjsonGetObjectName(item, &itemName);
      tjsonGetObjectValueString(item, &itemValueString);
      if (itemValueString != NULL && itemName != NULL) {
        size_t itemNameLen = strlen(itemName);
        size_t itemValueStringLen = strlen(itemValueString);
        cfgLineBuf = taosMemoryMalloc(itemNameLen + itemValueStringLen + 2);
        memcpy(cfgLineBuf, itemName, itemNameLen);
        cfgLineBuf[itemNameLen] = ' ';
H
Hongze Cheng 已提交
981
        memcpy(&cfgLineBuf[itemNameLen + 1], itemValueString, itemValueStringLen);
wafwerar's avatar
wafwerar 已提交
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
        cfgLineBuf[itemNameLen + itemValueStringLen + 1] = '\0';

        paGetToken(cfgLineBuf, &name, &olen);
        if (olen == 0) continue;
        name[olen] = 0;

        paGetToken(name + olen + 1, &value, &vlen);
        if (vlen == 0) continue;
        value[vlen] = 0;

        paGetToken(value + vlen + 1, &value2, &vlen2);
        if (vlen2 != 0) {
          value2[vlen2] = 0;
          paGetToken(value2 + vlen2 + 1, &value3, &vlen3);
          if (vlen3 != 0) value3[vlen3] = 0;
        }
998 999 1000 1001

        code = cfgSetItem(pConfig, name, value, CFG_STYPE_APOLLO_URL);
        if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break;
        
1002
        if (strcasecmp(name, "dataDir") == 0) {
1003 1004
          code = cfgSetTfsItem(pConfig, name, value, value2, value3, CFG_STYPE_APOLLO_URL);
          if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break;
wafwerar's avatar
wafwerar 已提交
1005 1006 1007 1008 1009
        }
      }
    }
    tjsonDelete(pJson);

H
Hongze Cheng 已提交
1010 1011
    // } else if (strncmp(url, "jsonUrl", 7) == 0) {
    // } else if (strncmp(url, "etcdUrl", 7) == 0) {
wafwerar's avatar
wafwerar 已提交
1012 1013 1014 1015 1016 1017
  } else {
    uError("Unsupported url: %s", url);
    return -1;
  }

  uInfo("load from apoll url not implemented yet");
S
Shengliang Guan 已提交
1018
  return 0;
1019
}
wafwerar's avatar
wafwerar 已提交
1020

H
Hongze Cheng 已提交
1021
int32_t cfgGetApollUrl(const char **envCmd, const char *envFile, char *apolloUrl) {
wafwerar's avatar
wafwerar 已提交
1022 1023
  int32_t index = 0;
  if (envCmd == NULL) return 0;
H
Hongze Cheng 已提交
1024
  while (envCmd[index] != NULL) {
wafwerar's avatar
wafwerar 已提交
1025
    if (strncmp(envCmd[index], "TAOS_APOLLO_URL", 14) == 0) {
wafwerar's avatar
wafwerar 已提交
1026 1027 1028 1029 1030
      char *p = strchr(envCmd[index], '=');
      if (p != NULL) {
        p++;
        if (*p == '\'') {
          p++;
H
Hongze Cheng 已提交
1031
          p[strlen(p) - 1] = '\0';
wafwerar's avatar
wafwerar 已提交
1032
        }
H
Hongze Cheng 已提交
1033
        memcpy(apolloUrl, p, TMIN(strlen(p) + 1, PATH_MAX));
wafwerar's avatar
wafwerar 已提交
1034 1035 1036 1037 1038 1039 1040
        uInfo("get apollo url from env cmd success");
        return 0;
      }
    }
    index++;
  }

wafwerar's avatar
wafwerar 已提交
1041 1042 1043
  char   line[1024];
  char **pEnv = environ;
  line[1023] = 0;
H
Hongze Cheng 已提交
1044 1045
  while (*pEnv != NULL) {
    strncpy(line, *pEnv, sizeof(line) - 1);
wafwerar's avatar
wafwerar 已提交
1046
    pEnv++;
wafwerar's avatar
wafwerar 已提交
1047 1048 1049 1050 1051
    if (strncmp(line, "TAOS_APOLLO_URL", 14) == 0) {
      char *p = strchr(line, '=');
      if (p != NULL) {
        p++;
        if (*p == '\'') {
wafwerar's avatar
wafwerar 已提交
1052
          p++;
H
Hongze Cheng 已提交
1053
          p[strlen(p) - 1] = '\0';
wafwerar's avatar
wafwerar 已提交
1054
        }
H
Hongze Cheng 已提交
1055 1056
        memcpy(apolloUrl, p, TMIN(strlen(p) + 1, PATH_MAX));
        uInfo("get apollo url from env variables success, apolloUrl=%s", apolloUrl);
wafwerar's avatar
wafwerar 已提交
1057
        return 0;
wafwerar's avatar
wafwerar 已提交
1058 1059 1060 1061 1062
      }
    }
  }

  const char *filepath = ".env";
H
Hongze Cheng 已提交
1063
  if (envFile != NULL && strlen(envFile) > 0) {
wafwerar's avatar
wafwerar 已提交
1064
    if (!taosCheckExistFile(envFile)) {
S
Shengliang Guan 已提交
1065
      uError("failed to load env file:%s", envFile);
wafwerar's avatar
wafwerar 已提交
1066 1067 1068
      return -1;
    }
    filepath = envFile;
H
Hongze Cheng 已提交
1069
  } else {
wafwerar's avatar
wafwerar 已提交
1070
    if (!taosCheckExistFile(filepath)) {
S
Shengliang Guan 已提交
1071
      uInfo("env file:%s not load", filepath);
wafwerar's avatar
wafwerar 已提交
1072 1073 1074
      return 0;
    }
  }
H
Hongze Cheng 已提交
1075
  int64_t   _bytes;
wafwerar's avatar
wafwerar 已提交
1076 1077 1078
  TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ | TD_FILE_STREAM);
  if (pFile != NULL) {
    while (!taosEOFFile(pFile)) {
wafwerar's avatar
wafwerar 已提交
1079
      _bytes = taosGetsFile(pFile, sizeof(line) - 1, line);
wafwerar's avatar
wafwerar 已提交
1080 1081 1082
      if (_bytes <= 0) {
        break;
      }
H
Hongze Cheng 已提交
1083
      if (line[_bytes - 1] == '\n') line[_bytes - 1] = 0;
wafwerar's avatar
wafwerar 已提交
1084
      if (strncmp(line, "TAOS_APOLLO_URL", 14) == 0) {
wafwerar's avatar
wafwerar 已提交
1085 1086 1087 1088 1089
        char *p = strchr(line, '=');
        if (p != NULL) {
          p++;
          if (*p == '\'') {
            p++;
H
Hongze Cheng 已提交
1090
            p[strlen(p) - 1] = '\0';
wafwerar's avatar
wafwerar 已提交
1091
          }
H
Hongze Cheng 已提交
1092
          memcpy(apolloUrl, p, TMIN(strlen(p) + 1, PATH_MAX));
wafwerar's avatar
wafwerar 已提交
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
          taosCloseFile(&pFile);
          uInfo("get apollo url from env file success");
          return 0;
        }
      }
    }
    taosCloseFile(&pFile);
  }

  uInfo("fail get apollo url from cmd env file");
  return -1;
D
dapan1121 已提交
1104
}