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.
28 #include <dbus/dbus-glib.h>
29 #include <dbus/dbus.h>
30 #include <dbus/dbus-glib-lowlevel.h>
31 #include <sys/statvfs.h>
33 #include <system_info.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"
43 #define MILLION 1000000L
44 struct timeval g_mmc_start_time;
45 struct timeval g_mmc_end_time;
48 #define MS_DRM_CONTENT_TYPE_LENGTH 100
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;
70 void ms_check_start_time(struct timeval *start_time)
72 gettimeofday(start_time, NULL);
75 void ms_check_end_time(struct timeval *end_time)
77 gettimeofday(end_time, NULL);
80 void ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
85 time.tv_sec = end_time->tv_sec - start_time->tv_sec;
86 time.tv_usec = end_time->tv_usec - start_time->tv_usec;
87 difftime = MILLION * time.tv_sec + time.tv_usec;
88 MS_DBG("The function_to_time took %ld microseconds or %f seconds.", difftime, difftime / (double)MILLION);
92 bool ms_is_mmc_inserted(void)
95 ms_stg_type_e stg_type = MS_STG_TYPE_MMC;
96 GArray *dev_list = NULL;
98 ret = ms_sys_get_device_list(stg_type, &dev_list);
99 if (ret == MS_MEDIA_ERR_NONE) {
100 if (dev_list != NULL) {
101 MS_DBG_ERR("MMC FOUND[%d]", dev_list->len);
102 ms_sys_release_device_list(&dev_list);
105 MS_DBG_ERR("MMC NOT FOUND");
108 MS_DBG_ERR("ms_sys_get_device_list failed");
114 static char* __media_get_path(uid_t uid)
116 char *result_passwd = NULL;
117 struct group *grpinfo = NULL;
119 if (uid == getuid()) {
120 grpinfo = getgrnam("users");
121 if (grpinfo == NULL) {
122 MS_DBG_ERR("getgrnam(users) returns NULL !");
125 if (MS_STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
126 result_passwd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
128 struct passwd *userinfo = getpwuid(uid);
129 if (userinfo == NULL) {
130 MS_DBG_ERR("getpwuid(%d) returns NULL !", uid);
133 grpinfo = getgrnam("users");
134 if (grpinfo == NULL) {
135 MS_DBG_ERR("getgrnam(users) returns NULL !");
138 // Compare git_t type and not group name
139 if (grpinfo->gr_gid != userinfo->pw_gid) {
140 MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
143 result_passwd = strndup(userinfo->pw_dir, strlen(userinfo->pw_dir));
146 return result_passwd;
149 ms_storage_type_t ms_get_storage_type_by_full(const char *path, uid_t uid)
152 int ret = MS_MEDIA_ERR_NONE;
153 char * user_path = NULL;
156 return MS_MEDIA_ERR_INVALID_PATH;
158 #ifdef _USE_SENIOR_MODE
159 if(ms_is_support_senior_mode()) {
160 if (strncmp(path, MEDIA_ROOT_PATH_SENIOR_MODE, strlen(MEDIA_ROOT_PATH_SENIOR_MODE)) == 0) {
161 return MS_STORAGE_EXTERNAL;
166 user_path = __media_get_path(uid);
167 if (user_path == NULL)
168 return MS_MEDIA_ERR_OUT_OF_MEMORY;
170 length_path = strlen(user_path);
172 if (strncmp(path, user_path, length_path) == 0) {
173 ret = MS_STORAGE_INTERNAL;
174 } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
175 ret = MS_STORAGE_EXTERNAL;
176 } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
177 ret = MS_STORAGE_EXTERNAL_USB;
179 ret = MS_MEDIA_ERR_INVALID_PATH;
182 MS_SAFE_FREE(user_path);
187 int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
190 int real_size = size - 1;
192 if (!res || !pattern || !str1 || !str2)
193 return MS_MEDIA_ERR_INVALID_PARAMETER;
195 if (real_size < (int)(strlen(str1) + strlen(str2)))
196 return MS_MEDIA_ERR_INVALID_PARAMETER;
198 len = snprintf(res, real_size, pattern, str1, str2);
200 return MS_MEDIA_ERR_INVALID_PARAMETER;
205 return MS_MEDIA_ERR_NONE;
208 int ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
211 int real_size = size;
213 if (!res || !pattern || !str1) {
214 MS_DBG_ERR("parameta is invalid");
215 return MS_MEDIA_ERR_INVALID_PARAMETER;
218 if (real_size < (int)(strlen(str1))) {
219 MS_DBG_ERR("size is wrong");
220 return MS_MEDIA_ERR_INVALID_PARAMETER;
223 len = snprintf(res, real_size, pattern, str1);
225 MS_DBG_ERR("snprintf failed");
226 return MS_MEDIA_ERR_INVALID_PARAMETER;
231 return MS_MEDIA_ERR_NONE;
234 bool ms_config_get_int(const char *key, int *value)
238 if (!key || !value) {
239 MS_DBG_ERR("Arguments key or value is NULL");
243 err = vconf_get_int(key, value);
249 MS_DBG_ERR("Unexpected error code: %d", err);
254 bool ms_config_set_int(const char *key, int value)
259 MS_DBG_ERR("Arguments key is NULL");
263 err = vconf_set_int(key, value);
269 MS_DBG_ERR("Unexpected error code: %d", err);
274 bool ms_config_get_str(const char *key, char **value)
278 if (key == NULL || value == NULL) {
279 MS_DBG_ERR("Arguments key or value is NULL");
283 res = vconf_get_str(key);
284 if (MS_STRING_VALID(res)) {
285 *value = strdup(res);
293 bool ms_config_set_str(const char *key, const char *value)
297 if (!key || !value) {
298 MS_DBG_ERR("Arguments key or value is NULL");
302 err = vconf_set_str(key, value);
306 MS_DBG_ERR("fail to vconf_set_str %d", err);
311 bool ms_config_get_bool(const char *key, int *value)
315 if (!key || !value) {
316 MS_DBG_ERR("Arguments key or value is NULL");
320 err = vconf_get_bool(key, value);
326 MS_DBG_ERR("Unexpected error code: %d", err);
331 static int get_memory_size(const char *path, struct statvfs_32 *buf)
336 ret = statvfs(path, &s);
338 MS_DBG_ERR("statvfs failed[%d]", ret);
340 return MS_MEDIA_ERR_INTERNAL;
343 buf->f_bsize = s.f_bsize;
344 buf->f_frsize = s.f_frsize;
345 buf->f_blocks = (unsigned long)s.f_blocks;
346 buf->f_bfree = (unsigned long)s.f_bfree;
347 buf->f_bavail = (unsigned long)s.f_bavail;
348 buf->f_files = (unsigned long)s.f_files;
349 buf->f_ffree = (unsigned long)s.f_ffree;
350 buf->f_favail = (unsigned long)s.f_favail;
351 buf->f_fsid = s.f_fsid;
352 buf->f_flag = s.f_flag;
353 buf->f_namemax = s.f_namemax;
355 return MS_MEDIA_ERR_NONE;
358 int ms_get_remain_space(double *free_space)
360 int ret = MS_MEDIA_ERR_NONE;
361 struct statvfs_32 temp;
363 ret = get_memory_size("/opt", &temp);
364 if (ret != MS_MEDIA_ERR_NONE) {
365 MS_DBG_ERR("fail to get memory size");
369 // MS_DBG_ERR("Total mem : %lf, Avail mem : %lf", (double)temp.f_frsize*temp.f_blocks, (double)temp.f_bsize*temp.f_bavail);
371 *free_space = (double)temp.f_bsize*temp.f_bavail;
376 #ifdef _USE_RECORDED_CONTENT
377 bool ms_is_support_pvr(void)
381 if (system_info_get_value_int(SYSTEM_INFO_KEY_PVR_SUPPORTED, &nSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
382 MS_DBG_ERR("Get PVR Support failed");
386 MS_DBG("PVR Support : [%d]", nSupportPVR);
388 return (nSupportPVR != 0);
392 #ifdef _USE_SENIOR_MODE
393 bool ms_is_support_senior_mode()
395 bool bSupportSeniorMode = false;
397 if(system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
398 MS_DBG_ERR("Get senior mode support failed");
402 MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
404 return bSupportSeniorMode;