Modify external storage scan 73/47273/4
authorMinje Ahn <minje.ahn@samsung.com>
Wed, 2 Sep 2015 01:04:55 +0000 (10:04 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Thu, 3 Sep 2015 07:25:52 +0000 (16:25 +0900)
Change-Id: I58b2957cbe01c1490b1780d5ad8dbcbe310e7992
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
38 files changed:
Makefile.am
configure.ac
lib/include/media-server-ipc.h
lib/include/media-util-err.h
lib/include/media-util-register.h
lib/include/media-util.h
lib/media-util-db.c
lib/media-util-ipc.c
lib/media-util-register.c
src/common/include/media-common-db-svc.h [new file with mode: 0755]
src/common/include/media-common-dbg.h
src/common/include/media-common-external-storage.h
src/common/include/media-common-system.h [new file with mode: 0755]
src/common/include/media-common-types.h
src/common/include/media-common-utils.h
src/common/media-common-db-svc.c [new file with mode: 0755]
src/common/media-common-external-storage.c
src/common/media-common-system.c [new file with mode: 0755]
src/common/media-common-utils.c
src/mediadb-update.c
src/scanner/include/media-scanner-db-svc.h [deleted file]
src/scanner/include/media-scanner-dbg.h
src/scanner/include/media-scanner-device-block.h [new file with mode: 0755]
src/scanner/include/media-scanner-scan.h
src/scanner/media-scanner-db-svc.c [deleted file]
src/scanner/media-scanner-device-block.c [new file with mode: 0755]
src/scanner/media-scanner-scan.c
src/scanner/media-scanner-socket.c
src/scanner/media-scanner.c
src/server/include/media-server-db-svc.h [deleted file]
src/server/include/media-server-dbg.h
src/server/include/media-server-device-block.h [new file with mode: 0755]
src/server/include/media-server-socket.h
src/server/media-server-db-svc.c [deleted file]
src/server/media-server-device-block.c [new file with mode: 0755]
src/server/media-server-main.c
src/server/media-server-scanner.c
src/server/media-server-socket.c

index ab5acd5..908c6d7 100755 (executable)
@@ -6,7 +6,39 @@ AM_CPPFLAGS = $(FMS_DEBUG_FLAGS)
 
 AM_LDFLAGS=-Wl,--as-needed -Wl,--hash-style=both
 
-### pkgconfig ###
+
+######################################################
+#common flags for binary
+######################################################
+COMMON_CFLAGS = -I${srcdir}/lib/include \
+                -I${srcdir}/src/common/include \
+               $(GLIB_CFLAGS) \
+               $(GTHREAD_CFLAGS) \
+               $(DLOG_CFLAGS) \
+               $(INIPARSER_CFLAGS) \
+               $(DBUS_CFLAGS) \
+               $(LIBPMCONTROL_CFLAGS) \
+               $(SECURITY_CFLAGS) \
+               $(SMACK_CFLAGS)
+
+COMMON_CFLAGS += $(STATUS_CFLAGS)
+
+COMMON_LDADD = libmedia-utils.la \
+               $(GLIB_LIBS) \
+               $(GTHREAD_LIBS) \
+               $(DLOG_LIBS) \
+               $(INIPARSER_LIBS) \
+               $(DBUS_LIBS) \
+               $(LIBPMCONTROL_LIBS) \
+               $(SECURITY_LIBS) \
+               $(SMACK_LIBS) \
+               -ldl
+
+COMMON_LDADD += $(STATUS_LIBS)
+
+######################################################
+#pkgconfig
+######################################################
 pkgconfigdir = $(libdir)/pkgconfig
 dist_pkgconfig_DATA = libmedia-utils.pc
 
@@ -25,7 +57,9 @@ install-data-hook:
 fmsvcdir = /etc/rc.d/init.d
 dist_fmsvc_DATA = ./data/mediasvr
 
-### libmedia-utils.la ###
+######################################################
+#libmedia utils
+######################################################
 lib_LTLIBRARIES = libmedia-utils.la
 libmedia_utils_la_SOURCES = lib/media-util-noti.c \
                             lib/media-util-ipc.c \
@@ -37,6 +71,7 @@ libmedia_utils_la_CFLAGS = -I${srcdir}/lib/include \
                               $(GLIB_CFLAGS) \
                              $(DLOG_CFLAGS) \
                              $(DBUS_CFLAGS) \
+                             $(GIO_CFLAGS) \
                              $(SQLITE3_CFLAGS) \
                              $(DB_UTIL_CFLAGS) \
                              $(PHONESTATUS_CFLAGS) \
@@ -49,6 +84,7 @@ libmedia_utils_la_CFLAGS = -I${srcdir}/lib/include \
 libmedia_utils_la_LIBADD  =   $(GLIB_LIBS) \
                               $(DLOG_LIBS) \
                               $(DBUS_LIBS) \
+                             $(GIO_LIBS) \
                              $(SQLITE3_LIBS) \
                              $(DB_UTIL_LIBS) \
                              $(PHONESTATUS_LIBS) \
@@ -58,75 +94,58 @@ libmedia_utils_la_LIBADD  =   $(GLIB_LIBS) \
                              $(CYNARA_SESSION_LIBS) \
                              $(CYNARA_CREDS_SOCKET_LIBS)
 
-
-### file-manager-server ###
 bin_PROGRAMS = media-server \
                media-scanner \
                mediadb-update
-
+######################################################
+#media server
+######################################################
 media_server_SOURCES = src/common/media-common-utils.c \
-                       src/common/media-common-external-storage.c \
-                       src/server/media-server-db-svc.c \
+                       src/common/media-common-system.c \
+                      src/common/media-common-external-storage.c \
+                       src/common/media-common-db-svc.c \
                        src/server/media-server-db.c \
                        src/server/media-server-socket.c \
                        src/server/media-server-thumb.c \
                        src/server/media-server-scanner.c \
+                      src/server/media-server-device-block.c \
                        src/server/media-server-main.c
 
-media_server_CFLAGS = -I${srcdir}/src/common/include \
-                      -I${srcdir}/src/server/include \
-                      -I${srcdir}/lib/include \
-                      $(GTHREAD_CFLAGS) \
-                      $(GLIB_CFLAGS) \
-                      $(PHONESTATUS_CFLAGS) \
-                      $(DLOG_CFLAGS) \
-                      $(AUL_CFLAG)\
-                      $(LIBPMCONTROL_CFLAGS) \
-                      $(DBUS_CFLAGS) \
-                      $(STATUS_CFLAGS)
-
-media_server_LDADD = libmedia-utils.la \
-                       $(GLIB_LIBS) \
-                       $(GTHREAD_LIBS) \
-                       $(PHONESTATUS_LIBS) \
-                       $(DLOG_LIBS) \
-                       $(AUL_LIBS) \
-                       $(LIBPMCONTROL_LIBS) \
-                       $(DBUS_LIBS) \
-                       -ldl \
-                       $(STATUS_LIBS)
+media_server_CFLAGS = -I${srcdir}/src/server/include \
+                       $(COMMON_CFLAGS) \
+                        $(PHONESTATUS_CFLAGS)
+
+media_server_CFLAGS += -fPIE
+
+media_server_LDFLAGS = -pie
 
+media_server_LDADD = $(COMMON_LDADD) \
+                    $(PHONESTATUS_LIBS)
+
+######################################################
+#media scanner v1
+######################################################
 media_scanner_SOURCES = src/common/media-common-utils.c \
+                       src/common/media-common-system.c \
                        src/common/media-common-external-storage.c \
-                       src/scanner/media-scanner-db-svc.c \
+                       src/common/media-common-db-svc.c \
+                        src/scanner/media-scanner-device-block.c \
                        src/scanner/media-scanner-scan.c \
                        src/scanner/media-scanner-socket.c \
                        src/scanner/media-scanner.c
 
-media_scanner_CFLAGS = -I${srcdir}/lib/include \
-                       -I${srcdir}/src/common/include \
-                       -I${srcdir}/src/scanner/include \
-                       $(GTHREAD_CFLAGS) \
-                       $(GLIB_CFLAGS) \
-                       $(PHONESTATUS_CFLAGS) \
-                       $(DLOG_CFLAGS) \
-                       $(AUL_CFLAG)\
-                       $(LIBPMCONTROL_CFLAGS) \
-                       $(DBUS_CFLAGS) \
-                       $(STATUS_CFLAGS)
-
-media_scanner_LDADD = libmedia-utils.la \
-                       $(GLIB_LIBS) \
-                       $(GTHREAD_LIBS) \
-                       $(PHONESTATUS_LIBS) \
-                       $(DLOG_LIBS) \
-                       $(AUL_LIBS) \
-                       $(LIBPMCONTROL_LIBS) \
-                       $(THUMB_GEN_LIBS) \
-                       $(DBUS_LIBS) \
-                       -ldl \
-                       $(STATUS_LIBS)
+media_scanner_CFLAGS = -I${srcdir}/src/scanner/include \
+                       $(COMMON_CFLAGS)
+
+media_scanner_CFLAGS += -fPIE
+
+media_scanner_LDFLAGS = -pie
+
+media_scanner_LDADD = $(COMMON_LDADD)
 
+######################################################
+#mediadb_udpate
+######################################################
 mediadb_update_SOURCES = src/mediadb-update.c
 
 mediadb_update_CFLAGS = -I${srcdir}/lib/include \
index fbb2da2..40f4b0e 100755 (executable)
@@ -82,6 +82,10 @@ PKG_CHECK_MODULES(DBUS, dbus-glib-1)
 AC_SUBST(DBUS_CFLAGS)
 AC_SUBST(DBUS_LIBS)
 
+PKG_CHECK_MODULES(GIO, gio-2.0)
+AC_SUBST(GIO_CFLAGS)
+AC_SUBST(GIO_LIBS)
+
 PKG_CHECK_MODULES(SQLITE, sqlite3)
 AC_SUBST(SQLITE3_CFLAGS)
 AC_SUBST(SQLITE3_LIBS)
index 1d90228..436182d 100755 (executable)
@@ -59,6 +59,8 @@ typedef enum{
        MS_MSG_SCANNER_BULK_RESULT,                     /**< Request bulk insert */
        MS_MSG_STORAGE_META,                            /**< Request updating meta data */
        MS_MSG_DIRECTORY_SCANNING_CANCEL,       /**< Request cancel directory scan*/
+       MS_MSG_STORAGE_SCANNER_COMPLETE,        /**< Storage Scanner complete */
+       MS_MSG_DIRECTORY_SCANNER_COMPLETE,      /**< Directory Scanner complete */
        MS_MSG_MAX                                                      /**< Invalid msg type */
 }ms_msg_type_e;
 
@@ -73,6 +75,8 @@ typedef struct
        char *sock_path;
 }ms_sock_info_s;
 
+#define MS_UUID_SIZE 37 /* size of uuid + NULL */
+
 typedef struct
 {
        ms_msg_type_e msg_type;
@@ -80,6 +84,7 @@ typedef struct
        uid_t uid;
        int result;
        size_t msg_size; /*this is size of message below and this does not include the terminationg null byte ('\0'). */
+       char storage_id[MS_UUID_SIZE];
        char msg[MAX_MSG_SIZE];
 }ms_comm_msg_s;
 
index 5873cf0..fcb6c37 100755 (executable)
@@ -83,6 +83,7 @@
 #define MS_MEDIA_ERR_INVALID_IPC_MESSAGE                       -905  /* received message is not valid */
 #define MS_MEDIA_ERR_DATA_TAINTED                              -906  /* received data is tainted */
 #define MS_MEDIA_ERR_SEND_NOTI_FAIL                            -907  /* sending notification is failed */
+#define MS_MEDIA_ERR_USB_UNMOUNTED                             -908 /* USB unmounted */
 
 #define MS_MEDIA_ERR_MAX                                                       -999
 
index b17c088..65bc2cd 100755 (executable)
@@ -46,7 +46,7 @@ typedef struct
 typedef void (*scan_complete_cb)(media_request_result_s *, void *);
 typedef void (*insert_complete_cb)(media_request_result_s *, void *);
 
-int media_directory_scanning_async(const char *directory_path, bool recursive_on, scan_complete_cb user_callback, void *user_data, uid_t uid);
+int media_directory_scanning_async(const char *directory_path, const char *storage_id, bool recursive_on, scan_complete_cb user_callback, void *user_data, uid_t uid);
 
 int media_directory_scanning_cancel(const char *directory_path, uid_t uid);
 
index efa4bb4..11d4c70 100755 (executable)
 #include <tzplatform_config.h>
 
 #define MEDIA_ROOT_PATH_INTERNAL       tzplatform_getenv(TZ_USER_HOME)
-#define MEDIA_ROOT_PATH_SDCARD tzplatform_mkpath(TZ_SYS_STORAGE, "sdcard")
+#define MEDIA_ROOT_PATH_EXTERNAL       tzplatform_getenv(TZ_SYS_STORAGE)
+#define MEDIA_ROOT_PATH_SDCARD tzplatform_getenv(TZ_SYS_STORAGE)
+#define MEDIA_ROOT_PATH_USB            tzplatform_getenv(TZ_SYS_STORAGE)
+#define MEDIA_ROOT_PATH_CLOUD  tzplatform_mkpath(TZ_SYS_STORAGE, "tnfs/cloud")
 #define MEDIA_DATA_PATH                tzplatform_mkpath(TZ_SYS_DATA, "file-manager-service")
 #define MEDIA_DB_NAME          tzplatform_mkpath(TZ_USER_DB, ".media.db")              /**<  media db name*/
 #define MEDIA_CONTENT_PATH             "content"               /**<  user content folder name*/
index d8cdbad..6a2327d 100755 (executable)
@@ -61,7 +61,7 @@ static int __media_db_busy_handler(void *pData, int count)
 
        MSAPI_DBG("media_db_busy_handler called : %d", count);
 
-       return 100 - count;
+       return 200 - count;
 }
 
 static char* __media_get_media_DB(uid_t uid)
@@ -374,26 +374,39 @@ static int __media_db_request_batch_update(ms_msg_type_e msg_type, const char *r
        return ret;
 }
 
