Multiuser feature
[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 /**
23  * This file defines api utilities of contents manager engines.
24  *
25  * @file                media-server-utils.c
26  * @author      Yong Yeon Kim(yy9875.kim@samsung.com)
27  * @version     1.0
28  * @brief       This file implements main database operation.
29  */
30
31 #define _GNU_SOURCE
32
33 #include <errno.h>
34 #include <vconf.h>
35 #include <aul/aul.h>
36 #include <grp.h>
37 #include <pwd.h>
38
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"
44
45 #ifdef FMS_PERF
46 #include <sys/time.h>
47 #define MILLION 1000000L
48 struct timeval g_mmc_start_time;
49 struct timeval g_mmc_end_time;
50 #endif
51
52 #define MS_DRM_CONTENT_TYPE_LENGTH 100
53 #define GLOBAL_USER     0 //#define     tzplatform_getenv(TZ_GLOBAL) //TODO
54
55 #ifdef FMS_PERF
56 void
57 ms_check_start_time(struct timeval *start_time)
58 {
59         gettimeofday(start_time, NULL);
60 }
61
62 void
63 ms_check_end_time(struct timeval *end_time)
64 {
65         gettimeofday(end_time, NULL);
66 }
67
68 void
69 ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
70 {
71         struct timeval time;
72         long difftime;
73
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);
79 }
80 #endif
81
82 bool
83 ms_is_mmc_inserted(void)
84 {
85         int data = -1;
86         ms_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &data);
87         if (data != VCONFKEY_SYSMAN_MMC_MOUNTED) {
88                 return false;
89         } else {
90                 return true;
91         }
92 }
93 static char* __media_get_path(uid_t uid)
94 {
95         char *result_psswd = NULL;
96         struct group *grpinfo = NULL;
97         if(uid == getuid())
98         {
99                 result_psswd = strdup(MEDIA_ROOT_PATH_INTERNAL);
100                 grpinfo = getgrnam("users");
101                 if(grpinfo == NULL) {
102                         MS_DBG_ERR("getgrnam(users) returns NULL !");
103                         return NULL;
104                 }
105         }
106         else
107         {
108                 struct passwd *userinfo = getpwuid(uid);
109                 if(userinfo == NULL) {
110                         MS_DBG_ERR("getpwuid(%d) returns NULL !", uid);
111                         return NULL;
112                 }
113                 grpinfo = getgrnam("users");
114                 if(grpinfo == NULL) {
115                         MS_DBG_ERR("getgrnam(users) returns NULL !");
116                         return NULL;
117                 }
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);
121                         return NULL;
122                 }
123                 asprintf(&result_psswd, "%s", userinfo->pw_dir);
124         }
125
126         return result_psswd;
127 }
128
129 ms_storage_type_t
130 ms_get_storage_type_by_full(const char *path, uid_t uid)
131 {
132         int lenght_path;
133         char * user_path = NULL;
134         
135         if (path == NULL)
136                 return false;
137
138         user_path = __media_get_path(uid);
139         lenght_path = strlen(user_path);
140
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;
145         } else
146                 return MS_MEDIA_ERR_INVALID_PATH;
147 }
148
149 int
150 ms_get_mime(const char *path, char *mimetype)
151 {
152         int ret = MS_MEDIA_ERR_NONE;
153
154         if (path == NULL)
155                 return MS_MEDIA_ERR_INVALID_PARAMETER;
156
157         /*get content type and mime type from file. */
158         /*in case of drm file. */
159         if (ms_is_drm_file(path)) {
160                 if (ms_get_mime_in_drm_info(path, mimetype) != MS_MEDIA_ERR_NONE) {
161                         MS_DBG_ERR("Fail to get mime from drm API");
162                         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
163                                 MS_DBG_ERR("aul_get_mime_from_file fail");
164                                 ret = MS_MEDIA_ERR_MIME_GET_FAIL;
165                         } else {
166                                 MS_DBG_ERR("aul_get_mime_from_file success");
167                                 ret = MS_MEDIA_ERR_NONE;
168                         }
169                 }
170         } else {
171                 /*in case of normal files */
172                 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
173                         MS_DBG_ERR("aul_get_mime_from_file fail");
174                         ret = MS_MEDIA_ERR_MIME_GET_FAIL;
175                 }
176         }
177
178         return ret;
179 }
180
181 int
182 ms_strappend(char *res, const int size, const char *pattern,
183              const char *str1, const char *str2)
184 {
185         int len = 0;
186         int real_size = size - 1;
187
188         if (!res ||!pattern || !str1 ||!str2 )
189                 return MS_MEDIA_ERR_INVALID_PARAMETER;
190
191         if (real_size < (strlen(str1) + strlen(str2)))
192                 return MS_MEDIA_ERR_INVALID_PARAMETER;
193
194         len = snprintf(res, real_size, pattern, str1, str2);
195         if (len < 0) {
196                 return MS_MEDIA_ERR_INVALID_PARAMETER;
197         }
198
199         res[len] = '\0';
200
201         return MS_MEDIA_ERR_NONE;
202 }
203
204 int
205 ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
206 {
207         int len = 0;
208         int real_size = size;
209
210         if (!res || !pattern || !str1) {
211                 MS_DBG_ERR("parameta is invalid");
212                 return MS_MEDIA_ERR_INVALID_PARAMETER;
213         }
214
215         if (real_size < strlen(str1)) {
216                 MS_DBG_ERR("size is wrong");
217                 return MS_MEDIA_ERR_INVALID_PARAMETER;
218         }
219
220         len = snprintf(res, real_size, pattern, str1);
221         if (len < 0) {
222                 MS_DBG_ERR("snprintf failed");
223                 return MS_MEDIA_ERR_INVALID_PARAMETER;
224         }
225
226         res[len] = '\0';
227
228         return MS_MEDIA_ERR_NONE;
229 }
230
231 bool
232 ms_config_get_int(const char *key, int *value)
233 {
234         int err;
235
236         if (!key || !value) {
237                 MS_DBG_ERR("Arguments key or value is NULL");
238                 return false;
239         }
240
241         err = vconf_get_int(key, value);
242         if (err == 0)
243                 return true;
244         else if (err == -1)
245                 return false;
246         else
247                 MS_DBG_ERR("Unexpected error code: %d", err);
248
249         return false;
250 }
251
252 bool
253 ms_config_set_int(const char *key, int value)
254 {
255         int err;
256
257         if (!key) {
258                 MS_DBG_ERR("Arguments key is NULL");
259                 return false;
260         }
261
262         err = vconf_set_int(key, value);
263         if (err == 0)
264                 return true;
265         else if (err == -1)
266                 return false;
267         else
268                 MS_DBG_ERR("Unexpected error code: %d", err);
269
270         return false;
271 }
272
273 bool
274 ms_config_get_str(const char *key, char *value)
275 {
276         char *res;
277         if (!key || !value) {
278                 MS_DBG_ERR("Arguments key or value is NULL");
279                 return false;
280         }
281
282         res = vconf_get_str(key);
283         if (res) {
284                 strncpy(value, res, strlen(res) + 1);
285                 return true;
286         }
287
288         return false;
289 }
290
291 bool
292 ms_config_set_str(const char *key, const char *value)
293 {
294         int err;
295
296         if (!key || !value) {
297                 MS_DBG_ERR("Arguments key or value is NULL");
298                 return false;
299         }
300
301         err = vconf_set_str(key, value);
302         if (err == 0)
303                 return true;
304         else
305                 MS_DBG_ERR("fail to vconf_set_str %d", err);
306
307         return false;
308 }
309