From 17cfa0cb58bcfadb08b355eb5fd511ed6829ee5d Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Mon, 30 Jun 2025 22:04:01 +0000 Subject: Refactor _at functions, add symlink_at Signed-off-by: Laurent Bercot --- src/libunixonacid/stat_at.c | 61 ++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 31 deletions(-) (limited to 'src/libunixonacid/stat_at.c') diff --git a/src/libunixonacid/stat_at.c b/src/libunixonacid/stat_at.c index e4040d5..57aa8dd 100644 --- a/src/libunixonacid/stat_at.c +++ b/src/libunixonacid/stat_at.c @@ -8,10 +8,11 @@ #define _ATFILE_SOURCE #endif +#include #include -#include +#include #include #include @@ -27,52 +28,50 @@ int lstat_at (int dirfd, char const *file, struct stat *st) #else - /* OpenBSD plz. lstat() is POSIX. */ +#include #include -#include #include +#include #include -#include #include +#include "at-internal.h" -static int fstat_at (int dirfd, char const *file, struct stat *st, int (*dostat)(char const *, struct stat *)) +struct stat_s { - int r ; - int fdhere = open_read(".") ; - if (fdhere < 0) return -1 ; - if (fd_chdir(dirfd) < 0) - { - fd_close(fdhere) ; - return -1 ; - } - r = (*dostat)(file, st) ; - if (r < 0) - { - int e = errno ; - fd_chdir(fdhere) ; - fd_close(fdhere) ; - errno = e ; - return -1 ; - } - if (fd_chdir(fdhere) < 0) - { - fd_close(fdhere) ; - return -1 ; - } - fd_close(fdhere) ; - return r ; + char const *file ; + struct stat *st ; +} ; + +static int do_stat (void *p) +{ + struct stat_s *b = p ; + return stat(b->file, b->st) ; +} + +static int do_lstat (void *p) +{ + struct stat_s *b = p ; + return lstat(b->file, b->st) ; +} + +static void cancel_stat (int r, void *p) +{ + (void)r ; + (void)p ; } int stat_at (int dirfd, char const *file, struct stat *st) { - return fstat_at(dirfd, file, st, &stat) ; + struct stat_s args = { .file = file, .st = st } ; + return emulate_at(dirfd, &do_stat, &cancel_stat, &args) ; } int lstat_at (int dirfd, char const *file, struct stat *st) { - return fstat_at(dirfd, file, st, &lstat) ; + struct stat_s args = { .file = file, .st = st } ; + return emulate_at(dirfd, &do_lstat, &cancel_stat, &args) ; } #endif -- cgit v1.3.1