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>
28 #include <dbus/dbus-glib.h>
29 #include <dbus/dbus.h>
30 #include <dbus/dbus-glib-lowlevel.h>
31 #include <sys/statvfs.h>
33 #ifdef _SET_VIP_PROCESS
34 #include <sys/prctl.h>
36 #include <system_info.h>
37 #include <dd-display.h>
39 #ifndef _USE_DEVICED_DBUS
40 #include <usb-device.h>
43 #include "media-util.h"
44 #include "media-server-ipc.h"
45 #include "media-common-dbg.h"
46 #include "media-common-system.h"
47 #include "media-common-utils.h"
49 int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
52 int real_size = size - 1;
54 if (!res || !pattern || !str1 || !str2)
55 return MS_MEDIA_ERR_INVALID_PARAMETER;
57 if (real_size < (int)(strlen(str1) + strlen(str2)))
58 return MS_MEDIA_ERR_INVALID_PARAMETER;
60 len = snprintf(res, real_size, pattern, str1, str2);
62 return MS_MEDIA_ERR_INVALID_PARAMETER;
67 return MS_MEDIA_ERR_NONE;
70 bool ms_config_get_int(const char *key, int *value)
75 MS_DBG_ERR("Arguments key or value is NULL");
79 err = vconf_get_int(key, value);
85 MS_DBG_ERR("Unexpected error code: %d", err);
90 bool ms_config_set_int(const char *key, int value)
95 MS_DBG_ERR("Arguments key is NULL");
99 err = vconf_set_int(key, value);
105 MS_DBG_ERR("Unexpected error code: %d", err);
110 bool ms_config_get_str(const char *key, char **value)
114 if (key == NULL || value == NULL) {
115 MS_DBG_ERR("Arguments key or value is NULL");
119 res = vconf_get_str(key);
120 if (MS_STRING_VALID(res)) {
121 *value = strdup(res);
129 int ms_get_remain_space(double *free_space)
131 int ret = MS_MEDIA_ERR_NONE;
132 const char *path = "/opt";
135 ret = statvfs(path, &s);
137 MS_DBG_ERR("statvfs failed[%d]", ret);
139 return MS_MEDIA_ERR_INTERNAL;
142 /* f_bsize:unsigned long, f_bavail:fsblkcnt_t(unsigned long) */
143 *free_space = (double)(s.f_bsize * s.f_bavail);
145 return MS_MEDIA_ERR_NONE;
148 #ifdef _USE_RECORDED_CONTENT
149 bool ms_is_support_pvr(void)
153 if (system_info_get_value_int(SYSTEM_INFO_KEY_PVR_SUPPORTED, &nSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
154 MS_DBG_ERR("Get PVR Support failed");
158 MS_DBG("PVR Support : [%d]", nSupportPVR);
160 return (nSupportPVR != 0);
164 #ifdef _USE_SENIOR_MODE
165 bool ms_is_support_senior_mode()
167 bool bSupportSeniorMode = false;
169 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
170 MS_DBG_ERR("Get senior mode support failed");
174 MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
176 return bSupportSeniorMode;
180 int ms_check_file_path(const char *file_path, uid_t uid)
182 ms_user_storage_type_e storage_type = -1;
183 int ret = MS_MEDIA_ERR_NONE;
185 if (!MS_STRING_VALID(file_path)) {
186 MS_DBG_ERR("Invalid path");
187 return MS_MEDIA_ERR_INVALID_PARAMETER;
190 ret = ms_user_get_storage_type(uid, file_path, &storage_type);
191 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PATH, "Invalid path");
193 if (!g_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
194 MS_DBG_SERR("g_file_test fail [%s]", file_path);
195 return MS_MEDIA_ERR_INVALID_PATH;
198 return MS_MEDIA_ERR_NONE;
201 int ms_check_ignore_dir(const char *full_path, uid_t uid)
203 int ret = MS_MEDIA_ERR_NONE;
204 char *dir_path = NULL;
205 char *leaf_path = NULL;
206 char *usr_path = NULL;
208 ret = ms_check_file_path(full_path, uid);
209 if (ret != MS_MEDIA_ERR_NONE) {
210 MS_DBG_ERR("invalid path : %s", full_path);
211 return MS_MEDIA_ERR_INVALID_PATH;
214 dir_path = g_path_get_dirname(full_path);
215 if (dir_path == NULL || strcmp(dir_path, ".") == 0) {
216 MS_DBG_ERR("getting directory path is failed : %s", full_path);
217 MS_SAFE_FREE(dir_path);
218 return MS_MEDIA_ERR_INVALID_PATH;
221 ret = ms_user_get_internal_root_path(uid, &usr_path);
222 if (ret != MS_MEDIA_ERR_NONE) {
223 MS_DBG_ERR("ms_user_get_internal_root_path() fail");
224 MS_SAFE_FREE(dir_path);
225 return MS_MEDIA_ERR_INTERNAL;
229 if (ms_check_scan_ignore(dir_path) != MS_MEDIA_ERR_NONE) {
230 ret = MS_MEDIA_ERR_INVALID_PATH;
234 #ifdef _USE_SENIOR_MODE
235 if (ms_is_support_senior_mode()) {
236 if (strcmp(dir_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)
240 if (strcmp(dir_path, usr_path) == 0)
242 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
244 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(dir_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
246 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(dir_path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0))
249 leaf_path = strrchr(dir_path, '/');
250 if (leaf_path != NULL) {
251 int seek_len = leaf_path -dir_path;
252 dir_path[seek_len] = '\0';
254 MS_DBG_ERR("Fail to find leaf path");
255 ret = MS_MEDIA_ERR_INVALID_PATH;
260 MS_SAFE_FREE(dir_path);
261 MS_SAFE_FREE(usr_path);
266 int ms_check_scan_ignore(char * path)
268 int ret = MS_MEDIA_ERR_NONE;
269 const char *ignore_file = ".scan_ignore";
270 char ignore_path[MS_FILE_PATH_LEN_MAX] = {0, };
272 if (strstr(path, "/.")) {
273 MS_DBG_ERR("hidden path");
274 return MS_MEDIA_ERR_INVALID_PATH;
277 if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
278 memset(ignore_path, 0, sizeof(ignore_path));
279 snprintf(ignore_path, sizeof(ignore_path), "%s/%s", path, ignore_file);
281 if (g_file_test(ignore_path, G_FILE_TEST_EXISTS)) {
282 MS_DBG_WARN("scan ignore file exist [%s]", ignore_path);
283 return MS_MEDIA_ERR_INVALID_PATH;
286 MS_DBG_ERR("g_file_test fails[%s]", path);
287 ret = MS_MEDIA_ERR_INVALID_PATH;
289 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
290 MS_DBG_ERR("Fail to get USB path");
294 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
295 /*if the directory does not exist, check the device is unmounted*/
296 if (!ms_storage_mount_status(path)) {
297 MS_DBG_ERR("Device is unmounted[%s]", path);
298 return MS_MEDIA_ERR_USB_UNMOUNTED;
306 bool ms_storage_mount_status(const char* start_path)
309 #ifndef _USE_DEVICED_DBUS
312 usb_device_list_h list;
314 char *mount_path = NULL;
316 char *storage_path = NULL;
317 char *remain_path = NULL;
320 remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
321 if (remain_path != NULL)
322 remain_len = strlen(remain_path);
324 storage_path = strndup(start_path, strlen(start_path) - remain_len);
326 MS_DBG_WARN("storage_path [%s]", storage_path);
328 err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
330 count = usb_device_list_get_count(list);
332 err = usb_device_list_get_first(list, &device);
333 if (err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
334 mount_path = usb_device_get_mountpath(device);
335 if (mount_path != NULL) {
336 MS_DBG_WARN("mount_path [%s]", mount_path);
337 if (strlen(mount_path) == strlen(storage_path)) {
338 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
339 MS_DBG_WARN("start path is mounted [%s]", start_path);
347 while (usb_device_list_get_next(list, &device) == 0) {
348 if (device != NULL) {
349 mount_path = usb_device_get_mountpath(device);
350 if (mount_path != NULL) {
351 MS_DBG_WARN("mount_path [%s]", mount_path);
352 if (strlen(mount_path) == strlen(storage_path)) {
353 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
354 MS_DBG_WARN("start path is mounted [%s]", start_path);
365 usb_device_free_device_list(list);
367 MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
370 MS_SAFE_FREE(storage_path);
375 int ms_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type)
377 int res = MS_MEDIA_ERR_NONE;
380 if (status == MS_DB_UPDATING) {
381 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) {
382 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
383 MS_DBG_ERR("ms_config_set_int failed");
386 if (storage_type == MS_STORAGE_EXTERNAL) {
387 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING)) {
388 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
389 MS_DBG_ERR("ms_config_set_int failed");
393 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) {
394 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
395 MS_DBG_ERR("ms_config_set_int failed");
398 if (storage_type == MS_STORAGE_EXTERNAL) {
399 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED)) {
400 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
401 MS_DBG_ERR("ms_config_set_int failed");
406 err = ms_set_power_mode(status);
407 if (err != MS_MEDIA_ERR_NONE) {
408 MS_DBG_ERR("ms_set_power_mode fail");
415 int ms_set_power_mode(ms_db_status_type_t status)
417 int res = MS_MEDIA_ERR_NONE;
422 err = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
424 res = MS_MEDIA_ERR_INTERNAL;
427 err = display_unlock_state(LCD_OFF, PM_RESET_TIMER);
429 res = MS_MEDIA_ERR_INTERNAL;
432 MS_DBG_ERR("Unacceptable type : %d", status);
439 void ms_trim_dir_path(char *dir_path)
441 /* need implementation */
442 /* if dir_path is not NULL terminated, this function will occure crash */
443 int len = strlen(dir_path);
445 if (dir_path[len -1] == '/')
446 dir_path[len -1] = '\0';
449 int ms_check_size_mediadb(uid_t uid, double *db_size)
451 int ret = MS_MEDIA_ERR_NONE;
452 char *db_path = NULL;
455 ret = ms_user_get_media_db_path(uid, &db_path);
457 if (stat(db_path, &buf) == 0) {
458 *db_size = buf.st_size;
460 MS_DBG_STRERROR("stat failed");
461 ret = MS_MEDIA_ERR_INTERNAL;
464 MS_SAFE_FREE(db_path);
469 #ifdef _SET_VIP_PROCESS
470 #define PROC_OOM_SCORE_ADJ_PATH "/proc/%d/oom_score_adj"
471 #define VIP_OOM_SCORE_ADJ (-1000)
472 #define PROC_NAME_MAX 1024
473 #define PROC_BUF_MAX 64
475 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
477 char buf[PROC_BUF_MAX];
478 char cmdline_buf[PROC_NAME_MAX];
482 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
483 fp = fopen(buf, "r");
485 return MS_MEDIA_ERR_INTERNAL;
487 if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
489 return MS_MEDIA_ERR_INTERNAL;
493 filename = strrchr(cmdline_buf, '/');
494 if (filename == NULL)
495 filename = cmdline_buf;
497 filename = filename + 1;
499 SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
501 return MS_MEDIA_ERR_NONE;
504 int ms_set_vip_process(void)
508 static pid_t pid = 0;
509 static char process_name[PROC_NAME_MAX] = {0};
510 static char *appid = NULL;
514 if (ms_get_cmdline_from_proc(pid, process_name)) {
515 MS_DBG_ERR("%s: Read process name failed pid[%d]\n", __func__, pid);
516 return MS_MEDIA_ERR_INTERNAL;
518 appid = process_name;
520 MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
522 if (prctl(PR_GET_DUMPABLE) == 0)
523 prctl(PR_SET_DUMPABLE, 1);
525 snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
526 id = open(buf, O_WRONLY, 0777);
528 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
529 return MS_MEDIA_ERR_INTERNAL;
531 snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
532 if (write(id, buf, strlen(buf)) < 0) {
533 MS_DBG_ERR("write() failed errno=%d", errno);
535 return MS_MEDIA_ERR_INTERNAL;
538 return MS_MEDIA_ERR_NONE;