Remove ms_config_get_str()
[platform/core/multimedia/media-server.git] / src / common / media-common-utils.c
old mode 100755 (executable)
new mode 100644 (file)
index e7ef1d5..a861da4
  *
  */
 
-#define _GNU_SOURCE
 #include <sys/types.h>
 #include <fcntl.h>
-#include <errno.h>
 #include <vconf.h>
-#include <aul/aul.h>
-#include <grp.h>
-#include <pwd.h>
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus.h>
-#include <dbus/dbus-glib-lowlevel.h>
 #include <sys/statvfs.h>
 #include <sys/stat.h>
-#ifdef _SET_VIP_PROCESS
-#include <sys/prctl.h>
-#endif
-#include <system_info.h>
-#include <dd-display.h>
 
-#ifndef _USE_DEVICED_DBUS
+#ifdef _USE_TVPD_MODE
+#include <sys/prctl.h>
 #include <usb-device.h>
 #endif
+#include <system_info.h>
+#include <device/power.h>
 
 #include "media-util.h"
 #include "media-server-ipc.h"
 #include "media-common-system.h"
 #include "media-common-utils.h"
 
-#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];
-};
-
-bool ms_is_mmc_inserted(void)
-{
-       bool ret = FALSE;
-       ms_stg_type_e stg_type = MS_STG_TYPE_MMC;
-       GArray *dev_list = NULL;
-
-       ret = ms_sys_get_device_list(stg_type, &dev_list);
-       if (ret == MS_MEDIA_ERR_NONE) {
-               if (dev_list != NULL) {
-                       MS_DBG_WARN("MMC FOUND[%d]", dev_list->len);
-                       ms_sys_release_device_list(&dev_list);
-                       ret = TRUE;
-               } else {
-                       MS_DBG_ERR("MMC NOT FOUND");
-               }
-       } else {
-               MS_DBG_ERR("ms_sys_get_device_list failed");
-       }
-
-       return ret;
-}
-
-static char* __media_get_path(uid_t uid)
-{
-       int ret = -1;
-       char *grpbuf = NULL;
-       char *result_passwd = NULL;
-       struct group grpinfo;
-       struct group *grpresult = NULL;
-       size_t grpbufsize;
-
-       grpbufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
-       if (grpbufsize == -1)           /* Value was indeterminate */
-               grpbufsize = 16384;             /* Should be more than enough (16*1024) */
-
-       grpbuf = malloc(grpbufsize);
-       if (grpbuf == NULL) {
-               MS_DBG_ERR("malloc grpbuf grpbufsize[%d] failed", grpbufsize);
-               goto END;
-       }
-
-       ret = getgrnam_r("users", &grpinfo, grpbuf, grpbufsize, &grpresult);
-       if((ret == 0) && (grpresult != NULL)) {
-//             MS_DBG("getgrnam_r users success...\n");
-       } else {
-               MS_DBG_ERR("getgrnam_r users failed ret[%d]", ret);
-               goto END;
-       }
-
-       if (uid == getuid()) {
-               if (MS_STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
-                       result_passwd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
-       } else {
-               struct passwd pwdinfo;
-               struct passwd *pwdresult = NULL;
-               char *pwdbuf;
-               size_t pwdbufsize;
-
-               pwdbufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
-               if (pwdbufsize == -1)           /* Value was indeterminate */
-                       pwdbufsize = 16384;             /* Should be more than enough (16*1024) */
-
-               pwdbuf = malloc(pwdbufsize);
-               if (pwdbuf == NULL) {
-                       MS_DBG_ERR("malloc pwdbuf pwdbufsize[%d] failed", pwdbufsize);
-                       goto END;
-               }
-
-               ret = getpwuid_r(uid, &pwdinfo, pwdbuf, pwdbufsize, &pwdresult);
-               if((ret == 0) && (pwdresult != NULL)) {
-//                     MS_DBG("getpwuid uid[%d] success\n", uid);
-               } else {
-                       MS_DBG_ERR("getpwuid uid[%d] failed ret[%d]", uid, ret);
-                       MS_SAFE_FREE(pwdbuf);
-                       goto END;
-               }
-
-               // Compare git_t type and not group name
-               if (grpinfo.gr_gid != pwdinfo.pw_gid) {
-                       MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
-                       MS_SAFE_FREE(pwdbuf);
-                       goto END;
-               }
-               result_passwd = strndup(pwdinfo.pw_dir, strlen(pwdinfo.pw_dir));
-
-               MS_SAFE_FREE(pwdbuf);
-       }
-
-END:
-       MS_SAFE_FREE(grpbuf);
-
-       return result_passwd;
-}
-
-
-ms_storage_type_t ms_get_storage_type_by_full(const char *path, uid_t uid)
-{
-       int length_path;
-       int ret = MS_MEDIA_ERR_NONE;
-       char * user_path = NULL;
-
-       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;
-
-       length_path = strlen(user_path);
-
-       if (strncmp(path, user_path, length_path) == 0) {
-               ret = MS_STORAGE_INTERNAL;
-       } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
-               ret = MS_STORAGE_EXTERNAL;
-       } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
-               ret = MS_STORAGE_EXTERNAL_USB;
-       } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0)) {
-               ret = MS_STORAGE_EXTERNAL_USB;
-       } else {
-               MS_DBG_ERR("[%s][%s][%s]", MEDIA_ROOT_PATH_SDCARD, MEDIA_ROOT_PATH_USB, path);
-               ret = MS_MEDIA_ERR_INVALID_PATH;
-       }
-
-       MS_SAFE_FREE(user_path);
-
-       return ret;
-}
-
-int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
-{
-       int len = 0;
-       int real_size = size - 1;
-
-       if (!res || !pattern || !str1 || !str2)
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-
-       if (real_size < (int)(strlen(str1) + strlen(str2)))
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-
-       len = snprintf(res, real_size, pattern, str1, str2);
-       if (len < 0) {
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
-
-       res[len] = '\0';
-
-       return MS_MEDIA_ERR_NONE;
-}
-
-int ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
-{
-       int len = 0;
-       int real_size = size;
-
-       if (!res || !pattern || !str1) {
-               MS_DBG_ERR("parameta is invalid");
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
-
-       if (real_size < (int)(strlen(str1))) {
-               MS_DBG_ERR("size is wrong");
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
-
-       len = snprintf(res, real_size, pattern, str1);
-       if (len < 0) {
-               MS_DBG_ERR("snprintf failed");
-               return MS_MEDIA_ERR_INVALID_PARAMETER;
-       }
-
-       res[len] = '\0';
-
-       return MS_MEDIA_ERR_NONE;
-}
-
 bool ms_config_get_int(const char *key, int *value)
 {
        int err;
@@ -262,12 +48,10 @@ bool ms_config_get_int(const char *key, int *value)
        }
 
        err = vconf_get_int(key, value);
-       if (err == 0)
+       if (err == VCONF_OK)
                return true;
-       else if (err == -1)
-               return false;
-       else
-               MS_DBG_ERR("Unexpected error code: %d", err);
+
+       MS_DBG_ERR("Error code: %d", err);
 
        return false;
 }
@@ -282,182 +66,78 @@ bool ms_config_set_int(const char *key, int value)
        }
 
        err = vconf_set_int(key, value);
-       if (err == 0)
+       if (err == VCONF_OK)
                return true;
-       else if (err == -1)
-               return false;
-       else
-               MS_DBG_ERR("Unexpected error code: %d", err);
 
-       return false;
-}
-
-bool ms_config_get_str(const char *key, char **value)
-{
-       char *res = NULL;
-
-       if (key == NULL || value == NULL) {
-               MS_DBG_ERR("Arguments key or value is NULL");
-               return false;
-       }
-
-       res = vconf_get_str(key);
-       if (MS_STRING_VALID(res)) {
-               *value = strdup(res);
-               MS_SAFE_FREE(res);
-               return true;
-       }
-
-       return false;
-}
-
-bool ms_config_set_str(const char *key, const char *value)
-{
-       int err;
-
-       if (!key || !value) {
-               MS_DBG_ERR("Arguments key or value is NULL");
-               return false;
-       }
-
-       err = vconf_set_str(key, value);
-       if (err == 0)
-               return true;
-       else
-               MS_DBG_ERR("fail to vconf_set_str %d", err);
+       MS_DBG_ERR("Error code: %d", err);
 
        return false;
 }
 
