From: Yu Watanabe Date: Mon, 3 Mar 2025 18:11:58 +0000 (+0900) Subject: dirent-util: introduce simple wrapper of posix_getdents() X-Git-Tag: v258-rc1~1195^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F36565%2Fhead;p=thirdparty%2Fsystemd.git dirent-util: introduce simple wrapper of posix_getdents() glibc exports getdents64 syscall as is, but musl exports it as posix_getdents(). Let's introduce a simple wrapper of posix_getdents(). Note, our baseline for glibc is 2.31. Hence, we can assume getdents64() always defined when building with glibc. --- diff --git a/meson.build b/meson.build index 6ef51069d9f..fdad63fd298 100644 --- a/meson.build +++ b/meson.build @@ -577,7 +577,7 @@ endforeach foreach ident : [ ['set_mempolicy', '''#include '''], # declared at numaif.h provided by libnuma, which we do not use ['get_mempolicy', '''#include '''], # declared at numaif.h provided by libnuma, which we do not use - ['getdents64', '''#include '''], # since glibc-2.30, but check it for musl + ['posix_getdents', '''#include '''], # glibc does not implement it, but musl does ['strerrorname_np', '''#include '''], # since glibc-2.32 ['mallinfo', '''#include '''], # deprecated since glibc-2.33, but check it for musl ['mallinfo2', '''#include '''], # since glibc-2.33 diff --git a/src/basic/dirent-util.h b/src/basic/dirent-util.h index 0a2fcbf957a..816b28e04fe 100644 --- a/src/basic/dirent-util.h +++ b/src/basic/dirent-util.h @@ -30,6 +30,14 @@ struct dirent *readdir_no_dot(DIR *dirp); continue; \ else +/* Musl provides posix_getdents(). But glibc does not, and provides their own implementation as getdents64(). + * Let's introduce a simple wrapper. */ +#if !HAVE_POSIX_GETDENTS +static inline ssize_t posix_getdents(int fd, void *buf, size_t nbyte, int flags) { + return getdents64(fd, buf, nbyte); +} +#endif + /* Maximum space one dirent structure might require at most */ #define DIRENT_SIZE_MAX CONST_MAX(sizeof(struct dirent), offsetof(struct dirent, d_name) + NAME_MAX + 1) diff --git a/src/basic/missing_syscall.h b/src/basic/missing_syscall.h index 53dcd69d9e4..aae07a3f37d 100644 --- a/src/basic/missing_syscall.h +++ b/src/basic/missing_syscall.h @@ -212,16 +212,6 @@ static inline int missing_close_range(unsigned first_fd, unsigned end_fd, unsign /* ======================================================================= */ -#if !HAVE_GETDENTS64 -static inline ssize_t missing_getdents64(int fd, void *buffer, size_t length) { - return syscall(__NR_getdents64, fd, buffer, length); -} - -# define getdents64 missing_getdents64 -#endif - -/* ======================================================================= */ - #if !HAVE_SCHED_SETATTR /* since kernel 3.14 (e6cfc0295c7d51b008999a8b13a44fb43f8685ea) */ static inline ssize_t missing_sched_setattr(pid_t pid, struct sched_attr *attr, unsigned int flags) { diff --git a/src/basic/recurse-dir.c b/src/basic/recurse-dir.c index 9612b7c6a50..31d19aef71b 100644 --- a/src/basic/recurse-dir.c +++ b/src/basic/recurse-dir.c @@ -5,7 +5,6 @@ #include "fd-util.h" #include "fileio.h" #include "fs-util.h" -#include "missing_syscall.h" #include "mountpoint-util.h" #include "recurse-dir.h" #include "sort-util.h" @@ -51,7 +50,7 @@ int readdir_all(int dir_fd, RecurseDirFlags flags, DirectoryEntries **ret) { bs = MIN(MALLOC_SIZEOF_SAFE(de) - offsetof(DirectoryEntries, buffer), (size_t) SSIZE_MAX); assert(bs > de->buffer_size); - n = getdents64(dir_fd, (uint8_t*) de->buffer + de->buffer_size, bs - de->buffer_size); + n = posix_getdents(dir_fd, (uint8_t*) de->buffer + de->buffer_size, bs - de->buffer_size, /* flags = */ 0); if (n < 0) return -errno; if (n == 0) diff --git a/src/basic/stat-util.c b/src/basic/stat-util.c index 8ae588e2e11..f328f4a78c8 100644 --- a/src/basic/stat-util.c +++ b/src/basic/stat-util.c @@ -19,7 +19,6 @@ #include "macro.h" #include "missing_fs.h" #include "missing_magic.h" -#include "missing_syscall.h" #include "mountpoint-util.h" #include "nulstr-util.h" #include "parse-util.h" @@ -172,7 +171,7 @@ int dir_is_empty_at(int dir_fd, const char *path, bool ignore_hidden_or_backup) struct dirent *de; ssize_t n; - n = getdents64(fd, buf, m); + n = posix_getdents(fd, buf, m, /* flags = */ 0); if (n < 0) return -errno; if (n == 0)