Merge "Revert "Modify error type"" into tizen
[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        { free(user_path);
147                 return MS_MEDIA_ERR_INVALID_PATH;
148     }
149 }
150 int
151 ms_get_mime(const char *path, char *mimetype)
152 {
153         int ret = MS_MEDIA_ERR_NONE;
154
155         if (path == NULL)
156                 return MS_MEDIA_ERR_INVALID_PARAMETER;
157
158         /*get content type and mime type from file. */
159         /*in case of drm file. */
160         if (ms_is_drm_file(path)) {
161                 if (ms_get_mime_in_drm_info(path, mimetype) != MS_MEDIA_ERR_NONE) {
162                         MS_DBG_ERR("Fail to get mime from drm API");
163                         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
164                                 MS_DBG_ERR("aul_get_mime_from_file fail");
165                                 ret = MS_MEDIA_ERR_MIME_GET_FAIL;
166                         } else {
167                                 MS_DBG_ERR("aul_get_mime_from_file success");
168                                 ret = MS_MEDIA_ERR_NONE;
169                         }
170                 }
171         } else {
172                 /*in case of normal files */
173                 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
174                         MS_DBG_ERR("aul_get_mime_from_file fail");
175                         ret = MS_MEDIA_ERR_MIME_GET_FAIL;
176                 }
177         }
178
179         return ret;
180 }
181
182 int
183 ms_strappend(char *res, const int size, const char *pattern,
184              const char *str1, const char *str2)
185 {
186         int len = 0;
187         int real_size = size - 1;
188
189         if (!res ||!pattern || !str1 ||!str2 )
190                 return MS_MEDIA_ERR_INVALID_PARAMETER;
191
192         if (real_size < (strlen(str1) + strlen(str2)))
193                 return MS_MEDIA_ERR_INVALID_PARAMETER;
194
195         len = snprintf(res, real_size, pattern, str1, str2);
196         if (len < 0) {
197                 return MS_MEDIA_ERR_INVALID_PARAMETER;
198         }
199
200         res[len] = '\0';
201
202         return MS_MEDIA_ERR_NONE;
203 }
204
205 int
206 ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
207 {
208         int len = 0;
209         int real_size = size;
210
211         if (!res || !pattern || !str1) {
212                 MS_DBG_ERR("parameta is invalid");
213                 return MS_MEDIA_ERR_INVALID_PARAMETER;
214         }
215
216         if (real_size < strlen(str1)) {
217                 MS_DBG_ERR("size is wrong");
218                 return MS_MEDIA_ERR_INVALID_PARAMETER;
219         }
220
221         len = snprintf(res, real_size, pattern, str1);
222         if (len < 0) {
223                 MS_DBG_ERR("snprintf failed");
224                 return MS_MEDIA_ERR_INVALID_PARAMETER;
225         }
226
227         res[len] = '\0';
228
229         return MS_MEDIA_ERR_NONE;
230 }
231
232 bool
233 ms_config_get_int(const char *key, int *value)
234 {
235         int err;
236
237         if (!key || !value) {
238                 MS_DBG_ERR("Arguments key or value is NULL");
239                 return false;
240         }
241
242         err = vconf_get_int(key, value);
243         if (err == 0)
244                 return true;
245         else if (err == -1)
246                 return false;
247         else
248                 MS_DBG_ERR("Unexpected error code: %d", err);
249
250         return false;
251 }
252
253 bool
254 ms_config_set_int(const char *key, int value)
255 {
256         int err;
257
258         if (!key) {
259                 MS_DBG_ERR("Arguments key is NULL");
260                 return false;
261         }
262
263         err = vconf_set_int(key, value);
264         if (err == 0)
265                 return true;
266         else if (err == -1)
267                 return false;
268         else
269                 MS_DBG_ERR("Unexpected error code: %d", err);
270
271         return false;
272 }
273
274 bool
275 ms_config_get_str(const char *key, char *value)
276 {
277         char *res;
278         if (!key || !value) {
279                 MS_DBG_ERR("Arguments key or value is NULL");
280                 return false;
281         }
282
283         res = vconf_get_str(key);
284         if (res) {
285                 strncpy(value, res, strlen(res) + 1);
286                 return true;
287         }
288
289         return false;
290 }
291
292 bool
293 ms_config_set_str(const char *key, const char *value)
294 {
295         int err;
296
297         if (!key || !value) {
298                 MS_DBG_ERR("Arguments key or value is NULL");
299                 return false;
300         }
301
302         err = vconf_set_str(key, value);
303         if (err == 0)
304                 return true;
305         else
306                 MS_DBG_ERR("fail to vconf_set_str %d", err);
307
308         return false;
309 }
310