Factorisation of the unix socket path computation 08/14608/1
authorJosé Bollo <jose.bollo@open.eurogiciel.org>
Wed, 8 Jan 2014 14:54:39 +0000 (15:54 +0100)
committerJosé Bollo <jose.bollo@open.eurogiciel.org>
Wed, 8 Jan 2014 14:54:39 +0000 (15:54 +0100)
The code that was appearing 3 times, doing the same thing
is factorized in a single function.

A check to a not possible error is removed.

Some memory clean-up is added to avoid a memory leak.

This commit prepares to multi-user because the path of the
unix sockets may depend of the user. Then having a central
place to perform this computation is simplier.

Change-Id: I88a4df4951bd240aef82e6960b9f91e6e4f1067d
Signed-off-by: José Bollo <jose.bollo@open.eurogiciel.org>
src/framework/event/config.c

index 5ede5cd..b4de237 100755 (executable)
@@ -41,6 +41,7 @@ static int _find_noti_spec_bag(const char *noti_key);
 
 static int _find_event_spec(int event_num);
 static int _find_noti_spec(int noti_spec_bag_num, int noti_num);
+static char *_make_communication_path(const char *root, const char *key);
 
 const char *event_get_noti_path(const char *noti_key)
 {
@@ -110,26 +111,16 @@ sync_agent_event_error_e event_set_event_spec_from_config_file(const char *event
 
        fclose(fp);
 
+       /*
+        * Setting communication_path_event
+        */
        if (__communication_path_event == NULL) {
-               int comm_path_event_len = strlen(EVENT_COMMUNICATION_PATH_EVENT) + strlen(event_key) + 1;
-               int len = 0;
-               __communication_path_event = (char *)calloc(comm_path_event_len, sizeof(char));
-
-               if (__communication_path_event != NULL) {
-                       len = g_strlcat(__communication_path_event, EVENT_COMMUNICATION_PATH_EVENT, comm_path_event_len);
-                       len = g_strlcat(__communication_path_event, event_key, comm_path_event_len);
-
-                       if (len >= comm_path_event_len) {
-                               _DEBUG_ERROR("__communication_path_event buffer overflow !!");
-                               free(__communication_path_event);
-                               return SYNC_AGENT_EVENT_FAIL;
-                       }
-
-                       _DEBUG_INFO("EVENT_COMMUNICATION_PATH_EVENT : %s", __communication_path_event);
-               } else {
+               __communication_path_event = _make_communication_path(EVENT_COMMUNICATION_PATH_EVENT, event_key);
+               if (__communication_path_event == NULL) {
                        _DEBUG_ERROR("__communication_path_event is NULL");
                        return SYNC_AGENT_EVENT_FAIL;
                }
+               _DEBUG_INFO("EVENT_COMMUNICATION_PATH_EVENT : %s", __communication_path_event);
        }
 
        _EXTERN_FUNC_EXIT;
@@ -226,54 +217,33 @@ sync_agent_event_error_e event_set_noti_spec_from_config_file(const char *noti_k
         * Setting communication_path_noti
         */
        if (noti_spec_bag->communication_path_noti == NULL) {
-               int comm_path_noti_len = strlen(EVENT_COMMUNICATION_PATH_NOTI) + strlen(noti_key) + 1;
-               int len = 0;
-               noti_spec_bag->communication_path_noti = (char *)calloc(comm_path_noti_len, sizeof(char));
-
-               if (noti_spec_bag->communication_path_noti != NULL) {
-                       len = g_strlcat(noti_spec_bag->communication_path_noti, EVENT_COMMUNICATION_PATH_NOTI, comm_path_noti_len);
-                       len = g_strlcat(noti_spec_bag->communication_path_noti, noti_key, comm_path_noti_len);
-
-                       if (len >= comm_path_noti_len) {
-                               _DEBUG_ERROR("noti_spec_bag->communication_path_noti buffer overflow !!");
-                               free(noti_spec_bag->communication_path_noti);
-                               free(noti_spec_bag);
-                               return SYNC_AGENT_EVENT_FAIL;
-                       }
-
-                       _DEBUG_INFO("EVENT_COMMUNICATION_PATH_NOTI : %s", noti_spec_bag->communication_path_noti);
-               } else {
-                       _DEBUG_ERROR("noti_spec_bag->communication_path_not is NULL");
+               noti_spec_bag->communication_path_noti = _make_communication_path(EVENT_COMMUNICATION_PATH_NOTI, noti_key);
+               if (noti_spec_bag->communication_path_noti == NULL) {
+                       _DEBUG_ERROR("noti_spec_bag->communication_path_noti is NULL");
+                       while(noti_spec_bag->noti_count > 0)
+                               free(noti_spec_bag->noti_spec_list[--noti_spec_bag->noti_count]);
+                       free(noti_spec_bag->noti_key);
                        free(noti_spec_bag);
                        return SYNC_AGENT_EVENT_FAIL;
                }
+               _DEBUG_INFO("EVENT_COMMUNICATION_PATH_NOTI : %s", noti_spec_bag->communication_path_noti);
        }
 
        /*
         * Setting communication_path_reply_noti
         */
        if (noti_spec_bag->communication_path_reply_noti == NULL) {
-               int comm_path_reply_noti_len = strlen(EVENT_REPLY_COMMUNICATION_PATH_NOTI) + strlen(noti_key) + 1;
-               int len = 0;
-               noti_spec_bag->communication_path_reply_noti = (char *)calloc(comm_path_reply_noti_len, sizeof(char));
-
-               if (noti_spec_bag->communication_path_reply_noti != NULL) {
-                       len = g_strlcat(noti_spec_bag->communication_path_reply_noti, EVENT_REPLY_COMMUNICATION_PATH_NOTI, comm_path_reply_noti_len);
-                       len = g_strlcat(noti_spec_bag->communication_path_reply_noti, noti_key, comm_path_reply_noti_len);
-
-                       if (len >= comm_path_reply_noti_len) {
-                               _DEBUG_ERROR("noti_spec_bag->communication_path_reply_noti buffer overflow !!");
-                               free(noti_spec_bag->communication_path_reply_noti);
-                               free(noti_spec_bag);
-                               return SYNC_AGENT_EVENT_FAIL;
-                       }
-
-                       _DEBUG_INFO("EVENT_REPLY_COMMUNICATION_PATH_NOTI : %s", noti_spec_bag->communication_path_reply_noti);
-               } else {
+               noti_spec_bag->communication_path_reply_noti = _make_communication_path(EVENT_REPLY_COMMUNICATION_PATH_NOTI, noti_key);
+               if (noti_spec_bag->communication_path_reply_noti == NULL) {
                        _DEBUG_ERROR("noti_spec_bag->communication_path_reply_noti is NULL");
+                       free(noti_spec_bag->communication_path_noti);
+                       while(noti_spec_bag->noti_count > 0)
+                               free(noti_spec_bag->noti_spec_list[--noti_spec_bag->noti_count]);
+                       free(noti_spec_bag->noti_key);
                        free(noti_spec_bag);
                        return SYNC_AGENT_EVENT_FAIL;
                }
+               _DEBUG_INFO("EVENT_REPLY_COMMUNICATION_PATH_NOTI : %s", noti_spec_bag->communication_path_reply_noti);
        }
 
        _DEBUG_INFO("before: current_noti_bag_count = %d", current_noti_bag_count);
@@ -478,3 +448,18 @@ static int _find_noti_spec(int noti_spec_bag_num, int noti_num)
 
        return -1;
 }
+
+static char *_make_communication_path(const char *root, const char *key)
+{
+       size_t root_length = strlen(root);
+       size_t key_length = strlen(key);
+
+       char *result = malloc(root_length + key_length + 1);
+
+       if (result != NULL) {
+               memcpy(result, root, root_length);
+               memcpy(result+root_length, key, key_length + 1);
+       }
+
+       return result;
+}