Update scanner-v2, Fix some bug, and code refactoring 91/83191/1 submit/tizen/20160812.084807
authorMinje Ahn <minje.ahn@samsung.com>
Tue, 9 Aug 2016 10:21:03 +0000 (19:21 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Tue, 9 Aug 2016 10:21:03 +0000 (19:21 +0900)
Change-Id: I21856399cf1406b1e6b77f5f9dd2c9f14c024f0e
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
47 files changed:
Makefile.am
configure.ac
data/media.db [new file with mode: 0755]
data/media_product.db [new file with mode: 0755]
lib/include/media-server-ipc.h
lib/include/media-util-db.h
lib/include/media-util-register.h
lib/include/media-util.h
lib/media-util-db.c
lib/media-util-register.c
media_content_config _product.ini [new file with mode: 0644]
packaging/media-server.service
packaging/media-server.spec
src/common/include/media-common-db-svc.h
src/common/include/media-common-external-storage.h
src/common/include/media-common-system.h
src/common/include/media-common-types.h
src/common/include/media-common-utils.h
src/common/media-common-db-svc.c
src/common/media-common-external-storage.c
src/common/media-common-system.c
src/common/media-common-utils.c
src/scanner-v2/include/media-scanner-db-manage-v2.h [new file with mode: 0755]
src/scanner-v2/include/media-scanner-device-block-v2.h
src/scanner-v2/include/media-scanner-extract-v2.h
src/scanner-v2/include/media-scanner-scan-v2.h
src/scanner-v2/include/media-scanner-socket-v2.h
src/scanner-v2/media-scanner-db-manage-v2.c [new file with mode: 0755]
src/scanner-v2/media-scanner-device-block-v2.c
src/scanner-v2/media-scanner-extract-v2.c
src/scanner-v2/media-scanner-scan-v2.c
src/scanner-v2/media-scanner-socket-v2.c
src/scanner-v2/media-scanner-v2.c
src/scanner/include/media-scanner-db-manage.h [new file with mode: 0755]
src/scanner/include/media-scanner-device-block.h
src/scanner/media-scanner-db-manage.c [new file with mode: 0755]
src/scanner/media-scanner-device-block.c
src/scanner/media-scanner-scan.c
src/scanner/media-scanner.c
src/server/include/media-server-db-manage.h [new file with mode: 0755]
src/server/include/media-server-device-block.h
src/server/include/media-server-socket.h
src/server/media-server-db-manage.c [new file with mode: 0755]
src/server/media-server-device-block.c
src/server/media-server-main.c
src/server/media-server-scanner.c
src/server/media-server-socket.c

index c1271b5..956b891 100644 (file)
@@ -21,7 +21,9 @@ COMMON_CFLAGS = -I${srcdir}/lib/include \
                $(LIBPMCONTROL_CFLAGS) \
                $(LIBSYSTEMDLOGIN_CFLAGS) \
                $(SECURITY_CFLAGS) \
-               $(SMACK_CFLAGS)
+               $(SYSTEM_INFO_CFLAGS) \
+               $(SMACK_CFLAGS) \
+               $(USB_DEVICE_CFLAGS)
 
 if USE_NOTIFICATION
 COMMON_CFLAGS += $(STATUS_CFLAGS)
@@ -36,7 +38,9 @@ COMMON_LDADD =        libmedia-utils.la \
                $(LIBPMCONTROL_LIBS) \
                $(LIBSYSTEMDLOGIN_LIBS) \
                $(SECURITY_LIBS) \
+               $(SYSTEM_INFO_LIBS) \
                $(SMACK_LIBS) \
+               $(USB_DEVICE_LIBS) \
                -ldl
 
 if USE_NOTIFICATION
@@ -73,7 +77,8 @@ libmedia_utils_la_CFLAGS = -I${srcdir}/lib/include \
                              $(LIBSMACK_CFLAGS) \
                              $(CYNARA_CLIENT_CFLAGS) \
                              $(CYNARA_SESSION_CFLAGS) \
-                             $(CYNARA_CREDS_SOCKET_CFLAGS)
+                             $(CYNARA_CREDS_SOCKET_CFLAGS) \
+                             $(SYSTEM_INFO_CFLAGS)
 
 libmedia_utils_la_LIBADD  =   $(GLIB_LIBS) \
                               $(DLOG_LIBS) \
@@ -86,7 +91,8 @@ libmedia_utils_la_LIBADD  =   $(GLIB_LIBS) \
                              $(LIBSMACK_LIBS) \
                              $(CYNARA_CLIENT_LIBS) \
                              $(CYNARA_SESSION_LIBS) \
-                             $(CYNARA_CREDS_SOCKET_LIBS)
+                             $(CYNARA_CREDS_SOCKET_LIBS) \
+                             $(SYSTEM_INFO_LIBS)
 
 bin_PROGRAMS = media-server \
                media-scanner \
@@ -99,6 +105,7 @@ media_server_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/server/media-server-db-manage.c \
                        src/server/media-server-db.c \
                        src/server/media-server-socket.c \
                        src/server/media-server-thumb.c \
@@ -112,6 +119,12 @@ media_server_CFLAGS = -I${srcdir}/src/server/include \
                         $(PHONESTATUS_CFLAGS) \
                         $(SYSTEM_INFO_CFLAGS)
 
+media_server_CFLAGS += $(SQLITE3_CFLAGS) \
+                                          $(USB_DEVICE_CFLAGS) \
+                                          $(DEVICED_CFLAGS) \
+                                          $(POWER_DEFS_CFLAGS) \
+                                          $(POWER_TV_CFLAGS)
+
 media_server_CFLAGS += -fPIE
 
 media_server_LDFLAGS = -pie
@@ -120,6 +133,12 @@ media_server_LDADD = $(COMMON_LDADD) \
                     $(PHONESTATUS_LIBS) \
                     $(SYSTEM_INFO_LIBS)
 
+media_server_LDADD += $(SECURITY_LIBS) \
+                             $(USB_DEVICE_LIBS) \
+                              $(DEVICED_LIBS) \
+                              $(POWER_DEFS_LIBS) \
+                              $(POWER_TV_LIBS)
+
 ######################################################
 #media scanner v1
 ######################################################
@@ -127,6 +146,7 @@ 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/scanner/media-scanner-db-manage.c \
                         src/scanner/media-scanner-device-block.c \
                        src/scanner/media-scanner-scan.c \
                        src/scanner/media-scanner-socket.c \
@@ -148,6 +168,7 @@ 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/scanner-v2/media-scanner-db-manage-v2.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 \
@@ -158,12 +179,22 @@ media_scanner_v2_SOURCES = src/common/media-common-utils.c \
 media_scanner_v2_CFLAGS = -I${srcdir}/src/scanner-v2/include \
                           $(COMMON_CFLAGS)
 
+media_scanner_v2_CFLAGS += $(USB_DEVICE_CFLAGS) \
+                                                  $(DEVICED_CFLAGS) \
+                                                  $(POWER_DEFS_CFLAGS) \
+                                                  $(POWER_TV_CFLAGS)
+
 media_scanner_v2_CFLAGS += -fPIE
 
 media_scanner_v2_LDFLAGS = -pie
 
 media_scanner_v2_LDADD = $(COMMON_LDADD)
 
+media_scanner_v2_LDADD += $(USB_DEVICE_LIBS) \
+                          $(DEVICED_LIBS) \
+                          $(POWER_DEFS_LIBS) \
+                          $(POWER_TV_LIBS)
+
 ######################################################
 #mediadb_udpate
 ######################################################
index 94b794e..d91a8e1 100644 (file)
@@ -150,6 +150,36 @@ if test "x$USE_NOTIFICATION" = "xyes"; then
 fi
 AM_CONDITIONAL(USE_NOTIFICATION, test "x$USE_NOTIFICATION" = "xyes")
 
+#deviced
+PKG_CHECK_MODULES(DEVICED, deviced)
+AC_SUBST(DEVICED_CFLAGS)
+AC_SUBST(DEVICE_LIBS)
+
+AC_ARG_ENABLE(product_tv, AC_HELP_STRING([--enable-product-tv], [using tv product features]),
+[
+   case "${enableval}" in
+       yes) USE_PRODUCT_TV=yes ;;
+        no) USE_PRODUCT_TV=no ;;
+         *) AC_MSG_ERROR(bad value ${enableval} for --enable-product-tv) ;;
+   esac
+],[USE_PRODUCT_TV=yes])
+if test "x$USE_PRODUCT_TV" = "xyes"; then
+       #usb server
+       PKG_CHECK_MODULES(USB_DEVICE, capi-system-usbdevice)
+       AC_SUBST(USB_DEVICE_CFLAGS)
+       AC_SUBST(USB_DEVICE_LIBS)
+
+       #power-defs
+       PKG_CHECK_MODULES(POWER_DEFS, power-defs)
+       AC_SUBST(POWER_DEFS_CFLAGS)
+       AC_SUBST(POWER_DEFS_LIBS)
+
+       PKG_CHECK_MODULES(POWER_TV, capi-system-power-tv)
+       AC_SUBST(POWER_TV_CFLAGS)
+       AC_SUBST(POWER_TV_LIBS)
+fi
+AM_CONDITIONAL(USE_PRODUCT_TV, test "x$USE_PRODUCT_TV" = "xyes")
+
 #Checks for header files.
 AC_HEADER_DIRENT
 AC_HEADER_STDC
diff --git a/data/media.db b/data/media.db
new file mode 100755 (executable)
index 0000000..96e8a26
Binary files /dev/null and b/data/media.db differ
diff --git a/data/media_product.db b/data/media_product.db
new file mode 100755 (executable)
index 0000000..cb2c798
Binary files /dev/null and b/data/media_product.db differ
index 86a9ec9..3ffccf0 100755 (executable)
@@ -69,6 +69,9 @@ typedef enum{
        MS_MSG_SCANNER_PARTIAL,
        MS_MSG_SCANNER_COMPLETE,
        MS_MSG_EXTRACTOR_COMPLETE,
+       MS_MSG_MEDIA_DB_RESET,  /**< media DB is reset*/
+       MS_MSG_RECEIVE_MSG_FAILED,   /**ms_ipc_receive_message_tcp failed**/
+       MS_MSG_MEDIA_DB_MALFORMED,
        MS_MSG_MAX                                                      /**< Invalid msg type */
 }ms_msg_type_e;
 
index f0f807a..fa4f5fa 100755 (executable)
@@ -44,6 +44,8 @@ int media_db_request_update_db_batch_end(const char *query_str, uid_t uid);
 
 int media_db_request_update_db_batch_clear(void);
 
+int media_db_get_media_db_path(uid_t uid, char **db_path);
+
 /**
 * @}
 */
index b15158c..2f47bc5 100755 (executable)
@@ -36,6 +36,7 @@ typedef enum
        MEDIA_REQUEST_SCAN_PARTIAL,
        MEDIA_REQUEST_SCAN_COMPLETE,
        MEDIA_REQUEST_EXTRACT_COMPLETE,
+       MEDIA_REQUEST_ERROR_SERVER_RECEIVE_MSG_FAILED,
 } media_request_type_e;
 
 typedef struct
index e722938..f4e12fc 100755 (executable)
 #define MEDIA_DB_NAME          tzplatform_mkpath(TZ_USER_DB, ".media.db")              /**<  media db name*/
 #define MEDIA_CONTENT_PATH             "content"               /**<  user content folder name*/
 
+#ifdef _USE_SENIOR_MODE
+#define MEDIA_ROOT_PATH_SENIOR_MODE "/opt/familytv"
+#endif
+
+#define MEDIA_DB_SIZE_LIMIT_2 20000000.0
+#define MEDIA_DB_SIZE_LIMIT_1 10000000.0
+#define MEDIA_DB_SPACE_LIMIT 20971520.0
+
+#define MEDIA_DEFAULT_UID 5001
 
 #endif /*_MEDIA_UTIL_H_*/
index 7a7ed1a..e98296c 100755 (executable)
@@ -41,6 +41,8 @@ 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, uid_t uid, bool need_write);
 static int __media_db_disconnect_db_with_handle(sqlite3 *db_handle);
+static int __media_db_request_update_tcp(ms_msg_type_e msg_type, const char *request_msg, uid_t uid);
+static int __media_db_request_recovery(uid_t uid);
 
 static void __media_db_destroy_sql_list()
 {
@@ -105,6 +107,7 @@ static int __media_db_connect_db_with_handle(sqlite3 **db_handle, uid_t uid, boo
 {
        int ret = SQLITE_OK;
        char *db_info = __media_get_media_DB(uid);
+       char *sql = NULL;
 
        if (!MS_STRING_VALID(db_info)) {
                MSAPI_DBG_ERR("__media_get_media_DB failed");
@@ -144,6 +147,30 @@ static int __media_db_connect_db_with_handle(sqlite3 **db_handle, uid_t uid, boo
                return MS_MEDIA_ERR_DB_CONNECT_FAIL;
        }
 
+       /*In TV case, if the HDD is inserted, so many data can be inserted.
+       So the size of the journal file is incresed.
+       We are trucate the journal for reducing the usage rate of the internal storage.*/
+//     ret = sqlite3_exec(*db_handle, "PRAGMA journal_mode = TRUNCATE", NULL, NULL, NULL);
+       sql = sqlite3_mprintf("%s", "PRAGMA journal_mode = OFF");
+       ret = sqlite3_exec(*db_handle, sql, NULL, NULL, NULL);
+       sqlite3_free(sql);
+       if (SQLITE_OK != ret) {
+
+               if (*db_handle) {
+                       MSAPI_DBG_ERR("[error when change the journal mode] %s", sqlite3_errmsg(*db_handle));
+               }
+
+               db_util_close(*db_handle);
+               *db_handle = NULL;
+
+               if (ret == SQLITE_CORRUPT) {
+                       MSAPI_DBG_ERR("MEDIA DB IS CORRUPTED");
+                       return MS_MEDIA_ERR_DB_CORRUPT;
+               }
+
+               return MS_MEDIA_ERR_DB_CONNECT_FAIL;
+       }
+
        return MS_MEDIA_ERR_NONE;
 }
 
@@ -426,12 +453,26 @@ int media_db_connect(MediaDBHandle **handle, uid_t uid, bool need_write)
 {
        int ret = MS_MEDIA_ERR_NONE;
        sqlite3 * db_handle = NULL;
+       int retry_cnt = 0;
 
        MSAPI_DBG_FUNC();
 
+RETRY_CONN:
        ret = __media_db_connect_db_with_handle(&db_handle, uid, need_write);
-       MSAPI_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
-
+       if (ret != MS_MEDIA_ERR_NONE) {
+               if (ret == MS_MEDIA_ERR_DB_CORRUPT && retry_cnt == 0) {
+                       retry_cnt++;
+
+                       ret = __media_db_request_recovery(uid);
+                       if (ret == MS_MEDIA_ERR_NONE)
+                               goto RETRY_CONN;
+                       else
+                               return MS_MEDIA_ERR_DB_CORRUPT;
+               } else {
+                       return ret;
+               }
+       }
+       
        *handle = db_handle;
        return MS_MEDIA_ERR_NONE;
 }
@@ -626,3 +667,142 @@ int media_db_request_update_db_batch_clear(void)
 
        return ret;
 }
+
+static int __media_db_request_recovery(uid_t uid)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       int request_msg_size = 0;
+       int sockfd = -1;
+       ms_sock_info_s sock_info;
+       struct sockaddr_un serv_addr;
+       int port = MS_SCANNER_PORT;
+       int retry_count = 0;
+       char *db_path = NULL;
+
+       media_db_get_media_db_path(uid, &db_path);
+
+       if (!MS_STRING_VALID(db_path)) {
+               MSAPI_DBG_ERR("invalid query");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+
+       request_msg_size = strlen(db_path);
+       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;
+       }
+
+       /*      MSAPI_DBG("querysize[%d] query[%s]", request_msg_size, request_msg); */
+
+       ms_comm_msg_s send_msg;
+       memset((void *)&send_msg, 0, sizeof(ms_comm_msg_s));
+
+       send_msg.msg_type = MS_MSG_MEDIA_DB_MALFORMED;
+       send_msg.msg_size = request_msg_size;
+       strncpy(send_msg.msg, db_path, request_msg_size);
+       MS_SAFE_FREE(db_path);
+       send_msg.uid = uid;
+
+       /*Create Socket*/
+       ret = ms_ipc_create_client_socket(MS_PROTOCOL_TCP, MS_TIMEOUT_SEC_10, &sock_info);
+       sockfd = sock_info.sock_fd;
+       MSAPI_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
+
+       /*Set server Address*/
+       memset(&serv_addr, 0, sizeof(serv_addr));
+       serv_addr.sun_family = AF_UNIX;
+       /*      MSAPI_DBG_SLOG("%s", MEDIA_IPC_PATH[port]); */
+       strncpy(serv_addr.sun_path, MEDIA_IPC_PATH[port], strlen(MEDIA_IPC_PATH[port]));
+
+       /* Connecting to the media db server */
+       if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
+               MSAPI_DBG_STRERROR("connect error");
+               close(sockfd);
+               return MS_MEDIA_ERR_SOCKET_CONN;
+       }
+
+       /* Send request */
+       if (send(sockfd, &send_msg, sizeof(send_msg), 0) != sizeof(send_msg)) {
+               MSAPI_DBG_STRERROR("send failed");
+               close(sockfd);
+               return MS_MEDIA_ERR_SOCKET_SEND;
+       }
+
+       /*Receive Response*/
+       int recv_msg_size = -1;
+       ms_comm_msg_s recv_msg;
+RETRY:
+       if ((recv_msg_size = recv(sockfd, &recv_msg, sizeof(recv_msg), 0)) < 0) {
+               MSAPI_DBG_ERR("recv failed : [%d]", sockfd);
+
+               if (errno == EINTR) {
+                       MSAPI_DBG_STRERROR("catch interrupt");
+                       goto RETRY;
+               }
+
+               if (errno == EWOULDBLOCK) {
+                       if (retry_count < MAX_RETRY_COUNT) {
+                               MSAPI_DBG_ERR("TIME OUT[%d]", retry_count);
+                               retry_count++;
+                               goto RETRY;
+                       }
+
+                       close(sockfd);
+                       MSAPI_DBG_ERR("Timeout. Can't try any more");
+                       return MS_MEDIA_ERR_SOCKET_RECEIVE_TIMEOUT;
+               } else {
+                       MSAPI_DBG_STRERROR("recv failed");
+
+                       close(sockfd);
+
+                       return MS_MEDIA_ERR_SOCKET_RECEIVE;
+               }
+       }
+
+       MSAPI_DBG("RECEIVE OK [%d]", recv_msg.result);
+       ret = recv_msg.result;
+
+       close(sockfd);
+
+       return ret;
+}
+
+int media_db_get_media_db_path(uid_t uid, char **db_path)
+{
+       int len = 0;
+       struct group *grpinfo = NULL;
+
+       *db_path = NULL;
+
+       if (uid == getuid()) {
+               grpinfo = getgrnam("users");
+               if (grpinfo == NULL) {
+                       MSAPI_DBG_ERR("getgrnam(users) returns NULL !");
+                       return MS_MEDIA_ERR_DB_INTERNAL;
+               }
+               if (MS_STRING_VALID(MEDIA_DB_NAME))
+                       *db_path = strdup(MEDIA_DB_NAME);
+       } else {
+               char passwd_str[MAX_FILEPATH_LEN] = {0, };
+               struct passwd *userinfo = getpwuid(uid);
+               if (userinfo == NULL) {
+                       MSAPI_DBG_ERR("getpwuid(%d) returns NULL !", uid);
+                       return MS_MEDIA_ERR_DB_INTERNAL;
+               }
+               grpinfo = getgrnam("users");
+               if (grpinfo == NULL) {
+                       MSAPI_DBG_ERR("getgrnam(users) returns NULL !");
+                       return MS_MEDIA_ERR_DB_INTERNAL;
+               }
+               // Compare git_t type and not group name
+               if (grpinfo->gr_gid != userinfo->pw_gid) {
+                       MSAPI_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
+                       return MS_MEDIA_ERR_DB_INTERNAL;
+               }
+               len = snprintf(passwd_str, sizeof(passwd_str), "%s/.applications/dbspace/.media.db", userinfo->pw_dir);
+               if (len > 0)
+                       *db_path = strndup(passwd_str, len);
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
index d4a0a55..65ba596 100755 (executable)
@@ -38,6 +38,7 @@
 #include "media-util-internal.h"
 #include "media-util-dbg.h"
 #include "media-util.h"
+#include <system_info.h>
 
 static GMutex scan_req_mutex;
 
@@ -57,6 +58,23 @@ typedef struct media_scan_data {
 
 GArray *req_list;
 
+#ifdef _USE_SENIOR_MODE
+static bool media_util_is_support_senior_mode();
+bool media_util_is_support_senior_mode()
+{
+       bool bSupportSeniorMode = false;
+
+       if(system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
+               MSAPI_DBG("Get familytv Support failed");
+               return false;
+       }
+
+       MSAPI_DBG("FamilyTv Support : [%d]", bSupportSeniorMode);
+
+       return bSupportSeniorMode;
+}
+#endif
+
 static char* __media_get_path(uid_t uid)
 {
        char *result_passwd = NULL;
@@ -100,6 +118,14 @@ static bool _is_valid_path(const char *path, uid_t uid)
        if (path == NULL)
                return false;
 
+#ifdef _USE_SENIOR_MODE
+       if(media_util_is_support_senior_mode()) {
+               if (strncmp(path, MEDIA_ROOT_PATH_SENIOR_MODE, strlen(MEDIA_ROOT_PATH_SENIOR_MODE)) == 0) {
+                       return true;
+               }
+       }
+#endif
+
        user_path = __media_get_path(uid);
        if (user_path == NULL)
                return false;
@@ -112,7 +138,13 @@ static bool _is_valid_path(const char *path, uid_t uid)
                ret = true;
        } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
                ret = true;
-       } else {
+       } 
+#ifdef _USE_SENIOR_MODE
+       else if (strncmp(path, MEDIA_ROOT_PATH_SENIOR_MODE, strlen(MEDIA_ROOT_PATH_SENIOR_MODE)) == 0) {
+               return true;
+       }
+#endif
+       else {
                ret = false;
        }
 
@@ -204,6 +236,12 @@ gboolean _read_socket(GIOChannel *src, GIOCondition condition, gpointer data)
        } else if (recv_msg.msg_type == MS_MSG_EXTRACTOR_COMPLETE) {
                req_result.complete_path = strndup(recv_msg.msg, recv_msg.msg_size);
                req_result.request_type = MEDIA_REQUEST_EXTRACT_COMPLETE;
+       } else if (recv_msg.msg_type == MS_MSG_RECEIVE_MSG_FAILED) {
+               MSAPI_DBG_ERR("in _read_socket MS_MSG_RECEIVE_MSG_FAILED msg_size[%d]", recv_msg.msg_size);
+               if((recv_msg.msg_size > 0) && (recv_msg.msg_size < MAX_MSG_SIZE)) {
+                       req_result.complete_path = strndup(recv_msg.msg, recv_msg.msg_size);
+               }
+               req_result.request_type = MEDIA_REQUEST_ERROR_SERVER_RECEIVE_MSG_FAILED;
        } else {
                MSAPI_DBG("The message is invalid!");
                return TRUE;
diff --git a/media_content_config _product.ini b/media_content_config _product.ini
new file mode 100644 (file)
index 0000000..0232395
--- /dev/null
@@ -0,0 +1,17 @@
+;thumbnail_activation level
+; 0 = Deny all requests related thumbnail (never activate thumbnail server)
+; 1 = Allow all requests related thumbnail
+; 2 = Allow user request only
+
+;dcm_activation level
+; 0 = Deny all requests related dcm (never activate dcm service)
+; 1 = Allow all requests related dcm
+
+[media-content-config]
+thumbnail_activation=1
+dcm_activation=1
+
+;scanner_type
+; 0 = extract all data at once
+; 1 = 2 step scanning
+scanner_type=1
index 11d06f1..c841661 100644 (file)
@@ -1,7 +1,5 @@
 [Unit]
 Description=Media server
-After=user@5001.service
-After=systemd-logind.service
 
 [Service]
 User=multimedia_fw
index 25c9839..0b45ac6 100755 (executable)
@@ -11,6 +11,7 @@ Source3:    media-server-user.path
 Source1001:     %{name}.manifest
 Source1002:     libmedia-utils.manifest
 Source1003:     libmedia-utils-devel.manifest
+Requires(post): /usr/bin/buxton2ctl
 BuildRequires:  pkgconfig(glib-2.0)
 BuildRequires:  pkgconfig(vconf)
 BuildRequires:  pkgconfig(dlog)
@@ -29,6 +30,15 @@ BuildRequires:  pkgconfig(cynara-session)
 BuildRequires:  pkgconfig(capi-system-info)
 BuildRequires:  gettext-tools
 
+%if 0%{?TIZEN_PRODUCT_TV}
+BuildRequires:  pkgconfig(capi-system-usbdevice)
+BuildRequires:  pkgconfig(power-defs)
+BuildRequires:  pkgconfig(capi-system-power-tv)
+%define product_tv 1
+%else
+%define product_tv 0
+%endif
+
 %description
 Description: A server for media content management.
 
@@ -55,11 +65,22 @@ cp %{SOURCE1001} %{SOURCE1002} %{SOURCE1003} .
 cp po/* .
 
 %build
+%if 0%{?product_tv}
+export CFLAGS="$CFLAGS -D_USE_SENIOR_MODE -D_USE_RECORDED_CONTENT -D_USE_SUSPEND_MODE"
+%else
+export CFLAGS="$CFLAGS -D_USE_MULTI_USER -D_USE_META_UPDATE -D_USE_DEVICED_DBUS"
+%endif
 export CFLAGS="$CFLAGS -DTIZEN_DEBUG_ENABLE -DSYSCONFDIR=\\\"%{_sysconfdir}\\\""
 rm -rf autom4te.cache
 rm -f aclocal.m4 ltmain.sh
 mkdir -p m4
-%reconfigure --prefix=%{_prefix} --disable-static
+%reconfigure --prefix=%{_prefix} \
+%if 0%{?product_tv}
+       --enable-product-tv \
+%else
+       --disable-product-tv \
+%endif
+       --disable-static
 %__make %{?jobs:-j%jobs}
 
 #install .po files
@@ -217,16 +238,35 @@ install -m 644 %{SOURCE3} %{buildroot}%{_unitdir_user}/media-server-user.path
 ln -s ../media-server.service %{buildroot}%{_unitdir}/multi-user.target.wants/media-server.service
 #ini file
 mkdir -p %{buildroot}/etc/multimedia
+%if 0%{?product_tv}
+cp -rf %{_builddir}/%{name}-%{version}/media_content_config_product.ini %{buildroot}/etc/multimedia/media_content_config.ini
+%else
 cp -rf %{_builddir}/%{name}-%{version}/media_content_config.ini %{buildroot}/etc/multimedia/media_content_config.ini
+%endif
 cp -rf %{_builddir}/%{name}-%{version}/media-server-plugin %{buildroot}/etc/multimedia/media-server-plugin
 mkdir -p %{buildroot}/etc/gumd/useradd.d
 cp -rf %{_builddir}/%{name}-%{version}/30_media-server-add.post %{buildroot}/etc/gumd/useradd.d/30_media-server-add.post
+#dummy db file for reset media DB
+%if 0%{?product_tv}
+cp -rf %{_builddir}/%{name}-%{version}/data/media_product.db %{buildroot}/etc/multimedia/.media.db
+%else
+cp -rf %{_builddir}/%{name}-%{version}/data/media.db %{buildroot}/etc/multimedia/.media.db
+%endif
 
 %post
 # setup dbupdate in user session
 mkdir -p %{_unitdir_user}/default.target.wants/
 ln -sf ../media-server-user.path  %{_unitdir_user}/default.target.wants/
 chmod 755 /etc/gumd/useradd.d/30_media-server-add.post
+buxton2ctl -i -u 100 create-int32 "system" "db/filemanager/dbupdate" "1" "http://tizen.org/privilege/internal/default/public" "http://tizen.org/privilege/internal/default/public"
+buxton2ctl -i -u 100 create-int32 "system" "db/private/extractstatus" "1" "http://tizen.org/privilege/internal/default/public" "http://tizen.org/privilege/internal/default/public"
+buxton2ctl -i -u 100 create-int32 "memory" "memory/filemanager/Mmc" "0" "http://tizen.org/privilege/internal/default/public" "http://tizen.org/privilege/internal/default/public"
+buxton2ctl -i -u 100 create-int32 "system" "file/private/mediaserver/scan_internal" "1" "http://tizen.org/privilege/internal/default/public" "http://tizen.org/privilege/internal/default/public"
+buxton2ctl -i -u 100 create-int32 "system" "file/private/mediaserver/scan_directory" "1" "http://tizen.org/privilege/internal/default/public" "http://tizen.org/privilege/internal/default/public"
+buxton2ctl -i -u 100 create-int32 "system" "file/private/mediaserver/db_limit" "0" "http://tizen.org/privilege/internal/default/public" "http://tizen.org/privilege/internal/default/public"
+buxton2ctl -i -u 100 create-int32 "system" "file/private/mediaserver/db_reset" "0" "http://tizen.org/privilege/internal/default/public" "http://tizen.org/privilege/internal/default/public"
+buxton2ctl -i -u 100 create-int32 "memory" "memory/private/mediaserver/server_status" "0" "http://tizen.org/privilege/internal/default/public" "http://tizen.org/privilege/internal/default/public"
+buxton2ctl -i -u 100 create-int32 "system" "db/mediascanner/poweroff_status" "0" "http://tizen.org/privilege/internal/default/public" "http://tizen.org/privilege/internal/default/public"
 
 %post -n libmedia-utils -p /sbin/ldconfig
 
@@ -246,6 +286,7 @@ chmod 755 /etc/gumd/useradd.d/30_media-server-add.post
 /etc/multimedia/media_content_config.ini
 /etc/multimedia/media-server-plugin
 /etc/gumd/useradd.d/30_media-server-add.post
+/etc/multimedia/.media.db
 %{_datadir}/locale/*/LC_MESSAGES/*
 %license LICENSE.APLv2.0
 
index 267332d..bb16ffe 100755 (executable)
@@ -112,6 +112,10 @@ typedef int (*DELETE_INVALID_FOLDER_BY_PATH)(void *, const char *, const 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 **);
+typedef int (*GET_EXTRACT_LIST)(void*, const char*, int, int, const char*, int, uid_t, void*, char**);
+typedef int (*UPDATE_ONE_EXTRACT_ITEM)(void*, const char*, int, void*, int, char**);
+typedef int (*QUERY_DO_UPDATE_LIST)(void*, char**);
+typedef int (*GET_MEDIA_TYPE)(void *, const char *, int *, char **);
 
 int ms_load_functions(void);
 void ms_unload_functions(void);
@@ -121,7 +125,7 @@ int ms_connect_db(void ***handle, uid_t uid);
 int ms_disconnect_db(void ***handle);
 int ms_validate_item(void **handle, const char *storage_id, const char *path, uid_t uid);
 int ms_insert_item_batch(void **handle, const char *storage_id, const char *path, uid_t uid);
-int ms_insert_item_pass2(void **handle, const char *storage_id, int storage_type, const char *path, int scan_type, int burst, uid_t uid);
+int ms_insert_item_pass2(void **handle, const char *storage_id, const char *path, int scan_type, int burst, uid_t uid);
 int ms_insert_item_immediately(void **handle, const char *storage_id, const char *path, uid_t uid);
 int ms_insert_burst_item(void **handle, const char *storage_id, const char *path, uid_t uid);
 bool ms_delete_all_items(void **handle, const char *storage_id, ms_storage_type_t store_type, uid_t uid);
@@ -140,8 +144,8 @@ int ms_get_storage_list(void **handle, GArray **storage_array);
 int ms_insert_folder(void **handle, const char *storage_id, const char *path, uid_t uid);
 int ms_delete_invalid_folder(void **handle, const char *storage_id, int storage_type, uid_t uid);
 int ms_set_folder_validity(void **handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid);
-int ms_scan_item_batch(void **handle, const char *storage_id, const char *path, uid_t uid, int *insert_count_for_partial, int *set_count_for_partial);
-int ms_scan_validate_item(void **handle, const char *storage_id, const char *path, uid_t uid, int *insert_count_for_partial, int *set_count_for_partial);
+int ms_scan_item_batch(void **handle, const char *storage_id, const char *path, uid_t uid);
+int ms_scan_validate_item(void **handle, const char *storage_id, const char *path, uid_t uid);
 int ms_get_folder_scan_status(void **handle, const char *storage_id, const char *path, int *scan_status);
 int ms_set_folder_scan_status(void **handle, const char *storage_id, const char *path, int scan_status, uid_t uid);
 int ms_check_folder_modified(void **handle, const char *path, const char *storage_id, bool *modified);
@@ -163,6 +167,8 @@ int ms_delete_invalid_folder_by_path(void **handle, const char *storage_id, cons
 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_media_type(void **handle, const char *path, int *media_type);
+
 int ms_get_delete_count();
 void ms_reset_delete_count();
 
@@ -178,4 +184,8 @@ void ms_insert_folder_end(void **handle, uid_t uid);
 void ms_update_start(void **handle);
 void ms_update_end(void **handle, uid_t uid);
 
+int ms_get_extract_list(void** handle, const char* storage_id, int storage_type, int scan_type, const char* path, int burst, uid_t uid, void* array);
+int ms_update_one_extract_item(void** handle, const char* storage_id, int storage_type, void* data, int burst);
+int ms_query_do_update_list(void** handle);
+
 #endif /*_MEDIA_COMMON_DB_SVC_H_*/
index d1b7ef3..4049022 100755 (executable)
@@ -33,4 +33,6 @@ int ms_get_stg_changed_event(void);
 int ms_read_device_info(const char *root_path, char **device_uuid);
 int ms_write_device_info(const char *root_path, char *device_uuid);
 
+int ms_get_added_stroage_path(void **handle, const char *add_path, char **device_id);
+
 #endif /*_MEDIA_SERVER_EXTERNAL_STORAGE_H_*/
index ce4e222..9886b80 100755 (executable)
 #define _MEDIA_COMMON_SYSTEM_H_
 
 #include <glib.h>
+#ifndef _USE_DEVICED_DBUS
+#include <usb-device.h>
+#include <dd-power.h>
+#include <power-defs.h>
+#include <power_tv.h>
+#endif
 
 typedef enum {
        MS_STG_REMOVED = 0,
@@ -44,8 +50,12 @@ typedef struct ms_block_info_s{
        char *mount_uuid;
 } ms_block_info_s;
 
+#ifdef _USE_DEVICED_DBUS
 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);
+#else
+int ms_sys_set_device_block_event_cb(usb_connected_cb usr_callback, void *usr_data);
+#endif
 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);
@@ -54,8 +64,13 @@ typedef struct ms_power_info_s{
        int option;
 } ms_power_info_s;
 
+#ifdef _USE_DEVICED_DBUS
 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);
