tizen 2.4 release accepted/tizen/2.4/mobile/20151029.032353 submit/tizen_2.4/20151028.064443 tizen_2.4_mobile_release
authorjk7744.park <jk7744.park@samsung.com>
Sat, 24 Oct 2015 07:54:46 +0000 (16:54 +0900)
committerjk7744.park <jk7744.park@samsung.com>
Sat, 24 Oct 2015 07:54:46 +0000 (16:54 +0900)
73 files changed:
LICENSE.APLv2.0
Makefile.am
NOTICE [new file with mode: 0755]
configure.ac
lib/include/media-server-ipc.h
lib/include/media-util-db.h
lib/include/media-util-dbg.h
lib/include/media-util-err.h
lib/include/media-util-internal.h
lib/include/media-util-ipc.h
lib/include/media-util-noti-common.h
lib/include/media-util-noti-internal.h [new file with mode: 0755]
lib/include/media-util-noti.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-noti-internal.c [new file with mode: 0755]
lib/media-util-noti.c
lib/media-util-register.c
media-server.manifest
media_content_config.ini [new file with mode: 0644]
packaging/media-scanner.service.wearable [deleted file]
packaging/media-server.service [moved from packaging/media-server.service.mobile with 71% similarity]
packaging/media-server.service.wearable [deleted file]
packaging/media-server.spec
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-security.h [new file with mode: 0644]
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-security.c [new file with mode: 0644]
src/common/media-common-system.c [new file with mode: 0755]
src/common/media-common-utils.c
src/mediadb-update.c [changed mode: 0644->0755]
src/scanner-v2/include/media-scanner-common-v2.h [new file with mode: 0644]
src/scanner-v2/include/media-scanner-dbg-v2.h [new file with mode: 0755]
src/scanner-v2/include/media-scanner-device-block-v2.h [new file with mode: 0755]
src/scanner-v2/include/media-scanner-extract-v2.h [new file with mode: 0755]
src/scanner-v2/include/media-scanner-scan-v2.h [new file with mode: 0755]
src/scanner-v2/include/media-scanner-socket-v2.h [moved from src/server/include/media-server-db-svc.h with 50% similarity]
src/scanner-v2/media-scanner-common-v2.c [new file with mode: 0644]
src/scanner-v2/media-scanner-device-block-v2.c [new file with mode: 0755]
src/scanner-v2/media-scanner-extract-v2.c [new file with mode: 0755]
src/scanner-v2/media-scanner-scan-v2.c [new file with mode: 0755]
src/scanner-v2/media-scanner-socket-v2.c [new file with mode: 0755]
src/scanner-v2/media-scanner-v2.c [new file with mode: 0755]
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/include/media-scanner-socket.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-dbg.h
src/server/include/media-server-device-block.h [new file with mode: 0755]
src/server/include/media-server-scanner.h
src/server/include/media-server-socket.h
src/server/include/media-server-thumb.h
src/server/media-server-db-svc.c [deleted file]
src/server/media-server-db.c
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
src/server/media-server-thumb.c

index f94008a..261eeb9 100644 (file)
@@ -1,5 +1,3 @@
-Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
-
                                  Apache License
                            Version 2.0, January 2004
                         http://www.apache.org/licenses/
@@ -201,6 +199,3 @@ Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
    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.
-
-
-
index 4cbb428..7dce0f2 100755 (executable)
@@ -1,4 +1,5 @@
 ACLOCAL_AMFLAGS = -I m4
+AUTOMAKE_OPTIONS = subdir-objects
 
 SUBDIRS = .
 
@@ -6,13 +7,52 @@ 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)
+
+if USE_NOTIFICATION
+COMMON_CFLAGS += $(STATUS_CFLAGS)
+endif
+
+COMMON_LDADD = libmedia-utils.la \
+               $(GLIB_LIBS) \
+               $(GTHREAD_LIBS) \
+               $(DLOG_LIBS) \
+               $(INIPARSER_LIBS) \
+               $(DBUS_LIBS) \
+               $(LIBPMCONTROL_LIBS) \
+               $(SECURITY_LIBS) \
+               $(SMACK_LIBS) \
+               -ldl
+
+if USE_NOTIFICATION
+COMMON_LDADD += $(STATUS_LIBS)
+endif
+
+######################################################
+#pkgconfig
+######################################################
 pkgconfigdir = $(libdir)/pkgconfig
 dist_pkgconfig_DATA = libmedia-utils.pc
 
-### libmedia-utils.la ###
+######################################################
+#libmedia utils
+######################################################
 lib_LTLIBRARIES = libmedia-utils.la
-libmedia_utils_la_SOURCES = lib/media-util-noti.c \
+libmedia_utils_la_SOURCES = lib/media-util-noti-internal.c \
+                           lib/media-util-noti.c \
                             lib/media-util-ipc.c \
                             lib/media-util-db.c \
                             lib/media-util-register.c
@@ -20,109 +60,99 @@ libmedia_utils_la_SOURCES = lib/media-util-noti.c \
 libmedia_utils_la_CFLAGS = -I${srcdir}/lib/include \
                               $(GLIB_CFLAGS) \
                              $(DLOG_CFLAGS) \
-                             $(DBUS_CFLAGS) \
+                             $(GIO_CFLAGS) \
                              $(SQLITE3_CFLAGS) \
                              $(DB_UTIL_CFLAGS) \
-                             $(PHONESTATUS_CFLAGS)
+                             $(PHONESTATUS_CFLAGS) \
+                             $(DBUS_CFLAGS)
 
 libmedia_utils_la_LIBADD  =   $(GLIB_LIBS) \
                               $(DLOG_LIBS) \
-                              $(DBUS_LIBS) \
+                             $(GIO_LIBS) \
                              $(SQLITE3_LIBS) \
                              $(DB_UTIL_LIBS) \
-                             $(PHONESTATUS_LIBS)
-
+                             $(PHONESTATUS_LIBS) \
+                             $(DBUS_LIBS)
 
-### file-manager-server ###
 bin_PROGRAMS = media-server \
                media-scanner \
+               media-scanner-v2 \
                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/common/media-common-security.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-main.c 
-
-media_server_CFLAGS = -I${srcdir}/src/common/include \
-                     -I${srcdir}/src/server/include \
-                      -I${srcdir}/lib/include \
-                             $(GTHREAD_CFLAGS) \
-                             $(GLIB_CFLAGS) \
-                             $(DLOG_CFLAGS) \
-                            $(PHONESTATUS_CFLAGS) \
-                            $(DBUS_CFLAGS)
-if USE_NOTIFICATION
-media_server_CFLAGS += $(STATUS_CFLAGS)
-endif
+                      src/server/media-server-device-block.c \
+                       src/server/media-server-main.c
 
-media_server_CFLAGS += $(LIBPMCONTROL_CFLAGS)
-
-media_server_CFLAGS += $(SECURITY_CFLAGS)
+media_server_CFLAGS = -I${srcdir}/src/server/include \
+                       $(COMMON_CFLAGS) \
+                        $(PHONESTATUS_CFLAGS)
 
 media_server_CFLAGS += -fPIE
 
 media_server_LDFLAGS = -pie
 
-media_server_LDADD = libmedia-utils.la \
-                             $(GLIB_LIBS) \
-                              $(GTHREAD_LIBS) \
-                              $(PHONESTATUS_LIBS) \
-                              $(DLOG_LIBS) \
-                             $(AUL_LIBS) \
-                             $(DBUS_LIBS) \
-                              -ldl
-if USE_NOTIFICATION
-media_server_LDADD += $(STATUS_LIBS)
-endif
+media_server_LDADD = $(COMMON_LDADD) \
+                    $(PHONESTATUS_LIBS)
 
-media_server_LDADD += $(LIBPMCONTROL_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/common/media-common-db-svc.c \
+                       src/common/media-common-security.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}/src/scanner/include \
+                       $(COMMON_CFLAGS)
 
-media_server_LDADD += $(SECURITY_LIBS)
+media_scanner_CFLAGS += -fPIE
 
+media_scanner_LDFLAGS = -pie
 
+media_scanner_LDADD = $(COMMON_LDADD)
 
-media_scanner_SOURCES = src/common/media-common-utils.c \
-                       src/common/media-common-external-storage.c \
-                               src/scanner/media-scanner-db-svc.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) \
-                             $(DBUS_CFLAGS)
-if USE_NOTIFICATION
-media_scanner_CFLAGS += $(STATUS_CFLAGS)
-endif
+######################################################
+#media scanner v2
+######################################################
+media_scanner_v2_SOURCES = src/common/media-common-utils.c \
+                       src/common/media-common-system.c \
+                        src/common/media-common-external-storage.c \
+                       src/common/media-common-db-svc.c \
+                       src/common/media-common-security.c \
+                       src/scanner-v2/media-scanner-common-v2.c \
+                       src/scanner-v2/media-scanner-device-block-v2.c\
+                        src/scanner-v2/media-scanner-scan-v2.c \
+                        src/scanner-v2/media-scanner-socket-v2.c \
+                       src/scanner-v2/media-scanner-extract-v2.c \
+                        src/scanner-v2/media-scanner-v2.c
 
-media_scanner_CFLAGS += $(LIBPMCONTROL_CFLAGS)
+media_scanner_v2_CFLAGS = -I${srcdir}/src/scanner-v2/include \
+                          $(COMMON_CFLAGS)
 
-media_scanner_CFLAGS += -fPIE
+media_scanner_v2_CFLAGS += -fPIE
 
-media_scanner_LDFLAGS = -pie
+media_scanner_v2_LDFLAGS = -pie
 
-media_scanner_LDADD = libmedia-utils.la \
-                              $(GLIB_LIBS) \
-                              $(GTHREAD_LIBS) \
-                              $(PHONESTATUS_LIBS) \
-                              $(DLOG_LIBS) \
-                              $(DBUS_LIBS) \
-                              -ldl
-if USE_NOTIFICATION
-media_scanner_LDADD += $(STATUS_LIBS)
-endif
-
-media_scanner_LDADD += $(LIBPMCONTROL_LIBS)
+media_scanner_v2_LDADD = $(COMMON_LDADD)
 
+######################################################
+#mediadb_udpate
+######################################################
 mediadb_update_SOURCES = src/mediadb-update.c
 
 mediadb_update_CFLAGS = -I${srcdir}/lib/include \
@@ -135,11 +165,13 @@ mediadb_update_LDFLAGS = -pie
 
 mediadb_update_LDADD = libmedia-utils.la \
                               $(GLIB_LIBS) \
-                              $(GTHREAD_LIBS)
+                              $(GTHREAD_LIBS) \
+                              -ldl
 
 ### includeheaders ###
 includeheadersdir = $(includedir)/media-utils
 includeheaders_HEADERS = lib/include/media-util-noti-common.h \
+                        lib/include/media-util-noti-internal.h \
                         lib/include/media-util-noti.h \
                          lib/include/media-util-register.h \
                          lib/include/media-util-err.h \
diff --git a/NOTICE b/NOTICE
new file mode 100755 (executable)
index 0000000..ccdad52
--- /dev/null
+++ b/NOTICE
@@ -0,0 +1,3 @@
+Copyright (c) Samsung Electronics Co., Ltd. All rights reserved.
+Except as noted, this software is licensed under Apache License, Version 2.
+Please, see the LICENSE file for Apache License terms and conditions.
index edcd8bf..3e07d86 100755 (executable)
@@ -38,6 +38,8 @@ CPPFLAGS="${CPPFLAGS} -DRND_LINUX"
 #For supporting Large file
 CPPFLAGS="${CPPFLAGS} -D_FILE_OFFSET_BITS=64"
 
+#CPPFLAGS="${CPPFLAGS} -D_USE_TV_PROFILE"
+
 # FMS_DEBUG - File Manager Service debug options
 # To open debug options:
 # export FMS_DEBUG=1 or configure --enable-debug
@@ -77,6 +79,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)
@@ -85,6 +91,14 @@ PKG_CHECK_MODULES(DB_UTIL, db-util)
 AC_SUBST(DB_UTIL_CFLAGS)
 AC_SUBST(DB_UTIL_LIBS)
 
