Update scanner-v2, Fix some bug, and code refactoring
[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 #include <sys/statvfs.h>
32 #include <sys/stat.h>
33 #include <system_info.h>
34
35 #include "media-util.h"
36 #include "media-server-ipc.h"
37 #include "media-common-dbg.h"
38 #include "media-common-system.h"
39 #include "media-common-utils.h"
40
41 #ifdef FMS_PERF
42 #include <sys/time.h>
43 #define MILLION 1000000L
44 struct timeval g_mmc_start_time;
45 struct timeval g_mmc_end_time;
46 #endif
47
48 #define MS_DRM_CONTENT_TYPE_LENGTH 100
49
50 /* it's for 32bit file offset */
51 struct statvfs_32 {
52         unsigned long int f_bsize;
53         unsigned long int f_frsize;
54         unsigned long int f_blocks;
55         unsigned long int f_bfree;
56         unsigned long int f_bavail;
57         unsigned long int f_files;
58         unsigned long int f_ffree;
59         unsigned long int f_favail;
60         unsigned long int f_fsid;
61 #ifdef _STATVFSBUF_F_UNUSED
62         int __f_unused;
63 #endif
64         unsigned long int f_flag;
65         unsigned long int f_namemax;
66         int __f_spare[6];
67 };
68
69 #ifdef FMS_PERF
70 void ms_check_start_time(struct timeval *start_time)
71 {
72         gettimeofday(start_time, NULL);
73 }
74
75 void ms_check_end_time(struct timeval *end_time)
76 {
77         gettimeofday(end_time, NULL);
78 }
79
80 void ms_check_time_diff(struct timeval *start_time, struct timeval *end_time)
81 {
82         struct timeval time;
83         long difftime;
84
85         time.tv_sec = end_time->tv_sec - start_time->tv_sec;
86         time.tv_usec = end_time->tv_usec - start_time->tv_usec;
87         difftime = MILLION * time.tv_sec + time.tv_usec;
88         MS_DBG("The function_to_time took %ld microseconds or %f seconds.", difftime, difftime / (double)MILLION);
89 }
90 #endif
91
92 bool ms_is_mmc_inserted(void)
93 {
94         bool ret = FALSE;
95         ms_stg_type_e stg_type = MS_STG_TYPE_MMC;
96         GArray *dev_list = NULL;
97
98         ret = ms_sys_get_device_list(stg_type, &dev_list);
99         if (ret == MS_MEDIA_ERR_NONE) {
100                 if (dev_list != NULL) {
101                         MS_DBG_ERR("MMC FOUND[%d]", dev_list->len);
102                         ms_sys_release_device_list(&dev_list);
103                         ret = TRUE;
104                 } else {
105                         MS_DBG_ERR("MMC NOT FOUND");
106                 }
107         } else {
108                 MS_DBG_ERR("ms_sys_get_device_list failed");
109         }
110
111         return ret;
112 }
113
114 static char* __media_get_path(uid_t uid)
115 {
116         char *result_passwd = NULL;
117         struct group *grpinfo = NULL;
118
119         if (uid == getuid()) {
120                 grpinfo = getgrnam("users");
121                 if (grpinfo == NULL) {
122                         MS_DBG_ERR("getgrnam(users) returns NULL !");
123                         return NULL;
124                 }
125                 if (MS_STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
126                         result_passwd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
127         } else {
128                 struct passwd *userinfo = getpwuid(uid);
129                 if (userinfo == NULL) {
130                         MS_DBG_ERR("getpwuid(%d) returns NULL !", uid);
131                         return NULL;
132                 }
133                 grpinfo = getgrnam("users");
134                 if (grpinfo == NULL) {
135                         MS_DBG_ERR("getgrnam(users) returns NULL !");
136                         return NULL;
137                 }
138                 // Compare git_t type and not group name
139                 if (grpinfo->gr_gid != userinfo->pw_gid) {
140                         MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
141                         return NULL;
142                 }
143                 result_passwd = strndup(userinfo->pw_dir, strlen(userinfo->pw_dir));
144         }
145
146         return result_passwd;
147 }
148
149 ms_storage_type_t ms_get_storage_type_by_full(const char *path, uid_t uid)
150 {
151         int length_path;
152         int ret = MS_MEDIA_ERR_NONE;
153         char * user_path = NULL;
154
155         if (path == NULL)
156                 return MS_MEDIA_ERR_INVALID_PATH;
157
158 #ifdef _USE_SENIOR_MODE
159         if(ms_is_support_senior_mode()) {
160                 if (strncmp(path, MEDIA_ROOT_PATH_SENIOR_MODE, strlen(MEDIA_ROOT_PATH_SENIOR_MODE)) == 0) {
161                         return MS_STORAGE_EXTERNAL;
162                 }
163         }
164 #endif
165
166         user_path = __media_get_path(uid);
167         if (user_path == NULL)
168                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
169
170         length_path = strlen(user_path);
171
172         if (strncmp(path, user_path, length_path) == 0) {
173                 ret = MS_STORAGE_INTERNAL;
174         } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
175                 ret = MS_STORAGE_EXTERNAL;
176         } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
177                 ret = MS_STORAGE_EXTERNAL_USB;
178         } else {
179                 ret = MS_MEDIA_ERR_INVALID_PATH;
180         }
181
182         MS_SAFE_FREE(user_path);
183
184         return ret;
185 }
186
187 int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
188 {
189         int len = 0;
190         int real_size = size - 1;
191
192         if (!res || !pattern || !str1 || !str2)
193                 return MS_MEDIA_ERR_INVALID_PARAMETER;
194
195         if (real_size < (int)(strlen(str1) + strlen(str2)))
196                 return MS_MEDIA_ERR_INVALID_PARAMETER;
197
198         len = snprintf(res, real_size, pattern, str1, str2);
199         if (len < 0) {
200                 return MS_MEDIA_ERR_INVALID_PARAMETER;
201         }
202
203         res[len] = '\0';
204
205         return MS_MEDIA_ERR_NONE;
206 }
207
208 int ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
209 {
210         int len = 0;
211         int real_size = size;
212
213         if (!res || !pattern || !str1) {
214                 MS_DBG_ERR("parameta is invalid");
215                 return MS_MEDIA_ERR_INVALID_PARAMETER;
216         }
217
218         if (real_size < (int)(strlen(str1))) {
219                 MS_DBG_ERR("size is wrong");
220                 return MS_MEDIA_ERR_INVALID_PARAMETER;
221         }
222
223         len = snprintf(res, real_size, pattern, str1);
224         if (len < 0) {
225                 MS_DBG_ERR("snprintf failed");
226                 return MS_MEDIA_ERR_INVALID_PARAMETER;
227         }
228
229         res[len] = '\0';
230
231         return MS_MEDIA_ERR_NONE;
232 }
233
234 bool ms_config_get_int(const char *key, int *value)
235 {
236         int err;
237
238         if (!key || !value) {
239                 MS_DBG_ERR("Arguments key or value is NULL");
240                 return false;
241         }
242
243         err = vconf_get_int(key, value);
244         if (err == 0)
245                 return true;
246         else if (err == -1)
247                 return false;
248         else
249                 MS_DBG_ERR("Unexpected error code: %d", err);
250
251         return false;
252 }
253
254 bool 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 ms_config_get_str(const char *key, char **value)
275 {
276         char *res = NULL;
277
278         if (key == NULL || value == NULL) {
279                 MS_DBG_ERR("Arguments key or value is NULL");
280                 return false;
281         }
282
283         res = vconf_get_str(key);
284         if (MS_STRING_VALID(res)) {
285                 *value = strdup(res);
286                 MS_SAFE_FREE(res);
287                 return true;
288         }
289
290         return false;
291 }
292
293 bool 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
311 bool ms_config_get_bool(const char *key, int *value)
312 {
313         int err;
314
315         if (!key || !value) {
316                 MS_DBG_ERR("Arguments key or value is NULL");
317                 return false;
318         }
319
320         err = vconf_get_bool(key, value);
321         if (err == 0)
322                 return true;
323         else if (err == -1)
324                 return false;
325         else
326                 MS_DBG_ERR("Unexpected error code: %d", err);
327
328         return false;
329 }
330
331 static int get_memory_size(const char *path, struct statvfs_32 *buf)
332 {
333         struct statvfs s;
334         int ret;
335
336         ret = statvfs(path, &s);
337         if (ret) {
338                 MS_DBG_ERR("statvfs failed[%d]", ret);
339                 MS_DBG_STRERROR();
340                 return MS_MEDIA_ERR_INTERNAL;
341         }
342
343         buf->f_bsize  = s.f_bsize;
344         buf->f_frsize = s.f_frsize;
345         buf->f_blocks = (unsigned long)s.f_blocks;
346         buf->f_bfree  = (unsigned long)s.f_bfree;
347         buf->f_bavail = (unsigned long)s.f_bavail;
348         buf->f_files  = (unsigned long)s.f_files;
349         buf->f_ffree  = (unsigned long)s.f_ffree;
350         buf->f_favail = (unsigned long)s.f_favail;
351         buf->f_fsid = s.f_fsid;
352         buf->f_flag = s.f_flag;
353         buf->f_namemax = s.f_namemax;
354
355         return MS_MEDIA_ERR_NONE;
356 }
357
358 int ms_get_remain_space(double *free_space)
359 {
360         int ret = MS_MEDIA_ERR_NONE;
361         struct statvfs_32 temp;
362
363         ret = get_memory_size("/opt", &temp);
364         if (ret != MS_MEDIA_ERR_NONE) {
365                 MS_DBG_ERR("fail to get memory size");
366                 return ret;
367         }
368
369 //      MS_DBG_ERR("Total mem : %lf, Avail mem : %lf", (double)temp.f_frsize*temp.f_blocks, (double)temp.f_bsize*temp.f_bavail);
370
371         *free_space = (double)temp.f_bsize*temp.f_bavail;
372
373         return ret;
374 }
375
376 #ifdef _USE_RECORDED_CONTENT
377 bool ms_is_support_pvr(void)
378 {
379
380         int nSupportPVR = 0;
381         if (system_info_get_value_int(SYSTEM_INFO_KEY_PVR_SUPPORTED, &nSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
382                 MS_DBG_ERR("Get PVR Support failed");
383                 return false;
384         }
385
386         MS_DBG("PVR Support : [%d]", nSupportPVR);
387
388         return (nSupportPVR != 0);
389 }
390 #endif
391
392 #ifdef _USE_SENIOR_MODE
393 bool ms_is_support_senior_mode()
394 {
395         bool bSupportSeniorMode = false;
396
397         if(system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
398                 MS_DBG_ERR("Get senior mode support failed");
399                 return false;
400         }
401
402         MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
403
404         return bSupportSeniorMode;
405 }
406 #endif
407