]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dirent-util: introduce simple wrapper of posix_getdents() 36565/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 3 Mar 2025 18:11:58 +0000 (03:11 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 3 Mar 2025 19:23:57 +0000 (04:23 +0900)
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.

meson.build
src/basic/dirent-util.h
src/basic/missing_syscall.h
src/basic/recurse-dir.c
src/basic/stat-util.c

index 6ef51069d9fea51f9386c84e907d25d19f29ea0d..fdad63fd298a233f614c5959d0f462fda2c6f813 100644 (file)
@@ -577,7 +577,7 @@ endforeach
 foreach ident : [
         ['set_mempolicy',     '''#include <sys/syscall.h>'''],  # declared at numaif.h provided by libnuma, which we do not use
         ['get_mempolicy',     '''#include <sys/syscall.h>'''],  # declared at numaif.h provided by libnuma, which we do not use
-        ['getdents64',        '''#include <dirent.h>'''],       # since glibc-2.30, but check it for musl
+        ['posix_getdents',    '''#include <dirent.h>'''],       # glibc does not implement it, but musl does
         ['strerrorname_np',   '''#include <string.h>'''],       # since glibc-2.32
         ['mallinfo',          '''#include <malloc.h>'''],       # deprecated since glibc-2.33, but check it for musl
         ['mallinfo2',         '''#include <malloc.h>'''],       # since glibc-2.33
index 0a2fcbf957a840254d9f20943749221e5b18ffc2..816b28e04feeda68d4373c955983d4dd8be0f887 100644 (file)
@@ -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)
 
index 53dcd69d9e4aba7d5da00de7ce46161822686bd1..aae07a3f37d470b8c71c71860c24f6b02c9d94a9 100644 (file)
@@ -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) {
index 9612b7c6a50b3c07f83095758fb19cfdd8d4eca9..31d19aef71b9fb15d8ca286967da5715922a1de1 100644 (file)
@@ -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)
index 8ae588e2e111782f72eec2b47f336587354a287b..f328f4a78c856f93b4263d8ef52914ef988739bc 100644 (file)
@@ -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)