-bool ms_config_get_bool(const char *key, int *value)
-{
-       int err;
-
-       if (!key || !value) {
-               MS_DBG_ERR("Arguments key or value is NULL");
-               return false;
-       }
-
-       err = vconf_get_bool(key, value);
-       if (err == 0)
-               return true;
-       else if (err == -1)
-               return false;
-       else
-               MS_DBG_ERR("Unexpected error code: %d", err);
-
-       return false;
-}
-
-static int get_memory_size(const char *path, struct statvfs_32 *buf)
+int ms_get_remain_space(uint64_t *free_space)
 {
+       int ret = MS_MEDIA_ERR_NONE;
+       const char *path = "/opt";
        struct statvfs s;
-       int ret;
 
        ret = statvfs(path, &s);
-       if (ret) {
+       if (ret != 0) {
                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;
+       /* f_bsize:unsigned long, f_bavail:fsblkcnt_t(unsigned long) */
+       *free_space = (uint64_t)s.f_bsize * (uint64_t)s.f_bavail;
 
        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
+#ifdef _USE_TVPD_MODE
 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) {
+       bool bSupportPVR = false;
+       if (system_info_get_custom_bool("com.samsung/featureconf/pvr.pvr_support", &bSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
                MS_DBG_ERR("Get PVR Support failed");
                return false;
        }
 
-       MS_DBG("PVR Support : [%d]", nSupportPVR);
+       MS_DBG("PVR Support : [%d]", bSupportPVR);
 
-       return (nSupportPVR != 0);
+       return bSupportPVR;
 }
 #endif
 
-#ifdef _USE_SENIOR_MODE
-bool ms_is_support_senior_mode()
+bool ms_is_valid_symlink(const char *path)
 {
-       bool bSupportSeniorMode = false;
+#ifdef _USE_TVPD_MODE
+       return false;
+#else
+       g_autofree char *real_path = realpath(path, NULL);
 
-       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");
+       if (!real_path)
                return false;
-       }
-
-       MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
 
-       return bSupportSeniorMode;
-}
+       return (g_strcmp0(real_path, MEDIA_SHARE_PATH) == 0);
 #endif
+}
 
 int ms_check_file_path(const char *file_path, uid_t uid)
 {
-       int exist;
-       struct stat file_st;
-
-       /* check location of file */
-       /* file must exists under "/opt/usr/media" or "/opt/storage/sdcard" */
-       if (!ms_is_valid_path(file_path, uid)) {
-               MS_DBG_ERR("Invalid path : %s", file_path);
-               return MS_MEDIA_ERR_INVALID_PATH;
-       }
+       ms_user_storage_type_e storage_type = -1;
+       int ret = MS_MEDIA_ERR_NONE;
 
-       /* check the file exits actually */
-       exist = open(file_path, O_RDONLY);
-       if (exist < 0) {
-               MS_DBG_ERR("[%s]open files");
-               return MS_MEDIA_ERR_INVALID_PATH;
+       if (!MS_STRING_VALID(file_path)) {
+               MS_DBG_ERR("Invalid path");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
-       close(exist);
-
-       /* check type of the path */
-       /* It must be a regular file */
-       memset(&file_st, 0, sizeof(struct stat));
-       if (stat(file_path, &file_st) == 0) {
-               if (!S_ISREG(file_st.st_mode)) {
-                       /* In this case, it is not a regula file */
-                       MS_DBG_ERR("this path is not a file");
-                       return MS_MEDIA_ERR_INVALID_PATH;
-               }
-       } else {
-               MS_DBG_STRERROR("stat failed");
-               return MS_MEDIA_ERR_INVALID_PATH;
+
+       ret = ms_user_get_storage_type(uid, file_path, &storage_type);
+       MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
+
+       if (!g_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
+               MS_DBG_SERR("g_file_test fail [%s]", file_path);
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
 
        return MS_MEDIA_ERR_NONE;
@@ -467,365 +147,189 @@ int ms_check_ignore_dir(const char *full_path, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
        char *dir_path = NULL;
-       char *leaf_path = NULL;
-       char *usr_path = NULL;
+       char *next = NULL;
+       int next_pos = 0;
 
        ret = ms_check_file_path(full_path, uid);
-       if (ret != MS_MEDIA_ERR_NONE) {
-               MS_DBG_ERR("invalid path : %s", full_path);
-               return MS_MEDIA_ERR_INVALID_PATH;
-       }
+       MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
 
-       dir_path = g_path_get_dirname(full_path);
-       if (dir_path == NULL || strcmp(dir_path, ".") == 0) {
-               MS_DBG_ERR("getting directory path is failed : %s", full_path);
-               MS_SAFE_FREE(dir_path);
-               return MS_MEDIA_ERR_INVALID_PATH;
-       }
+       if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && !strncmp(full_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB))) {
+               next_pos = strlen(MEDIA_ROOT_PATH_USB) + 1;
+       } else {
+               ret = ms_user_get_internal_root_path(uid, &dir_path);
+               MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_internal_root_path() fail");
 
-       usr_path = ms_get_path(uid);
-       if (usr_path == NULL) {
-               MS_DBG_ERR("ms_get_path() fail");
-               MS_SAFE_FREE(dir_path);
-               return MS_MEDIA_ERR_INTERNAL;
+               next_pos = strlen(dir_path);
+               g_free(dir_path);
+               dir_path = NULL;
        }
 
-       while (1) {
-               if (ms_check_scan_ignore(dir_path) != MS_MEDIA_ERR_NONE) {
-                       ret = MS_MEDIA_ERR_INVALID_PATH;
-                       break;
-               }
+       while ((next = strstr(full_path + next_pos, "/"))) {
+               next_pos = (next - full_path);
+               dir_path = g_strndup(full_path, next_pos);
+               next_pos++;
 
-#ifdef _USE_SENIOR_MODE
-               if(ms_is_support_senior_mode()) {
-                       if(strcmp(dir_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)
-                               break;
-               }
-#endif
-               if (strcmp(dir_path, usr_path) == 0)
+               ret = ms_check_scan_ignore(dir_path, uid);
+               g_free(dir_path);
+               if (ret != MS_MEDIA_ERR_NONE)
                        break;
-               else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
-                       break;
-               else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(dir_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
-                       break;
-               else if (MS_STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(dir_path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0))
-                       break;
-
-               leaf_path = strrchr(dir_path, '/');
-               if (leaf_path != NULL) {
-                               int seek_len = leaf_path -dir_path;
-                               dir_path[seek_len] = '\0';
-               } else {
-                       MS_DBG_ERR("Fail to find leaf path");
-                       ret = MS_MEDIA_ERR_INVALID_PATH;
-                       break;
-               }
        }
 
-       MS_SAFE_FREE(dir_path);
-       MS_SAFE_FREE(usr_path);
-
        return ret;
 }
 
-int ms_check_scan_ignore(char * path)
+int ms_check_scan_ignore(char * path, uid_t uid)
 {
-       int fd = -1;
-       int exist = -1;
-       const char *ignore_path = "/.scan_ignore";
-       char *check_ignore_file = NULL;
        int ret = MS_MEDIA_ERR_NONE;
+       const char *ignore_file = ".scan_ignore";
+       char *tmp_path = NULL;
+       char *org_path = NULL;
+       char ignore_path[MS_FILE_PATH_LEN_MAX] = {0, };
+
+#ifndef _USE_TVPD_MODE
+       char replace[MS_FILE_PATH_LEN_MAX] = {0, };
+       char *mediashared = NULL;
+#endif
 
-       if (strstr(path, "/.")) {
-               MS_DBG_ERR("hidden path");
-               ret = MS_MEDIA_ERR_INVALID_PATH;
-               goto ERROR;
+       /* Check for symbolic link */
+       tmp_path = realpath(path, NULL);
+       /* Get trimmed path */
+       org_path = g_canonicalize_filename(path, NULL);
+
+#ifdef _USE_TVPD_MODE
+       if (g_strcmp0(tmp_path, org_path) != 0) {
+               MS_SAFE_FREE(tmp_path);
+               g_free(org_path);
+               MS_DBG_ERR("symbolic link(directory)");
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
+       }
+#else
+       if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
+               ms_user_get_mediashared_path(uid, &mediashared);
+               snprintf(replace, MS_FILE_PATH_LEN_MAX, "%s%s", mediashared, tmp_path + strlen(MEDIA_SHARE_PATH));
+               MS_SAFE_FREE(mediashared);
+               if (g_strcmp0(replace, org_path) != 0) {
+                       MS_SAFE_FREE(tmp_path);
+                       g_free(org_path);
+                       MS_DBG_ERR("symbolic link(directory)");
+                       return MS_MEDIA_ERR_INVALID_PARAMETER;
+               }
+       } else {
+               if (g_strcmp0(tmp_path, org_path) != 0) {
+                       MS_SAFE_FREE(tmp_path);
+                       g_free(org_path);
+                       MS_DBG_ERR("symbolic link(directory)");
+                       return MS_MEDIA_ERR_INVALID_PARAMETER;
+               }
        }
+#endif
+       MS_SAFE_FREE(tmp_path);
+       g_free(org_path);
 
-       fd = open(path, O_RDONLY | O_DIRECTORY);
-       if (fd == -1) {
-               MS_DBG_ERR("%s folder opendir fails", path);
-               ret = MS_MEDIA_ERR_INVALID_PATH;
+       if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
+               snprintf(ignore_path, sizeof(ignore_path), "%s/%s", path, ignore_file);
+
+               if (g_file_test(ignore_path, G_FILE_TEST_EXISTS)) {
+                       MS_DBG_WARN("scan ignore file exist [%s]", ignore_path);
+                       return MS_MEDIA_ERR_INVALID_PARAMETER;
+               }
+       } else {
+               MS_DBG_ERR("g_file_test fails[%s]", path);
+               ret = MS_MEDIA_ERR_INVALID_PARAMETER;
 
                if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
                        MS_DBG_ERR("Fail to get USB path");
-                       goto ERROR;
+                       return ret;
                }
 
                if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
-                       if (errno == ENOENT) {
-                               /*if the directory does not exist, check the device is unmounted*/
-                               if (!ms_storage_mount_status(path)) {
-                                       MS_DBG_ERR("Device is unmounted[%s]", path);
-                                       ret = MS_MEDIA_ERR_USB_UNMOUNTED;
-                                       goto ERROR;
-                               }
+                       /*if the directory does not exist, check the device is unmounted*/
+                       if (!ms_storage_mount_status(path)) {
+                               MS_DBG_ERR("Device is unmounted[%s]", path);
+                               return MS_MEDIA_ERR_USB_UNMOUNTED;
                        }
                }
-
-               struct stat folder_st;
-               if (stat(path, &folder_st) == 0) {
-                       MS_DBG_WARN("DEV[%ld] INODE[%lld] UID[%ld] GID[%ld] MODE[%lo] PATH[%s]", (long)folder_st.st_dev, (long long)folder_st.st_ino,
-                               (long)folder_st.st_uid, (long)folder_st.st_gid, (unsigned long) folder_st.st_mode, path);
-               } else {
-                       MS_DBG_ERR("%s folder stat fails", path);
-               }
-
-               goto ERROR;
-       } else {
-               /* check the file exits actually */
-               int path_len = 0;
-
-               path_len = strlen(path) + strlen(ignore_path) + 1;
-               check_ignore_file = malloc(path_len);
-               if (check_ignore_file != NULL) {
-                       memset(check_ignore_file, 0x0, path_len);
-                       snprintf(check_ignore_file, path_len, "%s%s", path, ignore_path);
-
-                       exist = open(check_ignore_file, O_RDONLY);
-                       if (exist >= 0) {
-                               MS_DBG_WARN("scan_ignore exists [%s]", check_ignore_file);
-                               ret = MS_MEDIA_ERR_INVALID_PATH;
-                       }
-
-                       MS_SAFE_FREE(check_ignore_file);
-               } else {
-                       MS_DBG_ERR("malloc failed");
-                       ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
-               }
-       }
-
-ERROR:
-
-       if (fd != -1) {
-               close(fd);
-               fd = -1;
        }
 
-       if (exist >= 0) close(exist);
-
        return ret;
 }
 
-char* ms_get_path(uid_t uid)
-{
-       int len = 0;
-       char *result_passwd = NULL;
-       int ret = -1;
-       char* grpbuf;
-       struct group grpinfo;
-       struct group* grpresult = NULL;
-       size_t grpbufsize;
-
-       grpbufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
-       if (grpbufsize == -1)           /* Value was indeterminate */
-               grpbufsize = 16384;             /* Should be more than enough (16*1024) */
-
-       grpbuf = malloc(grpbufsize);
-       if (grpbuf == NULL) {
-               MS_DBG_ERR("malloc grpbuf grpbufsize[%d] failed", grpbufsize);
-               return NULL;
-       }
-
-       ret = getgrnam_r("users", &grpinfo, grpbuf, grpbufsize, &grpresult);
-       if((ret == 0) && (grpresult != NULL)) {
-//             MS_DBG("getgrnam_r users success...\n");
-       } else {
-               MS_DBG_ERR("getgrnam_r users failed ret[%d]", ret);
-               goto END;
-       }
-
-       if (uid == getuid()) {
-               if (MS_STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
-                       result_passwd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
-       } else {
-               char passwd_str[MAX_FILEPATH_LEN] = {0, };
-               struct passwd pwdinfo;
-               struct passwd* pwdresult = NULL;
-               char* pwdbuf;
-               size_t pwdbufsize;
-
-               pwdbufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
-               if (pwdbufsize == -1)           /* Value was indeterminate */
-                       pwdbufsize = 16384;             /* Should be more than enough (16*1024) */
-
-               pwdbuf = malloc(pwdbufsize);
-               if (pwdbuf == NULL) {
-                       MS_DBG_ERR("malloc pwdbuf pwdbufsize[%d] failed", pwdbufsize);
-                       goto END;
-               }
-
-               ret = getpwuid_r(uid, &pwdinfo, pwdbuf, pwdbufsize, &pwdresult);
-               if((ret == 0) && (pwdresult != NULL)) {
-//                     MS_DBG("getpwuid uid[%d] success\n", uid);
-               } else {
-                       MS_DBG_ERR("getpwuid uid[%d] failed ret[%d]", uid, ret);
-                       MS_SAFE_FREE(pwdbuf);
-                       goto END;
-               }
-
-               // Compare git_t type and not group name
-               if (grpinfo.gr_gid != pwdinfo.pw_gid) {
-                       MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
-                       MS_SAFE_FREE(pwdbuf);
-                       goto END;
-               }
+#ifdef _USE_TVPD_MODE
+typedef struct storage_result {
+       char *storage_path;
+       bool result;
+} storage_result_s;
 
-               len = snprintf(passwd_str, sizeof(passwd_str), "%s/%s", pwdinfo.pw_dir, MEDIA_CONTENT_PATH);
-               if (len > 0)
-                       result_passwd = strndup(passwd_str, len);
-
-               MS_SAFE_FREE(pwdbuf);
-       }
+static void __ms_check_mount_status(usb_device_h usb_device, void *user_data)
+{
+       storage_result_s *data = (storage_result_s *)user_data;
+       char *mount_path = NULL;
 
-END:
-       MS_SAFE_FREE(grpbuf);
+       mount_path = usb_device_get_mountpath(usb_device);
+       if (!mount_path)
+               return;
 
-       return result_passwd;
+       MS_DBG_SWARN("mount_path [%s]", mount_path);
+       data->result = (g_strcmp0(mount_path, data->storage_path) == 0);
 }
+#endif
 
-bool ms_storage_mount_status(const charstart_path)
+bool ms_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;
+#ifdef _USE_TVPD_MODE
+       storage_result_s res = {0, };
        char *remain_path = NULL;
        int remain_len = 0;
 
-       remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
+       remain_path = strstr(start_path + strlen(MEDIA_ROOT_PATH_USB) + 1, "/");
        if (remain_path != NULL)
                remain_len = strlen(remain_path);
 
-       storage_path = strndup(start_path, strlen(start_path) - remain_len);
-
-       MS_DBG_WARN("storage_path [%s]", storage_path);
-
-       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_WARN("mount_path [%s]", mount_path);
-                                       if (strlen(mount_path) == strlen(storage_path)) {
-                                               if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
-                                                       MS_DBG_WARN("start path is mounted [%s]", start_path);
-                                                       ret = true;
-                                               }
-                                       }
-                               }
-                       }
+       res.storage_path = g_strndup(start_path, strlen(start_path) - remain_len);
 
-                       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_WARN("mount_path [%s]", mount_path);
-                                                       if (strlen(mount_path) == strlen(storage_path)) {
-                                                               if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
-                                                                       MS_DBG_WARN("start path is mounted [%s]", start_path);
-                                                                       ret = true;
-                                                                       break;
-                                                               }
-                                                       }
-                                               }
-                                       }
-                               }
-                       }
-               }
+       MS_DBG_SWARN("storage_path [%s]", res.storage_path);
 
