diff options
| author | Laurent Bercot <ska-skaware@skarnet.org> | 2025-06-30 22:04:01 +0000 |
|---|---|---|
| committer | Laurent Bercot <ska@appnovation.com> | 2025-06-30 22:04:01 +0000 |
| commit | 17cfa0cb58bcfadb08b355eb5fd511ed6829ee5d (patch) | |
| tree | c543b313369bb13e5a6a894c0c1b1fb408ef4586 /src/libunixonacid | |
| parent | d4895d80b1b1af9086b08f7c6cb12f274baf2ddc (diff) | |
| download | skalibs-17cfa0cb58bcfadb08b355eb5fd511ed6829ee5d.tar.gz | |
Refactor _at functions, add symlink_at
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libunixonacid')
| -rw-r--r-- | src/libunixonacid/access_at.c | 44 | ||||
| -rw-r--r-- | src/libunixonacid/at-internal.h | 10 | ||||
| -rw-r--r-- | src/libunixonacid/emulate_at.c | 43 | ||||
| -rw-r--r-- | src/libunixonacid/open2_at.c | 48 | ||||
| -rw-r--r-- | src/libunixonacid/open3_at.c | 48 | ||||
| -rw-r--r-- | src/libunixonacid/stat_at.c | 61 | ||||
| -rw-r--r-- | src/libunixonacid/symlink_at.c | 60 |
7 files changed, 210 insertions, 104 deletions
diff --git a/src/libunixonacid/access_at.c b/src/libunixonacid/access_at.c index e7f988c..f974fbe 100644 --- a/src/libunixonacid/access_at.c +++ b/src/libunixonacid/access_at.c @@ -27,30 +27,32 @@ int access_at (int dirfd, char const *file, int amode, unsigned int flag) #include <skalibs/djbunix.h> #include <skalibs/unix-transactional.h> +#include "at-internal.h" + +struct access_s +{ + char const *file ; + int amode ; +} ; + +static int do_access (void *p) +{ + struct access_s *b = p ; + return access(b->file, b->amode) ; +} + +static void cancel_access (int r, void *p) +{ + (void)r ; + (void)p ; +} int access_at (int dirfd, char const *file, int amode, unsigned int flag) { - int fdhere ; - if (getuid() != geteuid() || getgid() != getegid()) - return (errno = ENOSYS, -1) ; - (void)flag ; - fdhere = openc_read(".") ; - if (fdhere < 0) return -1 ; - if (fd_chdir(dirfd) < 0) - { - fd_close(fdhere) ; - return -1 ; - } - if (access(file, amode) < 0) - { - int e = errno ; - fd_chdir(fdhere) ; - fd_close(fdhere) ; - return (errno = e, -1) ; - } - fd_chdir(fdhere) ; - fd_close(fdhere) ; - return 0 ; + struct access_s args = { .file = file, .amode = amode } ; + if (flag && (getuid() != geteuid() || getgid() != getegid())) + return (errno = ENOSYS, 1) ; + return emulate_at(dirfd, &do_access, &cancel_access, &args) ; } #endif diff --git a/src/libunixonacid/at-internal.h b/src/libunixonacid/at-internal.h new file mode 100644 index 0000000..e76d68a --- /dev/null +++ b/src/libunixonacid/at-internal.h @@ -0,0 +1,10 @@ +/* ISC license. */ + +#ifndef SKALIBS_AT_INTERNAL_H +#define SKALIBS_AT_INTERNAL_H + +#include <skalibs/functypes.h> + +extern int emulate_at (int, init_func_ref, deinit_func_ref, void *) ; + +#endif diff --git a/src/libunixonacid/emulate_at.c b/src/libunixonacid/emulate_at.c new file mode 100644 index 0000000..0bb194a --- /dev/null +++ b/src/libunixonacid/emulate_at.c @@ -0,0 +1,43 @@ +/* ISC license. */ + +#include <skalibs/sysdeps.h> + +#if !defined(SKALIBS_HASOPENAT) || !defined(SKALIBS_HASLINKAT) + +#include <skalibs/bsdsnowflake.h> + +#include <errno.h> + +#include <skalibs/functypes.h> +#include <skalibs/djbunix.h> +#include <skalibs/unix-transactional.h> +#include "at-internal.h" + +int emulate_at (int dirfd, init_func_ref f, deinit_func_ref g, void *p) +{ + int r ; + int fdhere = open_read(".") ; + if (fdhere < 0) return -1 ; + if (fd_chdir(dirfd) < 0) goto errclose ; + r = (*f)(p) ; + if (r < 0) + { + int e = errno ; + fd_chdir(fdhere) ; + errno = e ; + goto errclose ; + } + if (fd_chdir(fdhere) < 0) + { + (*g)(r, p) ; + goto errclose ; + } + fd_close(fdhere) ; + return r ; + + errclose: + fd_close(fdhere) ; + return -1 ; +} + +#endif diff --git a/src/libunixonacid/open2_at.c b/src/libunixonacid/open2_at.c index d96c0b7..3afc4fb 100644 --- a/src/libunixonacid/open2_at.c +++ b/src/libunixonacid/open2_at.c @@ -12,6 +12,7 @@ #include <errno.h> +#include <skalibs/stat.h> #include <skalibs/fcntl.h> #include <skalibs/unix-transactional.h> @@ -25,38 +26,33 @@ int open2_at (int dirfd, char const *file, int flags) #else -#include <sys/stat.h> -#include <errno.h> - #include <skalibs/fcntl.h> #include <skalibs/djbunix.h> #include <skalibs/unix-transactional.h> +#include "at-internal.h" -int open2_at (int dirfd, char const *file, int flags) +struct open2_s { - int fd ; - int fdhere = open_read(".") ; - if (fdhere < 0) return -1 ; - if (fd_chdir(dirfd) < 0) goto errclose ; - fd = open2(file, flags) ; - if (fd < 0) - { - int e = errno ; - fd_chdir(fdhere) ; - errno = e ; - goto errclose ; - } - if (fd_chdir(fdhere) < 0) - { - fd_close(fd) ; - goto errclose ; - } - fd_close(fdhere) ; - return fd ; + char const *file ; + int flags ; +} ; - errclose: - fd_close(fdhere) ; - return -1 ; +static int do_open2 (void *p) +{ + struct open2_s *b = p ; + return open2(b->file, b->flags) ; +} + +static void cancel_open2 (int fd, void *p) +{ + (void)p ; + fd_close(fd) ; +} + +int open2_at (int dirfd, char const *file, int flags) +{ + struct open2_s args = { .file = file, .flags = flags } ; + return emulate_at(dirfd, &do_open2, &cancel_open2, &args) ; } #endif diff --git a/src/libunixonacid/open3_at.c b/src/libunixonacid/open3_at.c index 742a579..a960339 100644 --- a/src/libunixonacid/open3_at.c +++ b/src/libunixonacid/open3_at.c @@ -25,38 +25,34 @@ int open3_at (int dirfd, char const *file, int flags, unsigned int mode) #else -#include <sys/stat.h> -#include <errno.h> - #include <skalibs/fcntl.h> #include <skalibs/djbunix.h> #include <skalibs/unix-transactional.h> +#include "at-internal.h" -int open3_at (int dirfd, char const *file, int flags, unsigned int mode) +struct open3_s { - int fd ; - int fdhere = open_read(".") ; - if (fdhere < 0) return -1 ; - if (fd_chdir(dirfd) < 0) goto errclose ; - fd = open3(file, flags, mode) ; - if (fd < 0) - { - int e = errno ; - fd_chdir(fdhere) ; - errno = e ; - goto errclose ; - } - if (fd_chdir(fdhere) < 0) - { - fd_close(fd) ; - goto errclose ; - } - fd_close(fdhere) ; - return fd ; + char const *file ; + int flags ; + unsigned int mode ; +} ; - errclose: - fd_close(fdhere) ; - return -1 ; +static int do_open3 (void *p) +{ + struct open3_s *b = p ; + return open3(b->file, b->flags, b->mode) ; +} + +static void cancel_open3 (int fd, void *p) +{ + (void)p ; + fd_close(fd) ; +} + +int open3_at (int dirfd, char const *file, int flags, unsigned int mode) +{ + struct open3_s args = { .file = file, .flags = flags, .mode = mode } ; + return emulate_at(dirfd, &do_open3, &cancel_open3, &args) ; } #endif 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 <skalibs/bsdsnowflake.h> #include <skalibs/nonposix.h> -#include <sys/stat.h> +#include <skalibs/stat.h> #include <skalibs/fcntl.h> #include <skalibs/unix-transactional.h> @@ -27,52 +28,50 @@ int lstat_at (int dirfd, char const *file, struct stat *st) #else - /* OpenBSD plz. lstat() is POSIX. */ +#include <skalibs/bsdsnowflake.h> #include <skalibs/nonposix.h> -#include <sys/stat.h> #include <errno.h> +#include <skalibs/stat.h> #include <skalibs/fcntl.h> -#include <skalibs/djbunix.h> #include <skalibs/unix-transactional.h> +#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 diff --git a/src/libunixonacid/symlink_at.c b/src/libunixonacid/symlink_at.c new file mode 100644 index 0000000..4d63120 --- /dev/null +++ b/src/libunixonacid/symlink_at.c @@ -0,0 +1,60 @@ +/* ISC license. */ + +#include <skalibs/sysdeps.h> + +#ifdef SKALIBS_HASLINKAT + +#ifndef _ATFILE_SOURCE +#define _ATFILE_SOURCE +#endif + +#include <skalibs/nonposix.h> + +#include <unistd.h> +#include <errno.h> + +#include <skalibs/fcntl.h> +#include <skalibs/unix-transactional.h> + +int symlink_at (char const *src, int dirfd, char const *dst) +{ + int r ; + do r = symlinkat(src, dirfd, dst) ; + while (r == -1 && errno == EINTR) ; + return r ; +} + +#else + +#include <unistd.h> +#include <errno.h> + +#include <skalibs/posixplz.h> +#include <skalibs/unix-transactional.h> + +struct symlink_s +{ + char const *src ; + char const *dst ; +} ; + +static int do_symlink (void *p) +{ + struct symlink_s *b = p ; + return symlink(b->src, b->dst) ; +} + +static void cancel_symlink (int r, void *p) +{ + struct symlink_s *b = p ; + (void)r ; + unlink_void(b->dst) ; +} + +int symlink_at (char const *src, int dirfd, char const *dst) +{ + struct symlink_s args = { .src = src, .dst = dst } ; + return emulate_at(dirfd, &do_symlink, &cancel_symlink, &args) ; +} + +#endif |