+#else
+int ms_sys_set_poweroff_cb(device_changed_cb user_callback, void *user_data);
+#endif
+
 int ms_sys_unset_poweroff_cb(void);
 
 int ms_sys_get_uid(uid_t *uid);
index 00d46cf..d919a1e 100755 (executable)
@@ -41,6 +41,9 @@
 
 /*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_SERVER_STATUS "memory/private/mediaserver/server_status"
+#define MS_DB_LIMIT "file/private/mediaserver/db_limit"
+#define MS_DB_RESET "file/private/mediaserver/db_reset"
 
 /*Use for Poweroff sequence*/
 #define POWEROFF -1 /*This number uses for stopping Scannig thread*/
@@ -62,6 +65,8 @@
 #define MS_SOCK_NOT_ALLOCATE -1
 
 #define MS_INI_DEFAULT_PATH SYSCONFDIR"/multimedia/media_content_config.ini"
+#define MS_MM_CONF_PATH SYSCONFDIR"/multimedia/"
+#define MS_DUMMY_MEDIA_DB_PATH MS_MM_CONF_PATH".media.db"
 
 typedef enum {
        MS_STORAGE_INTERNAL = 0,        /**< The device's internal storage */
@@ -107,6 +112,11 @@ typedef struct {
        int pid;
 } ms_register_data_t;
 
+typedef struct ms_otg_data {
+       char *path;
+       char *device_id;
+//     ms_otg_scan_status status;
+} ms_otg_data;
 
 /*use for MS_SCANNER_STATUS */
 typedef enum {
@@ -132,6 +142,7 @@ typedef enum {
        MS_DIR_SCAN_COMPLETE                    = 7,                    /**< Complete scanning*/
 }ms_dir_scan_status_e;
 
+#if 0
 typedef enum{
        MEDIA_SCAN_PREPARE              = 0,    /**< Prepare scanning*/
        MEDIA_SCAN_PROCESSING   = 1,    /**< Process scanning*/
@@ -143,5 +154,33 @@ typedef enum{
        MEDIA_EXTRACT_STOP                      = 7,    /**< Stop extract*/
        MEDIA_EXTRACT_COMPLETE          = 8             /**< Complete extract*/
 }media_scan_status_e;
+#else
+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_EXTRACT_PREPARE           = 4,    /**< Prepare extract*/
+       MEDIA_EXTRACT_PROCESSING        = 5,    /**< Process extract*/
+       MEDIA_EXTRACT_STOP                      = 6,    /**< Stop extract*/
+       MEDIA_EXTRACT_COMPLETE          = 7             /**< Complete extract*/
+}media_scan_status_e;
+#endif
+
+typedef enum{
+       MS_MEDIA_TYPE_IMAGE     = 0,    /**< Image Content*/
+       MS_MEDIA_TYPE_VIDEO     = 1,    /**< Video Content*/
+       MS_MEDIA_TYPE_SOUND     = 2,    /**< Sound Content like Ringtone*/
+       MS_MEDIA_TYPE_MUSIC     = 3,    /**< Music Content like mp3*/
+       MS_MEDIA_TYPE_OTHER     = 4,    /**< Not media Content*/
+       MS_MEDIA_TYPE_PVR       = 5,    /**< PVR Content*/
+       MS_MEDIA_TYPE_UHD       = 6,    /**< UHD Content*/
+       MS_SVC_MEDIA_TYPE_SCSA  = 7,    /**< SCSA Content*/
+}ms_media_type_e;
+
+
+/**
+ * @}
+ */
 
 #endif /*_MEDIA_SERVER_TYPES_H_*/
index 22a9481..a82a5a9 100755 (executable)
@@ -43,6 +43,15 @@ 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 */
+int ms_get_remain_space(double *free_space);
+
+#ifdef _USE_RECORDED_CONTENT
+bool ms_is_support_pvr(void);
+#endif
+
+#ifdef _USE_SENIOR_MODE
+bool ms_is_support_senior_mode(void);
+#endif
 
 #endif/*_MEDIA_SERVER_UTILS_H__*/
 
index 339b8d9..b40ba79 100755 (executable)
@@ -39,6 +39,8 @@ static void ***func_array;
 static int lib_num;
 static void **func_handle = NULL; /*dlopen handel*/
 static int insert_count;
+int insert_count_for_partial = 0;
+int set_count_for_partial = 0;
 static int delete_count;
 
 enum func_list {
@@ -97,6 +99,10 @@ enum func_list {
        eCHECK_FOLDER_EXIST,
        eCOUNT_SUBFOLDER,
        eGET_FOLDER_ID,
+       eGET_MEDIA_TYPE,
+       eGET_EXTRACT_LIST,
+       eUPDATE_ONE_EXTRACT_ITEM,
+       eQUERY_DO_UPDATE_LIST,
        eFUNC_MAX
 };
 
@@ -211,6 +217,10 @@ int ms_load_functions(void)
                "check_folder_exist",
                "count_subfolder",
                "get_folder_id",
+               "get_media_type",
+               "get_extract_list",
+               "update_one_extract_item",
+               "query_do_update_list",
                };
        /*init array for adding name of so*/
        so_array = g_array_new(FALSE, FALSE, sizeof(char*));
@@ -448,7 +458,7 @@ int ms_validate_item(void **handle, const char *storage_id, const char *path, ui
        return res;
 }
 
-int ms_scan_validate_item(void **handle, const char *storage_id, const char *path, uid_t uid, int *insert_count_for_partial, int *set_count_for_partial)
+int ms_scan_validate_item(void **handle, const char *storage_id, const char *path, uid_t uid)
 {
        int lib_index;
        int res = MS_MEDIA_ERR_NONE;
@@ -461,12 +471,12 @@ int ms_scan_validate_item(void **handle, const char *storage_id, const char *pat
                ret = ((CHECK_ITEM_EXIST)func_array[lib_index][eEXIST])(handle[lib_index], storage_id, path, &modified, &err_msg); /*dlopen*/
                if (ret != 0) {
                        MS_SAFE_FREE(err_msg);
-                       ret = ms_scan_item_batch(handle, storage_id, path, uid, insert_count_for_partial, set_count_for_partial);
+                       ret = ms_scan_item_batch(handle, storage_id, path, uid);
                        if (ret != 0) {
                                res = MS_MEDIA_ERR_DB_INSERT_FAIL;
                        } else {
                                insert_count++;
-                               (*insert_count_for_partial)++;
+                               insert_count_for_partial++;
                        }
                } else {
                        if (modified == FALSE) {
@@ -477,7 +487,7 @@ int ms_scan_validate_item(void **handle, const char *storage_id, const char *pat
                                        MS_SAFE_FREE(err_msg);
                                        res = MS_MEDIA_ERR_DB_UPDATE_FAIL;
                                } else {
-                                       (*set_count_for_partial)++;
+                                       set_count_for_partial++;
                                }
                        } else {
                                /* the file has same name but it is changed, so we have to update DB */
@@ -487,12 +497,12 @@ int ms_scan_validate_item(void **handle, const char *storage_id, const char *pat
                                        MS_SAFE_FREE(err_msg);
                                        res = MS_MEDIA_ERR_DB_DELETE_FAIL;
                                } else {
-                                       ret = ms_scan_item_batch(handle, storage_id, path, uid, insert_count_for_partial, set_count_for_partial);
+                                       ret = ms_scan_item_batch(handle, storage_id, path, uid);
                                        if (ret != 0) {
                                                res = MS_MEDIA_ERR_DB_INSERT_FAIL;
                                        } else {
                                                insert_count++;
-                                               (*insert_count_for_partial)++;
+                                               insert_count_for_partial++;
                                        }
                                }
                        }
@@ -545,7 +555,7 @@ int ms_insert_item_batch(void **handle, const char* storage_id, const char *path
        return res;
 }
 
-int ms_scan_item_batch(void **handle, const char* storage_id, const char *path, uid_t uid, int *insert_count_for_partial, int *set_count_for_partial)
+int ms_scan_item_batch(void **handle, const char* storage_id, const char *path, uid_t uid)
 {
        int lib_index;
        int res = MS_MEDIA_ERR_NONE;
@@ -569,15 +579,15 @@ int ms_scan_item_batch(void **handle, const char* storage_id, const char *path,
        return res;
 }
 
-int ms_insert_item_pass2(void **handle, const char* storage_id, int storage_type, const char *path, int scan_type, int burst, uid_t uid)
+int ms_insert_item_pass2(void **handle, const char* storage_id, const char *path, int scan_type, int burst, uid_t uid)
 {
        int lib_index;
        int res = MS_MEDIA_ERR_NONE;
        int ret;
        char *err_msg = NULL;
-       //ms_storage_type_t storage_type;
+       ms_storage_type_t storage_type;
 
-       //storage_type = ms_get_storage_type_by_full(path, uid);
+       storage_type = ms_get_storage_type_by_full(path, uid);
 
        for (lib_index = 0; lib_index < lib_num; lib_index++) {
                ret = ((UPDATE_ITEM_EXTRACT)func_array[lib_index][eUPDATE_EXTRACT])(handle[lib_index], storage_id, storage_type, scan_type, uid, path, burst, &err_msg); /*dlopen*/
@@ -591,6 +601,67 @@ int ms_insert_item_pass2(void **handle, const char* storage_id, int storage_type
        return res;
 }
 
+int ms_get_extract_list(void** handle, const char* storage_id, int storage_type, int scan_type, const char* path, int burst, uid_t uid, void* array)
+{
+       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 = ((GET_EXTRACT_LIST)func_array[lib_index][eGET_EXTRACT_LIST])(handle[lib_index], storage_id, storage_type, scan_type, path, burst, uid, array, &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_update_one_extract_item(void** handle, const char* storage_id, int storage_type, void* data, int burst)
+{
+       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 = ((UPDATE_ONE_EXTRACT_ITEM)func_array[lib_index][eUPDATE_ONE_EXTRACT_ITEM])(handle[lib_index], storage_id, storage_type, data, burst, &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);
+                       if (ret == -2) {
+                               res = MS_MEDIA_ERR_FILE_IO;
+                       } else {
+                               res = MS_MEDIA_ERR_DB_INSERT_FAIL;
+                       }
+               }
+       }
+
+       return res;
+}
+
+int ms_query_do_update_list(void** handle)
+{
+       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 = ((QUERY_DO_UPDATE_LIST)func_array[lib_index][eQUERY_DO_UPDATE_LIST])(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_INSERT_FAIL;
+               }
+       }
+
+       return res;
+}
+
 int ms_insert_item_immediately(void **handle, const char *storage_id, const char *path, uid_t uid)
 {
        int lib_index;
@@ -713,25 +784,15 @@ int ms_send_dir_update_noti(void **handle, const char* storage_id, const char*pa
        int lib_index;
        int ret;
        char *err_msg = NULL;
-       char *get_path = NULL;
-
-       if (MS_STRING_VALID(path)) {
-               get_path = strndup(path, strlen(path));
-       } else {
-               MS_DBG_ERR("path is invalid string");
-               return MS_MEDIA_ERR_SEND_NOTI_FAIL;
-       }
 
        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, get_path, folder_id, (int)noti_type, pid, &err_msg); /*dlopen*/
+               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);
-                       MS_SAFE_FREE(get_path);
                        return MS_MEDIA_ERR_SEND_NOTI_FAIL;
                }
        }
-       MS_SAFE_FREE(get_path);
 
        return MS_MEDIA_ERR_NONE;
 }
@@ -1609,3 +1670,23 @@ int ms_get_scan_done_items(void **handle, const char *storage_id, const char *pa
 }
 #endif
 
+int ms_get_media_type(void **handle, const char *path, int *media_type)
+{
+       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 = ((GET_MEDIA_TYPE)func_array[lib_index][eGET_MEDIA_TYPE])(handle[lib_index], path, media_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;
+}
+
+
index b825f9a..40fe8a3 100755 (executable)
@@ -254,3 +254,29 @@ int ms_write_device_info(const char *root_path, char *device_uuid)
        return MS_MEDIA_ERR_NONE;
 }
 
+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;
+}
+
index c2b87c9..43112c2 100755 (executable)
 
 #include "media-common-dbg.h"
 #include "media-common-types.h"
+#include "media-common-external-storage.h"
+#include "media-common-db-svc.h"
 #include "media-common-system.h"
 
 
 //////////////////////////////////////////////////////////////////////////////
 /// CHECK THE STORATE(MMC, USB) STATE
 //////////////////////////////////////////////////////////////////////////////
-
+#ifdef _USE_DEVICED_DBUS
 #define DEVICED_BUS_NAME       "org.tizen.system.storage"
 #define DEVICED_OBJECT_PATH    "/Org/Tizen/System/Storage"
 #define DEVICED_INTERFACE_NAME DEVICED_BUS_NAME
@@ -125,7 +127,9 @@ static void __ms_block_changed(GDBusConnection* connection,
 
        MS_DBG_ERR("user callback done");
 }
+#endif
 
+#ifdef _USE_DEVICED_DBUS
 static int __ms_sys_subscribe_device_block_event(block_changed_cb usr_callback, void *usr_data)
 {
        int ret = MS_MEDIA_ERR_NONE;
@@ -182,7 +186,21 @@ ERROR:
 
        return ret;
 }
+#else
+static int __ms_sys_subscribe_device_block_event(usb_connected_cb usr_callback, void *usr_data)
+{
+       MS_DBG_FENTER();
+
+       usb_device_init();
+       usb_set_connected_cb(usr_callback, usr_data);
+
+       MS_DBG_FLEAVE();
+
+       return MS_MEDIA_ERR_NONE;
+}
+#endif
 
+#ifdef _USE_DEVICED_DBUS
 int ms_sys_set_device_block_event_cb(block_changed_cb usr_callback, void *usr_data)
 {
        int ret = MS_MEDIA_ERR_NONE;
@@ -193,7 +211,20 @@ int ms_sys_set_device_block_event_cb(block_changed_cb usr_callback, void *usr_da
 
        return ret;
 }
+#else
+int ms_sys_set_device_block_event_cb(usb_connected_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;
+}
+#endif
 
+#ifdef _USE_DEVICED_DBUS
 int ms_sys_unset_device_block_event_cb(void)
 {
        if (g_usb_bus == NULL) {
@@ -209,7 +240,16 @@ int ms_sys_unset_device_block_event_cb(void)
 
        return MS_MEDIA_ERR_NONE;
 }
+#else
+int ms_sys_unset_device_block_event_cb(void)
+{
+       usb_device_fini();
+
+       return MS_MEDIA_ERR_NONE;
+}
+#endif
 
+#ifdef _USE_DEVICED_DBUS
 #define DBUS_REPLY_TIMEOUT (-1)
 static int __ms_gdbus_method_sync(const char *dest, const char *path, const char *interface, const char *method, const char *param, GArray **dev_list)
 {
@@ -329,7 +369,9 @@ static int __ms_gdbus_method_sync(const char *dest, const char *path, const char
 
        return result;
 }
+#endif
 
+#ifdef _USE_DEVICED_DBUS
 int ms_sys_get_device_list(ms_stg_type_e stg_type, GArray **dev_list)
 {
        int ret;
@@ -351,6 +393,78 @@ int ms_sys_get_device_list(ms_stg_type_e stg_type, GArray **dev_list)
 
        return MS_MEDIA_ERR_NONE;
 }
+#else
+int ms_sys_get_device_list(ms_stg_type_e stg_type, GArray **dev_list)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       int i = 0;
+       int count = 0;
+       int err = 0;
+       usb_device_list_h list;
+       usb_device_h device;
+       char *mount_path = NULL;
+       void **handle = NULL;
+
+       ret = ms_load_functions();
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_load_functions failed [%d]", ret);
+               return ret;
+       }
+
+       err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
+       if (err == 0) {
+               count = usb_device_list_get_count(list);
+               if (count > 0) {
+                       if (*dev_list == NULL) {
+                               MS_DBG_ERR("DEV LIST IS NULL");
+                               *dev_list = g_array_new(FALSE, FALSE, sizeof(ms_block_info_s*));
+                       }
+
+                       for (i = 0; i < count; i++) {
+                               if (i == 0) {
+                                       err = usb_device_list_get_first(list, &device);
+                                       if(err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
+                                               mount_path = usb_device_get_mountpath(device);
+                                               if (mount_path != NULL) {
+                                                       MS_DBG_ERR("mount_path [%s]", mount_path);
+                                               }
+                                       }
+                               } else {
+                                       usb_device_list_get_next(list, &device);
+                                       if(device != NULL) {
+                                               mount_path = usb_device_get_mountpath(device);
+                                               if (mount_path != NULL) {
+                                                       MS_DBG_ERR("mount_path [%s]", mount_path);
+                                               }
+                                       }
+                               }
+
+                               char *storage_id = NULL;
+                               ms_block_info_s *data = NULL;
+
+                               err = ms_get_added_stroage_path(handle, mount_path, &storage_id);
+                               if ((err == MS_MEDIA_ERR_NONE) && (storage_id != NULL)) {
+                                       data = malloc(sizeof(ms_block_info_s));
+                                       data->mount_path = strdup(mount_path);
+                                       data->mount_uuid = strdup(storage_id);
+                                       g_array_append_val(*dev_list, data);
+                               } else {
+                                       MS_DBG_ERR("ms_get_added_stroage_path failed [%d]", err);
+                               }
+                       }
+               }
+
+               usb_device_free_device_list(list);
+       } else {
+               MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
+               ret = MS_MEDIA_ERR_INTERNAL;
+       }
+
+       ms_unload_functions();
+
+       return ret;
+}
+#endif
 
 int ms_sys_release_device_list(GArray **dev_list)
 {
@@ -393,7 +507,7 @@ int ms_sys_get_uid(uid_t *uid)
 //////////////////////////////////////////////////////////////////////////////
 /// CHECK THE POWER STATE
 //////////////////////////////////////////////////////////////////////////////
-
+#ifdef _USE_DEVICED_DBUS
 #define POWER_DBUS_NAME "ChangeState"
 #define POWER_DBUS_PATH "/Org/Tizen/System/DeviceD/PowerOff"
 #define POWER_DBUS_INTERFACE "org.tizen.system.deviced.PowerOff"
@@ -497,7 +611,21 @@ int ms_sys_set_poweroff_cb(power_off_cb user_callback, void *user_data)
 
        return MS_MEDIA_ERR_NONE;
 }
+#else
+device_changed_cb g_user_callback;
 
+int ms_sys_set_poweroff_cb(device_changed_cb user_callback, void *user_data)
+{
+       MS_DBG_FENTER();
+       device_power_subscribe_standby(user_callback, user_data);
+       g_user_callback = user_callback;
+       MS_DBG_FLEAVE();
+
+       return MS_MEDIA_ERR_NONE;
+}
+#endif
+
+#ifdef _USE_DEVICED_DBUS
 int ms_sys_unset_poweroff_cb(void)
 {
        if (g_pwr_bus == NULL) {
@@ -512,4 +640,13 @@ int ms_sys_unset_poweroff_cb(void)
 
        return MS_MEDIA_ERR_NONE;
 }
+#else
+int ms_sys_unset_poweroff_cb(void)
+{
+       device_power_unsubscribe_standby(g_user_callback);
+       g_user_callback = NULL;
+
+       return MS_MEDIA_ERR_NONE;
+}
+#endif
 
index b74ec32..f6e063b 100755 (executable)
@@ -28,6 +28,9 @@
 #include <dbus/dbus-glib.h>
 #include <dbus/dbus.h>
 #include <dbus/dbus-glib-lowlevel.h>
+#include <sys/statvfs.h>
+#include <sys/stat.h>
+#include <system_info.h>
 
 #include "media-util.h"
 #include "media-server-ipc.h"
@@ -44,6 +47,25 @@ struct timeval g_mmc_end_time;
 
 #define MS_DRM_CONTENT_TYPE_LENGTH 100
 
+/* it's for 32bit file offset */
+struct statvfs_32 {
+        unsigned long int f_bsize;
+        unsigned long int f_frsize;
+        unsigned long int f_blocks;
+        unsigned long int f_bfree;
+        unsigned long int f_bavail;
+        unsigned long int f_files;
+        unsigned long int f_ffree;
+        unsigned long int f_favail;
+        unsigned long int f_fsid;
+#ifdef _STATVFSBUF_F_UNUSED
+        int __f_unused;
+#endif
+        unsigned long int f_flag;
+        unsigned long int f_namemax;
+        int __f_spare[6];
+};
+
 #ifdef FMS_PERF
 void ms_check_start_time(struct timeval *start_time)
 {
@@ -133,6 +155,14 @@ ms_storage_type_t ms_get_storage_type_by_full(const char *path, uid_t uid)
        if (path == NULL)
                return MS_MEDIA_ERR_INVALID_PATH;
 
+#ifdef _USE_SENIOR_MODE
+       if(ms_is_support_senior_mode()) {
+               if (strncmp(path, MEDIA_ROOT_PATH_SENIOR_MODE, strlen(MEDIA_ROOT_PATH_SENIOR_MODE)) == 0) {
+                       return MS_STORAGE_EXTERNAL;
+               }
+       }
+#endif
+
        user_path = __media_get_path(uid);
        if (user_path == NULL)
                return MS_MEDIA_ERR_OUT_OF_MEMORY;
@@ -297,3 +327,81 @@ bool ms_config_get_bool(const char *key, int *value)
 
        return false;
 }
+
+static int get_memory_size(const char *path, struct statvfs_32 *buf)
+{
+       struct statvfs s;
+       int ret;
+
+       ret = statvfs(path, &s);
+       if (ret) {
+               MS_DBG_ERR("statvfs failed[%d]", ret);
+               MS_DBG_STRERROR();
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       buf->f_bsize  = s.f_bsize;
+       buf->f_frsize = s.f_frsize;
+       buf->f_blocks = (unsigned long)s.f_blocks;
+       buf->f_bfree  = (unsigned long)s.f_bfree;
+       buf->f_bavail = (unsigned long)s.f_bavail;
+       buf->f_files  = (unsigned long)s.f_files;
+       buf->f_ffree  = (unsigned long)s.f_ffree;
+       buf->f_favail = (unsigned long)s.f_favail;
+       buf->f_fsid = s.f_fsid;
+       buf->f_flag = s.f_flag;
+       buf->f_namemax = s.f_namemax;
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+int ms_get_remain_space(double *free_space)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       struct statvfs_32 temp;
+
+       ret = get_memory_size("/opt", &temp);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("fail to get memory size");
+               return ret;
+       }
+
+//     MS_DBG_ERR("Total mem : %lf, Avail mem : %lf", (double)temp.f_frsize*temp.f_blocks, (double)temp.f_bsize*temp.f_bavail);
+
+       *free_space = (double)temp.f_bsize*temp.f_bavail;
+
+       return ret;
+}
+
+#ifdef _USE_RECORDED_CONTENT
+bool ms_is_support_pvr(void)
+{
+
+       int nSupportPVR = 0;
+       if (system_info_get_value_int(SYSTEM_INFO_KEY_PVR_SUPPORTED, &nSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
+               MS_DBG_ERR("Get PVR Support failed");
+               return false;
+       }
+
+       MS_DBG("PVR Support : [%d]", nSupportPVR);
+
+       return (nSupportPVR != 0);
+}
+#endif
+
+#ifdef _USE_SENIOR_MODE
+bool ms_is_support_senior_mode()
+{
+       bool bSupportSeniorMode = false;
+
+       if(system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
+               MS_DBG_ERR("Get senior mode support failed");
+               return false;
+       }
+
+       MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
+
+       return bSupportSeniorMode;
+}
+#endif
+
diff --git a/src/scanner-v2/include/media-scanner-db-manage-v2.h b/src/scanner-v2/include/media-scanner-db-manage-v2.h
new file mode 100755 (executable)
index 0000000..67d3969
--- /dev/null
@@ -0,0 +1,32 @@
+/*\r
+ *  Media Server\r
+ *\r
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.\r
+ *\r
+ * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ */\r
+\r
+#ifndef _MEDIA_SCANNER_DB_MANAGER_V2_H_\r
+#define _MEDIA_SCANNER_DB_MANAGER_V2_H_\r
+\r
+#include "media-common-types.h"\r
+\r
+int msc_check_size_mediadb(uid_t uid, double *db_size);\r
+int msc_check_db_size(uid_t uid, int scan_type);\r
+int msc_cleanup_invalid_values(uid_t uid);\r
+\r
+#endif\r
+\r
index b70f6d6..64cad9d 100755 (executable)
 
 #include "media-common-system.h"
 
+#ifdef _USE_DEVICED_DBUS
 void msc_device_block_changed_cb(ms_block_info_s *block_info, void *user_data);
+#else
+void msc_device_block_changed_cb(usb_device_h usb_device, char *action, void *user_data);
+#endif
 
 #endif
\ No newline at end of file
index 4946e4b..d42d05f 100755 (executable)
 #ifndef _MEDIA_SCANNER_EXTRACT_V2_H_
 #define _MEDIA_SCANNER_EXTRACT_V2_H_
 
+typedef struct _s_extract_item{
+       char* path;
+       int pid;
+}s_extract_item;
+
 typedef enum {
        MS_EXTRACT_STORAGE = 0,
        MS_EXTRACT_DIRECTORY = 1,
@@ -34,13 +39,22 @@ 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, uid_t uid);
 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_item(s_extract_item** item, const char* path, int pid);
+int _msc_del_extract_item(s_extract_item** item);
+
+int msc_set_cancel_extract_item(const char* path, int pid);
+int msc_del_cancel_extract_item();
+
+int msc_set_cur_extract_item(const char* path, int pid);
+int msc_del_cur_extract_item();
+
 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);
+int msc_set_extract_cancel_path(const char *cancel_path);
 
 #endif /*_MEDIA_SCANNER_EXTRACT_V2_H_*/
index 261fb1f..91845aa 100755 (executable)
 #ifndef _MEDIA_SCANNER_SCAN_V2_H_
 #define _MEDIA_SCANNER_SCAN_V2_H_
 
-#include "media-common-system.h"
+typedef struct _s_scan_item {
+       char* path;
+       int pid;
+} s_scan_item;
 
 typedef enum {
        MS_SCAN_STORAGE = 0,
@@ -36,18 +39,26 @@ 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_scan_item(s_scan_item** item, const char *path, int pid);
+int _msc_del_scan_item(s_scan_item** item);
+
+int msc_set_cancel_scan_item(const char *path, int pid);
+int msc_del_cancel_scan_item(void);
+
+int msc_set_cur_scan_item(const char *path, int pid);
+int msc_del_cur_scan_item(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);
+int msc_set_cancel_path(const char *cancel_path);
 
 #endif /*_MEDIA_SCANNER_SCAN_V2_H_*/
\ No newline at end of file
index 9b68c20..0248b53 100755 (executable)
@@ -28,6 +28,6 @@
 gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer data);
 int msc_send_ready(void);
 int msc_send_result(int result, ms_comm_msg_s *scan_data);
-int msc_send_result_partial(int result, ms_msg_type_e msg_type, int pid, char *msg);
+int msc_send_result_partial(int result, ms_msg_type_e msg_type, int pid, const char *msg);
 
 #endif /*_MEDIA_SCANNER_SOCKET_V2_H_*/
diff --git a/src/scanner-v2/media-scanner-db-manage-v2.c b/src/scanner-v2/media-scanner-db-manage-v2.c
new file mode 100755 (executable)
index 0000000..0242509
--- /dev/null
@@ -0,0 +1,302 @@
+/*\r
+ *  Media Server\r
+ *\r
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.\r
+ *\r
+ * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ */\r
+\r
+#include <errno.h>\r
+#include <sys/stat.h>\r
+#include <sqlite3.h>\r
+\r
+#include "media-util.h"\r
+#include "media-common-utils.h"\r
+#include "media-common-system.h"\r
+#include "media-common-db-svc.h"\r
+#include "media-scanner-dbg-v2.h"\r
+#include "media-scanner-db-manage-v2.h"\r
+\r
+int msc_check_size_mediadb(uid_t uid, double *db_size)\r
+{\r
+       int ret = MS_MEDIA_ERR_NONE;\r
+       char *db_path = NULL;\r
+       struct stat buf;\r
+\r
+       ret = media_db_get_media_db_path(uid, &db_path);\r
+\r
+       if(stat(db_path, &buf) == 0) {\r
+               *db_size = buf.st_size;\r
+       } else {\r
+               MS_DBG_STRERROR("stat failed");\r
+               ret = MS_MEDIA_ERR_INTERNAL;\r
+       }\r
+\r
+       MS_SAFE_FREE(db_path);\r
+\r
+       return ret;\r
+}\r
+\r
+int msc_check_db_size(uid_t uid, int scan_type)\r
+{\r
+       double db_size = 0.0;\r
+       double free_space = 0.0;\r
+       int err = MS_MEDIA_ERR_NONE;\r
+\r
+       msc_check_size_mediadb(uid, &db_size);\r
+\r
+       if (scan_type == MS_MSG_STORAGE_ALL\r
+               || scan_type == MS_MSG_STORAGE_PARTIAL) {\r
+               if (db_size >= MEDIA_DB_SIZE_LIMIT_1) {\r
+                       MS_DBG_ERR("DB SIZE [%lf] reach the MEDIA_DB_SIZE_LIMIT_1[%lf]", db_size, MEDIA_DB_SIZE_LIMIT_1);\r
+                       return MS_MEDIA_ERR_DB_LIMIT_1;\r
+               }\r
+       } else if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE\r
+               || scan_type == MS_MSG_DIRECTORY_SCANNING) {\r
+               if (db_size >= MEDIA_DB_SIZE_LIMIT_2) {\r
+                       MS_DBG_ERR("DB SIZE [%lf] reach the MEDIA_DB_SIZE_LIMIT_2[%lf]", db_size, MEDIA_DB_SIZE_LIMIT_2);\r
+\r
+                       MS_DBG_ERR("START REMOVE INVALID DATA");\r
+                       msc_cleanup_invalid_values(uid);\r
+                       MS_DBG_ERR("END REMOVE INVALID DATA");\r
+\r
+                       msc_check_size_mediadb(uid, &db_size);\r
+                       if (db_size >= MEDIA_DB_SIZE_LIMIT_2) {\r
+                               ms_config_set_int(MS_DB_LIMIT, 0);\r
+                               MS_DBG_ERR("DB IS FULL. CANNOT UPDATE");\r
+                               return MS_MEDIA_ERR_DB_FULL_FAIL;\r
+                       }\r
+               }\r
+       }\r
+\r
+       /*check remain space*/\r
+       err = ms_get_remain_space(&free_space);\r
+       if (err != MS_MEDIA_ERR_NONE) {\r
+               MS_DBG_ERR("ms_get_remain_space failed");\r
+       } else {\r
+               if (free_space < MEDIA_DB_SPACE_LIMIT) {\r
+                       MS_DBG_ERR("FREE SPACE [%lf] DB SIZE [%lf]", free_space, db_size);\r
+                       return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;\r
+               }\r
+       }\r
+\r
+       return MS_MEDIA_ERR_NONE;\r
+}\r
+\r
+#define MEDIA_DB_TABLE_STORAGE "storage"\r
+\r
+static int __msc_get_invalid_storage(void *handle, GArray **storage_list)\r
+{\r
+       int ret = MS_MEDIA_ERR_NONE;\r
+       sqlite3_stmt *sql_stmt = NULL;\r
+       char *sql = NULL;\r
+       char *storage_uuid = NULL;\r
+       int err = -1;\r
+\r
+       sql = sqlite3_mprintf("SELECT storage_uuid FROM '%s' WHERE validity=0", MEDIA_DB_TABLE_STORAGE);\r
+       if (sql == NULL) {\r
+               MS_DBG_ERR("sqlite3_mprintf failed");\r
+               return MS_MEDIA_ERR_OUT_OF_MEMORY;\r
+       }\r
+\r
+       err = sqlite3_prepare_v2(handle, sql, -1, &sql_stmt, NULL);\r
+       sqlite3_free((char *)sql);\r
+\r
+       if (err != SQLITE_OK) {\r
+               MS_DBG_ERR("prepare error %d[%s]", err, sqlite3_errmsg(handle));\r
+               return MS_MEDIA_ERR_DB_READ_FAIL;\r
+       }\r
+\r
+       if (*storage_list == NULL) {\r
+               *storage_list = g_array_new (FALSE, FALSE, sizeof (char*));\r
+       }\r
+\r
+       while(sqlite3_step(sql_stmt) == SQLITE_ROW)\r
+       {\r
+               storage_uuid = strdup((const char *)sqlite3_column_text(sql_stmt, 0));\r
+               MS_DBG_ERR("INVALID STORAGE ID : %s", storage_uuid);\r
+               g_array_append_val (*storage_list, storage_uuid);\r
+       }\r
+\r
+       if (sql_stmt) sqlite3_finalize(sql_stmt);\r
+\r
+       return ret;\r
+}\r
+\r
+static int __msc_cleanup_invalid_storage_table(void *handle, GArray *storage_list)\r
+{\r
+       int ret = MS_MEDIA_ERR_NONE;\r
+       int i = 0;\r
+       int count = 0;\r
+       char *storage_uuid = NULL;\r
+       char *sql = NULL;\r
+       char *zErrMsg = NULL;\r
+\r
+       if (storage_list == NULL || storage_list->len == 0) {\r
+               MS_DBG_ERR("There is no invalid storage");\r
+       } else {\r
+               count = storage_list->len;\r
+\r
+               for (i = 0; i < count; i ++) {\r
+                       storage_uuid = g_array_index(storage_list, char*, i);\r
+                       sql = sqlite3_mprintf("drop table '%s'", storage_uuid);\r
+                       ret = sqlite3_exec(handle, sql, NULL, NULL, &zErrMsg);\r
+                       if (SQLITE_OK != ret) {\r
+                               MS_DBG_ERR("DB UPDATE FAILED [%s]", zErrMsg);\r
+                       }\r
+                       sqlite3_free(sql);\r
+               }\r
+       }\r
+\r
+       return ret;\r
+}\r
+\r
+static int __msc_cleanup_invalid_folder(void *handle, GArray *storage_list)\r
+{\r
+       int ret = MS_MEDIA_ERR_NONE;\r
+       int i = 0;\r
+       int count = 0;\r
+       char *storage_uuid = NULL;\r
+       char *sql = NULL;\r
+       char *zErrMsg = NULL;\r
+\r
+       if (storage_list == NULL || storage_list->len == 0) {\r
+               MS_DBG_ERR("There is no invalid storage");\r
+       } else {\r
+               count = storage_list->len;\r
+\r
+               for (i = 0; i < count; i ++) {\r
+                       storage_uuid = g_array_index(storage_list, char*, i);\r
+                       sql = sqlite3_mprintf("delete from folder where storage_uuid = '%s'", storage_uuid);\r
+                       ret = sqlite3_exec(handle, sql, NULL, NULL, &zErrMsg);\r
+                       if (SQLITE_OK != ret) {\r
+                               MS_DBG_ERR("DB UPDATE FAILED [%s]", zErrMsg);\r
+                       }\r
+                       sqlite3_free(sql);\r
+               }\r
+       }\r
+\r
+       return ret;\r
+}\r
+\r
+static int __msc_cleanup_invalid_storage(void *handle, GArray *storage_list)\r
+{\r
+       int ret = MS_MEDIA_ERR_NONE;\r
+       int i = 0;\r
+       int count = 0;\r
+       char *storage_uuid = NULL;\r
+       char *sql = NULL;\r
+       char *zErrMsg = NULL;\r
+\r
+       if (storage_list == NULL || storage_list->len == 0) {\r
+               MS_DBG_ERR("There is no invalid storage");\r
+       } else {\r
+               count = storage_list->len;\r
+\r
+               for (i = 0; i < count; i ++) {\r
+                       storage_uuid = g_array_index(storage_list, char*, i);\r
+                       sql = sqlite3_mprintf("delete from storage where storage_uuid = '%s'", storage_uuid);\r
+                       ret = sqlite3_exec(handle, sql, NULL, NULL, &zErrMsg);\r
+                       if (SQLITE_OK != ret) {\r
+                               MS_DBG_ERR("DB UPDATE FAILED [%s]", zErrMsg);\r
+                       }\r
+                       sqlite3_free(sql);\r
+               }\r
+       }\r
+\r
+       return ret;\r
+}\r
+\r
+static int __msc_cleanup_start(void *handle)\r
+{\r
+       int ret = MS_MEDIA_ERR_NONE;\r
+       char *zErrMsg = NULL;\r
+       char *sql = NULL;\r
+\r
+       sql = sqlite3_mprintf("%s", "BEGIN IMMEDIATE;");\r
+\r
+       ret = sqlite3_exec(handle, sql, NULL, NULL, &zErrMsg);\r
+       if (SQLITE_OK != ret) {\r
+               MS_DBG_ERR("DB UPDATE FAILED [%s]", zErrMsg);\r
+       }\r
+\r
+       sqlite3_free(sql);\r
+\r
+       return ret;\r
+}\r
+\r
+static int __msc_cleanup_end(void *handle)\r
+{\r
+       int ret = MS_MEDIA_ERR_NONE;\r
+       char *zErrMsg = NULL;\r
+       char *sql = NULL;\r
+\r
+       sql = sqlite3_mprintf("%s", "COMMIT;");\r
+\r
+       ret = sqlite3_exec(handle, sql, NULL, NULL, &zErrMsg);\r
+       if (SQLITE_OK != ret) {\r
+               MS_DBG_ERR("DB UPDATE FAILED [%s]", zErrMsg);\r
+       }\r
+\r
+       sqlite3_free(sql);\r
+\r
+       return ret;\r
+}\r
+\r
+int msc_cleanup_invalid_values(uid_t uid)\r
+{\r
+       int i = 0;\r
+       int count = 0;\r
+       void *handle = NULL;\r
+       GArray *storage_list = NULL;\r
+       double db_size = 0.0;\r
+       char *storage_uuid = NULL;\r
+\r
+       MS_DBG_ERR("START CLEANUP MEDIA DB");\r
+\r
+       media_db_connect(&handle, uid, TRUE);\r
+       \r
+       __msc_get_invalid_storage(handle, &storage_list);\r
+\r
+       if (storage_list != NULL && storage_list->len != 0) {\r
+               __msc_cleanup_start(handle);\r
+               __msc_cleanup_invalid_storage_table(handle, storage_list);\r
+               __msc_cleanup_invalid_folder(handle, storage_list);\r
+               __msc_cleanup_invalid_storage(handle, storage_list);\r
+               __msc_cleanup_end(handle);\r
+\r
+               count = storage_list->len;\r
+\r
+       } else {\r
+               MS_DBG_ERR("There is no invalid storage");\r
+       }\r
+\r
+       media_db_disconnect(handle);\r
+\r
+       msc_check_size_mediadb(uid, &db_size);\r
+       MS_DBG_ERR("END CLEANUP MEDIA DB[%lf]", db_size);\r
+\r
+       for (i = 0; i < count; i ++) {\r
+               storage_uuid = g_array_index(storage_list, char*, 0);\r
+               MS_SAFE_FREE(storage_uuid);\r
+               g_array_remove_index(storage_list, 0);\r
+       }\r
+\r
+       g_array_free(storage_list, FALSE);\r
+\r
+       return MS_MEDIA_ERR_NONE;\r
+}\r
+\r
index ed8cd73..9c060fc 100755 (executable)
 #include "media-scanner-scan-v2.h"
 #include "media-scanner-extract-v2.h"
 #include "media-scanner-device-block-v2.h"
+#include "media-scanner-common-v2.h"
 
+#ifdef _USE_DEVICED_DBUS
 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;
@@ -79,6 +82,7 @@ static void __msc_usb_remove_event(const char *mount_path)
 
        return;
 }
+#endif
 
 int msc_mmc_remove_handler(const char *mount_path)
 {
@@ -99,7 +103,7 @@ void _msc_mmc_changed_event(const char *mount_path, ms_stg_status_e mount_status
        return;
 }
 
-
+#ifdef _USE_DEVICED_DBUS
 void msc_device_block_changed_cb(ms_block_info_s *block_info, void *user_data)
 {
        if (block_info->block_type == 0) {
@@ -114,3 +118,62 @@ void msc_device_block_changed_cb(ms_block_info_s *block_info, void *user_data)
                _msc_mmc_changed_event(block_info->mount_path, block_info->state);
        }
 }
+#else
+extern GAsyncQueue *storage_queue2;
+extern GAsyncQueue *storage_extract_queue;
+extern int g_directory_scan_processing2;
+extern bool g_directory_extract_processing;
+
+void msc_device_block_changed_cb(usb_device_h usb_device, char *action, void *user_data)
+{
+       char *mount_path = NULL;
+       MS_DBG_ERR("_usb_event_callback BEGIN");
+
+       if (!usb_device) {
+               MS_DBG_ERR("usb_device is NULL");
+               return;
+       }
+
+       if (usb_device_get_class(usb_device) != USB_MASS_STORAGE) {
+               free_usb_device_h(usb_device);
+               MS_DBG_ERR("usb_device is NOT STORAGE");
+               return;
+       }
+
+       mount_path = usb_device_get_mountpath(usb_device);
+
+       if (!strcmp(action, "blocked")) {
+               MS_DBG_ERR("===========================================================");
+               MS_DBG_ERR("USB BLOCKED, mountpath : %s", mount_path);
+               MS_DBG_ERR("===========================================================");
+               int status  = -1;
+
+               if(!ms_config_get_int(VCONFKEY_FILEMANAGER_DB_STATUS, &status)) {
+                       MS_DBG_ERR("ms_config_get_int[VCONFKEY_FILEMANAGER_DB_STATUS]");
+               }
+
+               if (!(g_async_queue_length(storage_queue2) == 0 && status == VCONFKEY_FILEMANAGER_DB_UPDATED)) {
+                       msc_set_blocked_path(mount_path);
+               }
+
+               if (g_directory_scan_processing2 != 0) {
+                       MS_DBG_ERR("Doing directory scanning. Set cancel path");
+                       msc_set_cancel_scan_item(mount_path, -1);
+               }
+
+               if (!(g_async_queue_length(storage_extract_queue) == 0 && status == VCONFKEY_FILEMANAGER_DB_UPDATED)) {
+                       msc_set_extract_blocked_path(mount_path);
+               }
+
+               if (g_directory_extract_processing == true) {
+                       MS_DBG_ERR("Doing directory extracting. Set cancel path");
+                       msc_set_cancel_extract_item(mount_path, -1);
+               }
+       }
+
+       free_usb_device_h(usb_device);
+       MS_DBG_ERR("_usb_event_callback END");
+
+       return;
+}
+#endif
index c83d3ea..2720038 100755 (executable)
 #include "media-common-db-svc.h"
 #include "media-scanner-dbg-v2.h"
 #include "media-scanner-common-v2.h"
+#include "media-scanner-db-manage-v2.h"
 #include "media-scanner-socket-v2.h"
 #include "media-scanner-extract-v2.h"
 
+extern bool power_off2;
 GAsyncQueue *storage_extract_queue;
 GAsyncQueue *folder_extract_queue;
 GMutex extract_req_mutex;
@@ -51,6 +53,10 @@ char *g_extract_blocked_path;
 bool g_directory_extract_processing;
 int stg_extract_status;
 
+GMutex extract_item_mutex;
+s_extract_item* cancel_extract_item = NULL;
+s_extract_item* cur_extract_item = NULL;
+
 GCond extract_data_cond;
 GMutex extract_data_mutex;
 #define VCONFKEY_PRIVATE_EXTRACTSTATUS "db/private/extractstatus"
@@ -61,13 +67,22 @@ 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_check_extract_stop_status(int scan_type, ms_storage_type_t storage_type, const char *start_path, int pid);
 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);
 
+typedef struct{
+       int media_type;
+       char *path;
+}ms_item_info_s;
+
+#define SAFE_FREE(src)      { if(src) {free(src); src = NULL;}}
+
+static int __msc_folder_bulk_extract(void **handle, const char* storage_id, int storage_type, const char *path, int scan_type, int burst, int pid, uid_t uid, unsigned int *io_err_count);
+
 int msc_init_extract_thread()
 {
        if (!storage_extract_queue) storage_extract_queue = g_async_queue_new();
@@ -77,6 +92,8 @@ int msc_init_extract_thread()
        g_mutex_init(&extract_blocked_mutex);
        g_mutex_init(&extract_data_mutex);
 
+       g_mutex_init(&extract_item_mutex);
+
        g_cond_init(&extract_data_cond);
 
        return MS_MEDIA_ERR_NONE;
@@ -91,11 +108,81 @@ int msc_deinit_extract_thread()
        g_mutex_clear(&extract_blocked_mutex);
        g_mutex_clear(&extract_data_mutex);
 
+       g_mutex_clear(&extract_item_mutex);
+
        g_cond_clear(&extract_data_cond);
 
        return MS_MEDIA_ERR_NONE;
 }
 
+int __msc_folder_bulk_extract(void **handle, const char* storage_id, int storage_type, const char *path, int scan_type, int burst, int pid, uid_t uid, unsigned int *io_err_count)
+{
+       MS_DBG_ERR("begin of __msc_folder_bulk_extract");
+       int ret = MS_MEDIA_ERR_NONE;
+       int tmp_ret = MS_MEDIA_ERR_NONE;
+       GArray *data_array = NULL;
+       ms_item_info_s* db_data = NULL;
+       int extract_count = 0;
+
+       ret = ms_get_extract_list(handle, storage_id, storage_type, scan_type, path, burst, uid, (void*)&data_array);
+       if(ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_get_extract_list failed!!!\n");
+               return ret;
+       }
+
+       if(data_array == NULL) {
+               MS_DBG_ERR("data_array is NULL!!!\n");
+               return MS_MEDIA_ERR_INTERNAL;
+       }
+
+       while (data_array->len != 0) {
+               tmp_ret = MS_MEDIA_ERR_NONE;
+               db_data = NULL;
+               db_data = g_array_index(data_array, ms_item_info_s*, 0);
+               g_array_remove_index (data_array, 0);
+
+               tmp_ret = ms_update_one_extract_item(handle, storage_id, storage_type, db_data, burst);
+               if(tmp_ret == MS_MEDIA_ERR_NONE) {
+                       extract_count++;
+               } else if (tmp_ret == MS_MEDIA_ERR_FILE_IO) {
+                       (*io_err_count)++;
+               }
+
+               if(db_data) {
+                       SAFE_FREE(db_data->path);
+                       free(db_data);
+                       db_data = NULL;
+               }
+
+               tmp_ret = __msc_check_extract_stop_status(scan_type, storage_type, path, pid);
+               if (tmp_ret == MS_MEDIA_ERR_SCANNER_FORCE_STOP) {
+                       MS_DBG_ERR("__msc_folder_bulk_extract MS_MEDIA_ERR_SCANNER_FORCE_STOP");
+                       break;
+               }
+       }
+
+       if(extract_count > 0) {
+               ret = ms_query_do_update_list(handle);
+       }
+
+       while (data_array->len != 0) {
+               db_data = NULL;
+               db_data = g_array_index(data_array, ms_item_info_s*, 0);
+               g_array_remove_index (data_array, 0);
+
+               if(db_data) {
+                       SAFE_FREE(db_data->path);
+                       free(db_data);
+                       db_data = NULL;
+               }
+       }
+       g_array_free(data_array, FALSE);
+       data_array = NULL;
+
+       MS_DBG_ERR("end of __msc_folder_bulk_extract");
+       return ret;
+}
+
 gboolean msc_folder_extract_thread(void *data)
 {
        ms_comm_msg_s *extract_data = NULL;
@@ -106,7 +193,8 @@ gboolean msc_folder_extract_thread(void *data)
        int storage_type;
        char *storage_id = NULL;
        char *update_path = NULL;
-       bool power_off_status = FALSE;
+       unsigned int io_err_count = 0;
+       uid_t uid = MEDIA_DEFAULT_UID;
 
        while (1) {
                extract_data = g_async_queue_pop(folder_extract_queue);
@@ -115,12 +203,16 @@ gboolean msc_folder_extract_thread(void *data)
                        goto _POWEROFF;
                }
 
-               MS_DBG_ERR("[No-Error] DIRECTORY EXTRACT START folder_path [%s], scan_type [%d]", extract_data->msg, extract_data->msg_type);
+               MS_DBG_ERR("DIRECTORY EXTRACT START [%s %d]", extract_data->msg, extract_data->msg_type);
 
+               io_err_count = 0;
+
+               msc_set_cur_extract_item(extract_data->msg, extract_data->pid);
                g_directory_extract_processing = true;
+               uid = extract_data->uid;
 
                /*connect to media db, if conneting is failed, db updating is stopped*/
-               err = ms_connect_db(&handle, extract_data->uid);
+               err = ms_connect_db(&handle, uid);
                if (err != MS_MEDIA_ERR_NONE)
                        continue;
 
@@ -150,8 +242,8 @@ gboolean msc_folder_extract_thread(void *data)
                        goto NEXT;
                }
 
-               storage_type = ms_get_storage_type_by_full(extract_data->msg, extract_data->uid);
-               ret = __msc_check_extract_stop_status(extract_data->msg_type, storage_type, update_path);
+               storage_type = ms_get_storage_type_by_full(extract_data->msg, uid);
+               ret = __msc_check_extract_stop_status(extract_data->msg_type, storage_type, update_path, extract_data->pid);
 
                if (ret == MS_MEDIA_ERR_SCANNER_FORCE_STOP) {
                        MS_DBG_ERR("MS_MEDIA_ERR_SCANNER_FORCE_STOP");
@@ -161,12 +253,20 @@ gboolean msc_folder_extract_thread(void *data)
                /*insert data into media db */
                int end_flag = extract_data->result ? LAST_EVENT : NORMAL_EVENT;
 
-               ret = ms_insert_item_pass2(handle, extract_data->storage_id, storage_type, update_path, scan_type, end_flag, extract_data->uid);
-               msc_del_extract_cancel_path();
+               ret = msc_check_db_size(uid, extract_data->msg_type);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("NOT ENOUGH MEMORY");
+                       if (ret == MS_MEDIA_ERR_DB_FULL_FAIL) {
+                               ret = MS_MEDIA_ERR_NONE;
+                       }
+                       goto NEXT;
+               }
+
+               //ret = ms_insert_item_pass2(handle, extract_data->storage_id, storage_type, update_path, scan_type, end_flag);
+               ret = __msc_folder_bulk_extract(handle, extract_data->storage_id, storage_type, update_path, scan_type, end_flag, extract_data->pid, uid, &io_err_count);
 
 NEXT:
-               msc_get_power_status(&power_off_status);
-               if (power_off_status) {
+               if (power_off2) {
                        MS_DBG_ERR("power off");
                        goto _POWEROFF;
                }
@@ -184,8 +284,10 @@ NEXT:
                MS_SAFE_FREE(storage_id);
 
                g_directory_extract_processing = false;
+               msc_del_cur_extract_item();
+               msc_del_cancel_extract_item();
 
-               MS_DBG_ERR("[No-Error] DIRECTORY EXTRACT END [%d]", ret);
+               MS_DBG_ERR("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);
@@ -202,6 +304,8 @@ NEXT:
        }                       /*thread while*/
 
 _POWEROFF:
+       __msc_resume_extract();
+
        MS_SAFE_FREE(update_path);
        MS_SAFE_FREE(extract_data);
        MS_SAFE_FREE(storage_id);
@@ -222,7 +326,8 @@ gboolean msc_storage_extract_thread(void *data)
        //bool valid_status = TRUE;
        char *update_path = NULL;
        //GArray *dir_array = NULL;
-       bool power_off_status = FALSE;
+       unsigned int io_err_count = 0;
+       uid_t uid = MEDIA_DEFAULT_UID;
 
        while (1) {
                __msc_set_storage_extract_status(MS_STORAGE_SCAN_DONE);
@@ -233,7 +338,10 @@ gboolean msc_storage_extract_thread(void *data)
                }
 
                __msc_set_storage_extract_status(MS_STORAGE_SCAN_META_PROCESSING);
-               MS_DBG_ERR("STORAGE extract START ");
+
+               io_err_count = 0;
+               
+               MS_DBG_ERR("STORAGE extract START extract len is %d ",  g_async_queue_length(storage_extract_queue));
 
                scan_type = extract_data->msg_type;
                if (scan_type != MS_MSG_STORAGE_ALL
@@ -244,8 +352,11 @@ gboolean msc_storage_extract_thread(void *data)
                        goto NEXT;
                }
 
+               uid =extract_data->uid;
+
                /*connect to media db, if conneting is failed, db updating is stopped*/
-               err = ms_connect_db(&handle, extract_data->uid);
+               err = ms_connect_db(&handle, uid);
+
                if (err != MS_MEDIA_ERR_NONE) {
                        MS_DBG_ERR("ms_connect_db falied!");
                        continue;
@@ -254,21 +365,28 @@ gboolean msc_storage_extract_thread(void *data)
                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, extract_data->uid);
+               ms_set_storage_scan_status(handle, extract_data->storage_id, MEDIA_EXTRACT_PROCESSING, uid);
                __msc_extract_set_db_status(MS_DB_UPDATING);
 
-               ret = __msc_check_extract_stop_status(extract_data->msg_type, storage_type, update_path);
+               ret = __msc_check_extract_stop_status(extract_data->msg_type, storage_type, update_path, extract_data->pid);
                if (ret == MS_MEDIA_ERR_SCANNER_FORCE_STOP) {
                        MS_DBG_ERR("MS_MEDIA_ERR_SCANNER_FORCE_STOP");
                        goto NEXT;
                }
 
+               ret = msc_check_db_size(uid, extract_data->msg_type);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("NOT ENOUGH MEMORY");
+                       __msc_extract_set_db_status(MS_DB_STOPPED);
+                       goto NEXT;
+               }
 
-               storage_type = ms_get_storage_type_by_full(extract_data->msg, extract_data->uid);
+               storage_type = ms_get_storage_type_by_full(extract_data->msg, uid);
 
                /*extract meta*/
                int end_flag = extract_data->result ? LAST_EVENT : NORMAL_EVENT;
-               ret = ms_insert_item_pass2(handle, extract_data->storage_id, storage_type, update_path, scan_type, end_flag, extract_data->uid);
+               //ret = ms_insert_item_pass2(handle, extract_data->storage_id, storage_type, update_path, scan_type, end_flag);
+               ret = __msc_folder_bulk_extract(handle, extract_data->storage_id, storage_type, update_path, scan_type, end_flag, extract_data->pid, uid, &io_err_count);
 
                msc_del_extract_blocked_path();
 
@@ -277,13 +395,13 @@ gboolean msc_storage_extract_thread(void *data)
                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, extract_data->uid);
+                       ms_set_storage_scan_status(handle, extract_data->storage_id, MEDIA_EXTRACT_STOP, uid);
                        __msc_set_storage_extract_status(MS_STORAGE_SCAN_META_STOP);
                        /* set vconf key db extract status */
                        __msc_extract_set_db_status(MS_DB_STOPPED);
                } 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, extract_data->uid);
