4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include <sys/types.h>
25 #include <sys/statvfs.h>
29 #include <sys/prctl.h>
31 #include <system_info.h>
32 #include <device/power.h>
34 #ifndef _USE_DEVICED_DBUS
35 #include <usb-device.h>
38 #include "media-util.h"
39 #include "media-server-ipc.h"
40 #include "media-common-dbg.h"
41 #include "media-common-system.h"
42 #include "media-common-utils.h"
44 bool ms_config_get_int(const char *key, int *value)
49 MS_DBG_ERR("Arguments key or value is NULL");
53 err = vconf_get_int(key, value);
57 MS_DBG_ERR("Error code: %d", err);
62 bool ms_config_set_int(const char *key, int value)
67 MS_DBG_ERR("Arguments key is NULL");
71 err = vconf_set_int(key, value);
75 MS_DBG_ERR("Error code: %d", err);
80 bool ms_config_get_str(const char *key, char **value)
84 if (key == NULL || value == NULL) {
85 MS_DBG_ERR("Arguments key or value is NULL");
89 res = vconf_get_str(key);
90 if (MS_STRING_VALID(res)) {
91 *value = g_strdup(res);
99 int ms_get_remain_space(uint64_t *free_space)
101 int ret = MS_MEDIA_ERR_NONE;
102 const char *path = "/opt";
105 ret = statvfs(path, &s);
107 MS_DBG_ERR("statvfs failed[%d]", ret);
109 return MS_MEDIA_ERR_INTERNAL;
112 /* f_bsize:unsigned long, f_bavail:fsblkcnt_t(unsigned long) */
113 *free_space = (uint64_t)s.f_bsize * (uint64_t)s.f_bavail;
115 return MS_MEDIA_ERR_NONE;
118 #ifdef _USE_TVPD_MODE
119 bool ms_is_support_pvr(void)
121 bool bSupportPVR = false;
122 if (system_info_get_custom_bool("com.samsung/featureconf/pvr.pvr_support", &bSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
123 MS_DBG_ERR("Get PVR Support failed");
127 MS_DBG("PVR Support : [%d]", bSupportPVR);
133 bool ms_is_valid_symlink(const char *path)
135 #ifdef _USE_TVPD_MODE
138 g_autofree char *real_path = realpath(path, NULL);
143 return (g_strcmp0(real_path, MEDIA_SHARE_PATH) == 0);
147 int ms_check_file_path(const char *file_path, uid_t uid)
149 ms_user_storage_type_e storage_type = -1;
150 int ret = MS_MEDIA_ERR_NONE;
152 if (!MS_STRING_VALID(file_path)) {
153 MS_DBG_ERR("Invalid path");
154 return MS_MEDIA_ERR_INVALID_PARAMETER;
157 ret = ms_user_get_storage_type(uid, file_path, &storage_type);
158 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
160 if (!g_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
161 MS_DBG_SERR("g_file_test fail [%s]", file_path);
162 return MS_MEDIA_ERR_INVALID_PARAMETER;
165 return MS_MEDIA_ERR_NONE;
168 int ms_check_ignore_dir(const char *full_path, uid_t uid)
170 int ret = MS_MEDIA_ERR_NONE;
171 char *dir_path = NULL;
175 ret = ms_check_file_path(full_path, uid);
176 MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
178 if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && !strncmp(full_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB))) {
179 next_pos = strlen(MEDIA_ROOT_PATH_USB) + 1;
181 ret = ms_user_get_internal_root_path(uid, &dir_path);
182 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_internal_root_path() fail");
184 next_pos = strlen(dir_path);
189 while ((next = strstr(full_path + next_pos, "/"))) {
190 next_pos = (next - full_path);
191 dir_path = g_strndup(full_path, next_pos);
194 ret = ms_check_scan_ignore(dir_path, uid);
196 if (ret != MS_MEDIA_ERR_NONE)
203 int ms_check_scan_ignore(char * path, uid_t uid)
205 int ret = MS_MEDIA_ERR_NONE;
206 const char *ignore_file = ".scan_ignore";
207 char *tmp_path = NULL;
208 char *org_path = NULL;
209 char ignore_path[MS_FILE_PATH_LEN_MAX] = {0, };
211 #ifndef _USE_TVPD_MODE
212 char replace[MS_FILE_PATH_LEN_MAX] = {0, };
213 char *mediashared = NULL;
216 /* Check for symbolic link */
217 tmp_path = realpath(path, NULL);
218 /* Get trimmed path */
219 org_path = g_canonicalize_filename(path, NULL);
221 #ifdef _USE_TVPD_MODE
222 if (g_strcmp0(tmp_path, org_path) != 0) {
223 MS_SAFE_FREE(tmp_path);
225 MS_DBG_ERR("symbolic link(directory)");
226 return MS_MEDIA_ERR_INVALID_PARAMETER;
229 if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
230 ms_user_get_mediashared_path(uid, &mediashared);
231 snprintf(replace, MS_FILE_PATH_LEN_MAX, "%s%s", mediashared, tmp_path + strlen(MEDIA_SHARE_PATH));
232 MS_SAFE_FREE(mediashared);
233 if (g_strcmp0(replace, org_path) != 0) {
234 MS_SAFE_FREE(tmp_path);
236 MS_DBG_ERR("symbolic link(directory)");
237 return MS_MEDIA_ERR_INVALID_PARAMETER;
240 if (g_strcmp0(tmp_path, org_path) != 0) {
241 MS_SAFE_FREE(tmp_path);
243 MS_DBG_ERR("symbolic link(directory)");
244 return MS_MEDIA_ERR_INVALID_PARAMETER;
248 MS_SAFE_FREE(tmp_path);
251 if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
252 snprintf(ignore_path, sizeof(ignore_path), "%s/%s", path, ignore_file);
254 if (g_file_test(ignore_path, G_FILE_TEST_EXISTS)) {
255 MS_DBG_WARN("scan ignore file exist [%s]", ignore_path);
256 return MS_MEDIA_ERR_INVALID_PARAMETER;
259 MS_DBG_ERR("g_file_test fails[%s]", path);
260 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
262 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
263 MS_DBG_ERR("Fail to get USB path");
267 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
268 /*if the directory does not exist, check the device is unmounted*/
269 if (!ms_storage_mount_status(path)) {
270 MS_DBG_ERR("Device is unmounted[%s]", path);
271 return MS_MEDIA_ERR_USB_UNMOUNTED;
279 #ifndef _USE_DEVICED_DBUS
281 typedef struct storage_result {
286 static void __ms_check_mount_status(usb_device_h usb_device, void *user_data)
288 storage_result_s *data = (storage_result_s *)user_data;
289 char *mount_path = NULL;
291 mount_path = usb_device_get_mountpath(usb_device);
295 MS_DBG_SWARN("mount_path [%s]", mount_path);
296 data->result = (g_strcmp0(mount_path, data->storage_path) == 0);
300 bool ms_storage_mount_status(const char *start_path)
303 #ifndef _USE_DEVICED_DBUS
304 storage_result_s res = {0, };
305 char *remain_path = NULL;
308 remain_path = strstr(start_path + strlen(MEDIA_ROOT_PATH_USB) + 1, "/");
309 if (remain_path != NULL)
310 remain_len = strlen(remain_path);
312 res.storage_path = g_strndup(start_path, strlen(start_path) - remain_len);
314 MS_DBG_SWARN("storage_path [%s]", res.storage_path);
316 usb_mass_storage_foreach(__ms_check_mount_status, &res);
317 g_free(res.storage_path);
321 MS_DBG_SWARN("start path is mounted [%s]", start_path);
326 int ms_set_db_status(ms_db_status_type_t status, ms_user_storage_type_e storage_type)
328 int ret = MS_MEDIA_ERR_NONE;
330 if (status == MS_DB_UPDATING) {
331 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING))
334 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
335 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING))
339 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED))
342 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
343 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED))
348 ret = ms_set_power_mode(status);
349 if (ret != MS_MEDIA_ERR_NONE)
350 MS_DBG_ERR("ms_set_power_mode fail");
354 MS_DBG_ERR("ms_config_set_int failed");
355 return MS_MEDIA_ERR_INTERNAL;
358 int ms_set_power_mode(ms_db_status_type_t status)
360 int res = MS_MEDIA_ERR_NONE;
365 err = device_power_request_lock(POWER_LOCK_CPU, 0);
367 res = MS_MEDIA_ERR_INTERNAL;
370 err = device_power_release_lock(POWER_LOCK_CPU);
372 res = MS_MEDIA_ERR_INTERNAL;
375 MS_DBG_ERR("Unacceptable type : %d", status);
382 void ms_trim_dir_path(char *dir_path)
384 /* need implementation */
385 /* if dir_path is not NULL terminated, this function will occure crash */
386 int len = strlen(dir_path);
388 if (dir_path[len -1] == '/')
389 dir_path[len -1] = '\0';
392 int ms_check_size_mediadb(uid_t uid, uint64_t *db_size)
394 int ret = MS_MEDIA_ERR_NONE;
395 char *db_path = NULL;
398 ret = ms_user_get_media_db_path(uid, &db_path);
400 if (stat(db_path, &buf) == 0) {
401 *db_size = (uint64_t)buf.st_size;
403 MS_DBG_STRERROR("stat failed");
404 ret = MS_MEDIA_ERR_INTERNAL;
412 #ifdef _USE_TVPD_MODE
413 #define PROC_OOM_SCORE_ADJ_PATH "/proc/%d/oom_score_adj"
414 #define VIP_OOM_SCORE_ADJ (-1000)
415 #define PROC_NAME_MAX 1024
416 #define PROC_BUF_MAX 64
418 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
420 char buf[PROC_BUF_MAX];
421 char cmdline_buf[PROC_NAME_MAX];
425 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
426 fp = fopen(buf, "r");
428 return MS_MEDIA_ERR_INTERNAL;
430 if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
432 return MS_MEDIA_ERR_INTERNAL;
436 filename = strrchr(cmdline_buf, '/');
437 if (filename == NULL)
438 filename = cmdline_buf;
440 filename = filename + 1;
442 SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
444 return MS_MEDIA_ERR_NONE;
447 int ms_set_vip_process(void)
451 static pid_t pid = 0;
452 static char process_name[PROC_NAME_MAX] = {0};
453 static char *appid = NULL;
457 if (ms_get_cmdline_from_proc(pid, process_name)) {
458 MS_DBG_ERR("%s: Read process name failed pid[%d]", __func__, pid);
459 return MS_MEDIA_ERR_INTERNAL;
461 appid = process_name;
463 MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
465 if (prctl(PR_GET_DUMPABLE) == 0)
466 prctl(PR_SET_DUMPABLE, 1);
468 snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
469 id = open(buf, O_WRONLY, 0777);
471 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
472 return MS_MEDIA_ERR_INTERNAL;
474 snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
475 if (write(id, buf, strlen(buf)) < 0) {
476 MS_DBG_ERR("write() failed errno=%d", errno);
478 return MS_MEDIA_ERR_INTERNAL;
481 return MS_MEDIA_ERR_NONE;