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.
23 #include <sys/types.h>
30 #include <dbus/dbus-glib.h>
31 #include <dbus/dbus.h>
32 #include <dbus/dbus-glib-lowlevel.h>
33 #include <sys/statvfs.h>
35 #ifdef _SET_VIP_PROCESS
36 #include <sys/prctl.h>
38 #include <system_info.h>
39 #include <dd-display.h>
41 #ifndef _USE_DEVICED_DBUS
42 #include <usb-device.h>
45 #include "media-util.h"
46 #include "media-server-ipc.h"
47 #include "media-common-dbg.h"
48 #include "media-common-system.h"
49 #include "media-common-utils.h"
51 #define MS_DRM_CONTENT_TYPE_LENGTH 100
53 /* it's for 32bit file offset */
55 unsigned long int f_bsize;
56 unsigned long int f_frsize;
57 unsigned long int f_blocks;
58 unsigned long int f_bfree;
59 unsigned long int f_bavail;
60 unsigned long int f_files;
61 unsigned long int f_ffree;
62 unsigned long int f_favail;
63 unsigned long int f_fsid;
64 #ifdef _STATVFSBUF_F_UNUSED
67 unsigned long int f_flag;
68 unsigned long int f_namemax;
72 bool ms_is_mmc_inserted(void)
75 ms_stg_type_e stg_type = MS_STG_TYPE_MMC;
76 GArray *dev_list = NULL;
78 ret = ms_sys_get_device_list(stg_type, &dev_list);
79 if (ret == MS_MEDIA_ERR_NONE) {
80 if (dev_list != NULL) {
81 MS_DBG_WARN("MMC FOUND[%d]", dev_list->len);
82 ms_sys_release_device_list(&dev_list);
85 MS_DBG_ERR("MMC NOT FOUND");
88 MS_DBG_ERR("ms_sys_get_device_list failed");
94 int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
97 int real_size = size - 1;
99 if (!res || !pattern || !str1 || !str2)
100 return MS_MEDIA_ERR_INVALID_PARAMETER;
102 if (real_size < (int)(strlen(str1) + strlen(str2)))
103 return MS_MEDIA_ERR_INVALID_PARAMETER;
105 len = snprintf(res, real_size, pattern, str1, str2);
107 return MS_MEDIA_ERR_INVALID_PARAMETER;
112 return MS_MEDIA_ERR_NONE;
115 int ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
118 int real_size = size;
120 if (!res || !pattern || !str1) {
121 MS_DBG_ERR("parameta is invalid");
122 return MS_MEDIA_ERR_INVALID_PARAMETER;
125 if (real_size < (int)(strlen(str1))) {
126 MS_DBG_ERR("size is wrong");
127 return MS_MEDIA_ERR_INVALID_PARAMETER;
130 len = snprintf(res, real_size, pattern, str1);
132 MS_DBG_ERR("snprintf failed");
133 return MS_MEDIA_ERR_INVALID_PARAMETER;
138 return MS_MEDIA_ERR_NONE;
141 bool ms_config_get_int(const char *key, int *value)
145 if (!key || !value) {
146 MS_DBG_ERR("Arguments key or value is NULL");
150 err = vconf_get_int(key, value);
156 MS_DBG_ERR("Unexpected error code: %d", err);
161 bool ms_config_set_int(const char *key, int value)
166 MS_DBG_ERR("Arguments key is NULL");
170 err = vconf_set_int(key, value);
176 MS_DBG_ERR("Unexpected error code: %d", err);
181 bool ms_config_get_str(const char *key, char **value)
185 if (key == NULL || value == NULL) {
186 MS_DBG_ERR("Arguments key or value is NULL");
190 res = vconf_get_str(key);
191 if (MS_STRING_VALID(res)) {
192 *value = strdup(res);
200 bool ms_config_set_str(const char *key, const char *value)
204 if (!key || !value) {
205 MS_DBG_ERR("Arguments key or value is NULL");
209 err = vconf_set_str(key, value);
213 MS_DBG_ERR("fail to vconf_set_str %d", err);
218 bool ms_config_get_bool(const char *key, int *value)
222 if (!key || !value) {
223 MS_DBG_ERR("Arguments key or value is NULL");
227 err = vconf_get_bool(key, value);
233 MS_DBG_ERR("Unexpected error code: %d", err);
238 static int get_memory_size(const char *path, struct statvfs_32 *buf)
243 ret = statvfs(path, &s);
245 MS_DBG_ERR("statvfs failed[%d]", ret);
247 return MS_MEDIA_ERR_INTERNAL;
250 buf->f_bsize = s.f_bsize;
251 buf->f_frsize = s.f_frsize;
252 buf->f_blocks = (unsigned long)s.f_blocks;
253 buf->f_bfree = (unsigned long)s.f_bfree;
254 buf->f_bavail = (unsigned long)s.f_bavail;
255 buf->f_files = (unsigned long)s.f_files;
256 buf->f_ffree = (unsigned long)s.f_ffree;
257 buf->f_favail = (unsigned long)s.f_favail;
258 buf->f_fsid = s.f_fsid;
259 buf->f_flag = s.f_flag;
260 buf->f_namemax = s.f_namemax;
262 return MS_MEDIA_ERR_NONE;
265 int ms_get_remain_space(double *free_space)
267 int ret = MS_MEDIA_ERR_NONE;
268 struct statvfs_32 temp;
270 ret = get_memory_size("/opt", &temp);
271 if (ret != MS_MEDIA_ERR_NONE) {
272 MS_DBG_ERR("fail to get memory size");
276 // MS_DBG_ERR("Total mem : %lf, Avail mem : %lf", (double)temp.f_frsize*temp.f_blocks, (double)temp.f_bsize*temp.f_bavail);
278 *free_space = (double)temp.f_bsize*temp.f_bavail;
283 #ifdef _USE_RECORDED_CONTENT
284 bool ms_is_support_pvr(void)
288 if (system_info_get_value_int(SYSTEM_INFO_KEY_PVR_SUPPORTED, &nSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
289 MS_DBG_ERR("Get PVR Support failed");
293 MS_DBG("PVR Support : [%d]", nSupportPVR);
295 return (nSupportPVR != 0);
299 #ifdef _USE_SENIOR_MODE
300 bool ms_is_support_senior_mode()
302 bool bSupportSeniorMode = false;
304 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
305 MS_DBG_ERR("Get senior mode support failed");
309 MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
311 return bSupportSeniorMode;
315 int ms_check_file_path(const char *file_path, uid_t uid)
319 ms_user_storage_type_t storage_type = -1;
320 int ret = MS_MEDIA_ERR_NONE;
322 /* check location of file */
323 /* file must exists under "/opt/usr/media" or "/opt/storage/sdcard" */
324 ret = ms_user_get_storage_type(uid, file_path, &storage_type);
325 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PATH, "Invalid path");
327 /* check the file exits actually */
328 exist = open(file_path, O_RDONLY);
330 MS_DBG_ERR("[%s]open files");
331 return MS_MEDIA_ERR_INVALID_PATH;
335 /* check type of the path */
336 /* It must be a regular file */
337 memset(&file_st, 0, sizeof(struct stat));
338 if (stat(file_path, &file_st) == 0) {
339 if (!S_ISREG(file_st.st_mode)) {
340 /* In this case, it is not a regula file */
341 MS_DBG_ERR("this path is not a file");
342 return MS_MEDIA_ERR_INVALID_PATH;
345 MS_DBG_STRERROR("stat failed");
346 return MS_MEDIA_ERR_INVALID_PATH;
349 return MS_MEDIA_ERR_NONE;
352 int ms_check_ignore_dir(const char *full_path, uid_t uid)
354 int ret = MS_MEDIA_ERR_NONE;
355 char *dir_path = NULL;
356 char *leaf_path = NULL;
357 char *usr_path = NULL;
359 ret = ms_check_file_path(full_path, uid);
360 if (ret != MS_MEDIA_ERR_NONE) {
361 MS_DBG_ERR("invalid path : %s", full_path);
362 return MS_MEDIA_ERR_INVALID_PATH;
365 dir_path = g_path_get_dirname(full_path);
366 if (dir_path == NULL || strcmp(dir_path, ".") == 0) {
367 MS_DBG_ERR("getting directory path is failed : %s", full_path);
368 MS_SAFE_FREE(dir_path);
369 return MS_MEDIA_ERR_INVALID_PATH;
372 ret = ms_user_get_internal_root_path(uid, &usr_path);
373 if (ret != MS_MEDIA_ERR_NONE) {
374 MS_DBG_ERR("ms_user_get_internal_root_path() fail");
375 MS_SAFE_FREE(dir_path);
376 return MS_MEDIA_ERR_INTERNAL;
380 if (ms_check_scan_ignore(dir_path) != MS_MEDIA_ERR_NONE) {
381 ret = MS_MEDIA_ERR_INVALID_PATH;
385 #ifdef _USE_SENIOR_MODE
386 if (ms_is_support_senior_mode()) {
387 if (strcmp(dir_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)
391 if (strcmp(dir_path, usr_path) == 0)
393 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
395 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(dir_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
397 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(dir_path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0))
400 leaf_path = strrchr(dir_path, '/');
401 if (leaf_path != NULL) {
402 int seek_len = leaf_path -dir_path;
403 dir_path[seek_len] = '\0';
405 MS_DBG_ERR("Fail to find leaf path");
406 ret = MS_MEDIA_ERR_INVALID_PATH;
411 MS_SAFE_FREE(dir_path);
412 MS_SAFE_FREE(usr_path);
417 int ms_check_scan_ignore(char * path)
421 const char *ignore_path = "/.scan_ignore";
422 char *check_ignore_file = NULL;
423 int ret = MS_MEDIA_ERR_NONE;
425 if (strstr(path, "/.")) {
426 MS_DBG_ERR("hidden path");
427 ret = MS_MEDIA_ERR_INVALID_PATH;
431 fd = open(path, O_RDONLY | O_DIRECTORY);
433 MS_DBG_ERR("%s folder opendir fails", path);
434 ret = MS_MEDIA_ERR_INVALID_PATH;
436 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
437 MS_DBG_ERR("Fail to get USB path");
441 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
442 if (errno == ENOENT) {
443 /*if the directory does not exist, check the device is unmounted*/
444 if (!ms_storage_mount_status(path)) {
445 MS_DBG_ERR("Device is unmounted[%s]", path);
446 ret = MS_MEDIA_ERR_USB_UNMOUNTED;
452 struct stat folder_st;
453 if (stat(path, &folder_st) == 0) {
454 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,
455 (long)folder_st.st_uid, (long)folder_st.st_gid, (unsigned long) folder_st.st_mode, path);
457 MS_DBG_ERR("%s folder stat fails", path);
462 /* check the file exits actually */
465 path_len = strlen(path) + strlen(ignore_path) + 1;
466 check_ignore_file = malloc(path_len);
467 if (check_ignore_file != NULL) {
468 memset(check_ignore_file, 0x0, path_len);
469 snprintf(check_ignore_file, path_len, "%s%s", path, ignore_path);
471 exist = open(check_ignore_file, O_RDONLY);
473 MS_DBG_WARN("scan_ignore exists [%s]", check_ignore_file);
474 ret = MS_MEDIA_ERR_INVALID_PATH;
477 MS_SAFE_FREE(check_ignore_file);
479 MS_DBG_ERR("malloc failed");
480 ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
491 if (exist >= 0) close(exist);
496 bool ms_storage_mount_status(const char* start_path)
499 #ifndef _USE_DEVICED_DBUS
502 usb_device_list_h list;
504 char *mount_path = NULL;
506 char *storage_path = NULL;
507 char *remain_path = NULL;
510 remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
511 if (remain_path != NULL)
512 remain_len = strlen(remain_path);
514 storage_path = strndup(start_path, strlen(start_path) - remain_len);
516 MS_DBG_WARN("storage_path [%s]", storage_path);
518 err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
520 count = usb_device_list_get_count(list);
522 err = usb_device_list_get_first(list, &device);
523 if (err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
524 mount_path = usb_device_get_mountpath(device);
525 if (mount_path != NULL) {
526 MS_DBG_WARN("mount_path [%s]", mount_path);
527 if (strlen(mount_path) == strlen(storage_path)) {
528 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
529 MS_DBG_WARN("start path is mounted [%s]", start_path);
537 while (usb_device_list_get_next(list, &device) == 0) {
538 if (device != NULL) {
539 mount_path = usb_device_get_mountpath(device);
540 if (mount_path != NULL) {
541 MS_DBG_WARN("mount_path [%s]", mount_path);
542 if (strlen(mount_path) == strlen(storage_path)) {
543 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
544 MS_DBG_WARN("start path is mounted [%s]", start_path);
555 usb_device_free_device_list(list);
557 MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
560 MS_SAFE_FREE(storage_path);
565 int ms_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type)
567 int res = MS_MEDIA_ERR_NONE;
570 if (status == MS_DB_UPDATING) {
571 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) {
572 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
573 MS_DBG_ERR("ms_config_set_int failed");
576 if (storage_type == MS_STORAGE_EXTERNAL) {
577 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING)) {
578 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
579 MS_DBG_ERR("ms_config_set_int failed");
583 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) {
584 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
585 MS_DBG_ERR("ms_config_set_int failed");
588 if (storage_type == MS_STORAGE_EXTERNAL) {
589 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED)) {
590 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
591 MS_DBG_ERR("ms_config_set_int failed");
596 err = ms_set_power_mode(status);
597 if (err != MS_MEDIA_ERR_NONE) {
598 MS_DBG_ERR("ms_set_power_mode fail");
605 int ms_set_power_mode(ms_db_status_type_t status)
607 int res = MS_MEDIA_ERR_NONE;
612 err = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
614 res = MS_MEDIA_ERR_INTERNAL;
617 err = display_unlock_state(LCD_OFF, PM_RESET_TIMER);
619 res = MS_MEDIA_ERR_INTERNAL;
622 MS_DBG_ERR("Unacceptable type : %d", status);
629 void ms_trim_dir_path(char *dir_path)
631 /* need implementation */
632 /* if dir_path is not NULL terminated, this function will occure crash */
633 int len = strlen(dir_path);
635 if (dir_path[len -1] == '/')
636 dir_path[len -1] = '\0';
639 bool ms_check_folder_path(const char *folder_path)
643 dp = opendir(folder_path);
645 MS_DBG_ERR("Deleted folder path");
653 int ms_check_size_mediadb(uid_t uid, double *db_size)
655 int ret = MS_MEDIA_ERR_NONE;
656 char *db_path = NULL;
659 ret = ms_user_get_media_db_path(uid, &db_path);
661 if (stat(db_path, &buf) == 0) {
662 *db_size = buf.st_size;
664 MS_DBG_STRERROR("stat failed");
665 ret = MS_MEDIA_ERR_INTERNAL;
668 MS_SAFE_FREE(db_path);
673 #ifdef _SET_VIP_PROCESS
674 #define PROC_OOM_SCORE_ADJ_PATH "/proc/%d/oom_score_adj"
675 #define VIP_OOM_SCORE_ADJ (-1000)
676 #define PROC_NAME_MAX 1024
677 #define PROC_BUF_MAX 64
679 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
681 char buf[PROC_BUF_MAX];
682 char cmdline_buf[PROC_NAME_MAX];
686 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
687 fp = fopen(buf, "r");
689 return MS_MEDIA_ERR_INTERNAL;
691 if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
693 return MS_MEDIA_ERR_INTERNAL;
697 filename = strrchr(cmdline_buf, '/');
698 if (filename == NULL)
699 filename = cmdline_buf;
701 filename = filename + 1;
703 SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
705 return MS_MEDIA_ERR_NONE;
708 int ms_set_vip_process(void)
712 static pid_t pid = 0;
713 static char process_name[PROC_NAME_MAX] = {0};
714 static char *appid = NULL;
718 if (ms_get_cmdline_from_proc(pid, process_name)) {
719 MS_DBG_ERR("%s: Read process name failed pid[%d]\n", __func__, pid);
720 return MS_MEDIA_ERR_INTERNAL;
722 appid = process_name;
724 MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
726 if (prctl(PR_GET_DUMPABLE) == 0)
727 prctl(PR_SET_DUMPABLE, 1);
729 snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
730 id = open(buf, O_WRONLY, 0777);
732 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
733 return MS_MEDIA_ERR_INTERNAL;
735 snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
736 if (write(id, buf, strlen(buf)) < 0) {
737 MS_DBG_ERR("write() failed errno=%d", errno);
739 return MS_MEDIA_ERR_INTERNAL;
742 return MS_MEDIA_ERR_NONE;