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-scan.c
26 * @author Yong Yeon Kim(yy9875.kim@samsung.com)
38 #include "media-util.h"
39 #include "media-server-ipc.h"
40 #include "media-scanner-dbg.h"
41 #include "media-scanner-utils.h"
42 #include "media-scanner-db-svc.h"
43 #include "media-scanner-socket.h"
44 #include "media-scanner-scan.h"
46 typedef struct msc_scan_data {
48 struct msc_scan_data *next;
53 GAsyncQueue * storage_queue;
54 GAsyncQueue *scan_queue;
55 GAsyncQueue *reg_queue;
58 extern struct timeval g_mmc_start_time;
59 extern struct timeval g_mmc_end_time;
63 _msc_set_power_mode(ms_db_status_type_t status)
65 int res = MS_MEDIA_ERR_NONE;
70 err = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
72 res = MS_MEDIA_ERR_INTERNAL;
75 err = pm_unlock_state(LCD_OFF, STAY_CUR_STATE);
77 res = MS_MEDIA_ERR_INTERNAL;
80 MSC_DBG_ERR("Unacceptable type : %d", status);
88 _msc_set_db_status(ms_scanning_location_t location, ms_db_status_type_t status)
90 int res = MS_MEDIA_ERR_NONE;
93 if (status == MS_DB_UPDATING) {
94 if (location != MS_SCANNING_DIRECTORY) {
95 if (!msc_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATING)) {
96 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
97 MSC_DBG_ERR("msc_config_set_int failed");
100 /* this is temporay code for tizen 2.0*/
101 if(location == MS_SCANNING_EXTERNAL) {
102 if (!msc_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADING)) {
103 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
104 MSC_DBG_ERR("msc_config_set_int failed");
108 /* Update private vconf key for media server*/
109 if (location == MS_SCANNING_INTERNAL) {
110 if (!msc_config_set_int(MS_SCAN_STATUS_INTERNAL, P_VCONF_SCAN_DOING)) {
111 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
112 MSC_DBG_ERR("msc_config_set_int failed");
114 } else if (location == MS_SCANNING_DIRECTORY) {
115 if (!msc_config_set_int(MS_SCAN_STATUS_DIRECTORY, P_VCONF_SCAN_DOING)) {
116 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
117 MSC_DBG_ERR("msc_config_set_int failed");
120 } else if (status == MS_DB_UPDATED) {
121 if (location != MS_SCANNING_DIRECTORY) {
122 if(!msc_config_set_int(VCONFKEY_FILEMANAGER_DB_STATUS, VCONFKEY_FILEMANAGER_DB_UPDATED)) {
123 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
124 MSC_DBG_ERR("msc_config_set_int failed");
128 /* this is temporay code for tizen 2.0*/
129 if(location == MS_SCANNING_EXTERNAL) {
130 if (!msc_config_set_int(VCONFKEY_FILEMANAGER_MMC_STATUS, VCONFKEY_FILEMANAGER_MMC_LOADED)) {
131 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
132 MSC_DBG_ERR("msc_config_set_int failed");
136 /* Update private vconf key for media server*/
137 if (location == MS_SCANNING_INTERNAL) {
138 if (!msc_config_set_int(MS_SCAN_STATUS_INTERNAL, P_VCONF_SCAN_DONE)) {
139 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
140 MSC_DBG_ERR("msc_config_set_int failed");
142 } else if (location == MS_SCANNING_DIRECTORY) {
143 if (!msc_config_set_int(MS_SCAN_STATUS_DIRECTORY, P_VCONF_SCAN_DONE)) {
144 res = MS_MEDIA_ERR_VCONF_SET_FAIL;
145 MSC_DBG_ERR("msc_config_set_int failed");
150 err = _msc_set_power_mode(status);
151 if (err != MS_MEDIA_ERR_NONE) {
152 MSC_DBG_ERR("_msc_set_power_mode fail");
159 static int _msc_scan_get_next_path_from_current_node(int find_folder,
160 ms_dir_scan_info **current_root,
161 ms_dir_scan_info **real_root, char **path, int *depth)
163 int err = MS_MEDIA_ERR_NONE;
164 char get_path[FAT_FILEPATH_LEN_MAX] = { 0 };
166 if (find_folder == 0) {
167 if ((*current_root)->Rbrother != NULL) {
168 *current_root = (*current_root)->Rbrother;
171 if ((*current_root)->parent == *real_root
172 || (*current_root)->parent == NULL) {
173 *current_root = NULL;
175 return MS_MEDIA_ERR_NONE;
176 } else if ((*current_root)->parent->Rbrother == NULL) {
177 *current_root = (*current_root)->parent;
180 *current_root = (*current_root)->parent->Rbrother;
189 err = msc_get_full_path_from_node(*current_root, get_path, *depth);
190 if (err != MS_MEDIA_ERR_NONE)
191 return MS_MEDIA_ERR_INVALID_PATH;
193 *path = strdup(get_path);
198 static int _msc_scan_add_node(msc_scan_data **first_node, ms_dir_scan_info *const node, int depth)
201 char full_path[MS_FILE_PATH_LEN_MAX] = { 0 };
202 msc_scan_data *current_dir = NULL;
203 msc_scan_data *prv_node = NULL;
204 msc_scan_data *last_node = NULL;
206 err = msc_get_full_path_from_node(node, full_path, depth);
207 if (err != MS_MEDIA_ERR_NONE)
208 return MS_MEDIA_ERR_INVALID_PATH;
210 last_node = *first_node;
211 while (last_node != NULL) {
212 last_node = last_node->next;
215 /*find same folder */
216 if (*first_node != NULL) {
217 last_node = *first_node;
218 while (last_node != NULL) {
219 if (strcmp(full_path, last_node->name) == 0) {
220 return MS_MEDIA_ERR_NONE;
222 prv_node = last_node;
223 last_node = last_node->next;
227 MS_MALLOC(current_dir, sizeof(msc_scan_data));
228 current_dir->name = strdup(full_path);
229 current_dir->next = NULL;
231 if (*first_node == NULL) {
232 *first_node = current_dir;
234 /*if next node of current node is NULL, it is the lastest node. */
235 prv_node->next = current_dir;
238 // MSC_DBG_INFO("scan path : %s %x %p", full_path, first_node, first_node);
240 return MS_MEDIA_ERR_NONE;
243 static bool _msc_check_scan_ignore(char * path)
247 struct dirent *result;
248 char *ignore_path = ".scan_ignore";
252 MSC_DBG_ERR("%s folder opendir fails", path);
256 while (!readdir_r(dp, &entry, &result)) {
260 if (strcmp(entry.d_name, ignore_path) == 0) {
270 static int _ms_check_stop_status(ms_storage_type_t storage_type)
272 int ret = MS_MEDIA_ERR_NONE;
274 /*check poweroff status*/
276 MSC_DBG_INFO("Power off");
277 ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
280 /*check SD card in out */
281 if ((mmc_state != VCONFKEY_SYSMAN_MMC_MOUNTED) && (storage_type == MS_STORAGE_EXTERNAL)) {
282 MSC_DBG_INFO("Directory scanning is stopped");
283 ret = MS_MEDIA_ERR_SCANNER_FORCE_STOP;
289 static void _msc_dir_check(msc_scan_data **first_node, const ms_comm_msg_s *scan_data)
294 char get_path[MS_FILE_PATH_LEN_MAX] = { 0 };
298 struct dirent *result;
299 ms_storage_type_t storage_type;
301 ms_dir_scan_info *root = NULL;
302 ms_dir_scan_info *tmp_root = NULL;
303 ms_dir_scan_info *cur_node = NULL; /*current node*/
304 ms_dir_scan_info *prv_node = NULL; /*previous node*/
305 ms_dir_scan_info *next_node = NULL;
307 MS_MALLOC(root, sizeof(ms_dir_scan_info));
309 MSC_DBG_ERR("malloc fail");
313 storage_type = msc_get_storage_type_by_full(scan_data->msg);
315 root->name = strndup(scan_data->msg, scan_data->msg_size);
316 if (root->name == NULL) {
317 MSC_DBG_ERR("strdup fail");
323 root->Rbrother = NULL;
328 MS_MALLOC(path, sizeof(char) * MS_FILE_PATH_LEN_MAX);
330 err = msc_get_full_path_from_node(tmp_root, path, depth);
331 if (err != MS_MEDIA_ERR_NONE) {
337 _msc_scan_add_node(first_node, root, depth);
340 /*check poweroff status*/
341 err = _ms_check_stop_status(storage_type);
342 if (err != MS_MEDIA_ERR_NONE) {
349 MSC_DBG_ERR("%s folder opendir fails", path);
353 while (!readdir_r(dp, &entry, &result)) {
354 /*check poweroff status*/
355 err = _ms_check_stop_status(storage_type);
356 if (err != MS_MEDIA_ERR_NONE) {
363 if (entry.d_name[0] == '.')
366 if (entry.d_type & DT_DIR) {
367 err = msc_strappend(get_path, sizeof(get_path), "%s/%s",path, entry.d_name);
368 if (err != MS_MEDIA_ERR_NONE) {
369 MSC_DBG_ERR("msc_strappend error");
373 if (_msc_check_scan_ignore(get_path)) {
374 MSC_DBG_ERR("%s is ignore", get_path);
378 MS_MALLOC(cur_node, sizeof(ms_dir_scan_info));
379 if (cur_node == NULL) {
380 MSC_DBG_ERR("malloc fail");
385 cur_node->name = strdup(entry.d_name);
386 cur_node->Rbrother = NULL;
387 cur_node->next = NULL;
390 if (find_folder == 0) {
391 cur_node->parent = tmp_root;
394 cur_node->parent = tmp_root->parent;
395 prv_node->Rbrother = cur_node;
397 prv_node->next = cur_node;
400 _msc_scan_add_node(first_node, cur_node, depth);
409 if (dp) closedir(dp);
412 err = _msc_scan_get_next_path_from_current_node(find_folder, &tmp_root, &root, &path, &depth);
413 if (err != MS_MEDIA_ERR_NONE)
416 if (tmp_root == NULL)
423 /*free allocated memory */
425 if (dp) closedir(dp);
429 while (cur_node != NULL) {
430 next_node = cur_node->next;
431 MS_SAFE_FREE(cur_node->name);
432 MS_SAFE_FREE(cur_node);
433 cur_node = next_node;
437 static int _msc_dir_scan(void **handle, msc_scan_data **first_node, const ms_comm_msg_s * scan_data)
441 int ret = MS_MEDIA_ERR_NONE;
443 char path[MS_FILE_PATH_LEN_MAX] = { 0 };
445 ms_storage_type_t storage_type;
447 err = msc_strcopy(path, sizeof(path), "%s", scan_data->msg);
448 if (err != MS_MEDIA_ERR_NONE) {
449 MSC_DBG_ERR("error : %d", err );
453 storage_type = msc_get_storage_type_by_full(scan_data->msg);
454 scan_type = scan_data->msg_type;
456 /*if scan type is not MS_SCAN_NONE, check data in db. */
457 if (scan_type != MS_MSG_STORAGE_INVALID) {
459 struct dirent *result = NULL;
461 if (scan_data->msg_type != MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
462 _msc_dir_check(first_node, scan_data);
464 msc_scan_data *scan_node;
466 MS_MALLOC(scan_node, sizeof(msc_scan_data));
468 scan_node->name = strdup(scan_data->msg);
469 scan_node->next = NULL;
471 *first_node = scan_node;
476 /*start db update. If node is null, db update is done.*/
477 while (node != NULL) {
478 /*check poweroff status*/
479 ret = _ms_check_stop_status(storage_type);
480 if (ret != MS_MEDIA_ERR_NONE) {
484 dp = opendir(node->name);
486 while (!readdir_r(dp, &entry, &result)) {
487 /*check poweroff status*/
488 ret = _ms_check_stop_status(storage_type);
489 if (ret != MS_MEDIA_ERR_NONE) {
496 if (entry.d_name[0] == '.')
499 if (entry.d_type & DT_REG) {
500 err = msc_strappend(path, sizeof(path), "%s/%s", node->name, entry.d_name);
502 MSC_DBG_ERR("error : %d", err);
506 if (scan_type == MS_MSG_STORAGE_ALL)
507 err = msc_insert_item_batch(handle, path);
509 err = msc_validate_item(handle,path);
511 MSC_DBG_ERR("failed to update db : %d , %d\n", err, scan_type);
517 MSC_DBG_ERR("%s folder opendir fails", node->name);
519 if (dp) closedir(dp);
523 } /*db update while */
524 } else if ( scan_type == MS_MSG_STORAGE_INVALID) {
525 /*In this case, update just validation record*/
526 /*update just valid type*/
527 err = msc_invalidate_all_items(handle, storage_type);
528 if (err != MS_MEDIA_ERR_NONE)
529 MSC_DBG_ERR("error : %d", err);
532 if (dp) closedir(dp);
537 while (node != NULL) {
538 MS_SAFE_FREE(node->name);
546 MSC_DBG_INFO("ret : %d", ret);
552 static void _msc_insert_array(GArray *garray, ms_comm_msg_s *insert_data)
554 ms_scan_data_t *data;
555 bool insert_ok = false;
556 int len = garray->len;
559 MSC_DBG_INFO("the length of array : %d", len);
560 MSC_DBG_INFO("path : %s", insert_data->msg);
561 MSC_DBG_INFO("scan_type : %d", insert_data->msg_type);
563 if (insert_data->pid == POWEROFF) {
564 g_array_prepend_val(garray, insert_data);
566 for (i=0; i < len; i++) {
567 data = g_array_index(garray, ms_scan_data_t*, i);
569 if (data->pid != POWEROFF) {
570 if (data->storage_type == insert_data->storage_type) {
571 if(data->scan_type > insert_data->scan_type) {
572 g_array_remove_index (garray, i);
573 g_array_insert_val(garray, i, insert_data);
581 if (insert_ok == false)
582 g_array_append_val(garray, insert_data);
586 void _msc_check_dir_path(char *dir_path)
588 int len = strlen(dir_path);
590 if (dir_path[len -1] == '/')
591 dir_path[len -1] = '\0';
594 gboolean msc_directory_scan_thread(void *data)
596 ms_comm_msg_s *scan_data = NULL;
597 ms_comm_msg_s *insert_data = NULL;
598 GArray *garray = NULL;
602 void **handle = NULL;
603 ms_storage_type_t storage_type;
605 msc_scan_data *first_node = NULL;
607 /*create array for processing overlay data*/
608 garray = g_array_new (FALSE, FALSE, sizeof (ms_comm_msg_s *));
609 if (garray == NULL) {
610 MSC_DBG_ERR("g_array_new error");
615 length = g_async_queue_length(scan_queue);
617 /*updating requests remain*/
618 if (garray->len != 0 && length == 0) {
619 scan_data = g_array_index(garray, ms_comm_msg_s*, 0);
620 g_array_remove_index (garray, 0);
621 if (scan_data->pid == POWEROFF) {
622 MSC_DBG_INFO("power off");
625 } else if (length != 0) {
626 insert_data = g_async_queue_pop(scan_queue);
627 _msc_insert_array(garray, insert_data);
629 } else if (garray->len == 0 && length == 0) {
630 /*Threre is no request, Wait until pushung new request*/
631 insert_data = g_async_queue_pop(scan_queue);
632 _msc_insert_array(garray, insert_data);
636 MSC_DBG_INFO("DIRECTORY SCAN START");
638 /*connect to media db, if conneting is failed, db updating is stopped*/
639 err = msc_connect_db(&handle);
640 if (err != MS_MEDIA_ERR_NONE)
643 storage_type = msc_get_storage_type_by_full(scan_data->msg);
644 scan_type = scan_data->msg_type;
646 if (scan_type != MS_MSG_DIRECTORY_SCANNING
647 && scan_type != MS_MSG_DIRECTORY_SCANNING_NON_RECURSIVE) {
648 MSC_DBG_ERR("Invalid request");
649 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
653 /*start db updating */
654 // _msc_set_db_status(MS_SCANNING_DIRECTORY, MS_DB_UPDATING);
656 _msc_check_dir_path(scan_data->msg);
658 /*change validity before scanning*/
659 if (scan_type == MS_MSG_DIRECTORY_SCANNING)
660 err = msc_set_folder_validity(handle, scan_data->msg, MS_INVALID, MS_RECURSIVE);
662 err = msc_set_folder_validity(handle, scan_data->msg, MS_INVALID, MS_NON_RECURSIVE);
663 if (err != MS_MEDIA_ERR_NONE)
664 MSC_DBG_ERR("error : %d", err);
666 /*call for bundle commit*/
667 msc_register_start(handle);
668 msc_validate_start(handle);
670 /*insert data into media db */
671 ret = _msc_dir_scan(handle, &first_node, scan_data);
673 /*call for bundle commit*/
674 msc_register_end(handle);
675 msc_validate_end(handle);
677 if (ret == MS_MEDIA_ERR_NONE) {
678 MSC_DBG_INFO("working normally");
679 msc_delete_invalid_items_in_folder(handle, scan_data->msg);
682 /*set vconf key mmc loading for indicator */
683 // _msc_set_db_status(MS_SCANNING_DIRECTORY, MS_DB_UPDATED);
686 MSC_DBG_INFO("power off");
690 /*disconnect form media db*/
691 if (handle) msc_disconnect_db(&handle);
696 msc_send_scan_result(ret, scan_data);
698 // MS_SAFE_FREE(scan_data->msg);
699 MS_SAFE_FREE(scan_data);
701 MSC_DBG_INFO("DIRECTORY SCAN END");
705 // MS_SAFE_FREE(scan_data->msg);
706 MS_SAFE_FREE(scan_data);
707 if (garray) g_array_free (garray, TRUE);
708 if (handle) msc_disconnect_db(&handle);
713 /* this thread process only the request of media-server */
714 gboolean msc_storage_scan_thread(void *data)
716 ms_comm_msg_s *scan_data = NULL;
717 ms_comm_msg_s *insert_data = NULL;
718 GArray *garray = NULL;
723 void **handle = NULL;
724 ms_storage_type_t storage_type;
726 msc_scan_data *first_node = NULL;
728 /*create array for processing overlay data*/
729 garray = g_array_new (FALSE, FALSE, sizeof (ms_comm_msg_s *));
730 if (garray == NULL) {
731 MSC_DBG_ERR("g_array_new error");
736 length = g_async_queue_length(storage_queue);
738 /*updating requests remain*/
739 if (garray->len != 0 && length == 0) {
740 scan_data = g_array_index(garray, ms_comm_msg_s*, 0);
741 g_array_remove_index (garray, 0);
742 if (scan_data->pid == POWEROFF) {
743 MSC_DBG_INFO("power off");
746 } else if (length != 0) {
747 insert_data = g_async_queue_pop(storage_queue);
748 _msc_insert_array(garray, insert_data);
750 } else if (garray->len == 0 && length == 0) {
751 /*Threre is no request, Wait until pushing new request*/
752 insert_data = g_async_queue_pop(storage_queue);
753 _msc_insert_array(garray, insert_data);
757 MSC_DBG_INFO("STORAGE SCAN START");
759 scan_type = scan_data->msg_type;
760 if (scan_type != MS_MSG_STORAGE_ALL
761 && scan_type != MS_MSG_STORAGE_PARTIAL
762 && scan_type != MS_MSG_STORAGE_INVALID) {
763 MSC_DBG_ERR("Invalid request");
764 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
768 storage_type = msc_get_storage_type_by_full(scan_data->msg);
769 MSC_DBG_INFO("%d", storage_type);
771 /*connect to media db, if conneting is failed, db updating is stopped*/
772 err = msc_connect_db(&handle);
773 if (err != MS_MEDIA_ERR_NONE)
776 /*start db updating */
777 if (storage_type == MS_STORAGE_INTERNAL)
778 _msc_set_db_status(MS_SCANNING_INTERNAL, MS_DB_UPDATING);
779 else if (storage_type == MS_STORAGE_EXTERNAL)
780 _msc_set_db_status(MS_SCANNING_EXTERNAL, MS_DB_UPDATING);
782 /*Delete all data before full scanning*/
783 if (scan_type == MS_MSG_STORAGE_ALL) {
784 res = msc_delete_all_items(handle, storage_type);
786 MSC_DBG_ERR("msc_delete_all_record fails");
788 } else if (scan_type == MS_MSG_STORAGE_PARTIAL) {
789 err = msc_invalidate_all_items(handle, storage_type);
790 if (err != MS_MEDIA_ERR_NONE)
791 MSC_DBG_ERR("error : %d", err);
794 if (storage_type == MS_STORAGE_EXTERNAL && scan_type == MS_MSG_STORAGE_ALL) {
795 msc_update_mmc_info();
799 if (storage_type == MS_STORAGE_EXTERNAL) {
800 msc_check_start_time(&g_mmc_start_time);
803 /*call for bundle commit*/
804 msc_register_start(handle);
805 if (scan_type == MS_MSG_STORAGE_PARTIAL) {
806 /*enable bundle commit*/
807 msc_validate_start(handle);
810 /*add inotify watch and insert data into media db */
811 ret = _msc_dir_scan(handle, &first_node, scan_data);
813 /*call for bundle commit*/
814 msc_register_end(handle);
815 if (scan_type == MS_MSG_STORAGE_PARTIAL) {
816 /*disable bundle commit*/
817 msc_validate_end(handle);
818 if (ret == MS_MEDIA_ERR_NONE) {
819 MSC_DBG_INFO("working normally");
820 msc_delete_invalid_items(handle, storage_type);
825 if (storage_type == MS_STORAGE_EXTERNAL) {
826 msc_check_end_time(&g_mmc_end_time);
827 msc_check_time_diff(&g_mmc_start_time, &g_mmc_end_time);
831 /*set vconf key mmc loading for indicator */
832 if (storage_type == MS_STORAGE_INTERNAL)
833 _msc_set_db_status(MS_SCANNING_INTERNAL, MS_DB_UPDATED);
834 else if (storage_type == MS_STORAGE_EXTERNAL)
835 _msc_set_db_status(MS_SCANNING_EXTERNAL, MS_DB_UPDATED);
838 MSC_DBG_INFO("power off");
842 /*disconnect form media db*/
843 if (handle) msc_disconnect_db(&handle);
849 msc_send_scan_result(ret, scan_data);
851 // MS_SAFE_FREE(scan_data->msg);
852 MS_SAFE_FREE(scan_data);
854 MSC_DBG_INFO("STORAGE SCAN END");
858 // MS_SAFE_FREE(scan_data->msg);
859 MS_SAFE_FREE(scan_data);
860 if (garray) g_array_free (garray, TRUE);
861 if (handle) msc_disconnect_db(&handle);
866 static void _msc_insert_register_request(GArray *register_array, ms_comm_msg_s *insert_data)
868 MSC_DBG_INFO("path : %s", insert_data->msg);
870 if (insert_data->pid == POWEROFF) {
871 g_array_prepend_val(register_array, insert_data);
873 g_array_append_val(register_array, insert_data);
877 static bool _is_valid_path(const char *path)
882 if (strncmp(path, MEDIA_ROOT_PATH_INTERNAL, strlen(MEDIA_ROOT_PATH_INTERNAL)) == 0) {
884 } else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) {
892 static int _check_file_path(const char *file_path)
897 /* check location of file */
898 /* file must exists under "/opt/usr/media" or "/opt/storage/sdcard" */
899 if(!_is_valid_path(file_path)) {
900 MSC_DBG_ERR("Invalid path : %s", file_path);
901 return MS_MEDIA_ERR_INVALID_PATH;
904 /* check the file exits actually */
905 exist = open(file_path, O_RDONLY);
907 MSC_DBG_ERR("Not exist path : %s", file_path);
908 return MS_MEDIA_ERR_INVALID_PATH;
912 /* check type of the path */
913 /* It must be a regular file */
914 memset(&file_st, 0, sizeof(struct stat));
915 if(stat(file_path, &file_st) == 0) {
916 if(!S_ISREG(file_st.st_mode)) {
917 /* In this case, it is not a regula file */
918 MSC_DBG_ERR("this path is not a file");
919 return MS_MEDIA_ERR_INVALID_PATH;
922 MSC_DBG_ERR("stat failed [%s]", strerror(errno));
923 return MS_MEDIA_ERR_INVALID_PATH;
926 return MS_MEDIA_ERR_NONE;
929 gboolean msc_register_thread(void *data)
931 ms_comm_msg_s *register_data = NULL;
932 ms_comm_msg_s *insert_data = NULL;
933 GArray *register_array = NULL;
934 GArray *path_array = NULL;
936 char buf[MS_FILE_PATH_LEN_MAX];
937 char *insert_path = NULL;
939 char *file_path = NULL;
945 void **handle = NULL;
947 /*create array for processing overlay data*/
948 register_array = g_array_new (FALSE, FALSE, sizeof (ms_comm_msg_s *));
949 if (register_array == NULL) {
950 MSC_DBG_ERR("g_array_new error");
955 length = g_async_queue_length(reg_queue);
957 /*updating requests remain*/
958 if (register_array->len != 0 && length == 0) {
959 register_data = g_array_index(register_array, ms_comm_msg_s*, 0);
960 g_array_remove_index (register_array, 0);
961 if (register_data->pid == POWEROFF) {
962 MSC_DBG_INFO("power off");
965 } else if (length != 0) {
966 insert_data = g_async_queue_pop(reg_queue);
967 _msc_insert_register_request(register_array, insert_data);
969 } else if (register_array->len == 0 && length == 0) {
970 /*Threre is no request, Wait until pushung new request*/
971 insert_data = g_async_queue_pop(reg_queue);
972 _msc_insert_register_request(register_array, insert_data);
976 if((register_data->msg_size <= 0) ||(register_data->msg_size > MS_FILE_PATH_LEN_MAX)) {
977 MSC_DBG_ERR("message size[%d] is wrong", register_data->msg_size);
980 path_size = register_data->msg_size + 1;
983 /* copy from received data */
984 MS_MALLOC(file_path, path_size);
985 if (file_path == NULL) {
986 MSC_DBG_ERR("MS_MALLOC failed");
990 ret = msc_strcopy(file_path, path_size, "%s", register_data->msg);
991 if (ret != MS_MEDIA_ERR_NONE){
992 MSC_DBG_ERR("msc_strcopy failed : %d", ret);
996 /* load the file list from file */
997 fp = fopen(register_data->msg, "rt");
999 MSC_DBG_ERR("fopen failed [%s]", strerror(errno));
1003 memset(buf, 0x0, MS_FILE_PATH_LEN_MAX);
1004 /* This is an array for storing the path of insert datas*/
1005 path_array = g_array_new (FALSE, FALSE, sizeof (char *));
1006 if (path_array == NULL) {
1007 MSC_DBG_ERR("g_array_new failed");
1011 /* read registering file path from stored file */
1012 while(fgets(buf, MS_FILE_PATH_LEN_MAX, fp) != NULL) {
1013 length = strlen(buf);
1014 buf[length - 1] = '\0';
1016 MSC_DBG_INFO("insert path : %s [%d]", path, strlen(path));
1017 /* insert getted path to the list */
1018 if (g_array_append_val(path_array, path) == NULL) {
1019 MSC_DBG_ERR("g_array_append_val failed");
1024 /* connect to media db, if conneting is failed, db updating is stopped */
1025 err = msc_connect_db(&handle);
1026 if (err != MS_MEDIA_ERR_NONE)
1029 /*start db updating */
1030 /*call for bundle commit*/
1031 msc_register_start(handle);
1033 /* get the inserting file path from array and insert to db */
1034 for (i = 0; i < path_array->len; i++) {
1036 insert_path = g_array_index(path_array, char*, i);
1037 /* check the file */
1038 /* it is really existing */
1039 /* it is in /opt/usr/media or /opt/storage/sdcard */
1040 /* it is really regular file */
1041 ret = _check_file_path(insert_path);
1042 if (ret != MS_MEDIA_ERR_NONE) {
1043 MSC_DBG_ERR("Can't insert the meta of the path");
1048 err = msc_insert_item_batch(handle, insert_path);
1051 MSC_DBG_INFO("power off");
1052 /*call for bundle commit*/
1053 msc_register_end(handle);
1058 /*call for bundle commit*/
1059 msc_register_end(handle);
1061 /*disconnect form media db*/
1062 if (handle) msc_disconnect_db(&handle);
1067 msc_send_register_result(MS_MEDIA_ERR_NONE, register_data);
1070 g_array_free(path_array, TRUE);
1074 MS_SAFE_FREE(file_path);
1075 MS_SAFE_FREE(register_data);
1082 MS_SAFE_FREE(file_path);
1083 MS_SAFE_FREE(register_data);
1084 if (register_array) g_array_free (register_array, TRUE);
1085 if (path_array) g_array_free (path_array, TRUE);
1086 if (handle) msc_disconnect_db(&handle);