From 7f0f877672b7189f20a2429b323ddd475e5d8acd Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 15 Oct 2025 15:46:06 +0200 Subject: [PATCH] treewide: use is_dotdir_dirent() helper This simplifies the code by using the is_dotdir_dirent() helper function instead of manual strcmp() checks for "." and ".." directory entries across multiple utilities. Signed-off-by: Karel Zak --- disk-utils/partx.c | 4 ++-- lib/loopdev.c | 6 ++---- libsmartcols/samples/tree.c | 3 ++- misc-utils/whereis.c | 3 ++- sys-utils/eject.c | 3 ++- sys-utils/switch_root.c | 3 ++- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/disk-utils/partx.c b/disk-utils/partx.c index 347d5daad..68d29f62f 100644 --- a/disk-utils/partx.c +++ b/disk-utils/partx.c @@ -41,6 +41,7 @@ #include "loopdev.h" #include "closestream.h" #include "optutils.h" +#include "fileutils.h" /* this is the default upper limit, could be modified by --nr */ #define SLICES_MAX 256 @@ -245,8 +246,7 @@ static int get_max_partno(const char *disk, dev_t devno) while ((d = readdir(dir))) { int fd; - if (!strcmp(d->d_name, ".") || - !strcmp(d->d_name, "..")) + if (is_dotdir_dirent(d)) continue; #ifdef _DIRENT_HAVE_D_TYPE if (d->d_type != DT_DIR && d->d_type != DT_UNKNOWN) diff --git a/lib/loopdev.c b/lib/loopdev.c index a32003d4b..5ef439440 100644 --- a/lib/loopdev.c +++ b/lib/loopdev.c @@ -478,7 +478,7 @@ static int loop_scandir(const char *dirname, int **ary, int hasprefix) d->d_type != DT_LNK) continue; #endif - if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) + if (is_dotdir_dirent(d)) continue; if (hasprefix) { @@ -584,9 +584,7 @@ static int loopcxt_next_from_sysfs(struct loopdev_cxt *lc) DBG(ITER, ul_debugobj(iter, "check %s", d->d_name)); - if (strcmp(d->d_name, ".") == 0 - || strcmp(d->d_name, "..") == 0 - || strncmp(d->d_name, "loop", 4) != 0) + if (is_dotdir_dirent(d) || strncmp(d->d_name, "loop", 4) != 0) continue; snprintf(name, sizeof(name), "%s/loop/backing_file", d->d_name); diff --git a/libsmartcols/samples/tree.c b/libsmartcols/samples/tree.c index e40ea9a31..bdb93629a 100644 --- a/libsmartcols/samples/tree.c +++ b/libsmartcols/samples/tree.c @@ -16,6 +16,7 @@ #include "c.h" #include "nls.h" #include "strutils.h" +#include "fileutils.h" #include "libsmartcols.h" @@ -124,7 +125,7 @@ static int add_children(struct libscols_table *tb, while ((d = readdir(dir))) { struct stat st; - if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) + if (is_dotdir_dirent(d)) continue; if (fstatat(fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0) continue; diff --git a/misc-utils/whereis.c b/misc-utils/whereis.c index 8e500c009..f705ac278 100644 --- a/misc-utils/whereis.c +++ b/misc-utils/whereis.c @@ -57,6 +57,7 @@ #include "c.h" #include "closestream.h" #include "canonicalize.h" +#include "fileutils.h" #include "debug.h" @@ -297,7 +298,7 @@ static void dirlist_add_subdir(struct wh_dirlist **ls, int type, const char *dir dir, buf, postfix ? postfix : "")); while ((dp = readdir(dirp)) != NULL) { - if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) + if (is_dotdir_dirent(dp)) continue; if (postfix) snprintf(d, PATH_MAX - len, "%s%s", dp->d_name, postfix); diff --git a/sys-utils/eject.c b/sys-utils/eject.c index dad88978d..f5d48badb 100644 --- a/sys-utils/eject.c +++ b/sys-utils/eject.c @@ -53,6 +53,7 @@ #include "pathnames.h" #include "sysfs.h" #include "monotonic.h" +#include "fileutils.h" /* * sg_io_hdr_t driver_status -- see kernel include/scsi/sg.h @@ -779,7 +780,7 @@ static int umount_partitions(struct eject_control *ctl) /* scan for partition subdirs */ while ((d = readdir(dir))) { - if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) + if (is_dotdir_dirent(d)) continue; if (sysfs_blkdev_is_partition_dirent(dir, d, ctl->device)) { diff --git a/sys-utils/switch_root.c b/sys-utils/switch_root.c index ab8c2a141..69ae77080 100644 --- a/sys-utils/switch_root.c +++ b/sys-utils/switch_root.c @@ -39,6 +39,7 @@ #include "nls.h" #include "closestream.h" #include "statfs_magic.h" +#include "fileutils.h" #ifndef MS_MOVE #define MS_MOVE 8192 @@ -81,7 +82,7 @@ static int recursiveRemove(int fd) break; /* end of directory */ } - if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) + if (is_dotdir_dirent(d)) continue; #ifdef _DIRENT_HAVE_D_TYPE if (d->d_type == DT_DIR || d->d_type == DT_UNKNOWN) -- 2.47.3