+PKG_CHECK_MODULES(INIPARSER, iniparser)
+AC_SUBST(INIPARSER_CFLAGS)
+AC_SUBST(INIPARSER_LIBS)
+
+PKG_CHECK_MODULES(SMACK, libsmack)
+AC_SUBST(SMACK_CFLAGS)
+AC_SUBST(SMACK_LIBS)
+
 dnl use notification --------------------------------------------------------------------------
 AC_ARG_ENABLE(notification, AC_HELP_STRING([--enable-notification], [using notification]),
 [
index 36f667f..9d7a893 100755 (executable)
  *
  */
 
-/**
- * This file defines IPC protocol
- *
- * @file               media-server-ipc.h
- * @author     Haejeong Kim(backto.kim@samsung.com)
- * @version    1.0
- * @brief
- */
 #ifndef _MEDIA_SERVER_IPC_H_
 #define _MEDIA_SERVER_IPC_H_
 
@@ -66,6 +58,9 @@ typedef enum{
        MS_MSG_SCANNER_RESULT,                          /**< Result of directory scanning */
        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;
 
@@ -79,12 +74,15 @@ 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;
        int pid;
        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;
 
@@ -104,7 +102,7 @@ typedef struct {
 
 typedef struct _thumbMsg{
        int msg_type;
-       int thumb_type;
+       int request_id;
        int status;
        int pid;
        int thumb_size;
@@ -114,9 +112,14 @@ typedef struct _thumbMsg{
        int origin_height;
        int origin_path_size;
        int dest_path_size;
+       unsigned char *thumb_data;
        char org_path[MAX_FILEPATH_LEN];
        char dst_path[MAX_FILEPATH_LEN];
 } thumbMsg;
 
+typedef struct _thumbRawAddMsg{
+       int thumb_size;
+       unsigned char *thumb_data;
+} thumbRawAddMsg;
 
 #endif /*_MEDIA_SERVER_IPC_H_*/
index efbea0f..183fd2e 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-util-noti.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
- #ifndef _MEDIA_UTIL_DB_H_
+#ifndef _MEDIA_UTIL_DB_H_
 #define _MEDIA_UTIL_DB_H_
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+#include <stdbool.h>
+
 typedef void MediaDBHandle;            /**< Handle */
 
-int media_db_connect(MediaDBHandle **handle);
+int media_db_connect(pid_t pid, MediaDBHandle **handle, bool need_write);
 
 int media_db_disconnect(MediaDBHandle *handle);
 
index d5e06a3..13e19b8 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-util-dbg.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
 #ifndef _MEDIA_UTIL_DBG_H_
 #define _MEDIA_UTIL_DBG_H_
 
index c39e7c4..fcb6c37 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-util-err.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
 #ifndef _MEDIA_UTIL_ERR_H_
 #define _MEDIA_UTIL_ERR_H_
 
-#define MS_MEDIA_ERR_NONE 0
+#define MS_MEDIA_ERR_NONE                                              0
 
-/* internal operation error*/
-#define MS_MEDIA_ERR_INTERNAL                          -1
-#define MS_MEDIA_ERR_INVALID_PARAMETER                         -2   /* invalid parameter(s) */
-#define MS_MEDIA_ERR_INVALID_PATH                      -3   /* Invalid path */
-#define MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL              -4   /* exception of memory allocation */
-#define MS_MEDIA_ERR_NOT_ENOUGH_SPACE                  -5   /* not enough space in storage */
+/* Internal operation error*/
+#define MS_MEDIA_ERR_INTERNAL                                  -1
+#define MS_MEDIA_ERR_INVALID_PARAMETER                         -2  /* Invalid parameter(s) */
+#define MS_MEDIA_ERR_INVALID_PATH                              -3  /* Invalid path */
+#define MS_MEDIA_ERR_OUT_OF_MEMORY                             -4  /* Out of memory */
 
 /* DB operation error*/
-#define MS_MEDIA_ERR_DB_CONNECT_FAIL                   -11  /* connecting database fails */
-#define MS_MEDIA_ERR_DB_DISCONNECT_FAIL                -12  /* disconnecting database fails */
-#define MS_MEDIA_ERR_DB_INSERT_FAIL                    -13  /* inserting record fails */
-#define MS_MEDIA_ERR_DB_DELETE_FAIL                    -14  /* deleting record fails */
-#define MS_MEDIA_ERR_DB_UPDATE_FAIL                    -15  /* updating record fails */
-#define MS_MEDIA_ERR_DB_BUSY_FAIL                      -16  /* DB Busy */
-#define MS_MEDIA_ERR_DB_CONSTRAINT_FAIL                -17  /* DB CONSTRAINT fails - In case of insert, the record already exists */
-#define MS_MEDIA_ERR_DB_BATCH_UPDATE_BUSY              -18  /* Batch update thread is full */
+#define MS_MEDIA_ERR_DB_CONNECT_FAIL                           -101  /* connecting database fails */
+#define MS_MEDIA_ERR_DB_DISCONNECT_FAIL                        -102  /* disconnecting database fails */
+#define MS_MEDIA_ERR_DB_INSERT_FAIL                            -103  /* inserting record fails */
+#define MS_MEDIA_ERR_DB_DELETE_FAIL                            -104  /* deleting record fails */
+#define MS_MEDIA_ERR_DB_UPDATE_FAIL                            -105  /* updating record fails */
+#define MS_MEDIA_ERR_DB_BUSY_FAIL                                      -106  /* DB Busy */
+#define MS_MEDIA_ERR_DB_SERVER_BUSY_FAIL                       -107  /* DB Server Busy */
+#define MS_MEDIA_ERR_DB_CONSTRAINT_FAIL                        -108  /* DB CONSTRAINT fails - In case of insert, the record already exists */
+#define MS_MEDIA_ERR_DB_BATCH_UPDATE_BUSY              -109  /* Batch update thread is full */
+#define MS_MEDIA_ERR_DB_NO_RECORD                              -110  /* Item not found in DB */
+#define MS_MEDIA_ERR_DB_CORRUPT                                        -112  /* DB corrut error */
+#define MS_MEDIA_ERR_DB_PERMISSION                             -113  /* DB permission error */
+#define MS_MEDIA_ERR_DB_FULL_FAIL                                      -114  /* DB storage full error */
+#define MS_MEDIA_ERR_DB_INTERNAL                                       -150  /* DB internal error */
 
 /* IPC operation error*/
-#define MS_MEDIA_ERR_SOCKET_INTERNAL                   -21  /* receive error from socket API */
-#define MS_MEDIA_ERR_SOCKET_CONN                       -22  /* socket connect error */
-#define MS_MEDIA_ERR_SOCKET_BIND                       -23  /* socket binding fails */
-#define MS_MEDIA_ERR_SOCKET_SEND                       -24  /* socket sending fails */
-#define MS_MEDIA_ERR_SOCKET_RECEIVE                    -25  /* socket receiving fails */
-#define MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT            -26  /* socket receive timeout error */
-#define MS_MEDIA_ERR_SOCKET_ACCEPT                     -27  /* socket accept fails */
-
-#define MS_MEDIA_ERR_DBUS_ADD_FILTER                   -31  /* DBUS add filter fails*/
-#define MS_MEDIA_ERR_DBUS_GET                          -32  /* DBUS get fails */
-
-#define MS_MEDIA_ERR_FIFO_MAKE_FAIL                    -41   /* FIFO making fails */
+#define MS_MEDIA_ERR_SOCKET_CONN                                       -201  /* socket connect error */
+#define MS_MEDIA_ERR_SOCKET_BIND                                       -202  /* socket binding fails */
+#define MS_MEDIA_ERR_SOCKET_SEND                                       -203  /* socket sending fails */
+#define MS_MEDIA_ERR_SOCKET_RECEIVE                            -204  /* socket receiving fails */
+#define MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT            -205  /* socket receive timeout error */
+#define MS_MEDIA_ERR_SOCKET_ACCEPT                             -206  /* socket accept fails */
+#define MS_MEDIA_ERR_SOCKET_INTERNAL                           -250  /* receive error from socket API */
 
 /* DIRECTORY error*/
-#define MS_MEDIA_ERR_DIR_OPEN_FAIL                     -51   /* direcotry opennig fails */
-#define MS_MEDIA_ERR_DIR_CLOSE_FAIL                    -53   /* directory closing fails */
-#define MS_MEDIA_ERR_DIR_READ_FAIL                     -52   /* directory reading fails */
+#define MS_MEDIA_ERR_DIR_OPEN_FAIL                             -501  /* direcotry opennig fails */
+#define MS_MEDIA_ERR_DIR_CLOSE_FAIL                            -502  /* directory closing fails */
+#define MS_MEDIA_ERR_DIR_READ_FAIL                             -503  /* directory reading fails */
 
 /* FILE error*/
-#define MS_MEDIA_ERR_FILE_OPEN_FAIL                    -61   /* file opennig fails */
-#define MS_MEDIA_ERR_FILE_CLOSE_FAIL                   -62   /* file closing fails */
-#define MS_MEDIA_ERR_FILE_READ_FAIL                    -63   /* file reading fails */
-#define MS_MEDIA_ERR_FILE_WRITE_FAIL                   -64   /* file writing fails */
+#define MS_MEDIA_ERR_FILE_OPEN_FAIL                            -601  /* file opennig fails */
+#define MS_MEDIA_ERR_FILE_CLOSE_FAIL                           -602  /* file closing fails */
+#define MS_MEDIA_ERR_FILE_READ_FAIL                            -603  /* file reading fails */
+#define MS_MEDIA_ERR_FILE_WRITE_FAIL                           -604  /* file writing fails */
+#define MS_MEDIA_ERR_FILE_NOT_EXIST                            -605  /* file does not exist */
 
 /* MEDIA SERVER error*/
-#define MS_MEDIA_ERR_DB_SERVER_BUSY_FAIL               -101  /* DB server busy */
-#define MS_MEDIA_ERR_SCANNER_FORCE_STOP                        -102  /* scanning is stopped forcely */
-#define MS_MEDIA_ERR_PERMISSION_DENIED                 -103  /* Do have permission of request */
+#define MS_MEDIA_ERR_SCANNER_FORCE_STOP                        -701  /* scanning is stopped forcely */
+#define MS_MEDIA_ERR_PERMISSION_DENIED                 -702  /* Do have permission of request */
+
+/* Thumbnail error*/
+#define MS_MEDIA_ERR_THUMB_TOO_BIG                             -801  /* Original is too big to make thumb */
+#define MS_MEDIA_ERR_THUMB_DUPLICATED_REQUEST  -802  /* Duplicated request of same path */
 
 /*ETC*/
-#define MS_MEDIA_ERR_VCONF_SET_FAIL                    -201  /* vconf setting fails*/
-#define MS_MEDIA_ERR_VCONF_GET_FAIL                    -202  /* vconf getting fails*/
-#define MS_MEDIA_ERR_SCANNER_NOT_READY                 -203  /* scanner is not ready */
-#define MS_MEDIA_ERR_DYNAMIC_LINK                      -204  /* fail to dynamic link */
-#define MS_MEDIA_ERR_INVALID_IPC_MESSAGE               -205  /* received message is not valid */
-#define MS_MEDIA_ERR_DATA_TAINTED                      -206  /* received data is tainted */
-#define MS_MEDIA_ERR_SEND_NOTI_FAIL                    -207  /* sending notification is failed */
+#define MS_MEDIA_ERR_VCONF_SET_FAIL                            -901  /* vconf setting fails*/
+#define MS_MEDIA_ERR_VCONF_GET_FAIL                            -902  /* vconf getting fails*/
+#define MS_MEDIA_ERR_SCANNER_NOT_READY                 -903  /* scanner is not ready */
+#define MS_MEDIA_ERR_DYNAMIC_LINK                              -904  /* fail to dynamic link */
+#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
+#define MS_MEDIA_ERR_MAX                                                       -999
 
 #endif /*_MEDIA_UTIL_ERR_H_*/
index 7cf8396..3fa1b0d 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-util-internal.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
-
 #ifndef _MEDIA_UTIL_INTERNAL_H_
 #define _MEDIA_UTIL_INTERNAL_H_
 
@@ -42,7 +33,7 @@
 
 #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); memset(src, 0x0, size);} }
+                                                       else { src = malloc(size); if(src) memset(src, 0x0, size);} }
 #define MS_STRING_VALID(str)   \
        ((str != NULL && strlen(str) > 0) ? TRUE : FALSE)
 
index aa4d8c5..d34d178 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of IPC.
- *
- * @file               media-util-ipc.h
- * @author     Haejeong Kim(backto.kim@samsung.com)
- * @version    1.0
- * @brief
- */
- #ifndef _MEDIA_UTIL_IPC_H_
+#ifndef _MEDIA_UTIL_IPC_H_
 #define _MEDIA_UTIL_IPC_H_
 
 #ifdef __cplusplus
@@ -56,6 +48,7 @@ int ms_ipc_send_msg_to_client_tcp(int sockfd, ms_comm_msg_s *send_msg, struct so
 int ms_ipc_receive_message(int sockfd, void *recv_msg, unsigned int msg_size, struct sockaddr_un *client_addr, unsigned int *size);
 int ms_ipc_receive_message_tcp(int client_sock, ms_comm_msg_s *recv_msg);
 int ms_ipc_wait_message(int sockfd, void  *recv_msg, unsigned int msg_size, struct sockaddr_un *recv_addr, unsigned int *size);
+int ms_ipc_wait_block_message(int sockfd, void  *recv_msg, unsigned int msg_size, struct sockaddr_un *recv_addr, unsigned int *size);
 int ms_ipc_delete_client_socket(ms_sock_info_s* sock_info);
 int ms_ipc_accept_client_tcp(int serv_sock, int* client_sock);
 
index 60e8cf4..c259b40 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-util-not-commoni.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
- #ifndef _MEDIA_UTIL_NOTI_COMMON_H_
+#ifndef _MEDIA_UTIL_NOTI_COMMON_H_
 #define _MEDIA_UTIL_NOTI_COMMON_H_
 
 #ifdef __cplusplus
diff --git a/lib/include/media-util-noti-internal.h b/lib/include/media-util-noti-internal.h
new file mode 100755 (executable)
index 0000000..3e58f4a
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ *  Media Utility
+ *
+ * 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-util-noti.h
+ * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
+ * @version    1.0
+ * @brief
+ */
+ #ifndef _MEDIA_UTIL_NOTI_INTERNAL_H_
+#define _MEDIA_UTIL_NOTI_INTERNAL_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "media-util-noti-common.h"
+
+/**
+* @fn          int media_db_update_subscribe(void);
+* @brief               This function announce media database is updated to other applications.<br>
+* @return      This function returns 0 on success, and -1 on failure.
+* @param[in]   none
+* @remark      This function is recommandation for other application being aware of database updating.<br>
+* @par example
+* @code
+
+#include <stdio.h>
+#include <glib.h>
+#include <media-util-noti.h>
+
+void callback()
+{
+        printf("listen dbus from media-server\n");
+}
+
+int
+main (int argc, char **argv)
+{
+       GMainLoop *loop;
+
+       loop = g_main_loop_new (NULL, FALSE);
+
+       media_db_update_subscribe(callback);
+
+       g_main_loop_run (loop);
+
+       return 0;
+}
+
+*/
+
+int media_db_update_send_internal(int pid, /* mandatory */
+                                                       media_item_type_e item, /* mandatory */
+                                                       media_item_update_type_e update_type, /* mandatory */
+                                                       char* path, /* mandatory */
+                                                       char* uuid, /* optional */
+                                                       media_type_e media_type, /* optional */
+                                                       char *mime_type /* optional */
+                                                       );
+
+int media_db_update_subscribe_internal(MediaNotiHandle *handle, db_update_cb user_cb, void *user_data);
+
+int media_db_update_unsubscribe_internal(MediaNotiHandle handle, clear_user_data_cb clear_cb);
+
+
+/**
+* @}
+*/
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /*_MEDIA_UTIL_NOTI_INTERNAL_H_*/
index c7e8b28..155b48f 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-util-noti.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
- #ifndef _MEDIA_UTIL_NOTI_H_
+#ifndef _MEDIA_UTIL_NOTI_H_
 #define _MEDIA_UTIL_NOTI_H_
 
 #ifdef __cplusplus
index cc6e16e..0147440 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-util-register.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
- #ifndef _MEDIA_UTIL_REGISTER_H_
+#ifndef _MEDIA_UTIL_REGISTER_H_
 #define _MEDIA_UTIL_REGISTER_H_
 
 #include <glib.h>
@@ -54,7 +46,9 @@ 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);
+int media_directory_scanning_async(const char *directory_path, const char *storage_id, bool recursive_on, scan_complete_cb user_callback, void *user_data);
+
+int media_directory_scanning_cancel(const char *directory_path);
 
 int media_files_register(const char *list_path, insert_complete_cb user_callback, void *user_data);
 
index f7e6948..c07cc9c 100755 (executable)
 #include <media-util-register.h>
 #include <media-util-db.h>
 #include <media-util-noti.h>
+#include <media-util-noti-internal.h>
 #include <media-util-ipc.h>
 
 #define MOUNT_PATH "/opt/usr"
 #define STORAGE_PATH "/opt/storage"
 
 #define MEDIA_ROOT_PATH_INTERNAL       MOUNT_PATH"/media"
+#define MEDIA_ROOT_PATH_EXTERNAL       STORAGE_PATH
 #define MEDIA_ROOT_PATH_SDCARD STORAGE_PATH"/sdcard"
-
+#define MEDIA_ROOT_PATH_USB            STORAGE_PATH
+#define MEDIA_ROOT_PATH_CLOUD  STORAGE_PATH"/tnfs/cloud"
 #define MEDIA_THUMB_ROOT_PATH          MOUNT_PATH"/share/media"
 #define MEDIA_DB_NAME                          MOUNT_PATH"/dbspace/.media.db"          /**<  media db name*/
-#define MEDIA_DATA_PATH                        MOUNT_PATH"/data/file-manager-service"
+#define MEDIA_DATA_PATH                        MOUNT_PATH"/apps/media-server/data"
 
 #endif /*_MEDIA_UTIL_H_*/
index a094eee..95bc281 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of Media DB.
- *
- * @file               media-util-db.c
- * @author     Haejeong Kim(backto.kim@samsung.com)
- * @version    1.0
- * @brief
- */
-
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
@@ -41,7 +32,7 @@ static __thread char **sql_list = NULL;
 static __thread int g_list_idx = 0;
 
 static int __media_db_busy_handler(void *pData, int count);
-static int __media_db_connect_db_with_handle(sqlite3 **db_handle);
+static int __media_db_connect_db_with_handle(pid_t pid, sqlite3 **db_handle, bool need_write);
 static int __media_db_disconnect_db_with_handle(sqlite3 *db_handle);
 
 static void __media_db_destroy_sql_list()
@@ -62,16 +53,23 @@ 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 int __media_db_connect_db_with_handle(sqlite3 **db_handle)
+static int __media_db_connect_db_with_handle(pid_t pid, sqlite3 **db_handle, bool need_write)
 {
-       int ret = MS_MEDIA_ERR_NONE;
+       int ret = SQLITE_OK;
+       char db_path[256] = {0, };
+       memset(db_path, 0, sizeof(db_path));
 
-       /*Connect DB*/
-       ret = db_util_open(MEDIA_DB_NAME, db_handle, DB_UTIL_REGISTER_HOOK_METHOD);
+       snprintf(db_path, sizeof(db_path), "%s", MEDIA_DB_NAME);
 
+       /*Connect DB*/
+       if (need_write) {
+               ret = db_util_open_with_options(db_path, db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
+       } else {
+               ret = db_util_open_with_options(db_path, db_handle, SQLITE_OPEN_READONLY, NULL);
+       }
        if (SQLITE_OK != ret) {
 
                MSAPI_DBG_ERR("error when db open");
@@ -335,24 +333,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
+               } else if (ret == SQLITE_FULL) {
+                       ret = MS_MEDIA_ERR_DB_FULL_FAIL;
+               } 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)
@@ -361,14 +374,14 @@ static int _media_db_update_directly(sqlite3 *db_handle, const char *sql_str)
        return ret;
 }
 
-int media_db_connect(MediaDBHandle **handle)
+int media_db_connect(pid_t pid, MediaDBHandle **handle, bool need_write)
 {
        int ret = MS_MEDIA_ERR_NONE;
        sqlite3 * db_handle = NULL;
 
        MSAPI_DBG_FUNC();
 
-       ret = __media_db_connect_db_with_handle(&db_handle);
+       ret = __media_db_connect_db_with_handle(pid, &db_handle, need_write);
        MSAPI_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
 
        *handle = db_handle;
@@ -484,9 +497,9 @@ int media_db_update_db_batch_start(const char *query_str)
                ret = MS_MEDIA_ERR_DB_SERVER_BUSY_FAIL;
        } else {
                sql_list = (char**)malloc(sizeof(char*));
-               MSAPI_RETVM_IF(sql_list == NULL, MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL, "Out of memory");
+               MSAPI_RETVM_IF(sql_list == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "Out of memory");
                sql_list[g_list_idx++] = strdup(query_str);
-               MSAPI_RETVM_IF(sql_list[g_list_idx - 1] == NULL, MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL, "Out of memory");
+               MSAPI_RETVM_IF(sql_list[g_list_idx - 1] == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "Out of memory");
        }
 
        return ret;
@@ -499,10 +512,10 @@ int media_db_update_db_batch(const char *query_str)
        MSAPI_RETVM_IF(!MS_STRING_VALID(query_str), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid Query");
 
        sql_list = (char**)realloc(sql_list, (g_list_idx + 1) * sizeof(char*));
-       MSAPI_RETVM_IF(sql_list == NULL, MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL, "Out of memory");
+       MSAPI_RETVM_IF(sql_list == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "Out of memory");
 
        sql_list[g_list_idx++] = strdup(query_str);
-       MSAPI_RETVM_IF(sql_list[g_list_idx - 1] == NULL, MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL, "Out of memory");
+       MSAPI_RETVM_IF(sql_list[g_list_idx - 1] == NULL, MS_MEDIA_ERR_OUT_OF_MEMORY, "Out of memory");
 
        return ret;
 }
@@ -522,14 +535,14 @@ int media_db_update_db_batch_end(MediaDBHandle *handle, const char *query_str)
        if (sql_list == NULL) {
                __media_db_destroy_sql_list();
                MSAPI_DBG_ERR("Out of memory");
-               return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
        }
 
        sql_list[g_list_idx++] = strdup(query_str);
        if (sql_list[g_list_idx - 1] == NULL) {
                __media_db_destroy_sql_list();
                MSAPI_DBG_ERR("Out of memory");
-               return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
        }
 
        int i = 0;
index e1c67c0..8d6e212 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of IPC.
- *
- * @file               media-util-ipc.c
- * @author     Haejeong Kim(backto.kim@samsung.com)
- * @version    1.0
- * @brief
- */
-
 #include <string.h>
 #include <unistd.h>
 #include <errno.h>
@@ -35,6 +26,7 @@
 
 #include "media-util-dbg.h"
 #include "media-util.h"
+#include "media-util-internal.h"
 
 char MEDIA_IPC_PATH[][50] ={
        {"/tmp/.media_ipc_dbbatchupdate"},
@@ -49,6 +41,7 @@ char MEDIA_IPC_PATH[][50] ={
 #define MS_SOCK_PATH_PRFX "/tmp/.media_ipc_client"
 #define MS_SOCK_PATH_TEMPLATE "XXXXXX"
 #define MS_SOCK_PATH MS_SOCK_PATH_PRFX MS_SOCK_PATH_TEMPLATE
+#define MS_SOCK_UDP_BLOCK_SIZE 512
 
 static const char abc[] =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
@@ -208,7 +201,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;
@@ -237,13 +230,11 @@ int ms_ipc_create_server_socket(ms_protocol_e protocol, ms_msg_port_type_e port,
        }
 
        /*change permission of sock file*/
-       if (chmod(MEDIA_IPC_PATH[serv_port], 0660) < 0) {
+       if (chmod(MEDIA_IPC_PATH[serv_port], 0660) < 0)
                MSAPI_DBG_STRERROR("chmod failed");
-       }
-
-       if (chown(MEDIA_IPC_PATH[serv_port], 0, 5000) < 0) {
+       if (chown(MEDIA_IPC_PATH[serv_port], 200, 5000) < 0)
                MSAPI_DBG_STRERROR("chown failed");
-       }
+
        *sock_fd = sock;
 
        return MS_MEDIA_ERR_NONE;
@@ -267,7 +258,7 @@ int ms_ipc_send_msg_to_server(int sockfd, ms_msg_port_type_e port, ms_comm_msg_s
                MSAPI_DBG("sent result [%d]", send_msg->result);
                MSAPI_DBG_SLOG("result message [%s]", send_msg->msg);
                if (serv_addr != NULL)
-       *serv_addr = addr;
+                       *serv_addr = addr;
        }
 
        return res;
@@ -289,7 +280,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))) {
@@ -386,6 +383,56 @@ int ms_ipc_wait_message(int sockfd, void *recv_msg, unsigned int msg_size, struc
        return MS_MEDIA_ERR_NONE;
 }
 
+int ms_ipc_wait_block_message(int sockfd, void *recv_msg, unsigned int msg_size, struct sockaddr_un *recv_addr, unsigned int *addr_size)
+{
+       int recv_msg_size;
+       socklen_t addr_len;
+       unsigned char *block_buf;
+       int block_size = 0;
+       int recv_size = 0;
+       unsigned int remain_size = msg_size;
+
+       if (!recv_msg ||!recv_addr)
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+
+       addr_len = sizeof(struct sockaddr_un);
+       block_size = MS_SOCK_UDP_BLOCK_SIZE;
+       block_buf = malloc(block_size * sizeof(unsigned char));
+       if(block_buf == NULL) {
+               MSAPI_DBG_ERR("malloc failed.");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
+       memset(block_buf, 0, block_size * sizeof(unsigned char));
+
+       while(remain_size > 0) {
+               if(remain_size < MS_SOCK_UDP_BLOCK_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_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_STRERROR("recvfrom error");
+                               MS_SAFE_FREE(block_buf);
+                               return MS_MEDIA_ERR_SOCKET_RECEIVE;
+                       }
+               }
+
+               memcpy(recv_msg+recv_size, block_buf, block_size);
+               recv_size += block_size;
+               remain_size -= block_size;
+       }
+       if (addr_size != NULL)
+               *addr_size  = addr_len;
+
+       MS_SAFE_FREE(block_buf);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
 int ms_ipc_accept_client_tcp(int serv_sock, int* client_sock)
 {
        int sockfd = -1;
diff --git a/lib/media-util-noti-internal.c b/lib/media-util-noti-internal.c
new file mode 100755 (executable)
index 0000000..4ce1932
--- /dev/null
@@ -0,0 +1,373 @@
+/*
+ *  Media Utility
+ *
+ * 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-util-noti.c
+ * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
+ * @version    1.0
+ * @brief
+ */
+
+#include <sys/errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <glib.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib-lowlevel.h>
+
+#include "media-util-internal.h"
+#include "media-util-dbg.h"
+#include "media-util.h"
+#include "media-util-noti-internal.h"
+
+GArray *handle_list_internal;
+static GMutex mutex_internal;
+int ref_count;
+
+#define MS_MEDIA_DBUS_NAME_INTERNAL "ms_db_updated_internal"
+
+typedef struct internal_noti_cb_data{
+       DBusConnection *dbus;
+       db_update_cb user_callback;
+       void *user_data;
+} internal_noti_cb_data;
+
+int media_db_update_send_internal(int pid, /* mandatory */
+                                                       media_item_type_e item, /* mandatory */
+                                                       media_item_update_type_e update_type, /* mandatory */
+                                                       char* path, /* mandatory */
+                                                       char* uuid, /* optional */
+                                                       media_type_e media_type, /* optional */
+                                                       char *mime_type /* optional */
+                                                       )
+{
+       DBusMessage *message = NULL;
+       DBusConnection *bus = NULL;
+       DBusError error;
+       unsigned char *path_array = NULL;
+       int path_length = strlen(path) + 1;
+
+       /* Get a connection to the session bus */
+       dbus_error_init (&error);
+       bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
+       if (!bus) {
+               MSAPI_DBG ("Failed to connect to the D-BUS daemon: %s", error.message);
+               dbus_error_free (&error);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       message = dbus_message_new_signal (MS_MEDIA_DBUS_PATH, MS_MEDIA_DBUS_INTERFACE, MS_MEDIA_DBUS_NAME_INTERNAL);
+       if (message != NULL) {
+               path_array = malloc(sizeof(unsigned char) * path_length);
+               memcpy(path_array, path, path_length);
+
+               if (item == MS_MEDIA_ITEM_FILE) {
+                       MSAPI_DBG("FILE CHANGED");
+                       if (uuid != NULL && mime_type != NULL) {
+                       /* fill all datas */
+                               dbus_message_append_args (message,
+                                                                               DBUS_TYPE_INT32, &item,
+                                                                               DBUS_TYPE_INT32, &pid,
+                                                                               DBUS_TYPE_INT32, &update_type,
+                                                                               DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &path_array, path_length,
+                                                                               DBUS_TYPE_STRING, &uuid,
+                                                                               DBUS_TYPE_INT32, &media_type,
+                                                                               DBUS_TYPE_STRING, &mime_type,
+                                                                               DBUS_TYPE_INVALID);
+                       } else {
+                               MSAPI_DBG_ERR("uuid or mime_type is NULL");
+                               MS_SAFE_FREE(path_array);
+                               dbus_message_unref (message);
+                               return MS_MEDIA_ERR_INVALID_PARAMETER;
+                       }
+               } else if (item == MS_MEDIA_ITEM_DIRECTORY) {
+                       MSAPI_DBG("DIRECTORY CHANGED");
+                       /* fill all datas */
+                       if(uuid != NULL) {
+                               dbus_message_append_args (message,
+                                                                               DBUS_TYPE_INT32, &item,
+                                                                               DBUS_TYPE_INT32, &pid,
+                                                                               DBUS_TYPE_INT32, &update_type,
+                                                                               DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &path_array, path_length,
+                                                                               DBUS_TYPE_STRING, &uuid,
+                                                                               DBUS_TYPE_INVALID);
+                       } else {
+                               dbus_message_append_args (message,
+                                                                               DBUS_TYPE_INT32, &item,
+                                                                               DBUS_TYPE_INT32, &pid,
+                                                                               DBUS_TYPE_INT32, &update_type,
+                                                                               DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &path_array, path_length,
+                                                                               DBUS_TYPE_INVALID);
+                       }
+               } else {
+                       MSAPI_DBG("this request is wrong");
+               }
+
+               MS_SAFE_FREE(path_array);
+
+               /* Send the signal */
+               dbus_connection_send (bus, message, NULL);
+
+               /* Free the signal now we have finished with it */
+               dbus_message_unref (message);
+
+               MSAPI_DBG_ERR("success send notification");
+       } else {
+               MSAPI_DBG_ERR("dbus_message_new_signal failed");
+       }
+
+       dbus_connection_unref(bus);
+
+       /* Return TRUE to tell the event loop we want to be called again */
+       return MS_MEDIA_ERR_NONE;
+}
+
+
+DBusHandlerResult
+__get_message_internal(DBusMessage *message, db_update_cb user_cb, void *userdata)
+{
+       /* A Ping signal on the com.burtonini.dbus.Signal interface */
+       if (dbus_message_is_signal (message, MS_MEDIA_DBUS_INTERFACE, MS_MEDIA_DBUS_NAME_INTERNAL)) {
+               int i = 0;
+               int current_type = DBUS_TYPE_INVALID;
+               DBusError error;
+               DBusMessageIter read_iter;
+               DBusBasicValue value[6];
+
+               dbus_int32_t item = -1;
+               dbus_int32_t pid = 0;
+               dbus_int32_t update_type = MS_MEDIA_UNKNOWN;
+               dbus_int32_t content_type = -1;
+               char *update_path = NULL;
+               char *uuid = NULL;
+               char *mime_type = NULL;
+               void *recevie_path = NULL;
+               int path_len = 0;
+
+               dbus_error_init (&error);
+               MSAPI_DBG("size [%d]", sizeof(value));
+               memset(value, 0x0, sizeof(value));
+
+               /* 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){
+                       if (current_type == DBUS_TYPE_ARRAY) {
+                               DBusMessageIter sub;
+                               dbus_message_iter_recurse(&read_iter, &sub);
+                               dbus_message_iter_get_fixed_array(&sub, &recevie_path, &path_len);
+                       } else {
+                               dbus_message_iter_get_basic (&read_iter, &value[i]);
+                               i ++;
+                       }
+                       dbus_message_iter_next (&read_iter);
+               }
+
+               item = value[0].i32;
+               pid = value[1].i32;
+               update_type = value[2].i32;
+               update_path = strndup(recevie_path, path_len);
+               if (value[3].str != NULL) uuid = strdup(value[3].str);
+               content_type = value[4].i32;
+               if (value[5].str != NULL) mime_type = strdup(value[5].str);
+
+               if (item == MS_MEDIA_ITEM_DIRECTORY)
+                       content_type = MS_MEDIA_UNKNOWN;
+
+               /* getting data complete */
+               MSAPI_DBG("update type[%d]", update_type);
+               user_cb(pid,
+                               item,
+                               update_type,
+                               update_path,
+                               uuid,
+                               content_type,
+                               mime_type,
+                               userdata);
+
+               MS_SAFE_FREE(update_path);
+               MS_SAFE_FREE(uuid);
+               MS_SAFE_FREE(mime_type);
+
+               return DBUS_HANDLER_RESULT_HANDLED;
+       }
+
+       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+DBusHandlerResult
+__message_filter_internal(DBusConnection *connection, DBusMessage *message, void *user_data)
+{
+       db_update_cb user_cb = ((internal_noti_cb_data*)user_data)->user_callback;
+       void *userdata = ((internal_noti_cb_data*)user_data)->user_data;
+       DBusHandlerResult ret;
+
+       ret = __get_message_internal(message, user_cb, userdata);
+
+       return ret;
+}
+
+int media_db_update_subscribe_internal(MediaNotiHandle *handle, db_update_cb user_cb, void *user_data)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       internal_noti_cb_data *noti_data = NULL;
+       DBusError error;
+       DBusConnection *dbus;
+
+       dbus_g_thread_init();
+
+       dbus_error_init (&error);
+
+       dbus = dbus_bus_get_private (DBUS_BUS_SYSTEM, &error);
+       if (!dbus) {
+               MSAPI_DBG ("Failed to connect to the D-BUS daemon: %s", error.message);
+               dbus_error_free (&error);
+               ret = MS_MEDIA_ERR_INTERNAL;
+               goto ERROR;
+       }
+
+       dbus_connection_setup_with_g_main (dbus, NULL);
+
+       MS_MALLOC(noti_data, sizeof(internal_noti_cb_data));
+       if (noti_data == NULL) {
+               MSAPI_DBG_ERR("MS_MALLOC failed");
+               ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
+               goto ERROR;
+       }
+       noti_data->user_callback = user_cb;
+       noti_data->user_data = user_data;
+
+       /* listening to messages from all objects as no path is specified */
+       dbus_bus_add_match (dbus, MS_MEDIA_DBUS_MATCH_RULE, &error);
+       if( !dbus_connection_add_filter (dbus, __message_filter_internal, noti_data, NULL)) {
+               dbus_bus_remove_match (dbus, MS_MEDIA_DBUS_MATCH_RULE, NULL);
+               ret =  MS_MEDIA_ERR_INTERNAL;
+               goto ERROR;
+       }
+
+       noti_data->dbus = dbus;
+       *handle = noti_data;
+
+       g_mutex_lock(&mutex_internal);
+
+       if (handle_list_internal == NULL) {
+               handle_list_internal = g_array_new(FALSE, FALSE, sizeof(MediaNotiHandle));
+               if (handle_list_internal == NULL) {
+                       g_mutex_unlock(&mutex_internal);
+                       ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
+                       goto ERROR;
+               }
+       }
+
+       g_array_append_val(handle_list_internal, *handle);
+
+       g_mutex_unlock(&mutex_internal);
+
+       return MS_MEDIA_ERR_NONE;
+
+ERROR:
+
+       if (dbus != NULL) {
+               dbus_connection_close(dbus);
+               dbus_connection_unref(dbus);
+               dbus = NULL;
+       }
+
+       MS_SAFE_FREE(noti_data);
+
+       return ret;
+}
+
+static int _find_handle(MediaNotiHandle handle, int *idx)
+{
+       unsigned int i;
+       int ret = MS_MEDIA_ERR_NONE;
+       bool find_flag = false;
+       MediaNotiHandle data;
+
+       /*delete all node*/
+       if(handle_list_internal != NULL) {
+               for (i =0; i < handle_list_internal->len; i++) {
+                       data = g_array_index(handle_list_internal , MediaNotiHandle, i);
+                       MSAPI_DBG_ERR("%x %x", handle, data);
+                       if (data == handle) {
+                               MSAPI_DBG("FIND HANDLE");
+                               *idx = i;
+                               find_flag = true;
+                               break;
+                       }
+               }
+       } else {
+               ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       if (find_flag == false)
+               ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+
+       return ret;
+}
+
+int media_db_update_unsubscribe_internal(MediaNotiHandle handle, clear_user_data_cb clear_cb)
+{
+       int idx = -1;
+       int ret = MS_MEDIA_ERR_NONE;
+       int err;
+
+       if (handle == NULL) {
+               MSAPI_DBG_ERR("INVALID PARAMETER");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       g_mutex_lock(&mutex_internal);
+
+       err = _find_handle(handle, &idx);
+       if(err == MS_MEDIA_ERR_NONE) {
+               DBusConnection *dbus = ((internal_noti_cb_data*)handle)->dbus;
+
+               dbus_connection_remove_filter(dbus, __message_filter_internal, (internal_noti_cb_data*)handle);
+               dbus_bus_remove_match (dbus, MS_MEDIA_DBUS_MATCH_RULE, NULL);
+               dbus_connection_close(dbus);
+               dbus_connection_unref(dbus);
+               g_array_remove_index(handle_list_internal, idx);
+
+               if (clear_cb != NULL)
+                       clear_cb(((internal_noti_cb_data*)handle)->user_data);
+
+               MS_SAFE_FREE(handle);
+
+               if ((handle_list_internal != NULL) && (handle_list_internal->len == 0)) {
+                       g_array_free(handle_list_internal, FALSE);
+                       handle_list_internal = NULL;
+               }
+       } else {
+               MSAPI_DBG_ERR("PARAMETER DOES NOT FIND");
+               ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       g_mutex_unlock(&mutex_internal);
+
+       return ret;
+}
+
+
index 46cddf1..938a974 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-util-noti.c
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
-
 #include <sys/errno.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <string.h>
 #include <glib.h>
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus.h>
-#include <dbus/dbus-glib-lowlevel.h>
+#include <stdlib.h>
+#include <gio/gio.h>
 
 #include "media-util-internal.h"
 #include "media-util-dbg.h"
 #include "media-util.h"
 #include "media-util-noti.h"
 
-DBusConnection *g_bus;
-void *g_data_store;
-GArray *handle_list;
-static GStaticMutex noti_mutex = G_STATIC_MUTEX_INIT;
-int ref_count;
+static GDBusConnection *g_bus = NULL;
+static guint g_handler = 0;
+static void *g_data_store = NULL;
+static GMutex noti_mutex;
+static int ref_count = 0;
 
 #define MS_MEDIA_DBUS_NAME "ms_db_updated"
 
@@ -55,153 +45,123 @@ typedef struct noti_callback_data{
        void *user_data;
 } noti_callback_data;
 
-static void
-__free_data_fuction(void *memory)
+static bool __gdbus_message_is_signal(const char *iface, const char *signal)
 {
-       MS_SAFE_FREE(memory);
-       g_data_store = NULL;
+       if ((strcmp(iface, MS_MEDIA_DBUS_INTERFACE) == 0) && (strcmp(signal, MS_MEDIA_DBUS_NAME) == 0))
+               return TRUE;
+
+       return FALSE;
 }
 
-DBusHandlerResult
-__get_message(DBusMessage *message, db_update_cb user_cb, void *userdata)
+static void __get_message(GVariant *message, db_update_cb user_cb, void *userdata)
 {
-       /* A Ping signal on the com.burtonini.dbus.Signal interface */
-       if (dbus_message_is_signal (message, MS_MEDIA_DBUS_INTERFACE, MS_MEDIA_DBUS_NAME)) {
-               int i = 0;
-               int current_type = DBUS_TYPE_INVALID;
-               DBusError error;
-               DBusMessageIter read_iter;
-               DBusBasicValue value[6];
-
-               dbus_int32_t item = -1;
-               dbus_int32_t pid = 0;
-               dbus_int32_t update_type = MS_MEDIA_UNKNOWN;
-               dbus_int32_t content_type = -1;
-               char *update_path = NULL;
-               char *uuid = NULL;
-               char *mime_type = NULL;
-               void *recevie_path = NULL;
-               int path_len = 0;
-
-               dbus_error_init (&error);
-               MSAPI_DBG("size [%d]", sizeof(value));
-               memset(value, 0x0, sizeof(value));
-
-               /* 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){
-                       if (current_type == DBUS_TYPE_ARRAY) {
-                               DBusMessageIter sub;
-                               dbus_message_iter_recurse(&read_iter, &sub);
-                               dbus_message_iter_get_fixed_array(&sub, &recevie_path, &path_len);
-                       } else {
-                               dbus_message_iter_get_basic (&read_iter, &value[i]);
-                               i ++;
-                       }
-                       dbus_message_iter_next (&read_iter);
-               }
-
-               item = value[0].i32;
-               pid = value[1].i32;
-               update_type = value[2].i32;
-               update_path = strndup(recevie_path, path_len);
-               if (value[3].str != NULL) uuid = strdup(value[3].str);
-               content_type = value[4].i32;
-               if (value[5].str != NULL) mime_type = strdup(value[5].str);
-
-               if (item == MS_MEDIA_ITEM_DIRECTORY)
-                       content_type = MS_MEDIA_UNKNOWN;
-
-               /* getting data complete */
-               user_cb(pid,
-                               item,
-                               update_type,
-                               update_path,
-                               uuid,
-                               content_type,
-                               mime_type,
-                               userdata);
-
-               MS_SAFE_FREE(update_path);
-               MS_SAFE_FREE(uuid);
-               MS_SAFE_FREE(mime_type);
-
-               return DBUS_HANDLER_RESULT_HANDLED;
-       }
-
-       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+       gint32 item = -1;
+       gint32 pid = 0;
+       gint32 update_type = MS_MEDIA_UNKNOWN;
+       gint32 content_type = -1;
+       char *update_path = NULL;
+       char *uuid = NULL;
+       char *mime_type = NULL;
+
+       int item_number = g_variant_n_children(message);
+
+       if (item_number == 7)
+               g_variant_get(message, "(iiissis)", &item, &pid, &update_type, &update_path, &uuid, &content_type, &mime_type);
+       else if (item_number == 5)
+               g_variant_get(message, "(iiiss)", &item, &pid, &update_type, &update_path, &uuid);
+       else if (item_number == 4)
+               g_variant_get(message, "(iiis)", &item, &pid, &update_type, &update_path);
+
+       if (item == MS_MEDIA_ITEM_DIRECTORY)
+               content_type = MS_MEDIA_UNKNOWN;
+
+       /* getting data complete */
+       user_cb(pid,
+                       item,
+                       update_type,
+                       update_path,
+                       uuid,
+                       content_type,
+                       mime_type,
+                       userdata);
+
+       MS_SAFE_FREE(update_path);
+       MS_SAFE_FREE(uuid);
+       MS_SAFE_FREE(mime_type);
 }
 
-static DBusHandlerResult
-__message_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
+static void __message_filter(GDBusConnection* connection,
+                                       const gchar* sender_name,
+                                       const gchar* object_path,
+                                       const gchar* interface_name,
+                                       const gchar* signal_name,
+                                       GVariant* parameters,
+                                       gpointer user_data)
 {
-       db_update_cb user_cb = ((noti_callback_data*)user_data)->user_callback;
-       void *userdata = ((noti_callback_data*)user_data)->user_data;
-       DBusHandlerResult ret;
-
-       ret = __get_message(message, user_cb, userdata);
+       if (__gdbus_message_is_signal(interface_name, signal_name)) {
+               db_update_cb user_cb = ((noti_callback_data*)user_data)->user_callback;
+               void *userdata = ((noti_callback_data*)user_data)->user_data;
 
-       return ret;
+               __get_message(parameters, user_cb, userdata);
+       }
 }
 
-
 int media_db_update_subscribe(db_update_cb user_cb, void *user_data)
 {
        int ret = MS_MEDIA_ERR_NONE;
-       DBusError error;
+       GError *error = NULL;
        noti_callback_data *callback_data = NULL;
 
-       g_static_mutex_lock(&noti_mutex);
+       g_mutex_lock(&noti_mutex);
 
        if (g_bus == NULL) {
-               dbus_g_thread_init();
-
-               dbus_error_init (&error);
-
-               g_bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
+               g_bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
                if (!g_bus) {
-                       MSAPI_DBG ("Failed to connect to the D-BUS daemon: %s", error.message);
-                       dbus_error_free (&error);
-                       ret = MS_MEDIA_ERR_DBUS_GET;
+                       MSAPI_DBG ("Failed to connect to the g D-BUS daemon: %s", error->message);
+                       g_error_free (error);
+                       ret = MS_MEDIA_ERR_INTERNAL;
                        goto ERROR;
                }
 
-               dbus_connection_setup_with_g_main (g_bus, NULL);
-
                MS_MALLOC(callback_data, sizeof(noti_callback_data));
                if (callback_data == NULL) {
                        MSAPI_DBG_ERR("MS_MALLOC failed");
-                       ret = MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
+                       ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
                        goto ERROR;
                }
                callback_data->user_callback = user_cb;
                callback_data->user_data = user_data;
 
                /* listening to messages from all objects as no path is specified */
-               dbus_bus_add_match (g_bus, MS_MEDIA_DBUS_MATCH_RULE, &error);
-               if( !dbus_connection_add_filter (g_bus, __message_filter, callback_data, __free_data_fuction)) {
-                       dbus_bus_remove_match (g_bus, MS_MEDIA_DBUS_MATCH_RULE, NULL);
-                       ret =  MS_MEDIA_ERR_DBUS_ADD_FILTER;
-                       goto ERROR;
-               }
+               g_handler = g_dbus_connection_signal_subscribe(
+                                               g_bus,
+                                               NULL,
+                                               MS_MEDIA_DBUS_INTERFACE,
+                                               MS_MEDIA_DBUS_NAME,
+                                               MS_MEDIA_DBUS_PATH,
+                                               NULL,
+                                               G_DBUS_SIGNAL_FLAGS_NONE,
+                                               __message_filter,
+                                               callback_data,
+                                               NULL);
                g_data_store = (void *)callback_data;
        }
 
        ref_count ++;
 
-       g_static_mutex_unlock(&noti_mutex);
+       g_mutex_unlock(&noti_mutex);
 
        return MS_MEDIA_ERR_NONE;
 
 ERROR:
 
        if (g_bus != NULL) {
-               dbus_connection_unref(g_bus);
+               g_object_unref(g_bus);
                g_bus = NULL;
        }
        MS_SAFE_FREE(callback_data);
 
-       g_static_mutex_unlock(&noti_mutex);
+       g_mutex_unlock(&noti_mutex);
 
        return ret;
 }
@@ -212,19 +172,21 @@ int media_db_update_unsubscribe(void)
                return MS_MEDIA_ERR_NONE;
        }
 
-       g_static_mutex_lock(&noti_mutex);
+       g_mutex_lock(&noti_mutex);
 
        if (ref_count == 1) {
-               dbus_connection_remove_filter(g_bus, __message_filter, g_data_store);
-               dbus_bus_remove_match (g_bus, MS_MEDIA_DBUS_MATCH_RULE, NULL);
-               dbus_connection_unref(g_bus);
-
+               g_dbus_connection_signal_unsubscribe(g_bus, g_handler);
+               g_object_unref(g_bus);
                g_bus = NULL;
+
+               /*Release Callback*/
+               MS_SAFE_FREE(g_data_store);
+               g_data_store = NULL;
        }
 
        ref_count --;
 
-       g_static_mutex_unlock(&noti_mutex);
+       g_mutex_unlock(&noti_mutex);
 
        return MS_MEDIA_ERR_NONE;
 }
@@ -238,82 +200,156 @@ int media_db_update_send(int pid, /* mandatory */
                                                        char *mime_type /* optional */
                                                        )
 {
-       DBusMessage *message = NULL;
-       DBusConnection *bus = NULL;
-       DBusError error;
-       unsigned char *path_array = NULL;
-       int path_length = strlen(path) + 1;
-
-       /* Get a connection to the session bus */
-       dbus_error_init (&error);
-       bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
+       GVariant *message = NULL;
+       GDBusConnection *bus = NULL;
+       GError *error = NULL;
+
+       bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
        if (!bus) {
-               MSAPI_DBG ("Failed to connect to the D-BUS daemon: %s", error.message);
-               dbus_error_free (&error);
-               return MS_MEDIA_ERR_DBUS_GET;
+               MSAPI_DBG ("Failed to get gdbus connection: %s", error->message);
+               g_error_free (error);
+               return MS_MEDIA_ERR_INTERNAL;
        }
 
-       message = dbus_message_new_signal (MS_MEDIA_DBUS_PATH, MS_MEDIA_DBUS_INTERFACE, MS_MEDIA_DBUS_NAME);
-       if (message != NULL) {
-               path_array = malloc(sizeof(unsigned char) * path_length);
-               memcpy(path_array, path, path_length);
-
-               if (item == MS_MEDIA_ITEM_FILE) {
-                       MSAPI_DBG("FILE CHANGED");
-                       if (uuid != NULL && mime_type != NULL) {
+       if (item == MS_MEDIA_ITEM_FILE) {
+               MSAPI_DBG("FILE CHANGED");
+               if (uuid != NULL && mime_type != NULL) {
                        /* fill all datas */
-                               dbus_message_append_args (message,
-                                                                               DBUS_TYPE_INT32, &item,
-                                                                               DBUS_TYPE_INT32, &pid,
-                                                                               DBUS_TYPE_INT32, &update_type,
-                                                                               DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &path_array, path_length,
-                                                                               DBUS_TYPE_STRING, &uuid,
-                                                                               DBUS_TYPE_INT32, &media_type,
-                                                                               DBUS_TYPE_STRING, &mime_type,
-                                                                               DBUS_TYPE_INVALID);
-                       } else {
-                               MSAPI_DBG_ERR("uuid or mime_type is NULL");
-                               MS_SAFE_FREE(path_array);
-                               dbus_message_unref (message);
-                               return MS_MEDIA_ERR_INVALID_PARAMETER;
-                       }
-               } else if (item == MS_MEDIA_ITEM_DIRECTORY) {
-                       MSAPI_DBG("DIRECTORY CHANGED");
-                       /* fill all datas */
-                       if(uuid != NULL) {
-                               dbus_message_append_args (message,
-                                                                               DBUS_TYPE_INT32, &item,
-                                                                               DBUS_TYPE_INT32, &pid,
-                                                                               DBUS_TYPE_INT32, &update_type,
-                                                                               DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &path_array, path_length,
-                                                                               DBUS_TYPE_STRING, &uuid,
-                                                                               DBUS_TYPE_INVALID);
-                       } else {
-                               dbus_message_append_args (message,
-                                                                               DBUS_TYPE_INT32, &item,
-                                                                               DBUS_TYPE_INT32, &pid,
-                                                                               DBUS_TYPE_INT32, &update_type,
-                                                                               DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &path_array, path_length,
-                                                                               DBUS_TYPE_INVALID);
-                       }
+                       message = g_variant_new("(iiissis)", item, pid, update_type, path, uuid, media_type, mime_type);
+               } else {
+                       MSAPI_DBG_ERR("uuid or mime_type is NULL");
+                       return MS_MEDIA_ERR_INVALID_PARAMETER;
+               }
+       } else if (item == MS_MEDIA_ITEM_DIRECTORY) {
+               MSAPI_DBG("DIRECTORY CHANGED");
+               /* fill all datas */
+               if(uuid != NULL) {
+                       message = g_variant_new("(iiiss)", item, pid, update_type, path, uuid);
                } else {
-                       MSAPI_DBG("this request is wrong");
+                       message = g_variant_new("(iiis)", item, pid, update_type, path);
                }
+       } else {
+               MSAPI_DBG("this request is wrong");
+       }
 
-               MS_SAFE_FREE(path_array);
+       gboolean emmiting = g_dbus_connection_emit_signal(
+                                       bus,
+                                       NULL,
+                                       MS_MEDIA_DBUS_PATH,
+                                       MS_MEDIA_DBUS_INTERFACE,
+                                       MS_MEDIA_DBUS_NAME,
+                                       message,
+                                       &error);
+       if (!emmiting)
+       {
+               MSAPI_DBG_ERR("g_dbus_connection_emit_signal failed : %s", error?error->message:"none");
+               if (error)
+               {
+                       MSAPI_DBG_ERR("Error in g_dbus_connection_emit_signal");
+                       g_object_unref(bus);
+                       g_error_free(error);
+               }
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+       MSAPI_DBG("success send notification");
+
+       gboolean flush = FALSE;
+       flush = g_dbus_connection_flush_sync(bus, NULL, &error);
+       if (!flush)
+       {
+               MSAPI_DBG_ERR("g_dbus_connection_flush_sync failed");
+               if (error)
+               {
+                       MSAPI_DBG_ERR("error : [%s]", error->message);
+                       g_object_unref(bus);
+                       g_error_free(error);
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
+       }
 
-               /* Send the signal */
-               dbus_connection_send (bus, message, NULL);
+       g_object_unref(bus);
 
-               /* Free the signal now we have finished with it */
-               dbus_message_unref (message);
+       /* Return TRUE to tell the event loop we want to be called again */
+       return MS_MEDIA_ERR_NONE;
+}
+
+int media_db_update_send_v2(int pid, /* mandatory */
+                                                       media_item_type_e item, /* mandatory */
+                                                       media_item_update_type_e update_type, /* mandatory */
+                                                       char* path, /* mandatory */
+                                                       char* uuid, /* optional */
+                                                       media_type_e media_type, /* optional */
+                                                       char *mime_type /* optional */
+                                                       )
+{
+       GVariant *message = NULL;
+       GDBusConnection *bus = NULL;
+       GError *error = NULL;
 
-               MSAPI_DBG("success send notification");
+       bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+       if (!bus) {
+               MSAPI_DBG ("Failed to get gdbus connection: %s", error->message);
+               g_error_free (error);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       if (item == MS_MEDIA_ITEM_FILE) {
+               MSAPI_DBG("FILE CHANGED");
+               if (uuid != NULL && mime_type != NULL) {
+                       /* fill all datas */
+                       message = g_variant_new("(iiissis)", item, pid, update_type, path, uuid, media_type, mime_type);
+               } else {
+                       MSAPI_DBG_ERR("uuid or mime_type is NULL");
+                       return MS_MEDIA_ERR_INVALID_PARAMETER;
+               }
+       } else if (item == MS_MEDIA_ITEM_DIRECTORY) {
+               MSAPI_DBG("DIRECTORY CHANGED");
+               /* fill all datas */
+               if(uuid != NULL) {
+                       message = g_variant_new("(iiiss)", item, pid, update_type, path, uuid);
+               } else {
+                       message = g_variant_new("(iiis)", item, pid, update_type, path);
+               }
        } else {
-               MSAPI_DBG_ERR("dbus_message_new_signal failed");
+               MSAPI_DBG("this request is wrong");
+       }
+
+       gboolean emmiting = g_dbus_connection_emit_signal(
+                                       bus,
+                                       NULL,
+                                       MS_MEDIA_DBUS_PATH,
+                                       MS_MEDIA_DBUS_INTERFACE,
+                                       MS_MEDIA_DBUS_NAME,
+                                       message,
+                                       &error);
+       if (!emmiting)
+       {
+               MSAPI_DBG_ERR("g_dbus_connection_emit_signal failed : %s", error?error->message:"none");
+               if (error)
+               {
+                       MSAPI_DBG_ERR("Error in g_dbus_connection_emit_signal");
+                       g_object_unref(bus);
+                       g_error_free(error);
+               }
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+       MSAPI_DBG("success send notification");
+
+       gboolean flush = FALSE;
+       flush = g_dbus_connection_flush_sync(bus, NULL, &error);
+       if (!flush)
+       {
+               MSAPI_DBG_ERR("g_dbus_connection_flush_sync failed");
+               if (error)
+               {
+                       MSAPI_DBG_ERR("error : [%s]", error->message);
+                       g_object_unref(bus);
+                       g_error_free(error);
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
        }
 
-       dbus_connection_unref(bus);
+       g_object_unref(bus);
 
        /* Return TRUE to tell the event loop we want to be called again */
        return MS_MEDIA_ERR_NONE;
index 9254209..0b287c7 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-util-register.c
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
 #include <errno.h>
 #include <unistd.h>
 #include <arpa/inet.h>
@@ -52,6 +44,15 @@ typedef struct media_callback_data{
        char *sock_path;
 } media_callback_data;
 
+typedef struct media_scan_data{
+       GIOChannel *src;
+       media_callback_data *cb_data;
+       int pid;
+       char *req_path;
+} media_scan_data;
+
+GArray *req_list;
+
 static bool _is_valid_path(const char *path)
 {
        if (path == NULL)
@@ -61,10 +62,10 @@ static bool _is_valid_path(const char *path)
                return true;
        } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
                return true;
+       } else if (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0) {
+               return true;
        } else
                return false;
-
-       return true;
 }
 
 static int _check_dir_path(const char *dir_path)
@@ -134,11 +135,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;
        }
 
@@ -173,13 +174,118 @@ ERROR:
        return TRUE;
 }
 
-static int _attach_callback(int *sockfd, char* sock_path, scan_complete_cb user_callback, void *user_data)
+static int _add_request(const char * req_path, media_callback_data *cb_data, GIOChannel *channel)
+{
+       media_scan_data *req_data = NULL;
+
+       if (req_list == NULL) {
+               req_list = g_array_new(FALSE, FALSE, sizeof(media_scan_data*));
+       }
+
+       if (req_list == NULL) {
+               MSAPI_DBG_ERR("g_array_new falied");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
+
+       req_data = malloc(sizeof(media_scan_data));
+       if (req_data == NULL) {
+               MSAPI_DBG_ERR("malloc falied");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
+
+       req_data->cb_data = cb_data;
+       req_data->req_path = strdup(req_path);
+       req_data->src = channel;
+
+       g_array_append_val(req_list, req_data);
+
+       return MS_MEDIA_ERR_NONE;
+
+}
+
+static int _remove_request(const char * req_path)
+{
+       media_scan_data *req_data = NULL;
+       media_callback_data *cb_data = NULL;
+       char *sock_path = NULL;
+       int i = 0;
+       int list_len = 0;
+       bool flag = false;
+       scan_complete_cb user_callback;
+       void *user_data = NULL;
+       GSource *source = NULL;
+       GIOChannel *src = NULL;
+       media_request_result_s req_result;
+
+       if (req_list == NULL) {
+               MSAPI_DBG_ERR("The request list is NULL. This is invalid operation.");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       list_len = req_list->len;
+
+       for (i = 0; i < list_len; i++) {
+               req_data = g_array_index(req_list, media_scan_data*, i);
+               if (strcmp(req_data->req_path, req_path) == 0) {
+                       flag = true;
+                       g_array_remove_index (req_list, i);
+               }
+       }
+
+       if (flag == false) {
+               MSAPI_DBG("Not in scan queue :%s", req_path);
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       req_result.pid = -1;
+       req_result.result = MS_MEDIA_ERR_NONE;
+       req_result.complete_path = strndup(req_path, strlen(req_path));
+       req_result.request_type = MEDIA_FILES_REGISTER;
+
+       src = req_data->src;
+       cb_data = req_data->cb_data;
+       source = ((media_callback_data *)cb_data)->source;
+       sock_path = ((media_callback_data *)cb_data)->sock_path;
+       user_callback = ((media_callback_data *)cb_data)->user_callback;
+       user_data = ((media_callback_data *)cb_data)->user_data;
+
+       /*call user define function*/
+       user_callback(&req_result, user_data);
+
+
+       /*close an IO channel*/
+       g_io_channel_shutdown(src,  FALSE, NULL);
+       g_io_channel_unref(src);
+
+       g_source_destroy(source);
+
+       if (sock_path != NULL) {
+               MSAPI_DBG("delete path :%s", sock_path);
+               unlink(sock_path);
+               MS_SAFE_FREE(sock_path);
+       }
+
+       MS_SAFE_FREE(req_data->req_path);
+       MS_SAFE_FREE(req_data);
+
+       return MS_MEDIA_ERR_NONE;
+
+}
+
+static int _attach_callback(const char *req_path, int *sockfd, char* sock_path, scan_complete_cb user_callback, void *user_data)
 {
        GIOChannel *channel = NULL;
        GMainContext *context = NULL;
        GSource *source = NULL;
        media_callback_data *cb_data;
 
+       cb_data = malloc(sizeof(media_callback_data));
+       if (cb_data == NULL) {
+               MSAPI_DBG_ERR("Malloc failed");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
+       memset(cb_data, 0, sizeof(media_callback_data));
+
        /*get the global default main context*/
        context = g_main_context_default();
 
@@ -187,7 +293,6 @@ static int _attach_callback(int *sockfd, char* sock_path, scan_complete_cb user_
        channel = g_io_channel_unix_new(*sockfd);
        source = g_io_create_watch(channel, G_IO_IN);
 
-       cb_data = malloc(sizeof(media_callback_data));
        cb_data->source = source;
        cb_data->user_callback = user_callback;
        cb_data->user_data = user_data;
@@ -198,10 +303,12 @@ static int _attach_callback(int *sockfd, char* sock_path, scan_complete_cb user_
        g_source_attach(source, context);
        g_source_unref(source);
 
+       _add_request(req_path, cb_data, channel);
+
        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)
+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)
 {
        int ret = MS_MEDIA_ERR_NONE;
        int request_msg_size = 0;
@@ -217,7 +324,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)
@@ -233,6 +340,9 @@ static int __media_db_request_update_async(ms_msg_type_e msg_type, const char *r
        send_msg.pid = syscall(__NR_getpid);
        send_msg.msg_size = request_msg_size;
        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);
@@ -249,15 +359,64 @@ static int __media_db_request_update_async(ms_msg_type_e msg_type, const char *r
                return ret;
        }
 
-       ret = _attach_callback(&sockfd, sock_path, user_callback ,user_data);
+       ret = _attach_callback(request_msg, &sockfd, sock_path, user_callback ,user_data);
        if(ret != MS_MEDIA_ERR_NONE)
                return ret;
 
        return ret;
 }
 
+static int __media_db_request_update_cancel(ms_msg_type_e msg_type, const char *request_msg)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       int request_msg_size = 0;
+       int sockfd = -1;
+       int port = MS_SCANNER_PORT;
+       ms_comm_msg_s send_msg;
+       ms_sock_info_s sock_info;
+
+       if(!MS_STRING_VALID(request_msg))
+       {
+               MSAPI_DBG_ERR("invalid query");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       MSAPI_DBG("REQUEST CANCEL DIRECTORY SCANNING[%s]", request_msg);
+
+       request_msg_size = strlen(request_msg);
+       if(request_msg_size >= MAX_MSG_SIZE)
+       {
+               MSAPI_DBG_ERR("Query is Too long. [%d] query size limit is [%d]", request_msg_size, MAX_MSG_SIZE);
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       memset((void *)&send_msg, 0, sizeof(ms_comm_msg_s));
+       send_msg.msg_type = msg_type;
+       send_msg.pid = syscall(__NR_getpid);
+       send_msg.msg_size = request_msg_size;
+       strncpy(send_msg.msg, request_msg, request_msg_size);
+
+       /*Create Socket*/
+       ret = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, 0, &sock_info);
+       sockfd = sock_info.sock_fd;
+
+       MSAPI_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
+
+       ret = ms_ipc_send_msg_to_server_tcp(sockfd, port, &send_msg, NULL);
+       ms_ipc_delete_client_socket(&sock_info);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MSAPI_DBG_ERR("ms_ipc_send_msg_to_server failed : %d", ret);
+               return ret;
+       }
+
+       ret = _remove_request(request_msg);
+       if(ret != MS_MEDIA_ERR_NONE)
+               return ret;
 
-int media_directory_scanning_async(const char *directory_path, bool recursive_on, scan_complete_cb user_callback, void *user_data)
+       return ret;
+}
+
+int media_directory_scanning_async(const char *directory_path, const char *storage_id, bool recursive_on, scan_complete_cb user_callback, void *user_data)
 {
        int ret = MS_MEDIA_ERR_NONE;
 
@@ -266,9 +425,22 @@ 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);
+               ret = __media_db_request_update_async(MS_MSG_DIRECTORY_SCANNING, storage_id, directory_path, user_callback, user_data);
        else
-               ret = __media_db_request_update_async(MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE, directory_path, user_callback, user_data);
+               ret = __media_db_request_update_async(MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE, storage_id, directory_path, user_callback, user_data);
+
+       return ret;
+}
+
+int media_directory_scanning_cancel(const char *directory_path)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+
+       ret = _check_dir_path(directory_path);
+       if(ret != MS_MEDIA_ERR_NONE)
+               return ret;
+
+       ret = __media_db_request_update_cancel(MS_MSG_DIRECTORY_SCANNING_CANCEL, directory_path);
 
        return ret;
 }
@@ -277,7 +449,7 @@ int media_files_register(const char *list_path, insert_complete_cb user_callback
 {
        int ret = MS_MEDIA_ERR_NONE;
 
-       ret = __media_db_request_update_async(MS_MSG_BULK_INSERT, list_path, user_callback, user_data);
+       ret = __media_db_request_update_async(MS_MSG_BULK_INSERT, NULL, list_path, user_callback, user_data);
 
        MSAPI_DBG("client receive: %d", ret);
 
@@ -288,11 +460,10 @@ 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);
+       ret = __media_db_request_update_async(MS_MSG_BURSTSHOT_INSERT, NULL, list_path, user_callback, user_data);
 
        MSAPI_DBG("client receive: %d", ret);
 
        return ret;
-
 }
 
index f447e3b..1066c9f 100644 (file)
@@ -5,16 +5,28 @@
                        <label name="media-server::vconf"/>
                </provide>
                <request>
+                        <smack request="system::homedir" type="rwxat"/>
                        <smack request="system::media" type="rwxat"/>
                        <smack request="system::media::root" type="rwxat"/>
                        <smack request="system::ext_storage" type="rwxat"/>
-                       <smack request="system::vconf" type="rwxat"/>
-                       <smack request="system::vconf_system" type="r"/>
+                       <smack request="device::app_logging" type="w"/>
+                        <smack request="device::sys_logging" type="w"/>
                        <smack request="deviced" type="rwx"/>
                        <smack request="deviced::display" type="rw"/>
-                       <smack request="security-server::api-privilege-by-pid" type="w"/>
+                       <smack request="deviced::block" type="rw"/>
                        <smack request="aul::lunch" type="x"/>
+                       <smack request="security-server::api-privilege-by-pid" type="w"/>
+                       <smack request="media-data::db" type="rw"/>
+                       <smack request="tizen::vconf::platform::rw" type="rw"/>
+                       <smack request="tizen::vconf::platform::r" type="rw"/>
+                       <smack request="tizen::vconf::public::r" type="rw"/>
+                       <smack request="tizen::vconf::setting::admin" type="rl"/>
+                       <smack request="tizen::vconf::camcorder" type="rl"/>
+                       <smack request="xorg" type="rwx"/>
                </request>
+               <permit>
+                       <smack permit="xorg" type="rwx"/>
+               </permit>
        </define>
        <request>
                <domain name="_"/>
@@ -22,6 +34,7 @@
        <assign>
                <filesystem path="/usr/bin/media-server" label="media-server" exec_label="media-server" />
                <filesystem path="/usr/bin/media-scanner" label="media-server" exec_label="media-server" />
+               <filesystem path="/usr/bin/media-scanner-v2" label="media-server" exec_label="media-server" />
                <filesystem path="/usr/bin/mediadb-update" label="media-server" exec_label="media-server" />
        </assign>
 </manifest>
diff --git a/media_content_config.ini b/media_content_config.ini
new file mode 100644 (file)
index 0000000..582874c
--- /dev/null
@@ -0,0 +1,12 @@
+;thumbnail_activation level
+; 0 = Deny all requests related thumbnail (never activate thumbnail server)
+; 1 = Allow all requests related thumbnail
+; 2 = Allow user request only
+
+[media-content-config]
+thumbnail_activation=1
+
+;scanner_type
+; 0 = extract all data at once
+; 1 = 2 step scanning
+scanner_type=0
diff --git a/packaging/media-scanner.service.wearable b/packaging/media-scanner.service.wearable
deleted file mode 100644 (file)
index d040e02..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-[Unit]
-Description=Media scanner
-
-[Service]
-Type=simple
-ExecStart=/usr/bin/media-scanner
-MemoryLimit=100M
-
-[Install]
-WantedBy=multi-user.target
similarity index 71%
rename from packaging/media-server.service.mobile
rename to packaging/media-server.service
index 7ad7b53..9687ca8 100644 (file)
@@ -7,7 +7,10 @@ Type=simple
 ExecStart=/usr/bin/media-server
 Restart=always
 RestartSec=0
-
+MemoryLimit=100M
+User=system
+Group=system
+SmackProcessLabel=media-server
 
 [Install]
 WantedBy=multi-user.target
diff --git a/packaging/media-server.service.wearable b/packaging/media-server.service.wearable
deleted file mode 100644 (file)
index c4326fe..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-[Unit]
-Description=Media server
-Requires=icd.service
-After=icd.service
-
-[Service]
-Type=simple
-ExecStart=/usr/bin/media-server
-Restart=always
-RestartSec=0
-MemoryLimit=100M
-
-[Install]
-WantedBy=multi-user.target
index dced0a0..4d0ac31 100755 (executable)
@@ -1,13 +1,11 @@
 Name:       media-server
 Summary:    File manager service server.
-Version: 0.2.89
+Version: 0.2.122
 Release:    1
 Group:      utils
 License:    Apache-2.0
 Source0:    %{name}-%{version}.tar.gz
-Source1:    media-server.service.wearable
-Source2:    media-scanner.service.wearable
-Source3:    media-server.service.mobile
+Source1:    media-server.service
 
 Requires(post): /usr/bin/vconftool
 Requires: deviced
@@ -16,12 +14,14 @@ BuildRequires:  pkgconfig(glib-2.0)
 BuildRequires:  pkgconfig(vconf)
 BuildRequires:  pkgconfig(dlog)
 BuildRequires:  pkgconfig(dbus-glib-1)
+BuildRequires:  pkgconfig(gio-2.0)
 BuildRequires:  pkgconfig(sqlite3)
 BuildRequires:  pkgconfig(db-util)
 BuildRequires:  pkgconfig(deviced)
 BuildRequires:  pkgconfig(security-server)
 BuildRequires:  pkgconfig(notification)
-
+BuildRequires:  pkgconfig(iniparser)
+BuildRequires:  pkgconfig(libsmack)
 
 %description
 Description: File manager service server
@@ -46,7 +46,9 @@ Description: media server development library.
 %setup -q
 
 %build
-
+export CFLAGS+=" -Wextra -Wno-array-bounds"
+export CFLAGS+=" -Wno-ignored-qualifiers -Wno-unused-parameter"
+export CFLAGS+=" -Wwrite-strings -Wswitch-default -Werror"
 export GC_SECTIONS_FLAGS="-fdata-sections -ffunction-sections -Wl,--gc-sections"
 export CFLAGS+=" ${GC_SECTIONS_FLAGS}"
 export CXXFLAGS+=" ${GC_SECTIONS_FLAGS}"
@@ -70,9 +72,6 @@ mkdir -p %{buildroot}/usr/lib/systemd/system/multi-user.target.wants
 install -m 644 %{SOURCE1} %{buildroot}/usr/lib/systemd/system/media-server.service
 ln -s ../media-server.service %{buildroot}/usr/lib/systemd/system/multi-user.target.wants/media-server.service
 
-install -m 644 %{SOURCE2} %{buildroot}/usr/lib/systemd/system/media-scanner.service
-#ln -s ../media-scanner.service %{buildroot}/usr/lib/systemd/system/multi-user.target.wants/media-scanner.service
-
 mkdir -p %{buildroot}/usr/etc
 cp -rf %{_builddir}/%{name}-%{version}/media-server-plugin %{buildroot}/usr/etc/media-server-plugin
 
@@ -81,22 +80,24 @@ mkdir -p %{buildroot}/%{_datadir}/license
 cp -rf %{_builddir}/%{name}-%{version}/LICENSE.APLv2.0 %{buildroot}/%{_datadir}/license/%{name}
 cp -rf %{_builddir}/%{name}-%{version}/LICENSE.APLv2.0 %{buildroot}/%{_datadir}/license/libmedia-utils
 
+#ini file
+mkdir -p %{buildroot}/opt/etc
+cp -rf %{_builddir}/%{name}-%{version}/media_content_config.ini %{buildroot}/usr/etc
+
 %post
-vconftool set -t int db/filemanager/dbupdate "1" -f -s system::vconf_inhouse
-vconftool set -t int memory/filemanager/Mmc "0" -i -f -s system::vconf_inhouse
-vconftool set -t string db/private/mediaserver/mmc_info "" -f -s media-server::vconf
+
 
 %files
 %manifest media-server.manifest
 %defattr(-,root,root,-)
 %{_bindir}/media-server
 %{_bindir}/media-scanner
+%{_bindir}/media-scanner-v2
 %{_bindir}/mediadb-update
 /usr/lib/systemd/system/media-server.service
 /usr/lib/systemd/system/multi-user.target.wants/media-server.service
-/usr/lib/systemd/system/media-scanner.service
-#/usr/lib/systemd/system/multi-user.target.wants/media-scanner.service
 /usr/etc/media-server-plugin
+/usr/etc/media_content_config.ini
 #License
 %{_datadir}/license/%{name}
 %{_datadir}/license/libmedia-utils
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..697748c
--- /dev/null
@@ -0,0 +1,166 @@
+/*
+ *  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 enum {
+       MS_ITEM_INSERT          = 0,
+       MS_ITEM_DELETE          = 1,
+       MS_ITEM_UPDATE          = 2,
+}ms_noti_type_e;
+
+typedef int (*CONNECT)(void**, char **);
+typedef int (*DISCONNECT)(void*, char **);
+typedef int (*INSERT_ITEM_BEGIN)(void*, int, int, int, char **);
+typedef int (*INSERT_ITEM_END)(void*, char **);
+typedef int (*SET_ITEM_VALIDITY_BEGIN)(void*, int, char **);
+typedef int (*SET_ITEM_VALIDITY_END)(void*, char **);
+typedef int (*UPDATE_BEGIN)();
+typedef int (*UPDATE_END)(const char*);
+
+typedef int (*SEND_DIR_UPDATE_NOTI)(void *, const char *, const char *, const char *, int, int, char **);
+typedef int (*CHECK_ITEM_EXIST)(void*, const char*, const char*, bool*, char **);
+typedef int (*INSERT_ITEM)(void*, const char*, const char*, int, char **);
+typedef int (*INSERT_ITEM_IMMEDIATELY)(void*, const char*, const char*, int, char **);
+typedef int (*INSERT_BURST_ITEM)(void *, const char*, const char *, int , char **);
+typedef int (*SET_ALL_STORAGE_ITEMS_VALIDITY)(void*, const char*, int, int, char **);
+typedef int (*SET_FOLDER_ITEM_VALIDITY)(void*, const char*, const char*, int, int, char**);
+typedef int (*SET_ITEM_VALIDITY)(void*, const char*, const char*, int, int, char **);
+typedef int (*DELETE_ITEM)(void *, const char*, const char *, char **);
+typedef int (*DELETE_ALL_ITEMS_IN_STORAGE)(void*, const char*, int, char **);
+typedef int (*DELETE_ALL_INVALID_ITMES_IN_STORAGE)(void*, const char*, int, char **);
+typedef int (*DELETE_ALL_INVALID_ITEMS_IN_FOLDER)(void*, const char*, const char*, bool, 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**, int *, char **);
+typedef int (*UPDATE_FOLDER_TIME)(void *, const char*, const char *, 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, char **);
+typedef int (*INSERT_FOLDER)(void*, const char*, const char*, int, char **);
+typedef int (*DELETE_INVALID_FOLDER)(void*, const char*, char **);
+typedef int (*SET_FOLDER_VALIDITY)(void*, const char*, const char*, int, bool, char **);
+typedef int (*INSERT_FOLDER_BEGIN)(void *, int, char**);
+typedef int (*INSERT_FOLDER_END)(void*, char**);
+typedef int (*INSERT_ITEM_SCAN)(void*, const char*, const char*, int, char **);
+typedef int (*UPDATE_ITEM_EXTRACT)(void*, const char*, int, int, const char*, char **);
+
+typedef int (*CHECK_DB)(void*, bool*, char **);
+typedef int (*GET_UUID)(void *handle, char **uuid, char **err_msg);
+typedef int (*GET_MMC_INFO)(void *handle, char **storage_name, char **storage_path, int *validity, bool *info_exist, char **err_msg);
+typedef int (*CHECK_STORAGE)(void *handle, const char *storage_id, const char *storage_name, char **storage_path, int *validity, char **err_msg);
+typedef int (*INSERT_STORAGE)(void *handle, const char *storage_id, int storage_type, const char *storage_name, const char *storage_path, char **err_msg);
+typedef int (*UPDATE_STORAGE)(void *handle, const char *storage_id, const char *storage_path, char **err_msg);
+typedef int (*DELETE_STORAGE)(void *handle, const char *storage_id, const char *storage_name, char **err_msg);
+typedef int (*SET_STORAGE_VALIDITY)(void *handle, const char *storage_id, int validity, char **err_msg);
+typedef int (*SET_ALL_STORAGE_VALIDITY)(void *handle, int validity, char **err_msg);
+
+typedef int (*UPDATE_ITEM_META)(void *, const char *, int, char **);
+typedef int (*UPDATE_ITEM_BEGIN)(void *, int, char **);
+typedef int (*UPDATE_ITEM_END)(void *, char **);
+
+typedef int (*DELETE_INVALID_FOLDER_BY_PATH)(void*, const char*, const char*, int *, char **);
+typedef int (*CHECK_FOLDER_EXIST)(void*, const char*, const char*, char **);
+typedef int (*COUNT_SUBFOLDER)(void*, const char*, const char*, int *, char **);
+typedef int (*GET_FOLDER_ID)(void *, const char *, const char *, char *, 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);
+int ms_disconnect_db(void ***handle);
+int ms_validate_item(void **handle, const char *storage_id, const char *path);
+int ms_insert_item_batch(void **handle, const char* storage_id, const char *path);
+int ms_insert_item_pass2(void **handle, const char* storage_id, const char *path, int scan_type);
+int ms_insert_item_immediately(void **handle, const char *storage_id, const char *path);
+int ms_insert_burst_item(void **handle, const char *storage_id, const char *path);
+bool ms_delete_all_items(void **handle, const char *storage_id, ms_storage_type_t store_type);
+int ms_validaty_change_all_items(void **handle, const char *storage_id, ms_storage_type_t store_type, bool validity);
+bool ms_delete_invalid_items(void **handle, const char *storage_id, ms_storage_type_t store_type);
+int ms_set_folder_item_validity(void **handle, const char *storage_id, const char *path, int validity, int recursive);
+int ms_delete_invalid_items_in_folder(void **handle, const char* storage_id, const char*path, bool is_recursive);
+int ms_send_dir_update_noti(void **handle, const char* storage_id, const char*path, const char*folder_id, ms_noti_type_e noti_type, int pid);
+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);
+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);
+int ms_insert_folder(void **handle, const char *storage_id, const char *path);
+int ms_delete_invalid_folder(void **handle, const char *storage_id);
+int ms_set_folder_validity(void **handle, const char *storage_id, const char *start_path, int validity, bool is_recursive);
+int ms_scan_item_batch(void **handle, const char *storage_id, const char *path);
+int ms_scan_validate_item(void **handle, const char *storage_id, const char *path);
+
+int ms_check_db_upgrade(void **handle, bool *need_full_scan);
+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);
+int ms_update_storage(void **handle, const char *storage_id, const char *storage_path);
+int ms_delete_storage(void **handle, const char *storage_id, const char *storage_name);
+int ms_set_storage_validity(void **handle, const char *storage_id, int validity);
+int ms_set_all_storage_validity(void **handle, int validity);
+int ms_update_meta_batch(void **handle, const char *path);
+int ms_delete_invalid_folder_by_path(void **handle, const char *storage_id, const char *folder_path, int *delete_count);
+int ms_check_folder_exist(void **handle, const char *storage_id, const char *folder_path);
+int ms_check_subfolder_count(void **handle, const char *storage_id, const char *folder_path, int *count);
+int ms_get_folder_id(void **handle, const char *storage_id, const char *path, char **folder_id);
+int ms_get_delete_count();
+void ms_reset_delete_count();
+
+/****************************************************************************************************
+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);
+void ms_validate_start(void **handle);
+void ms_validate_end(void **handle);
+void ms_insert_folder_start(void **handle);
+void ms_insert_folder_end(void **handle);
+void ms_update_start(void **handle);
+void ms_update_end(void **handle);
+
+#endif /*_MEDIA_COMMON_DB_SVC_H_*/
index af9e42f..eaee743 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-dbg.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
-
-#ifndef _MEDIA_SERVER_DBG_H_
-#define _MEDIA_SERVER_DBG_H_
+#ifndef _MEDIA_COMMON_DBG_H_
+#define _MEDIA_COMMON_DBG_H_
 
 #include <sys/syscall.h>
 #include <dlog.h>
 
 #define MS_DBG_STRERROR(fmt) do { \
                        char buf[BUF_LENGTH] = {0,}; \
-                       strerror_r(errno, buf, BUF_LENGTH); \
-                       LOGE(fmt" : STANDARD ERROR [%s]", buf);  \
+                       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_SLOG(fmt, args...)      SECURE_LOGD(fmt "\n", ##args);
-
-#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)
 
-#endif /*_MEDIA_SERVER_DBG_H_*/
+#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 a6cf430..dfd4fcd 100755 (executable)
 
 #include "media-common-types.h"
 
-void
-ms_make_default_path_mmc(void);
-
-int
-ms_update_mmc_info(void);
-
-void
-ms_mmc_removed_handler(void);
+void ms_make_default_path_mmc(void);
 
 #ifndef DISABLE_NOTIFICATION
-int
-ms_present_mmc_status(ms_sdcard_status_type_t status);
+int ms_present_mmc_status(ms_sdcard_status_type_t status, int flags);
 #endif
 
-void
-ms_mmc_vconf_cb(void *data);
+int ms_get_mmc_id(char **cid);
+int ms_get_stg_changed_event(void);
 
-ms_dir_scan_type_t
-ms_get_mmc_state(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-security.h b/src/common/include/media-common-security.h
new file mode 100644 (file)
index 0000000..76d3fa6
--- /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_COMMON_SECURITY_H_
+#define _MEDIA_COMMON_SECURITY_H_
+
+int ms_privilege_ask(int client_sockfd);
+int ms_check_client_permission_by_fd(int client_sockfd, const char *file_path);
+int ms_check_client_permission_by_pid(int pid, const char *file_path);
+
+#endif
+
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..ee24c16
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ *  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;
+       int flags;
+} ms_block_info_s;
+
+typedef void (*block_changed_cb)(ms_block_info_s *block_info, 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 struct ms_power_info_s{
+       int option;
+} ms_power_info_s;
+
+typedef void (*power_off_cb)(ms_power_info_s *power_info, void *user_data);
+int ms_sys_set_poweroff_cb(power_off_cb user_callback, void *user_data);
+int ms_sys_unset_poweroff_cb(void);
+
+#endif
index dfe0b75..36267b5 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-common-types.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
-
 #ifndef _MEDIA_SERVER_TYPES_H_
 #define _MEDIA_SERVER_TYPES_H_
 
 #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 -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); memset(src, 0x0, size);} }
+                                                       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 */
 
 #define MS_SOCK_NOT_ALLOCATE -1
 
+#define MS_INI_DEFAULT_PATH "/usr/etc/media_content_config.ini"
+
 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 {
        MS_SDCARD_INSERTED,    /**< Stored only in phone */
        MS_SDCARD_REMOVED,           /**< Stored only in MMC */
 } ms_sdcard_status_type_t;
+#define MS_SDCARD_REMOVED_UNSAFE 0x0001 /* Removed without unmount */
 
 typedef enum {
        MS_SCAN_INVALID,
@@ -83,7 +80,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 {
@@ -106,8 +104,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 3f01b7e..e4c012e 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-common-utils.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
 
 #ifndef _MEDIA_SERVER_UTILS_H__
 #define _MEDIA_SERVER_UTILS_H__
 
 #include "media-common-types.h"
 
-int
-ms_db_init(bool need_db_create);
-
-bool
-ms_is_mmc_inserted(void);
-
-void
-ms_usb_vconf_cb(void *data);
-
-int
-ms_start(bool need_db_create);
-
-void
-ms_end(void);
-
-ms_storage_type_t
-ms_get_storage_type_by_full(const char *path);
-
-int
-ms_strappend(char *res, const int size, const char *pattern,
-                const char *str1, const char *str2);
-
-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_set_str(const char *key, const char *value);
-
-bool
-ms_config_get_bool(const char *key, int *value);
-
+int ms_db_init(bool need_db_create);
+bool ms_is_mmc_inserted(void);
+void ms_usb_vconf_cb(void *data);
+int ms_start(bool need_db_create);
+void ms_end(void);
+ms_storage_type_t ms_get_storage_type_by_full(const char *path);
+int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2);
+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_set_str(const char *key, const char *value);
+bool ms_config_get_bool(const char *key, int *value);
 #ifdef FMS_PERF
-void
-ms_check_start_time(struct timeval *start_time);
-
-void
-ms_check_end_time(struct timeval *end_time);
-
-void
-ms_check_time_diff(struct timeval *start_time, struct timeval *end_time);
+void ms_check_start_time(struct timeval *start_time);
+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 */
 
-typedef void (*power_off_cb)(void *user_data);
-
-int ms_add_poweoff_event_receiver(power_off_cb user_callback, void *user_data);
-/**
- * @}
- */
 #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..834e2ee
--- /dev/null
@@ -0,0 +1,1385 @@
+/*
+ *  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"
+
+#define CONFIG_PATH "/usr/etc/media-server-plugin"
+#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;
+static int delete_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,
+       eDELETE_INVALID_FOLDER_BY_PATH,
+       eCHECK_FOLDER_EXIST,
+       eCOUNT_SUBFOLDER,
+       eGET_FOLDER_ID,
+       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",
+               "delete_invalid_folder_by_path",
+               "check_folder_exist",
+               "count_subfolder",
+               "get_folder_id",
+               };
+       /*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;
+}
+
+int ms_get_delete_count()
+{
+       return delete_count;
+}
+
+void ms_reset_insert_count()
+{
+       insert_count = 0;
+}
+
+void ms_reset_delete_count()
+{
+       delete_count = 0;
+}
+
+int ms_connect_db(void ***handle)
+{
+       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);
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((CONNECT)func_array[lib_index][eCONNECT])(&((*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);
+                       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)
+{
+       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);
+                       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, &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, &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);
+                                       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)
+{
+       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);
+                       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, &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, &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);
+                                       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)
+{
+       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, &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)
+{
+       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);
+
+       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, &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)
+{
+       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);
+
+       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, &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)
+{
+       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);
+
+       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, &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)
+{
+       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);
+
+       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, &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)
+{
+       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);
+
+       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, &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)
+{
+       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, &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)
+{
+       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, &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)
+{
+       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,&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)
+{
+       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, &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, const char*folder_id, ms_noti_type_e noti_type, int pid)
+{
+       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, folder_id, (int)noti_type, 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);
+                       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;
+               }
+       }
+
+       delete_count = *count;
+
+       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 *item_num_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, &item_num_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];
+                               dir_info->item_num= item_num_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)
+{
+       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, &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)
+{
+       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, &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)
+{
+       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);
+
+       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, &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)
+{
+       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, &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 *start_path, int validity, bool is_recursive)
+{
+       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, start_path, validity, is_recursive, &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)
+{
+       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, &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)
+{
+       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);
+
+       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, &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)
+{
+       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, &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)
+{
+       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, &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)
+{
+       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, &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)
+{
+       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, &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)
+{
+       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);
+
+       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, &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_by_path(void **handle, const char *storage_id, const char *folder_path, int *delete_count)
+{
+       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_BY_PATH)func_array[lib_index][eDELETE_INVALID_FOLDER_BY_PATH])(handle[lib_index], storage_id, folder_path, delete_count, &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_folder_exist(void **handle, const char *storage_id, const char *folder_path)
+{
+       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 = ((CHECK_FOLDER_EXIST)func_array[lib_index][eCHECK_FOLDER_EXIST])(handle[lib_index], storage_id, folder_path, &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_subfolder_count(void **handle, const char *storage_id, const char *folder_path, int *count)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       int cnt = 0;
+       char *err_msg = NULL;
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((COUNT_SUBFOLDER)func_array[lib_index][eCOUNT_SUBFOLDER])(handle[lib_index], storage_id, folder_path, &cnt, &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;
+               }
+       }
+
+       *count = cnt;
+
+       return res;
+}
+
+int ms_get_folder_id(void **handle, const char *storage_id, const char *path, char **folder_id)
+{
+       int lib_index;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret;
+       char *err_msg = NULL;
+       char folder_uuid[MS_UUID_SIZE] = {0,};
+
+       MS_DBG_FENTER();
+
+       for (lib_index = 0; lib_index < lib_num; lib_index++) {
+               ret = ((GET_FOLDER_ID)func_array[lib_index][eGET_FOLDER_ID])(handle[lib_index], storage_id, path, folder_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);
+                       res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
+               }
+       }
+
+       if (strlen(folder_uuid) == (MS_UUID_SIZE-1)) {
+               *folder_id = strdup(folder_uuid);
+       } else {
+               *folder_id = NULL;
+       }
+
+       MS_DBG("folder_id [%s]", folder_id);
+
+       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)
+{
+       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], &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);/*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)
+{
+       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], &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)
+{
+       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], &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)
+{
+       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], &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);
+               }
+       }
+}
+
index 279de76..bbbee85 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"
 
 #define MMC_INFO_SIZE 256
@@ -44,6 +56,23 @@ char default_path[][MS_FILE_NAME_LEN_MAX + 1] = {
 
 #define DIR_NUM       ((int)(sizeof(default_path)/sizeof(default_path[0])))
 
+#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    "DeviceChanged2"
+
+GDBusConnection *g_stg_bus;
+int g_stg_added_handler;
+
 void
 ms_make_default_path_mmc(void)
 {
@@ -74,54 +103,7 @@ ms_make_default_path_mmc(void)
        }
 }
 
-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;
-       }
-
-       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;
-}
-
-static int
-_get_contents(const char *filename, char *buf)
+static int __get_contents(const char *filename, char *buf)
 {
        FILE *fp;
 
@@ -139,52 +121,22 @@ _get_contents(const char *filename, char *buf)
 }
 
 /*need optimize*/
-int
-_ms_get_mmc_info(char *cid)
+int ms_get_mmc_id(char **cid)
 {
-       char *path = "/sys/block/mmcblk1/device/cid";
+       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, cid) != MS_MEDIA_ERR_NONE) {
+       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)
@@ -205,8 +157,7 @@ void update_lang(void)
        }
 }
 
-int
-ms_present_mmc_status(ms_sdcard_status_type_t status)
+int ms_present_mmc_status(ms_sdcard_status_type_t status, int flags)
 {
        int ret = NOTIFICATION_ERROR_NONE;
 
@@ -214,11 +165,148 @@ ms_present_mmc_status(ms_sdcard_status_type_t status)
 
        if (status == MS_SDCARD_INSERTED)
                ret = notification_status_message_post(_GETSYSTEMSTR("IDS_COM_BODY_PREPARING_SD_CARD"));
-       else if (status == MS_SDCARD_REMOVED)
-               ret = notification_status_message_post(_GETSYSTEMSTR("IDS_COM_BODY_SD_CARD_UNEXPECTEDLY_REMOVED"));
+       else if (status == MS_SDCARD_REMOVED) {
+               if (flags & MS_SDCARD_REMOVED_UNSAFE)
+                       ret = notification_status_message_post(_GETSYSTEMSTR("IDS_COM_BODY_SD_CARD_UNEXPECTEDLY_REMOVED"));
+       }
 
        if(ret != NOTIFICATION_ERROR_NONE)
                return MS_MEDIA_ERR_INTERNAL;
        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_getdents, 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-security.c b/src/common/media-common-security.c
new file mode 100644 (file)
index 0000000..528fbb8
--- /dev/null
@@ -0,0 +1,209 @@
+/*
+ *  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 <glib.h>
+#include <sys/smack.h>
+#include <security-server.h>
+
+#include "media-util-err.h"
+
+#include "media-common-dbg.h"
+#include "media-common-types.h"
+#include "media-common-security.h"
+
+#define MEDIA_DB_SMACK_LABEL "media-data::db"
+#define MEDIA_DB_SMACK_ACCESS_TYPE "w"
+
+int ms_privilege_ask(int client_sockfd)
+{
+       int ret = 0;
+       int res = MS_MEDIA_ERR_NONE;
+
+       ret = security_server_check_privilege_by_sockfd(client_sockfd, MEDIA_DB_SMACK_LABEL, MEDIA_DB_SMACK_ACCESS_TYPE);
+       if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
+               MS_DBG_ERR("You do not have permission for this operation.");
+               res = MS_MEDIA_ERR_PERMISSION_DENIED;
+       } else {
+               MS_DBG("PERMISSION OK");
+       }
+
+       return res;
+}
+
+int ms_get_file_smack_label(const char *path, char **label)
+{
+       int ret = 0;
+       int res = MS_MEDIA_ERR_NONE;
+       enum smack_label_type type = SMACK_LABEL_ACCESS;
+
+       ret = smack_getlabel(path, label, type);
+       if (ret == -1) {
+               MS_DBG_ERR("Fail to get the label");
+               res = MS_MEDIA_ERR_INTERNAL;
+       }
+
+       return res;
+}
+
+int ms_get_client_smack_label(int client_sockfd, char **label)
+{
+       int ret = 0;
+       int res = MS_MEDIA_ERR_NONE;
+
+       ret = smack_new_label_from_socket(client_sockfd, label);
+       if (ret <= 0) {
+               MS_DBG_ERR("Fail to get the client label");
+               res = MS_MEDIA_ERR_INTERNAL;
+       }
+
+       return res;
+}
+
+int ms_check_smack_rule(const char *subject, const char *object, const char *access_type)
+{
+       int ret = 0;
+       int res = MS_MEDIA_ERR_NONE;
+
+       ret = smack_have_access(subject, object, access_type);
+       if (ret != 1) {
+               if (ret == -1) {
+                       MS_DBG_ERR("Fail to check the smack rule");
+                       res = MS_MEDIA_ERR_INTERNAL;
+               } else {
+                       MS_DBG_ERR("Do not have permission");
+                       res = MS_MEDIA_ERR_PERMISSION_DENIED;
+               }
+       }
+
+       return res;
+}
+
+int ms_check_smack_by_pid(int pid, const char *object, const char *access_type)
+{
+       int ret = 0;
+       int res = MS_MEDIA_ERR_NONE;
+
+       ret = security_server_check_privilege_by_pid(pid, object, access_type);
+       if (ret != SECURITY_SERVER_API_SUCCESS) {
+               if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
+                       MS_DBG_ERR("Do not have permission");
+                       res = MS_MEDIA_ERR_PERMISSION_DENIED;
+               } else {
+                       MS_DBG_ERR("Fail to check the smack rule[%d]", ret);
+                       res = MS_MEDIA_ERR_INTERNAL;
+               }
+       }
+
+       return res;
+}
+
+int ms_check_client_permission_by_fd(int client_sockfd, const char *file_path)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       char *dir_path = NULL;
+       char *subject_label = NULL;
+       char *file_object_label = NULL;
+       char *dir_object_label = NULL;
+       const char *dir_access = "x";
+       const char *file_access = "r";
+
+       dir_path = (char*)g_path_get_dirname(file_path);
+
+       ret = ms_get_client_smack_label(client_sockfd, &subject_label);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               goto ERROR;
+       }
+
+       ret = ms_get_file_smack_label(file_path, &file_object_label);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               goto ERROR;
+       }
+
+       ret = ms_get_file_smack_label(dir_path, &dir_object_label);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               goto ERROR;
+       }
+
+       ret = ms_check_smack_rule(subject_label, dir_object_label, dir_access);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("Err[%d] Subject[%s] Object[%s] Path[%s]", ret, subject_label, dir_object_label, dir_path);
+               goto ERROR;
+       }
+
+       ret = ms_check_smack_rule(subject_label, file_object_label, file_access);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("Err[%d] Subject[%s] Object[%s] Path[%s]", ret, subject_label, file_object_label, file_path);
+               goto ERROR;
+       }
+
+ERROR:
+
+       MS_SAFE_FREE(dir_path);
+       MS_SAFE_FREE(subject_label);
+       MS_SAFE_FREE(file_object_label);
+       MS_SAFE_FREE(dir_object_label);
+
+       return ret;
+}
+
+int ms_check_client_permission_by_pid(int pid, const char *file_path)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       char *dir_path = NULL;
+       char *subject_label = NULL;
+       char *file_object_label = NULL;
+       char *dir_object_label = NULL;
+       const char *dir_access = "x";
+       const char *file_access = "r";
+
+       dir_path = (char*)g_path_get_dirname(file_path);
+
+       ret = ms_get_file_smack_label(file_path, &file_object_label);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               goto ERROR;
+       }
+
+       ret = ms_get_file_smack_label(dir_path, &dir_object_label);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               goto ERROR;
+       }
+
+       ret = ms_check_smack_by_pid(pid, dir_object_label, dir_access);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("Err[%d] PID[%d] Object[%s] Path[%s]", ret, pid, dir_object_label, dir_path);
+               goto ERROR;
+       }
+
+       ret = ms_check_smack_by_pid(pid, file_object_label, file_access);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("Err[%d] PID[%d] Object[%s] Path[%s]", ret, pid, file_object_label, file_path);
+               goto ERROR;
+       }
+
+ERROR:
+
+       MS_SAFE_FREE(dir_path);
+       MS_SAFE_FREE(subject_label);
+       MS_SAFE_FREE(file_object_label);
+       MS_SAFE_FREE(dir_object_label);
+
+       return ret;
+}
+
diff --git a/src/common/media-common-system.c b/src/common/media-common-system.c
new file mode 100755 (executable)
index 0000000..66234ab
--- /dev/null
@@ -0,0 +1,487 @@
+/*
+ *  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 <stdlib.h>
+#include <gio/gio.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib-lowlevel.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    "DeviceChanged2"
+
+#define BLOCK_DEVICE_METHOD "GetDeviceList2"
+#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_path = NULL;
+       GVariant *tmp;
+       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;
+       ms_block_info_s *block_info = NULL;
+
+       MS_MALLOC(block_info, sizeof(ms_block_info_s));
+       if (block_info == NULL) {
+               MS_DBG_ERR("malloc failed");
+               return;
+       }
+
+       tmp = g_variant_get_child_value(parameters, 0);
+       block_info->block_type = g_variant_get_int32(tmp);
+       MS_DBG_ERR("block_type : %d", block_info->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_path = g_variant_get_string (tmp, &size);
+       if (mount_path != NULL) {
+               block_info->mount_path = strdup(mount_path);
+               MS_DBG_ERR("mount_point : %s", block_info->mount_path);
+       }
+
+       tmp = g_variant_get_child_value(parameters, 9);
+       block_info->state = g_variant_get_int32 (tmp);
+       MS_DBG_ERR("state : %d", block_info->state);
+
+       tmp = g_variant_get_child_value(parameters, 11);
+       block_info->flags = g_variant_get_int32 (tmp);
+       MS_DBG_ERR("flags : %d", block_info->flags);
+
+       ((block_changed_cb)usr_cb)(block_info, usr_data);
+       MS_SAFE_FREE(block_info->mount_path);
+       MS_SAFE_FREE(block_info);
+
+       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(&piter);
+               dbus_message_iter_get_basic(&piter, &val_int);
+               MS_DBG("\tflags(%d)", val_int);
+               data->flags = val_int;
+
+               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;
+}
+
+//////////////////////////////////////////////////////////////////////////////
+/// 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;
+               ms_power_info_s *power_info = NULL;
+
+               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(power_info, 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 bdf86be..446b50d 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-utils.c
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief       This file implements main database operation.
- */
-
 #include <errno.h>
 #include <vconf.h>
 #include <dbus/dbus-glib.h>
@@ -37,6 +28,7 @@
 #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
@@ -49,20 +41,17 @@ struct timeval g_mmc_end_time;
 #define MS_DRM_CONTENT_TYPE_LENGTH 100
 
 #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;
@@ -75,31 +64,41 @@ 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;
 }
 
-ms_storage_type_t
-ms_get_storage_type_by_full(const char *path)
+ms_storage_type_t ms_get_storage_type_by_full(const char *path)
 {
        if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
                return MS_STORAGE_INTERNAL;
        } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
                return MS_STORAGE_EXTERNAL;
+       }  else if (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0) {
+               return MS_STORAGE_EXTERNAL_USB;
        } else
                return MS_MEDIA_ERR_INVALID_PATH;
 }
 
-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;
@@ -108,7 +107,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);
@@ -121,8 +120,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;
@@ -132,7 +130,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;
        }
@@ -148,8 +146,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;
 
@@ -169,8 +166,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;
 
@@ -190,27 +186,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 = NULL;
-       if (!key || !value) {
+
+       if (key == NULL || value == NULL) {
                MS_DBG_ERR("Arguments key or value is NULL");
                return false;
        }
 
        res = vconf_get_str(key);
-       if ((res!= NULL) && (strlen(res) > 0)) {
-               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;
 
@@ -228,8 +223,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;
 
@@ -248,106 +242,3 @@ ms_config_get_bool(const char *key, int *value)
 
        return false;
 }
-
-#define SYS_DBUS_NAME "ChangeState"
-#define SYS_DBUS_PATH "/Org/Tizen/System/DeviceD/PowerOff"
-#define SYS_DBUS_INTERFACE "org.tizen.system.deviced.PowerOff"
-#define SYS_DBUS_MATCH_RULE "type='signal',interface='org.tizen.system.deviced.PowerOff'"
-
-typedef struct pwoff_callback_data{
-       power_off_cb user_callback;
-       void *user_data;
-} pwoff_callback_data;
-
-DBusHandlerResult
-__get_dbus_message(DBusMessage *message, void *user_cb, void *userdata)
-{
-       MS_DBG("");
-
-       /* A Ping signal on the com.burtonini.dbus.Signal interface */
-       if (dbus_message_is_signal (message, SYS_DBUS_INTERFACE, SYS_DBUS_NAME)) {
-               int current_type = DBUS_TYPE_INVALID;
-               DBusError error;
-               DBusMessageIter read_iter;
-               DBusBasicValue value;
-               power_off_cb cb_func = (power_off_cb)user_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(userdata);
-
-               return DBUS_HANDLER_RESULT_HANDLED;
-       }
-
-       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
-}
-
-static DBusHandlerResult
-__sysman_message_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
-{
-       DBusHandlerResult ret;
-
-       pwoff_callback_data *cb_data = (pwoff_callback_data *)user_data;
-
-       MS_DBG("");
-
-       ret = __get_dbus_message(message, cb_data->user_callback, cb_data->user_data);
-
-       MS_DBG("");
-
-       return ret;
-}
-
-int ms_add_poweoff_event_receiver(power_off_cb user_callback, void *user_data)
-{
-       DBusConnection *dbus;
-       DBusError error;
-       pwoff_callback_data *cb_data = NULL;
-
-       /*add noti receiver for power off*/
-       dbus_error_init (&error);
-
-       dbus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
-       if (!dbus) {
-               MS_DBG_ERR ("Failed to connect to the D-BUS daemon: %s", error.message);
-               return MS_MEDIA_ERR_DBUS_GET;
-       }
-
-       dbus_connection_setup_with_g_main (dbus, NULL);
-
-       cb_data = malloc(sizeof(pwoff_callback_data));
-       cb_data->user_callback = user_callback;
-       cb_data->user_data = user_data;
-
-       /* listening to messages from all objects as no path is specified */
-       dbus_bus_add_match (dbus, SYS_DBUS_MATCH_RULE, &error);
-       if( !dbus_connection_add_filter (dbus, __sysman_message_filter, cb_data, NULL)) {
-               dbus_bus_remove_match (dbus, SYS_DBUS_MATCH_RULE, NULL);
-               return MS_MEDIA_ERR_DBUS_ADD_FILTER;
-               MS_DBG_ERR("");
-       }
-
-       return MS_MEDIA_ERR_NONE;
-}
\ No newline at end of file
old mode 100644 (file)
new mode 100755 (executable)
index 20552e2..35b6a20
 #include <sys/stat.h>
 #include <unistd.h>
 #include <dirent.h>
-
+#include <dlfcn.h>
 
 #include <glib.h>
 #include "media-util.h"
 
 GMainLoop * mainloop = NULL;
 
+int (*svc_connect)                             (void ** handle, char ** err_msg);
+int (*svc_disconnect)                  (void * handle, char ** err_msg);
+int (*svc_check_db)                    (void * handle, char ** err_msg);
+int (*svc_get_storage_id)              (void * handle, const char *path, char *storage_id, char ** err_msg);
+
 void callback(media_request_result_s * result, void *user_data)
 {
        printf("db updating done\n");
@@ -57,14 +62,92 @@ void print_help()
        printf("=======================================================================================\n");
 }
 
-int dir_scan_non_recursive(char *path)
+static void __check_media_db(void)
+{
+       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_check_db            = dlsym (funcHandle, "check_db");
+
+       ret = svc_connect(&db_handle, &err_msg);
+       if(ret < 0)
+               printf("Error svc_connect\n");
+
+       ret = svc_check_db(db_handle, &err_msg);
+       if(ret < 0)
+               printf("Error svc_check_db\n");
+
+       ret = svc_disconnect(db_handle, &err_msg);
+       if(ret < 0)
+               printf("Error svc_disconnect\n");
+
+       printf("Check media db done\n");
+
+       dlclose (funcHandle);
+}
+
+static void __get_storage_id(const char *path, char *storage_id)
 {
-       return media_directory_scanning_async(path, FALSE, callback, NULL);
+       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, &err_msg);
+       if(ret < 0)
+               printf("Error svc_connect\n");
+
+       ret = svc_get_storage_id(db_handle, path, storage_id, &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);
+
+       return media_directory_scanning_async(path, storage_id, FALSE, callback, NULL);
 }
 
 int dir_scan_recursive(char *path)
 {
-       return media_directory_scanning_async(path, TRUE, callback, NULL);
+       char storage_id[36+1] = {0,};
+
+       __get_storage_id(path, storage_id);
+
+       return media_directory_scanning_async(path, storage_id, TRUE, callback, NULL);
 }
 
 typedef enum {
@@ -125,29 +208,22 @@ int main(int argc, char **argv)
                        exit(1);
                }
 
-               if (check_path(argv1) == DIRECTORY_OK) {
-                       ret = dir_scan_non_recursive(argv1);
-                       if (ret != 0) {
-                               printf("error : %d\n", ret);
-                               exit(1);
-                       }
-               } else {
-                       printf("[%d]invalid path\n", __LINE__);
-                       print_help();
+               if (strcmp(argv1 , "check_db") == 0) {
+                       __check_media_db();
+                       exit(1);
+               }
+
+               ret = dir_scan_non_recursive(argv1);
+               if (ret != 0) {
+                       printf("error : %d\n", ret);
                        exit(1);
                }
        } else if (argc == 3) {
                argv2 = strdup(argv[2]);
                if (strcmp(argv1, "-r") == 0) {
-                       if (check_path(argv2) == DIRECTORY_OK) {
-                               ret = dir_scan_recursive(argv2);
-                               if (ret != 0) {
-                                       printf("error : %d\n", ret);
-                                       exit(1);
-                               }
-                       } else {
-                               printf("[%d]invalid path\n", __LINE__);
-                               print_help();
+                       ret = dir_scan_recursive(argv2);
+                       if (ret != 0) {
+                               printf("error : %d\n", ret);
                                exit(1);
                        }
                } else {
diff --git a/src/scanner-v2/include/media-scanner-common-v2.h b/src/scanner-v2/include/media-scanner-common-v2.h
new file mode 100644 (file)
index 0000000..30f6356
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ *  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_COMMON_V2_H_
+#define _MEDIA_SCANNER_COMMON_V2_H_
+
+#include "stdbool.h"
+#include "media-common-system.h"
+
+int msc_set_mmc_status(ms_stg_status_e status);
+int msc_get_mmc_status(ms_stg_status_e *status);
+int msc_set_power_status(bool status);
+int msc_get_power_status(bool *status);
+
+#endif /*_MEDIA_SCANNER_COMMON_V2_H_*/
diff --git a/src/scanner-v2/include/media-scanner-dbg-v2.h b/src/scanner-v2/include/media-scanner-dbg-v2.h
new file mode 100755 (executable)
index 0000000..d45d756
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ *  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_DBG_V2_H_
+#define _MEDIA_SCANNER_DBG_V2_H_
+#include <sys/syscall.h>
+#include <dlog.h>
+#include <errno.h>
+
+#include "media-common-dbg.h"
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+
+#define LOG_TAG "MEDIA_SCANNER_V2"
+
+#endif /*_MEDIA_SCANNER_DBG_V2_H_*/
+
diff --git a/src/scanner-v2/include/media-scanner-device-block-v2.h b/src/scanner-v2/include/media-scanner-device-block-v2.h
new file mode 100755 (executable)
index 0000000..b70f6d6
--- /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_V2_H_
+#define _MEDIA_SCANNER_DEVICE_BLOCK_V2_H_
+
+#include "media-common-system.h"
+
+void msc_device_block_changed_cb(ms_block_info_s *block_info, void *user_data);
+
+#endif
\ No newline at end of file
diff --git a/src/scanner-v2/include/media-scanner-extract-v2.h b/src/scanner-v2/include/media-scanner-extract-v2.h
new file mode 100755 (executable)
index 0000000..a7855e2
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ *  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_EXTRACT_V2_H_
+#define _MEDIA_SCANNER_EXTRACT_V2_H_
+
+typedef enum {
+       MS_EXTRACT_STORAGE = 0,
+       MS_EXTRACT_DIRECTORY = 1,
+       MS_EXTRACT_MAX,
+} ms_extract_type_e;
+
+int msc_init_extract_thread();
+int msc_deinit_extract_thread();
+gboolean msc_folder_extract_thread(void *data);
+gboolean msc_storage_extract_thread(void *data);
+void msc_insert_exactor_request(int message_type, bool ins_status, const char *storage_id, const char *path, int pid);
+int msc_remove_extract_request(const ms_comm_msg_s *recv_msg);
+int msc_set_extract_cancel_path(const char *cancel_path);
+int msc_del_extract_cancel_path(void);
+int msc_set_extract_blocked_path(const char *blocked_path);
+int msc_del_extract_blocked_path(void);
+int msc_push_extract_request(ms_extract_type_e scan_type, ms_comm_msg_s *recv_msg);
+int msc_stop_extract_thread(void);
+int msc_get_remain_extract_request(ms_extract_type_e scan_type, int *remain_request);
+int msc_get_dir_extract_status(bool *extract_status);
+
+#endif /*_MEDIA_SCANNER_EXTRACT_V2_H_*/
diff --git a/src/scanner-v2/include/media-scanner-scan-v2.h b/src/scanner-v2/include/media-scanner-scan-v2.h
new file mode 100755 (executable)
index 0000000..261fb1f
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ *  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_SCAN_V2_H_
+#define _MEDIA_SCANNER_SCAN_V2_H_
+
+#include "media-common-system.h"
+
+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_init_scan_thread();
+int msc_deinit_scan_thread();
+int msc_set_blocked_path(const char *blocked_path);
+int msc_del_blocked_path(void);
+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_stop_scan_thread(void);
+int msc_get_remain_scan_request(ms_scan_type_e scan_type, int *remain_request);
+int msc_get_dir_scan_status(bool *scan_status);
+
+#endif /*_MEDIA_SCANNER_SCAN_V2_H_*/
\ No newline at end of file
similarity index 50%
rename from src/server/include/media-server-db-svc.h
rename to src/scanner-v2/include/media-scanner-socket-v2.h
index 010de85..15434bf 100755 (executable)
  *
  */
 
-/**
- * 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_
+#ifndef _MEDIA_SCANNER_SOCKET_V2_H_
+#define _MEDIA_SCANNER_SOCKET_V2_H_
 
 #include "media-common-types.h"
+#include "media-server-ipc.h"
 
-typedef int (*CONNECT)(void**, char **);
-typedef int (*DISCONNECT)(void*, char **);
-typedef int (*SET_ALL_STORAGE_ITEMS_VALIDITY)(void*, int, int, char **);
-typedef int (*CHECK_DB)(void*, char **);
-
-int
-ms_load_functions(void);
-
-void
-ms_unload_functions(void);
-
-int
-ms_connect_db(void ***handle);
-
-int
-ms_disconnect_db(void ***handle);
+gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer data);
 
-int
-ms_invalidate_all_items(void **handle, ms_storage_type_t store_type);
+int msc_send_ready(void);
 
-int
-ms_check_db_upgrade(void **handle);
+int msc_send_result(int result, ms_comm_msg_s *scan_data);
 
-#endif /*_MEDIA_SERVER_DB_SVC_H_*/
\ No newline at end of file
+#endif /*_MEDIA_SCANNER_SOCKET_V2_H_*/
diff --git a/src/scanner-v2/media-scanner-common-v2.c b/src/scanner-v2/media-scanner-common-v2.c
new file mode 100644 (file)
index 0000000..b747376
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ *  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-err.h"
+
+#include "media-scanner-dbg-v2.h"
+#include "media-scanner-common-v2.h"
+
+int mmc_state2 = 0;
+bool power_off2;
+
+int msc_set_mmc_status(ms_stg_status_e status)
+{
+       mmc_state2 = status;
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_get_mmc_status(ms_stg_status_e *status)
+{
+       *status = mmc_state2;
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_set_power_status(bool status)
+{
+       power_off2 = status;
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_get_power_status(bool *status)
+{
+       *status = power_off2;
+
+       return MS_MEDIA_ERR_NONE;
+}
+
diff --git a/src/scanner-v2/media-scanner-device-block-v2.c b/src/scanner-v2/media-scanner-device-block-v2.c
new file mode 100755 (executable)
index 0000000..9564130
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ *  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 <vconf.h>
+
+#include "media-util.h"
+
+#include "media-common-utils.h"
+#include "media-common-db-svc.h"
+#include "media-common-external-storage.h"
+
+#include "media-util-err.h"
+#include "media-scanner-dbg-v2.h"
+#include "media-scanner-scan-v2.h"
+#include "media-scanner-extract-v2.h"
+#include "media-scanner-device-block-v2.h"
+
+static void __msc_usb_remove_event(const char *mount_path)
+{
+       MS_DBG_ERR("===========================================================");
+       MS_DBG_ERR("USB REMOVED, mountpath : %s", mount_path);
+       MS_DBG_ERR("===========================================================");
+       int update_status  = -1;
+       int remain_request = 0;
+       bool status = FALSE;
+
+       if(!ms_config_get_int(VCONFKEY_FILEMANAGER_DB_STATUS, &update_status)) {
+               MS_DBG_ERR("ms_config_get_int[VCONFKEY_FILEMANAGER_DB_STATUS]");
+       }
+
+       if (msc_get_remain_scan_request(MS_SCAN_STORAGE, &remain_request) == MS_MEDIA_ERR_NONE) {
+               if (!(remain_request == 0 && update_status == VCONFKEY_FILEMANAGER_DB_UPDATED)) {
+                       msc_set_blocked_path(mount_path);
+               }
+               remain_request = 0;
+       }
+
+
+       if (msc_get_dir_scan_status(&status) == MS_MEDIA_ERR_NONE) {
+               if (status == TRUE) {
+                       MS_DBG_ERR("Doing directory scanning. Set cancel path");
+                       msc_set_cancel_path(mount_path);
+               }
+               status = FALSE;
+       }
+
+       if (msc_get_remain_extract_request(MS_EXTRACT_STORAGE, &remain_request) == MS_MEDIA_ERR_NONE) {
+               if (!(remain_request == 0 && update_status == VCONFKEY_FILEMANAGER_DB_UPDATED)) {
+                       msc_set_extract_blocked_path(mount_path);
+               }
+               remain_request = 0;
+       }
+
+       if (msc_get_dir_scan_status(&status) == MS_MEDIA_ERR_NONE) {
+               if (status == true) {
+                       MS_DBG_ERR("Doing directory extracting. Set cancel path");
+                       msc_set_extract_cancel_path(mount_path);
+               }
+               status = FALSE;
+       }
+
+       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(ms_block_info_s *block_info, void *user_data)
+{
+       if (block_info->block_type == 0) {
+               MS_DBG_ERR("GET THE USB EVENT");
+               if (block_info->state == MS_STG_INSERTED) {
+                       /*DO NOT THING*/
+               } else {
+                       __msc_usb_remove_event(block_info->mount_path);
+               }
+       } else {
+               MS_DBG_ERR("GET THE MMC EVENT");
+               _msc_mmc_changed_event(block_info->mount_path, block_info->state);
+       }
+}
diff --git a/src/scanner-v2/media-scanner-extract-v2.c b/src/scanner-v2/media-scanner-extract-v2.c
new file mode 100755 (executable)
index 0000000..aac49a8
--- /dev/null
@@ -0,0 +1,762 @@
+/*
+ *  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.
+ *
+ */
+
+#define _GNU_SOURCE
+#include <dirent.h>     /* Defines DT_* constants */
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+
+#include <dirent.h>
+#include <errno.h>
+#include <malloc.h>
+#include <dd-display.h>
+#include <vconf.h>
+
+#include "media-util.h"
+#include "media-server-ipc.h"
+#include "media-common-utils.h"
+#include "media-common-db-svc.h"
+#include "media-scanner-dbg-v2.h"
+#include "media-scanner-common-v2.h"
+#include "media-scanner-socket-v2.h"
+#include "media-scanner-extract-v2.h"
+
+GAsyncQueue *storage_extract_queue;
+GAsyncQueue *folder_extract_queue;
+GMutex extract_req_mutex;
+GMutex extract_blocked_mutex;
+char *g_extract_cancel_path;
+char *g_extract_blocked_path;
+bool g_directory_extract_processing;
+int stg_extract_status;
+
+GCond extract_data_cond;
+GMutex extract_data_mutex;
+
+
+#ifdef FMS_PERF
+extern struct timeval g_mmc_start_time;
+extern struct timeval g_mmc_end_time;
+#endif
+
+static int __msc_check_extract_stop_status(int scan_type, ms_storage_type_t storage_type, const char *start_path);
+static int __msc_set_storage_extract_status(ms_storage_scan_status_e status);
+static int __msc_get_storage_extract_status(ms_storage_scan_status_e *status);
+static int __msc_resume_extract();
+static int __msc_pause_extract();
+static int __msc_extract_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type);
+static int __msc_extract_set_power_mode(ms_db_status_type_t status);
+
+int msc_init_extract_thread()
+{
+       if (!storage_extract_queue) storage_extract_queue = g_async_queue_new();
+       if (!folder_extract_queue) folder_extract_queue = g_async_queue_new();
+
+       g_mutex_init(&extract_req_mutex);
+       g_mutex_init(&extract_blocked_mutex);
+       g_mutex_init(&extract_data_mutex);
+
+       g_cond_init(&extract_data_cond);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_deinit_extract_thread()
+{
+       if (storage_extract_queue) g_async_queue_unref(storage_extract_queue);
+       if (folder_extract_queue) g_async_queue_unref(folder_extract_queue);
+
+       g_mutex_clear(&extract_req_mutex);
+       g_mutex_clear(&extract_blocked_mutex);
+       g_mutex_clear(&extract_data_mutex);
+
+       g_cond_clear(&extract_data_cond);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+gboolean msc_folder_extract_thread(void *data)
+{
+       ms_comm_msg_s *extract_data = NULL;
+       int err;
+       int ret;
+       void **handle = NULL;
+       int scan_type;
+       int storage_type;
+       char *storage_id = NULL;
+       char *update_path = NULL;
+       bool power_off_status = FALSE;
+
+       while (1) {
+               extract_data = g_async_queue_pop(folder_extract_queue);
+               if (extract_data->pid == POWEROFF) {
+                       MS_DBG_ERR("power off");
+                       goto _POWEROFF;
+               }
+
+               MS_DBG_ERR("[No-Error] DIRECTORY EXTRACT START folder_path [%s], scan_type [%d]", extract_data->msg, extract_data->msg_type);
+
+               g_directory_extract_processing = true;
+
+               /*connect to media db, if conneting is failed, db updating is stopped*/
+               err = ms_connect_db(&handle);
+               if (err != MS_MEDIA_ERR_NONE)
+                       continue;
+
+               scan_type = extract_data->msg_type;
+
+               storage_id = strdup(extract_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]", extract_data->msg, storage_id);
+
+               update_path = strndup(extract_data->msg, extract_data->msg_size);
+
+               if (strlen(storage_id) == 0) {
+                       MS_DBG_ERR("storage_id length is 0. There is no information of your request [%s]", extract_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) {
+                       MS_DBG_ERR("Invalid request");
+                       ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+                       goto NEXT;
+               }
+
+               storage_type = ms_get_storage_type_by_full(extract_data->msg);
+               ret = __msc_check_extract_stop_status(extract_data->msg_type, storage_type, update_path);
+
+               if (ret == MS_MEDIA_ERR_SCANNER_FORCE_STOP) {
+                       MS_DBG_ERR("MS_MEDIA_ERR_SCANNER_FORCE_STOP");
+                       goto NEXT;
+               }
+
+               /*insert data into media db */
+               ret = ms_insert_item_pass2(handle, extract_data->storage_id, update_path, scan_type);
+               msc_del_extract_cancel_path();
+
+               /*call for bundle commit*/
+               //__msc_bacth_commit_disable(handle, TRUE, TRUE, extract_data->storage_id, ret);
+
+NEXT:
+               msc_get_power_status(&power_off_status);
+               if (power_off_status) {
+                       MS_DBG_ERR("power off");
+                       goto _POWEROFF;
+               }
+
+               /*Active flush */
+               malloc_trim(0);
+
+               if (extract_data->result) {
+                       msc_send_result(ret, extract_data);
+               }
+
+               MS_SAFE_FREE(update_path);
+               MS_SAFE_FREE(extract_data);
+               MS_SAFE_FREE(storage_id);
+
+               g_directory_extract_processing = false;
+
+               MS_DBG_ERR("[No-Error] DIRECTORY EXTRACT END [%d]", ret);
+
+               ms_storage_scan_status_e storage_scan_status = MS_STORAGE_SCAN_NONE;
+               ret = __msc_get_storage_extract_status(&storage_scan_status);
+               if (ret == MS_MEDIA_ERR_NONE) {
+                       /*get storage list and scan status from media db*/
+                       if (storage_scan_status != MS_STORAGE_SCAN_COMPLETE) {
+                               __msc_resume_extract();
+                               MS_DBG_ERR("extract RESUME OK");
+                       }
+               }
+
+               /*disconnect form media db*/
+               if (handle) ms_disconnect_db(&handle);
+       }                       /*thread while*/
+
+_POWEROFF:
+       MS_SAFE_FREE(update_path);
+       MS_SAFE_FREE(extract_data);
+       MS_SAFE_FREE(storage_id);
+
+       if (handle) ms_disconnect_db(&handle);
+
+       return false;
+}
+
+gboolean msc_storage_extract_thread(void *data)
+{
+       ms_comm_msg_s *extract_data = NULL;
+       int ret;
+       int err;
+       void **handle = NULL;
+       ms_storage_type_t storage_type = MS_STORAGE_INTERNAL;
+       int scan_type;
+       //bool valid_status = TRUE;
+       char *update_path = NULL;
+       //GArray *dir_array = NULL;
+       bool power_off_status = FALSE;
+
+       while (1) {
+               __msc_set_storage_extract_status(MS_STORAGE_SCAN_DONE);
+               extract_data = g_async_queue_pop(storage_extract_queue);
+               if (extract_data->pid == POWEROFF) {
+                       MS_DBG_ERR("power off");
+                       goto _POWEROFF;
+               }
+
+               __msc_set_storage_extract_status(MS_STORAGE_SCAN_META_PROCESSING);
+               MS_DBG_ERR("STORAGE extract START ");
+
+               scan_type = extract_data->msg_type;
+               if (scan_type != MS_MSG_STORAGE_ALL
+                       && scan_type != MS_MSG_STORAGE_PARTIAL
+                       && scan_type != MS_MSG_STORAGE_INVALID) {
+                       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 = ms_connect_db(&handle);
+               if (err != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("ms_connect_db falied!");
+                       continue;
+               }
+
+               update_path = strndup(extract_data->msg, extract_data->msg_size);
+               MS_DBG_ERR("extract storage_id is [%s], path [%s]", extract_data->storage_id, update_path);
+
+               ms_set_storage_scan_status(handle, extract_data->storage_id, MEDIA_EXTRACT_PROCESSING);
+               storage_type = ms_get_storage_type_by_full(extract_data->msg);
+               __msc_extract_set_db_status(MS_DB_UPDATING, storage_type);
+
+               ret = __msc_check_extract_stop_status(extract_data->msg_type, storage_type, update_path);
+               if (ret == MS_MEDIA_ERR_SCANNER_FORCE_STOP) {
+                       MS_DBG_ERR("MS_MEDIA_ERR_SCANNER_FORCE_STOP");
+                       goto NEXT;
+               }
+
+               /*extract meta*/
+               ret = ms_insert_item_pass2(handle, extract_data->storage_id, update_path, scan_type);
+
+               storage_type = ms_get_storage_type_by_full(extract_data->msg);
+               //update_path = strndup(extract_data->msg, extract_data->msg_size);
+
+               msc_del_extract_blocked_path();
+
+               MS_DBG_ERR("extract PAUSE");
+               __msc_pause_extract();
+               MS_DBG_ERR("extract RESUME");
+
+               if (ret == MS_MEDIA_ERR_SCANNER_FORCE_STOP) {
+                       ms_set_storage_scan_status(handle, extract_data->storage_id, MEDIA_EXTRACT_STOP);
+                       __msc_set_storage_extract_status(MS_STORAGE_SCAN_META_STOP);
+                       /*set vconf key mmc loading for indicator */
+                       __msc_extract_set_db_status(MS_DB_STOPPED, storage_type);
+               } else if (extract_data->result == TRUE) {
+                       MS_DBG_ERR("extract_data->result == TRUE, MS_STORAGE_SCAN_COMPLETE");
+                       ms_set_storage_scan_status(handle, extract_data->storage_id, MEDIA_EXTRACT_COMPLETE);
+                       __msc_set_storage_extract_status(MS_STORAGE_SCAN_COMPLETE);
+                       ms_delete_invalid_items(handle, extract_data->storage_id, storage_type);
+                       /*set vconf key mmc loading for indicator */
+                       __msc_extract_set_db_status(MS_DB_UPDATED, storage_type);
+               }
+
+#if 0
+               if (scan_type == MS_MSG_STORAGE_PARTIAL && storage_type == MS_STORAGE_INTERNAL) {
+                       ms_validaty_change_all_items(handle, storage_id, storage_type, true);
+
+                       /* find and compare modified time */
+                       ret = __msc_compare_with_db(handle, storage_id, update_path, scan_data->msg_type, &dir_array);
+                       if (ret != MS_MEDIA_ERR_NONE) {
+                               MS_DBG_WARN("__msc_compare_with_db is falied");
+                               goto NEXT;
+                       }
+
+                       if (dir_array->len != 0) {
+                               MSC_DBG_INFO("DB UPDATING IS NEEDED");
+                               ret = _msc_db_update_partial(handle, storage_id, storage_type, dir_array);
+                       } else {
+                               MSC_DBG_INFO("THERE IS NO UPDATE");
+                       }
+               } else {
+                       if (scan_type == MS_MSG_STORAGE_ALL) {
+                               /*  Delete all data before full scanning */
+                               if (!ms_delete_all_items(handle, storage_id, storage_type)) {
+                                       MS_DBG_ERR("msc_delete_all_record fails");
+                               }
+                       } else if (scan_type == MS_MSG_STORAGE_PARTIAL) {
+                                       ms_validaty_change_all_items(handle, storage_id, storage_type, false);
+                       }
+
+                       ret = __msc_db_update(handle, storage_id, scan_data);
+               }
+
+               /*call for bundle commit*/
+               if (extract_data.msg_type != MS_MSG_STORAGE_INVALID)
+                       __msc_bacth_commit_disable(handle, TRUE, valid_status, scan_data->storage_id, ret);
+
+               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);
+
+                       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) {
+                               MS_DBG_ERR("storage thread delete count [%d]", del_count);
+                               ms_delete_invalid_items(handle, storage_id, storage_type);
+                       }
+               }
+
+               /* send notification */
+               ms_send_dir_update_noti(handle,  storage_id, update_path);
+#endif
+
+NEXT:
+
+               MS_SAFE_FREE(update_path);
+
+               msc_get_power_status(&power_off_status);
+               if (power_off_status) {
+                       MS_DBG_ERR("power off");
+                       goto _POWEROFF;
+               }
+
+               /*disconnect form media db*/
+               if (handle) ms_disconnect_db(&handle);
+
+               /*Active flush */
+               malloc_trim(0);
+
+               if(extract_data->result)
+               {
+                       msc_send_result(ret, extract_data);
+               }
+
+               MS_SAFE_FREE(extract_data);
+
+               MS_DBG_ERR("STORAGE EXTRACT END[%d]", ret);
+       }                       /*thread while*/
+
+_POWEROFF:
+       MS_SAFE_FREE(extract_data);
+       if (handle) ms_disconnect_db(&handle);
+
+       return false;
+}
+
+void msc_insert_exactor_request(int message_type, bool ins_status, const char *storage_id, const char *path, int pid)
+{
+       ms_comm_msg_s *extract_data = NULL;
+       MS_MALLOC(extract_data, sizeof(ms_comm_msg_s));
+       if (extract_data == NULL) {
+               MS_DBG_ERR("MS_MALLOC failed");
+               return;
+       }
+
+       extract_data->msg_type = message_type;
+       extract_data->pid = pid;
+       extract_data->result = ins_status;
+       extract_data->msg_size = strlen(path);
+       strncpy(extract_data->msg, path, extract_data->msg_size );
+       strncpy(extract_data->storage_id, storage_id, MS_UUID_SIZE-1);
+
+       if (message_type == MS_MSG_STORAGE_ALL || message_type == MS_MSG_STORAGE_PARTIAL || message_type == MS_MSG_STORAGE_INVALID) {
+               g_async_queue_push(storage_extract_queue, GINT_TO_POINTER(extract_data));
+               MS_DBG("insert to storage exactor queue. msg_type [%d]", ins_status);
+       } else if(message_type == MS_MSG_DIRECTORY_SCANNING || message_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
+               g_async_queue_push(folder_extract_queue, GINT_TO_POINTER(extract_data));
+               MS_DBG("insert to dir exactor queue. msg_type [%d]", ins_status);
+       } else {
+               MS_DBG_ERR("try to insert to exactor scan with msg_type [%d]", message_type);
+               MS_SAFE_FREE(extract_data);
+       }
+
+       return;
+}
+
+int msc_remove_extract_request(const ms_comm_msg_s *recv_msg)
+{
+       if(recv_msg == NULL) {
+               MS_DBG_ERR("recv_msg is null");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+       const char *storageid = recv_msg->storage_id;
+       int len = g_async_queue_length(storage_extract_queue);
+       ms_comm_msg_s *msg = NULL;
+       GAsyncQueue *temp_queue = NULL;
+
+       MS_DBG_WARN("exactor_req_mutex is LOCKED");
+       g_mutex_lock(&extract_req_mutex);
+
+       if (len == 0) {
+               MS_DBG_ERR("Request is not stacked");
+               goto END_REMOVE_REQUEST;
+       }
+
+       temp_queue = g_async_queue_new();
+       int i = 0;
+       for (; i <len; i++) {
+               /*create new queue to compare request*/
+               msg = g_async_queue_pop(storage_extract_queue);
+               if ((strcmp(msg->storage_id, storageid) == 0)) {
+                       MS_SAFE_FREE(msg);
+               } else {
+                       g_async_queue_push(temp_queue, GINT_TO_POINTER(msg));
+               }
+       }
+       g_async_queue_unref (storage_extract_queue);
+       storage_extract_queue = temp_queue;
+
+END_REMOVE_REQUEST:
+       g_mutex_unlock(&extract_req_mutex);
+       MS_DBG_WARN("exactor_req_mutex is UNLOCKED");
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_set_extract_cancel_path(const char *cancel_path)
+{
+       if (g_extract_cancel_path != NULL) {
+               MS_DBG_WARN("g_extract_cancel_path is not NULL");
+               free(g_extract_cancel_path);
+               g_extract_cancel_path = NULL;
+       }
+
+       g_extract_cancel_path = strdup(cancel_path);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_del_extract_cancel_path(void)
+{
+       if (g_extract_cancel_path != NULL) {
+               MS_DBG_WARN("g_extract_cancel_path is not NULL");
+               free(g_extract_cancel_path);
+               g_extract_cancel_path = NULL;
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_set_extract_blocked_path(const char *blocked_path)
+{
+       MS_DBG_FENTER();
+
+       g_mutex_lock(&extract_blocked_mutex);
+
+       if (g_extract_blocked_path != NULL) {
+               MS_DBG_ERR("g_extract_blocked_path is not NULL [%s]", g_extract_blocked_path);
+               free(g_extract_blocked_path);
+               g_extract_blocked_path = NULL;
+       }
+
+       g_extract_blocked_path = strdup(blocked_path);
+
+       g_mutex_unlock(&extract_blocked_mutex);
+
+       MS_DBG_FLEAVE();
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_del_extract_blocked_path(void)
+{
+       MS_DBG_FENTER();
+
+       g_mutex_lock(&extract_blocked_mutex);
+
+       if (g_extract_blocked_path != NULL) {
+               MS_DBG_ERR("g_extract_blocked_path is not NULL [%s]", g_extract_blocked_path);
+               free(g_extract_blocked_path);
+               g_extract_blocked_path = NULL;
+       }
+
+       g_mutex_unlock(&extract_blocked_mutex);
+
+       MS_DBG_FLEAVE();
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+static int __msc_check_extract_stop_status(int scan_type, ms_storage_type_t storage_type, const char *start_path)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       bool power_off_status = FALSE;
+
+       /*check poweroff status*/
+       msc_get_power_status(&power_off_status);
+       if (power_off_status) {
+               MS_DBG_ERR("Power off");
+               ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
+       }
+
+       if (scan_type == MS_MSG_DIRECTORY_SCANNING || scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
+               g_mutex_lock(&extract_req_mutex);
+               /* check cancel path */
+               if (g_extract_cancel_path != NULL) {
+                       MS_DBG_ERR("check cancel storage [%s][%s]", g_extract_cancel_path, start_path);
+                       if (strncmp(g_extract_cancel_path, start_path, strlen(g_extract_cancel_path)) == 0) {
+                               MS_DBG_ERR("Receive cancel request [%s][%s]. STOP extract!!", g_extract_cancel_path, start_path);
+                               unsigned int path_len = strlen(g_extract_cancel_path);
+
+                               if (strlen(start_path) > path_len) {
+                                       if (start_path[path_len] == '/') {
+                                               MS_DBG_ERR("start path is same as cancel path");
+                                               ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
+                                       } else {
+                                               MS_DBG_ERR("start path is not same as cancel path");
+                                       }
+                               } else {
+                                       ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
+                               }
+                       }
+
+                       MS_SAFE_FREE(g_extract_cancel_path);
+               }
+
+               g_mutex_unlock(&extract_req_mutex);
+       }
+
+       if (scan_type == MS_MSG_STORAGE_ALL || scan_type == MS_MSG_STORAGE_PARTIAL) {
+               if (g_directory_extract_processing == true) {
+                       MS_DBG_ERR("Now directory extract is start. So, storage extract is stopped.");
+                       __msc_pause_extract();
+                       MS_DBG_ERR("Now directory extract is end. So, storage extract is resumed.");
+               }
+
+               g_mutex_lock(&extract_blocked_mutex);
+               /* check block path */
+               if (g_extract_blocked_path != NULL) {
+                       MS_DBG_ERR("check blocked storage [%s][%s]", g_extract_blocked_path, start_path);
+                       if (strncmp(start_path, g_extract_blocked_path, strlen(start_path)) == 0) {
+                               MS_DBG_ERR("Receive blocked message[%s][%s]. STOP extract!!", g_extract_blocked_path, start_path);
+                               ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
+                       }
+
+                       MS_SAFE_FREE(g_extract_blocked_path);
+               }
+
+               g_mutex_unlock(&extract_blocked_mutex);
+       }
+
+       return ret;
+}
+
+static int __msc_set_storage_extract_status(ms_storage_scan_status_e status)
+{
+       int res = MS_MEDIA_ERR_NONE;
+
+       stg_extract_status = status;
+
+       return res;
+}
+
+static int __msc_get_storage_extract_status(ms_storage_scan_status_e *status)
+{
+       int res = MS_MEDIA_ERR_NONE;
+
+       *status = stg_extract_status;
+
+       return res;
+}
+
+static int __msc_resume_extract()
+{
+       g_mutex_lock (&extract_data_mutex);
+
+       g_cond_signal (&extract_data_cond);
+
+       g_mutex_unlock (&extract_data_mutex);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+static int __msc_pause_extract()
+{
+       g_mutex_lock (&extract_data_mutex);
+
+       while (g_directory_extract_processing)
+               g_cond_wait (&extract_data_cond, &extract_data_mutex);
+
+       g_mutex_unlock (&extract_data_mutex);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+static int __msc_extract_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type)
+{
+       int res = MS_MEDIA_ERR_NONE;
+       int err = 0;
+
+       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;
+                       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;
+                               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;
+                       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;
+                               MS_DBG_ERR("ms_config_set_int failed");
+                       }
+               }
+       } else {
+               if(!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS,  VCONFKEY_FILEMANAGER_DB_UPDATED)) {
+                       res = MS_MEDIA_ERR_VCONF_SET_FAIL;
+                       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;
+                               MS_DBG_ERR("ms_config_set_int failed");
+                       }
+               }
+       }
+
+       err = __msc_extract_set_power_mode(status);
+       if (err != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("__msc_tv_set_power_mode fail");
+               res = err;
+       }
+
+       return res;
+}
+
+static int __msc_extract_set_power_mode(ms_db_status_type_t status)
+{
+       int res = MS_MEDIA_ERR_NONE;
+       int err;
+
+       switch (status) {
+       case MS_DB_UPDATING:
+               err = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
+               if (err != 0)
+                       res = MS_MEDIA_ERR_INTERNAL;
+               break;
+       case MS_DB_UPDATED:
+               err = display_unlock_state(LCD_OFF, STAY_CUR_STATE);
+               if (err != 0)
+                       res = MS_MEDIA_ERR_INTERNAL;
+               break;
+       default:
+               MS_DBG_ERR("Unacceptable type : %d", status);
+               break;
+       }
+
+       return res;
+}
+
+int msc_push_extract_request(ms_extract_type_e scan_type, ms_comm_msg_s *recv_msg)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+
+       switch(scan_type) {
+               case MS_EXTRACT_STORAGE:
+                       g_async_queue_push(storage_extract_queue, GINT_TO_POINTER(recv_msg));
+                       break;
+               case MS_EXTRACT_DIRECTORY:
+                       g_async_queue_push(folder_extract_queue, GINT_TO_POINTER(recv_msg));
+                       break;
+               default:
+                       MS_DBG_ERR("invalid parameter");
+                       ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+                       break;
+       }
+
+       return ret;
+}
+
+int msc_stop_extract_thread(void)
+{
+       ms_comm_msg_s *data = NULL;
+
+       if (storage_extract_queue) {
+               /*notify to register thread*/
+               MS_MALLOC(data, sizeof(ms_comm_msg_s));
+               data->pid = POWEROFF;
+               msc_push_extract_request(MS_EXTRACT_STORAGE, data);
+       }
+
+       if (folder_extract_queue) {
+               /*notify to register thread*/
+               MS_MALLOC(data, sizeof(ms_comm_msg_s));
+               data->pid = POWEROFF;
+               msc_push_extract_request(MS_EXTRACT_DIRECTORY, data);
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_get_remain_extract_request(ms_extract_type_e scan_type, int *remain_request)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+
+       switch(scan_type) {
+               case MS_EXTRACT_STORAGE:
+                       *remain_request = g_async_queue_length(storage_extract_queue);
+                       break;
+               case MS_EXTRACT_DIRECTORY:
+                       *remain_request = g_async_queue_length(folder_extract_queue);
+                       break;
+               default:
+                       MS_DBG_ERR("invalid parameter");
+                       ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+                       break;
+       }
+
+       return ret;
+}
+
+int msc_get_dir_extract_status(bool *extract_status)
+{
+       *extract_status = g_directory_extract_processing;
+
+       return MS_MEDIA_ERR_NONE;
+}
+
diff --git a/src/scanner-v2/media-scanner-scan-v2.c b/src/scanner-v2/media-scanner-scan-v2.c
new file mode 100755 (executable)
index 0000000..bb7babe
--- /dev/null
@@ -0,0 +1,1719 @@
+/*
+ *  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.
+ *
+ */
+
+#define _GNU_SOURCE
+#include <dirent.h>     /* Defines DT_* constants */
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+
+#include <dirent.h>
+#include <errno.h>
+#include <malloc.h>
+#include <dd-display.h>
+#include <vconf.h>
+
+#include "media-util.h"
+#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-v2.h"
+#include "media-scanner-common-v2.h"
+#include "media-scanner-socket-v2.h"
+#include "media-scanner-scan-v2.h"
+#include "media-scanner-extract-v2.h"
+#define MAX_SCAN_COUNT 300
+
+GAsyncQueue *storage_queue2;
+GAsyncQueue *scan_queue2;
+GAsyncQueue *reg_queue2;
+GMutex scan_req_mutex2;
+GMutex blocked_mutex2;
+char *g_cancel_path2;
+char *g_blocked_path2;
+bool g_directory_scan_processing2;
+
+int stg_scan_status2;
+
+#ifdef FMS_PERF
+extern struct timeval g_mmc_start_time;
+extern struct timeval g_mmc_end_time;
+#endif
+
+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(int scan_type, ms_storage_type_t storage_type, const char *start_path);
+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);
+static int __msc_make_file_list(char *file_path, GArray **path_array);
+static int __msc_batch_insert(ms_msg_type_e current_msg, int pid, GArray *path_array);
+static int __msc_pop_register_request(GArray *register_array, ms_comm_msg_s **register_data);
+static int __msc_clear_file_list(GArray *path_array);
+static int __msc_check_scan_ignore(char * path);
+static bool __msc_is_valid_path(const char *path);
+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, const char *path, int result);
+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);
+static int __msc_dir_scan(void **handle, const char *storage_id, const char*start_path, ms_storage_type_t storage_type, int scan_type);
+static bool __msc_storage_mount_status(const char* start_path);
+
+static int __msc_set_power_mode(ms_db_status_type_t status)
+{
+       int res = MS_MEDIA_ERR_NONE;
+       int err;
+
+       switch (status) {
+       case MS_DB_UPDATING:
+               err = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
+               if (err != 0)
+                       res = MS_MEDIA_ERR_INTERNAL;
+               break;
+       case MS_DB_UPDATED:
+               err = display_unlock_state(LCD_OFF, STAY_CUR_STATE);
+               if (err != 0)
+                       res = MS_MEDIA_ERR_INTERNAL;
+               break;
+       default:
+               MS_DBG_ERR("Unacceptable type : %d", status);
+               break;
+       }
+
+       return res;
+}
+
+static int __msc_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type)
+{
+       int res = MS_MEDIA_ERR_NONE;
+       int err = 0;
+
+       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;
+                       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;
+                               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;
+                       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;
+                               MS_DBG_ERR("ms_config_set_int failed");
+                       }
+               }
+       } else {
+               if(!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS,  VCONFKEY_FILEMANAGER_DB_UPDATED)) {
+                       res = MS_MEDIA_ERR_VCONF_SET_FAIL;
+                       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;
+                               MS_DBG_ERR("ms_config_set_int failed");
+                       }
+               }
+       }
+
+       err = __msc_set_power_mode(status);
+       if (err != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("__msc_tv_set_power_mode fail");
+               res = err;
+       }
+
+       return res;
+}
+
+static int __msc_check_scan_ignore(char * path)
+{
+       int fd = -1;
+       int exist = -1;
+       const char *ignore_path = "/.scan_ignore";
+       char *check_ignore_file = NULL;
+       int ret = MS_MEDIA_ERR_NONE;
+
+       if(strstr(path, "/."))
+       {
+               MS_DBG_ERR("hidden path");
+               ret = MS_MEDIA_ERR_INVALID_PATH;
+               goto ERROR;
+       }
+
+       fd = open(path, O_RDONLY | O_DIRECTORY);
+       if (fd == -1) {
+               MS_DBG_ERR("%s folder opendir fails", path);
+               ret = MS_MEDIA_ERR_INVALID_PATH;
+
+               if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
+                       if (errno == ENOENT) {
+                               /*if the directory does not exist, check the device is unmounted*/
+                               if(!__msc_storage_mount_status(path)) {
+                                       MS_DBG_ERR("Device is unmounted[%s]", path);
+                                       ret = MS_MEDIA_ERR_USB_UNMOUNTED;
+                                       goto ERROR;
+                               }
+                       }
+               }
+
+               struct stat folder_st;
+               if(stat(path, &folder_st) == 0) {
+                       MS_DBG_ERR("DEV[%ld] INODE[%lld] UID[%ld] GID[%ld] MODE[%lo] PATH[%s]", (long)folder_st.st_dev, (long long)folder_st.st_ino,
+                               (long)folder_st.st_uid, (long)folder_st.st_gid, (unsigned long) folder_st.st_mode, path);
+               } else {
+                       MS_DBG_ERR("%s folder stat fails", path);
+               }
+
+               goto ERROR;
+       } else {
+               /* check the file exits actually */
+               int path_len = 0;
+
+               path_len = strlen(path) + strlen(ignore_path) + 1;
+               check_ignore_file = malloc(path_len);
+               if(check_ignore_file != NULL) {
+                       memset(check_ignore_file, 0x0, path_len);
+                       snprintf(check_ignore_file, path_len, "%s%s", path, ignore_path);
+
+                       exist = open(check_ignore_file, O_RDONLY);
+                       if(exist >=  0) {
+                               MS_DBG_ERR("scan_ignore exists [%s]", check_ignore_file);
+                               ret = MS_MEDIA_ERR_INVALID_PATH;
+                       }
+
+                       MS_SAFE_FREE(check_ignore_file);
+               } else {
+                       MS_DBG_ERR("malloc failed");
+                       ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
+               }
+       }
+
+ERROR:
+
+       if(fd != -1) {
+               close(fd);
+               fd = -1;
+       }
+
+       if (exist >= 0) close(exist);
+
+       return ret;
+}
+
+GCond data_cond2;   /* Must be initialized somewhere */
+GMutex data_mutex2; /* Must be initialized somewhere */
+gpointer current_data2 = NULL;
+
+int msc_init_scan_thread()
+{
+       g_mutex_init(&data_mutex2);
+       g_mutex_init(&blocked_mutex2);
+
+       g_cond_init(&data_cond2);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_deinit_scan_thread()
+{
+       g_mutex_clear(&data_mutex2);
+       g_mutex_clear(&blocked_mutex2);
+
+       g_cond_clear(&data_cond2);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+static int __msc_resume_scan()
+{
+       g_mutex_lock (&data_mutex2);
+
+       g_cond_signal (&data_cond2);
+
+       g_mutex_unlock (&data_mutex2);
+
+       return MS_MEDIA_ERR_NONE;
+}
+static int __msc_pause_scan()
+{
+       g_mutex_lock (&data_mutex2);
+
+       while (g_directory_scan_processing2)
+               g_cond_wait (&data_cond2, &data_mutex2);
+
+       g_mutex_unlock (&data_mutex2);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+
+static int __msc_check_stop_status(int scan_type, ms_storage_type_t storage_type, const char *start_path)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       bool power_off_status = FALSE;
+
+       /*check poweroff status*/
+       msc_get_power_status(&power_off_status);
+       if (power_off_status) {
+               MS_DBG_ERR("Power off");
+               ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
+       }
+
+       if (scan_type == MS_MSG_DIRECTORY_SCANNING ||scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
+               g_mutex_lock(&scan_req_mutex2);
+               /* check cancel path */
+               if (g_cancel_path2 != NULL) {
+                       MS_DBG_ERR("check blocked storage [%s][%s]", g_cancel_path2, start_path);
+                       if (strncmp(g_cancel_path2, start_path, strlen(g_cancel_path2)) == 0) {
+                               MS_DBG_ERR("Receive cancel request [%s][%s]. STOP scan!!",
+                               g_cancel_path2, start_path);
+                               unsigned int path_len = strlen(g_cancel_path2);
+
+                               if (strlen(start_path) > path_len) {
+                                       if (start_path[path_len] == '/') {
+                                               MS_DBG_ERR("start path is same as cancel path");
+                                               ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
+                                       } else {
+                                               MS_DBG_ERR("start path is not same as cancel path");
+                                       }
+                               } else {
+                                       ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
+                               }
+                       }
+
+                       MS_SAFE_FREE(g_cancel_path2);
+               }
+
+               g_mutex_unlock(&scan_req_mutex2);
+       }
+
+       if (scan_type == MS_MSG_STORAGE_ALL || scan_type == MS_MSG_STORAGE_PARTIAL) {
+               if (g_directory_scan_processing2 == true) {
+                       MS_DBG_ERR("Now directory scanning is start. So, storage scannig is stopped.");
+                       __msc_pause_scan();
+                       MS_DBG_ERR("Now directory scanning is end. So, storage scannig is resumed.");
+               }
+
+               g_mutex_lock(&blocked_mutex2);
+               /* check cancel path */
+               if (g_blocked_path2!= NULL) {
+                       MS_DBG_ERR("check blocked storage [%s][%s]", g_blocked_path2, start_path);
+                       if (strncmp(start_path, g_blocked_path2, strlen(start_path)) == 0) {
+                               MS_DBG_ERR("Receive blocked message[%s][%s]. STOP scan!!",
+                               g_blocked_path2, start_path);
+                               ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
+                       }
+
+                       MS_SAFE_FREE(g_blocked_path2);
+               }
+
+               g_mutex_unlock(&blocked_mutex2);
+       }
+
+       return ret;
+}
+
+static void __msc_check_dir_path(char *dir_path)
+{
+       /* need implementation */
+       /* if dir_path is not NULL terminated, this function will occure crash */
+       int len = strlen(dir_path);
+
+       if (dir_path[len -1] == '/')
+               dir_path[len -1] = '\0';
+}
+
+struct linux_dirent {
+       int           d_ino;
+       long           d_off;
+       unsigned short d_reclen;
+       char           d_name[];
+};
+
+#define BUF_SIZE 1024
+
+static int __msc_dir_scan(void **handle, const char *storage_id, const char*start_path, ms_storage_type_t storage_type, int scan_type)
+{
+       GArray *dir_array = NULL;
+       int ret = MS_MEDIA_ERR_NONE;
+       char *new_path = NULL;
+       char *current_path = NULL;
+       char path[MS_FILE_PATH_LEN_MAX] = {0, };
+       int (*scan_function)(void **, const char*, const char*) = NULL;
+       bool is_recursive = true;
+       char *new_start_path = NULL;
+
+       int fd = -1;
+       int nread = 0;
+       char buf[BUF_SIZE] = {0, };
+       struct linux_dirent *d;
+       int bpos = 0;
+       int scan_count = 0;
+
+       char d_type;
+       const char *trash = "$RECYCLE.BIN";
+
+       MS_DBG_ERR("storage id [%s] start path [%s]", storage_id, start_path);
+
+       /* make new array for storing directory */
+       dir_array = g_array_new (FALSE, FALSE, sizeof (char*));
+
+       if (dir_array == NULL) {
+               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("strdup failed");
+               g_array_free(dir_array, FALSE);
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
+
+       MS_DBG_ERR("new start path [%s]", new_start_path);
+       g_array_append_val (dir_array, start_path);
+       if(ms_insert_folder(handle, storage_id, new_start_path) != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("insert folder failed");
+       }
+
+       scan_function = (scan_type == MS_MSG_STORAGE_ALL) ? ms_scan_item_batch : ms_scan_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) != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("set_folder_validity failed [%d] ", scan_type);
+               }
+       }
+
+       /*start db update. the number of element in the array , db update is complete.*/
+       while (dir_array->len != 0) {
+               /*check poweroff status*/
+               ret = __msc_check_stop_status(scan_type, storage_type, new_start_path);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       goto STOP_SCAN;
+               }
+               /* get the current path from directory array */
+               current_path = g_array_index(dir_array , char*, 0);
+               g_array_remove_index (dir_array, 0);
+
+               ret = __msc_check_scan_ignore(current_path);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("%s is ignore", current_path);
+                       MS_SAFE_FREE(current_path);
+                       if (ret == MS_MEDIA_ERR_USB_UNMOUNTED) {
+                               goto STOP_SCAN;
+                       } else {
+                               ret = MS_MEDIA_ERR_NONE;
+                       }
+
+                       continue;
+               }
+
+               ms_insert_folder_start(handle);
+
+               fd = open(current_path, O_RDONLY | O_DIRECTORY);
+               if (fd == -1) {
+                       MS_DBG_STRERROR("open fails");
+                       continue;
+               }
+
+               for ( ; ; ) {
+                       nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
+                       if (nread == -1) {
+                               MS_DBG_STRERROR("getdents");
+                               break;
+                       }
+
+                       /*check poweroff status*/
+                       ret = __msc_check_stop_status(scan_type, storage_type, new_start_path);
+                       if (ret != MS_MEDIA_ERR_NONE) {
+                               goto STOP_SCAN;
+                       }
+
+                       if (nread == 0)
+                               break;
+
+                       for (bpos = 0; bpos < nread;) {
+                               /*check poweroff status*/
+                               ret = __msc_check_stop_status(scan_type, storage_type, new_start_path);
+                               if (ret != MS_MEDIA_ERR_NONE) {
+                                       goto STOP_SCAN;
+                               }
+
+                               d = (struct linux_dirent *) (buf + bpos);
+                               d_type = *(buf + bpos + d->d_reclen - 1);
+
+                               if (d->d_name[0] == '.') {
+                                       bpos += d->d_reclen;
+                                       continue;
+                               }
+
+                               if (strcmp(d->d_name, trash) == 0) {
+                                       MS_DBG_ERR("trash directory");
+                                       bpos += d->d_reclen;
+                                       continue;
+                               }
+
+                               if (ms_strappend(path, sizeof(path), "%s/%s", current_path, d->d_name) != MS_MEDIA_ERR_NONE) {
+                                       MS_DBG_ERR("ms_strappend failed");
+                                       bpos += d->d_reclen;
+                                       continue;
+                               }
+
+                               if (d_type == DT_REG) {
+                                       /* insert into media DB */
+                                       if (scan_function(handle,storage_id, path) != MS_MEDIA_ERR_NONE) {
+                                               MS_DBG_ERR("failed to update db : %d\n", scan_type);
+                                               bpos += d->d_reclen;
+                                               continue;
+                                       }
+                                       else {
+                                               ++scan_count;
+                                               //MSC_DBG_ERR("insert count %d", nScanCount);
+                                               if(scan_count/MAX_SCAN_COUNT>0) {
+                                                       scan_count = 0;
+                                                       MS_DBG_ERR("storage_id = [%s]", storage_id);
+                                                       msc_insert_exactor_request(scan_type, FALSE, storage_id, current_path, 0);
+                                               }
+                                       }
+                               } else if (d_type == DT_DIR) {
+                                       if  (scan_type != MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
+                                               /* this request is recursive scanning */
+                                               /* add new directory to dir_array */
+                                               new_path = strdup(path);
+                                               g_array_append_val (dir_array, new_path);
+
+                                               if(ms_insert_folder(handle, storage_id, new_path) != MS_MEDIA_ERR_NONE) {
+                                                       MS_DBG_ERR("insert folder failed");
+                                               }
+                                       } else {
+                                               /* this request is non-recursive scanning */
+                                               /* don't add new directory to dir_array */
+                                               if(ms_insert_folder(handle, storage_id, path) != MS_MEDIA_ERR_NONE) {
+                                                       MS_DBG_ERR("insert folder failed");
+                                                       bpos += d->d_reclen;
+                                                       continue;
+                                               }
+                                       }
+                               }
+
+                               bpos += d->d_reclen;
+                       }
+               }
+
+               ms_insert_folder_end(handle);
+//             msc_insert_exactor_request(scan_type, FALSE, storage_id, current_path, 0);
+//             scan_count = 0;
+
+               if(fd != -1) {
+                       close(fd);
+                       fd = -1;
+               }
+
+               MS_SAFE_FREE(current_path);
+       }
+
+       /*remove invalid folder in folder table.*/
+       if (scan_type == MS_MSG_STORAGE_ALL || scan_type == MS_MSG_STORAGE_PARTIAL) {
+               if (__msc_storage_mount_status(new_start_path)) {
+                       if(ms_delete_invalid_folder(handle, storage_id) != 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(fd != -1) {
+               close(fd);
+               fd = -1;
+       }
+
+       MS_SAFE_FREE(new_start_path);
+
+       __msc_clear_file_list(dir_array);
+
+       if (ret != MS_MEDIA_ERR_NONE) MS_DBG_INFO("ret : %d", ret);
+
+       return ret;
+}
+
+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;
+       char *start_path = NULL;
+       ms_storage_type_t storage_type;
+
+       storage_type = ms_get_storage_type_by_full(scan_data->msg);
+       scan_type = scan_data->msg_type;
+
+       /*if scan type is not MS_SCAN_NONE, check data in db. */
+       if (scan_type != MS_MSG_STORAGE_INVALID) {
+               MS_DBG_ERR("INSERT");
+               start_path = strndup(scan_data->msg, scan_data->msg_size);
+               scan_type = scan_data->msg_type;
+
+               err = __msc_dir_scan(handle, storage_id, start_path, storage_type, scan_type);
+               if (err != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("error : %d", err);
+               }
+       } else if ( scan_type == MS_MSG_STORAGE_INVALID) {
+               MS_DBG_ERR("INVALID");
+               /*In this case, update just validation record*/
+               /*update just valid type*/
+               err = ms_validaty_change_all_items(handle, storage_id, storage_type, false);
+               if (err != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("error : %d", err);
+               }
+
+               /* folder validity set 0 under the start_path in folder table*/
+               if(ms_set_folder_validity(handle, storage_id, scan_data->msg, MS_INVALID, TRUE) != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("set_folder_validity failed");
+               }
+               msc_remove_extract_request(scan_data);
+       }
+
+       sync();
+
+       return err;
+}
+static int __msc_pop_new_request(ms_comm_msg_s **scan_data)
+{
+       *scan_data = g_async_queue_pop(scan_queue2);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+gboolean msc_directory_scan_thread(void *data)
+{
+       ms_comm_msg_s *scan_data = NULL;
+       int err;
+       int ret;
+       void **handle = NULL;
+       int scan_type;
+       char *noti_path = NULL;
+       char *storage_id = NULL;
+       bool power_off_status = FALSE;
+
+       while (1) {
+               __msc_pop_new_request(&scan_data);
+
+               if (scan_data->pid == POWEROFF) {
+                       MS_DBG_ERR("power off");
+                       goto _POWEROFF;
+               }
+
+               MS_DBG_ERR("DIRECTORY SCAN START [%s, %d]", scan_data->msg, scan_data->msg_type);
+
+               g_directory_scan_processing2 = true;
+
+               /*connect to media db, if conneting is failed, db updating is stopped*/
+               err = ms_connect_db(&handle);
+               if (err != MS_MEDIA_ERR_NONE)
+                       continue;
+
+               scan_type = scan_data->msg_type;
+
+               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_type [%d]", scan_data->msg, storage_id, scan_type);
+
+               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) {
+                       MS_DBG_ERR("Invalid request");
+                       ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+                       goto NEXT;
+               }
+
+               __msc_check_dir_path(scan_data->msg);
+
+               /*change validity before scanning*/
+               if (scan_type == MS_MSG_DIRECTORY_SCANNING) {
+                       err = ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_INVALID, MS_RECURSIVE);
+               } else {
+
+                       err = ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_INVALID, MS_NON_RECURSIVE);
+               }
+
+               if (err != MS_MEDIA_ERR_NONE)
+                       MS_DBG_ERR("error : %d", err);
+
+               /*call for bundle commit*/
+               __msc_bacth_commit_enable(handle, TRUE, TRUE, MS_NOTI_SWITCH_OFF, 0);
+
+               /*insert data into media db */
+               ret = __msc_db_update(handle, storage_id, scan_data);
+               msc_del_cancel_path();
+
+               /*call for bundle commit*/
+               __msc_bacth_commit_disable(handle, TRUE, TRUE, scan_data->msg, ret);
+
+               MS_DBG_ERR("storage_id = [%s], scan_data->storage_id = [%s]", storage_id, scan_data->storage_id);
+               msc_insert_exactor_request(scan_type, TRUE, scan_data->storage_id, scan_data->msg, scan_data->pid);
+
+               if (ret == MS_MEDIA_ERR_NONE) {
+                       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);
+
+                       ms_count_delete_items_in_folder(handle, storage_id, noti_path, &count);
+
+                       MS_DBG_ERR("delete count %d", count);
+                       MS_DBG_ERR("insert count %d", insert_count);
+
+                       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);
+
+                       if ( !(count == 0 && insert_count == 0)) {
+                               ms_send_dir_update_noti(handle, storage_id, noti_path, NULL, MS_ITEM_UPDATE, scan_data->pid);
+                       }
+                       MS_SAFE_FREE(noti_path);
+               }
+
+               ms_reset_insert_count();
+
+               msc_get_power_status(&power_off_status);
+               if (power_off_status) {
+                       MS_DBG_ERR("power off");
+                       goto _POWEROFF;
+               }
+
+NEXT:
+               /*Active flush */
+               malloc_trim(0);
+
+//             msc_send_result(ret, scan_data);
+
+               MS_SAFE_FREE(scan_data);
+               MS_SAFE_FREE(storage_id);
+
+               g_directory_scan_processing2 = false;
+
+               MS_DBG_ERR("DIRECTORY SCAN END [%d]", ret);
+
+               ms_storage_scan_status_e storage_scan_status = MS_STORAGE_SCAN_NONE;
+               ret = __msc_get_storage_scan_status(&storage_scan_status);
+               if (ret == MS_MEDIA_ERR_NONE) {
+                       /*get storage list and scan status from media db*/
+                       if (storage_scan_status != MS_STORAGE_SCAN_DONE) {
+                               __msc_resume_scan();
+                               MS_DBG_ERR("RESUME OK");
+                       }
+               }
+
+               /*disconnect form media db*/
+               if (handle) ms_disconnect_db(&handle);
+       }                       /*thread while*/
+
+_POWEROFF:
+       MS_SAFE_FREE(scan_data);
+       if (handle) ms_disconnect_db(&handle);
+
+       return false;
+}
+
+/* this thread process only the request of media-server */
+static int _check_folder_from_list(char *folder_path, GArray *dir_array)
+{
+       int i;
+       int array_len = dir_array->len;
+       ms_dir_info_s* dir_info = NULL;
+       struct stat buf;
+       time_t mtime;
+       bool find_flag = false;
+
+       if(stat(folder_path, &buf) == 0) {
+               mtime = buf.st_mtime;
+       } else {
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       for (i = 0; i < array_len; 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) {
+                               g_array_remove_index (dir_array, i);
+                               MS_SAFE_FREE(dir_info->dir_path);
+                               MS_SAFE_FREE(dir_info);
+                       }
+                       find_flag = true;
+                       break;
+               }
+       }
+
+       /* this folder does not exist in media DB, so this folder has to insert to DB */
+       if (find_flag == false) {
+               dir_info = NULL;
+               dir_info = malloc(sizeof(ms_dir_info_s));
+               if (dir_info == NULL) {
+                       MS_DBG_ERR("MALLOC failed");
+                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
+               }
+               dir_info->dir_path = strdup(folder_path);
+               dir_info->modified_time = -1;
+               g_array_append_val(dir_array, dir_info);
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+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;
+       struct dirent entry;
+       struct dirent *result = NULL;
+       int ret = MS_MEDIA_ERR_NONE;
+       char *new_path = NULL;
+       char *current_path = NULL;
+       char path[MS_FILE_PATH_LEN_MAX] = { 0 };
+       char * start_path = strdup(update_path);
+
+       MS_DBG_FENTER();
+
+       /*get directories list from media db*/
+       ret = ms_get_folder_list(handle, storage_id, start_path, dir_array);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("msc_tv_get_folder_list is failed", ret);
+               return ret;
+       }
+
+       /* make new array for storing directory */
+       read_dir_array = g_array_new (FALSE, FALSE, sizeof (char*));
+       if (read_dir_array == NULL){
+               MS_DBG_ERR("g_array_new failed");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
+       /* add first direcotiry to directory array */
+       g_array_append_val (read_dir_array, start_path);
+
+       /*start db update. the number of element in the array , db update is complete.*/
+       while (read_dir_array->len != 0) {
+               /* get the current path from directory array */
+               current_path = g_array_index(read_dir_array , char*, 0);
+               g_array_remove_index (read_dir_array, 0);
+//             MSC_DBG_ERR("%s", current_path);
+
+               if (__msc_check_scan_ignore(current_path) != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("%s is ignore", current_path);
+                       MS_SAFE_FREE(current_path);
+                       continue;
+               }
+
+               dp = opendir(current_path);
+               if (dp != NULL) {
+                       /* find and compare modified time */
+                       _check_folder_from_list(current_path, *dir_array);
+
+                       while (!readdir_r(dp, &entry, &result)) {
+                               if (result == NULL)
+                                       break;
+
+                               if (entry.d_name[0] == '.') {
+                                       continue;
+                               }
+
+                                if (entry.d_type & DT_DIR) {
+                                        if (ms_strappend(path, sizeof(path), "%s/%s", current_path, entry.d_name) != MS_MEDIA_ERR_NONE) {
+                                               MS_DBG_ERR("ms_strappend failed");
+                                               continue;
+                                       }
+                                       /* add new directory to dir_array */
+                                       new_path = strdup(path);
+                                       g_array_append_val (read_dir_array, new_path);
+                               }
+                       }
+               } else {
+                       MS_DBG_ERR("%s folder opendir fails", current_path);
+               }
+               if (dp) closedir(dp);
+               dp = NULL;
+               MS_SAFE_FREE(current_path);
+       }               /*db update while */
+
+       __msc_clear_file_list(read_dir_array);
+
+       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, const char *storage_id, ms_storage_type_t storage_type, GArray *dir_array)
+{
+       unsigned int i;
+       int err = MS_MEDIA_ERR_NONE;
+       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, ms_dir_info_s*, i);
+               update_path = strdup(dir_info->dir_path);
+               if (update_path == NULL) {
+                       MS_DBG_ERR("malloc failed");
+                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
+               }
+
+               if (dir_info->modified_time != -1) {
+                       err = ms_set_folder_item_validity(handle, storage_id, update_path, MS_INVALID, MS_NON_RECURSIVE);
+                       if (err != MS_MEDIA_ERR_NONE) {
+                               MS_DBG_SLOG("update_path : %s, %d", update_path, dir_info->modified_time);
+                               MS_DBG_ERR("error : %d", err);
+                       }
+               }
+
+               __msc_dir_scan(handle, storage_id, update_path, storage_type, MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE);
+       }
+
+       /*delete all node*/
+       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);
+               MS_SAFE_FREE(data);
+       }
+       g_array_free(dir_array, FALSE);
+       dir_array = NULL;
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+gboolean msc_storage_scan_thread(void *data)
+{
+       ms_comm_msg_s *scan_data = NULL;
+       int ret;
+       int err;
+       void **handle = NULL;
+       ms_storage_type_t storage_type = MS_STORAGE_INTERNAL;
+       int scan_type;
+       bool valid_status = TRUE;
+       char *update_path = NULL;
+       GArray *dir_array = NULL;
+       bool power_off_status = FALSE;
+
+       while (1) {
+               __msc_set_storage_scan_status(MS_STORAGE_SCAN_PREPARE);
+               scan_data = g_async_queue_pop(storage_queue2);
+               if (scan_data->pid == POWEROFF) {
+                       MS_DBG_ERR("power off");
+                       goto _POWEROFF;
+               }
+
+               __msc_set_storage_scan_status(MS_STORAGE_SCAN_PROCESSING);
+               MS_DBG_ERR("STORAGE SCAN START [%s]", scan_data->msg);
+
+               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) {
+                       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 = ms_connect_db(&handle);
+               if (err != MS_MEDIA_ERR_NONE)
+                       continue;
+
+               storage_type = ms_get_storage_type_by_full(scan_data->msg);
+               update_path = strndup(scan_data->msg, scan_data->msg_size);
+
+               ms_set_storage_scan_status(handle, scan_data->storage_id, MEDIA_SCAN_PROCESSING);
+
+               /*start db updating */
+               __msc_set_db_status(MS_DB_UPDATING, storage_type);
+
+               valid_status = (scan_type == MS_MSG_STORAGE_PARTIAL) ? TRUE : FALSE;
+
+               if (scan_type != MS_MSG_STORAGE_INVALID)
+                       __msc_bacth_commit_enable(handle, TRUE, valid_status, MS_NOTI_SWITCH_OFF, 0);
+
+               if (scan_type == MS_MSG_STORAGE_PARTIAL && storage_type == MS_STORAGE_INTERNAL) {
+                       ms_validaty_change_all_items(handle, scan_data->storage_id, storage_type, true);
+
+                       /* find and compare modified time */
+                       ret = __msc_compare_with_db(handle, scan_data->storage_id, update_path, scan_data->msg_type, &dir_array);
+                       if (ret != MS_MEDIA_ERR_NONE) {
+                               MS_DBG_WARN("__msc_compare_with_db is falied");
+                               goto NEXT;
+                       }
+
+                       if (dir_array->len != 0) {
+                               MS_DBG_INFO("DB UPDATING IS NEEDED");
+                               ret = _msc_db_update_partial(handle, scan_data->storage_id, storage_type, dir_array);
+                       } else {
+                               MS_DBG_INFO("THERE IS NO UPDATE");
+                       }
+               } else {
+                       if (scan_type == MS_MSG_STORAGE_ALL) {
+                               /*  Delete all data before full scanning */
+                               if (!ms_delete_all_items(handle, scan_data->storage_id, storage_type)) {
+                                       MS_DBG_ERR("msc_delete_all_record fails");
+                               }
+                       } else if (scan_type == MS_MSG_STORAGE_PARTIAL) {
+                                       ms_validaty_change_all_items(handle, scan_data->storage_id, storage_type, false);
+                       }
+
+                       ret = __msc_db_update(handle, scan_data->storage_id, scan_data);
+               }
+
+               msc_del_blocked_path();
+
+               /*call for bundle commit*/
+               if (scan_type != MS_MSG_STORAGE_INVALID) {
+                       __msc_bacth_commit_disable(handle, TRUE, valid_status, scan_data->msg, ret);
+               }
+
+               MS_DBG_ERR("scan_data->storage_id = [%s]", scan_data->storage_id);
+
+               msc_insert_exactor_request(scan_type, TRUE, scan_data->storage_id, scan_data->msg, scan_data->pid);
+
+               MS_DBG_ERR("PAUSE");
+               __msc_pause_scan();
+               MS_DBG_ERR("RESUME");
+               /* move to msc_tv_storage_extract_thread */
+               if (0) {
+                       int del_count = 0;
+
+                       /*check delete count*/
+                       MS_DBG_INFO("update path : %s", update_path);
+
+                       ms_count_delete_items_in_folder(handle, scan_data->storage_id, update_path, &del_count);
+
+                       /*if there is no delete content, do not call delete API*/
+                       if (del_count != 0) {
+                               MS_DBG_ERR("storage thread delete count [%d]", del_count);
+                               ms_delete_invalid_items(handle, scan_data->storage_id, storage_type);
+                       }
+               }
+
+               /* send notification */
+               ms_send_dir_update_noti(handle,  scan_data->storage_id, update_path, NULL, MS_ITEM_UPDATE, scan_data->pid);
+
+               if (ret == MS_MEDIA_ERR_SCANNER_FORCE_STOP) {
+                       ms_set_storage_scan_status(handle, scan_data->storage_id, MEDIA_SCAN_STOP);
+                       __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, scan_data->storage_id, MEDIA_SCAN_COMPLETE);
+                       __msc_set_storage_scan_status(MS_STORAGE_SCAN_DONE);
+                       /*set vconf key mmc loading for indicator */
+                       __msc_set_db_status(MS_DB_UPDATED, storage_type);
+               }
+
+NEXT:
+
+               MS_SAFE_FREE(update_path);
+
+               msc_get_power_status(&power_off_status);
+               if (power_off_status) {
+                       MS_DBG_ERR("power off");
+                       goto _POWEROFF;
+               }
+
+               /*disconnect form media db*/
+               if (handle) ms_disconnect_db(&handle);
+
+               /*Active flush */
+               malloc_trim(0);
+
+//             msc_send_result(ret, scan_data);
+
+               MS_SAFE_FREE(scan_data);
+
+               MS_DBG_ERR("STORAGE SCAN END[%d]", ret);
+       }                       /*thread while*/
+
+_POWEROFF:
+       MS_SAFE_FREE(scan_data);
+       if (handle) ms_disconnect_db(&handle);
+
+       return false;
+}
+
+static void __msc_insert_register_request(GArray *register_array, ms_comm_msg_s *insert_data)
+{
+       MS_DBG_SLOG("path : %s", insert_data->msg);
+
+       if (insert_data->pid == POWEROFF) {
+               g_array_prepend_val(register_array, insert_data);
+       } else if (insert_data->msg_type == MS_MSG_BURSTSHOT_INSERT) {
+               g_array_prepend_val(register_array, insert_data);
+       } else {
+               g_array_append_val(register_array, insert_data);
+       }
+}
+
+static bool __msc_is_valid_path(const char *path)
+{
+       if (path == NULL)
+               return false;
+
+       if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
+               return true;
+       } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
+               return true;
+       } else if (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0) {
+               return true;
+       } else
+               return false;
+}
+
+static int __msc_check_file_path(const char *file_path)
+{
+       int exist;
+       struct stat file_st;
+
+       /* check location of file */
+       /* file must exists under "/opt/usr/media" or "/opt/storage/sdcard" */
+       if(!__msc_is_valid_path(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) {
+               MS_DBG_ERR("[%s]open files");
+               return MS_MEDIA_ERR_INVALID_PATH;
+       }
+       close(exist);
+
+       /* check type of the path */
+       /* It must be a regular file */
+       memset(&file_st, 0, sizeof(struct stat));
+       if(stat(file_path, &file_st) == 0) {
+               if(!S_ISREG(file_st.st_mode)) {
+                       /* In this case, it is not a regula file */
+                       MS_DBG_ERR("this path is not a file");
+                       return MS_MEDIA_ERR_INVALID_PATH;
+               }
+       } else {
+               MS_DBG_STRERROR("stat failed");
+               return MS_MEDIA_ERR_INVALID_PATH;
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+static int __msc_clear_file_list(GArray *path_array)
+{
+       if (path_array) {
+               while(path_array->len != 0) {
+                       char *data = NULL;
+                       data = g_array_index(path_array , char*, 0);
+                       g_array_remove_index (path_array, 0);
+                       MS_SAFE_FREE(data);
+               }
+               g_array_free(path_array, FALSE);
+               path_array = NULL;
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+static int __msc_check_ignore_dir(const char *full_path)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       char *dir_path = NULL;
+       char *leaf_path = NULL;
+
+       ret = __msc_check_file_path(full_path);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               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) {
+               MS_DBG_ERR("getting directory path is failed : %s", full_path);
+               MS_SAFE_FREE(dir_path);
+               return MS_MEDIA_ERR_INVALID_PATH;
+       }
+
+       while(1) {
+               if(__msc_check_scan_ignore(dir_path) != MS_MEDIA_ERR_NONE) {
+                       ret = MS_MEDIA_ERR_INVALID_PATH;
+                       break;
+               }
+
+               /*If root path, Stop Scanning*/
+               if(strcmp(dir_path, MEDIA_ROOT_PATH_INTERNAL) == 0)
+                       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 {
+                       MS_DBG_ERR("Fail to find leaf path");
+                       ret = MS_MEDIA_ERR_INVALID_PATH;
+                       break;
+               }
+       }
+
+       MS_SAFE_FREE(dir_path);
+
+       return ret;
+}
+
+static int __msc_make_file_list(char *file_path, GArray **path_array)
+{
+       FILE *fp = NULL;
+       char buf[MS_FILE_PATH_LEN_MAX] = {0,};
+       char *path = NULL;
+       int length;
+       int res = MS_MEDIA_ERR_NONE;
+       int ret = MS_MEDIA_ERR_NONE;
+
+       /* load the file list from file */
+       fp = fopen(file_path, "rt");
+       if (fp == NULL) {
+               MS_DBG_STRERROR("fopen failed");
+               res = MS_MEDIA_ERR_FILE_OPEN_FAIL;
+               goto FREE_RESOURCE;
+       }
+
+       memset(buf, 0x0, MS_FILE_PATH_LEN_MAX);
+       /* This is an array for storing the path of insert datas*/
+       *path_array = g_array_new (FALSE, FALSE, sizeof (char *));
+       if (*path_array == NULL) {
+               MS_DBG_ERR("g_array_new failed");
+               res = MS_MEDIA_ERR_OUT_OF_MEMORY;
+               goto FREE_RESOURCE;
+       }
+
+       /* read registering file path from stored file */
+       while(fgets(buf, MS_FILE_PATH_LEN_MAX, fp) != NULL) {
+               length = strlen(buf); /*the return value of function, strlen(), includes "\n" */
+               path = strndup(buf, length - 1); /*copying except "\n" and strndup fuction adds "\0" at the end of the copying string */
+
+               /* check valid path */
+               ret = __msc_check_ignore_dir(path);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       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) {
+                       MS_DBG_ERR("g_array_append_val failed");
+                       res = MS_MEDIA_ERR_OUT_OF_MEMORY;
+                       goto FREE_RESOURCE;
+               }
+       }
+
+       if(fp) fclose(fp);
+       fp = NULL;
+
+       return MS_MEDIA_ERR_NONE;
+
+FREE_RESOURCE:
+
+       __msc_clear_file_list(*path_array);
+
+       if(fp) fclose(fp);
+       fp = NULL;
+
+       return res;
+}
+
+static int __msc_batch_insert(ms_msg_type_e current_msg, int pid, GArray *path_array)
+{
+       int err;
+       unsigned int i;
+       void **handle = NULL;
+       char *insert_path = NULL;
+       int (*insert_function)(void **, const char*, const char*) = NULL;
+       char storage_id[MS_UUID_SIZE] = {0,};
+       bool power_off_status = FALSE;
+
+       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 = ms_connect_db(&handle);
+       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_SWITCH_ON, pid);
+
+       MS_DBG_ERR("BULK REGISTER START");
+
+       /* 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, storage_id, insert_path);
+
+               msc_get_power_status(&power_off_status);
+               if (power_off_status) {
+                       MS_DBG_ERR("power off");
+                       /*call for bundle commit*/
+                       break;
+               }
+       }
+
+       /*call for bundle commit*/
+       __msc_bacth_commit_disable(handle, TRUE, FALSE, NULL, MS_MEDIA_ERR_NONE);
+
+       /*disconnect form media db*/
+       if (handle) ms_disconnect_db(&handle);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+static int __msc_pop_register_request(GArray *register_array, ms_comm_msg_s **register_data)
+{
+       int remain_request;
+       ms_comm_msg_s *insert_data = NULL;
+
+       while (1) {
+               remain_request  = g_async_queue_length(reg_queue2);
+
+               /*updating requests remain*/
+               if (register_array->len != 0 && remain_request == 0) {
+                       *register_data = g_array_index(register_array, ms_comm_msg_s*, 0);
+                       g_array_remove_index (register_array, 0);
+                       break;
+               } else if (remain_request != 0) {
+                       insert_data = g_async_queue_pop(reg_queue2);
+                       __msc_insert_register_request(register_array, insert_data);
+                       continue;
+               } else if (register_array->len == 0 && remain_request == 0) {
+               /*Threre is no request, Wait until pushung new request*/
+                       insert_data = g_async_queue_pop(reg_queue2);
+                       __msc_insert_register_request(register_array, insert_data);
+                       continue;
+               }
+       }
+
+       if(((*register_data)->msg_size <= 0) ||((*register_data)->msg_size > MS_FILE_PATH_LEN_MAX)) {
+               MS_DBG_ERR("message size[%d] is wrong", (*register_data)->msg_size);
+               return MS_MEDIA_ERR_INVALID_IPC_MESSAGE;
+       }
+
+       return MS_MEDIA_ERR_NONE;
+
+}
+
+gboolean msc_register_thread(void *data)
+{
+       ms_comm_msg_s *register_data = NULL;
+       GArray *register_array = NULL;
+       GArray *path_array = NULL;
+       char *file_path = NULL;
+       int ret;
+       int pid = 0;
+       ms_msg_type_e current_msg = MS_MSG_MAX;
+
+       /*create array for processing overlay data*/
+       register_array = g_array_new (FALSE, FALSE, sizeof (ms_comm_msg_s *));
+       if (register_array == NULL) {
+               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) {
+                       MS_DBG_ERR("power off");
+                       goto _POWEROFF;
+               }
+
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("__msc_tv_pop_register_request failed [%d]", ret);
+                       goto FREE_RESOURCE;
+               }
+
+               /* check current request */
+               current_msg = register_data->msg_type;
+               pid = register_data->pid;
+
+               if ((current_msg != MS_MSG_BULK_INSERT) &&
+                       (current_msg != MS_MSG_BURSTSHOT_INSERT)) {
+                       MS_DBG_ERR("wrong message type");
+                       goto FREE_RESOURCE;
+               }
+
+               file_path = strndup(register_data->msg, register_data->msg_size);
+               if (file_path == NULL) {
+                       MS_DBG_ERR("file path is NULL");
+                       goto FREE_RESOURCE;
+               }
+
+               ret = __msc_make_file_list(file_path, &path_array);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("__msc_tv_make_file_list failed [%d]", ret);
+                       goto FREE_RESOURCE;
+               }
+
+               ret = __msc_batch_insert(current_msg, pid, path_array);
+
+FREE_RESOURCE:
+               /*Active flush */
+               malloc_trim(0);
+
+               /* If register_files operation is stopped, there is no necessrty for sending result. */
+               msc_send_result(ret, register_data);
+
+               MS_DBG_ERR("BULK REGISTER END [%d |%s]", ret, register_data->msg);
+
+               __msc_clear_file_list(path_array);
+
+               MS_SAFE_FREE(file_path);
+               MS_SAFE_FREE(register_data);
+       }                       /*thread while*/
+
+_POWEROFF:
+       MS_SAFE_FREE(file_path);
+       MS_SAFE_FREE(register_data);
+       if (register_array) {
+               while(register_array->len != 0) {
+                       ms_comm_msg_s *data = NULL;
+                       data = g_array_index(register_array , ms_comm_msg_s*, 0);
+                       g_array_remove_index (register_array, 0);
+                       MS_SAFE_FREE(data);
+               }
+               g_array_free (register_array, FALSE);
+               register_array = NULL;
+       }
+
+       __msc_clear_file_list(path_array);
+
+       return false;
+}
+
+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) 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, const char *path, int result)
+{
+       /*call for bundle commit*/
+       if (valid_status) ms_validate_end(handle);
+       if (ins_status) ms_register_end(handle, path);
+
+       return;
+}
+
+int msc_set_cancel_path(const char *cancel_path)
+{
+       if (g_cancel_path2 != NULL) {
+               MS_DBG_WARN("g_cancel_path2 is not NULL");
+               free(g_cancel_path2);
+               g_cancel_path2 = NULL;
+       }
+
+       g_cancel_path2 = strdup(cancel_path);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_del_cancel_path()
+{
+       if (g_cancel_path2 != NULL) {
+               MS_DBG_WARN("g_tv_cancel_path is not NULL");
+               free(g_cancel_path2);
+               g_cancel_path2 = NULL;
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+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;
+               MSC_DBG_ERR("ms_config_set_int failed");
+       }
+#else
+       stg_scan_status2 = status;
+#endif
+       return res;
+}
+
+static int __msc_get_storage_scan_status(ms_storage_scan_status_e *status)
+{
+       int res = MS_MEDIA_ERR_NONE;
+
+#if 0
+       if (!ms_config_get_int(MS_SCANNER_STATUS, status)) {
+               res = MS_MEDIA_ERR_VCONF_SET_FAIL;
+               MSC_DBG_ERR("ms_config_get_int failed");
+       }
+#else
+       *status = stg_scan_status2;
+#endif
+
+       return res;
+}
+
+static bool __msc_storage_mount_status(const char* start_path)
+{
+       bool ret = false;
+       char *storage_path = NULL;
+       char *remain_path = NULL;
+       int remain_len = 0;
+       GArray *dev_list = NULL;
+
+       remain_path = strstr(start_path+strlen(STORAGE_PATH) +1, "/");
+       if (remain_path != NULL)
+               remain_len = strlen(remain_path);
+
+       storage_path = strndup(start_path, strlen(start_path) - remain_len);
+
+       MS_DBG_ERR("storage_path [%s]", storage_path);
+
+       ret = ms_sys_get_device_list(MS_STG_TYPE_ALL, &dev_list);
+       if (ret == MS_MEDIA_ERR_NONE) {
+               if (dev_list != NULL) {
+                       MS_DBG_ERR("DEV 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);
+                               if (strcmp(block_info->mount_path, storage_path) == 0) {
+                                       ret = TRUE;
+                                       MS_DBG_ERR("STORAGE FOUND [%s]", block_info->mount_path);
+                                       break;
+                               }
+                       }
+                       ms_sys_release_device_list(&dev_list);
+               } else {
+                       MS_DBG_ERR("DEV NOT FOUND");
+               }
+       } else {
+               MS_DBG_ERR("ms_sys_get_device_list failed");
+       }
+
+       MS_SAFE_FREE(storage_path);
+
+       return ret;
+}
+
+int msc_set_blocked_path(const char *blocked_path)
+{
+       MS_DBG_FENTER();
+
+       g_mutex_lock(&blocked_mutex2);
+
+       if (g_blocked_path2 != NULL) {
+               MS_DBG_ERR("g_blocked_path is not NULL [%s]", g_blocked_path2);
+               free(g_blocked_path2);
+               g_blocked_path2 = NULL;
+       }
+
+       g_blocked_path2 = strdup(blocked_path);
+
+       g_mutex_unlock(&blocked_mutex2);
+
+       MS_DBG_FLEAVE();
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_del_blocked_path(void)
+{
+       MS_DBG_FENTER();
+
+       g_mutex_lock(&blocked_mutex2);
+
+       if (g_blocked_path2 != NULL) {
+               MS_DBG_ERR("g_blocked_path is not NULL [%s]", g_blocked_path2);
+               free(g_blocked_path2);
+               g_blocked_path2 = NULL;
+       }
+
+       g_mutex_unlock(&blocked_mutex2);
+
+       MS_DBG_FLEAVE();
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_init_scanner(void)
+{
+       if (!scan_queue2) scan_queue2 = g_async_queue_new();
+       if (!reg_queue2) reg_queue2 = g_async_queue_new();
+       if (!storage_queue2) storage_queue2 = g_async_queue_new();
+
+       /*Init mutex variable*/
+       g_mutex_init(&scan_req_mutex2);
+
+       return MS_MEDIA_ERR_NONE;
+}
+int msc_deinit_scanner(void)
+{
+       if (scan_queue2) g_async_queue_unref(scan_queue2);
+       if (reg_queue2) g_async_queue_unref(reg_queue2);
+       if (storage_queue2) g_async_queue_unref(storage_queue2);
+
+       /*Clear db mutex variable*/
+       g_mutex_clear(&scan_req_mutex2);
+
+       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_queue2, GINT_TO_POINTER(recv_msg));
+                       break;
+               case MS_SCAN_DIRECTORY:
+                       g_async_queue_push(scan_queue2, GINT_TO_POINTER(recv_msg));
+                       break;
+               case MS_SCAN_REGISTER:
+                       g_async_queue_push(reg_queue2, GINT_TO_POINTER(recv_msg));
+                       break;
+               default:
+                       MS_DBG_ERR("invalid parameter");
+                       ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+                       break;
+       }
+
+       return ret;
+}
+
+int msc_stop_scan_thread(void)
+{
+       ms_comm_msg_s *data = NULL;
+
+       msc_set_power_status(TRUE);
+
+       if (scan_queue2) {
+               /*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_queue2) {
+               /*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_queue2) {
+               /*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_get_remain_scan_request(ms_scan_type_e scan_type, int *remain_request)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+
+       switch(scan_type) {
+               case MS_SCAN_STORAGE:
+                       *remain_request = g_async_queue_length(storage_queue2);
+                       break;
+               case MS_SCAN_DIRECTORY:
+                       *remain_request = g_async_queue_length(scan_queue2);
+                       break;
+               case MS_SCAN_REGISTER:
+                       *remain_request = g_async_queue_length(reg_queue2);
+                       break;
+               default:
+                       MS_DBG_ERR("invalid parameter");
+                       ret = MS_MEDIA_ERR_INVALID_PARAMETER;
+                       break;
+       }
+
+       return ret;
+}
+
+int msc_get_dir_scan_status(bool *scan_status)
+{
+       *scan_status = g_directory_scan_processing2;
+
+       return MS_MEDIA_ERR_NONE;
+}
+
diff --git a/src/scanner-v2/media-scanner-socket-v2.c b/src/scanner-v2/media-scanner-socket-v2.c
new file mode 100755 (executable)
index 0000000..3184848
--- /dev/null
@@ -0,0 +1,244 @@
+/*
+ *  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 <arpa/inet.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/un.h>
+#include <malloc.h>
+#include <vconf.h>
+
+#include "media-util.h"
+#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-v2.h"
+#include "media-scanner-scan-v2.h"
+#include "media-scanner-socket-v2.h"
+#include "media-scanner-extract-v2.h"
+
+extern GAsyncQueue *storage_queue2;
+extern GAsyncQueue *scan_queue2;
+extern GAsyncQueue *reg_queue2;
+extern GMutex scan_req_mutex2;
+extern GAsyncQueue *folder_extract_queue;
+
+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;
+
+       MS_DBG_WARN("scan_req_mutex2 is LOCKED");
+       g_mutex_lock(&scan_req_mutex2);
+
+       if (len == 0) {
+               MS_DBG_WARN("Request is not stacked");
+               goto END_REMOVE_REQUEST;
+       }
+
+       msc_set_cancel_path(recv_msg->msg);
+       msc_set_extract_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_queue2);
+               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_queue2);
+       scan_queue2 = temp_scan_queue;
+
+END_REMOVE_REQUEST:
+       g_mutex_unlock(&scan_req_mutex2);
+       MS_DBG_WARN("scan_req_mutex2 is UNLOCKED");
+
+       GAsyncQueue *temp_extract_queue = NULL;
+       int len_extract = g_async_queue_length(folder_extract_queue);
+       if(len_extract != 0)
+       {
+               temp_extract_queue = g_async_queue_new();
+               for (i = 0; i <len_extract; i++) {
+                       /*create new queue to compare request*/
+                       msg = g_async_queue_pop(folder_extract_queue);
+                       if ((strcmp(msg->msg, cancel_path) == 0) && (pid == msg->pid)) {
+                               MS_SAFE_FREE(msg);
+                       } else {
+                               g_async_queue_push(temp_extract_queue, GINT_TO_POINTER(msg));
+                       }
+               }
+               g_async_queue_unref (folder_extract_queue);
+               folder_extract_queue = temp_extract_queue;
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer data)
+{
+       ms_comm_msg_s *recv_msg = NULL;
+       int sockfd = MS_SOCK_NOT_ALLOCATE;
+       int req_num = MS_MSG_MAX;
+       int err = -1;
+
+       sockfd = g_io_channel_unix_get_fd(src);
+       if (sockfd < 0) {
+               MS_DBG_ERR("sock fd is invalid!");
+               return TRUE;
+       }
+
+       MS_MALLOC(recv_msg, sizeof(ms_comm_msg_s));
+       if (recv_msg == NULL) {
+               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) {
+               MS_DBG_STRERROR("fifo read failed");
+               MS_SAFE_FREE(recv_msg);
+               return MS_MEDIA_ERR_FILE_READ_FAIL;
+       }
+
+       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;
+
+       switch(req_num){
+               case MS_MSG_BULK_INSERT:
+               case MS_MSG_BURSTSHOT_INSERT:
+                       {
+                               MS_DBG_INFO("BULK INSERT");
+                               /* request bulk insert*/
+                               msc_push_scan_request(MS_SCAN_REGISTER, recv_msg);
+                       }
+                       break;
+               case MS_MSG_DIRECTORY_SCANNING:
+               case MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE:
+                       {
+                               /* this request from another apps */
+                               /* set the scan data for scanning thread */
+                               msc_push_scan_request(MS_SCAN_DIRECTORY, recv_msg);
+                       }
+                       break;
+               case MS_MSG_STORAGE_ALL:
+               case MS_MSG_STORAGE_PARTIAL:
+               case MS_MSG_STORAGE_INVALID:
+                       {
+                               /* this request from media-server */
+                               msc_push_scan_request(MS_SCAN_STORAGE, recv_msg);
+                       }
+                       break;
+               case MS_MSG_DIRECTORY_SCANNING_CANCEL:
+                       {
+                               _msc_remove_request(scan_queue2, recv_msg);
+                               MS_SAFE_FREE(recv_msg);
+                       }
+                       break;
+               default:
+                       {
+                               MS_DBG_ERR("THIS REQUEST IS INVALID %d", req_num);
+                               MS_SAFE_FREE(recv_msg);
+                       }
+                       break;
+       }
+
+       /*Active flush */
+       malloc_trim(0);
+
+       return TRUE;
+}
+
+int msc_send_ready(void)
+{
+       int res = MS_MEDIA_ERR_NONE;
+       ms_comm_msg_s send_msg;
+       int fd = -1;
+       int err = -1;
+
+       fd = open(MS_SCANNER_FIFO_PATH_RES, O_WRONLY);
+       if (fd < 0) {
+               MS_DBG_STRERROR("fifo read failed");
+               return MS_MEDIA_ERR_FILE_OPEN_FAIL;
+       }
+
+       /* send ready message */
+       memset(&send_msg, 0, sizeof(send_msg));
+       send_msg.msg_type = MS_MSG_SCANNER_READY;
+
+       /* send ready message */
+       err = write(fd, &send_msg, sizeof(send_msg));
+       if (err < 0) {
+               MS_DBG_STRERROR("fifo write failed");
+               res = MS_MEDIA_ERR_FILE_READ_FAIL;
+       }
+
+       close(fd);
+
+       return res;
+}
+
+int msc_send_result(int result, ms_comm_msg_s *res_data)
+{
+       int res = MS_MEDIA_ERR_NONE;
+       ms_comm_msg_s send_msg;
+       int fd = -1;
+       int err = -1;
+
+       fd = open(MS_SCANNER_FIFO_PATH_RES, O_WRONLY);
+       if (fd < 0) {
+               MS_DBG_STRERROR("fifo open failed");
+               return MS_MEDIA_ERR_FILE_OPEN_FAIL;
+       }
+
+       /* send result message */
+       memset(&send_msg, 0x0, sizeof(ms_comm_msg_s));
+       send_msg.msg_type = MS_MSG_SCANNER_BULK_RESULT;
+       send_msg.pid = res_data->pid;
+       send_msg.result = result;
+       send_msg.msg_size = res_data->msg_size;
+       strncpy(send_msg.msg, res_data->msg, send_msg.msg_size);
+
+       /* send ready message */
+       err = write(fd, &send_msg, sizeof(send_msg));
+       if (err < 0) {
+               MS_DBG_STRERROR("fifo write failed");
+               res = MS_MEDIA_ERR_FILE_READ_FAIL;
+       }
+
+       close(fd);
+
+       return res;
+}
+
diff --git a/src/scanner-v2/media-scanner-v2.c b/src/scanner-v2/media-scanner-v2.c
new file mode 100755 (executable)
index 0000000..8342064
--- /dev/null
@@ -0,0 +1,181 @@
+/*
+ *  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 <dirent.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <dd-display.h>
+
+#include "media-common-utils.h"
+#include "media-common-external-storage.h"
+#include "media-common-system.h"
+#include "media-common-db-svc.h"
+
+#include "media-util.h"
+#include "media-scanner-dbg-v2.h"
+#include "media-scanner-common-v2.h"
+#include "media-scanner-device-block-v2.h"
+#include "media-scanner-scan-v2.h"
+#include "media-scanner-socket-v2.h"
+#include "media-scanner-extract-v2.h"
+
+static GMainLoop *scanner_mainloop2 = NULL;
+
+static void __msc_power_off_cb(ms_power_info_s *power_info, void* data);
+static void __msc_add_event_receiver(void *data);
+static void __msc_remove_event_receiver(void);
+
+int main(int argc, char **argv)
+{
+       GThread *storage_scan_thread = NULL;
+       GThread *scan_thread = NULL;
+       GThread *register_thread = NULL;
+       GThread *storage_extract_thread = NULL;
+       GThread *folder_extract_thread = NULL;
+       GSource *source = NULL;
+       GIOChannel *channel = NULL;
+       GMainContext *context = NULL;
+       bool power_off_status = FALSE;
+
+       int err = -1;
+       int fd = -1;
+
+       MS_DBG_ERR("[No-Error] ========== Scanner start ========");
+
+       __msc_add_event_receiver(NULL);
+
+       /*load functions from plusin(s)*/
+       err = ms_load_functions();
+       if (err != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("function load failed[%d]", err);
+               goto EXIT;
+       }
+
+       /*Init main loop*/
+       scanner_mainloop2 = g_main_loop_new(NULL, FALSE);
+
+       msc_init_scanner();
+       msc_init_extract_thread();
+       msc_init_scan_thread();
+
+       /* Create pipe */
+       err = unlink(MS_SCANNER_FIFO_PATH_REQ);
+       if (err !=0) {
+               MS_DBG_STRERROR("[No-Error] unlink failed");
+       }
+
+       err = mkfifo(MS_SCANNER_FIFO_PATH_REQ, MS_SCANNER_FIFO_MODE);
+       if (err !=0) {
+               MS_DBG_STRERROR("mkfifo failed");
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       fd = open(MS_SCANNER_FIFO_PATH_REQ, O_RDWR);
+       if (fd < 0) {
+               MS_DBG_STRERROR("fifo open failed");
+               return MS_MEDIA_ERR_FILE_OPEN_FAIL;
+       }
+
+       context = g_main_loop_get_context(scanner_mainloop2);
+
+       /* Create new channel to watch pipe */
+       channel = g_io_channel_unix_new(fd);
+       source = g_io_create_watch(channel, G_IO_IN);
+
+       /* Set callback to be called when pipe is readable */
+       g_source_set_callback(source, (GSourceFunc)msc_receive_request, NULL, NULL);
+       g_source_attach(source, context);
+       g_source_unref(source);
+
+       /*create each threads*/
+       storage_scan_thread = g_thread_new("storage_scan_thread", (GThreadFunc)msc_storage_scan_thread, NULL);
+       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);
+       storage_extract_thread = g_thread_new("storage_extract_thread", (GThreadFunc)msc_storage_extract_thread, NULL);
+       folder_extract_thread = g_thread_new("folder_extract_thread", (GThreadFunc)msc_folder_extract_thread, NULL);
+
+       if (ms_is_mmc_inserted()) {
+               msc_set_mmc_status(MS_STG_INSERTED);
+       }
+
+       msc_send_ready();
+       MS_DBG_ERR("[No-Error] ========== Scanner is ready ========");
+
+       MS_DBG_ERR("[No-Error] ========== Scanner is running  ========");
+
+       g_main_loop_run(scanner_mainloop2);
+
+       g_thread_join (scan_thread);
+       g_thread_join (register_thread);
+       g_thread_join (storage_scan_thread);
+       g_thread_join (storage_extract_thread);
+       g_thread_join (folder_extract_thread);
+
+       g_io_channel_shutdown(channel, FALSE, NULL);
+       g_io_channel_unref(channel);
+
+       msc_get_power_status(&power_off_status);
+       if (power_off_status) {
+               display_unlock_state(LCD_OFF, STAY_CUR_STATE);
+       }
+
+       msc_deinit_extract_thread();
+       msc_deinit_scan_thread();
+       msc_deinit_scanner();
+
+       /*close pipe*/
+       close(fd);
+
+       /*unload functions*/
+       ms_unload_functions();
+
+       __msc_remove_event_receiver();
+
+EXIT:
+       MS_DBG_ERR("[No-Error] ========== Scanner end  ========");
+
+       return 0;
+}
+
+
+static void __msc_power_off_cb(ms_power_info_s *power_info, void* data)
+{
+       msc_stop_scan_thread();
+       msc_stop_extract_thread();
+
+       if (g_main_loop_is_running(scanner_mainloop2)) g_main_loop_quit(scanner_mainloop2);
+}
+
+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/scanner/include/media-scanner-db-svc.h b/src/scanner/include/media-scanner-db-svc.h
deleted file mode 100755 (executable)
index f9bffaf..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**, 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*, char **);
-typedef int (*INSERT_ITEM)(void*, const char*, int, char **);
-typedef int (*INSERT_ITEM_IMMEDIATELY)(void*, const char*, int, char **);
-typedef int (*SET_ALL_STORAGE_ITEMS_VALIDITY)(void*, int, int, char **);
-typedef int (*SET_ITEM_VALIDITY_BEGIN)(void*, int, char **);
-typedef int (*SET_ITEM_VALIDITY_END)(void*, char **);
-typedef int (*SET_ITEM_VALIDITY)(void*, const char*, int, int, char **);
-typedef int (*DELETE_ALL_ITEMS_IN_STORAGE)(void*, int, char **);
-typedef int (*DELETE_ALL_INVALID_ITMES_IN_STORAGE)(void*, int, char **);
-typedef int (*UPDATE_BEGIN)(void);
-typedef int (*UPDATE_END)(void);
-typedef int (*SET_FOLDER_ITEM_VALIDITY)(void*, const char*, int, int, char**);
-typedef int (*DELETE_ALL_INVALID_ITEMS_IN_FOLDER)(void*, const char*, char**);
-typedef int (*INSERT_BURST_ITEM)(void *, const char *, int , 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 *, char **);
-typedef int (*GET_FOLDER_LIST)(void *, char*, char ***, int **, int **, int *, char **);
-typedef int (*UPDATE_FOLDER_TIME)(void *, const char *, char **);
-typedef int (*UPDATE_ITEM_META)(void *, const char *, int, char **);
-typedef int (*UPDATE_ITEM_BEGIN)(void *, int, char **);
-typedef int (*UPDATE_ITEM_END)(void *, char **);
-
-int
-msc_load_functions(void);
-
-void
-msc_unload_functions(void);
-
-int
-msc_connect_db(void ***handle);
-
-int
-msc_disconnect_db(void ***handle);
-
-int
-msc_validate_item(void **handle, const char *path);
-
-int
-msc_insert_item_batch(void **handle, const char *path);
-
-int
-msc_insert_burst_item(void **handle, const char *path);
-
-bool
-msc_delete_all_items(void **handle, ms_storage_type_t store_type);
-
-int
-msc_validaty_change_all_items(void **handle, ms_storage_type_t store_type, bool validity);
-
-bool
-msc_delete_invalid_items(void **handle, ms_storage_type_t store_type);
-
-int
-msc_set_folder_validity(void **handle, const char *path, int validity, int recursive);
-
-int
-msc_delete_invalid_items_in_folder(void **handle, const char*path);
-
-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);
-
-int
-msc_insert_item_immediately(void **handle, const char *path);
-
-int
-msc_update_meta_batch(void **handle, const char *path);
-
-/****************************************************************************************************
-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);
-
-void
-msc_validate_start(void **handle);
-
-void
-msc_validate_end(void **handle);
-
-void
-msc_update_start(void **handle);
-
-void
-msc_update_end(void **handle);
-
-#endif /*_MEDIA_SCANNER_DB_SVC_H_*/
index 735e4c1..35e2e52 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-scanner-dbg.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
-
 #ifndef _MEDIA_SCANNER_DBG_H_
 #define _MEDIA_SCANNER_DBG_H_
 #include <sys/syscall.h>
 #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,}; \
