xstd: add xgetdents()

This commit is contained in:
CismonX 2025-05-28 18:27:55 +08:00
parent 5a28b069c4
commit e6809b7e84
No known key found for this signature in database
GPG key ID: 3094873E29A482FB
2 changed files with 10 additions and 23 deletions

View file

@ -28,10 +28,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
#include "backend.h" #include "backend.h"
@ -67,10 +65,6 @@ struct fsck_dir {
#define FSCK_DIR_DONE ( 1u << 0 ) #define FSCK_DIR_DONE ( 1u << 0 )
// Forward declaration start // Forward declaration start
#ifdef __linux__
static ssize_t getdents_ (int, void *, size_t);
#endif
static int next_subdir (struct fsck_ctx *, struct fsck_dir *, static int next_subdir (struct fsck_ctx *, struct fsck_dir *,
struct dirent const **); struct dirent const **);
static int open_subdir (int, char const *, uint64_t *); static int open_subdir (int, char const *, uint64_t *);
@ -79,22 +73,6 @@ static void print_help (void);
static void print_version (void); static void print_version (void);
// Forward declaration end // Forward declaration end
#ifdef __linux__
// Some libc (e.g., musl) may declare a getdents() function
// in dirent.h with conflicting types.
#define getdents getdents_
static ssize_t
getdents_ (
int dirfd,
void *buf,
size_t bufsize
) {
return syscall(SYS_getdents64, dirfd, buf, bufsize);
}
#endif /* defined(__linux__) */
static int static int
next_subdir ( next_subdir (
struct fsck_ctx *ctx, struct fsck_ctx *ctx,
@ -112,7 +90,7 @@ next_subdir (
ctx->dent_buf = xrealloc(ctx->dent_buf, ctx->dent_buf_size); ctx->dent_buf = xrealloc(ctx->dent_buf, ctx->dent_buf_size);
} }
ssize_t nbytes ssize_t nbytes
= getdents(dir->fd, ctx->dent_buf + start, DIRENT_BUFSIZE); = xgetdents(dir->fd, ctx->dent_buf + start, DIRENT_BUFSIZE);
if (nbytes < 0) { if (nbytes < 0) {
log_printf("getdents(): %s", xstrerror(errno)); log_printf("getdents(): %s", xstrerror(errno));
return -1; return -1;

View file

@ -26,6 +26,9 @@
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <dirent.h>
#include <sys/syscall.h>
#include "defs.h" #include "defs.h"
#ifdef HAVE___BUILTIN_EXPECT #ifdef HAVE___BUILTIN_EXPECT
@ -84,6 +87,12 @@
} while (0) } while (0)
#endif #endif
#if defined(__linux__)
# define xgetdents(fd, buf, bufsz) syscall(SYS_getdents64, fd, buf, bufsz)
#elif defined(__FreeBSD__)
# define xgetdents(fd, buf, bufsz) getdents(fd, buf, bufsz)
#endif
/** /**
* Prints a message to standard error, and then aborts. * Prints a message to standard error, and then aborts.
*/ */