-               usb_device_free_device_list(list);
-       } else {
-               MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
-       }
+       usb_mass_storage_foreach(__ms_check_mount_status, &res);
+       g_free(res.storage_path);
+       ret = res.result;
 
-       MS_SAFE_FREE(storage_path);
+       if (ret)
+               MS_DBG_SWARN("start path is mounted [%s]", start_path);
 #endif
        return ret;
 }
 
-bool ms_is_valid_path(const char *path, uid_t uid)
+int ms_set_db_status(ms_db_status_type_t status, ms_user_storage_type_e storage_type)
 {
-       bool ret = false;
-       char *usr_path = NULL;
-
-       if (path == NULL)
-               return false;
-
-       usr_path = ms_get_path(uid);
-       if (usr_path == NULL)
-               return 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 if (MS_STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0))
-               ret = true;
-       else
-               ret = false;
-
-       MS_SAFE_FREE(usr_path);
-
-       return ret;
-}
-
-int ms_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type)
-{
-       int res = MS_MEDIA_ERR_NONE;
-       int err = 0;
+       int ret = MS_MEDIA_ERR_NONE;
 
        if (status == MS_DB_UPDATING) {
-               if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) {
-                       res = MS_MEDIA_ERR_VCONF_SET_FAIL;
-                       MS_DBG_ERR("ms_config_set_int failed");
-               }
+               if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING))
+                               goto ERROR;
 