-                       strerror_r(errno, buf, BUF_LENGTH); \
-                       LOGE(fmt" : STANDARD ERROR [%s]", buf);  \
-               } 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..bb73520
--- /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(ms_block_info_s *block_info, void *user_data);
+
+#endif
\ No newline at end of file
index 49e32c9..408720a 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-scan.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
 #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
index 4d026b3..b3e33fb 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-thumb.c
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
 #ifndef _MEDIA_SCANNER_SOCKET_H_
 #define _MEDIA_SCANNER_SOCKET_H_
 
diff --git a/src/scanner/media-scanner-db-svc.c b/src/scanner/media-scanner-db-svc.c
deleted file mode 100755 (executable)
index c1842f1..0000000
+++ /dev/null
@@ -1,769 +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"
-
-#define CONFIG_PATH "/usr/etc/media-server-plugin"
-#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_ALLOCATE_MEMORY_FAIL;
-       }
-
-       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_ALLOCATE_MEMORY_FAIL;
-       }
-
-       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_ALLOCATE_MEMORY_FAIL;
-               }
-       }
-
-       /*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) g_array_free(so_array, TRUE);
-}
-
-int
-msc_connect_db(void ***handle)
-{
-       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);
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((CONNECT)func_array[lib_index][eCONNECT])(&((*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);
-                       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)
-{
-       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);
-                       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, &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, &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);
-                                       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)
-{
-       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, &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)
-{
-       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);
-
-       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, &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)
-{
-       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);
-
-       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, &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)
-{
-       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);
-
-       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, &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)
-{
-       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, &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)
-{
-       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, &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)
-{
-       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,&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)
-{
-       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, &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));
-                       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)
-{
-       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, &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)
-{
-       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);
-
-       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, &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)
-{
-       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], &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])();/*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)
-{
-       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], &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)
-{
-       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], &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..7ac1c34
--- /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(ms_block_info_s *block_info, void *user_data)
+{
+       if (block_info->block_type == 0) {
+               MS_DBG_ERR("GET THE USB EVENT");
+               if (block_info->state == MS_STG_INSERTED) {
+                       /*DO NOT THING*/
+               } else {
+                       __msc_usb_remove_event(block_info->mount_path);
+               }
+       } else {
+               MS_DBG_ERR("GET THE MMC EVENT");
+               _msc_mmc_changed_event(block_info->mount_path, block_info->state);
+       }
+}
index c403ab4..8233f8a 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-scan.c
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <dirent.h>
@@ -39,8 +31,9 @@
 #include "media-server-ipc.h"
 #include "media-common-utils.h"
 #include "media-common-external-storage.h"
