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>
29 #include <sys/prctl.h>
31 #include <system_info.h>
32 #include <device/power.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 bool ms_config_get_int(const char *key, int *value)
49 MS_DBG_ERR("Arguments key or value is NULL");
53 err = vconf_get_int(key, value);
57 MS_DBG_ERR("Error code: %d", err);
62 bool ms_config_set_int(const char *key, int value)
67 MS_DBG_ERR("Arguments key is NULL");
71 err = vconf_set_int(key, value);
75 MS_DBG_ERR("Error code: %d", err);
80 bool ms_config_get_str(const char *key, char **value)
84 if (key == NULL || value == NULL) {
85 MS_DBG_ERR("Arguments key or value is NULL");
89 res = vconf_get_str(key);
90 if (MS_STRING_VALID(res)) {
91 *value = g_strdup(res);
99 int ms_get_remain_space(double *free_space)
101 int ret = MS_MEDIA_ERR_NONE;
102 const char *path = "/opt";
105 ret = statvfs(path, &s);
107 MS_DBG_ERR("statvfs failed[%d]", ret);
109 return MS_MEDIA_ERR_INTERNAL;
112 /* f_bsize:unsigned long, f_bavail:fsblkcnt_t(unsigned long) */
113 *free_space = (double)(s.f_bsize * s.f_bavail);
115 return MS_MEDIA_ERR_NONE;
118 #ifdef _USE_TVPD_MODE
119 bool ms_is_support_pvr(void)
121 bool bSupportPVR = false;
122 if (system_info_get_custom_bool("com.samsung/featureconf/pvr.pvr_support", &bSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
123 MS_DBG_ERR("Get PVR Support failed");
127 MS_DBG("PVR Support : [%d]", bSupportPVR);
133 int ms_check_file_path(const char *file_path, uid_t uid)
135 ms_user_storage_type_e storage_type = -1;
136 int ret = MS_MEDIA_ERR_NONE;
138 if (!MS_STRING_VALID(file_path)) {
139 MS_DBG_ERR("Invalid path");
140 return MS_MEDIA_ERR_INVALID_PARAMETER;
143 ret = ms_user_get_storage_type(uid, file_path, &storage_type);
144 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
146 if (!g_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
147 MS_DBG_SERR("g_file_test fail [%s]", file_path);
148 return MS_MEDIA_ERR_INVALID_PARAMETER;
151 return MS_MEDIA_ERR_NONE;
154 int ms_check_ignore_dir(const char *full_path, uid_t uid)
156 int ret = MS_MEDIA_ERR_NONE;
157 char *dir_path = NULL;
161 ret = ms_check_file_path(full_path, uid);
162 MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
164 if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && !strncmp(full_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB))) {
165 next_pos = strlen(MEDIA_ROOT_PATH_USB) + 1;
167 ret = ms_user_get_internal_root_path(uid, &dir_path);
168 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_internal_root_path() fail");
170 next_pos = strlen(dir_path);
175 while ((next = strstr(full_path + next_pos, "/"))) {
176 next_pos = (next - full_path);
177 dir_path = g_strndup(full_path, next_pos);
180 ret = ms_check_scan_ignore(dir_path, uid);
182 if (ret != MS_MEDIA_ERR_NONE)
189 static void __ms_trim_path(const char *input_path, char **output_path)
191 char buf[4096] = {0, };
192 char tmp[4096] = {0, };
195 SAFE_STRLCPY(buf, input_path, sizeof(buf));
197 while ((pos = strstr(buf, "//")) != NULL) {
198 memset(tmp, 0, sizeof(tmp));
199 SAFE_STRLCPY(tmp, buf, pos - buf + 1);
200 SAFE_STRLCAT(tmp, pos + 1, sizeof(tmp));
202 memset(buf, 0, sizeof(buf));
203 SAFE_STRLCPY(buf, tmp, sizeof(buf));
206 if (g_str_has_suffix(buf, "/"))
207 *output_path = g_strndup(buf, strlen(buf) - 1);
209 *output_path = g_strdup(buf);
212 int ms_check_scan_ignore(char * path, uid_t uid)
214 int ret = MS_MEDIA_ERR_NONE;
215 const char *ignore_file = ".scan_ignore";
216 char *tmp_path = NULL;
217 char *org_path = NULL;
218 char ignore_path[MS_FILE_PATH_LEN_MAX] = {0, };
220 #ifndef _USE_TVPD_MODE
221 char replace[MS_FILE_PATH_LEN_MAX] = {0, };
222 char *mediashared = NULL;
225 /* Check for symbolic link */
226 tmp_path = realpath(path, NULL);
227 /* Get trimmed path */
228 __ms_trim_path(path, &org_path);
230 #ifdef _USE_TVPD_MODE
231 if (g_strcmp0(tmp_path, org_path) != 0) {
232 MS_SAFE_FREE(tmp_path);
234 MS_DBG_ERR("symbolic link(directory)");
235 return MS_MEDIA_ERR_INVALID_PARAMETER;
238 if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
239 ms_user_get_mediashared_path(uid, &mediashared);
240 snprintf(replace, MS_FILE_PATH_LEN_MAX, "%s%s", mediashared, tmp_path + strlen(MEDIA_SHARE_PATH));
241 MS_SAFE_FREE(mediashared);
242 if (g_strcmp0(replace, org_path) != 0) {
243 MS_SAFE_FREE(tmp_path);
245 MS_DBG_ERR("symbolic link(directory)");
246 return MS_MEDIA_ERR_INVALID_PARAMETER;
249 if (g_strcmp0(tmp_path, org_path) != 0) {
250 MS_SAFE_FREE(tmp_path);
252 MS_DBG_ERR("symbolic link(directory)");
253 return MS_MEDIA_ERR_INVALID_PARAMETER;
257 MS_SAFE_FREE(tmp_path);
260 if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
261 snprintf(ignore_path, sizeof(ignore_path), "%s/%s", path, ignore_file);
263 if (g_file_test(ignore_path, G_FILE_TEST_EXISTS)) {
264 MS_DBG_WARN("scan ignore file exist [%s]", ignore_path);
265 return MS_MEDIA_ERR_INVALID_PARAMETER;
268 MS_DBG_ERR("g_file_test fails[%s]", path);
269 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
271 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
272 MS_DBG_ERR("Fail to get USB path");
276 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
277 /*if the directory does not exist, check the device is unmounted*/
278 if (!ms_storage_mount_status(path)) {
279 MS_DBG_ERR("Device is unmounted[%s]", path);
280 return MS_MEDIA_ERR_USB_UNMOUNTED;
288 #ifndef _USE_DEVICED_DBUS
290 typedef struct storage_result {
295 static void __ms_check_mount_status(usb_device_h usb_device, void *user_data)
297 storage_result_s *data = (storage_result_s *)user_data;
298 char *mount_path = NULL;
300 mount_path = usb_device_get_mountpath(usb_device);
304 MS_DBG_SWARN("mount_path [%s]", mount_path);
305 data->result = (g_strcmp0(mount_path, data->storage_path) == 0);
309 bool ms_storage_mount_status(const char *start_path)
312 #ifndef _USE_DEVICED_DBUS
313 storage_result_s res = {0, };
314 char *remain_path = NULL;
317 remain_path = strstr(start_path + strlen(MEDIA_ROOT_PATH_USB) + 1, "/");
318 if (remain_path != NULL)
319 remain_len = strlen(remain_path);
321 res.storage_path = g_strndup(start_path, strlen(start_path) - remain_len);
323 MS_DBG_SWARN("storage_path [%s]", res.storage_path);
325 usb_mass_storage_foreach(__ms_check_mount_status, &res);
326 g_free(res.storage_path);
330 MS_DBG_SWARN("start path is mounted [%s]", start_path);
335 int ms_set_db_status(ms_db_status_type_t status, ms_user_storage_type_e storage_type)
337 int ret = MS_MEDIA_ERR_NONE;
339 if (status == MS_DB_UPDATING) {
340 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING))
343 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
344 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING))
348 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED))
351 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
352 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED))
357 ret = ms_set_power_mode(status);
358 if (ret != MS_MEDIA_ERR_NONE)
359 MS_DBG_ERR("ms_set_power_mode fail");
363 MS_DBG_ERR("ms_config_set_int failed");
364 return MS_MEDIA_ERR_INTERNAL;
367 int ms_set_power_mode(ms_db_status_type_t status)
369 int res = MS_MEDIA_ERR_NONE;
374 err = device_power_request_lock(POWER_LOCK_CPU, 0);
376 res = MS_MEDIA_ERR_INTERNAL;
379 err = device_power_release_lock(POWER_LOCK_CPU);
381 res = MS_MEDIA_ERR_INTERNAL;
384 MS_DBG_ERR("Unacceptable type : %d", status);
391 void ms_trim_dir_path(char *dir_path)
393 /* need implementation */
394 /* if dir_path is not NULL terminated, this function will occure crash */
395 int len = strlen(dir_path);
397 if (dir_path[len -1] == '/')
398 dir_path[len -1] = '\0';
401 int ms_check_size_mediadb(uid_t uid, double *db_size)
403 int ret = MS_MEDIA_ERR_NONE;
404 char *db_path = NULL;
407 ret = ms_user_get_media_db_path(uid, &db_path);
409 if (stat(db_path, &buf) == 0) {
410 *db_size = buf.st_size;
412 MS_DBG_STRERROR("stat failed");
413 ret = MS_MEDIA_ERR_INTERNAL;
421 #ifdef _USE_TVPD_MODE
422 #define PROC_OOM_SCORE_ADJ_PATH "/proc/%d/oom_score_adj"
423 #define VIP_OOM_SCORE_ADJ (-1000)
424 #define PROC_NAME_MAX 1024
425 #define PROC_BUF_MAX 64
427 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
429 char buf[PROC_BUF_MAX];
430 char cmdline_buf[PROC_NAME_MAX];
434 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
435 fp = fopen(buf, "r");
437 return MS_MEDIA_ERR_INTERNAL;
439 if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
441 return MS_MEDIA_ERR_INTERNAL;
445 filename = strrchr(cmdline_buf, '/');
446 if (filename == NULL)
447 filename = cmdline_buf;
449 filename = filename + 1;
451 SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
453 return MS_MEDIA_ERR_NONE;
456 int ms_set_vip_process(void)
460 static pid_t pid = 0;
461 static char process_name[PROC_NAME_MAX] = {0};
462 static char *appid = NULL;
466 if (ms_get_cmdline_from_proc(pid, process_name)) {
467 MS_DBG_ERR("%s: Read process name failed pid[%d]", __func__, pid);
468 return MS_MEDIA_ERR_INTERNAL;
470 appid = process_name;
472 MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
474 if (prctl(PR_GET_DUMPABLE) == 0)
475 prctl(PR_SET_DUMPABLE, 1);
477 snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
478 id = open(buf, O_WRONLY, 0777);
480 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
481 return MS_MEDIA_ERR_INTERNAL;
483 snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
484 if (write(id, buf, strlen(buf)) < 0) {
485 MS_DBG_ERR("write() failed errno=%d", errno);
487 return MS_MEDIA_ERR_INTERNAL;
490 return MS_MEDIA_ERR_NONE;