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 /* it's for 32bit file offset */
52 unsigned long int f_bsize;
53 unsigned long int f_frsize;
54 unsigned long int f_blocks;
55 unsigned long int f_bfree;
56 unsigned long int f_bavail;
57 unsigned long int f_files;
58 unsigned long int f_ffree;
59 unsigned long int f_favail;
60 unsigned long int f_fsid;
61 #ifdef _STATVFSBUF_F_UNUSED
64 unsigned long int f_flag;
65 unsigned long int f_namemax;
69 int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
72 int real_size = size - 1;
74 if (!res || !pattern || !str1 || !str2)
75 return MS_MEDIA_ERR_INVALID_PARAMETER;
77 if (real_size < (int)(strlen(str1) + strlen(str2)))
78 return MS_MEDIA_ERR_INVALID_PARAMETER;
80 len = snprintf(res, real_size, pattern, str1, str2);
82 return MS_MEDIA_ERR_INVALID_PARAMETER;
87 return MS_MEDIA_ERR_NONE;
90 bool ms_config_get_int(const char *key, int *value)
95 MS_DBG_ERR("Arguments key or value is NULL");
99 err = vconf_get_int(key, value);
105 MS_DBG_ERR("Unexpected error code: %d", err);
110 bool ms_config_set_int(const char *key, int value)
115 MS_DBG_ERR("Arguments key is NULL");
119 err = vconf_set_int(key, value);
125 MS_DBG_ERR("Unexpected error code: %d", err);
130 bool ms_config_get_str(const char *key, char **value)
134 if (key == NULL || value == NULL) {
135 MS_DBG_ERR("Arguments key or value is NULL");
139 res = vconf_get_str(key);
140 if (MS_STRING_VALID(res)) {
141 *value = strdup(res);
149 bool ms_config_set_str(const char *key, const char *value)
153 if (!key || !value) {
154 MS_DBG_ERR("Arguments key or value is NULL");
158 err = vconf_set_str(key, value);
162 MS_DBG_ERR("fail to vconf_set_str %d", err);
167 bool ms_config_get_bool(const char *key, int *value)
171 if (!key || !value) {
172 MS_DBG_ERR("Arguments key or value is NULL");
176 err = vconf_get_bool(key, value);
182 MS_DBG_ERR("Unexpected error code: %d", err);
187 static int get_memory_size(const char *path, struct statvfs_32 *buf)
192 ret = statvfs(path, &s);
194 MS_DBG_ERR("statvfs failed[%d]", ret);
196 return MS_MEDIA_ERR_INTERNAL;
199 buf->f_bsize = s.f_bsize;
200 buf->f_frsize = s.f_frsize;
201 buf->f_blocks = (unsigned long)s.f_blocks;
202 buf->f_bfree = (unsigned long)s.f_bfree;
203 buf->f_bavail = (unsigned long)s.f_bavail;
204 buf->f_files = (unsigned long)s.f_files;
205 buf->f_ffree = (unsigned long)s.f_ffree;
206 buf->f_favail = (unsigned long)s.f_favail;
207 buf->f_fsid = s.f_fsid;
208 buf->f_flag = s.f_flag;
209 buf->f_namemax = s.f_namemax;
211 return MS_MEDIA_ERR_NONE;
214 int ms_get_remain_space(double *free_space)
216 int ret = MS_MEDIA_ERR_NONE;
217 struct statvfs_32 temp;
219 ret = get_memory_size("/opt", &temp);
220 if (ret != MS_MEDIA_ERR_NONE) {
221 MS_DBG_ERR("fail to get memory size");
225 // MS_DBG_ERR("Total mem : %lf, Avail mem : %lf", (double)temp.f_frsize*temp.f_blocks, (double)temp.f_bsize*temp.f_bavail);
227 *free_space = (double)temp.f_bsize*temp.f_bavail;
232 #ifdef _USE_RECORDED_CONTENT
233 bool ms_is_support_pvr(void)
237 if (system_info_get_value_int(SYSTEM_INFO_KEY_PVR_SUPPORTED, &nSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
238 MS_DBG_ERR("Get PVR Support failed");
242 MS_DBG("PVR Support : [%d]", nSupportPVR);
244 return (nSupportPVR != 0);
248 #ifdef _USE_SENIOR_MODE
249 bool ms_is_support_senior_mode()
251 bool bSupportSeniorMode = false;
253 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
254 MS_DBG_ERR("Get senior mode support failed");
258 MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
260 return bSupportSeniorMode;
264 int ms_check_file_path(const char *file_path, uid_t uid)
268 ms_user_storage_type_e storage_type = -1;
269 int ret = MS_MEDIA_ERR_NONE;
271 if (!MS_STRING_VALID(file_path)) {
272 MS_DBG_ERR("Invalid path");
273 return MS_MEDIA_ERR_INVALID_PARAMETER;
276 /* check location of file */
277 /* file must exists under "/opt/usr/media" or "/opt/storage/sdcard" */
278 ret = ms_user_get_storage_type(uid, file_path, &storage_type);
279 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PATH, "Invalid path");
281 /* check the file exits actually */
282 exist = open(file_path, O_RDONLY);
284 MS_DBG_SERR("open file fail [%s]", file_path);
285 return MS_MEDIA_ERR_INVALID_PATH;
289 /* check type of the path */
290 /* It must be a regular file */
291 memset(&file_st, 0, sizeof(struct stat));
292 if (stat(file_path, &file_st) == 0) {
293 if (!S_ISREG(file_st.st_mode)) {
294 /* In this case, it is not a regula file */
295 MS_DBG_ERR("this path is not a file");
296 return MS_MEDIA_ERR_INVALID_PATH;
299 MS_DBG_STRERROR("stat failed");
300 return MS_MEDIA_ERR_INVALID_PATH;
303 return MS_MEDIA_ERR_NONE;
306 int ms_check_ignore_dir(const char *full_path, uid_t uid)
308 int ret = MS_MEDIA_ERR_NONE;
309 char *dir_path = NULL;
310 char *leaf_path = NULL;
311 char *usr_path = NULL;
313 ret = ms_check_file_path(full_path, uid);
314 if (ret != MS_MEDIA_ERR_NONE) {
315 MS_DBG_ERR("invalid path : %s", full_path);
316 return MS_MEDIA_ERR_INVALID_PATH;
319 dir_path = g_path_get_dirname(full_path);
320 if (dir_path == NULL || strcmp(dir_path, ".") == 0) {
321 MS_DBG_ERR("getting directory path is failed : %s", full_path);
322 MS_SAFE_FREE(dir_path);
323 return MS_MEDIA_ERR_INVALID_PATH;
326 ret = ms_user_get_internal_root_path(uid, &usr_path);
327 if (ret != MS_MEDIA_ERR_NONE) {
328 MS_DBG_ERR("ms_user_get_internal_root_path() fail");
329 MS_SAFE_FREE(dir_path);
330 return MS_MEDIA_ERR_INTERNAL;
334 if (ms_check_scan_ignore(dir_path) != MS_MEDIA_ERR_NONE) {
335 ret = MS_MEDIA_ERR_INVALID_PATH;
339 #ifdef _USE_SENIOR_MODE
340 if (ms_is_support_senior_mode()) {
341 if (strcmp(dir_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)
345 if (strcmp(dir_path, usr_path) == 0)
347 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
349 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(dir_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
351 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(dir_path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0))
354 leaf_path = strrchr(dir_path, '/');
355 if (leaf_path != NULL) {
356 int seek_len = leaf_path -dir_path;
357 dir_path[seek_len] = '\0';
359 MS_DBG_ERR("Fail to find leaf path");
360 ret = MS_MEDIA_ERR_INVALID_PATH;
365 MS_SAFE_FREE(dir_path);
366 MS_SAFE_FREE(usr_path);
371 int ms_check_scan_ignore(char * path)
375 const char *ignore_path = "/.scan_ignore";
376 char *check_ignore_file = NULL;
377 int ret = MS_MEDIA_ERR_NONE;
379 if (strstr(path, "/.")) {
380 MS_DBG_ERR("hidden path");
381 ret = MS_MEDIA_ERR_INVALID_PATH;
385 fd = open(path, O_RDONLY | O_DIRECTORY);
387 MS_DBG_ERR("%s folder opendir fails", path);
388 ret = MS_MEDIA_ERR_INVALID_PATH;
390 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
391 MS_DBG_ERR("Fail to get USB path");
395 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
396 if (errno == ENOENT) {
397 /*if the directory does not exist, check the device is unmounted*/
398 if (!ms_storage_mount_status(path)) {
399 MS_DBG_ERR("Device is unmounted[%s]", path);
400 ret = MS_MEDIA_ERR_USB_UNMOUNTED;
406 struct stat folder_st;
407 if (stat(path, &folder_st) == 0) {
408 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,
409 (long)folder_st.st_uid, (long)folder_st.st_gid, (unsigned long) folder_st.st_mode, path);
411 MS_DBG_ERR("%s folder stat fails", path);
416 /* check the file exits actually */
419 path_len = strlen(path) + strlen(ignore_path) + 1;
420 check_ignore_file = malloc(path_len);
421 if (check_ignore_file != NULL) {
422 memset(check_ignore_file, 0x0, path_len);
423 snprintf(check_ignore_file, path_len, "%s%s", path, ignore_path);
425 exist = open(check_ignore_file, O_RDONLY);
427 MS_DBG_WARN("scan_ignore exists [%s]", check_ignore_file);
428 ret = MS_MEDIA_ERR_INVALID_PATH;
431 MS_SAFE_FREE(check_ignore_file);
433 MS_DBG_ERR("malloc failed");
434 ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
445 if (exist >= 0) close(exist);
450 bool ms_storage_mount_status(const char* start_path)
453 #ifndef _USE_DEVICED_DBUS
456 usb_device_list_h list;
458 char *mount_path = NULL;
460 char *storage_path = NULL;
461 char *remain_path = NULL;
464 remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
465 if (remain_path != NULL)
466 remain_len = strlen(remain_path);
468 storage_path = strndup(start_path, strlen(start_path) - remain_len);
470 MS_DBG_WARN("storage_path [%s]", storage_path);
472 err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
474 count = usb_device_list_get_count(list);
476 err = usb_device_list_get_first(list, &device);
477 if (err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
478 mount_path = usb_device_get_mountpath(device);
479 if (mount_path != NULL) {
480 MS_DBG_WARN("mount_path [%s]", mount_path);
481 if (strlen(mount_path) == strlen(storage_path)) {
482 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
483 MS_DBG_WARN("start path is mounted [%s]", start_path);
491 while (usb_device_list_get_next(list, &device) == 0) {
492 if (device != NULL) {
493 mount_path = usb_device_get_mountpath(device);
494 if (mount_path != NULL) {
495 MS_DBG_WARN("mount_path [%s]", mount_path);
496 if (strlen(mount_path) == strlen(storage_path)) {
497 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
498 MS_DBG_WARN("start path is mounted [%s]", start_path);
509 usb_device_free_device_list(list);
511 MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
514 MS_SAFE_FREE(storage_path);
519 int ms_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type)
521 int res = MS_MEDIA_ERR_NONE;
524 if (status == MS_DB_UPDATING) {
525 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) {
526 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
527 MS_DBG_ERR("ms_config_set_int failed");
530 if (storage_type == MS_STORAGE_EXTERNAL) {
531 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING)) {
532 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
533 MS_DBG_ERR("ms_config_set_int failed");
537 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) {
538 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
539 MS_DBG_ERR("ms_config_set_int failed");
542 if (storage_type == MS_STORAGE_EXTERNAL) {
543 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED)) {
544 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
545 MS_DBG_ERR("ms_config_set_int failed");
550 err = ms_set_power_mode(status);
551 if (err != MS_MEDIA_ERR_NONE) {
552 MS_DBG_ERR("ms_set_power_mode fail");
559 int ms_set_power_mode(ms_db_status_type_t status)
561 int res = MS_MEDIA_ERR_NONE;
566 err = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
568 res = MS_MEDIA_ERR_INTERNAL;
571 err = display_unlock_state(LCD_OFF, PM_RESET_TIMER);
573 res = MS_MEDIA_ERR_INTERNAL;
576 MS_DBG_ERR("Unacceptable type : %d", status);
583 void ms_trim_dir_path(char *dir_path)
585 /* need implementation */
586 /* if dir_path is not NULL terminated, this function will occure crash */
587 int len = strlen(dir_path);
589 if (dir_path[len -1] == '/')
590 dir_path[len -1] = '\0';
593 int ms_check_size_mediadb(uid_t uid, double *db_size)
595 int ret = MS_MEDIA_ERR_NONE;
596 char *db_path = NULL;
599 ret = ms_user_get_media_db_path(uid, &db_path);
601 if (stat(db_path, &buf) == 0) {
602 *db_size = buf.st_size;
604 MS_DBG_STRERROR("stat failed");
605 ret = MS_MEDIA_ERR_INTERNAL;
608 MS_SAFE_FREE(db_path);
613 #ifdef _SET_VIP_PROCESS
614 #define PROC_OOM_SCORE_ADJ_PATH "/proc/%d/oom_score_adj"
615 #define VIP_OOM_SCORE_ADJ (-1000)
616 #define PROC_NAME_MAX 1024
617 #define PROC_BUF_MAX 64
619 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
621 char buf[PROC_BUF_MAX];
622 char cmdline_buf[PROC_NAME_MAX];
626 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
627 fp = fopen(buf, "r");
629 return MS_MEDIA_ERR_INTERNAL;
631 if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
633 return MS_MEDIA_ERR_INTERNAL;
637 filename = strrchr(cmdline_buf, '/');
638 if (filename == NULL)
639 filename = cmdline_buf;
641 filename = filename + 1;
643 SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
645 return MS_MEDIA_ERR_NONE;
648 int ms_set_vip_process(void)
652 static pid_t pid = 0;
653 static char process_name[PROC_NAME_MAX] = {0};
654 static char *appid = NULL;
658 if (ms_get_cmdline_from_proc(pid, process_name)) {
659 MS_DBG_ERR("%s: Read process name failed pid[%d]\n", __func__, pid);
660 return MS_MEDIA_ERR_INTERNAL;
662 appid = process_name;
664 MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
666 if (prctl(PR_GET_DUMPABLE) == 0)
667 prctl(PR_SET_DUMPABLE, 1);
669 snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
670 id = open(buf, O_WRONLY, 0777);
672 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
673 return MS_MEDIA_ERR_INTERNAL;
675 snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
676 if (write(id, buf, strlen(buf)) < 0) {
677 MS_DBG_ERR("write() failed errno=%d", errno);
679 return MS_MEDIA_ERR_INTERNAL;
682 return MS_MEDIA_ERR_NONE;