From a63b96b7e2d3edb4b3b1283558da7ca6735d66da Mon Sep 17 00:00:00 2001 From: Jason Zaman Date: Sat, 30 Apr 2016 15:58:06 +0800 Subject: [PATCH] genhomedircon: factor out common replacement code All the write_*_contexts() methods use exactly the same code. This splits it off into a common helper function. Signed-off-by: Jason Zaman --- libsemanage/src/genhomedircon.c | 74 ++++++++++++--------------------- 1 file changed, 26 insertions(+), 48 deletions(-) diff --git a/libsemanage/src/genhomedircon.c b/libsemanage/src/genhomedircon.c index 1a9e87e6..09c2a107 100644 --- a/libsemanage/src/genhomedircon.c +++ b/libsemanage/src/genhomedircon.c @@ -548,6 +548,29 @@ static int check_line(genhomedircon_settings_t * s, Ustr *line) return result; } +static int write_replacements(genhomedircon_settings_t * s, FILE * out, + const semanage_list_t * tpl, + const replacement_pair_t *repl) +{ + Ustr *line = USTR_NULL; + + for (; tpl; tpl = tpl->next) { + line = replace_all(tpl->data, repl); + if (!line) + goto fail; + if (check_line(s, line) == STATUS_SUCCESS) { + if (!ustr_io_putfileline(&line, out)) + goto fail; + } + ustr_sc_free(&line); + } + return STATUS_SUCCESS; + + fail: + ustr_sc_free(&line); + return STATUS_ERR; +} + static int write_home_dir_context(genhomedircon_settings_t * s, FILE * out, semanage_list_t * tpl, const char *user, const char *seuser, const char *home, @@ -560,26 +583,11 @@ static int write_home_dir_context(genhomedircon_settings_t * s, FILE * out, {.search_for = TEMPLATE_LEVEL,.replace_with = level}, {NULL, NULL} }; - Ustr *line = USTR_NULL; if (fprintf(out, COMMENT_USER_HOME_CONTEXT, user) < 0) return STATUS_ERR; - for (; tpl; tpl = tpl->next) { - line = replace_all(tpl->data, repl); - if (!line) - goto fail; - if (check_line(s, line) == STATUS_SUCCESS) { - if (!ustr_io_putfileline(&line, out)) - goto fail; - } - ustr_sc_free(&line); - } - return STATUS_SUCCESS; - - fail: - ustr_sc_free(&line); - return STATUS_ERR; + return write_replacements(s, out, tpl, repl); } static int write_home_root_context(genhomedircon_settings_t * s, FILE * out, @@ -589,23 +597,8 @@ static int write_home_root_context(genhomedircon_settings_t * s, FILE * out, {.search_for = TEMPLATE_HOME_ROOT,.replace_with = homedir}, {NULL, NULL} }; - Ustr *line = USTR_NULL; - for (; tpl; tpl = tpl->next) { - line = replace_all(tpl->data, repl); - if (!line) - goto fail; - if (check_line(s, line) == STATUS_SUCCESS) { - if (!ustr_io_putfileline(&line, out)) - goto fail; - } - ustr_sc_free(&line); - } - return STATUS_SUCCESS; - - fail: - ustr_sc_free(&line); - return STATUS_ERR; + return write_replacements(s, out, tpl, repl); } static int write_user_context(genhomedircon_settings_t * s, FILE * out, @@ -618,23 +611,8 @@ static int write_user_context(genhomedircon_settings_t * s, FILE * out, {.search_for = TEMPLATE_SEUSER,.replace_with = seuser}, {NULL, NULL} }; - Ustr *line = USTR_NULL; - for (; tpl; tpl = tpl->next) { - line = replace_all(tpl->data, repl); - if (!line) - goto fail; - if (check_line(s, line) == STATUS_SUCCESS) { - if (!ustr_io_putfileline(&line, out)) - goto fail; - } - ustr_sc_free(&line); - } - return STATUS_SUCCESS; - - fail: - ustr_sc_free(&line); - return STATUS_ERR; + return write_replacements(s, out, tpl, repl); } static int user_sort_func(semanage_user_t ** arg1, semanage_user_t ** arg2)