Use g_canonicalize_filename() instead
[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 _USE_TVPD_MODE
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 bool ms_config_get_int(const char *key, int *value)
45 {
46         int err;
47
48         if (!key || !value) {
49                 MS_DBG_ERR("Arguments key or value is NULL");
50                 return false;
51         }
52
53         err = vconf_get_int(key, value);
54         if (err == VCONF_OK)
55                 return true;
56
57         MS_DBG_ERR("Error code: %d", err);
58
59         return false;
60 }
61
62 bool ms_config_set_int(const char *key, int value)
63 {
64         int err;
65
66         if (!key) {
67                 MS_DBG_ERR("Arguments key is NULL");
68                 return false;
69         }
70
71         err = vconf_set_int(key, value);
72         if (err == VCONF_OK)
73                 return true;
74
75         MS_DBG_ERR("Error code: %d", err);
76
77         return false;
78 }
79
80 bool ms_config_get_str(const char *key, char **value)
81 {
82         char *res = NULL;
83
84         if (key == NULL || value == NULL) {
85                 MS_DBG_ERR("Arguments key or value is NULL");
86                 return false;
87         }
88
89         res = vconf_get_str(key);
90         if (MS_STRING_VALID(res)) {
91                 *value = g_strdup(res);
92                 MS_SAFE_FREE(res);
93                 return true;
94         }
95
96         return false;
97 }
98
99 int ms_get_remain_space(double *free_space)
100 {
101         int ret = MS_MEDIA_ERR_NONE;
102         const char *path = "/opt";
103         struct statvfs s;
104
105         ret = statvfs(path, &s);
106         if (ret != 0) {
107                 MS_DBG_ERR("statvfs failed[%d]", ret);
108                 MS_DBG_STRERROR();
109                 return MS_MEDIA_ERR_INTERNAL;
110         }
111
112         /* f_bsize:unsigned long, f_bavail:fsblkcnt_t(unsigned long) */
113         *free_space = (double)(s.f_bsize * s.f_bavail);
114
115         return MS_MEDIA_ERR_NONE;
116 }
117
118 #ifdef _USE_TVPD_MODE
119 bool ms_is_support_pvr(void)
120 {
121         bool bSupportPVR = false;
122         if (system_info_get_custom_bool("com.samsung/featureconf/pvr.pvr_support", &bSupportPVR) != SYSTEM_INFO_ERROR_NONE) {
123                 MS_DBG_ERR("Get PVR Support failed");
124                 return false;
125         }
126
127         MS_DBG("PVR Support : [%d]", bSupportPVR);
128
129         return bSupportPVR;
130 }
131 #endif
132
133 int ms_check_file_path(const char *file_path, uid_t uid)
134 {
135         ms_user_storage_type_e storage_type = -1;
136         int ret = MS_MEDIA_ERR_NONE;
137
138         if (!MS_STRING_VALID(file_path)) {
139                 MS_DBG_ERR("Invalid path");
140                 return MS_MEDIA_ERR_INVALID_PARAMETER;
141         }
142
143         ret = ms_user_get_storage_type(uid, file_path, &storage_type);
144         MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
145
146         if (!g_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
147                 MS_DBG_SERR("g_file_test fail [%s]", file_path);
148                 return MS_MEDIA_ERR_INVALID_PARAMETER;
149         }
150
151         return MS_MEDIA_ERR_NONE;
152 }
153
154 int ms_check_ignore_dir(const char *full_path, uid_t uid)
155 {
156         int ret = MS_MEDIA_ERR_NONE;
157         char *dir_path = NULL;
158         char *next = NULL;
159         int next_pos = 0;
160
161         ret = ms_check_file_path(full_path, uid);
162         MS_DBG_RETV_IF(ret != MS_MEDIA_ERR_NONE, ret);
163
164         if (MS_STRING_VALID(MEDIA_ROOT_PATH_USB) && !strncmp(full_path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB))) {
165                 next_pos = strlen(MEDIA_ROOT_PATH_USB) + 1;
166         } else {
167                 ret = ms_user_get_internal_root_path(uid, &dir_path);
168                 MS_DBG_RETVM_IF(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_internal_root_path() fail");
169
170                 next_pos = strlen(dir_path);
171                 g_free(dir_path);
172                 dir_path = NULL;
173         }
174
175         while ((next = strstr(full_path + next_pos, "/"))) {
176                 next_pos = (next - full_path);
177                 dir_path = g_strndup(full_path, next_pos);
178                 next_pos++;
179
180                 ret = ms_check_scan_ignore(dir_path, uid);
181                 g_free(dir_path);
182                 if (ret != MS_MEDIA_ERR_NONE)
183                         break;
184         }
185
186         return ret;
187 }
188
189 int ms_check_scan_ignore(char * path, uid_t uid)
190 {
191         int ret = MS_MEDIA_ERR_NONE;
192         const char *ignore_file = ".scan_ignore";
193         char *tmp_path = NULL;
194         char *org_path = NULL;
195         char ignore_path[MS_FILE_PATH_LEN_MAX] = {0, };
196
197 #ifndef _USE_TVPD_MODE
198         char replace[MS_FILE_PATH_LEN_MAX] = {0, };
199         char *mediashared = NULL;
200 #endif
201
202         /* Check for symbolic link */
203         tmp_path = realpath(path, NULL);
204         /* Get trimmed path */
205         org_path = g_canonicalize_filename(path, NULL);
206
207 #ifdef _USE_TVPD_MODE
208         if (g_strcmp0(tmp_path, org_path) != 0) {
209                 MS_SAFE_FREE(tmp_path);
210                 g_free(org_path);
211                 MS_DBG_ERR("symbolic link(directory)");
212                 return MS_MEDIA_ERR_INVALID_PARAMETER;
213         }
214 #else
215         if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
216                 ms_user_get_mediashared_path(uid, &mediashared);
217                 snprintf(replace, MS_FILE_PATH_LEN_MAX, "%s%s", mediashared, tmp_path + strlen(MEDIA_SHARE_PATH));
218                 MS_SAFE_FREE(mediashared);
219                 if (g_strcmp0(replace, org_path) != 0) {
220                         MS_SAFE_FREE(tmp_path);
221                         g_free(org_path);
222                         MS_DBG_ERR("symbolic link(directory)");
223                         return MS_MEDIA_ERR_INVALID_PARAMETER;
224                 }
225         } else {
226                 if (g_strcmp0(tmp_path, org_path) != 0) {
227                         MS_SAFE_FREE(tmp_path);
228                         g_free(org_path);
229                         MS_DBG_ERR("symbolic link(directory)");
230                         return MS_MEDIA_ERR_INVALID_PARAMETER;
231                 }
232         }
233 #endif
234         MS_SAFE_FREE(tmp_path);
235         g_free(org_path);
236
237         if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
238                 snprintf(ignore_path, sizeof(ignore_path), "%s/%s", path, ignore_file);
239
240                 if (g_file_test(ignore_path, G_FILE_TEST_EXISTS)) {
241                         MS_DBG_WARN("scan ignore file exist [%s]", ignore_path);
242                         return MS_MEDIA_ERR_INVALID_PARAMETER;
243                 }
244         } else {
245                 MS_DBG_ERR("g_file_test fails[%s]", path);
246                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
247
248                 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
249                         MS_DBG_ERR("Fail to get USB path");
250                         return ret;
251                 }
252
253                 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
254                         /*if the directory does not exist, check the device is unmounted*/
255                         if (!ms_storage_mount_status(path)) {
256                                 MS_DBG_ERR("Device is unmounted[%s]", path);
257                                 return MS_MEDIA_ERR_USB_UNMOUNTED;
258                         }
259                 }
260         }
261
262         return ret;
263 }
264
265 #ifndef _USE_DEVICED_DBUS
266
267 typedef struct storage_result {
268         char *storage_path;
269         bool result;
270 } storage_result_s;
271
272 static void __ms_check_mount_status(usb_device_h usb_device, void *user_data)
273 {
274         storage_result_s *data = (storage_result_s *)user_data;
275         char *mount_path = NULL;
276
277         mount_path = usb_device_get_mountpath(usb_device);
278         if (!mount_path)
279                 return;
280
281         MS_DBG_SWARN("mount_path [%s]", mount_path);
282         data->result = (g_strcmp0(mount_path, data->storage_path) == 0);
283 }
284 #endif
285
286 bool ms_storage_mount_status(const char *start_path)
287 {
288         bool ret = false;
289 #ifndef _USE_DEVICED_DBUS
290         storage_result_s res = {0, };
291         char *remain_path = NULL;
292         int remain_len = 0;
293
294         remain_path = strstr(start_path + strlen(MEDIA_ROOT_PATH_USB) + 1, "/");
295         if (remain_path != NULL)
296                 remain_len = strlen(remain_path);
297
298         res.storage_path = g_strndup(start_path, strlen(start_path) - remain_len);
299
300         MS_DBG_SWARN("storage_path [%s]", res.storage_path);
301
302         usb_mass_storage_foreach(__ms_check_mount_status, &res);
303         g_free(res.storage_path);
304         ret = res.result;
305
306         if (ret)
307                 MS_DBG_SWARN("start path is mounted [%s]", start_path);
308 #endif
309         return ret;
310 }
311
312 int ms_set_db_status(ms_db_status_type_t status, ms_user_storage_type_e storage_type)
313 {
314         int ret = MS_MEDIA_ERR_NONE;
315
316         if (status == MS_DB_UPDATING) {
317                 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING))
318                                 goto ERROR;
319
320                 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
321                         if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING))
322                                 goto ERROR;
323                 }
324         } else {
325                 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED))
326                                 goto ERROR;
327
328                 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
329                         if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED))
330                                 goto ERROR;
331                 }
332         }
333
334         ret = ms_set_power_mode(status);
335         if (ret != MS_MEDIA_ERR_NONE)
336                 MS_DBG_ERR("ms_set_power_mode fail");
337
338         return ret;
339 ERROR:
340         MS_DBG_ERR("ms_config_set_int failed");
341         return MS_MEDIA_ERR_INTERNAL;
342 }
343
344 int ms_set_power_mode(ms_db_status_type_t status)
345 {
346         int res = MS_MEDIA_ERR_NONE;
347         int err;
348
349         switch (status) {
350         case MS_DB_UPDATING:
351                 err = device_power_request_lock(POWER_LOCK_CPU, 0);
352                 if (err != 0)
353                         res = MS_MEDIA_ERR_INTERNAL;
354                 break;
355         case MS_DB_UPDATED:
356                 err = device_power_release_lock(POWER_LOCK_CPU);
357                 if (err != 0)
358                         res = MS_MEDIA_ERR_INTERNAL;
359                 break;
360         default:
361                 MS_DBG_ERR("Unacceptable type : %d", status);
362                 break;
363         }
364
365         return res;
366 }
367
368 void ms_trim_dir_path(char *dir_path)
369 {
370         /* need implementation */
371         /* if dir_path is not NULL terminated, this function will occure crash */
372         int len = strlen(dir_path);
373
374         if (dir_path[len -1] == '/')
375                 dir_path[len -1] = '\0';
376 }
377
378 int ms_check_size_mediadb(uid_t uid, double *db_size)
379 {
380         int ret = MS_MEDIA_ERR_NONE;
381         char *db_path = NULL;
382         struct stat buf;
383
384         ret = ms_user_get_media_db_path(uid, &db_path);
385
386         if (stat(db_path, &buf) == 0) {
387                 *db_size = buf.st_size;
388         } else {
389                 MS_DBG_STRERROR("stat failed");
390                 ret = MS_MEDIA_ERR_INTERNAL;
391         }
392
393         g_free(db_path);
394
395         return ret;
396 }
397
398 #ifdef _USE_TVPD_MODE
399 #define PROC_OOM_SCORE_ADJ_PATH         "/proc/%d/oom_score_adj"
400 #define VIP_OOM_SCORE_ADJ                       (-1000)
401 #define PROC_NAME_MAX 1024
402 #define PROC_BUF_MAX 64
403
404 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
405 {
406         char buf[PROC_BUF_MAX];
407         char cmdline_buf[PROC_NAME_MAX];
408         char *filename;
409         FILE *fp;
410
411         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
412         fp = fopen(buf, "r");
413         if (fp == NULL)
414                 return MS_MEDIA_ERR_INTERNAL;
415
416         if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
417                 fclose(fp);
418                 return MS_MEDIA_ERR_INTERNAL;
419         }
420         fclose(fp);
421
422         filename = strrchr(cmdline_buf, '/');
423         if (filename == NULL)
424                 filename = cmdline_buf;
425         else
426                 filename = filename + 1;
427
428         SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
429
430         return MS_MEDIA_ERR_NONE;
431 }
432
433 int ms_set_vip_process(void)
434 {
435         char buf[100] = {0};
436         int id = 0;
437         static pid_t pid = 0;
438         static char process_name[PROC_NAME_MAX] = {0};
439         static char *appid = NULL;
440
441         /* Get Pid */
442         pid = getpid();
443         if (ms_get_cmdline_from_proc(pid, process_name)) {
444                 MS_DBG_ERR("%s: Read process name failed pid[%d]", __func__, pid);
445                 return MS_MEDIA_ERR_INTERNAL;
446         }
447         appid = process_name;
448
449         MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
450
451         if (prctl(PR_GET_DUMPABLE) == 0)
452                 prctl(PR_SET_DUMPABLE, 1);
453
454         snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
455         id = open(buf, O_WRONLY, 0777);
456         if (id < 0) {
457                 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
458                 return MS_MEDIA_ERR_INTERNAL;
459         }
460         snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
461         if (write(id, buf, strlen(buf)) < 0) {
462                 MS_DBG_ERR("write() failed errno=%d", errno);
463                 close(id);
464                 return MS_MEDIA_ERR_INTERNAL;
465         }
466         close(id);
467         return MS_MEDIA_ERR_NONE;
468 }
469 #endif