+#include "media-common-db-svc.h"
+#include "media-common-security.h"
 #include "media-scanner-dbg.h"
-#include "media-scanner-db-svc.h"
 #include "media-scanner-socket.h"
 #include "media-scanner-scan.h"
 
@@ -54,7 +47,14 @@ 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;
+char *g_blocked_path;
+bool g_directory_scan_processing;
+GArray *blocked_stg_list;
+
+int stg_scan_status;
 
 #ifdef FMS_PERF
 extern struct timeval g_mmc_start_time;
@@ -64,19 +64,24 @@ 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);
-static int __msc_db_update(void **handle, const ms_comm_msg_s * scan_data);
+static int __msc_stg_scan(void **handle, const char *storage_id, const char*start_path, ms_storage_type_t storage_type, int scan_type);
+static int __msc_dir_scan(void **handle, const char *storage_id, const char*start_path, ms_storage_type_t storage_type, int scan_type, bool is_recursive);
+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);
+static bool __msc_check_folder_path(const char *folder_path);
 static int __msc_make_file_list(char *file_path, GArray **path_array);
 static int __msc_batch_insert(ms_msg_type_e current_msg, int pid, GArray *path_array);
 static int __msc_pop_register_request(GArray *register_array, ms_comm_msg_s **register_data);
 static int __msc_clear_file_list(GArray *path_array);
 static bool __msc_check_scan_ignore(char * path);
 static bool __msc_is_valid_path(const char *path);
