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 7403d4f..a861da4
 #include <sys/statvfs.h>
 #include <sys/stat.h>
 
-#ifdef _SET_VIP_PROCESS
+#ifdef _USE_TVPD_MODE
 #include <sys/prctl.h>
+#include <usb-device.h>
 #endif
 #include <system_info.h>
 #include <device/power.h>
 
-#ifndef _USE_DEVICED_DBUS
-#include <usb-device.h>
-#endif
-
 #include "media-util.h"
 #include "media-server-ipc.h"
 #include "media-common-dbg.h"
 #include "media-common-system.h"
 #include "media-common-utils.h"
 
-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;
-}
-
 bool ms_config_get_int(const char *key, int *value)
 {
        int err;
@@ -97,26 +74,7 @@ bool ms_config_set_int(const char *key, int value)
        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;
-}
-
-int ms_get_remain_space(double *free_space)
+int ms_get_remain_space(uint64_t *free_space)
 {
        int ret = MS_MEDIA_ERR_NONE;
        const char *path = "/opt";
@@ -130,42 +88,39 @@ int ms_get_remain_space(double *free_space)
        }
 
        /* f_bsize:unsigned long, f_bavail:fsblkcnt_t(unsigned long) */
-       *free_space = (double)(s.f_bsize * s.f_bavail);
+       *free_space = (uint64_t)s.f_bsize * (uint64_t)s.f_bavail;
 
        return MS_MEDIA_ERR_NONE;
 }
 
-#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)
 {
@@ -178,11 +133,11 @@ int ms_check_file_path(const char *file_path, uid_t uid)
        }
 
        ret = ms_user_get_storage_type(uid, file_path, &storage_type);
-       MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PATH, "Invalid path");
+       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_PATH;
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
 
        return MS_MEDIA_ERR_NONE;
@@ -192,91 +147,37 @@ 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");
 
-       ret = ms_user_get_internal_root_path(uid, &usr_path);
-       if (ret != MS_MEDIA_ERR_NONE) {
-               MS_DBG_ERR("ms_user_get_internal_root_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, uid) != 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)
-                       break;
-               else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 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_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;
 }
 
-void __ms_trim_path(const char *input_path, char **output_path)
-{
-       char buf[4096] = {0,};
-       char tmp[4096] = {0,};
-       char *pos = NULL;
-
-       memset(buf, 0, sizeof(buf));
-       SAFE_STRLCPY(buf, input_path, sizeof(buf));
-
-       while ((pos = strstr(buf, "//")) != NULL) {
-               memset(tmp, 0, sizeof(tmp));
-               SAFE_STRLCPY(tmp, buf, pos - buf + 1);
-               SAFE_STRLCAT(tmp, pos + 1, sizeof(tmp));
-
-               memset(buf, 0, sizeof(buf));
-               SAFE_STRLCPY(buf, tmp, sizeof(buf));
-       }
-
-       if (g_str_has_suffix(buf, "/"))
-               *output_path = g_strndup(buf, strlen(buf) - 1);
-       else
-               *output_path = g_strdup(buf);
-}
-
 int ms_check_scan_ignore(char * path, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
@@ -293,14 +194,14 @@ int ms_check_scan_ignore(char * path, uid_t uid)
        /* Check for symbolic link */
        tmp_path = realpath(path, NULL);
        /* Get trimmed path */
-       __ms_trim_path(path, &org_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);
-               MS_SAFE_FREE(org_path);
+               g_free(org_path);
                MS_DBG_ERR("symbolic link(directory)");
-               return MS_MEDIA_ERR_INVALID_PATH;
+               return MS_MEDIA_ERR_INVALID_PARAMETER;
        }
 #else
        if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
@@ -309,33 +210,32 @@ int ms_check_scan_ignore(char * path, uid_t uid)
                MS_SAFE_FREE(mediashared);
                if (g_strcmp0(replace, org_path) != 0) {
                        MS_SAFE_FREE(tmp_path);
-                       MS_SAFE_FREE(org_path);
+                       g_free(org_path);
                        MS_DBG_ERR("symbolic link(directory)");
-                       return MS_MEDIA_ERR_INVALID_PATH;
+                       return MS_MEDIA_ERR_INVALID_PARAMETER;
                }
        } else {
                if (g_strcmp0(tmp_path, org_path) != 0) {
                        MS_SAFE_FREE(tmp_path);
-                       MS_SAFE_FREE(org_path);
+                       g_free(org_path);
                        MS_DBG_ERR("symbolic link(directory)");
-                       return MS_MEDIA_ERR_INVALID_PATH;
+                       return MS_MEDIA_ERR_INVALID_PARAMETER;
                }
        }
 #endif
        MS_SAFE_FREE(tmp_path);
-       MS_SAFE_FREE(org_path);
+       g_free(org_path);
 
        if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
-               memset(ignore_path, 0, sizeof(ignore_path));
                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_PATH;
+                       return MS_MEDIA_ERR_INVALID_PARAMETER;
                }
        } else {
                MS_DBG_ERR("g_file_test fails[%s]", path);
-               ret = MS_MEDIA_ERR_INVALID_PATH;
+               ret = MS_MEDIA_ERR_INVALID_PARAMETER;
 
                if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
                        MS_DBG_ERR("Fail to get USB path");
@@ -354,113 +254,82 @@ int ms_check_scan_ignore(char * path, uid_t uid)
        return ret;
 }
 
-bool ms_storage_mount_status(const char* start_path)
+#ifdef _USE_TVPD_MODE
+typedef struct storage_result {
+       char *storage_path;
+       bool result;
+} storage_result_s;
+
+static void __ms_check_mount_status(usb_device_h usb_device, void *user_data)
 {
-       bool ret = false;
-#ifndef _USE_DEVICED_DBUS
-       int count = 0;
-       int err = 0;
-       usb_device_list_h list;
-       usb_device_h device;
+       storage_result_s *data = (storage_result_s *)user_data;
        char *mount_path = NULL;
 
-       char *storage_path = NULL;
+       mount_path = usb_device_get_mountpath(usb_device);
+       if (!mount_path)
+               return;
+
+       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 char *start_path)
+{
+       bool ret = false;
+#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;
 }
 
 int ms_set_db_status(ms_db_status_type_t status, ms_user_storage_type_e 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_USER_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 (!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_USER_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 (!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)
@@ -497,7 +366,7 @@ void ms_trim_dir_path(char *dir_path)
                dir_path[len -1] = '\0';
 }
 
-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;
@@ -506,18 +375,18 @@ int ms_check_size_mediadb(uid_t uid, double *db_size)
        ret = ms_user_get_media_db_path(uid, &db_path);
 
        if (stat(db_path, &buf) == 0) {
-               *db_size = buf.st_size;
+               *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