Load default config file when user config is invalid 63/171863/2
authorWonnam Jang <wn.jang@samsung.com>
Thu, 8 Mar 2018 00:02:04 +0000 (09:02 +0900)
committerWonnam Jang <wn.jang@samsung.com>
Thu, 8 Mar 2018 00:14:06 +0000 (09:14 +0900)
Change-Id: Ia34d37a7799c923b1e6127f1bd127868a59a380d
Signed-off-by: Wonnam Jang <wn.jang@samsung.com>
common/stt_config_parser.c

index 6786c71..77d0c79 100644 (file)
@@ -12,6 +12,8 @@
 */
 
 #include <dlog.h>
+#include <sys/stat.h>
+#include <unistd.h>
 #include <vconf.h>
 
 #include "stt_defs.h"
@@ -319,14 +321,37 @@ int stt_parser_load_config(stt_config_s** config_info)
        xmlChar *key;
        bool is_default_open = false;
 
-       doc = xmlParseFile(STT_CONFIG);
-       if (doc == NULL) {
+       if (0 != access(STT_CONFIG, F_OK)) {
                doc = xmlParseFile(STT_DEFAULT_CONFIG);
                if (doc == NULL) {
                        SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_DEFAULT_CONFIG);
+                       xmlCleanupParser();
                        return -1;
                }
                is_default_open = true;
+       } else {
+               int retry_count = 0;
+
+               while (NULL == doc) {
+                       doc = xmlParseFile(STT_CONFIG);
+                       if (NULL != doc) {
+                               break;
+                       }
+                       retry_count++;
+                       usleep(10000);
+
+                       if (STT_RETRY_COUNT == retry_count) {
+                               SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_CONFIG);
+                               doc = xmlParseFile(STT_DEFAULT_CONFIG);
+                               if (NULL == doc) {
+                                       SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to parse file error : %s", STT_DEFAULT_CONFIG);
+                                       xmlCleanupParser();
+                                       return -1;
+                               }
+                               is_default_open = true;
+                               break;
+                       }
+               }
        }
 
        cur = xmlDocGetRootElement(doc);
@@ -450,8 +475,33 @@ int stt_parser_load_config(stt_config_s** config_info)
        *config_info = temp;
        g_config_doc = doc;
 
-       if (is_default_open)
-               xmlSaveFile(STT_CONFIG, g_config_doc);
+       if (is_default_open) {
+               int retry_count = 0;
+               int ret = -1;
+               do {
+                       ret = xmlSaveFile(STT_CONFIG, g_config_doc);
+                       if (0 < ret)
+                               break;
+                       retry_count++;
+                       usleep(10000);
+
+                       if (STT_RETRY_COUNT == retry_count) {
+                               SLOG(LOG_ERROR, stt_tag(), "[ERROR] Save result : %d", ret);
+                               return -1;
+                       }
+               } while (0 != ret);
+
+               /* Set mode */
+               if (0 > chmod(STT_CONFIG, 0600)) {
+                       SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to change file mode : %d", ret);
+               }
+
+               /* Set owner */
+               if (0 > chown(STT_CONFIG, 5000, 5000)) {
+                       SLOG(LOG_ERROR, stt_tag(), "[ERROR] Fail to change file owner : %d", ret);
+               }
+               SLOG(LOG_DEBUG, stt_tag(), "Default config is changed : pid(%d)", getpid());
+       }
 
        return 0;
 }