-static void __msc_check_dir_path(char *dir_path);
+static void __msc_trim_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);
+static void __msc_bacth_commit_disable(void* handle, bool ins_status, bool valid_status, const char *path);
+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);
+//static bool __msc_storage_mount_status(const char* start_path);
 
 static int __msc_set_power_mode(ms_db_status_type_t status)
 {
@@ -95,7 +100,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;
        }
 
@@ -110,32 +115,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;
        }
 
@@ -147,17 +152,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");
+               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;
        }
 
@@ -175,26 +180,59 @@ static bool __msc_check_scan_ignore(char * path)
        return false;
 }
 
+#if 0
+GCond data_cond;
+GMutex data_mutex;
+gpointer current_data = NULL;
+
+static int __msc_resume_scan()
+{
+       g_mutex_lock (&data_mutex);
+
+//     current_data = GINT_TO_POINTER(g_directory_scan_processing);
+       g_cond_signal (&data_cond);
+
+       g_mutex_unlock (&data_mutex);
+
+       return MS_MEDIA_ERR_NONE;
+}
+static int __msc_pause_scan()
+{
+       g_mutex_lock (&data_mutex);
+
+//     while (current_data)
+       while (g_directory_scan_processing)
+               g_cond_wait (&data_cond, &data_mutex);
+
+//     current_data = GPOINTER_TO_INT(g_directory_scan_processing);
+
+       g_mutex_unlock (&data_mutex);
+
+       return MS_MEDIA_ERR_NONE;
+}
+#endif
+
 static int __msc_check_stop_status(ms_storage_type_t storage_type)
 {
        int ret = MS_MEDIA_ERR_NONE;
 
        /*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 ((mmc_state != VCONFKEY_SYSMAN_MMC_MOUNTED) && (storage_type == MS_STORAGE_EXTERNAL)) {
-               MSC_DBG_ERR("Directory scanning is stopped");
+#if 0
+       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
        return ret;
 }
 
-static void __msc_check_dir_path(char *dir_path)
+static void __msc_trim_dir_path(char *dir_path)
 {
        /* need implementation */
        /* if dir_path is not NULL terminated, this function will occure crash */
@@ -204,7 +242,171 @@ 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)
+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_read_dir(void **handle, GArray *dir_array, const char *start_path, const char *storage_id, ms_storage_type_t storage_type, int scan_type)
+{
+       DIR *dp = NULL;
+       struct dirent entry;
+       struct dirent *result = NULL;
+       int ret = MS_MEDIA_ERR_NONE;
+       char *new_path = NULL;
+       char *current_path = NULL;
+       char path[MS_FILE_PATH_LEN_MAX] = { 0 };
+       int (*scan_function)(void **, const char*, const char*) = NULL;
+
+       if (dir_array != NULL) {
+               MS_DBG_ERR("dir_array is NULL");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       /* make new array for storing directory */
+       dir_array = g_array_new (FALSE, FALSE, sizeof (char*));
+       if (dir_array == NULL){
+               MS_DBG_ERR("g_array_new failed");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
+
+       g_array_append_val (dir_array, start_path);
+
+       scan_function = (scan_type == MS_MSG_STORAGE_ALL) ? ms_insert_item_batch : ms_validate_item;
+
+       /*start db update. the number of element in the array , db update is complete.*/
+       while (dir_array->len != 0) {
+               /*check poweroff status*/
+               ret = __msc_check_stop_status(storage_type);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       goto STOP_DIR_SCAN;
+               }
+               /* get the current path from directory array */
+               current_path = g_array_index(dir_array , char*, 0);
+               g_array_remove_index (dir_array, 0);
+               MS_DBG_SLOG("%d", dir_array->len);
+
+               if (__msc_check_scan_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) {
+                       /*insert current directory*/
+                       if(ms_insert_folder(handle, storage_id, current_path) != MS_MEDIA_ERR_NONE) {
+                               MS_DBG_ERR("insert folder failed");
+                       }
+
+                       while (!readdir_r(dp, &entry, &result)) {
+                               /*check poweroff status*/
+                               ret = __msc_check_stop_status(storage_type);
+                               if (ret != MS_MEDIA_ERR_NONE) {
+                                       ms_insert_folder_end(handle);
+                                       goto STOP_DIR_SCAN;
+                               }
+
+                               if (result == NULL)
+                                       break;
+
+                               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) {
+                                       /* insert into media DB */
+                                       if (scan_function(handle,storage_id, path) != 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 */
+                                               /* add new directory to dir_array */
+                                               new_path = strdup(path);
+                                               if (new_path != NULL) {
+                                                       g_array_append_val (dir_array, new_path);
+                                               } 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) != MS_MEDIA_ERR_NONE) {
+                                                       MS_DBG_ERR("insert folder failed");
+                                               }
+                                       }
+                               }
+                       }
+               } else {
+                       MS_DBG_ERR("%s folder opendir fails", current_path);
+               }
+               if (dp) closedir(dp);
+               dp = NULL;
+
+               ms_insert_folder_end(handle);
+
+               MS_SAFE_FREE(current_path);
+       }
+
+STOP_DIR_SCAN:
+       if (dp) closedir(dp);
+       __msc_clear_file_list(dir_array);
+
+       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, bool is_recursive)
+{
+       GArray *dir_array = NULL;
+       int ret = MS_MEDIA_ERR_NONE;
+       char *new_start_path = NULL;
+
+       new_start_path = strdup(start_path);
+       if (new_start_path == NULL) {
+               MS_DBG_ERR("g_array_new failed");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
+
+       MS_DBG_ERR("[No-Error] start path [%s]", new_start_path);
+
+       /*call for bundle commit*/
+       __msc_bacth_commit_enable(handle, TRUE, TRUE, MS_NOTI_SWITCH_OFF, 0);
+
+       __msc_read_dir(handle, dir_array, start_path, storage_id, storage_type, scan_type);
+
+       /*call for bundle commit*/
+       __msc_bacth_commit_disable(handle, TRUE, TRUE, new_start_path);
+
+       MS_SAFE_FREE(new_start_path);
+
+       MS_DBG_INFO("ret : %d", ret);
+
+       return ret;
+}
+
+
+static int __msc_stg_scan(void **handle, const char *storage_id, const char*start_path, ms_storage_type_t storage_type, int scan_type)
 {
        DIR *dp = NULL;
        GArray *dir_array = NULL;
@@ -214,18 +416,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*) = NULL;
+       bool is_recursive = true;
+       char *new_start_path = NULL;
+       int (*scan_function)(void **, const char*, const char*) = 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");
-               return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
+               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) != 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);
+       }
 
        /*start db update. the number of element in the array , db update is complete.*/
        while (dir_array->len != 0) {
@@ -237,14 +463,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)) {
@@ -260,54 +488,79 @@ 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) != MS_MEDIA_ERR_NONE) {
-                                               MSC_DBG_ERR("failed to update db : %d", scan_type);
+                                       if (scan_function(handle,storage_id, path) != 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) != 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) != 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);
+                       if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE && storage_type == MS_STORAGE_INTERNAL)
+                               ms_update_folder_time(handle, INTERNAL_STORAGE_ID, current_path);
                } 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);