+#define RETRY_CNT 9
+#define SLEEP_TIME 1000 * 1000
 static int _media_db_update_directly(sqlite3 *db_handle, const char *sql_str)
 {
        int ret = MS_MEDIA_ERR_NONE;
        char *zErrMsg = NULL;
+       int retry_count = 0;
 
 //     MSAPI_DBG_SLOG("SQL = [%s]", sql_str);
 
+EXEC_RETRY:
        ret = sqlite3_exec(db_handle, sql_str, NULL, NULL, &zErrMsg);
 
        if (SQLITE_OK != ret) {
                MSAPI_DBG_ERR("DB Update Fail SQL:%s", sql_str);
                MSAPI_DBG_ERR("ERROR [%s]", zErrMsg);
-               if (ret == SQLITE_BUSY)
+               if (ret == SQLITE_BUSY) {
                        ret = MS_MEDIA_ERR_DB_BUSY_FAIL;
-               else if (ret == SQLITE_CONSTRAINT)
+               } else if (ret == SQLITE_CONSTRAINT) {
                        ret = MS_MEDIA_ERR_DB_CONSTRAINT_FAIL;
-               else if (ret == SQLITE_FULL)
+               } else if (ret == SQLITE_FULL) {
                        ret = MS_MEDIA_ERR_DB_FULL_FAIL;
-               else
+               } else if (ret == SQLITE_LOCKED) {
+                       if (retry_count < RETRY_CNT) {
+                               MSAPI_DBG_ERR("Locked retry[%d]", retry_count);
+                               retry_count++;
+                               usleep(SLEEP_TIME);
+                               goto EXEC_RETRY;
+                       }
                        ret = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               } else {
+                       ret = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               }
        }
 
        if (zErrMsg)
index b22e850..1f43278 100755 (executable)
@@ -206,7 +206,7 @@ int ms_ipc_create_server_socket(ms_protocol_e protocol, ms_msg_port_type_e port,
        strncpy(serv_addr.sun_path, MEDIA_IPC_PATH[serv_port], strlen(MEDIA_IPC_PATH[serv_port]));
 
        /* Bind to the local address */
-       for (i = 0; i < 20; i ++) {
+       for (i = 0; i < 100; i ++) {
                if (bind(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == 0) {
                        bind_success = true;
                        break;
@@ -283,7 +283,13 @@ int ms_ipc_send_msg_to_server_tcp(int sockfd, ms_msg_port_type_e port, ms_comm_m
        if (connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
                MSAPI_DBG_STRERROR("connect error");
                close(sockfd);
-               return MS_MEDIA_ERR_SOCKET_CONN;
+
+               res = MS_MEDIA_ERR_SOCKET_CONN;
+
+               if (errno == EACCES || errno == EPERM)
+                       res = MS_MEDIA_ERR_PERMISSION_DENIED;
+
+               return res;
        }
 
        if (write(sockfd, send_msg, sizeof(*(send_msg))) != sizeof(*(send_msg))) {
@@ -401,13 +407,13 @@ int ms_ipc_wait_block_message(int sockfd, void *recv_msg, unsigned int msg_size,
                        block_size = remain_size;
                }
                if ((recv_msg_size = recvfrom(sockfd, block_buf, block_size, 0, (struct sockaddr *)recv_addr, &addr_len)) < 0) {
-                       MSAPI_DBG_ERR("recvfrom failed [%s]", strerror(errno));
+                       MSAPI_DBG_STRERROR("recvfrom failed");
                        if (errno == EWOULDBLOCK) {
                                MSAPI_DBG_ERR("recvfrom Timeout.");
                                MS_SAFE_FREE(block_buf);
                                return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
                        } else {
-                               MSAPI_DBG_ERR("recvfrom error [%s]", strerror(errno));
+                               MSAPI_DBG_STRERROR("recvfrom error");
                                MS_SAFE_FREE(block_buf);
                                return MS_MEDIA_ERR_SOCKET_RECEIVE;
                        }
index 361d071..ed0a215 100755 (executable)
@@ -181,11 +181,11 @@ gboolean _read_socket(GIOChannel *src, GIOCondition condition, gpointer data)
        req_result.pid = recv_msg.pid;
        req_result.result = recv_msg.result;
        if (recv_msg.msg_type ==MS_MSG_SCANNER_RESULT) {
-               req_result.complete_path = strndup(recv_msg.msg, MAX_FILEPATH_LEN);
+               req_result.complete_path = strndup(recv_msg.msg, recv_msg.msg_size);
                req_result.request_type = MEDIA_DIRECTORY_SCAN;
                MSAPI_DBG("complete_path :%d", req_result.complete_path);
        } else if (recv_msg.msg_type == MS_MSG_SCANNER_BULK_RESULT) {
-               req_result.complete_path = strndup(recv_msg.msg, MAX_FILEPATH_LEN);
+               req_result.complete_path = strndup(recv_msg.msg, recv_msg.msg_size);
                req_result.request_type = MEDIA_FILES_REGISTER;
        }
 
@@ -354,7 +354,7 @@ static int _attach_callback(const char *req_path, int *sockfd, char* sock_path,
        return MS_MEDIA_ERR_NONE;
 }
 
-static int __media_db_request_update_async(ms_msg_type_e msg_type, const char *request_msg, scan_complete_cb user_callback, void *user_data, uid_t uid)
+static int __media_db_request_update_async(ms_msg_type_e msg_type, const char *storage_id, const char *request_msg, scan_complete_cb user_callback, void *user_data, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
        int request_msg_size = 0;
@@ -370,7 +370,7 @@ static int __media_db_request_update_async(ms_msg_type_e msg_type, const char *r
                return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
 
-       MSAPI_DBG("REQUEST DIRECTORY SCANNING");
+       MSAPI_DBG("REQUEST DIRECTORY SCANNING[%s]", request_msg);
 
        request_msg_size = strlen(request_msg);
        if(request_msg_size >= MAX_MSG_SIZE)
@@ -387,6 +387,9 @@ static int __media_db_request_update_async(ms_msg_type_e msg_type, const char *r
        send_msg.msg_size = request_msg_size;
        send_msg.uid = uid;
        strncpy(send_msg.msg, request_msg, request_msg_size);
+       if (storage_id != NULL) {
+               strncpy(send_msg.storage_id, storage_id, MS_UUID_SIZE -1);
+       }
 
        /*Create Socket*/
        ret = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, 0, &sock_info);
@@ -460,7 +463,7 @@ static int __media_db_request_update_cancel(ms_msg_type_e msg_type, const char *
        return ret;
 }
 
-int media_directory_scanning_async(const char *directory_path, bool recursive_on, scan_complete_cb user_callback, void *user_data, uid_t uid)
+int media_directory_scanning_async(const char *directory_path, const char *storage_id, bool recursive_on, scan_complete_cb user_callback, void *user_data, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
 
@@ -469,9 +472,9 @@ int media_directory_scanning_async(const char *directory_path, bool recursive_on
                return ret;
 
        if (recursive_on == TRUE)
-               ret = __media_db_request_update_async(MS_MSG_DIRECTORY_SCANNING, directory_path, user_callback, user_data, uid);
+               ret = __media_db_request_update_async(MS_MSG_DIRECTORY_SCANNING, storage_id, directory_path, user_callback, user_data, uid);
        else
-               ret = __media_db_request_update_async(MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE, directory_path, user_callback, user_data, uid);
+               ret = __media_db_request_update_async(MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE, storage_id, directory_path, user_callback, user_data, uid);
 
        return ret;
 }
@@ -489,12 +492,11 @@ int media_directory_scanning_cancel(const char *directory_path, uid_t uid)
        return ret;
 }
 
-
 int media_files_register(const char *list_path, insert_complete_cb user_callback, void *user_data, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
 
-       ret = __media_db_request_update_async(MS_MSG_BULK_INSERT, list_path, user_callback, user_data, uid);
+       ret = __media_db_request_update_async(MS_MSG_BULK_INSERT, NULL, list_path, user_callback, user_data, uid);
 
        MSAPI_DBG("client receive: %d", ret);
 
@@ -505,7 +507,7 @@ int media_burstshot_register(const char *list_path, insert_complete_cb user_call
 {
        int ret = MS_MEDIA_ERR_NONE;
 
-       ret = __media_db_request_update_async(MS_MSG_BURSTSHOT_INSERT, list_path, user_callback, user_data, uid);
+       ret = __media_db_request_update_async(MS_MSG_BURSTSHOT_INSERT, NULL, list_path, user_callback, user_data, uid);
 
        MSAPI_DBG("client receive: %d", ret);
 
diff --git a/src/common/include/media-common-db-svc.h b/src/common/include/media-common-db-svc.h
new file mode 100755 (executable)
index 0000000..ccb29a6
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ *  Media Server
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+#ifndef _MEDIA_COMMON_DB_SVC_H_
+#define _MEDIA_COMMON_DB_SVC_H_
+
+#include <glib.h>
+
+#include "media-common-types.h"
+
+#define INTERNAL_STORAGE_ID    "media"
+#define MMC_STORAGE_ID         "media"
+
+typedef struct ms_dir_info_s {
+       char *dir_path;
+       int modified_time;
+       int item_num;
+} ms_dir_info_s;
+
+typedef struct ms_stg_info_s {
+       char *stg_path;
+       char *storage_id;
+       int scan_status;
+} ms_stg_info_s;
+
+typedef enum {
+       MS_NOTI_SWITCH_ON = 0,
+       MS_NOTI_SWITCH_OFF = 1,
+} ms_noti_switch_e;
+
+typedef int (*CONNECT)(void**, uid_t, char **);
+typedef int (*DISCONNECT)(void*, char **);
+typedef int (*INSERT_ITEM_BEGIN)(void*, int, int, int, char **);
+typedef int (*INSERT_ITEM_END)(void*, uid_t, char **);
+typedef int (*SET_ITEM_VALIDITY_BEGIN)(void*, int, char **);
+typedef int (*SET_ITEM_VALIDITY_END)(void*, uid_t, char **);
+typedef int (*UPDATE_BEGIN)(void);
+typedef int (*UPDATE_END)(const char *, uid_t);
+
+typedef int (*SEND_DIR_UPDATE_NOTI)(void *, const char *, const char *, char **);
+typedef int (*CHECK_ITEM_EXIST)(void*, const char *, const char *, bool*, char **);
+typedef int (*INSERT_ITEM)(void*, const char*, const char *, int, uid_t, char **);
+typedef int (*INSERT_ITEM_IMMEDIATELY)(void*, const char *, const char *, int, uid_t, char **);
+typedef int (*INSERT_BURST_ITEM)(void *, const char *, const char *, int, uid_t, char **);
+typedef int (*SET_ALL_STORAGE_ITEMS_VALIDITY)(void*, const char *, int, int, uid_t, char **);
+typedef int (*SET_FOLDER_ITEM_VALIDITY)(void*, const char*, const char *, int, int, uid_t, char**);
+typedef int (*SET_ITEM_VALIDITY)(void*, const char*, const char *, int, int, uid_t, char **);
+typedef int (*DELETE_ITEM)(void *, const char *, const char *, uid_t, char **);
+typedef int (*DELETE_ALL_ITEMS_IN_STORAGE)(void*, const char *, int, uid_t, char **);
+typedef int (*DELETE_ALL_INVALID_ITMES_IN_STORAGE)(void*, const char *, int, uid_t, char **);
+typedef int (*DELETE_ALL_INVALID_ITEMS_IN_FOLDER)(void*, const char *, const char*, bool, uid_t, char**);
+typedef int (*COUNT_DELETE_ITEMS_IN_FOLDER)(void *, const char *, const char *, int *, char **);
+typedef int (*GET_FOLDER_LIST)(void *, const char *, char*, char ***, int **, int *, char **);
+typedef int (*UPDATE_FOLDER_TIME)(void *, const char *, const char *, uid_t, char **);
+typedef int (*GET_STORAGE_ID)(void *, const char *, char *, char **);
+typedef int (*GET_STORAGE_SCAN_STATUS)(void *, const char *, int *, char **);
+typedef int (*SET_STORAGE_SCAN_STATUS)(void *, const char *, int, uid_t, char **);
+typedef int (*INSERT_FOLDER)(void*, const char*, const char*, int, uid_t, char **);
+typedef int (*DELETE_INVALID_FOLDER)(void*, const char*, uid_t, char **);
+typedef int (*SET_FOLDER_VALIDITY)(void*, const char*, const char*, int, bool, uid_t, char **);
+typedef int (*INSERT_FOLDER_BEGIN)(void *, int, char**);
+typedef int (*INSERT_FOLDER_END)(void*, uid_t, char**);
+typedef int (*INSERT_ITEM_SCAN)(void*, const char*, const char*, int, uid_t, char **);
+typedef int (*UPDATE_ITEM_EXTRACT)(void*, const char*, int, int, const char*, uid_t, char **);
+
+typedef int (*CHECK_DB)(void*, bool*, uid_t, char **);
+typedef int (*GET_UUID)(void *, char **, char **);
+typedef int (*GET_MMC_INFO)(void *, char **, char **, int *, bool *, char **);
+typedef int (*CHECK_STORAGE)(void *, const char *, const char *, char **, int *, char **);
+typedef int (*INSERT_STORAGE)(void *, const char *, int , const char *, const char *, uid_t, char **);
+typedef int (*UPDATE_STORAGE)(void *, const char *, const char *, uid_t, char **);
+typedef int (*DELETE_STORAGE)(void *, const char *, const char *, uid_t, char **);
+typedef int (*SET_STORAGE_VALIDITY)(void *, const char *, int, uid_t uid, char **);
+typedef int (*SET_ALL_STORAGE_VALIDITY)(void *, int, uid_t, char **);
+
+typedef int (*UPDATE_ITEM_META)(void *, const char *, int, uid_t, char **);
+typedef int (*UPDATE_ITEM_BEGIN)(void *, int, char **);
+typedef int (*UPDATE_ITEM_END)(void *, uid_t, char **);
+
+int ms_load_functions(void);
+void ms_unload_functions(void);
+int ms_get_insert_count();
+void ms_reset_insert_count();
+int ms_connect_db(void ***handle, uid_t uid);
+int ms_disconnect_db(void ***handle);
+int ms_validate_item(void **handle, const char *storage_id, const char *path, uid_t uid);
+int ms_insert_item_batch(void **handle, const char *storage_id, const char *path, uid_t uid);
+int ms_insert_item_pass2(void **handle, const char *storage_id, const char *path, int scan_type, uid_t uid);
+int ms_insert_item_immediately(void **handle, const char *storage_id, const char *path, uid_t uid);
+int ms_insert_burst_item(void **handle, const char *storage_id, const char *path, uid_t uid);
+bool ms_delete_all_items(void **handle, const char *storage_id, ms_storage_type_t store_type, uid_t uid);
+int ms_validaty_change_all_items(void **handle, const char *storage_id, ms_storage_type_t store_type, bool validity, uid_t uid);
+bool ms_delete_invalid_items(void **handle, const char *storage_id, ms_storage_type_t store_type, uid_t uid);
+int ms_set_folder_item_validity(void **handle, const char *storage_id, const char *path, int validity, int recursive, uid_t uid);
+int ms_delete_invalid_items_in_folder(void **handle, const char *storage_id, const char *path, bool is_recursive, uid_t uid);
+int ms_send_dir_update_noti(void **handle, const char *storage_id, const char *path);
+int ms_count_delete_items_in_folder(void **handle, const char *storage_id, const char*path, int *count);
+int ms_get_folder_list(void **handle, const char *storage_id, char* start_path, GArray **dir_array);
+int ms_update_folder_time(void **handle, const char *storage_id, char *folder_path, uid_t uid);
+int ms_get_storage_id(void **handle, const char *path, char *storage_id);
+int ms_get_storage_scan_status(void **handle, char *storage_id, media_scan_status_e *scan_status);
+int ms_set_storage_scan_status(void **handle, char *storage_id, media_scan_status_e scan_status, uid_t uid);
+int ms_insert_folder(void **handle, const char *storage_id, const char *path, uid_t uid);
+int ms_delete_invalid_folder(void **handle, const char *storage_id, uid_t uid);
+int ms_set_folder_validity(void **handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid);
+int ms_scan_item_batch(void **handle, const char *storage_id, const char *path, uid_t uid);
+int ms_scan_validate_item(void **handle, const char *storage_id, const char *path, uid_t uid);
+
+int ms_check_db_upgrade(void **handle, bool *need_full_scan, uid_t uid);
+int ms_genarate_uuid(void **handle, char **uuid);
+int ms_get_mmc_info(void **handle, char **storage_name, char **storage_path, int *validity, bool *info_exist);
+int ms_check_storage(void **handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity);
+int ms_insert_storage(void **handle, const char *storage_id, const char *storage_name, const char *storage_path, uid_t uid);
+int ms_update_storage(void **handle, const char *storage_id, const char *storage_path, uid_t uid);
+int ms_delete_storage(void **handle, const char *storage_id, const char *storage_name, uid_t uid);
+int ms_set_storage_validity(void **handle, const char *storage_id, int validity, uid_t uid);
+int ms_set_all_storage_validity(void **handle, int validity, uid_t uid);
+int ms_update_meta_batch(void **handle, const char *path, uid_t uid);
+
+/****************************************************************************************************
+FOR BULK COMMIT
+*****************************************************************************************************/
+void ms_register_start(void **handle, ms_noti_switch_e noti_status, int pid);
+void ms_register_end(void **handle, const char *start_path, uid_t uid);
+void ms_validate_start(void **handle);
+void ms_validate_end(void **handle, uid_t uid);
+void ms_insert_folder_start(void **handle);
+void ms_insert_folder_end(void **handle, uid_t uid);
+void ms_update_start(void **handle);
+void ms_update_end(void **handle, uid_t uid);
+
+#endif /*_MEDIA_COMMON_DB_SVC_H_*/
index 9f0336d..eaee743 100755 (executable)
                        LOGE(fmt" : STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGTH)); \
                } while (0)
 
-#define MS_DBG_SLOG(fmt, args...)      SECURE_LOGD(fmt "\n", ##args);
+#define MS_DBG_SLOG(fmt, args...) do{ if (true) { \
+               SECURE_LOGD(fmt "\n" , ##args); \
+               }} while(false)
 
-#define MS_DBG(fmt, args...)        LOGD(fmt "\n", ##args);
+#define MS_DBG(fmt, args...) do{ if (true) { \
+               LOGD(fmt "\n" , ##args); \
+               }} while(false)
 
 #define MS_DBG_INFO(fmt, args...) do{ if (true) { \
                LOGI(fmt "\n" , ##args); \
                LOGE(fmt "\n", ##args); \
                }} while(false)
 
+#define MS_DBG_FENTER() do{ if (true) { \
+               LOGD("<ENTER> \n"); \
+               }} while(false)
+
+#define MS_DBG_FLEAVE() do{ if (true) { \
+               LOGD("<LEAVE> \n"); \
+               }} while(false)
+
 #endif /*_MEDIA_COMMON_DBG_H_*/
index 500191b..d1b7ef3 100755 (executable)
 
 #include "media-common-types.h"
 
-void ms_init_default_path(void);
-void ms_make_default_path_mmc(void);
-int ms_update_mmc_info(void);
-void ms_mmc_removed_handler(void);
+#ifndef DISABLE_NOTIFICATION
 int ms_present_mmc_status(ms_sdcard_status_type_t status);
-void ms_mmc_vconf_cb(void *data);
-ms_dir_scan_type_t ms_get_mmc_state(void);
+#endif
+
+int ms_get_mmc_id(char **cid);
+int ms_get_stg_changed_event(void);
+
+int ms_read_device_info(const char *root_path, char **device_uuid);
+int ms_write_device_info(const char *root_path, char *device_uuid);
 
 #endif /*_MEDIA_SERVER_EXTERNAL_STORAGE_H_*/
diff --git a/src/common/include/media-common-system.h b/src/common/include/media-common-system.h
new file mode 100755 (executable)
index 0000000..944b8f3
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ *  Media Server
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+#ifndef _MEDIA_COMMON_SYSTEM_H_
+#define _MEDIA_COMMON_SYSTEM_H_
+
+#include <glib.h>
+
+typedef enum {
+       MS_STG_REMOVED = 0,
+       MS_STG_INSERTED = 1,
+       MS_STG_MAX,
+} ms_stg_status_e;
+
+typedef enum {
+       MS_STG_TYPE_USB = 0,
+       MS_STG_TYPE_MMC = 1,
+       MS_STG_TYPE_ALL = 2,
+       MS_STG_TYPE_MAX,
+} ms_stg_type_e;
+
+typedef struct ms_block_info_s{
+       char *mount_path;
+       int state;
+       int block_type;
+} ms_block_info_s;
+
+typedef void (*block_changed_cb)(const char *mount_path, int block_type, ms_stg_status_e mount_status, void *user_data);
+int ms_sys_set_device_block_event_cb(block_changed_cb usr_callback, void *usr_data);
+int ms_sys_unset_device_block_event_cb(void);
+int ms_sys_get_device_list(ms_stg_type_e stg_type, GArray **dev_list);
+int ms_sys_release_device_list(GArray **dev_list);
+
+typedef void (*power_off_cb)(void *user_data);
+int ms_sys_set_poweroff_cb(power_off_cb user_callback, void *user_data);
+int ms_sys_unset_poweroff_cb(void);
+
+int ms_sys_get_uid(uid_t *uid);
+
+
+#endif
index 95674ed..cdbf708 100755 (executable)
 #define MS_NON_RECURSIVE 0
 
 /*This macro is used to save and check information of inserted memory card*/
-#define MS_MMC_INFO_KEY "db/private/mediaserver/mmc_info"
+//#define MS_MMC_INFO_KEY "db/private/mediaserver/mmc_info"
 
 /*Use for Poweroff sequence*/
-#define POWEROFF_NOTI_NAME "power_off_start" /*poeroff noti from system-server*/
-#define POWEROFF_DIR_PATH tzplatform_mkpath(TZ_SYS_DATA,"data-media/_POWER_OFF") /*This path uses for stopping Inotify thread and Socket thread*/
-#define POWEROFF_DIR_NAME "_POWER_OFF" /*This path uses for stopping Inotify thread and Socket thread*/
 #define POWEROFF -1 /*This number uses for stopping Scannig thread*/
 
 #define MS_SAFE_FREE(src)      { if(src) {free(src); src = NULL;} }
 #define MS_MALLOC(src, size)   { if (size > SIZE_MAX || size <= 0) {src = NULL;} \
                                                        else { src = malloc(size); if(src) memset(src, 0x0, size);} }
+#define MS_STRING_VALID(str)   \
+                                                               ((str != NULL && strlen(str) > 0) ? TRUE : FALSE)
 
 /*System default folder definition*/
 #define FAT_FILENAME_LEN_MAX          255      /* not inc null */
@@ -61,8 +60,9 @@
 #define MS_SOCK_NOT_ALLOCATE -1
 
 typedef enum {
-       MS_STORAGE_INTERNAL,    /**< Stored only in phone */
-       MS_STORAGE_EXTERNAL,         /**< Stored only in MMC */
+       MS_STORAGE_INTERNAL = 0,        /**< The device's internal storage */
+       MS_STORAGE_EXTERNAL = 1,        /**< The device's external storage */
+       MS_STORAGE_EXTERNAL_USB = 2,    /**< The external USB storage (Since 2.4) */
 } ms_storage_type_t;
 
 typedef enum {
@@ -79,7 +79,8 @@ typedef enum {
 
 typedef enum {
        MS_DB_UPDATING = 0,
-       MS_DB_UPDATED = 1
+       MS_DB_UPDATED = 1,
+       MS_DB_STOPPED = 2
 } ms_db_status_type_t;
 
 typedef struct ms_dir_scan_info {
@@ -102,4 +103,29 @@ typedef struct {
        int pid;
 } ms_register_data_t;
 
+
+/*use for MS_SCANNER_STATUS */
+typedef enum {
+       MS_STORAGE_SCAN_NONE,                   /**< Media Scanner not detect storage yet*/
+       MS_STORAGE_SCAN_PREPARE,                        /**< Media Scanner detect storage but not scanning yet*/
+       MS_STORAGE_SCAN_PROCESSING,             /**< Media Scanner Start Scanning storage*/
+       MS_STORAGE_SCAN_STOP,                   /**< Stop scanning storage*/
+       MS_STORAGE_SCAN_DONE,                   /**< Scanning Done but need to extract metadata*/
+       MS_STORAGE_SCAN_META_PROCESSING,        /**< Scanning Done and start to extract metadata*/
+       MS_STORAGE_SCAN_META_STOP,              /**< Stop extract metadata*/
+       MS_STORAGE_SCAN_COMPLETE,               /**< Complete scanning*/
+}ms_storage_scan_status_e;
+
+typedef enum{
+       MEDIA_SCAN_PREPARE              = 0,    /**< Prepare scanning*/
+       MEDIA_SCAN_PROCESSING   = 1,    /**< Process scanning*/
+       MEDIA_SCAN_STOP                 = 2,    /**< Stop scanning*/
+       MEDIA_SCAN_COMPLETE             = 3,    /**< Complete scanning*/
+       MEDIA_SCAN_MAX                  = 4,    /**< Invalid status*/
+       MEDIA_EXTRACT_PREPARE           = 5,    /**< Prepare extract*/
+       MEDIA_EXTRACT_PROCESSING        = 6,    /**< Process extract*/
+       MEDIA_EXTRACT_STOP                      = 7,    /**< Stop extract*/
+       MEDIA_EXTRACT_COMPLETE          = 8             /**< Complete extract*/
+}media_scan_status_e;
+
 #endif /*_MEDIA_SERVER_TYPES_H_*/
index 8a43be4..d91dfdc 100755 (executable)
@@ -35,7 +35,7 @@ int ms_strappend(char *res, const int size, const char *pattern, const char *str
 int ms_strcopy(char *res, const int size, const char *pattern, const char *str1);
 bool ms_config_get_int(const char *key, int *value);
 bool ms_config_set_int(const char *key, int value);
-bool ms_config_get_str(const char *key, char *value);
+bool ms_config_get_str(const char *key, char **value);
 bool ms_config_set_str(const char *key, const char *value);
 bool ms_config_get_bool(const char *key, int *value);
 #ifdef FMS_PERF
@@ -44,6 +44,5 @@ void ms_check_end_time(struct timeval *end_time);
 void ms_check_time_diff(struct timeval *start_time, struct timeval *end_time);
 #endif/*FMS_PERF */
 
-
 #endif/*_MEDIA_SERVER_UTILS_H__*/
 
diff --git a/src/common/media-common-db-svc.c b/src/common/media-common-db-svc.c
new file mode 100755 (executable)
index 0000000..26feabc
--- /dev/null
@@ -0,0 +1,1278 @@
+/*
+ *  Media Server
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+#include <dlfcn.h>
+
+#include "media-util.h"
+
+#include "media-common-utils.h"
+#include "media-common-db-svc.h"
+#include "media-common-dbg.h"
+#include <tzplatform_config.h>
+
+#define CONFIG_PATH tzplatform_mkpath(TZ_SYS_DATA,"file-manager-service/plugin-config")
+#define EXT ".so"
+#define EXT_LEN 3
+#define MSC_REGISTER_COUNT 300 /*For bundle commit*/
+#define MSC_VALID_COUNT 300 /*For bundle commit*/
+
+static GMutex db_mutex;
+static GArray *so_array;
+static void ***func_array;
+static int lib_num;
+static void **func_handle = NULL; /*dlopen handel*/
+static int insert_count;
+
+enum func_list {
+       eCONNECT,
+       eDISCONNECT,
+       eEXIST,
+       eINSERT_BEGIN,
+       eINSERT_END,
+       eINSERT_BATCH,
+       eINSERT_SCAN,
+       eUPDATE_EXTRACT,
+       eINSERT_ITEM_IMMEDIATELY,
+       eSET_ALL_VALIDITY,
+       eSET_VALIDITY_BEGIN,
+       eSET_VALIDITY_END,
+       eSET_VALIDITY,
+       eDELETE_ALL,
+       eDELETE_INVALID_ITEMS,
+       eUPDATE_BEGIN,
+       eUPDATE_END,
+       eSET_FOLDER_ITEM_VALIDITY,
+       eDELETE_FOLDER,
+       eINSERT_BURST,
+       eSEND_DIR_UPDATE_NOTI,
+       eCOUNT_DELETE_ITEMS_IN_FOLDER,
+       eDELETE_ITEM,
+       eGET_FOLDER_LIST,
+       eUPDATE_FOLDER_TIME,
+       eGET_STORAGE_ID,
+       eGET_STORAGE_SCAN_STATUS,
+       eSET_STORAGE_SCAN_STATUS,
+       eINSERT_FOLDER,
+       eDELETE_INVALID_FOLDER,
+       eSET_FOLDER_VALIDITY,
+       eINSERT_FOLDER_BEGIN,
+       eINSERT_FOLDER_END,
+       eCHECK_DB,
+       eGET_UUID,
+       eGET_MMC_INFO,
+       eCHECK_STORAGE,
+       eINSERT_STORAGE,
+       eUPDATE_STORAGE,
+       eDELETE_STORAGE,
+       eSET_STORAGE_VALIDITY,
+       eSET_ALL_STORAGE_VALIDITY,
+       eUPDATE_ITEM_META,
+       eUPDATE_ITEM_BEGIN,
+       eUPDATE_ITEM_END,
+       eFUNC_MAX
+};
+
+static int _ms_token_data(char *buf, char **name)
+{
+       int len;
+       char* pos = NULL;
+
+       pos = strstr(buf, EXT);
+       if (pos == NULL) {
+               MS_DBG_ERR("This is not shared object library.");
+               name = NULL;
+               return -1;
+       } else {
+               len = pos - buf + EXT_LEN;
+               *name = strndup(buf, len);
+       }
+
+       return 0;
+}
+
+static bool _ms_load_config()
+{
+       int ret;
+       FILE *fp;
+       char *so_name = NULL;
+       char buf[256] = {0};
+
+       fp = fopen(CONFIG_PATH, "rt");
+       if (fp == NULL) {
+               MS_DBG_ERR("fp is NULL");
+               return MS_MEDIA_ERR_FILE_OPEN_FAIL;
+       }
+       while(1) {
+               if (fgets(buf, 256, fp) == NULL)
+                       break;
+
+               ret = _ms_token_data(buf, &so_name);
+               if (ret == 0) {
+                       /*add to array*/
+                       g_array_append_val(so_array, so_name);
+                       so_name = NULL;
+               }
+       }
+
+       fclose(fp);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int ms_load_functions(void)
+{
+       int lib_index = 0, func_index;
+       char func_list[eFUNC_MAX][40] = {
+               "connect_db",
+               "disconnect_db",
+               "check_item_exist",
+               "insert_item_begin",
+               "insert_item_end",
+               "insert_item",
+               "insert_item_scan",
+               "update_item_extract",
+               "insert_item_immediately",
+               "set_all_storage_items_validity",
+               "set_item_validity_begin",
+               "set_item_validity_end",
+               "set_item_validity",
+               "delete_all_items_in_storage",
+               "delete_all_invalid_items_in_storage",
+               "update_begin",
+               "update_end",
+               "set_folder_item_validity",
+               "delete_all_invalid_items_in_folder",
+               "insert_burst_item",
+               "send_dir_update_noti",
+               "count_delete_items_in_folder",
+               "delete_item",
+               "get_folder_list",
+               "update_folder_time",
+               "get_storage_id",
+               "get_storage_scan_status",
+               "set_storage_scan_status",
+               "insert_folder",
+               "delete_invalid_folder",
+               "set_folder_validity",
+               "insert_folder_begin",
+               "insert_folder_end",
+               "check_db",
+               "get_uuid",
+               "get_mmc_info",
+               "check_storage",
+               "insert_storage",
+               "update_storage",
+               "delete_storage",
+               "set_storage_validity",
+               "set_all_storage_validity",
+               "update_item_meta",
+               "update_item_begin",
+               "update_item_end",
+               };
+       /*init array for adding name of so*/
+       so_array = g_array_new(FALSE, FALSE, sizeof(char*));
+
+       /*load information of so*/
+       _ms_load_config();
+
+       if(so_array->len == 0) {
+               MS_DBG_ERR("There is no information for functions");
+               return MS_MEDIA_ERR_DYNAMIC_LINK;
+       }
+
+       /*the number of functions*/
+       lib_num = so_array->len;
+
+       MS_DBG_SLOG("The number of information of so : %d", lib_num);
+       MS_MALLOC(func_handle, sizeof(void*) * lib_num);
+       if (func_handle == NULL) {
+               MS_DBG_ERR("malloc failed");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
+
+       while(lib_index < lib_num) {
+               /*get handle*/
+               MS_DBG_SLOG("[name of so : %s]", g_array_index(so_array, char*, lib_index));
+               func_handle[lib_index] = dlopen(g_array_index(so_array, char*, lib_index), RTLD_LAZY);
+               if (!func_handle[lib_index]) {
+                       MS_DBG_ERR("%s", dlerror());
+                       MS_SAFE_FREE(func_handle);
+                       return MS_MEDIA_ERR_DYNAMIC_LINK;
+               }
+               lib_index++;
+       }
+
+       dlerror();    /* Clear any existing error */
+
+       /*allocate for array of functions*/
+       MS_MALLOC(func_array, sizeof(void**) * lib_num);
+       if (func_array == NULL) {
+               MS_DBG_ERR("malloc failed");
+               MS_SAFE_FREE(func_handle);
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
+
+       for(lib_index = 0 ; lib_index < lib_num; lib_index ++) {
+               MS_MALLOC(func_array[lib_index], sizeof(void*) * eFUNC_MAX);
+               if (func_array[lib_index] == NULL) {
+                       int index;
+
+                       for (index = 0; index < lib_index; index ++) {
+                               MS_SAFE_FREE(func_array[index]);
+                       }
+                       MS_SAFE_FREE(func_array);
+                       MS_SAFE_FREE(func_handle);
+
+                       MS_DBG_ERR("malloc failed");
+                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
+               }
+       }
+
+       /*add functions to array */
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               for (func_index = 0; func_index < eFUNC_MAX ; func_index++) {
+                       func_array[lib_index][func_index] = dlsym(func_handle[lib_index], func_list[func_index]);
+                       if (func_array[lib_index][func_index] == NULL) {
+                               int index;
+
+                               for (index = 0; index < lib_index; index ++) {
+                                       MS_SAFE_FREE(func_array[index]);
+                               }
+                               MS_SAFE_FREE(func_array);
+                               MS_SAFE_FREE(func_handle);
+
+                               MS_DBG_ERR("dlsym failed[%s]", func_list[func_index]);
+                               return MS_MEDIA_ERR_DYNAMIC_LINK;
+                       }
+               }
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+void ms_unload_functions(void)
+{
+       int lib_index;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index ++)
+               dlclose(func_handle[lib_index]);
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+                       if (func_array[lib_index]) {
+                               MS_SAFE_FREE(func_array[lib_index]);
+                       }
+       }
+
+       MS_SAFE_FREE (func_array);
+       MS_SAFE_FREE (func_handle);
+
+       if (so_array) {
+               /*delete all node*/
+               while(so_array->len != 0) {
+                       char *so_name = NULL;
+                       so_name = g_array_index(so_array , char*, 0);
+                       g_array_remove_index (so_array, 0);
+                       MS_SAFE_FREE(so_name);
+               }
+               g_array_free(so_array, FALSE);
+               so_array = NULL;
+       }
+}
+
+int ms_get_insert_count()
+{
+       return insert_count;
+}
+
+void ms_reset_insert_count()
+{
+       insert_count = 0;
+}
+
+int ms_connect_db(void ***handle, uid_t uid)
+{
+       int lib_index;
+       int ret;
+       char * err_msg = NULL;
+
+       /*Lock mutex for openning db*/
+       g_mutex_lock(&db_mutex);
+
+       MS_MALLOC(*handle, sizeof (void*) * lib_num);
+       if (*handle == NULL) {
+               MS_DBG_ERR("malloc failed");
+               g_mutex_unlock(&db_mutex);
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((CONNECT)func_array[lib_index][eCONNECT])(&((*handle)[lib_index]), uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       g_mutex_unlock(&db_mutex);
+
+                       return MS_MEDIA_ERR_DB_CONNECT_FAIL;
+               }
+       }
+
+       MS_DBG_INFO("connect Media DB");
+
+       g_mutex_unlock(&db_mutex);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int ms_disconnect_db(void ***handle)
+{
+       int lib_index;
+       int ret;
+       char * err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((DISCONNECT)func_array[lib_index][eDISCONNECT])((*handle)[lib_index], &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       return MS_MEDIA_ERR_DB_DISCONNECT_FAIL;
+               }
+       }
+
+       MS_SAFE_FREE(*handle);
+
+       MS_DBG_INFO("Disconnect Media DB");
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int ms_validate_item(void **handle, const char *storage_id, const char *path, uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+       bool modified = FALSE;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               /*check exist in Media DB, If file is not exist, insert data in DB. */
+               ret = ((CHECK_ITEM_EXIST)func_array[lib_index][eEXIST])(handle[lib_index], storage_id, path, &modified, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_SAFE_FREE(err_msg);
+                       ret = ms_insert_item_batch(handle, storage_id, path, uid);
+                       if (ret != 0) {
+                               res = MS_MEDIA_ERR_DB_INSERT_FAIL;
+                       } else {
+                               insert_count++;
+                       }
+               } else {
+                       if (modified == FALSE) {
+                               /*if meta data of file exist, change valid field to "1" */
+                               ret = ((SET_ITEM_VALIDITY)func_array[lib_index][eSET_VALIDITY])(handle[lib_index], storage_id, path, true, true, uid, &err_msg); /*dlopen*/
+                               if (ret != 0) {
+                                       MS_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
+                                       MS_SAFE_FREE(err_msg);
+                                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+                               }
+                       } else {
+                               /* the file has same name but it is changed, so we have to update DB */
+                               ret = ((DELETE_ITEM)func_array[lib_index][eDELETE_ITEM])(handle[lib_index], storage_id, path, uid, &err_msg); /*dlopen*/
+                               if (ret != 0) {
+                                       MS_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
+                                       MS_SAFE_FREE(err_msg);
+                                       res = MS_MEDIA_ERR_DB_DELETE_FAIL;
+                               } else {
+                                       ret = ms_insert_item_batch(handle, storage_id, path, uid);
+                                       if (ret != 0) {
+                                               res = MS_MEDIA_ERR_DB_INSERT_FAIL;
+                                       } else {
+                                               insert_count++;
+                                       }
+                               }
+                       }
+               }
+       }
+
+       return res;
+}
+
+int ms_scan_validate_item(void **handle, const char *storage_id, const char *path, uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+       bool modified = FALSE;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               /*check exist in Media DB, If file is not exist, insert data in DB. */
+               ret = ((CHECK_ITEM_EXIST)func_array[lib_index][eEXIST])(handle[lib_index], storage_id, path, &modified, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_SAFE_FREE(err_msg);
+                       ret = ms_scan_item_batch(handle, storage_id, path, uid);
+                       if (ret != 0) {
+                               res = MS_MEDIA_ERR_DB_INSERT_FAIL;
+                       } else {
+                               insert_count++;
+                       }
+               } else {
+                       if (modified == FALSE) {
+                               /*if meta data of file exist, change valid field to "1" */
+                               ret = ((SET_ITEM_VALIDITY)func_array[lib_index][eSET_VALIDITY])(handle[lib_index], storage_id, path, true, true, uid, &err_msg); /*dlopen*/
+                               if (ret != 0) {
+                                       MS_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
+                                       MS_SAFE_FREE(err_msg);
+                                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+                               }
+                       } else {
+                               /* the file has same name but it is changed, so we have to update DB */
+                               ret = ((DELETE_ITEM)func_array[lib_index][eDELETE_ITEM])(handle[lib_index], storage_id, path, uid, &err_msg); /*dlopen*/
+                               if (ret != 0) {
+                                       MS_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
+                                       MS_SAFE_FREE(err_msg);
+                                       res = MS_MEDIA_ERR_DB_DELETE_FAIL;
+                               } else {
+                                       ret = ms_scan_item_batch(handle, storage_id, path, uid);
+                                       if (ret != 0) {
+                                               res = MS_MEDIA_ERR_DB_INSERT_FAIL;
+                                       } else {
+                                               insert_count++;
+                                       }
+                               }
+                       }
+               }
+       }
+
+       return res;
+}
+
+int ms_validaty_change_all_items(void **handle, const char *storage_id, ms_storage_type_t store_type, bool validity , uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((SET_ALL_STORAGE_ITEMS_VALIDITY)func_array[lib_index][eSET_ALL_VALIDITY])(handle[lib_index], storage_id, store_type, validity, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               }
+       }
+
+       return res;
+}
+
+int ms_insert_item_batch(void **handle, const char* storage_id, const char *path, uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+       ms_storage_type_t storage_type;
+
+       MS_DBG_FENTER();
+
+       storage_type = ms_get_storage_type_by_full(path, uid);
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((INSERT_ITEM)func_array[lib_index][eINSERT_BATCH])(handle[lib_index], storage_id, path, storage_type, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_INSERT_FAIL;
+               }
+       }
+
+       return res;
+}
+
+int ms_scan_item_batch(void **handle, const char* storage_id, const char *path, uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+       ms_storage_type_t storage_type;
+
+       MS_DBG_FENTER();
+
+       storage_type = ms_get_storage_type_by_full(path, uid);
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((INSERT_ITEM_SCAN)func_array[lib_index][eINSERT_SCAN])(handle[lib_index], storage_id, path, storage_type, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_INSERT_FAIL;
+               }
+       }
+
+       return res;
+}
+
+int ms_insert_item_pass2(void **handle, const char* storage_id, const char *path, int scan_type, uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+       ms_storage_type_t storage_type;
+
+       storage_type = ms_get_storage_type_by_full(path, uid);
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((UPDATE_ITEM_EXTRACT)func_array[lib_index][eUPDATE_EXTRACT])(handle[lib_index], storage_id, storage_type, scan_type, path, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_INSERT_FAIL;
+               }
+       }
+
+       return res;
+}
+
+int ms_insert_item_immediately(void **handle, const char *storage_id, const char *path, uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+       ms_storage_type_t storage_type;
+
+       storage_type = ms_get_storage_type_by_full(path, uid);
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((INSERT_ITEM_IMMEDIATELY)func_array[lib_index][eINSERT_ITEM_IMMEDIATELY])(handle[lib_index], storage_id, path, storage_type, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_INSERT_FAIL;
+               }
+       }
+
+       return res;
+}
+
+int ms_insert_burst_item(void **handle, const char *storage_id, const char *path , uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+       ms_storage_type_t storage_type;
+
+       storage_type = ms_get_storage_type_by_full(path, uid);
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((INSERT_BURST_ITEM)func_array[lib_index][eINSERT_BURST])(handle[lib_index], storage_id, path, storage_type, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_INSERT_FAIL;
+               }
+       }
+
+       return res;
+}
+
+bool ms_delete_all_items(void **handle, const char *storage_id, ms_storage_type_t store_type, uid_t uid)
+{
+       int lib_index;
+       int ret = 0;
+       char *err_msg = NULL;
+
+       /* To reset media db when differnet mmc is inserted. */
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((DELETE_ALL_ITEMS_IN_STORAGE)func_array[lib_index][eDELETE_ALL])(handle[lib_index], storage_id, store_type, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+bool ms_delete_invalid_items(void **handle, const char *storage_id, ms_storage_type_t store_type, uid_t uid)
+{
+       int lib_index;
+       int ret;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((DELETE_ALL_INVALID_ITMES_IN_STORAGE)func_array[lib_index][eDELETE_INVALID_ITEMS])(handle[lib_index], storage_id, store_type, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+int ms_set_folder_item_validity(void **handle, const char *storage_id, const char *path, int validity, int recursive, uid_t uid)
+{
+       int lib_index;
+       int ret;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((SET_FOLDER_ITEM_VALIDITY)func_array[lib_index][eSET_FOLDER_ITEM_VALIDITY])(handle[lib_index], storage_id, path, validity, recursive, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       return MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               }
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int ms_delete_invalid_items_in_folder(void **handle, const char* storage_id, const char*path, bool is_recursive, uid_t uid)
+{
+       int lib_index;
+       int ret;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((DELETE_ALL_INVALID_ITEMS_IN_FOLDER)func_array[lib_index][eDELETE_FOLDER])(handle[lib_index], storage_id, path, is_recursive, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       return MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               }
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int ms_send_dir_update_noti(void **handle, const char* storage_id, const char*path)
+{
+       int lib_index;
+       int ret;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((SEND_DIR_UPDATE_NOTI)func_array[lib_index][eSEND_DIR_UPDATE_NOTI])(handle[lib_index], storage_id, path, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       return MS_MEDIA_ERR_SEND_NOTI_FAIL;
+               }
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int ms_count_delete_items_in_folder(void **handle, const char *storage_id, const char*path, int *count)
+{
+       int lib_index;
+       int ret;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((COUNT_DELETE_ITEMS_IN_FOLDER)func_array[lib_index][eCOUNT_DELETE_ITEMS_IN_FOLDER])(handle[lib_index], storage_id, path, count, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int ms_get_folder_list(void **handle, const char* storage_id, char* start_path, GArray **dir_array)
+{
+       int lib_index;
+       int ret;
+       char *err_msg = NULL;
+
+       char **folder_list = NULL;
+       int *modified_time_list = NULL;
+       int count = 0;
+       int i = 0;
+
+       ms_dir_info_s* dir_info = NULL;
+       MS_DBG("start path: %s", start_path);
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((GET_FOLDER_LIST)func_array[lib_index][eGET_FOLDER_LIST])(handle[lib_index], storage_id, start_path, &folder_list, &modified_time_list, &count, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
+       }
+
+       *dir_array = g_array_new(FALSE, FALSE, sizeof(ms_dir_info_s*));
+       if (count != 0) {
+               for(i = 0; i < count; i ++) {
+                       dir_info = malloc(sizeof(ms_dir_info_s));
+                       if (dir_info != NULL) {
+                               dir_info->dir_path = strdup(folder_list[i]);
+                               dir_info->modified_time = modified_time_list[i];
+                               g_array_append_val(*dir_array, dir_info);
+                       } else {
+                               MS_DBG_ERR("malloc failed");
+                               ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
+                               goto ERROR;
+                       }
+               }
+       }
+
+       for (i = 0; i < count; i++) {
+               MS_SAFE_FREE(folder_list[i]);
+       }
+
+       MS_SAFE_FREE(folder_list);
+       MS_SAFE_FREE(modified_time_list);
+
+       return MS_MEDIA_ERR_NONE;
+ERROR:
+
+       if (*dir_array) {
+               while((*dir_array)->len != 0) {
+                       ms_dir_info_s *data = NULL;
+                       data = g_array_index(*dir_array , ms_dir_info_s*, 0);
+                       g_array_remove_index (*dir_array, 0);
+                       MS_SAFE_FREE(data->dir_path);
+               }
+               g_array_free(*dir_array, FALSE);
+               *dir_array = NULL;
+       }
+
+       for (i = 0; i < count; i++) {
+               MS_SAFE_FREE(folder_list[i]);
+       }
+
+       MS_SAFE_FREE(folder_list);
+       MS_SAFE_FREE(modified_time_list);
+
+       return ret;
+}
+
+int ms_update_folder_time(void **handle, const char *storage_id, char *folder_path, uid_t uid)
+{
+       int lib_index;
+       int ret;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((UPDATE_FOLDER_TIME)func_array[lib_index][eUPDATE_FOLDER_TIME])(handle[lib_index], storage_id, folder_path, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int ms_get_storage_id(void **handle, const char *path, char *storage_id)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+
+       MS_DBG_FENTER();
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((GET_STORAGE_ID)func_array[lib_index][eGET_STORAGE_ID])(handle[lib_index], path, storage_id, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               }
+       }
+
+       MS_DBG("storage_id [%s]", storage_id);
+
+       return res;
+}
+
+int ms_get_storage_scan_status(void **handle, char *storage_id, media_scan_status_e *scan_status)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+       int status = 0;
+
+       MS_DBG_FENTER();
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((GET_STORAGE_SCAN_STATUS)func_array[lib_index][eGET_STORAGE_SCAN_STATUS])(handle[lib_index], storage_id, &status, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_INTERNAL;
+               } else {
+                       *scan_status = status;
+               }
+       }
+
+       MS_DBG("storage_id [%s], scan_status [%d]", storage_id, *scan_status);
+
+       return res;
+}
+
+int ms_set_storage_scan_status(void **handle, char *storage_id, media_scan_status_e scan_status, uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+       int status = scan_status;
+
+       MS_DBG_FENTER();
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((SET_STORAGE_SCAN_STATUS)func_array[lib_index][eSET_STORAGE_SCAN_STATUS])(handle[lib_index], storage_id, status, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               }
+       }
+
+       MS_DBG("storage_id [%s], scan_status [%d]", storage_id, scan_status);
+
+       return res;
+}
+
+int ms_insert_folder(void **handle, const char *storage_id, const char *path, uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+       ms_storage_type_t storage_type;
+       storage_type = ms_get_storage_type_by_full(path, uid);
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((INSERT_FOLDER)func_array[lib_index][eINSERT_FOLDER])(handle[lib_index], storage_id, path, storage_type, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_INSERT_FAIL;
+               }
+       }
+
+       return res;
+}
+
+int ms_delete_invalid_folder(void **handle, const char *storage_id, uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((DELETE_INVALID_FOLDER)func_array[lib_index][eDELETE_INVALID_FOLDER])(handle[lib_index], storage_id, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, storage_id);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_INSERT_FAIL;
+               }
+       }
+
+       return res;
+}
+
+int ms_set_folder_validity(void **handle, const char *storage_id, const char *path, int validity, bool is_recursive, uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((SET_FOLDER_VALIDITY)func_array[lib_index][eSET_FOLDER_VALIDITY])(handle[lib_index], storage_id, path, validity, is_recursive, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, storage_id);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_INSERT_FAIL;
+               }
+       }
+
+       return res;
+}
+
+int ms_check_db_upgrade(void **handle, bool *need_full_scan, uid_t uid)
+{
+       int lib_index = 0;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       bool full_scan = false;
+       char *err_msg = NULL;
+
+       MS_DBG_FENTER();
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((CHECK_DB)func_array[lib_index][eCHECK_DB])(handle[lib_index], &full_scan, uid, &err_msg);
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               }
+       }
+       *need_full_scan = full_scan;
+       MS_DBG_FLEAVE();
+
+       return res;
+}
+
+ int ms_genarate_uuid(void **handle, char **uuid)
+{
+       int lib_index = 0;
+       int ret;
+       char * err_msg = NULL;
+
+       ret = ((GET_UUID)func_array[lib_index][eGET_UUID])(handle[lib_index], uuid, &err_msg); /*dlopen*/
+       if (ret != 0) {
+               MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+               MS_SAFE_FREE(err_msg);
+               return MS_MEDIA_ERR_DB_DISCONNECT_FAIL;
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int ms_get_mmc_info(void **handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
+{
+       int lib_index = 0;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret = 0;
+       char *err_msg = NULL;
+
+       MS_DBG("ms_get_mmc_info Start");
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((GET_MMC_INFO)func_array[lib_index][eGET_MMC_INFO])(handle[lib_index], storage_name, storage_path, validity, info_exist, &err_msg);
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               }
+       }
+
+       MS_DBG("ms_get_mmc_info End");
+
+       return res;
+}
+
+int ms_check_storage(void **handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity)
+{
+       int lib_index = 0;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret = 0;
+       char *err_msg = NULL;
+
+       MS_DBG("ms_check_storage Start");
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((CHECK_STORAGE)func_array[lib_index][eCHECK_STORAGE])(handle[lib_index], storage_id, storage_name, storage_path, validity, &err_msg);
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_INTERNAL;
+               }
+       }
+
+       MS_DBG("ms_check_storage End");
+
+       return res;
+}
+
+int ms_insert_storage(void **handle, const char *storage_id, const char *storage_name, const char *storage_path, uid_t uid)
+{
+       int lib_index = 0;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret = 0;
+       char *err_msg = NULL;
+       ms_storage_type_t storage_type;
+
+       MS_DBG("ms_insert_storage Start");
+
+       storage_type = ms_get_storage_type_by_full(storage_path, uid);
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((INSERT_STORAGE)func_array[lib_index][eINSERT_STORAGE])(handle[lib_index], storage_id, storage_type, storage_name, storage_path, uid, &err_msg);
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               }
+       }
+
+       MS_DBG("ms_insert_storage End");
+
+       return res;
+}
+
+int ms_update_storage(void **handle, const char *storage_id, const char *storage_path, uid_t uid)
+{
+       int lib_index = 0;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret = 0;
+       char *err_msg = NULL;
+
+       MS_DBG("ms_update_storage Start");
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((UPDATE_STORAGE)func_array[lib_index][eUPDATE_STORAGE])(handle[lib_index], storage_id, storage_path, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               }
+       }
+
+       MS_DBG("ms_update_storage End");
+
+       return res;
+}
+
+int ms_delete_storage(void **handle, const char *storage_id, const char *storage_name, uid_t uid)
+{
+       int lib_index = 0;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret = 0;
+       char *err_msg = NULL;
+
+       MS_DBG("ms_delete_storage Start");
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((DELETE_STORAGE)func_array[lib_index][eDELETE_STORAGE])(handle[lib_index], storage_id, storage_name, uid, &err_msg);
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               }
+       }
+
+       MS_DBG("ms_delete_storage End");
+
+       return res;
+}
+
+int ms_set_storage_validity(void **handle, const char *storage_id, int validity, uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((SET_STORAGE_VALIDITY)func_array[lib_index][eSET_STORAGE_VALIDITY])(handle[lib_index], storage_id, validity, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               }
+       }
+
+       return res;
+}
+
+int ms_set_all_storage_validity(void **handle, int validity, uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((SET_ALL_STORAGE_VALIDITY)func_array[lib_index][eSET_ALL_STORAGE_VALIDITY])(handle[lib_index], validity, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               }
+       }
+
+       return res;
+}
+
+int ms_update_meta_batch(void **handle, const char *path, uid_t uid)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+       ms_storage_type_t storage_type;
+
+       storage_type = ms_get_storage_type_by_full(path, uid);
+
+       MS_DBG_FENTER();
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((UPDATE_ITEM_META)func_array[lib_index][eUPDATE_ITEM_META])(handle[lib_index], path, storage_type, uid, &err_msg); /*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
+                       MS_SAFE_FREE(err_msg);
+                       res = MS_MEDIA_ERR_DB_INSERT_FAIL;
+               }
+       }
+
+       return res;
+}
+
+
+/****************************************************************************************************
+FOR BULK COMMIT
+*****************************************************************************************************/
+
+void ms_register_start(void **handle, ms_noti_switch_e noti_status, int pid)
+{
+       int lib_index;
+       int ret = 0;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((INSERT_ITEM_BEGIN)func_array[lib_index][eINSERT_BEGIN])(handle[lib_index], MSC_REGISTER_COUNT, noti_status, pid, &err_msg);/*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+               }
+       }
+}
+
+void ms_register_end(void **handle, const char *start_path, uid_t uid)
+{
+       int lib_index;
+       int ret = 0;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((INSERT_ITEM_END)func_array[lib_index][eINSERT_END])(handle[lib_index], uid, &err_msg);/*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+               }
+       }
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((UPDATE_END)func_array[lib_index][eUPDATE_END])(start_path, uid);/*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+               }
+       }
+}
+
+void ms_validate_start(void **handle)
+{
+       int lib_index;
+       int ret = 0;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((SET_ITEM_VALIDITY_BEGIN)func_array[lib_index][eSET_VALIDITY_BEGIN])(handle[lib_index], MSC_VALID_COUNT, &err_msg);/*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+               }
+       }
+}
+
+void ms_validate_end(void **handle, uid_t uid)
+{
+       int lib_index;
+       int ret = 0;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((SET_ITEM_VALIDITY_END)func_array[lib_index][eSET_VALIDITY_END])(handle[lib_index], uid, &err_msg);/*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+               }
+       }
+}
+
+void ms_insert_folder_start(void **handle)
+{
+       int lib_index;
+       int ret = 0;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((INSERT_FOLDER_BEGIN)func_array[lib_index][eINSERT_FOLDER_BEGIN])(handle[lib_index], MSC_REGISTER_COUNT, &err_msg);/*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+               }
+       }
+}
+
+void ms_insert_folder_end(void **handle, uid_t uid)
+{
+       int lib_index;
+       int ret = 0;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((INSERT_FOLDER_END)func_array[lib_index][eINSERT_FOLDER_END])(handle[lib_index], uid, &err_msg);/*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+               }
+       }
+}
+
+void ms_update_start(void **handle)
+{
+       int lib_index;
+       int ret = 0;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((UPDATE_ITEM_BEGIN)func_array[lib_index][eUPDATE_ITEM_BEGIN])(handle[lib_index], MSC_VALID_COUNT, &err_msg);/*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+               }
+       }
+}
+
+void ms_update_end(void **handle, uid_t uid)
+{
+       int lib_index;
+       int ret = 0;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((UPDATE_ITEM_END)func_array[lib_index][eUPDATE_ITEM_END])(handle[lib_index], uid, &err_msg);/*dlopen*/
+               if (ret != 0) {
+                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
+                       MS_SAFE_FREE(err_msg);
+               }
+       }
+}
\ No newline at end of file
index 6d4cb15..2ab64d9 100755 (executable)
  * limitations under the License.
  *
  */
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/syscall.h>
+
 #include <locale.h>
 #include <libintl.h>
 #include <sys/stat.h>
 #include <malloc.h>
 #include <vconf.h>
 #include <errno.h>
