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>
24 #include <sys/statvfs.h>
29 #include <sys/prctl.h>
30 #include <usb-device.h>
32 #include <system_info.h>
33 #include <device/power.h>
35 #include "media-util.h"
36 #include "media-server-ipc.h"
37 #include "media-common-dbg.h"
38 #include "media-common-system.h"
39 #include "media-common-utils.h"
42 bool ms_config_get_int(const char *key, int *value)
47 MS_DBG_ERR("Arguments key or value is NULL");
51 err = vconf_get_int(key, value);
55 MS_DBG_ERR("Error code: %d", err);
60 bool ms_config_set_int(const char *key, int value)
65 MS_DBG_ERR("Arguments key is NULL");
69 err = vconf_set_int(key, value);
73 MS_DBG_ERR("Error code: %d", err);
78 int ms_get_remain_space(uint64_t *free_space)
80 int ret = MS_MEDIA_ERR_NONE;
81 const char *path = "/opt";
84 ret = statvfs(path, &s);
86 MS_DBG_ERR("statvfs failed[%d]", ret);
88 return MS_MEDIA_ERR_INTERNAL;
91 /* f_bsize:unsigned long, f_bavail:fsblkcnt_t(unsigned long) */
92 *free_space = (uint64_t)s.f_bsize * (uint64_t)s.f_bavail;
94 return MS_MEDIA_ERR_NONE;
97 bool ms_is_support_pvr(void)
99 bool bSupportPVR = false;
100 if (system_info_get_custom_bool("com.samsung/featureconf/pvr.pvr_support", &bSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
101 MS_DBG_ERR("Get PVR Support failed");
105 MS_DBG("PVR Support : [%d]", bSupportPVR);
111 bool ms_is_valid_symlink(const char *path)
113 #ifdef _USE_TVPD_MODE
116 g_autofree char *real_path = realpath(path, NULL);
121 return (g_strcmp0(real_path, MEDIA_SHARE_PATH) == 0);
125 int ms_verify_all_parent_dirs(const char *full_path, uid_t uid)
127 int ret = MS_MEDIA_ERR_NONE;
128 char *dir_path = NULL;
131 ms_user_storage_type_e storage_type = MS_USER_STORAGE_INTERNAL;
133 ret = ms_user_get_storage_type(uid, full_path, &storage_type);
134 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "Invalid path");
136 if (storage_type == MS_USER_STORAGE_INTERNAL) {
137 ret = ms_user_get_internal_root_path(uid, &dir_path);
138 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_internal_root_path() fail");
140 next_pos = strlen(dir_path);
144 next_pos = strlen(MEDIA_ROOT_PATH_USB) + 1;
147 while ((next = strstr(full_path + next_pos, "/"))) {
148 next_pos = (next - full_path);
149 dir_path = g_strndup(full_path, next_pos);
152 ret = ms_check_scan_ignore(dir_path, uid);
154 if (ret != MS_MEDIA_ERR_NONE)
161 int ms_check_scan_ignore(char *path, uid_t uid)
163 int ret = MS_MEDIA_ERR_NONE;
164 const char *ignore_file = ".scan_ignore";
165 char *tmp_path = NULL;
166 char *org_path = NULL;
167 char ignore_path[MS_FILE_PATH_LEN_MAX] = {0, };
169 #ifndef _USE_TVPD_MODE
170 char replace[MS_FILE_PATH_LEN_MAX] = {0, };
171 char *mediashared = NULL;
174 /* Check for symbolic link */
175 tmp_path = realpath(path, NULL);
176 /* Get trimmed path */
177 org_path = g_canonicalize_filename(path, NULL);
179 #ifdef _USE_TVPD_MODE
180 if (g_strcmp0(tmp_path, org_path) != 0) {
181 MS_SAFE_FREE(tmp_path);
183 MS_DBG_ERR("symbolic link(directory)");
184 return MS_MEDIA_ERR_INVALID_PARAMETER;
187 if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
188 ms_user_get_mediashared_path(uid, &mediashared);
189 snprintf(replace, MS_FILE_PATH_LEN_MAX, "%s%s", mediashared, tmp_path + strlen(MEDIA_SHARE_PATH));
190 MS_SAFE_FREE(mediashared);
191 if (g_strcmp0(replace, org_path) != 0) {
192 MS_SAFE_FREE(tmp_path);
194 MS_DBG_ERR("symbolic link(directory)");
195 return MS_MEDIA_ERR_INVALID_PARAMETER;
198 if (g_strcmp0(tmp_path, org_path) != 0) {
199 MS_SAFE_FREE(tmp_path);
201 MS_DBG_ERR("symbolic link(directory)");
202 return MS_MEDIA_ERR_INVALID_PARAMETER;
206 MS_SAFE_FREE(tmp_path);
209 if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
210 snprintf(ignore_path, sizeof(ignore_path), "%s/%s", path, ignore_file);
212 if (g_file_test(ignore_path, G_FILE_TEST_EXISTS)) {
213 MS_DBG_WARN("scan ignore file exist [%s]", ignore_path);
214 return MS_MEDIA_ERR_INVALID_PARAMETER;
217 MS_DBG_ERR("g_file_test fails[%s]", path);
218 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
219 #ifdef _USE_TVPD_MODE
220 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
221 MS_DBG_ERR("Fail to get USB path");
225 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
226 /*if the directory does not exist, check the device is unmounted*/
227 if (!ms_storage_mount_status(path)) {
228 MS_DBG_ERR("Device is unmounted[%s]", path);
229 return MS_MEDIA_ERR_USB_UNMOUNTED;
238 #ifdef _USE_TVPD_MODE
239 typedef struct storage_result {
244 static void __ms_check_mount_status(usb_device_h usb_device, void *user_data)
246 storage_result_s *data = (storage_result_s *)user_data;
247 char *mount_path = NULL;
249 mount_path = usb_device_get_mountpath(usb_device);
253 MS_DBG_SWARN("mount_path [%s]", mount_path);
254 data->result = (g_strcmp0(mount_path, data->storage_path) == 0);
257 bool ms_storage_mount_status(const char *start_path)
260 storage_result_s res = {0, };
261 char *remain_path = NULL;
264 remain_path = strstr(start_path + strlen(MEDIA_ROOT_PATH_USB) + 1, "/");
265 if (remain_path != NULL)
266 remain_len = strlen(remain_path);
268 res.storage_path = g_strndup(start_path, strlen(start_path) - remain_len);
270 MS_DBG_SWARN("storage_path [%s]", res.storage_path);
272 usb_mass_storage_foreach(__ms_check_mount_status, &res);
273 g_free(res.storage_path);
277 MS_DBG_SWARN("start path is mounted [%s]", start_path);
281 int ms_check_size_mediadb(uid_t uid, uint64_t *db_size)
283 int ret = MS_MEDIA_ERR_NONE;
284 char *db_path = NULL;
287 ret = ms_user_get_media_db_path(uid, &db_path);
289 if (stat(db_path, &buf) == 0) {
290 *db_size = (uint64_t)buf.st_size;
292 MS_DBG_STRERROR("stat failed");
293 ret = MS_MEDIA_ERR_INTERNAL;
301 int ms_set_db_status(ms_db_status_type_t status)
303 int ret = MS_MEDIA_ERR_NONE;
305 if (status == MS_DB_UPDATING) {
306 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING))
309 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED))
313 ret = ms_set_power_mode(status);
314 if (ret != MS_MEDIA_ERR_NONE)
315 MS_DBG_ERR("ms_set_power_mode fail");
319 MS_DBG_ERR("ms_config_set_int failed");
320 return MS_MEDIA_ERR_INTERNAL;
324 int ms_set_power_mode(ms_db_status_type_t status)
326 int res = MS_MEDIA_ERR_NONE;
331 err = device_power_request_lock(POWER_LOCK_CPU, 0);
333 res = MS_MEDIA_ERR_INTERNAL;
336 err = device_power_release_lock(POWER_LOCK_CPU);
338 res = MS_MEDIA_ERR_INTERNAL;
341 MS_DBG_ERR("Unacceptable type : %d", status);
348 void ms_trim_dir_path(char *dir_path)
350 /* need implementation */
351 /* if dir_path is not NULL terminated, this function will occure crash */
352 int len = strlen(dir_path);
354 if (dir_path[len -1] == '/')
355 dir_path[len -1] = '\0';
358 #ifdef _USE_TVPD_MODE
359 #define PROC_OOM_SCORE_ADJ_PATH "/proc/%d/oom_score_adj"
360 #define VIP_OOM_SCORE_ADJ (-1000)
361 #define PROC_NAME_MAX 1024
362 #define PROC_BUF_MAX 64
364 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
366 char buf[PROC_BUF_MAX];
367 char cmdline_buf[PROC_NAME_MAX];
371 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
372 fp = fopen(buf, "r");
374 return MS_MEDIA_ERR_INTERNAL;
376 if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
378 return MS_MEDIA_ERR_INTERNAL;
382 filename = strrchr(cmdline_buf, '/');
383 if (filename == NULL)
384 filename = cmdline_buf;
386 filename = filename + 1;
388 SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
390 return MS_MEDIA_ERR_NONE;
393 int ms_set_vip_process(void)
397 static pid_t pid = 0;
398 static char process_name[PROC_NAME_MAX] = {0};
399 static char *appid = NULL;
403 if (ms_get_cmdline_from_proc(pid, process_name)) {
404 MS_DBG_ERR("%s: Read process name failed pid[%d]", __func__, pid);
405 return MS_MEDIA_ERR_INTERNAL;
407 appid = process_name;
409 MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
411 if (prctl(PR_GET_DUMPABLE) == 0)
412 prctl(PR_SET_DUMPABLE, 1);
414 snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
415 id = open(buf, O_WRONLY, 0777);
417 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
418 return MS_MEDIA_ERR_INTERNAL;
420 snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
421 if (write(id, buf, strlen(buf)) < 0) {
422 MS_DBG_ERR("write() failed errno=%d", errno);
424 return MS_MEDIA_ERR_INTERNAL;
427 return MS_MEDIA_ERR_NONE;