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.
This commit is contained in:
Yu Watanabe
2025-03-04 03:11:58 +09:00
parent ec32732043
commit e86a492ff0
5 changed files with 11 additions and 15 deletions

View 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

View 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)

View 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) {

View 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)

View 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)