From: aman.jeph Date: Sun, 4 Oct 2020 09:24:07 +0000 (+0530) Subject: [TIZENIOT-1970] Implemented delete operation for folders X-Git-Tag: submit/tizen/20201008.085909~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2074a43e25a9ed6dda914f64e1b010cbbf6f3caf;p=profile%2Fiot%2Fapps%2Fnative%2Fvideo-player.git [TIZENIOT-1970] Implemented delete operation for folders Change-Id: I4b417830d9a62f25ddca0fd64c8ba043b33a0003 Signed-off-by: aman.jeph --- diff --git a/src/feature/mp-util-move.c b/src/feature/mp-util-move.c index e0a0946..ad73f8d 100755 --- a/src/feature/mp-util-move.c +++ b/src/feature/mp-util-move.c @@ -24,11 +24,18 @@ #include #include #include -#include #include #include #include +// this is required for nftw functions +#define __USE_GNU +#define __USE_XOPEN_EXTENDED +#include +#undef __USE_XOPEN_EXTENDED +#undef __USE_GNU + + #include "mp-util.h" #include "mp-video-log.h" @@ -318,7 +325,7 @@ static int __mp_util_move_copy_reg_file(const char *szSrcPath, if (buf_size == 0) { struct stat dst_info; if (stat(szDstPath, &dst_info)) { - VideoLogError("Fail to stat dst file", szDstPath); + VideoLogError("Fail to stat dst file: %s", szDstPath); goto ERROR_CLOSE_FD; } } @@ -355,7 +362,7 @@ static int __mp_util_move_copy_reg_file(const char *szSrcPath, ssize_t w_size = 0; w_size = fwrite(buf_p, 1, total, dst_f); if (ferror(dst_f) != 0 || (r_size != w_size)) { - VideoLogError("fail to write:%d,%d", dst_f, errno); + VideoLogError("fail to write:%p ,%s", dst_f, strerror(errno)); goto ERROR_CLOSE_FD; } @@ -663,7 +670,7 @@ int __mp_util_move_copy_internal(const char *szSrcPath, } if (stat(szSrcPath, &src_info)) { - VideoLogError("Fail to stat src ", szSrcPath); + VideoLogError("Fail to stat src: %s ", szSrcPath); return -1; } @@ -886,6 +893,69 @@ static bool __mp_util_check_if_in_same_folder(const char *sourcePath, } } +static int __mp_util_delete_directory_traversal_cb(const char *pathname, + const struct stat *statptr, + int type, struct FTW *buff) +{ + MpUtilDirListInfo *info = NULL; + MpUtilDirListInfo *info_title = NULL; + VideoSecureLogInfo("pathname is [%s]\t type is [%d]\t", pathname, type); + int ret = FTW_CONTINUE; + switch (type) { + case FTW_F: + if (mp_util_check_video_file(pathname)) { + struct stat src_info; + if (stat(pathname, &src_info)) { + VideoSecureLogInfo("Error in stat: %s", strerror(errno)); + } else { + info = calloc(sizeof(MpUtilDirListInfo), 1); + if (!info) { + VideoSecureLogInfo("failed to allocate memory"); + return FTW_STOP; + } + + info->ftw_path = g_strdup(pathname); + info->type = type; + info->size = (unsigned long long) src_info.st_size; + g_copy_list = g_slist_append(g_copy_list, info); + //find subtitle file for video file and append that too. + char *szSubTitle = NULL; + mp_util_get_subtitle_path(pathname, &szSubTitle); + if (szSubTitle) { + if (stat(szSubTitle, &src_info)) { + VideoSecureLogInfo("Error in stat: %s", strerror(errno)); + } else { + info_title = calloc(sizeof(MpUtilDirListInfo), 1); + if (info_title) { + info_title->ftw_path = g_strdup(szSubTitle); + info_title->type = type; + info_title->size = (unsigned long long) src_info.st_size; + g_copy_list = g_slist_append(g_copy_list, info_title); + } + } + MP_FREE_STRING(szSubTitle); + } + } + } + break; + case FTW_D: + // we only want to check files in current directory ..all the subdirectory of currrent directory will be skipped. + // so we check for level which is zero for current and greater than zero for sub directory. + // when level is zero we continue otherwise we skip. + if(buff->level == 0) { + ret = FTW_CONTINUE; + } else { + ret = FTW_SKIP_SUBTREE; + } + break; + default: + VideoSecureLogInfo("Default pathname is [%s]", pathname); + } + + return ret; +} + + /** * * @param szSrcPath @@ -904,34 +974,26 @@ static int __mp_util_move_delete_internal(const char *szSrcPath, __mp_util_move_free_directory_hierarchies(&g_copy_list); g_copy_list = NULL; } - ret = ftw(szSrcPath, __mp_util_move_directory_hierarchies, 16); + ret = nftw(szSrcPath, __mp_util_delete_directory_traversal_cb, 16, FTW_ACTIONRETVAL); if (ret == 0) { MpUtilDirListInfo *ent = NULL; GSList *list = NULL; list = g_copy_list; while (list) { if (mp_util_move_cancel_check()) { - goto DO_CANCEL; + ret = 1; + goto ERROR_CLOSE_FD; } ent = (MpUtilDirListInfo *) list->data; - VideoSecureLogInfo("name is [%s] type is [%d]", ent->ftw_path, - ent->type); - if (ent->type == FTW_D) { - list = g_slist_next(list); - continue; - } else if (ent->type == FTW_F) { + VideoSecureLogInfo("name is [%s] type is [%d]", ent->ftw_path, ent->type); + if (ent->type == FTW_F) { if (ent->ftw_path == NULL || strlen(ent->ftw_path) == 0) { list = g_slist_next(list); - continue; - } - if (!__mp_util_check_if_in_same_folder - (szSrcPath, ent->ftw_path)) { - list = g_slist_next(list); + VideoLogDebug("File name is invalid or empty skipping to next one"); continue; } if (!vp_file_unlink(ent->ftw_path)) { - VideoLogInfo("remove failed."); - goto ERROR_CLOSE_FD; + VideoLogError("Failed to remove file: %s", strerror(errno)); } mp_util_svc_scan_file(ent->ftw_path); } @@ -939,20 +1001,29 @@ static int __mp_util_move_delete_internal(const char *szSrcPath, } } else { + VideoLogError("Error in nftw: %s", strerror(errno)); + ret = -1; goto ERROR_CLOSE_FD; } - return 0; + if(ret == 0) { + int retval = rmdir(szSrcPath); + if(retval == 0) { + VideoLogDebug("Deleting empty dir: %s\n", szSrcPath); + } else { + if(errno == ENOTEMPTY) { + VideoLogDebug("Dir: [%s] is not empty, can't delete", szSrcPath); + } else { + VideoLogError("failed to delete dir: [%s], Error: %s", szSrcPath, strerror(errno)); + } + } + } ERROR_CLOSE_FD: __mp_util_move_free_directory_hierarchies(&g_copy_list); g_copy_list = NULL; - return -1; + return ret; - DO_CANCEL: - __mp_util_move_free_directory_hierarchies(&g_copy_list); - g_copy_list = NULL; - return 1; } /** @@ -977,7 +1048,7 @@ int mp_util_move_delete_video_folder(const char *szFolderPath, g_copy_list = NULL; } - /* copy items */ + /* delete items */ nRet = __mp_util_move_delete_internal(szFolderPath, userData); __mp_util_move_free_directory_hierarchies(&g_copy_list); g_copy_list = NULL; diff --git a/src/view/mp-video-list-remove-ctrl.c b/src/view/mp-video-list-remove-ctrl.c index f6dbb68..1d4b16a 100755 --- a/src/view/mp-video-list-remove-ctrl.c +++ b/src/view/mp-video-list-remove-ctrl.c @@ -157,31 +157,22 @@ static Eina_Bool mp_remove_ctrl_delete_item_idler_cb(void *pUserData) char *szMediaUrlForPreview = mp_util_config_get_preview(); VideoLogInfo("Media-Url For Preview : %s", szMediaUrlForPreview); int nResult = 0; - - for (nCounter = 0; nCounter < g_pRemoveCtrl->nDelEditListSize; - nCounter++) { + for (nCounter = 0; nCounter < g_pRemoveCtrl->nDelEditListSize; nCounter++) { if (g_pRemoveCtrl->pDelEditList[nCounter]) { if (g_pRemoveCtrl->bFolderDel) { - char *szFolderUrl = - mp_util_svc_get_video_folder_url(nCounter); - if (szFolderUrl) { - nResult = - mp_util_move_delete_video_folder(szFolderUrl, - NULL); + char *szFolderUrl = mp_util_svc_get_video_folder_url(nCounter); + if (szFolderUrl) { + nResult = mp_util_move_delete_video_folder(szFolderUrl,NULL); - if (szMediaUrlForPreview - && - !mp_util_check_local_file_exist - (szMediaUrlForPreview)) { + if (szMediaUrlForPreview && !mp_util_check_local_file_exist(szMediaUrlForPreview)) { mp_util_config_set_preview(NULL); MP_FREE_STRING(szMediaUrlForPreview); } - mp_util_svc_remove_folder_list_item_from_list - (nCounter); + mp_util_svc_remove_folder_list_item_from_list(nCounter); MP_FREE_STRING(szFolderUrl); if (nResult == 1) { - VideoLogInfo("cancel delete"); + VideoLogInfo("Deleting folder cancelled"); return FALSE; } }