diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2021-09-25 19:48:26 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2021-09-25 19:48:26 +0000 |
commit | f268a8871a061395633d943e6f177fa8f8008963 (patch) | |
tree | c01e2ddf63e4934ced4eb34764b520bd47070e08 /src/libstddjb | |
parent | 754da814c32de5fbbd8b065e9f106cbb7bfc8ae6 (diff) | |
download | skalibs-f268a8871a061395633d943e6f177fa8f8008963.tar.xz |
Axe iobuffer
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libstddjb')
-rw-r--r-- | src/libstddjb/fd_cat.c | 26 | ||||
-rw-r--r-- | src/libstddjb/fd_catn.c | 39 | ||||
-rw-r--r-- | src/libstddjb/iobuffer_fill.c | 14 | ||||
-rw-r--r-- | src/libstddjb/iobuffer_flush.c | 14 | ||||
-rw-r--r-- | src/libstddjb/iobuffer_init.c | 17 | ||||
-rw-r--r-- | src/libstddjb/iobuffer_kfromu.c | 38 | ||||
-rw-r--r-- | src/libstddjb/iobuffer_salvage.c | 19 | ||||
-rw-r--r-- | src/libstddjb/iobuffer_ufromk.c | 40 | ||||
-rw-r--r-- | src/libstddjb/iobufferk_fill.c | 56 | ||||
-rw-r--r-- | src/libstddjb/iobufferk_finish.c | 45 | ||||
-rw-r--r-- | src/libstddjb/iobufferk_flush.c | 67 | ||||
-rw-r--r-- | src/libstddjb/iobufferk_init.c | 74 | ||||
-rw-r--r-- | src/libstddjb/iobufferk_isworking.c | 47 | ||||
-rw-r--r-- | src/libstddjb/iobufferu_fill.c | 11 | ||||
-rw-r--r-- | src/libstddjb/iobufferu_finish.c | 9 | ||||
-rw-r--r-- | src/libstddjb/iobufferu_flush.c | 11 | ||||
-rw-r--r-- | src/libstddjb/iobufferu_init.c | 16 |
17 files changed, 25 insertions, 518 deletions
diff --git a/src/libstddjb/fd_cat.c b/src/libstddjb/fd_cat.c index 4acc08a..d9d5c99 100644 --- a/src/libstddjb/fd_cat.c +++ b/src/libstddjb/fd_cat.c @@ -1,27 +1,23 @@ /* ISC license. */ -#include <sys/types.h> +#include <unistd.h> -#include <skalibs/iobuffer.h> +#include <skalibs/allreadwrite.h> #include <skalibs/djbunix.h> -int fd_cat (int from, int to) +#define BSIZE 65536 + +off_t fd_cat (int from, int to) { - iobuffer b ; - size_t n = 0 ; - if (!iobuffer_init(&b, from, to)) return -1 ; + off_t n = 0 ; + char buf[BSIZE] ; for (;;) { - ssize_t r = iobuffer_fill(&b) ; - if (r < 0) goto err ; - else if (!r) break ; - if (!iobuffer_flush(&b)) goto err ; + ssize_t r = fd_read(from, buf, BSIZE) ; + if (r == -1) return -1 ; + if (!r) break ; + if (allwrite(to, buf, r) < r) return -1 ; n += r ; } - iobuffer_finish(&b) ; return n ; - - err: - iobuffer_finish(&b) ; - return -1 ; } diff --git a/src/libstddjb/fd_catn.c b/src/libstddjb/fd_catn.c index c2ddd7a..182a054 100644 --- a/src/libstddjb/fd_catn.c +++ b/src/libstddjb/fd_catn.c @@ -1,39 +1,28 @@ /* ISC license. */ -#include <sys/types.h> +#include <unistd.h> #include <errno.h> + #include <skalibs/allreadwrite.h> -#include <skalibs/iobuffer.h> #include <skalibs/djbunix.h> -size_t fd_catn (int from, int to, size_t n) +#define BSIZE 65536 + +off_t fd_catn (int from, int to, off_t n) { - size_t w = 0 ; - if (n >= IOBUFFER_SIZE) + off_t w = 0 ; + char buf[BSIZE] ; + while (n >= BSIZE) { - iobuffer b ; - if (!iobuffer_init(&b, from, to)) return 0 ; - while (n >= IOBUFFER_SIZE) - { - ssize_t r = iobuffer_fill(&b) ; - if (r <= 0) - { - iobuffer_finish(&b) ; - if (!r) errno = EPIPE ; - return w ; - } - if (!iobuffer_flush(&b)) - { - iobuffer_finish(&b) ; - return w ; - } - n -= r ; w += r ; - } - iobuffer_finish(&b) ; + ssize_t r = fd_read(from, buf, BSIZE) ; + if (r == -1) return w ; + if (!r) return (errno = EPIPE, w) ; + if (allwrite(to, buf, r) < r) return w ; + n -= r ; w += r ; } + if (n) { - char buf[n] ; size_t r = allread(from, buf, n) ; size_t v = 0 ; if (r) v = allwrite(to, buf, r) ; diff --git a/src/libstddjb/iobuffer_fill.c b/src/libstddjb/iobuffer_fill.c deleted file mode 100644 index b99e22d..0000000 --- a/src/libstddjb/iobuffer_fill.c +++ /dev/null @@ -1,14 +0,0 @@ -/* ISC license. */ - -#include <errno.h> -#include <skalibs/iobuffer.h> - -ssize_t iobuffer_fill (iobuffer_ref b) -{ - if (b->isk) - { - ssize_t r = iobufferk_fill(&b->x.k) ; - if (r >= 0 || errno != ENOSYS || !iobuffer_salvage(b)) return r ; - } - return iobufferu_fill(&b->x.u) ; -} diff --git a/src/libstddjb/iobuffer_flush.c b/src/libstddjb/iobuffer_flush.c deleted file mode 100644 index 93efc9d..0000000 --- a/src/libstddjb/iobuffer_flush.c +++ /dev/null @@ -1,14 +0,0 @@ -/* ISC license. */ - -#include <errno.h> -#include <skalibs/iobuffer.h> - -int iobuffer_flush (iobuffer *b) -{ - if (b->isk) - { - if (iobufferk_flush(&b->x.k)) return 1 ; - if (errno != ENOSYS || !iobuffer_salvage(b)) return 0 ; - } - return iobufferu_flush(&b->x.u) ; -} diff --git a/src/libstddjb/iobuffer_init.c b/src/libstddjb/iobuffer_init.c deleted file mode 100644 index fc699a0..0000000 --- a/src/libstddjb/iobuffer_init.c +++ /dev/null @@ -1,17 +0,0 @@ -/* ISC license. */ - -#include <skalibs/iobuffer.h> - -int iobuffer_init (iobuffer *b, int fdin, int fdout) -{ - if (!iobufferk_init(&b->x.k, fdin, fdout)) goto user ; - if (!iobufferk_isworking(&b->x.k)) goto fk ; - b->isk = 1 ; - return 1 ; - fk: - iobufferk_finish(&b->x.k) ; - user: - if (!iobufferu_init(&b->x.u, fdin, fdout)) return 0 ; - b->isk = 0 ; - return 1 ; -} diff --git a/src/libstddjb/iobuffer_kfromu.c b/src/libstddjb/iobuffer_kfromu.c deleted file mode 100644 index 91525a5..0000000 --- a/src/libstddjb/iobuffer_kfromu.c +++ /dev/null @@ -1,38 +0,0 @@ -/* ISC license. */ - -#include <skalibs/sysdeps.h> - -#ifdef SKALIBS_HASSPLICE - -#include <skalibs/nonposix.h> -#include <fcntl.h> -#include <sys/uio.h> -#include <skalibs/buffer.h> -#include <skalibs/iobuffer.h> - -int iobuffer_kfromu (iobufferk *k, iobufferu *u) -{ - struct iovec v[2] ; - ssize_t r ; - buffer_rpeek(&u->b[0], v) ; - r = vmsplice(k->p[1], v, 2, 0) ; - if (r < 0) return 0 ; - k->n += r ; - buffer_rseek(&u->b[0], r) ; - u->b[1].c.p = u->b[0].c.p ; - return iobufferu_isempty(u) ; -} - -#else - -#include <errno.h> -#include <skalibs/iobuffer.h> - -int iobuffer_kfromu (iobufferk *k, iobufferu *u) -{ - (void)k ; - (void)u ; - return (errno = ENOSYS, 0) ; -} - -#endif diff --git a/src/libstddjb/iobuffer_salvage.c b/src/libstddjb/iobuffer_salvage.c deleted file mode 100644 index 6f2c5d8..0000000 --- a/src/libstddjb/iobuffer_salvage.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ISC license. */ - -#include <skalibs/iobuffer.h> - -int iobuffer_salvage (iobuffer *b) -{ - iobufferu u ; - if (!b->isk) return 1 ; - if (!iobufferu_init(&u, b->x.k.fd[0], b->x.k.fd[1])) return 0 ; - if (!iobuffer_ufromk(&u, &b->x.k)) goto err ; - iobufferk_finish(&b->x.k) ; - b->x.u = u ; - b->isk = 0 ; - return 1 ; - -err: - iobufferu_finish(&u) ; - return 0 ; -} diff --git a/src/libstddjb/iobuffer_ufromk.c b/src/libstddjb/iobuffer_ufromk.c deleted file mode 100644 index 940818d..0000000 --- a/src/libstddjb/iobuffer_ufromk.c +++ /dev/null @@ -1,40 +0,0 @@ -/* ISC license. */ - -#include <skalibs/sysdeps.h> - -#ifdef SKALIBS_HASSPLICE - -#include <sys/types.h> -#include <skalibs/iobuffer.h> - -int iobuffer_ufromk (iobufferu *u, iobufferk *k) -{ - int in = u->b[0].fd ; - u->b[0].fd = k->p[0] ; - while (k->n) - { - ssize_t r = iobufferu_fill(u) ; - if (r <= 0) goto err ; - k->n -= r ; - } - u->b[0].fd = in ; - return 1 ; - - err: - u->b[0].fd = in ; - return 0 ; -} - -#else - -#include <errno.h> -#include <skalibs/iobuffer.h> - -int iobuffer_ufromk (iobufferu *u, iobufferk *k) -{ - (void)u ; - (void)k ; - return (errno = ENOSYS, 0) ; -} - -#endif diff --git a/src/libstddjb/iobufferk_fill.c b/src/libstddjb/iobufferk_fill.c deleted file mode 100644 index c24ceba..0000000 --- a/src/libstddjb/iobufferk_fill.c +++ /dev/null @@ -1,56 +0,0 @@ -/* ISC license. */ - -#include <skalibs/sysdeps.h> - -#ifdef SKALIBS_HASSPLICE - -#include <skalibs/nonposix.h> -#include <fcntl.h> -#include <errno.h> -#include <skalibs/iobuffer.h> - -static ssize_t iobufferk_tee (iobufferk *k) -{ - ssize_t r = tee(k->fd[0], k->fd[1], IOBUFFERK_SIZE - k->n, k->nb & 1 ? SPLICE_F_NONBLOCK : 0) ; - if (r > 0) k->n += r ; - return r ; -} - -static ssize_t iobufferk_splice (iobufferk *k) -{ - ssize_t r = splice(k->fd[0], 0, k->fd[1], 0, IOBUFFERK_SIZE, k->nb ? SPLICE_F_NONBLOCK : 0) ; - if (r > 0) k->n += r ; - if ((r < 0) && (errno == EINVAL)) errno = ENOSYS ; - return r ; -} - -static ssize_t iobufferk_fill_3 (iobufferk *k) -{ - ssize_t r = splice(k->fd[0], 0, k->p[1], 0, IOBUFFERK_SIZE - k->n, k->nb & 1 ? SPLICE_F_NONBLOCK : 0) ; - if (r > 0) k->n += r ; - if ((r < 0) && (errno == EINVAL)) errno = ENOSYS ; - return r ; -} - -iobufferk_input_func_ref const iobufferk_fill_f[4] = -{ - &iobufferk_tee, &iobufferk_splice, &iobufferk_splice, &iobufferk_fill_3 -} ; - -#else - -#include <errno.h> -#include <skalibs/iobuffer.h> - -static ssize_t iobufferk_inosys (iobufferk *k) -{ - (void)k ; - return (errno = ENOSYS, -1) ; -} - -iobufferk_input_func_ref const iobufferk_fill_f[4] = -{ - &iobufferk_inosys, &iobufferk_inosys, &iobufferk_inosys, &iobufferk_inosys -} ; - -#endif diff --git a/src/libstddjb/iobufferk_finish.c b/src/libstddjb/iobufferk_finish.c deleted file mode 100644 index 1640836..0000000 --- a/src/libstddjb/iobufferk_finish.c +++ /dev/null @@ -1,45 +0,0 @@ -/* ISC license. */ - -#include <skalibs/sysdeps.h> - -#ifdef SKALIBS_HASSPLICE - -#include <skalibs/djbunix.h> -#include <skalibs/iobuffer.h> - -static void iobufferk_nop (iobufferk *k) -{ - (void)k ; -} - -static void iobufferk_finish_0 (iobufferk *k) -{ - fd_close(k->p[1]) ; -} - -static void iobufferk_finish_3 (iobufferk *k) -{ - fd_close(k->p[1]) ; - fd_close(k->p[0]) ; -} - -iobufferk_finish_func_ref const iobufferk_finish_f[4] = -{ - &iobufferk_finish_0, &iobufferk_nop, &iobufferk_nop, &iobufferk_finish_3 -} ; - -#else - -#include <skalibs/iobuffer.h> - -static void iobufferk_nop (iobufferk *k) -{ - (void)k ; -} - -iobufferk_finish_func_ref const iobufferk_finish_f[4] = -{ - &iobufferk_nop, &iobufferk_nop, &iobufferk_nop, &iobufferk_nop -} ; - -#endif diff --git a/src/libstddjb/iobufferk_flush.c b/src/libstddjb/iobufferk_flush.c deleted file mode 100644 index 3f8d657..0000000 --- a/src/libstddjb/iobufferk_flush.c +++ /dev/null @@ -1,67 +0,0 @@ -/* ISC license. */ - -#include <skalibs/sysdeps.h> - -#ifdef SKALIBS_HASSPLICE - -#include <skalibs/nonposix.h> -#include <fcntl.h> -#include <errno.h> -#include <skalibs/iobuffer.h> - -static int iobufferk_flush_0 (iobufferk *k) -{ - while (k->n) - { - ssize_t r = splice(k->fd[0], 0, k->p[1], 0, k->n, 0) ; - if (r < 0) return 0 ; - else if (!r) break ; - k->n -= r ; - } - return 1 ; -} - -static int fakeflush (iobufferk *k) -{ - k->n = 0 ; - return 1 ; -} - -static int iobufferk_flush_3 (iobufferk *k) -{ - while (k->n) - { - ssize_t r = splice(k->p[0], 0, k->fd[1], 0, k->n, SPLICE_F_MORE | (k->nb & 2 ? SPLICE_F_NONBLOCK : 0)) ; - if (r < 0) - { - if (errno == EINVAL) errno = ENOSYS ; - return 0 ; - } - else if (!r) return (errno = EPIPE, 0) ; - k->n -= r ; - } - return 1 ; -} - -iobufferk_output_func_ref const iobufferk_flush_f[4] = -{ - &iobufferk_flush_0, &fakeflush, &fakeflush, &iobufferk_flush_3 -} ; - -#else - -#include <errno.h> -#include <skalibs/iobuffer.h> - -static int iobufferk_onosys (iobufferk *k) -{ - (void)k ; - return (errno = ENOSYS, -1) ; -} - -iobufferk_output_func_ref const iobufferk_flush_f[4] = -{ - &iobufferk_onosys, &iobufferk_onosys, &iobufferk_onosys, &iobufferk_onosys -} ; - -#endif diff --git a/src/libstddjb/iobufferk_init.c b/src/libstddjb/iobufferk_init.c deleted file mode 100644 index b7a91d7..0000000 --- a/src/libstddjb/iobufferk_init.c +++ /dev/null @@ -1,74 +0,0 @@ -/* ISC license. */ - -#include <skalibs/sysdeps.h> - -#ifdef SKALIBS_HASSPLICE - -#include <sys/stat.h> -#include <fcntl.h> - -#include <skalibs/djbunix.h> -#include <skalibs/iobuffer.h> - -static int iobufferk_init_0 (iobufferk *k) -{ - int fd = openc_write("/dev/null") ; - if (fd < 0) return 0 ; - k->p[0] = -1 ; - k->p[1] = fd ; - return 1 ; -} - -static int iobufferk_nofd (iobufferk *k) -{ - k->p[0] = k->p[1] = -1 ; - return 1 ; -} - -static int iobufferk_init_3 (iobufferk *k) -{ - return (pipecoe(k->p) >= 0) ; -} - -static iobufferk_output_func_ref iobufferk_init_f[4] = -{ - &iobufferk_init_0, &iobufferk_nofd, &iobufferk_nofd, &iobufferk_init_3 -} ; - -int iobufferk_init (iobufferk *k, int fdin, int fdout) -{ - iobufferk tmp ; - struct stat st ; - int r ; - if (fstat(fdin, &st) < 0) return 0 ; - r = fcntl(fdin, F_GETFL) ; - if (r < 0) return 0 ; - tmp.type = !S_ISFIFO(st.st_mode) ; - tmp.nb = !!(r & O_NONBLOCK) ; - if (fstat(fdout, &st) < 0) return 0 ; - r = fcntl(fdout, F_GETFL) ; - if (r < 0) return 0 ; - tmp.type |= (!S_ISFIFO(st.st_mode) << 1) ; - tmp.nb |= (r & O_NONBLOCK) ? 2 : 0 ; - tmp.fd[0] = fdin ; - tmp.fd[1] = fdout ; - tmp.n = 0 ; - if (!(*iobufferk_init_f[tmp.type])(&tmp)) return 0 ; - *k = tmp ; - return 1 ; -} - -#else - -#include <errno.h> -#include <skalibs/iobuffer.h> - -int iobufferk_init (iobufferk *k, int fdin, int fdout) -{ - (void)k ; - (void)fdin ; - (void)fdout ; - return (errno = ENOSYS, 0) ; -} - -#endif diff --git a/src/libstddjb/iobufferk_isworking.c b/src/libstddjb/iobufferk_isworking.c deleted file mode 100644 index 119d130..0000000 --- a/src/libstddjb/iobufferk_isworking.c +++ /dev/null @@ -1,47 +0,0 @@ -/* ISC license. */ - -#include <skalibs/sysdeps.h> - -#ifdef SKALIBS_HASSPLICE - -#include <skalibs/nonposix.h> -#include <errno.h> -#include <fcntl.h> -#include <skalibs/iobuffer.h> - -int iobufferk_isworking (iobufferk *k) -{ - /* for now splice() with a length of 0 returns 0 no matter what, so this */ - /* test is useless. splice() should test the underlying filesystems even */ - /* if the length is 0. */ - -# if 0 - - int e = errno ; - if (splice(k->fd[0], 0, k->p[1], 0, 0, 0) < 0) goto no ; - if (splice(k->p[0], 0, k->fd[1], 0, 0, 0) < 0) goto no ; - errno = e ; - return 1 ; - no: - errno = e ; - return 0 ; - -# else - - (void)k ; - return 1 ; - -# endif -} - -#else - -#include <skalibs/iobuffer.h> - -int iobufferk_isworking (iobufferk *k) -{ - (void)k ; - return 0 ; -} - -#endif diff --git a/src/libstddjb/iobufferu_fill.c b/src/libstddjb/iobufferu_fill.c deleted file mode 100644 index 7867444..0000000 --- a/src/libstddjb/iobufferu_fill.c +++ /dev/null @@ -1,11 +0,0 @@ -/* ISC license. */ - -#include <skalibs/buffer.h> -#include <skalibs/iobuffer.h> - -ssize_t iobufferu_fill (iobufferu *b) -{ - ssize_t r = buffer_fill(&b->b[0]) ; - b->b[1].c.n = b->b[0].c.n ; - return r ; -} diff --git a/src/libstddjb/iobufferu_finish.c b/src/libstddjb/iobufferu_finish.c deleted file mode 100644 index 612ec18..0000000 --- a/src/libstddjb/iobufferu_finish.c +++ /dev/null @@ -1,9 +0,0 @@ -/* ISC license. */ - -#include <skalibs/alloc.h> -#include <skalibs/iobuffer.h> - -void iobufferu_finish (iobufferu *b) -{ - alloc_free(b->buf) ; -} diff --git a/src/libstddjb/iobufferu_flush.c b/src/libstddjb/iobufferu_flush.c deleted file mode 100644 index ad0fd9e..0000000 --- a/src/libstddjb/iobufferu_flush.c +++ /dev/null @@ -1,11 +0,0 @@ -/* ISC license. */ - -#include <skalibs/buffer.h> -#include <skalibs/iobuffer.h> - -int iobufferu_flush (iobufferu *b) -{ - int r = buffer_flush(&b->b[1]) ; - b->b[0].c.p = b->b[1].c.p ; - return r ; -} diff --git a/src/libstddjb/iobufferu_init.c b/src/libstddjb/iobufferu_init.c deleted file mode 100644 index affd01b..0000000 --- a/src/libstddjb/iobufferu_init.c +++ /dev/null @@ -1,16 +0,0 @@ -/* ISC license. */ - -#include <skalibs/alloc.h> -#include <skalibs/allreadwrite.h> -#include <skalibs/buffer.h> -#include <skalibs/iobuffer.h> - -int iobufferu_init (iobufferu *b, int fdin, int fdout) -{ - char *x = alloc(IOBUFFERU_SIZE) ; - if (!x) return 0 ; - b->buf = x ; - buffer_init(&b->b[0], &fd_readv, fdin, x, IOBUFFERU_SIZE) ; - buffer_init(&b->b[1], &fd_writev, fdout, x, IOBUFFERU_SIZE) ; - return 1 ; -} |