From 21da323763355e6ae1e140b9f19c974f48f2fd74 Mon Sep 17 00:00:00 2001 From: Phillip Whelan Date: Thu, 26 Oct 2023 11:38:00 -0300 Subject: [PATCH] in_calyptia_fleet: improve config_commit/add/rollback. Signed-off-by: Phillip Whelan --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 137 +++++++++++++----- 1 file changed, 104 insertions(+), 33 deletions(-) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index af46b758c10..0fe148a2c04 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -490,12 +490,12 @@ static int test_config_is_valid(flb_sds_t cfgpath) } static int parse_config_name_timestamp(struct flb_in_calyptia_fleet_config *ctx, - const char *cfgpath, + const char *cfgpath, long *config_timestamp) { char *ext = NULL; long timestamp; - char realname[4096]; + char realname[4096] = {0}; char *fname; ssize_t len; @@ -1170,28 +1170,53 @@ static struct cfl_array *read_glob(const char *path) static int calyptia_config_add(struct flb_in_calyptia_fleet_config *ctx, const char *cfgname) { - flb_sds_t cfgnewname; - flb_sds_t cfgoldname; + int rc = FLB_FALSE; + + flb_sds_t cfgnewname = NULL; + flb_sds_t cfgoldname = NULL; + flb_sds_t cfgcurname = NULL; cfgnewname = new_fleet_config_filename(ctx); - if (cfgnewname == NULL) { - return -1; + cfgcurname = cur_fleet_config_filename(ctx); + cfgoldname = old_fleet_config_filename(ctx); + + if (cfgnewname == NULL || cfgcurname == NULL || cfgoldname == NULL) { + goto error; } if (exists_new_fleet_config(ctx) == FLB_TRUE) { - cfgoldname = old_fleet_config_filename(ctx); - rename(cfgnewname, cfgoldname); - unlink(cfgnewname); - flb_sds_destroy(cfgoldname); + + if (rename(cfgnewname, cfgoldname)) { + goto error; + } + } + else if (exists_cur_fleet_config(ctx) == FLB_TRUE) { + + if (rename(cfgcurname, cfgoldname)) { + goto error; + } } if (symlink(cfgname, cfgnewname)) { + goto error; + } + + rc = FLB_TRUE; + +error: + if (cfgnewname) { flb_sds_destroy(cfgnewname); - return -1; } - flb_sds_destroy(cfgnewname); - return 0; + if (cfgcurname) { + flb_sds_destroy(cfgcurname); + } + + if (cfgoldname) { + flb_sds_destroy(cfgoldname); + } + + return rc; } static int cfl_array_qsort_ini(const void *arg_a, const void *arg_b) @@ -1258,8 +1283,16 @@ static int calyptia_config_delete_old_dir(const char *cfgpath) unlink(files->entries[idx]->data.as_string); } + /* attempt to delete the main directory */ + ext = strrchr(cfg_glob, '/'); + if (ext) { + *ext = '\0'; + rmdir(cfg_glob); + } + flb_sds_destroy(cfg_glob); cfl_array_destroy(files); + return FLB_TRUE; } @@ -1300,23 +1333,25 @@ static int calyptia_config_delete_old(struct flb_in_calyptia_fleet_config *ctx) cfl_array_destroy(inis); flb_sds_destroy(glob_ini); + return 0; } static int calyptia_config_commit(struct flb_in_calyptia_fleet_config *ctx) { - flb_sds_t cfgnewname; - flb_sds_t cfgcurname; - flb_sds_t cfgoldname; - flb_sds_t cfgtimename; + int rc = FLB_FALSE; + flb_sds_t cfgnewname = NULL; + flb_sds_t cfgcurname = NULL; + flb_sds_t cfgoldname = NULL; cfgnewname = new_fleet_config_filename(ctx); cfgcurname = cur_fleet_config_filename(ctx); cfgoldname = old_fleet_config_filename(ctx); - cfgtimename = time_fleet_config_filename(ctx, ctx->config_timestamp); - if (exists_new_fleet_config(ctx) == FLB_TRUE) { - unlink(cfgnewname); + if (cfgnewname == NULL || + cfgcurname == NULL || + cfgoldname == NULL) { + goto error; } if (exists_old_fleet_config(ctx) == FLB_TRUE) { @@ -1324,24 +1359,40 @@ static int calyptia_config_commit(struct flb_in_calyptia_fleet_config *ctx) } if (exists_cur_fleet_config(ctx) == FLB_TRUE) { - unlink(cfgcurname); + if (rename(cfgcurname, cfgoldname)) { + goto error; + } } - symlink(cfgtimename, cfgcurname); - - flb_sds_destroy(cfgnewname); - flb_sds_destroy(cfgcurname); - flb_sds_destroy(cfgoldname); - flb_sds_destroy(cfgtimename); + if (exists_new_fleet_config(ctx) == FLB_TRUE) { + if (rename(cfgnewname, cfgcurname)) { + goto error; + } + } calyptia_config_delete_old(ctx); + rc = FLB_TRUE; - return 0; +error: + if (cfgnewname) { + flb_sds_destroy(cfgnewname); + } + + if (cfgcurname) { + flb_sds_destroy(cfgcurname); + } + + if (cfgoldname) { + flb_sds_destroy(cfgoldname); + } + + return rc; } static int calyptia_config_rollback(struct flb_in_calyptia_fleet_config *ctx, const char *cfgname) { + int rc = FLB_TRUE; flb_sds_t cfgnewname; flb_sds_t cfgcurname; flb_sds_t cfgoldname; @@ -1350,6 +1401,10 @@ static int calyptia_config_rollback(struct flb_in_calyptia_fleet_config *ctx, cfgcurname = cur_fleet_config_filename(ctx); cfgoldname = old_fleet_config_filename(ctx); + if (cfgnewname == NULL || cfgcurname == NULL || cfgoldname == NULL) { + goto error; + } + if (exists_new_fleet_config(ctx) == FLB_TRUE) { unlink(cfgnewname); } @@ -1358,11 +1413,22 @@ static int calyptia_config_rollback(struct flb_in_calyptia_fleet_config *ctx, rename(cfgoldname, cfgcurname); } - flb_sds_destroy(cfgnewname); - flb_sds_destroy(cfgcurname); - flb_sds_destroy(cfgoldname); + rc = FLB_TRUE; - return 0; +error: + if (cfgnewname) { + flb_sds_destroy(cfgnewname); + } + + if (cfgcurname) { + flb_sds_destroy(cfgcurname); + } + + if (cfgoldname) { + flb_sds_destroy(cfgoldname); + } + + return rc; } static int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx, @@ -1438,7 +1504,12 @@ static int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx, get_calyptia_files(ctx, u_conn, ctx->fleet_files_url, time_last_modified); cfgname = time_fleet_config_filename(ctx, time_last_modified); - calyptia_config_add(ctx, cfgname); + + if (calyptia_config_add(ctx, cfgname) == FLB_FALSE) { + flb_sds_destroy(cfgname); + return -1; + } + flb_sds_destroy(cfgname); cfgnewname = new_fleet_config_filename(ctx);