-               if (storage_type == MS_STORAGE_EXTERNAL) {
-                       if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING)) {
-                               res = MS_MEDIA_ERR_VCONF_SET_FAIL;
-                               MS_DBG_ERR("ms_config_set_int failed");
-                       }
+               if (storage_type == MS_USER_STORAGE_EXTERNAL) {
+                       if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING))
+                               goto ERROR;
                }
        } else {
-               if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) {
-                       res = MS_MEDIA_ERR_VCONF_SET_FAIL;
-                       MS_DBG_ERR("ms_config_set_int failed");
-               }
+               if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED))
+                               goto ERROR;
 
-               if (storage_type == MS_STORAGE_EXTERNAL) {
-                       if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED)) {
-                               res = MS_MEDIA_ERR_VCONF_SET_FAIL;
-                               MS_DBG_ERR("ms_config_set_int failed");
-                       }
+               if (storage_type == MS_USER_STORAGE_EXTERNAL) {
+                       if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED))
+                               goto ERROR;
                }
        }
 
-       err = ms_set_power_mode(status);
-       if (err != MS_MEDIA_ERR_NONE) {
+       ret = ms_set_power_mode(status);
+       if (ret != MS_MEDIA_ERR_NONE)
                MS_DBG_ERR("ms_set_power_mode fail");
-               res = err;
-       }
 