+                       ms_set_storage_scan_status(handle, extract_data->storage_id, MEDIA_EXTRACT_COMPLETE, uid);
                        __msc_set_storage_extract_status(MS_STORAGE_SCAN_COMPLETE);
                        /* set vconf key db extract status */
                        __msc_extract_set_db_status(MS_DB_UPDATED);
@@ -346,8 +464,7 @@ NEXT:
 
                MS_SAFE_FREE(update_path);
 
-               msc_get_power_status(&power_off_status);
-               if (power_off_status) {
+               if (power_off2) {
                        MS_DBG_ERR("power off");
                        goto _POWEROFF;
                }
@@ -431,6 +548,11 @@ int msc_remove_extract_request(const ms_comm_msg_s *recv_msg)
                /*create new queue to compare request*/
                msg = g_async_queue_pop(storage_extract_queue);
                if ((strcmp(msg->storage_id, storageid) == 0)) {
+                       if (msg->result) {
+                               MS_DBG_WARN("force stop storage scan");
+                               msg->msg_type = MS_MSG_EXTRACTOR_COMPLETE;
+                               msc_send_result(MS_MEDIA_ERR_SCANNER_FORCE_STOP, msg);
+                       }
                        MS_SAFE_FREE(msg);
                } else {
                        g_async_queue_push(temp_queue, GINT_TO_POINTER(msg));
@@ -453,30 +575,89 @@ END_REMOVE_REQUEST:
        return MS_MEDIA_ERR_NONE;
 }
 
-int msc_set_extract_cancel_path(const char *cancel_path)
+int _msc_set_extract_item(s_extract_item** item, const char* path, int pid)
 {
-       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;
+       s_extract_item* extract_item = *item;
+       if (extract_item != NULL) {
+               MS_SAFE_FREE(extract_item->path);
+               extract_item->pid = -1;
+       } else {
+               extract_item = (s_extract_item*)malloc(sizeof(s_extract_item));
+               if(extract_item == NULL) {
+                       MS_DBG_ERR("malloc item failed...");
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
+               memset(extract_item, 0, sizeof(s_extract_item));
        }
 
-       g_extract_cancel_path = strdup(cancel_path);
+       extract_item->path = strdup(path);
+       extract_item->pid = pid;
+       *item = extract_item;
 
+       MS_DBG_ERR("_msc_set_extract_item path[%s],pid[%d]", extract_item->path, extract_item->pid);
        return MS_MEDIA_ERR_NONE;
 }
 
-int msc_del_extract_cancel_path(void)
+int _msc_del_extract_item(s_extract_item** item)
 {
-       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;
+       if (*item != NULL) {
+               MS_SAFE_FREE((*item)->path);
+               MS_SAFE_FREE(*item);
        }
 
        return MS_MEDIA_ERR_NONE;
 }
 
+int msc_set_cancel_extract_item(const char* cancel_path, int pid)
+{
+       int ret = -1;
+       MS_DBG_ERR("msc_set_cancel_extract_item begin");
+       g_mutex_lock(&extract_item_mutex);
+       if(cancel_path == NULL) {
+               MS_DBG_ERR("cancel_path invalid...");
+               return ret;
+       }
+
+       if(cur_extract_item && cur_extract_item->path) {
+               if ((strcmp(cur_extract_item->path, cancel_path) == 0) && (pid == cur_extract_item->pid)) {
+                       ret = _msc_set_extract_item(&cancel_extract_item, cancel_path, pid);
+               }
+       }
+       g_mutex_unlock(&extract_item_mutex);
+
+       MS_DBG_ERR("msc_set_cancel_extract_item end");
+       return ret;
+}
+
+int msc_del_cancel_extract_item()
+{
+       int ret = -1;
+       g_mutex_lock(&extract_item_mutex);
+       ret = _msc_del_extract_item(&cancel_extract_item);
+       g_mutex_unlock(&extract_item_mutex);
+
+       return ret;
+}
+
+int msc_set_cur_extract_item(const char* cur_path, int pid)
+{
+       int ret = -1;
+       g_mutex_lock(&extract_item_mutex);
+       ret = _msc_set_extract_item(&cur_extract_item, cur_path, pid);
+       g_mutex_unlock(&extract_item_mutex);
+
+       return ret;
+}
+int msc_del_cur_extract_item()
+{
+       int ret = -1;
+       g_mutex_lock(&extract_item_mutex);
+       ret = _msc_del_extract_item(&cur_extract_item);
+       g_mutex_unlock(&extract_item_mutex);
+
+       return ret;
+}
+
 int msc_set_extract_blocked_path(const char *blocked_path)
 {
        MS_DBG_FENTER();
@@ -517,14 +698,12 @@ int msc_del_extract_blocked_path(void)
        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)
+static int __msc_check_extract_stop_status(int scan_type, ms_storage_type_t storage_type, const char *start_path, int pid)
 {
        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) {
+       if (power_off2) {
                MS_DBG_ERR("Power off");
                ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
        }
@@ -532,11 +711,13 @@ static int __msc_check_extract_stop_status(int scan_type, ms_storage_type_t stor
        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);
+               //MS_DBG_ERR("__msc_check_extract_stop_status...");
+               if ((cancel_extract_item != NULL) && (cancel_extract_item->path != NULL)) {
+                       MS_DBG_ERR("check cancel storage [%s][%s]", cancel_extract_item->path, start_path);
+                       if ((strncmp(cancel_extract_item->path, start_path, strlen(cancel_extract_item->path)) == 0)
+                               && (cancel_extract_item->pid == -1 || cancel_extract_item->pid == pid)){
+                               MS_DBG_ERR("Receive cancel request [%s][%s]. STOP scan!!", cancel_extract_item->path, start_path);
+                               unsigned int path_len = strlen(cancel_extract_item->path);
 
                                if (strlen(start_path) > path_len) {
                                        if (start_path[path_len] == '/') {
@@ -549,8 +730,6 @@ static int __msc_check_extract_stop_status(int scan_type, ms_storage_type_t stor
                                        ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
                                }
                        }
-
-                       MS_SAFE_FREE(g_extract_cancel_path);
                }
 
                g_mutex_unlock(&extract_req_mutex);
@@ -635,16 +814,16 @@ static int __msc_extract_set_db_status(ms_db_status_type_t status)
        return res;
 }
 
-int msc_push_extract_request(ms_extract_type_e scan_type, ms_comm_msg_s *recv_msg)
+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:
-                       g_async_queue_push(storage_extract_queue, GINT_TO_POINTER(recv_msg));
+                       *remain_request = g_async_queue_length(storage_extract_queue);
                        break;
                case MS_EXTRACT_DIRECTORY:
-                       g_async_queue_push(folder_extract_queue, GINT_TO_POINTER(recv_msg));
+                       *remain_request = g_async_queue_length(folder_extract_queue);
                        break;
                default:
                        MS_DBG_ERR("invalid parameter");
@@ -655,58 +834,47 @@ int msc_push_extract_request(ms_extract_type_e scan_type, ms_comm_msg_s *recv_ms
        return ret;
 }
 
+int msc_get_dir_extract_status(bool *extract_status)
+{
+       *extract_status = g_directory_extract_processing;
+
+       return MS_MEDIA_ERR_NONE;
+}
+
 int msc_stop_extract_thread(void)
 {
        ms_comm_msg_s *data = NULL;
 
        if (storage_extract_queue) {
-               /*notify to register thread*/
+               /*notify to storage extract thread*/
                MS_MALLOC(data, sizeof(ms_comm_msg_s));
                if (data != NULL) {
                        data->pid = POWEROFF;
-                       msc_push_extract_request(MS_EXTRACT_STORAGE, data);
-               } else {
-                       MS_DBG_ERR("memory allocation failed");
+                       g_async_queue_push(storage_extract_queue, GINT_TO_POINTER(data));
                }
        }
 
        if (folder_extract_queue) {
-               /*notify to register thread*/
+               /*notify to folder extract thread*/
                MS_MALLOC(data, sizeof(ms_comm_msg_s));
                if (data != NULL) {
                        data->pid = POWEROFF;
-                       msc_push_extract_request(MS_EXTRACT_DIRECTORY, data);
-               } else {
-                       MS_DBG_ERR("memory allocation failed");
+                       g_async_queue_push(folder_extract_queue, GINT_TO_POINTER(data));
                }
        }
 
        return MS_MEDIA_ERR_NONE;
 }
 
-int msc_get_remain_extract_request(ms_extract_type_e scan_type, int *remain_request)
+int msc_set_extract_cancel_path(const char *cancel_path)
 {
-       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;
+       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 ret;
-}
-
-int msc_get_dir_extract_status(bool *extract_status)
-{
-       *extract_status = g_directory_extract_processing;
+       g_extract_cancel_path = strdup(cancel_path);
 
        return MS_MEDIA_ERR_NONE;
 }
index 9d12fe4..e6a32b4 100755 (executable)
@@ -35,6 +35,9 @@
 #include <malloc.h>
 #include <dd-display.h>
 #include <vconf.h>
+#ifndef _USE_DEVICED_DBUS
+#include <usb-device.h>
+#endif
 
 #include "media-util.h"
 #include "media-server-ipc.h"
 #include "media-scanner-socket-v2.h"
 #include "media-scanner-scan-v2.h"
 #include "media-scanner-extract-v2.h"
+#include "media-scanner-db-manage-v2.h"
+
 #define MAX_SCAN_COUNT 300
 
 #define DIR_SCAN_NON_SCAN              0
 #define DIR_SCAN_RECURSIVE             1
 #define DIR_SCAN_NON_RECURSIVE 2
 
+extern bool power_off2;
 GAsyncQueue *storage_queue2;
 GAsyncQueue *scan_queue2;
 GAsyncQueue *reg_queue2;
@@ -63,11 +69,18 @@ GMutex dir_scan_mutex2;
 char *g_cancel_path2;
 char *g_blocked_path2;
 
-int insert_count_for_partial = 0;
-int set_count_for_partial = 0;
+GMutex scan_item_mutex;
+s_scan_item* cancel_scan_item = NULL;
+s_scan_item* cur_scan_item = NULL;
+
+extern int insert_count_for_partial;
+extern int set_count_for_partial;
 int g_directory_scan_processing2 = DIR_SCAN_NON_SCAN;
 char *g_storage_scan_path;
 char* g_dir_scan_path;
+unsigned int io_err_folder;
+unsigned int io_err_storage;
+
 
 typedef int (*DIR_SCAN_CB)();
 DIR_SCAN_CB g_dir_scan_cb;
@@ -79,7 +92,7 @@ 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(int scan_type, ms_storage_type_t storage_type, const char *start_path);
+static int __msc_check_stop_status(int scan_type, ms_storage_type_t storage_type, const char *start_path, int pid);
 static int __msc_db_update(void **handle, const char *storage_id, const ms_comm_msg_s * scan_data);
 static int __msc_check_file_path(const char *file_path, uid_t uid);
 static int __msc_make_file_list(char *file_path, GArray **path_array, uid_t uid);
@@ -91,9 +104,10 @@ static bool __msc_is_valid_path(const char *path, uid_t uid);
 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, int result, uid_t uid);
+static void __msc_bacth_commit_disable(void* handle, bool ins_status, bool valid_status, const char *path, int result, uid_t uid);
 static int __msc_dir_scan_for_folder(void **handle, const char *storage_id, const char*start_path, ms_storage_type_t storage_type, int scan_type, int pid, uid_t uid);
-static int __msc_dir_scan_for_storage(void **handle, const char *storage_id, const char*start_path, ms_storage_type_t storage_type, int scan_type, uid_t uid);
+static int __msc_dir_scan_for_storage(void **handle, const char *storage_id, const char*start_path, ms_storage_type_t storage_type, int scan_type, int pid, uid_t uid);
+
 static bool __msc_storage_mount_status(const char* start_path);
 static char* __msc_get_path(uid_t uid);
 
@@ -102,7 +116,10 @@ static int __msc_dir_scan_cb();
 static int __msc_check_scan_same_path(char *scan_path);
 static int __msc_set_storage_scan_cur_path(char *scan_path);
 static int __msc_set_dir_scan_cur_path(char *scan_path);
-static int __msc_dir_and_storage_scan_same_path(char *start_path);
+static int __msc_dir_and_storage_scan_same_path();
+static int __msc_check_memory_status(uid_t uid);
+static int __msc_check_remain_space(uid_t uid);
+static bool __msc_check_folder_path(const char *folder_path);
 
 static int __msc_set_dir_scan_cb(DIR_SCAN_CB cb)
 {
@@ -333,8 +350,8 @@ int msc_init_scan_thread()
        g_mutex_init(&blocked_mutex2);
        g_mutex_init(&scan_req_mutex2);
        g_mutex_init(&storage_scan_mutex2);
-
        g_mutex_init(&dir_scan_mutex2);
+       g_mutex_init(&scan_item_mutex);
 
        g_cond_init(&data_cond2);
 
@@ -347,8 +364,8 @@ int msc_deinit_scan_thread()
        g_mutex_clear(&blocked_mutex2);
        g_mutex_clear(&scan_req_mutex2);
        g_mutex_clear(&storage_scan_mutex2);
-
        g_mutex_clear(&dir_scan_mutex2);
+       g_mutex_clear(&scan_item_mutex);
 
        g_cond_clear(&data_cond2);
 
@@ -377,14 +394,12 @@ static int __msc_pause_scan()
        return MS_MEDIA_ERR_NONE;
 }
 
-static int __msc_check_stop_status(int scan_type, ms_storage_type_t storage_type, const char *start_path)
+static int __msc_check_stop_status(int scan_type, ms_storage_type_t storage_type, const char *start_path, int pid)
 {
        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) {
+       if (power_off2) {
                MS_DBG_ERR("Power off");
                ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
        }
@@ -392,12 +407,13 @@ static int __msc_check_stop_status(int scan_type, ms_storage_type_t storage_type
        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);
+               //MS_DBG_ERR("__msc_check_stop_status...");
+               if ((cancel_scan_item != NULL) && (cancel_scan_item->path != NULL)) {
+                       MS_DBG_ERR("check blocked storage [%s][%s]", cancel_scan_item->path, start_path);
+                       if ((strncmp(cancel_scan_item->path, start_path, strlen(cancel_scan_item->path)) == 0)
+                               && (cancel_scan_item->pid == -1 || cancel_scan_item->pid == pid)){
+                               MS_DBG_ERR("Receive cancel request [%s][%s]. STOP scan!!", cancel_scan_item->path, start_path);
+                               unsigned int path_len = strlen(cancel_scan_item->path);
 
                                if (strlen(start_path) > path_len) {
                                        if (start_path[path_len] == '/') {
@@ -410,8 +426,6 @@ static int __msc_check_stop_status(int scan_type, ms_storage_type_t storage_type
                                        ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
                                }
                        }
-
-                       MS_SAFE_FREE(g_cancel_path2);
                }
 
                g_mutex_unlock(&scan_req_mutex2);
@@ -447,6 +461,13 @@ static void __msc_trim_dir_path(char *dir_path)
                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_for_folder(void **handle, const char *storage_id, const char*start_path, ms_storage_type_t storage_type, int scan_type, int pid, uid_t uid)
@@ -458,11 +479,16 @@ static int __msc_dir_scan_for_folder(void **handle, const char *storage_id, cons
        char path[MS_FILE_PATH_LEN_MAX] = {0, };
        bool is_recursive = true;
        char *new_start_path = NULL;
+       int media_type = MS_MEDIA_TYPE_OTHER;
 
-       DIR *dp = NULL;
-       struct dirent entry;
-       struct dirent *result = 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";
 
        insert_count_for_partial = 0;
@@ -481,16 +507,12 @@ static int __msc_dir_scan_for_folder(void **handle, const char *storage_id, cons
        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);
        ms_insert_folder_end(handle, uid);
-       if (ms_insert_folder(handle, storage_id, new_start_path, uid) != MS_MEDIA_ERR_NONE) {
-               MS_DBG_ERR("insert folder failed");
-       }
 
        is_recursive = (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) ? false : true;
 
@@ -499,10 +521,18 @@ static int __msc_dir_scan_for_folder(void **handle, const char *storage_id, cons
                MS_DBG_ERR("set_folder_validity failed [%d] ", scan_type);
        }
 
+       if (__msc_check_folder_path(new_start_path) == TRUE) {
+               if (ms_insert_folder(handle, storage_id, new_start_path, uid) != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("insert folder failed");
+               }
+
+               MS_DBG_ERR("ms_insert_folder insert path11112222[%s]", new_start_path);
+       }
+
        /*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);
+               ret = __msc_check_stop_status(scan_type, storage_type, new_start_path, pid);
                if (ret != MS_MEDIA_ERR_NONE) {
                        if (is_recursive == true) {
                                ms_change_validity_item_batch(handle, storage_id, new_start_path, 1, 2, uid);
@@ -511,7 +541,7 @@ static int __msc_dir_scan_for_folder(void **handle, const char *storage_id, cons
                }
                /* get the current path from directory array */
                current_path = g_array_index(dir_array , char*, 0);
-               g_array_remove_index(dir_array, 0);
+               g_array_remove_index (dir_array, 0);
 
                __msc_set_dir_scan_cur_path(current_path);
 
@@ -528,14 +558,67 @@ static int __msc_dir_scan_for_folder(void **handle, const char *storage_id, cons
                        continue;
                }
 
+               ret = msc_check_db_size(uid, scan_type);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       if (ret == MS_MEDIA_ERR_DB_FULL_FAIL) {
+                               ret = MS_MEDIA_ERR_NONE;
+                       }
+
+                       if (is_recursive == true) {
+                               ms_change_validity_item_batch(handle, storage_id, new_start_path, 1, 2, uid);
+                       }
+                       goto STOP_SCAN;
+               }
+
                ms_insert_folder_start(handle);
                ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_PROCESSING, uid);
 
-               dp = opendir(current_path);
-               if (dp != NULL) {
-                       while (!readdir_r(dp, &entry, &result)) {
+               fd = open(current_path, O_RDONLY | O_DIRECTORY);
+               if (fd == -1) {
+                       MS_DBG_STRERROR("open fails");
+                       ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_STOP, uid);
+                       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, pid);
+                       if (ret != MS_MEDIA_ERR_NONE) {
+                               if (is_recursive == true) {
+                                       ms_change_validity_item_batch(handle, storage_id, new_start_path, 1, 2, uid);
+                               }
+                               goto STOP_SCAN;
+                       }
+
+                       ret = msc_check_db_size(uid, scan_type);
+                       if (ret != MS_MEDIA_ERR_NONE) {
+                               //ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_STOP, ui);
+                               if (ret == MS_MEDIA_ERR_DB_FULL_FAIL) {
+                                       ret = MS_MEDIA_ERR_NONE;
+                               }
+                               goto STOP_SCAN;
+                       }
+#if 0
+                       if (io_err_folder == MS_MAX_IO_ERR_COUNT) {
+                               MS_DBG_ERR("REACH I/O MAX COUNT[%d]", io_err_folder);
+                               if (is_recursive == true) {
+                                       ms_change_validity_item_batch(handle, storage_id, new_start_path, 1, 2, uid);
+                               }
+                               goto STOP_SCAN;
+                       }
+#endif
+                       if (nread == 0)
+                               break;
+
+                       for (bpos = 0; bpos < nread;) {
                                /*check poweroff status*/
-                               ret = __msc_check_stop_status(scan_type, storage_type, new_start_path);
+                               ret = __msc_check_stop_status(scan_type, storage_type, new_start_path, pid);
                                if (ret != MS_MEDIA_ERR_NONE) {
                                        if (is_recursive == true) {
                                                ms_change_validity_item_batch(handle, storage_id, new_start_path, 1, 2, uid);
@@ -543,21 +626,62 @@ static int __msc_dir_scan_for_folder(void **handle, const char *storage_id, cons
                                        goto STOP_SCAN;
                                }
 
-                               if (result == NULL)
-                                       break;
+                               ret = msc_check_db_size(uid, scan_type);
+                               if (ret != MS_MEDIA_ERR_NONE) {
+                                       if (ret == MS_MEDIA_ERR_DB_FULL_FAIL) {
+                                               ret = MS_MEDIA_ERR_NONE;
+                                       }
 
-                               if (entry.d_name[0] == '.' || strcmp(entry.d_name, trash) == 0)
+                                       if (is_recursive == true) {
+                                               ms_change_validity_item_batch(handle, storage_id, new_start_path, 1, 2, uid);
+                                       }
+                                       //ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_STOP, uid);
+                                       goto STOP_SCAN;
+                               }
+#if 0
+                               if (io_err_folder == MS_MAX_IO_ERR_COUNT) {
+                                       MS_DBG_ERR("REACH I/O MAX COUNT[%d]", io_err_folder);
+                                       if (is_recursive == true) {
+                                               ms_change_validity_item_batch(handle, storage_id, new_start_path, 1, 2, uid);
+                                       }
+                                       goto STOP_SCAN;
+                               }
+#endif
+                               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 (ms_strappend(path, sizeof(path), "%s/%s", current_path, entry.d_name) != MS_MEDIA_ERR_NONE) {
-                                       MS_DBG_ERR("ms_strappend failed");
+                               if (strcmp(d->d_name, trash) == 0) {
+                                       MS_DBG_ERR("trash directory");
+                                       bpos += d->d_reclen;
                                        continue;
                                }
 
-                               if (entry.d_type & DT_REG) {
+                               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) {
+                                       ms_get_media_type(handle, path, &media_type);
+                                       if (media_type == MS_MEDIA_TYPE_OTHER) {
+                                               bpos += d->d_reclen;
+                                               continue;
+                                       }
+
                                        /* insert into media DB */
-                                       if (ms_scan_validate_item(handle, storage_id, path, uid, &insert_count_for_partial, &set_count_for_partial) != MS_MEDIA_ERR_NONE) {
+                                       ret = ms_scan_validate_item(handle,storage_id, path, uid);
+                                       if (ret != MS_MEDIA_ERR_NONE) {
                                                MS_DBG_ERR("failed to update db : %d\n", scan_type);
+                                               if (ret == MS_MEDIA_ERR_FILE_IO) {
+                                                       MS_DBG_ERR("I/O ERROR COUNT[%d]", io_err_folder++);
+                                               }                                                       
+                                               bpos += d->d_reclen;
                                                continue;
                                        } else {
                                                ++scan_count;
@@ -582,7 +706,7 @@ static int __msc_dir_scan_for_folder(void **handle, const char *storage_id, cons
                                                        set_count_for_partial = 0;
                                                }
                                        }
-                               } else if (entry.d_type & DT_DIR) {
+                               } 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 */
@@ -597,31 +721,48 @@ static int __msc_dir_scan_for_folder(void **handle, const char *storage_id, cons
                                                /* don't add new directory to dir_array */
                                                if (ms_insert_folder(handle, storage_id, path, uid) != MS_MEDIA_ERR_NONE) {
                                                        MS_DBG_ERR("insert folder failed");
+                                                       bpos += d->d_reclen;
                                                        continue;
                                                }
                                        }
                                }
+
+                               bpos += d->d_reclen;
                        }
                        /*update modifiec type for the internal storage*/
-                       if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE && storage_type == MS_STORAGE_INTERNAL)
-                               ms_update_folder_time(handle, INTERNAL_STORAGE_ID, current_path, uid);
-               } else {
-                       MS_DBG_ERR("%s folder opendir fails", current_path);
-                       ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_STOP, uid);
+                       if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
+                               if (storage_type == MS_STORAGE_INTERNAL)
+                                       ms_update_folder_time(handle, INTERNAL_STORAGE_ID, current_path, uid);
+                               else
+                                       ms_update_folder_time(handle, storage_id, current_path, uid);
+                       }
                }
 
                ms_insert_folder_end(handle, uid);
                ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_DONE, uid);
 
-               if (dp) closedir(dp);
-               dp = NULL;
+               if (fd != -1) {
+                       close(fd);
+                       fd = -1;
+               }
 
                MS_SAFE_FREE(current_path);
                __msc_set_dir_scan_cur_path(NULL);
        }
 
+       goto END_SCAN;
+
 STOP_SCAN:
-       if (dp) closedir(dp);
+       MS_DBG_ERR("stop folder scan...");
+       ms_insert_folder_end(handle, uid);
+       ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_STOP, uid);
+
+END_SCAN:
+       if(fd != -1) {
+               close(fd);
+               fd = -1;
+       }
+
        MS_SAFE_FREE(new_start_path);
        __msc_set_dir_scan_cur_path(NULL);
 
@@ -632,22 +773,27 @@ STOP_SCAN:
        return ret;
 }
 
-static int __msc_dir_scan_for_storage(void **handle, const char *storage_id, const char*start_path, ms_storage_type_t storage_type, int scan_type, uid_t uid)
+static int __msc_dir_scan_for_storage(void **handle, const char *storage_id, const char*start_path, ms_storage_type_t storage_type, int scan_type, int pid, uid_t uid)
 {
        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*, uid_t, int *, int *) = NULL;
+       int (*scan_function)(void **, const char*, const char*, uid_t) = NULL;
        char *new_start_path = NULL;
+       int media_type = MS_MEDIA_TYPE_OTHER;
 
-       DIR *dp = NULL;
-       struct dirent entry;
-       struct dirent *result = NULL;
+       int fd = -1;
+       int nread = 0;
+       char buf[BUF_SIZE] = {0,};
+       struct linux_dirent *d;
+       int bpos = 0;
        int scan_count = 0;
-       const char *trash = "$RECYCLE.BIN";
 
+       char d_type;
+       const char *trash = "$RECYCLE.BIN"; 
+       
        bool is_missing = false;
        ms_dir_scan_status_e scan_status = MS_DIR_SCAN_NONE;
 
@@ -678,7 +824,7 @@ static int __msc_dir_scan_for_storage(void **handle, const char *storage_id, con
        /*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);
+               ret = __msc_check_stop_status(scan_type, storage_type, new_start_path, pid);
                if (ret != MS_MEDIA_ERR_NONE) {
                        goto STOP_SCAN;
                }
@@ -701,7 +847,12 @@ static int __msc_dir_scan_for_storage(void **handle, const char *storage_id, con
                        continue;
                }
 
-               ms_get_folder_scan_status(handle, storage_id, current_path, (int*)&scan_status);
+               ret = msc_check_db_size(uid, scan_type);
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       goto STOP_SCAN;
+               }
+
+               ms_get_folder_scan_status(handle, storage_id, current_path, (int *)&scan_status);
 
                if (scan_status == MS_DIR_SCAN_PROCESSING && g_directory_scan_processing2 != DIR_SCAN_NON_SCAN) {
 
@@ -729,30 +880,93 @@ static int __msc_dir_scan_for_storage(void **handle, const char *storage_id, con
                ms_insert_folder_start(handle);
                ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_PROCESSING, uid);
 
-               dp = opendir(current_path);
-               if (dp != NULL) {
-                       while (!readdir_r(dp, &entry, &result)) {
+               fd = open(current_path, O_RDONLY | O_DIRECTORY);
+               if (fd == -1) {
+                       MS_DBG_STRERROR("open fails");
+                       ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_STOP, uid);
+                       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, pid);
+                       if (ret != MS_MEDIA_ERR_NONE) {
+                               goto STOP_SCAN;
+                       }
+
+                       ret = msc_check_db_size(uid, scan_type);
+                       if (ret != MS_MEDIA_ERR_NONE) {
+                               //ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_STOP, uid);
+                               goto STOP_SCAN;
+                       }
+#if 0
+                       if (io_err_storage == MS_MAX_IO_ERR_COUNT) {
+                               MS_DBG_ERR("REACH I/O MAX COUNT[%d]", io_err_storage);
+                               goto STOP_SCAN;
+                       }
+#endif
+                       if (nread == 0)
+                               break;
+
+                       for (bpos = 0; bpos < nread;) {
                                /*check poweroff status*/
-                               ret = __msc_check_stop_status(scan_type, storage_type, new_start_path);
+                               ret = __msc_check_stop_status(scan_type, storage_type, new_start_path, pid);
                                if (ret != MS_MEDIA_ERR_NONE) {
                                        goto STOP_SCAN;
                                }
 
-                               if (result == NULL)
-                                       break;
+                               ret = msc_check_db_size(uid, scan_type);
+                               if (ret != MS_MEDIA_ERR_NONE) {
+                                       //ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_STOP, uid);
+                                       goto STOP_SCAN;
+                               }
+#if 0
+                               if (io_err_storage == MS_MAX_IO_ERR_COUNT) {
+                                       MS_DBG_ERR("REACH I/O MAX COUNT[%d]", io_err_storage);
+                                       goto STOP_SCAN;
+                               }
+#endif
+                               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 (entry.d_name[0] == '.' || strcmp(entry.d_name, trash) == 0)
+                               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, entry.d_name) != MS_MEDIA_ERR_NONE) {
+                               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 (entry.d_type & DT_REG) {
+                               if (d_type == DT_REG) {
+                                       /* check media type */
+                                       ms_get_media_type(handle, path, &media_type);
+                                       if (media_type == MS_MEDIA_TYPE_OTHER) {
+                                               bpos += d->d_reclen;
+                                               continue;
+                                       }
                                        /* insert into media DB */
-                                       if (scan_function(handle, storage_id, path, uid, &insert_count_for_partial, &set_count_for_partial) != MS_MEDIA_ERR_NONE) {
+                                       ret = scan_function(handle,storage_id, path, uid);
+                                       if (ret != MS_MEDIA_ERR_NONE) {
                                                MS_DBG_ERR("failed to update db : %d\n", scan_type);
+                                               if (ret == MS_MEDIA_ERR_FILE_IO) {
+                                                       MS_DBG_ERR("I/O ERROR COUNT[%d]", io_err_storage++);
+                                               }       
+                                               bpos += d->d_reclen;
                                                continue;
                                        } else {
                                                ++scan_count;
@@ -763,7 +977,7 @@ static int __msc_dir_scan_for_storage(void **handle, const char *storage_id, con
                                                        msc_insert_exactor_request(scan_type, FALSE, storage_id, current_path, 0, uid);
                                                }
                                        }
-                               } else if (entry.d_type & DT_DIR) {
+                               } else if (d_type == DT_DIR) {
                                        /* this request is recursive scanning */
                                        /* add new directory to dir_array */
                                        new_path = strdup(path);
@@ -773,17 +987,18 @@ static int __msc_dir_scan_for_storage(void **handle, const char *storage_id, con
                                                MS_DBG_ERR("insert folder failed");
                                        }
                                }
+
+                               bpos += d->d_reclen;
                        }
-               } else {
-                       MS_DBG_ERR("%s folder opendir fails", current_path);
-                       ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_STOP, uid);
                }
 
                ms_insert_folder_end(handle, uid);
                ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_DONE, uid);
 
