From 37b42c179bb00d05cb47c739cb95283548f84fb2 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Mon, 30 May 2022 19:51:44 +0000 Subject: Add openc_*at functions Signed-off-by: Laurent Bercot --- src/include/skalibs/unix-transactional.h | 22 ++++++++++++++-------- src/libunixonacid/access_at.c | 2 +- src/libunixonacid/openc_appendat.c | 11 +++++++++++ src/libunixonacid/openc_appendatb.c | 12 ++++++++++++ src/libunixonacid/openc_readat.c | 10 ++++++++++ src/libunixonacid/openc_readatb.c | 12 ++++++++++++ src/libunixonacid/openc_truncat.c | 10 ++++++++++ src/libunixonacid/openc_truncatb.c | 12 ++++++++++++ src/libunixonacid/openc_writeat.c | 10 ++++++++++ src/libunixonacid/openc_writeatb.c | 12 ++++++++++++ src/libunixonacid/opendir_at.c | 6 +++++- src/libunixonacid/opengetlnclose.c | 2 +- src/libunixonacid/opengetlnclose_at.c | 2 +- src/libunixonacid/openreadnclose_at.c | 3 ++- src/libunixonacid/openslurpclose_at.c | 2 +- src/libunixonacid/openwritenclose_at.c | 3 ++- src/libunixonacid/openwritevnclose_at.c | 3 ++- 17 files changed, 118 insertions(+), 16 deletions(-) create mode 100644 src/libunixonacid/openc_appendat.c create mode 100644 src/libunixonacid/openc_appendatb.c create mode 100644 src/libunixonacid/openc_readat.c create mode 100644 src/libunixonacid/openc_readatb.c create mode 100644 src/libunixonacid/openc_truncat.c create mode 100644 src/libunixonacid/openc_truncatb.c create mode 100644 src/libunixonacid/openc_writeat.c create mode 100644 src/libunixonacid/openc_writeatb.c (limited to 'src') diff --git a/src/include/skalibs/unix-transactional.h b/src/include/skalibs/unix-transactional.h index 88d1838..5b9c4fa 100644 --- a/src/include/skalibs/unix-transactional.h +++ b/src/include/skalibs/unix-transactional.h @@ -3,21 +3,23 @@ #ifndef SKALIBS_UNIX_TRANSACTIONAL_H #define SKALIBS_UNIX_TRANSACTIONAL_H -#include +#include #include #include -#include #include #include /* Transactional/reliable filesystem operations */ +extern int opengetlnclose (char const *, stralloc *, int) ; + extern int open2_at (int, char const *, int) ; extern int open3_at (int, char const *, int, unsigned int) ; extern int access_at (int, char const *, int, unsigned int) ; - -extern int opengetlnclose (char const *, stralloc *, int) ; +extern DIR *opendir_at (int, char const *) ; +extern int stat_at (int, char const *, struct stat *) ; +extern int lstat_at (int, char const *, struct stat *) ; extern int open_readat (int, char const *) ; extern int open_readatb (int, char const *) ; @@ -27,10 +29,14 @@ extern int open_truncat (int, char const *) ; extern int open_truncatb (int, char const *) ; extern int open_appendat (int, char const *) ; extern int open_appendatb (int, char const *) ; -extern DIR *opendir_at (int, char const *) ; - -extern int stat_at (int, char const *, struct stat *) ; -extern int lstat_at (int, char const *, struct stat *) ; +extern int openc_readat (int, char const *) ; +extern int openc_readatb (int, char const *) ; +extern int openc_writeat (int, char const *) ; +extern int openc_writeatb (int, char const *) ; +extern int openc_truncat (int, char const *) ; +extern int openc_truncatb (int, char const *) ; +extern int openc_appendat (int, char const *) ; +extern int openc_appendatb (int, char const *) ; extern size_t openreadnclose_at (int, char const *, char *, size_t) ; extern int openslurpclose_at (int, char const *, stralloc *) ; diff --git a/src/libunixonacid/access_at.c b/src/libunixonacid/access_at.c index ebe6dd7..69fdce4 100644 --- a/src/libunixonacid/access_at.c +++ b/src/libunixonacid/access_at.c @@ -34,7 +34,7 @@ int access_at (int dirfd, char const *file, int amode, unsigned int flag) if (getuid() != geteuid() || getgid() != getegid()) return (errno = ENOSYS, -1) ; (void)flag ; - fdhere = open_read(".") ; + fdhere = openc_read(".") ; if (fdhere < 0) return -1 ; if (fd_chdir(dirfd) < 0) { diff --git a/src/libunixonacid/openc_appendat.c b/src/libunixonacid/openc_appendat.c new file mode 100644 index 0000000..70b6df7 --- /dev/null +++ b/src/libunixonacid/openc_appendat.c @@ -0,0 +1,11 @@ +/* ISC license. */ + +#include +#include + +#include + +int openc_appendat (int fd, char const *name) +{ + return open3_at(fd, name, O_WRONLY | O_NONBLOCK | O_APPEND | O_CREAT | O_CLOEXEC, 0666) ; +} diff --git a/src/libunixonacid/openc_appendatb.c b/src/libunixonacid/openc_appendatb.c new file mode 100644 index 0000000..066e695 --- /dev/null +++ b/src/libunixonacid/openc_appendatb.c @@ -0,0 +1,12 @@ +/* ISC license. */ + +#include +#include + +int openc_appendatb (int dirfd, char const *name) +{ + int fd = openc_appendat(dirfd, name) ; + if (fd < 0) return -1 ; + if (ndelay_off(fd) < 0) return -1 ; + return fd ; +} diff --git a/src/libunixonacid/openc_readat.c b/src/libunixonacid/openc_readat.c new file mode 100644 index 0000000..dc092b6 --- /dev/null +++ b/src/libunixonacid/openc_readat.c @@ -0,0 +1,10 @@ +/* ISC license. */ + +#include + +#include + +int openc_readat (int fd, char const *name) +{ + return open2_at(fd, name, O_RDONLY | O_NONBLOCK | O_CLOEXEC) ; +} diff --git a/src/libunixonacid/openc_readatb.c b/src/libunixonacid/openc_readatb.c new file mode 100644 index 0000000..4da65ab --- /dev/null +++ b/src/libunixonacid/openc_readatb.c @@ -0,0 +1,12 @@ +/* ISC license. */ + +#include +#include + +int openc_readatb (int dirfd, char const *name) +{ + int fd = openc_readat(dirfd, name) ; + if (fd < 0) return -1 ; + if (ndelay_off(fd) < 0) return -1 ; + return fd ; +} diff --git a/src/libunixonacid/openc_truncat.c b/src/libunixonacid/openc_truncat.c new file mode 100644 index 0000000..8ce50d2 --- /dev/null +++ b/src/libunixonacid/openc_truncat.c @@ -0,0 +1,10 @@ +/* ISC license. */ + +#include + +#include + +int openc_truncat (int fd, char const *name) +{ + return open3_at(fd, name, O_WRONLY | O_NONBLOCK | O_TRUNC | O_CREAT | O_CLOEXEC, 0666) ; +} diff --git a/src/libunixonacid/openc_truncatb.c b/src/libunixonacid/openc_truncatb.c new file mode 100644 index 0000000..a791790 --- /dev/null +++ b/src/libunixonacid/openc_truncatb.c @@ -0,0 +1,12 @@ +/* ISC license. */ + +#include +#include + +int openc_truncatb (int dirfd, char const *name) +{ + int fd = openc_truncat(dirfd, name) ; + if (fd < 0) return -1 ; + if (ndelay_off(fd) < 0) return -1 ; + return fd ; +} diff --git a/src/libunixonacid/openc_writeat.c b/src/libunixonacid/openc_writeat.c new file mode 100644 index 0000000..e584e57 --- /dev/null +++ b/src/libunixonacid/openc_writeat.c @@ -0,0 +1,10 @@ +/* ISC license. */ + +#include + +#include + +int openc_writeat (int fd, char const *name) +{ + return open2_at(fd, name, O_WRONLY | O_NONBLOCK | O_CLOEXEC) ; +} diff --git a/src/libunixonacid/openc_writeatb.c b/src/libunixonacid/openc_writeatb.c new file mode 100644 index 0000000..daa1fe8 --- /dev/null +++ b/src/libunixonacid/openc_writeatb.c @@ -0,0 +1,12 @@ +/* ISC license. */ + +#include +#include + +int openc_writeatb (int dirfd, char const *name) +{ + int fd = openc_writeat(dirfd, name) ; + if (fd < 0) return -1 ; + if (ndelay_off(fd) < 0) return -1 ; + return fd ; +} diff --git a/src/libunixonacid/opendir_at.c b/src/libunixonacid/opendir_at.c index bee971f..c1de4f1 100644 --- a/src/libunixonacid/opendir_at.c +++ b/src/libunixonacid/opendir_at.c @@ -1,5 +1,9 @@ /* ISC license. */ +/* Should not be necessary but old NetBSD/OpenBSD fail to + properly expose fdopendir() otherwise */ +#include + #include #include #include @@ -7,7 +11,7 @@ DIR *opendir_at (int dfd, char const *name) { DIR *dir ; - int fd = open_readatb(dfd, name) ; + int fd = openc_readatb(dfd, name) ; if (fd < 0) return 0 ; dir = fdopendir(fd) ; if (!dir) fd_close(fd) ; diff --git a/src/libunixonacid/opengetlnclose.c b/src/libunixonacid/opengetlnclose.c index 8c6e076..4840713 100644 --- a/src/libunixonacid/opengetlnclose.c +++ b/src/libunixonacid/opengetlnclose.c @@ -11,7 +11,7 @@ int opengetlnclose (char const *fn, stralloc *sa, int sep) char buf[BUFFER_INSIZE] ; buffer b ; int r ; - int fd = open_readb(fn) ; + int fd = openc_readb(fn) ; if (fd < 0) return -1 ; buffer_init(&b, &fd_readv, fd, buf, BUFFER_INSIZE) ; r = skagetln(&b, sa, sep) ; diff --git a/src/libunixonacid/opengetlnclose_at.c b/src/libunixonacid/opengetlnclose_at.c index cc2f86a..ff500e7 100644 --- a/src/libunixonacid/opengetlnclose_at.c +++ b/src/libunixonacid/opengetlnclose_at.c @@ -11,7 +11,7 @@ int opengetlnclose_at (int dirfd, char const *fn, stralloc *sa, int sep) char buf[BUFFER_INSIZE] ; buffer b ; int r ; - int fd = open_readatb(dirfd, fn) ; + int fd = openc_readatb(dirfd, fn) ; if (fd < 0) return -1 ; buffer_init(&b, &buffer_read, fd, buf, BUFFER_INSIZE) ; r = skagetln(&b, sa, sep) ; diff --git a/src/libunixonacid/openreadnclose_at.c b/src/libunixonacid/openreadnclose_at.c index e03c04b..8100997 100644 --- a/src/libunixonacid/openreadnclose_at.c +++ b/src/libunixonacid/openreadnclose_at.c @@ -1,6 +1,7 @@ /* ISC license. */ #include + #include #include #include @@ -9,7 +10,7 @@ size_t openreadnclose_at (int dirfd, char const *file, char *s, size_t n) { size_t r ; int e ; - int fd = open_readatb(dirfd, file) ; + int fd = openc_readatb(dirfd, file) ; if (fd < 0) return 0 ; r = allread(fd, s, n) ; e = errno ; diff --git a/src/libunixonacid/openslurpclose_at.c b/src/libunixonacid/openslurpclose_at.c index bf6d3ab..4ab6fa5 100644 --- a/src/libunixonacid/openslurpclose_at.c +++ b/src/libunixonacid/openslurpclose_at.c @@ -5,7 +5,7 @@ int openslurpclose_at (int dirfd, char const *fn, stralloc *sa) { - int fd = open_readatb(dirfd, fn) ; + int fd = openc_readatb(dirfd, fn) ; if (fd < 0) return 0 ; if (!slurp(sa, fd)) { diff --git a/src/libunixonacid/openwritenclose_at.c b/src/libunixonacid/openwritenclose_at.c index 015c0c8..c901952 100644 --- a/src/libunixonacid/openwritenclose_at.c +++ b/src/libunixonacid/openwritenclose_at.c @@ -1,6 +1,7 @@ /* ISC license. */ #include + #include #include #include @@ -8,7 +9,7 @@ size_t openwritenclose_at (int dirfd, char const *file, char const *s, size_t n) { size_t r ; - int fd = open_truncatb(dirfd, file) ; + int fd = openc_truncatb(dirfd, file) ; if (fd < 0) return 0 ; r = allwrite(fd, s, n) ; if (r >= n) fsync(fd) ; diff --git a/src/libunixonacid/openwritevnclose_at.c b/src/libunixonacid/openwritevnclose_at.c index ba1e789..5fcdc0b 100644 --- a/src/libunixonacid/openwritevnclose_at.c +++ b/src/libunixonacid/openwritevnclose_at.c @@ -2,6 +2,7 @@ #include #include + #include #include #include @@ -10,7 +11,7 @@ size_t openwritevnclose_at (int dirfd, char const *file, struct iovec const *v, unsigned int n) { size_t r ; - int fd = open_truncatb(dirfd, file) ; + int fd = openc_truncatb(dirfd, file) ; if (fd < 0) return 0 ; r = allwritev(fd, v, n) ; if (r >= siovec_len(v, n)) fsync(fd) ; -- cgit v1.2.3