+
                MS_SAFE_FREE(current_path);
        }               /*db update while */
+
+               /*remove invalid folder in folder table.*/
+       if (__msc_check_mount_storage(new_start_path)) {
+               if(ms_delete_invalid_folder(handle, storage_id) != 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;
@@ -320,20 +573,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);
+               err = __msc_stg_scan(handle, storage_id, start_path, storage_type, scan_type);
                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);
+               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);
                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);
+
                MS_SAFE_FREE(start_path);
        }
 
@@ -345,81 +601,155 @@ static int __msc_db_update(void **handle, const ms_comm_msg_s * scan_data)
 gboolean msc_directory_scan_thread(void *data)
 {
        ms_comm_msg_s *scan_data = NULL;
-       int err;
-       int ret;
+       int ret = MS_MEDIA_ERR_NONE;
        void **handle = NULL;
        int scan_type;
-       char *noti_path = NULL;
+       char *storage_id = NULL;
+       bool is_recursive = true;
+       char *start_path = NULL;
+       ms_storage_type_t storage_type;
+       ms_noti_type_e noti_type = MS_ITEM_INSERT;
+       int before_count = 0;
+       int after_count = 0;
+       int delete_count = 0;
+       int delete_folder_count = 0;
+       char *folder_uuid = 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);
-               if (err != MS_MEDIA_ERR_NONE)
+               ret = ms_connect_db(&handle);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("ms_connect_db failed");
                        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");
+
+               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;
                }
 
-               __msc_check_dir_path(scan_data->msg);
+               __msc_trim_dir_path(scan_data->msg);
 
-               /*change validity before scanning*/
-               if (scan_type == MS_MSG_DIRECTORY_SCANNING)
-                       err = msc_set_folder_validity(handle, scan_data->msg, MS_INVALID, MS_RECURSIVE);
-               else
-                       err = msc_set_folder_validity(handle, scan_data->msg, MS_INVALID, MS_NON_RECURSIVE);
-               if (err != MS_MEDIA_ERR_NONE)
-                       MSC_DBG_ERR("error : %d", err);
+               if (__msc_check_folder_path(scan_data->msg)) {
+                       is_recursive = (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) ? false : true;
 
-               /*call for bundle commit*/
-               __msc_bacth_commit_enable(handle, TRUE, TRUE, MS_NOTI_DISABLE, 0);
+                       if (ms_check_folder_exist(handle, storage_id, scan_data->msg) == MS_MEDIA_ERR_NONE) {
+                               /*already exist in media DB*/
+                               noti_type = MS_ITEM_UPDATE;
+                               MS_DBG_ERR("[%s] already exist", scan_data->msg);
+                       } else {
+                               noti_type = MS_ITEM_INSERT;
+                               MS_DBG_ERR("[%s] new insert path", scan_data->msg);
+                       }
+               } else {
+                       /*directory is deleted*/
+                       is_recursive = true;
+                       noti_type = MS_ITEM_DELETE;
+
+                       /*get the UUID of deleted folder*/
+                       ms_get_folder_id(handle, storage_id, scan_data->msg, &folder_uuid);
+
+                       MS_DBG_ERR("[%s][%s] delete path", scan_data->msg, folder_uuid);
+               }
+
+               ms_check_subfolder_count(handle, storage_id, scan_data->msg, &before_count);
+               MS_DBG_ERR("BEFORE COUNT[%d]", before_count);
+
+               /* folder validity set 0 under the start_path in folder table*/
+               if(ms_set_folder_validity(handle, storage_id, scan_data->msg, MS_INVALID, is_recursive) != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("set_folder_validity failed [%d] ", scan_type);
+               }
+
+               /*change validity before scanning*/
+               if(ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_INVALID, is_recursive) != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("set_folder_validity failed [%d] ", scan_type);
+               }
 
                /*insert data into media db */
-               ret = __msc_db_update(handle, scan_data);
+               storage_type = ms_get_storage_type_by_full(scan_data->msg);
+               start_path = strndup(scan_data->msg, scan_data->msg_size);
 
-               /*call for bundle commit*/
-               __msc_bacth_commit_disable(handle, TRUE, TRUE);
+               ret = __msc_dir_scan(handle, storage_id, start_path, storage_type, scan_data->msg_type, is_recursive);
 
