Save uci changes whenever a set/add/del operation is called

This commit is contained in:
Amin Ben Romdhane 2025-01-20 16:28:47 +01:00
parent 95c7f85234
commit bfe8db9f6d
3 changed files with 102 additions and 145 deletions

View file

@ -230,7 +230,6 @@ int bbf_entry_method(struct dmctx *ctx, int cmd)
break;
}
dmuci_save();
return bbf_fault_map(ctx, fault);
}

View file

@ -21,6 +21,10 @@ static struct uci_context *uci_ctx = NULL;
NEW_UCI_PATH(bbfdm)
NEW_UCI_PATH(varstate)
static const char *config_dir = "/etc/config/";
static const char *bbfdm_dir = "/etc/bbfdm/dmmap/";
static const char *varstate_dir = "/var/state/";
static void bbfdm_uci_init_ctx(struct uci_context **uci_ctx, const char *confdir, const char *savedir)
{
*uci_ctx = uci_alloc_context();
@ -36,17 +40,17 @@ static void bbfdm_uci_init_ctx(struct uci_context **uci_ctx, const char *confdir
void bbfdm_uci_init(struct dmctx *bbf_ctx)
{
if (bbf_ctx->dm_type == BBFDM_CWMP) {
bbfdm_uci_init_ctx(&bbf_ctx->config_uci_ctx, "/etc/config/", "/tmp/bbfdm/.cwmp/config/");
bbfdm_uci_init_ctx(&bbf_ctx->dmmap_uci_ctx, "/etc/bbfdm/dmmap/", "/tmp/bbfdm/.cwmp/dmmap/");
bbfdm_uci_init_ctx(&bbf_ctx->config_uci_ctx, config_dir, "/tmp/bbfdm/.cwmp/config/");
bbfdm_uci_init_ctx(&bbf_ctx->dmmap_uci_ctx, bbfdm_dir, "/tmp/bbfdm/.cwmp/dmmap/");
} else if (bbf_ctx->dm_type == BBFDM_USP) {
bbfdm_uci_init_ctx(&bbf_ctx->config_uci_ctx, "/etc/config/", "/tmp/bbfdm/.usp/config/");
bbfdm_uci_init_ctx(&bbf_ctx->dmmap_uci_ctx, "/etc/bbfdm/dmmap/", "/tmp/bbfdm/.usp/dmmap/");
bbfdm_uci_init_ctx(&bbf_ctx->config_uci_ctx, config_dir, "/tmp/bbfdm/.usp/config/");
bbfdm_uci_init_ctx(&bbf_ctx->dmmap_uci_ctx, bbfdm_dir, "/tmp/bbfdm/.usp/dmmap/");
} else {
bbfdm_uci_init_ctx(&bbf_ctx->config_uci_ctx, "/etc/config/", "/tmp/bbfdm/.bbfdm/config/");
bbfdm_uci_init_ctx(&bbf_ctx->dmmap_uci_ctx, "/etc/bbfdm/dmmap/", "/tmp/bbfdm/.bbfdm/dmmap/");
bbfdm_uci_init_ctx(&bbf_ctx->config_uci_ctx, config_dir, "/tmp/bbfdm/.bbfdm/config/");
bbfdm_uci_init_ctx(&bbf_ctx->dmmap_uci_ctx, bbfdm_dir, "/tmp/bbfdm/.bbfdm/dmmap/");
}
bbfdm_uci_init_ctx(&bbf_ctx->varstate_uci_ctx, "/var/state/", "/tmp/bbfdm/.varstate/");
bbfdm_uci_init_ctx(&bbf_ctx->varstate_uci_ctx, varstate_dir, "/tmp/bbfdm/.varstate/");
uci_ctx = bbf_ctx->config_uci_ctx;
uci_ctx_bbfdm = bbf_ctx->dmmap_uci_ctx;
@ -74,6 +78,29 @@ void bbfdm_uci_exit(struct dmctx *bbf_ctx)
}
}
static struct uci_context *get_uci_context_by_section(struct uci_section *section)
{
size_t config_dir_len = strlen(config_dir);
size_t bbfdm_dir_len = strlen(bbfdm_dir);
size_t varstate_dir_len = strlen(varstate_dir);
if (section && section->package && section->package->ctx) {
const char *confdir = section->package->ctx->confdir;
if (DM_STRNCMP(confdir, config_dir, config_dir_len) == 0) {
return uci_ctx;
} else if (DM_STRNCMP(confdir, bbfdm_dir, bbfdm_dir_len) == 0) {
return uci_ctx_bbfdm;
} else if (DM_STRNCMP(confdir, varstate_dir, varstate_dir_len) == 0) {
return uci_ctx_varstate;
} else {
return uci_ctx;
}
}
return uci_ctx;
}
char *dmuci_list_to_string(struct uci_list *list, const char *delimitor)
{
if (list) {
@ -362,63 +389,6 @@ int dmuci_commit(void)
return 0;
}
/**** UCI SAVE *****/
int dmuci_save_package(char *package)
{
struct uci_ptr ptr = {0};
if (uci_lookup_ptr(uci_ctx, &ptr, package, true) != UCI_OK)
return -1;
if (uci_save(uci_ctx, ptr.p) != UCI_OK)
return -1;
return 0;
}
int dmuci_save(void)
{
char **configs = NULL;
char **bbfdm_configs = NULL;
char **p;
int rc = 0;
if (uci_list_configs(uci_ctx, &configs) != UCI_OK) {
rc = -1;
goto end;
}
if (!configs) {
rc = -1;
goto end;
}
for (p = configs; *p; p++)
dmuci_save_package(*p);
if (uci_ctx_bbfdm) {
if (uci_list_configs(uci_ctx_bbfdm, &bbfdm_configs) != UCI_OK) {
rc = -1;
goto out;
}
if (!bbfdm_configs) {
rc = -1;
goto out;
}
for (p = bbfdm_configs; *p; p++)
dmuci_save_package_bbfdm(*p);
free(bbfdm_configs);
}
out:
free(configs);
end:
return rc;
}
/**** UCI REVERT *****/
int dmuci_revert_package(char *package)
{
@ -433,23 +403,6 @@ int dmuci_revert_package(char *package)
return 0;
}
int dmuci_revert(void)
{
char **configs = NULL;
char **p;
if (uci_list_configs(uci_ctx, &configs) != UCI_OK)
return -1;
if (!configs)
return -1;
for (p = configs; *p; p++)
dmuci_revert_package(*p);
free(configs);
return 0;
}
/**** UCI SET *****/
int dmuci_set_value(const char *package, const char *section, const char *option, const char *value)
@ -462,6 +415,9 @@ int dmuci_set_value(const char *package, const char *section, const char *option
if (uci_set(uci_ctx, &ptr) != UCI_OK)
return -1;
if (uci_save(uci_ctx, ptr.p) != UCI_OK)
return -1;
return 0;
}
@ -476,6 +432,9 @@ int dmuci_add_list_value(const char *package, const char *section, const char *o
if (uci_add_list(uci_ctx, &ptr) != UCI_OK)
return -1;
if (uci_save(uci_ctx, ptr.p) != UCI_OK)
return -1;
return 0;
}
@ -490,6 +449,9 @@ int dmuci_del_list_value(const char *package, const char *section, const char *o
if (uci_del_list(uci_ctx, &ptr) != UCI_OK)
return -1;
if (uci_save(uci_ctx, ptr.p) != UCI_OK)
return -1;
return 0;
}
@ -512,6 +474,9 @@ int dmuci_add_section(const char *package, const char *stype, struct uci_section
if (uci_add_section(uci_ctx, ptr.p, stype, s) != UCI_OK)
return -1;
if (uci_save(uci_ctx, ptr.p) != UCI_OK)
return -1;
return 0;
}
@ -526,6 +491,9 @@ int dmuci_delete(const char *package, const char *section, const char *option, c
if (uci_delete(uci_ctx, &ptr) != UCI_OK)
return -1;
if (uci_save(uci_ctx, ptr.p) != UCI_OK)
return -1;
return 0;
}
@ -540,6 +508,9 @@ int dmuci_rename_section(const char *package, const char *section, const char *v
if (uci_rename(uci_ctx, &ptr) != UCI_OK)
return -1;
if (uci_save(uci_ctx, ptr.p) != UCI_OK)
return -1;
return 0;
}
@ -657,12 +628,16 @@ int dmuci_get_value_by_section_list(struct uci_section *s, const char *option, s
/**** UCI SET by section pointer ****/
int dmuci_set_value_by_section(struct uci_section *s, const char *option, const char *value)
{
struct uci_context *curr_ctx = get_uci_context_by_section(s);
struct uci_ptr up = {0};
if (dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value) == -1)
if (dmuci_lookup_ptr_by_section(curr_ctx, &up, s, option, value) == -1)
return -1;
if (uci_set(uci_ctx, &up) != UCI_OK)
if (uci_set(curr_ctx, &up) != UCI_OK)
return -1;
if (uci_save(curr_ctx, up.p) != UCI_OK)
return -1;
return 0;
@ -671,13 +646,18 @@ int dmuci_set_value_by_section(struct uci_section *s, const char *option, const
/**** UCI DELETE by section pointer *****/
int dmuci_delete_by_section(struct uci_section *s, const char *option, const char *value)
{
struct uci_context *curr_ctx = get_uci_context_by_section(s);
struct uci_ptr up = {0};
uci_ctx->flags |= UCI_FLAG_EXPORT_NAME;
if (dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value) == -1)
curr_ctx->flags |= UCI_FLAG_EXPORT_NAME;
if (dmuci_lookup_ptr_by_section(curr_ctx, &up, s, option, value) == -1)
return -1;
if (uci_delete(uci_ctx, &up) != UCI_OK)
if (uci_delete(curr_ctx, &up) != UCI_OK)
return -1;
if (uci_save(curr_ctx, up.p) != UCI_OK)
return -1;
return 0;
@ -685,12 +665,16 @@ int dmuci_delete_by_section(struct uci_section *s, const char *option, const cha
int dmuci_delete_by_section_unnamed(struct uci_section *s, const char *option, const char *value)
{
struct uci_context *curr_ctx = get_uci_context_by_section(s);
struct uci_ptr up = {0};
if (dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value) == -1)
if (dmuci_lookup_ptr_by_section(curr_ctx, &up, s, option, value) == -1)
return -1;
if (uci_delete(uci_ctx, &up) != UCI_OK)
if (uci_delete(curr_ctx, &up) != UCI_OK)
return -1;
if (uci_save(curr_ctx, up.p) != UCI_OK)
return -1;
return 0;
@ -699,12 +683,16 @@ int dmuci_delete_by_section_unnamed(struct uci_section *s, const char *option, c
/**** UCI ADD LIST by section pointer *****/
int dmuci_add_list_value_by_section(struct uci_section *s, const char *option, const char *value)
{
struct uci_context *curr_ctx = get_uci_context_by_section(s);
struct uci_ptr up = {0};
if (dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value) == -1)
if (dmuci_lookup_ptr_by_section(curr_ctx, &up, s, option, value) == -1)
return -1;
if (uci_add_list(uci_ctx, &up) != UCI_OK)
if (uci_add_list(curr_ctx, &up) != UCI_OK)
return -1;
if (uci_save(curr_ctx, up.p) != UCI_OK)
return -1;
return 0;
@ -713,12 +701,16 @@ int dmuci_add_list_value_by_section(struct uci_section *s, const char *option, c
/**** UCI DEL LIST by section pointer *****/
int dmuci_del_list_value_by_section(struct uci_section *s, const char *option, const char *value)
{
struct uci_context *curr_ctx = get_uci_context_by_section(s);
struct uci_ptr up = {0};
if (dmuci_lookup_ptr_by_section(uci_ctx, &up, s, option, value) == -1)
if (dmuci_lookup_ptr_by_section(curr_ctx, &up, s, option, value) == -1)
return -1;
if (uci_del_list(uci_ctx, &up) != UCI_OK)
if (uci_del_list(curr_ctx, &up) != UCI_OK)
return -1;
if (uci_save(curr_ctx, up.p) != UCI_OK)
return -1;
return 0;
@ -727,12 +719,16 @@ int dmuci_del_list_value_by_section(struct uci_section *s, const char *option, c
/**** UCI RENAME SECTION by section pointer *****/
int dmuci_rename_section_by_section(struct uci_section *s, const char *value)
{
struct uci_context *curr_ctx = get_uci_context_by_section(s);
struct uci_ptr up = {0};
if (dmuci_lookup_ptr_by_section(uci_ctx, &up, s, NULL, value) == -1)
if (dmuci_lookup_ptr_by_section(curr_ctx, &up, s, NULL, value) == -1)
return -1;
if (uci_rename(uci_ctx, &up) != UCI_OK)
if (uci_rename(curr_ctx, &up) != UCI_OK)
return -1;
if (uci_save(curr_ctx, up.p) != UCI_OK)
return -1;
return 0;
@ -741,12 +737,16 @@ int dmuci_rename_section_by_section(struct uci_section *s, const char *value)
/**** UCI REORDER SECTION by section pointer *****/
int dmuci_reoder_section_by_section(struct uci_section *s, char *pos)
{
struct uci_context *curr_ctx = get_uci_context_by_section(s);
struct uci_ptr up = {0};
if (dmuci_lookup_ptr_by_section(uci_ctx, &up, s, NULL, pos) == -1)
if (dmuci_lookup_ptr_by_section(curr_ctx, &up, s, NULL, pos) == -1)
return -1;
if (uci_reorder_section(uci_ctx, up.s, strtoul(up.value, NULL, 10)) != UCI_OK)
if (uci_reorder_section(curr_ctx, up.s, strtoul(up.value, NULL, 10)) != UCI_OK)
return -1;
if (uci_save(curr_ctx, up.p) != UCI_OK)
return -1;
return 0;
@ -860,16 +860,6 @@ int db_get_value_string(const char *package, const char *section, const char *op
return 0;
}
void commit_and_free_uci_ctx_bbfdm(char *dmmap_config)
{
dmuci_commit_package_bbfdm(dmmap_config);
if (uci_ctx_bbfdm) {
uci_free_context(uci_ctx_bbfdm);
uci_ctx_bbfdm = NULL;
}
}
bool dmuci_string_to_boolean(const char *value)
{
if (!value)

View file

@ -264,33 +264,6 @@ int dmuci_commit_##UCI_PATH(void) \
uci_ctx = save_uci_ctx; \
return res; \
}\
int dmuci_revert_##UCI_PATH(void) \
{\
struct uci_context *save_uci_ctx; \
save_uci_ctx = uci_ctx; \
uci_ctx = uci_ctx_##UCI_PATH; \
int res = dmuci_revert(); \
uci_ctx = save_uci_ctx; \
return res; \
}\
int dmuci_save_package_##UCI_PATH(char *package) \
{\
struct uci_context *save_uci_ctx; \
save_uci_ctx = uci_ctx; \
uci_ctx = uci_ctx_##UCI_PATH; \
int res = dmuci_save_package(package); \
uci_ctx = save_uci_ctx; \
return res; \
}\
int dmuci_revert_package_##UCI_PATH(char *package) \
{\
struct uci_context *save_uci_ctx; \
save_uci_ctx = uci_ctx; \
uci_ctx = uci_ctx_##UCI_PATH; \
int res = dmuci_revert_package(package); \
uci_ctx = save_uci_ctx; \
return res; \
}\
int dmuci_delete_by_section_unnamed_##UCI_PATH(struct uci_section *s, const char *option, const char *value)\
{\
struct uci_context *save_uci_ctx; \
@ -311,10 +284,7 @@ int dmuci_export_package(char *package, const char *output_path);
int dmuci_export(const char *output_path);
int dmuci_commit_package(char *package);
int dmuci_commit(void);
int dmuci_save_package(char *package);
int dmuci_save(void);
int dmuci_revert_package(char *package);
int dmuci_revert(void);
int dmuci_get_section_type(const char *package, const char *section, char **value);
int dmuci_get_option_value_string(const char *package, const char *section, const char *option, char **value);
@ -342,25 +312,23 @@ struct uci_section *dmuci_walk_all_sections(const char *package, struct uci_sect
int dmuci_get_option_value_string_bbfdm(const char *package, const char *section, const char *option, char **value);
int dmuci_set_value_bbfdm(const char *package, const char *section, const char *option, const char *value);
int dmuci_set_value_by_section_bbfdm(struct uci_section *s, const char *option, const char *value);
int dmuci_set_value_by_section_varstate(struct uci_section *s, const char *option, const char *value);
int dmuci_add_section_bbfdm(const char *package, const char *stype, struct uci_section **s);
int dmuci_delete_bbfdm(const char *package, const char *section, const char *option, const char *value);
int dmuci_delete_by_section_unnamed_bbfdm(struct uci_section *s, const char *option, const char *value);
int dmuci_delete_by_section_bbfdm(struct uci_section *s, const char *option, const char *value);
int dmuci_delete_by_section_varstate(struct uci_section *s, const char *option, const char *value);
int dmuci_commit_package_bbfdm(char *package);
int dmuci_commit_bbfdm(void);
int dmuci_revert_bbfdm(void);
int dmuci_commit_package_varstate(char *package);
int dmuci_save_package_varstate(char *package);
int dmuci_revert_package_varstate(char *package);
struct uci_section *dmuci_walk_section_bbfdm(const char *package, const char *stype, const void *arg1, const void *arg2, int cmp , int (*filter)(struct uci_section *s, const void *value), struct uci_section *prev_section, int walk);
struct uci_section *dmuci_walk_section_varstate(const char *package, const char *stype, const void *arg1, const void *arg2, int cmp , int (*filter)(struct uci_section *s, const void *value), struct uci_section *prev_section, int walk);
void commit_and_free_uci_ctx_bbfdm(char *dmmap_config);
int dmuci_add_section_varstate(const char *package, const char *stype, struct uci_section **s);
int db_get_value_string(const char *package, const char *section, const char *option, char **value);
int dmuci_delete_by_section_varstate(struct uci_section *s, const char *option, const char *value);
int dmuci_get_option_value_string_varstate(const char *package, const char *section, const char *option, char **value);
int dmuci_set_value_varstate(const char *package, const char *section, const char *option, const char *value);
int dmuci_set_value_by_section_varstate(struct uci_section *s, const char *option, const char *value);
int dmuci_commit_package_varstate(char *package);
int db_get_value_string(const char *package, const char *section, const char *option, char **value);
int dmuci_get_section_name(const char *sec_name, char **value);
int dmuci_set_section_name(const char *sec_name, char *str, size_t size);