4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * This file defines api utilities of contents manager engines.
25 * @file media-server-utils.c
26 * @author Yong Yeon Kim(yy9875.kim@samsung.com)
28 * @brief This file implements main database operation.
35 #include "media-util.h"
36 #include "media-scanner-dbg.h"
37 #include "media-scanner-utils.h"
38 #include "media-scanner-drm.h"
42 #define MILLION 1000000L
43 struct timeval g_mmc_start_time;
44 struct timeval g_mmc_end_time;
47 #define MMC_INFO_SIZE 256
50 extern GAsyncQueue *storage_queue;
51 extern GAsyncQueue *scan_queue;
57 msc_check_start_time(struct timeval *start_time)
59 gettimeofday(start_time, NULL);
63 msc_check_end_time(struct timeval *end_time)
65 gettimeofday(end_time, NULL);
69 msc_check_time_diff(struct timeval *start_time, struct timeval *end_time)
74 time.tv_sec = end_time->tv_sec - start_time->tv_sec;
75 time.tv_usec = end_time->tv_usec - start_time->tv_usec;
76 difftime = MILLION * time.tv_sec + time.tv_usec;
77 MSC_DBG_INFO("The function_to_time took %ld microseconds or %f seconds.",
78 difftime, difftime / (double)MILLION);
83 msc_is_mmc_inserted(void)
86 msc_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &data);
87 if (data != VCONFKEY_SYSMAN_MMC_MOUNTED) {
95 _msc_update_mmc_info(const char *cid)
100 MSC_DBG_ERR("Parameters are invalid");
101 return MS_MEDIA_ERR_INVALID_PARAMETER;
104 res = msc_config_set_str(MS_MMC_INFO_KEY, cid);
106 MSC_DBG_ERR("fail to get MS_MMC_INFO_KEY");
107 return MS_MEDIA_ERR_VCONF_SET_FAIL;
110 return MS_MEDIA_ERR_NONE;
114 _msc_get_contents(const char *filename, char *buf)
118 fp = fopen(filename, "rt");
120 MSC_DBG_ERR("fp is NULL. file name : %s", filename);
121 return MS_MEDIA_ERR_FILE_OPEN_FAIL;
124 if (fgets(buf, 255, fp) == NULL)
125 MSC_DBG_ERR("fgets failed");
129 return MS_MEDIA_ERR_NONE;
134 _msc_get_mmc_info(char *cid)
140 bool getdata = false;
141 bool bHasColon = false;
142 char path[MS_FILE_PATH_LEN_MAX] = { 0 };
143 char mmcpath[MS_FILE_PATH_LEN_MAX] = { 0 };
147 struct dirent *res = NULL;
149 /* mmcblk0 and mmcblk1 is reserved for movinand */
150 for (j = 1; j < 3; j++) {
151 len = snprintf(mmcpath, MS_FILE_PATH_LEN_MAX, "/sys/class/mmc_host/mmc%d/", j);
153 MSC_DBG_ERR("FAIL : snprintf");
154 return MS_MEDIA_ERR_INTERNAL;
160 dp = opendir(mmcpath);
162 MSC_DBG_ERR("dp is NULL");
163 return MS_MEDIA_ERR_DIR_OPEN_FAIL;
166 while (!readdir_r(dp, &ent, &res)) {
172 if (ent.d_name[0] == '.')
175 if (ent.d_type == DT_DIR) {
176 /*ent->d_name is including ':' */
177 for (i = 0; i < strlen(ent.d_name); i++) {
178 if (ent.d_name[i] == ':') {
186 err = msc_strappend(path, sizeof(path), "%s%s/cid", mmcpath, ent.d_name);
188 MSC_DBG_ERR("ms_strappend error : %d", err);
192 if (_msc_get_contents(path, cid) != MS_MEDIA_ERR_NONE)
201 if (getdata == true) {
206 return MS_MEDIA_ERR_NONE;
210 msc_update_mmc_info(void)
213 char cid[MMC_INFO_SIZE] = { 0 };
215 err = _msc_get_mmc_info(cid);
217 err = _msc_update_mmc_info(cid);
221 MSC_DBG_ERR("malloc_trim is failed");
227 _msc_check_mmc_info(const char *cid)
229 char pre_mmc_info[MMC_INFO_SIZE] = { 0 };
233 MSC_DBG_ERR("Parameters are invalid");
237 res = msc_config_get_str(MS_MMC_INFO_KEY, pre_mmc_info);
239 MSC_DBG_ERR("fail to get MS_MMC_INFO_KEY");
243 MSC_DBG_INFO("Last MMC info = %s", pre_mmc_info);
244 MSC_DBG_INFO("Current MMC info = %s", cid);
246 if (strcmp(pre_mmc_info, cid) == 0) {
255 msc_get_mmc_state(void)
257 char cid[MMC_INFO_SIZE] = { 0 };
258 ms_dir_scan_type_t ret = MS_SCAN_ALL;
261 _msc_get_mmc_info(cid);
263 /*check it's same mmc */
264 if (_msc_check_mmc_info(cid)) {
272 msc_mmc_vconf_cb(void *data)
275 ms_comm_msg_s *scan_msg;
276 ms_dir_scan_type_t scan_type = MS_SCAN_PART;
278 if (!msc_config_get_int(VCONFKEY_SYSMAN_MMC_STATUS, &status)) {
279 MSC_DBG_ERR("Get VCONFKEY_SYSMAN_MMC_STATUS failed.");
282 MSC_DBG_INFO("VCONFKEY_SYSMAN_MMC_STATUS :%d", status);
286 MS_MALLOC(scan_msg, sizeof(ms_comm_msg_s));
288 if (mmc_state == VCONFKEY_SYSMAN_MMC_REMOVED ||
289 mmc_state == VCONFKEY_SYSMAN_MMC_INSERTED_NOT_MOUNTED) {
291 if (!msc_drm_extract_ext_memory())
292 MSC_DBG_ERR("ms_drm_extract_ext_memory failed");
294 scan_type = MS_SCAN_INVALID;
295 } else if (mmc_state == VCONFKEY_SYSMAN_MMC_MOUNTED) {
297 if (!msc_drm_insert_ext_memory())
298 MSC_DBG_ERR("ms_drm_insert_ext_memory failed");
300 scan_type = msc_get_mmc_state();
305 scan_msg->msg_type = MS_MSG_STORAGE_ALL;
308 scan_msg->msg_type = MS_MSG_STORAGE_PARTIAL;
310 case MS_SCAN_INVALID:
311 scan_msg->msg_type = MS_MSG_STORAGE_INVALID;
316 scan_msg->msg_size = strlen(MEDIA_ROOT_PATH_SDCARD);
317 msc_strcopy(scan_msg->msg, scan_msg->msg_size+1, "%s", MEDIA_ROOT_PATH_SDCARD);
319 MSC_DBG_INFO("ms_get_mmc_state is %d", scan_msg->msg_type);
321 g_async_queue_push(storage_queue, GINT_TO_POINTER(scan_msg));
326 /*CAUTION : Before using this function, Have to allocate static memory of ret_path*/
327 /*And the array length does not over MS_FILE_PATH_LEN_MAX*/
328 /*for example : char path[MS_FILE_PATH_LEN_MAX] = {0};*/
330 msc_get_full_path_from_node(ms_dir_scan_info * const node, char *ret_path, int depth)
335 ms_dir_scan_info *cur_node;
339 MSC_DBG_ERR("depth < 0");
340 return MS_MEDIA_ERR_INVALID_PATH;
343 MS_MALLOC(path_array, sizeof(char*) * (depth + 1));
348 path_array[i] = cur_node->name;
349 if (cur_node->parent == NULL)
352 cur_node = cur_node->parent;
356 for(i = depth ; i >= 0 ; i --) {
357 length = strlen(path_array[i]);
359 if (path_length + length > MS_FILE_PATH_LEN_MAX) {
360 MSC_DBG_ERR("This is invalid path, %s, %d", node->name, depth);
361 MS_SAFE_FREE(path_array);
362 return MS_MEDIA_ERR_INVALID_PATH;
365 strncpy(ret_path+path_length, path_array[i], length);
366 path_length += length;
368 ret_path[path_length] = '/';
372 ret_path[-- path_length] = '\0';
374 MS_SAFE_FREE(path_array);
376 return MS_MEDIA_ERR_NONE;
380 msc_get_storage_type_by_full(const char *path)
382 if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
383 return MS_STORAGE_INTERNAL;
384 } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
385 return MS_STORAGE_EXTERNAL;
387 return MS_MEDIA_ERR_INVALID_PATH;
391 msc_strappend(char *res, const int size, const char *pattern,
392 const char *str1, const char *str2)
395 int real_size = size - 1;
397 if (!res ||!pattern || !str1 ||!str2 )
398 return MS_MEDIA_ERR_INVALID_PARAMETER;
400 if (real_size < (strlen(str1) + strlen(str2)))
401 return MS_MEDIA_ERR_INVALID_PARAMETER;
403 len = snprintf(res, real_size, pattern, str1, str2);
405 return MS_MEDIA_ERR_INVALID_PARAMETER;
410 return MS_MEDIA_ERR_NONE;
414 msc_strcopy(char *res, const int size, const char *pattern, const char *str1)
417 int real_size = size;
419 if (!res || !pattern || !str1)
420 return MS_MEDIA_ERR_INVALID_PARAMETER;
422 if (real_size < strlen(str1))
423 return MS_MEDIA_ERR_INVALID_PARAMETER;
425 len = snprintf(res, real_size, pattern, str1);
427 return MS_MEDIA_ERR_INVALID_PARAMETER;
432 return MS_MEDIA_ERR_NONE;
436 msc_config_get_int(const char *key, int *value)
440 if (!key || !value) {
441 MSC_DBG_ERR("Arguments key or value is NULL");
445 err = vconf_get_int(key, value);
451 MSC_DBG_ERR("Unexpected error code: %d", err);
457 msc_config_set_int(const char *key, int value)
462 MSC_DBG_ERR("Arguments key is NULL");
466 err = vconf_set_int(key, value);
472 MSC_DBG_ERR("Unexpected error code: %d", err);
478 msc_config_get_str(const char *key, char *value)
481 if (!key || !value) {
482 MSC_DBG_ERR("Arguments key or value is NULL");
486 res = vconf_get_str(key);
488 strncpy(value, res, strlen(res) + 1);
496 msc_config_set_str(const char *key, const char *value)
500 if (!key || !value) {
501 MSC_DBG_ERR("Arguments key or value is NULL");
505 err = vconf_set_str(key, value);
509 MSC_DBG_ERR("fail to vconf_set_str %d", err);