]> pilppa.org Git - libplp.git/commitdiff
revert previous save_immediately flag
authorMika Laitio <lamikr@pilppa.org>
Sat, 16 Apr 2011 14:44:10 +0000 (17:44 +0300)
committerMika Laitio <lamikr@pilppa.org>
Sat, 16 Apr 2011 14:44:10 +0000 (17:44 +0300)
could not get working the save of variables whose value were not saved
immediately after setting them.

Signed-off-by: Mika Laitio <lamikr@pilppa.org>
src/DeviceConfig.cc
src/DeviceConfig.hh
src/config.c
src/config.h
src_test/test_config.c

index b663259088aeba93fa22ff566e074dd1746989d8..18870b7473bd1ab967dde5d1ba6d8113f7e94c91 100644 (file)
@@ -7,7 +7,6 @@
 
 #include "DeviceConfig.hh"
 
-#include <stdbool.h>
 #include <string.h>
 #include <malloc.h>
 
@@ -21,14 +20,153 @@ using namespace plp;
 
 string DeviceConfig::store_base_dir    = DEFAULT_STORAGE_BASE_DIR;
 
-ConfigHandle::ConfigHandle(uci_context *ctx_param, uci_package *pkg_param) {
-       _ctx    = ctx_param;
-       _pkg    = pkg_param;
+ConfigHandle::ConfigHandle(uci_context *ctx_param,
+                       uci_package *pkg_param,
+                       const char *short_fname_param,
+                       const char *full_fname_param) {
+       _ctx            = ctx_param;
+       _pkg            = pkg_param;
+       short_fname     = strdup(short_fname_param);
+       full_fname      = strdup(full_fname_param);
 }
 
 ConfigHandle::~ConfigHandle() {
        uci_unload(_ctx, _pkg);
        uci_free_context(_ctx);
+       free(short_fname);
+       free(full_fname);
+}
+
+static int uci_create_named_section(ConfigHandle *cfg_handle,
+                               const char *section_type,
+                               const char *section_name,
+                               bool save_immediately)
+{
+       struct uci_ptr  ptr;
+       int             ret_val;
+       char            *cmd_dta;
+       int             len;
+
+       ret_val = -1;
+       if ((cfg_handle != NULL) &&
+           (cfg_handle->_ctx != NULL) &&
+           (cfg_handle->short_fname != NULL) &&
+           (cfg_handle->full_fname != NULL) &&
+           (section_type != NULL) &&
+           (section_name != NULL)) {
+               len     = strlen(cfg_handle->short_fname);
+               len     = len + 1;
+               len     = len + strlen(section_type);
+               len     = len + 1;
+               len     = len + strlen(section_name);
+               len     = len + 1;
+               cmd_dta = (char *)malloc(len);
+               if (cmd_dta != NULL) {
+                       snprintf(cmd_dta,
+                               len,
+                               "%s.%s=%s",
+                               cfg_handle->short_fname,
+                               section_name,
+                               section_type);
+                       if (uci_lookup_ptr(cfg_handle->_ctx, &ptr, cmd_dta, true) == UCI_OK) {
+                               ret_val = uci_set(cfg_handle->_ctx, &ptr);
+                               if (ret_val == UCI_OK) {
+                                       if (save_immediately) {
+                                               ret_val = uci_save(cfg_handle->_ctx, ptr.p);
+                                       }
+                               }
+                       }
+                       free(cmd_dta);
+               }
+       }
+       return ret_val;
+}
+
+static bool set_config_value_to_section(ConfigHandle *cfg_handle,
+                       const char *section_type,
+                       const char *section_name,
+                       const char *key,
+                       const char *value) {
+       struct uci_section      *sct;
+       struct uci_section      *tmp_sct;
+       int                     err_flg;
+       struct uci_element      *elem;
+       struct uci_ptr          ptr;
+       bool                    ret_val;
+
+       ret_val = false;
+       if ((cfg_handle != NULL) &&
+           (cfg_handle->_ctx != NULL) &&
+           (cfg_handle->_pkg != NULL) &&
+           (section_type != NULL) &&
+           (section_name != NULL) &&
+           (key != NULL) &&
+           (value != NULL)) {
+               sct     = NULL;
+               uci_foreach_element(&cfg_handle->_pkg->sections, elem) {
+                       tmp_sct = uci_to_section(elem);
+                       if (strcmp(tmp_sct->type, section_type) == 0) {
+                               sct     = tmp_sct;
+                               break;
+                       }
+               }
+               if (sct == NULL) {
+                       log_debug("Creating new section %s to configuration file: %s\n", section_name, cfg_handle->full_fname);
+                       //err_flg       = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
+                       //err_flg       = uci_add_section(ctx, pkg, section_name, &sct);
+                       err_flg = uci_create_named_section(cfg_handle,
+                                               section_type,
+                                               section_name,
+                                               false);
+                       if (err_flg == UCI_OK) {
+                               uci_foreach_element(&cfg_handle->_pkg->sections, elem) {
+                                       tmp_sct = uci_to_section(elem);
+                                       if (strcmp(tmp_sct->type, section_type) == 0) {
+                                               sct     = tmp_sct;
+                                               break;
+                                       }
+                               }
+                       }
+               }
+               if (err_flg == UCI_OK) {
+                       memset(&ptr, 0, sizeof(ptr));
+                       ptr.package     = cfg_handle->_pkg->e.name;
+                       ptr.section     = sct->e.name;
+                       ptr.option      = key;
+                       err_flg         = uci_lookup_ptr(cfg_handle->_ctx, &ptr, NULL, false);
+                       if (err_flg == UCI_OK) {
+                               ptr.value       = value;
+                               err_flg         = uci_set(cfg_handle->_ctx, &ptr);
+                               if (err_flg == UCI_OK) {
+                                       err_flg = uci_save(cfg_handle->_ctx, cfg_handle->_pkg);
+                                       if (err_flg == UCI_OK) {
+                                               ret_val = true;
+                                               log_debug("Set value to section %s in configuration file: %s\n", section_name, cfg_handle->full_fname);
+                                       }
+                                       else {
+                                               log_error("Failed to set value to configuration file: %s\n. Could not save the file\n", cfg_handle->full_fname);
+                                       }
+                               }
+                               else {
+                                       log_error("Failed to set value to configuration file: %s\n. Could not set new value\n", cfg_handle->full_fname);
+                               }
+                       }
+                       else {
+                               log_error("Failed to set value to configuration file: %s\n. Could not look-up pointer for package %s section %s.\n",
+                                       cfg_handle->full_fname,
+                                       cfg_handle->_pkg->e.name,
+                                       sct->e.name);
+                       }
+                       uci_free_context(cfg_handle->_ctx);
+               }
+               else {
+                       log_error("Failed to set value to configuration file: %s. Could not create section %s.\n", cfg_handle->full_fname, section_name);
+               }
+       }
+       else {
+               log_error("Failed to set value to configuration file, invalid parameters\n");
+       }
+       return ret_val;
 }
 
 DeviceConfig::DeviceConfig(string device_id_param) {
@@ -92,7 +230,9 @@ string DeviceConfig::get_config_value(string key) {
        string                  ret_val;
 
        if (uci_handle != NULL) {
-               section = uci_lookup_section(uci_handle->_ctx, uci_handle->_pkg, DEVICE_CONFIG__SECTION_NAME);
+               section = uci_lookup_section(uci_handle->_ctx,
+                                       uci_handle->_pkg,
+                                       DEVICE_CONFIG__SECTION_NAME);
                if (section != NULL) {
                        option  = uci_lookup_option(uci_handle->_ctx, section, key.c_str());
                        if (option != NULL) {
@@ -115,20 +255,17 @@ string DeviceConfig::get_config_value(string key) {
 }
 
 void DeviceConfig::set_config_value(string key,
-                               string value,
-                               bool save_immediately) {
+                               string value) {
        string  cfg_dir;
        string  cfg_fl;
 
        cfg_dir = get_dir_name(device_id);
        cfg_fl  = DEVICE_CONFIG__FILE_NAME;
-       set_config_value_to_section(cfg_dir.c_str(),
-                       cfg_fl.c_str(),
+       set_config_value_to_section(uci_handle,
                        DEVICE_CONFIG__SECTION_TYPE,
                        DEVICE_CONFIG__SECTION_NAME,
                        key.c_str(),
-                       value.c_str(),
-                       save_immediately);
+                       value.c_str());
 }
 
 EnumSummaryCalculationType DeviceConfig::get_summary_calculation_type() {
@@ -166,17 +303,29 @@ ConfigHandle *DeviceConfig::load_device_config(string device_id_param) {
                                err_flg = uci_load(ctx, cfg_fl.c_str(), &pkg);
                                if (err_flg == UCI_OK) {
                                        //log_debug("Loaded device configuration: %s.\n", cfg_fl.c_str());
-                                       ret_val = new ConfigHandle(ctx, pkg);
+                                       int b_count;
+                                       char *fname;
+
+                                       b_count = strlen(cfg_dir.c_str()) + strlen(cfg_fl.c_str()) + 10;
+                                       fname   = (char *)calloc(1, b_count);
+                                       if (fname != NULL) {
+                                               strncpy(fname, cfg_dir.c_str(), b_count);
+                                               strncat(fname, "/", 1);
+                                               strncat(fname, cfg_fl.c_str(), strlen(cfg_fl.c_str()) + 1);
+
+                                               ret_val = new ConfigHandle(ctx, pkg, cfg_fl.c_str(), fname);
+                                               free(fname);
+                                       }
                                }
                                else {
                                        log_debug("Failed to load device configuration: %s, err code: %d.\n", cfg_fl.c_str(), UCI_OK);
-                                       set_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "", true);
+                                       set_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "");
                                        uci_free_context(ctx);
                                }
                        }
                        else {
                                log_error("Failed to load device device configuration, memory allocation error.\n");
-                               set_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "", true);
+                               set_config_value(DEVICE_CONFIG_VALUE_KEY__TYPE, "");
                        }
                }
                else {
index bc8241603753689f5022b3f72c2bbe693425e87f..841a3693ff5451fb3562ec727ab076d190121328 100644 (file)
@@ -12,7 +12,6 @@
 
 extern "C" {
        #include <uci.h>
-       #include "config.h"
        #include <stdbool.h>
 }
 
@@ -36,10 +35,15 @@ const std::string CALCULATION_TYPE_NAMES_ARRAY[]    = {"sum", "delta", "min", "max"
 namespace plp {
        struct ConfigHandle {
                public:
-                       ConfigHandle(uci_context *ctx_param, uci_package *pkg_param);
+                       ConfigHandle(uci_context *ctx_param,
+                               uci_package *pkg_param,
+                               const char *short_fname_param,
+                               const char *full_fname_param);
                        ~ConfigHandle();
                        struct uci_context      *_ctx;
                        struct uci_package      *_pkg;
+                       char                    *short_fname;
+                       char                    *full_fname;
        };
 
        class DeviceConfig {
@@ -51,8 +55,7 @@ namespace plp {
                        static DeviceConfig *get_device_config(std::string device_id);
                        std::string get_config_value(std::string key);
                        void set_config_value(std::string key,
-                                       std::string value,
-                                       bool save_immediately);
+                                       std::string value);
                        EnumSummaryCalculationType get_summary_calculation_type();
                private:
                        static std::string      store_base_dir;
index 92c8df303875424f5e455f9203f20a2006be875f..32f0a93b63ee0d5c1b30819e69031874d44f9d65 100644 (file)
@@ -3,16 +3,14 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <uci.h>
-#include <stdbool.h>
 
-#include "log.h"
 #include "config.h"
+#include "log.h"
 
 static int uci_create_named_section(struct uci_context *ctx,
                                const char *conf_file_name,
                                const char *section_type,
-                               const char *section_name,
-                               bool save_immediately)
+                               const char *section_name)
 {
        struct uci_ptr  ptr;
        int             ret_val;
@@ -36,9 +34,7 @@ static int uci_create_named_section(struct uci_context *ctx,
                        if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
                                ret_val = uci_set(ctx, &ptr);
                                if (ret_val == UCI_OK) {
-                                       if (save_immediately) {
-                                               ret_val = uci_save(ctx, ptr.p);
-                                       }
+                                       ret_val = uci_save(ctx, ptr.p);
                                }
                        }
                        free(cmd_data);
@@ -52,8 +48,7 @@ bool set_config_value_to_section(const char *conf_dir_name,
                        const char *section_type,
                        const char *section_name,
                        const char *key,
-                       const char *value,
-                       bool save_immediately) {
+                       const char *value) {
        struct uci_context      *ctx;
        struct uci_package      *pkg;
        struct uci_section      *sct;
@@ -105,8 +100,7 @@ bool set_config_value_to_section(const char *conf_dir_name,
                                                err_flg = uci_create_named_section(ctx,
                                                                        conf_file_name,
                                                                        section_type,
-                                                                       section_name,
-                                                                       false);
+                                                                       section_name);
                                                if (err_flg == UCI_OK) {
                                                        uci_foreach_element(&pkg->sections, elem) {
                                                                tmp_sct = uci_to_section(elem);
@@ -127,16 +121,15 @@ bool set_config_value_to_section(const char *conf_dir_name,
                                                        ptr.value       = value;
                                                        err_flg         = uci_set(ctx, &ptr);
                                                        if (err_flg == UCI_OK) {
-                                                               if (save_immediately == true) {
-                                                                       err_flg = uci_save(ctx, pkg);
-                                                                       if (err_flg != UCI_OK) {
-                                                                               log_error("Failed to set value to configuration file: %s\n. Could not save the file\n", fname);
-                                                                       }
-                                                               }
+                                                               err_flg = uci_save(ctx, pkg);
                                                                if (err_flg == UCI_OK) {
                                                                        ret_val = true;
                                                                        log_debug("Set value to section %s in configuration file: %s\n", section_name, fname);
                                                                }
+                                                               else {
+                                                                       log_error("Failed to set value to configuration file: %s\n. Could not save the file\n", fname);
+                                                               }
+
                                                        }
                                                        else {
                                                                log_error("Failed to set value to configuration file: %s\n. Could not set new value\n", fname);
index 07a6429f77ca33977ba6d9b9db9c6d01f4ac77de..378ec3e9bae5b9842548029c8318622d03d7da73 100644 (file)
 
 #include <stdbool.h>
 
+struct uci_context     *_ctx;
+struct uci_package     *_pkg;
+
 bool set_config_value_to_section(const char *conf_dir_name,
                        const char *conf_file_name,
                        const char *section_type,
                        const char *section_name,
                        const char *key,
-                       const char *value,
-                       bool save_immediately);
+                       const char *value);
 
 #endif /* CONFIG_H_ */
index ac1b2a086a2b818cea094e326f73583dfb464b84..ef30eeb16c037d6040f3cc4fd6c79b0615993a06 100644 (file)
@@ -19,15 +19,25 @@ void test_config() {
                        "mysection_type",
                        "mysection_name",
                        "myoption_name",
-                       "my_option_value",
-                       false);
+                       "my_option_value_tmp");
        set_config_value_to_section(work_dir,
                        "dev_cfg_txt",
                        "mysection_type",
                        "mysection_name",
                        "myoption_name",
-                       "my_option_value3",
-                       true);
+                       "my_option_value");
+       set_config_value_to_section(work_dir,
+                       "dev_cfg_txt",
+                       "mysection_type",
+                       "mysection_name",
+                       "myoption_name2",
+                       "my_option_value2");
+       set_config_value_to_section(work_dir,
+                       "dev_cfg_txt",
+                       "mysection3_type",
+                       "mysection3_name",
+                       "myoption_name",
+                       "my_option_value3");
 }
 
 int main(int argc, char** argv) {