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