diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2023-06-09 13:32:27 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2023-06-09 13:32:27 +0000 |
commit | c005b541bf9d54327ff860aad88367b854bae3d8 (patch) | |
tree | 6af88b1dd6637084b9e20e0c62b764656b9deaf4 /src/libstddjb | |
parent | 4af2899ac7ed0da49db7a6e94914fee391212f92 (diff) | |
download | skalibs-c005b541bf9d54327ff860aad88367b854bae3d8.tar.xz |
Add devino, refactor openwrite(v)nclose
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libstddjb')
23 files changed, 258 insertions, 168 deletions
diff --git a/src/libstddjb/devino_cmp.c b/src/libstddjb/devino_cmp.c new file mode 100644 index 0000000..567ff6d --- /dev/null +++ b/src/libstddjb/devino_cmp.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include <skalibs/devino.h> + +int devino_cmp (void const *a, void const *b) +{ + devino const *aa = a ; + devino const *bb = b ; + return aa->dev < bb->dev ? -1 : + aa->dev > bb->dev ? 1 : + aa->ino < bb->ino ? -1 : + aa->ino > bb->ino ; +} diff --git a/src/libstddjb/openwritenclose.c b/src/libstddjb/openwritenclose5.c index 427e53e..eef625c 100644 --- a/src/libstddjb/openwritenclose.c +++ b/src/libstddjb/openwritenclose5.c @@ -1,38 +1,31 @@ /* ISC license. */ #include <string.h> -#include <errno.h> -#include <unistd.h> #include <stdio.h> #include <stdlib.h> + +#include <skalibs/posixplz.h> +#include <skalibs/devino.h> #include <skalibs/djbunix.h> #define SUFFIX ":skalibs-openwritenclose:XXXXXX" -int openwritenclose_internal (char const *fn, char const *s, size_t n, dev_t *dev, ino_t *ino, int dosync) +int openwritenclose5 (char const *fn, char const *s, size_t n, devino *devino, unsigned int options) { - dev_t tmpdev ; - ino_t tmpino ; - size_t fnlen = strlen(fn) ; int fd ; + size_t fnlen = strlen(fn) ; char tmp[fnlen + sizeof(SUFFIX)] ; memcpy(tmp, fn, fnlen) ; memcpy(tmp + fnlen, SUFFIX, sizeof(SUFFIX)) ; fd = mkstemp(tmp) ; if (fd < 0) return 0 ; - if (!writenclose_unsafe_internal(fd, s, n, dev ? &tmpdev : 0, ino ? &tmpino : 0, dosync)) goto failclose ; + if (!writenclose_unsafe5(fd, s, n, devino, options)) goto failclose ; if (rename(tmp, fn) < 0) goto fail ; - if (dev) *dev = tmpdev ; - if (ino) *ino = tmpino ; return 1 ; failclose: fd_close(fd) ; fail: - { - int e = errno ; - unlink(tmp) ; - errno = e ; - } + unlink_void(tmp) ; return 0 ; } diff --git a/src/libstddjb/openwritenclose_internal_deprecated.c b/src/libstddjb/openwritenclose_internal_deprecated.c new file mode 100644 index 0000000..89f8dd2 --- /dev/null +++ b/src/libstddjb/openwritenclose_internal_deprecated.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include <skalibs/devino.h> +#include <skalibs/djbunix.h> + +int openwritenclose_internal (char const *fn, char const *s, size_t n, dev_t *dev, ino_t *ino, int dosync) +{ + devino di ; + if (!openwritenclose5(fn, s, n, dev || ino ? &di : 0, !!dosync)) return 0 ; + if (dev) *dev = di.dev ; + if (ino) *ino = di.ino ; + return 1 ; +} diff --git a/src/libstddjb/openwritenclose_suffix.c b/src/libstddjb/openwritenclose_suffix.c deleted file mode 100644 index 279c19f..0000000 --- a/src/libstddjb/openwritenclose_suffix.c +++ /dev/null @@ -1,29 +0,0 @@ -/* ISC license. */ - -#include <string.h> -#include <errno.h> -#include <unistd.h> -#include <stdio.h> -#include <skalibs/djbunix.h> - -int openwritenclose_suffix_internal (char const *fn, char const *s, size_t n, dev_t *dev, ino_t *ino, int dosync, char const *suffix) -{ - dev_t tmpdev ; - ino_t tmpino ; - size_t len = strlen(fn) ; - size_t suffixlen = strlen(suffix) ; - char tmp[len + suffixlen + 1] ; - memcpy(tmp, fn, len) ; - memcpy(tmp + len, suffix, suffixlen + 1) ; - if (!openwritenclose_unsafe_internal(tmp, s, n, dev ? &tmpdev : 0, ino ? &tmpino : 0, dosync)) return 0 ; - if (rename(tmp, fn) < 0) - { - int e = errno ; - unlink(tmp) ; - errno = e ; - return 0 ; - } - if (dev) *dev = tmpdev ; - if (ino) *ino = tmpino ; - return 1 ; -} diff --git a/src/libstddjb/openwritenclose_suffix6.c b/src/libstddjb/openwritenclose_suffix6.c new file mode 100644 index 0000000..be92a87 --- /dev/null +++ b/src/libstddjb/openwritenclose_suffix6.c @@ -0,0 +1,23 @@ +/* ISC license. */ + +#include <string.h> +#include <stdio.h> + +#include <skalibs/posixplz.h> +#include <skalibs/djbunix.h> + +int openwritenclose_suffix6 (char const *fn, char const *s, size_t n, devino *devino, unsigned int options, char const *suffix) +{ + size_t len = strlen(fn) ; + size_t suffixlen = strlen(suffix) ; + char tmp[len + suffixlen + 1] ; + memcpy(tmp, fn, len) ; + memcpy(tmp + len, suffix, suffixlen + 1) ; + if (!openwritenclose_unsafe5(tmp, s, n, devino, options)) return 0 ; + if (rename(tmp, fn) < 0) + { + unlink_void(tmp) ; + return 0 ; + } + return 1 ; +} diff --git a/src/libstddjb/openwritenclose_suffix_internal_deprecated.c b/src/libstddjb/openwritenclose_suffix_internal_deprecated.c new file mode 100644 index 0000000..95c1fdf --- /dev/null +++ b/src/libstddjb/openwritenclose_suffix_internal_deprecated.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include <skalibs/devino.h> +#include <skalibs/djbunix.h> + +int openwritenclose_suffix_internal (char const *fn, char const *s, size_t n, dev_t *dev, ino_t *ino, int dosync, char const *suffix) +{ + devino di ; + if (!openwritenclose_suffix6(fn, s, n, dev || ino ? &di : 0, !!dosync, suffix)) return 0 ; + if (dev) *dev = di.dev ; + if (ino) *ino = di.ino ; + return 1 ; +} diff --git a/src/libstddjb/openwritenclose_unsafe.c b/src/libstddjb/openwritenclose_unsafe.c deleted file mode 100644 index 8022f74..0000000 --- a/src/libstddjb/openwritenclose_unsafe.c +++ /dev/null @@ -1,20 +0,0 @@ -/* ISC license. */ - -#include <errno.h> -#include <unistd.h> -#include <skalibs/djbunix.h> - -int openwritenclose_unsafe_internal (char const *fn, char const *s, size_t len, dev_t *dev, ino_t *ino, int dosync) -{ - int fd = openc_trunc(fn) ; - if (fd < 0) return 0 ; - if (!writenclose_unsafe_internal(fd, s, len, dev, ino, dosync)) - { - int e = errno ; - fd_close(fd) ; - unlink(fn) ; - errno = e ; - return 0 ; - } - return 1 ; -} diff --git a/src/libstddjb/openwritenclose_unsafe5.c b/src/libstddjb/openwritenclose_unsafe5.c new file mode 100644 index 0000000..45a6b14 --- /dev/null +++ b/src/libstddjb/openwritenclose_unsafe5.c @@ -0,0 +1,20 @@ +/* ISC license. */ + +#include <errno.h> +#include <unistd.h> + +#include <skalibs/posixplz.h> +#include <skalibs/djbunix.h> + +int openwritenclose_unsafe5 (char const *fn, char const *s, size_t len, devino *devino, unsigned int options) +{ + int fd = openc_trunc(fn) ; + if (fd < 0) return 0 ; + if (!writenclose_unsafe5(fd, s, len, devino, options)) + { + fd_close(fd) ; + unlink_void(fn) ; + return 0 ; + } + return 1 ; +} diff --git a/src/libstddjb/openwritenclose_unsafe_internal_deprecated.c b/src/libstddjb/openwritenclose_unsafe_internal_deprecated.c new file mode 100644 index 0000000..58f88e6 --- /dev/null +++ b/src/libstddjb/openwritenclose_unsafe_internal_deprecated.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include <skalibs/devino.h> +#include <skalibs/djbunix.h> + +int openwritenclose_unsafe_internal (char const *fn, char const *s, size_t len, dev_t *dev, ino_t *ino, int dosync) +{ + devino di ; + if (!openwritenclose_unsafe5(fn, s, len, dev || ino ? &di : 0, !!dosync)) return 0 ; + if (dev) *dev = di.dev ; + if (ino) *ino = di.ino ; + return 1 ; +} diff --git a/src/libstddjb/openwritevnclose.c b/src/libstddjb/openwritevnclose5.c index 01df3e3..6465c4d 100644 --- a/src/libstddjb/openwritevnclose.c +++ b/src/libstddjb/openwritevnclose5.c @@ -1,39 +1,30 @@ /* ISC license. */ -#include <sys/uio.h> #include <string.h> -#include <errno.h> -#include <unistd.h> #include <stdio.h> #include <stdlib.h> + +#include <skalibs/posixplz.h> #include <skalibs/djbunix.h> #define SUFFIX ":skalibs-openwritevnclose:XXXXXX" -int openwritevnclose_internal (char const *fn, struct iovec const *v, unsigned int vlen, dev_t *dev, ino_t *ino, int dosync) +int openwritevnclose5 (char const *fn, struct iovec const *v, unsigned int vlen, devino *devino, unsigned int options) { - dev_t tmpdev ; - ino_t tmpino ; - size_t fnlen = strlen(fn) ; int fd ; + size_t fnlen = strlen(fn) ; char tmp[fnlen + sizeof(SUFFIX)] ; memcpy(tmp, fn, fnlen) ; memcpy(tmp + fnlen, SUFFIX, sizeof(SUFFIX)) ; fd = mkstemp(tmp) ; if (fd < 0) return 0 ; - if (!writevnclose_unsafe_internal(fd, v, vlen, dev ? &tmpdev : 0, ino ? &tmpino : 0, dosync)) goto failclose ; + if (!writevnclose_unsafe5(fd, v, vlen, devino, options)) goto failclose ; if (rename(tmp, fn) < 0) goto fail ; - if (dev) *dev = tmpdev ; - if (ino) *ino = tmpino ; return 1 ; failclose: fd_close(fd) ; fail: - { - int e = errno ; - unlink(tmp) ; - errno = e ; - } + unlink_void(tmp) ; return 0 ; } diff --git a/src/libstddjb/openwritevnclose_internal_deprecated.c b/src/libstddjb/openwritevnclose_internal_deprecated.c new file mode 100644 index 0000000..1e0bfd1 --- /dev/null +++ b/src/libstddjb/openwritevnclose_internal_deprecated.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include <skalibs/devino.h> +#include <skalibs/djbunix.h> + +int openwritevnclose_internal (char const *fn, struct iovec const *v, unsigned int vlen, dev_t *dev, ino_t *ino, int dosync) +{ + devino di ; + if (!openwritevnclose5(fn, v, vlen, dev || ino ? &di : 0, !!dosync)) return 0 ; + if (dev) *dev = di.dev ; + if (ino) *ino = di.ino ; + return 1 ; +} diff --git a/src/libstddjb/openwritevnclose_suffix.c b/src/libstddjb/openwritevnclose_suffix.c deleted file mode 100644 index 2085fd3..0000000 --- a/src/libstddjb/openwritevnclose_suffix.c +++ /dev/null @@ -1,30 +0,0 @@ -/* ISC license. */ - -#include <sys/uio.h> -#include <string.h> -#include <errno.h> -#include <unistd.h> -#include <stdio.h> -#include <skalibs/djbunix.h> - -int openwritevnclose_suffix_internal (char const *fn, struct iovec const *v, unsigned int n, dev_t *dev, ino_t *ino, int dosync, char const *suffix) -{ - dev_t tmpdev ; - ino_t tmpino ; - size_t len = strlen(fn) ; - size_t suffixlen = strlen(suffix) ; - char tmp[len + suffixlen + 1] ; - memcpy(tmp, fn, len) ; - memcpy(tmp + len, suffix, suffixlen + 1) ; - if (!openwritevnclose_unsafe_internal(tmp, v, n, dev ? &tmpdev : 0, ino ? &tmpino : 0, dosync)) return 0 ; - if (rename(tmp, fn) < 0) - { - int e = errno ; - unlink(tmp) ; - errno = e ; - return 0 ; - } - if (dev) *dev = tmpdev ; - if (ino) *ino = tmpino ; - return 1 ; -} diff --git a/src/libstddjb/openwritevnclose_suffix6.c b/src/libstddjb/openwritevnclose_suffix6.c new file mode 100644 index 0000000..3a9af3d --- /dev/null +++ b/src/libstddjb/openwritevnclose_suffix6.c @@ -0,0 +1,23 @@ +/* ISC license. */ + +#include <string.h> +#include <stdio.h> + +#include <skalibs/posixplz.h> +#include <skalibs/djbunix.h> + +int openwritevnclose_suffix6 (char const *fn, struct iovec const *v, unsigned int n, devino *devino, unsigned int options, char const *suffix) +{ + size_t len = strlen(fn) ; + size_t suffixlen = strlen(suffix) ; + char tmp[len + suffixlen + 1] ; + memcpy(tmp, fn, len) ; + memcpy(tmp + len, suffix, suffixlen + 1) ; + if (!openwritevnclose_unsafe5(tmp, v, n, devino, options)) return 0 ; + if (rename(tmp, fn) < 0) + { + unlink_void(tmp) ; + return 0 ; + } + return 1 ; +} diff --git a/src/libstddjb/openwritevnclose_suffix_internal_deprecated.c b/src/libstddjb/openwritevnclose_suffix_internal_deprecated.c new file mode 100644 index 0000000..47278e5 --- /dev/null +++ b/src/libstddjb/openwritevnclose_suffix_internal_deprecated.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include <skalibs/devino.h> +#include <skalibs/djbunix.h> + +int openwritevnclose_suffix_internal (char const *fn, struct iovec const *v, unsigned int n, dev_t *dev, ino_t *ino, int dosync, char const *suffix) +{ + devino di ; + if (!openwritevnclose_suffix6(fn, v, n, dev || ino ? &di : 0, !!dosync, suffix)) return 0 ; + if (dev) *dev = di.dev ; + if (ino) *ino = di.ino ; + return 1 ; +} diff --git a/src/libstddjb/openwritevnclose_unsafe.c b/src/libstddjb/openwritevnclose_unsafe.c deleted file mode 100644 index ca98f1c..0000000 --- a/src/libstddjb/openwritevnclose_unsafe.c +++ /dev/null @@ -1,22 +0,0 @@ -/* ISC license. */ - -#include <sys/uio.h> -#include <errno.h> -#include <unistd.h> -#include <skalibs/allreadwrite.h> -#include <skalibs/djbunix.h> - -int openwritevnclose_unsafe_internal (char const *fn, struct iovec const *v, unsigned int vlen, dev_t *dev, ino_t *ino, int dosync) -{ - int fd = openc_trunc(fn) ; - if (fd < 0) return 0 ; - if (!writevnclose_unsafe_internal(fd, v, vlen, dev, ino, dosync)) - { - int e = errno ; - fd_close(fd) ; - unlink(fn) ; - errno = e ; - return 0 ; - } - return 1 ; -} diff --git a/src/libstddjb/openwritevnclose_unsafe5.c b/src/libstddjb/openwritevnclose_unsafe5.c new file mode 100644 index 0000000..db82f72 --- /dev/null +++ b/src/libstddjb/openwritevnclose_unsafe5.c @@ -0,0 +1,17 @@ +/* ISC license. */ + +#include <skalibs/posixplz.h> +#include <skalibs/djbunix.h> + +int openwritevnclose_unsafe5 (char const *fn, struct iovec const *v, unsigned int vlen, devino *devino, unsigned int options) +{ + int fd = openc_trunc(fn) ; + if (fd < 0) return 0 ; + if (!writevnclose_unsafe5(fd, v, vlen, devino, options)) + { + fd_close(fd) ; + unlink_void(fn) ; + return 0 ; + } + return 1 ; +} diff --git a/src/libstddjb/openwritevnclose_unsafe_internal_deprecated.c b/src/libstddjb/openwritevnclose_unsafe_internal_deprecated.c new file mode 100644 index 0000000..b81881d --- /dev/null +++ b/src/libstddjb/openwritevnclose_unsafe_internal_deprecated.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include <skalibs/devino.h> +#include <skalibs/djbunix.h> + +int openwritevnclose_unsafe_internal (char const *fn, struct iovec const *v, unsigned int vlen, dev_t *dev, ino_t *ino, int dosync) +{ + devino di ; + if (!openwritevnclose_unsafe5(fn, v, vlen, dev || ino ? &di : 0, !!dosync)) return 0 ; + if (dev) *dev = di.dev ; + if (ino) *ino = di.ino ; + return 1 ; +} diff --git a/src/libstddjb/writenclose_unsafe.c b/src/libstddjb/writenclose_unsafe.c deleted file mode 100644 index f71caea..0000000 --- a/src/libstddjb/writenclose_unsafe.c +++ /dev/null @@ -1,18 +0,0 @@ -/* ISC license. */ - -#include <sys/stat.h> -#include <errno.h> -#include <skalibs/allreadwrite.h> -#include <skalibs/djbunix.h> - -int writenclose_unsafe_internal (int fd, char const *s, size_t len, dev_t *dev, ino_t *ino, int dosync) -{ - struct stat st ; - if (allwrite(fd, s, len) < len) return 0 ; - if ((dev || ino) && (fstat(fd, &st) < 0)) return 0 ; - if (dosync && (fd_sync(fd) < 0) && (errno != EINVAL)) return 0 ; - fd_close(fd) ; - if (dev) *dev = st.st_dev ; - if (ino) *ino = st.st_ino ; - return 1 ; -} diff --git a/src/libstddjb/writenclose_unsafe5.c b/src/libstddjb/writenclose_unsafe5.c new file mode 100644 index 0000000..b0cbe48 --- /dev/null +++ b/src/libstddjb/writenclose_unsafe5.c @@ -0,0 +1,22 @@ +/* ISC license. */ + +#include <sys/stat.h> +#include <errno.h> + +#include <skalibs/allreadwrite.h> +#include <skalibs/djbunix.h> + +int writenclose_unsafe5 (int fd, char const *s, size_t len, devino *devino, unsigned int options) +{ + if (allwrite(fd, s, len) < len) return 0 ; + if (options & 1 && fd_sync(fd) == -1 && errno != EINVAL) return 0 ; + if (devino) + { + struct stat st ; + if (fstat(fd, &st) == -1) return 0 ; + devino->dev = st.st_dev ; + devino->ino = st.st_ino ; + } + fd_close(fd) ; + return 1 ; +} diff --git a/src/libstddjb/writenclose_unsafe_internal_deprecated.c b/src/libstddjb/writenclose_unsafe_internal_deprecated.c new file mode 100644 index 0000000..d5ba77c --- /dev/null +++ b/src/libstddjb/writenclose_unsafe_internal_deprecated.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include <skalibs/devino.h> +#include <skalibs/djbunix.h> + +int writenclose_unsafe_internal (int fd, char const *s, size_t len, dev_t *dev, ino_t *ino, int dosync) +{ + devino di ; + if (!writenclose_unsafe5(fd, s, len, dev || ino ? &di : 0, !!dosync)) return 0 ; + if (dev) *dev = di.dev ; + if (ino) *ino = di.ino ; + return 1 ; +} diff --git a/src/libstddjb/writevnclose_unsafe.c b/src/libstddjb/writevnclose_unsafe.c deleted file mode 100644 index 9d2348a..0000000 --- a/src/libstddjb/writevnclose_unsafe.c +++ /dev/null @@ -1,20 +0,0 @@ -/* ISC license. */ - -#include <sys/uio.h> -#include <sys/stat.h> -#include <errno.h> -#include <skalibs/allreadwrite.h> -#include <skalibs/siovec.h> -#include <skalibs/djbunix.h> - -int writevnclose_unsafe_internal (int fd, struct iovec const *v, unsigned int vlen, dev_t *dev, ino_t *ino, int dosync) -{ - struct stat st ; - if (allwritev(fd, v, vlen) < siovec_len(v, vlen)) return 0 ; - if ((dev || ino) && (fstat(fd, &st) < 0)) return 0 ; - if (dosync && (fd_sync(fd) < 0) && (errno != EINVAL)) return 0 ; - fd_close(fd) ; - if (dev) *dev = st.st_dev ; - if (ino) *ino = st.st_ino ; - return 1 ; -} diff --git a/src/libstddjb/writevnclose_unsafe5.c b/src/libstddjb/writevnclose_unsafe5.c new file mode 100644 index 0000000..49b1444 --- /dev/null +++ b/src/libstddjb/writevnclose_unsafe5.c @@ -0,0 +1,23 @@ +/* ISC license. */ + +#include <sys/stat.h> +#include <errno.h> + +#include <skalibs/allreadwrite.h> +#include <skalibs/siovec.h> +#include <skalibs/djbunix.h> + +int writevnclose_unsafe5 (int fd, struct iovec const *v, unsigned int vlen, devino *devino, unsigned int options) +{ + if (allwritev(fd, v, vlen) < siovec_len(v, vlen)) return 0 ; + if (options & 1 && fd_sync(fd) == -1 && errno != EINVAL) return 0 ; + if (devino) + { + struct stat st ; + if (fstat(fd, &st) == -1) return 0 ; + devino->dev = st.st_dev ; + devino->ino = st.st_ino ; + } + fd_close(fd) ; + return 1 ; +} diff --git a/src/libstddjb/writevnclose_unsafe_internal_deprecated.c b/src/libstddjb/writevnclose_unsafe_internal_deprecated.c new file mode 100644 index 0000000..d966500 --- /dev/null +++ b/src/libstddjb/writevnclose_unsafe_internal_deprecated.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include <skalibs/devino.h> +#include <skalibs/djbunix.h> + +int writevnclose_unsafe_internal (int fd, struct iovec const *v, unsigned int vlen, dev_t *dev, ino_t *ino, int dosync) +{ + devino di ; + if (!writevnclose_unsafe5(fd, v, vlen, dev || ino ? &di : 0, !!dosync)) return 0 ; + if (dev) *dev = di.dev ; + if (ino) *ino = di.ino ; + return 1 ; +} |