Add function to trim the 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 #include <sys/types.h>
23 #include <fcntl.h>
24 #include <vconf.h>
25 #include <sys/statvfs.h>
26 #include <sys/stat.h>
27
28 #ifdef _SET_VIP_PROCESS
29 #include <sys/prctl.h>
30 #endif
31 #include <system_info.h>
32 #include <device/power.h>
33
34 #ifndef _USE_DEVICED_DBUS
35 #include <usb-device.h>
36 #endif
37
38 #include "media-util.h"
39 #include "media-server-ipc.h"
40 #include "media-common-dbg.h"
41 #include "media-common-system.h"
42 #include "media-common-utils.h"
43
44 int ms_strappend(char *res, const int size, const char *pattern, const char *str1, const char *str2)
45 {
46         int len = 0;
47         int real_size = size - 1;
48
49         if (!res || !pattern || !str1 || !str2)
50                 return MS_MEDIA_ERR_INVALID_PARAMETER;
51
52         if (real_size < (int)(strlen(str1) + strlen(str2)))
53                 return MS_MEDIA_ERR_INVALID_PARAMETER;
54
55         len = snprintf(res, real_size, pattern, str1, str2);
56         if (len < 0)
57                 return MS_MEDIA_ERR_INVALID_PARAMETER;
58
59         res[len] = '\0';
60
61         return MS_MEDIA_ERR_NONE;
62 }
63
64 bool ms_config_get_int(const char *key, int *value)
65 {
66         int err;
67
68         if (!key || !value) {
69                 MS_DBG_ERR("Arguments key or value is NULL");
70                 return false;
71         }
72
73         err = vconf_get_int(key, value);
74         if (err == VCONF_OK)
75                 return true;
76
77         MS_DBG_ERR("Error code: %d", err);
78
79         return false;
80 }
81
82 bool ms_config_set_int(const char *key, int value)
83 {
84         int err;
85
86         if (!key) {
87                 MS_DBG_ERR("Arguments key is NULL");
88                 return false;
89         }
90
91         err = vconf_set_int(key, value);
92         if (err == VCONF_OK)
93                 return true;
94
95         MS_DBG_ERR("Error code: %d", err);
96
97         return false;
98 }
99
100 bool ms_config_get_str(const char *key, char **value)
101 {
102         char *res = NULL;
103
104         if (key == NULL || value == NULL) {
105                 MS_DBG_ERR("Arguments key or value is NULL");
106                 return false;
107         }
108
109         res = vconf_get_str(key);
110         if (MS_STRING_VALID(res)) {
111                 *value = strdup(res);
112                 MS_SAFE_FREE(res);
113                 return true;
114         }
115
116         return false;
117 }
118
119 int ms_get_remain_space(double *free_space)
120 {
121         int ret = MS_MEDIA_ERR_NONE;
122         const char *path = "/opt";
123         struct statvfs s;
124
125         ret = statvfs(path, &s);
126         if (ret != 0) {
127                 MS_DBG_ERR("statvfs failed[%d]", ret);
128                 MS_DBG_STRERROR();
129                 return MS_MEDIA_ERR_INTERNAL;
130         }
131
132         /* f_bsize:unsigned long, f_bavail:fsblkcnt_t(unsigned long) */
133         *free_space = (double)(s.f_bsize * s.f_bavail);
134
135         return MS_MEDIA_ERR_NONE;
136 }
137
138 #ifdef _USE_RECORDED_CONTENT
139 bool ms_is_support_pvr(void)
140 {
141
142         int nSupportPVR = 0;
143         if (system_info_get_value_int(SYSTEM_INFO_KEY_PVR_SUPPORTED, &nSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
144                 MS_DBG_ERR("Get PVR Support failed");
145                 return false;
146         }
147
148         MS_DBG("PVR Support : [%d]", nSupportPVR);
149
150         return (nSupportPVR != 0);
151 }
152 #endif
153
154 #ifdef _USE_SENIOR_MODE
155 bool ms_is_support_senior_mode()
156 {
157         bool bSupportSeniorMode = false;
158
159         if (system_info_get_value_bool(SYSTEM_INFO_KEY_GET_SENIOR_MODE_SUPPORTED, &bSupportSeniorMode) != SYSTEM_INFO_ERROR_NONE) {
160                 MS_DBG_ERR("Get senior mode support failed");
161                 return false;
162         }
163
164         MS_DBG("Senior mode support : [%d]", bSupportSeniorMode);
165
166         return bSupportSeniorMode;
167 }
168 #endif
169
170 int ms_check_file_path(const char *file_path, uid_t uid)
171 {
172         ms_user_storage_type_e storage_type = -1;
173         int ret = MS_MEDIA_ERR_NONE;
174
175         if (!MS_STRING_VALID(file_path)) {
176                 MS_DBG_ERR("Invalid path");
177                 return MS_MEDIA_ERR_INVALID_PARAMETER;
178         }
179
180         ret = ms_user_get_storage_type(uid, file_path, &storage_type);
181         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PATH, "Invalid path");
182
183         if (!g_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
184                 MS_DBG_SERR("g_file_test fail [%s]", file_path);
185                 return MS_MEDIA_ERR_INVALID_PATH;
186         }
187
188         return MS_MEDIA_ERR_NONE;
189 }
190
191 int ms_check_ignore_dir(const char *full_path, uid_t uid)
192 {
193         int ret = MS_MEDIA_ERR_NONE;
194         char *dir_path = NULL;
195         char *leaf_path = NULL;
196         char *usr_path = NULL;
197
198         ret = ms_check_file_path(full_path, uid);
199         if (ret != MS_MEDIA_ERR_NONE) {
200                 MS_DBG_ERR("invalid path : %s", full_path);
201                 return MS_MEDIA_ERR_INVALID_PATH;
202         }
203
204         dir_path = g_path_get_dirname(full_path);
205         if (dir_path == NULL || strcmp(dir_path, ".") == 0) {
206                 MS_DBG_ERR("getting directory path is failed : %s", full_path);
207                 MS_SAFE_FREE(dir_path);
208                 return MS_MEDIA_ERR_INVALID_PATH;
209         }
210
211         ret = ms_user_get_internal_root_path(uid, &usr_path);
212         if (ret != MS_MEDIA_ERR_NONE) {
213                 MS_DBG_ERR("ms_user_get_internal_root_path() fail");
214                 MS_SAFE_FREE(dir_path);
215                 return MS_MEDIA_ERR_INTERNAL;
216         }
217
218         while (1) {
219                 if (ms_check_scan_ignore(dir_path, uid) != MS_MEDIA_ERR_NONE) {
220                         ret = MS_MEDIA_ERR_INVALID_PATH;
221                         break;
222                 }
223
224 #ifdef _USE_SENIOR_MODE
225                 if (ms_is_support_senior_mode()) {
226                         if (strcmp(dir_path, MEDIA_ROOT_PATH_SENIOR_MODE) == 0)
227                                 break;
228                 }
229 #endif
230                 if (strcmp(dir_path, usr_path) == 0)
231                         break;
232                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(dir_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0))
233                         break;
234                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(dir_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0))
235                         break;
236                 else if (MS_STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(dir_path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0))
237                         break;
238
239                 leaf_path = strrchr(dir_path, '/');
240                 if (leaf_path != NULL) {
241                                 int seek_len = leaf_path -dir_path;
242                                 dir_path[seek_len] = '\0';
243                 } else {
244                         MS_DBG_ERR("Fail to find leaf path");
245                         ret = MS_MEDIA_ERR_INVALID_PATH;
246                         break;
247                 }
248         }
249
250         MS_SAFE_FREE(dir_path);
251         MS_SAFE_FREE(usr_path);
252
253         return ret;
254 }
255
256 void __ms_trim_path(const char *input_path, char **output_path)
257 {
258         char buf[4096] = {0,};
259         char tmp[4096] = {0,};
260         char *pos = NULL;
261
262         memset(buf, 0, sizeof(buf));
263         SAFE_STRLCPY(buf, input_path, sizeof(buf));
264
265         while ((pos = strstr(buf, "//")) != NULL) {
266                 memset(tmp, 0, sizeof(tmp));
267                 SAFE_STRLCPY(tmp, buf, pos - buf + 1);
268                 SAFE_STRLCAT(tmp, pos + 1, sizeof(tmp));
269
270                 memset(buf, 0, sizeof(buf));
271                 SAFE_STRLCPY(buf, tmp, sizeof(buf));
272         }
273
274         if (g_str_has_suffix(buf, "/"))
275                 *output_path = g_strndup(buf, strlen(buf) - 1);
276         else
277                 *output_path = g_strdup(buf);
278 }
279
280 int ms_check_scan_ignore(char * path, uid_t uid)
281 {
282         int ret = MS_MEDIA_ERR_NONE;
283         const char *ignore_file = ".scan_ignore";
284         char *tmp_path = NULL;
285         char *org_path = NULL;
286         char ignore_path[MS_FILE_PATH_LEN_MAX] = {0, };
287
288 #ifndef _USE_TVPD_MODE
289         char replace[MS_FILE_PATH_LEN_MAX] = {0, };
290         char *mediashared = NULL;
291 #endif
292
293         if (strstr(path, "/.")) {
294                 MS_DBG_ERR("hidden path");
295                 return MS_MEDIA_ERR_INVALID_PATH;
296         }
297
298         /* Check for symbolic link */
299         tmp_path = realpath(path, NULL);
300         /* Get trimmed path */
301         __ms_trim_path(path, &org_path);
302
303 #ifdef _USE_TVPD_MODE
304         if (g_strcmp0(tmp_path, org_path) != 0) {
305                 MS_SAFE_FREE(tmp_path);
306                 MS_SAFE_FREE(org_path);
307                 MS_DBG_ERR("symbolic link(directory)");
308                 return MS_MEDIA_ERR_INVALID_PATH;
309         }
310 #else
311         if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
312                 ms_user_get_mediashared_path(uid, &mediashared);
313                 snprintf(replace, MS_FILE_PATH_LEN_MAX, "%s%s", mediashared, tmp_path + strlen(MEDIA_SHARE_PATH));
314                 MS_SAFE_FREE(mediashared);
315                 if (g_strcmp0(replace, org_path) != 0) {
316                         MS_SAFE_FREE(tmp_path);
317                         MS_SAFE_FREE(org_path);
318                         MS_DBG_ERR("symbolic link(directory)");
319                         return MS_MEDIA_ERR_INVALID_PATH;
320                 }
321         } else {
322                 if (g_strcmp0(tmp_path, org_path) != 0) {
323                         MS_SAFE_FREE(tmp_path);
324                         MS_SAFE_FREE(org_path);
325                         MS_DBG_ERR("symbolic link(directory)");
326                         return MS_MEDIA_ERR_INVALID_PATH;
327                 }
328         }
329 #endif
330         MS_SAFE_FREE(tmp_path);
331         MS_SAFE_FREE(org_path);
332
333         if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
334                 memset(ignore_path, 0, sizeof(ignore_path));
335                 snprintf(ignore_path, sizeof(ignore_path), "%s/%s", path, ignore_file);
336
337                 if (g_file_test(ignore_path, G_FILE_TEST_EXISTS)) {
338                         MS_DBG_WARN("scan ignore file exist [%s]", ignore_path);
339                         return MS_MEDIA_ERR_INVALID_PATH;
340                 }
341         } else {
342                 MS_DBG_ERR("g_file_test fails[%s]", path);
343                 ret = MS_MEDIA_ERR_INVALID_PATH;
344
345                 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
346                         MS_DBG_ERR("Fail to get USB path");
347                         return ret;
348                 }
349
350                 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
351                         /*if the directory does not exist, check the device is unmounted*/
352                         if (!ms_storage_mount_status(path)) {
353                                 MS_DBG_ERR("Device is unmounted[%s]", path);
354                                 return MS_MEDIA_ERR_USB_UNMOUNTED;
355                         }
356                 }
357         }
358
359         return ret;
360 }
361
362 bool ms_storage_mount_status(const char* start_path)
363 {
364         bool ret = false;
365 #ifndef _USE_DEVICED_DBUS
366         int count = 0;
367         int err = 0;
368         usb_device_list_h list;
369         usb_device_h device;
370         char *mount_path = NULL;
371
372         char *storage_path = NULL;
373         char *remain_path = NULL;
374         int remain_len = 0;
375
376         remain_path = strstr(start_path+strlen(MEDIA_ROOT_PATH_USB) +1, "/");
377         if (remain_path != NULL)
378                 remain_len = strlen(remain_path);
379
380         storage_path = strndup(start_path, strlen(start_path) - remain_len);
381
382         MS_DBG_WARN("storage_path [%s]", storage_path);
383
384         err = usb_device_get_device_list(USB_MASS_STORAGE, &list);
385         if (err == 0) {
386                 count = usb_device_list_get_count(list);
387                 if (count > 0) {
388                         err = usb_device_list_get_first(list, &device);
389                         if (err != USB_ERROR_LIST_FAILED_TO_GET && device != NULL) {
390                                 mount_path = usb_device_get_mountpath(device);
391                                 if (mount_path != NULL) {
392                                         MS_DBG_WARN("mount_path [%s]", mount_path);
393                                         if (strlen(mount_path) == strlen(storage_path)) {
394                                                 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
395                                                         MS_DBG_WARN("start path is mounted [%s]", start_path);
396                                                         ret = true;
397                                                 }
398                                         }
399                                 }
400                         }
401
402                         if (ret != true) {
403                                 while (usb_device_list_get_next(list, &device) == 0) {
404                                         if (device != NULL) {
405                                                 mount_path = usb_device_get_mountpath(device);
406                                                 if (mount_path != NULL) {
407                                                         MS_DBG_WARN("mount_path [%s]", mount_path);
408                                                         if (strlen(mount_path) == strlen(storage_path)) {
409                                                                 if (strncmp(mount_path, storage_path, strlen(mount_path)) == 0) {
410                                                                         MS_DBG_WARN("start path is mounted [%s]", start_path);
411                                                                         ret = true;
412                                                                         break;
413                                                                 }
414                                                         }
415                                                 }
416                                         }
417                                 }
418                         }
419                 }
420
421                 usb_device_free_device_list(list);
422         } else {
423                 MS_DBG_ERR("usb_device_get_device_list falied [%d]", err);
424         }
425
426         MS_SAFE_FREE(storage_path);
427 #endif
428         return ret;
429 }
430
431 int ms_set_db_status(ms_db_status_type_t status, ms_user_storage_type_e storage_type)
432 {
433         int res = MS_MEDIA_ERR_NONE;
434         int err = 0;
435
436         if (status == MS_DB_UPDATING) {
437                 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) {
438                         res = MS_MEDIA_ERR_VCONF_SET_FAIL;
439                         MS_DBG_ERR("ms_config_set_int failed");
440                 }
441
442                 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
443                         if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING)) {
444                                 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
445                                 MS_DBG_ERR("ms_config_set_int failed");
446                         }
447                 }
448         } else {
449                 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) {
450                         res = MS_MEDIA_ERR_VCONF_SET_FAIL;
451                         MS_DBG_ERR("ms_config_set_int failed");
452                 }
453
454                 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
455                         if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED)) {
456                                 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
457                                 MS_DBG_ERR("ms_config_set_int failed");
458                         }
459                 }
460         }
461
462         err = ms_set_power_mode(status);
463         if (err != MS_MEDIA_ERR_NONE) {
464                 MS_DBG_ERR("ms_set_power_mode fail");
465                 res = err;
466         }
467
468         return res;
469 }
470
471 int ms_set_power_mode(ms_db_status_type_t status)
472 {
473         int res = MS_MEDIA_ERR_NONE;
474         int err;
475
476         switch (status) {
477         case MS_DB_UPDATING:
478                 err = device_power_request_lock(POWER_LOCK_CPU, 0);
479                 if (err != 0)
480                         res = MS_MEDIA_ERR_INTERNAL;
481                 break;
482         case MS_DB_UPDATED:
483                 err = device_power_release_lock(POWER_LOCK_CPU);
484                 if (err != 0)
485                         res = MS_MEDIA_ERR_INTERNAL;
486                 break;
487         default:
488                 MS_DBG_ERR("Unacceptable type : %d", status);
489                 break;
490         }
491
492         return res;
493 }
494
495 void ms_trim_dir_path(char *dir_path)
496 {
497         /* need implementation */
498         /* if dir_path is not NULL terminated, this function will occure crash */
499         int len = strlen(dir_path);
500
501         if (dir_path[len -1] == '/')
502                 dir_path[len -1] = '\0';
503 }
504
505 int ms_check_size_mediadb(uid_t uid, double *db_size)
506 {
507         int ret = MS_MEDIA_ERR_NONE;
508         char *db_path = NULL;
509         struct stat buf;
510
511         ret = ms_user_get_media_db_path(uid, &db_path);
512
513         if (stat(db_path, &buf) == 0) {
514                 *db_size = buf.st_size;
515         } else {
516                 MS_DBG_STRERROR("stat failed");
517                 ret = MS_MEDIA_ERR_INTERNAL;
518         }
519
520         MS_SAFE_FREE(db_path);
521
522         return ret;
523 }
524
525 #ifdef _SET_VIP_PROCESS
526 #define PROC_OOM_SCORE_ADJ_PATH         "/proc/%d/oom_score_adj"
527 #define VIP_OOM_SCORE_ADJ                       (-1000)
528 #define PROC_NAME_MAX 1024
529 #define PROC_BUF_MAX 64
530
531 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
532 {
533         char buf[PROC_BUF_MAX];
534         char cmdline_buf[PROC_NAME_MAX];
535         char *filename;
536         FILE *fp;
537
538         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
539         fp = fopen(buf, "r");
540         if (fp == NULL)
541                 return MS_MEDIA_ERR_INTERNAL;
542
543         if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
544                 fclose(fp);
545                 return MS_MEDIA_ERR_INTERNAL;
546         }
547         fclose(fp);
548
549         filename = strrchr(cmdline_buf, '/');
550         if (filename == NULL)
551                 filename = cmdline_buf;
552         else
553                 filename = filename + 1;
554
555         SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
556
557         return MS_MEDIA_ERR_NONE;
558 }
559
560 int ms_set_vip_process(void)
561 {
562         char buf[100] = {0};
563         int id = 0;
564         static pid_t pid = 0;
565         static char process_name[PROC_NAME_MAX] = {0};
566         static char *appid = NULL;
567
568         /* Get Pid */
569         pid = getpid();
570         if (ms_get_cmdline_from_proc(pid, process_name)) {
571                 MS_DBG_ERR("%s: Read process name failed pid[%d]", __func__, pid);
572                 return MS_MEDIA_ERR_INTERNAL;
573         }
574         appid = process_name;
575
576         MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
577
578         if (prctl(PR_GET_DUMPABLE) == 0)
579                 prctl(PR_SET_DUMPABLE, 1);
580
581         snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
582         id = open(buf, O_WRONLY, 0777);
583         if (id < 0) {
584                 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
585                 return MS_MEDIA_ERR_INTERNAL;
586         }
587         snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
588         if (write(id, buf, strlen(buf)) < 0) {
589                 MS_DBG_ERR("write() failed errno=%d", errno);
590                 close(id);
591                 return MS_MEDIA_ERR_INTERNAL;
592         }
593         close(id);
594         return MS_MEDIA_ERR_NONE;
595 }
596 #endif