-               if (ret == MS_MEDIA_ERR_NONE) {
-                       MSC_DBG_INFO("working normally");
-                       int count = 0;
+               ms_check_subfolder_count(handle, storage_id, scan_data->msg, &after_count);
+               MS_DBG_ERR("BEFORE COUNT[%d]", before_count);
 
-                       noti_path = strndup(scan_data->msg, scan_data->msg_size);
-                       msc_count_delete_items_in_folder(handle, noti_path, &count);
+               if (ms_count_delete_items_in_folder(handle, storage_id, scan_data->msg, &delete_count) != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("counting invalid items failed");
+               }
+               
+               if (ms_delete_invalid_items_in_folder(handle, storage_id, scan_data->msg, is_recursive)  != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("deleting invalid items in folder failed");
+               }
+               
+               /*remove invalid folder in folder table.*/
+               if(ms_delete_invalid_folder_by_path(handle, storage_id, scan_data->msg, &delete_folder_count) != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("deleting invalid folder failed");
+               }
 
-                       MSC_DBG_SLOG("delete count %d", count);
-                       MSC_DBG_SLOG("insert count %d", insert_count);
+               MS_DBG_SLOG("delete folder count %d", delete_folder_count);
+
+               if (ret != MS_MEDIA_ERR_SCANNER_FORCE_STOP) {
+                       MS_DBG_INFO("working normally");
+
+                       if (noti_type != MS_ITEM_UPDATE) {
+                               ms_send_dir_update_noti(handle, storage_id, scan_data->msg, folder_uuid, noti_type, scan_data->pid);
+                       } else {
+                               int insert_count = ms_get_insert_count();
+                               int delete_count = ms_get_delete_count();
 
-                       msc_delete_invalid_items_in_folder(handle, scan_data->msg);
+                               MS_DBG_SLOG("delete count %d", delete_count);
+                               MS_DBG_SLOG("insert count %d", insert_count);
 
-                       if ( !(count == 0 && insert_count == 0)) {
-                               msc_send_dir_update_noti(handle, noti_path);
+                               if (!(delete_count == 0 && insert_count == 0) 
+                                       || !(before_count == after_count)
+                                       || delete_folder_count != 0) {
+                                       ms_send_dir_update_noti(handle, storage_id, scan_data->msg, folder_uuid, noti_type, scan_data->pid);
+                               }
                        }
-                       MS_SAFE_FREE(noti_path);
                }
 
-               insert_count = 0;
+               ms_reset_insert_count();
+               ms_reset_delete_count();
 
                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);
+               /*disconnect from media db*/
+               if (handle) ms_disconnect_db(&handle);
 NEXT:
                /*Active flush */
                malloc_trim(0);
@@ -427,13 +757,15 @@ NEXT:
                msc_send_result(ret, scan_data);
 
                MS_SAFE_FREE(scan_data);
+               MS_SAFE_FREE(storage_id);
+               MS_SAFE_FREE(folder_uuid);
 
-               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;
 }
@@ -441,9 +773,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;
@@ -455,7 +787,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)) {
@@ -471,11 +803,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) {
+                       MS_DBG_ERR("MALLOC failed");
+                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
+               }
+               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);
@@ -484,7 +820,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;
@@ -497,19 +833,21 @@ 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) {
-               MSC_DBG_ERR("msc_get_folder_list is failed", ret);
+               MS_SAFE_FREE(start_path);
+               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");
-               return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
+               MS_DBG_ERR("g_array_new failed");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
        }
        /* add first direcotiry to directory array */
        g_array_append_val (read_dir_array, start_path);
@@ -519,10 +857,10 @@ static int __msc_compare_with_db(void **handle, const char*update_path, int scan
                /* get the current path from directory array */
                current_path = g_array_index(read_dir_array , char*, 0);
                g_array_remove_index (read_dir_array, 0);
-//             MSC_DBG_ERR("%s", current_path);
+//             MS_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;
                }
@@ -539,7 +877,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 */
@@ -554,7 +892,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;
@@ -563,43 +901,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)
+static int _msc_db_update_partial(void **handle, const char *storage_id, ms_storage_type_t storage_type, GArray *dir_array)
 {
-       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);
+                       err = ms_set_folder_item_validity(handle, storage_id, update_path, MS_INVALID, MS_NON_RECURSIVE);
                        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);
+               __msc_stg_scan(handle, storage_id, update_path, storage_type, MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE);
 
 //             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);
@@ -621,112 +959,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);
+               err = ms_connect_db(&handle);
                if (err != MS_MEDIA_ERR_NONE)
                        continue;
 
                storage_type = ms_get_storage_type_by_full(scan_data->msg);
                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);
+
                /*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);
+                       ms_validaty_change_all_items(handle, storage_id, storage_type, true);
 
                        /* 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_WAN("__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);
+                               ret = _msc_db_update_partial(handle, storage_id, storage_type, dir_array);
                        } 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)) {
-                                       MSC_DBG_ERR("msc_delete_all_record fails");
+                               if (!ms_delete_all_items(handle, storage_id, storage_type)) {
+                                       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);
+                               ms_validaty_change_all_items(handle, storage_id, storage_type, false);
                        }
 
-                       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);
+               __msc_bacth_commit_disable(handle, TRUE, valid_status, scan_data->msg);
 
-               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);
+                       if (del_count != 0) {
+                               MS_DBG_ERR("storage thread delete count [%d]", del_count);
+                               ms_delete_invalid_items(handle, storage_id, storage_type);
+                       }
                }
 
                /* 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, NULL, MS_ITEM_UPDATE, scan_data->pid);
 
 #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);
+                       __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);
+                       __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_WAN("power off");
+                       MS_DBG_ERR("[No-Error] power off");
                        goto _POWEROFF;
                }
 
-               /*disconnect form media db*/
-               if (handle) msc_disconnect_db(&handle);
+               /*disconnect from media db*/
+               if (handle) ms_disconnect_db(&handle);
 
                /*Active flush */
                malloc_trim(0);
@@ -735,19 +1098,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);
@@ -767,10 +1130,10 @@ static bool __msc_is_valid_path(const char *path)
                return true;
        } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
                return true;
+       } else if (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0) {
+               return true;
        } else
                return false;
-
-       return true;
 }
 
 static int __msc_check_file_path(const char *file_path)
@@ -781,15 +1144,15 @@ static int __msc_check_file_path(const char *file_path)
        /* check location of file */
        /* file must exists under "/opt/usr/media" or "/opt/storage/sdcard" */
        if(!__msc_is_valid_path(file_path)) {
-               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]", file_path);
-               MSC_DBG_STRERROR("file is not exist");
+               MS_DBG_STRERROR("Open failed");
+               MS_DBG_ERR("error path [%s]", file_path);
                return MS_MEDIA_ERR_INVALID_PATH;
        }
        close(exist);
@@ -800,17 +1163,31 @@ static int __msc_check_file_path(const char *file_path)
        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_STRERROR("stat failed");
+               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) {
@@ -835,13 +1212,13 @@ static int __msc_check_ignore_dir(const char *full_path)
 
        ret = __msc_check_file_path(full_path);
        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;
        }
@@ -857,13 +1234,15 @@ static int __msc_check_ignore_dir(const char *full_path)
                        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;
                }
@@ -886,7 +1265,7 @@ static int __msc_make_file_list(char *file_path, GArray **path_array)
        /* 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;
        }
@@ -895,8 +1274,8 @@ static int __msc_make_file_list(char *file_path, GArray **path_array)
        /* 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");
-               res = MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
+               MS_DBG_ERR("g_array_new failed");
+               res = MS_MEDIA_ERR_OUT_OF_MEMORY;
                goto FREE_RESOURCE;
        }
 
@@ -908,14 +1287,14 @@ static int __msc_make_file_list(char *file_path, GArray **path_array)
                /* check valid path */
                ret = __msc_check_ignore_dir(path);
                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");
-                       res = MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
+                       MS_DBG_ERR("g_array_append_val failed");
+                       res = MS_MEDIA_ERR_OUT_OF_MEMORY;
                        goto FREE_RESOURCE;
                }
        }
@@ -938,45 +1317,59 @@ FREE_RESOURCE:
 static int __msc_batch_insert(ms_msg_type_e current_msg, int pid, GArray *path_array)
 {
        int err;
-       int i;
+       unsigned int i;
        void **handle = NULL;
        char *insert_path = NULL;
-       int (*insert_function)(void **, const char*) = NULL;
+       int (*insert_function)(void **, const char*, const char*) = 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);
+       err = ms_connect_db(&handle);
        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);
 
+               err = ms_check_client_permission_by_pid(pid, insert_path);
+               if (err != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("ms_check_client_permission_by_pid failed[%d]", err);
+                       continue;
+               }
+
+               /* 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);
+               err = insert_function(handle, storage_id, insert_path);
 
                if (power_off) {
-                       MSC_DBG_ERR("power off");
+                       MS_DBG_ERR("power off");
                        /*call for bundle commit*/
-                       msc_register_end(handle);
+                       ms_register_end(handle, NULL);
                        break;
                }
        }
 
        /*call for bundle commit*/
-       __msc_bacth_commit_disable(handle, TRUE, FALSE);
+       __msc_bacth_commit_disable(handle, TRUE, FALSE, NULL);
 
        /*disconnect form media db*/
-       if (handle) msc_disconnect_db(&handle);
+       if (handle) ms_disconnect_db(&handle);
 
        return MS_MEDIA_ERR_NONE;
 }
@@ -1007,7 +1400,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;
        }
 
@@ -1028,19 +1421,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;
                }
 
@@ -1050,15 +1443,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) {
+                       MS_DBG_ERR("file_path is NULL");
+                       goto FREE_RESOURCE;
+               }
 
                ret = __msc_make_file_list(file_path, &path_array);
                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;
                }
 
@@ -1071,7 +1468,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);
 
@@ -1101,21 +1498,34 @@ _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)
+static void __msc_bacth_commit_disable(void* handle, bool ins_status, bool valid_status, const char *path)
 {
        /*call for bundle commit*/
-       if (ins_status) msc_register_end(handle);
-       if (valid_status) msc_validate_end(handle);
+       if (ins_status) ms_register_end(handle, path);
+       if (valid_status) ms_validate_end(handle);
 
        return;
 }
 
+int msc_set_cancel_path(const char *cancel_path)
+{
+       if (g_cancel_path != NULL) {
+               MS_DBG_ERR("g_cancel_path is not NULL");
+               free(g_cancel_path);
+               g_cancel_path = NULL;
+       }
+
+       g_cancel_path = strdup(cancel_path);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
 static int __msc_dir_scan_meta_update(void **handle, const char*start_path, ms_storage_type_t storage_type)
 {
        DIR *dp = NULL;
@@ -1126,13 +1536,13 @@ 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*) = msc_update_meta_batch;
+       int (*scan_function)(void **, const char*) = 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");
-               return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
+               MS_DBG_ERR("g_array_new failed");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
        }
        /* add first direcotiry to directory array */
        g_array_append_val (dir_array, start_path);
@@ -1148,10 +1558,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;
                }
@@ -1172,21 +1582,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) != 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 */
@@ -1195,8 +1605,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);
@@ -1206,7 +1617,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;
 }
@@ -1215,48 +1626,49 @@ STOP_SCAN:
 gboolean msc_metadata_update(void *data)
 {
        ms_comm_msg_s *scan_data = data;
-       int err;
-       int ret;
+       int ret = MS_MEDIA_ERR_NONE;
        void **handle = NULL;
        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);
-       if (err != MS_MEDIA_ERR_NONE)
+       ret = ms_connect_db(&handle);
+       if (ret != MS_MEDIA_ERR_NONE)
                return false;
 
        /*call for bundle commit*/
-       msc_update_start(handle);
+       ms_update_start(handle);
 
        /*insert data into media db */
-
        start_path = strdup(MEDIA_ROOT_PATH_INTERNAL);
        ret = __msc_dir_scan_meta_update(handle, start_path, storage_type);
 
        /* send notification */
-       msc_send_dir_update_noti(handle, MEDIA_ROOT_PATH_INTERNAL);
+       ms_send_dir_update_noti(handle, INTERNAL_STORAGE_ID, MEDIA_ROOT_PATH_INTERNAL, NULL, MS_ITEM_UPDATE, scan_data->pid);
 
-       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);
                /* 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, NULL, MS_ITEM_UPDATE, scan_data->pid);
        }
 
+       /*FIX ME*/
+       /*__msc_dir_scan_meta_update For Each USB Storage*/
+
        /*call for bundle commit*/
-       msc_update_end(handle);
+       ms_update_end(handle);
 
        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);
@@ -1265,12 +1677,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;
 }
@@ -1280,3 +1692,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 9ee6060..2fd5f82 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-thumb.c
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
 #include <arpa/inet.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #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"
-
-extern GAsyncQueue *storage_queue;
-extern GAsyncQueue *scan_queue;
-extern GAsyncQueue *reg_queue;
+#include "media-scanner-scan.h"
 
 gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer data)
 {
@@ -57,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", recv_msg->pid, recv_msg->msg_type, recv_msg->msg);
+       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;
@@ -84,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:
@@ -94,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:
@@ -102,7 +91,13 @@ 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_dir_scan_request(recv_msg);
+                               MS_SAFE_FREE(recv_msg);
                        }
                        break;
                case MS_MSG_STORAGE_META:
@@ -110,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;
@@ -131,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;
        }
 
@@ -142,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;
        }
 
@@ -160,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;
        }
 
@@ -175,7 +170,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 c62dbbd..b41c6b9 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-main.c
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
-
 #include <dirent.h>
 #include <vconf.h>
 #include <sys/types.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-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;
-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));
-               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));
-               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));
-               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;
-/*
-       ms_comm_msg_s *scan_msg;
-       ms_dir_scan_type_t scan_type = MS_SCAN_PART;
-*/
-       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;
-/*
-       MS_MALLOC(scan_msg, sizeof(ms_comm_msg_s));
-
-       if (mmc_state == VCONFKEY_SYSMAN_MMC_REMOVED ||
-               mmc_state == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
-               scan_type = MS_SCAN_INVALID;
-       } else if (mmc_state == VCONFKEY_SYSMAN_MMC_MOUNTED) {
-               scan_type = ms_get_mmc_state();
-       }
-
-       switch (scan_type) {
-               case MS_SCAN_ALL:
-                       scan_msg->msg_type = MS_MSG_STORAGE_ALL;
-                       break;
-               case MS_SCAN_PART:
-                       scan_msg->msg_type = MS_MSG_STORAGE_PARTIAL;
-                       break;
-               case MS_SCAN_INVALID:
-                       scan_msg->msg_type = MS_MSG_STORAGE_INVALID;
-                       break;
-       }
-
-       scan_msg->pid = 0;
-       scan_msg->msg_size = strlen(MEDIA_ROOT_PATH_SDCARD);
-       ms_strcopy(scan_msg->msg, scan_msg->msg_size+1, "%s", MEDIA_ROOT_PATH_SDCARD);
-
-       MSC_DBG_INFO("ms_get_mmc_state is %d", scan_msg->msg_type);
-
-       g_async_queue_push(storage_queue, GINT_TO_POINTER(scan_msg));
-*/
-       return;
-}
+static void __msc_power_off_cb(ms_power_info_s *power_info, void* data);
+static void __msc_add_event_receiver(void *data);
+static void __msc_remove_event_receiver(void);
 
 int main(int argc, char **argv)
 {
@@ -211,52 +55,36 @@ int main(int argc, char **argv)
        int err = -1;
        int fd = -1;
 
-#if 0 /* temporary */
-       check_result = check_process();
-       if (check_result == false)
-               exit(0);
-#endif
-       if (!g_thread_supported()) {
-               g_thread_init(NULL);
-       }
-
-       /*Init main loop*/
-       scanner_mainloop = g_main_loop_new(NULL, FALSE);
-
        /*set power off callback function*/
-       ms_add_poweoff_event_receiver(_power_off_cb, NULL);
+       __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 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();
+       /*Init main loop*/
+       scanner_mainloop = g_main_loop_new(NULL, FALSE);
 
-       /*Init mutex variable*/
-       if (!db_mutex) db_mutex = g_mutex_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");
-               return MS_MEDIA_ERR_FIFO_MAKE_FAIL;
+               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;
        }
 
@@ -276,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);
 
@@ -299,27 +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);
-
-       /*Clear db mutex variable*/
-       if (db_mutex) g_mutex_free (db_mutex);
+       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(ms_power_info_s *power_info, 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();
+}
+
index a4130f9..aee27ac 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-dbg.h
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
-
 #ifndef _MEDIA_SERVER_DBG_H_
 #define _MEDIA_SERVER_DBG_H_
 
 #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,}; \
-                       strerror_r(errno, buf, BUF_LENGTH); \
-                       LOGE(fmt" : STANDARD ERROR [%s]", buf);  \
-               } 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..d55075c
--- /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(ms_block_info_s *block_info, void *user_data);
+
+#endif
\ No newline at end of file
index 735a5df..d9ff3fa 100755 (executable)
  *
  */
 
-int ms_scanner_start(void);
+#ifndef _MEDIA_SERVER_SCANNER_H
+#define _MEDIA_SERVER_SCANNER_H
 
+int ms_scanner_start(void);
 bool ms_get_scanner_status(void);
-
 void ms_reset_scanner_status(void);
+int ms_get_scanner_pid(void);
+void ms_cleanup_scanner(void);
 
-int ms_get_scanner_pid(void);
\ No newline at end of file
+#endif /*_MEDIA_SERVER_SCANNER_H*/
\ No newline at end of file
index a9d3a61..4a0138c 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-thumb.c
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
 #ifndef _MEDIA_SERVER_SOCKET_H_
 #define _MEDIA_SERVER_SOCKET_H_
 
 #include "media-server-ipc.h"
 
 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(char *root_path, 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);
 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_*/
index a8558a9..62d5588 100755 (executable)
@@ -22,6 +22,7 @@
 #include <glib.h>
 #include <sys/un.h>
 #include <arpa/inet.h>
+#include <iniparser.h>
 #include "media-common-types.h"
 #include "media-server-ipc.h"
 
 
 #define MAX_THUMB_REQUEST 100
 
-GMainLoop *
-ms_get_thumb_thread_mainloop(void);
-
-int
-ms_thumb_get_server_pid();
-
-void
-ms_thumb_reset_server_status();
-
-gpointer
-ms_thumb_agent_start_thread(gpointer data);
+GMainLoop * ms_get_thumb_thread_mainloop(void);
+int ms_thumb_get_server_pid();
+void ms_thumb_reset_server_status();
+gpointer ms_thumb_agent_start_thread(gpointer data);
+int ms_thumb_get_config();
 
 #endif /*_MEDIA_SERVER_THUMB_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 11dc2b1..0000000
+++ /dev/null
@@ -1,304 +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"
-
-#define CONFIG_PATH "/usr/etc/media-server-plugin"
-#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_ALLOCATE_MEMORY_FAIL;
-       }
-
-       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_ALLOCATE_MEMORY_FAIL;
-       }
-
-       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_ALLOCATE_MEMORY_FAIL;
-               }
-       }
-
-       /*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)
-{
-       int lib_index;
-       int ret;
-       char * err_msg = NULL;
-
-       MS_MALLOC(*handle, sizeof (void*) * lib_num);
-
-       for (lib_index = 0; lib_index < lib_num; lib_index++) {
-               ret = ((CONNECT)func_array[lib_index][eCONNECT])(&((*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_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)
-{
-       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, &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)
-{
-       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], &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;
-}
-
index 1b1b282..f2151e9 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of media db write.
- *
- * @file               media-server-db.c
- * @author     Haejeong Kim(backto.kim@samsung.com)
- * @version    1.0
- * @brief
- */
-
 #include <unistd.h>
 
 #include "media-util.h"
@@ -63,7 +54,7 @@ gboolean ms_db_thread(void *data)
        MediaDBHandle *db_handle = NULL;
 
        /* Connect Media DB*/
-       if(media_db_connect(&db_handle) != MS_MEDIA_ERR_NONE) {
+       if(media_db_connect(0, &db_handle, TRUE) != MS_MEDIA_ERR_NONE) {
                MS_DBG_ERR("Failed to connect DB");
                return FALSE;
        }
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..49044d1
--- /dev/null
@@ -0,0 +1,358 @@
+/*
+ *  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-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;
+       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_connect_db(&handle);
+
+       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);
+                               }
+                               scan_type = MS_SCAN_PART;
+                               ms_set_storage_validity(handle, storage_id, 1);
+                               if (ms_set_storage_scan_status(handle, storage_id, MEDIA_SCAN_PREPARE) != 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);
+                       }
+               } 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,};
+
+       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_connect_db(&handle);
+
+               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);
+                               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;
+
+       ret = ms_insert_storage(handle, MMC_STORAGE_ID, storage_name, storage_path);
+       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;
+
+       ret = ms_delete_storage(handle, MMC_STORAGE_ID, old_storage_name);
+       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);
+       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;
+
+       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;
+       }
+
+       ret = ms_connect_db(&db_handle);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_connect_db failed [%d]", ret);
+               ms_unload_functions();
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       ms_make_default_path_mmc();
+       ms_present_mmc_status(MS_SDCARD_INSERTED, 0);
+
+       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);
+
+       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);
+
+       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;
+
+       ret = ms_load_functions();
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_load_functions failed [%d]", ret);
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       ret = ms_connect_db(&handle);
+       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);
+
+               ms_send_storage_scan_request(storage_path, MMC_STORAGE_ID, MS_SCAN_INVALID);
+
+               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, int flags)
+{
+       /* 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, flags);
+               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(ms_block_info_s *block_info, void *user_data)
+{
+       if (block_info->block_type == 0) {
+               MS_DBG_ERR("GET THE USB EVENT");
+               _ms_usb_changed_event(block_info->mount_path, block_info->state);
+       } else {
+               MS_DBG_ERR("GET THE MMC EVENT");
+               _ms_mmc_changed_event(block_info->mount_path, block_info->state, block_info->flags);
+       }
+}
+
index 3b12d0f..ca8bb64 100755 (executable)
  *
  */
 
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-main.c
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
-
 #include <sys/wait.h>
 #include <sys/types.h>
 #include <dirent.h>
 #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"
+#include "media-server-device-block.h"
 
-#define APP_NAME "media-server"
-
-extern GMutex *scanner_mutex;
+extern GMutex scanner_mutex;
 
 GMainLoop *mainloop = NULL;
 bool power_off; /*If this is TRUE, poweroff notification received*/
@@ -58,65 +50,12 @@ 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)
+void _power_off_cb(ms_power_info_s *power_info, void* data)
 {
        MS_DBG_ERR("POWER OFF");
 
@@ -143,7 +82,7 @@ void _power_off_cb(void* data)
        return;
 }
 
-static void _db_clear(void)
+static void __db_clear(bool *need_full_scan)
 {
        int err = MS_MEDIA_ERR_NONE;
        void **handle = NULL;
@@ -160,13 +99,18 @@ static void _db_clear(void)
 
        /* check schema of db is chaged and need upgrade */
        MS_DBG_WARN("Check DB upgrade start");
-       if (ms_check_db_upgrade(handle)  != MS_MEDIA_ERR_NONE)
+       if (ms_check_db_upgrade(handle, need_full_scan)  != MS_MEDIA_ERR_NONE)
                MS_DBG_ERR("ms_check_db_upgrade fail");
        MS_DBG_WARN("Check DB upgrade end");
 
+       /*update just sd card item's valid type*/
+       if (ms_validaty_change_all_items(handle, MMC_STORAGE_ID, MS_STORAGE_EXTERNAL, 0)  != MS_MEDIA_ERR_NONE)
+               MS_DBG_ERR("ms_validaty_change_all_items fail");
+
        /*update just valid type*/
-       if (ms_invalidate_all_items(handle, MS_STORAGE_EXTERNAL)  != MS_MEDIA_ERR_NONE)
-               MS_DBG_ERR("ms_change_valid_type fail");
+       if (ms_set_all_storage_validity(handle, 0)  != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_set_all_storage_validity fail");
+       }
 
        /*disconnect form media db*/
        if (handle) ms_disconnect_db(&handle);
@@ -194,127 +138,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*/
-       if (!scanner_mutex) scanner_mutex = g_mutex_new();
+       g_mutex_init(&scanner_mutex);
 }
 
-static void _ms_free_global_variable(void)
+static void __ms_free_global_variable(void)
 {
        /*Clear mutex variable*/
-       if (scanner_mutex) g_mutex_free(scanner_mutex);
-}
-
-void
-_ms_mmc_vconf_cb(keynode_t *key, 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_ERR("CURRENT STATUS OF SD CARD[%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(MEDIA_ROOT_PATH_SDCARD, 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(MEDIA_ROOT_PATH_SDCARD, ms_get_mmc_state());
-       }
-
-       return;
+       g_mutex_clear(&scanner_mutex);
 }
 
-void
-_ms_change_lang_vconf_cb(keynode_t *key, void* data)
+void _ms_change_lang_vconf_cb(keynode_t *key, void* data)
 {
-       char lang[100] = {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);
 
-       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(NULL, MS_SCAN_META);
+       if (need_update == TRUE) {
+               ms_send_storage_scan_request(NULL, INTERNAL_STORAGE_ID, MS_SCAN_META);  /*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;
-}
 
-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 (MS_STRING_VALID(lang))
+               priv_lang = strdup(lang);
 
-       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;
 }
@@ -324,25 +213,13 @@ int main(int argc, char **argv)
        GThread *db_thread = NULL;
        GThread *thumb_thread = NULL;
        GIOChannel *channel = NULL;
-       bool check_result = false;
 
        power_off = FALSE;
 
-       _ms_check_pw_status();
-
-       check_result = check_process();
-       if (check_result == false) {
-               goto EXIT;
-       }
-
-       if (!g_thread_supported()) {
-               g_thread_init(NULL);
-       }
-
        /*Init main loop*/
        mainloop = g_main_loop_new(NULL, FALSE);
 
-       _ms_new_global_variable();
+       __ms_new_global_variable();
 
        __ms_add_requst_receiver(mainloop, &channel);
 
@@ -367,6 +244,9 @@ int main(int argc, char **argv)
        /*Active flush */
        malloc_trim(0);
 
+       /*Read ini file */
+       ms_thumb_get_config();
+
        MS_DBG_ERR("*** Media Server is running ***");
 
        g_main_loop_run(mainloop);
@@ -374,12 +254,10 @@ int main(int argc, char **argv)
        g_thread_join(db_thread);
        g_thread_join(thumb_thread);
 
-       _ms_free_global_variable();
+       __ms_free_global_variable();
 
        MS_DBG_ERR("*** Media Server is stopped ***");
 
-EXIT:
-
        return 0;
 }
 
@@ -392,8 +270,7 @@ static void __ms_add_requst_receiver(GMainLoop *mainloop, GIOChannel **channel)
 
        /*prepare socket*/
        /* Create and bind new UDP socket */
-       if (ms_ipc_create_server_socket(MS_PROTOCOL_TCP, MS_SCANNER_PORT, &sockfd)
-               != MS_MEDIA_ERR_NONE) {
+       if (ms_ipc_create_server_socket(MS_PROTOCOL_TCP, MS_SCANNER_PORT, &sockfd) != MS_MEDIA_ERR_NONE) {
                MS_DBG_ERR("Failed to create socket");
                return;
        } else {
@@ -428,34 +305,36 @@ static void __ms_remove_requst_receiver(GIOChannel *channel)
 
 static void __ms_add_event_receiver(GIOChannel *channel)
 {
-       int err;
-       char lang[100] = {0,};
+       int err = 0;
+       char *lang = NULL;
 
        /*set power off callback function*/
-       ms_add_poweoff_event_receiver(_power_off_cb,channel);
-
-       /*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);
+       ms_sys_set_poweroff_cb(_power_off_cb,channel);
+       ms_sys_set_device_block_event_cb(ms_device_block_changed_cb, NULL);
 
-       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);
 
-       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);
+               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);
+       }
 
+       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)
@@ -470,23 +349,88 @@ static void __ms_add_signal_handler(void)
 
        if (sigaction(SIGCHLD, &sigset, NULL) < 0) {
                MS_DBG_STRERROR("sigaction failed");
-       } 
+       }
 
        signal(SIGPIPE,SIG_IGN);
 }
+
 static void __ms_check_mediadb(void)
 {
-       _db_clear();
+       bool need_full_scan = false;
 
-       /* update internal storage */
-       ms_send_storage_scan_request(MEDIA_ROOT_PATH_INTERNAL, MS_SCAN_PART);
+       __db_clear(&need_full_scan);
 
+       /* update internal storage */
+       if(need_full_scan) {
+               ms_send_storage_scan_request(MEDIA_ROOT_PATH_INTERNAL, INTERNAL_STORAGE_ID, MS_SCAN_ALL);
+       } else {
+               ms_send_storage_scan_request(MEDIA_ROOT_PATH_INTERNAL, INTERNAL_STORAGE_ID, MS_SCAN_PART);
+       }
        /* 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();
+}
 
-               ms_send_storage_scan_request(MEDIA_ROOT_PATH_SDCARD, ms_get_mmc_state());
+
+////////////////////////////////////////////////////////////////////
+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_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 21dd923..26304b1 100755 (executable)
@@ -26,6 +26,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <iniparser.h>
 
 #include "media-util.h"
 #include "media-server-ipc.h"
 
 extern GMainLoop *mainloop;
 extern GArray *owner_list;
-GMutex *scanner_mutex;
+GMutex scanner_mutex;
 
 static bool scanner_ready;
 static int alarm_id;
 static int child_pid;
 static int receive_id;
 
+int fifo_fd;
+
 static int _ms_check_remain_task(void)
 {
        int remain_task;
@@ -75,20 +78,20 @@ static gboolean _ms_stop_scanner (gpointer user_data)
 {
        int task_num = MS_NO_REMAIN_TASK;
 
-       g_mutex_lock(scanner_mutex);
+       g_mutex_lock(&scanner_mutex);
 
        /* check status of scanner */
        /* If some task remain or scanner is running, scanner must not stop*/
        task_num = _ms_check_remain_task();
        if (task_num != MS_NO_REMAIN_TASK) {
                MS_DBG("[%d] task(s) remains", task_num);
-               g_mutex_unlock(scanner_mutex);
+               g_mutex_unlock(&scanner_mutex);
                return TRUE;
        }
 
        if (ms_check_scanning_status() == MS_DB_UPDATING) {
                MS_DBG("DB is updating");
-               g_mutex_unlock(scanner_mutex);
+               g_mutex_unlock(&scanner_mutex);
                return TRUE;
        } else {
                MS_DBG("DB updating is not working");
@@ -98,7 +101,7 @@ static gboolean _ms_stop_scanner (gpointer user_data)
        if (child_pid >0 ) {
                if (kill(child_pid, SIGKILL) < 0) {
                        MS_DBG_STRERROR("kill failed");
-                       g_mutex_unlock(scanner_mutex);
+                       g_mutex_unlock(&scanner_mutex);
                        return TRUE;
                }
        }
@@ -122,10 +125,51 @@ static gboolean _ms_stop_scanner (gpointer user_data)
        return FALSE;
 }
 
+static int _ms_get_ini_config(const char *key)
+{
+       dictionary *dict = NULL;
+       int value = 0;
+
+       dict = iniparser_load(MS_INI_DEFAULT_PATH);
+       if(!dict) {
+               MS_DBG_ERR("%s load failed", MS_INI_DEFAULT_PATH);
+               return 0;
+       }
+
+       value = iniparser_getint(dict, key, 0);
+       MS_DBG("Key = [%s], Value = [%d]", key, value);
+
+       iniparser_freedict(dict);
+
+       return value;
+}
+
+void ms_cleanup_scanner(void)
+{
+       g_mutex_lock(&scanner_mutex);
+       MS_DBG_ERR("_ms_cleanup_scanner START");
+
+       close(fifo_fd);
+       MS_DBG_ERR("close fd[%d]", fifo_fd);
+
+       unlink(MS_SCANNER_FIFO_PATH_RES);
+       unlink(MS_SCANNER_FIFO_PATH_REQ);
+
+       g_source_destroy(g_main_context_find_source_by_id(g_main_loop_get_context (mainloop), alarm_id));
+       g_source_destroy(g_main_context_find_source_by_id(g_main_loop_get_context (mainloop), receive_id));
+
+       g_mutex_unlock(&scanner_mutex);
+
+       MS_DBG_ERR("_ms_cleanup_scanner END");
+
+       return;
+}
+
 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);
@@ -133,22 +177,21 @@ static void _ms_add_timeout(guint interval, GSourceFunc func, gpointer data)
        g_source_unref(src);
 }
 
-int
-ms_scanner_start(void)
+int ms_scanner_start(void)
 {
        int pid;
 
-       g_mutex_lock(scanner_mutex);
+       g_mutex_lock(&scanner_mutex);
 
        if (child_pid > 0) {
                MS_DBG_ERR("media scanner is already started");
-               g_mutex_unlock(scanner_mutex);
+               g_mutex_unlock(&scanner_mutex);
                return MS_MEDIA_ERR_NONE;
        }
 
        if((pid = fork()) < 0) {
                MS_DBG_ERR("Fork error\n");
-               g_mutex_unlock(scanner_mutex);
+               g_mutex_unlock(&scanner_mutex);
        } else if (pid > 0) {
                /* parent process */
                /* wait until scanner is ready*/
@@ -160,12 +203,12 @@ 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) {
                        MS_DBG_STRERROR("mkfifo failed");
-                       return MS_MEDIA_ERR_FIFO_MAKE_FAIL;
+                       return MS_MEDIA_ERR_INTERNAL;
                }
 
                fd = open(MS_SCANNER_FIFO_PATH_RES, O_RDWR);
@@ -188,7 +231,8 @@ 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 */
@@ -212,16 +256,23 @@ ms_scanner_start(void)
                        close(fd);
                }
 
-               g_mutex_unlock(scanner_mutex);
+               fifo_fd = fd;
+
+               g_mutex_unlock(&scanner_mutex);
 
                return ret;
                /* attach socket receive message callback */
        } else if(pid == 0) {
                /* child process */
-               MS_DBG_ERR("CHILD PROCESS");
-               MS_DBG("EXECUTE MEDIA SCANNER");
-               execl("/usr/bin/media-scanner", "media-scanner", NULL);
-               g_mutex_unlock(scanner_mutex);
+               MS_DBG_ERR("[No-Error] CHILD PROCESS");
+               MS_DBG("[No-Error] EXECUTE MEDIA SCANNER");
+
+               if(_ms_get_ini_config("media-content-config:scanner_type") == 1)
+                       execl("/usr/bin/media-scanner-v2", "media-scanner-v2", NULL);
+               else
+                       execl("/usr/bin/media-scanner", "media-scanner", NULL);
+
+               g_mutex_unlock(&scanner_mutex);
        }
 
        return MS_MEDIA_ERR_NONE;
@@ -234,10 +285,17 @@ 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;
 
-       g_mutex_unlock(scanner_mutex);
+       g_mutex_unlock(&scanner_mutex);
 }
 
 int ms_get_scanner_pid(void)
index 03d1245..c347830 100755 (executable)
  * limitations under the License.
  *
  */
-
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file               media-server-thumb.c
- * @author     Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version    1.0
- * @brief
- */
+#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>
 #include <malloc.h>
 #include <vconf.h>
-#include <security-server.h>
 
 #include "media-util.h"
 #include "media-util-internal.h"
 #include "media-server-ipc.h"
 #include "media-common-utils.h"
+#include "media-common-db-svc.h"
+#include "media-common-security.h"
 #include "media-server-dbg.h"
-#include "media-server-db-svc.h"
 #include "media-server-scanner.h"
 #include "media-server-socket.h"
 
 extern GAsyncQueue *scan_queue;
 GAsyncQueue* ret_queue;
 GArray *owner_list;
-extern GMutex *scanner_mutex;
+GMutex scanner_mutex;
 gint cur_running_task;
 
 extern bool power_off;
@@ -59,41 +52,57 @@ typedef struct ms_req_owner_data
        int pid;
        int index;
        int client_sockfd;
+       char *req_path;
 }ms_req_owner_data;
 
-static int __ms_add_owner(int pid, int client_sock)
+static int __ms_add_owner(int pid, int client_sock, char *path)
 {
        if (pid != 0) {
                ms_req_owner_data *owner_data = NULL;
+               int len = strlen(path);
 
-               /* If owner list is NULL, create it */
-               /* pid and client address are stored in ower list */
-               /* These are used for sending result of scanning */
-               if (owner_list == NULL) {
-                       /*create array for processing overlay data*/
-                       owner_list = g_array_new (FALSE, FALSE, sizeof (ms_req_owner_data *));
+               if (len > 0) {
+                       /* If owner list is NULL, create it */
+                       /* pid and client address are stored in ower list */
+                       /* These are used for sending result of scanning */
                        if (owner_list == NULL) {
-                               MS_DBG_ERR("g_array_new error");
-                               return MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
+                               /*create array for processing overlay data*/
+                               owner_list = g_array_new (FALSE, FALSE, sizeof (ms_req_owner_data *));
+                               if (owner_list == NULL) {
+                                       MS_DBG_ERR("g_array_new error");
+                                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
+                               }
                        }
-               }
 
-               /* store pid and client address */
-               MS_MALLOC(owner_data, sizeof(ms_req_owner_data));
-               owner_data->pid = pid;
-               owner_data->client_sockfd = client_sock;
-       //      MS_DBG("the length of array : %d", owner_list->len);
-       //      MS_DBG("pid : %d", owner_data->pid);
-       //      MS_DBG("client_addr : %p", owner_data->client_addr);
+                       /* store pid and client address */
+                       MS_MALLOC(owner_data, sizeof(ms_req_owner_data));
 
-               owner_data->index = -1;
-               g_array_append_val(owner_list, owner_data);
+                       if (owner_data == NULL) {
+                               MS_DBG_ERR("MS_MALLOC failed");
+                               g_array_free (owner_list, FALSE);
+                               owner_list = NULL;
+                               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+                       }
+
+                       owner_data->pid = pid;
+                       owner_data->client_sockfd = client_sock;
+
+                       if (path[len -1] == '/')
+                               path[len -1] = '\0';
+                       owner_data->req_path = strdup(path);
+               //      MS_DBG("the length of array : %d", owner_list->len);
+               //      MS_DBG("pid : %d", owner_data->pid);
+               //      MS_DBG("client_addr : %p", owner_data->client_addr);
+
+                       owner_data->index = -1;
+                       g_array_append_val(owner_list, owner_data);
+               }
        }
 
        return MS_MEDIA_ERR_NONE;
 }
 
