fixed the db init script 93/50993/1
authorSunghyun Kwon <sh0701.kwon@samsung.com>
Wed, 4 Nov 2015 09:27:48 +0000 (18:27 +0900)
committerSunghyun Kwon <sh0701.kwon@samsung.com>
Wed, 4 Nov 2015 09:27:48 +0000 (18:27 +0900)
Change-Id: Ia0782850b58412ff3cf87182ccc64ec26aef4fcb

email-core/email-storage/email-storage.c
email-core/email-storage/include/email-storage.h
email-daemon/email-daemon-init.c

index 509a56a..ce51aad 100755 (executable)
@@ -1751,12 +1751,16 @@ int _xsystem(const char *argv[])
        pid = fork();
 
        switch (pid) {
-               case -1 : 
+               case -1 :
                        perror("fork failed");
                        return -1;
                case 0 :
-                       execvp(argv[0], (char *const *)argv);
-                       _exit(-1);
+                       if (execvp(argv[0], (char *const *)argv) == -1) {
+                               perror("execute init db script");
+                               _exit(-1);
+                       }
+
+                       _exit(2);
                default:
                        /* parent */
                        break;
@@ -1782,6 +1786,60 @@ int _xsystem(const char *argv[])
 
 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
 
+INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
+{
+       EM_DEBUG_FUNC_BEGIN();
+       int err = EMAIL_ERROR_NONE;
+       char *prefix_path = NULL;
+       char *output_file_path = NULL;
+       char temp_file_path[MAX_PATH] = {0};
+
+       if (EM_SAFE_STRLEN(multi_user_name) > 0) {
+               err = emcore_get_container_path(multi_user_name, &prefix_path);
+               if (err != EMAIL_ERROR_NONE) {
+                       if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
+                               EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
+                               goto FINISH_OFF;
+                       }
+               }
+       } else {
+               prefix_path = strdup("");
+       }
+
+       if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
+               err = emcore_get_canonicalize_path(EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
+                       goto FINISH_OFF;
+               }
+
+               SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
+       } else {
+               SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
+       }
+
+       EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
+
+       if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
+               int ret = true;
+               const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
+               ret = _xsystem(argv_script);
+
+               if (ret == -1) {
+                       EM_DEBUG_EXCEPTION("_xsystem failed");
+                       err = EMAIL_ERROR_SYSTEM_FAILURE;
+               }
+       }
+
+FINISH_OFF:
+
+       EM_SAFE_FREE(prefix_path);
+       EM_SAFE_FREE(output_file_path);
+
+       EM_DEBUG_FUNC_END();
+       return err;
+}
+
 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
 {
        EM_DEBUG_FUNC_BEGIN();
@@ -1798,14 +1856,6 @@ INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *e
                return true;
        }
 
-       /* Generate db file */
-       struct stat sts;
-       ret = stat(db_file_path, &sts);
-       if (ret == -1 && errno == ENOENT) {
-               const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
-               ret = _xsystem(argv_script);
-       }
-
     EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
 
        /*  db open */
@@ -4047,7 +4097,7 @@ static int _read_password_from_secure_storage(char *file_name, char **password)
        ret = emcore_get_password_in_key_manager(file_name, password);
        if (ret != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
-               goto FINISH_OFF;        
+               goto FINISH_OFF;
        }
 
 FINISH_OFF:
@@ -4391,17 +4441,17 @@ INTERNAL_FUNC int emstorage_update_account_password(char *multi_user_name, int i
        }
 
        /*  get password file name */
-       if ((err = _get_password_file_name(multi_user_name, 
-                                                                               input_account_id, 
-                                                                               recv_password_file_name, 
+       if ((err = _get_password_file_name(multi_user_name,
+                                                                               input_account_id,
+                                                                               recv_password_file_name,
                                                                                send_password_file_name)) != EMAIL_ERROR_NONE) {
                EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
                goto FINISH_OFF;
        }
 
-       EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]", 
+       EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
                                                recv_password_file_name, input_incoming_server_password);
-       EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]", 
+       EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
                                                send_password_file_name, input_outgoing_server_password);
 
        err = emcore_remove_password_in_key_manager(recv_password_file_name);