-               if (dp) closedir(dp);
-               dp = NULL;
+               if (fd != -1) {
+                       close(fd);
+                       fd = -1;
+               }
 
 NEXT_SCAN:
                MS_SAFE_FREE(current_path);
@@ -838,9 +1053,19 @@ NEXT_SCAN:
                MS_DBG_ERR("start path is unmounted");
        }
 
+       goto EXIT;
+
 STOP_SCAN:
-       if (dp) closedir(dp);
-       dp = NULL;
+       ms_insert_folder_end(handle, uid);
+       if (current_path != NULL)
+               ms_set_folder_scan_status(handle, storage_id, current_path, MS_DIR_SCAN_STOP, uid);
+
+EXIT:
+       
+       if (fd != -1) {
+               close(fd);
+               fd = -1;
+       }
 
        MS_SAFE_FREE(new_start_path);
        __msc_set_storage_scan_cur_path(NULL);
@@ -920,7 +1145,7 @@ int __msc_set_storage_scan_cur_path(char *scan_path)
        return ret;
 }
 
-static int __msc_dir_and_storage_scan_same_path(char *start_path)
+int __msc_dir_and_storage_scan_same_path(char *start_path)
 {
        int ret = MS_MEDIA_ERR_NONE;
        unsigned int storage_len, folder_len;
@@ -995,7 +1220,7 @@ static int __msc_db_update(void **handle, const char *storage_id, const ms_comm_
                if (scan_type == MS_MSG_DIRECTORY_SCANNING || scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
                        err = __msc_dir_scan_for_folder(handle, storage_id, start_path, storage_type, scan_type, scan_data->pid, scan_data->uid);
                } else if (scan_type == MS_MSG_STORAGE_ALL || scan_type == MS_MSG_STORAGE_PARTIAL) {
-                       err = __msc_dir_scan_for_storage(handle, storage_id, start_path, storage_type, scan_type, scan_data->uid);
+                       err = __msc_dir_scan_for_storage(handle, storage_id, start_path, storage_type, scan_type, scan_data->pid, scan_data->uid);
                }
 
                if (err != MS_MEDIA_ERR_NONE) {
@@ -1003,19 +1228,6 @@ static int __msc_db_update(void **handle, const char *storage_id, const ms_comm_
                }
        } else if (scan_type == MS_MSG_STORAGE_INVALID) {
                MS_DBG_ERR("INVALID");
-#if 1
-               /*In this case, update just validation record*/
-               /*update just valid type*/
-               err = ms_validaty_change_all_items(handle, storage_id, storage_type, false, scan_data->uid);
-               if (err != MS_MEDIA_ERR_NONE) {
-                       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, scan_data->uid) != MS_MEDIA_ERR_NONE) {
-                       MS_DBG_ERR("set_folder_validity failed");
-               }
-#endif
                msc_remove_extract_request(scan_data);
        }
 
@@ -1063,10 +1275,11 @@ gboolean msc_directory_scan_thread(void *data)
        int delete_folder_count = 0;
        char *folder_uuid = NULL;
        bool is_recursive = true;
-       bool power_off_status = FALSE;
+       uid_t uid = MEDIA_DEFAULT_UID;
 
        while (1) {
                __msc_pop_new_request(&scan_data);
+               ret = MS_MEDIA_ERR_NONE;
 
                if (scan_data->pid == POWEROFF) {
                        MS_DBG_ERR("power off");
@@ -1075,15 +1288,25 @@ gboolean msc_directory_scan_thread(void *data)
 
                MS_DBG_ERR("DIRECTORY SCAN START [%s, %d]", scan_data->msg, scan_data->msg_type);
 
+               msc_set_cur_scan_item(scan_data->msg, scan_data->pid);
                g_directory_scan_processing2 = DIR_SCAN_NON_RECURSIVE;
+               uid = scan_data->uid;
 
                /*connect to media db, if conneting is failed, db updating is stopped*/
-               err = ms_connect_db(&handle, scan_data->uid);
+               err = ms_connect_db(&handle, uid);
                if (err != MS_MEDIA_ERR_NONE)
                        continue;
 
+               /*check remain space*/
+               err = __msc_check_remain_space(uid);
+               if (err == MS_MEDIA_ERR_NOT_ENOUGH_SPACE
+                       || err == MS_MEDIA_ERR_DB_FULL_FAIL){
+                       ret = MS_MEDIA_ERR_NONE;
+                       goto SCAN_DONE;
+               }
+
                scan_type = scan_data->msg_type;
-               storage_type = ms_get_storage_type_by_full(scan_data->msg, scan_data->uid);
+               storage_type = ms_get_storage_type_by_full(scan_data->msg, uid);
                ms_reset_insert_count();
 
                storage_id = strdup(scan_data->storage_id);
@@ -1150,7 +1373,7 @@ gboolean msc_directory_scan_thread(void *data)
                                while (1) {
                                        sleep(1);
 
-                                       ret = __msc_check_stop_status(scan_type, storage_type, scan_data->msg);
+                                       ret = __msc_check_stop_status(scan_type, storage_type, scan_data->msg, scan_data->pid);
                                        if (ret != MS_MEDIA_ERR_NONE) {
                                                goto NEXT;
                                        }
@@ -1173,9 +1396,9 @@ gboolean msc_directory_scan_thread(void *data)
                                ms_get_folder_scan_status(handle, storage_id, scan_data->msg, (int*)&scan_status);
 
                                if (scan_status == MS_DIR_SCAN_DONE) {
-                                       ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_VALIND_V2, MS_RECURSIVE, scan_data->uid);
+                                       ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_VALIND_V2, MS_RECURSIVE, uid);
                                } else {
-                                       ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_INVALID, MS_RECURSIVE, scan_data->uid);
+                                       ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_INVALID, MS_RECURSIVE, uid);
                                }
                        }
                } else {
@@ -1192,7 +1415,7 @@ gboolean msc_directory_scan_thread(void *data)
                                if (scan_status == MS_DIR_SCAN_DONE) {
                                        /* do nothing */
                                        MS_DBG_ERR("[%s] scan done", scan_data->msg);
-                                       err = ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_VALIND, MS_NON_RECURSIVE, scan_data->uid);
+                                       err = ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_VALIND, MS_NON_RECURSIVE, uid);
                                        goto SCAN_DONE;
                                } else if (scan_status == MS_DIR_SCAN_PROCESSING) {
                                        MS_DBG_ERR("[%s] scanning, waiting...", scan_data->msg);
@@ -1200,7 +1423,7 @@ gboolean msc_directory_scan_thread(void *data)
                                        while (1) {
                                                sleep(1);
 
-                                               ret = __msc_check_stop_status(scan_type, storage_type, scan_data->msg);
+                                               ret = __msc_check_stop_status(scan_type, storage_type, scan_data->msg, scan_data->pid);
                                                if (ret != MS_MEDIA_ERR_NONE) {
                                                        goto NEXT;
                                                }
@@ -1210,20 +1433,26 @@ gboolean msc_directory_scan_thread(void *data)
                                                }
 
                                                ms_get_folder_scan_status(handle, storage_id, scan_data->msg, (int*)&scan_status);
-                                               if (scan_status == MS_DIR_SCAN_DONE) {
+                                               if ((scan_status == MS_DIR_SCAN_DONE ) || (scan_status == MS_DIR_SCAN_STOP)) {
+                                                       MS_DBG_ERR("[%s] scan status [%d]!!!", scan_data->msg, scan_status);
                                                        break;
                                                }
                                        }
 
                                        index = 0;
-                                       MS_DBG_ERR("[%s] scan done, wait finished", scan_data->msg);
-                                       goto SCAN_DONE;
+                                       if (scan_status == MS_DIR_SCAN_DONE) {
+                                               MS_DBG_ERR("[%s] scan done, wait finished", scan_data->msg);
+                                               goto SCAN_DONE;
+                                       } else {
+                                               MS_DBG_ERR("set folder item invalid");
+                                               err = ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_INVALID, MS_NON_RECURSIVE, uid);
+                                       }
                                } else {
-                                       err = ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_INVALID, MS_NON_RECURSIVE, scan_data->uid);
+                                       err = ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_INVALID, MS_NON_RECURSIVE, uid);
                                }
                        } else {
                                MS_DBG_ERR("check [%s] has been modified !!", scan_data->msg);
-                               err = ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_INVALID, MS_NON_RECURSIVE, scan_data->uid);
+                               err = ms_set_folder_item_validity(handle, storage_id, scan_data->msg, MS_INVALID, MS_NON_RECURSIVE, uid);
 #if 0
                                ms_get_folder_scan_status(handle, storage_id, scan_data->msg, &scan_status);
                                MS_DBG_ERR("folder scan status = [%d]", scan_status);