-static int __ms_find_owner(int pid, ms_req_owner_data **owner_data)
+static int __ms_find_owner(int pid, const char *req_path, ms_req_owner_data **owner_data)
 {
        int i;
        int len = owner_list->len;
@@ -106,7 +115,8 @@ static int __ms_find_owner(int pid, ms_req_owner_data **owner_data)
        for (i=0; i < len; i++) {
                data = g_array_index(owner_list, ms_req_owner_data*, i);
                MS_DBG("%d %d", data->pid, pid);
-               if (data->pid == pid) {
+               MS_DBG("%s %s", data->req_path, req_path);
+               if (data->pid == pid && (strcmp(data->req_path, req_path) == 0)) {
                        data->index = i;
                        *owner_data = data;
                        MS_DBG("FIND OWNER");
@@ -121,6 +131,7 @@ static int __ms_delete_owner(ms_req_owner_data *owner_data)
 {
        if (owner_data->index != -1) {
                g_array_remove_index(owner_list, owner_data->index);
+               MS_SAFE_FREE(owner_data->req_path);
                MS_SAFE_FREE(owner_data);
                MS_DBG("DELETE OWNER");
        }
@@ -130,13 +141,23 @@ static int __ms_delete_owner(ms_req_owner_data *owner_data)
 
 static int __ms_send_result_to_client(int pid, ms_comm_msg_s *recv_msg)
 {
+       if(strlen(recv_msg->msg) == 0) {
+               MS_DBG_ERR("msg is NULL");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
        if (owner_list != NULL) {
                /* If the owner of result message is not media-server, media-server notify to the owner */
                /* The owner of message is distingushied by pid in received message*/
                /* find owner data */
                ms_req_owner_data *owner_data = NULL;
+               char *res_path = strdup(recv_msg->msg);
+               if(res_path  == NULL) {
+                       MS_DBG_ERR("res_path is NULL");
+                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
+               }
 
-               __ms_find_owner(pid, &owner_data);
+               __ms_find_owner(pid, res_path, &owner_data);
                if (owner_data != NULL) {
                        MS_DBG("PID : %d", owner_data->pid);
                        /* owner data exists */
@@ -144,10 +165,13 @@ static int __ms_send_result_to_client(int pid, ms_comm_msg_s *recv_msg)
                        ms_ipc_send_msg_to_client_tcp(owner_data->client_sockfd, recv_msg, NULL);
                        close(owner_data->client_sockfd);
 
+                       MS_SAFE_FREE(res_path);
+
                        /* free owner data*/
                        __ms_delete_owner(owner_data);
                } else {
                        MS_DBG_ERR("Not found Owner");
+                       MS_SAFE_FREE(res_path);
                        return MS_MEDIA_ERR_INTERNAL;
                }
        } else {
@@ -170,13 +194,13 @@ static int __ms_privilege_check(const char *msg, gboolean *privilege)
        int t_idx = 0;
        gboolean is_privilege = TRUE;
 
-       char *operation[operation_cnt] = {
+       const char *operation[operation_cnt] = {
                "INSERT INTO ",
                "DELETE FROM ",
                "UPDATE "
        };
 
-       char *db_table[db_table_cnt] = {
+       const char *db_table[db_table_cnt] = {
                "playlist_map",
                "playlist",
                "tag_map",
@@ -207,27 +231,28 @@ static int __ms_privilege_check(const char *msg, gboolean *privilege)
        return MS_MEDIA_ERR_NONE;
 }
 
-static int __ms_privilege_ask(int client_sockfd)
+static int __ms_get_peer_pid(int sockfd)
 {
-       int ret = 0;
-       int res = MS_MEDIA_ERR_NONE;
+       struct ucred uc;
+       socklen_t uc_len = sizeof(uc);
 
-       ret = security_server_check_privilege_by_sockfd(client_sockfd, "media-data::db", "w");
-       if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
-               MS_DBG_ERR("You do not have permission for this operation.");
-               res = MS_MEDIA_ERR_PERMISSION_DENIED;
-       } else {
-               MS_DBG("PERMISSION OK");
+       if (getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &uc, &uc_len) < 0) {
+               MS_DBG_STRERROR("Credential failed");
+               return -1;
        }
 
-       return res;
+       MS_DBG_ERR("Credential pid = %d\n", uc.pid);
+
+       return uc.pid;
 }
+
 gboolean ms_read_socket(gpointer user_data)
 {
        ms_comm_msg_s recv_msg;
        int sockfd = MS_SOCK_NOT_ALLOCATE;
        int ret;
        int res;
+       int pid;
        int req_num = -1;
        int client_sock = -1;
        GIOChannel *src = user_data;
@@ -250,7 +275,7 @@ gboolean ms_read_socket(gpointer user_data)
                goto ERROR;
        }
 
-       ret = __ms_privilege_ask(client_sock);
+       ret = ms_privilege_ask(client_sock);
        if (ret == MS_MEDIA_ERR_PERMISSION_DENIED) {
                res = MS_MEDIA_ERR_PERMISSION_DENIED;
                goto ERROR;
@@ -258,17 +283,30 @@ gboolean ms_read_socket(gpointer user_data)
 
        /* copy received data */
        req_num = recv_msg.msg_type;
+       pid = recv_msg.pid;
 
        /* register file request
          * media server inserts the meta data of one file into media db */
        if (req_num == MS_MSG_DIRECTORY_SCANNING
                ||req_num == MS_MSG_BULK_INSERT
                ||req_num == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE
-               || req_num == MS_MSG_BURSTSHOT_INSERT) {
+               || req_num == MS_MSG_BURSTSHOT_INSERT
+               || req_num == MS_MSG_DIRECTORY_SCANNING_CANCEL) {
+
+               if (req_num == MS_MSG_BULK_INSERT
+                       ||req_num == MS_MSG_BURSTSHOT_INSERT ) {
+                       /*check pid by client sock fd*/
+                       pid = __ms_get_peer_pid(client_sock);
+                       recv_msg.pid = pid;
+               }
+
                if ((ret = ms_send_scan_request(&recv_msg, client_sock)) != MS_MEDIA_ERR_NONE) {
                        res = ret;
                        goto ERROR;
                }
+
+               if (req_num == MS_MSG_DIRECTORY_SCANNING_CANCEL)
+                       ms_remove_request_owner(pid, recv_msg.msg);
        } else {
                /* NEED IMPLEMENTATION */
                close(client_sock);
@@ -293,6 +331,8 @@ ERROR:
                        case MS_MSG_BURSTSHOT_INSERT:
                                res_msg.msg_type = MS_MSG_SCANNER_BULK_RESULT;
                                break;
+                       default :
+                               break;
                }
 
                res_msg.result = res;
@@ -335,16 +375,16 @@ int ms_send_scan_request(ms_comm_msg_s *send_msg, int client_sock)
        int err =  MS_MEDIA_ERR_NONE;
        int pid = send_msg->pid;
 
-       g_mutex_lock(scanner_mutex);
+       g_mutex_lock(&scanner_mutex);
 
        if (ms_get_scanner_status()) {
                MS_DBG_WARN("Scanner is ready");
                __ms_send_request(send_msg);
 
-               g_mutex_unlock(scanner_mutex);
+               g_mutex_unlock(&scanner_mutex);
        } else {
                MS_DBG_WARN("Scanner starts");
-               g_mutex_unlock(scanner_mutex);
+               g_mutex_unlock(&scanner_mutex);
 
                err = ms_scanner_start();
                if(err == MS_MEDIA_ERR_NONE) {
@@ -358,9 +398,9 @@ int ms_send_scan_request(ms_comm_msg_s *send_msg, int client_sock)
                }
        }
 
-       if (res == MS_MEDIA_ERR_NONE) {
+       if ((res == MS_MEDIA_ERR_NONE) && (send_msg->msg_type != MS_MSG_DIRECTORY_SCANNING_CANCEL)) {
                /* this request process in media scanner */
-               if ((err = __ms_add_owner(pid, client_sock)) != MS_MEDIA_ERR_NONE) {
+               if ((err = __ms_add_owner(pid, client_sock, send_msg->msg)) != MS_MEDIA_ERR_NONE) {
                        MS_DBG_ERR("__ms_add_owner failed. %d", err);
                        res = err;
                }
@@ -369,7 +409,7 @@ int ms_send_scan_request(ms_comm_msg_s *send_msg, int client_sock)
        return res;
 }
 
-int ms_send_storage_scan_request(char *root_path, 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)
 {
        int ret = MS_MEDIA_ERR_NONE;
        ms_comm_msg_s scan_msg = {
@@ -407,6 +447,10 @@ int ms_send_storage_scan_request(char *root_path, ms_dir_scan_type_t scan_type)
                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);
+       }
+
        ret = ms_send_scan_request(&scan_msg, -1);
 
 ERROR:
@@ -457,7 +501,7 @@ gboolean ms_read_db_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer
        }
 
        if (privilege == TRUE) {
-               ret = __ms_privilege_ask(client_sock);
+               ret = ms_privilege_ask(client_sock);
                if (ret == MS_MEDIA_ERR_PERMISSION_DENIED) {
                        send_msg = MS_MEDIA_ERR_PERMISSION_DENIED;
                        goto ERROR;
@@ -473,7 +517,7 @@ gboolean ms_read_db_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer
                send_msg = ret;
                MS_SAFE_FREE(sql_query);
        } else {
-               send_msg = MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
+               send_msg = MS_MEDIA_ERR_OUT_OF_MEMORY;
        }
 
 ERROR:
@@ -503,7 +547,7 @@ void _ms_process_tcp_message(gpointer data,  gpointer user_data)
        memset((void *)&recv_msg, 0, sizeof(ms_comm_msg_s));
 
        /* Connect Media DB*/
-       if(media_db_connect(&db_handle) != MS_MEDIA_ERR_NONE) {
+       if(media_db_connect(0, &db_handle, TRUE) != MS_MEDIA_ERR_NONE) {
                MS_DBG_ERR("Failed to connect DB");
                send_msg = MS_MEDIA_ERR_DB_CONNECT_FAIL;
                goto ERROR;
@@ -573,7 +617,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;
@@ -594,7 +638,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;
@@ -630,7 +674,7 @@ gboolean ms_read_db_tcp_batch_socket(GIOChannel *src, GIOCondition condition, gp
                MS_DBG_SLOG("Create New Thread Pool %d", client_sock);
                gtp = g_thread_pool_new((GFunc)_ms_process_tcp_message, NULL, MAX_THREAD_NUM, TRUE, &error);
                if (error != NULL) {
-                       res = MS_MEDIA_ERR_ALLOCATE_MEMORY_FAIL;
+                       res = MS_MEDIA_ERR_OUT_OF_MEMORY;
                        goto ERROR;
                }
        }
@@ -721,3 +765,76 @@ gboolean ms_receive_message_from_scanner(GIOChannel *src, GIOCondition condition
        return TRUE;
 }
 
+int ms_remove_request_owner(int pid, const char *req_path)
+{
+       ms_req_owner_data *owner_data = NULL;
+
+       __ms_find_owner(pid, req_path, &owner_data);
+       if (owner_data != NULL) {
+               MS_DBG("PID : %d", owner_data->pid);
+
+               close(owner_data->client_sockfd);
+               /* free owner data*/
+               __ms_delete_owner(owner_data);
+       } else {
+               MS_DBG_ERR("Not found Owner");
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       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;
+}
+
+
index ce95de7..c159316 100755 (executable)
 #define LOG_TAG "MEDIA_SERVER_THUMB"
 
 #define THUMB_SERVER_NAME "media-thumbnail"
-
+#define MS_SOCK_BLOCK_SIZE 512
+#define MS_INI_GET_INT(dict, key, value, default) \
+       do { \
+               value = iniparser_getint(dict, key, default); \
+               MS_DBG("get %s = %d", key, value); \
+       } while(0)
 
 static GMainLoop *g_thumb_agent_loop = NULL;
 static GIOChannel *g_udp_channel = NULL;
@@ -44,7 +49,8 @@ static gboolean g_shutdowning_thumb_server = FALSE;
 static gboolean g_thumb_server_queued_all_extracting_request = FALSE;
 static int g_communicate_sock = 0;
 static int g_timer_id = 0;
-static int g_server_pid = 0;
+static int g_thumb_server_pid = 0;
+static int g_thumb_server_active = -1;
 
 static GQueue *g_request_queue = NULL;
 static int g_queue_work = 0;
@@ -56,9 +62,7 @@ typedef struct {
 
 extern char MEDIA_IPC_PATH[][50];
 
-int _ms_thumb_create_socket(int sock_type, int *sock);
-int _ms_thumb_create_udp_socket(int *sock);
-int _ms_thumb_recv_msg(int sock, int header_size, thumbMsg *msg);
+int _ms_thumb_recv_msg(int sock, thumbMsg *msg);
 int _ms_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sockaddr_un *from_addr, unsigned int *from_size);
 int _ms_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size);
 gboolean _ms_thumb_check_queued_request(gpointer data);
@@ -68,14 +72,14 @@ gboolean _ms_thumb_agent_timer();
 
 gboolean _ms_thumb_agent_start_jobs(gpointer data)
 {
-       MS_DBG("");
+       MS_DBG_FENTER();
 
        return FALSE;
 }
 
 void _ms_thumb_agent_finish_jobs()
 {
-       MS_DBG("");
+       MS_DBG_FENTER();
 
        return;
 }
@@ -87,7 +91,7 @@ GMainLoop* ms_get_thumb_thread_mainloop(void)
 
 int ms_thumb_get_server_pid()
 {
-       return g_server_pid;
+       return g_thumb_server_pid;
 }
 
 void ms_thumb_reset_server_status()
@@ -104,10 +108,10 @@ void ms_thumb_reset_server_status()
                /* Need to inplement when crash happens */
                MS_DBG_ERR("Thumbnail server is dead when processing all-thumbs extraction");
                g_thumb_server_extracting = FALSE;
-               g_server_pid = 0;
+               g_thumb_server_pid = 0;
        } else {
                g_thumb_server_extracting = FALSE;
-               g_server_pid = 0;
+               g_thumb_server_pid = 0;
        }
 
        return;
@@ -127,11 +131,13 @@ void _ms_thumb_create_timer(int id)
 /* This checks if thumbnail server is running */
 bool _ms_thumb_check_process()
 {
+#if 0
        DIR *pdir;
        struct dirent pinfo;
        struct dirent *result = NULL;
+#endif
        bool ret = FALSE;
-
+#if 0
        pdir = opendir("/proc");
        if (pdir == NULL) {
                MS_DBG_ERR("err: NO_DIR");
@@ -167,67 +173,10 @@ bool _ms_thumb_check_process()
        }
 
        closedir(pdir);
-
+#endif
        return ret;
 }
 
-int _ms_thumb_create_socket(int sock_type, int *sock)
-{
-       int sock_fd = 0;
-
-       if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
-               MS_DBG_STRERROR("socket failed");
-               return MS_MEDIA_ERR_SOCKET_CONN;
-       }
-
-       if (sock_type == CLIENT_SOCKET) {
-
-               struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
-
-               if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
-                       MS_DBG_STRERROR("setsockopt failed");
-                       close(sock_fd);
-                       return MS_MEDIA_ERR_SOCKET_INTERNAL;
-               }
-       } else if (sock_type == SERVER_SOCKET) {
-
-               int n_reuse = 1;
-
-               if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &n_reuse, sizeof(n_reuse)) == -1) {
-                       MS_DBG_STRERROR("setsockopt failed: %s");
-                       close(sock_fd);
-                       return MS_MEDIA_ERR_SOCKET_INTERNAL;
-               }
-       }
-
-       *sock = sock_fd;
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-
-int _ms_thumb_create_udp_socket(int *sock)
-{
-       int sock_fd = 0;
-
-       if ((sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
-               MS_DBG_STRERROR("socket failed");
-               return MS_MEDIA_ERR_SOCKET_CONN;
-       }
-
-       struct timeval tv_timeout = { MS_TIMEOUT_SEC_10, 0 };
-
-       if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &tv_timeout, sizeof(tv_timeout)) == -1) {
-               MS_DBG_STRERROR("setsockopt failed");
-               close(sock_fd);
-               return MS_MEDIA_ERR_SOCKET_INTERNAL;
-       }
-
-       *sock = sock_fd;
-
-       return MS_MEDIA_ERR_NONE;
-}
-
 int _media_thumb_get_error()
 {
        if (errno == EWOULDBLOCK) {
@@ -244,21 +193,28 @@ int _media_thumb_get_error()
        }
 }
 
-int _ms_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
+int _ms_thumb_recv_msg(int sock, thumbMsg *msg)
 {
        int recv_msg_len = 0;
        unsigned char *buf = NULL;
+       unsigned int header_size = 0;
 
-       buf = (unsigned char*)malloc(header_size);
+       header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
+
+       MS_MALLOC(buf, header_size);
+       if(buf == NULL) {
+               MS_DBG_STRERROR("malloc failed");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
 
        if ((recv_msg_len = recv(sock, buf, header_size, 0)) < 0) {
                MS_DBG_STRERROR("recv failed");
                MS_SAFE_FREE(buf);
                return _media_thumb_get_error();
        }
-
        memcpy(msg, buf, header_size);
-       //MS_DBG("origin_path_size : %d, dest_path_size : %d", msg->origin_path_size, msg->dest_path_size);
+
+       MS_DBG("origin_path_size : %d, dest_path_size : %d, thumb_size : %d", msg->origin_path_size, msg->dest_path_size, msg->thumb_size);
 
        MS_SAFE_FREE(buf);
 
@@ -268,16 +224,18 @@ int _ms_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
                return MS_MEDIA_ERR_DATA_TAINTED;
        }
 
-       buf = (unsigned char*)malloc(msg->origin_path_size);
+       MS_MALLOC(buf, (unsigned int)(msg->origin_path_size));
+       if(buf == NULL) {
+               MS_DBG_STRERROR("malloc failed");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
 
        if ((recv_msg_len = recv(sock, buf, msg->origin_path_size, 0)) < 0) {
                MS_DBG_STRERROR("recv failed");
                MS_SAFE_FREE(buf);
                return _media_thumb_get_error();
        }
-
        strncpy(msg->org_path, (char*)buf, msg->origin_path_size);
-       //MS_DBG("original path : %s", msg->org_path);
 
        MS_SAFE_FREE(buf);
 
@@ -287,18 +245,52 @@ int _ms_thumb_recv_msg(int sock, int header_size, thumbMsg *msg)
                return MS_MEDIA_ERR_DATA_TAINTED;
        }
 
-       buf = (unsigned char*)malloc(msg->dest_path_size);
+       MS_MALLOC(buf, (unsigned int)(msg->dest_path_size));
+       if(buf == NULL) {
+               MS_DBG_ERR("malloc failed");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
 
        if ((recv_msg_len = recv(sock, buf, msg->dest_path_size, 0)) < 0) {
                MS_DBG_ERR("recv failed : %s");
                MS_SAFE_FREE(buf);
                return _media_thumb_get_error();
        }
-
        strncpy(msg->dst_path, (char*)buf, msg->dest_path_size);
-       //MS_DBG("destination path : %s", msg->dst_path);
 
        MS_SAFE_FREE(buf);
+
+       if (msg->thumb_size < 0) {
+               MS_SAFE_FREE(buf);
+               MS_DBG_ERR("msg->thumb_size is invalid %d", msg->thumb_size );
+               return MS_MEDIA_ERR_DATA_TAINTED;
+       }
+
+       if(msg->thumb_size > 0) {
+               MS_MALLOC(buf, (unsigned int)(msg->thumb_size));
+               if(buf == NULL) {
+                       MS_DBG_ERR("malloc failed");
+                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
+               }
+
+               if ((recv_msg_len = recv(sock, buf, msg->thumb_size, 0)) < 0) {
+                       MS_DBG_STRERROR("recv failed");
+                       MS_SAFE_FREE(buf);
+                       return _media_thumb_get_error();
+               }
+
+               MS_SAFE_FREE(msg->thumb_data);
+               MS_MALLOC(msg->thumb_data, (unsigned int)(msg->thumb_size));
+               if(msg->thumb_data == NULL) {
+                       MS_DBG_ERR("malloc failed");
+                       MS_SAFE_FREE(buf);
+                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
+               }
+               memcpy(msg->thumb_data, buf, msg->thumb_size);
+
+               MS_SAFE_FREE(buf);
+       }
+
        return MS_MEDIA_ERR_NONE;
 }
 
@@ -308,7 +300,11 @@ int _ms_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sock
        unsigned int from_addr_size = sizeof(struct sockaddr_un);
        unsigned char *buf = NULL;
 
-       buf = (unsigned char*)malloc(sizeof(thumbMsg));
+       MS_MALLOC(buf, sizeof(thumbMsg));
+       if(buf == NULL) {
+               MS_DBG_ERR("malloc failed");
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;
+       }
 
        recv_msg_len = ms_ipc_wait_message(sock, buf, sizeof(thumbMsg), from_addr, &from_addr_size);
        if (recv_msg_len != MS_MEDIA_ERR_NONE) {
@@ -322,12 +318,11 @@ int _ms_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sock
 
        if (msg->origin_path_size <= 0  || msg->origin_path_size > MS_FILE_PATH_LEN_MAX) {
                MS_SAFE_FREE(buf);
-               MS_DBG_ERR("msg->origin_path_size is invalid %d", msg->origin_path_size );
+               MS_DBG_ERR("msg->origin_path_size is invalid %d", msg->origin_path_size);
                return MS_MEDIA_ERR_DATA_TAINTED;
        }
 
-       strncpy(msg->org_path, (char*)buf + header_size, msg->origin_path_size);
-       //MS_DBG("original path : %s", msg->org_path);
+       strncpy(msg->org_path, (char *)buf + header_size, msg->origin_path_size);
 
        if (msg->dest_path_size <= 0  || msg->dest_path_size > MS_FILE_PATH_LEN_MAX) {
                MS_SAFE_FREE(buf);
@@ -336,9 +331,60 @@ int _ms_thumb_recv_udp_msg(int sock, int header_size, thumbMsg *msg, struct sock
        }
 
        strncpy(msg->dst_path, (char*)buf + header_size + msg->origin_path_size, msg->dest_path_size);
-       //MS_DBG("destination path : %s", msg->dst_path);
 
        MS_SAFE_FREE(buf);
+
+       //Additional data
+       if(msg->msg_type == 10 && msg->thumb_size > 0) { //THUMB_RESPONSE_RAW_DATA
+               thumbRawAddMsg *thumbaddmsg = NULL;
+               MS_MALLOC(thumbaddmsg, sizeof(thumbRawAddMsg));
+               if(thumbaddmsg == NULL) {
+                       MS_DBG_ERR("malloc failed");
+                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
+               }
+
+               MS_MALLOC(buf, (unsigned int)(msg->thumb_size));
+               if(buf == NULL) {
+                       MS_DBG_ERR("malloc failed");
+                       MS_SAFE_FREE(thumbaddmsg);
+                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
+               }
+
+               recv_msg_len = ms_ipc_wait_block_message(sock, buf, msg->thumb_size, from_addr, &from_addr_size);
+               if (recv_msg_len != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_STRERROR("ms_ipc_wait_message failed");
+                       MS_SAFE_FREE(buf);
+                       MS_SAFE_FREE(thumbaddmsg);
+                       return _media_thumb_get_error();
+               }
+               header_size = sizeof(thumbaddmsg);
+
+               memcpy(thumbaddmsg, buf, header_size);
+
+               msg->thumb_size = thumbaddmsg->thumb_size;
+
+               if (msg->thumb_size <= 0) {
+                       MS_SAFE_FREE(buf);
+                       MS_SAFE_FREE(thumbaddmsg);
+                       MS_DBG_ERR("msg->thumb_size is invalid %d", msg->thumb_size);
+                       return MS_MEDIA_ERR_DATA_TAINTED;
+               }
+
+               MS_SAFE_FREE(msg->thumb_data);
+               MS_MALLOC(msg->thumb_data, (unsigned int)(msg->thumb_size));
+               if(msg->thumb_data == NULL) {
+                       MS_DBG_ERR("malloc failed");
+                       MS_SAFE_FREE(buf);
+                       MS_SAFE_FREE(thumbaddmsg);
+                       return MS_MEDIA_ERR_OUT_OF_MEMORY;
+               }
+               memcpy(msg->thumb_data, buf + header_size, msg->thumb_size);
+
+               MS_SAFE_FREE(thumbaddmsg->thumb_data);
+               MS_SAFE_FREE(thumbaddmsg);
+               MS_SAFE_FREE(buf);
+       }
+
        *from_size = from_addr_size;
 
        return MS_MEDIA_ERR_NONE;
@@ -352,20 +398,29 @@ int _ms_thumb_set_buffer(thumbMsg *req_msg, unsigned char **buf, int *buf_size)
 
        int org_path_len = 0;
        int dst_path_len = 0;
-       int size = 0;
+       int data_len = 0;
        int header_size = 0;
+       unsigned int size = 0;
+
+       header_size = sizeof(thumbMsg) -(MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
+       org_path_len = req_msg->origin_path_size;
+       dst_path_len = req_msg->dest_path_size;
+       data_len = req_msg->thumb_size;
 
-       header_size = sizeof(thumbMsg) - MAX_FILEPATH_LEN*2;
-       org_path_len = strlen(req_msg->org_path) + 1;
-       dst_path_len = strlen(req_msg->dst_path) + 1;
+       MS_DBG_SLOG("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d], thumb_data : %d", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len, req_msg->thumb_size);
 
-       MS_DBG_SLOG("Basic Size : %d, org_path : %s[%d], dst_path : %s[%d]", header_size, req_msg->org_path, org_path_len, req_msg->dst_path, dst_path_len);
+       size = header_size + org_path_len + dst_path_len + data_len;
+       MS_MALLOC(*buf, size);
+       if(*buf == NULL) {
+               MS_DBG_STRERROR("MALLOC failed");
+               return -1;
+       }
 
-       size = header_size + org_path_len + dst_path_len;
-       *buf = malloc(size);
        memcpy(*buf, req_msg, header_size);
        memcpy((*buf)+header_size, req_msg->org_path, org_path_len);
        memcpy((*buf)+header_size + org_path_len, req_msg->dst_path, dst_path_len);
+       if(data_len > 0)
+               memcpy((*buf)+header_size + org_path_len + dst_path_len, req_msg->thumb_data, data_len);
 
        *buf_size = size;
 
@@ -452,7 +507,7 @@ gboolean _ms_thumb_agent_execute_server()
                g_folk_thumb_server = TRUE;
        }
 
-       g_server_pid = pid;
+       g_thumb_server_pid = pid;
 
        if (!_ms_thumb_agent_recv_msg_from_server()) {
                MS_DBG_ERR("_ms_thumb_agent_recv_msg_from_server is failed");
@@ -503,7 +558,7 @@ gboolean _ms_thumb_agent_send_msg_to_thumb_server(thumbMsg *recv_msg, thumbMsg *
 
        struct sockaddr_un client_addr;
        unsigned int client_addr_len;
-       header_size = sizeof(thumbMsg) - MAX_FILEPATH_LEN*2;
+       header_size = sizeof(thumbMsg) - (MAX_FILEPATH_LEN * 2) - sizeof(unsigned char *);
 
        if (_ms_thumb_recv_udp_msg(sock, header_size, res_msg, &client_addr, &client_addr_len) < 0) {
                MS_DBG_ERR("_ms_thumb_recv_udp_msg failed");
@@ -534,7 +589,7 @@ gboolean _ms_thumb_agent_send_msg_to_thumb_server(thumbMsg *recv_msg, thumbMsg *
 gboolean _ms_thumb_check_queued_request(gpointer data)
 {
        if (g_thumb_server_queued_all_extracting_request) {
-               MS_DBG_WARN("There is  queued request");
+               MS_DBG_WARN("There is queued request");
 
                /* request all-thumb extraction to thumbnail server */
                thumbMsg msg;
@@ -547,6 +602,7 @@ gboolean _ms_thumb_check_queued_request(gpointer data)
                msg.origin_path_size = 1;
                msg.dst_path[0] = '\0';
                msg.dest_path_size = 1;
+               msg.thumb_size = 0;
 
                /* Command All-thumb extraction to thumbnail server */
                if (!_ms_thumb_agent_send_msg_to_thumb_server(&msg, &recv_msg)) {
@@ -565,16 +621,16 @@ gboolean _ms_thumb_check_queued_request(gpointer data)
 gboolean _ms_thumb_agent_timer()
 {
        if (g_thumb_server_extracting) {
-               MS_DBG("Timer is called.. But media-thumbnail-server[%d] is busy.. so timer is recreated", g_server_pid);
+               MS_DBG("Timer is called.. But media-thumbnail-server[%d] is busy.. so timer is recreated", g_thumb_server_pid);
 
                _ms_thumb_create_timer(g_timer_id);
                return FALSE;
        }
 
        g_timer_id = 0;
-       MS_DBG("Timer is called.. Now killing media-thumbnail-server[%d]", g_server_pid);
+       MS_DBG("Timer is called.. Now killing media-thumbnail-server[%d]", g_thumb_server_pid);
 
-       if (g_server_pid > 0) {
+       if (g_thumb_server_pid > 0) {
                /* Kill thumbnail server */
                thumbMsg msg;
                thumbMsg recv_msg;
@@ -586,6 +642,7 @@ gboolean _ms_thumb_agent_timer()
                msg.origin_path_size = 1;
                msg.dst_path[0] = '\0';
                msg.dest_path_size = 1;
+               msg.thumb_size = 0;
 
                /* Command Kill to thumbnail server */
                g_shutdowning_thumb_server = TRUE;
@@ -598,115 +655,14 @@ gboolean _ms_thumb_agent_timer()
                g_io_channel_unref(g_udp_channel);
                g_udp_channel = NULL;
                g_communicate_sock = 0;
+               MS_SAFE_FREE(recv_msg.thumb_data);
        } else {
-               MS_DBG_ERR("g_server_pid is %d. Maybe there's problem in thumbnail-server", g_server_pid);
+               MS_DBG_ERR("g_thumb_server_pid is %d. Maybe there's problem in thumbnail-server", g_thumb_server_pid);
        }
 
        return FALSE;
 }
 
-int _ms_thumb_cancel_media(const char *path, int pid)
-{
-       int ret = -1;
-       int i = 0;
-       int req_len = 0;
-
-       req_len = g_queue_get_length(g_request_queue);
-
-       MS_DBG("Queue length : %d", req_len);
-
-       for (i = 0; i < req_len; i++) {
-               thumbRequest *req = NULL;
-               req = (thumbRequest *)g_queue_peek_nth(g_request_queue, i);
-               if (req == NULL) continue;
-
-               if ((req->recv_msg->pid) == pid && (strncmp(path, req->recv_msg->org_path, strlen(path))) == 0) {
-                       MS_DBG("Remove %s from queue", req->recv_msg->org_path);
-                       g_queue_pop_nth(g_request_queue, i);
-
-                       close(req->client_sock);
-                       MS_SAFE_FREE(req->recv_msg);
-                       MS_SAFE_FREE(req);
-                       ret = 0;
-
-                       break;
-               }
-       }
-
-       return ret;
-}
-
-int _ms_thumb_cancel_all(int pid)
-{
-       int ret = -1;
-       int i = 0;
-       int req_len = 0;
-
-       req_len = g_queue_get_length(g_request_queue);
-
-       MS_DBG("Queue length : %d", req_len);
-
-       for (i = 0; i < req_len; i++) {
-               thumbRequest *req = NULL;
-               req = (thumbRequest *)g_queue_peek_nth(g_request_queue, i);
-               if (req == NULL) continue;
-
-               if (req->recv_msg->pid == pid) {
-                       MS_DBG("Remove [%d] %s from queue", req->recv_msg->pid, req->recv_msg->org_path);
-                       g_queue_pop_nth(g_request_queue, i);
-                       i--;
-                       req_len--;
-
-                       close(req->client_sock);
-                       MS_SAFE_FREE(req->recv_msg);
-                       MS_SAFE_FREE(req);
-                       ret = 0;
-               }
-       }
-
-       return ret;
-}
-
-void _ms_thumb_cancle_request(thumbRequest *thumb_req)
-{
-       MS_DBG("");
-       int ret = -1;
-
-       if (thumb_req == NULL) return;
-
-       thumbMsg *recv_msg = thumb_req->recv_msg;
-       if (recv_msg == NULL) {
-               MS_SAFE_FREE(thumb_req);
-               return;
-       }
-
-       if (recv_msg->msg_type == 3)
-               ret = _ms_thumb_cancel_media(recv_msg->org_path, recv_msg->pid);
-       else if (recv_msg->msg_type == 4)
-               ret = _ms_thumb_cancel_all(recv_msg->pid);
-
-       if (ret == 0) {
-               recv_msg->status = 0;  // THUMB_SUCCESS
-       } else {
-               recv_msg->status = 0;  // THUMB_SUCCESS
-       }
-
-       if (recv_msg->origin_path_size <= 0  || recv_msg->origin_path_size > MS_FILE_PATH_LEN_MAX) {
-               MS_DBG_ERR("recv_msg->origin_path_size is invalid %d", recv_msg->origin_path_size );
-               return;
-       }
-
-       recv_msg->dest_path_size = recv_msg->origin_path_size;
-       strncpy(recv_msg->dst_path, recv_msg->org_path, recv_msg->dest_path_size);
-
-       close(thumb_req->client_sock);
-
-       MS_SAFE_FREE(thumb_req->recv_msg);
-       MS_SAFE_FREE(thumb_req);
-
-       return;
-}
-
 gboolean _ms_thumb_request_to_server(gpointer data)
 {
        int req_len = 0;
@@ -783,6 +739,8 @@ gboolean _ms_thumb_request_to_server(gpointer data)
                                strncpy(res_msg.org_path, recv_msg->org_path, res_msg.origin_path_size);
                                res_msg.dst_path[0] = '\0';
                                res_msg.dest_path_size = 1;
+                               res_msg.thumb_data = (unsigned char *)"\0";
+                               res_msg.thumb_size = 1;
 
                                int buf_size = 0;
                                unsigned char *buf = NULL;
@@ -812,19 +770,33 @@ gboolean _ms_thumb_request_to_server(gpointer data)
        res_msg.dest_path_size = strlen(res_msg.dst_path) + 1;
 
        int buf_size = 0;
+       int sending_block = 0;
+       int block_size = sizeof(res_msg) - MAX_FILEPATH_LEN*2 - sizeof(unsigned char *);
        unsigned char *buf = NULL;
        _ms_thumb_set_buffer(&res_msg, &buf, &buf_size);
 
-       if (send(client_sock, buf, buf_size, 0) != buf_size) {
-               MS_DBG_STRERROR("sendto failed");
-       } else {
-               MS_DBG_SLOG("Sent %s(%d) from %s", res_msg.dst_path, strlen(res_msg.dst_path), res_msg.org_path);
+       while(buf_size > 0) {
+               if(buf_size < MS_SOCK_BLOCK_SIZE) {
+                       block_size = buf_size;
+               }
+               if (send(client_sock, buf+sending_block, block_size, 0) != block_size) {
+                       MS_DBG_STRERROR("sendto failed");
+               }
+               sending_block += block_size;
+               buf_size -= block_size;
+               if(block_size < MS_SOCK_BLOCK_SIZE) {
+                       block_size = MS_SOCK_BLOCK_SIZE;
+               }
+       }
+       if(buf_size == 0) {
+               MS_DBG_SLOG("Sent data(%d) from %s", res_msg.thumb_size, res_msg.org_path);
        }
 
        close(client_sock);
        MS_SAFE_FREE(buf);
        MS_SAFE_FREE(req->recv_msg);
        MS_SAFE_FREE(req);
+       MS_SAFE_FREE(res_msg.thumb_data);
 
        return TRUE;
 }
@@ -834,7 +806,6 @@ gboolean _ms_thumb_agent_read_socket(GIOChannel *src,
                                                                        gpointer data)
 {
        thumbMsg *recv_msg = NULL;
-       int header_size = 0;
        int sock = -1;
        int client_sock = -1;
 
@@ -844,21 +815,19 @@ gboolean _ms_thumb_agent_read_socket(GIOChannel *src,
                return TRUE;
        }
 
-       header_size = sizeof(thumbMsg) - MAX_FILEPATH_LEN*2;
-
        if (ms_ipc_accept_client_tcp(sock, &client_sock) < 0) {
                MS_DBG_STRERROR("accept failed");
                return TRUE;
        }
 
-       recv_msg = calloc(1, sizeof(thumbMsg));
+       MS_MALLOC(recv_msg, sizeof(thumbMsg));
        if (recv_msg == NULL) {
                MS_DBG_ERR("Failed to allocate memory");
                close(client_sock);
                return TRUE;
        }
 
-       if (_ms_thumb_recv_msg(client_sock, header_size, recv_msg) < 0) {
+       if (_ms_thumb_recv_msg(client_sock, recv_msg) < 0) {
                MS_DBG_ERR("_ms_thumb_recv_msg failed ");
                close(client_sock);
                MS_SAFE_FREE(recv_msg);
@@ -868,10 +837,12 @@ gboolean _ms_thumb_agent_read_socket(GIOChannel *src,
        MS_DBG_SLOG("Received [%d] %s(%d) from PID(%d)", recv_msg->msg_type, recv_msg->org_path, strlen(recv_msg->org_path), recv_msg->pid);
 
        thumbRequest *thumb_req = NULL;
-       thumb_req = calloc(1, sizeof(thumbRequest));
+
+       MS_MALLOC(thumb_req, sizeof(thumbRequest));
        if (thumb_req == NULL) {
                MS_DBG_ERR("Failed to create request element");
                close(client_sock);
+               MS_SAFE_FREE(recv_msg->thumb_data);
                MS_SAFE_FREE(recv_msg);
                return TRUE;
        }
@@ -879,14 +850,9 @@ gboolean _ms_thumb_agent_read_socket(GIOChannel *src,
        thumb_req->client_sock = client_sock;
        thumb_req->recv_msg = recv_msg;
 
-       if (recv_msg->msg_type == 3 || recv_msg->msg_type == 4) { // THUMB_REQUEST_CANCEL_MEDIA || THUMB_REQUEST_CANCEL_ALL
-               _ms_thumb_cancle_request(thumb_req);
-               return TRUE;
-       }
-
        if (g_request_queue == NULL) {
                MS_DBG_WARN("queue is init");
-                g_request_queue = g_queue_new();
+               g_request_queue = g_queue_new();
        }
 
        if (g_queue_get_length(g_request_queue) >= MAX_THUMB_REQUEST) {
@@ -896,10 +862,11 @@ gboolean _ms_thumb_agent_read_socket(GIOChannel *src,
 
                res_msg.msg_type = 6; // THUMB_RESPONSE
                res_msg.status = 1; //THUMB_FAIL
-               res_msg.origin_path_size = strlen(recv_msg->org_path);
+               res_msg.origin_path_size = recv_msg->origin_path_size;
                strncpy(res_msg.org_path, recv_msg->org_path, res_msg.origin_path_size);
                res_msg.dst_path[0] = '\0';
                res_msg.dest_path_size = 1;
+               res_msg.thumb_size = 0;
 
                int buf_size = 0;
                unsigned char *buf = NULL;
@@ -968,15 +935,32 @@ gboolean _ms_thumb_agent_prepare_udp_socket()
        return TRUE;
 }
 
+int ms_thumb_get_config()
+{
+       dictionary *dict = NULL;
+
+       dict = iniparser_load(MS_INI_DEFAULT_PATH);
+       if(!dict) {
+               MS_DBG_ERR("%s load failed", MS_INI_DEFAULT_PATH);
+               return -1;
+       }
+
+       MS_INI_GET_INT(dict, "media-content-config:thumbnail_activation",g_thumb_server_active, 0);
+       MS_DBG("Thumb-server activation level = %d", g_thumb_server_active);
+
+       iniparser_freedict(dict);
+       return 0;
+}
+
 gpointer ms_thumb_agent_start_thread(gpointer data)
 {
-       MS_DBG("");
        int sockfd = -1;
-
        GSource *source = NULL;
        GIOChannel *channel = NULL;
        GMainContext *context = NULL;
 
+       MS_DBG_FENTER();
+
        /* Create and bind new TCP socket */
        if (!_ms_thumb_agent_prepare_tcp_socket(&sockfd)) {
                MS_DBG_ERR("Failed to create socket");