maint: use enums to make dir_status code easier to read

* src/system.h: Add DS_EMPTY, and DS_NONEMPTY enums.
* src/remove.c: Use the new enums to make code easier to understand.
This commit is contained in:
Pádraig Brady committed 2022-09-25 15:26:20 +01:00
1 parent 800ff60f6f
commit 627c9a97c1
2 files changed
+13 -8

No files matched your search

+7 -7
View File
@@ -164,11 +164,11 @@ write_protected_non_symlink (int fd_cwd,
This is -1 if the directory is empty, 0 if it is nonempty, This is -1 if the directory is empty, 0 if it is nonempty,
and a positive error number if there was trouble determining the status, and a positive error number if there was trouble determining the status,
e.g., it is not a directory, or permissions problems, or I/O errors. e.g., it is not a directory, or permissions problems, or I/O errors.
Use *DIR_STATUS is a cache for the status. */ Use *DIR_STATUS as a cache for the status. */
static int static int
get_dir_status (FTS const *fts, FTSENT const *ent, int *dir_status) get_dir_status (FTS const *fts, FTSENT const *ent, int *dir_status)
{ {
if (*dir_status < -1) if (*dir_status == DS_UNKNOWN)
*dir_status = directory_status (fts->fts_cwd_fd, ent->fts_accpath); *dir_status = directory_status (fts->fts_cwd_fd, ent->fts_accpath);
return *dir_status; return *dir_status;
} }
@@ -254,7 +254,7 @@ prompt (FTS const *fts, FTSENT const *ent, bool is_dir,
prompting the user */ prompting the user */
if ( ! (x->recursive if ( ! (x->recursive
|| (x->remove_empty_directories || (x->remove_empty_directories
&& get_dir_status (fts, ent, dir_status) < 0))) && get_dir_status (fts, ent, dir_status) == DS_EMPTY)))
{ {
write_protected = -1; write_protected = -1;
wp_errno = *dir_status <= 0 ? EISDIR : *dir_status; wp_errno = *dir_status <= 0 ? EISDIR : *dir_status;
@@ -273,7 +273,7 @@ prompt (FTS const *fts, FTSENT const *ent, bool is_dir,
/* Issue the prompt. */ /* Issue the prompt. */
if (dirent_type == DT_DIR if (dirent_type == DT_DIR
&& mode == PA_DESCEND_INTO_DIR && mode == PA_DESCEND_INTO_DIR
&& get_dir_status (fts, ent, dir_status) == 0) && get_dir_status (fts, ent, dir_status) == DS_NONEMPTY)
fprintf (stderr, fprintf (stderr,
(write_protected (write_protected
? _("%s: descend into write-protected directory %s? ") ? _("%s: descend into write-protected directory %s? ")
@@ -427,14 +427,14 @@ excise (FTS *fts, FTSENT *ent, struct rm_options const *x, bool is_dir)
static enum RM_status static enum RM_status
rm_fts (FTS *fts, FTSENT *ent, struct rm_options const *x) rm_fts (FTS *fts, FTSENT *ent, struct rm_options const *x)
{ {
int dir_status = -2; int dir_status = DS_UNKNOWN;
switch (ent->fts_info) switch (ent->fts_info)
{ {
case FTS_D: /* preorder directory */ case FTS_D: /* preorder directory */
if (! x->recursive if (! x->recursive
&& !(x->remove_empty_directories && !(x->remove_empty_directories
&& get_dir_status (fts, ent, &dir_status) < 0)) && get_dir_status (fts, ent, &dir_status) == DS_EMPTY))
{ {
/* This is the first (pre-order) encounter with a directory /* This is the first (pre-order) encounter with a directory
that we cannot delete. that we cannot delete.
@@ -512,7 +512,7 @@ rm_fts (FTS *fts, FTSENT *ent, struct rm_options const *x)
enum RM_status s = prompt (fts, ent, true /*is_dir*/, x, enum RM_status s = prompt (fts, ent, true /*is_dir*/, x,
PA_DESCEND_INTO_DIR, &dir_status); PA_DESCEND_INTO_DIR, &dir_status);
if (s == RM_USER_ACCEPTED && dir_status == -1) if (s == RM_USER_ACCEPTED && dir_status == DS_EMPTY)
{ {
/* When we know (from prompt when in interactive mode) /* When we know (from prompt when in interactive mode)
that this is an empty directory, don't prompt twice. */ that this is an empty directory, don't prompt twice. */
+6 -1
View File
@@ -291,6 +291,11 @@ readdir_ignoring_dot_and_dotdot (DIR *dirp)
0 if DIR is a nonempty directory, 0 if DIR is a nonempty directory,
and a positive error number if there was trouble determining and a positive error number if there was trouble determining
whether DIR is an empty or nonempty directory. */ whether DIR is an empty or nonempty directory. */
enum {
DS_UNKNOWN = -2,
DS_EMPTY = -1,
DS_NONEMPTY = 0,
};
static inline int static inline int
directory_status (int fd_cwd, char const *dir) directory_status (int fd_cwd, char const *dir)
{ {
@@ -316,7 +321,7 @@ directory_status (int fd_cwd, char const *dir)
no_direntries = !readdir_ignoring_dot_and_dotdot (dirp); no_direntries = !readdir_ignoring_dot_and_dotdot (dirp);
saved_errno = errno; saved_errno = errno;
closedir (dirp); closedir (dirp);
return no_direntries && saved_errno == 0 ? -1 : saved_errno; return no_direntries && saved_errno == 0 ? DS_EMPTY : saved_errno;
} }
/* Factor out some of the common --help and --version processing code. */ /* Factor out some of the common --help and --version processing code. */