@@ -1245,35 +1474,36 @@ gboolean msc_directory_scan_thread(void *data)
 
                /*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, ret, scan_data->uid);
+               __msc_bacth_commit_disable(handle, TRUE, TRUE, scan_data->msg, ret, uid);
 
                MS_DBG_ERR("folder scan done, sent cb event path = [%s]", scan_data->msg);
                __msc_call_dir_scan_cb();
 
                ms_check_subfolder_count(handle, storage_id, scan_data->msg, &after_count);
-               MS_DBG_ERR("BEFORE COUNT[%d]", before_count);
+               MS_DBG_ERR("AFTER COUNT[%d]", before_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, scan_data->uid)  != MS_MEDIA_ERR_NONE) {
+               if (ms_delete_invalid_items_in_folder(handle, storage_id, scan_data->msg, is_recursive, uid)  != 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");
-               //}
-
-               MS_DBG_SLOG("delete folder count %d", delete_folder_count);
-
                if (ret != MS_MEDIA_ERR_SCANNER_FORCE_STOP) {
                        MS_DBG_INFO("working normally");
 
+                       /*remove invalid folder in folder table.*/
+                       if (is_recursive == true) {
+                               if(ms_delete_invalid_folder_by_path(handle, storage_id, scan_data->msg, uid, &delete_folder_count) != MS_MEDIA_ERR_NONE) {
+                                       MS_DBG_ERR("deleting invalid folder failed");
+                               }
+                       }
+
+               MS_DBG_SLOG("delete folder count %d", delete_folder_count);
+
                        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 {
@@ -1296,10 +1526,9 @@ gboolean msc_directory_scan_thread(void *data)
 
 SCAN_DONE:
                MS_DBG_ERR("storage_id = [%s], dir Path = [%s]", storage_id, scan_data->msg);
-               msc_insert_exactor_request(scan_data->msg_type, TRUE, scan_data->storage_id, scan_data->msg, scan_data->pid, scan_data->uid);
+               msc_insert_exactor_request(scan_data->msg_type, TRUE, scan_data->storage_id, scan_data->msg, scan_data->pid, uid);
 
-               msc_get_power_status(&power_off_status);
-               if (power_off_status) {
+               if (power_off2) {
                        MS_DBG_ERR("power off");
                        goto _POWEROFF;
                }
@@ -1316,9 +1545,13 @@ NEXT:
                MS_SAFE_FREE(folder_uuid);
 
                g_directory_scan_processing2 = DIR_SCAN_NON_SCAN;
+               msc_del_cur_scan_item();
+               msc_del_cancel_scan_item();
 
                MS_DBG_ERR("DIRECTORY SCAN END [%d]", ret);
 
+               io_err_folder = 0;
+
                /*disconnect form media db*/
                if (handle) ms_disconnect_db(&handle);
        }                       /*thread while*/
@@ -1505,6 +1738,30 @@ int __msc_dir_scan_cb()
        return __msc_resume_scan();
 }
 
+int __msc_check_pvr_svc()
+{
+#ifdef _USE_RECORDED_CONTENT
+       if (ms_is_support_pvr()) {
+               while (1) {
+                       FILE* file = fopen( "/run/pvr_ready", "rb" );
+
+                       if (file != NULL) {
+                               fclose( file );
+                               MS_DBG_ERR("PVR service is ready");
+                               break;
+                       } else {
+                               MS_DBG_ERR("Waiting PVR service");
+                               sleep(1);
+                       }
+               }
+       }
+#else
+       MS_DBG("NOT SUPPORT RECORED CONTENT");
+#endif
+
+       return MS_MEDIA_ERR_NONE;
+}
+
 gboolean msc_storage_scan_thread(void *data)
 {
        ms_comm_msg_s *scan_data = NULL;
@@ -1516,7 +1773,7 @@ gboolean msc_storage_scan_thread(void *data)
        bool valid_status = TRUE;
        char *update_path = NULL;
        GArray *dir_array = NULL;
-       bool power_off_status = FALSE;
+       uid_t uid = MEDIA_DEFAULT_UID;
 
        while (1) {
                scan_data = g_async_queue_pop(storage_queue2);
@@ -1525,7 +1782,7 @@ gboolean msc_storage_scan_thread(void *data)
                        goto _POWEROFF;
                }
 
-               MS_DBG_ERR("[No-Error] STORAGE SCAN START [%s][%s]", scan_data->msg, scan_data->storage_id);
+               MS_DBG_ERR("STORAGE SCAN START [%s]", scan_data->msg);
 
                scan_type = scan_data->msg_type;
                if (scan_type != MS_MSG_STORAGE_ALL
@@ -1536,15 +1793,28 @@ gboolean msc_storage_scan_thread(void *data)
                        goto NEXT;
                }
 
+               __msc_check_pvr_svc();
+
+               uid = scan_data->uid;
+
                /*connect to media db, if conneting is failed, db updating is stopped*/
-               err = ms_connect_db(&handle, scan_data->uid);
+               err = ms_connect_db(&handle, uid);
                if (err != MS_MEDIA_ERR_NONE)
                        continue;
 
-               storage_type = ms_get_storage_type_by_full(scan_data->msg, scan_data->uid);
+               storage_type = ms_get_storage_type_by_full(scan_data->msg, uid);
                update_path = strndup(scan_data->msg, scan_data->msg_size);
 
-               ms_set_storage_scan_status(handle, scan_data->storage_id, MEDIA_SCAN_PROCESSING, scan_data->uid);
+               err = __msc_check_memory_status(uid);
+               if (err != MS_MEDIA_ERR_NONE) {
+                       ret = err;
+                       ms_set_storage_scan_status(handle, scan_data->storage_id, MEDIA_SCAN_STOP, uid);
+                       MS_DBG_ERR("storage_id = [%s]", scan_data->storage_id);
+                       msc_insert_exactor_request(scan_type, TRUE, scan_data->storage_id, scan_data->msg, scan_data->pid, uid);
+                       goto NEXT;
+               }               
+
+               ms_set_storage_scan_status(handle, scan_data->storage_id, MEDIA_SCAN_PROCESSING, uid);
 
                /*start db updating */
                __msc_set_db_status(MS_DB_UPDATING, storage_type);
@@ -1555,7 +1825,7 @@ gboolean msc_storage_scan_thread(void *data)
                        __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, scan_data->uid);
+                       ms_validaty_change_all_items(handle, scan_data->storage_id, storage_type, true, uid);
 
                        /* find and compare modified time */
                        ret = __msc_compare_with_db(handle, scan_data->storage_id, update_path, scan_data->msg_type, &dir_array);
@@ -1566,56 +1836,54 @@ gboolean msc_storage_scan_thread(void *data)
 
                        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, scan_data->pid, scan_data->uid);
+                               ret = _msc_db_update_partial(handle, scan_data->storage_id, storage_type, dir_array, 0, uid);
                        } 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, scan_data->uid)) {
-                                       MS_DBG_ERR("msc_delete_all_record fails");
+                               if (!ms_delete_invalid_items(handle, scan_data->storage_id, storage_type, uid)) {
+                                       MS_DBG_ERR("ms_delete_invalid_items fails");
                                }
-                       } else if (scan_type == MS_MSG_STORAGE_PARTIAL) {
-                               ms_validaty_change_all_items(handle, scan_data->storage_id, storage_type, false, scan_data->uid);
-                       }
+                       }/* else if (scan_type == MS_MSG_STORAGE_PARTIAL) {
+                                       ms_validaty_change_all_items(handle, scan_data->storage_id, storage_type, false, uid);
+                       }*/
 
                        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, ret, scan_data->uid);
+                       __msc_bacth_commit_disable(handle, TRUE, valid_status, scan_data->msg, ret, uid);
                }
 
                if (scan_type == MS_MSG_STORAGE_PARTIAL && ret == MS_MEDIA_ERR_NONE) {
-                       ms_delete_invalid_items(handle, scan_data->storage_id, storage_type, scan_data->uid);
+                       ms_delete_invalid_items(handle, scan_data->storage_id, storage_type, uid);
                }
 
                /* 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, scan_data->uid);
+                       ms_set_storage_scan_status(handle, scan_data->storage_id, MEDIA_SCAN_STOP, uid);
                        /*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, scan_data->uid);
+                       ms_set_storage_scan_status(handle, scan_data->storage_id, MEDIA_SCAN_COMPLETE, uid);
                        /*set vconf key mmc loading for indicator */
                        __msc_set_db_status(MS_DB_UPDATED, storage_type);
                }
 
                MS_DBG_ERR("storage_id = [%s]", scan_data->storage_id);
-               msc_insert_exactor_request(scan_type, TRUE, scan_data->storage_id, scan_data->msg, scan_data->pid, scan_data->uid);
+               msc_insert_exactor_request(scan_type, TRUE, scan_data->storage_id, scan_data->msg, scan_data->pid, uid);
 
 NEXT:
+               msc_del_blocked_path();
 
                MS_SAFE_FREE(update_path);
 
-               msc_get_power_status(&power_off_status);
-               if (power_off_status) {
+               if (power_off2) {
                        MS_DBG_ERR("power off");
                        goto _POWEROFF;
                }
@@ -1632,6 +1900,8 @@ NEXT:
                MS_SAFE_FREE(scan_data);
 
                MS_DBG_ERR("STORAGE SCAN END[%d]", ret);
+
+               io_err_storage = 0;
        }                       /*thread while*/
 
 _POWEROFF:
@@ -1666,15 +1936,21 @@ static bool __msc_is_valid_path(const char *path, uid_t uid)
        if (usr_path == NULL)
                return false;
 
-       if (strncmp(path, usr_path, strlen(usr_path)) == 0) {
-               ret = true;
-       } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
-               ret = true;
-       } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
-               ret = true;
-       } else {
-               ret = false;
+#ifdef _USE_SENIOR_MODE
+       if(ms_is_support_senior_mode()) {
+               if (strncmp(path, MEDIA_ROOT_PATH_SENIOR_MODE, strlen(MEDIA_ROOT_PATH_SENIOR_MODE)) == 0)
+                       return true;
        }
+#endif
+
+       if (strncmp(path, usr_path, strlen(usr_path)) == 0)
+               ret =  true;
+       else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
+               ret =  true;
+       else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
+               ret =  true;
+       else
+               ret =  false;
 
        MS_SAFE_FREE(usr_path);
 
@@ -1760,6 +2036,13 @@ static int __msc_check_ignore_dir(const char *full_path, uid_t uid)
                        break;
                }
 
