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