From: Alan T. DeKok Date: Fri, 6 Jul 2018 15:48:43 +0000 (-0400) Subject: expose cf_section_write() X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a0855a3edf266b2ead2c2ac7c3d34e6be33c7b3d;p=thirdparty%2Ffreeradius-server.git expose cf_section_write() --- diff --git a/src/include/cf_file.h b/src/include/cf_file.h index b7a61726f78..007509bbcf4 100644 --- a/src/include/cf_file.h +++ b/src/include/cf_file.h @@ -54,9 +54,7 @@ int cf_file_changed(CONF_SECTION *cs, rb_walker_t callback); /* * Config file writing */ -#ifdef WITH_CONF_WRITE -size_t cf_section_write(FILE *fp, CONF_SECTION *cs, int depth); -#endif +int cf_section_write(FILE *fp, CONF_SECTION *cs, int depth); /* * Misc diff --git a/src/main/cf_file.c b/src/main/cf_file.c index 08eb8665a4a..69e319f4bd3 100644 --- a/src/main/cf_file.c +++ b/src/main/cf_file.c @@ -1738,6 +1738,43 @@ int cf_file_changed(CONF_SECTION *cs, rb_walker_t callback) } #ifdef WITH_CONF_WRITE +static FILE *cf_file_write(CONF_SECTION *cs, char const *filename) +{ + FILE *fp; + char *p; + char const *q; + char buffer[8192]; + + q = filename; + if ((q[0] == '.') && (q[1] == '/')) q += 2; + + snprintf(buffer, sizeof(buffer), "%s/%s", main_config->write_dir, q); + + p = strrchr(buffer, '/'); + *p = '\0'; + if ((rad_mkdir(buffer, 0700, -1, -1) < 0) && + (errno != EEXIST)) { + cf_log_err(cs, "Failed creating directory %s: %s", + buffer, strerror(errno)); + return NULL; + } + + /* + * And again, because rad_mkdir() butchers the buffer. + */ + snprintf(buffer, sizeof(buffer), "%s/%s", main_config->write_dir, q); + + fp = fopen(buffer, "a"); + if (!fp) { + cf_log_err(cs, "Failed creating file %s: %s", + buffer, strerror(errno)); + return NULL; + } + + return fp; +} +#endif /* WITH_CONF_WRITE */ + static char const parse_tabs[] = " "; static ssize_t cf_string_write(FILE *fp, char const *string, size_t len, FR_TOKEN t) @@ -1773,7 +1810,7 @@ static ssize_t cf_string_write(FILE *fp, char const *string, size_t len, FR_TOKE return 1; } -static size_t cf_pair_write(FILE *fp, CONF_PAIR *cp) +static int cf_pair_write(FILE *fp, CONF_PAIR *cp) { if (!cp->value) { fprintf(fp, "%s\n", cp->attr); @@ -1782,49 +1819,14 @@ static size_t cf_pair_write(FILE *fp, CONF_PAIR *cp) cf_string_write(fp, cp->attr, strlen(cp->attr), cp->lhs_quote); fprintf(fp, " %s ", fr_int2str(fr_tokens_table, cp->op, "")); - cf_string_write(fp, cp->orig_value, strlen(cp->orig_value), cp->rhs_quote); + cf_string_write(fp, cp->value, strlen(cp->value), cp->rhs_quote); fprintf(fp, "\n"); return 1; /* FIXME */ } -static FILE *cf_file_write(CONF_SECTION *cs, char const *filename) -{ - FILE *fp; - char *p; - char const *q; - char buffer[8192]; - - q = filename; - if ((q[0] == '.') && (q[1] == '/')) q += 2; - - snprintf(buffer, sizeof(buffer), "%s/%s", main_config->write_dir, q); - - p = strrchr(buffer, '/'); - *p = '\0'; - if ((rad_mkdir(buffer, 0700, -1, -1) < 0) && - (errno != EEXIST)) { - cf_log_err(cs, "Failed creating directory %s: %s", - buffer, strerror(errno)); - return NULL; - } - - /* - * And again, because rad_mkdir() butchers the buffer. - */ - snprintf(buffer, sizeof(buffer), "%s/%s", main_config->write_dir, q); - - fp = fopen(buffer, "a"); - if (!fp) { - cf_log_err(cs, "Failed creating file %s: %s", - buffer, strerror(errno)); - return NULL; - } - - return fp; -} -size_t cf_section_write(FILE *in_fp, CONF_SECTION *cs, int depth) +int cf_section_write(FILE *in_fp, CONF_SECTION *cs, int depth) { bool prev = false; CONF_ITEM *ci; @@ -1900,60 +1902,6 @@ size_t cf_section_write(FILE *in_fp, CONF_SECTION *cs, int depth) prev = true; break; - case CONF_ITEM_COMMENT: - rad_assert(fp != NULL); - - prev = false; - fwrite(parse_tabs, depth + 1, 1, fp); - fprintf(fp, "#%s", ((CONF_COMMENT *)ci)->comment); - break; - - case CONF_ITEM_INCLUDE: - /* - * Filename == open the new filename and use that. - * - * NULL == close the previous filename - */ - if (((CONF_INCLUDE *) ci)->filename) { - CONF_INCLUDE *cc = (CONF_INCLUDE *) ci; - - /* - * Print out - * - * $INCLUDE foo.conf - * $INCLUDE foo/ - * - * but not the files included from the last one. - */ - if (fp && (cc->file_type != CONF_INCLUDE_FROMDIR)) { - fprintf(fp, "$INCLUDE %s\n", ((CONF_INCLUDE *)ci)->filename); - } - - /* - * If it's a file, we write the - * file. We ignore the - * directories. They're just for printing. - */ - if (cc->file_type != CONF_INCLUDE_DIR) { - fp = cf_file_write(cs, ((CONF_INCLUDE *) ci)->filename); - if (!fp) return 0; - - fp_max++; - array[fp_max] = fp; - } - } else { - /* - * We're done the current file. - */ - rad_assert(fp != NULL); - rad_assert(fp_max > 0); - fclose(fp); - - fp_max--; - fp = array[fp_max]; - } - break; - default: break; } @@ -1966,7 +1914,6 @@ size_t cf_section_write(FILE *in_fp, CONF_SECTION *cs, int depth) return 1; } -#endif /* WITH_CONF_WRITE */