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>
29 #include <dbus/dbus-glib.h>
30 #include <dbus/dbus.h>
31 #include <dbus/dbus-glib-lowlevel.h>
32 #include <sys/statvfs.h>
34 #ifdef _SET_VIP_PROCESS
35 #include <sys/prctl.h>
37 #include <system_info.h>
38 #include <dd-display.h>
40 #ifndef _USE_DEVICED_DBUS
41 #include <usb-device.h>
44 #include "media-util.h"
45 #include "media-server-ipc.h"
46 #include "media-common-dbg.h"
47 #include "media-common-system.h"
48 #include "media-common-utils.h"
50 #define MS_DRM_CONTENT_TYPE_LENGTH 100
52 /* it's for 32bit file offset */
54 unsigned long int f_bsize;
55 unsigned long int f_frsize;
56 unsigned long int f_blocks;
57 unsigned long int f_bfree;
58 unsigned long int f_bavail;
59 unsigned long int f_files;
60 unsigned long int f_ffree;
61 unsigned long int f_favail;
62 unsigned long int f_fsid;
63 #ifdef _STATVFSBUF_F_UNUSED
66 unsigned long int f_flag;
67 unsigned long int f_namemax;
71 bool ms_is_mmc_inserted(void)
74 ms_stg_type_e stg_type = MS_STG_TYPE_MMC;
75 GArray *dev_list = NULL;
77 ret = ms_sys_get_device_list(stg_type, &dev_list);
78 if (ret == MS_MEDIA_ERR_NONE) {
79 if (dev_list != NULL) {
80 MS_DBG_WARN("MMC FOUND[%d]", dev_list->len);
81 ms_sys_release_device_list(&dev_list);
84 MS_DBG_ERR("MMC NOT FOUND");
87 MS_DBG_ERR("ms_sys_get_device_list failed");
93 int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
96 int real_size = size - 1;
98 if (!res || !pattern || !str1 || !str2)
99 return MS_MEDIA_ERR_INVALID_PARAMETER;
101 if (real_size < (int)(strlen(str1) + strlen(str2)))
102 return MS_MEDIA_ERR_INVALID_PARAMETER;
104 len = snprintf(res, real_size, pattern, str1, str2);
106 return MS_MEDIA_ERR_INVALID_PARAMETER;
111 return MS_MEDIA_ERR_NONE;
114 int ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
117 int real_size = size;
119 if (!res || !pattern || !str1) {
120 MS_DBG_ERR("parameta is invalid");
121 return MS_MEDIA_ERR_INVALID_PARAMETER;
124 if (real_size < (int)(strlen(str1))) {
125 MS_DBG_ERR("size is wrong");
126 return MS_MEDIA_ERR_INVALID_PARAMETER;
129 len = snprintf(res, real_size, pattern, str1);
131 MS_DBG_ERR("snprintf failed");
132 return MS_MEDIA_ERR_INVALID_PARAMETER;
137 return MS_MEDIA_ERR_NONE;
140 bool ms_config_get_int(const char *key, int *value)
144 if (!key || !value) {
145 MS_DBG_ERR("Arguments key or value is NULL");
149 err = vconf_get_int(key, value);
155 MS_DBG_ERR("Unexpected error code: %d", err);
160 bool ms_config_set_int(const char *key, int value)
165 MS_DBG_ERR("Arguments key is NULL");
169 err = vconf_set_int(key, value);
175 MS_DBG_ERR("Unexpected error code: %d", err);
180 bool ms_config_get_str(const char *key, char **value)
184 if (key == NULL || value == NULL) {
185 MS_DBG_ERR("Arguments key or value is NULL");
189 res = vconf_get_str(key);
190 if (MS_STRING_VALID(res)) {
191 *value = strdup(res);
199 bool ms_config_set_str(const char *key, const char *value)
203 if (!key || !value) {
204 MS_DBG_ERR("Arguments key or value is NULL");
208 err = vconf_set_str(key, value);
212 MS_DBG_ERR("fail to vconf_set_str %d", err);
217 bool ms_config_get_bool(const char *key, int *value)
221 if (!key || !value) {
222 MS_DBG_ERR("Arguments key or value is NULL");
226 err = vconf_get_bool(key, value);
232 MS_DBG_ERR("Unexpected error code: %d", err);
237 static int get_memory_size(const char *path, struct statvfs_32 *buf)
242 ret = statvfs(path, &s);
244 MS_DBG_ERR("statvfs failed[%d]", ret);
246 return MS_MEDIA_ERR_INTERNAL;
249 buf->f_bsize = s.f_bsize;
250 buf->f_frsize = s.f_frsize;
251 buf->f_blocks = (unsigned long)s.f_blocks;
252 buf->f_bfree = (unsigned long)s.f_bfree;
253 buf->f_bavail = (unsigned long)s.f_bavail;
254 buf->f_files = (unsigned long)s.f_files;
255 buf->f_ffree = (unsigned long)s.f_ffree;
256 buf->f_favail = (unsigned long)s.f_favail;
257 buf->f_fsid = s.f_fsid;
258 buf->f_flag = s.f_flag;
259 buf->f_namemax = s.f_namemax;
261 return MS_MEDIA_ERR_NONE;
264 int ms_get_remain_space(double *free_space)
266 int ret = MS_MEDIA_ERR_NONE;
267 struct statvfs_32 temp;
269 ret = get_memory_size("/opt", &temp);
270 if (ret != MS_MEDIA_ERR_NONE) {
271 MS_DBG_ERR("fail to get memory size");
275 // MS_DBG_ERR("Total mem : %lf, Avail mem : %lf", (double)temp.f_frsize*temp.f_blocks, (double)temp.f_bsize*temp.f_bavail);
277 *free_space = (double)temp.f_bsize*temp.f_bavail;
282 #ifdef _USE_RECORDED_CONTENT
283 bool ms_is_support_pvr(void)
287 if (system_info_get_value_int(SYSTEM_INFO_KEY_PVR_SUPPORTED, &nSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
288 MS_DBG_ERR("Get PVR Support failed");
292 MS_DBG("PVR Support : [%d]", nSupportPVR);
294 return (nSupportPVR != 0);
298 #ifdef _USE_SENIOR_MODE
299 bool ms_is_support_senior_mode()
301 bool bSupportSeniorMode = false;
303 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
304 MS_DBG_ERR("Get senior mode support failed");
308 MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
310 return bSupportSeniorMode;
314 int ms_check_file_path(const char *file_path, uid_t uid)
318 ms_user_storage_type_e storage_type = -1;
319 int ret = MS_MEDIA_ERR_NONE;
321 if (!MS_STRING_VALID(file_path)) {
322 MS_DBG_ERR("Invalid path");
323 return MS_MEDIA_ERR_INVALID_PARAMETER;
326 /* check location of file */
327 /* file must exists under "/opt/usr/media" or "/opt/storage/sdcard" */
328 ret = ms_user_get_storage_type(uid, file_path, &storage_type);
329 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PATH, "Invalid path");
331 /* check the file exits actually */
332 exist = open(file_path, O_RDONLY);
334 MS_DBG_SERR("open file fail [%s]", file_path);
335 return MS_MEDIA_ERR_INVALID_PATH;
339 /* check type of the path */
340 /* It must be a regular file */
341 memset(&file_st, 0, sizeof(struct stat));
342 if (stat(file_path, &file_st) == 0) {
343 if (!S_ISREG(file_st.st_mode)) {
344 /* In this case, it is not a regula file */
345 MS_DBG_ERR("this path is not a file");
346 return MS_MEDIA_ERR_INVALID_PATH;
349 MS_DBG_STRERROR("stat failed");
350 return MS_MEDIA_ERR_INVALID_PATH;
353 return MS_MEDIA_ERR_NONE;
356 int ms_check_ignore_dir(const char *full_path, uid_t uid)
358 int ret = MS_MEDIA_ERR_NONE;
359 char *dir_path = NULL;
360 char *leaf_path = NULL;
361 char *usr_path = NULL;
363 ret = ms_check_file_path(full_path, uid);
364 if (ret != MS_MEDIA_ERR_NONE) {
365 MS_DBG_ERR("invalid path : %s", full_path);
366 return MS_MEDIA_ERR_INVALID_PATH;
369 dir_path = g_path_get_dirname(full_path);
370 if (dir_path == NULL || strcmp(dir_path, ".") == 0) {
371 MS_DBG_ERR("getting directory path is failed : %s", full_path);
372 MS_SAFE_FREE(dir_path);
373 return MS_MEDIA_ERR_INVALID_PATH;
376 ret = ms_user_get_internal_root_path(uid, &usr_path);
377 if (ret != MS_MEDIA_ERR_NONE) {
378 MS_DBG_ERR("ms_user_get_internal_root_path() fail");
379 MS_SAFE_FREE(dir_path);
380 return MS_MEDIA_ERR_INTERNAL;
384 if (ms_check_scan_ignore(dir_path) != MS_MEDIA_ERR_NONE) {
385 ret = MS_MEDIA_ERR_INVALID_PATH;
389 #ifdef _USE_SENIOR_MODE
390 if (ms_is_support_senior_mode()) {
391 if (strcmp(dir_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)
395 if (strcmp(dir_path, usr_path) == 0)
397 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
399 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(dir_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
401 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(dir_path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0))
404 leaf_path = strrchr(dir_path, '/');
405 if (leaf_path != NULL) {
406 int seek_len = leaf_path -dir_path;
407 dir_path[seek_len] = '\0';
409 MS_DBG_ERR("Fail to find leaf path");
410 ret = MS_MEDIA_ERR_INVALID_PATH;
415 MS_SAFE_FREE(dir_path);
416 MS_SAFE_FREE(usr_path);
421 int ms_check_scan_ignore(char * path)
425 const char *ignore_path = "/.scan_ignore";
426 char *check_ignore_file = NULL;
427 int ret = MS_MEDIA_ERR_NONE;
429 if (strstr(path, "/.")) {
430 MS_DBG_ERR("hidden path");
431 ret = MS_MEDIA_ERR_INVALID_PATH;
435 fd = open(path, O_RDONLY | O_DIRECTORY);
437 MS_DBG_ERR("%s folder opendir fails", path);
438 ret = MS_MEDIA_ERR_INVALID_PATH;
440 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
441 MS_DBG_ERR("Fail to get USB path");
445 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
446 if (errno == ENOENT) {
447 /*if the directory does not exist, check the device is unmounted*/
448 if (!ms_storage_mount_status(path)) {
449 MS_DBG_ERR("Device is unmounted[%s]", path);
450 ret = MS_MEDIA_ERR_USB_UNMOUNTED;
456 struct stat folder_st;
457 if (stat(path, &folder_st) == 0) {
458 MS_DBG_WARN("DEV[%ld] INODE[%lld] UID[%ld] GID[%ld] MODE[%lo] PATH[%s]", (long)folder_st.st_dev, (long long)folder_st.st_ino,
459 (long)folder_st.st_uid, (long)folder_st.st_gid, (unsigned long) folder_st.st_mode, path);
461 MS_DBG_ERR("%s folder stat fails", path);
466 /* check the file exits actually */
469 path_len = strlen(path) + strlen(ignore_path) + 1;
470 check_ignore_file = malloc(path_len);
471 if (check_ignore_file != NULL) {
472 memset(check_ignore_file, 0x0, path_len);
473 snprintf(check_ignore_file, path_len, "%s%s", path, ignore_path);
475 exist = open(check_ignore_file, O_RDONLY);
477 MS_DBG_WARN("scan_ignore exists [%s]", check_ignore_file);
478 ret = MS_MEDIA_ERR_INVALID_PATH;
481 MS_SAFE_FREE(check_ignore_file);
483 MS_DBG_ERR("malloc failed");
484 ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
495 if (exist >= 0) close(exist);
500 bool ms_storage_mount_status(const char* start_path)
503 #ifndef _USE_DEVICED_DBUS
506 usb_device_list_h list;
508 char *mount_path = NULL;
510 char *storage_path = NULL;
511 char *remain_path = NULL;
514 remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
515 if (remain_path != NULL)
516 remain_len = strlen(remain_path);
518 storage_path = strndup(start_path, strlen(start_path) - remain_len);
520 MS_DBG_WARN("storage_path [%s]", storage_path);
522 err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
524 count = usb_device_list_get_count(list);
526 err = usb_device_list_get_first(list, &device);
527 if (err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
528 mount_path = usb_device_get_mountpath(device);
529 if (mount_path != NULL) {
530 MS_DBG_WARN("mount_path [%s]", mount_path);
531 if (strlen(mount_path) == strlen(storage_path)) {
532 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
533 MS_DBG_WARN("start path is mounted [%s]", start_path);
541 while (usb_device_list_get_next(list, &device) == 0) {
542 if (device != NULL) {
543 mount_path = usb_device_get_mountpath(device);
544 if (mount_path != NULL) {
545 MS_DBG_WARN("mount_path [%s]", mount_path);
546 if (strlen(mount_path) == strlen(storage_path)) {
547 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
548 MS_DBG_WARN("start path is mounted [%s]", start_path);
559 usb_device_free_device_list(list);
561 MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
564 MS_SAFE_FREE(storage_path);
569 int ms_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type)
571 int res = MS_MEDIA_ERR_NONE;
574 if (status == MS_DB_UPDATING) {
575 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) {
576 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
577 MS_DBG_ERR("ms_config_set_int failed");
580 if (storage_type == MS_STORAGE_EXTERNAL) {
581 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING)) {
582 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
583 MS_DBG_ERR("ms_config_set_int failed");
587 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) {
588 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
589 MS_DBG_ERR("ms_config_set_int failed");
592 if (storage_type == MS_STORAGE_EXTERNAL) {
593 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED)) {
594 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
595 MS_DBG_ERR("ms_config_set_int failed");
600 err = ms_set_power_mode(status);
601 if (err != MS_MEDIA_ERR_NONE) {
602 MS_DBG_ERR("ms_set_power_mode fail");
609 int ms_set_power_mode(ms_db_status_type_t status)
611 int res = MS_MEDIA_ERR_NONE;
616 err = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
618 res = MS_MEDIA_ERR_INTERNAL;
621 err = display_unlock_state(LCD_OFF, PM_RESET_TIMER);
623 res = MS_MEDIA_ERR_INTERNAL;
626 MS_DBG_ERR("Unacceptable type : %d", status);
633 void ms_trim_dir_path(char *dir_path)
635 /* need implementation */
636 /* if dir_path is not NULL terminated, this function will occure crash */
637 int len = strlen(dir_path);
639 if (dir_path[len -1] == '/')
640 dir_path[len -1] = '\0';
643 bool ms_check_folder_path(const char *folder_path)
647 dp = opendir(folder_path);
649 MS_DBG_ERR("Deleted folder path");
657 int ms_check_size_mediadb(uid_t uid, double *db_size)
659 int ret = MS_MEDIA_ERR_NONE;
660 char *db_path = NULL;
663 ret = ms_user_get_media_db_path(uid, &db_path);
665 if (stat(db_path, &buf) == 0) {
666 *db_size = buf.st_size;
668 MS_DBG_STRERROR("stat failed");
669 ret = MS_MEDIA_ERR_INTERNAL;
672 MS_SAFE_FREE(db_path);
677 #ifdef _SET_VIP_PROCESS
678 #define PROC_OOM_SCORE_ADJ_PATH "/proc/%d/oom_score_adj"
679 #define VIP_OOM_SCORE_ADJ (-1000)
680 #define PROC_NAME_MAX 1024
681 #define PROC_BUF_MAX 64
683 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
685 char buf[PROC_BUF_MAX];
686 char cmdline_buf[PROC_NAME_MAX];
690 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
691 fp = fopen(buf, "r");
693 return MS_MEDIA_ERR_INTERNAL;
695 if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
697 return MS_MEDIA_ERR_INTERNAL;
701 filename = strrchr(cmdline_buf, '/');
702 if (filename == NULL)
703 filename = cmdline_buf;
705 filename = filename + 1;
707 SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
709 return MS_MEDIA_ERR_NONE;
712 int ms_set_vip_process(void)
716 static pid_t pid = 0;
717 static char process_name[PROC_NAME_MAX] = {0};
718 static char *appid = NULL;
722 if (ms_get_cmdline_from_proc(pid, process_name)) {
723 MS_DBG_ERR("%s: Read process name failed pid[%d]\n", __func__, pid);
724 return MS_MEDIA_ERR_INTERNAL;
726 appid = process_name;
728 MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
730 if (prctl(PR_GET_DUMPABLE) == 0)
731 prctl(PR_SET_DUMPABLE, 1);
733 snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
734 id = open(buf, O_WRONLY, 0777);
736 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
737 return MS_MEDIA_ERR_INTERNAL;
739 snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
740 if (write(id, buf, strlen(buf)) < 0) {
741 MS_DBG_ERR("write() failed errno=%d", errno);
743 return MS_MEDIA_ERR_INTERNAL;
746 return MS_MEDIA_ERR_NONE;