Reorganise create and remove functionality into functions for later reuse in calls from multiple places. Signed-off-by: Ioanna Alifieraki <ioanna-maria.alifieraki@xxxxxxxxxxxxx> --- src/security/virt-aa-helper.c | 55 ++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/src/security/virt-aa-helper.c b/src/security/virt-aa-helper.c index 77b2307594..269c372704 100644 --- a/src/security/virt-aa-helper.c +++ b/src/security/virt-aa-helper.c @@ -262,7 +262,7 @@ update_include_file(const char *include_file, const char *included_files, * Create a profile based on a template */ static int -create_profile(const char *profile, const char *profile_name, +_create_profile(const char *profile, const char *profile_name, const char *profile_files, int virtType) { g_autofree char *template = NULL; @@ -1437,6 +1437,36 @@ vahParseArgv(vahControl * ctl, int argc, char **argv) return 0; } +static int create_profile(vahControl *ctl, char *profile, char *include_file) +{ + + int rc = 0; + char *tmp = g_strdup_printf(" #include <libvirt/%s.files>\n", ctl->uuid); + if (ctl->dryrun) { + vah_info(profile); + vah_info(ctl->uuid); + vah_info(tmp); + } else if ((rc = _create_profile(profile, ctl->uuid, tmp, + ctl->def->virtType)) != 0) { + unlink(include_file); + rc = -1; + } + VIR_FREE(tmp); + + return rc; +} + +static int remove_profile(vahControl *ctl, char *include_file) +{ + int rc = 0; + + if ((rc = parserRemove(ctl->uuid)) != 0) + return rc; + if (ctl->cmd == 'D') + unlink(include_file); + + return rc; +} /* * virt-aa-helper -c -u UUID < file.xml @@ -1489,9 +1519,7 @@ main(int argc, char **argv) if (ctl->cmd == 'a') { rc = parserLoad(ctl->uuid); } else if (ctl->cmd == 'R' || ctl->cmd == 'D') { - rc = parserRemove(ctl->uuid); - if (ctl->cmd == 'D') - unlink(include_file); + rc = remove_profile(ctl, include_file); } else if (ctl->cmd == 'c' || ctl->cmd == 'r') { char *included_files = NULL; g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; @@ -1538,22 +1566,9 @@ main(int argc, char **argv) /* create the profile from TEMPLATE */ - if (ctl->cmd == 'c') { - char *tmp = NULL; - tmp = g_strdup_printf(" #include <libvirt/%s.files>\n", ctl->uuid); - - if (ctl->dryrun) { - vah_info(profile); - vah_info(ctl->uuid); - vah_info(tmp); - rc = 0; - } else if ((rc = create_profile(profile, ctl->uuid, tmp, - ctl->def->virtType)) != 0) { - vah_error(ctl, 0, _("could not create profile")); - unlink(include_file); - } - VIR_FREE(tmp); - } + if (ctl->cmd == 'c') + if ((rc = create_profile(ctl, profile, include_file)) != 0) + vah_error(ctl, 0, _("could not create profile")); if (rc == 0 && !ctl->dryrun) { if (ctl->cmd == 'c') -- 2.17.1