+#ifdef _USE_SENIOR_MODE
+               if(ms_is_support_senior_mode()) {
+                       if(strcmp(dir_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)
+                               break;
+               }
+#endif
+
                /*If root path, Stop Scanning*/
                user_path = __msc_get_path(uid);
                if (user_path == NULL) {
@@ -1899,7 +2182,7 @@ static int __msc_batch_insert(ms_msg_type_e current_msg, int pid, GArray *path_a
        }
 
        /*call for bundle commit*/
-       __msc_bacth_commit_disable(handle, TRUE, FALSE, MS_MEDIA_ERR_NONE, uid);
+       __msc_bacth_commit_disable(handle, TRUE, FALSE, NULL, MS_MEDIA_ERR_NONE, uid);
 
        /*disconnect form media db*/
        if (handle) ms_disconnect_db(&handle);
@@ -1950,6 +2233,7 @@ gboolean msc_register_thread(void *data)
        int ret;
        int pid = 0;
        ms_msg_type_e current_msg = MS_MSG_MAX;
+       uid_t uid = MEDIA_DEFAULT_UID;
 
        /*create array for processing overlay data*/
        register_array = g_array_new(FALSE, FALSE, sizeof(ms_comm_msg_s *));
@@ -1973,6 +2257,7 @@ gboolean msc_register_thread(void *data)
                /* check current request */
                current_msg = register_data->msg_type;
                pid = register_data->pid;
+               uid = register_data->uid;
 
                if ((current_msg != MS_MSG_BULK_INSERT) &&
                        (current_msg != MS_MSG_BURSTSHOT_INSERT)) {
@@ -1986,13 +2271,13 @@ gboolean msc_register_thread(void *data)
                        goto FREE_RESOURCE;
                }
 
-               ret = __msc_make_file_list(file_path, &path_array, register_data->uid);
+               ret = __msc_make_file_list(file_path, &path_array, uid);
                if (ret != MS_MEDIA_ERR_NONE) {
                        MS_DBG_ERR("__msc_make_file_list failed [%d]", ret);
                        goto FREE_RESOURCE;
                }
 
-               ret = __msc_batch_insert(current_msg, pid, path_array, register_data->uid);
+               ret = __msc_batch_insert(current_msg, pid, path_array, uid);
 
 FREE_RESOURCE:
                /*Active flush */
@@ -2037,7 +2322,7 @@ static void __msc_bacth_commit_enable(void* handle, bool ins_status, bool valid_
        return;
 }
 
-static void __msc_bacth_commit_disable(void* handle, bool ins_status, bool valid_status, int result, uid_t uid)
+static void __msc_bacth_commit_disable(void* handle, bool ins_status, bool valid_status, const char *path, int result, uid_t uid)
 {
        /*call for bundle commit*/
        if (valid_status) ms_validate_end(handle, uid);
@@ -2046,44 +2331,108 @@ static void __msc_bacth_commit_disable(void* handle, bool ins_status, bool valid
        return;
 }
 
-int msc_set_cancel_path(const char *cancel_path)
+int _msc_set_scan_item(s_scan_item** item, const char* path, int pid)
 {
-       if (g_cancel_path2 != NULL) {
-               MS_DBG_WARN("g_cancel_path2 is not NULL");
-               free(g_cancel_path2);
-               g_cancel_path2 = NULL;
+       s_scan_item* scan_item = *item;
+       if (scan_item != NULL) {
+               MS_SAFE_FREE(scan_item->path);
+               scan_item->pid = -1;
+       } else {
+               scan_item = (s_scan_item*)malloc(sizeof(s_scan_item));
+               if(scan_item == NULL) {
+                       MS_DBG_ERR("malloc item failed...");
+                       return MS_MEDIA_ERR_INTERNAL;
+               }
+               memset(scan_item, 0, sizeof(s_scan_item));
        }
 
-       g_cancel_path2 = strdup(cancel_path);
+       scan_item->path = strdup(path);
+       scan_item->pid = pid;
+       *item = scan_item;
 
+       MS_DBG_ERR("path[%s],pid[%d]", scan_item->path, scan_item->pid);
        return MS_MEDIA_ERR_NONE;
 }
 
-int msc_del_cancel_path()
+int _msc_del_scan_item(s_scan_item** item)
 {
-       if (g_cancel_path2 != NULL) {
-               MS_DBG_WARN("g_tv_cancel_path is not NULL");
-               free(g_cancel_path2);
-               g_cancel_path2 = NULL;
+       if (*item != NULL) {
+               MS_SAFE_FREE((*item)->path);
+               MS_SAFE_FREE(*item);
        }
 
        return MS_MEDIA_ERR_NONE;
 }
 
+int msc_set_cancel_scan_item(const char* cancel_path, int pid)
+{
+       int ret = -1;
+       MS_DBG_ERR("msc_set_cancel_scan_item begin");
+       g_mutex_lock(&scan_item_mutex);
+       if(cancel_path == NULL) {
+               MS_DBG_ERR("cancel_path invalid...");
+               return ret;
+       }
+
+       if(cur_scan_item != NULL && cur_scan_item->path != NULL) {
+               MS_DBG_ERR("cur_scan_item->path[%s], cur_scan_item->pid[%d]", cur_scan_item->path, cur_scan_item->pid);
+               MS_DBG_ERR("cancel_path[%s], pid[%d]", cancel_path, pid);
+               if ((strcmp(cur_scan_item->path, cancel_path) == 0) && (pid == cur_scan_item->pid)) {
+                       ret = _msc_set_scan_item(&cancel_scan_item, cancel_path, pid);
+               }
+       } else {
+               MS_DBG_ERR("cur_scan_item == NULL || cur_scan_item->path == NULL");
+       }
+       g_mutex_unlock(&scan_item_mutex);
+
+       MS_DBG_ERR("msc_set_cancel_scan_item end");
+       return ret;
+}
+
+int msc_del_cancel_scan_item(void)
+{
+       int ret = -1;
+       g_mutex_lock(&scan_item_mutex);
+       ret = _msc_del_scan_item(&cancel_scan_item);
+       g_mutex_unlock(&scan_item_mutex);
+
+       return ret;
+}
+
+int msc_set_cur_scan_item(const char* cur_scan_path, int pid)
+{
+       int ret = -1;
+       g_mutex_lock(&scan_item_mutex);
+       ret = _msc_set_scan_item(&cur_scan_item, cur_scan_path, pid);
+       g_mutex_unlock(&scan_item_mutex);
+
+       return ret;
+}
+int msc_del_cur_scan_item(void)
+{
+       int ret = -1;
+       g_mutex_lock(&scan_item_mutex);
+       ret = _msc_del_scan_item(&cur_scan_item);
+       g_mutex_unlock(&scan_item_mutex);
+
+       return ret;
+}
+
 static bool __msc_storage_mount_status(const char* start_path)
 {
        bool ret = false;
+#ifndef _USE_DEVICED_DBUS
+       int count = 0;
+       int err = 0;
+       usb_device_list_h list;
+       usb_device_h device;
+       char *mount_path = NULL;
+
        char *storage_path = NULL;
        char *remain_path = NULL;
        int remain_len = 0;
-       GArray *dev_list = NULL;
-
-       if (!MS_STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL)) {
-               MS_DBG_ERR("Fail to get external path");
-               return FALSE;
-       }
 
-       remain_path = strstr(start_path + strlen(MEDIA_ROOT_PATH_EXTERNAL) + 1, "/");
+       remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
        if (remain_path != NULL)
                remain_len = strlen(remain_path);
 
@@ -2091,32 +2440,50 @@ static bool __msc_storage_mount_status(const char* start_path)
 
        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;
+       err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
+       if (err == 0) {
+               count = usb_device_list_get_count(list);
+               if (count > 0) {
+                       err = usb_device_list_get_first(list, &device);
+                       if(err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
+                               mount_path = usb_device_get_mountpath(device);
+                               if (mount_path != NULL) {
+                                       MS_DBG_ERR("mount_path [%s]", mount_path);
+                                       if (strlen(mount_path) == strlen(storage_path)) {
+                                               if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
+                                                       MS_DBG_ERR("start path is mounted [%s]", start_path);
+                                                       ret = true;
+                                               }
+                                       }
+                               }
+                       }
+
+                       if (ret != true) {
+                               while(usb_device_list_get_next(list, &device) == 0) {
+                                       if(device != NULL) {
+                                               mount_path = usb_device_get_mountpath(device);
+                                               if (mount_path != NULL) {
+                                                       MS_DBG_ERR("mount_path [%s]", mount_path);
+                                                       if (strlen(mount_path) == strlen(storage_path)) {
+                                                               if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
+                                                                       MS_DBG_ERR("start path is mounted [%s]", start_path);
+                                                                       ret = true;
+                                                                       break;
+                                                               }
+                                                       }
+                                               }
+                                       }
                                }
                        }
-                       ms_sys_release_device_list(&dev_list);
-               } else {
-                       MS_DBG_ERR("DEV NOT FOUND");
                }
+
+               usb_device_free_device_list(list);
        } else {
-               MS_DBG_ERR("ms_sys_get_device_list failed");
+               MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
        }
 
        MS_SAFE_FREE(storage_path);
-
+#endif
        return ret;
 }
 
@@ -2160,6 +2527,68 @@ int msc_del_blocked_path(void)
        return MS_MEDIA_ERR_NONE;
 }
 
+static int __msc_check_memory_status(uid_t uid)
+{
+       int err = MS_MEDIA_ERR_NONE;
+       double db_size = 0.0;
+
+       /*check remain space*/
+       err = __msc_check_remain_space(uid);
+       if (err == MS_MEDIA_ERR_NOT_ENOUGH_SPACE
+               || err == MS_MEDIA_ERR_DB_FULL_FAIL){
+               return err;
+       }
+       
+       /*check db size*/
+       msc_check_size_mediadb(uid, &db_size);
+       if (db_size >= MEDIA_DB_SIZE_LIMIT_1) {
+               MS_DBG_ERR("DB SIZE [%lf] REACH THE FIRST LIMIT [%lf]", db_size, MEDIA_DB_SIZE_LIMIT_1);
+               return MS_MEDIA_ERR_DB_LIMIT_1;
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+static int __msc_check_remain_space(uid_t uid)
+{
+       int err = MS_MEDIA_ERR_NONE;
+       double free_space = 0.0;
+       double db_size = 0.0;
+
+       err = ms_get_remain_space(&free_space);
+       if (err != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_get_remain_space failed");
+       } else {
+               if (free_space < MEDIA_DB_SPACE_LIMIT) {
+                       MS_DBG_ERR("NOT ENOUGH SPACE WE DO NOT UPDATE MEDIA DB FREE SPACE [%lf]", free_space);
+                       ms_config_set_int(MS_DB_LIMIT, 0);
+                       return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;
+               }
+       }
+
+       err = msc_check_size_mediadb(uid, &db_size);
+       if (err != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("msc_check_dbsize failed");
+       } else {
+               if (db_size >= MEDIA_DB_SIZE_LIMIT_2) {
+                       MS_DBG_ERR("REACH LIMIT DB SIZE [%lf]", db_size);
+
+                       MS_DBG_ERR("START REMOVE INVALID DATA");
+                       msc_cleanup_invalid_values(uid);
+                       MS_DBG_ERR("END REMOVE INVALID DATA");
+
+                       msc_check_size_mediadb(uid, &db_size);
+                       if (db_size >= MEDIA_DB_SIZE_LIMIT_2) {
+                               ms_config_set_int(MS_DB_LIMIT, 0);
+                               MS_DBG_ERR("DB IS FULL. CANNOT UPDATE[%lf]", db_size);
+                               return MS_MEDIA_ERR_DB_FULL_FAIL;
+                       }
+               }
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
 int msc_init_scanner(void)
 {
        if (!scan_queue2) scan_queue2 = g_async_queue_new();
@@ -2279,3 +2708,16 @@ int msc_get_dir_scan_status(bool *scan_status)
        return MS_MEDIA_ERR_NONE;
 }
 
+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;
+}
+
index cddd6fc..84f4f24 100755 (executable)
@@ -43,7 +43,8 @@ extern GAsyncQueue *scan_queue2;
 extern GAsyncQueue *reg_queue2;
 extern GMutex scan_req_mutex2;
 extern GAsyncQueue *folder_extract_queue;
-
+extern int g_directory_scan_processing2;
+//#define SUPPORT_PARTIAL_EVENT
 static int _msc_remove_request(GAsyncQueue *req_queue, ms_comm_msg_s *recv_msg)
 {
        char *cancel_path = recv_msg->msg;
@@ -56,49 +57,85 @@ static int _msc_remove_request(GAsyncQueue *req_queue, ms_comm_msg_s *recv_msg)
        MS_DBG_WARN("scan_req_mutex2 is LOCKED");
        g_mutex_lock(&scan_req_mutex2);
 
-       if (len == 0) {
-               MS_DBG_WARN("Request is not stacked");
+       if (len <= 0 && g_directory_scan_processing2 == 0) {
+               MS_DBG_WARN("Request is not stacked[%d]", len);
                goto END_REMOVE_REQUEST;
        }
 
-       msc_set_cancel_path(recv_msg->msg);
-       msc_set_extract_cancel_path(recv_msg->msg);
+       MS_DBG_WARN("len == [%d]", len);
 
-       temp_scan_queue = g_async_queue_new();
+       msc_set_cancel_scan_item(recv_msg->msg, recv_msg->pid);
+       msc_set_cancel_extract_item(recv_msg->msg, recv_msg->pid);
 
-       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;
+       if (len > 0) {
+               temp_scan_queue = g_async_queue_new();
 
-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++) {
+               MS_DBG_WARN("start update scan_queue2");
+               for (i = 0; i <len; i++) {
                        /*create new queue to compare request*/
-                       msg = g_async_queue_pop(folder_extract_queue);
+                       msg = g_async_queue_pop(scan_queue2);
+                       MS_DBG_WARN("msg->msg[%s], msg->pid[%d]", msg->msg, msg->pid);
                        if ((strcmp(msg->msg, cancel_path) == 0) && (pid == msg->pid)) {
+                               msg->msg_type = MS_MSG_DIRECTORY_SCANNING_CANCEL;
+                               msc_send_result(MS_MEDIA_ERR_SCANNER_FORCE_STOP, msg);
                                MS_SAFE_FREE(msg);
                        } else {
-                               g_async_queue_push(temp_extract_queue, GINT_TO_POINTER(msg));
+                               g_async_queue_push(temp_scan_queue, GINT_TO_POINTER(msg));
                        }
                }
-               g_async_queue_unref(folder_extract_queue);
-               folder_extract_queue = temp_extract_queue;
+
+               len = g_async_queue_length(temp_scan_queue);
+               int j = 0;
+               for (; j <len; j++) {
+                       msg = g_async_queue_pop(temp_scan_queue);
+                       if (msg) {
+                               g_async_queue_push(scan_queue2, GINT_TO_POINTER(msg));
+                       }
+               }
+               g_async_queue_unref (temp_scan_queue);
+               MS_DBG_WARN("end update scan_queue2");
        }
 
+END_REMOVE_REQUEST:
+       {
+               GAsyncQueue *temp_extract_queue = NULL;
+               int len_extract = g_async_queue_length(folder_extract_queue);
+               MS_DBG_WARN("len [%d]", len_extract);
+
+               if (len_extract > 0) {
+                       temp_extract_queue = g_async_queue_new();
+                       MS_DBG_WARN("start update folder_extract_queue");
+                       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_DBG_WARN("cancel path [%s]", cancel_path);
+                                       if (msg->result) {
+                                               msg->msg_type = MS_MSG_DIRECTORY_SCANNING_CANCEL;
+                                               msc_send_result(MS_MEDIA_ERR_SCANNER_FORCE_STOP, msg);
+                                       }
+                                       MS_SAFE_FREE(msg);
+                               } else {
+                                       g_async_queue_push(temp_extract_queue, GINT_TO_POINTER(msg));
+                               }
+                       }
+
+                       len = g_async_queue_length(temp_extract_queue);
+                       int j = 0;
+                       for (; j < len; j++) {
+                               msg = g_async_queue_pop(temp_extract_queue);
+                               if (msg) {
+                                       g_async_queue_push(folder_extract_queue, GINT_TO_POINTER(msg));
+                               }
+                       }
+                       g_async_queue_unref (temp_extract_queue);
+                       MS_DBG_WARN("end update folder_extract_queue");
+               }
+       }
+
+       g_mutex_unlock(&scan_req_mutex2);
+       MS_DBG_WARN("scan_req_mutex2 is UNLOCKED");
+
        return MS_MEDIA_ERR_NONE;
 }
 
@@ -140,7 +177,7 @@ gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer d
                        {
                                MS_DBG_INFO("BULK INSERT");
                                /* request bulk insert*/
-                               msc_push_scan_request(MS_SCAN_REGISTER, recv_msg);
+                               g_async_queue_push(reg_queue2, GINT_TO_POINTER(recv_msg));
                        }
                        break;
                case MS_MSG_DIRECTORY_SCANNING:
@@ -148,7 +185,7 @@ gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer d
                        {
                                /* this request from another apps */
                                /* set the scan data for scanning thread */
-                               msc_push_scan_request(MS_SCAN_DIRECTORY, recv_msg);
+                               g_async_queue_push(scan_queue2, GINT_TO_POINTER(recv_msg));
                        }
                        break;
                case MS_MSG_STORAGE_ALL:
@@ -156,12 +193,13 @@ gboolean msc_receive_request(GIOChannel *src, GIOCondition condition, gpointer d
                case MS_MSG_STORAGE_INVALID:
                        {
                                /* this request from media-server */
-                               msc_push_scan_request(MS_SCAN_STORAGE, recv_msg);
+                               g_async_queue_push(storage_queue2, GINT_TO_POINTER(recv_msg));
                        }
                        break;
                case MS_MSG_DIRECTORY_SCANNING_CANCEL:
                        {
                                _msc_remove_request(scan_queue2, recv_msg);
+                               msc_send_result(MS_MEDIA_ERR_SCANNER_FORCE_STOP, recv_msg);
                                MS_SAFE_FREE(recv_msg);
                        }
                        break;
@@ -210,6 +248,7 @@ int msc_send_ready(void)
 
 int msc_send_result(int result, ms_comm_msg_s *res_data)
 {
+       MS_DBG_SLOG("msc_send_result msg_type=%d", res_data->msg_type);
        int res = MS_MEDIA_ERR_NONE;
        ms_comm_msg_s send_msg;
        int fd = -1;
@@ -232,7 +271,6 @@ int msc_send_result(int result, ms_comm_msg_s *res_data)
        send_msg.pid = res_data->pid;
        send_msg.result = result;
        send_msg.msg_size = res_data->msg_size;
-       send_msg.uid = res_data->uid;
        strncpy(send_msg.msg, res_data->msg, send_msg.msg_size);
 
        /* send ready message */
@@ -247,8 +285,9 @@ int msc_send_result(int result, ms_comm_msg_s *res_data)
        return res;
 }
 
-int msc_send_result_partial(int result, ms_msg_type_e msg_type, int pid, char *msg)
+int msc_send_result_partial(int result, ms_msg_type_e msg_type, int pid, const char *msg)
 {
+#ifdef SUPPORT_PARTIAL_EVENT
        MS_DBG_SLOG("msc_send_result msg_type=%d", msg_type);
        int res = MS_MEDIA_ERR_NONE;
        ms_comm_msg_s send_msg;
@@ -279,6 +318,10 @@ int msc_send_result_partial(int result, ms_msg_type_e msg_type, int pid, char *m
        close(fd);
 
        return res;
+#else
+       MS_DBG_SLOG("partial even not support!");
+       return MS_MEDIA_ERR_NONE;
+#endif
 }
 
 
index 5a1e424..5384e4b 100755 (executable)
  */
 
 #include <dirent.h>
+#include <vconf.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-scanner-socket-v2.h"
 #include "media-scanner-extract-v2.h"
 
+#define APP_NAME "media-scanner-v2"
+
+#define MSC_VCONF_KEY_SCANNER_POWEROFF_STATUS "db/mediascanner/poweroff_status"
+
+extern int mmc_state2;
+
+extern GAsyncQueue *storage_queue2;
+extern GAsyncQueue *scan_queue2;
+extern GAsyncQueue *reg_queue2;
+extern GAsyncQueue *storage_extract_queue;
+extern GAsyncQueue *folder_extract_queue;
+extern GMutex scan_req_mutex2;
+extern int g_directory_scan_processing2;
+extern bool g_directory_extract_processing;
+bool power_off2; /*If this is TRUE, poweroff notification received*/
+
 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);
 
+#ifndef _USE_DEVICED_DBUS
+static void _power_off_cb(void* data)
+{
+       ms_comm_msg_s *scan_data;
+       ms_comm_msg_s *reg_data;
+
+       MS_DBG_ERR("POWER OFF CB Begin");
+
+       //display_lock_state(POWER_STATE_STANDBY, 0);
+
+       power_off2 = true;
+
+       if (scan_queue2) {
+               /*notify to scannig thread*/
+               MS_MALLOC(scan_data, sizeof(ms_comm_msg_s));
+               if (scan_data != NULL) {
+                       scan_data->pid = POWEROFF;
+                       g_async_queue_push(scan_queue2, GINT_TO_POINTER(scan_data));
+               }
+       }
+
+       if (reg_queue2) {
+               /*notify to register thread*/
+               MS_MALLOC(reg_data, sizeof(ms_comm_msg_s));
+               if (reg_data != NULL) {
+                       reg_data->pid = POWEROFF;
+                       g_async_queue_push(reg_queue2, GINT_TO_POINTER(reg_data));
+               }
+       }
+
+       if (storage_queue2) {
+               /*notify to register thread*/
+               MS_MALLOC(reg_data, sizeof(ms_comm_msg_s));
+               if (reg_data != NULL) {
+                       reg_data->pid = POWEROFF;
+                       g_async_queue_push(storage_queue2, GINT_TO_POINTER(reg_data));
+               }
+       }
+
+       if (storage_extract_queue) {
+               /*notify to storage extract thread*/
+               MS_MALLOC(reg_data, sizeof(ms_comm_msg_s));
+               if (reg_data != NULL) {
+                       reg_data->pid = POWEROFF;
+                       g_async_queue_push(storage_extract_queue, GINT_TO_POINTER(reg_data));
+               }
+       }
+
+       if (folder_extract_queue) {
+               /*notify to folder extract thread*/
+               MS_MALLOC(reg_data, sizeof(ms_comm_msg_s));
+               if (reg_data != NULL) {
+                       reg_data->pid = POWEROFF;
+                       g_async_queue_push(folder_extract_queue, GINT_TO_POINTER(reg_data));
+               }
+       }
+
+       if (g_main_loop_is_running(scanner_mainloop2)) g_main_loop_quit(scanner_mainloop2);
+
+       if(!ms_config_set_int(MSC_VCONF_KEY_SCANNER_POWEROFF_STATUS, 1)) {
+               MS_DBG_ERR("set power off status to 1 failed");
+       }
+
+       MS_DBG_ERR("POWER OFF END");
+}
+#endif
+
+void _msc_mmc_vconf_cb(void *data)
+{
+       int status = 0;
+
+       if (!ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status)) {
+               MS_DBG_ERR("Get VCONFKEY_SYSMAN_MMC_STATUS failed.");
+       }
+
+       MS_DBG_INFO("VCONFKEY_SYSMAN_MMC_STATUS :%d", status);
+
+       mmc_state2 = status;
+
+       return;
+}
+
 int main(int argc, char **argv)
 {
        GThread *storage_scan_thread = NULL;
@@ -55,7 +152,6 @@ int main(int argc, char **argv)
        GSource *source = NULL;
        GIOChannel *channel = NULL;
        GMainContext *context = NULL;
-       bool power_off_status = FALSE;
 
        int err = -1;
        int fd = -1;
@@ -74,6 +170,8 @@ int main(int argc, char **argv)
        /*Init main loop*/
        scanner_mainloop2 = g_main_loop_new(NULL, FALSE);
 
+       /*Init for register file*/
+       /*These are a communicator for thread*/
        msc_init_scanner();
        msc_init_extract_thread();
        msc_init_scan_thread();
@@ -96,6 +194,10 @@ int main(int argc, char **argv)
                return MS_MEDIA_ERR_FILE_OPEN_FAIL;
        }
 
+       if(!ms_config_set_int(MSC_VCONF_KEY_SCANNER_POWEROFF_STATUS, 0)) {
+               MS_DBG_ERR("set power off status to 0 failed");
+       }
+
        context = g_main_loop_get_context(scanner_mainloop2);
 
        /* Create new channel to watch pipe */
@@ -107,6 +209,9 @@ int main(int argc, char **argv)
        g_source_attach(source, context);
        g_source_unref(source);
 
+       MS_DBG_ERR("media_scanner_v2 set power_off2 to false...");
+       power_off2 = false;
+
        /*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);
@@ -114,9 +219,12 @@ int main(int argc, char **argv)
        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);
 
+#ifdef _USE_SDCARD
        if (ms_is_mmc_inserted()) {
                msc_set_mmc_status(MS_STG_INSERTED);
        }
+#endif
+       MS_DBG_INFO("scanner v2 is ready");
 
        msc_send_ready();
        MS_DBG_ERR("[No-Error] ========== Scanner is ready ========");
@@ -125,6 +233,8 @@ int main(int argc, char **argv)
 
        g_main_loop_run(scanner_mainloop2);
 
+       __msc_remove_event_receiver();
+
        g_thread_join(scan_thread);
        g_thread_join(register_thread);
        g_thread_join(storage_scan_thread);
@@ -134,14 +244,9 @@ int main(int argc, char **argv)
        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, PM_RESET_TIMER);
-       }
-
+       msc_deinit_scanner();
        msc_deinit_extract_thread();
        msc_deinit_scan_thread();
-       msc_deinit_scanner();
 
        /*close pipe*/
        close(fd);
@@ -149,15 +254,16 @@ int main(int argc, char **argv)
        /*unload functions*/
        ms_unload_functions();
 
-       __msc_remove_event_receiver();
-
 EXIT:
-       MS_DBG_ERR("[No-Error] ========== Scanner end  ========");
+       if(!ms_config_set_int(MSC_VCONF_KEY_SCANNER_POWEROFF_STATUS, 0)) {
+               MS_DBG_ERR("set power off status to 0 failed");
+       }
 
+       MS_DBG_ERR("SCANNER V2 IS END");
        return 0;
 }
 
-
+#ifdef _USE_DEVICED_DBUS
 static void __msc_power_off_cb(ms_power_info_s *power_info, void* data)
 {
        msc_stop_scan_thread();
@@ -165,6 +271,14 @@ static void __msc_power_off_cb(ms_power_info_s *power_info, void* data)
 
        if (g_main_loop_is_running(scanner_mainloop2)) g_main_loop_quit(scanner_mainloop2);
 }
+#else
+int __msc_power_off_cb(int option, void *data)
+{
+       _power_off_cb(NULL);
+
+       return MS_MEDIA_ERR_NONE;
+}
+#endif
 
 static void __msc_add_event_receiver(void *data)
 {
diff --git a/src/scanner/include/media-scanner-db-manage.h b/src/scanner/include/media-scanner-db-manage.h
new file mode 100755 (executable)
index 0000000..5c6ae8d
--- /dev/null
@@ -0,0 +1,30 @@
+/*\r
+ *  Media Server\r
+ *\r
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.\r
+ *\r
+ * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ */\r
+\r
+#ifndef _MEDIA_SCANNER_DB_MANAGER_H__\r
+#define _MEDIA_SCANNER_DB_MANAGER_H__\r
+\r
+#include "media-common-types.h"\r
+\r
+int msc_check_db_size(uid_t uid, int scan_type);\r
+\r
+#endif\r
+\r
index bb73520..01eb87c 100755 (executable)
 
 #include "media-common-system.h"
 
+#ifdef _USE_DEVICED_DBUS
 void msc_device_block_changed_cb(ms_block_info_s *block_info, void *user_data);
+#else
+void msc_device_block_changed_cb(usb_device_h usb_device, char *action, void *user_data);
+#endif
 
 #endif
\ No newline at end of file
diff --git a/src/scanner/media-scanner-db-manage.c b/src/scanner/media-scanner-db-manage.c
new file mode 100755 (executable)
index 0000000..bfc97ea
--- /dev/null
@@ -0,0 +1,72 @@
+/*\r
+ *  Media Server\r
+ *\r
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.\r
+ *\r
+ * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ */\r
+\r
+#include <errno.h>\r
+#include <vconf.h>\r
+#include <aul/aul.h>\r
+#include <sys/statvfs.h>\r
+#include <sys/stat.h>\r
+#include <errno.h>\r
+\r
+#include "media-util.h"\r
+#include "media-common-db-svc.h"\r
+#include "media-common-utils.h"\r
+#include "media-scanner-dbg.h"\r
+#include "media-scanner-db-manage.h"\r
+\r
+int msc_check_db_size(uid_t uid, int scan_type)\r
+{\r
+       double db_size = 0.0;\r
+       double free_space = 0.0;\r
+       int err = MS_MEDIA_ERR_NONE;\r
+\r
+//     ms_check_dbsize(&db_size);\r
+\r
+       if (scan_type == MS_MSG_STORAGE_ALL\r
+               || scan_type == MS_MSG_STORAGE_PARTIAL) {\r
+               if (db_size >= MEDIA_DB_SIZE_LIMIT_1) {\r
+                       MS_DBG_ERR("DB SIZE [%lf] reach the MEDIA_DB_SIZE_LIMIT_1[%lf]", db_size, MEDIA_DB_SIZE_LIMIT_1);\r
+                       return MS_MEDIA_ERR_DB_LIMIT_1;\r
+               }\r
+       } else if (scan_type == MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE\r
+               || scan_type == MS_MSG_DIRECTORY_SCANNING) {\r
+               if (db_size >= MEDIA_DB_SIZE_LIMIT_2) {\r
+                       MS_DBG_ERR("DB SIZE [%lf] reach the MEDIA_DB_SIZE_LIMIT_2[%lf]", db_size, MEDIA_DB_SIZE_LIMIT_2);\r
+                       ms_config_set_int(MS_DB_LIMIT, 0);\r
+                       return MS_MEDIA_ERR_DB_FULL_FAIL;\r
+               }\r
+       }\r
+\r
+       /*check remain space*/\r
+       err = ms_get_remain_space(&free_space);\r
+       if (err != MS_MEDIA_ERR_NONE) {\r
+               MS_DBG_ERR("ms_get_remain_space failed");\r
+       } else {\r
+               if (free_space < MEDIA_DB_SPACE_LIMIT) {\r
+                       MS_DBG_ERR("FREE SPACE [%lf] DB SIZE [%lf]", free_space, db_size);\r
+                       return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;\r
+               }\r
+       }\r
+\r
+       return MS_MEDIA_ERR_NONE;\r
+}\r
+\r
+\r
index 7ac1c34..618e066 100755 (executable)
  * 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-scanner-scan.h"
 #include "media-scanner-device-block.h"
 
+extern GAsyncQueue * storage_queue;
+extern bool g_directory_scan_processing;
+
+#ifdef _USE_DEVICED_DBUS
 static void __msc_usb_remove_event(const char *mount_path)
 {
        return;
@@ -67,3 +74,45 @@ void msc_device_block_changed_cb(ms_block_info_s *block_info, void *user_data)
                _msc_mmc_changed_event(block_info->mount_path, block_info->state);
        }
 }
+#else
+void msc_device_block_changed_cb(usb_device_h usb_device, char *action, void *user_data)
+{
+       char *mount_path = NULL;
+
+       if (!usb_device) {
+               MS_DBG_ERR("usb_device is NULL");
+               return;
+       }
+
+       if (usb_device_get_class(usb_device) != USB_MASS_STORAGE) {
+               free_usb_device_h(usb_device);
+               MS_DBG_ERR("usb_device is NOT STORAGE");
+               return;
+       }
+
+       mount_path = usb_device_get_mountpath(usb_device);
+
+        if (!strcmp(action, "blocked")) {
+               MS_DBG_ERR("===========================================================");
+               MS_DBG_ERR("USB BLOCKED, mountpath : %s", mount_path);
+               MS_DBG_ERR("===========================================================");
+               int status  = -1;
+
+               if(!ms_config_get_int(VCONFKEY_FILEMANAGER_DB_STATUS, &status)) {
+                       MS_DBG_ERR("ms_config_get_int[VCONFKEY_FILEMANAGER_DB_STATUS]");
+               }
+               if (!(g_async_queue_length(storage_queue) == 0 && status == VCONFKEY_FILEMANAGER_DB_UPDATED)) {
+                       msc_set_blocked_path(mount_path);
+               }
+
+               if (g_directory_scan_processing == true) {
+                       MS_DBG_ERR("Doing directory scanning. Set cancel path");
+                       msc_set_cancel_path(mount_path);
+               }
+        }
+
+       free_usb_device_h(usb_device);
+
+       return;
+}
+#endif
index 63fb99d..02c22e8 100755 (executable)
@@ -464,7 +464,7 @@ static int __msc_stg_scan(void **handle, const char *storage_id, const char*star
                return MS_MEDIA_ERR_OUT_OF_MEMORY;
        }
 
-       g_array_append_val(dir_array, start_path);
+       g_array_append_val(dir_array, new_start_path);
 
        MS_DBG_ERR("[No-Error] start path [%s]", new_start_path);
 
@@ -568,7 +568,7 @@ static int __msc_stg_scan(void **handle, const char *storage_id, const char*star
        }               /*db update while */
 
                /*remove invalid folder in folder table.*/
-       if (__msc_check_mount_storage(new_start_path)) {
+       if (__msc_check_mount_storage(start_path)) {
                if (ms_delete_invalid_folder(handle, storage_id, storage_type, uid) != MS_MEDIA_ERR_NONE) {
                        MS_DBG_ERR("delete invalid folder failed");
                        ret = MS_MEDIA_ERR_DB_DELETE_FAIL;
@@ -581,8 +581,6 @@ STOP_SCAN:
        if (dp) closedir(dp);
        dp = NULL;
 
-       MS_SAFE_FREE(new_start_path);
-
        __msc_clear_file_list(dir_array);
 
        if (ret != MS_MEDIA_ERR_NONE) MS_DBG_INFO("ret : %d", ret);
@@ -962,6 +960,7 @@ static int _msc_db_update_partial(void **handle, const char *storage_id, ms_stor
 //             if (dir_info->modified_time != -1) {
 //                     ms_update_folder_time(handle, tmp_path);
 //             }
+               MS_SAFE_FREE(update_path);
        }
 
        /*delete all node*/
@@ -1927,3 +1926,37 @@ END_REMOVE_REQUEST:
        return MS_MEDIA_ERR_NONE;
 }
 
+int msc_set_blocked_path(const char *blocked_path)
+{
+       MS_DBG_FENTER();
+       g_mutex_lock(&blocked_mutex);
+
+       if (g_blocked_path != NULL) {
+               MS_DBG_ERR("g_blocked_path is not NULL [%s]", g_blocked_path);
+               free(g_blocked_path);
+               g_blocked_path = NULL;
+       }
+
+       g_blocked_path = strdup(blocked_path);
+
+       g_mutex_unlock(&blocked_mutex);
+       MS_DBG_FLEAVE();
+       return MS_MEDIA_ERR_NONE;
+}
+
+int msc_del_blocked_path(void)
+{
+       MS_DBG_FENTER();
+       g_mutex_lock(&blocked_mutex);
+
+       if (g_blocked_path != NULL) {
+               MS_DBG_ERR("g_blocked_path is not NULL [%s]", g_blocked_path);
+               free(g_blocked_path);
+               g_blocked_path = NULL;
+       }
+
+       g_mutex_unlock(&blocked_mutex);
+       MS_DBG_FLEAVE();
+       return MS_MEDIA_ERR_NONE;
+}
+
index 8631e2c..5e56d13 100755 (executable)
@@ -104,10 +104,11 @@ 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);
 
+#ifdef _USE_SDCARD
        if (ms_is_mmc_inserted()) {
                msc_set_mmc_status(MS_STG_INSERTED);
        }
-
+#endif
        MS_DBG_INFO("scanner is ready");
 
        msc_send_ready();
@@ -141,12 +142,23 @@ EXIT:
        return 0;
 }
 
+#ifdef _USE_DEVICED_DBUS
 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);
 }
