master
c 114 lines 3.13 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "inicfg_internals.h"
4
5 void inicfg_section_destroy_non_loaded(struct config *root, const char *section)
6 {
7 struct config_section *sect;
8 struct config_option *opt;
9
10 netdata_log_debug(D_CONFIG, "Destroying section '%s'.", section);
11
12 sect = inicfg_section_find(root, section);
13 if(!sect) {
14 netdata_log_error("Could not destroy section '%s'. Not found.", section);
15 return;
16 }
17
18 SECTION_LOCK(sect);
19
20 // find if there is any loaded option
21 for(opt = sect->values; opt; opt = opt->next) {
22 if (opt->flags & CONFIG_VALUE_LOADED) {
23 // do not destroy values that were loaded from the configuration files.
24 SECTION_UNLOCK(sect);
25 return;
26 }
27 }
28
29 // no option is loaded, free them all
30 inicfg_section_remove_and_delete(root, sect, false, true);
31 }
32
33 void inicfg_section_option_destroy_non_loaded(struct config *root, const char *section, const char *name) {
34 struct config_section *sect;
35 sect = inicfg_section_find(root, section);
36 if (!sect) {
37 netdata_log_error("Could not destroy section option '%s -> %s'. The section not found.", section, name);
38 return;
39 }
40
41 SECTION_LOCK(sect);
42
43 struct config_option *opt = inicfg_option_find(sect, name);
44 if (opt && opt->flags & CONFIG_VALUE_LOADED) {
45 SECTION_UNLOCK(sect);
46 return;
47 }
48
49 if (unlikely(!(opt && inicfg_option_del(sect, opt)))) {
50 SECTION_UNLOCK(sect);
51 netdata_log_error("Could not destroy section option '%s -> %s'. The option not found.", section, name);
52 return;
53 }
54
55 DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(sect->values, opt, prev, next);
56
57 inicfg_option_free(opt);
58 SECTION_UNLOCK(sect);
59 }
60
61 /**
62 * Free all config memory
63 *
64 * This function frees all memory associated with a config structure,
65 * including all sections and options.
66 *
67 * @param root The config structure to free
68 */
69 void inicfg_free(struct config *root) {
70 if (!root)
71 return;
72
73 nd_log(NDLS_DAEMON, NDLP_DEBUG, "Freeing config memory");
74
75 // First let's free the linked list (this will properly free all sections and options)
76 APPCONFIG_LOCK(root);
77 struct config_section *sect = root->sections;
78
79 while (sect) {
80 struct config_section *next_sect = sect->next;
81
82 // Remove and free all options in this section
83 SECTION_LOCK(sect);
84 struct config_option *opt = sect->values;
85 while (opt) {
86 struct config_option *next_opt = opt->next;
87
88 // Remove from index
89 if(inicfg_option_del(sect, opt)) { ; }
90
91 // Free the option
92 inicfg_option_free(opt);
93
94 opt = next_opt;
95 }
96 SECTION_UNLOCK(sect);
97
98 // Remove from index
99 if(inicfg_section_del(root, sect)) { ; }
100
101 // Free the section
102 inicfg_section_free(sect);
103
104 sect = next_sect;
105 }
106
107 // Reset the sections pointer
108 root->sections = NULL;
109 APPCONFIG_UNLOCK(root);
110
111 // Destroy the tree
112 avl_destroy_lock(&root->index);
113 }
114