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>
32 #include "media-util.h"
33 #include "media-server-ipc.h"
34 #include "media-common-dbg.h"
35 #include "media-common-system.h"
36 #include "media-common-utils.h"
40 #define MILLION 1000000L
41 struct timeval g_mmc_start_time;
42 struct timeval g_mmc_end_time;
45 #define MS_DRM_CONTENT_TYPE_LENGTH 100
48 void ms_check_start_time(struct timeval *start_time)
50 gettimeofday(start_time, NULL);
53 void ms_check_end_time(struct timeval *end_time)
55 gettimeofday(end_time, NULL);
58 void ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
63 time.tv_sec = end_time->tv_sec - start_time->tv_sec;
64 time.tv_usec = end_time->tv_usec - start_time->tv_usec;
65 difftime = MILLION * time.tv_sec + time.tv_usec;
66 MS_DBG("The function_to_time took %ld microseconds or %f seconds.",
67 difftime, difftime / (double)MILLION);
71 bool ms_is_mmc_inserted(void)
74 ms_stg_type_e stg_type = MS_STG_TYPE_MMC;
75 GArray *dev_list = NULL;
77 ret = ms_sys_get_device_list(stg_type, &dev_list);
78 if (ret == MS_MEDIA_ERR_NONE) {
79 if (dev_list != NULL) {
80 MS_DBG_ERR("MMC FOUND[%d]", dev_list->len);
81 ms_sys_release_device_list(&dev_list);
84 MS_DBG_ERR("MMC NOT FOUND");
87 MS_DBG_ERR("ms_sys_get_device_list failed");
93 static char* __media_get_path(uid_t uid)
95 char *result_psswd = NULL;
96 struct group *grpinfo = NULL;
101 result_psswd = strdup(MEDIA_ROOT_PATH_INTERNAL);
102 grpinfo = getgrnam("users");
103 if(grpinfo == NULL) {
104 MS_DBG_ERR("getgrnam(users) returns NULL !");
105 MS_SAFE_FREE(result_psswd);
111 struct passwd *userinfo = getpwuid(uid);
112 if(userinfo == NULL) {
113 MS_DBG_ERR("getpwuid(%d) returns NULL !", uid);
116 grpinfo = getgrnam("users");
117 if(grpinfo == NULL) {
118 MS_DBG_ERR("getgrnam(users) returns NULL !");
121 // Compare git_t type and not group name
122 if (grpinfo->gr_gid != userinfo->pw_gid) {
123 MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
126 err = asprintf(&result_psswd, "%s", userinfo->pw_dir);
128 MS_DBG_ERR("asprintf failed [%d]", err);
136 ms_storage_type_t ms_get_storage_type_by_full(const char *path, uid_t uid)
139 char * user_path = NULL;
144 user_path = __media_get_path(uid);
145 length_path = strlen(user_path);
147 if (strncmp(path, user_path, length_path) == 0) {
148 return MS_STORAGE_INTERNAL;
149 } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
150 return MS_STORAGE_EXTERNAL;
153 return MS_MEDIA_ERR_INVALID_PATH;
157 int ms_strappend(char *res, const int size, const char *pattern,
158 const char *str1, const char *str2)
161 int real_size = size - 1;
163 if (!res ||!pattern || !str1 ||!str2 )
164 return MS_MEDIA_ERR_INVALID_PARAMETER;
166 if (real_size < (int)(strlen(str1) + strlen(str2)))
167 return MS_MEDIA_ERR_INVALID_PARAMETER;
169 len = snprintf(res, real_size, pattern, str1, str2);
171 return MS_MEDIA_ERR_INVALID_PARAMETER;
176 return MS_MEDIA_ERR_NONE;
179 int ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
182 int real_size = size;
184 if (!res || !pattern || !str1) {
185 MS_DBG_ERR("parameta is invalid");
186 return MS_MEDIA_ERR_INVALID_PARAMETER;
189 if (real_size < (int)(strlen(str1))) {
190 MS_DBG_ERR("size is wrong");
191 return MS_MEDIA_ERR_INVALID_PARAMETER;
194 len = snprintf(res, real_size, pattern, str1);
196 MS_DBG_ERR("snprintf failed");
197 return MS_MEDIA_ERR_INVALID_PARAMETER;
202 return MS_MEDIA_ERR_NONE;
205 bool ms_config_get_int(const char *key, int *value)
209 if (!key || !value) {
210 MS_DBG_ERR("Arguments key or value is NULL");
214 err = vconf_get_int(key, value);
220 MS_DBG_ERR("Unexpected error code: %d", err);
225 bool ms_config_set_int(const char *key, int value)
230 MS_DBG_ERR("Arguments key is NULL");
234 err = vconf_set_int(key, value);
240 MS_DBG_ERR("Unexpected error code: %d", err);
245 bool ms_config_get_str(const char *key, char **value)
249 if (key == NULL || value == NULL) {
250 MS_DBG_ERR("Arguments key or value is NULL");
254 res = vconf_get_str(key);
255 if (MS_STRING_VALID(res)) {
256 *value = strdup(res);
264 bool ms_config_set_str(const char *key, const char *value)
268 if (!key || !value) {
269 MS_DBG_ERR("Arguments key or value is NULL");
273 err = vconf_set_str(key, value);
277 MS_DBG_ERR("fail to vconf_set_str %d", err);
282 bool ms_config_get_bool(const char *key, int *value)
286 if (!key || !value) {
287 MS_DBG_ERR("Arguments key or value is NULL");
291 err = vconf_get_bool(key, value);
297 MS_DBG_ERR("Unexpected error code: %d", err);