+#else
+int __msc_power_off_cb(int option, void *data)
+{
+       msc_send_power_off_request();
+
+       if (g_main_loop_is_running(scanner_mainloop)) g_main_loop_quit(scanner_mainloop);
+
+       return MS_MEDIA_ERR_NONE;
+}
+#endif
 
 static void __msc_add_event_receiver(void *data)
 {
diff --git a/src/server/include/media-server-db-manage.h b/src/server/include/media-server-db-manage.h
new file mode 100755 (executable)
index 0000000..992285c
--- /dev/null
@@ -0,0 +1,32 @@
+/*\r
+ *  Media Server\r
+ *\r
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.\r
+ *\r
+ * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ */\r
+\r
+#ifndef _MEDIA_SERVER_DB_MANAGER_H__\r
+#define _MEDIA_SERVER_DB_MANAGER_H__\r
+\r
+#include "media-common-types.h"\r
+\r
+int ms_check_size_mediadb(uid_t uid, double *db_size);\r
+int ms_reset_mediadb(uid_t uid);\r
+int ms_check_mediadb(uid_t uid, bool *is_reset);\r
+int ms_check_corrupt_mediadb(void);\r
+\r
+#endif\r
index 446c62e..8f01033 100755 (executable)
@@ -27,6 +27,11 @@ 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, const char *mount_uuid);
 int ms_usb_remove_handler(const char *mount_path, const char *mount_uuid);
+#ifdef _USE_DEVICED_DBUS
 void ms_device_block_changed_cb(ms_block_info_s *block_info, void *user_data);
+#else
+void ms_device_block_changed_cb(usb_device_h usb_device, char *action, void *user_data);
+#endif
+int ms_check_mounted_storage(uid_t uid);
 
 #endif
\ No newline at end of file
index 69af500..f8efb11 100755 (executable)
@@ -34,5 +34,9 @@ int ms_send_storage_scan_request(const char *root_path, const char *storage_id,
 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, uid_t uid);
+int ms_reset_ownerlist();
+#ifdef _USE_SENIOR_MODE
+int ms_send_storage_scan_request_senior_mode(void **handle);
+#endif
 
 #endif /*_MEDIA_SERVER_SOCKET_H_*/
diff --git a/src/server/media-server-db-manage.c b/src/server/media-server-db-manage.c
new file mode 100755 (executable)
index 0000000..83ef79c
--- /dev/null
@@ -0,0 +1,151 @@
+/*\r
+ *  Media Server\r
+ *\r
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.\r
+ *\r
+ * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ */\r
+\r
+#include <errno.h>\r
+#include <sys/stat.h>\r
+#include <sqlite3.h>\r
+\r
+#include "media-util.h"\r
+#include "media-common-db-svc.h"\r
+#include "media-common-system.h"\r
+#include "media-common-utils.h"\r
+#include "media-server-ipc.h"\r
+#include "media-server-dbg.h"\r
+#include "media-server-scanner.h"\r
+#include "media-server-thumb.h"\r
+#include "media-server-db-manage.h"\r
+\r
+int ms_check_size_mediadb(uid_t uid, double *db_size)\r
+{\r
+       int ret = MS_MEDIA_ERR_NONE;\r
+       char *db_path = NULL;\r
+       struct stat buf;\r
+\r
+       ret = media_db_get_media_db_path(uid, &db_path);\r
+\r
+       if(stat(db_path, &buf) == 0) {\r
+               *db_size = buf.st_size;\r
+       } else {\r
+               MS_DBG_STRERROR("stat failed");\r
+               ret = MS_MEDIA_ERR_INTERNAL;\r
+       }\r
+\r
+       MS_SAFE_FREE(db_path);\r
+\r
+       return ret;\r
+}\r
+\r
+int ms_reset_mediadb(uid_t uid)\r
+{\r
+       MS_DBG_ERR("[MEDIA DB RESET START]");\r
+       int ret = MS_MEDIA_ERR_NONE;\r
+       char *command = "/bin/cp";\r
+       char *dummy_path = MS_DUMMY_MEDIA_DB_PATH;\r
+       char *db_path = NULL;\r
+\r
+       ret = media_db_get_media_db_path(uid, &db_path);\r
+       if ((ret == MS_MEDIA_ERR_NONE) && (db_path != NULL)) {\r
+               char command_line[MS_FILE_PATH_LEN_MAX] = {0,};\r
+               snprintf(command_line, sizeof(command_line), "%s %s %s",  command, dummy_path, db_path);\r
+\r
+               MS_DBG_ERR("[RESET COMMAND %s]",command_line);\r
+               WEXITSTATUS(system(command_line));\r
+       }\r
+\r
+       MS_SAFE_FREE(db_path);\r
+\r
+       MS_DBG_ERR("[MEDIA DB RESET END]");\r
+\r
+       return MS_MEDIA_ERR_NONE;\r
+}\r
+\r
+static int __ms_remake_mediadb(uid_t uid)\r
+{\r
+       int err = MS_MEDIA_ERR_NONE;\r
+       void **handle = NULL;\r
+\r
+       MS_DBG_ERR("THE SIZE OF MEDIA DB REACH THE LIMIT. RESET MEDIA DB.");\r
+\r
+       /*write read schema*/\r
+       /*load functions from plusin(s)*/\r
+       err = ms_load_functions();\r
+       if (err != MS_MEDIA_ERR_NONE) {\r
+               MS_DBG_ERR("function load failed [%d]", err);\r
+               return err;\r
+       }\r
+\r
+       ms_config_set_int(MS_DB_RESET, 1);\r
+\r
+       ms_reset_mediadb(uid);\r
+       \r
+       ms_connect_db(&handle, uid);\r
+\r
+       MS_DBG_ERR("START WRITE SCHEMA");\r
+       if (ms_check_db_upgrade(handle, uid)  != MS_MEDIA_ERR_NONE) {\r
+               MS_DBG_ERR("ms_check_db_upgrade fail");\r
+       }\r
+       MS_DBG_ERR("END WRITE SCHEMA");\r
+       \r
+       /*disconnect form media db*/\r
+       if (handle) ms_disconnect_db(&handle);\r
+       \r
+       /*unload functions*/\r
+       ms_unload_functions();\r
+\r
+       ms_config_set_int(MS_DB_RESET, 0);\r
+\r
+       return MS_MEDIA_ERR_NONE;\r
+}\r
+\r
+int ms_check_mediadb(uid_t uid, bool *is_reset)\r
+{\r
+       double db_size = 0.0;\r
+\r
+       ms_check_size_mediadb(uid, &db_size);\r
+\r
+       MS_DBG_ERR("[DB SIZE : %lf] [LIMIT1 : %lf] [LIMIT2 : %lf]", db_size, MEDIA_DB_SIZE_LIMIT_1, MEDIA_DB_SIZE_LIMIT_2);\r
+       if (db_size > MEDIA_DB_SIZE_LIMIT_2) {\r
+               MS_DBG_ERR("THE SIZE OF MEDIA DB REACH THE LIMIT. RESET MEDIA DB.");\r
+               __ms_remake_mediadb(uid);\r
+               *is_reset = TRUE;\r
+       }\r
+\r
+       return MS_MEDIA_ERR_NONE;\r
+}\r
+\r
+int ms_check_corrupt_mediadb(void)\r
+{\r
+       MediaDBHandle *db_handle = NULL;\r
+       uid_t uid = MEDIA_DEFAULT_UID;\r
+\r
+       ms_sys_get_uid(&uid);   \r
+\r
+       if(media_db_connect(&db_handle, uid, TRUE) != MS_MEDIA_ERR_NONE) {\r
+               MS_DBG_ERR("Failed to connect DB\n");\r
+               return MS_MEDIA_ERR_DB_CONNECT_FAIL;\r
+       }\r
+\r
+       /* Disconnect DB*/\r
+       media_db_disconnect(db_handle);\r
+\r
+       return MS_MEDIA_ERR_NONE;\r
+}\r
+\r
index 76d3b85..3b78c57 100755 (executable)
 #include <sys/stat.h>
 #include "media-common-db-svc.h"
 #include "media-common-external-storage.h"
+#include "media-common-system.h"
 
 #include "media-util-err.h"
 #include "media-server-dbg.h"
 #include "media-server-socket.h"
+#include "media-server-scanner.h"
+#include "media-server-db-manage.h"
 #include "media-server-device-block.h"
 
-#if 0
+#ifndef _USE_DEVICED_DBUS
 static int __ms_get_added_stroage_path(void **handle, const char *add_path, char **device_id)
 {
        int ret = MS_MEDIA_ERR_NONE;
@@ -63,7 +66,7 @@ int ms_usb_insert_handler(const char *mount_path, const char *mount_uuid)
        void **handle = NULL;
        char *uuid = NULL;
        int validity = 0;
-       uid_t uid;
+       uid_t uid = MEDIA_DEFAULT_UID;
        ms_dir_scan_type_t scan_type = MS_SCAN_ALL;
 
        ret = ms_load_functions();
@@ -130,7 +133,7 @@ int ms_usb_remove_handler(const char *mount_path, const char *mount_uuid)
 {
        int ret = MS_MEDIA_ERR_NONE;
        void **handle = NULL;
-       uid_t uid;
+       uid_t uid = MEDIA_DEFAULT_UID;
 
        if (mount_path != NULL && mount_uuid != NULL) {
                ret = ms_load_functions();
@@ -202,7 +205,7 @@ static int __ms_get_mmc_info(void **handle, char **storage_name, char **storage_
 static int __ms_insert_mmc_info(void **handle, const char *storage_name, const char *storage_path)
 {
        int ret = MS_MEDIA_ERR_NONE;
-       uid_t uid;
+       uid_t uid = MEDIA_DEFAULT_UID;
 
        ms_sys_get_uid(&uid);
 
@@ -218,7 +221,7 @@ static int __ms_insert_mmc_info(void **handle, const char *storage_name, const c
 static int __ms_update_mmc_info(void **handle, const char *old_storage_name, const char *new_storage_name, const char *new_storage_path)
 {
        int ret = MS_MEDIA_ERR_NONE;
-       uid_t uid;
+       uid_t uid = MEDIA_DEFAULT_UID;
 
        ms_sys_get_uid(&uid);
 
@@ -246,7 +249,7 @@ int ms_mmc_insert_handler(const char *mount_path)
        int validity = NULL;
        bool info_exist = FALSE;
        char *cid = NULL;
-       uid_t uid;
+       uid_t uid = MEDIA_DEFAULT_UID;
 
        void **db_handle = NULL;
 
@@ -307,7 +310,7 @@ int ms_mmc_remove_handler(const char *mount_path)
        char *storage_path = NULL;
        void **handle = NULL;
        int validity = 0;
-       uid_t uid;
+       uid_t uid = MEDIA_DEFAULT_UID;
 
        ret = ms_load_functions();
        if (ret != MS_MEDIA_ERR_NONE) {
@@ -386,6 +389,7 @@ void _ms_usb_changed_event(const char *mount_path, const char *mount_uuid, ms_st
        return;
 }
 
+#ifdef _USE_DEVICED_DBUS
 void ms_device_block_changed_cb(ms_block_info_s *block_info, void *user_data)
 {
        if (block_info->block_type == 0) {
@@ -396,4 +400,348 @@ void ms_device_block_changed_cb(ms_block_info_s *block_info, void *user_data)
                _ms_mmc_changed_event(block_info->mount_path, block_info->state, block_info->flags);
        }
 }
+#else
+static void __ms_usb_add_event(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;
+       uid_t uid = MEDIA_DEFAULT_UID;
+
+       ret = ms_load_functions();
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_load_functions failed [%d]", ret);
+               return;
+       }
+
+       ms_connect_db(&handle, uid);
+
+       if (mount_path != NULL) {
+               MS_DBG_ERR("added path [%s]", mount_path);
+
+               __ms_get_added_stroage_path(handle, mount_path, &storage_id);
+
+               if (storage_id != NULL) {
+                       /* update storage information into media DB */
+                       ret = ms_check_storage(handle, storage_id, NULL, &storage_path, &validity);
+                       if (ret == 0) {
+                               if (validity == 1) {
+                                       MS_DBG_ERR("This storage is already updated. So ignore this event.");
+                                       goto ERROR;
+                               }
+
+                               if (strcmp(mount_path, storage_path)) {
+                                       /* update storage path */
+                                       ret = ms_update_storage(handle, storage_id, mount_path, uid);
+                               }
+                               scan_type = MS_SCAN_PART;
+                               ms_set_storage_validity(handle, storage_id, 1, uid);
+                               if (ms_set_storage_scan_status(handle, storage_id, MEDIA_SCAN_PREPARE, uid) != MS_MEDIA_ERR_NONE){
+                                       MS_DBG_ERR("ms_set_storage_scan_status failed");
+                               }
+                       } else {
+                               /* there is no information of this storage in Media DB */
+                               ret = ms_insert_storage(handle, storage_id, NULL, mount_path, uid);
+                       }
+               } else {
+                       MS_DBG_ERR("STORAGE ID IS NUILL");
+                       goto ERROR;
+               }
+
+               /* update storage_id table set validity=0 first */
+               ms_validaty_change_all_items(handle, storage_id, MS_STORAGE_EXTERNAL_USB, false, uid);
+               //ms_set_folder_validity(handle, storage_id, mount_path, MS_INVALID, TRUE);
+               /* update folder table set scan_status=0 first */
+               ms_set_folder_scan_status(handle, storage_id, NULL, MS_DIR_SCAN_NONE, uid);
+
+               /* request to update media db */
+               ms_send_storage_otg_scan_request(mount_path, storage_id, scan_type, uid);
+       }
+
+ERROR:
+       MS_SAFE_FREE(storage_id);
+       MS_SAFE_FREE(storage_path);
+
+       ms_disconnect_db(&handle);
+
+       ms_unload_functions();
+
+       return;
+}
+
+static void __ms_usb_remove_event(const char *mount_path)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       void **handle = NULL;
+       char device_id[MS_UUID_SIZE] = {0,};
+       uid_t uid = MEDIA_DEFAULT_UID;
+
+       if (mount_path != NULL) {
+               ret = ms_load_functions();
+               if (ret != MS_MEDIA_ERR_NONE) {
+                       MS_DBG_ERR("ms_load_functions failed [%d]", ret);
+                       return;
+               }
+
+               ms_connect_db(&handle, uid);
+
+               while(1) {
+                       memset(device_id, 0x0, sizeof(device_id));
+                       ret = ms_get_storage_id(handle, mount_path, device_id);
+
+                       MS_DBG_ERR("removed path [%s %s]", mount_path, device_id);
+
+                       if (ret == MS_MEDIA_ERR_NONE) {
+                               ms_set_storage_validity(handle, device_id, 0, uid);
+
+                               /* folder validity set 0 under the start_path in folder table*/
+                               if(ms_set_folder_validity(handle, device_id, mount_path, MS_INVALID, TRUE, uid) != MS_MEDIA_ERR_NONE) {
+                                       MS_DBG_ERR("set_folder_validity failed");
+                               }
+
+                               if (ms_get_scanner_status()) {
+                                       /*If the media scanner is running, the removed USB can be queueing in the job list.*/
+                                       /*So try to remove it.*/
+                                       ms_send_storage_otg_scan_request(mount_path, device_id, MS_SCAN_INVALID, uid);
+                               }
+                       } else {
+                               MS_DBG_ERR("Device ID is INVALID");
+                               break;
+                       }
+               }
+
+               ms_disconnect_db(&handle);
+
+               ms_unload_functions();
+       }
+
+       return;
+}
+
+void ms_device_block_changed_cb(usb_device_h usb_device, char *action, void *user_data)
+{
+       char *mount_path = NULL;
+       bool is_reset = FALSE;
+       uid_t uid = MEDIA_DEFAULT_UID;
+
+       MS_DBG_ERR("usb_event_callback BEGIN[%s]", action);
+       if (!usb_device) {
+               MS_DBG_ERR("usb_device is NULL");
+               return;
+       }
+
+       if (usb_device_get_class(usb_device) != USB_MASS_STORAGE) {
+               free_usb_device_h(usb_device);
+               MS_DBG_ERR("usb_device is NOT STORAGE");
+               return;
+       }
+
+       mount_path = usb_device_get_mountpath(usb_device);
+
+       ms_sys_get_uid(&uid);
+
+       if (!strcmp(action, "mounted")) {
+               MS_DBG_ERR("USB MOUNTED, mountpath : %s", mount_path);
+               ms_check_mediadb(uid, &is_reset);
+               __ms_usb_add_event(mount_path);
+       } else if (!strcmp(action, "unmounted")) {
+               MS_DBG_ERR("USB UNMOUNTED, mountpath : %s", mount_path);
+               ms_check_mediadb(uid, &is_reset);
+               if (!is_reset) {
+                       __ms_usb_remove_event(mount_path);
+               }
+       }
+
+       free_usb_device_h(usb_device);
+
+       if (is_reset) {
+               ms_check_mounted_storage(uid);
+       }
+       MS_DBG_ERR("usb_event_callback END[%s]", action);
+
+}
+
+static int __ms_check_mounted_storage_list(GArray **added_list)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       int count = 0;
+       int err = 0;
+       usb_device_list_h list;
+       usb_device_h device;
+       char *mount_path = NULL;
+       char *storage_path = NULL;
+
+       err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
+       if (err == 0) {
+               if (*added_list == NULL) {
+                       *added_list = g_array_new(FALSE, FALSE, sizeof(char*));
+               }
+
+               count = usb_device_list_get_count(list);
+               if (count > 0) {
+                       int i = 0;
+                       for(i = 0; i < count; i++) {
+                               if(i == 0) {
+                                       if (0 == usb_device_list_get_first(list, &device) && device != NULL) {
+                                               mount_path = usb_device_get_mountpath(device);
+                                               if (mount_path != NULL) {
+                                                       MS_DBG_ERR("mount_path [%s]", mount_path);
+                                                       storage_path = strdup(mount_path);
+                                                       g_array_append_val (*added_list, storage_path);
+                                               }
+                                       } else {
+                                               MS_DBG_ERR(" FAILED TO usb_device_list_get_first \n");
+                                       }
+                               } else {
+                                       if(0 == usb_device_list_get_next(list, &device) && device != NULL ) {
+                                               mount_path = usb_device_get_mountpath(device);
+                                               if (mount_path != NULL) {
+                                                       MS_DBG_ERR("mount_path [%s]", mount_path);
+                                                       storage_path = strdup(mount_path);
+                                                       g_array_append_val (*added_list, storage_path);
+                                               }
+                                       } else {
+                                               MS_DBG_ERR(" FAILED TO usb_device_list_get_next \n");
+                                       }
+                               }
+                       }
+               }
+
+               usb_device_free_device_list(list);
+       } else {
+               MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
+               ret = MS_MEDIA_ERR_INTERNAL;
+       }
+
+       return ret;
+}
+#endif
+
+int ms_check_mounted_storage(uid_t uid)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       int count  = 0;
+       int i = 0;
+       GArray *added_list = NULL;
+       char *mounted_path = NULL;
+       //char *id = NULL;
+       char *storage_id = NULL;
+       ms_dir_scan_type_t scan_type = MS_SCAN_ALL;
+       void **handle = NULL;
+       int err;
+
+       /*load functions from plusin(s)*/
+       err = ms_load_functions();
+       if (err != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("function load failed [%d]", err);
+               return ret;
+       }
+
+       ms_connect_db(&handle, uid);
+
+#ifdef _USE_DEVICED_DBUS
+       ret = ms_sys_get_device_list(MS_STG_TYPE_USB, &added_list);
+#else
+       ret = __ms_check_mounted_storage_list(&added_list);
+#endif
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("__ms_check_mounted_storage_list failed");
+       }
+
+       if (added_list != NULL) {
+               count = added_list->len;
+
+               for (i = 0; i < count; i ++) {
+                       mounted_path = g_array_index(added_list, char *, i);
+                       /* read deive information */
+                       ret = ms_read_device_info(mounted_path, &storage_id);
+
+                       if (storage_id == NULL) {
+                               /* in this case, all new storage */
+                               char *device_uuid = NULL;
+                               ret = ms_genarate_uuid(handle, &device_uuid);
+                               ret = ms_write_device_info(mounted_path, device_uuid);
+                               if (ret != MS_MEDIA_ERR_NONE) {
+                                       MS_DBG_ERR("ms_write_device_info failed");
+                                       MS_SAFE_FREE(device_uuid);
+                                       continue;
+                               }
+                               ret = ms_insert_storage(handle, device_uuid, NULL, mounted_path, uid);
+
+                               scan_type = MS_SCAN_ALL;
+                               storage_id = strdup(device_uuid);
+
+                               MS_SAFE_FREE(device_uuid);
+                       } else {
+                               /* update storage information into media DB */
+                               int validity = 0;
+                               char *db_storage_path = NULL;
+
+                               ret = ms_check_storage(handle, storage_id, NULL, &db_storage_path, &validity);
+                               if (ret == 0) {
+                                       if (validity == 1) {
+                                               MS_DBG_ERR("This storage is already updated.");
+                                               MS_SAFE_FREE (db_storage_path);
+                                               continue;
+                                       }
+
+                                       if (db_storage_path != NULL) {
+                                               MS_DBG_ERR("%s %s", mounted_path, db_storage_path);
+                                               if (strcmp(mounted_path, db_storage_path)) {
+                                                       /* update storage path */
+                                                       MS_DBG_ERR("update storage from %s to %s", db_storage_path, mounted_path);
+                                                       ret = ms_update_storage(handle, storage_id, mounted_path, uid);
+                                               }
+
+                                               ms_set_storage_validity(handle, storage_id, 1, uid);
+                                               /* update storage_id table set validity=0 first */
+                                               ms_validaty_change_all_items(handle, storage_id, MS_STORAGE_EXTERNAL, false, uid);
+                                               ms_set_folder_validity(handle, storage_id, mounted_path, MS_INVALID, TRUE, uid);
+                                               /* update folder table set scan_status=0 first */
+                                               ms_set_folder_scan_status(handle, storage_id, NULL, MS_DIR_SCAN_NONE, uid);
+                                               
+                                               scan_type = MS_SCAN_PART;
+                                               MS_SAFE_FREE(db_storage_path);
+                                       } else {
+                                               /* there is no information of this storage in Media DB */
+                                               MS_DBG_ERR("insert storage %s", mounted_path);
+                                               ret = ms_insert_storage(handle, storage_id, NULL, mounted_path, uid);
+                                               scan_type = MS_SCAN_ALL;
+                                       }
+                               } else {
+                                       /* there is no information of this storage in Media DB */
+                                       MS_DBG_ERR("insert storage %s", mounted_path);
+                                       ret = ms_insert_storage(handle, storage_id, NULL, mounted_path, uid);
+                                       scan_type = MS_SCAN_ALL;
+                               }
+                       }
+
+                       /* request to update media db */
+                       ret = ms_insert_folder(handle, storage_id, mounted_path, uid);
+                       if (ret != MS_MEDIA_ERR_NONE) {
+                               MS_DBG_ERR("error : ms_insert_folder failed");
+                       }
+
+                       ms_send_storage_otg_scan_request(mounted_path, storage_id, scan_type, uid);
+                       MS_SAFE_FREE(storage_id);
+               }
+
+               for (i = 0; i < count; i ++) {
+                       mounted_path = g_array_index(added_list, char*, 0);
+                       MS_SAFE_FREE(mounted_path);
+                       g_array_remove_index(added_list, 0);
+               }
+
+               g_array_free(added_list, FALSE);
+               added_list = NULL;
+       }
+
+       /*disconnect form media db*/
+       if (handle) ms_disconnect_db(&handle);
+
+       return ret;
+}
 
index 2f77034..648fc36 100644 (file)
 #include "media-server-scanner.h"
 #include "media-server-device-block.h"
 #include "media-server-dcm.h"
+#include "media-server-db-manage.h"
 
+#define MS_VCONF_KEY_RESET_STATUS "db/media_server/reset_status"
+#ifdef _USE_SENIOR_MODE
+#define DO_SCAN_ON_BOOT 1 /* 0: Don't scan on boot, 1: Do scan on boot */
+#else
 #define DO_SCAN_ON_BOOT 0 /* 0: Don't scan on boot, 1: Do scan on boot */
+#endif
 
 extern GMutex scanner_mutex;
 
 GMainLoop *mainloop = NULL;
 bool power_off; /*If this is TRUE, poweroff notification received*/
 
+#define SMARTHUB_RESET_VCONF "db/smarthub/reset/firstscreen"
+bool smarthub_reset_start;
+bool smarthub_reset;
+
+#if DO_SCAN_ON_BOOT == 1
 static void __ms_check_mediadb(void);
+#ifdef _USE_SDCARD
+static int __ms_check_mmc_status(void);
+#endif
+static int __ms_check_usb_status(void);
+#endif
 static void __ms_add_signal_handler(void);
-static void __ms_remove_event_receiver(void);
 static void __ms_add_event_receiver(GIOChannel *channel);
-static void __ms_remove_request_receiver(GIOChannel *channel);
 static void __ms_add_request_receiver(GMainLoop *mainloop, GIOChannel **channel);
+#ifdef _USE_DEVICED_DBUS
+static void __ms_remove_event_receiver(void);
+static void __ms_remove_request_receiver(GIOChannel *channel);
+#endif
+
 static bool __ms_is_dcm_supported(void);
 
 static char *priv_lang = NULL;
 
+bool __ms_deal_reset_status()
+{
+       int value = 0;
+       uid_t uid = MEDIA_DEFAULT_UID;
+
+       if(!ms_config_get_int(MS_VCONF_KEY_RESET_STATUS, &value))
+       {
+               MS_DBG_ERR("get db/media_server/reset_status failed\n");
+               return false;
+       }
+
+       MS_DBG_ERR("db/media_server/reset_status is [%d]\n", value);
+
+       if(value == 1 || value == 2)
+       {
+               MS_DBG_ERR("POWER_OFF_REASON_FACTORY_RESET or SERVICE RESET");
+               
+               ms_reset_mediadb(uid);
+
+               if(!ms_config_set_int(MS_VCONF_KEY_RESET_STATUS, 0))
+               {
+                       MS_DBG_ERR("set db/media_server/reset_status failed\n");
+                       return false;
+               }
+       }
+
+       MS_DBG_ERR("end of __ms_deal_reset_status\n");
+
+       return true;
+}
+#ifdef _USE_SENIOR_MODE
+void _ms_smarthub_vconf_cb(void *data)
+{
+       int status = 0;
+       int scanner_pid = 0;
+       int thumb_pid = 0;
+       uid_t uid = MEDIA_DEFAULT_UID;
+
+       if (power_off == FALSE) {
+               if (!ms_config_get_int(SMARTHUB_RESET_VCONF, &status)) {
+                       MS_DBG_ERR("Get VCONFKEY_SYSMAN_MMC_STATUS failed.");
+               }
+
+               MS_DBG_ERR("CURRENT STATUS OF SMARTHUB_RESET_VCONF[%d]", status);
+
+               smarthub_reset = TRUE;
+
+               if (status == 0 ) {
+                       scanner_pid = ms_get_scanner_pid();
+                       thumb_pid = ms_thumb_get_server_pid();
+
+                       /*Kill the running child process*/
+                       if (scanner_pid > 0 ) {
+                               if (kill(scanner_pid, SIGKILL) < 0) {
+                                       MS_DBG_ERR("kill failed");
+                                       MS_DBG_STRERROR();
+                               }
+                       }
+                       MS_DBG("KILL SCANNER");
+
+                       if (thumb_pid > 0 ) {
+                               if (kill(thumb_pid, SIGKILL) < 0) {
+                                       MS_DBG_ERR("kill failed");
+                                       MS_DBG_STRERROR();
+                               }
+                       }
+                       MS_DBG("KILL THUMB SERVER");
+                       MS_DBG_ERR("START SMART HUB RESET");
+                       ms_config_set_int(MS_DB_RESET, 1);
+                       ms_reset_ownerlist();
+                       ms_reset_mediadb(uid);
+                       __ms_check_mediadb();
+                       ms_config_set_int(MS_DB_RESET, 0);
+                       MS_DBG_ERR("END SMART HUB RESET");
+                       smarthub_reset = FALSE;
+               }
+       }
+
+       return;
+}
+#endif
+int ms_write_media_server_status(void)
+{
+       FILE * fp = NULL;
+       const char *path = "/tmp/media-server";
+       const char *status = "MEDIA_SERVER OK";
+
+       fp = fopen(path, "wt");
+       if (fp == NULL) {
+               MS_DBG_ERR("fp is NULL. file name : %s", path);
+               return MS_MEDIA_ERR_FILE_OPEN_FAIL;
+       }
+
+       if (fputs(status, fp) < 0) {
+               MS_DBG_ERR("fputs failed");
+               MS_DBG_STRERROR();
+       }
+
+       fclose(fp);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
+
+#ifdef _USE_DEVICED_DBUS
 void _power_off_cb(ms_power_info_s *power_info, void* data)
 {
        MS_DBG_ERR("POWER OFF");
@@ -88,6 +212,14 @@ void _power_off_cb(ms_power_info_s *power_info, void* data)
 
        return;
 }
+#else
+int _power_off_cb(int option, void *data)
+{
+       MS_DBG_ERR("POWER OFF");
+
+       return MS_MEDIA_ERR_NONE;
+}
+#endif
 
 void _ms_signal_handler(int n)
 {
@@ -167,7 +299,7 @@ void _ms_change_lang_vconf_cb(keynode_t *key, void* data)
        }
 
        if (need_update == TRUE) {
-               uid_t uid  = 0;
+               uid_t uid  = MEDIA_DEFAULT_UID;
                ms_sys_get_uid(&uid);
                ms_send_storage_scan_request(NULL, INTERNAL_STORAGE_ID, MS_SCAN_META, uid);
        } else {
@@ -211,12 +343,37 @@ int main(int argc, char **argv)
        GThread *dcm_thread = NULL;
        GIOChannel *channel = NULL;
        bool is_dcm_supported = __ms_is_dcm_supported();
+       double db_size = 0.0;
+       double free_space = 0.0;
+       uid_t uid = MEDIA_DEFAULT_UID;
 
        power_off = FALSE;
 
+       __ms_deal_reset_status();
+
        /*Init main loop*/
        mainloop = g_main_loop_new(NULL, FALSE);
 
+       ms_check_size_mediadb(uid, &db_size);
+       ms_get_remain_space(&free_space);
+       MS_DBG_ERR("==========================");
+       MS_DBG_ERR("DB SIZE : %lf", db_size);
+       MS_DBG_ERR("DB LIMIT1   : %lf", MEDIA_DB_SIZE_LIMIT_1);
+       MS_DBG_ERR("DB LIMIT2   : %lf", MEDIA_DB_SIZE_LIMIT_2);
+       MS_DBG_ERR("FREE SPACE  : %lf", free_space);
+       MS_DBG_ERR("SPACE LIMIT : %lf", MEDIA_DB_SPACE_LIMIT);
+       MS_DBG_ERR("==========================");
+
+       if (db_size > MEDIA_DB_SIZE_LIMIT_2) {
+               MS_DBG_ERR("THE SIZE OF MEDIA DB REACH THE LIMIT. RESET MEDIA DB.");
+               ms_reset_mediadb(uid);
+       }
+
+       if (ms_check_corrupt_mediadb() != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("MEDIA DB IS CORRUPTED. RESET MEDIA DB.");
+               ms_reset_mediadb(uid);
+       }
+
        __ms_new_global_variable();
 
        if (ms_cynara_initialize() != MS_MEDIA_ERR_NONE) {
@@ -256,6 +413,11 @@ int main(int argc, char **argv)
 
        MS_DBG_ERR("*** Media Server is running ***");
 
+       ms_write_media_server_status();
+
+       if (!ms_config_set_int(MS_SERVER_STATUS, 1))
+               MS_DBG_ERR("Fail to change sever status");
+
        g_main_loop_run(mainloop);
 
        g_thread_join(db_thread);
@@ -304,31 +466,12 @@ static void __ms_add_request_receiver(GMainLoop *mainloop, GIOChannel **channel)
        }
 }
 
-static void __ms_remove_request_receiver(GIOChannel *channel)
-{
-       int fd = -1;
-
-       /*close an IO channel*/
-       fd = g_io_channel_unix_get_fd(channel);
-       g_io_channel_shutdown(channel, FALSE, NULL);
-       g_io_channel_unref(channel);
-
-       if (fd > 0) {
-               if (close(fd) < 0) {
-                       MS_DBG_STRERROR("CLOSE ERROR");
-               }
-       }
-}
-
 static void __ms_add_event_receiver(GIOChannel *channel)
 {
        int err = 0;
+#ifdef _USE_META_UPDATE
        char *lang = NULL;
 
-       /*set power off callback function*/
-       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)) {
                MS_DBG_ERR("Get VCONFKEY_LANGSET failed.");
                return;
@@ -344,16 +487,44 @@ static void __ms_add_event_receiver(GIOChannel *channel)
                if (err == -1)
                        MS_DBG_ERR("add call back function for event %s fails", VCONFKEY_LANGSET);
        }
+#endif
+#ifdef _USE_SENIOR_MODE
+       /*add noti receiver for smart hub reset */
+       err = vconf_notify_key_changed(SMARTHUB_RESET_VCONF, (vconf_callback_fn) _ms_smarthub_vconf_cb, NULL);
+       if (err == -1)
+               MS_DBG_ERR("add call back function for event %s fails", SMARTHUB_RESET_VCONF);
+#endif
+       /*set power off callback function*/
+       ms_sys_set_poweroff_cb(_power_off_cb, channel);
+       ms_sys_set_device_block_event_cb(ms_device_block_changed_cb, NULL);
 
        return;
 }
 
+#ifdef _USE_DEVICED_DBUS
 static void __ms_remove_event_receiver(void)
 {
        ms_sys_unset_device_block_event_cb();
        ms_sys_unset_poweroff_cb();
 }
 
+static void __ms_remove_request_receiver(GIOChannel *channel)
+{
+       int fd = -1;
+
+       /*close an IO channel*/
+       fd = g_io_channel_unix_get_fd(channel);
+       g_io_channel_shutdown(channel, FALSE, NULL);
+       g_io_channel_unref(channel);
+
+       if (fd > 0) {
+               if (close(fd) < 0) {
+                       MS_DBG_STRERROR("CLOSE ERROR");
+               }
+       }
+}
+#endif
+
 static void __ms_add_signal_handler(void)
 {
        struct sigaction sigset;
@@ -411,7 +582,7 @@ static char* __ms_get_path(uid_t uid)
 
        return result_passwd;
 }
-
+#ifdef _USE_SDCARD
 static int __ms_check_mmc_status(void)
 {
        int ret = MS_MEDIA_ERR_NONE;
@@ -442,6 +613,7 @@ static int __ms_check_mmc_status(void)
 
        return MS_MEDIA_ERR_NONE;
 }
+#endif
 
 static int __ms_check_usb_status(void)
 {
@@ -476,13 +648,58 @@ static int __ms_check_usb_status(void)
 
 static void __ms_check_mediadb(void)
 {
-       uid_t uid = 0;
+       uid_t uid = MEDIA_DEFAULT_UID;
+#ifndef _USE_MULTI_USER
+       void **db_handle = NULL;
+       int ret = MS_MEDIA_ERR_NONE;
+
+       ret = ms_load_functions();
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_load_functions failed [%d]", ret);
+               return;
+       }
+
+       ms_sys_get_uid(&uid);
+
+       ret = ms_connect_db(&db_handle, uid);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_connect_db failed [%d]", ret);
+               ms_unload_functions();
+               return;
+       }
+       
+       ret = ms_check_db_upgrade(db_handle, uid);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_check_db_upgrade failed [%d]", ret);
+               ms_disconnect_db(&db_handle);
+               ms_unload_functions();
+               return;
+       }
+
+       if (ms_set_storage_scan_status(db_handle, NULL, MEDIA_SCAN_PREPARE, uid) != MS_MEDIA_ERR_NONE)
+               MS_DBG_ERR("ms_set_storage_scan_status fail");
+
+       /*update just valid type*/
+       if (ms_set_all_storage_validity(db_handle, 0, uid)  != MS_MEDIA_ERR_NONE)
+               MS_DBG_ERR("ms_set_all_storage_validity fail");
+
+       ms_disconnect_db(&db_handle);
+       ms_unload_functions();
+#endif
 
        ms_sys_get_uid(&uid);
        ms_send_storage_scan_request(__ms_get_path(uid), INTERNAL_STORAGE_ID, MS_SCAN_PART, uid);
 
+#ifdef _USE_SENIOR_MODE
+       if(ms_is_support_senior_mode()) {
+               ms_send_storage_scan_request_senior_mode(db_handle);
+       }
+#endif
+
        /* update external storage */
+#ifdef _USE_SDCARD
        __ms_check_mmc_status();
+#endif
        __ms_check_usb_status();
 }
 #endif
