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. */
| 1132 | * If it is impossible to read the old file, NULL is returned. |
| 1133 | * If the old file does not exist at all, an empty state is returned. */ |
| 1134 | struct rewriteConfigState *rewriteConfigReadOldFile(char *path) { |
| 1135 | FILE *fp = fopen(path,"r"); |
| 1136 | if (fp == NULL && errno != ENOENT) return NULL; |
| 1137 | |
| 1138 | char buf[CONFIG_MAX_LINE+1]; |
| 1139 | int linenum = -1; |
| 1140 | struct rewriteConfigState *state = zmalloc(sizeof(*state)); |
| 1141 | state->option_to_line = dictCreate(&optionToLineDictType,NULL); |
| 1142 | state->rewritten = dictCreate(&optionSetDictType,NULL); |
| 1143 | state->numlines = 0; |
| 1144 | state->lines = NULL; |
| 1145 | state->has_tail = 0; |
| 1146 | state->force_all = 0; |
| 1147 | if (fp == NULL) return state; |
| 1148 | |
| 1149 | /* Read the old file line by line, populate the state. */ |
| 1150 | while(fgets(buf,CONFIG_MAX_LINE+1,fp) != NULL) { |
| 1151 | int argc; |
| 1152 | sds *argv; |
| 1153 | sds line = sdstrim(sdsnew(buf),"\r\n\t "); |
| 1154 | |
| 1155 | linenum++; /* Zero based, so we init at -1 */ |
| 1156 | |
| 1157 | /* Handle comments and empty lines. */ |
| 1158 | if (line[0] == '#' || line[0] == '\0') { |
| 1159 | if (!state->has_tail && !strcmp(line,REDIS_CONFIG_REWRITE_SIGNATURE)) |
| 1160 | state->has_tail = 1; |
| 1161 | rewriteConfigAppendLine(state,line); |
| 1162 | continue; |
| 1163 | } |
| 1164 | |
| 1165 | /* Not a comment, split into arguments. */ |
| 1166 | argv = sdssplitargs(line,&argc); |
| 1167 | if (argv == NULL) { |
| 1168 | /* Apparently the line is unparsable for some reason, for |
| 1169 | * instance it may have unbalanced quotes. Load it as a |
| 1170 | * comment. */ |
| 1171 | sds aux = sdsnew("# ??? "); |
| 1172 | aux = sdscatsds(aux,line); |
| 1173 | sdsfree(line); |
| 1174 | rewriteConfigAppendLine(state,aux); |
| 1175 | continue; |
| 1176 | } |
| 1177 | |
| 1178 | sdstolower(argv[0]); /* We only want lowercase config directives. */ |
| 1179 | |
| 1180 | /* Now we populate the state according to the content of this line. |
| 1181 | * Append the line and populate the option -> line numbers map. */ |
| 1182 | rewriteConfigAppendLine(state,line); |
| 1183 | |
| 1184 | /* Translate options using the word "slave" to the corresponding name |
| 1185 | * "replica", before adding such option to the config name -> lines |
| 1186 | * mapping. */ |
| 1187 | char *p = strstr(argv[0],"slave"); |
| 1188 | if (p) { |
| 1189 | sds alt = sdsempty(); |
| 1190 | alt = sdscatlen(alt,argv[0],p-argv[0]); |
| 1191 | alt = sdscatlen(alt,"replica",7); |
no test coverage detected