Improve code regarding GCond for Scanning 06/240406/11
authorhj kim <backto.kim@samsung.com>
Thu, 6 Aug 2020 05:50:45 +0000 (14:50 +0900)
committerhj kim <backto.kim@samsung.com>
Wed, 12 Aug 2020 00:06:45 +0000 (09:06 +0900)
Change-Id: I35980814945d4abc9237dbc948503c523fd07192

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

index f5d6c46..1be792a 100644 (file)
  *
  */
 
-#include <fcntl.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <stdlib.h>
-
 #include <malloc.h>
-#include <vconf.h>
-#include <usb-device.h>
 
 #include "media-util.h"
 #include "media-server-ipc.h"
@@ -68,13 +61,10 @@ static s_scan_item* cur_scan_item = NULL;
 int g_directory_scan_processing2 = DIR_SCAN_NON_SCAN;
 static char *g_storage_scan_path;
 static char* g_dir_scan_path;
-
-typedef int (*DIR_SCAN_CB)();
-static DIR_SCAN_CB g_dir_scan_cb;
+static bool is_scanner_paused = false;
 
 static int __msc_check_stop_status(int scan_type, const char *start_path, int pid);
 static int __msc_get_null_scan_folder_list(sqlite3 *handle, const char *stroage_id, char *path, GPtrArray *dir_array);
-static int __msc_dir_scan_cb(void);
 static int __msc_check_scan_same_path(char *scan_path);
 static void __msc_set_storage_scan_cur_path(char *scan_path);
 static void __msc_set_dir_scan_cur_path(char *scan_path);
@@ -83,31 +73,6 @@ static int __msc_check_memory_status(uid_t uid);
 static int __msc_check_remain_space(uid_t uid);
 static void __msc_del_blocked_path(void);
 
-static int __msc_set_dir_scan_cb(DIR_SCAN_CB cb)
-{
-       g_dir_scan_cb = cb;
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-static int __msc_call_dir_scan_cb(void)
-{
-       if (g_dir_scan_cb != NULL) {
-               g_dir_scan_cb();
-
-               return MS_MEDIA_ERR_NONE;
-       }
-
-       return MS_MEDIA_ERR_INTERNAL;
-}
-
-static int __msc_remove_dir_scan_cb(void)
-{
-       g_dir_scan_cb = NULL;
-
-       return MS_MEDIA_ERR_NONE;
-}
-
 void msc_init_scan_thread(void)
 {
        g_mutex_init(&data_mutex2);
@@ -132,22 +97,30 @@ void msc_deinit_scan_thread(void)
        g_cond_clear(&data_cond2);
 }
 
-static int __msc_resume_scan(void)
+static void __msc_resume_scan(void)
 {
        g_mutex_lock(&data_mutex2);
 
-       g_cond_signal(&data_cond2);
+       if (is_scanner_paused) {
+               is_scanner_paused = false;
+               g_cond_signal(&data_cond2);
+               MS_DBG_WARN("storage scan resumed");
+       }
 
        g_mutex_unlock(&data_mutex2);
-
-       return MS_MEDIA_ERR_NONE;
 }
 
 static void __msc_pause_scan(void)
 {
        g_mutex_lock(&data_mutex2);
 
-       g_cond_wait(&data_cond2, &data_mutex2);
+       if (!is_scanner_paused) {
+               is_scanner_paused = true;
+               MS_DBG_WARN("storage scan paused");
+
+               while (is_scanner_paused)
+                       g_cond_wait(&data_cond2, &data_mutex2);
+       }
 
        g_mutex_unlock(&data_mutex2);
 }
@@ -354,7 +327,7 @@ static int __msc_dir_scan_for_folder(sqlite3 *handle, const char *storage_id, ch
                                        }
                                }
                        } else {
-                               if (scan_type != MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
+                               if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
                                        new_path = g_strdup(path);
                                        g_ptr_array_add(dir_array, new_path);
 
@@ -584,14 +557,8 @@ NEXT_SCAN:
                                if (ret != MS_MEDIA_ERR_NONE)
                                        break;
                        } else {
-                               __msc_set_dir_scan_cb(__msc_dir_scan_cb);
-
                                __msc_pause_scan();
 
-                               MS_DBG_WARN("wait folder scan callback");
-
-                               __msc_remove_dir_scan_cb();
-
                                ret = __msc_get_null_scan_folder_list(handle, storage_id, NULL, dir_array);
 
                                if (ret != MS_MEDIA_ERR_NONE)
@@ -614,16 +581,9 @@ NEXT_SCAN:
        }
 
        if ((g_directory_scan_processing2 != DIR_SCAN_NON_SCAN)
-               && (__msc_dir_and_storage_scan_same_path(new_start_path) == MS_MEDIA_ERR_NONE)) {
-               __msc_set_dir_scan_cb(__msc_dir_scan_cb);
-               MS_DBG_WARN("storage scan pause");
+               && (__msc_dir_and_storage_scan_same_path(new_start_path) == MS_MEDIA_ERR_NONE))
                __msc_pause_scan();
 
-               MS_DBG_WARN("wait folder scan callback");
-
-               __msc_remove_dir_scan_cb();
-       }
-
        goto END_SCAN;
 
 STOP_SCAN:
@@ -1006,7 +966,8 @@ gpointer msc_directory_scan_thread(gpointer data)
                ms_batch_commit_disable(uid);
 
                MS_DBG_WARN("folder scan done, sent cb event path = [%.*s]", MAX_MSG_SIZE, scan_data->msg);
-               __msc_call_dir_scan_cb();
+
+               __msc_resume_scan();
 
                /*revert to 3.0,folder scan should only delete invalid in folder instead of delete invalid in storage*/
                if (ms_delete_invalid_items_in_folder(handle, storage_id, scan_data->msg, is_recursive, uid) != MS_MEDIA_ERR_NONE)
@@ -1062,11 +1023,6 @@ _POWEROFF:
        return NULL;
 }
 
-static int __msc_dir_scan_cb(void)
-{
-       return __msc_resume_scan();
-}
-
 static void __msc_check_pvr_svc(void)
 {
        if (ms_is_support_pvr()) {