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.
23 * This file defines api utilities of contents manager engines.
25 * @file media-server-utils.c
26 * @author Yong Yeon Kim(yy9875.kim@samsung.com)
28 * @brief This file implements main database operation.
39 #include "media-util.h"
40 #include "media-server-ipc.h"
41 #include "media-common-dbg.h"
42 #include "media-common-drm.h"
43 #include "media-common-utils.h"
47 #define MILLION 1000000L
48 struct timeval g_mmc_start_time;
49 struct timeval g_mmc_end_time;
52 #define MS_DRM_CONTENT_TYPE_LENGTH 100
53 #define GLOBAL_USER 0 //#define tzplatform_getenv(TZ_GLOBAL) //TODO
57 ms_check_start_time(struct timeval *start_time)
59 gettimeofday(start_time, NULL);
63 ms_check_end_time(struct timeval *end_time)
65 gettimeofday(end_time, NULL);
69 ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
74 time.tv_sec = end_time->tv_sec - start_time->tv_sec;
75 time.tv_usec = end_time->tv_usec - start_time->tv_usec;
76 difftime = MILLION * time.tv_sec + time.tv_usec;
77 MS_DBG("The function_to_time took %ld microseconds or %f seconds.",
78 difftime, difftime / (double)MILLION);
83 ms_is_mmc_inserted(void)
86 ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &data);
87 if (data != VCONFKEY_SYSMAN_MMC_MOUNTED) {
93 static char* __media_get_path(uid_t uid)
95 char *result_psswd = NULL;
96 struct group *grpinfo = NULL;
99 result_psswd = strdup(MEDIA_ROOT_PATH_INTERNAL);
100 grpinfo = getgrnam("users");
101 if(grpinfo == NULL) {
102 MS_DBG_ERR("getgrnam(users) returns NULL !");
108 struct passwd *userinfo = getpwuid(uid);
109 if(userinfo == NULL) {
110 MS_DBG_ERR("getpwuid(%d) returns NULL !", uid);
113 grpinfo = getgrnam("users");
114 if(grpinfo == NULL) {
115 MS_DBG_ERR("getgrnam(users) returns NULL !");
118 // Compare git_t type and not group name
119 if (grpinfo->gr_gid != userinfo->pw_gid) {
120 MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
123 asprintf(&result_psswd, "%s", userinfo->pw_dir);
130 ms_get_storage_type_by_full(const char *path, uid_t uid)
133 char * user_path = NULL;
138 user_path = __media_get_path(uid);
139 lenght_path = strlen(user_path);
141 if (strncmp(path, user_path, lenght_path) == 0) {
142 return MS_STORAGE_INTERNAL;
143 } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
144 return MS_STORAGE_EXTERNAL;
147 return MS_MEDIA_ERR_INVALID_PATH;
152 ms_strappend(char *res, const int size, const char *pattern,
153 const char *str1, const char *str2)
156 int real_size = size - 1;
158 if (!res ||!pattern || !str1 ||!str2 )
159 return MS_MEDIA_ERR_INVALID_PARAMETER;
161 if (real_size < (strlen(str1) + strlen(str2)))
162 return MS_MEDIA_ERR_INVALID_PARAMETER;
164 len = snprintf(res, real_size, pattern, str1, str2);
166 return MS_MEDIA_ERR_INVALID_PARAMETER;
171 return MS_MEDIA_ERR_NONE;
175 ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
178 int real_size = size;
180 if (!res || !pattern || !str1) {
181 MS_DBG_ERR("parameta is invalid");
182 return MS_MEDIA_ERR_INVALID_PARAMETER;
185 if (real_size < strlen(str1)) {
186 MS_DBG_ERR("size is wrong");
187 return MS_MEDIA_ERR_INVALID_PARAMETER;
190 len = snprintf(res, real_size, pattern, str1);
192 MS_DBG_ERR("snprintf failed");
193 return MS_MEDIA_ERR_INVALID_PARAMETER;
198 return MS_MEDIA_ERR_NONE;
202 ms_config_get_int(const char *key, int *value)
206 if (!key || !value) {
207 MS_DBG_ERR("Arguments key or value is NULL");
211 err = vconf_get_int(key, value);
217 MS_DBG_ERR("Unexpected error code: %d", err);
223 ms_config_set_int(const char *key, int value)
228 MS_DBG_ERR("Arguments key is NULL");
232 err = vconf_set_int(key, value);
238 MS_DBG_ERR("Unexpected error code: %d", err);
244 ms_config_get_str(const char *key, char *value)
247 if (!key || !value) {
248 MS_DBG_ERR("Arguments key or value is NULL");
252 res = vconf_get_str(key);
254 strncpy(value, res, strlen(res) + 1);
263 ms_config_set_str(const char *key, const char *value)
267 if (!key || !value) {
268 MS_DBG_ERR("Arguments key or value is NULL");
272 err = vconf_set_str(key, value);
276 MS_DBG_ERR("fail to vconf_set_str %d", err);