Read the old file, split it into lines to populate a newly created * config rewrite state, and return it to the caller. * * If it is impossible to read the old file, NULL is returned. * If the old file does not exist at all, an empty state is returned. */
| 1382 | * If it is impossible to read the old file, NULL is returned. |
| 1383 | * If the old file does not exist at all, an empty state is returned. */ |
| 1384 | struct rewriteConfigState *rewriteConfigReadOldFile(char *path) { |
| 1385 | FILE *fp = fopen(path,"r"); |
| 1386 | struct rewriteConfigState *state = (rewriteConfigState*)zmalloc(sizeof(*state), MALLOC_LOCAL); |
| 1387 | char buf[CONFIG_MAX_LINE+1]; |
| 1388 | int linenum = -1; |
| 1389 | |
| 1390 | if (fp == NULL && errno != ENOENT) return NULL; |
| 1391 | |
| 1392 | state->option_to_line = dictCreate(&optionToLineDictType,NULL); |
| 1393 | state->rewritten = dictCreate(&optionSetDictType,NULL); |
| 1394 | state->numlines = 0; |
| 1395 | state->lines = NULL; |
| 1396 | state->has_tail = 0; |
| 1397 | state->force_all = 0; |
| 1398 | if (fp == NULL) return state; |
| 1399 | |
| 1400 | /* Read the old file line by line, populate the state. */ |
| 1401 | while(fgets(buf,CONFIG_MAX_LINE+1,fp) != NULL) { |
| 1402 | int argc; |
| 1403 | sds *argv; |
| 1404 | sds line = sdstrim(sdsnew(buf),"\r\n\t "); |
| 1405 | |
| 1406 | linenum++; /* Zero based, so we init at -1 */ |
| 1407 | |
| 1408 | /* Handle comments and empty lines. */ |
| 1409 | if (line[0] == '#' || line[0] == '\0') { |
| 1410 | if (!state->has_tail && !strcmp(line,REDIS_CONFIG_REWRITE_SIGNATURE)) |
| 1411 | state->has_tail = 1; |
| 1412 | rewriteConfigAppendLine(state,line); |
| 1413 | continue; |
| 1414 | } |
| 1415 | |
| 1416 | /* Not a comment, split into arguments. */ |
| 1417 | argv = sdssplitargs(line,&argc); |
| 1418 | if (argv == NULL) { |
| 1419 | /* Apparently the line is unparsable for some reason, for |
| 1420 | * instance it may have unbalanced quotes. Load it as a |
| 1421 | * comment. */ |
| 1422 | sds aux = sdsnew("# ??? "); |
| 1423 | aux = sdscatsds(aux,line); |
| 1424 | sdsfree(line); |
| 1425 | rewriteConfigAppendLine(state,aux); |
| 1426 | continue; |
| 1427 | } |
| 1428 | |
| 1429 | sdstolower(argv[0]); /* We only want lowercase config directives. */ |
| 1430 | |
| 1431 | /* Now we populate the state according to the content of this line. |
| 1432 | * Append the line and populate the option -> line numbers map. */ |
| 1433 | rewriteConfigAppendLine(state,line); |
| 1434 | |
| 1435 | /* Translate options using the word "slave" to the corresponding name |
| 1436 | * "replica", before adding such option to the config name -> lines |
| 1437 | * mapping. */ |
| 1438 | char *p = strstr(argv[0],"slave"); |
| 1439 | if (p) { |
| 1440 | sds alt = sdsempty(); |
| 1441 | alt = sdscatlen(alt,argv[0],p-argv[0]); |
no test coverage detected