Change asprintf() to strndup()/snprintf()
[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 #include <dbus/dbus-glib.h>
29 #include <dbus/dbus.h>
30 #include <dbus/dbus-glib-lowlevel.h>
31
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"
37
38 #ifdef FMS_PERF
39 #include <sys/time.h>
40 #define MILLION 1000000L
41 struct timeval g_mmc_start_time;
42 struct timeval g_mmc_end_time;
43 #endif
44
45 #define MS_DRM_CONTENT_TYPE_LENGTH 100
46
47 #ifdef FMS_PERF
48 void ms_check_start_time(struct timeval *start_time)
49 {
50         gettimeofday(start_time, NULL);
51 }
52
53 void ms_check_end_time(struct timeval *end_time)
54 {
55         gettimeofday(end_time, NULL);
56 }
57
58 void 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 ms_is_mmc_inserted(void)
72 {
73         bool ret = FALSE;
74         ms_stg_type_e stg_type = MS_STG_TYPE_MMC;
75         GArray *dev_list = NULL;
76
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);
82                         ret = TRUE;
83                 } else {
84                         MS_DBG_ERR("MMC NOT FOUND");
85                 }
86         } else {
87                 MS_DBG_ERR("ms_sys_get_device_list failed");
88         }
89
90         return ret;
91 }
92
93 static char* __media_get_path(uid_t uid)
94 {
95         char *result_psswd = NULL;
96         struct group *grpinfo = NULL;
97         int err = 0;
98
99         if (uid == getuid()) {
100                 result_psswd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
101                 grpinfo = getgrnam("users");
102                 if (grpinfo == NULL) {
103                         MS_DBG_ERR("getgrnam(users) returns NULL !");
104                         MS_SAFE_FREE(result_psswd);
105                         return NULL;
106                 }
107         } else {
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                 result_psswd = strndup(userinfo->pw_dir, strlen(userinfo->pw_dir));
124         }
125
126         return result_psswd;
127 }
128
129 ms_storage_type_t ms_get_storage_type_by_full(const char *path, uid_t uid)
130 {
131         int length_path;
132         int ret = MS_MEDIA_ERR_NONE;
133         char * user_path = NULL;
134
135         if (path == NULL)
136                 return MS_MEDIA_ERR_INVALID_PATH;
137
138         user_path = __media_get_path(uid);
139         if (user_path == NULL)
140                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
141
142         length_path = strlen(user_path);
143
144         if (strncmp(path, user_path, length_path) == 0) {
145                 ret = MS_STORAGE_INTERNAL;
146         } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
147                 ret = MS_STORAGE_EXTERNAL;
148         } else if (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0) {
149                 ret = MS_STORAGE_EXTERNAL_USB;
150         } else {
151                 ret = MS_MEDIA_ERR_INVALID_PATH;
152         }
153
154         MS_SAFE_FREE(user_path);
155
156         return ret;
157 }
158
159 int ms_strappend(char *res, const int size, const char *pattern,
160              const char *str1, const char *str2)
161 {
162         int len = 0;
163         int real_size = size - 1;
164
165         if (!res ||!pattern || !str1 ||!str2 )
166                 return MS_MEDIA_ERR_INVALID_PARAMETER;
167
168         if (real_size < (int)(strlen(str1) + strlen(str2)))
169                 return MS_MEDIA_ERR_INVALID_PARAMETER;
170
171         len = snprintf(res, real_size, pattern, str1, str2);
172         if (len < 0) {
173                 return MS_MEDIA_ERR_INVALID_PARAMETER;
174         }
175
176         res[len] = '\0';
177
178         return MS_MEDIA_ERR_NONE;
179 }
180
181 int ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
182 {
183         int len = 0;
184         int real_size = size;
185
186         if (!res || !pattern || !str1) {
187                 MS_DBG_ERR("parameta is invalid");
188                 return MS_MEDIA_ERR_INVALID_PARAMETER;
189         }
190
191         if (real_size < (int)(strlen(str1))) {
192                 MS_DBG_ERR("size is wrong");
193                 return MS_MEDIA_ERR_INVALID_PARAMETER;
194         }
195
196         len = snprintf(res, real_size, pattern, str1);
197         if (len < 0) {
198                 MS_DBG_ERR("snprintf failed");
199                 return MS_MEDIA_ERR_INVALID_PARAMETER;
200         }
201
202         res[len] = '\0';
203
204         return MS_MEDIA_ERR_NONE;
205 }
206
207 bool ms_config_get_int(const char *key, int *value)
208 {
209         int err;
210
211         if (!key || !value) {
212                 MS_DBG_ERR("Arguments key or value is NULL");
213                 return false;
214         }
215
216         err = vconf_get_int(key, value);
217         if (err == 0)
218                 return true;
219         else if (err == -1)
220                 return false;
221         else
222                 MS_DBG_ERR("Unexpected error code: %d", err);
223
224         return false;
225 }
226
227 bool ms_config_set_int(const char *key, int value)
228 {
229         int err;
230
231         if (!key) {
232                 MS_DBG_ERR("Arguments key is NULL");
233                 return false;
234         }
235
236         err = vconf_set_int(key, value);
237         if (err == 0)
238                 return true;
239         else if (err == -1)
240                 return false;
241         else
242                 MS_DBG_ERR("Unexpected error code: %d", err);
243
244         return false;
245 }
246
247 bool ms_config_get_str(const char *key, char **value)
248 {
249         char *res = NULL;
250
251         if (key == NULL || value == NULL) {
252                 MS_DBG_ERR("Arguments key or value is NULL");
253                 return false;
254         }
255
256         res = vconf_get_str(key);
257         if (MS_STRING_VALID(res)) {
258                 *value = strdup(res);
259                 MS_SAFE_FREE(res);
260                 return true;
261         }
262
263         return false;
264 }
265
266 bool ms_config_set_str(const char *key, const char *value)
267 {
268         int err;
269
270         if (!key || !value) {
271                 MS_DBG_ERR("Arguments key or value is NULL");
272                 return false;
273         }
274
275         err = vconf_set_str(key, value);
276         if (err == 0)
277                 return true;
278         else
279                 MS_DBG_ERR("fail to vconf_set_str %d", err);
280
281         return false;
282 }
283
284 bool ms_config_get_bool(const char *key, int *value)
285 {
286         int err;
287
288         if (!key || !value) {
289                 MS_DBG_ERR("Arguments key or value is NULL");
290                 return false;
291         }
292
293         err = vconf_get_bool(key, value);
294         if (err == 0)
295                 return true;
296         else if (err == -1)
297                 return false;
298         else
299                 MS_DBG_ERR("Unexpected error code: %d", err);
300
301         return false;
302 }