Sync batch insert code between scanner and scanner-v2 23/239323/8
authorhj kim <backto.kim@samsung.com>
Fri, 24 Jul 2020 02:41:34 +0000 (11:41 +0900)
committerhj kim <backto.kim@samsung.com>
Tue, 28 Jul 2020 05:05:36 +0000 (14:05 +0900)
- add some debug log
- Use GPtrArray instead of GArray
- bug fix when batch_insert is stopped due to power_off

Change-Id: I7b7837d50b701aac61c17c93e9c1c1b6ce2faca7

src/scanner-v2/media-scanner-scan-v2.c
src/scanner/media-scanner-scan.c

index aa342d1..c7d4665 100644 (file)
@@ -84,8 +84,6 @@ struct linux_dirent {
 #define BUF_SIZE 1024
 
 static int __msc_check_stop_status(int scan_type, const char *start_path, int pid);
-static int __msc_make_file_list(char *file_path, GArray **path_array, uid_t uid);
-static int __msc_batch_insert(int pid, GArray *path_array, uid_t uid);
 static int __msc_pop_register_request(GArray *register_array, ms_comm_msg_s **register_data);
 static void __msc_clear_file_list(GArray *path_array);
 static void __msc_insert_register_request(GArray *register_array, ms_comm_msg_s *insert_data);
@@ -1393,31 +1391,24 @@ static void __msc_clear_file_list(GArray *path_array)
        }
 }
 
-static int __msc_make_file_list(char *file_path, GArray **path_array, uid_t uid)
+static int __msc_make_file_list(char *file_path, GPtrArray **path_array, uid_t uid)
 {
        FILE *fp = NULL;
-       char buf[MS_FILE_PATH_LEN_MAX] = {0,};
+       char buf[MS_FILE_PATH_LEN_MAX] = {0, };
        char *path = NULL;
        int length;
-       int res = MS_MEDIA_ERR_NONE;
+       GPtrArray *_path_array = NULL;
        int ret = MS_MEDIA_ERR_NONE;
 
-       /* load the file list from file */
        fp = fopen(file_path, "rt");
-       if (fp == NULL) {
-               MS_DBG_STRERROR("fopen failed");
-               res = MS_MEDIA_ERR_INTERNAL;
-               goto FREE_RESOURCE;
-       }
+       MS_DBG_RETVM_IF(!fp, MS_MEDIA_ERR_INTERNAL, "fopen failed");
 
-       memset(buf, 0x0, MS_FILE_PATH_LEN_MAX);
-       /* This is an array for storing the path of insert datas*/
-       *path_array = g_array_new(FALSE, FALSE, sizeof(char *));
+       _path_array = g_ptr_array_new_with_free_func(g_free);
 
-       /* read registering file path from stored file */
        while (fgets(buf, MS_FILE_PATH_LEN_MAX, fp) != NULL) {
-               length = strlen(buf); /*the return value of function, strlen(), includes "\n" */
-               path = g_strndup(buf, length - 1); /*copying except "\n" and strndup fuction adds "\0" at the end of the copying string */
+               /* Remove '\n' */
+               length = strlen(buf);
+               path = g_strndup(buf, length - 1);
 
                /* check valid path */
                ret = ms_check_ignore_dir(path, uid);
@@ -1426,72 +1417,56 @@ static int __msc_make_file_list(char *file_path, GArray **path_array, uid_t uid)
                        g_free(path);
                        continue;
                }
-               /* insert getted path to the list */
-               g_array_append_val(*path_array, path);
+
+               g_ptr_array_add(_path_array, path);
        }
 
        if (fp) fclose(fp);
-       fp = NULL;
-
-       return MS_MEDIA_ERR_NONE;
-
-FREE_RESOURCE:
 
-       __msc_clear_file_list(*path_array);
+       *path_array = _path_array;
 
-       if (fp) fclose(fp);
-       fp = NULL;
-
-       return res;
+       return MS_MEDIA_ERR_NONE;
 }
 
