Fix bug when batch insert
[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 static void __ms_trim_path(const char *input_path, char **output_path)
190 {
191         char buf[4096] = {0, };
192         char tmp[4096] = {0, };
193         char *pos = NULL;
194
195         SAFE_STRLCPY(buf, input_path, sizeof(buf));
196
197         while ((pos = strstr(buf, "//")) != NULL) {
198                 memset(tmp, 0, sizeof(tmp));
199                 SAFE_STRLCPY(tmp, buf, pos - buf + 1);
200                 SAFE_STRLCAT(tmp, pos + 1, sizeof(tmp));
201
202                 memset(buf, 0, sizeof(buf));
203                 SAFE_STRLCPY(buf, tmp, sizeof(buf));
204         }
205
206         if (g_str_has_suffix(buf, "/"))
207                 *output_path = g_strndup(buf, strlen(buf) - 1);
208         else
209                 *output_path = g_strdup(buf);
210 }
211
212 int ms_check_scan_ignore(char * path, uid_t uid)
213 {
214         int ret = MS_MEDIA_ERR_NONE;
215         const char *ignore_file = ".scan_ignore";
216         char *tmp_path = NULL;
217         char *org_path = NULL;
218         char ignore_path[MS_FILE_PATH_LEN_MAX] = {0, };
219
220 #ifndef _USE_TVPD_MODE
221         char replace[MS_FILE_PATH_LEN_MAX] = {0, };
222         char *mediashared = NULL;
223 #endif
224
225         /* Check for symbolic link */
226         tmp_path = realpath(path, NULL);
227         /* Get trimmed path */
228         __ms_trim_path(path, &org_path);
229
230 #ifdef _USE_TVPD_MODE
231         if (g_strcmp0(tmp_path, org_path) != 0) {
232                 MS_SAFE_FREE(tmp_path);
233                 g_free(org_path);
234                 MS_DBG_ERR("symbolic link(directory)");
235                 return MS_MEDIA_ERR_INVALID_PARAMETER;
236         }
237 #else
238         if (g_str_has_prefix(tmp_path, MEDIA_SHARE_PATH)) {
239                 ms_user_get_mediashared_path(uid, &mediashared);
240                 snprintf(replace, MS_FILE_PATH_LEN_MAX, "%s%s", mediashared, tmp_path + strlen(MEDIA_SHARE_PATH));
241                 MS_SAFE_FREE(mediashared);
242                 if (g_strcmp0(replace, org_path) != 0) {
243                         MS_SAFE_FREE(tmp_path);
244                         g_free(org_path);
245                         MS_DBG_ERR("symbolic link(directory)");
246                         return MS_MEDIA_ERR_INVALID_PARAMETER;
247                 }
248         } else {
249                 if (g_strcmp0(tmp_path, org_path) != 0) {
250                         MS_SAFE_FREE(tmp_path);
251                         g_free(org_path);
252                         MS_DBG_ERR("symbolic link(directory)");
253                         return MS_MEDIA_ERR_INVALID_PARAMETER;
254                 }
255         }
256 #endif
257         MS_SAFE_FREE(tmp_path);
258         g_free(org_path);
259
260         if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
261                 snprintf(ignore_path, sizeof(ignore_path), "%s/%s", path, ignore_file);
262
263                 if (g_file_test(ignore_path, G_FILE_TEST_EXISTS)) {
264                         MS_DBG_WARN("scan ignore file exist [%s]", ignore_path);
265                         return MS_MEDIA_ERR_INVALID_PARAMETER;
266                 }
267         } else {
268                 MS_DBG_ERR("g_file_test fails[%s]", path);
269                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
270
271                 if (!MS_STRING_VALID(MEDIA_ROOT_PATH_USB)) {
272                         MS_DBG_ERR("Fail to get USB path");
273                         return ret;
274                 }
275
276                 if (strstr(path, MEDIA_ROOT_PATH_USB) != NULL) {
277                         /*if the directory does not exist, check the device is unmounted*/
278                         if (!ms_storage_mount_status(path)) {
279                                 MS_DBG_ERR("Device is unmounted[%s]", path);
280                                 return MS_MEDIA_ERR_USB_UNMOUNTED;
281                         }
282                 }
283         }
284
285         return ret;
286 }
287
288 #ifndef _USE_DEVICED_DBUS
289
290 typedef struct storage_result {
291         char *storage_path;
292         bool result;
293 } storage_result_s;
294
295 static void __ms_check_mount_status(usb_device_h usb_device, void *user_data)
296 {
297         storage_result_s *data = (storage_result_s *)user_data;
298         char *mount_path = NULL;
299
300         mount_path = usb_device_get_mountpath(usb_device);
301         if (!mount_path)
302                 return;
303
304         MS_DBG_SWARN("mount_path [%s]", mount_path);
305         data->result = (g_strcmp0(mount_path, data->storage_path) == 0);
306 }
307 #endif
308
309 bool ms_storage_mount_status(const char *start_path)
310 {
311         bool ret = false;
312 #ifndef _USE_DEVICED_DBUS
313         storage_result_s res = {0, };
314         char *remain_path = NULL;
315         int remain_len = 0;
316
317         remain_path = strstr(start_path + strlen(MEDIA_ROOT_PATH_USB) + 1, "/");
318         if (remain_path != NULL)
319                 remain_len = strlen(remain_path);
320
321         res.storage_path = g_strndup(start_path, strlen(start_path) - remain_len);
322
323         MS_DBG_SWARN("storage_path [%s]", res.storage_path);
324
325         usb_mass_storage_foreach(__ms_check_mount_status, &res);
326         g_free(res.storage_path);
327         ret = res.result;
328
329         if (ret)
330                 MS_DBG_SWARN("start path is mounted [%s]", start_path);
331 #endif
332         return ret;
333 }
334
335 int ms_set_db_status(ms_db_status_type_t status, ms_user_storage_type_e storage_type)
336 {
337         int ret = MS_MEDIA_ERR_NONE;
338
339         if (status == MS_DB_UPDATING) {
340                 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING))
341                                 goto ERROR;
342
343                 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
344                         if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING))
345                                 goto ERROR;
346                 }
347         } else {
348                 if (!ms_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED))
349                                 goto ERROR;
350
351                 if (storage_type == MS_USER_STORAGE_EXTERNAL) {
352                         if (!ms_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED))
353                                 goto ERROR;
354                 }
355         }
356
357         ret = ms_set_power_mode(status);
358         if (ret != MS_MEDIA_ERR_NONE)
359                 MS_DBG_ERR("ms_set_power_mode fail");
360
361         return ret;
362 ERROR:
363         MS_DBG_ERR("ms_config_set_int failed");
364         return MS_MEDIA_ERR_INTERNAL;
365 }
366
367 int ms_set_power_mode(ms_db_status_type_t status)
368 {
369         int res = MS_MEDIA_ERR_NONE;
370         int err;
371
372         switch (status) {
373         case MS_DB_UPDATING:
374                 err = device_power_request_lock(POWER_LOCK_CPU, 0);
375                 if (err != 0)
376                         res = MS_MEDIA_ERR_INTERNAL;
377                 break;
378         case MS_DB_UPDATED:
379                 err = device_power_release_lock(POWER_LOCK_CPU);
380                 if (err != 0)
381                         res = MS_MEDIA_ERR_INTERNAL;
382                 break;
383         default:
384                 MS_DBG_ERR("Unacceptable type : %d", status);
385                 break;
386         }
387
388         return res;
389 }
390
391 void ms_trim_dir_path(char *dir_path)
392 {
393         /* need implementation */
394         /* if dir_path is not NULL terminated, this function will occure crash */
395         int len = strlen(dir_path);
396
397         if (dir_path[len -1] == '/')
398                 dir_path[len -1] = '\0';
399 }
400
401 int ms_check_size_mediadb(uid_t uid, double *db_size)
402 {
403         int ret = MS_MEDIA_ERR_NONE;
404         char *db_path = NULL;
405         struct stat buf;
406
407         ret = ms_user_get_media_db_path(uid, &db_path);
408
409         if (stat(db_path, &buf) == 0) {
410                 *db_size = buf.st_size;
411         } else {
412                 MS_DBG_STRERROR("stat failed");
413                 ret = MS_MEDIA_ERR_INTERNAL;
414         }
415
416         g_free(db_path);
417
418         return ret;
419 }
420
421 #ifdef _USE_TVPD_MODE
422 #define PROC_OOM_SCORE_ADJ_PATH         "/proc/%d/oom_score_adj"
423 #define VIP_OOM_SCORE_ADJ                       (-1000)
424 #define PROC_NAME_MAX 1024
425 #define PROC_BUF_MAX 64
426
427 static int ms_get_cmdline_from_proc(pid_t pid, char *cmdline)
428 {
429         char buf[PROC_BUF_MAX];
430         char cmdline_buf[PROC_NAME_MAX];
431         char *filename;
432         FILE *fp;
433
434         snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
435         fp = fopen(buf, "r");
436         if (fp == NULL)
437                 return MS_MEDIA_ERR_INTERNAL;
438
439         if (fgets(cmdline_buf, PROC_NAME_MAX-1, fp) == NULL) {
440                 fclose(fp);
441                 return MS_MEDIA_ERR_INTERNAL;
442         }
443         fclose(fp);
444
445         filename = strrchr(cmdline_buf, '/');
446         if (filename == NULL)
447                 filename = cmdline_buf;
448         else
449                 filename = filename + 1;
450
451         SAFE_STRLCPY(cmdline, filename, PROC_NAME_MAX);
452
453         return MS_MEDIA_ERR_NONE;
454 }
455
456 int ms_set_vip_process(void)
457 {
458         char buf[100] = {0};
459         int id = 0;
460         static pid_t pid = 0;
461         static char process_name[PROC_NAME_MAX] = {0};
462         static char *appid = NULL;
463
464         /* Get Pid */
465         pid = getpid();
466         if (ms_get_cmdline_from_proc(pid, process_name)) {
467                 MS_DBG_ERR("%s: Read process name failed pid[%d]", __func__, pid);
468                 return MS_MEDIA_ERR_INTERNAL;
469         }
470         appid = process_name;
471
472         MS_DBG("Process name[%s]:Pid[%d]", appid, pid);
473
474         if (prctl(PR_GET_DUMPABLE) == 0)
475                 prctl(PR_SET_DUMPABLE, 1);
476
477         snprintf(buf, sizeof(buf), PROC_OOM_SCORE_ADJ_PATH, pid);
478         id = open(buf, O_WRONLY, 0777);
479         if (id < 0) {
480                 MS_DBG_ERR("fopen %s failed errno:%d", buf, errno);
481                 return MS_MEDIA_ERR_INTERNAL;
482         }
483         snprintf(buf, sizeof(buf), "%d", VIP_OOM_SCORE_ADJ);
484         if (write(id, buf, strlen(buf)) < 0) {
485                 MS_DBG_ERR("write() failed errno=%d", errno);
486                 close(id);
487                 return MS_MEDIA_ERR_INTERNAL;
488         }
489         close(id);
490         return MS_MEDIA_ERR_NONE;
491 }
492 #endif