lib: Add iov_advance
authorVolker Lendecke <vl@samba.org>
Sat, 27 Dec 2014 13:16:20 +0000 (13:16 +0000)
committerJeremy Allison <jra@samba.org>
Mon, 29 Dec 2014 23:25:08 +0000 (00:25 +0100)
This chops off n bytes from an iovec array. Used for short writev's

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/lib/iov_buf.c
source3/lib/iov_buf.h

index e05dfc9524895bd537cfdf004dce5cda599019f9..f0e05a64dad67fe58b3703d71fc61a5f36cf2ba4 100644 (file)
@@ -53,3 +53,36 @@ ssize_t iov_buf(const struct iovec *iov, int iovcnt,
 
        return needed;
 }
+
+bool iov_advance(struct iovec **iov, int *iovcnt, size_t n)
+{
+       struct iovec *v = *iov;
+       int cnt = *iovcnt;
+
+       while (n > 0) {
+               if (cnt == 0) {
+                       return false;
+               }
+               if (n < v->iov_len) {
+                       v->iov_base = (char *)v->iov_base + n;
+                       v->iov_len -= n;
+                       break;
+               }
+               n -= v->iov_len;
+               v += 1;
+               cnt -= 1;
+       }
+
+       /*
+        * Skip 0-length iovec's
+        */
+
+       while ((cnt > 0) && (v->iov_len == 0)) {
+               v += 1;
+               cnt -= 1;
+       }
+
+       *iov = v;
+       *iovcnt = cnt;
+       return true;
+}
index 397e906eb47044d0e05d8c29b5c6e3bc8f73521a..8f0ca266da9439740d6eab537fde6d2c4396eec3 100644 (file)
 
 #include <unistd.h>
 #include <stdint.h>
+#include <stdbool.h>
 
 ssize_t iov_buflen(const struct iovec *iov, int iovlen);
 ssize_t iov_buf(const struct iovec *iov, int iovcnt,
                uint8_t *buf, size_t buflen);
+bool iov_advance(struct iovec **iov, int *iovcnt, size_t n);
 
 #endif