-static int __msc_batch_insert(int pid, GArray *path_array, uid_t uid)
+static int __msc_batch_insert(int pid, GPtrArray *path_array, uid_t uid)
 {
-       int err;
-       unsigned int i;
+       int ret = MS_MEDIA_ERR_NONE;
        sqlite3 *handle = NULL;
        char *insert_path = NULL;
        char storage_id[MS_UUID_SIZE] = {0,};
+       int i = 0;
 
-       /* connect to media db, if conneting is failed, db updating is stopped */
-       err = ms_connect_db(&handle, uid);
-       if (err != MS_MEDIA_ERR_NONE)
-               return MS_MEDIA_ERR_DB_INTERNAL;
+       MS_DBG_FENTER();
 
-       /*start db updating */
-       /*call for bundle commit*/
-       ms_batch_commit_enable(true, pid);
+       ret = ms_connect_db(&handle, uid);
+       MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_DB_INTERNAL);
 
-       MS_DBG_WARN("BULK REGISTER START");
+       ms_batch_commit_enable(true, pid);
 
-       /* get the inserting file path from array and insert to db */
        for (i = 0; i < path_array->len; i++) {
+               insert_path = g_ptr_array_index(path_array, i);
 
-               insert_path = g_array_index(path_array, char*, i);
-
-               /* get storage list */
                memset(storage_id, 0x0, MS_UUID_SIZE);
-               if (ms_get_storage_id(handle, insert_path, storage_id, uid) < 0) {
+               if (ms_get_storage_id(handle, insert_path, storage_id, uid) != MS_MEDIA_ERR_NONE) {
                        MS_DBG_ERR("There is no storage id in media db");
                        continue;
                }
 
-               /* insert to db */
-               err = ms_insert_item_batch(handle, storage_id, insert_path, uid);
+               ret = ms_insert_item_batch(handle, storage_id, insert_path, uid);
 
                if (_msc_is_power_off()) {
-                       /*call for bundle commit*/
+                       ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
                        break;
                }
        }
 
        /*call for bundle commit*/
        ms_batch_commit_disable(uid);
-
-       /*disconnect form media db*/
        ms_disconnect_db(handle);
 
-       return MS_MEDIA_ERR_NONE;
+       MS_DBG_FLEAVE();
+
+       return ret;
 }
 
 static int __msc_pop_register_request(GArray *register_array, ms_comm_msg_s **register_data)
@@ -1535,7 +1510,7 @@ gpointer msc_register_thread(gpointer data)
 {
        ms_comm_msg_s *register_data = NULL;
        GArray *register_array = NULL;
-       GArray *path_array = NULL;
+       GPtrArray *path_array = NULL;
        char *file_path = NULL;
        int ret;
        int pid = 0;
@@ -1593,7 +1568,8 @@ FREE_RESOURCE:
 
                MS_DBG_WARN("BULK REGISTER END [%d |%.*s]", ret, MAX_MSG_SIZE, register_data->msg);
 
-               __msc_clear_file_list(path_array);
+               g_ptr_array_free(path_array, TRUE);
+               path_array = NULL;
 
                g_free(file_path);
                file_path = NULL;
@@ -1615,8 +1591,6 @@ _POWEROFF:
                register_array = NULL;
        }
 
-       __msc_clear_file_list(path_array);
-
        return NULL;
 }
 
index 71450a8..240a1a8 100644 (file)
@@ -477,6 +477,8 @@ static int __msc_batch_insert(int pid, GPtrArray *path_array, uid_t uid)
        char storage_id[MS_UUID_SIZE] = {0,};
        int i = 0;
 
+       MS_DBG_FENTER();
+
        ret = ms_connect_db(&handle, uid);
        MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_DB_INTERNAL);
 
@@ -503,6 +505,8 @@ static int __msc_batch_insert(int pid, GPtrArray *path_array, uid_t uid)
        ms_batch_commit_disable(uid);
        ms_disconnect_db(handle);
 
+       MS_DBG_FLEAVE();
+
        return ret;
 }
 
@@ -534,6 +538,7 @@ gpointer msc_register_thread(gpointer data)
                MS_DBG_SLOG("BULK REGISTER START [%.*s]", MAX_MSG_SIZE, register_data->msg);
                ret = __msc_batch_insert(register_data->pid, path_array, register_data->uid);
                g_ptr_array_free(path_array, TRUE);
+               path_array = NULL;
 
 NEXT:
                /*Active flush */