index 38d0c25..d2b95a0 100755 (executable)
@@ -136,7 +136,7 @@ typedef struct
        email_digest_type                  digest_type;                              /* Sepeifies the digest algorithm*/
 
     /* Multi user and KNOX options */
-    char                              *user_name;                                /* Specifies the container name */ 
+    char                              *user_name;                                /* Specifies the container name */
 } emstorage_account_tbl_t;
 
 typedef struct
@@ -363,7 +363,7 @@ typedef struct
 
 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name);
 
-
+INTERNAL_FUNC int emstorage_init_db(char *multi_user_name);
 /************** Database Management ***************/
 /*
  * em_db_open
@@ -780,12 +780,12 @@ INTERNAL_FUNC int emstorage_change_read_mail_uid(char *multi_user_name, int acco
  * arguments :
  * return  :
  */
-INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name, 
-                                                                                                       int account_id, 
+INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
+                                                                                                       int account_id,
                                                                                                        int mailbox_id,
-                                                                                                       char *mailbox_name, 
-                                                                                                       char *uid, 
-                                                                                                       int transaction, 
+                                                                                                       char *mailbox_name,
+                                                                                                       char *uid,
+                                                                                                       int transaction,
                                                                                                        int *err_code);
 
 INTERNAL_FUNC int emstorage_update_read_mail_uid(char *multi_user_name, int mail_id, char *new_server_uid, char *mbox_name, int *err_code);
@@ -1012,11 +1012,11 @@ INTERNAL_FUNC int emstorage_get_mails(char *multi_user_name, int account_id, int
 
 INTERNAL_FUNC int emstorage_get_searched_mail_list(char *multi_user_name, int account_id, int mailbox_id, int thread_id, int search_type, const char *search_value, int start_index, int limit_count, email_sort_type_t sorting, int transaction, email_mail_list_item_t **mail_list,  int *result_count,  int *err_code);
 
-INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name, 
-                                                                                                                       char *server_mail_id, 
-                                                                                                                       int mailbox_id, 
-                                                                                                                       emstorage_mail_tbl_t **mail, 
-                                                                                                                       int transaction, 
+INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
+                                                                                                                       char *server_mail_id,
+                                                                                                                       int mailbox_id,
+                                                                                                                       emstorage_mail_tbl_t **mail,
+                                                                                                                       int transaction,
                                                                                                                        int *err_code);
 
 INTERNAL_FUNC int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code);
@@ -1036,14 +1036,14 @@ INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int accoun
  * @remarks N/A
  * @return This function returns 0 on success or error code on failure.
  */
-INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name, 
-                                                                                       emstorage_search_filter_t *search, 
-                                                                                       int account_id, 
-                                                                                       int mailbox_id, 
-                                                                                       int sorting, 
-                                                                                       DB_STMT *search_handle, 
-                                                                                       int *searched, 
-                                                                                       int transaction, 
+INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
+                                                                                       emstorage_search_filter_t *search,
+                                                                                       int account_id,
+                                                                                       int mailbox_id,
+                                                                                       int sorting,
+                                                                                       DB_STMT *search_handle,
+                                                                                       int *searched,
+                                                                                       int transaction,
                                                                                        int *err_code);
 
 /*
@@ -1055,10 +1055,10 @@ INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
  *    mail  :  double pointer to hold mail
  * return  :
  */
-INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, 
-                                                                                               emstorage_mail_field_type_t type, 
-                                                                                               void **data, 
-                                                                                               int transaction, 
+INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle,
+                                                                                               emstorage_mail_field_type_t type,
+                                                                                               void **data,
+                                                                                               int transaction,
                                                                                                int *err_code);
 
 /*
@@ -1132,10 +1132,10 @@ INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emst
 
 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int  *err_code);
 
-INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name, 
+INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
                                                                                                int mail_id,
-                                                                                               char *old_server_uid, 
-                                                                                               char *new_server_uid, 
+                                                                                               char *old_server_uid,
+                                                                                               char *new_server_uid,
                                                                                                int *err_code);
 
 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code);
@@ -1250,7 +1250,7 @@ INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail
  * emstorage_get_attachment_list
  *
  * description :  get attachment list from attachment table
- * arguments : 
+ * arguments :
  *    input_mail_id           : mail id
  *    input_transaction       : transaction option
  *    attachment_list         : result attachment list
@@ -1514,16 +1514,16 @@ INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
 
 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code);
 
-INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name, 
-                                                                                                               int account_id, 
-                                                                                                               int mailbox_id, 
+INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
+                                                                                                               int account_id,
+                                                                                                               int mailbox_id,
                                                                                                                int mailbox_type,
-                                                                                                               int thread_id, 
-                                                                                                               int *updated_thread_id, 
-                                                                                                               int latest_mail_id, 
-                                                                                                               int thread_item_count, 
-                                                                                                               int noti_type, 
-                                                                                                               int transaction, 
+                                                                                                               int thread_id,
+                                                                                                               int *updated_thread_id,
+                                                                                                               int latest_mail_id,
+                                                                                                               int thread_item_count,
+                                                                                                               int noti_type,
+                                                                                                               int transaction,
                                                                                                                int *err_code);
 
 INTERNAL_FUNC int emstorage_get_thread_id_from_mailbox(char *multi_user_name, int account_id, int mailbox_id, char *mail_subject, int *thread_id, int *thread_item_count);
index 3352949..f433bd0 100755 (executable)
@@ -317,7 +317,7 @@ static void callback_for_NETWORK_STATUS(connection_type_e new_conn_type, void *i
                /* check if inbox folder sync is finished */
                if (!emstorage_get_mailbox_by_mailbox_type (multi_user_name, account_info->account_id, EMAIL_MAILBOX_TYPE_INBOX, &local_mailbox, false, &err)) {
                        if (err == EMAIL_ERROR_MAILBOX_NOT_FOUND) {
-                               int handle = 0; 
+                               int handle = 0;
                                emdaemon_get_imap_mailbox_list(multi_user_name, account_info->account_id, "", &handle, &err);
                                if (err != EMAIL_ERROR_NONE) {
                                        EM_DEBUG_EXCEPTION("emdaemon_get_imap_mailbox_list error [%d]", err);
@@ -511,11 +511,16 @@ INTERNAL_FUNC int emdaemon_initialize(char *multi_user_name, int* err_code)
                EM_DEBUG_LOG("Initialization was already done. increased counter=[%d]", g_client_count);
 
                EM_DEBUG_LOG("************* start email service build time [%s %s] ************* ", __DATE__, __TIME__);
+               err = emstorage_init_db(multi_user_name);
+               if (err != EMAIL_ERROR_NONE) {
+                       EM_DEBUG_EXCEPTION("DB create failed");
+                       goto FINISH_OFF;
+               }
 
         dbus_threads_init_default();
 
-#if !GLIB_CHECK_VERSION(2, 36, 0) 
-               g_type_init(); 
+#if !GLIB_CHECK_VERSION(2, 36, 0)
+               g_type_init();
 #endif
         if ((err = _emdaemon_load_email_core()) != EMAIL_ERROR_NONE)  {
             EM_DEBUG_EXCEPTION("_emdaemon_load_email_core failed [%d]", err);
@@ -533,6 +538,7 @@ INTERNAL_FUNC int emdaemon_initialize(char *multi_user_name, int* err_code)
             vconf_notify_key_changed(VCONFKEY_MSG_SERVER_READY, callback_for_VCONFKEY_MSG_SERVER_READY, NULL);
         }
 #endif
+
     }
 
     /* open database */
@@ -541,7 +547,7 @@ INTERNAL_FUNC int emdaemon_initialize(char *multi_user_name, int* err_code)
                goto FINISH_OFF;
        }
 
-       if ((err = emstorage_update_db_table_schema(multi_user_name)) != EMAIL_ERROR_NONE) 
+       if ((err = emstorage_update_db_table_schema(multi_user_name)) != EMAIL_ERROR_NONE)
                EM_DEBUG_EXCEPTION("emstorage_update_db_table_schema failed [%d]", err);
 
        if (!emstorage_clean_save_status(multi_user_name, EMAIL_MAIL_STATUS_SAVED, &err))
@@ -662,7 +668,7 @@ INTERNAL_FUNC int emdaemon_start_auto_polling(char *multi_user_name, int* err_co
 
        /* get account list */
        if (!emstorage_get_account_list(multi_user_name, &count, &account_list, false, false, &err))  {
-               EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);              
+               EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
                goto FINISH_OFF;
        }