index d546ca0..8a473ae 100755 (executable)
 extern GMainLoop *mainloop;
 extern GArray *owner_list;
 GMutex scanner_mutex;
+extern int ms_req_num;
 
 static bool scanner_ready;
 static int alarm_id;
 static int child_pid;
 static int receive_id;
 
+static bool scanner_cleaned;
+
 int fifo_fd;
 
 static int _ms_check_remain_task(void)
@@ -109,15 +112,13 @@ static gboolean _ms_stop_scanner(gpointer user_data)
                MS_DBG("DB updating is not working");
        }
 
-       /* stop media scanner */
-       if (child_pid > 0) {
-               if (kill(child_pid, SIGKILL) < 0) {
-                       MS_DBG_STRERROR("kill failed");
-                       g_mutex_unlock(&scanner_mutex);
-                       return TRUE;
-               }
+       if (ms_req_num > 0) {
+               MS_DBG("[%d] request(s) remain(s)", ms_req_num);
+               g_mutex_unlock(&scanner_mutex);
+               return TRUE;
+       } else {
+               MS_DBG("DB updating is not working");
        }
-       MS_DBG("KILL SCANNER");
 
        /* close & delete FIFO */
        int res_fd = -1;
@@ -133,6 +134,24 @@ static gboolean _ms_stop_scanner(gpointer user_data)
 
        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));
+       scanner_cleaned = true;
+
+       /* stop media scanner */
+       if (child_pid > 0) {
+               if (kill(child_pid, SIGKILL) < 0) {
+                       MS_DBG_ERR("kill failed[%d]", child_pid);
+               }
+               MS_DBG_ERR("KILL SCANNER");
+       } else {
+               MS_DBG_ERR("SCANNER IS ALREADY DEAD");
+       }
+
+       child_pid = 0;
+       scanner_ready = false;
+       ms_req_num = 0;
+
+       MS_DBG_ERR("stop_scanner unlock...");
+       g_mutex_unlock(&scanner_mutex);
 
        return FALSE;
 }
@@ -158,19 +177,22 @@ static int _ms_get_ini_config(const char *key)
 
 void ms_cleanup_scanner(void)
 {
-       g_mutex_lock(&scanner_mutex);
        MS_DBG_ERR("_ms_cleanup_scanner START");
+       if (!scanner_cleaned) {
+               if (g_mutex_trylock(&scanner_mutex)) {
+                       close(fifo_fd);
 
-       close(fifo_fd);
-       MS_DBG_ERR("close fd[%d]", fifo_fd);
+                       unlink(MS_SCANNER_FIFO_PATH_RES);
+                       unlink(MS_SCANNER_FIFO_PATH_REQ);
 
-       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_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));
+                       scanner_cleaned = true;
 
-       g_mutex_unlock(&scanner_mutex);
+                       g_mutex_unlock(&scanner_mutex);
+               }
+       }
 
        MS_DBG_ERR("_ms_cleanup_scanner END");
 
@@ -246,6 +268,7 @@ int ms_scanner_start(void)
                        MS_DBG_ERR("[No-Error] SCANNER is ready");
 
                        scanner_ready = true;
+                       scanner_cleaned = false;
                        child_pid = pid;
                        /* attach result receiving socket to mainloop */
                        res_context = g_main_loop_get_context(mainloop);
@@ -297,17 +320,15 @@ bool ms_get_scanner_status(void)
 
 void ms_reset_scanner_status(void)
 {
-       gboolean ret = FALSE;
+       if (g_mutex_trylock(&scanner_mutex)) {
+               MS_DBG_ERR("ms_reset_scanner_status lock...");
 
-       ret = g_mutex_trylock(&scanner_mutex);
-       if (ret == FALSE) {
-               MS_DBG_ERR("scanner_mutex is already locked");
-       }
+               child_pid = 0;
+               scanner_ready = false;
+               ms_req_num = 0;
 
-       child_pid = 0;
-       scanner_ready = false;
-
-       g_mutex_unlock(&scanner_mutex);
+               g_mutex_unlock(&scanner_mutex);
+       }
 }
 
 int ms_get_scanner_pid(void)
index d9610de..019f129 100755 (executable)
@@ -38,6 +38,8 @@
 #include "media-common-db-svc.h"
 #include "media-common-system.h"
 #include "media-server-dbg.h"
+#include "media-server-db-manage.h"
+#include "media-server-device-block.h"
 #include "media-server-scanner.h"
 #include "media-server-socket.h"
 
@@ -46,6 +48,7 @@ GAsyncQueue* ret_queue;
 GArray *owner_list;
 GMutex scanner_mutex;
 gint cur_running_task;
+int ms_req_num;
 extern bool power_off;
 
 typedef struct ms_req_owner_data {
@@ -110,18 +113,24 @@ static int __ms_find_owner(int pid, const char *req_path, ms_req_owner_data **ow
 
        *owner_data = NULL;
 
-       MS_DBG("length list : %d", len);
-
-       for (i = 0; i < len; i++) {
-               data = g_array_index(owner_list, ms_req_owner_data*, i);
-               MS_DBG("%d %d", 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");
-                       break;
+       if (owner_list != NULL) {
+               MS_DBG("length list :  %d", len);
+
+               len = owner_list->len;
+
+               for (i = 0; i < len; i++) {
+                       data = g_array_index(owner_list, ms_req_owner_data*, i);
+                       MS_DBG("%d %d", 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");
+                               break;
+                       }
                }
+       } else {
+               MS_DBG_ERR("owner list is NULL");
        }
 
        return MS_MEDIA_ERR_NONE;
@@ -139,6 +148,39 @@ static int __ms_delete_owner(ms_req_owner_data *owner_data)
        return MS_MEDIA_ERR_NONE;
 }
 
+int ms_reset_ownerlist()
+{
+       int i;
+       int len = 0;
+       ms_req_owner_data *data = NULL;
+       ms_comm_msg_s send_msg = {0,};
+
+       if(owner_list != NULL) {
+               len = owner_list->len;
+               MS_DBG("length list :  %d", len);
+
+               for (i=0; i < len; i++) {
+                       data = g_array_index(owner_list, ms_req_owner_data*, 0);
+                       g_array_remove_index (owner_list, 0);
+                       MS_DBG("%d", data->pid);
+                       MS_DBG("%s", data->req_path);
+                       send_msg.pid = data->pid;
+                       send_msg.result = MS_MEDIA_ERR_DB_RESET;
+                       send_msg.msg_type = MS_MSG_MEDIA_DB_RESET;
+                       send_msg.msg_size = strlen(data->req_path);
+                       strncpy(send_msg.msg, data->req_path, send_msg.msg_size);
+                       
+                       /* owner data exists */
+                       /* send result to the owner of request */
+                       ms_ipc_send_msg_to_client_tcp(data->client_sockfd, &send_msg, NULL);
+
+                       close(data->client_sockfd);
+               }
+       }
+
+       return MS_MEDIA_ERR_NONE;
+}
+
 static int __ms_send_result_to_client(int pid, ms_comm_msg_s *recv_msg)
 {
        if (strlen(recv_msg->msg) == 0) {
@@ -163,12 +205,14 @@ static int __ms_send_result_to_client(int pid, ms_comm_msg_s *recv_msg)
                        /* owner data exists */
                        /* send result to the owner of request */
                        ms_ipc_send_msg_to_client_tcp(owner_data->client_sockfd, recv_msg, NULL);
-                       close(owner_data->client_sockfd);
+                       MS_DBG("add handle MS_MSG_SCANNER_PARTIAL message");
+                       if ((recv_msg->msg_type != MS_MSG_SCANNER_COMPLETE) && (recv_msg->msg_type != MS_MSG_SCANNER_PARTIAL))
+                               close(owner_data->client_sockfd);
 
                        MS_SAFE_FREE(res_path);
 
-                       /* free owner data*/
-                       __ms_delete_owner(owner_data);
+                       if ((recv_msg->msg_type != MS_MSG_SCANNER_COMPLETE) && (recv_msg->msg_type != MS_MSG_SCANNER_PARTIAL))
+                               __ms_delete_owner(owner_data);
                } else {
                        MS_DBG_ERR("Not found Owner");
                        MS_SAFE_FREE(res_path);
@@ -185,6 +229,46 @@ static int __ms_send_result_to_client(int pid, ms_comm_msg_s *recv_msg)
 
 }
 
+static int __ms_recovery_media_db(uid_t uid)
+{
+       int err = MS_MEDIA_ERR_NONE;
+       void **handle = NULL;
+
+       /* stop the child process */
+       
+       /*reset */
+       ms_config_set_int(MS_DB_RESET, 1);
+       ms_reset_ownerlist();
+       ms_reset_mediadb(uid);
+
+       /*write read schema*/
+       /*load functions from plusin(s)*/
+       err = ms_load_functions();
+       if (err != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("function load failed [%d]", err);
+               return err;
+       }
+       
+       ms_connect_db(&handle, uid);
+       
+       MS_DBG_ERR("START WRITE SCHEMA");
+       if (ms_check_db_upgrade(handle, uid)  != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("ms_check_db_upgrade fail");
+       }
+       MS_DBG_ERR("END WRITE SCHEMA");
+       
+       /*disconnect form media db*/
+       if (handle) ms_disconnect_db(&handle);
+       
+       /*unload functions*/
+       ms_unload_functions();
+
+       ms_check_mounted_storage(uid);
+       ms_config_set_int(MS_DB_RESET, 0);
+
+       return MS_MEDIA_ERR_NONE;
+}
+
 gboolean ms_read_socket(gpointer user_data)
 {
        ms_comm_msg_s recv_msg;
@@ -249,6 +333,11 @@ gboolean ms_read_socket(gpointer user_data)
 
                if (req_num == MS_MSG_DIRECTORY_SCANNING_CANCEL)
                        ms_remove_request_owner(pid, recv_msg.msg);
+       } else if (req_num == MS_MSG_MEDIA_DB_MALFORMED) {
+               /*media DB is corrupted, recovery media DB*/
+               ret = __ms_recovery_media_db(recv_msg.uid);
+               res = ret;
+               goto ERROR;
        } else {
                /* NEED IMPLEMENTATION */
                close(client_sock);
@@ -273,7 +362,13 @@ ERROR:
                        case MS_MSG_BURSTSHOT_INSERT:
                                res_msg.msg_type = MS_MSG_SCANNER_BULK_RESULT;
                                break;
-                       default:
+                       case MS_MSG_RECEIVE_MSG_FAILED:
+                               res_msg.msg_type = MS_MSG_RECEIVE_MSG_FAILED;
+                               break;
+                       case MS_MSG_MEDIA_DB_MALFORMED:
+                               res_msg.msg_type = MS_MSG_MEDIA_DB_MALFORMED;
+                               break;
+                       default :
                                break;
                }
 
@@ -308,6 +403,8 @@ static int __ms_send_request(ms_comm_msg_s *send_msg)
 
        close(fd);
 
+       ms_req_num++;
+
        return res;
 }
 
@@ -409,6 +506,70 @@ ERROR:
        return ret;
 }
 
+#ifdef _USE_SENIOR_MODE
+int ms_send_storage_scan_request_senior_mode(void **handle)
+{
+       int ret = MS_MEDIA_ERR_NONE;
+       uid_t uid = MEDIA_DEFAULT_UID;
+
+       /* update storage information into media DB */
+       int validity = 0;
+       ms_dir_scan_type_t scan_type = MS_SCAN_ALL;
+       char *storage_path = NULL;
+       char* storage_id =  strdup("familytv");
+       char* path = MEDIA_ROOT_PATH_SENIOR_MODE;
+
+       ms_sys_get_uid(&uid);
+
+       ret = ms_check_storage(handle, storage_id, NULL, &storage_path, &validity);
+       if (ret == 0) {
+               if (storage_path != NULL) {
+                       MS_DBG_ERR("##[senior mode]####[path [%s], %s]###", path, storage_path);
+                       if (strcmp(path, storage_path)) {
+                               /* update storage path */
+                               MS_DBG_ERR("##[senior mode]update storage from %s to %s", storage_path, path);
+                               ret = ms_update_storage(handle, storage_id, path, uid);
+                       }
+
+                       if (validity== 0) {
+                               ms_set_storage_validity(handle, storage_id, 1, uid);
+                               /* update storage_id table set validity=0 first */
+                               ms_validaty_change_all_items(handle, storage_id, MS_STORAGE_EXTERNAL, false, uid);
+                               ms_set_folder_validity(handle, storage_id, storage_path, MS_INVALID, TRUE, uid);
+                               /* update folder table set scan_status=0 first */
+                               ms_set_folder_scan_status(handle, storage_id, NULL, MS_DIR_SCAN_NONE, uid);
+                       }
+
+                       scan_type = MS_SCAN_PART;
+                       MS_SAFE_FREE(storage_path);
+               } else {
+                       /* there is no information of this storage in Media DB */
+                       MS_DBG_ERR("##[senior mode]insert storage %s", path);
+                       ret = ms_insert_storage(handle, storage_id, NULL, path, uid);
+                       MS_DBG_ERR("##[senior mode]");
+                       scan_type = MS_SCAN_ALL;
+               }
+       } else {
+               /* there is no information of this storage in Media DB */
+               MS_DBG_ERR("##[senior mode]insert storage %s", path);
+               ret = ms_insert_storage(handle, storage_id, NULL, path, uid);
+               MS_DBG_ERR("##[senior mode]");
+               scan_type = MS_SCAN_ALL;
+       }
+
+       /* request to update media db */
+       ret = ms_insert_folder(handle, storage_id, path, uid);
+       if (ret != MS_MEDIA_ERR_NONE) {
+               MS_DBG_ERR("##[senior mode]error : ms_insert_folder failed");
+       }
+
+       ms_send_storage_otg_scan_request(path, storage_id, scan_type, uid);
+       MS_SAFE_FREE(storage_id);
+
+       return ret;
+}
+#endif
+
 gboolean ms_read_db_tcp_socket(GIOChannel *src, GIOCondition condition, gpointer data)
 {
        int sock = -1;
@@ -713,6 +874,15 @@ gboolean ms_receive_message_from_scanner(GIOChannel *src, GIOCondition condition
                (msg_type == MS_MSG_DIRECTORY_SCANNING_CANCEL)) {
                MS_DBG_WARN("DB UPDATING IS DONE[%d]", msg_type);
 
+               if((msg_type == MS_MSG_SCANNER_RESULT) ||
+               (msg_type == MS_MSG_SCANNER_BULK_RESULT) ||
+               (msg_type == MS_MSG_EXTRACTOR_COMPLETE) ||
+               (msg_type == MS_MSG_DIRECTORY_SCANNING_CANCEL))
+               {
+                       ms_req_num--;
+                       MS_DBG_WARN("ms_req_num-- [%d]", ms_req_num);
+               }
+
                if (pid != 0) {
                        __ms_send_result_to_client(pid, &recv_msg);
                } else {