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>
28 #ifdef _SET_VIP_PROCESS
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 int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
47 int real_size = size - 1;
49 if (!res || !pattern || !str1 || !str2)
50 return MS_MEDIA_ERR_INVALID_PARAMETER;
52 if (real_size < (int)(strlen(str1) + strlen(str2)))
53 return MS_MEDIA_ERR_INVALID_PARAMETER;
55 len = snprintf(res, real_size, pattern, str1, str2);
57 return MS_MEDIA_ERR_INVALID_PARAMETER;
61 return MS_MEDIA_ERR_NONE;
64 bool ms_config_get_int(const char *key, int *value)
69 MS_DBG_ERR("Arguments key or value is NULL");
73 err = vconf_get_int(key, value);
77 MS_DBG_ERR("Error code: %d", err);
82 bool ms_config_set_int(const char *key, int value)
87 MS_DBG_ERR("Arguments key is NULL");
91 err = vconf_set_int(key, value);
95 MS_DBG_ERR("Error code: %d", err);
100 bool ms_config_get_str(const char *key, char **value)
104 if (key == NULL || value == NULL) {
105 MS_DBG_ERR("Arguments key or value is NULL");
109 res = vconf_get_str(key);
110 if (MS_STRING_VALID(res)) {
111 *value = g_strdup(res);
119 int ms_get_remain_space(double *free_space)
121 int ret = MS_MEDIA_ERR_NONE;
122 const char *path = "/opt";
125 ret = statvfs(path, &s);
127 MS_DBG_ERR("statvfs failed[%d]", ret);
129 return MS_MEDIA_ERR_INTERNAL;
132 /* f_bsize:unsigned long, f_bavail:fsblkcnt_t(unsigned long) */
133 *free_space = (double)(s.f_bsize * s.f_bavail);
135 return MS_MEDIA_ERR_NONE;
138 #ifdef _USE_RECORDED_CONTENT
139 bool ms_is_support_pvr(void)
141 bool bSupportPVR = false;
142 if (system_info_get_custom_bool("com.samsung/featureconf/pvr.pvr_support", &bSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
143 MS_DBG_ERR("Get PVR Support failed");
147 MS_DBG("PVR Support : [%d]", bSupportPVR);
153 #ifdef _USE_SENIOR_MODE
154 bool ms_is_support_senior_mode(void)
156 bool bSupportSeniorMode = false;
158 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
159 MS_DBG_ERR("Get senior mode support failed");
163 MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
165 return bSupportSeniorMode;
169 int ms_check_file_path(const char *file_path, uid_t uid)
171 ms_user_storage_type_e storage_type = -1;
172 int ret = MS_MEDIA_ERR_NONE;
174 if (!MS_STRING_VALID(file_path)) {
175 MS_DBG_ERR("Invalid path");
176 return MS_MEDIA_ERR_INVALID_PARAMETER;
179 ret = ms_user_get_storage_type(uid, file_path, &storage_type);
180 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
182 if (!g_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
183 MS_DBG_SERR("g_file_test fail [%s]", file_path);
184 return MS_MEDIA_ERR_INVALID_PARAMETER;
187 return MS_MEDIA_ERR_NONE;
190 int ms_check_ignore_dir(const char *full_path, uid_t uid)
192 int ret = MS_MEDIA_ERR_NONE;
193 char *dir_path = NULL;
194 char *leaf_path = NULL;
195 char *usr_path = NULL;
197 ret = ms_check_file_path(full_path, uid);
198 if (ret != MS_MEDIA_ERR_NONE) {
199 MS_DBG_ERR("invalid path : %s", full_path);
200 return MS_MEDIA_ERR_INVALID_PARAMETER;
203 dir_path = g_path_get_dirname(full_path);
204 if (dir_path == NULL || strcmp(dir_path, ".") == 0) {
205 MS_DBG_ERR("getting directory path is failed : %s", full_path);
207 return MS_MEDIA_ERR_INVALID_PARAMETER;
210 ret = ms_user_get_internal_root_path(uid, &usr_path);
211 if (ret != MS_MEDIA_ERR_NONE) {
212 MS_DBG_ERR("ms_user_get_internal_root_path() fail");
214 return MS_MEDIA_ERR_INTERNAL;
218 if (ms_check_scan_ignore(dir_path, uid) != MS_MEDIA_ERR_NONE) {
219 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
223 #ifdef _USE_SENIOR_MODE
224 if (ms_is_support_senior_mode()) {
225 if (strcmp(dir_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)
229 if (strcmp(dir_path, usr_path) == 0)
231 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
233 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(dir_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
235 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(dir_path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0))
238 leaf_path = strrchr(dir_path, '/');
239 if (leaf_path != NULL) {
240 int seek_len = leaf_path -dir_path;
241 dir_path[seek_len] = '\0';
243 MS_DBG_ERR("Fail to find leaf path");
244 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
255 static void __ms_trim_path(const char *input_path, char **output_path)
257 char buf[4096] = {0, };
258 char tmp[4096] = {0, };
261 SAFE_STRLCPY(buf, input_path, sizeof(buf));
263 while ((pos = strstr(buf, "//")) != NULL) {
264 memset(tmp, 0, sizeof(tmp));
265 SAFE_STRLCPY(tmp, buf, pos - buf + 1);
266 SAFE_STRLCAT(tmp, pos + 1, sizeof(tmp));
268 memset(buf, 0, sizeof(buf));
269 SAFE_STRLCPY(buf, tmp, sizeof(buf));
272 if (g_str_has_suffix(buf, "/"))
273 *output_path = g_strndup(buf, strlen(buf) - 1);
275 *output_path = g_strdup(buf);
278 int ms_check_scan_ignore(char * path, uid_t uid)
280 int ret = MS_MEDIA_ERR_NONE;
281 const char *ignore_file = ".scan_ignore";
282 char *tmp_path = NULL;
283 char *org_path = NULL;
284 char ignore_path[MS_FILE_PATH_LEN_MAX] = {0, };
286 #ifndef _USE_TVPD_MODE
287 char replace[MS_FILE_PATH_LEN_MAX] = {0, };
288 char *mediashared = NULL;
291 /* Check for symbolic link */
292 tmp_path = realpath(path, NULL);
293 /* Get trimmed path */
294 __ms_trim_path(path, &org_path);
296 #ifdef _USE_TVPD_MODE
297 if (g_strcmp0(tmp_path, org_path) != 0) {
298 MS_SAFE_FREE(tmp_path);
300 MS_DBG_ERR("symbolic link(directory)");
301 return MS_MEDIA_ERR_INVALID_PARAMETER;
304 if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
305 ms_user_get_mediashared_path(uid, &mediashared);
306 snprintf(replace, MS_FILE_PATH_LEN_MAX, "%s%s", mediashared, tmp_path + strlen(MEDIA_SHARE_PATH));
307 MS_SAFE_FREE(mediashared);
308 if (g_strcmp0(replace, org_path) != 0) {
309 MS_SAFE_FREE(tmp_path);
311 MS_DBG_ERR("symbolic link(directory)");
312 return MS_MEDIA_ERR_INVALID_PARAMETER;
315 if (g_strcmp0(tmp_path, org_path) != 0) {
316 MS_SAFE_FREE(tmp_path);
318 MS_DBG_ERR("symbolic link(directory)");
319 return MS_MEDIA_ERR_INVALID_PARAMETER;
323 MS_SAFE_FREE(tmp_path);
326 if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
327 snprintf(ignore_path, sizeof(ignore_path), "%s/%s", path, ignore_file);
329 if (g_file_test(ignore_path, G_FILE_TEST_EXISTS)) {
330 MS_DBG_WARN("scan ignore file exist [%s]", ignore_path);
331 return MS_MEDIA_ERR_INVALID_PARAMETER;
334 MS_DBG_ERR("g_file_test fails[%s]", path);
335 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
337 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
338 MS_DBG_ERR("Fail to get USB path");
342 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
343 /*if the directory does not exist, check the device is unmounted*/
344 if (!ms_storage_mount_status(path)) {
345 MS_DBG_ERR("Device is unmounted[%s]", path);
346 return MS_MEDIA_ERR_USB_UNMOUNTED;
354 bool ms_storage_mount_status(const char* start_path)
357 #ifndef _USE_DEVICED_DBUS
360 usb_device_list_h list;
362 char *mount_path = NULL;
364 char *storage_path = NULL;
365 char *remain_path = NULL;
368 remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
369 if (remain_path != NULL)
370 remain_len = strlen(remain_path);
372 storage_path = g_strndup(start_path, strlen(start_path) - remain_len);
374 MS_DBG_SWARN("storage_path [%s]", storage_path);
376 err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
378 count = usb_device_list_get_count(list);
380 err = usb_device_list_get_first(list, &device);
381 if (err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
382 mount_path = usb_device_get_mountpath(device);
383 if (mount_path != NULL) {
384 MS_DBG_SWARN("mount_path [%s]", mount_path);
385 if (strlen(mount_path) == strlen(storage_path)) {
386 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
387 MS_DBG_SWARN("start path is mounted [%s]", start_path);
395 while (usb_device_list_get_next(list, &device) == 0) {
396 if (device != NULL) {
397 mount_path = usb_device_get_mountpath(device);
398 if (mount_path != NULL) {
399 MS_DBG_SWARN("mount_path [%s]", mount_path);
400 if (strlen(mount_path) == strlen(storage_path)) {
401 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
402 MS_DBG_SWARN("start path is mounted [%s]", start_path);
413 usb_device_free_device_list(list);
415 MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
418 g_free(storage_path);
423 int ms_set_db_status(ms_db_status_type_t status, ms_user_storage_type_e storage_type)
425 int ret = MS_MEDIA_ERR_NONE;
427 if (status == MS_DB_UPDATING) {
428 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING))
431 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
432 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING))
436 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED))
439 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
440 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED))
445 ret = ms_set_power_mode(status);
446 if (ret != MS_MEDIA_ERR_NONE)
447 MS_DBG_ERR("ms_set_power_mode fail");
451 MS_DBG_ERR("ms_config_set_int failed");
452 return MS_MEDIA_ERR_INTERNAL;
455 int ms_set_power_mode(ms_db_status_type_t status)
457 int res = MS_MEDIA_ERR_NONE;
462 err = device_power_request_lock(POWER_LOCK_CPU, 0);
464 res = MS_MEDIA_ERR_INTERNAL;
467 err = device_power_release_lock(POWER_LOCK_CPU);
469 res = MS_MEDIA_ERR_INTERNAL;
472 MS_DBG_ERR("Unacceptable type : %d", status);
479 void ms_trim_dir_path(char *dir_path)
481 /* need implementation */
482 /* if dir_path is not NULL terminated, this function will occure crash */
483 int len = strlen(dir_path);
485 if (dir_path[len -1] == '/')
486 dir_path[len -1] = '\0';
489 int ms_check_size_mediadb(uid_t uid, double *db_size)
491 int ret = MS_MEDIA_ERR_NONE;
492 char *db_path = NULL;
495 ret = ms_user_get_media_db_path(uid, &db_path);
497 if (stat(db_path, &buf) == 0) {
498 *db_size = buf.st_size;
500 MS_DBG_STRERROR("stat failed");
501 ret = MS_MEDIA_ERR_INTERNAL;
509 #ifdef _SET_VIP_PROCESS
510 #define PROC_OOM_SCORE_ADJ_PATH "/proc/%d/oom_score_adj"
511 #define VIP_OOM_SCORE_ADJ (-1000)
512 #define PROC_NAME_MAX 1024
513 #define PROC_BUF_MAX 64
515 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
517 char buf[PROC_BUF_MAX];
518 char cmdline_buf[PROC_NAME_MAX];
522 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
523 fp = fopen(buf, "r");
525 return MS_MEDIA_ERR_INTERNAL;
527 if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
529 return MS_MEDIA_ERR_INTERNAL;
533 filename = strrchr(cmdline_buf, '/');
534 if (filename == NULL)
535 filename = cmdline_buf;
537 filename = filename + 1;
539 SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
541 return MS_MEDIA_ERR_NONE;
544 int ms_set_vip_process(void)
548 static pid_t pid = 0;
549 static char process_name[PROC_NAME_MAX] = {0};
550 static char *appid = NULL;
554 if (ms_get_cmdline_from_proc(pid, process_name)) {
555 MS_DBG_ERR("%s: Read process name failed pid[%d]", __func__, pid);
556 return MS_MEDIA_ERR_INTERNAL;
558 appid = process_name;
560 MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
562 if (prctl(PR_GET_DUMPABLE) == 0)
563 prctl(PR_SET_DUMPABLE, 1);
565 snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
566 id = open(buf, O_WRONLY, 0777);
568 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
569 return MS_MEDIA_ERR_INTERNAL;
571 snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
572 if (write(id, buf, strlen(buf)) < 0) {
573 MS_DBG_ERR("write() failed errno=%d", errno);
575 return MS_MEDIA_ERR_INTERNAL;
578 return MS_MEDIA_ERR_NONE;