lib: Use iov_advance in writev_handler
authorVolker Lendecke <vl@samba.org>
Sat, 27 Dec 2014 16:39:08 +0000 (16:39 +0000)
committerJeremy Allison <jra@samba.org>
Mon, 29 Dec 2014 23:25:08 +0000 (00:25 +0100)
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/async_req/async_sock.c
lib/async_req/wscript_build

index 74b2cb7baa803223ff14d8c01cf54c6c481b0d03..b986e45b0aa70d66b4b52529cbac61ff0c71d040 100644 (file)
@@ -27,6 +27,7 @@
 #include <talloc.h>
 #include <tevent.h>
 #include "lib/async_req/async_sock.h"
+#include "lib/iov_buf.h"
 
 /* Note: lib/util/ is currently GPL */
 #include "lib/util/tevent_unix.h"
@@ -470,10 +471,8 @@ static void writev_handler(struct tevent_context *ev, struct tevent_fd *fde,
                private_data, struct tevent_req);
        struct writev_state *state =
                tevent_req_data(req, struct writev_state);
-       size_t to_write, written;
-       int i;
-
-       to_write = 0;
+       size_t written;
+       bool ok;
 
        if ((state->flags & TEVENT_FD_READ) && (flags & TEVENT_FD_READ)) {
                int ret, value;
@@ -507,10 +506,6 @@ static void writev_handler(struct tevent_context *ev, struct tevent_fd *fde,
                }
        }
 
-       for (i=0; i<state->count; i++) {
-               to_write += state->iov[i].iov_len;
-       }
-
        written = writev(state->fd, state->iov, state->count);
        if ((written == -1) && (errno == EINTR)) {
                /* retry */
@@ -526,26 +521,15 @@ static void writev_handler(struct tevent_context *ev, struct tevent_fd *fde,
        }
        state->total_size += written;
 
-       if (written == to_write) {
-               tevent_req_done(req);
+       ok = iov_advance(&state->iov, &state->count, written);
+       if (!ok) {
+               tevent_req_error(req, EIO);
                return;
        }
 
-       /*
-        * We've written less than we were asked to, drop stuff from
-        * state->iov.
-        */
-
-       while (written > 0) {
-               if (written < state->iov[0].iov_len) {
-                       state->iov[0].iov_base =
-                               (char *)state->iov[0].iov_base + written;
-                       state->iov[0].iov_len -= written;
-                       break;
-               }
-               written -= state->iov[0].iov_len;
-               state->iov += 1;
-               state->count -= 1;
+       if (state->count == 0) {
+               tevent_req_done(req);
+               return;
        }
 }
 
index 7802935f682cf20a6c5205b4662988d84f8d61bb..e8af569b4f9d619e3ff00faa3d6f881a67801382 100644 (file)
@@ -3,7 +3,7 @@
 
 bld.SAMBA_SUBSYSTEM('LIBASYNC_REQ',
        source='async_sock.c',
-       public_deps='talloc tevent',
+       public_deps='talloc tevent iov_buf',
        deps='tevent-util socket-blocking'
        )