(check_dir_is_empty): handle the return value of mc_opendir().

Show error message if mc_opendir() returns NULL.

  * (erase_dir_iff_empty): sync with modified check_dir_is_empty().
  * (erase_dir): likewise.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2024-12-28 12:38:02 +03:00
parent 878491238f
commit 5fe1f3fce0

View File

@ -1501,7 +1501,9 @@ recursive_erase (file_op_context_t *ctx, const vfs_path_t *vpath)
/** /**
* Check if directory is empty or not. * Check if directory is empty or not.
* *
* @param ctx file operation context descriptor
* @param vpath directory handler * @param vpath directory handler
* @param error status of directory reading
* *
* @returns -1 on error, * @returns -1 on error,
* 1 if there are no entries besides "." and ".." in the directory path points to, * 1 if there are no entries besides "." and ".." in the directory path points to,
@ -1512,15 +1514,32 @@ recursive_erase (file_op_context_t *ctx, const vfs_path_t *vpath)
* in SHELL) don't return "." and ".." entries. * in SHELL) don't return "." and ".." entries.
*/ */
static int static int
check_dir_is_empty (const vfs_path_t *vpath) check_dir_is_empty (file_op_context_t *ctx, const vfs_path_t *vpath, FileProgressStatus *error)
{ {
DIR *dir; DIR *dir;
struct vfs_dirent *d; struct vfs_dirent *d;
int i = 1; int i = 1;
dir = mc_opendir (vpath); while ((dir = mc_opendir (vpath)) == NULL)
if (dir == NULL) {
return -1; if (ctx->ignore_all)
*error = FILE_IGNORE_ALL;
else
{
const FileProgressStatus status =
file_error (ctx, TRUE, _("Cannot enter into directory \"%s\"\n%s"),
vfs_path_as_str (vpath));
if (status == FILE_RETRY)
continue;
if (status == FILE_IGNORE_ALL)
ctx->ignore_all = TRUE;
*error = status; /* FILE_IGNORE, FILE_IGNORE_ALL, FILE_ABORT */
}
return (-1);
}
for (d = mc_readdir (dir); d != NULL; d = mc_readdir (dir)) for (d = mc_readdir (dir); d != NULL; d = mc_readdir (dir))
if (!DIR_IS_DOT (d->d_name) && !DIR_IS_DOTDOT (d->d_name)) if (!DIR_IS_DOT (d->d_name) && !DIR_IS_DOTDOT (d->d_name))
@ -1530,6 +1549,7 @@ check_dir_is_empty (const vfs_path_t *vpath)
} }
mc_closedir (dir); mc_closedir (dir);
*error = FILE_CONT;
return i; return i;
} }
@ -1538,6 +1558,8 @@ check_dir_is_empty (const vfs_path_t *vpath)
static FileProgressStatus static FileProgressStatus
erase_dir_iff_empty (file_op_context_t *ctx, const vfs_path_t *vpath) erase_dir_iff_empty (file_op_context_t *ctx, const vfs_path_t *vpath)
{ {
FileProgressStatus error = FILE_CONT;
file_progress_show_deleting (ctx, vpath, NULL); file_progress_show_deleting (ctx, vpath, NULL);
file_progress_show_count (ctx); file_progress_show_count (ctx);
if (file_progress_check_buttons (ctx) == FILE_ABORT) if (file_progress_check_buttons (ctx) == FILE_ABORT)
@ -1545,7 +1567,12 @@ erase_dir_iff_empty (file_op_context_t *ctx, const vfs_path_t *vpath)
mc_refresh (); mc_refresh ();
if (check_dir_is_empty (vpath) != 1) const int res = check_dir_is_empty (ctx, vpath, &error);
if (res == -1)
return error;
if (res != 1)
return FILE_CONT; return FILE_CONT;
/* not empty or error */ /* not empty or error */
@ -3331,6 +3358,8 @@ move_dir_dir (file_op_context_t *ctx, const char *s, const char *d)
FileProgressStatus FileProgressStatus
erase_dir (file_op_context_t *ctx, const vfs_path_t *vpath) erase_dir (file_op_context_t *ctx, const vfs_path_t *vpath)
{ {
FileProgressStatus error = FILE_CONT;
file_progress_show_deleting (ctx, vpath, NULL); file_progress_show_deleting (ctx, vpath, NULL);
file_progress_show_count (ctx); file_progress_show_count (ctx);
if (file_progress_check_buttons (ctx) == FILE_ABORT) if (file_progress_check_buttons (ctx) == FILE_ABORT)
@ -3345,10 +3374,14 @@ erase_dir (file_op_context_t *ctx, const vfs_path_t *vpath)
we would have to check also for EIO. I hope the new way is we would have to check also for EIO. I hope the new way is
fool proof. (Norbert) fool proof. (Norbert)
*/ */
if (check_dir_is_empty (vpath) == 0) const int res = check_dir_is_empty (ctx, vpath, &error);
{ /* not empty */
FileProgressStatus error;
if (res == -1)
return error;
if (res == 0)
{
/* not empty */
error = query_recursive (ctx, vfs_path_as_str (vpath)); error = query_recursive (ctx, vfs_path_as_str (vpath));
if (error == FILE_CONT) if (error == FILE_CONT)
error = recursive_erase (ctx, vpath); error = recursive_erase (ctx, vpath);