#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>
-#include <ftw.h>
#include <Ecore_Evas.h>
#include <sys/stat.h>
#include <sys/statfs.h>
+// this is required for nftw functions
+#define __USE_GNU
+#define __USE_XOPEN_EXTENDED
+#include <ftw.h>
+#undef __USE_XOPEN_EXTENDED
+#undef __USE_GNU
+
+
#include "mp-util.h"
#include "mp-video-log.h"
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;
}
}
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;
}
}
if (stat(szSrcPath, &src_info)) {
- VideoLogError("Fail to stat src ", szSrcPath);
+ VideoLogError("Fail to stat src: %s ", szSrcPath);
return -1;
}
}
}
+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
__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);
}
}
} 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;
}
/**
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;
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;
}
}