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