Merge commit 'refs/changes/94/42094/2' of ssh://review.tizen.org:29418/platform/core...
[platform/core/multimedia/media-server.git] / src / common / media-common-utils.c
1 /*
2  *  Media Server
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #define _GNU_SOURCE
23 #include <errno.h>
24 #include <vconf.h>
25 #include <aul/aul.h>
26 #include <grp.h>
27 #include <pwd.h>
28
29 #include "media-util.h"
30 #include "media-server-ipc.h"
31 #include "media-common-dbg.h"
32 #include "media-common-utils.h"
33
34 #ifdef FMS_PERF
35 #include <sys/time.h>
36 #define MILLION 1000000L
37 struct timeval g_mmc_start_time;
38 struct timeval g_mmc_end_time;
39 #endif
40
41 #define MS_DRM_CONTENT_TYPE_LENGTH 100
42 #define GLOBAL_USER     0 //#define     tzplatform_getenv(TZ_GLOBAL) //TODO
43
44 #ifdef FMS_PERF
45 void
46 ms_check_start_time(struct timeval *start_time)
47 {
48         gettimeofday(start_time, NULL);
49 }
50
51 void
52 ms_check_end_time(struct timeval *end_time)
53 {
54         gettimeofday(end_time, NULL);
55 }
56
57 void
58 ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
59 {
60         struct timeval time;
61         long difftime;
62
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);
68 }
69 #endif
70
71 bool
72 ms_is_mmc_inserted(void)
73 {
74         int data = -1;
75         ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &data);
76         if (data != VCONFKEY_SYSMAN_MMC_MOUNTED) {
77                 return false;
78         } else {
79                 return true;
80         }
81 }
82 static char* __media_get_path(uid_t uid)
83 {
84         char *result_psswd = NULL;
85         struct group *grpinfo = NULL;
86         if(uid == getuid())
87         {
88                 result_psswd = strdup(MEDIA_ROOT_PATH_INTERNAL);
89                 grpinfo = getgrnam("users");
90                 if(grpinfo == NULL) {
91                         MS_DBG_ERR("getgrnam(users) returns NULL !");
92                         return NULL;
93                 }
94     }
95         else
96         {
97                 struct passwd *userinfo = getpwuid(uid);
98                 if(userinfo == NULL) {
99                         MS_DBG_ERR("getpwuid(%d) returns NULL !", uid);
100                         return NULL;
101                 }
102                 grpinfo = getgrnam("users");
103                 if(grpinfo == NULL) {
104                         MS_DBG_ERR("getgrnam(users) returns NULL !");
105                         return NULL;
106                 }
107                 // Compare git_t type and not group name
108                 if (grpinfo->gr_gid != userinfo->pw_gid) {
109                         MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
110                         return NULL;
111                 }
112                 asprintf(&result_psswd, "%s", userinfo->pw_dir);
113         }
114
115         return result_psswd;
116 }
117
118 ms_storage_type_t
119 ms_get_storage_type_by_full(const char *path, uid_t uid)
120 {
121         int length_path;
122         char * user_path = NULL;
123         
124         if (path == NULL)
125                 return false;
126
127         user_path = __media_get_path(uid);
128         length_path = strlen(user_path);
129
130         if (strncmp(path, user_path, length_path) == 0) {
131                 return MS_STORAGE_INTERNAL;
132         } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
133                 return MS_STORAGE_EXTERNAL;
134         } else {
135                 free(user_path);
136                 return MS_MEDIA_ERR_INVALID_PATH;
137     }
138 }
139
140 int
141 ms_strappend(char *res, const int size, const char *pattern,
142              const char *str1, const char *str2)
143 {
144         int len = 0;
145         int real_size = size - 1;
146
147         if (!res ||!pattern || !str1 ||!str2 )
148                 return MS_MEDIA_ERR_INVALID_PARAMETER;
149
150         if (real_size < (strlen(str1) + strlen(str2)))
151                 return MS_MEDIA_ERR_INVALID_PARAMETER;
152
153         len = snprintf(res, real_size, pattern, str1, str2);
154         if (len < 0) {
155                 return MS_MEDIA_ERR_INVALID_PARAMETER;
156         }
157
158         res[len] = '\0';
159
160         return MS_MEDIA_ERR_NONE;
161 }
162
163 int
164 ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
165 {
166         int len = 0;
167         int real_size = size;
168
169         if (!res || !pattern || !str1) {
170                 MS_DBG_ERR("parameta is invalid");
171                 return MS_MEDIA_ERR_INVALID_PARAMETER;
172         }
173
174         if (real_size < strlen(str1)) {
175                 MS_DBG_ERR("size is wrong");
176                 return MS_MEDIA_ERR_INVALID_PARAMETER;
177         }
178
179         len = snprintf(res, real_size, pattern, str1);
180         if (len < 0) {
181                 MS_DBG_ERR("snprintf failed");
182                 return MS_MEDIA_ERR_INVALID_PARAMETER;
183         }
184
185         res[len] = '\0';
186
187         return MS_MEDIA_ERR_NONE;
188 }
189
190 bool
191 ms_config_get_int(const char *key, int *value)
192 {
193         int err;
194
195         if (!key || !value) {
196                 MS_DBG_ERR("Arguments key or value is NULL");
197                 return false;
198         }
199
200         err = vconf_get_int(key, value);
201         if (err == 0)
202                 return true;
203         else if (err == -1)
204                 return false;
205         else
206                 MS_DBG_ERR("Unexpected error code: %d", err);
207
208         return false;
209 }
210
211 bool
212 ms_config_set_int(const char *key, int value)
213 {
214         int err;
215
216         if (!key) {
217                 MS_DBG_ERR("Arguments key is NULL");
218                 return false;
219         }
220
221         err = vconf_set_int(key, value);
222         if (err == 0)
223                 return true;
224         else if (err == -1)
225                 return false;
226         else
227                 MS_DBG_ERR("Unexpected error code: %d", err);
228
229         return false;
230 }
231
232 bool
233 ms_config_get_str(const char *key, char *value)
234 {
235         char *res;
236         if (!key || !value) {
237                 MS_DBG_ERR("Arguments key or value is NULL");
238                 return false;
239         }
240
241         res = vconf_get_str(key);
242         if (res) {
243                 strncpy(value, res, strlen(res) + 1);
244                 free(res);
245                 return true;
246         }
247
248         return false;
249 }
250
251 bool
252 ms_config_set_str(const char *key, const char *value)
253 {
254         int err;
255
256         if (!key || !value) {
257                 MS_DBG_ERR("Arguments key or value is NULL");
258                 return false;
259         }
260
261         err = vconf_set_str(key, value);
262         if (err == 0)
263                 return true;
264         else
265                 MS_DBG_ERR("fail to vconf_set_str %d", err);
266
267         return false;
268 }
269
270 bool
271 ms_config_get_bool(const char *key, int *value)
272 {
273         int err;
274
275         if (!key || !value) {
276                 MS_DBG_ERR("Arguments key or value is NULL");
277                 return false;
278         }
279
280         err = vconf_get_bool(key, value);
281         if (err == 0)
282                 return true;
283         else if (err == -1)
284                 return false;
285         else
286                 MS_DBG_ERR("Unexpected error code: %d", err);
287
288         return false;
289 }