+#include <glib.h>
+#include <stdlib.h>
+#include <gio/gio.h>
+
 #include <notification.h>
 
 #include "media-util.h"
 #include "media-server-ipc.h"
 #include "media-common-dbg.h"
 #include "media-common-utils.h"
+#include "media-common-db-svc.h"
 #include "media-common-external-storage.h"
 
 #include <pwd.h>
 #include <tzplatform_config.h>
 
 #define MMC_INFO_SIZE 256
-#define PATH_IMAGES tzplatform_mkpath(TZ_SYS_STORAGE,"sdcard/Images")
-#define PATH_VIDEOS tzplatform_mkpath(TZ_SYS_STORAGE,"sdcard/Videos")
-#define PATH_SOUNDS tzplatform_mkpath(TZ_SYS_STORAGE,"sdcard/Sounds")
-#define PATH_DOWNLOADS tzplatform_mkpath(TZ_SYS_STORAGE,"sdcard/Downloads")
-#define PATH_CAMERA tzplatform_mkpath(TZ_SYS_STORAGE,"sdcard/Camera")
-
-char default_path[][MS_FILE_NAME_LEN_MAX + 1] = {
-               {"/opt/storage/sdcard/Images"},
-               {"/opt/storage/sdcard/Videos"},
-               {"/opt/storage/sdcard/Sounds"},
-               {"/opt/storage/sdcard/Downloads"},
-               {"/opt/storage/sdcard/Camera"}
-};
-
-#define DIR_NUM       ((int)(sizeof(default_path)/sizeof(default_path[0])))
 
-void ms_init_default_path(void){
+#define DEVICED_BUS_NAME       "org.tizen.system.deviced"
+#define DEVICED_OBJECT_PATH    "/Org/Tizen/System/DeviceD"
+#define DEVICED_INTERFACE_NAME DEVICED_BUS_NAME
 
-       strcpy (default_path[0], PATH_IMAGES);
-       strcpy (default_path[1], PATH_VIDEOS);
-       strcpy (default_path[2], PATH_SOUNDS);
-       strcpy (default_path[3], PATH_DOWNLOADS);
-       strcpy (default_path[4], PATH_CAMERA);
-}
+#define DEVICED_PATH_BLOCK                  DEVICED_OBJECT_PATH"/Block"
+#define DEVICED_PATH_BLOCK_DEVICES          DEVICED_PATH_BLOCK"/Devices"
+#define DEVICED_PATH_BLOCK_MANAGER          DEVICED_PATH_BLOCK"/Manager"
+#define DEVICED_INTERFACE_BLOCK             DEVICED_INTERFACE_NAME".Block"
+#define DEVICED_INTERFACE_BLOCK_MANAGER     DEVICED_INTERFACE_NAME".BlockManager"
 