-       return res;
+       return ret;
+ERROR:
+       MS_DBG_ERR("ms_config_set_int failed");
+       return MS_MEDIA_ERR_INTERNAL;
 }
 
 int ms_set_power_mode(ms_db_status_type_t status)
@@ -835,12 +339,12 @@ int ms_set_power_mode(ms_db_status_type_t status)
 
        switch (status) {
        case MS_DB_UPDATING:
-               err = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
+               err = device_power_request_lock(POWER_LOCK_CPU, 0);
                if (err != 0)
                        res = MS_MEDIA_ERR_INTERNAL;
                break;
        case MS_DB_UPDATED:
-               err = display_unlock_state(LCD_OFF, PM_RESET_TIMER);
+               err = device_power_release_lock(POWER_LOCK_CPU);
                if (err != 0)
                        res = MS_MEDIA_ERR_INTERNAL;
                break;
@@ -862,41 +366,27 @@ void ms_trim_dir_path(char *dir_path)
                dir_path[len -1] = '\0';
 }
 
-bool ms_check_folder_path(const char *folder_path)
-{
-       DIR *dp = NULL;
-
-       dp = opendir(folder_path);
-       if (dp == NULL) {
-               MS_DBG_ERR("Deleted folder path");
-               return false;
-       }
-       closedir(dp);
-
-       return true;
-}
-
-int ms_check_size_mediadb(uid_t uid, double *db_size)
+int ms_check_size_mediadb(uid_t uid, uint64_t *db_size)
 {
        int ret = MS_MEDIA_ERR_NONE;
        char *db_path = NULL;
        struct stat buf;
 
-       ret = media_db_get_media_db_path(uid, &db_path);
+       ret = ms_user_get_media_db_path(uid, &db_path);
 
-       if(stat(db_path, &buf) == 0) {
-               *db_size = buf.st_size;
+       if (stat(db_path, &buf) == 0) {
+               *db_size = (uint64_t)buf.st_size;
        } else {
                MS_DBG_STRERROR("stat failed");
                ret = MS_MEDIA_ERR_INTERNAL;
        }
 
-       MS_SAFE_FREE(db_path);
+       g_free(db_path);
 
        return ret;
 }
 
-#ifdef _SET_VIP_PROCESS
+#ifdef _USE_TVPD_MODE
 #define PROC_OOM_SCORE_ADJ_PATH                "/proc/%d/oom_score_adj"
 #define VIP_OOM_SCORE_ADJ                      (-1000)
 #define PROC_NAME_MAX 1024
@@ -926,7 +416,7 @@ static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
        else
                filename = filename + 1;
 
-       strncpy(cmdline, filename, PROC_NAME_MAX-1);
+       SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
 
        return MS_MEDIA_ERR_NONE;
 }
@@ -942,7 +432,7 @@ int ms_set_vip_process(void)
        /* Get Pid */
        pid = getpid();
        if (ms_get_cmdline_from_proc(pid, process_name)) {
-               MS_DBG_ERR("%s: Read process name failed pid[%d]\n", __func__, pid);
+               MS_DBG_ERR("%s: Read process name failed pid[%d]", __func__, pid);
                return MS_MEDIA_ERR_INTERNAL;
        }
        appid = process_name;