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 <dd-display.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);
79 MS_DBG_ERR("Unexpected error code: %d", err);
84 bool ms_config_set_int(const char *key, int value)
89 MS_DBG_ERR("Arguments key is NULL");
93 err = vconf_set_int(key, value);
99 MS_DBG_ERR("Unexpected error code: %d", err);
104 bool ms_config_get_str(const char *key, char **value)
108 if (key == NULL || value == NULL) {
109 MS_DBG_ERR("Arguments key or value is NULL");
113 res = vconf_get_str(key);
114 if (MS_STRING_VALID(res)) {
115 *value = strdup(res);
123 int ms_get_remain_space(double *free_space)
125 int ret = MS_MEDIA_ERR_NONE;
126 const char *path = "/opt";
129 ret = statvfs(path, &s);
131 MS_DBG_ERR("statvfs failed[%d]", ret);
133 return MS_MEDIA_ERR_INTERNAL;
136 /* f_bsize:unsigned long, f_bavail:fsblkcnt_t(unsigned long) */
137 *free_space = (double)(s.f_bsize * s.f_bavail);
139 return MS_MEDIA_ERR_NONE;
142 #ifdef _USE_RECORDED_CONTENT
143 bool ms_is_support_pvr(void)
147 if (system_info_get_value_int(SYSTEM_INFO_KEY_PVR_SUPPORTED, &nSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
148 MS_DBG_ERR("Get PVR Support failed");
152 MS_DBG("PVR Support : [%d]", nSupportPVR);
154 return (nSupportPVR != 0);
158 #ifdef _USE_SENIOR_MODE
159 bool ms_is_support_senior_mode()
161 bool bSupportSeniorMode = false;
163 if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
164 MS_DBG_ERR("Get senior mode support failed");
168 MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
170 return bSupportSeniorMode;
174 int ms_check_file_path(const char *file_path, uid_t uid)
176 ms_user_storage_type_e storage_type = -1;
177 int ret = MS_MEDIA_ERR_NONE;
179 if (!MS_STRING_VALID(file_path)) {
180 MS_DBG_ERR("Invalid path");
181 return MS_MEDIA_ERR_INVALID_PARAMETER;
184 ret = ms_user_get_storage_type(uid, file_path, &storage_type);
185 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PATH, "Invalid path");
187 if (!g_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
188 MS_DBG_SERR("g_file_test fail [%s]", file_path);
189 return MS_MEDIA_ERR_INVALID_PATH;
192 return MS_MEDIA_ERR_NONE;
195 int ms_check_ignore_dir(const char *full_path, uid_t uid)
197 int ret = MS_MEDIA_ERR_NONE;
198 char *dir_path = NULL;
199 char *leaf_path = NULL;
200 char *usr_path = NULL;
202 ret = ms_check_file_path(full_path, uid);
203 if (ret != MS_MEDIA_ERR_NONE) {
204 MS_DBG_ERR("invalid path : %s", full_path);
205 return MS_MEDIA_ERR_INVALID_PATH;
208 dir_path = g_path_get_dirname(full_path);
209 if (dir_path == NULL || strcmp(dir_path, ".") == 0) {
210 MS_DBG_ERR("getting directory path is failed : %s", full_path);
211 MS_SAFE_FREE(dir_path);
212 return MS_MEDIA_ERR_INVALID_PATH;
215 ret = ms_user_get_internal_root_path(uid, &usr_path);
216 if (ret != MS_MEDIA_ERR_NONE) {
217 MS_DBG_ERR("ms_user_get_internal_root_path() fail");
218 MS_SAFE_FREE(dir_path);
219 return MS_MEDIA_ERR_INTERNAL;
223 if (ms_check_scan_ignore(dir_path) != MS_MEDIA_ERR_NONE) {
224 ret = MS_MEDIA_ERR_INVALID_PATH;
228 #ifdef _USE_SENIOR_MODE
229 if (ms_is_support_senior_mode()) {
230 if (strcmp(dir_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)
234 if (strcmp(dir_path, usr_path) == 0)
236 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
238 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(dir_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
240 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(dir_path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0))
243 leaf_path = strrchr(dir_path, '/');
244 if (leaf_path != NULL) {
245 int seek_len = leaf_path -dir_path;
246 dir_path[seek_len] = '\0';
248 MS_DBG_ERR("Fail to find leaf path");
249 ret = MS_MEDIA_ERR_INVALID_PATH;
254 MS_SAFE_FREE(dir_path);
255 MS_SAFE_FREE(usr_path);
260 int ms_check_scan_ignore(char * path)
262 int ret = MS_MEDIA_ERR_NONE;
263 const char *ignore_file = ".scan_ignore";
264 char ignore_path[MS_FILE_PATH_LEN_MAX] = {0, };
266 if (strstr(path, "/.")) {
267 MS_DBG_ERR("hidden path");
268 return MS_MEDIA_ERR_INVALID_PATH;
271 if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
272 memset(ignore_path, 0, sizeof(ignore_path));
273 snprintf(ignore_path, sizeof(ignore_path), "%s/%s", path, ignore_file);
275 if (g_file_test(ignore_path, G_FILE_TEST_EXISTS)) {
276 MS_DBG_WARN("scan ignore file exist [%s]", ignore_path);
277 return MS_MEDIA_ERR_INVALID_PATH;
280 MS_DBG_ERR("g_file_test fails[%s]", path);
281 ret = MS_MEDIA_ERR_INVALID_PATH;
283 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
284 MS_DBG_ERR("Fail to get USB path");
288 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
289 /*if the directory does not exist, check the device is unmounted*/
290 if (!ms_storage_mount_status(path)) {
291 MS_DBG_ERR("Device is unmounted[%s]", path);
292 return MS_MEDIA_ERR_USB_UNMOUNTED;
300 bool ms_storage_mount_status(const char* start_path)
303 #ifndef _USE_DEVICED_DBUS
306 usb_device_list_h list;
308 char *mount_path = NULL;
310 char *storage_path = NULL;
311 char *remain_path = NULL;
314 remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
315 if (remain_path != NULL)
316 remain_len = strlen(remain_path);
318 storage_path = strndup(start_path, strlen(start_path) - remain_len);
320 MS_DBG_WARN("storage_path [%s]", storage_path);
322 err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
324 count = usb_device_list_get_count(list);
326 err = usb_device_list_get_first(list, &device);
327 if (err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
328 mount_path = usb_device_get_mountpath(device);
329 if (mount_path != NULL) {
330 MS_DBG_WARN("mount_path [%s]", mount_path);
331 if (strlen(mount_path) == strlen(storage_path)) {
332 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
333 MS_DBG_WARN("start path is mounted [%s]", start_path);
341 while (usb_device_list_get_next(list, &device) == 0) {
342 if (device != NULL) {
343 mount_path = usb_device_get_mountpath(device);
344 if (mount_path != NULL) {
345 MS_DBG_WARN("mount_path [%s]", mount_path);
346 if (strlen(mount_path) == strlen(storage_path)) {
347 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
348 MS_DBG_WARN("start path is mounted [%s]", start_path);
359 usb_device_free_device_list(list);
361 MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
364 MS_SAFE_FREE(storage_path);
369 int ms_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type)
371 int res = MS_MEDIA_ERR_NONE;
374 if (status == MS_DB_UPDATING) {
375 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) {
376 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
377 MS_DBG_ERR("ms_config_set_int failed");
380 if (storage_type == MS_STORAGE_EXTERNAL) {
381 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING)) {
382 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
383 MS_DBG_ERR("ms_config_set_int failed");
387 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) {
388 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
389 MS_DBG_ERR("ms_config_set_int failed");
392 if (storage_type == MS_STORAGE_EXTERNAL) {
393 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED)) {
394 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
395 MS_DBG_ERR("ms_config_set_int failed");
400 err = ms_set_power_mode(status);
401 if (err != MS_MEDIA_ERR_NONE) {
402 MS_DBG_ERR("ms_set_power_mode fail");
409 int ms_set_power_mode(ms_db_status_type_t status)
411 int res = MS_MEDIA_ERR_NONE;
416 err = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
418 res = MS_MEDIA_ERR_INTERNAL;
421 err = display_unlock_state(LCD_OFF, PM_RESET_TIMER);
423 res = MS_MEDIA_ERR_INTERNAL;
426 MS_DBG_ERR("Unacceptable type : %d", status);
433 void ms_trim_dir_path(char *dir_path)
435 /* need implementation */
436 /* if dir_path is not NULL terminated, this function will occure crash */
437 int len = strlen(dir_path);
439 if (dir_path[len -1] == '/')
440 dir_path[len -1] = '\0';
443 int ms_check_size_mediadb(uid_t uid, double *db_size)
445 int ret = MS_MEDIA_ERR_NONE;
446 char *db_path = NULL;
449 ret = ms_user_get_media_db_path(uid, &db_path);
451 if (stat(db_path, &buf) == 0) {
452 *db_size = buf.st_size;
454 MS_DBG_STRERROR("stat failed");
455 ret = MS_MEDIA_ERR_INTERNAL;
458 MS_SAFE_FREE(db_path);
463 #ifdef _SET_VIP_PROCESS
464 #define PROC_OOM_SCORE_ADJ_PATH "/proc/%d/oom_score_adj"
465 #define VIP_OOM_SCORE_ADJ (-1000)
466 #define PROC_NAME_MAX 1024
467 #define PROC_BUF_MAX 64
469 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
471 char buf[PROC_BUF_MAX];
472 char cmdline_buf[PROC_NAME_MAX];
476 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
477 fp = fopen(buf, "r");
479 return MS_MEDIA_ERR_INTERNAL;
481 if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
483 return MS_MEDIA_ERR_INTERNAL;
487 filename = strrchr(cmdline_buf, '/');
488 if (filename == NULL)
489 filename = cmdline_buf;
491 filename = filename + 1;
493 SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
495 return MS_MEDIA_ERR_NONE;
498 int ms_set_vip_process(void)
502 static pid_t pid = 0;
503 static char process_name[PROC_NAME_MAX] = {0};
504 static char *appid = NULL;
508 if (ms_get_cmdline_from_proc(pid, process_name)) {
509 MS_DBG_ERR("%s: Read process name failed pid[%d]", __func__, pid);
510 return MS_MEDIA_ERR_INTERNAL;
512 appid = process_name;
514 MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
516 if (prctl(PR_GET_DUMPABLE) == 0)
517 prctl(PR_SET_DUMPABLE, 1);
519 snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
520 id = open(buf, O_WRONLY, 0777);
522 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
523 return MS_MEDIA_ERR_INTERNAL;
525 snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
526 if (write(id, buf, strlen(buf)) < 0) {
527 MS_DBG_ERR("write() failed errno=%d", errno);
529 return MS_MEDIA_ERR_INTERNAL;
532 return MS_MEDIA_ERR_NONE;