2 * Copyright 2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
28 #include <sys/syscall.h>
31 #include "mf-delete-internal.h"
32 #include "mf-cancel.h"
33 #include "mf-fo-common.h"
34 #include "mf-fo-internal.h"
35 #include "mf-fo-debug.h"
36 #include "mf-media-content.h"
38 GSList *delete_list = NULL;
41 #define SAFE_FREE(x) do {\
49 int _mf_delete_delete_regfile(const char *file, struct stat *file_statp, mf_cancel *cancel, _mf_fo_msg_cb msg_cb, void *msg_data)
54 char err_buf[MF_ERR_BUF] = {0,};
56 mf_fo_loge("file is NULL");
57 err = MF_FO_ERR_SET(MF_FO_ERR_SRC_CLASS | MF_FO_ERR_ARGUMENT);
59 msg_cb(MF_MSG_ERROR, NULL, 0, err, msg_data);
64 if (cancel && mf_cancel_check_cancel(cancel)) {
69 mode = file_statp->st_mode;
70 size = file_statp->st_size;
73 if (stat(file, &info)) {
74 MF_FILE_ERROR_LOG(err_buf, "Fail to stat file ", file);
75 err = (_mf_fo_errno_to_mferr(errno) | MF_FO_ERR_SRC_CLASS);
77 msg_cb(MF_MSG_ERROR, file, 0, err, msg_data);
86 mf_fo_loge("[%s] is not regular file", file);
88 err = MF_FO_ERR_SET(MF_FO_ERR_SRC_CLASS | MF_FO_ERR_TYPE);
90 msg_cb(MF_MSG_ERROR, file, 0, err, msg_data);
96 MF_FILE_ERROR_LOG(err_buf, "Fail to delete ", file);
97 err = (_mf_fo_errno_to_mferr(errno) | MF_FO_ERR_SRC_CLASS);
99 msg_cb(MF_MSG_ERROR, file, 0, err, msg_data);
104 mf_fo_logd("success to delete FILE : %s", file);
107 msg_cb(MF_MSG_DOING, file, size, 0, msg_data);
113 static int __get_delete_directory_hierarchies( const char *pathname, const struct stat *statptr, int type)
115 mf_fo_dir_list_info *info = NULL;
116 mf_debug("pathname is [%s]\t type is [%d]\t",
122 info = calloc(sizeof(mf_fo_dir_list_info), 1);
123 info->ftw_path = g_strdup(pathname);
125 delete_list = g_slist_prepend(delete_list, info);
126 mf_debug("File pathname is [%s]", pathname);
129 info = calloc(sizeof(mf_fo_dir_list_info), 1);
130 info->ftw_path = g_strdup(pathname);
132 delete_list = g_slist_prepend(delete_list, info);
133 mf_debug("Directory pathname is [%s]", pathname);
137 mf_debug("Default pathname is [%s]", pathname);
143 int _mf_delete_delete_directory(const char *dir, mf_cancel *cancel, _mf_fo_msg_cb msg_cb, void *msg_data)
147 char err_buf[MF_ERR_BUF] = {0,};
149 mf_fo_loge("check argument dir");
150 err = MF_FO_ERR_SET(MF_FO_ERR_SRC_CLASS | MF_FO_ERR_ARGUMENT);
152 msg_cb(MF_MSG_ERROR, NULL, 0, err, msg_data);
157 ret = ftw(dir, __get_delete_directory_hierarchies, 16);
160 mf_fo_dir_list_info *ent = NULL;
164 if (cancel && mf_cancel_check_cancel(cancel)) {
167 ent = (mf_fo_dir_list_info *)list->data;
168 if (ent->type == FTW_F) {
169 if (ent->ftw_path == NULL || strlen(ent->ftw_path) == 0) {
170 list = g_slist_next(list);
173 err = _mf_delete_delete_regfile(ent->ftw_path, NULL, cancel, msg_cb, msg_data);
175 mf_media_content_scan_file(ent->ftw_path);
179 } else if (err < 0) {
183 } else if (ent->type == FTW_D) {
184 if (cancel && mf_cancel_check_cancel(cancel)) {
187 if (ent->ftw_path == NULL || strlen(ent->ftw_path) == 0) {
188 list = g_slist_next(list);
192 if (remove(ent->ftw_path)) {
193 MF_FILE_ERROR_LOG(err_buf, "Fail to delete ", ent->ftw_path);
194 err = (_mf_fo_errno_to_mferr(errno) | MF_FO_ERR_SRC_CLASS);
196 msg_cb(MF_MSG_ERROR, ent->ftw_path, 0, err, msg_data);
200 mf_fo_logd("success to delete DIR : %s", ent->ftw_path);
202 msg_cb(MF_MSG_DOING, ent->ftw_path, 0, 0, msg_data);
206 mf_debug("ent->path is [%s]", ent->ftw_path);
207 list = g_slist_next(list);
210 err = (_mf_fo_errno_to_mferr(errno) | MF_FO_ERR_SRC_CLASS);
212 msg_cb(MF_MSG_ERROR, dir, 0, err, msg_data);
214 _mf_fo_free_directory_hierarchies(&delete_list);
218 _mf_fo_free_directory_hierarchies(&delete_list);
223 _mf_fo_free_directory_hierarchies(&delete_list);
227 _mf_fo_free_directory_hierarchies(&delete_list);
231 int _mf_delete_del_internal(const char *item, mf_cancel *cancel, _mf_fo_msg_cb msg_callback, void *msg_data)
235 char err_buf[MF_ERR_BUF] = {0,};
236 if (!item || strlen(item) <= 1) {
237 err = MF_FO_ERR_SET(MF_FO_ERR_SRC_CLASS | MF_FO_ERR_ARGUMENT);
239 msg_callback(MF_MSG_ERROR, NULL, 0, err, msg_data);
244 if (stat(item, &info)) {
245 MF_FILE_ERROR_LOG(err_buf, "Fail to stat item ", item);
246 err = (_mf_fo_errno_to_mferr(errno) | MF_FO_ERR_SRC_CLASS);
248 msg_callback(MF_MSG_ERROR, item, 0, err, msg_data);
253 if (cancel && mf_cancel_check_cancel(cancel)) {
257 if (S_ISDIR(info.st_mode)) {
258 err = _mf_delete_delete_directory(item, cancel, msg_callback, msg_data);
260 mf_media_content_scan_folder(item);
262 } else if (S_ISREG(info.st_mode)) {
263 err = _mf_delete_delete_regfile(item, &info, cancel, msg_callback, msg_data);
265 mf_media_content_scan_file(item);
269 mf_fo_loge("item[%s] is not file or directory", item);
270 err = MF_FO_ERR_SET(MF_FO_ERR_SRC_CLASS | MF_FO_ERR_TYPE | MF_FO_ERR_REPORT_CLASS);
272 msg_callback(MF_MSG_ERROR, item, 0, err, msg_data);
279 } else if (err < 0) {