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