From a569578e7533bf46104ad00ef7e469be9be79d0a Mon Sep 17 00:00:00 2001 From: Seungbae Shin Date: Wed, 17 Jan 2024 18:32:26 +0900 Subject: [PATCH] media-common-utils.c: extract product implementations to media-common-utils-tv.c Change-Id: I5026f8ef0d905fdebb4f6536a8422d48f9b88894 --- Makefile.am | 5 + src/common/media-common-utils-tv.c | 315 ++++++++++++++++++++++++++++++++ src/common/media-common-utils.c | 363 +++++-------------------------------- 3 files changed, 370 insertions(+), 313 deletions(-) create mode 100644 src/common/media-common-utils-tv.c diff --git a/Makefile.am b/Makefile.am index f5a13af..5c32d57 100644 --- a/Makefile.am +++ b/Makefile.am @@ -112,6 +112,10 @@ media_server_SOURCES = src/common/media-common-utils.c \ src/server/media-server-dcm.c \ src/server/media-server-main.c +if USE_PRODUCT_TV +media_server_SOURCES += src/common/media-common-utils-tv.c +endif + media_server_CFLAGS = -I${srcdir}/src/server/include \ $(SQLITE3_CFLAGS) \ $(COMMON_CFLAGS) \ @@ -162,6 +166,7 @@ else #media scanner v2 ###################################################### media_scanner_v2_SOURCES = src/common/media-common-utils.c \ + src/common/media-common-utils-tv.c \ src/common/media-common-system.c \ src/common/media-common-external-storage.c \ src/common/media-common-db-svc.c \ diff --git a/src/common/media-common-utils-tv.c b/src/common/media-common-utils-tv.c new file mode 100644 index 0000000..7a47c2e --- /dev/null +++ b/src/common/media-common-utils-tv.c @@ -0,0 +1,315 @@ +/* + * media-common-utils-tv.c + * + * Copyright (c) 2024 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "media-util.h" +#include "media-server-ipc.h" +#include "media-common-dbg.h" +#include "media-common-system.h" +#include "media-common-utils.h" + +#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 + +typedef struct storage_result { + char *storage_path; + bool result; +} storage_result_s; + +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; + + MS_DBG_ERR("Error code: %d", err); + + return false; +} + +bool ms_config_set_int(const char *key, int value) +{ + 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; +} + +int ms_get_remain_space(uint64_t *free_space) +{ + 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; +} + +bool ms_is_support_pvr(void) +{ + 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; +} + +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; + + 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); +} + +bool ms_storage_mount_status(const char *start_path) +{ + bool ret = false; + storage_result_s res = {0, }; + char *remain_path = NULL; + int remain_len = 0; + + remain_path = strstr(start_path + strlen(MEDIA_ROOT_PATH_USB) + 1, "/"); + if (remain_path != NULL) + remain_len = strlen(remain_path); + + 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); + return ret; +} + +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 = 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; + } + + g_free(db_path); + + return ret; +} + +int ms_set_db_status(ms_db_status_type_t status) +{ + int ret = MS_MEDIA_ERR_NONE; + + if (status == MS_DB_UPDATING) { + if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) + goto ERROR; + } else { + if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) + goto ERROR; + } + + ret = ms_set_power_mode(status); + if (ret != MS_MEDIA_ERR_NONE) + MS_DBG_ERR("ms_set_power_mode fail"); + + return ret; +ERROR: + MS_DBG_ERR("ms_config_set_int failed"); + return MS_MEDIA_ERR_INTERNAL; +} + +static int __ms_get_cmdline_from_proc(pid_t pid, char *cmdline) +{ + 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); + + filename = strrchr(cmdline_buf, '/'); + if (filename == NULL) + filename = cmdline_buf; + else + filename = filename + 1; + + SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX); + + return MS_MEDIA_ERR_NONE; +} + +int ms_set_vip_process(void) +{ + 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; + + MS_DBG("Process name[%s]:Pid[%d]", appid, pid); + + 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; +} + +/* Common functions but with a product implementation */ + +bool ms_is_valid_symlink(const char *path) +{ + return false; +} + +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, }; + + /* Check for symbolic link */ + tmp_path = realpath(path, NULL); + /* Get trimmed path */ + org_path = g_canonicalize_filename(path, NULL); + + 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; + } + + MS_SAFE_FREE(tmp_path); + g_free(org_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"); + 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; +} diff --git a/src/common/media-common-utils.c b/src/common/media-common-utils.c index 256b14f..a6262df 100644 --- a/src/common/media-common-utils.c +++ b/src/common/media-common-utils.c @@ -17,17 +17,6 @@ * */ -#include -#include -#include -#include - -#ifdef _USE_TVPD_MODE -#include -#include -#include -#endif -#include #include #include "media-util.h" @@ -36,90 +25,6 @@ #include "media-common-system.h" #include "media-common-utils.h" -#ifdef _USE_TVPD_MODE -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; - - MS_DBG_ERR("Error code: %d", err); - - return false; -} - -bool ms_config_set_int(const char *key, int value) -{ - 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; -} - -int ms_get_remain_space(uint64_t *free_space) -{ - 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; -} - -bool ms_is_support_pvr(void) -{ - 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_valid_symlink(const char *path) -{ -#ifdef _USE_TVPD_MODE - return false; -#else - g_autofree char *real_path = realpath(path, NULL); - - if (!real_path) - return false; - - return (g_strcmp0(real_path, MEDIA_SHARE_PATH) == 0); -#endif -} - int ms_verify_all_parent_dirs(const char *full_path, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; @@ -156,6 +61,54 @@ int ms_verify_all_parent_dirs(const char *full_path, uid_t uid) return ret; } +int ms_set_power_mode(ms_db_status_type_t status) +{ + int res = MS_MEDIA_ERR_NONE; + int err; + + 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; + } + + return res; +} + +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'; +} + +#ifdef _USE_TVPD_MODE +/* See media-common-utils-tv.c for common functions with a TV product implementation */ +#else +/* Public implementation */ +bool ms_is_valid_symlink(const char *path) +{ + g_autofree char *real_path = realpath(path, NULL); + + if (!real_path) + return false; + + return (g_strcmp0(real_path, MEDIA_SHARE_PATH) == 0); +} + int ms_check_scan_ignore(char *path, uid_t uid) { int ret = MS_MEDIA_ERR_NONE; @@ -164,24 +117,14 @@ int ms_check_scan_ignore(char *path, uid_t uid) 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); -#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)); @@ -200,7 +143,7 @@ int ms_check_scan_ignore(char *path, uid_t uid) return MS_MEDIA_ERR_INVALID_PARAMETER; } } -#endif + MS_SAFE_FREE(tmp_path); g_free(org_path); @@ -214,214 +157,8 @@ int ms_check_scan_ignore(char *path, uid_t uid) } else { MS_DBG_ERR("g_file_test fails[%s]", path); ret = MS_MEDIA_ERR_INVALID_PARAMETER; -#ifdef _USE_TVPD_MODE - 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; - } - } -#endif } return ret; } - -#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) -{ - storage_result_s *data = (storage_result_s *)user_data; - char *mount_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); -} - -bool ms_storage_mount_status(const char *start_path) -{ - bool ret = false; - storage_result_s res = {0, }; - char *remain_path = NULL; - int remain_len = 0; - - remain_path = strstr(start_path + strlen(MEDIA_ROOT_PATH_USB) + 1, "/"); - if (remain_path != NULL) - remain_len = strlen(remain_path); - - 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); - return ret; -} - -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 = 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; - } - - g_free(db_path); - - return ret; -} - -int ms_set_db_status(ms_db_status_type_t status) -{ - int ret = MS_MEDIA_ERR_NONE; - - if (status == MS_DB_UPDATING) { - if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) - goto ERROR; - } else { - if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) - goto ERROR; - } - - ret = ms_set_power_mode(status); - if (ret != MS_MEDIA_ERR_NONE) - MS_DBG_ERR("ms_set_power_mode fail"); - - return ret; -ERROR: - MS_DBG_ERR("ms_config_set_int failed"); - return MS_MEDIA_ERR_INTERNAL; -} -#endif - -int ms_set_power_mode(ms_db_status_type_t status) -{ - int res = MS_MEDIA_ERR_NONE; - int err; - - 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; - } - - return res; -} - -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'; -} - -#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 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); - - filename = strrchr(cmdline_buf, '/'); - if (filename == NULL) - filename = cmdline_buf; - else - filename = filename + 1; - - SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX); - - return MS_MEDIA_ERR_NONE; -} - -int ms_set_vip_process(void) -{ - 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; - - MS_DBG("Process name[%s]:Pid[%d]", appid, pid); - - 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 +#endif /* _USE_TVPD_MODE */ \ No newline at end of file -- 2.7.4