improve callback of data disc
[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 <sys/types.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <vconf.h>
27 #include <aul/aul.h>
28 #include <grp.h>
29 #include <pwd.h>
30 #include <dbus/dbus-glib.h>
31 #include <dbus/dbus.h>
32 #include <dbus/dbus-glib-lowlevel.h>
33 #include <sys/statvfs.h>
34 #include <sys/stat.h>
35 #ifdef _SET_VIP_PROCESS
36 #include <sys/prctl.h>
37 #endif
38 #include <system_info.h>
39 #include <dd-display.h>
40
41 #ifndef _USE_DEVICED_DBUS
42 #include <usb-device.h>
43 #endif
44
45 #include "media-util.h"
46 #include "media-server-ipc.h"
47 #include "media-common-dbg.h"
48 #include "media-common-system.h"
49 #include "media-common-utils.h"
50
51 #define MS_DRM_CONTENT_TYPE_LENGTH 100
52
53 /* it's for 32bit file offset */
54 struct statvfs_32 {
55         unsigned long int f_bsize;
56         unsigned long int f_frsize;
57         unsigned long int f_blocks;
58         unsigned long int f_bfree;
59         unsigned long int f_bavail;
60         unsigned long int f_files;
61         unsigned long int f_ffree;
62         unsigned long int f_favail;
63         unsigned long int f_fsid;
64 #ifdef _STATVFSBUF_F_UNUSED
65         int __f_unused;
66 #endif
67         unsigned long int f_flag;
68         unsigned long int f_namemax;
69         int __f_spare[6];
70 };
71
72 bool ms_is_mmc_inserted(void)
73 {
74         bool ret = FALSE;
75         ms_stg_type_e stg_type = MS_STG_TYPE_MMC;
76         GArray *dev_list = NULL;
77
78         ret = ms_sys_get_device_list(stg_type, &dev_list);
79         if (ret == MS_MEDIA_ERR_NONE) {
80                 if (dev_list != NULL) {
81                         MS_DBG_WARN("MMC FOUND[%d]", dev_list->len);
82                         ms_sys_release_device_list(&dev_list);
83                         ret = TRUE;
84                 } else {
85                         MS_DBG_ERR("MMC NOT FOUND");
86                 }
87         } else {
88                 MS_DBG_ERR("ms_sys_get_device_list failed");
89         }
90
91         return ret;
92 }
93
94 static char* __media_get_path(uid_t uid)
95 {
96         int ret = -1;
97         char *grpbuf = NULL;
98         char *result_passwd = NULL;
99         struct group grpinfo;
100         struct group *grpresult = NULL;
101         size_t grpbufsize;
102
103         grpbufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
104         if (grpbufsize == -1)           /* Value was indeterminate */
105                 grpbufsize = 16384;             /* Should be more than enough (16*1024) */
106
107         grpbuf = malloc(grpbufsize);
108         if (grpbuf == NULL) {
109                 MS_DBG_ERR("malloc grpbuf grpbufsize[%d] failed", grpbufsize);
110                 goto END;
111         }
112
113         ret = getgrnam_r("users", &grpinfo, grpbuf, grpbufsize, &grpresult);
114         if((ret == 0) && (grpresult != NULL)) {
115 //              MS_DBG("getgrnam_r users success...\n");
116         } else {
117                 MS_DBG_ERR("getgrnam_r users failed ret[%d]", ret);
118                 goto END;
119         }
120
121         if (uid == getuid()) {
122                 if (MS_STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
123                         result_passwd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
124         } else {
125                 struct passwd pwdinfo;
126                 struct passwd *pwdresult = NULL;
127                 char *pwdbuf;
128                 size_t pwdbufsize;
129
130                 pwdbufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
131                 if (pwdbufsize == -1)           /* Value was indeterminate */
132                         pwdbufsize = 16384;             /* Should be more than enough (16*1024) */
133
134                 pwdbuf = malloc(pwdbufsize);
135                 if (pwdbuf == NULL) {
136                         MS_DBG_ERR("malloc pwdbuf pwdbufsize[%d] failed", pwdbufsize);
137                         goto END;
138                 }
139
140                 ret = getpwuid_r(uid, &pwdinfo, pwdbuf, pwdbufsize, &pwdresult);
141                 if((ret == 0) && (pwdresult != NULL)) {
142 //                      MS_DBG("getpwuid uid[%d] success\n", uid);
143                 } else {
144                         MS_DBG_ERR("getpwuid uid[%d] failed ret[%d]", uid, ret);
145                         MS_SAFE_FREE(pwdbuf);
146                         goto END;
147                 }
148
149                 // Compare git_t type and not group name
150                 if (grpinfo.gr_gid != pwdinfo.pw_gid) {
151                         MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
152                         MS_SAFE_FREE(pwdbuf);
153                         goto END;
154                 }
155                 result_passwd = strndup(pwdinfo.pw_dir, strlen(pwdinfo.pw_dir));
156
157                 MS_SAFE_FREE(pwdbuf);
158         }
159
160 END:
161         MS_SAFE_FREE(grpbuf);
162
163         return result_passwd;
164 }
165
166
167 ms_storage_type_t ms_get_storage_type_by_full(const char *path, uid_t uid)
168 {
169         int length_path;
170         int ret = MS_MEDIA_ERR_NONE;
171         char * user_path = NULL;
172
173         if (path == NULL)
174                 return MS_MEDIA_ERR_INVALID_PATH;
175
176 #ifdef _USE_SENIOR_MODE
177         if(ms_is_support_senior_mode()) {
178                 if (strncmp(path, MEDIA_ROOT_PATH_SENIOR_MODE, strlen(MEDIA_ROOT_PATH_SENIOR_MODE)) == 0) {
179                         return MS_STORAGE_EXTERNAL;
180                 }
181         }
182 #endif
183
184         user_path = __media_get_path(uid);
185         if (user_path == NULL)
186                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
187
188         length_path = strlen(user_path);
189
190         if (strncmp(path, user_path, length_path) == 0) {
191                 ret = MS_STORAGE_INTERNAL;
192         } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
193                 ret = MS_STORAGE_EXTERNAL;
194         } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
195                 ret = MS_STORAGE_EXTERNAL_USB;
196         } else if (MS_STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0)) {
197                 ret = MS_STORAGE_EXTERNAL_USB;
198         } else {
199                 MS_DBG_ERR("[%s][%s][%s]", MEDIA_ROOT_PATH_SDCARD, MEDIA_ROOT_PATH_USB, path);
200                 ret = MS_MEDIA_ERR_INVALID_PATH;
201         }
202
203         MS_SAFE_FREE(user_path);
204
205         return ret;
206 }
207
208 int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
209 {
210         int len = 0;
211         int real_size = size - 1;
212
213         if (!res || !pattern || !str1 || !str2)
214                 return MS_MEDIA_ERR_INVALID_PARAMETER;
215
216         if (real_size < (int)(strlen(str1) + strlen(str2)))
217                 return MS_MEDIA_ERR_INVALID_PARAMETER;
218
219         len = snprintf(res, real_size, pattern, str1, str2);
220         if (len < 0) {
221                 return MS_MEDIA_ERR_INVALID_PARAMETER;
222         }
223
224         res[len] = '\0';
225
226         return MS_MEDIA_ERR_NONE;
227 }
228
229 int ms_strcopy(char *res, const int size, const char *pattern, const char *str1)
230 {
231         int len = 0;
232         int real_size = size;
233
234         if (!res || !pattern || !str1) {
235                 MS_DBG_ERR("parameta is invalid");
236                 return MS_MEDIA_ERR_INVALID_PARAMETER;
237         }
238
239         if (real_size < (int)(strlen(str1))) {
240                 MS_DBG_ERR("size is wrong");
241                 return MS_MEDIA_ERR_INVALID_PARAMETER;
242         }
243
244         len = snprintf(res, real_size, pattern, str1);
245         if (len < 0) {
246                 MS_DBG_ERR("snprintf failed");
247                 return MS_MEDIA_ERR_INVALID_PARAMETER;
248         }
249
250         res[len] = '\0';
251
252         return MS_MEDIA_ERR_NONE;
253 }
254
255 bool ms_config_get_int(const char *key, int *value)
256 {
257         int err;
258
259         if (!key || !value) {
260                 MS_DBG_ERR("Arguments key or value is NULL");
261                 return false;
262         }
263
264         err = vconf_get_int(key, value);
265         if (err == 0)
266                 return true;
267         else if (err == -1)
268                 return false;
269         else
270                 MS_DBG_ERR("Unexpected error code: %d", err);
271
272         return false;
273 }
274
275 bool ms_config_set_int(const char *key, int value)
276 {
277         int err;
278
279         if (!key) {
280                 MS_DBG_ERR("Arguments key is NULL");
281                 return false;
282         }
283
284         err = vconf_set_int(key, value);
285         if (err == 0)
286                 return true;
287         else if (err == -1)
288                 return false;
289         else
290                 MS_DBG_ERR("Unexpected error code: %d", err);
291
292         return false;
293 }
294
295 bool ms_config_get_str(const char *key, char **value)
296 {
297         char *res = NULL;
298
299         if (key == NULL || value == NULL) {
300                 MS_DBG_ERR("Arguments key or value is NULL");
301                 return false;
302         }
303
304         res = vconf_get_str(key);
305         if (MS_STRING_VALID(res)) {
306                 *value = strdup(res);
307                 MS_SAFE_FREE(res);
308                 return true;
309         }
310
311         return false;
312 }
313
314 bool ms_config_set_str(const char *key, const char *value)
315 {
316         int err;
317
318         if (!key || !value) {
319                 MS_DBG_ERR("Arguments key or value is NULL");
320                 return false;
321         }
322
323         err = vconf_set_str(key, value);
324         if (err == 0)
325                 return true;
326         else
327                 MS_DBG_ERR("fail to vconf_set_str %d", err);
328
329         return false;
330 }
331
332 bool ms_config_get_bool(const char *key, int *value)
333 {
334         int err;
335
336         if (!key || !value) {
337                 MS_DBG_ERR("Arguments key or value is NULL");
338                 return false;
339         }
340
341         err = vconf_get_bool(key, value);
342         if (err == 0)
343                 return true;
344         else if (err == -1)
345                 return false;
346         else
347                 MS_DBG_ERR("Unexpected error code: %d", err);
348
349         return false;
350 }
351
352 static int get_memory_size(const char *path, struct statvfs_32 *buf)
353 {
354         struct statvfs s;
355         int ret;
356
357         ret = statvfs(path, &s);
358         if (ret) {
359                 MS_DBG_ERR("statvfs failed[%d]", ret);
360                 MS_DBG_STRERROR();
361                 return MS_MEDIA_ERR_INTERNAL;
362         }
363
364         buf->f_bsize  = s.f_bsize;
365         buf->f_frsize = s.f_frsize;
366         buf->f_blocks = (unsigned long)s.f_blocks;
367         buf->f_bfree  = (unsigned long)s.f_bfree;
368         buf->f_bavail = (unsigned long)s.f_bavail;
369         buf->f_files  = (unsigned long)s.f_files;
370         buf->f_ffree  = (unsigned long)s.f_ffree;
371         buf->f_favail = (unsigned long)s.f_favail;
372         buf->f_fsid = s.f_fsid;
373         buf->f_flag = s.f_flag;
374         buf->f_namemax = s.f_namemax;
375
376         return MS_MEDIA_ERR_NONE;
377 }
378
379 int ms_get_remain_space(double *free_space)
380 {
381         int ret = MS_MEDIA_ERR_NONE;
382         struct statvfs_32 temp;
383
384         ret = get_memory_size("/opt", &temp);
385         if (ret != MS_MEDIA_ERR_NONE) {
386                 MS_DBG_ERR("fail to get memory size");
387                 return ret;
388         }
389
390 //      MS_DBG_ERR("Total mem : %lf, Avail mem : %lf", (double)temp.f_frsize*temp.f_blocks, (double)temp.f_bsize*temp.f_bavail);
391
392         *free_space = (double)temp.f_bsize*temp.f_bavail;
393
394         return ret;
395 }
396
397 #ifdef _USE_RECORDED_CONTENT
398 bool ms_is_support_pvr(void)
399 {
400
401         int nSupportPVR = 0;
402         if (system_info_get_value_int(SYSTEM_INFO_KEY_PVR_SUPPORTED, &nSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
403                 MS_DBG_ERR("Get PVR Support failed");
404                 return false;
405         }
406
407         MS_DBG("PVR Support : [%d]", nSupportPVR);
408
409         return (nSupportPVR != 0);
410 }
411 #endif
412
413 #ifdef _USE_SENIOR_MODE
414 bool ms_is_support_senior_mode()
415 {
416         bool bSupportSeniorMode = false;
417
418         if(system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
419                 MS_DBG_ERR("Get senior mode support failed");
420                 return false;
421         }
422
423         MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
424
425         return bSupportSeniorMode;
426 }
427 #endif
428
429 int ms_check_file_path(const char *file_path, uid_t uid)
430 {
431         int exist;
432         struct stat file_st;
433
434         /* check location of file */
435         /* file must exists under "/opt/usr/media" or "/opt/storage/sdcard" */
436         if (!ms_is_valid_path(file_path, uid)) {
437                 MS_DBG_ERR("Invalid path : %s", file_path);
438                 return MS_MEDIA_ERR_INVALID_PATH;
439         }
440
441         /* check the file exits actually */
442         exist = open(file_path, O_RDONLY);
443         if (exist < 0) {
444                 MS_DBG_ERR("[%s]open files");
445                 return MS_MEDIA_ERR_INVALID_PATH;
446         }
447         close(exist);
448
449         /* check type of the path */
450         /* It must be a regular file */
451         memset(&file_st, 0, sizeof(struct stat));
452         if (stat(file_path, &file_st) == 0) {
453                 if (!S_ISREG(file_st.st_mode)) {
454                         /* In this case, it is not a regula file */
455                         MS_DBG_ERR("this path is not a file");
456                         return MS_MEDIA_ERR_INVALID_PATH;
457                 }
458         } else {
459                 MS_DBG_STRERROR("stat failed");
460                 return MS_MEDIA_ERR_INVALID_PATH;
461         }
462
463         return MS_MEDIA_ERR_NONE;
464 }
465
466 int ms_check_ignore_dir(const char *full_path, uid_t uid)
467 {
468         int ret = MS_MEDIA_ERR_NONE;
469         char *dir_path = NULL;
470         char *leaf_path = NULL;
471         char *usr_path = NULL;
472
473         ret = ms_check_file_path(full_path, uid);
474         if (ret != MS_MEDIA_ERR_NONE) {
475                 MS_DBG_ERR("invalid path : %s", full_path);
476                 return MS_MEDIA_ERR_INVALID_PATH;
477         }
478
479         dir_path = g_path_get_dirname(full_path);
480         if (dir_path == NULL || strcmp(dir_path, ".") == 0) {
481                 MS_DBG_ERR("getting directory path is failed : %s", full_path);
482                 MS_SAFE_FREE(dir_path);
483                 return MS_MEDIA_ERR_INVALID_PATH;
484         }
485
486         usr_path = ms_get_path(uid);
487         if (usr_path == NULL) {
488                 MS_DBG_ERR("ms_get_path() fail");
489                 MS_SAFE_FREE(dir_path);
490                 return MS_MEDIA_ERR_INTERNAL;
491         }
492
493         while (1) {
494                 if (ms_check_scan_ignore(dir_path) != MS_MEDIA_ERR_NONE) {
495                         ret = MS_MEDIA_ERR_INVALID_PATH;
496                         break;
497                 }
498
499 #ifdef _USE_SENIOR_MODE
500                 if(ms_is_support_senior_mode()) {
501                         if(strcmp(dir_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)
502                                 break;
503                 }
504 #endif
505                 if (strcmp(dir_path, usr_path) == 0)
506                         break;
507                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
508                         break;
509                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(dir_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
510                         break;
511                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(dir_path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0))
512                         break;
513
514                 leaf_path = strrchr(dir_path, '/');
515                 if (leaf_path != NULL) {
516                                 int seek_len = leaf_path -dir_path;
517                                 dir_path[seek_len] = '\0';
518                 } else {
519                         MS_DBG_ERR("Fail to find leaf path");
520                         ret = MS_MEDIA_ERR_INVALID_PATH;
521                         break;
522                 }
523         }
524
525         MS_SAFE_FREE(dir_path);
526         MS_SAFE_FREE(usr_path);
527
528         return ret;
529 }
530
531 int ms_check_scan_ignore(char * path)
532 {
533         int fd = -1;
534         int exist = -1;
535         const char *ignore_path = "/.scan_ignore";
536         char *check_ignore_file = NULL;
537         int ret = MS_MEDIA_ERR_NONE;
538
539         if (strstr(path, "/.")) {
540                 MS_DBG_ERR("hidden path");
541                 ret = MS_MEDIA_ERR_INVALID_PATH;
542                 goto ERROR;
543         }
544
545         fd = open(path, O_RDONLY | O_DIRECTORY);
546         if (fd == -1) {
547                 MS_DBG_ERR("%s folder opendir fails", path);
548                 ret = MS_MEDIA_ERR_INVALID_PATH;
549
550                 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
551                         MS_DBG_ERR("Fail to get USB path");
552                         goto ERROR;
553                 }
554
555                 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
556                         if (errno == ENOENT) {
557                                 /*if the directory does not exist, check the device is unmounted*/
558                                 if (!ms_storage_mount_status(path)) {
559                                         MS_DBG_ERR("Device is unmounted[%s]", path);
560                                         ret = MS_MEDIA_ERR_USB_UNMOUNTED;
561                                         goto ERROR;
562                                 }
563                         }
564                 }
565
566                 struct stat folder_st;
567                 if (stat(path, &folder_st) == 0) {
568                         MS_DBG_WARN("DEV[%ld] INODE[%lld] UID[%ld] GID[%ld] MODE[%lo] PATH[%s]", (long)folder_st.st_dev, (long long)folder_st.st_ino,
569                                 (long)folder_st.st_uid, (long)folder_st.st_gid, (unsigned long) folder_st.st_mode, path);
570                 } else {
571                         MS_DBG_ERR("%s folder stat fails", path);
572                 }
573
574                 goto ERROR;
575         } else {
576                 /* check the file exits actually */
577                 int path_len = 0;
578
579                 path_len = strlen(path) + strlen(ignore_path) + 1;
580                 check_ignore_file = malloc(path_len);
581                 if (check_ignore_file != NULL) {
582                         memset(check_ignore_file, 0x0, path_len);
583                         snprintf(check_ignore_file, path_len, "%s%s", path, ignore_path);
584
585                         exist = open(check_ignore_file, O_RDONLY);
586                         if (exist >= 0) {
587                                 MS_DBG_WARN("scan_ignore exists [%s]", check_ignore_file);
588                                 ret = MS_MEDIA_ERR_INVALID_PATH;
589                         }
590
591                         MS_SAFE_FREE(check_ignore_file);
592                 } else {
593                         MS_DBG_ERR("malloc failed");
594                         ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
595                 }
596         }
597
598 ERROR:
599
600         if (fd != -1) {
601                 close(fd);
602                 fd = -1;
603         }
604
605         if (exist >= 0) close(exist);
606
607         return ret;
608 }
609
610 char* ms_get_path(uid_t uid)
611 {
612         int len = 0;
613         char *result_passwd = NULL;
614         int ret = -1;
615         char* grpbuf;
616         struct group grpinfo;
617         struct group* grpresult = NULL;
618         size_t grpbufsize;
619
620         grpbufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
621         if (grpbufsize == -1)           /* Value was indeterminate */
622                 grpbufsize = 16384;             /* Should be more than enough (16*1024) */
623
624         grpbuf = malloc(grpbufsize);
625         if (grpbuf == NULL) {
626                 MS_DBG_ERR("malloc grpbuf grpbufsize[%d] failed", grpbufsize);
627                 return NULL;
628         }
629
630         ret = getgrnam_r("users", &grpinfo, grpbuf, grpbufsize, &grpresult);
631         if((ret == 0) && (grpresult != NULL)) {
632 //              MS_DBG("getgrnam_r users success...\n");
633         } else {
634                 MS_DBG_ERR("getgrnam_r users failed ret[%d]", ret);
635                 goto END;
636         }
637
638         if (uid == getuid()) {
639                 if (MS_STRING_VALID(MEDIA_ROOT_PATH_INTERNAL))
640                         result_passwd = strndup(MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL));
641         } else {
642                 char passwd_str[MAX_FILEPATH_LEN] = {0, };
643                 struct passwd pwdinfo;
644                 struct passwd* pwdresult = NULL;
645                 char* pwdbuf;
646                 size_t pwdbufsize;
647
648                 pwdbufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
649                 if (pwdbufsize == -1)           /* Value was indeterminate */
650                         pwdbufsize = 16384;             /* Should be more than enough (16*1024) */
651
652                 pwdbuf = malloc(pwdbufsize);
653                 if (pwdbuf == NULL) {
654                         MS_DBG_ERR("malloc pwdbuf pwdbufsize[%d] failed", pwdbufsize);
655                         goto END;
656                 }
657
658                 ret = getpwuid_r(uid, &pwdinfo, pwdbuf, pwdbufsize, &pwdresult);
659                 if((ret == 0) && (pwdresult != NULL)) {
660 //                      MS_DBG("getpwuid uid[%d] success\n", uid);
661                 } else {
662                         MS_DBG_ERR("getpwuid uid[%d] failed ret[%d]", uid, ret);
663                         MS_SAFE_FREE(pwdbuf);
664                         goto END;
665                 }
666
667                 // Compare git_t type and not group name
668                 if (grpinfo.gr_gid != pwdinfo.pw_gid) {
669                         MS_DBG_ERR("UID [%d] does not belong to 'users' group!", uid);
670                         MS_SAFE_FREE(pwdbuf);
671                         goto END;
672                 }
673
674                 len = snprintf(passwd_str, sizeof(passwd_str), "%s/%s", pwdinfo.pw_dir, MEDIA_CONTENT_PATH);
675                 if (len > 0)
676                         result_passwd = strndup(passwd_str, len);
677
678                 MS_SAFE_FREE(pwdbuf);
679         }
680
681 END:
682         MS_SAFE_FREE(grpbuf);
683
684         return result_passwd;
685 }
686
687 bool ms_storage_mount_status(const char* start_path)
688 {
689         bool ret = false;
690 #ifndef _USE_DEVICED_DBUS
691         int count = 0;
692         int err = 0;
693         usb_device_list_h list;
694         usb_device_h device;
695         char *mount_path = NULL;
696
697         char *storage_path = NULL;
698         char *remain_path = NULL;
699         int remain_len = 0;
700
701         remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
702         if (remain_path != NULL)
703                 remain_len = strlen(remain_path);
704
705         storage_path = strndup(start_path, strlen(start_path) - remain_len);
706
707         MS_DBG_WARN("storage_path [%s]", storage_path);
708
709         err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
710         if (err == 0) {
711                 count = usb_device_list_get_count(list);
712                 if (count > 0) {
713                         err = usb_device_list_get_first(list, &device);
714                         if(err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
715                                 mount_path = usb_device_get_mountpath(device);
716                                 if (mount_path != NULL) {
717                                         MS_DBG_WARN("mount_path [%s]", mount_path);
718                                         if (strlen(mount_path) == strlen(storage_path)) {
719                                                 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
720                                                         MS_DBG_WARN("start path is mounted [%s]", start_path);
721                                                         ret = true;
722                                                 }
723                                         }
724                                 }
725                         }
726
727                         if (ret != true) {
728                                 while(usb_device_list_get_next(list, &device) == 0) {
729                                         if(device != NULL) {
730                                                 mount_path = usb_device_get_mountpath(device);
731                                                 if (mount_path != NULL) {
732                                                         MS_DBG_WARN("mount_path [%s]", mount_path);
733                                                         if (strlen(mount_path) == strlen(storage_path)) {
734                                                                 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
735                                                                         MS_DBG_WARN("start path is mounted [%s]", start_path);
736                                                                         ret = true;
737                                                                         break;
738                                                                 }
739                                                         }
740                                                 }
741                                         }
742                                 }
743                         }
744                 }
745
746                 usb_device_free_device_list(list);
747         } else {
748                 MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
749         }
750
751         MS_SAFE_FREE(storage_path);
752 #endif
753         return ret;
754 }
755
756 bool ms_is_valid_path(const char *path, uid_t uid)
757 {
758         bool ret = false;
759         char *usr_path = NULL;
760
761         if (path == NULL)
762                 return false;
763
764         usr_path = ms_get_path(uid);
765         if (usr_path == NULL)
766                 return false;
767
768 #ifdef _USE_SENIOR_MODE
769         if(ms_is_support_senior_mode()) {
770                 if (strncmp(path, MEDIA_ROOT_PATH_SENIOR_MODE, strlen(MEDIA_ROOT_PATH_SENIOR_MODE)) == 0)
771                         return true;
772         }
773 #endif
774
775         if (strncmp(path, usr_path, strlen(usr_path)) == 0)
776                 ret = true;
777         else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
778                 ret = true;
779         else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
780                 ret = true;
781         else if (MS_STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0))
782                 ret = true;
783         else
784                 ret = false;
785
786         MS_SAFE_FREE(usr_path);
787
788         return ret;
789 }
790
791 int ms_set_db_status(ms_db_status_type_t status, ms_storage_type_t storage_type)
792 {
793         int res = MS_MEDIA_ERR_NONE;
794         int err = 0;
795
796         if (status == MS_DB_UPDATING) {
797                 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) {
798                         res = MS_MEDIA_ERR_VCONF_SET_FAIL;
799                         MS_DBG_ERR("ms_config_set_int failed");
800                 }
801
802                 if (storage_type == MS_STORAGE_EXTERNAL) {
803                         if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING)) {
804                                 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
805                                 MS_DBG_ERR("ms_config_set_int failed");
806                         }
807                 }
808         } else {
809                 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) {
810                         res = MS_MEDIA_ERR_VCONF_SET_FAIL;
811                         MS_DBG_ERR("ms_config_set_int failed");
812                 }
813
814                 if (storage_type == MS_STORAGE_EXTERNAL) {
815                         if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED)) {
816                                 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
817                                 MS_DBG_ERR("ms_config_set_int failed");
818                         }
819                 }
820         }
821
822         err = ms_set_power_mode(status);
823         if (err != MS_MEDIA_ERR_NONE) {
824                 MS_DBG_ERR("ms_set_power_mode fail");
825                 res = err;
826         }
827
828         return res;
829 }
830
831 int ms_set_power_mode(ms_db_status_type_t status)
832 {
833         int res = MS_MEDIA_ERR_NONE;
834         int err;
835
836         switch (status) {
837         case MS_DB_UPDATING:
838                 err = display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
839                 if (err != 0)
840                         res = MS_MEDIA_ERR_INTERNAL;
841                 break;
842         case MS_DB_UPDATED:
843                 err = display_unlock_state(LCD_OFF, PM_RESET_TIMER);
844                 if (err != 0)
845                         res = MS_MEDIA_ERR_INTERNAL;
846                 break;
847         default:
848                 MS_DBG_ERR("Unacceptable type : %d", status);
849                 break;
850         }
851
852         return res;
853 }
854
855 void ms_trim_dir_path(char *dir_path)
856 {
857         /* need implementation */
858         /* if dir_path is not NULL terminated, this function will occure crash */
859         int len = strlen(dir_path);
860
861         if (dir_path[len -1] == '/')
862                 dir_path[len -1] = '\0';
863 }
864
865 bool ms_check_folder_path(const char *folder_path)
866 {
867         DIR *dp = NULL;
868
869         dp = opendir(folder_path);
870         if (dp == NULL) {
871                 MS_DBG_ERR("Deleted folder path");
872                 return false;
873         }
874         closedir(dp);
875
876         return true;
877 }
878
879 int ms_check_size_mediadb(uid_t uid, double *db_size)
880 {
881         int ret = MS_MEDIA_ERR_NONE;
882         char *db_path = NULL;
883         struct stat buf;
884
885         ret = media_db_get_media_db_path(uid, &db_path);
886
887         if(stat(db_path, &buf) == 0) {
888                 *db_size = buf.st_size;
889         } else {
890                 MS_DBG_STRERROR("stat failed");
891                 ret = MS_MEDIA_ERR_INTERNAL;
892         }
893
894         MS_SAFE_FREE(db_path);
895
896         return ret;
897 }
898
899 #ifdef _SET_VIP_PROCESS
900 #define PROC_OOM_SCORE_ADJ_PATH         "/proc/%d/oom_score_adj"
901 #define VIP_OOM_SCORE_ADJ                       (-1000)
902 #define PROC_NAME_MAX 1024
903 #define PROC_BUF_MAX 64
904
905 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
906 {
907         char buf[PROC_BUF_MAX];
908         char cmdline_buf[PROC_NAME_MAX];
909         char *filename;
910         FILE *fp;
911
912         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
913         fp = fopen(buf, "r");
914         if (fp == NULL)
915                 return MS_MEDIA_ERR_INTERNAL;
916
917         if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
918                 fclose(fp);
919                 return MS_MEDIA_ERR_INTERNAL;
920         }
921         fclose(fp);
922
923         filename = strrchr(cmdline_buf, '/');
924         if (filename == NULL)
925                 filename = cmdline_buf;
926         else
927                 filename = filename + 1;
928
929         strncpy(cmdline, filename, PROC_NAME_MAX-1);
930
931         return MS_MEDIA_ERR_NONE;
932 }
933
934 int ms_set_vip_process(void)
935 {
936         char buf[100] = {0};
937         int id = 0;
938         static pid_t pid = 0;
939         static char process_name[PROC_NAME_MAX] = {0};
940         static char *appid = NULL;
941
942         /* Get Pid */
943         pid = getpid();
944         if (ms_get_cmdline_from_proc(pid, process_name)) {
945                 MS_DBG_ERR("%s: Read process name failed pid[%d]\n", __func__, pid);
946                 return MS_MEDIA_ERR_INTERNAL;
947         }
948         appid = process_name;
949
950         MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
951
952         if (prctl(PR_GET_DUMPABLE) == 0)
953                 prctl(PR_SET_DUMPABLE, 1);
954
955         snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
956         id = open(buf, O_WRONLY, 0777);
957         if (id < 0) {
958                 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
959                 return MS_MEDIA_ERR_INTERNAL;
960         }
961         snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
962         if (write(id, buf, strlen(buf)) < 0) {
963                 MS_DBG_ERR("write() failed errno=%d", errno);
964                 close(id);
965                 return MS_MEDIA_ERR_INTERNAL;
966         }
967         close(id);
968         return MS_MEDIA_ERR_NONE;
969 }
970 #endif