-void ms_make_default_path_mmc(void)
-{
-       int i = 0;
-       int ret = 0;
-       DIR *dp = NULL;
-       ms_init_default_path();
-       for (i = 0; i < DIR_NUM; ++i) {
-               dp = opendir(default_path[i]);
-               if (dp == NULL) {
-                       ret = mkdir(default_path[i], 0777);
-                       if (ret < 0) {
-                               MS_DBG_ERR("make fail");
-                       }
-                       /*this fuction for emulator*/
-                       /*at the first time, the directroies are made permission 755*/
-                       ret = chmod(default_path[i], 0777);
-                       if (ret != 0) {
-                               MS_DBG_STRERROR("chmod failed");
-                       }
-                       ret = chown(default_path[i], tzplatform_getuid(TZ_USER_NAME), tzplatform_getgid(TZ_USER_NAME));
-                       if (ret != 0) {
-                               MS_DBG_STRERROR("chown failed");
-                       }
-               } else {
-                       closedir(dp);
-               }
-       }
-}
-
-int _ms_update_mmc_info(const char *cid)
-{
-       bool res;
-
-       if (cid == NULL) {
-               MS_DBG_ERR("Parameters are invalid");
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
-
-       res = ms_config_set_str(MS_MMC_INFO_KEY, cid);
-       if (!res) {
-               MS_DBG_ERR("fail to get MS_MMC_INFO_KEY");
-               return MS_MEDIA_ERR_VCONF_SET_FAIL;
-       }
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-bool _ms_check_mmc_info(const char *cid)
-{
-       char pre_mmc_info[MMC_INFO_SIZE] = { 0 };
-       bool res = false;
-
-       if (cid == NULL) {
-               MS_DBG_ERR("Parameters are invalid");
-               return false;
-       }
-
-       res = ms_config_get_str(MS_MMC_INFO_KEY, pre_mmc_info);
-       if (!res) {
-               MS_DBG_ERR("fail to get MS_MMC_INFO_KEY");
-               return false;
-       }
+#define BLOCK_OBJECT_ADDED      "ObjectAdded"
+#define BLOCK_OBJECT_REMOVED    "ObjectRemoved"
+#define BLOCK_DEVICE_CHANGED    "DeviceChanged"
 
-       MS_DBG("Last MMC info   = %s", pre_mmc_info);
-       MS_DBG("Current MMC info = %s", cid);
-
-       if (strcmp(pre_mmc_info, cid) == 0) {
-               return true;
-       }
-
-       return false;
-}
+GDBusConnection *g_stg_bus;
+int g_stg_added_handler;
 
 static int __get_contents(const char *filename, char *buf)
 {
@@ -153,113 +85,22 @@ static int __get_contents(const char *filename, char *buf)
 }
 
 /*need optimize*/
-int _ms_get_mmc_info(char *cid)
+int ms_get_mmc_id(char **cid)
 {
-       int i;
-       int j;
-       int len;
-       int err = -1;
-       bool getdata = false;
-       bool bHasColon = false;
-       char path[MS_FILE_PATH_LEN_MAX] = { 0 };
-       char mmcpath[MS_FILE_PATH_LEN_MAX] = { 0 };
-
-       DIR *dp;
-       struct dirent ent;
-       struct dirent *res = NULL;
-
-       /* mmcblk0 and mmcblk1 is reserved for movinand */
-       for (j = 1; j < 3; j++) {
-               len = snprintf(mmcpath, MS_FILE_PATH_LEN_MAX, "/sys/class/mmc_host/mmc%d/", j);
-               if (len < 0) {
-                       MS_DBG_ERR("FAIL : snprintf");
-                       return MS_MEDIA_ERR_INTERNAL;
-               }
-               else {
-                       mmcpath[len] = '\0';
-               }
-
-               dp = opendir(mmcpath);
-               if (dp == NULL) {
-                       MS_DBG_ERR("dp is NULL");
-                       return MS_MEDIA_ERR_DIR_OPEN_FAIL;
-               }
-
-               while (!readdir_r(dp, &ent, &res)) {
-                        /*end of read dir*/
-                       if (res == NULL)
-                               break;
-
-                       bHasColon = false;
-                       if (ent.d_name[0] == '.')
-                               continue;
-
-                       if (ent.d_type == DT_DIR) {
-                               /*ent->d_name is including ':' */
-                               for (i = 0; i < strlen(ent.d_name); i++) {
-                                       if (ent.d_name[i] == ':') {
-                                               bHasColon = true;
-                                               break;
-                                       }
-                               }
-
-                               if (bHasColon) {
-                                       /*check serial */
-                                       err = ms_strappend(path, sizeof(path), "%s%s/cid", mmcpath, ent.d_name);
-                                       if (err < 0) {
-                                               MS_DBG_ERR("ms_strappend error : %d", err);
-                                               continue;
-                                       }
-
-                                       if (__get_contents(path, cid) != MS_MEDIA_ERR_NONE)
-                                               break;
-                                       else
-                                               getdata = true;
-                               }
-                       }
-               }
-               closedir(dp);
-
-               if (getdata == true) {
-                       break;
-               }
+       const char *path = "/sys/block/mmcblk1/device/cid";
+       char mmc_cid[MMC_INFO_SIZE] = {0, };
+       memset(mmc_cid, 0x00, sizeof(mmc_cid));
+
+       if (__get_contents(path, mmc_cid) != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("_get_contents failed");
+               *cid = NULL;
+       } else {
+               *cid = strndup(mmc_cid, strlen(mmc_cid) - 1);           //last is carriage return
        }
 
        return MS_MEDIA_ERR_NONE;
 }
 
-ms_dir_scan_type_t ms_get_mmc_state(void)
-{
-       char cid[MMC_INFO_SIZE] = { 0 };
-       ms_dir_scan_type_t ret = MS_SCAN_ALL;
-
-       /*get new info */
-       _ms_get_mmc_info(cid);
-
-       /*check it's same mmc */
-       if (_ms_check_mmc_info(cid)) {
-               ret = MS_SCAN_PART;
-       }
-
-       return ret;
-}
-
-int ms_update_mmc_info(void)
-{
-       int err;
-       char cid[MMC_INFO_SIZE] = { 0 };
-
-       err = _ms_get_mmc_info(cid);
-
-       err = _ms_update_mmc_info(cid);
-
-       /*Active flush */
-       if (!malloc_trim(0))
-               MS_DBG_ERR("malloc_trim is failed");
-
-       return err;
-}
-
 #define _GETSYSTEMSTR(ID)      dgettext("sys_string", (ID))
 
 void update_lang(void)
@@ -296,3 +137,138 @@ int ms_present_mmc_status(ms_sdcard_status_type_t status)
        return MS_MEDIA_ERR_NONE;
 }
 
+#define DEVICE_INFO_FILE ".device_info_"
+
+struct linux_dirent {
+       int           d_ino;
+       long           d_off;
+       unsigned short d_reclen;
+       char           d_name[];
+};
+
+#define BUF_SIZE 1024
+
+int __ms_check_mount_path(const char *mount_path)
+{
+       DIR *dp = NULL;
+
+       /*check mount path*/
+       dp = opendir(mount_path);
+       if (dp == NULL) {
+               MS_DBG_ERR("Mount path is not exist");
+               return MS_MEDIA_ERR_INVALID_PATH;
+       }
+
+       closedir(dp);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int ms_read_device_info(const char *root_path, char **device_uuid)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       int len = 0;
+
+       int fd = -1;
+       int nread = 0;
+       char buf[BUF_SIZE] = {0,};
+       struct linux_dirent *d;
+       int bpos = 0;
+       char d_type;
+       bool find_uuid = FALSE;
+
+       ret = __ms_check_mount_path(root_path);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_strappend falied");
+               goto ERROR;
+       }
+
+       fd = open(root_path, O_RDONLY | O_DIRECTORY);
+       if (fd == -1) {
+               MS_DBG_ERR("open failed [%s]", root_path);
+               MS_DBG_STRERROR();
+               ret = MS_MEDIA_ERR_INVALID_PATH;
+               goto ERROR;
+       }
+
+       for ( ; ; ) {
+               nread = syscall(SYS_getdents64, fd, buf, BUF_SIZE);
+               if (nread == -1) {
+                       MS_DBG_ERR("getdents");
+                       MS_DBG_STRERROR();
+                       ret = MS_MEDIA_ERR_INTERNAL;
+                       goto ERROR;
+               }
+
+               if (nread == 0)
+                       break;
+
+               for (bpos = 0; bpos < nread;) {
+                       d = (struct linux_dirent *) (buf + bpos);
+                       d_type = *(buf + bpos + d->d_reclen - 1);
+
+                       if (d_type == DT_REG && (strncmp(d->d_name, DEVICE_INFO_FILE, strlen(DEVICE_INFO_FILE)) == 0)) {
+                               MS_DBG_ERR("FIND DEV INFO : %s", d->d_name);
+                               bpos += d->d_reclen;
+                               find_uuid = TRUE;
+                               goto FIND_UUID;
+                       }
+
+                       bpos += d->d_reclen;
+               }
+       }
+
+FIND_UUID:
+       if (find_uuid) {
+               len = strlen(d->d_name + strlen(DEVICE_INFO_FILE));
+               if (len > 0) {
+                       *device_uuid = strndup(d->d_name + strlen(DEVICE_INFO_FILE), MS_UUID_SIZE -1);
+                       MS_DBG_ERR("[%s][DEV ID: %s]", root_path, *device_uuid);
+               }
+       } else {
+               ret = MS_MEDIA_ERR_FILE_NOT_EXIST;
+               MS_DBG_ERR("[%s]DEV INFO NOT EXIST", root_path);
+               goto ERROR;
+       }
+
+       if (fd > -1) {
+               close(fd);
+               fd = -1;
+       }
+
+       return MS_MEDIA_ERR_NONE;
+
+ERROR:
+
+       *device_uuid = NULL;
+       if (fd > -1) {
+               close(fd);
+               fd = -1;
+       }
+       return ret;
+}
+
+int ms_write_device_info(const char *root_path, char *device_uuid)
+{
+       FILE * fp = NULL;
+       int len = 0;
+       char path[MS_FILE_PATH_LEN_MAX] = {0,};
+
+       len = snprintf(path, MS_FILE_PATH_LEN_MAX -1 , "%s/%s%s", root_path, DEVICE_INFO_FILE,device_uuid);
+       if (len < 0) {
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       path[len] = '\0';
+
+       fp = fopen(path, "wt");
+       if (fp == NULL) {
+               MS_DBG_ERR("fp is NULL. file name : %s", path);
+               return MS_MEDIA_ERR_FILE_OPEN_FAIL;
+       }
+
+       fclose(fp);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
diff --git a/src/common/media-common-system.c b/src/common/media-common-system.c
new file mode 100755 (executable)
index 0000000..4b1a7b7
--- /dev/null
@@ -0,0 +1,560 @@
+/*
+ *  Media Server
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <errno.h>
+#include <glib.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <gio/gio.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include <sys/stat.h>
+
+#include "media-util.h"
+
+#include "media-common-dbg.h"
+#include "media-common-types.h"
+#include "media-common-system.h"
+
+
+//////////////////////////////////////////////////////////////////////////////
+/// CHECK THE STORATE(MMC, USB)  STATE
+//////////////////////////////////////////////////////////////////////////////
+
+#define DEVICED_BUS_NAME       "org.tizen.system.deviced"
+#define DEVICED_OBJECT_PATH    "/Org/Tizen/System/DeviceD"
+#define DEVICED_INTERFACE_NAME DEVICED_BUS_NAME
+
+#define DEVICED_PATH_BLOCK                  DEVICED_OBJECT_PATH"/Block"
+#define DEVICED_PATH_BLOCK_DEVICES          DEVICED_PATH_BLOCK"/Devices"
+#define DEVICED_PATH_BLOCK_MANAGER          DEVICED_PATH_BLOCK"/Manager"
+#define DEVICED_INTERFACE_BLOCK             DEVICED_INTERFACE_NAME".Block"
+#define DEVICED_INTERFACE_BLOCK_MANAGER     DEVICED_INTERFACE_NAME".BlockManager"
+
+#define BLOCK_OBJECT_ADDED      "ObjectAdded"
+#define BLOCK_OBJECT_REMOVED    "ObjectRemoved"
+#define BLOCK_DEVICE_CHANGED    "DeviceChanged"
+
+#define BLOCK_DEVICE_METHOD "GetDeviceList"
+#define BLOCK_DEVICE_USB "scsi"
+#define BLOCK_DEVICE_MMC "mmc"
+#define BLOCK_DEVICE_ALL "all"
+
+static GDBusConnection *g_usb_bus;
+
+static int g_usb_handler;
+
+typedef struct block_cb_data{
+       block_changed_cb usr_cb;
+       void *usr_data;
+} block_cb_data;
+
+block_cb_data *g_usb_cb_data = NULL;
+
+static void __ms_block_changed(GDBusConnection* connection,
+                                       const gchar* sender_name,
+                                       const gchar* object_path,
+                                       const gchar* interface_name,
+                                       const gchar* signal_name,
+                                       GVariant* parameters,
+                                       gpointer user_data)
+{
+       const char *devnode = NULL;
+       const char *mount_point = NULL;
+       GVariant *tmp;
+       int block_type;
+       int state;
+       gsize size = 0;
+       block_cb_data *cb_data = (block_cb_data *)user_data;
+       void *usr_cb = cb_data->usr_cb;
+       void *usr_data = cb_data->usr_data;
+
+       tmp = g_variant_get_child_value(parameters, 0);
+       block_type = g_variant_get_int32(tmp);
+       MS_DBG_ERR("block_type : %d", block_type);
+
+       tmp = g_variant_get_child_value(parameters, 1);
+       devnode = g_variant_get_string (tmp, &size);
+       MS_DBG_ERR("devnode : %s", devnode);
+
+       tmp = g_variant_get_child_value(parameters, 8);
+       mount_point = g_variant_get_string (tmp, &size);
+       MS_DBG_ERR("mount_point : %s", mount_point);
+
+       tmp = g_variant_get_child_value(parameters, 9);
+       state = g_variant_get_int32 (tmp);
+       MS_DBG_ERR("state : %d", state);
+
+       ((block_changed_cb)usr_cb)(mount_point, block_type, state, usr_data);
+       MS_DBG_ERR("user callback done");
+}
+
+static int __ms_sys_subscribe_device_block_event(block_changed_cb usr_callback, void *usr_data)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       GError *error = NULL;
+
+       MS_DBG_FENTER();
+
+       g_usb_cb_data = malloc(sizeof(block_cb_data));
+       if (g_usb_cb_data == NULL) {
+               MS_DBG_ERR("malloc failed");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
+
+       g_usb_cb_data->usr_cb = usr_callback;
+       g_usb_cb_data->usr_data = usr_data;
+
+       if (g_usb_bus == NULL) {
+               g_usb_bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+               if (!g_usb_bus) {
+                       MS_DBG_ERR ("Failed to connect to the g D-BUS daemon: %s", error->message);
+                       g_error_free (error);
+                       ret = MS_MEDIA_ERR_INTERNAL;
+                       goto ERROR;
+               }
+       }
+
+       /* listening to messages from all objects as no path is specified */
+       g_usb_handler = g_dbus_connection_signal_subscribe(
+                                       g_usb_bus,
+                                       NULL,
+                                       DEVICED_INTERFACE_BLOCK,
+                                       BLOCK_DEVICE_CHANGED,
+                                       NULL,
+                                       NULL,
+                                       G_DBUS_SIGNAL_FLAGS_NONE,
+                                       __ms_block_changed,
+                                       g_usb_cb_data,
+                                       NULL);
+
+       MS_DBG_FLEAVE();
+
+       return MS_MEDIA_ERR_NONE;
+
+ERROR:
+
+       if (g_usb_bus != NULL) {
+               g_object_unref(g_usb_bus);
+               g_usb_bus = NULL;
+       }
+
+       MS_SAFE_FREE(g_usb_cb_data);
+
+       MS_DBG_FLEAVE();
+
+       return ret;
+}
+
+int ms_sys_set_device_block_event_cb(block_changed_cb usr_callback, void *usr_data)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+
+       MS_DBG_FENTER();
+
+       ret = __ms_sys_subscribe_device_block_event(usr_callback, usr_data);
+
+       return ret;
+}
+
+int ms_sys_unset_device_block_event_cb(void)
+{
+       if (g_usb_bus == NULL) {
+               return MS_MEDIA_ERR_NONE;
+       }
+
+       g_dbus_connection_signal_unsubscribe(g_usb_bus, g_usb_handler);
+       g_object_unref(g_usb_bus);
+       g_usb_bus = NULL;
+
+       /*Release Callback*/
+       MS_SAFE_FREE(g_usb_cb_data);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+#define DBUS_REPLY_TIMEOUT  (-1)
+static int __ms_dbus_method_sync(const char *dest, const char *path,
+        const char *interface, const char *method, const char *param, GArray **dev_list)
+{
+       DBusConnection *conn;
+       DBusMessage *msg;
+       DBusMessageIter iiiter;
+       DBusMessage *reply;
+       DBusError err;
+       DBusMessageIter iter;
+       DBusMessageIter aiter, piter;
+       int result;
+
+       int val_int;
+       char *val_str;
+       bool val_bool;
+
+       conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
+       if (!conn) {
+               MS_DBG_ERR("dbus_bus_get error");
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       msg = dbus_message_new_method_call(dest, path, interface, method);
+       if (!msg) {
+               MS_DBG_ERR("dbus_message_new_method_call(%s:%s-%s)",
+               path, interface, method);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       dbus_message_iter_init_append(msg, &iiiter);
+       dbus_message_iter_append_basic(&iiiter, DBUS_TYPE_STRING, &param);
+
+       dbus_error_init(&err);
+
+       reply = dbus_connection_send_with_reply_and_block(conn, msg, DBUS_REPLY_TIMEOUT, &err);
+       dbus_message_unref(msg);
+       if (!reply) {
+               MS_DBG_ERR("dbus_connection_send error(%s:%s) %s %s:%s-%s",
+               err.name, err.message, dest, path, interface, method);
+               dbus_error_free(&err);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       dbus_message_iter_init(reply, &iter);
+       dbus_message_iter_recurse(&iter, &aiter);
+
+       result = 0;
+       while (dbus_message_iter_get_arg_type(&aiter) != DBUS_TYPE_INVALID) {
+               result++;
+               MS_DBG("(%d)th block device information", result);
+
+               ms_block_info_s *data = NULL;
+               data = malloc(sizeof(ms_block_info_s));
+
+               dbus_message_iter_recurse(&aiter, &piter);
+               dbus_message_iter_get_basic(&piter, &val_int);
+               MS_DBG("\tType(%d)", val_int);
+               data->block_type = val_int;
+
+               dbus_message_iter_next(&piter);
+               dbus_message_iter_get_basic(&piter, &val_str);
+               MS_DBG("\tdevnode(%s)", val_str);
+
+               dbus_message_iter_next(&piter);
+               dbus_message_iter_get_basic(&piter, &val_str);
+               MS_DBG("\tsyspath(%s)", val_str);
+
+               dbus_message_iter_next(&piter);
+               dbus_message_iter_get_basic(&piter, &val_str);
+               MS_DBG("\tfs_usage(%s)", val_str);
+
+               dbus_message_iter_next(&piter);
+               dbus_message_iter_get_basic(&piter, &val_str);
+               MS_DBG("\tfs_type(%s)", val_str);
+
+               dbus_message_iter_next(&piter);
+               dbus_message_iter_get_basic(&piter, &val_str);
+               MS_DBG("\tfs_version(%s)", val_str);
+
+               dbus_message_iter_next(&piter);
+               dbus_message_iter_get_basic(&piter, &val_str);
+               MS_DBG("\tfs_uuid_enc(%s)", val_str);
+
+               dbus_message_iter_next(&piter);
+               dbus_message_iter_get_basic(&piter, &val_int);
+               MS_DBG("\treadonly(%d)", val_int);
+
+               dbus_message_iter_next(&piter);
+               dbus_message_iter_get_basic(&piter, &val_str);
+               MS_DBG("\tmount point(%s)", val_str);
+               data->mount_path = strdup(val_str);
+
+               dbus_message_iter_next(&piter);
+               dbus_message_iter_get_basic(&piter, &val_int);
+               MS_DBG("\tstate(%d)", val_int);
+               data->state = val_int;
+
+               dbus_message_iter_next(&piter);
+               dbus_message_iter_get_basic(&piter, &val_bool);
+               MS_DBG("\tprimary(%d)", val_bool ? "true" : "false");
+
+               dbus_message_iter_next(&aiter);
+
+               if (*dev_list == NULL) {
+                       MS_DBG_ERR("DEV LIST IS NULL");
+                       *dev_list = g_array_new(FALSE, FALSE, sizeof(ms_block_info_s*));
+               }
+               g_array_append_val(*dev_list, data);
+       }
+
+       dbus_message_unref(reply);
+
+       return result;
+}
+
+
+int ms_sys_get_device_list(ms_stg_type_e stg_type, GArray **dev_list)
+{
+       int ret;
+       const char *dev_params[MS_STG_TYPE_MAX] = { BLOCK_DEVICE_USB,
+               BLOCK_DEVICE_MMC,
+               BLOCK_DEVICE_ALL,
+               };
+
+       ret = __ms_dbus_method_sync(DEVICED_BUS_NAME,
+               DEVICED_PATH_BLOCK_MANAGER,
+               DEVICED_INTERFACE_BLOCK_MANAGER,
+               BLOCK_DEVICE_METHOD,
+               dev_params[stg_type], dev_list);
+       if (ret < 0) {
+               MS_DBG("Failed to send dbus (%d)", ret);
+       } else {
+               MS_DBG("%d block devices are founded", ret);
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int ms_sys_release_device_list(GArray **dev_list)
+{
+       if (*dev_list) {
+               while((*dev_list)->len != 0) {
+                       ms_block_info_s *data = NULL;
+                       data = g_array_index(*dev_list , ms_block_info_s*, 0);
+                       g_array_remove_index (*dev_list, 0);
+                       MS_DBG("MOUNT PATH [%s] RELEASED",data->mount_path);
+                       MS_SAFE_FREE(data->mount_path);
+                       MS_SAFE_FREE(data);
+               }
+               g_array_free (*dev_list, FALSE);
+               *dev_list = NULL;
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+//////////////////////////////////////////////////////////////////////////////
+/// GET ACTIVATE USER ID
+//////////////////////////////////////////////////////////////////////////////
+#define UID_DBUS_NAME           "org.freedesktop.login1"
+#define UID_DBUS_PATH           "/org/freedesktop/login1"
+#define UID_DBUS_INTERFACE      UID_DBUS_NAME".Manager"
+#define UID_DBUS_METHOD                 "ListUsers"
+
+static int __ms_dbus_get_uid(const char *dest, const char *path, const char *interface, const char *method, uid_t *uid)
+{
+       DBusConnection *conn;
+       DBusMessage *msg;
+       DBusMessageIter iiiter;
+       DBusMessage *reply;
+       DBusError err;
+       DBusMessageIter iter;
+       DBusMessageIter aiter, piter;
+       int result;
+
+       int val_int;
+       char *val_str;
+       bool val_bool;
+
+
+       conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
+       if (!conn) {
+               MS_DBG_ERR("dbus_bus_get error");
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       msg = dbus_message_new_method_call(dest, path, interface, method);
+       if (!msg) {
+               MS_DBG_ERR("dbus_message_new_method_call(%s:%s-%s)",
+               path, interface, method);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       dbus_message_iter_init_append(msg, &iiiter);
+
+       dbus_error_init(&err);
+
+       reply = dbus_connection_send_with_reply_and_block(conn, msg, DBUS_REPLY_TIMEOUT, &err);
+       dbus_message_unref(msg);
+       if (!reply) {
+               MS_DBG_ERR("dbus_connection_send error(%s:%s) %s %s:%s-%s",
+               err.name, err.message, dest, path, interface, method);
+               dbus_error_free(&err);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       dbus_message_iter_init(reply, &iter);
+       dbus_message_iter_recurse(&iter, &aiter);
+
+       result = 0;
+       while (dbus_message_iter_get_arg_type(&aiter) != DBUS_TYPE_INVALID) {
+               result++;
+               MS_DBG("(%d)th block device information", result);
+
+               dbus_message_iter_recurse(&aiter, &piter);
+               dbus_message_iter_get_basic(&piter, &val_int);
+               MS_DBG("\tType(%d)", val_int);
+
+               dbus_message_iter_next(&piter);
+               dbus_message_iter_get_basic(&piter, &val_str);
+               MS_DBG("\tdevnode(%s)", val_str);
+
+               dbus_message_iter_next(&piter);
+               dbus_message_iter_get_basic(&piter, &val_str);
+               MS_DBG("\tsyspath(%s)", val_str);
+
+               dbus_message_iter_next(&aiter);
+       }
+
+       *uid = (uid_t) val_int;
+
+       return result;
+}
+
+int ms_sys_get_uid(uid_t *uid)
+{
+       int ret;
+
+       ret = __ms_dbus_get_uid(UID_DBUS_NAME,UID_DBUS_PATH, UID_DBUS_INTERFACE, UID_DBUS_METHOD, uid);
+       if (ret < 0) {
+               MS_DBG("Failed to send dbus (%d)", ret);
+       } else {
+               MS_DBG("%d get UID[%d]", ret, *uid);
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+//////////////////////////////////////////////////////////////////////////////
+/// CHECK THE POWER STATE
+//////////////////////////////////////////////////////////////////////////////
+
+#define POWER_DBUS_NAME "ChangeState"
+#define POWER_DBUS_PATH "/Org/Tizen/System/DeviceD/PowerOff"
+#define POWER_DBUS_INTERFACE "org.tizen.system.deviced.PowerOff"
+#define POWER_DBUS_MATCH_RULE "type='signal',interface='org.tizen.system.deviced.PowerOff'"
+
+typedef struct pwoff_cb_data{
+       power_off_cb usr_cb;
+       void *usr_data;
+} pwoff_cb_data;
+
+DBusConnection *g_pwr_dbus;
+
+pwoff_cb_data *g_pwr_cb_data = NULL;
+
+static DBusHandlerResult __poweroff_msg_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
+{
+       pwoff_cb_data *cb_data = (pwoff_cb_data *)user_data;
+       void *usr_cb = cb_data->usr_cb;
+       void *usr_data = cb_data->usr_data;
+
+       MS_DBG_FENTER();
+
+       /* A Ping signal on the com.burtonini.dbus.Signal interface */
+       if (dbus_message_is_signal (message, POWER_DBUS_INTERFACE, POWER_DBUS_NAME)) {
+               int current_type = DBUS_TYPE_INVALID;
+               DBusError error;
+               DBusMessageIter read_iter;
+               DBusBasicValue value;
+               power_off_cb cb_func = (power_off_cb)usr_cb;
+
+               dbus_error_init (&error);
+
+               /* get data from dbus message */
+               dbus_message_iter_init (message, &read_iter);
+               while ((current_type = dbus_message_iter_get_arg_type (&read_iter)) != DBUS_TYPE_INVALID){
+                       dbus_message_iter_get_basic (&read_iter, &value);
+                       switch(current_type) {
+                               case DBUS_TYPE_INT32:
+                                       MS_DBG_WARN("value[%d]", value.i32);
+                                       break;
+                               default:
+                                       MS_DBG_ERR("current type : %d", current_type);
+                                       break;
+                       }
+
+                       if (value.i32 == 2 || value.i32 == 3) {
+                               MS_DBG_WARN("PREPARE POWER OFF");
+                               break;
+                       }
+
+                       dbus_message_iter_next (&read_iter);
+               }
+
+               if (value.i32 == 2 || value.i32 == 3)
+                       cb_func(usr_data);
+
+               return DBUS_HANDLER_RESULT_HANDLED;
+       }
+
+       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+int ms_sys_set_poweroff_cb(power_off_cb user_callback, void *user_data)
+{
+       DBusError error;
+
+       /*add noti receiver for power off*/
+       dbus_error_init (&error);
+
+       g_pwr_dbus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
+       if (!g_pwr_dbus) {
+               MS_DBG_ERR ("Failed to connect to the D-BUS daemon: %s", error.message);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       dbus_connection_setup_with_g_main (g_pwr_dbus, NULL);
+
+       g_pwr_cb_data = malloc(sizeof(pwoff_cb_data));
+       if (g_pwr_cb_data == NULL) {
+               MS_DBG_ERR ("malloc failed");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
+
+       g_pwr_cb_data->usr_cb = user_callback;
+       g_pwr_cb_data->usr_data = user_data;
+
+       /* listening to messages from all objects as no path is specified */
+       dbus_bus_add_match (g_pwr_dbus, POWER_DBUS_MATCH_RULE, &error);
+       if( !dbus_connection_add_filter (g_pwr_dbus, __poweroff_msg_filter, g_pwr_cb_data, NULL)) {
+               dbus_bus_remove_match (g_pwr_dbus, POWER_DBUS_MATCH_RULE, NULL);
+               MS_DBG_ERR("dbus_connection_add_filter failed");
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int ms_sys_unset_poweroff_cb(void)
+{
+       if (g_pwr_dbus == NULL) {
+               return MS_MEDIA_ERR_NONE;
+       }
+
+       dbus_connection_remove_filter(g_pwr_dbus, __poweroff_msg_filter, g_pwr_cb_data);
+       dbus_bus_remove_match (g_pwr_dbus, MS_MEDIA_DBUS_MATCH_RULE, NULL);
+       dbus_connection_unref(g_pwr_dbus);
+       g_pwr_dbus = NULL;
+
+       MS_SAFE_FREE(g_pwr_cb_data);
+
+        return MS_MEDIA_ERR_NONE;
+}
+
index 55ad4d2..3fe7418 100755 (executable)
 #include <aul/aul.h>
 #include <grp.h>
 #include <pwd.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib-lowlevel.h>
 
 #include "media-util.h"
 #include "media-server-ipc.h"
 #include "media-common-dbg.h"
+#include "media-common-system.h"
 #include "media-common-utils.h"
 
 #ifdef FMS_PERF
@@ -39,23 +43,19 @@ struct timeval g_mmc_end_time;
 #endif
 
 #define MS_DRM_CONTENT_TYPE_LENGTH 100
-#define GLOBAL_USER    0 //#define     tzplatform_getenv(TZ_GLOBAL) //TODO
 
 #ifdef FMS_PERF
-void
-ms_check_start_time(struct timeval *start_time)
+void ms_check_start_time(struct timeval *start_time)
 {
        gettimeofday(start_time, NULL);
 }
 
-void
-ms_check_end_time(struct timeval *end_time)
+void ms_check_end_time(struct timeval *end_time)
 {
        gettimeofday(end_time, NULL);
 }
 
-void
-ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
+void ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
 {
        struct timeval time;
        long difftime;
@@ -68,17 +68,28 @@ ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
 }
 #endif
 
-bool
-ms_is_mmc_inserted(void)
+bool ms_is_mmc_inserted(void)
 {
-       int data = -1;
-       ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &data);
-       if (data != VCONFKEY_SYSMAN_MMC_MOUNTED) {
-               return false;
+       bool ret = FALSE;
+       ms_stg_type_e stg_type = MS_STG_TYPE_MMC;
+       GArray *dev_list = NULL;
+
+       ret = ms_sys_get_device_list(stg_type, &dev_list);
+       if (ret == MS_MEDIA_ERR_NONE) {
+               if (dev_list != NULL) {
+                       MS_DBG_ERR("MMC FOUND[%d]", dev_list->len);
+                       ms_sys_release_device_list(&dev_list);
+                       ret = TRUE;
+               } else {
+                       MS_DBG_ERR("MMC NOT FOUND");
+               }
        } else {
-               return true;
+               MS_DBG_ERR("ms_sys_get_device_list failed");
        }
+
+       return ret;
 }
+
 static char* __media_get_path(uid_t uid)
 {
        char *result_psswd = NULL;
@@ -122,8 +133,7 @@ static char* __media_get_path(uid_t uid)
        return result_psswd;
 }
 
-ms_storage_type_t
-ms_get_storage_type_by_full(const char *path, uid_t uid)
+ms_storage_type_t ms_get_storage_type_by_full(const char *path, uid_t uid)
 {
        int length_path;
        char * user_path = NULL;
@@ -144,8 +154,7 @@ ms_get_storage_type_by_full(const char *path, uid_t uid)
     }
 }
 
-int
-ms_strappend(char *res, const int size, const char *pattern,
+int ms_strappend(char *res, const int size, const char *pattern,
             const char *str1, const char *str2)
 {
        int len = 0;
@@ -154,7 +163,7 @@ ms_strappend(char *res, const int size, const char *pattern,
        if (!res ||!pattern || !str1 ||!str2 )
                return MS_MEDIA_ERR_INVALID_PARAMETER;
 
-       if (real_size < (strlen(str1) + strlen(str2)))
+       if (real_size < (int)(strlen(str1) + strlen(str2)))
                return MS_MEDIA_ERR_INVALID_PARAMETER;
 
        len = snprintf(res, real_size, pattern, str1, str2);
@@ -167,8 +176,7 @@ ms_strappend(char *res, const int size, const char *pattern,
        return MS_MEDIA_ERR_NONE;
 }
 
-int
-ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
+int ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
 {
        int len = 0;
        int real_size = size;
@@ -178,7 +186,7 @@ ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
                return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
 
-       if (real_size < strlen(str1)) {
+       if (real_size < (int)(strlen(str1))) {
                MS_DBG_ERR("size is wrong");
                return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
@@ -194,8 +202,7 @@ ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
        return MS_MEDIA_ERR_NONE;
 }
 
-bool
-ms_config_get_int(const char *key, int *value)
+bool ms_config_get_int(const char *key, int *value)
 {
        int err;
 
@@ -215,8 +222,7 @@ ms_config_get_int(const char *key, int *value)
        return false;
 }
 
-bool
-ms_config_set_int(const char *key, int value)
+bool ms_config_set_int(const char *key, int value)
 {
        int err;
 
@@ -236,27 +242,26 @@ ms_config_set_int(const char *key, int value)
        return false;
 }
 
-bool
-ms_config_get_str(const char *key, char *value)
+bool ms_config_get_str(const char *key, char **value)
 {
-       char *res;
-       if (!key || !value) {
+       char *res = NULL;
+
+       if (key == NULL || value == NULL) {
                MS_DBG_ERR("Arguments key or value is NULL");
                return false;
        }
 
        res = vconf_get_str(key);
-       if (res) {
-               strncpy(value, res, strlen(res) + 1);
-               free(res);
+       if (MS_STRING_VALID(res)) {
+               *value = strdup(res);
+               MS_SAFE_FREE(res);
                return true;
        }
 
        return false;
 }
 
-bool
-ms_config_set_str(const char *key, const char *value)
+bool ms_config_set_str(const char *key, const char *value)
 {
        int err;
 
@@ -274,8 +279,7 @@ ms_config_set_str(const char *key, const char *value)
        return false;
 }
 
-bool
-ms_config_get_bool(const char *key, int *value)
+bool ms_config_get_bool(const char *key, int *value)
 {
        int err;
 
@@ -293,4 +297,4 @@ ms_config_get_bool(const char *key, int *value)
                MS_DBG_ERR("Unexpected error code: %d", err);
 
        return false;
-}
\ No newline at end of file
+}
index dbae740..b6594cf 100644 (file)
@@ -21,6 +21,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -37,7 +38,8 @@ GMainLoop * mainloop = NULL;
 
 int (*svc_connect)                             (void ** handle, uid_t uid, char ** err_msg);
 int (*svc_disconnect)                  (void * handle, char ** err_msg);
-int (*svc_check_db)                    (void * handle, uid_t uid, char ** err_msg);
+int (*svc_check_db)                    (void * handle, bool *, uid_t uid, char ** err_msg);
+int (*svc_get_storage_id)              (void * handle, const char *path, char *storage_id, uid_t uid, char ** err_msg);
 
 void callback(media_request_result_s * result, void *user_data)
 {
@@ -63,12 +65,13 @@ void print_help()
        printf("=======================================================================================\n");
 }
 
-void check_media_db(void)
+static void __check_media_db(void)
 {
        void *funcHandle = NULL;
        void *db_handle = NULL;
        char *err_msg = NULL;
        int ret = 0;
+       bool fscan= false;
 
        funcHandle = dlopen ("/usr/lib/libmedia-content-plugin.so", RTLD_LAZY);
        if(funcHandle == NULL)
@@ -85,7 +88,7 @@ void check_media_db(void)
        if(ret < 0)
                printf("Error svc_connect\n");
 
-       ret = svc_check_db(db_handle,tzplatform_getuid(TZ_USER_NAME), &err_msg);
+       ret = svc_check_db(db_handle, &fscan, tzplatform_getuid(TZ_USER_NAME), &err_msg);
        if(ret < 0)
                printf("Error svc_check_db\n");
 
@@ -98,14 +101,57 @@ void check_media_db(void)
        dlclose (funcHandle);
 }
 
-int dir_scan_non_recursive(char *path)
+static void __get_storage_id(const char *path, char *storage_id, uid_t uid)
 {
-       return media_directory_scanning_async(path, FALSE, callback, NULL, tzplatform_getuid(TZ_USER_NAME));
+       void *funcHandle = NULL;
+       void *db_handle = NULL;
+       char *err_msg = NULL;
+       int ret = 0;
+
+       funcHandle = dlopen ("/usr/lib/libmedia-content-plugin.so", RTLD_LAZY);
+       if(funcHandle == NULL)
+       {
+               printf("Error when open plug-in\n");
+               return;
+       }
+
+       svc_connect                     = dlsym (funcHandle, "connect_db");
+       svc_disconnect          = dlsym (funcHandle, "disconnect_db");
+       svc_get_storage_id      = dlsym (funcHandle, "get_storage_id");
+
+       ret = svc_connect(&db_handle, uid, &err_msg);
+       if(ret < 0)
+               printf("Error svc_connect\n");
+
+       ret = svc_get_storage_id(db_handle, path, storage_id, uid, &err_msg);
+       if(ret < 0)
+               printf("Error svc_get_storage_id\n");
+
+       ret = svc_disconnect(db_handle, &err_msg);
+       if(ret < 0)
+               printf("Error svc_disconnect\n");
+
+       printf("Start Scanning for [%s][%s]\n", path, storage_id);
+
+       dlclose (funcHandle);
+}
+
+int dir_scan_non_recursive(const char *path)
+{
+       char storage_id[36+1] = {0,};
+
+       __get_storage_id(path, storage_id, tzplatform_getuid(TZ_USER_NAME));
+
+       return media_directory_scanning_async(path, storage_id, FALSE, callback, NULL, tzplatform_getuid(TZ_USER_NAME));
 }
 
 int dir_scan_recursive(char *path)
 {
-       return media_directory_scanning_async(path, TRUE, callback, NULL, tzplatform_getuid(TZ_USER_NAME));
+       char storage_id[36+1] = {0,};
+
+       __get_storage_id(path, storage_id, tzplatform_getuid(TZ_USER_NAME));
+
+       return media_directory_scanning_async(path, storage_id, TRUE, callback, NULL, tzplatform_getuid(TZ_USER_NAME));
 }
 
 typedef enum {
@@ -167,7 +213,7 @@ int main(int argc, char **argv)
                }
 
                if (strcmp(argv1 , "check_db") == 0) {
-                       check_media_db();
+                       __check_media_db();
                        exit(1);
                }
 
diff --git a/src/scanner/include/media-scanner-db-svc.h b/src/scanner/include/media-scanner-db-svc.h
deleted file mode 100755 (executable)
index ef7f337..0000000
+++ /dev/null
@@ -1,148 +0,0 @@
-/*
- *  Media Server
- *
- * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-db-svc.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
-#ifndef _MEDIA_SCANNER_DB_SVC_H_
-#define _MEDIA_SCANNER_DB_SVC_H_
-
-#include "media-common-types.h"
-
-typedef int (*CONNECT)(void**, uid_t, char **);
-typedef int (*DISCONNECT)(void*, char **);
-typedef int (*CHECK_ITEM_EXIST)(void*, const char*, bool*, char **);
-typedef int (*INSERT_ITEM_BEGIN)(void*, int, int, int, char **);
-typedef int (*INSERT_ITEM_END)(void*, uid_t, char **);
-typedef int (*INSERT_ITEM)(void*, const char*, int, uid_t, char **);
-typedef int (*INSERT_ITEM_IMMEDIATELY)(void*, const char*, int, uid_t, char **);
-typedef int (*SET_ALL_STORAGE_ITEMS_VALIDITY)(void*, int, int, uid_t, char **);
-typedef int (*SET_ITEM_VALIDITY_BEGIN)(void*, int, char **);
-typedef int (*SET_ITEM_VALIDITY_END)(void*, uid_t, char **);
-typedef int (*SET_ITEM_VALIDITY)(void*, const char*, int, int, uid_t, char **);
-typedef int (*DELETE_ALL_ITEMS_IN_STORAGE)(void*, int, uid_t, char **);
-typedef int (*DELETE_ALL_INVALID_ITMES_IN_STORAGE)(void*, int, uid_t, char **);
-typedef int (*UPDATE_BEGIN)(void);
-typedef int (*UPDATE_END)(uid_t);
-typedef int (*SET_FOLDER_ITEM_VALIDITY)(void*, const char*, int, int, uid_t, char**);
-typedef int (*DELETE_ALL_INVALID_ITEMS_IN_FOLDER)(void*, const char*, uid_t, char**);
-typedef int (*INSERT_BURST_ITEM)(void *, const char *, int, uid_t, char **);
-typedef int (*SEND_DIR_UPDATE_NOTI)(void *, const char *, char **);
-typedef int (*COUNT_DELETE_ITEMS_IN_FOLDER)(void *, const char *, int *, char **);
-typedef int (*DELETE_ITEM)(void *, const char *, uid_t, char **);
-typedef int (*GET_FOLDER_LIST)(void *, char*, char ***, int **, int **, int *, char **);
-typedef int (*UPDATE_FOLDER_TIME)(void *, const char *, uid_t, char **);
-typedef int (*UPDATE_ITEM_META)(void *, const char *, int, uid_t, char **);
-typedef int (*UPDATE_ITEM_BEGIN)(void *, int, char **);
-typedef int (*UPDATE_ITEM_END)(void *, uid_t, char **);
-
-int
-msc_load_functions(void);
-
-void
-msc_unload_functions(void);
-
-int
-msc_connect_db(void ***handle, uid_t uid);
-
-int
-msc_disconnect_db(void ***handle);
-
-int
-msc_validate_item(void **handle, const char *path, uid_t uid);
-
-int
-msc_insert_item_batch(void **handle, const char *path, uid_t uid);
-
-int
-msc_insert_burst_item(void **handle, const char *path, uid_t uid);
-
-bool
-msc_delete_all_items(void **handle, ms_storage_type_t store_type, uid_t uid);
-
-int
-msc_validaty_change_all_items(void **handle, ms_storage_type_t store_type, bool validity, uid_t uid);
-
-bool
-msc_delete_invalid_items(void **handle, ms_storage_type_t store_type, uid_t uid);
-
-int
-msc_set_folder_validity(void **handle, const char *path, int validity, int recursive, uid_t uid);
-
-int
-msc_delete_invalid_items_in_folder(void **handle, const char*path, uid_t uid);
-
-int
-msc_send_dir_update_noti(void **handle, const char*path);
-
-int
-msc_count_delete_items_in_folder(void **handle, const char*path, int *count);
-
-typedef struct msc_dir_info_s {
-       char *dir_path;
-       int modified_time;
-       int item_num;
-} msc_dir_info_s;
-
-int
-msc_get_folder_list(void **handle, char* start_path, GArray **dir_array);
-
-int
-msc_update_folder_time(void **handle, char *folder_path, uid_t uid);
-
-int
-msc_insert_item_immediately(void **handle, const char *path, uid_t uid);
-
-int
-msc_update_meta_batch(void **handle, const char *path, uid_t uid);
-
-/****************************************************************************************************
-FOR BULK COMMIT
-*****************************************************************************************************/
-typedef enum {
-       MS_NOTI_DISABLE = 0,    /**< Stored only in phone */
-       MS_NOTI_ENABLE = 1,          /**< Stored only in MMC */
-} ms_noti_status_e;
-
-void
-msc_register_start(void **handle, ms_noti_status_e noti_status, int pid);
-
-void
-msc_register_end(void **handle, uid_t uid);
-
-void
-msc_validate_start(void **handle);
-
-void
-msc_validate_end(void **handle, uid_t uid);
-
-void
-msc_update_start(void **handle);
-
-void
-msc_update_end(void **handle, uid_t uid);
-
-#endif /*_MEDIA_SCANNER_DB_SVC_H_*/
index 9315d15..35e2e52 100755 (executable)
 #include <dlog.h>
 #include <errno.h>
 
+#include "media-common-dbg.h"
+
 #ifdef LOG_TAG
 #undef LOG_TAG
 #endif
-#define LOG_TAG "MEDIA_SCANNER"
-#define BUF_LENGTH 256
-
-#define MSC_DBG_STRERROR(fmt) do { \
-                       char buf[BUF_LENGTH] = {0,}; \
-                       LOGE(fmt" : STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGTH)); \
-               } while (0)
-
-#define MSC_DBG_SLOG(fmt, args...)     do{ if (true) { \
-               SECURE_LOGD(fmt "\n", ##args); \
-               }} while(false)
-
-#define MSC_DBG(fmt, args...) do{ if (true) { \
-               LOGD(fmt "\n", ##args); \
-               }} while(false)
 
-#define MSC_DBG_INFO(fmt, args...) do{ if (true) { \
-               LOGI(fmt "\n", ##args); \
-               }} while(false)
-
-#define MSC_DBG_ERR(fmt, args...) do{ if (true) { \
-               LOGE(fmt "\n", ##args); \
-               }} while(false)
-
-#define MSC_DBG_WAN(fmt, args...) do{ if (true) { \
-               LOGW(fmt "\n", ##args); \
-               }} while(false)
+#define LOG_TAG "MEDIA_SCANNER"
 
-/**
- * @}
- */
 #endif /*_MEDIA_SCANNER_DBG_H_*/
 
diff --git a/src/scanner/include/media-scanner-device-block.h b/src/scanner/include/media-scanner-device-block.h
new file mode 100755 (executable)
index 0000000..59cc8ba
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ *  Media Server
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef _MEDIA_SCANNER_DEVICE_BLOCK_H_
+#define _MEDIA_SCANNER_DEVICE_BLOCK_H_
+
+#include "media-common-system.h"
+
+void msc_device_block_changed_cb(const char *mount_path, int block_type, ms_stg_status_e mount_status, void *user_data);
+
+#endif
\ No newline at end of file
index 9d778e5..408720a 100755 (executable)
 #ifndef _MEDIA_SCANNER_SCAN_H_
 #define _MEDIA_SCANNER_SCAN_H_
 
-gboolean msc_directory_scan_thread(void *data);
+#include "media-common-system.h"
 
-gboolean msc_register_thread(void *data);
+typedef enum {
+       MS_SCAN_STORAGE = 0,
+       MS_SCAN_DIRECTORY = 1,
+       MS_SCAN_REGISTER = 2,
+       MS_SCAN_MAX,
+} ms_scan_type_e;
 
+gboolean msc_directory_scan_thread(void *data);
+gboolean msc_register_thread(void *data);
 gboolean msc_storage_scan_thread(void *data);
-
 int msc_check_remain_task(void);
-
 ms_db_status_type_t msc_check_scanning_status(void);
-
 int msc_set_cancel_path(const char *cancel_path);
-
 int msc_del_cancel_path(void);
-
 int msc_set_blocked_path(const char *blocked_path);
-
 int msc_del_blocked_path(void);
-
 void msc_metadata_update_thread(ms_comm_msg_s *recv_msg);
+int msc_init_scanner(void);
+int msc_deinit_scanner(void);
+int msc_set_mmc_status(ms_stg_status_e status);
+int msc_push_scan_request(ms_scan_type_e scan_type, ms_comm_msg_s *recv_msg);
+int msc_send_power_off_request(void);
+int msc_remove_dir_scan_request(ms_comm_msg_s *recv_msg);
 
 #endif /*_MEDIA_SCANNER_SCAN_H_*/
\ No newline at end of file
diff --git a/src/scanner/media-scanner-db-svc.c b/src/scanner/media-scanner-db-svc.c
deleted file mode 100755 (executable)
index 51b93f0..0000000
+++ /dev/null
@@ -1,790 +0,0 @@
-/*
- *  Media Server
- *
- * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-db-svc.c
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief       This file implements main database operation.
- */
-
-#include <dlfcn.h>
-
-#include "media-util.h"
-
-#include "media-common-utils.h"
-#include "media-scanner-dbg.h"
-#include "media-scanner-db-svc.h"
-
-#include <tzplatform_config.h>
-
-#define CONFIG_PATH tzplatform_mkpath(TZ_SYS_DATA,"file-manager-service/plugin-config")
-#define EXT ".so"
-#define EXT_LEN 3
-#define MSC_REGISTER_COUNT 100 /*For bundle commit*/
-#define MSC_VALID_COUNT 100 /*For bundle commit*/
-
-GMutex db_mutex;
-GArray *so_array;
-void ***func_array;
-int lib_num;
-void **scan_func_handle = NULL; /*dlopen handel*/
-extern int insert_count;
-
-enum func_list {
-       eCONNECT,
-       eDISCONNECT,
-       eEXIST,
-       eINSERT_BEGIN,
-       eINSERT_END,
-       eINSERT_BATCH,
-       eINSERT_ITEM_IMMEDIATELY,
-       eSET_ALL_VALIDITY,
-       eSET_VALIDITY_BEGIN,
-       eSET_VALIDITY_END,
-       eSET_VALIDITY,
-       eDELETE_ALL,
-       eDELETE_INVALID_ITEMS,
-       eUPDATE_BEGIN,
-       eUPDATE_END,
-       eSET_FOLDER_VALIDITY,
-       eDELETE_FOLDER,
-       eINSERT_BURST,
-       eSEND_DIR_UPDATE_NOTI,
-       eCOUNT_DELETE_ITEMS_IN_FOLDER,
-       eDELETE_ITEM,
-       eGET_FOLDER_LIST,
-       eUPDATE_FOLDER_TIME,
-       eUPDATE_ITEM_META,
-       eUPDATE_ITEM_BEGIN,
-       eUPDATE_ITEM_END,
-       eFUNC_MAX
-};
-
-static int
-_msc_token_data(char *buf, char **name)
-{
-       int len;
-       char* pos = NULL;
-
-       pos = strstr(buf, EXT);
-       if (pos == NULL) {
-               MSC_DBG_ERR("This is not shared object library.");
-               name = NULL;
-               return -1;
-       } else {
-               len = pos - buf + EXT_LEN;
-               *name = strndup(buf, len);
-       }
-
-       return 0;
-}
-
-static bool
-_msc_load_config()
-{
-       int ret;
-       FILE *fp;
-       char *so_name = NULL;
-       char buf[256] = {0};
-
-       fp = fopen(CONFIG_PATH, "rt");
-       if (fp == NULL) {
-               MSC_DBG_ERR("fp is NULL");
-               return MS_MEDIA_ERR_FILE_OPEN_FAIL;
-       }
-       while(1) {
-               if (fgets(buf, 256, fp) == NULL)
-                       break;
-
-               ret = _msc_token_data(buf, &so_name);
-               if (ret == 0) {
-                       /*add to array*/
-                       g_array_append_val(so_array, so_name);
-                       so_name = NULL;
-               }
-       }
-
-       fclose(fp);
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int
-msc_load_functions(void)
-{
-       int lib_index = 0, func_index;
-       char func_list[eFUNC_MAX][40] = {
-               "connect_db",
-               "disconnect_db",
-               "check_item_exist",
-               "insert_item_begin",
-               "insert_item_end",
-               "insert_item",
-               "insert_item_immediately",
-               "set_all_storage_items_validity",
-               "set_item_validity_begin",
-               "set_item_validity_end",
-               "set_item_validity",
-               "delete_all_items_in_storage",
-               "delete_all_invalid_items_in_storage",
-               "update_begin",
-               "update_end",
-               "set_folder_item_validity",
-               "delete_all_invalid_items_in_folder",
-               "insert_burst_item",
-               "send_dir_update_noti",
-               "count_delete_items_in_folder",
-               "delete_item",
-               "get_folder_list",
-               "update_folder_time",
-               "update_item_meta",
-               "update_item_begin",
-               "update_item_end",
-               };
-       /*init array for adding name of so*/
-       so_array = g_array_new(FALSE, FALSE, sizeof(char*));
-
-       /*load information of so*/
-       _msc_load_config();
-
-       if(so_array->len == 0) {
-               MSC_DBG_ERR("There is no information for functions");
-               return MS_MEDIA_ERR_DYNAMIC_LINK;
-       }
-
-       /*the number of functions*/
-       lib_num = so_array->len;
-
-       MSC_DBG_SLOG("The number of information of so : %d", lib_num);
-       MS_MALLOC(scan_func_handle, sizeof(void*) * lib_num);
-       if (scan_func_handle == NULL) {
-               MSC_DBG_ERR("malloc failed");
-               return MS_MEDIA_ERR_OUT_OF_MEMORY;
-       }
-
-       while(lib_index < lib_num) {
-               /*get handle*/
-               MSC_DBG_SLOG("[name of so : %s]", g_array_index(so_array, char*, lib_index));
-               scan_func_handle[lib_index] = dlopen(g_array_index(so_array, char*, lib_index), RTLD_LAZY);
-               if (!scan_func_handle[lib_index]) {
-                       MSC_DBG_ERR("%s", dlerror());
-                       MS_SAFE_FREE(scan_func_handle);
-                       return MS_MEDIA_ERR_DYNAMIC_LINK;
-               }
-               lib_index++;
-       }
-
-       dlerror();    /* Clear any existing error */
-
-       /*allocate for array of functions*/
-       MS_MALLOC(func_array, sizeof(void**) * lib_num);
-       if (func_array == NULL) {
-               MSC_DBG_ERR("malloc failed");
-               MS_SAFE_FREE(scan_func_handle);
-               return MS_MEDIA_ERR_OUT_OF_MEMORY;
-       }
-
-       for(lib_index = 0 ; lib_index < lib_num; lib_index ++) {
-               MS_MALLOC(func_array[lib_index], sizeof(void*) * eFUNC_MAX);
-               if (func_array[lib_index] == NULL) {
-                       int index;
-
-                       for (index = 0; index < lib_index; index ++) {
-                               MS_SAFE_FREE(func_array[index]);
-                       }
-                       MS_SAFE_FREE(func_array);
-                       MS_SAFE_FREE(scan_func_handle);
-
-                       MSC_DBG_ERR("malloc failed");
-                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
-               }
-       }
-
-       /*add functions to array */
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               for (func_index = 0; func_index < eFUNC_MAX ; func_index++) {
-                       func_array[lib_index][func_index] = dlsym(scan_func_handle[lib_index], func_list[func_index]);
-                       if (func_array[lib_index][func_index] == NULL) {
-                               int index;
-
-                               for (index = 0; index < lib_index; index ++) {
-                                       MS_SAFE_FREE(func_array[index]);
-                               }
-                               MS_SAFE_FREE(func_array);
-                               MS_SAFE_FREE(scan_func_handle);
-
-                               MSC_DBG_ERR("dlsym failed [%s]", func_list[func_index]);
-                               return MS_MEDIA_ERR_DYNAMIC_LINK;
-                       }
-               }
-       }
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-void
-msc_unload_functions(void)
-{
-       int lib_index;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index ++)
-               dlclose(scan_func_handle[lib_index]);
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-                       if (func_array[lib_index]) {
-                               MS_SAFE_FREE(func_array[lib_index]);
-                       }
-       }
-
-       MS_SAFE_FREE (func_array);
-       MS_SAFE_FREE (scan_func_handle);
-
-       if (so_array) {
-               /*delete all node*/
-               while(so_array->len != 0) {
-                       char *so_name = NULL;
-                       so_name = g_array_index(so_array , char*, 0);
-                       g_array_remove_index (so_array, 0);
-                       MS_SAFE_FREE(so_name);
-               }
-               g_array_free(so_array, FALSE);
-               so_array = NULL;
-       }
-}
-
-int
-msc_connect_db(void ***handle, uid_t uid)
-{
-       int lib_index;
-       int ret;
-       char * err_msg = NULL;
-
-       /*Lock mutex for openning db*/
-       g_mutex_lock(&db_mutex);
-
-       MS_MALLOC(*handle, sizeof (void*) * lib_num);
-       if (*handle == NULL) {
-               MSC_DBG_ERR("malloc failed");
-               g_mutex_unlock(&db_mutex);
-               return MS_MEDIA_ERR_OUT_OF_MEMORY;
-       }
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((CONNECT)func_array[lib_index][eCONNECT])(&((*handle)[lib_index]), uid, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       g_mutex_unlock(&db_mutex);
-
-                       return MS_MEDIA_ERR_DB_CONNECT_FAIL;
-               }
-       }
-
-       MSC_DBG_INFO("connect Media DB");
-
-       g_mutex_unlock(&db_mutex);
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int
-msc_disconnect_db(void ***handle)
-{
-       int lib_index;
-       int ret;
-       char * err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((DISCONNECT)func_array[lib_index][eDISCONNECT])((*handle)[lib_index], &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       return MS_MEDIA_ERR_DB_DISCONNECT_FAIL;
-               }
-       }
-
-       MS_SAFE_FREE(*handle);
-
-       MSC_DBG_INFO("Disconnect Media DB");
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int
-msc_validate_item(void **handle, const char *path, uid_t uid)
-{
-       int lib_index;
-       int res = MS_MEDIA_ERR_NONE;
-       int ret;
-       char *err_msg = NULL;
-       bool modified = FALSE;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               /*check exist in Media DB, If file is not exist, insert data in DB. */
-               ret = ((CHECK_ITEM_EXIST)func_array[lib_index][eEXIST])(handle[lib_index], path, &modified, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MS_SAFE_FREE(err_msg);
-                       ret = msc_insert_item_batch(handle, path, uid);
-                       if (ret != 0) {
-                               res = MS_MEDIA_ERR_DB_INSERT_FAIL;
-                       } else {
-                               insert_count++;
-                       }
-               } else {
-                       if (modified == FALSE) {
-                               /*if meta data of file exist, change valid field to "1" */
-                               ret = ((SET_ITEM_VALIDITY)func_array[lib_index][eSET_VALIDITY])(handle[lib_index], path, true, true, uid, &err_msg); /*dlopen*/
-                               if (ret != 0) {
-                                       MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
-                                       MS_SAFE_FREE(err_msg);
-                                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
-                               }
-                       } else {
-                               /* the file has same name but it is changed, so we have to update DB */
-                               MSC_DBG_WAN("DELETE ITEM [%s]", path);
-                               ret = ((DELETE_ITEM)func_array[lib_index][eDELETE_ITEM])(handle[lib_index], path, uid, &err_msg); /*dlopen*/
-                               if (ret != 0) {
-                                       MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
-                                       MS_SAFE_FREE(err_msg);
-                                       res = MS_MEDIA_ERR_DB_DELETE_FAIL;
-                               } else {
-                                       ret = msc_insert_item_batch(handle, path, uid);
-                                       if (ret != 0) {
-                                               res = MS_MEDIA_ERR_DB_INSERT_FAIL;
-                                       } else {
-                                               insert_count++;
-                                       }
-                               }
-                       }
-               }
-       }
-
-       return res;
-}
-
-int
-msc_validaty_change_all_items(void **handle, ms_storage_type_t store_type, bool validity , uid_t uid)
-{
-       int lib_index;
-       int res = MS_MEDIA_ERR_NONE;
-       int ret;
-       char *err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((SET_ALL_STORAGE_ITEMS_VALIDITY)func_array[lib_index][eSET_ALL_VALIDITY])(handle[lib_index], store_type, validity, uid, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
-               }
-       }
-
-       return res;
-}
-
-int
-msc_insert_item_batch(void **handle, const char *path, uid_t uid)
-{
-       int lib_index;
-       int res = MS_MEDIA_ERR_NONE;
-       int ret;
-       char *err_msg = NULL;
-       ms_storage_type_t storage_type;
-
-       storage_type = ms_get_storage_type_by_full(path, uid);
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((INSERT_ITEM)func_array[lib_index][eINSERT_BATCH])(handle[lib_index], path, storage_type, uid, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
-                       MS_SAFE_FREE(err_msg);
-                       res = MS_MEDIA_ERR_DB_INSERT_FAIL;
-               }
-       }
-
-       return res;
-}
-
-int
-msc_insert_item_immediately(void **handle, const char *path, uid_t uid)
-{
-       int lib_index;
-       int res = MS_MEDIA_ERR_NONE;
-       int ret;
-       char *err_msg = NULL;
-       ms_storage_type_t storage_type;
-
-       storage_type = ms_get_storage_type_by_full(path, uid);
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((INSERT_ITEM_IMMEDIATELY)func_array[lib_index][eINSERT_ITEM_IMMEDIATELY])(handle[lib_index], path, storage_type, uid, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
-                       MS_SAFE_FREE(err_msg);
-                       res = MS_MEDIA_ERR_DB_INSERT_FAIL;
-               }
-       }
-
-       return res;
-}
-
-int
-msc_insert_burst_item(void **handle, const char *path , uid_t uid)
-{
-       int lib_index;
-       int res = MS_MEDIA_ERR_NONE;
-       int ret;
-       char *err_msg = NULL;
-       ms_storage_type_t storage_type;
-
-       storage_type = ms_get_storage_type_by_full(path,uid);
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((INSERT_BURST_ITEM)func_array[lib_index][eINSERT_BURST])(handle[lib_index], path, storage_type, uid, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
-                       MS_SAFE_FREE(err_msg);
-                       res = MS_MEDIA_ERR_DB_INSERT_FAIL;
-               }
-       }
-
-       return res;
-}
-
-bool
-msc_delete_all_items(void **handle, ms_storage_type_t store_type, uid_t uid)
-{
-       int lib_index;
-       int ret = 0;
-       char *err_msg = NULL;
-
-       /* To reset media db when differnet mmc is inserted. */
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((DELETE_ALL_ITEMS_IN_STORAGE)func_array[lib_index][eDELETE_ALL])(handle[lib_index], store_type, uid, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       return false;
-               }
-       }
-
-       return true;
-}
-
-bool
-msc_delete_invalid_items(void **handle, ms_storage_type_t store_type, uid_t uid)
-{
-       int lib_index;
-       int ret;
-       char *err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((DELETE_ALL_INVALID_ITMES_IN_STORAGE)func_array[lib_index][eDELETE_INVALID_ITEMS])(handle[lib_index], store_type, uid, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       return false;
-               }
-       }
-
-       return true;
-}
-
-int
-msc_set_folder_validity(void **handle, const char *path, int validity, int recursive, uid_t uid)
-{
-       int lib_index;
-       int ret;
-       char *err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((SET_FOLDER_ITEM_VALIDITY)func_array[lib_index][eSET_FOLDER_VALIDITY])(handle[lib_index], path, validity, recursive, uid, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       return MS_MEDIA_ERR_DB_UPDATE_FAIL;
-               }
-       }
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int
-msc_delete_invalid_items_in_folder(void **handle, const char*path, uid_t uid)
-{
-       int lib_index;
-       int ret;
-       char *err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((DELETE_ALL_INVALID_ITEMS_IN_FOLDER)func_array[lib_index][eDELETE_FOLDER])(handle[lib_index], path, uid, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       return MS_MEDIA_ERR_DB_UPDATE_FAIL;
-               }
-       }
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int
-msc_send_dir_update_noti(void **handle, const char*path)
-{
-       int lib_index;
-       int ret;
-       char *err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((SEND_DIR_UPDATE_NOTI)func_array[lib_index][eSEND_DIR_UPDATE_NOTI])(handle[lib_index], path, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       return MS_MEDIA_ERR_SEND_NOTI_FAIL;
-               }
-       }
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int
-msc_count_delete_items_in_folder(void **handle, const char*path, int *count)
-{
-       int lib_index;
-       int ret;
-       char *err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((COUNT_DELETE_ITEMS_IN_FOLDER)func_array[lib_index][eCOUNT_DELETE_ITEMS_IN_FOLDER])(handle[lib_index], path, count, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       return MS_MEDIA_ERR_INTERNAL;
-               }
-       }
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int
-msc_get_folder_list(void **handle, char* start_path, GArray **dir_array)
-{
-       int lib_index;
-       int ret;
-       char *err_msg = NULL;
-
-       char **folder_list = NULL;
-       int *modified_time_list = NULL;
-       int *item_num_list = NULL;
-       int count = 0;
-       int i = 0;
-
-       msc_dir_info_s* dir_info = NULL;
-       MSC_DBG_ERR("start path: %s", start_path);
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((GET_FOLDER_LIST)func_array[lib_index][eGET_FOLDER_LIST])(handle[lib_index], start_path, &folder_list, &modified_time_list, &item_num_list, &count, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       return MS_MEDIA_ERR_INTERNAL;
-               }
-       }
-
-       MSC_DBG_ERR("OK");
-
-       *dir_array = g_array_new(FALSE, FALSE, sizeof(msc_dir_info_s*));
-       if (count != 0) {
-               for(i = 0; i < count; i ++) {
-                       dir_info = malloc(sizeof(msc_dir_info_s));
-                       if(dir_info) {
-                               memset(dir_info, 0, sizeof(msc_dir_info_s));
-                               dir_info->dir_path = strdup(folder_list[i]);
-                               dir_info->modified_time = modified_time_list[i];
-                               dir_info->item_num = item_num_list[i];
-                               g_array_append_val(*dir_array, dir_info);
-                               MS_SAFE_FREE(folder_list[i]);
-//                             MSC_DBG("%d get path : %s, %d", i, dir_info->dir_path, dir_info->modified_time);
-                       }
-               }
-       }
-
-       MS_SAFE_FREE(folder_list);
-       MS_SAFE_FREE(modified_time_list);
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int
-msc_update_folder_time(void **handle, char *folder_path, uid_t uid)
-{
-       int lib_index;
-       int ret;
-       char *err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((UPDATE_FOLDER_TIME)func_array[lib_index][eUPDATE_FOLDER_TIME])(handle[lib_index], folder_path, uid, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       return MS_MEDIA_ERR_INTERNAL;
-               }
-       }
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int
-msc_update_meta_batch(void **handle, const char *path, uid_t uid)
-{
-       int lib_index;
-       int res = MS_MEDIA_ERR_NONE;
-       int ret;
-       char *err_msg = NULL;
-       ms_storage_type_t storage_type;
-
-       storage_type = ms_get_storage_type_by_full(path, uid);
-
-       MSC_DBG_ERR("");
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((UPDATE_ITEM_META)func_array[lib_index][eUPDATE_ITEM_META])(handle[lib_index], path, storage_type, uid, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s] %s", g_array_index(so_array, char*, lib_index), err_msg, path);
-                       MS_SAFE_FREE(err_msg);
-                       res = MS_MEDIA_ERR_DB_INSERT_FAIL;
-               }
-       }
-
-       return res;
-}
-
-/****************************************************************************************************
-FOR BULK COMMIT
-*****************************************************************************************************/
-
-void
-msc_register_start(void **handle, ms_noti_status_e noti_status, int pid)
-{
-       int lib_index;
-       int ret = 0;
-       char *err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((INSERT_ITEM_BEGIN)func_array[lib_index][eINSERT_BEGIN])(handle[lib_index], MSC_REGISTER_COUNT, noti_status, pid, &err_msg);/*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-               }
-       }
-}
-
-void
-msc_register_end(void **handle, uid_t uid)
-{
-       int lib_index;
-       int ret = 0;
-       char *err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((INSERT_ITEM_END)func_array[lib_index][eINSERT_END])(handle[lib_index], uid, &err_msg);/*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-               }
-       }
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((UPDATE_END)func_array[lib_index][eUPDATE_END])(uid);/*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-               }
-       }
-}
-
-void
-msc_validate_start(void **handle)
-{
-       int lib_index;
-       int ret = 0;
-       char *err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((SET_ITEM_VALIDITY_BEGIN)func_array[lib_index][eSET_VALIDITY_BEGIN])(handle[lib_index], MSC_VALID_COUNT, &err_msg);/*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-               }
-       }
-}
-
-void
-msc_validate_end(void **handle, uid_t uid)
-{
-       int lib_index;
-       int ret = 0;
-       char *err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((SET_ITEM_VALIDITY_END)func_array[lib_index][eSET_VALIDITY_END])(handle[lib_index], uid, &err_msg);/*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-               }
-       }
-}
-
-void
-msc_update_start(void **handle)
-{
-       int lib_index;
-       int ret = 0;
-       char *err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((UPDATE_ITEM_BEGIN)func_array[lib_index][eUPDATE_ITEM_BEGIN])(handle[lib_index], MSC_VALID_COUNT, &err_msg);/*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-               }
-       }
-}
-
-void
-msc_update_end(void **handle, uid_t uid)
-{
-       int lib_index;
-       int ret = 0;
-       char *err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((UPDATE_ITEM_END)func_array[lib_index][eUPDATE_ITEM_END])(handle[lib_index], uid, &err_msg);/*dlopen*/
-               if (ret != 0) {
-                       MSC_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-               }
-       }
-}
-
diff --git a/src/scanner/media-scanner-device-block.c b/src/scanner/media-scanner-device-block.c
new file mode 100755 (executable)
index 0000000..5d10c79
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ *  Media Server
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+#include "media-util.h"
+
+#include "media-common-db-svc.h"
+#include "media-common-external-storage.h"
+
+#include "media-util-err.h"
+#include "media-scanner-dbg.h"
+#include "media-scanner-scan.h"
+#include "media-scanner-device-block.h"
+
+static void __msc_usb_remove_event(const char *mount_path)
+{
+       return;
+}
+
+int msc_mmc_remove_handler(const char *mount_path)
+{
+       return MS_MEDIA_ERR_NONE;
+}
+
+void _msc_mmc_changed_event(const char *mount_path, ms_stg_status_e mount_status)
+{
+       /* If scanner is not working, media server executes media scanner and sends request. */
+       /* If scanner is working, it detects changing status of SD card. */
+       if (mount_status == MS_STG_INSERTED) {
+               /*DO NOT THING*/
+       } else if (mount_status == MS_STG_REMOVED) {
+               msc_set_mmc_status(MS_STG_REMOVED);
+               msc_mmc_remove_handler(mount_path);
+       }
+
+       return;
+}
+
+
+void msc_device_block_changed_cb(const char *mount_path, int block_type, ms_stg_status_e mount_status, void *user_data)
+{
+       if (block_type == 0) {
+               MS_DBG_ERR("GET THE USB EVENT");
+               if (mount_status == MS_STG_INSERTED) {
+                       /*DO NOT THING*/
+               } else {
+                       __msc_usb_remove_event(mount_path);
+               }
+       } else {
+               MS_DBG_ERR("GET THE MMC EVENT");
+               _msc_mmc_changed_event(mount_path, mount_status);
+       }
+}
index 255c705..62e871f 100755 (executable)
 #include "media-server-ipc.h"
 #include "media-common-utils.h"
 #include "media-common-external-storage.h"
+#include "media-common-db-svc.h"
 #include "media-scanner-dbg.h"
-#include "media-scanner-db-svc.h"
 #include "media-scanner-socket.h"
 #include "media-scanner-scan.h"
 
-#define GLOBAL_USER    0 //#define     tzplatform_getenv(TZ_GLOBAL) //TODO
-
 typedef struct msc_scan_data {
        char *name;
        struct msc_scan_data *next;
@@ -50,7 +48,6 @@ bool power_off;
 GAsyncQueue * storage_queue;
 GAsyncQueue *scan_queue;
 GAsyncQueue *reg_queue;
-int insert_count;
 GMutex scan_req_mutex;
 GMutex blocked_mutex;
 char *g_cancel_path;
@@ -68,9 +65,10 @@ extern struct timeval g_mmc_end_time;
 static int __msc_set_power_mode(ms_db_status_type_t status);
 static int __msc_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type);
 static int __msc_check_stop_status(ms_storage_type_t storage_type);
-static int __msc_dir_scan(void **handle, const char*start_path, ms_storage_type_t storage_type, int scan_type, uid_t uid);
-static int __msc_db_update(void **handle, const ms_comm_msg_s * scan_data);
+static int __msc_dir_scan(void **handle, const char *storage_id, const char*start_path, ms_storage_type_t storage_type, int scan_type, uid_t uid);
+static int __msc_db_update(void **handle, const char *storage_id, const ms_comm_msg_s * scan_data);
 static int __msc_check_file_path(const char *file_path, uid_t uid);
+static bool __msc_check_folder_path(const char *folder_path);
 static int __msc_make_file_list(char *file_path, GArray **path_array, uid_t uid);
 static int __msc_batch_insert(ms_msg_type_e current_msg, int pid, GArray *path_array, uid_t uid);
 static int __msc_pop_register_request(GArray *register_array, ms_comm_msg_s **register_data);
@@ -80,7 +78,10 @@ static bool __msc_is_valid_path(const char *path, uid_t uid);
 static void __msc_check_dir_path(char *dir_path);
 static void __msc_insert_register_request(GArray *register_array, ms_comm_msg_s *insert_data);
 static void __msc_bacth_commit_enable(void* handle, bool ins_status, bool valid_status, bool noti_enable, int pid);
-static void __msc_bacth_commit_disable(void* handle, bool ins_status, bool valid_status, uid_t uid);
+static void __msc_bacth_commit_disable(void* handle, bool ins_status, bool valid_status, const char *path, uid_t uid);
+static int __msc_set_storage_scan_status(ms_storage_scan_status_e status);
+//static int __msc_get_storage_scan_status(ms_storage_scan_status_e *status, uid_t uid);
+//static bool __msc_storage_mount_status(const char* start_path);
 static char* __msc_get_path(uid_t uid);
 
 static char* __msc_get_path(uid_t uid)
@@ -92,7 +93,7 @@ static char* __msc_get_path(uid_t uid)
                result_psswd = strdup(MEDIA_ROOT_PATH_INTERNAL);
                grpinfo = getgrnam("users");
                if(grpinfo == NULL) {
-                       MSC_DBG_ERR("getgrnam(users) returns NULL !");
+                       MS_DBG_ERR("getgrnam(users) returns NULL !");
                        return NULL;
                }
     }
@@ -100,17 +101,17 @@ static char* __msc_get_path(uid_t uid)
        {
                struct passwd *userinfo = getpwuid(uid);
                if(userinfo == NULL) {
-                       MSC_DBG_ERR("getpwuid(%d) returns NULL !", uid);
+                       MS_DBG_ERR("getpwuid(%d) returns NULL !", uid);
                        return NULL;
                }
                grpinfo = getgrnam("users");
                if(grpinfo == NULL) {
-                       MSC_DBG_ERR("getgrnam(users) returns NULL !");
+                       MS_DBG_ERR("getgrnam(users) returns NULL !");
                        return NULL;
                }
                // Compare git_t type and not group name
                if (grpinfo->gr_gid != userinfo->pw_gid) {
-                       MSC_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
+                       MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
                        return NULL;
                }
                asprintf(&result_psswd, "%s/%s", userinfo->pw_dir, MEDIA_CONTENT_PATH);
@@ -136,7 +137,7 @@ static int __msc_set_power_mode(ms_db_status_type_t status)
                        res = MS_MEDIA_ERR_INTERNAL;
                break;
        default:
-               MSC_DBG_ERR("Unacceptable type : %d", status);
+               MS_DBG_ERR("Unacceptable type : %d", status);
                break;
        }
 
@@ -151,32 +152,32 @@ static int __msc_set_db_status(ms_db_status_type_t status, ms_storage_type_t sto
        if (status == MS_DB_UPDATING) {
                if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) {
                        res = MS_MEDIA_ERR_VCONF_SET_FAIL;
-                       MSC_DBG_ERR("ms_config_set_int failed");
+                       MS_DBG_ERR("ms_config_set_int failed");
                }
 
                if (storage_type == MS_STORAGE_EXTERNAL) {
                        if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING)) {
                                res = MS_MEDIA_ERR_VCONF_SET_FAIL;
-                               MSC_DBG_ERR("ms_config_set_int failed");
+                               MS_DBG_ERR("ms_config_set_int failed");
                        }
                }
        } else if (status == MS_DB_UPDATED) {
                if(!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS,  VCONFKEY_FILEMANAGER_DB_UPDATED)) {
                        res = MS_MEDIA_ERR_VCONF_SET_FAIL;
-                       MSC_DBG_ERR("ms_config_set_int failed");
+                       MS_DBG_ERR("ms_config_set_int failed");
                }
 
                if (storage_type == MS_STORAGE_EXTERNAL) {
                        if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED)) {
                                res = MS_MEDIA_ERR_VCONF_SET_FAIL;
-                               MSC_DBG_ERR("ms_config_set_int failed");
+                               MS_DBG_ERR("ms_config_set_int failed");
                        }
                }
        }
 
        err = __msc_set_power_mode(status);
        if (err != MS_MEDIA_ERR_NONE) {
-               MSC_DBG_ERR("__msc_set_power_mode fail");
+               MS_DBG_ERR("__msc_set_power_mode fail");
                res = err;
        }
 
@@ -188,17 +189,17 @@ static bool __msc_check_scan_ignore(char * path)
        DIR *dp = NULL;
        struct dirent entry;
        struct dirent *result;
-       char *ignore_path = ".scan_ignore";
+       const char *ignore_path = ".scan_ignore";
 
        if(strstr(path, "/."))
        {
-               MSC_DBG_ERR("hidden path");
-               return true;;
+               MS_DBG_ERR("hidden path");
+               return true;
        }
 
        dp = opendir(path);
        if (dp == NULL) {
-               MSC_DBG_ERR("%s folder opendir fails", path);
+               MS_DBG_ERR("%s folder opendir fails", path);
                return true;
        }
 
@@ -254,14 +255,14 @@ static int __msc_check_stop_status(ms_storage_type_t storage_type)
 
        /*check poweroff status*/
        if (power_off) {
-               MSC_DBG_ERR("Power off");
+               MS_DBG_ERR("Power off");
                ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
        }
 
        /*check SD card in out */
 #if 0
-       if ((mmc_state != VCONFKEY_SYSMAN_MMC_MOUNTED) && (storage_type == MS_STORAGE_EXTERNAL)) {
-               MSC_DBG_ERR("Directory scanning is stopped");
+       if ((mmc_state != MS_STG_INSERTED) && (storage_type == MS_STORAGE_EXTERNAL)) {
+               MS_DBG_ERR("Directory scanning is stopped");
                ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
        }
 #endif
@@ -278,7 +279,23 @@ static void __msc_check_dir_path(char *dir_path)
                dir_path[len -1] = '\0';
 }
 
-static int __msc_dir_scan(void **handle, const char*start_path, ms_storage_type_t storage_type, int scan_type, uid_t uid)
+static bool __msc_check_mount_storage(const char* start_path)
+{
+       bool ret = FALSE;
+       DIR *dp = NULL;
+
+       dp = opendir(start_path);
+
+       if (dp != NULL) {
+               ret = TRUE;
+               closedir(dp);
+               MS_DBG_ERR("STORAGE IS MOUNTED[%s]", start_path);
+       }
+
+       return ret;
+}
+
+static int __msc_dir_scan(void **handle, const char *storage_id, const char*start_path, ms_storage_type_t storage_type, int scan_type, uid_t uid)
 {
        DIR *dp = NULL;
        GArray *dir_array = NULL;
@@ -288,18 +305,42 @@ static int __msc_dir_scan(void **handle, const char*start_path, ms_storage_type_
        char *new_path = NULL;
        char *current_path = NULL;
        char path[MS_FILE_PATH_LEN_MAX] = { 0 };
-       int (*scan_function)(void **, const char*, uid_t) = NULL;
+       bool is_recursive = true;
+       char *new_start_path = NULL;
+       int (*scan_function)(void **, const char*, const char*, uid_t) = NULL;
 
        /* make new array for storing directory */
        dir_array = g_array_new (FALSE, FALSE, sizeof (char*));
        if (dir_array == NULL){
-               MSC_DBG_ERR("g_array_new failed");
+               MS_DBG_ERR("g_array_new failed");
                return MS_MEDIA_ERR_OUT_OF_MEMORY;
        }
+
        /* add first direcotiry to directory array */
+       new_start_path = strdup(start_path);
+       if (new_start_path == NULL) {
+               MS_DBG_ERR("g_array_new failed");
+               g_array_free(dir_array, FALSE);
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
+
        g_array_append_val (dir_array, start_path);
 
-       scan_function = (scan_type == MS_MSG_STORAGE_ALL) ? msc_insert_item_batch : msc_validate_item;
+       MS_DBG_ERR("[No-Error] start path [%s]", new_start_path);
+
+       scan_function = (scan_type == MS_MSG_STORAGE_ALL) ? ms_insert_item_batch : ms_validate_item;
+       is_recursive = (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) ? false : true;
+
+       /* folder validity set 0 under the start_path in folder table*/
+       if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE || scan_type == MS_MSG_DIRECTORY_SCANNING) {
+               if(ms_set_folder_validity(handle, storage_id, new_start_path, MS_INVALID, is_recursive, uid) != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("set_folder_validity failed [%d] ", scan_type);
+               }
+       }
+
+       if(__msc_check_folder_path(new_start_path)) {
+               ms_insert_folder(handle, storage_id, new_start_path, uid);
+       }
 
        /*start db update. the number of element in the array , db update is complete.*/
        while (dir_array->len != 0) {
@@ -311,14 +352,16 @@ static int __msc_dir_scan(void **handle, const char*start_path, ms_storage_type_
                /* get the current path from directory array */
                current_path = g_array_index(dir_array , char*, 0);
                g_array_remove_index (dir_array, 0);
-//             MSC_DBG_SLOG("%d", dir_array->len);
+//             MS_DBG_SLOG("%d", dir_array->len);
 
                if (__msc_check_scan_ignore(current_path)) {
-                       MSC_DBG_ERR("%s is ignore", current_path);
+                       MS_DBG_ERR("%s is ignore", current_path);
                        MS_SAFE_FREE(current_path);
                        continue;
                }
 
+               ms_insert_folder_start(handle);
+
                dp = opendir(current_path);
                if (dp != NULL) {
                        while (!readdir_r(dp, &entry, &result)) {
@@ -334,54 +377,80 @@ static int __msc_dir_scan(void **handle, const char*start_path, ms_storage_type_
                                if (entry.d_name[0] == '.')
                                        continue;
 
+                                if (ms_strappend(path, sizeof(path), "%s/%s", current_path, entry.d_name) != MS_MEDIA_ERR_NONE) {
+                                       MS_DBG_ERR("ms_strappend failed");
+                                       continue;
+                               }
+
                                if (entry.d_type & DT_REG) {
-                                        if (ms_strappend(path, sizeof(path), "%s/%s", current_path, entry.d_name) != MS_MEDIA_ERR_NONE) {
-                                               MSC_DBG_ERR("ms_strappend failed");
-                                               continue;
-                                       }
                                        /* insert into media DB */
-                                       if (scan_function(handle,path,uid) != MS_MEDIA_ERR_NONE) {
-                                               MSC_DBG_ERR("failed to update db : %d", scan_type);
+                                       if (scan_function(handle,storage_id, path, uid) != MS_MEDIA_ERR_NONE) {
+                                               MS_DBG_ERR("failed to update db : %d", scan_type);
                                                continue;
                                        }
                                } else if (entry.d_type & DT_DIR) {
                                        if  (scan_type != MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
                                                /* this request is recursive scanning */
-                                                if (ms_strappend(path, sizeof(path), "%s/%s", current_path, entry.d_name) != MS_MEDIA_ERR_NONE) {
-                                                       MSC_DBG_ERR("ms_strappend failed");
-                                                       continue;
-                                               }
                                                /* add new directory to dir_array */
                                                new_path = strdup(path);
-                                               g_array_append_val (dir_array, new_path);
+                                               if (new_path != NULL) {
+                                                       g_array_append_val (dir_array, new_path);
+
+                                                       if(ms_insert_folder(handle, storage_id, new_path, uid) != MS_MEDIA_ERR_NONE) {
+                                                               MS_DBG_ERR("insert folder failed");
+                                                       }
+                                               } else {
+                                                       MS_DBG_ERR("strdup failed");
+                                                       continue;
+                                               }
                                        } else {
                                                /* this request is recursive scanning */
                                                /* don't add new directory to dir_array */
+                                               if(ms_insert_folder(handle, storage_id, path, uid) != MS_MEDIA_ERR_NONE) {
+                                                       MS_DBG_ERR("insert folder failed");
+                                                       continue;
+                                               }
                                        }
                                }
                        }
                        /* update modified time of directory */
-                       if (scan_type == MS_MSG_STORAGE_PARTIAL
-                               && storage_type == MS_STORAGE_INTERNAL)
-                               msc_update_folder_time(handle, current_path, uid);
+                       if (scan_type == MS_MSG_STORAGE_PARTIAL && storage_type == MS_STORAGE_INTERNAL)
+                               ms_update_folder_time(handle, INTERNAL_STORAGE_ID, current_path, uid);
                } else {
-                       MSC_DBG_ERR("%s folder opendir fails", current_path);
+                       MS_DBG_ERR("%s folder opendir fails", current_path);
                }
                if (dp) closedir(dp);
                dp = NULL;
+
+               ms_insert_folder_end(handle, uid);
+
                MS_SAFE_FREE(current_path);
        }               /*db update while */
+
+               /*remove invalid folder in folder table.*/
+       if (scan_type == MS_MSG_STORAGE_ALL || scan_type == MS_MSG_STORAGE_PARTIAL) {
+               if (__msc_check_mount_storage(new_start_path)) {
+                       if(ms_delete_invalid_folder(handle, storage_id, uid) != MS_MEDIA_ERR_NONE) {
+                               MS_DBG_ERR("delete invalid folder failed");
+                               ret =  MS_MEDIA_ERR_DB_DELETE_FAIL;
+                       }
+               } else {
+                       MS_DBG_ERR("start path is unmounted");
+               }
+       }
 STOP_SCAN:
        if (dp) closedir(dp);
 
+       MS_SAFE_FREE(new_start_path);
+
        __msc_clear_file_list(dir_array);
 
-       if (ret != MS_MEDIA_ERR_NONE) MSC_DBG_INFO("ret : %d", ret);
+       if (ret != MS_MEDIA_ERR_NONE) MS_DBG_INFO("ret : %d", ret);
 
        return ret;
 }
 
-static int __msc_db_update(void **handle, const ms_comm_msg_s * scan_data)
+static int __msc_db_update(void **handle, const char *storage_id, const ms_comm_msg_s * scan_data)
 {
        int scan_type;
        int err = MS_MEDIA_ERR_NONE;
@@ -394,20 +463,23 @@ static int __msc_db_update(void **handle, const ms_comm_msg_s * scan_data)
 
        /*if scan type is not MS_SCAN_NONE, check data in db. */
        if (scan_type != MS_MSG_STORAGE_INVALID) {
-               MSC_DBG_INFO("INSERT");
+               MS_DBG_INFO("INSERT");
 
-               err = __msc_dir_scan(handle, start_path, storage_type, scan_type, scan_data->uid);
+               err = __msc_dir_scan(handle, storage_id, start_path, storage_type, scan_type, scan_data->uid);
                if (err != MS_MEDIA_ERR_NONE) {
-                       MSC_DBG_ERR("error : %d", err);
+                       MS_DBG_ERR("error : %d", err);
                }
        } else if (scan_type == MS_MSG_STORAGE_INVALID) {
-               MSC_DBG_INFO("INVALID");
-
-               err = msc_set_folder_validity(handle, start_path, false, true, scan_data->uid);
+               MS_DBG_INFO("INVALID");
+               /*In this case, update just validation record*/
+               /*update just valid type*/
+               err = ms_validaty_change_all_items(handle, storage_id, storage_type, false, scan_data->uid);
                if (err != MS_MEDIA_ERR_NONE) {
-                       MSC_DBG_ERR("error : %d", err);
+                       MS_DBG_ERR("error : %d", err);
                }
 
+               ms_set_folder_validity(handle, MMC_STORAGE_ID, start_path, 0, TRUE, scan_data->uid);
+
                MS_SAFE_FREE(start_path);
        }
 
@@ -424,26 +496,47 @@ gboolean msc_directory_scan_thread(void *data)
        void **handle = NULL;
        int scan_type;
        char *noti_path = NULL;
+       char *storage_id = NULL;
 
        while (1) {
                scan_data = g_async_queue_pop(scan_queue);
                if (scan_data->pid == POWEROFF) {
-                       MSC_DBG_ERR("power off");
+                       MS_DBG_ERR("power off");
                        goto _POWEROFF;
                }
 
-               MSC_DBG_INFO("DIRECTORY SCAN START [%s]", scan_data->msg);
+               MS_DBG_ERR("DIRECTORY SCAN START [%s %d]", scan_data->msg, scan_data->msg_type);
 
                /*connect to media db, if conneting is failed, db updating is stopped*/
-               err = msc_connect_db(&handle, scan_data->uid);
+               err = ms_connect_db(&handle, scan_data->uid);
                if (err != MS_MEDIA_ERR_NONE)
                        continue;
 
                scan_type = scan_data->msg_type;
 
+               if (strlen(scan_data->storage_id) > 0)
+                       storage_id = strdup(scan_data->storage_id);
+               else
+                       storage_id = strdup("media");
+
+               //storage_id = strdup(scan_data->storage_id);
+               if (storage_id == NULL) {
+                       MS_DBG_ERR("storage_id NULL");
+                       ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+                       goto NEXT;
+               }
+
+               MS_DBG("path : [%s], storage_id : [%s]", scan_data->msg, storage_id);
+
+               if (strlen(storage_id) == 0) {
+                       MS_DBG_ERR("storage_id length is 0. There is no information of your request [%s]", scan_data->msg);
+                       ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+                       goto NEXT;
+               }
+
                if (scan_type != MS_MSG_DIRECTORY_SCANNING
                        && scan_type != MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
-                       MSC_DBG_ERR("Invalid request");
+                       MS_DBG_ERR("Invalid request");
                        ret = MS_MEDIA_ERR_INVALID_PARAMETER;
                        goto NEXT;
                }
@@ -452,48 +545,53 @@ gboolean msc_directory_scan_thread(void *data)
 
                /*change validity before scanning*/
                if (scan_type == MS_MSG_DIRECTORY_SCANNING)
-                       err = msc_set_folder_validity(handle, scan_data->msg, MS_INVALID, MS_RECURSIVE,scan_data->uid);
+                       err = ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_INVALID, MS_RECURSIVE, scan_data->uid);
                else
-                       err = msc_set_folder_validity(handle, scan_data->msg, MS_INVALID, MS_NON_RECURSIVE,scan_data->uid);
+                       err = ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_INVALID, MS_NON_RECURSIVE, scan_data->uid);
                if (err != MS_MEDIA_ERR_NONE)
-                       MSC_DBG_ERR("error : %d", err);
+                       MS_DBG_ERR("error : %d", err);
 
                /*call for bundle commit*/
-               __msc_bacth_commit_enable(handle, TRUE, TRUE, MS_NOTI_DISABLE, 0);
+               __msc_bacth_commit_enable(handle, TRUE, TRUE, MS_NOTI_SWITCH_OFF, 0);
 
                /*insert data into media db */
-               ret = __msc_db_update(handle, scan_data);
+               ret = __msc_db_update(handle, storage_id, scan_data);
 
                /*call for bundle commit*/
-               __msc_bacth_commit_disable(handle, TRUE, TRUE,scan_data->uid);
+               __msc_bacth_commit_disable(handle, TRUE, TRUE, scan_data->msg, scan_data->uid);
 
                if (ret == MS_MEDIA_ERR_NONE) {
-                       MSC_DBG_INFO("working normally");
+                       MS_DBG_INFO("working normally");
                        int count = 0;
+                       bool is_recursive = true;
+                       int insert_count = ms_get_insert_count();
 
                        noti_path = strndup(scan_data->msg, scan_data->msg_size);
-                       msc_count_delete_items_in_folder(handle, noti_path, &count);
+                       ms_count_delete_items_in_folder(handle, storage_id, noti_path, &count);
 
-                       MSC_DBG_SLOG("delete count %d", count);
-                       MSC_DBG_SLOG("insert count %d", insert_count);
+                       MS_DBG_SLOG("delete count %d", count);
+                       MS_DBG_SLOG("insert count %d", insert_count);
 
-                       msc_delete_invalid_items_in_folder(handle, scan_data->msg,scan_data->uid);
+                       if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE)
+                               is_recursive = false;
+
+                       ms_delete_invalid_items_in_folder(handle, storage_id, scan_data->msg, is_recursive, scan_data->uid);
 
                        if ( !(count == 0 && insert_count == 0)) {
-                               msc_send_dir_update_noti(handle, noti_path);
+                               ms_send_dir_update_noti(handle, storage_id, noti_path);
                        }
                        MS_SAFE_FREE(noti_path);
                }
 
-               insert_count = 0;
+               ms_reset_insert_count();
 
                if (power_off) {
-                       MSC_DBG_WAN("power off");
+                       MS_DBG_WARN("power off");
                        goto _POWEROFF;
                }
 
                /*disconnect from media db*/
-               if (handle) msc_disconnect_db(&handle);
+               if (handle) ms_disconnect_db(&handle);
 NEXT:
                /*Active flush */
                malloc_trim(0);
@@ -501,13 +599,14 @@ NEXT:
                msc_send_result(ret, scan_data);
 
                MS_SAFE_FREE(scan_data);
+               MS_SAFE_FREE(storage_id);
 
-               MSC_DBG_INFO("DIRECTORY SCAN END [%d]", ret);
+               MS_DBG_INFO("DIRECTORY SCAN END [%d]", ret);
        }                       /*thread while*/
 
 _POWEROFF:
        MS_SAFE_FREE(scan_data);
-       if (handle) msc_disconnect_db(&handle);
+       if (handle) ms_disconnect_db(&handle);
 
        return false;
 }
@@ -515,9 +614,9 @@ _POWEROFF:
 /* this thread process only the request of media-server */
 static int _check_folder_from_list(char *folder_path, int item_num, GArray *dir_array)
 {
-       int i;
+       int i = 0;
        int array_len = dir_array->len;
-       msc_dir_info_s* dir_info = NULL;
+       ms_dir_info_s* dir_info = NULL;
        struct stat buf;
        time_t mtime;
        bool find_flag = false;
@@ -529,7 +628,7 @@ static int _check_folder_from_list(char *folder_path, int item_num, GArray *dir_
        }
 
        for (i = 0; i < array_len; i++) {
-               dir_info = g_array_index (dir_array, msc_dir_info_s*, i);
+               dir_info = g_array_index (dir_array, ms_dir_info_s*, i);
                if (strcmp(folder_path, dir_info->dir_path) == 0) {
                        /* if modified time is same, the folder does not need updating */
                        if ((mtime == dir_info->modified_time) && (item_num == dir_info->item_num)) {
@@ -545,16 +644,15 @@ static int _check_folder_from_list(char *folder_path, int item_num, GArray *dir_
                }
        }
 
-       /* this folder does not exist in media DB, so this folder has to insert to DB */
-       if ((find_flag == false) &&
-               (item_num > 0)) {
+       /* this folder does not exist in media DB, so this folder has to be inserted to DB */
+       if (find_flag == false) {
                dir_info = NULL;
-               dir_info = malloc(sizeof(msc_dir_info_s));
+               dir_info = malloc(sizeof(ms_dir_info_s));
                if (dir_info == NULL) {
-                       MSC_DBG_ERR("MALLOC failed");
+                       MS_DBG_ERR("MALLOC failed");
                        return MS_MEDIA_ERR_OUT_OF_MEMORY;
                }
-               memset(dir_info, 0, sizeof(msc_dir_info_s));
+               memset(dir_info, 0, sizeof(ms_dir_info_s));
                dir_info->dir_path = strdup(folder_path);
                dir_info->modified_time = -1;
                g_array_append_val(dir_array, dir_info);
@@ -563,7 +661,7 @@ static int _check_folder_from_list(char *folder_path, int item_num, GArray *dir_
        return MS_MEDIA_ERR_NONE;
 }
 
-static int __msc_compare_with_db(void **handle, const char*update_path, int scan_type, GArray **dir_array)
+static int __msc_compare_with_db(void **handle, const char *storage_id, const char*update_path, int scan_type, GArray **dir_array)
 {
        DIR *dp = NULL;
        GArray *read_dir_array = NULL;
@@ -576,19 +674,20 @@ static int __msc_compare_with_db(void **handle, const char*update_path, int scan
        char * start_path = strdup(update_path);
        int item_num = 0;
 
+       MS_DBG_FENTER();
+
        /*get directories list from media db*/
-       ret = msc_get_folder_list(handle, start_path, dir_array);
+       ret = ms_get_folder_list(handle, storage_id, start_path, dir_array);
        if (ret != MS_MEDIA_ERR_NONE) {
                MS_SAFE_FREE(start_path);
-               MSC_DBG_ERR("msc_get_folder_list is failed", ret);
+               MS_DBG_ERR("ms_get_folder_list is failed", ret);
                return ret;
        }
-       
-       MSC_DBG_ERR(" ");
+
        /* make new array for storing directory */
        read_dir_array = g_array_new (FALSE, FALSE, sizeof (char*));
        if (read_dir_array == NULL){
-               MSC_DBG_ERR("g_array_new failed");
+               MS_DBG_ERR("g_array_new failed");
                return MS_MEDIA_ERR_OUT_OF_MEMORY;
        }
        /* add first direcotiry to directory array */
@@ -602,7 +701,7 @@ static int __msc_compare_with_db(void **handle, const char*update_path, int scan
 //             MSC_DBG_ERR("%s", current_path);
 
                if (__msc_check_scan_ignore(current_path)) {
-                       MSC_DBG_ERR("%s is ignore", current_path);
+                       MS_DBG_ERR("%s is ignore", current_path);
                        MS_SAFE_FREE(current_path);
                        continue;
                }
@@ -619,7 +718,7 @@ static int __msc_compare_with_db(void **handle, const char*update_path, int scan
 
                                 if (entry.d_type & DT_DIR) {
                                         if (ms_strappend(path, sizeof(path), "%s/%s", current_path, entry.d_name) != MS_MEDIA_ERR_NONE) {
-                                               MSC_DBG_ERR("ms_strappend failed");
+                                               MS_DBG_ERR("ms_strappend failed");
                                                continue;
                                        }
                                        /* add new directory to dir_array */
@@ -634,7 +733,7 @@ static int __msc_compare_with_db(void **handle, const char*update_path, int scan
                        _check_folder_from_list(current_path, item_num, *dir_array);
                        item_num = 0;
                } else {
-                       MSC_DBG_ERR("%s folder opendir fails", current_path);
+                       MS_DBG_ERR("%s folder opendir fails", current_path);
                }
                if (dp) closedir(dp);
                dp = NULL;
@@ -643,43 +742,43 @@ static int __msc_compare_with_db(void **handle, const char*update_path, int scan
 
        __msc_clear_file_list(read_dir_array);
 
-       MSC_DBG_INFO("ret : %d", ret);
-       MSC_DBG_INFO("update count : %d", (*dir_array)->len);
+       MS_DBG_INFO("ret : %d", ret);
+       MS_DBG_INFO("update count : %d", (*dir_array)->len);
 
        return ret;
 }
 
-static int _msc_db_update_partial(void **handle, ms_storage_type_t storage_type, GArray *dir_array, uid_t uid)
+static int _msc_db_update_partial(void **handle, const char *storage_id, ms_storage_type_t storage_type, GArray *dir_array, uid_t uid)
 {
-       int i;
+       unsigned int i;
        int err = MS_MEDIA_ERR_NONE;
-       msc_dir_info_s* dir_info = NULL;
+       ms_dir_info_s* dir_info = NULL;
        char *update_path = NULL;
 
        for (i = 0; i < dir_array->len; i ++) {
-               dir_info = g_array_index (dir_array, msc_dir_info_s*, i);
+               dir_info = g_array_index (dir_array, ms_dir_info_s*, i);
                update_path = strdup(dir_info->dir_path);
 
-//             MSC_DBG_SLOG("update_path : %s, %d", update_path, dir_info->modified_time);
+//             MS_DBG_SLOG("update_path : %s, %d", update_path, dir_info->modified_time);
                if (dir_info->modified_time != -1) {
-                       err = msc_set_folder_validity(handle, update_path, MS_INVALID, MS_NON_RECURSIVE, uid);
+                       err = ms_set_folder_item_validity(handle, storage_id, update_path, MS_INVALID, MS_NON_RECURSIVE, uid);
                        if (err != MS_MEDIA_ERR_NONE) {
-                               MSC_DBG_SLOG("update_path : %s, %d", update_path, dir_info->modified_time);
-                               MSC_DBG_ERR("error : %d", err);
+                               MS_DBG_SLOG("update_path : %s, %d", update_path, dir_info->modified_time);
+                               MS_DBG_ERR("error : %d", err);
                        }
                }
 
-               __msc_dir_scan(handle, update_path, storage_type, MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE, uid);
+               __msc_dir_scan(handle, storage_id, update_path, storage_type, MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE, uid);
 
 //             if (dir_info->modified_time != -1) {
-//                     msc_update_folder_time(handle, tmp_path);
+//                     ms_update_folder_time(handle, tmp_path);
 //             }
        }
 
        /*delete all node*/
        while(dir_array->len != 0) {
-               msc_dir_info_s *data = NULL;
-               data = g_array_index(dir_array , msc_dir_info_s*, 0);
+               ms_dir_info_s *data = NULL;
+               data = g_array_index(dir_array , ms_dir_info_s*, 0);
                g_array_remove_index (dir_array, 0);
                MS_SAFE_FREE(data->dir_path);
                MS_SAFE_FREE(data);
@@ -701,112 +800,137 @@ gboolean msc_storage_scan_thread(void *data)
        bool valid_status = TRUE;
        char *update_path = NULL;
        GArray *dir_array = NULL;
+       char *storage_id = NULL;
 
        while (1) {
+               __msc_set_storage_scan_status(MS_STORAGE_SCAN_PREPARE);
                scan_data = g_async_queue_pop(storage_queue);
                if (scan_data->pid == POWEROFF) {
-                       MSC_DBG_WAN("power off");
+                       MS_DBG_WARN("power off");
                        goto _POWEROFF;
                }
 
-               MSC_DBG_INFO("STORAGE SCAN START [%s]", scan_data->msg);
+               __msc_set_storage_scan_status(MS_STORAGE_SCAN_PROCESSING);
+               MS_DBG_ERR("[No-Error] STORAGE SCAN START [%s][%s]", scan_data->msg, scan_data->storage_id);
 
                scan_type = scan_data->msg_type;
                if (scan_type != MS_MSG_STORAGE_ALL
                        && scan_type != MS_MSG_STORAGE_PARTIAL
                        && scan_type != MS_MSG_STORAGE_INVALID) {
-                       MSC_DBG_ERR("Invalid request");
+                       MS_DBG_ERR("Invalid request[%d]", scan_type);
                        ret = MS_MEDIA_ERR_INVALID_PARAMETER;
                        goto NEXT;
                }
 
                /*connect to media db, if conneting is failed, db updating is stopped*/
-               err = msc_connect_db(&handle,scan_data->uid);
+               err = ms_connect_db(&handle, scan_data->uid);
                if (err != MS_MEDIA_ERR_NONE)
                        continue;
 
                storage_type = ms_get_storage_type_by_full(scan_data->msg, scan_data->uid);
                update_path = strndup(scan_data->msg, scan_data->msg_size);
 
+               if (strlen(scan_data->storage_id) > 0)
+                       storage_id = strdup(scan_data->storage_id);
+               else
+                       storage_id = strdup("media");
+
+               ms_set_storage_scan_status(handle, storage_id, MEDIA_SCAN_PROCESSING, scan_data->uid);
+
                /*start db updating */
                __msc_set_db_status(MS_DB_UPDATING, storage_type);
 
                valid_status = (scan_type == MS_MSG_STORAGE_PARTIAL) ? TRUE : FALSE;
-               __msc_bacth_commit_enable(handle, TRUE, valid_status, MS_NOTI_DISABLE, 0);
+               __msc_bacth_commit_enable(handle, TRUE, valid_status, MS_NOTI_SWITCH_OFF, 0);
 
 #ifdef FMS_PERF
                ms_check_start_time(&g_mmc_start_time);
 #endif
 
                if (scan_type == MS_MSG_STORAGE_PARTIAL && storage_type == MS_STORAGE_INTERNAL) {
-                       msc_validaty_change_all_items(handle, storage_type, true, scan_data->uid);
+                       ms_validaty_change_all_items(handle, storage_id, storage_type, true, scan_data->uid);
 
                        /* find and compare modified time */
-                       ret = __msc_compare_with_db(handle, update_path, scan_data->msg_type, &dir_array);
+                       ret = __msc_compare_with_db(handle, storage_id, update_path, scan_data->msg_type, &dir_array);
                        if (ret != MS_MEDIA_ERR_NONE) {
-                               MSC_DBG_ERR("__msc_compare_with_db is falied");
+                               MS_DBG_ERR("__msc_compare_with_db is falied");
                                goto NEXT;
                        }
 
                        if (dir_array->len != 0) {
-                               MSC_DBG_INFO("DB UPDATING IS NEEDED");
+                               MS_DBG_INFO("DB UPDATING IS NEEDED");
 
-                               ret = _msc_db_update_partial(handle, storage_type, dir_array, scan_data->uid);
+                               ret = _msc_db_update_partial(handle, storage_id, storage_type, dir_array, scan_data->uid);
                        } else {
-                               MSC_DBG_INFO("THERE IS NO UPDATE");
+                               MS_DBG_INFO("THERE IS NO UPDATE");
                        }
                } else {
                        if (scan_type == MS_MSG_STORAGE_ALL) {
                                /*  Delete all data before full scanning */
-                               if (!msc_delete_invalid_items(handle, storage_type, scan_data->uid)) {
-                                       MSC_DBG_ERR("msc_delete_all_record fails");
+                               if (!ms_delete_all_items(handle, storage_id, storage_type, scan_data->uid)) {
+                                       MS_DBG_ERR("msc_delete_all_record fails");
                                }
 
                                if (storage_type == MS_STORAGE_EXTERNAL) {
-                                       ms_update_mmc_info();
+                                       /*storage info updated in media-server*/
+                                       /*ms_update_mmc_info();*/
                                }
                        } else if (scan_type == MS_MSG_STORAGE_PARTIAL) {
-                               msc_validaty_change_all_items(handle, storage_type, false, scan_data->uid);
+                               ms_validaty_change_all_items(handle, storage_id, storage_type, false, scan_data->uid);
                        }
 
-                       ret = __msc_db_update(handle, scan_data);
+                       ret = __msc_db_update(handle, storage_id, scan_data);
                }
 
                /*call for bundle commit*/
-               __msc_bacth_commit_disable(handle, TRUE, valid_status,scan_data->uid);
+               __msc_bacth_commit_disable(handle, TRUE, valid_status, scan_data->msg, scan_data->uid);
 
-               if (scan_type == MS_MSG_STORAGE_PARTIAL) {
+               if (scan_type == MS_MSG_STORAGE_PARTIAL && ret == MS_MEDIA_ERR_NONE) {
                        int del_count = 0;
 
                        /*check delete count*/
-                       MSC_DBG_INFO("update path : %s", update_path);
-                       msc_count_delete_items_in_folder(handle, update_path, &del_count);
+                       MS_DBG_INFO("update path : %s", update_path);
+                       ms_count_delete_items_in_folder(handle, storage_id, update_path, &del_count);
 
                        /*if there is no delete content, do not call delete API*/
-                       if (del_count != 0)
-                               msc_delete_invalid_items(handle, storage_type, scan_data->uid);
+                       if (del_count != 0) {
+                               MS_DBG_ERR("storage thread delete count [%d]", del_count);
+                               ms_delete_invalid_items(handle, storage_id, storage_type, scan_data->uid);
+                       }
                }
 
                /* send notification */
-               msc_send_dir_update_noti(handle, update_path);
-               MS_SAFE_FREE(update_path);
+               ms_send_dir_update_noti(handle,  storage_id, update_path);
 
 #ifdef FMS_PERF
                ms_check_end_time(&g_mmc_end_time);
                ms_check_time_diff(&g_mmc_start_time, &g_mmc_end_time);
 #endif
 
+               if (ret == MS_MEDIA_ERR_SCANNER_FORCE_STOP) {
+                       ms_set_storage_scan_status(handle, storage_id, MEDIA_SCAN_STOP, scan_data->uid);
+                       __msc_set_storage_scan_status(MS_STORAGE_SCAN_STOP);
+                       /*set vconf key mmc loading for indicator */
+                       __msc_set_db_status(MS_DB_STOPPED, storage_type);
+               } else {
+                       ms_set_storage_scan_status(handle, storage_id, MEDIA_SCAN_COMPLETE, scan_data->uid);
+                       __msc_set_storage_scan_status(MS_STORAGE_SCAN_COMPLETE);
+                       /*set vconf key mmc loading for indicator */
+                       __msc_set_db_status(MS_DB_UPDATED, storage_type);
+               }
+
 NEXT:
-               /*set vconf key mmc loading for indicator */
-               __msc_set_db_status(MS_DB_UPDATED, storage_type);
+
+               MS_SAFE_FREE(update_path);
+               MS_SAFE_FREE(storage_id);
 
                if (power_off) {
-                       MSC_DBG_ERR("[No-error] power off");
+                       MS_DBG_ERR("[No-Error] power off");
                        goto _POWEROFF;
                }
 
                /*disconnect from media db*/
-               if (handle)     msc_disconnect_db(&handle);
+               if (handle) ms_disconnect_db(&handle);
 
                /*Active flush */
                malloc_trim(0);
@@ -815,19 +939,19 @@ NEXT:
 
                MS_SAFE_FREE(scan_data);
 
-               MSC_DBG_INFO("STORAGE SCAN END[%d]", ret);
+               MS_DBG_ERR("STORAGE SCAN END[%d]", ret);
        }                       /*thread while*/
 
 _POWEROFF:
        MS_SAFE_FREE(scan_data);
-       if (handle) msc_disconnect_db(&handle);
+       if (handle) ms_disconnect_db(&handle);
 
        return false;
 }
 
 static void __msc_insert_register_request(GArray *register_array, ms_comm_msg_s *insert_data)
 {
-       MSC_DBG_SLOG("path : %s", insert_data->msg);
+       MS_DBG_SLOG("path : %s", insert_data->msg);
 
        if (insert_data->pid == POWEROFF) {
                g_array_prepend_val(register_array, insert_data);
@@ -870,14 +994,15 @@ static int __msc_check_file_path(const char *file_path, uid_t uid)
        /* check location of file */
        /* file must exists under "/opt/usr/media" or "/opt/storage/sdcard" */
        if(!__msc_is_valid_path(file_path, uid)) {
-               MSC_DBG_ERR("Invalid path : %s", file_path);
+               MS_DBG_ERR("Invalid path : %s", file_path);
                return MS_MEDIA_ERR_INVALID_PATH;
        }
 
        /* check the file exits actually */
        exist = open(file_path, O_RDONLY);
        if(exist < 0) {
-               MSC_DBG_ERR("error [%s, %s]", file_path, strerror(errno));
+               MS_DBG_STRERROR("Open failed");
+               MS_DBG_ERR("error path [%s]", file_path);
                return MS_MEDIA_ERR_INVALID_PATH;
        }
        close(exist);
@@ -888,17 +1013,31 @@ static int __msc_check_file_path(const char *file_path, uid_t uid)
        if(stat(file_path, &file_st) == 0) {
                if(!S_ISREG(file_st.st_mode)) {
                        /* In this case, it is not a regula file */
-                       MSC_DBG_ERR("this path is not a file");
+                       MS_DBG_ERR("this path is not a file");
                        return MS_MEDIA_ERR_INVALID_PATH;
                }
        } else {
-               MSC_DBG_ERR("stat failed [%s]", strerror(errno));
+               MS_DBG_STRERROR("stat failed");
                return MS_MEDIA_ERR_INVALID_PATH;
        }
 
        return MS_MEDIA_ERR_NONE;
 }
 
+static bool __msc_check_folder_path(const char *folder_path)
+{
+       DIR *dp = NULL;
+
+       dp = opendir(folder_path);
+       if (dp == NULL) {
+               MS_DBG_ERR("Deleted folder path");
+               return false;
+       }
+       closedir(dp);
+
+       return true;
+}
+
 static int __msc_clear_file_list(GArray *path_array)
 {
        if (path_array) {
@@ -923,13 +1062,13 @@ static int __msc_check_ignore_dir(const char *full_path, uid_t uid)
 
        ret = __msc_check_file_path(full_path, uid);
        if (ret != MS_MEDIA_ERR_NONE) {
-               MSC_DBG_ERR("invalid path : %s", full_path);
+               MS_DBG_ERR("invalid path : %s", full_path);
                return MS_MEDIA_ERR_INVALID_PATH;
        }
 
        dir_path = g_path_get_dirname(full_path);
        if (strcmp(dir_path, ".") == 0) {
-               MSC_DBG_ERR("getting directory path is failed : %s", full_path);
+               MS_DBG_ERR("getting directory path is failed : %s", full_path);
                MS_SAFE_FREE(dir_path);
                return MS_MEDIA_ERR_INVALID_PATH;
        }
@@ -945,13 +1084,15 @@ static int __msc_check_ignore_dir(const char *full_path, uid_t uid)
                        break;
                else if(strcmp(dir_path, MEDIA_ROOT_PATH_SDCARD) == 0)
                        break;
+               else if(strcmp(dir_path, MEDIA_ROOT_PATH_USB) == 0)
+                       break;
 
                leaf_path = strrchr(dir_path, '/');
                if(leaf_path != NULL) {
                                int seek_len = leaf_path -dir_path;
                                dir_path[seek_len] = '\0';
                } else {
-                       MSC_DBG_ERR("Fail to find leaf path");
+                       MS_DBG_ERR("Fail to find leaf path");
                        ret = MS_MEDIA_ERR_INVALID_PATH;
                        break;
                }
@@ -974,7 +1115,7 @@ static int __msc_make_file_list(char *file_path, GArray **path_array, uid_t uid)
        /* load the file list from file */
        fp = fopen(file_path, "rt");
        if (fp == NULL) {
-               MSC_DBG_STRERROR("fopen failed");
+               MS_DBG_STRERROR("fopen failed");
                res = MS_MEDIA_ERR_FILE_OPEN_FAIL;
                goto FREE_RESOURCE;
        }
@@ -983,7 +1124,7 @@ static int __msc_make_file_list(char *file_path, GArray **path_array, uid_t uid)
        /* This is an array for storing the path of insert datas*/
        *path_array = g_array_new (FALSE, FALSE, sizeof (char *));
        if (*path_array == NULL) {
-               MSC_DBG_ERR("g_array_new failed");
+               MS_DBG_ERR("g_array_new failed");
                res = MS_MEDIA_ERR_OUT_OF_MEMORY;
                goto FREE_RESOURCE;
        }
@@ -996,13 +1137,13 @@ static int __msc_make_file_list(char *file_path, GArray **path_array, uid_t uid)
                /* check valid path */
                ret = __msc_check_ignore_dir(path, uid);
                if (ret != MS_MEDIA_ERR_NONE) {
-                       MSC_DBG_ERR("invalide path : %s", path);
+                       MS_DBG_ERR("invalide path : %s", path);
                        MS_SAFE_FREE(path);
                        continue;
                }
                /* insert getted path to the list */
                if (g_array_append_val(*path_array, path)  == NULL) {
-                       MSC_DBG_ERR("g_array_append_val failed");
+                       MS_DBG_ERR("g_array_append_val failed");
                        res = MS_MEDIA_ERR_OUT_OF_MEMORY;
                        goto FREE_RESOURCE;
                }
@@ -1026,45 +1167,53 @@ FREE_RESOURCE:
 static int __msc_batch_insert(ms_msg_type_e current_msg, int pid, GArray *path_array, uid_t uid)
 {
        int err;
-       int i;
+       unsigned int i;
        void **handle = NULL;
        char *insert_path = NULL;
-       int (*insert_function)(void **, const char*, uid_t) = NULL;
+       int (*insert_function)(void **, const char*, const char*, uid_t) = NULL;
+       char storage_id[MS_UUID_SIZE] = {0,};
 
-       insert_function = (current_msg == MS_MSG_BULK_INSERT) ? msc_insert_item_batch : msc_insert_burst_item;
+       insert_function = (current_msg == MS_MSG_BULK_INSERT) ? ms_insert_item_batch : ms_insert_burst_item;
 
        /* connect to media db, if conneting is failed, db updating is stopped */
-       err = msc_connect_db(&handle, uid);
+       err = ms_connect_db(&handle, uid);
        if (err != MS_MEDIA_ERR_NONE)
                return MS_MEDIA_ERR_DB_CONNECT_FAIL;
 
        /*start db updating */
        /*call for bundle commit*/
-       __msc_bacth_commit_enable(handle, TRUE, FALSE, MS_NOTI_ENABLE, pid);
+       __msc_bacth_commit_enable(handle, TRUE, FALSE, MS_NOTI_SWITCH_ON, pid);
 
-       MSC_DBG_ERR("BULK REGISTER START");
+       MS_DBG_ERR("BULK REGISTER START[%d]", pid);
 
        /* get the inserting file path from array  and insert to db */
        for (i = 0; i < path_array->len; 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) < 0) {
+                       MS_DBG_ERR("There is no storage id in media db");
+                       continue;
+               }
+
                /* insert to db */
-               err = insert_function(handle, insert_path, uid);
+               err = insert_function(handle, storage_id, insert_path, uid);
 
                if (power_off) {
-                       MSC_DBG_ERR("power off");
+                       MS_DBG_ERR("power off");
                        /*call for bundle commit*/
-                       msc_register_end(handle, uid);
+                       ms_register_end(handle, NULL, uid);
                        break;
                }
        }
 
        /*call for bundle commit*/
-       __msc_bacth_commit_disable(handle, TRUE, FALSE, uid);
+       __msc_bacth_commit_disable(handle, TRUE, FALSE, NULL, uid);
 
        /*disconnect form media db*/
-       if (handle) msc_disconnect_db(&handle);
+       if (handle) ms_disconnect_db(&handle);
 
        return MS_MEDIA_ERR_NONE;
 }
@@ -1095,7 +1244,7 @@ static int __msc_pop_register_request(GArray *register_array, ms_comm_msg_s **re
        }
 
        if(((*register_data)->msg_size <= 0) ||((*register_data)->msg_size > MS_FILE_PATH_LEN_MAX)) {
-               MSC_DBG_ERR("message size[%d] is wrong", (*register_data)->msg_size);
+               MS_DBG_ERR("message size[%d] is wrong", (*register_data)->msg_size);
                return MS_MEDIA_ERR_INVALID_IPC_MESSAGE;
        }
 
@@ -1116,19 +1265,19 @@ gboolean msc_register_thread(void *data)
        /*create array for processing overlay data*/
        register_array = g_array_new (FALSE, FALSE, sizeof (ms_comm_msg_s *));
        if (register_array == NULL) {
-               MSC_DBG_ERR("g_array_new error");
+               MS_DBG_ERR("g_array_new error");
                return false;
        }
 
        while (1) {
                ret = __msc_pop_register_request(register_array, &register_data);
                if (register_data->pid == POWEROFF) {
-                       MSC_DBG_ERR("power off");
+                       MS_DBG_ERR("power off");
                        goto _POWEROFF;
                }
 
                if (ret != MS_MEDIA_ERR_NONE) {
-                       MSC_DBG_ERR("__msc_pop_register_request failed [%d]", ret);
+                       MS_DBG_ERR("__msc_pop_register_request failed [%d]", ret);
                        goto FREE_RESOURCE;
                }
 
@@ -1138,19 +1287,19 @@ gboolean msc_register_thread(void *data)
 
                if ((current_msg != MS_MSG_BULK_INSERT) &&
                        (current_msg != MS_MSG_BURSTSHOT_INSERT)) {
-                       MSC_DBG_ERR("wrong message type");
+                       MS_DBG_ERR("wrong message type");
                        goto FREE_RESOURCE;
                }
 
                file_path = strndup(register_data->msg, register_data->msg_size);
                if (file_path == NULL) {
-                       MSC_DBG_ERR("file_path is NULL");
+                       MS_DBG_ERR("file_path is NULL");
                        goto FREE_RESOURCE;
                }
 
                ret = __msc_make_file_list(file_path, &path_array, register_data->uid);
                if (ret != MS_MEDIA_ERR_NONE) {
-                       MSC_DBG_ERR("__msc_make_file_list failed [%d]", ret);
+                       MS_DBG_ERR("__msc_make_file_list failed [%d]", ret);
                        goto FREE_RESOURCE;
                }
 
@@ -1163,7 +1312,7 @@ FREE_RESOURCE:
                /* If register_files operation is stopped, there is no necessrty for sending result. */
                msc_send_result(ret, register_data);
 
-               MSC_DBG_ERR("BULK REGISTER END [%d |%s]", ret, register_data->msg);
+               MS_DBG_ERR("BULK REGISTER END [%d |%s]", ret, register_data->msg);
 
                __msc_clear_file_list(path_array);
 
@@ -1193,17 +1342,17 @@ _POWEROFF:
 static void __msc_bacth_commit_enable(void* handle, bool ins_status, bool valid_status, bool noti_enable, int pid)
 {
        /*call for bundle commit*/
-       if (ins_status) msc_register_start(handle, noti_enable, pid);
-       if (valid_status) msc_validate_start(handle);
+       if (ins_status) ms_register_start(handle, noti_enable, pid);
+       if (valid_status) ms_validate_start(handle);
 
        return;
 }
 
-static void __msc_bacth_commit_disable(void* handle, bool ins_status, bool valid_status, uid_t uid)
+static void __msc_bacth_commit_disable(void* handle, bool ins_status, bool valid_status, const char *path, uid_t uid)
 {
        /*call for bundle commit*/
-       if (ins_status) msc_register_end(handle, uid);
-       if (valid_status) msc_validate_end(handle, uid);
+       if (ins_status) ms_register_end(handle, path, uid);
+       if (valid_status) ms_validate_end(handle, uid);
 
        return;
 }
@@ -1211,7 +1360,7 @@ static void __msc_bacth_commit_disable(void* handle, bool ins_status, bool valid
 int msc_set_cancel_path(const char *cancel_path)
 {
        if (g_cancel_path != NULL) {
-               MSC_DBG_ERR("g_cancel_path is not NULL");
+               MS_DBG_ERR("g_cancel_path is not NULL");
                free(g_cancel_path);
                g_cancel_path = NULL;
        }
@@ -1231,12 +1380,12 @@ static int __msc_dir_scan_meta_update(void **handle, const char*start_path, ms_s
        char *new_path = NULL;
        char *current_path = NULL;
        char path[MS_FILE_PATH_LEN_MAX] = { 0 };
-       int (*scan_function)(void **, const char*, uid_t) = msc_update_meta_batch;
+       int (*scan_function)(void **, const char*, uid_t) = ms_update_meta_batch;
 
        /* make new array for storing directory */
        dir_array = g_array_new (FALSE, FALSE, sizeof (char*));
        if (dir_array == NULL){
-               MSC_DBG_ERR("g_array_new failed");
+               MS_DBG_ERR("g_array_new failed");
                return MS_MEDIA_ERR_OUT_OF_MEMORY;
        }
        /* add first direcotiry to directory array */
@@ -1253,10 +1402,10 @@ static int __msc_dir_scan_meta_update(void **handle, const char*start_path, ms_s
                /* get the current path from directory array */
                current_path = g_array_index(dir_array , char*, 0);
                g_array_remove_index (dir_array, 0);
-//             MSC_DBG_SLOG("%d", dir_array->len);
+//             MS_DBG_SLOG("%d", dir_array->len);
 
                if (__msc_check_scan_ignore(current_path)) {
-                       MSC_DBG_ERR("%s is ignore", current_path);
+                       MS_DBG_ERR("%s is ignore", current_path);
                        MS_SAFE_FREE(current_path);
                        continue;
                }
@@ -1277,21 +1426,21 @@ static int __msc_dir_scan_meta_update(void **handle, const char*start_path, ms_s
                                        continue;
 
                                if (entry.d_type & DT_REG) {
-                                       MSC_DBG_ERR("");
                                         if (ms_strappend(path, sizeof(path), "%s/%s", current_path, entry.d_name) != MS_MEDIA_ERR_NONE) {
-                                               MSC_DBG_ERR("ms_strappend failed");
+                                               MS_DBG_ERR("ms_strappend failed");
                                                continue;
                                        }
+
                                        /* insert into media DB */
-                                       MSC_DBG_ERR("%s", path);
+                                       MS_DBG_ERR("%s", path);
                                        if (scan_function(handle,path, uid) != MS_MEDIA_ERR_NONE) {
-                                               MSC_DBG_ERR("failed to update db");
+                                               MS_DBG_ERR("failed to update db");
                                                continue;
                                        }
                                } else if (entry.d_type & DT_DIR) {
                                        /* this request is recursive scanning */
                                         if (ms_strappend(path, sizeof(path), "%s/%s", current_path, entry.d_name) != MS_MEDIA_ERR_NONE) {
-                                               MSC_DBG_ERR("ms_strappend failed");
+                                               MS_DBG_ERR("ms_strappend failed");
                                                continue;
                                        }
                                        /* add new directory to dir_array */
@@ -1300,8 +1449,9 @@ static int __msc_dir_scan_meta_update(void **handle, const char*start_path, ms_s
                                }
                        }
                } else {
-                       MSC_DBG_ERR("%s folder opendir fails", current_path);
+                       MS_DBG_ERR("%s folder opendir fails", current_path);
                }
+
                if (dp) closedir(dp);
                dp = NULL;
                MS_SAFE_FREE(current_path);
@@ -1311,7 +1461,7 @@ STOP_SCAN:
 
        __msc_clear_file_list(dir_array);
 
-       if (ret != MS_MEDIA_ERR_NONE) MSC_DBG_INFO("ret : %d", ret);
+       if (ret != MS_MEDIA_ERR_NONE) MS_DBG_INFO("ret : %d", ret);
 
        return ret;
 }
@@ -1326,15 +1476,15 @@ gboolean msc_metadata_update(void *data)
        char *start_path = NULL;
        ms_storage_type_t storage_type = MS_STORAGE_INTERNAL;
 
-       MSC_DBG_INFO("META UPDATE START");
+       MS_DBG_INFO("META UPDATE START");
 
        /*connect to media db, if conneting is failed, db updating is stopped*/
-       err = msc_connect_db(&handle, scan_data->uid);
+       err = ms_connect_db(&handle, scan_data->uid);
        if (err != MS_MEDIA_ERR_NONE)
                return false;
 
        /*call for bundle commit*/
-       msc_update_start(handle);
+       ms_update_start(handle);
 
        /*insert data into media db */
 
@@ -1342,26 +1492,29 @@ gboolean msc_metadata_update(void *data)
        ret = __msc_dir_scan_meta_update(handle, start_path, storage_type, scan_data->uid);
 
        /* send notification */
-       msc_send_dir_update_noti(handle, __msc_get_path(scan_data->uid));
+       ms_send_dir_update_noti(handle, INTERNAL_STORAGE_ID, __msc_get_path(scan_data->uid));
 
-       if (mmc_state == VCONFKEY_SYSMAN_MMC_MOUNTED) {
+       if (mmc_state == MS_STG_INSERTED) {
                storage_type = MS_STORAGE_EXTERNAL;
                start_path = strdup(MEDIA_ROOT_PATH_SDCARD);
                ret = __msc_dir_scan_meta_update(handle, start_path, storage_type, scan_data->uid);
                /* send notification */
-               msc_send_dir_update_noti(handle, MEDIA_ROOT_PATH_SDCARD);
+               ms_send_dir_update_noti(handle, MMC_STORAGE_ID, MEDIA_ROOT_PATH_SDCARD);
        }
 
+       /*FIX ME*/
+       /*__msc_dir_scan_meta_update For Each USB Storage*/
+
        /*call for bundle commit*/
-       msc_update_end(handle, scan_data->uid);
+       ms_update_end(handle, scan_data->uid);
 
        if (power_off) {
-               MSC_DBG_WAN("power off");
+               MS_DBG_WARN("power off");
                goto _POWEROFF;
        }
 
        /*disconnect form media db*/
-       if (handle) msc_disconnect_db(&handle);
+       if (handle) ms_disconnect_db(&handle);
 
        /*Active flush */
        malloc_trim(0);
@@ -1370,12 +1523,12 @@ gboolean msc_metadata_update(void *data)
 
        MS_SAFE_FREE(scan_data);
 
-       MSC_DBG_INFO("META UPDATE END [%d]", ret);
+       MS_DBG_INFO("META UPDATE END [%d]", ret);
 
 
 _POWEROFF:
        MS_SAFE_FREE(scan_data);
-       if (handle) msc_disconnect_db(&handle);
+       if (handle) ms_disconnect_db(&handle);
 
        return false;
 }
@@ -1385,3 +1538,142 @@ void msc_metadata_update_thread(ms_comm_msg_s *recv_msg)
 {
         g_thread_new("update_thread", (GThreadFunc)msc_metadata_update, recv_msg);
 }
+
+static int __msc_set_storage_scan_status(ms_storage_scan_status_e status)
+{
+       int res = MS_MEDIA_ERR_NONE;
+#if 0
+       if (!ms_config_set_int(MS_SCANNER_STATUS, status)) {
+               res = MS_MEDIA_ERR_VCONF_SET_FAIL;
+               MS_DBG_ERR("ms_config_set_int failed");
+       }
+#else
+       stg_scan_status = status;
+#endif
+       return res;
+}
+
+int msc_init_scanner(void)
+{
+       if (!scan_queue) scan_queue = g_async_queue_new();
+       if (!reg_queue) reg_queue = g_async_queue_new();
+       if (!storage_queue) storage_queue = g_async_queue_new();
+
+       /*Init mutex variable*/
+       g_mutex_init(&scan_req_mutex);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_deinit_scanner(void)
+{
+       if (scan_queue) g_async_queue_unref(scan_queue);
+       if (reg_queue) g_async_queue_unref(reg_queue);
+       if (storage_queue) g_async_queue_unref(storage_queue);
+
+       /*Clear db mutex variable*/
+       g_mutex_clear(&scan_req_mutex);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_set_mmc_status(ms_stg_status_e status)
+{
+       mmc_state = status;
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_push_scan_request(ms_scan_type_e scan_type, ms_comm_msg_s *recv_msg)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+
+       switch(scan_type) {
+               case MS_SCAN_STORAGE:
+                       g_async_queue_push(storage_queue, GINT_TO_POINTER(recv_msg));
+                       break;
+               case MS_SCAN_DIRECTORY:
+                       g_async_queue_push(scan_queue, GINT_TO_POINTER(recv_msg));
+                       break;
+               case MS_SCAN_REGISTER:
+                       g_async_queue_push(reg_queue, GINT_TO_POINTER(recv_msg));
+                       break;
+               default:
+                       MS_DBG_ERR("invalid parameter");
+                       ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+                       break;
+       }
+
+       return ret;
+}
+
+int msc_send_power_off_request(void)
+{
+       ms_comm_msg_s *data;
+
+       power_off = true;
+
+       if (scan_queue) {
+               /*notify to scannig thread*/
+               MS_MALLOC(data, sizeof(ms_comm_msg_s));
+               data->pid = POWEROFF;
+               msc_push_scan_request(MS_SCAN_DIRECTORY, data);
+       }
+
+       if (reg_queue) {
+               /*notify to register thread*/
+               MS_MALLOC(data, sizeof(ms_comm_msg_s));
+               data->pid = POWEROFF;
+               msc_push_scan_request(MS_SCAN_REGISTER, data);
+       }
+
+       if (storage_queue) {
+               /*notify to register thread*/
+               MS_MALLOC(data, sizeof(ms_comm_msg_s));
+               data->pid = POWEROFF;
+               msc_push_scan_request(MS_SCAN_STORAGE, data);
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_remove_dir_scan_request(ms_comm_msg_s *recv_msg)
+{
+       char *cancel_path = recv_msg->msg;
+       int pid = recv_msg->pid;
+       int i = 0;
+       int len = g_async_queue_length(scan_queue);
+       ms_comm_msg_s *msg = NULL;
+       GAsyncQueue *temp_scan_queue = NULL;
+
+       MS_DBG_ERR("scan_req_mutex is LOCKED");
+       g_mutex_lock(&scan_req_mutex);
+
+       if (len == 0) {
+               MS_DBG_ERR("Request is not stacked");
+               goto END_REMOVE_REQUEST;
+       }
+
+       msc_set_cancel_path(recv_msg->msg);
+
+       temp_scan_queue = g_async_queue_new();
+
+       for (i = 0; i <len; i++) {
+               /*create new queue to compare request*/
+               msg = g_async_queue_pop(scan_queue);
+               if ((strcmp(msg->msg, cancel_path) == 0) && (pid == msg->pid)) {
+                       MS_SAFE_FREE(msg);
+               } else {
+                       g_async_queue_push(temp_scan_queue, GINT_TO_POINTER(msg));
+               }
+       }
+       g_async_queue_unref (scan_queue);
+       scan_queue = temp_scan_queue;
+
+END_REMOVE_REQUEST:
+       g_mutex_unlock(&scan_req_mutex);
+       MS_DBG_ERR("scan_req_mutex is UNLOCKED");
+
+       return MS_MEDIA_ERR_NONE;
+}
+
index 1df8396..30f976d 100755 (executable)
 #include "media-server-ipc.h"
 #include "media-common-types.h"
 #include "media-common-utils.h"
+#include "media-common-db-svc.h"
 #include "media-scanner-dbg.h"
-#include "media-scanner-db-svc.h"
 #include "media-scanner-socket.h"
 #include "media-scanner-scan.h"
 
-extern GAsyncQueue *storage_queue;
-extern GAsyncQueue *scan_queue;
-extern GAsyncQueue *reg_queue;
-extern GMutex scan_req_mutex;
-
-static int _msc_remove_request(GAsyncQueue *req_queue, ms_comm_msg_s *recv_msg)
-{
-       char *cancel_path = recv_msg->msg;
-       int pid = recv_msg->pid;
-       int i = 0;
-       int len = g_async_queue_length(req_queue);
-       ms_comm_msg_s *msg = NULL;
-       GAsyncQueue *temp_scan_queue = NULL;
-
-       MSC_DBG_ERR("scan_req_mutex is LOCKED");
-       g_mutex_lock(&scan_req_mutex);
-
-       if (len == 0) {
-               MSC_DBG_ERR("Request is not stacked");
-               goto END_REMOVE_REQUEST;
-       }
-
-       msc_set_cancel_path(recv_msg->msg);
-
-       temp_scan_queue = g_async_queue_new();
-
-       for (i = 0; i <len; i++) {
-               /*create new queue to compare request*/
-               msg = g_async_queue_pop(scan_queue);
-               if ((strcmp(msg->msg, cancel_path) == 0) && (pid == msg->pid)) {
-                       MS_SAFE_FREE(msg);
-               } else {
-                       g_async_queue_push(temp_scan_queue, GINT_TO_POINTER(msg));
-               }
-       }
-       g_async_queue_unref (scan_queue);
-       scan_queue = temp_scan_queue;
-
-END_REMOVE_REQUEST:
-       g_mutex_unlock(&scan_req_mutex);
-       MSC_DBG_ERR("scan_req_mutex is UNLOCKED");
-
-       return MS_MEDIA_ERR_NONE;
-}
-
 gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer data)
 {
        ms_comm_msg_s *recv_msg = NULL;
@@ -91,25 +46,25 @@ gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer d
 
        sockfd = g_io_channel_unix_get_fd(src);
        if (sockfd < 0) {
-               MSC_DBG_ERR("sock fd is invalid!");
+               MS_DBG_ERR("sock fd is invalid!");
                return TRUE;
        }
 
        MS_MALLOC(recv_msg, sizeof(ms_comm_msg_s));
        if (recv_msg == NULL) {
-               MSC_DBG_ERR("MS_MALLOC failed");
+               MS_DBG_ERR("MS_MALLOC failed");
                return TRUE;
        }
 
        /* read() is blocked until media scanner sends message */
        err = read(sockfd, recv_msg, sizeof(ms_comm_msg_s));
        if (err < 0) {
-               MSC_DBG_STRERROR("fifo read failed");
+               MS_DBG_STRERROR("fifo read failed");
                MS_SAFE_FREE(recv_msg);
                return MS_MEDIA_ERR_FILE_READ_FAIL;
        }
 
-       MSC_DBG_SLOG("receive msg from [%d] %d, %s, uid %d", recv_msg->pid, recv_msg->msg_type, recv_msg->msg, recv_msg->uid);
+       MS_DBG_SLOG("receive msg from [%d] %d, %s", recv_msg->pid, recv_msg->msg_type, recv_msg->msg);
 
        /* copy from recived data */
        req_num = recv_msg->msg_type;
@@ -118,9 +73,9 @@ gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer d
                case MS_MSG_BULK_INSERT:
                case MS_MSG_BURSTSHOT_INSERT:
                        {
-                               MSC_DBG_INFO("BULK INSERT");
+                               MS_DBG_INFO("BULK INSERT");
                                /* request bulk insert*/
-                               g_async_queue_push(reg_queue, GINT_TO_POINTER(recv_msg));
+                               msc_push_scan_request(MS_SCAN_REGISTER, recv_msg);
                        }
                        break;
                case MS_MSG_DIRECTORY_SCANNING:
@@ -128,7 +83,7 @@ gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer d
                        {
                                /* this request from another apps */
                                /* set the scan data for scanning thread */
-                               g_async_queue_push(scan_queue, GINT_TO_POINTER(recv_msg));
+                               msc_push_scan_request(MS_SCAN_DIRECTORY, recv_msg);
                        }
                        break;
                case MS_MSG_STORAGE_ALL:
@@ -136,12 +91,12 @@ gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer d
                case MS_MSG_STORAGE_INVALID:
                        {
                                /* this request from media-server */
-                               g_async_queue_push(storage_queue, GINT_TO_POINTER(recv_msg));
+                               msc_push_scan_request(MS_SCAN_STORAGE, recv_msg);
                        }
                        break;
                case MS_MSG_DIRECTORY_SCANNING_CANCEL:
                        {
-                               _msc_remove_request(scan_queue, recv_msg);
+                               msc_remove_dir_scan_request(recv_msg);
                                MS_SAFE_FREE(recv_msg);
                        }
                        break;
@@ -150,7 +105,7 @@ gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer d
                        break;
                default:
                        {
-                               MSC_DBG_ERR("THIS REQUEST IS INVALID %d", req_num);
+                               MS_DBG_ERR("THIS REQUEST IS INVALID %d", req_num);
                                MS_SAFE_FREE(recv_msg);
                        }
                        break;
@@ -171,7 +126,7 @@ int msc_send_ready(void)
 
        fd = open(MS_SCANNER_FIFO_PATH_RES, O_WRONLY);
        if (fd < 0) {
-               MSC_DBG_STRERROR("fifo open failed");
+               MS_DBG_STRERROR("fifo open failed");
                return MS_MEDIA_ERR_FILE_OPEN_FAIL;
        }
 
@@ -182,7 +137,7 @@ int msc_send_ready(void)
        /* send ready message */
        err = write(fd, &send_msg, sizeof(send_msg));
        if (err < 0) {
-               MSC_DBG_STRERROR("fifo write failed");
+               MS_DBG_STRERROR("fifo write failed");
                res = MS_MEDIA_ERR_FILE_READ_FAIL;
        }
 
@@ -200,7 +155,7 @@ int msc_send_result(int result, ms_comm_msg_s *res_data)
 
        fd = open(MS_SCANNER_FIFO_PATH_RES, O_WRONLY);
        if (fd < 0) {
-               MSC_DBG_STRERROR("fifo open failed");
+               MS_DBG_STRERROR("fifo open failed");
                return MS_MEDIA_ERR_FILE_OPEN_FAIL;
        }
 
@@ -216,7 +171,7 @@ int msc_send_result(int result, ms_comm_msg_s *res_data)
        /* send ready message */
        err = write(fd, &send_msg, sizeof(send_msg));
        if (err < 0) {
-               MSC_DBG_STRERROR("fifo write failed");
+               MS_DBG_STRERROR("fifo write failed");
                res = MS_MEDIA_ERR_FILE_READ_FAIL;
        }
 
index 95d5850..bcfd537 100755 (executable)
 
 #include "media-common-utils.h"
 #include "media-common-external-storage.h"
+#include "media-common-db-svc.h"
+#include "media-common-system.h"
 
 #include "media-util.h"
 #include "media-scanner-dbg.h"
-#include "media-scanner-db-svc.h"
+#include "media-scanner-device-block.h"
 #include "media-scanner-scan.h"
 #include "media-scanner-socket.h"
 
-#define APP_NAME "media-scanner"
-
-extern int mmc_state;
-
-extern GAsyncQueue *storage_queue;
-extern GAsyncQueue *scan_queue;
-extern GAsyncQueue *reg_queue;
-extern GMutex db_mutex;
-extern GMutex scan_req_mutex;
-extern bool g_directory_scan_processing;
-bool power_off; /*If this is TRUE, poweroff notification received*/
-
 static GMainLoop *scanner_mainloop = NULL;
 
-bool check_process(void)
-{
-       DIR *pdir;
-       struct dirent pinfo;
-       struct dirent *result = NULL;
-       bool ret = false;
-       int find_pid = 0;
-       pid_t current_pid = 0;
-
-       current_pid = getpid();
-
-       pdir = opendir("/proc");
-       if (pdir == NULL) {
-               MSC_DBG_ERR("err: NO_DIR\n");
-               return 0;
-       }
-
-       while (!readdir_r(pdir, &pinfo, &result)) {
-               if (result == NULL)
-                       break;
-
-               if (pinfo.d_type != 4 || pinfo.d_name[0] == '.'
-                   || pinfo.d_name[0] > 57)
-                       continue;
-
-               FILE *fp;
-               char buff[128];
-               char path[128];
-
-               ms_strcopy(path, sizeof(path), "/proc/%s/status", pinfo.d_name);
-               fp = fopen(path, "rt");
-               if (fp) {
-                       if (fgets(buff, 128, fp) == NULL)
-                               MSC_DBG_ERR("fgets failed");
-                       fclose(fp);
-
-                       if (strstr(buff, APP_NAME)) {
-                               find_pid = atoi(pinfo.d_name);
-                               if (find_pid == current_pid)
-                                       ret = true;
-                               else {
-                                       ret = false;
-                                       break;
-                               }
-                       }
-               } else {
-                       MSC_DBG_ERR("Can't read file [%s]", path);
-               }
-       }
-
-       closedir(pdir);
-
-       return ret;
-}
-
-void init_process()
-{
-
-}
-
-static void _power_off_cb(void* data)
-{
-       ms_comm_msg_s *scan_data;
-       ms_comm_msg_s *reg_data;
-
-       MSC_DBG_INFO("++++++++++++++++++++++++++++++++++++++");
-       MSC_DBG_INFO("POWER OFF");
-       MSC_DBG_INFO("++++++++++++++++++++++++++++++++++++++");
-
-       power_off = true;
-
-       if (scan_queue) {
-               /*notify to scannig thread*/
-               MS_MALLOC(scan_data, sizeof(ms_comm_msg_s));
-               if(scan_data) {
-                       scan_data->pid = POWEROFF;
-                       g_async_queue_push(scan_queue, GINT_TO_POINTER(scan_data));
-               }
-       }
-
-       if (reg_queue) {
-               /*notify to register thread*/
-               MS_MALLOC(reg_data, sizeof(ms_comm_msg_s));
-               if(reg_data) {
-                       reg_data->pid = POWEROFF;
-                       g_async_queue_push(reg_queue, GINT_TO_POINTER(reg_data));
-               }
-       }
-
-       if (storage_queue) {
-               /*notify to register thread*/
-               MS_MALLOC(reg_data, sizeof(ms_comm_msg_s));
-               if(reg_data) {
-                       reg_data->pid = POWEROFF;
-                       g_async_queue_push(storage_queue, GINT_TO_POINTER(reg_data));
-               }
-       }
-
-       if (g_main_loop_is_running(scanner_mainloop)) g_main_loop_quit(scanner_mainloop);
-}
-
-void _msc_mmc_vconf_cb(keynode_t *key, void* data)
-{
-       int status = 0;
-
-       if (!ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status)) {
-               MSC_DBG_ERR("Get VCONFKEY_SYSMAN_MMC_STATUS failed.");
-       }
-
-       MSC_DBG_INFO("VCONFKEY_SYSMAN_MMC_STATUS :%d", status);
-
-       mmc_state = status;
-
-       return;
-}
+static void __msc_power_off_cb(void* data);
+static void __msc_add_event_receiver(void *data);
+static void __msc_remove_event_receiver(void);
 
 int main(int argc, char **argv)
 {
@@ -177,37 +55,36 @@ int main(int argc, char **argv)
        int err = -1;
        int fd = -1;
 
+       /*set power off callback function*/
+       __msc_add_event_receiver(NULL);
+
        /*load functions from plusin(s)*/
-       err = msc_load_functions();
+       err = ms_load_functions();
        if (err != MS_MEDIA_ERR_NONE) {
-               MSC_DBG_ERR("function load failed[%d]", err);
+               MS_DBG_ERR("function load failed[%d]", err);
                goto EXIT;
        }
 
        /*Init main loop*/
        scanner_mainloop = g_main_loop_new(NULL, FALSE);
 
-       /*Init for register file*/
-       /*These are a communicator for thread*/
-       if (!scan_queue) scan_queue = g_async_queue_new();
-       if (!reg_queue) reg_queue = g_async_queue_new();
-       if (!storage_queue) storage_queue = g_async_queue_new();
+       msc_init_scanner();
 
        /* Create pipe */
        err = unlink(MS_SCANNER_FIFO_PATH_REQ);
        if (err !=0) {
-               MSC_DBG_STRERROR("unlink failed");
+               MS_DBG_STRERROR("unlink failed");
        }
 
        err = mkfifo(MS_SCANNER_FIFO_PATH_REQ, MS_SCANNER_FIFO_MODE);
        if (err !=0) {
-               MSC_DBG_STRERROR("mkfifo failed");
+               MS_DBG_STRERROR("mkfifo failed");
                return MS_MEDIA_ERR_INTERNAL;
        }
 
        fd = open(MS_SCANNER_FIFO_PATH_REQ, O_RDWR);
        if (fd < 0) {
-               MSC_DBG_STRERROR("fifo open failed");
+               MS_DBG_STRERROR("fifo open failed");
                return MS_MEDIA_ERR_FILE_OPEN_FAIL;
        }
 
@@ -227,22 +104,17 @@ int main(int argc, char **argv)
        scan_thread = g_thread_new("scanner_thread", (GThreadFunc)msc_directory_scan_thread, NULL);
        register_thread = g_thread_new("register_thread", (GThreadFunc)msc_register_thread, NULL);
 
-       /*set vconf callback function*/
-       err = vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, (vconf_callback_fn) _msc_mmc_vconf_cb, NULL);
-       if (err == -1)
-               MSC_DBG_ERR("add call back function for event %s fails", VCONFKEY_SYSMAN_MMC_STATUS);
-
        if (ms_is_mmc_inserted()) {
-               mmc_state = VCONFKEY_SYSMAN_MMC_MOUNTED;
+               msc_set_mmc_status(MS_STG_INSERTED);
        }
 
-       MSC_DBG_INFO("scanner is ready");
+       MS_DBG_INFO("scanner is ready");
 
        msc_send_ready();
 
-       MSC_DBG_ERR("*****************************************");
-       MSC_DBG_ERR("*** Scanner is running ***");
-       MSC_DBG_ERR("*****************************************");
+       MS_DBG_ERR("*****************************************");
+       MS_DBG_ERR("*** Scanner is running ***");
+       MS_DBG_ERR("*****************************************");
 
        g_main_loop_run(scanner_mainloop);
 
@@ -250,24 +122,42 @@ int main(int argc, char **argv)
        g_thread_join (register_thread);
        g_thread_join (storage_scan_thread);
 
-       if (power_off) {
-               g_io_channel_shutdown(channel, FALSE, NULL);
-               g_io_channel_unref(channel);
-       }
+       g_io_channel_shutdown(channel, FALSE, NULL);
+       g_io_channel_unref(channel);
 
-       if (scan_queue) g_async_queue_unref(scan_queue);
-       if (reg_queue) g_async_queue_unref(reg_queue);
-       if (storage_queue) g_async_queue_unref(storage_queue);
+       msc_deinit_scanner();
 
        /*close pipe*/
        close(fd);
 
        /*unload functions*/
-       msc_unload_functions();
+       ms_unload_functions();
+
+       __msc_remove_event_receiver();
 
 EXIT:
-       MSC_DBG_INFO("SCANNER IS END");
+       MS_DBG_INFO("SCANNER IS END");
 
        return 0;
 }
 
+static void __msc_power_off_cb(void* data)
+{
+       msc_send_power_off_request();
+
+       if (g_main_loop_is_running(scanner_mainloop)) g_main_loop_quit(scanner_mainloop);
+}
+
+static void __msc_add_event_receiver(void *data)
+{
+       /*set power off callback function*/
+       ms_sys_set_poweroff_cb(__msc_power_off_cb, NULL);
+       ms_sys_set_device_block_event_cb(msc_device_block_changed_cb, NULL);
+}
+
+static void __msc_remove_event_receiver(void)
+{
+       ms_sys_unset_device_block_event_cb();
+       ms_sys_unset_poweroff_cb();
+}
+
diff --git a/src/server/include/media-server-db-svc.h b/src/server/include/media-server-db-svc.h
deleted file mode 100755 (executable)
index c951f92..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- *  Media Server
- *
- * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-db-svc.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
-#ifndef _MEDIA_SERVER_DB_SVC_H_
-#define _MEDIA_SERVER_DB_SVC_H_
-
-#include "media-common-types.h"
-
-typedef int (*CONNECT)(void**, uid_t, char **);
-typedef int (*DISCONNECT)(void*, char **);
-typedef int (*SET_ALL_STORAGE_ITEMS_VALIDITY)(void*, int, int, uid_t,char **);
-typedef int (*CHECK_DB)(void*, uid_t uid, char **);
-
-int
-ms_load_functions(void);
-
-void
-ms_unload_functions(void);
-
-int
-ms_connect_db(void ***handle, uid_t uid);
-
-int
-ms_disconnect_db(void ***handle);
-
-int
-ms_invalidate_all_items(void **handle, ms_storage_type_t store_type, uid_t uid);
-
-int
-ms_check_db_upgrade(void **handle, uid_t uid);
-
-#endif /*_MEDIA_SERVER_DB_SVC_H_*/
\ No newline at end of file
index 0c32939..aee27ac 100755 (executable)
 #include <dlog.h>
 #include <errno.h>
 
+#include "media-common-dbg.h"
+
 #ifdef LOG_TAG
 #undef LOG_TAG
 #endif
-#define LOG_TAG "MEDIA_SERVER"
-#define BUF_LENGTH 256
-
-#define MS_DBG_STRERROR(fmt) do { \
-                       char buf[BUF_LENGTH] = {0,}; \
-                       LOGE(fmt" : STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGTH));   \
-               } while (0)
-
-#define MS_DBG_SLOG(fmt, args...) do{ if (true) { \
-               SECURE_LOGD(fmt "\n" , ##args); \
-               }} while(false)
 
-#define MS_DBG(fmt, args...) do{ if (true) { \
-               LOGD(fmt "\n" , ##args); \
-               }} while(false)
-
-#define MS_DBG_INFO(fmt, args...) do{ if (true) { \
-               LOGI(fmt "\n" , ##args); \
-               }} while(false)
-
-#define MS_DBG_WARN(fmt, args...) do{ if (true) { \
-               LOGW(fmt "\n", ##args); \
-               }} while(false)
-
-#define MS_DBG_ERR(fmt, args...) do{ if (true) { \
-               LOGE(fmt "\n", ##args); \
-               }} while(false)
+#define LOG_TAG "MEDIA_SERVER"
 
 #endif /*_MEDIA_SERVER_DBG_H_*/
diff --git a/src/server/include/media-server-device-block.h b/src/server/include/media-server-device-block.h
new file mode 100755 (executable)
index 0000000..111d3cd
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ *  Media Server
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+#ifndef _MEDIA_SERVER_DEVICE_BLOCK_H_
+#define _MEDIA_SERVER_DEVICE_BLOCK_H_
+
+#include "media-common-system.h"
+
+int ms_mmc_insert_handler(const char *mount_path);
+int ms_mmc_remove_handler(const char *mount_path);
+int ms_usb_insert_handler(const char *mount_path);
+int ms_usb_remove_handler(const char *mount_path);
+void ms_device_block_changed_cb(const char *mount_path, int block_type, ms_stg_status_e mount_status, void *user_data);
+
+#endif
\ No newline at end of file
index 6f7cae1..9da9f61 100755 (executable)
@@ -22,6 +22,7 @@
 #ifndef _MEDIA_SERVER_SOCKET_H_
 #define _MEDIA_SERVER_SOCKET_H_
 
+#include <sys/stat.h>
 #include "media-common-types.h"
 #include "media-server-ipc.h"
 
@@ -29,8 +30,9 @@ gboolean ms_read_socket(gpointer user_data);
 gboolean ms_read_db_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer data);
 gboolean ms_read_db_tcp_batch_socket(GIOChannel *src, GIOCondition condition, gpointer data);
 int ms_send_scan_request(ms_comm_msg_s *send_msg, int client_sock);
-int ms_send_storage_scan_request(ms_storage_type_t storage_type, ms_dir_scan_type_t scan_type);
+int ms_send_storage_scan_request(const char *root_path, const char *storage_id, ms_dir_scan_type_t scan_type, uid_t uid);
 gboolean ms_receive_message_from_scanner(GIOChannel *src, GIOCondition condition, gpointer data);
 int ms_remove_request_owner(int pid, const char *req_path);
+int ms_send_storage_otg_scan_request(const char *path, const char *device_uuid, ms_dir_scan_type_t scan_type);
 
 #endif /*_MEDIA_SERVER_SOCKET_H_*/
diff --git a/src/server/media-server-db-svc.c b/src/server/media-server-db-svc.c
deleted file mode 100755 (executable)
index 551d9cb..0000000
+++ /dev/null
@@ -1,310 +0,0 @@
-/*
- *  Media Server
- *
- * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-db-svc.c
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief       This file implements main database operation.
- */
-
-#include <dlfcn.h>
-
-#include "media-util.h"
-
-#include "media-common-utils.h"
-#include "media-server-dbg.h"
-#include "media-server-db-svc.h"
-
-#include <tzplatform_config.h>
-
-#define CONFIG_PATH tzplatform_mkpath(TZ_SYS_DATA,"file-manager-service/plugin-config")
-#define EXT ".so"
-#define EXT_LEN 3
-
-GArray *so_array;
-void ***func_array;
-int lib_num;
-
-void **func_handle = NULL; /*dlopen handle*/
-
-enum func_list {
-       eCONNECT,
-       eDISCONNECT,
-       eSET_ALL_VALIDITY,
-       eCHECK_DB,
-       eFUNC_MAX
-};
-
-static int
-_ms_token_data(char *buf, char **name)
-{
-       int len;
-       char* pos = NULL;
-
-       pos = strstr(buf, EXT);
-       if (pos == NULL) {
-               MS_DBG_ERR("This is not shared object library.");
-               name = NULL;
-               return -1;
-       } else {
-               len = pos - buf + EXT_LEN;
-               *name = strndup(buf, len);
-               MS_DBG("%s", *name);
-       }
-
-       return 0;
-}
-
-static bool
-_ms_load_config()
-{
-       int ret;
-       FILE *fp;
-       char *so_name = NULL;
-       char buf[256] = {0};
-
-       fp = fopen(CONFIG_PATH, "rt");
-       if (fp == NULL) {
-               MS_DBG_ERR("fp is NULL");
-               return MS_MEDIA_ERR_FILE_OPEN_FAIL;
-       }
-       while(1) {
-               if (fgets(buf, 256, fp) == NULL) {
-                       MS_DBG_ERR("fgets failed");
-                       break;
-               }
-
-               ret = _ms_token_data(buf, &so_name);
-               if (ret == 0) {
-                       /*add to array*/
-                       g_array_append_val(so_array, so_name);
-                       so_name = NULL;
-               }
-       }
-
-       fclose(fp);
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int
-ms_load_functions(void)
-{
-       int lib_index = 0, func_index;
-       char func_list[eFUNC_MAX][40] = {
-               "connect_db",
-               "disconnect_db",
-               "set_all_storage_items_validity",
-               "check_db",
-               };
-       /*init array for adding name of so*/
-       so_array = g_array_new(FALSE, FALSE, sizeof(char*));
-
-       /*load information of so*/
-       _ms_load_config();
-
-       if(so_array->len == 0) {
-               MS_DBG("There is no information for functions");
-               return MS_MEDIA_ERR_DYNAMIC_LINK;
-       }
-
-       /*the number of functions*/
-       lib_num = so_array->len;
-
-       MS_DBG("The number of information of so : %d", lib_num);
-       MS_MALLOC(func_handle, sizeof(void*) * lib_num);
-       if (func_handle == NULL) {
-               MS_DBG_ERR("malloc failed");
-               return MS_MEDIA_ERR_OUT_OF_MEMORY;
-       }
-
-       while(lib_index < lib_num) {
-               /*get handle*/
-               MS_DBG("[name of so : %s]", g_array_index(so_array, char*, lib_index));
-               func_handle[lib_index] = dlopen(g_array_index(so_array, char*, lib_index), RTLD_LAZY);
-               if (!func_handle[lib_index]) {
-                       MS_DBG_ERR("%s", dlerror());
-                       MS_SAFE_FREE(func_handle);
-                       return MS_MEDIA_ERR_DYNAMIC_LINK;
-               }
-               lib_index++;
-       }
-
-       dlerror();    /* Clear any existing error */
-
-       /*allocate for array of functions*/
-       MS_MALLOC(func_array, sizeof(void**) * lib_num);
-       if (func_array == NULL) {
-               MS_DBG_ERR("malloc failed");
-               MS_SAFE_FREE(func_handle);
-               return MS_MEDIA_ERR_OUT_OF_MEMORY;
-       }
-
-       for(lib_index = 0 ; lib_index < lib_num; lib_index ++) {
-               MS_MALLOC(func_array[lib_index], sizeof(void*) * eFUNC_MAX);
-               if (func_array[lib_index] == NULL) {
-                       int index;
-
-                       for (index = 0; index < lib_index; index ++) {
-                               MS_SAFE_FREE(func_array[index]);
-                       }
-                       MS_SAFE_FREE(func_array);
-                       MS_SAFE_FREE(func_handle);
-
-                       MS_DBG_ERR("malloc failed");
-                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
-               }
-       }
-
-       /*add functions to array */
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               for (func_index = 0; func_index < eFUNC_MAX ; func_index++) {
-                       func_array[lib_index][func_index] = dlsym(func_handle[lib_index], func_list[func_index]);
-                       if (func_array[lib_index][func_index] == NULL) {
-                               int index;
-
-                               for (index = 0; index < lib_index; index ++) {
-                                       MS_SAFE_FREE(func_array[index]);
-                               }
-                               MS_SAFE_FREE(func_array);
-                               MS_SAFE_FREE(func_handle);
-
-                               MS_DBG_ERR("dlsym failed");
-                               return MS_MEDIA_ERR_DYNAMIC_LINK;
-                       }
-               }
-       }
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-void
-ms_unload_functions(void)
-{
-       int lib_index;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index ++)
-               dlclose(func_handle[lib_index]);
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-                       if (func_array[lib_index]) {
-                               MS_SAFE_FREE(func_array[lib_index]);
-                       }
-       }
-
-       MS_SAFE_FREE (func_array);
-       MS_SAFE_FREE (func_handle);
-       if (so_array) g_array_free(so_array, TRUE);
-}
-
-int
-ms_connect_db(void ***handle, uid_t uid)
-{
-       int lib_index;
-       int ret;
-       char * err_msg = NULL;
-
-       MS_MALLOC(*handle, sizeof (void*) * lib_num);
-       if (*handle == NULL) {
-               MS_DBG_ERR("MS_MALLOC failed");
-               return TRUE;
-       }
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((CONNECT)func_array[lib_index][eCONNECT])(&((*handle)[lib_index]), uid, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       return MS_MEDIA_ERR_DB_CONNECT_FAIL;
-               }
-       }
-
-       MS_DBG("connect Media DB");
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int
-ms_disconnect_db(void ***handle)
-{
-       int lib_index;
-       int ret;
-       char * err_msg = NULL;
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((DISCONNECT)func_array[lib_index][eDISCONNECT])((*handle)[lib_index], &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       return MS_MEDIA_ERR_DB_DISCONNECT_FAIL;
-               }
-       }
-
-       MS_SAFE_FREE(*handle);
-
-       MS_DBG("Disconnect Media DB");
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int
-ms_invalidate_all_items(void **handle, ms_storage_type_t store_type, uid_t uid)
-{
-       int lib_index;
-       int res = MS_MEDIA_ERR_NONE;
-       int ret;
-       char *err_msg = NULL;
-       MS_DBG("");
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((SET_ALL_STORAGE_ITEMS_VALIDITY)func_array[lib_index][eSET_ALL_VALIDITY])(handle[lib_index], store_type, false, uid, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
-               }
-       }
-       MS_DBG("");
-       return res;
-}
-
-int
-ms_check_db_upgrade(void **handle, uid_t uid)
-{
-       int lib_index;
-       int res = MS_MEDIA_ERR_NONE;
-       int ret;
-       char *err_msg = NULL;
-       MS_DBG("");
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((CHECK_DB)func_array[lib_index][eCHECK_DB])(handle[lib_index], uid, &err_msg); /*dlopen*/
-               if (ret != 0) {
-                       MS_DBG_ERR("error : %s [%s]", g_array_index(so_array, char*, lib_index), err_msg);
-                       MS_SAFE_FREE(err_msg);
-                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
-               }
-       }
-       MS_DBG("");
-       return res;
-}
-
diff --git a/src/server/media-server-device-block.c b/src/server/media-server-device-block.c
new file mode 100755 (executable)
index 0000000..b13caa2
--- /dev/null
@@ -0,0 +1,374 @@
+/*
+ *  Media Server
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+#include "media-util.h"
+#include <sys/stat.h>
+#include "media-common-db-svc.h"
+#include "media-common-external-storage.h"
+
+#include "media-util-err.h"
+#include "media-server-dbg.h"
+#include "media-server-socket.h"
+#include "media-server-device-block.h"
+
+static int __ms_get_added_stroage_path(void **handle, const char *add_path, char **device_id)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       char *id = NULL;
+
+       *device_id = NULL;
+
+       /* read deive information */
+       ret = ms_read_device_info(add_path, &id);
+       if (id == NULL) {
+               if (ret == MS_MEDIA_ERR_FILE_NOT_EXIST) {
+                       ret = ms_genarate_uuid(handle, &id);
+                       ret = ms_write_device_info(add_path , id);
+                       if (ret == MS_MEDIA_ERR_NONE) {
+                               *device_id = strdup(id);
+                       }
+               }
+       } else {
+               *device_id = strdup(id);
+       }
+
+       MS_SAFE_FREE(id);
+
+       return ret;
+}
+
+int ms_usb_insert_handler(const char *mount_path)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       char *storage_id = NULL;
+       char *storage_path = NULL;
+       void **handle = NULL;
+       int validity = 0;
+       uid_t uid;
+       ms_dir_scan_type_t scan_type = MS_SCAN_ALL;
+
+       ret = ms_load_functions();
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_load_functions failed [%d]", ret);
+               return ret;
+       }
+
+       ms_sys_get_uid(&uid);
+       ms_connect_db(&handle, uid);
+
+       if (mount_path != NULL) {
+               MS_DBG_ERR("added path [%s]", mount_path);
+
+               __ms_get_added_stroage_path(handle, mount_path, &storage_id);
+
+               if (storage_id != NULL) {
+                       /* update storage information into media DB */
+                       ret = ms_check_storage(handle, storage_id, NULL, &storage_path, &validity);
+                       if (ret == 0) {
+                               if (validity == 1) {
+                                       MS_DBG_ERR("This storage is already updated. So ignore this event.");
+                                       ret = MS_MEDIA_ERR_NONE;
+                                       goto ERROR;
+                               }
+
+                               if (strcmp(mount_path, storage_path)) {
+                                       /* update storage path */
+                                       ret = ms_update_storage(handle, storage_id, mount_path, uid);
+                               }
+                               scan_type = MS_SCAN_PART;
+                               ms_set_storage_validity(handle, storage_id, 1, uid);
+                               if (ms_set_storage_scan_status(handle, storage_id, MEDIA_SCAN_PREPARE, uid) != MS_MEDIA_ERR_NONE){
+                                       MS_DBG_ERR("ms_set_storage_scan_status failed");
+                               }
+                       } else {
+                               /* there is no information of this storage in Media DB */
+                               ret = ms_insert_storage(handle, storage_id, NULL, mount_path, uid);
+                       }
+               } else {
+                       MS_DBG_ERR("STORAGE ID IS NUILL");
+                       ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+                       goto ERROR;
+               }
+
+               /* request to update media db */
+               ms_send_storage_otg_scan_request(mount_path, storage_id, scan_type);
+       }
+
+ERROR:
+       MS_SAFE_FREE(storage_id);
+       MS_SAFE_FREE(storage_path);
+
+       ms_disconnect_db(&handle);
+
+       ms_unload_functions();
+
+       return ret;
+}
+
+int ms_usb_remove_handler(const char *mount_path)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       void **handle = NULL;
+       char device_id[MS_UUID_SIZE] = {0,};
+       uid_t uid;
+
+       if (mount_path != NULL) {
+               ret = ms_load_functions();
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("ms_load_functions failed [%d]", ret);
+                       return ret;
+               }
+
+               ms_sys_get_uid(&uid);
+
+               ms_connect_db(&handle, uid);
+
+               while(1) {
+                       memset(device_id, 0x0, sizeof(device_id));
+                       ms_get_storage_id(handle, mount_path, device_id);
+
+                       MS_DBG_ERR("removed path [%s %s]", mount_path, device_id);
+
+                       if (strlen(device_id) == (MS_UUID_SIZE-1)) {
+                               ms_set_storage_validity(handle, device_id, 0, uid);
+                               ms_send_storage_otg_scan_request(mount_path, device_id, MS_SCAN_INVALID);
+                       } else {
+                               MS_DBG_ERR("Device ID is INVALID");
+                               break;
+                       }
+               }
+
+               ms_disconnect_db(&handle);
+
+               ms_unload_functions();
+       }
+
+       return ret;
+}
+
+static ms_dir_scan_type_t __ms_get_mmc_scan_type(const char *cid, const char *storage_name, const char *storage_path)
+{
+       ms_dir_scan_type_t scan_type = MS_SCAN_ALL;
+
+       MS_DBG("Last MMC id = [%s] MMC path = [%s]", storage_name, storage_path);
+       MS_DBG("Current MMC info = [%s]", cid);
+
+       if(storage_name != NULL) {
+               if (strcmp(storage_name, cid) == 0) {
+                       scan_type = MS_SCAN_PART;
+               }
+       }
+
+       if(scan_type == MS_SCAN_PART)
+               MS_DBG("MMC Scan type [MS_SCAN_PART]");
+       else
+               MS_DBG("MMC Scan type [MS_SCAN_ALL]");
+
+       return scan_type;
+}
+
+static int __ms_get_mmc_info(void **handle, char **storage_name, char **storage_path, int *validity, bool *info_exist)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+
+       ret = ms_get_mmc_info(handle, storage_name, storage_path, validity, info_exist);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("[No-Error] ms_get_mmc_info failed. Maybe storage info not in media db [%d]", ret);
+       }
+
+       return ret;
+}
+
+static int __ms_insert_mmc_info(void **handle, const char *storage_name, const char *storage_path)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       uid_t uid;
+
+       ms_sys_get_uid(&uid);
+
+       ret = ms_insert_storage(handle, MMC_STORAGE_ID, storage_name, storage_path, uid);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_insert_storage failed [%d]", ret);
+               ret = MS_MEDIA_ERR_INTERNAL;
+       }
+
+       return ret;
+}
+
+static int __ms_update_mmc_info(void **handle, const char *old_storage_name, const char *new_storage_name, const char *new_storage_path)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       uid_t uid;
+
+       ms_sys_get_uid(&uid);
+
+       ret = ms_delete_storage(handle, MMC_STORAGE_ID, old_storage_name, uid);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_delete_storage failed [%d]", ret);
+       }
+
+       ret = ms_insert_storage(handle, MMC_STORAGE_ID, new_storage_name, new_storage_path, uid);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_insert_storage failed [%d]", ret);
+               ret = MS_MEDIA_ERR_INTERNAL;
+       }
+
+       return ret;
+}
+
+int ms_mmc_insert_handler(const char *mount_path)
+{
+       /*FIX ME*/
+       int ret = MS_MEDIA_ERR_NONE;
+       ms_dir_scan_type_t scan_type = MS_SCAN_ALL;
+       char *storage_name = NULL;
+       char *storage_path = NULL;
+       int validity = NULL;
+       bool info_exist = FALSE;
+       char *cid = NULL;
+       uid_t uid;
+
+       void **db_handle = NULL;
+
+       ret = ms_load_functions();
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_load_functions failed [%d]", ret);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       ms_sys_get_uid(&uid);
+
+       ret = ms_connect_db(&db_handle, uid);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_connect_db failed [%d]", ret);
+               ms_unload_functions();
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       ms_present_mmc_status(MS_SDCARD_INSERTED);
+
+       ms_get_mmc_id(&cid);
+       __ms_get_mmc_info(db_handle, &storage_name, &storage_path, &validity, &info_exist);
+
+       if (info_exist == TRUE) {
+               scan_type = __ms_get_mmc_scan_type(cid, storage_name, storage_path);
+               if(scan_type == MS_SCAN_ALL) /*FIX ME. path should be compared*/
+                       __ms_update_mmc_info(db_handle, storage_name, cid, mount_path);
+       } else {
+               __ms_insert_mmc_info(db_handle, cid, mount_path);
+       }
+
+       ms_set_storage_validity(db_handle, MMC_STORAGE_ID, 1, uid);
+
+       ms_disconnect_db(&db_handle);
+       ms_unload_functions();
+
+       MS_SAFE_FREE(cid);
+       MS_SAFE_FREE(storage_name);
+       MS_SAFE_FREE(storage_path);
+
+       ms_send_storage_scan_request(mount_path, MMC_STORAGE_ID, scan_type, uid);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int ms_mmc_remove_handler(const char *mount_path)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       char *storage_path = NULL;
+       void **handle = NULL;
+       int validity = 0;
+       uid_t uid;
+
+       ret = ms_load_functions();
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_load_functions failed [%d]", ret);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       ms_sys_get_uid(&uid);
+
+       ret = ms_connect_db(&handle, uid);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_connect_db failed [%d]", ret);
+               ms_unload_functions();
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       if (mount_path == NULL) {
+               ret = ms_check_storage(handle, MMC_STORAGE_ID, NULL, &storage_path, &validity);
+       } else {
+               storage_path = strdup(mount_path);
+       }
+
+       if (storage_path != NULL) {
+               MS_DBG_ERR("mmc_path[%s]", storage_path);
+
+               ms_set_storage_validity(handle, MMC_STORAGE_ID, 0, uid);
+
+               ms_send_storage_scan_request(storage_path, MMC_STORAGE_ID, MS_SCAN_INVALID, uid);
+
+               MS_SAFE_FREE(storage_path);
+       }
+
+       ms_disconnect_db(&handle);
+       ms_unload_functions();
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+void _ms_mmc_changed_event(const char *mount_path, ms_stg_status_e mount_status)
+{
+       /* If scanner is not working, media server executes media scanner and sends request. */
+       /* If scanner is working, it detects changing status of SD card. */
+       if (mount_status == MS_STG_INSERTED) {
+               ms_mmc_insert_handler(mount_path);
+       } else if (mount_status == MS_STG_REMOVED) {
+               /*remove added watch descriptors */
+               ms_present_mmc_status(MS_SDCARD_REMOVED);
+               ms_mmc_remove_handler(mount_path);
+       }
+
+       return;
+}
+
+void _ms_usb_changed_event(const char *mount_path, ms_stg_status_e mount_status)
+{
+       if (mount_status == MS_STG_INSERTED) {
+               ms_usb_insert_handler(mount_path);
+       } else if (mount_status == MS_STG_REMOVED) {
+               /*remove added watch descriptors */
+               ms_usb_remove_handler(mount_path);
+       }
+
+       return;
+}
+
+void ms_device_block_changed_cb(const char *mount_path, int block_type, ms_stg_status_e mount_status, void *user_data)
+{
+       if (block_type == 0) {
+               MS_DBG_ERR("GET THE USB EVENT");
+               _ms_usb_changed_event(mount_path, mount_status);
+       } else {
+               MS_DBG_ERR("GET THE MMC EVENT");
+               _ms_mmc_changed_event(mount_path, mount_status);
+       }
+}
+
index 52b7cbb..2f9895a 100755 (executable)
 #include "media-util.h"
 #include "media-common-utils.h"
 #include "media-common-external-storage.h"
+#include "media-common-db-svc.h"
+#include "media-common-system.h"
+
 #include "media-server-dbg.h"
-#include "media-server-db-svc.h"
 #include "media-server-socket.h"
 #include "media-server-db.h"
 #include "media-server-thumb.h"
 #include "media-server-scanner.h"
-
-#define APP_NAME "media-server"
+#include "media-server-device-block.h"
 
 extern GMutex scanner_mutex;
 
@@ -50,64 +51,11 @@ static void __ms_remove_event_receiver(void);
 static void __ms_add_event_receiver(GIOChannel *channel);
 static void __ms_remove_requst_receiver(GIOChannel *channel);
 static void __ms_add_requst_receiver(GMainLoop *mainloop, GIOChannel **channel);
+static int __ms_check_mmc_status(void);
+static int __ms_check_usb_status(void);
 
 static char *priv_lang = NULL;
 
-bool check_process()
-{
-       DIR *pdir;
-       struct dirent pinfo;
-       struct dirent *result = NULL;
-       bool ret = false;
-       int find_pid = 0;
-       pid_t current_pid = 0;
-
-       current_pid = getpid();
-
-       pdir = opendir("/proc");
-       if (pdir == NULL) {
-               MS_DBG_ERR("err: NO_DIR");
-               return 0;
-       }
-
-       while (!readdir_r(pdir, &pinfo, &result)) {
-               if (result == NULL)
-                       break;
-
-               if (pinfo.d_type != 4 || pinfo.d_name[0] == '.'
-                   || pinfo.d_name[0] > 57)
-                       continue;
-
-               FILE *fp;
-               char buff[128];
-               char path[128];
-
-               ms_strcopy(path, sizeof(path), "/proc/%s/status", pinfo.d_name);
-               fp = fopen(path, "rt");
-               if (fp) {
-                       if (fgets(buff, 128, fp) == NULL)
-                               MS_DBG_ERR("fgets failed");
-                       fclose(fp);
-
-                       if (strstr(buff, APP_NAME)) {
-                               find_pid = atoi(pinfo.d_name);
-                               if (find_pid == current_pid)
-                                       ret = true;
-                               else {
-                                       ret = false;
-                                       break;
-                               }
-                       }
-               } else {
-                       MS_DBG_ERR("Can't read file [%s]", path);
-               }
-       }
-
-       closedir(pdir);
-
-       return ret;
-}
-
 void _power_off_cb(void* data)
 {
        MS_DBG_ERR("POWER OFF");
@@ -135,38 +83,6 @@ void _power_off_cb(void* data)
        return;
 }
 
-static void _db_clear(void)
-{
-       int err = MS_MEDIA_ERR_NONE;
-       void **handle = NULL;
-
-       /*load functions from plusin(s)*/
-       err = ms_load_functions();
-       if (err != MS_MEDIA_ERR_NONE) {
-               MS_DBG_ERR("function load failed [%d]", err);
-               return;
-       }
-
-       /*connect to media db, if conneting is failed, db updating is stopped*/
-       ms_connect_db(&handle,tzplatform_getuid(TZ_USER_NAME));
-
-       /* check schema of db is chaged and need upgrade */
-       MS_DBG_WARN("Check DB upgrade start");
-       if (ms_check_db_upgrade(handle,tzplatform_getuid(TZ_USER_NAME))  != MS_MEDIA_ERR_NONE)
-               MS_DBG_ERR("ms_check_db_upgrade fail");
-       MS_DBG_WARN("Check DB upgrade end");
-
-       /*update just valid type*/
-       if (ms_invalidate_all_items(handle, MS_STORAGE_EXTERNAL, tzplatform_getuid(TZ_USER_NAME))  != MS_MEDIA_ERR_NONE)
-               MS_DBG_ERR("ms_change_valid_type fail");
-
-       /*disconnect form media db*/
-       if (handle) ms_disconnect_db(&handle);
-
-       /*unload functions*/
-       ms_unload_functions();
-}
-
 void _ms_signal_handler(int n)
 {
        int stat, pid, thumb_pid;
@@ -186,127 +102,72 @@ void _ms_signal_handler(int n)
                } else {
                        MS_DBG_ERR("[%d] is stopped", pid);
                }
-/*
-               if (WIFEXITED(stat)) {
-                       MS_DBG_ERR("normal termination , exit status : %d", WEXITSTATUS(stat));
-               } else if (WIFSIGNALED(stat)) {
-                       MS_DBG_ERR("abnormal termination , signal number : %d", WTERMSIG(stat));
-               } else if (WIFSTOPPED(stat)) {
-                       MS_DBG_ERR("child process is stoped, signal number : %d", WSTOPSIG(stat));
-               }
-*/
        }
 
        return;
 }
 
-static void _ms_new_global_variable(void)
+static void __ms_new_global_variable(void)
 {
        /*Init mutex variable*/
        /*media scanner stop/start mutex*/
        g_mutex_init(&scanner_mutex);
 }
 
-static void _ms_free_global_variable(void)
+static void __ms_free_global_variable(void)
 {
        /*Clear mutex variable*/
        g_mutex_clear(&scanner_mutex);
 }
 
-void
-_ms_mmc_vconf_cb(void *data)
-{
-       int status = 0;
-
-       if (!ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status)) {
-               MS_DBG_ERR("Get VCONFKEY_SYSMAN_MMC_STATUS failed.");
-       }
-
-       MS_DBG("VCONFKEY_SYSMAN_MMC_STATUS :%d", status);
-
-       /* If scanner is not working, media server executes media scanner and sends request. */
-       /* If scanner is working, it detects changing status of SD card. */
-       if (status == VCONFKEY_SYSMAN_MMC_REMOVED ||
-               status == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
-
-               /*remove added watch descriptors */
-               ms_present_mmc_status(MS_SDCARD_REMOVED);
-
-               ms_send_storage_scan_request(MS_STORAGE_EXTERNAL, MS_SCAN_INVALID);
-       } else if (status == VCONFKEY_SYSMAN_MMC_MOUNTED) {
-
-               ms_make_default_path_mmc();
-
-               ms_present_mmc_status(MS_SDCARD_INSERTED);
-
-               ms_send_storage_scan_request(MS_STORAGE_EXTERNAL, ms_get_mmc_state());
-       }
-
-       return;
-}
-
-void
-_ms_change_lang_vconf_cb(keynode_t *key, void* data)
+void _ms_change_lang_vconf_cb(keynode_t *key, void* data)
 {
-       char lang[10] = {0,};
-       char *eng = "en";
-       char *chi = "zh";
-       char *jpn = "ja";
-       char *kor = "ko";
+       char *lang = NULL;
+       const char *eng = "en";
+       const char *chi = "zh";
+       const char *jpn = "ja";
+       const char *kor = "ko";
        bool need_update = FALSE;
 
-       if (!ms_config_get_str(VCONFKEY_LANGSET, lang)) {
+       if (!ms_config_get_str(VCONFKEY_LANGSET, &lang)) {
                MS_DBG_ERR("Get VCONFKEY_LANGSET failed.");
                return;
        }
 
-       MS_DBG("CURRENT LANGUAGE [%s %s]", priv_lang, lang);
+       MS_DBG("CURRENT LANGUAGE [%s] [%s]", priv_lang, lang);
 
-       if (strcmp(priv_lang, lang) == 0) {
-               need_update = FALSE;
-       } else if ((strncmp(lang, eng, strlen(eng)) == 0) ||
-                       (strncmp(lang, chi, strlen(chi)) == 0) ||
-                       (strncmp(lang, jpn, strlen(jpn)) == 0) ||
-                       (strncmp(lang, kor, strlen(kor)) == 0)) {
-                       need_update = TRUE;
-       } else {
-               if ((strncmp(priv_lang, eng, strlen(eng)) == 0) ||
-                       (strncmp(priv_lang, chi, strlen(chi)) == 0) ||
-                       (strncmp(priv_lang, jpn, strlen(jpn)) == 0) ||
-                       (strncmp(priv_lang, kor, strlen(kor)) == 0)) {
-                       need_update = TRUE;
+       if (MS_STRING_VALID(priv_lang) && MS_STRING_VALID(lang)) {
+               if (strcmp(priv_lang, lang) == 0) {
+                       need_update = FALSE;
+               } else if ((strncmp(lang, eng, strlen(eng)) == 0) ||
+                               (strncmp(lang, chi, strlen(chi)) == 0) ||
+                               (strncmp(lang, jpn, strlen(jpn)) == 0) ||
+                               (strncmp(lang, kor, strlen(kor)) == 0)) {
+                               need_update = TRUE;
+               } else {
+                       if ((strncmp(priv_lang, eng, strlen(eng)) == 0) ||
+                               (strncmp(priv_lang, chi, strlen(chi)) == 0) ||
+                               (strncmp(priv_lang, jpn, strlen(jpn)) == 0) ||
+                               (strncmp(priv_lang, kor, strlen(kor)) == 0)) {
+                               need_update = TRUE;
+                       }
                }
+       } else {
+               need_update = TRUE;
        }
 
-       if (need_update) {
-               ms_send_storage_scan_request(MS_STORAGE_INTERNAL, MS_SCAN_META);
+       if (need_update == TRUE) {
+               ms_send_storage_scan_request(NULL, INTERNAL_STORAGE_ID, MS_SCAN_META, NULL);    /*FIX ME*/
        } else {
                MS_DBG_WARN("language is changed but do not update meta data");
        }
 
        MS_SAFE_FREE(priv_lang);
-       priv_lang = strdup(lang);
 
-       return;
-}
+       if (MS_STRING_VALID(lang))
+               priv_lang = strdup(lang);
 
-static void _ms_check_pw_status(void)
-{
-       int pw_status = 0;
-
-       if (!ms_config_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &pw_status)) {
-               MS_DBG_ERR("Get VCONFKEY_SYSMAN_POWER_OFF_STATUS failed.");
-       }
-
-       if (pw_status == VCONFKEY_SYSMAN_POWER_OFF_DIRECT ||
-               pw_status == VCONFKEY_SYSMAN_POWER_OFF_RESTART) {
-               power_off = TRUE;
-
-               while(1) {
-                       MS_DBG_WARN("wait power off");
-                       sleep(3);
-               }
-       }
+       MS_SAFE_FREE(lang);
 
        return;
 }
@@ -334,22 +195,13 @@ int main(int argc, char **argv)
        GThread *db_thread = NULL;
        GThread *thumb_thread = NULL;
        GIOChannel *channel = NULL;
-#if 0
-       bool check_result = false;
-#endif
-       power_off = FALSE;
 
-       _ms_check_pw_status();
+       power_off = FALSE;
 
-#if 0
-       check_result = check_process();
-       if (check_result == false)
-               goto EXIT;
-#endif
        /*Init main loop*/
        mainloop = g_main_loop_new(NULL, FALSE);
 
-       _ms_new_global_variable();
+       __ms_new_global_variable();
 
        if (ms_cynara_initialize() != MS_MEDIA_ERR_NONE) {
                MS_DBG_ERR("Failed to initialize cynara");
@@ -389,12 +241,10 @@ int main(int argc, char **argv)
 
        ms_cynara_finish();
 
-       _ms_free_global_variable();
+       __ms_free_global_variable();
 
        MS_DBG_ERR("*** Media Server is stopped ***");
-#if 0
-EXIT:
-#endif
+
        return 0;
 }
 
@@ -449,34 +299,36 @@ static void __ms_remove_requst_receiver(GIOChannel *channel)
 
 static void __ms_add_event_receiver(GIOChannel *channel)
 {
-       int err;
-       char lang[10] = {0,};
+       int err = 0;
+       char *lang = NULL;
 
        /*set power off callback function*/
-//     ms_add_poweoff_event_receiver(_power_off_cb,channel);
+       ms_sys_set_poweroff_cb(_power_off_cb,channel);
+       ms_sys_set_device_block_event_cb(ms_device_block_changed_cb, NULL);
 
-       /*add noti receiver for SD card event */
-       err = vconf_notify_key_changed(VCONFKEY_SYSMAN_MMC_STATUS, (vconf_callback_fn) _ms_mmc_vconf_cb, NULL);
-       if (err == -1)
-               MS_DBG_ERR("add call back function for event %s fails", VCONFKEY_SYSMAN_MMC_STATUS);
-
-       if (!ms_config_get_str(VCONFKEY_LANGSET, lang)) {
+       if (!ms_config_get_str(VCONFKEY_LANGSET, &lang)) {
                MS_DBG_ERR("Get VCONFKEY_LANGSET failed.");
                return;
        }
 
-       priv_lang = strdup(lang);
+       if (MS_STRING_VALID(lang)) {
+               MS_DBG("Set language change cb [%s]", lang);
+
+               priv_lang = strdup(lang);
+               MS_SAFE_FREE(lang);
 
-       err = vconf_notify_key_changed(VCONFKEY_LANGSET, (vconf_callback_fn) _ms_change_lang_vconf_cb, NULL);
-       if (err == -1)
-               MS_DBG_ERR("add call back function for event %s fails", VCONFKEY_LANGSET);
+               err = vconf_notify_key_changed(VCONFKEY_LANGSET, (vconf_callback_fn) _ms_change_lang_vconf_cb, NULL);
+               if (err == -1)
+                       MS_DBG_ERR("add call back function for event %s fails", VCONFKEY_LANGSET);
+       }
 
+       return;
 }
 
 static void __ms_remove_event_receiver(void)
 {
-       vconf_ignore_key_changed(VCONFKEY_SYSMAN_MMC_STATUS,
-                                (vconf_callback_fn) _ms_mmc_vconf_cb);
+       ms_sys_unset_device_block_event_cb();
+       ms_sys_unset_poweroff_cb();
 }
 
 static void __ms_add_signal_handler(void)
@@ -495,19 +347,76 @@ static void __ms_add_signal_handler(void)
 
        signal(SIGPIPE,SIG_IGN);
 }
+
 static void __ms_check_mediadb(void)
 {
-       _db_clear();
-
-       /* update internal storage */
-       ms_send_storage_scan_request(MS_STORAGE_INTERNAL, MS_SCAN_PART);
+       ms_send_storage_scan_request(MEDIA_ROOT_PATH_INTERNAL, INTERNAL_STORAGE_ID, MS_SCAN_PART, NULL);
 
        /* update external storage */
-       if (ms_is_mmc_inserted()) {
-               ms_make_default_path_mmc();
-               ms_present_mmc_status(MS_SDCARD_INSERTED);
+       __ms_check_mmc_status();
+       __ms_check_usb_status();
+}
+
+
+////////////////////////////////////////////////////////////////////
+static int __ms_check_mmc_status(void)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       ms_stg_type_e stg_type = MS_STG_TYPE_MMC;
+       GArray *dev_list = NULL;
+
+       ret = ms_sys_get_device_list(stg_type, &dev_list);
+       if (ret == MS_MEDIA_ERR_NONE) {
+               if (dev_list != NULL) {
+                       MS_DBG_ERR("MMC FOUND[%d]", dev_list->len);
+                       int i = 0 ;
+                       int dev_num = dev_list->len;
+                       ms_block_info_s *block_info = NULL;
+
+                       for (i = 0; i < dev_num; i ++) {
+                               block_info = (ms_block_info_s *)g_array_index(dev_list , ms_stg_type_e*, i);
+                               ms_mmc_insert_handler(block_info->mount_path);
+                       }
+
+                       ms_sys_release_device_list(&dev_list);
+               } else {
+                       MS_DBG_ERR("MMC NOT FOUND");
+                       ms_mmc_remove_handler(NULL);
+               }
+       } else {
+               MS_DBG_ERR("ms_sys_get_device_list failed");
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+static int __ms_check_usb_status(void)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       ms_stg_type_e stg_type = MS_STG_TYPE_USB;
+       GArray *dev_list = NULL;
+
+       ret = ms_sys_get_device_list(stg_type, &dev_list);
+       if (ret == MS_MEDIA_ERR_NONE) {
+               if (dev_list != NULL) {
+                       MS_DBG_ERR("USB FOUND[%d]", dev_list->len);
+                       int i = 0 ;
+                       int dev_num = dev_list->len;
+                       ms_block_info_s *block_info = NULL;
+
+                       for (i = 0; i < dev_num; i ++) {
+                               block_info = (ms_block_info_s *)g_array_index(dev_list , ms_stg_type_e*, i);
+                               ms_usb_insert_handler(block_info->mount_path);
+                       }
 
-               ms_send_storage_scan_request(MS_STORAGE_EXTERNAL, ms_get_mmc_state());
+                       ms_sys_release_device_list(&dev_list);
+               } else {
+                       MS_DBG_ERR("USB NOT FOUND");
+               }
+       } else {
+               MS_DBG_ERR("ms_sys_get_device_list failed");
        }
+
+       return MS_MEDIA_ERR_NONE;
 }
 
index 2dc7548..93931c3 100755 (executable)
@@ -151,8 +151,9 @@ void ms_cleanup_scanner(void)
 
 static void _ms_add_timeout(guint interval, GSourceFunc func, gpointer data)
 {
-       MS_DBG("");
-       GSource *src;
+       GSource *src = NULL;
+
+       MS_DBG_FENTER();
 
        src = g_timeout_source_new_seconds(interval);
        g_source_set_callback(src, func, data, NULL);
@@ -186,7 +187,7 @@ int ms_scanner_start(void)
 
                err = unlink(MS_SCANNER_FIFO_PATH_RES);
                if (err !=0) {
-                       MS_DBG_STRERROR("unlink failed");
+                       MS_DBG_STRERROR("[No-Error] unlink failed");
                }
                err = mkfifo(MS_SCANNER_FIFO_PATH_RES, MS_SCANNER_FIFO_MODE);
                if (err !=0) {
@@ -214,7 +215,8 @@ int ms_scanner_start(void)
                        GIOChannel *res_channel = NULL;
                        GMainContext *res_context = NULL;
 
-                       MS_DBG_ERR("SCANNER is ready");
+                       MS_DBG_ERR("[No-Error] SCANNER is ready");
+
                        scanner_ready = true;
                        child_pid = pid;
                        /* attach result receiving socket to mainloop */
@@ -246,8 +248,9 @@ int ms_scanner_start(void)
                /* attach socket receive message callback */
        } else if(pid == 0) {
                /* child process */
-               MS_DBG_ERR("CHILD PROCESS");
-               MS_DBG("EXECUTE MEDIA SCANNER");
+               MS_DBG_ERR("[No-Error] CHILD PROCESS");
+               MS_DBG("[No-Error] EXECUTE MEDIA SCANNER");
+
                execl(MEDIA_SERVER_PATH, "media-scanner", NULL);
                g_mutex_unlock(&scanner_mutex);
        }
@@ -262,6 +265,13 @@ bool ms_get_scanner_status(void)
 
 void ms_reset_scanner_status(void)
 {
+       gboolean ret = FALSE;
+
+       ret = g_mutex_trylock(&scanner_mutex);
+       if (ret == FALSE) {
+               MS_DBG_ERR("scanner_mutex is already locked");
+       }
+
        child_pid = 0;
        scanner_ready = false;
 
index 78db07e..c8388b1 100755 (executable)
  * limitations under the License.
  *
  */
-
 #define _GNU_SOURCE
 #include <arpa/inet.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/socket.h>
 #include <fcntl.h>
 #include <sys/un.h>
 #include <errno.h>
@@ -35,8 +35,9 @@
 #include "media-util-internal.h"
 #include "media-server-ipc.h"
 #include "media-common-utils.h"
+#include "media-common-db-svc.h"
+#include "media-common-system.h"
 #include "media-server-dbg.h"
-#include "media-server-db-svc.h"
 #include "media-server-scanner.h"
 #include "media-server-socket.h"
 
@@ -140,48 +141,6 @@ static int __ms_delete_owner(ms_req_owner_data *owner_data)
        return MS_MEDIA_ERR_NONE;
 }
 
-static char* __ms_get_path(uid_t uid)
-{
-       char *result_psswd = NULL;
-       struct group *grpinfo = NULL;
-       int err = 0;
-
-       if(uid == getuid())
-       {
-               result_psswd = strdup(MEDIA_ROOT_PATH_INTERNAL);
-               grpinfo = getgrnam("users");
-               if(grpinfo == NULL) {
-                       MS_DBG_ERR("getgrnam(users) returns NULL !");
-                       return NULL;
-               }
-    }
-       else
-       {
-               struct passwd *userinfo = getpwuid(uid);
-               if(userinfo == NULL) {
-                       MS_DBG_ERR("getpwuid(%d) returns NULL !", uid);
-                       return NULL;
-               }
-               grpinfo = getgrnam("users");
-               if(grpinfo == NULL) {
-                       MS_DBG_ERR("getgrnam(users) returns NULL !");
-                       return NULL;
-               }
-               // Compare git_t type and not group name
-               if (grpinfo->gr_gid != userinfo->pw_gid) {
-                       MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
-                       return NULL;
-               }
-               err = asprintf(&result_psswd, "%s/%s", userinfo->pw_dir, MEDIA_CONTENT_PATH);
-               if(err < 0) {
-                       MS_DBG_ERR("asprintf failed![%d]", err);
-                       return NULL;
-               }
-       }
-
-       return result_psswd;
-}
-
 static int __ms_send_result_to_client(int pid, ms_comm_msg_s *recv_msg)
 {
        if(strlen(recv_msg->msg) == 0) {
@@ -388,12 +347,13 @@ int ms_send_scan_request(ms_comm_msg_s *send_msg, int client_sock)
        return res;
 }
 
-int ms_send_storage_scan_request(ms_storage_type_t storage_type, ms_dir_scan_type_t scan_type)
+int ms_send_storage_scan_request(const char *root_path, const char *storage_id, ms_dir_scan_type_t scan_type, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
        ms_comm_msg_s scan_msg = {
                .msg_type = MS_MSG_STORAGE_INVALID,
                .pid = 0, /* pid 0 means media-server */
+               .uid = 0,
                .result = -1,
                .msg_size = 0,
                .msg = {0},
@@ -421,22 +381,21 @@ int ms_send_storage_scan_request(ms_storage_type_t storage_type, ms_dir_scan_typ
        }
 
        /* msg_size & msg */
-       switch (storage_type) {
-               case MS_STORAGE_INTERNAL:
-                       scan_msg.msg_size = strlen(__ms_get_path(tzplatform_getuid(TZ_USER_NAME)));
-                       strncpy(scan_msg.msg, __ms_get_path(tzplatform_getuid(TZ_USER_NAME)), scan_msg.msg_size );
-                       break;
-               case MS_STORAGE_EXTERNAL:
-                       scan_msg.msg_size = strlen(MEDIA_ROOT_PATH_SDCARD);
-                       strncpy(scan_msg.msg, MEDIA_ROOT_PATH_SDCARD, scan_msg.msg_size );
-                       break;
-               default :
-                       ret = MS_MEDIA_ERR_INVALID_PARAMETER;
-                       MS_DBG_ERR("ms_send_storage_scan_request invalid parameter");
-                       goto ERROR;
-                       break;
+       if (root_path != NULL) {
+               scan_msg.msg_size = strlen(root_path);
+               strncpy(scan_msg.msg, root_path, scan_msg.msg_size );
+       }
+
+       if (storage_id != NULL) {
+               strncpy(scan_msg.storage_id, storage_id, MS_UUID_SIZE-1);
        }
 
+       if(uid == 0) {
+               ms_sys_get_uid(&uid);
+       }
+
+       scan_msg.uid = uid;
+
        ret = ms_send_scan_request(&scan_msg, -1);
 
 ERROR:
@@ -608,7 +567,7 @@ void _ms_process_tcp_message(gpointer data,  gpointer user_data)
        /* Disconnect DB*/
        media_db_disconnect(db_handle);
        MS_DBG_ERR("END");
-       if (g_atomic_int_dec_and_test(&cur_running_task) == FALSE) 
+       if (g_atomic_int_dec_and_test(&cur_running_task) == FALSE)
                MS_DBG_ERR("g_atomic_int_dec_and_test failed");
 
        return;
@@ -629,7 +588,7 @@ ERROR:
        /* Disconnect DB*/
        media_db_disconnect(db_handle);
        MS_DBG_ERR("END");
-       if (g_atomic_int_dec_and_test(&cur_running_task) == FALSE) 
+       if (g_atomic_int_dec_and_test(&cur_running_task) == FALSE)
                MS_DBG_ERR("g_atomic_int_dec_and_test failed");
 
        return;
@@ -775,3 +734,57 @@ int ms_remove_request_owner(int pid, const char *req_path)
        return MS_MEDIA_ERR_NONE;
 }
 
+int ms_send_storage_otg_scan_request(const char *path, const char *device_uuid, ms_dir_scan_type_t scan_type)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+
+       if (path == NULL) {
+               MS_DBG_ERR("Invalid path");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       if (device_uuid == NULL) {
+               MS_DBG_ERR("Invalid device_uuid");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       ms_comm_msg_s scan_msg = {
+               .msg_type = MS_MSG_STORAGE_INVALID,
+               .pid = 0, /* pid 0 means media-server */
+               .result = -1,
+               .msg_size = 0,
+               .storage_id = {0},
+               .msg = {0},
+       };
+
+       /* msg_type */
+       switch (scan_type) {
+               case MS_SCAN_PART:
+                       scan_msg.msg_type = MS_MSG_STORAGE_PARTIAL;
+                       break;
+               case MS_SCAN_ALL:
+                       scan_msg.msg_type = MS_MSG_STORAGE_ALL;
+                       break;
+               case MS_SCAN_INVALID:
+                       scan_msg.msg_type = MS_MSG_STORAGE_INVALID;
+                       break;
+               default :
+                       ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+                       MS_DBG_ERR("ms_send_storage_scan_request invalid parameter");
+                       goto ERROR;
+                       break;
+       }
+
+       /* msg_size & msg */
+       scan_msg.msg_size = strlen(path);
+       strncpy(scan_msg.msg, path, scan_msg.msg_size );
+       strncpy(scan_msg.storage_id, device_uuid, MS_UUID_SIZE-1);
+
+       ret = ms_send_scan_request(&scan_msg, -1);
+
+ERROR:
+
+       return ret;
+}
+
+