]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Don't release the loaded slurm when there's an error applying it
authorpcarana <pc.moreno2099@gmail.com>
Wed, 22 Apr 2020 17:43:07 +0000 (12:43 -0500)
committerpcarana <pc.moreno2099@gmail.com>
Wed, 22 Apr 2020 17:43:07 +0000 (12:43 -0500)
src/rtr/db/db_table.c
src/slurm/slurm_loader.c

index 6d4cd52849a54155d3464bb3bd991c016b18923f..195f6c2ac5f798110dd32ff8eda68e79de675e91 100644 (file)
@@ -190,17 +190,21 @@ db_table_merge(struct db_table *dst, struct db_table *src)
 int
 db_table_clone(struct db_table **dst, struct db_table *src)
 {
+       struct db_table *result;
        int error;
 
-       *dst = db_table_create();
-       if (*dst == NULL)
+       result = db_table_create();
+       if (result == NULL)
                return pr_enomem();
 
-       error = db_table_merge(*dst, src);
-       if (error)
-               free(*dst);
+       error = db_table_merge(result, src);
+       if (error) {
+               db_table_destroy(result);
+               return error;
+       }
 
-       return error;
+       *dst = result;
+       return 0;
 }
 
 unsigned int
index 00395e4d5e10c3aff210b99aaa3700463381ed6a..9305daec794ed6333112ac6ed62d2f987f796c1d 100644 (file)
@@ -19,7 +19,7 @@
  * slurm db from @params is set as NULL.
  */
 static int
-slurm_load(struct slurm_parser_params *params)
+load_slurm_files(struct slurm_parser_params *params)
 {
        struct db_slurm *db;
        int error;
@@ -196,23 +196,15 @@ slurm_load_checksums(struct slurm_csum_list *csum_list)
        return 0;
 }
 
+/* Returns whether a new slurm was allocated */
 static void
-__slurm_apply(struct db_slurm **last_slurm, struct slurm_parser_params *params,
-    struct slurm_csum_list *csum_list)
+__load_slurm_files(struct db_slurm **last_slurm,
+    struct slurm_parser_params *params, struct slurm_csum_list *csum_list)
 {
        int error;
 
-       error = slurm_load(params);
-       if (!error) {
-               /* Use new SLURM as last valid slurm */
-               if (*last_slurm != NULL)
-                       db_slurm_destroy(*last_slurm);
-               *last_slurm = params->db_slurm;
-               if (*last_slurm != NULL) {
-                       db_slurm_update_time(*last_slurm);
-                       db_slurm_set_csum_list(*last_slurm, csum_list);
-               }
-       } else {
+       error = load_slurm_files(params);
+       if (error) {
                /* Any error: use last valid SLURM */
                pr_warn("Error loading SLURM, the validation will still continue.");
                if (*last_slurm != NULL) {
@@ -222,6 +214,17 @@ __slurm_apply(struct db_slurm **last_slurm, struct slurm_parser_params *params,
                        db_slurm_log(params->db_slurm);
                }
                destroy_local_csum_list(csum_list);
+               return;
+       }
+
+       /* Use new SLURM as last valid slurm */
+       if (*last_slurm != NULL)
+               db_slurm_destroy(*last_slurm);
+
+       *last_slurm = params->db_slurm;
+       if (*last_slurm != NULL) {
+               db_slurm_update_time(*last_slurm);
+               db_slurm_set_csum_list(*last_slurm, csum_list);
        }
 }
 
@@ -258,16 +261,16 @@ are_csum_lists_equals(struct slurm_csum_list *new_list,
        return true;
 }
 
-int
-slurm_apply(struct db_table **base, struct db_slurm **last_slurm)
+/* Load SLURM file(s) that have updates */
+static int
+load_updated_slurm(struct db_slurm **last_slurm,
+    struct slurm_parser_params *params)
 {
-       struct slurm_parser_params *params = NULL;
        struct slurm_csum_list csum_list, old_csum_list;
        int error;
-       bool list_equals = false;
+       bool list_equals;
 
-       if (config_get_slurm() == NULL)
-               return 0;
+       list_equals = false;
 
        pr_info("Checking if there are new or modified SLURM files");
        error = slurm_load_checksums(&csum_list);
@@ -279,28 +282,48 @@ slurm_apply(struct db_table **base, struct db_slurm **last_slurm)
                list_equals = are_csum_lists_equals(&csum_list, &old_csum_list);
        }
 
-       error = slurm_create_parser_params(&params);
-       if (error)
-               return error;
-
        if (list_equals) {
                if (*last_slurm != NULL) {
                        pr_info("Applying same old SLURM, no changes found.");
                        params->db_slurm = *last_slurm;
                }
                destroy_local_csum_list(&csum_list);
-       } else {
-               if (csum_list.list_size == 0) {
-                       if (*last_slurm != NULL)
-                               db_slurm_destroy(*last_slurm);
-                       *last_slurm = params->db_slurm;
-                       /* Empty DIR or FILE SLURM not found */
-                       goto success;
-               }
-               pr_info("Applying configured SLURM");
-               __slurm_apply(last_slurm, params, &csum_list);
+               return 0;
        }
 
+       /* Empty DIR or FILE SLURM not found */
+       if (csum_list.list_size == 0) {
+               if (*last_slurm != NULL)
+                       db_slurm_destroy(*last_slurm);
+               *last_slurm = NULL;
+               params->db_slurm = NULL;
+               return 0;
+       }
+
+       pr_info("Applying configured SLURM");
+       __load_slurm_files(last_slurm, params, &csum_list);
+
+       return 0;
+}
+
+int
+slurm_apply(struct db_table **base, struct db_slurm **last_slurm)
+{
+       struct slurm_parser_params *params;
+       int error;
+
+       if (config_get_slurm() == NULL)
+               return 0;
+
+       params = NULL;
+       error = slurm_create_parser_params(&params);
+       if (error)
+               return error;
+
+       error = load_updated_slurm(last_slurm, params);
+       if (error)
+               goto release_params;
+
        /* If there's no new SLURM loaded, stop */
        if (params->db_slurm == NULL)
                goto success;
@@ -308,7 +331,7 @@ slurm_apply(struct db_table **base, struct db_slurm **last_slurm)
        /* Deep copy of the base so that updates can be reverted */
        error = db_table_clone(&params->db_table, *base);
        if (error)
-               goto release_slurm;
+               goto release_params;
 
        error = db_table_foreach_roa(params->db_table, slurm_pfx_filters_apply,
            params);
@@ -336,8 +359,7 @@ success:
        return 0;
 release_table:
        db_table_destroy(params->db_table);
-release_slurm:
-       db_slurm_destroy(params->db_slurm);
+release_params:
        free(params);
        return error;
 }