Fix memory leak
[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                         MS_SAFE_FREE(result_psswd);
93                         return NULL;
94                 }
95         }
96         else
97         {
98                 struct passwd *userinfo = getpwuid(uid);
99                 if(userinfo == NULL) {
100                         MS_DBG_ERR("getpwuid(%d) returns NULL !", uid);
101                         return NULL;
102                 }
103                 grpinfo = getgrnam("users");
104                 if(grpinfo == NULL) {
105                         MS_DBG_ERR("getgrnam(users) returns NULL !");
106                         return NULL;
107                 }
108                 // Compare git_t type and not group name
109                 if (grpinfo->gr_gid != userinfo->pw_gid) {
110                         MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
111                         return NULL;
112                 }
113                 asprintf(&result_psswd, "%s", userinfo->pw_dir);
114         }
115
116         return result_psswd;
117 }
118
119 ms_storage_type_t
120 ms_get_storage_type_by_full(const char *path, uid_t uid)
121 {
122         int length_path;
123         char * user_path = NULL;
124
125         if (path == NULL)
126                 return false;
127
128         user_path = __media_get_path(uid);
129         length_path = strlen(user_path);
130
131         if (strncmp(path, user_path, length_path) == 0) {
132                 return MS_STORAGE_INTERNAL;
133         } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
134                 return MS_STORAGE_EXTERNAL;
135         } else {
136                 free(user_path);
137                 return MS_MEDIA_ERR_INVALID_PATH;
138     }
139 }
140
141 int
142 ms_strappend(char *res, const int size, const char *pattern,
143              const char *str1, const char *str2)
144 {
145         int len = 0;
146         int real_size = size - 1;
147
148         if (!res ||!pattern || !str1 ||!str2 )
149                 return MS_MEDIA_ERR_INVALID_PARAMETER;
150
151         if (real_size < (strlen(str1) + strlen(str2)))
152                 return MS_MEDIA_ERR_INVALID_PARAMETER;
153
154         len = snprintf(res, real_size, pattern, str1, str2);
155         if (len < 0) {
156                 return MS_MEDIA_ERR_INVALID_PARAMETER;
157         }
158
159         res[len] = '\0';
160
161         return MS_MEDIA_ERR_NONE;
162 }
163
164 int
165 ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
166 {
167         int len = 0;
168         int real_size = size;
169
170         if (!res || !pattern || !str1) {
171                 MS_DBG_ERR("parameta is invalid");
172                 return MS_MEDIA_ERR_INVALID_PARAMETER;
173         }
174
175         if (real_size < strlen(str1)) {
176                 MS_DBG_ERR("size is wrong");
177                 return MS_MEDIA_ERR_INVALID_PARAMETER;
178         }
179
180         len = snprintf(res, real_size, pattern, str1);
181         if (len < 0) {
182                 MS_DBG_ERR("snprintf failed");
183                 return MS_MEDIA_ERR_INVALID_PARAMETER;
184         }
185
186         res[len] = '\0';
187
188         return MS_MEDIA_ERR_NONE;
189 }
190
191 bool
192 ms_config_get_int(const char *key, int *value)
193 {
194         int err;
195
196         if (!key || !value) {
197                 MS_DBG_ERR("Arguments key or value is NULL");
198                 return false;
199         }
200
201         err = vconf_get_int(key, value);
202         if (err == 0)
203                 return true;
204         else if (err == -1)
205                 return false;
206         else
207                 MS_DBG_ERR("Unexpected error code: %d", err);
208
209         return false;
210 }
211
212 bool
213 ms_config_set_int(const char *key, int value)
214 {
215         int err;
216
217         if (!key) {
218                 MS_DBG_ERR("Arguments key is NULL");
219                 return false;
220         }
221
222         err = vconf_set_int(key, value);
223         if (err == 0)
224                 return true;
225         else if (err == -1)
226                 return false;
227         else
228                 MS_DBG_ERR("Unexpected error code: %d", err);
229
230         return false;
231 }
232
233 bool
234 ms_config_get_str(const char *key, char *value)
235 {
236         char *res;
237         if (!key || !value) {
238                 MS_DBG_ERR("Arguments key or value is NULL");
239                 return false;
240         }
241
242         res = vconf_get_str(key);
243         if (res) {
244                 strncpy(value, res, strlen(res) + 1);
245                 free(res);
246                 return true;
247         }
248
249         return false;
250 }
251
252 bool
253 ms_config_set_str(const char *key, const char *value)
254 {
255         int err;
256
257         if (!key || !value) {
258                 MS_DBG_ERR("Arguments key or value is NULL");
259                 return false;
260         }
261
262         err = vconf_set_str(key, value);
263         if (err == 0)
264                 return true;
265         else
266                 MS_DBG_ERR("fail to vconf_set_str %d", err);
267
268         return false;
269 }
270
271 bool
272 ms_config_get_bool(const char *key, int *value)
273 {
274         int err;
275
276         if (!key || !value) {
277                 MS_DBG_ERR("Arguments key or value is NULL");
278                 return false;
279         }
280
281         err = vconf_get_bool(key, value);
282         if (err == 0)
283                 return true;
284         else if (err == -1)
285                 return false;
286         else
287                 MS_DBG_ERR("Unexpected error code: %d", err);
288
289         return false;
290 }