/*
- * Media Server
+ * Media Server
*
* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
*
*
*/
-/**
- * This file defines api utilities of contents manager engines.
- *
- * @file media-server-utils.c
- * @author Yong Yeon Kim(yy9875.kim@samsung.com)
- * @version 1.0
- * @brief This file implements main database operation.
- */
-
-#include <errno.h>
-#include <pmapi.h>
+#include <sys/types.h>
+#include <fcntl.h>
#include <vconf.h>
-#include <aul/aul.h>
+#include <sys/statvfs.h>
+#include <sys/stat.h>
+
+#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-dbg.h"
-#include "media-common-drm.h"
+#include "media-common-system.h"
#include "media-common-utils.h"
-#ifdef FMS_PERF
-#include <sys/time.h>
-#define MILLION 1000000L
-struct timeval g_mmc_start_time;
-struct timeval g_mmc_end_time;
-#endif
+bool ms_config_get_int(const char *key, int *value)
+{
+ int err;
+
+ if (!key || !value) {
+ MS_DBG_ERR("Arguments key or value is NULL");
+ return false;
+ }
+
+ err = vconf_get_int(key, value);
+ if (err == VCONF_OK)
+ return true;
-#define MS_DRM_CONTENT_TYPE_LENGTH 100
+ MS_DBG_ERR("Error code: %d", err);
-#ifdef FMS_PERF
-void
-ms_check_start_time(struct timeval *start_time)
+ return false;
+}
+
+bool ms_config_set_int(const char *key, int value)
{
- gettimeofday(start_time, NULL);
+ int err;
+
+ if (!key) {
+ MS_DBG_ERR("Arguments key is NULL");
+ return false;
+ }
+
+ err = vconf_set_int(key, value);
+ if (err == VCONF_OK)
+ return true;
+
+ MS_DBG_ERR("Error code: %d", err);
+
+ return false;
}
-void
-ms_check_end_time(struct timeval *end_time)
+int ms_get_remain_space(uint64_t *free_space)
{
- gettimeofday(end_time, NULL);
+ int ret = MS_MEDIA_ERR_NONE;
+ const char *path = "/opt";
+ struct statvfs s;
+
+ ret = statvfs(path, &s);
+ if (ret != 0) {
+ MS_DBG_ERR("statvfs failed[%d]", ret);
+ MS_DBG_STRERROR();
+ return MS_MEDIA_ERR_INTERNAL;
+ }
+
+ /* 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;
}
-void
-ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
+#ifdef _USE_TVPD_MODE
+bool ms_is_support_pvr(void)
{
- struct timeval time;
- long difftime;
-
- time.tv_sec = end_time->tv_sec - start_time->tv_sec;
- time.tv_usec = end_time->tv_usec - start_time->tv_usec;
- difftime = MILLION * time.tv_sec + time.tv_usec;
- MS_DBG("The function_to_time took %ld microseconds or %f seconds.",
- difftime, difftime / (double)MILLION);
+ 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]", bSupportPVR);
+
+ return bSupportPVR;
}
#endif
-bool
-ms_is_mmc_inserted(void)
+bool ms_is_valid_symlink(const char *path)
{
- int data = -1;
- ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &data);
- if (data != VCONFKEY_SYSMAN_MMC_MOUNTED) {
+#ifdef _USE_TVPD_MODE
+ return false;
+#else
+ g_autofree char *real_path = realpath(path, NULL);
+
+ if (!real_path)
return false;
- } else {
- return true;
+
+ return (g_strcmp0(real_path, MEDIA_SHARE_PATH) == 0);
+#endif
+}
+
+int ms_check_file_path(const char *file_path, uid_t uid)
+{
+ ms_user_storage_type_e storage_type = -1;
+ int ret = MS_MEDIA_ERR_NONE;
+
+ if (!MS_STRING_VALID(file_path)) {
+ MS_DBG_ERR("Invalid path");
+ return MS_MEDIA_ERR_INVALID_PARAMETER;
+ }
+
+ 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;
}
-ms_storage_type_t
-ms_get_storage_type_by_full(const char *path)
+int ms_check_ignore_dir(const char *full_path, uid_t uid)
{
- if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
- return MS_STORAGE_INTERNAL;
- } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
- return MS_STORAGE_EXTERNAL;
- } else
- return MS_MEDIA_ERR_INVALID_PATH;
+ int ret = MS_MEDIA_ERR_NONE;
+ char *dir_path = NULL;
+ char *next = NULL;
+ int next_pos = 0;
+
+ ret = ms_check_file_path(full_path, uid);
+ MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
+
+ 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");
+
+ next_pos = strlen(dir_path);
+ g_free(dir_path);
+ dir_path = NULL;
+ }
+
+ while ((next = strstr(full_path + next_pos, "/"))) {
+ next_pos = (next - full_path);
+ dir_path = g_strndup(full_path, next_pos);
+ next_pos++;
+
+ ret = ms_check_scan_ignore(dir_path, uid);
+ g_free(dir_path);
+ if (ret != MS_MEDIA_ERR_NONE)
+ break;
+ }
+
+ return ret;
}
-int
-ms_get_mime(const char *path, char *mimetype)
+int ms_check_scan_ignore(char * path, uid_t uid)
{
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
+
+ /* Check for symbolic link */
+ tmp_path = realpath(path, NULL);
+ /* Get trimmed path */
+ org_path = g_canonicalize_filename(path, NULL);
- if (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);
- /*get content type and mime type from file. */
- /*in case of drm file. */
- if (ms_is_drm_file(path)) {
- if (ms_get_mime_in_drm_info(path, mimetype) != MS_MEDIA_ERR_NONE) {
- MS_DBG_ERR("Fail to get mime from drm API");
- if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
- MS_DBG_ERR("aul_get_mime_from_file fail");
- ret = MS_MEDIA_ERR_MIME_GET_FAIL;
- } else {
- MS_DBG_ERR("aul_get_mime_from_file success");
- ret = MS_MEDIA_ERR_NONE;
- }
+ 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 {
- /*in case of normal files */
- if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
- MS_DBG_ERR("aul_get_mime_from_file fail");
- ret = MS_MEDIA_ERR_MIME_GET_FAIL;
+ 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");
+ return ret;
+ }
+
+ if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
+ /*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;
+ }
}
}
return ret;
}
-int
-ms_strappend(char *res, const int size, const char *pattern,
- const char *str1, const char *str2)
+#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)
{
- int len = 0;
- int real_size = size - 1;
+ storage_result_s *data = (storage_result_s *)user_data;
+ char *mount_path = NULL;
- if (!res ||!pattern || !str1 ||!str2 )
- return MS_MEDIA_ERR_INVALID_PARAMETER;
+ mount_path = usb_device_get_mountpath(usb_device);
+ if (!mount_path)
+ return;
- if (real_size < (strlen(str1) + strlen(str2)))
- return MS_MEDIA_ERR_INVALID_PARAMETER;
+ MS_DBG_SWARN("mount_path [%s]", mount_path);
+ data->result = (g_strcmp0(mount_path, data->storage_path) == 0);
+}
+#endif
- len = snprintf(res, real_size, pattern, str1, str2);
- if (len < 0) {
- return MS_MEDIA_ERR_INVALID_PARAMETER;
- }
+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;
- res[len] = '\0';
+ remain_path = strstr(start_path + strlen(MEDIA_ROOT_PATH_USB) + 1, "/");
+ if (remain_path != NULL)
+ remain_len = strlen(remain_path);
- return MS_MEDIA_ERR_NONE;
+ res.storage_path = g_strndup(start_path, strlen(start_path) - remain_len);
+
+ MS_DBG_SWARN("storage_path [%s]", res.storage_path);
+
+ usb_mass_storage_foreach(__ms_check_mount_status, &res);
+ g_free(res.storage_path);
+ ret = res.result;
+
+ if (ret)
+ MS_DBG_SWARN("start path is mounted [%s]", start_path);
+#endif
+ return ret;
}
-int
-ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
+int ms_set_db_status(ms_db_status_type_t status, ms_user_storage_type_e storage_type)
{
- int len = 0;
- int real_size = size;
+ int ret = MS_MEDIA_ERR_NONE;
- if (!res || !pattern || !str1) {
- MS_DBG_ERR("parameta is invalid");
- return MS_MEDIA_ERR_INVALID_PARAMETER;
- }
+ if (status == MS_DB_UPDATING) {
+ if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING))
+ goto ERROR;
- if (real_size < strlen(str1)) {
- MS_DBG_ERR("size is wrong");
- return MS_MEDIA_ERR_INVALID_PARAMETER;
- }
+ 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))
+ goto ERROR;
- len = snprintf(res, real_size, pattern, str1);
- if (len < 0) {
- MS_DBG_ERR("snprintf failed");
- return MS_MEDIA_ERR_INVALID_PARAMETER;
+ if (storage_type == MS_USER_STORAGE_EXTERNAL) {
+ if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED))
+ goto ERROR;
+ }
}
- res[len] = '\0';
+ ret = ms_set_power_mode(status);
+ if (ret != MS_MEDIA_ERR_NONE)
+ MS_DBG_ERR("ms_set_power_mode fail");
- return MS_MEDIA_ERR_NONE;
+ return ret;
+ERROR:
+ MS_DBG_ERR("ms_config_set_int failed");
+ return MS_MEDIA_ERR_INTERNAL;
}
-bool
-ms_config_get_int(const char *key, int *value)
+int ms_set_power_mode(ms_db_status_type_t status)
{
+ int res = MS_MEDIA_ERR_NONE;
int err;
- if (!key || !value) {
- MS_DBG_ERR("Arguments key or value is NULL");
- return false;
+ switch (status) {
+ case MS_DB_UPDATING:
+ err = device_power_request_lock(POWER_LOCK_CPU, 0);
+ if (err != 0)
+ res = MS_MEDIA_ERR_INTERNAL;
+ break;
+ case MS_DB_UPDATED:
+ err = device_power_release_lock(POWER_LOCK_CPU);
+ if (err != 0)
+ res = MS_MEDIA_ERR_INTERNAL;
+ break;
+ default:
+ MS_DBG_ERR("Unacceptable type : %d", status);
+ break;
}
- err = vconf_get_int(key, value);
- if (err == 0)
- return true;
- else if (err == -1)
- return false;
- else
- MS_DBG_ERR("Unexpected error code: %d", err);
+ return res;
+}
- return false;
+void ms_trim_dir_path(char *dir_path)
+{
+ /* need implementation */
+ /* if dir_path is not NULL terminated, this function will occure crash */
+ int len = strlen(dir_path);
+
+ if (dir_path[len -1] == '/')
+ dir_path[len -1] = '\0';
}
-bool
-ms_config_set_int(const char *key, int value)
+int ms_check_size_mediadb(uid_t uid, uint64_t *db_size)
{
- int err;
+ int ret = MS_MEDIA_ERR_NONE;
+ char *db_path = NULL;
+ struct stat buf;
- if (!key) {
- MS_DBG_ERR("Arguments key is NULL");
- return false;
+ ret = ms_user_get_media_db_path(uid, &db_path);
+
+ if (stat(db_path, &buf) == 0) {
+ *db_size = (uint64_t)buf.st_size;
+ } else {
+ MS_DBG_STRERROR("stat failed");
+ ret = MS_MEDIA_ERR_INTERNAL;
}
- err = vconf_set_int(key, value);
- if (err == 0)
- return true;
- else if (err == -1)
- return false;
- else
- MS_DBG_ERR("Unexpected error code: %d", err);
+ g_free(db_path);
- return false;
+ return ret;
}
-bool
-ms_config_get_str(const char *key, char *value)
+#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
+#define PROC_BUF_MAX 64
+
+static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
{
- char *res;
- if (!key || !value) {
- MS_DBG_ERR("Arguments key or value is NULL");
- return false;
+ char buf[PROC_BUF_MAX];
+ char cmdline_buf[PROC_NAME_MAX];
+ char *filename;
+ FILE *fp;
+
+ snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
+ fp = fopen(buf, "r");
+ if (fp == NULL)
+ return MS_MEDIA_ERR_INTERNAL;
+
+ if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
+ fclose(fp);
+ return MS_MEDIA_ERR_INTERNAL;
}
+ fclose(fp);
- res = vconf_get_str(key);
- if (res) {
- strncpy(value, res, strlen(res) + 1);
- return true;
- }
+ filename = strrchr(cmdline_buf, '/');
+ if (filename == NULL)
+ filename = cmdline_buf;
+ else
+ filename = filename + 1;
- return false;
+ SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
+
+ return MS_MEDIA_ERR_NONE;
}
-bool
-ms_config_set_str(const char *key, const char *value)
+int ms_set_vip_process(void)
{
- int err;
-
- if (!key || !value) {
- MS_DBG_ERR("Arguments key or value is NULL");
- return false;
+ char buf[100] = {0};
+ int id = 0;
+ static pid_t pid = 0;
+ static char process_name[PROC_NAME_MAX] = {0};
+ static char *appid = NULL;
+
+ /* Get Pid */
+ pid = getpid();
+ if (ms_get_cmdline_from_proc(pid, process_name)) {
+ MS_DBG_ERR("%s: Read process name failed pid[%d]", __func__, pid);
+ return MS_MEDIA_ERR_INTERNAL;
}
+ appid = process_name;
- err = vconf_set_str(key, value);
- if (err == 0)
- return true;
- else
- MS_DBG_ERR("fail to vconf_set_str %d", err);
+ MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
- return false;
-}
+ if (prctl(PR_GET_DUMPABLE) == 0)
+ prctl(PR_SET_DUMPABLE, 1);
+ snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
+ id = open(buf, O_WRONLY, 0777);
+ if (id < 0) {
+ MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
+ return MS_MEDIA_ERR_INTERNAL;
+ }
+ snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
+ if (write(id, buf, strlen(buf)) < 0) {
+ MS_DBG_ERR("write() failed errno=%d", errno);
+ close(id);
+ return MS_MEDIA_ERR_INTERNAL;
+ }
+ close(id);
+ return MS_MEDIA_ERR_NONE;
+}
+#endif