diff options
Diffstat (limited to 'common/media-server-inotify.c')
-rwxr-xr-x | common/media-server-inotify.c | 416 |
1 files changed, 294 insertions, 122 deletions
diff --git a/common/media-server-inotify.c b/common/media-server-inotify.c index 5a00039..531d1a8 100755 --- a/common/media-server-inotify.c +++ b/common/media-server-inotify.c @@ -27,18 +27,21 @@ * @version 1.0 * @brief */ +#include <vconf.h> + #include "media-server-utils.h" #include "media-server-db-svc.h" #include "media-server-inotify-internal.h" #include "media-server-inotify.h" +extern bool power_off; extern int inoti_fd; -ms_dir_data *first_inoti_node; +extern int mmc_state; +ms_inoti_dir_data *first_inoti_node; ms_ignore_file_info *latest_ignore_file; -int _ms_inoti_directory_scan_and_register_file(void *handle, char *dir_path) +int _ms_inoti_directory_scan_and_register_file(void **handle, char *dir_path) { - MS_DBG_START(); struct dirent ent; struct dirent *res = NULL; DIR *dp = NULL; @@ -50,7 +53,7 @@ int _ms_inoti_directory_scan_and_register_file(void *handle, char *dir_path) dp = opendir(dir_path); if (dp == NULL) { - MS_DBG("Fail to open dir"); + MS_DBG_ERR("Fail to open dir %s", dir_path); return MS_ERR_DIR_OPEN_FAIL; } @@ -65,7 +68,7 @@ int _ms_inoti_directory_scan_and_register_file(void *handle, char *dir_path) err = ms_strappend(path, sizeof(path), "%s/%s", dir_path, ent.d_name); if (err != MS_ERR_NONE) { - MS_DBG("ms_strappend error : %d", err); + MS_DBG_ERR("ms_strappend error : %d", err); continue; } @@ -73,10 +76,9 @@ int _ms_inoti_directory_scan_and_register_file(void *handle, char *dir_path) if (ent.d_type == DT_DIR) { _ms_inoti_directory_scan_and_register_file(handle, path); } else { - err = ms_register_file(handle, path, NULL); if (err != MS_ERR_NONE) { - MS_DBG("ms_register_file error : %d", err); + MS_DBG_ERR("ms_register_file error : %d", err); continue; } } @@ -84,20 +86,11 @@ int _ms_inoti_directory_scan_and_register_file(void *handle, char *dir_path) closedir(dp); - MS_DBG_END(); - return 0; } -int _ms_inoti_scan_renamed_folder(void *handle, char *org_path, char *chg_path) +int _ms_inoti_scan_renamed_folder(void **handle, char *org_path, char *chg_path) { - if (org_path == NULL || chg_path == NULL) { - MS_DBG("Parameter is wrong"); - return MS_ERR_ARG_INVALID; - } - - MS_DBG_START(); - int err = -1; struct dirent ent; struct dirent *res = NULL; @@ -105,12 +98,17 @@ int _ms_inoti_scan_renamed_folder(void *handle, char *org_path, char *chg_path) DIR *dp = NULL; char path_from[MS_FILE_PATH_LEN_MAX] = { 0 }; char path_to[MS_FILE_PATH_LEN_MAX] = { 0 }; - ms_store_type_t src_storage = 0; - ms_store_type_t des_storage = 0; + ms_storage_type_t src_storage = 0; + ms_storage_type_t des_storage = 0; + + if (org_path == NULL || chg_path == NULL) { + MS_DBG_ERR("Parameter is wrong"); + return MS_ERR_ARG_INVALID; + } dp = opendir(chg_path); if (dp == NULL) { - MS_DBG("Fail to open dir"); + MS_DBG_ERR("Fail to open dir %s", chg_path); return MS_ERR_DIR_OPEN_FAIL; } else { MS_DBG("Modify added watch"); @@ -126,13 +124,13 @@ int _ms_inoti_scan_renamed_folder(void *handle, char *org_path, char *chg_path) err = ms_strappend(path_from, sizeof(path_from), "%s/%s", org_path, ent.d_name); if (err != MS_ERR_NONE) { - MS_DBG("ms_strappend error : %d", err); + MS_DBG_ERR("ms_strappend error : %d", err); continue; } err = ms_strappend(path_to, sizeof(path_to), "%s/%s", chg_path, ent.d_name); if (err != MS_ERR_NONE) { - MS_DBG("ms_strappend error : %d", err); + MS_DBG_ERR("ms_strappend error : %d", err); continue; } @@ -143,21 +141,21 @@ int _ms_inoti_scan_renamed_folder(void *handle, char *org_path, char *chg_path) /*in case of file */ if (ent.d_type == DT_REG) { - src_storage = ms_get_store_type_by_full(path_from); - des_storage = ms_get_store_type_by_full(path_to); + src_storage = ms_get_storage_type_by_full(path_from); + des_storage = ms_get_storage_type_by_full(path_to); if ((src_storage != MS_ERR_INVALID_FILE_PATH) && (des_storage != MS_ERR_INVALID_FILE_PATH)) ms_move_item(handle, src_storage, des_storage, path_from, path_to); else { - MS_DBG("ms_get_store_type_by_full error"); + MS_DBG_ERR("src_storage : %s", src_storage); + MS_DBG_ERR("des_storage : %s", des_storage); } } } closedir(dp); - MS_DBG_END(); return 0; } @@ -165,6 +163,10 @@ int ms_inoti_add_ignore_file(const char *path) { ms_ignore_file_info *new_node; + new_node = ms_inoti_find_ignore_file(path); + if (new_node != NULL) + return MS_ERR_NONE; + new_node = malloc(sizeof(ms_ignore_file_info)); new_node->path = strdup(path); @@ -185,7 +187,6 @@ int ms_inoti_add_ignore_file(const char *path) int ms_inoti_delete_ignore_file(ms_ignore_file_info * delete_node) { - MS_DBG(""); if (delete_node->previous != NULL) delete_node->previous->next = delete_node->next; if (delete_node->next != NULL) @@ -195,10 +196,9 @@ int ms_inoti_delete_ignore_file(ms_ignore_file_info * delete_node) latest_ignore_file = delete_node->previous; } - free(delete_node->path); - free(delete_node); + MS_SAFE_FREE(delete_node->path); + MS_SAFE_FREE(delete_node); - MS_DBG(""); return MS_ERR_NONE; } @@ -221,7 +221,6 @@ ms_ignore_file_info *ms_inoti_find_ignore_file(const char *path) void ms_inoti_delete_mmc_ignore_file(void) { - MS_DBG_START(); ms_ignore_file_info *prv_node = NULL; ms_ignore_file_info *cur_node = NULL; ms_ignore_file_info *del_node = NULL; @@ -229,7 +228,7 @@ void ms_inoti_delete_mmc_ignore_file(void) if (latest_ignore_file != NULL) { cur_node = latest_ignore_file; while (cur_node != NULL) { - if (strstr(cur_node->path, MS_MMC_ROOT_PATH) != NULL) { + if (strstr(cur_node->path, MS_ROOT_PATH_EXTERNAL) != NULL) { if (prv_node != NULL) { prv_node->previous = cur_node->previous; } @@ -245,17 +244,14 @@ void ms_inoti_delete_mmc_ignore_file(void) cur_node = cur_node->previous; if (del_node != NULL) { - free(del_node->path); - free(del_node); - del_node = NULL; + MS_SAFE_FREE(del_node->path); + MS_SAFE_FREE(del_node); } } } /*active flush */ malloc_trim(0); - - MS_DBG_END(); } int ms_inoti_init(void) @@ -263,7 +259,7 @@ int ms_inoti_init(void) inoti_fd = inotify_init(); if (inoti_fd < 0) { perror("inotify_init"); - MS_DBG("inotify_init failed"); + MS_DBG_ERR("inotify_init failed"); return inoti_fd; } @@ -272,14 +268,12 @@ int ms_inoti_init(void) void ms_inoti_add_watch(char *path) { - MS_DBG(""); - ms_dir_data *current_dir = NULL; - ms_dir_data *prv_node = NULL; - ms_dir_data *last_node = NULL; + ms_inoti_dir_data *current_dir = NULL; + ms_inoti_dir_data *prv_node = NULL; + ms_inoti_dir_data *last_node = NULL; /*find same folder */ if (first_inoti_node != NULL) { - MS_DBG("find same folder"); last_node = first_inoti_node; while (last_node != NULL) { if (strcmp(path, last_node->name) == 0) { @@ -291,17 +285,13 @@ void ms_inoti_add_watch(char *path) } } - MS_DBG("start add watch"); - /*there is no same path. */ - current_dir = malloc(sizeof(ms_dir_data)); + current_dir = malloc(sizeof(ms_inoti_dir_data)); current_dir->wd = inotify_add_watch(inoti_fd, path, IN_CLOSE_WRITE | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO); if (current_dir->wd > 0) { - MS_DBG("wd : %d", current_dir->wd); - current_dir->name = strdup(path); current_dir->next = NULL; @@ -309,32 +299,32 @@ void ms_inoti_add_watch(char *path) first_inoti_node = current_dir; } else { /*if next node of current node is NULL, it is the lastest node. */ - MS_DBG("last_node : %s", prv_node->name); prv_node->next = current_dir; } - MS_DBG("add watch : %s", path); } else { - MS_DBG("wd : %d", current_dir->wd); - free(current_dir); + MS_DBG_ERR("inotify_add_watch failed"); + MS_SAFE_FREE(current_dir); } } -int ms_inoti_add_watch_with_node(ms_dir_scan_info * const node) +int ms_inoti_add_watch_with_node(ms_dir_scan_info * const node, int depth) { + int err; char full_path[MS_FILE_PATH_LEN_MAX] = { 0 }; - ms_dir_data *current_dir = NULL; - ms_dir_data *prv_node = NULL; - ms_dir_data *last_node = NULL; + ms_inoti_dir_data *current_dir = NULL; + ms_inoti_dir_data *prv_node = NULL; + ms_inoti_dir_data *last_node = NULL; - ms_get_full_path_from_node(node, full_path); + err = ms_get_full_path_from_node(node, full_path, depth); + if (err != MS_ERR_NONE) + return MS_ERR_INVALID_DIR_PATH; /*find same folder */ if (first_inoti_node != NULL) { last_node = first_inoti_node; while (last_node != NULL) { if (strcmp(full_path, last_node->name) == 0) { - MS_DBG("watch is already added: %s", full_path); return MS_ERR_NONE; } prv_node = last_node; @@ -343,27 +333,24 @@ int ms_inoti_add_watch_with_node(ms_dir_scan_info * const node) } /*there is no same path. */ - current_dir = malloc(sizeof(ms_dir_data)); + current_dir = malloc(sizeof(ms_inoti_dir_data)); current_dir->wd = inotify_add_watch(inoti_fd, full_path, IN_CLOSE_WRITE | IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO); if( current_dir->wd > 0) { - MS_DBG("wd : %d", current_dir->wd); current_dir->name = strdup(full_path); current_dir->next = NULL; - current_dir->db_updated = false; if (first_inoti_node == NULL) { first_inoti_node = current_dir; } else { /*if next node of current node is NULL, it is the lastest node. */ - MS_DBG("last_node : %s", prv_node->name); prv_node->next = current_dir; } MS_DBG("add watch : %s", full_path); } else { - MS_DBG("wd : %d", current_dir->wd); - free(current_dir); + MS_DBG_ERR("inotify_add_watch failed : %d", current_dir->wd); + MS_SAFE_FREE(current_dir); } return MS_ERR_NONE; @@ -371,10 +358,9 @@ int ms_inoti_add_watch_with_node(ms_dir_scan_info * const node) void ms_inoti_remove_watch_recursive(char *path) { - MS_DBG_START(); - ms_dir_data *prv_node = NULL; - ms_dir_data *cur_node = NULL; - ms_dir_data *del_node = NULL; + ms_inoti_dir_data *prv_node = NULL; + ms_inoti_dir_data *cur_node = NULL; + ms_inoti_dir_data *del_node = NULL; if (first_inoti_node != NULL) { cur_node = first_inoti_node; @@ -397,23 +383,20 @@ void ms_inoti_remove_watch_recursive(char *path) cur_node = cur_node->next; if (del_node != NULL) { - free(del_node->name); - free(del_node); - del_node = NULL; + MS_SAFE_FREE(del_node->name); + MS_SAFE_FREE(del_node); } } } /*active flush */ malloc_trim(0); - - MS_DBG_END(); } void ms_inoti_remove_watch(char *path) { - ms_dir_data *del_node = NULL; - ms_dir_data *prv_node = NULL; + ms_inoti_dir_data *del_node = NULL; + ms_inoti_dir_data *prv_node = NULL; if (strcmp(first_inoti_node->name, path) == 0) { del_node = first_inoti_node; @@ -431,8 +414,8 @@ void ms_inoti_remove_watch(char *path) prv_node->next = del_node->next; } /*free deleted node */ - free(del_node->name); - free(del_node); + MS_SAFE_FREE(del_node->name); + MS_SAFE_FREE(del_node); break; } prv_node = del_node; @@ -448,7 +431,7 @@ void ms_inoti_remove_watch(char *path) void ms_inoti_modify_watch(char *path_from, char *path_to) { bool find = false; - ms_dir_data *mod_node; + ms_inoti_dir_data *mod_node; if (strcmp(first_inoti_node->name, path_from) == 0) { mod_node = first_inoti_node; @@ -459,12 +442,9 @@ void ms_inoti_modify_watch(char *path_from, char *path_to) while (mod_node != NULL) { /*find previous directory*/ if (strcmp(path_from, mod_node->name) == 0) { - MS_DBG("find change node: %s", mod_node->name); - MS_DBG("new name : %s", path_to); /*change path of directory*/ /*free previous name of node */ - free(mod_node->name); - mod_node->name = NULL; + MS_SAFE_FREE(mod_node->name); /*add new name */ mod_node->name = strdup(path_to); @@ -482,7 +462,6 @@ void ms_inoti_modify_watch(char *path_from, char *path_to) /*this is new directory*/ if (find == false) { - MS_DBG("This is new directory"); ms_inoti_add_watch(path_to); } } @@ -500,13 +479,13 @@ gboolean ms_inoti_thread(void *data) char buffer[INOTI_BUF_LEN] = { 0 }; char path[MS_FILE_PATH_LEN_MAX] = { 0 }; struct inotify_event *event; - void *handle = NULL; + void **handle = NULL; MS_DBG("START INOTIFY"); err = ms_connect_db(&handle); if (err != MS_ERR_NONE) { - MS_DBG(" INOTIFY : sqlite3_open: ret = %d", err); + MS_DBG_ERR(" INOTIFY : sqlite3_open: ret = %d", err); return false; } @@ -515,16 +494,23 @@ gboolean ms_inoti_thread(void *data) length = read(inoti_fd, buffer, sizeof(buffer) - 1); if (length < 0 || length > sizeof(buffer)) { /*this is error */ - MS_DBG("fail read"); - perror("read"); continue; } while (i < length && i < INOTI_BUF_LEN) { + /*check poweroff status*/ + if(power_off) { + MS_DBG("power off"); + goto POWER_OFF; + } + /*it's possible that ums lets reset phone data... */ event = (struct inotify_event *)&buffer[i]; - if(strcmp(event->name, "_FILEOPERATION_END") == 0) { + if (strcmp(event->name, POWEROFF_DIR_NAME) == 0) { + MS_DBG("power off"); + goto POWER_OFF; + } else if(strcmp(event->name, "_FILEOPERATION_END") == 0) { /*file operation is end*/ /* announce db is updated*/ ms_set_db_status(MS_DB_UPDATED); @@ -532,11 +518,10 @@ gboolean ms_inoti_thread(void *data) goto NEXT_INOTI_EVENT; } else if (event->name[0] == '.') { /*event of hidden folder is ignored */ - MS_DBG("Ignore : First character of event->name includes invalid character"); goto NEXT_INOTI_EVENT; } else if (event->wd < 1) { /*this is error */ - MS_DBG("invalid wd : %d", event->wd); + MS_DBG_ERR("invalid wd : %d", event->wd); goto NEXT_INOTI_EVENT; } @@ -545,14 +530,14 @@ gboolean ms_inoti_thread(void *data) /*Add for fixing prevent defect 2011-02-15 */ err = ms_strcopy(name, sizeof(name), "%s", event->name); if (err != MS_ERR_NONE) { - MS_DBG("ms_strcopy error : %d", err); + MS_DBG_ERR("ms_strcopy error : %d", err); goto NEXT_INOTI_EVENT; } /*get full path of file or directory */ res = _ms_inoti_get_full_path(event->wd, name, path, sizeof(path)); if (res == false) { - MS_DBG("_ms_inoti_get_full_path error"); + MS_DBG_ERR("_ms_inoti_get_full_path error"); goto NEXT_INOTI_EVENT; } @@ -568,7 +553,7 @@ gboolean ms_inoti_thread(void *data) err = ms_strcopy(prev_name, sizeof(prev_name), "%s", event->name); if (err != MS_ERR_NONE) { - MS_DBG("ms_strcopy fail"); + MS_DBG_ERR("ms_strcopy fail"); goto NEXT_INOTI_EVENT; } } @@ -579,7 +564,7 @@ gboolean ms_inoti_thread(void *data) res = _ms_inoti_get_full_path(prev_wd, prev_name, full_path_from, sizeof(full_path_from)); if (res == false) { - MS_DBG("_ms_inoti_get_full_path error"); + MS_DBG_ERR("_ms_inoti_get_full_path error"); goto NEXT_INOTI_EVENT; } /*enable bundle commit*/ @@ -613,7 +598,7 @@ gboolean ms_inoti_thread(void *data) err = ms_delete_item(handle, path); if (err != MS_ERR_NONE) { - MS_DBG("ms_media_db_delete fail error : %d", err); + MS_DBG_ERR("ms_media_db_delete fail error : %d", err); } } else if (event->mask & IN_MOVED_TO) { @@ -621,7 +606,7 @@ gboolean ms_inoti_thread(void *data) err = ms_register_file(handle, path, NULL); if (err != MS_ERR_NONE) { - MS_DBG("ms_register_file error : %d", err); + MS_DBG_ERR("ms_register_file error : %d", err); } } else if (event->mask & IN_CREATE) { @@ -634,43 +619,36 @@ gboolean ms_inoti_thread(void *data) err = ms_delete_item(handle, path); if (err != MS_ERR_NONE) { - MS_DBG("ms_media_db_delete error : %d", err); + MS_DBG_ERR("ms_media_db_delete error : %d", err); } } else if (event->mask & IN_CLOSE_WRITE) { MS_DBG("CLOSE_WRITE"); - ms_create_file_info *node; - node = _ms_inoti_find_create_file_list (event->wd, name); + node = _ms_inoti_find_create_file_list (event->wd, name); if (node != NULL || ((prev_mask & IN_ISDIR) & IN_CREATE)) { - err = ms_register_file(handle, path, NULL); if (err != MS_ERR_NONE) { - MS_DBG("ms_register_file error : %d", err); + MS_DBG_ERR("ms_register_file error : %d", err); } if (node != NULL) _ms_inoti_delete_create_file_list(node); } else { - /*in case of replace */ - MS_DBG("This case is replacement or changing meta data."); ms_ignore_file_info *ignore_file; ignore_file = ms_inoti_find_ignore_file(path); if (ignore_file == NULL) { - err = ms_delete_item(handle, path); - if (err != MS_ERR_NONE) { - MS_DBG("ms_media_db_delete error : %d", err); - } - /*update = delete + regitster */ - err = ms_register_file(handle, path, NULL); + /*in case of replace */ + MS_DBG("This case is replacement or changing meta data."); + err = ms_refresh_item(handle, path); if (err != MS_ERR_NONE) { - MS_DBG("ms_register_file error : %d", err); + MS_DBG_ERR("ms_refresh_item error : %d", err); goto NEXT_INOTI_EVENT; } } else { - MS_DBG(" Ignore this file"); + /*This is ignore case*/ } } prev_mask = prev_wd = 0; /*reset */ @@ -678,7 +656,7 @@ gboolean ms_inoti_thread(void *data) } } /*end of one event */ else { - MS_DBG("Event length is zero or over MS_FILE_NAME_LEN_MAX"); + /*This is ignore case*/ if (event->mask & IN_IGNORED) { MS_DBG("This case is ignored"); } @@ -686,24 +664,218 @@ gboolean ms_inoti_thread(void *data) NEXT_INOTI_EVENT: ; i += INOTI_EVENT_SIZE + event->len; } + /*Active flush */ - sqlite3_release_memory(-1); malloc_trim(0); } - +POWER_OFF: ms_inoti_remove_watch(MS_DB_UPDATE_NOTI_PATH); - ms_inoti_remove_watch_recursive(MS_PHONE_ROOT_PATH); - ms_inoti_remove_watch_recursive(MS_MMC_ROOT_PATH); + ms_inoti_remove_watch_recursive(MS_ROOT_PATH_INTERNAL); + ms_inoti_remove_watch_recursive(MS_ROOT_PATH_EXTERNAL); close(inoti_fd); - err = ms_disconnect_db(handle); - if (err != MS_ERR_NONE) { - MS_DBG("ms_media_db_close error : %d", err); - return false; - } - MS_DBG("Disconnect MEDIA DB"); + if (handle) ms_disconnect_db(&handle); return false; } + +int _ms_get_path_from_current_node(int find_folder, + ms_dir_scan_info **current_root, + ms_dir_scan_info **real_root, char **path, int *depth) +{ + int err = MS_ERR_NONE; + char get_path[FAT_FILEPATH_LEN_MAX] = { 0 }; + + if (find_folder == 0) { + if ((*current_root)->Rbrother != NULL) { + *current_root = (*current_root)->Rbrother; + } else { + while (1) { + if ((*current_root)->parent == *real_root + || (*current_root)->parent == NULL) { + *current_root = NULL; + *depth = 0; + return MS_ERR_NONE; + } else if ((*current_root)->parent->Rbrother == NULL) { + *current_root = (*current_root)->parent; + (*depth) --; + } else { + *current_root = (*current_root)->parent->Rbrother; + (*depth) --; + break; + } + } + } + (*depth) --; + } + + err = ms_get_full_path_from_node(*current_root, get_path, *depth); + if (err != MS_ERR_NONE) + return MS_ERR_INVALID_DIR_PATH; + + *path = strdup(get_path); + + return err; +} + +void ms_inoti_add_watch_all_directory(ms_storage_type_t storage_type) +{ + int err = 0; + int depth = 0; + int find_folder = 0; + char get_path[MS_FILE_PATH_LEN_MAX] = { 0 }; + char *path = NULL; + DIR *dp = NULL; + struct dirent entry; + struct dirent *result; + + ms_dir_scan_info *root; + ms_dir_scan_info *tmp_root = NULL; + ms_dir_scan_info *cur_node = NULL; /*current node*/ + ms_dir_scan_info *prv_node = NULL; /*previous node*/ + ms_dir_scan_info *next_node = NULL; + + root = malloc(sizeof(ms_dir_scan_info)); + if (root == NULL) { + MS_DBG_ERR("malloc fail"); + return; + } + + if (storage_type == MS_STORAGE_INTERNAL) + root->name = strdup(MS_ROOT_PATH_INTERNAL); + else + root->name = strdup(MS_ROOT_PATH_EXTERNAL); + if (root->name == NULL) { + MS_DBG_ERR("strdup fail"); + free(root); + return; + } + + root->parent = NULL; + root->Rbrother = NULL; + root->next = NULL; + tmp_root = root; + prv_node = root; + + path = malloc(sizeof(char) * MS_FILE_PATH_LEN_MAX); + + err = ms_get_full_path_from_node(tmp_root, path, depth); + if (err != MS_ERR_NONE) { + return; + } + + ms_inoti_add_watch_with_node(root, depth); + + while (1) { + /*check poweroff status*/ + if (power_off) { + MS_DBG("Power off"); + goto FREE_RESOURCES; + } + + /*check SD card in out*/ + if ((mmc_state != VCONFKEY_SYSMAN_MMC_MOUNTED) && (storage_type == MS_STORATE_EXTERNAL)) + goto FREE_RESOURCES; + + depth ++; + dp = opendir(path); + if (dp == NULL) { + MS_DBG_ERR("%s folder opendir fails", path); + goto NEXT_DIR; + } + + while (!readdir_r(dp, &entry, &result)) { + /*check poweroff status*/ + if (power_off) { + MS_DBG("Power off"); + goto FREE_RESOURCES; + } + + if (result == NULL) + break; + + if (entry.d_name[0] == '.') + continue; + + /*check SD card in out*/ + if ((mmc_state != VCONFKEY_SYSMAN_MMC_MOUNTED) && (storage_type == MS_STORATE_EXTERNAL)) { + goto FREE_RESOURCES; + } + + if (entry.d_type & DT_DIR) { + DIR *tmp_dp = NULL; + err = ms_strappend(get_path, sizeof(get_path), "%s/%s",path, entry.d_name); + if (err != MS_ERR_NONE) { + MS_DBG_ERR("ms_strappend error"); + continue; + } + + tmp_dp = opendir(get_path); + if (tmp_dp == NULL) { + MS_DBG_ERR("%s folder opendir fails", get_path); + MS_DBG("error : %d, %s", errno ,strerror(errno)); + continue; + } + else + closedir(tmp_dp); + + cur_node = malloc(sizeof(ms_dir_scan_info)); + if (cur_node == NULL) { + MS_DBG_ERR("malloc fail"); + + goto FREE_RESOURCES; + } + + cur_node->name = strdup(entry.d_name); + cur_node->Rbrother = NULL; + cur_node->next = NULL; + + /*1. 1st folder */ + if (find_folder == 0) { + cur_node->parent = tmp_root; + tmp_root = cur_node; + } else { + cur_node->parent = tmp_root->parent; + prv_node->Rbrother = cur_node; + } + prv_node->next = cur_node; + + /*add watch */ + ms_inoti_add_watch_with_node(cur_node, depth); + + /*change previous */ + prv_node = cur_node; + find_folder++; + } + } +NEXT_DIR: + if (dp) closedir(dp); + if (path) free(path); + dp = NULL; + path = NULL; + + err = _ms_get_path_from_current_node(find_folder, &tmp_root, &root, &path, &depth); + if (err != MS_ERR_NONE) + break; + + if (tmp_root == NULL) + break; + + find_folder = 0; + } + +FREE_RESOURCES: + /*free allocated memory */ + if (path) free(path); + if (dp) closedir(dp); + + cur_node = root; + while (cur_node != NULL) { + next_node = cur_node->next; + free(cur_node->name); + free(cur_node); + cur_node = next_node; + } +}
\ No newline at end of file |