Fix Red Hat bugzilla bug : https://bugzilla.redhat.com/show_bug.cgi?id=516165
[metze/samba/wip.git] / source3 / libsmb / clireadwrite.c
index bb1e2f66c27f11b3cf5ddef4699c290dac54095c..0d1f9e515ef778f5dd9670ffc3a5ee5f3b8ddfa5 100644 (file)
@@ -82,7 +82,7 @@ static void cli_read_andx_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
                                        struct event_context *ev,
-                                       struct cli_state *cli, int fnum,
+                                       struct cli_state *cli, uint16_t fnum,
                                        off_t offset, size_t size,
                                        struct tevent_req **psmbreq)
 {
@@ -135,17 +135,23 @@ struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
 
 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
                                      struct event_context *ev,
-                                     struct cli_state *cli, int fnum,
+                                     struct cli_state *cli, uint16_t fnum,
                                      off_t offset, size_t size)
 {
        struct tevent_req *req, *subreq;
+       NTSTATUS status;
 
        req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
                                   &subreq);
-       if ((req == NULL) || !cli_smb_req_send(subreq)) {
-               TALLOC_FREE(req);
+       if (req == NULL) {
                return NULL;
        }
+
+       status = cli_smb_req_send(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return tevent_req_post(req, ev);
+       }
        return req;
 }
 
@@ -194,7 +200,7 @@ static void cli_read_andx_done(struct tevent_req *subreq)
        state->buf = (uint8_t *)smb_base(inbuf) + SVAL(vwv+6, 0);
 
        if (trans_oob(smb_len(inbuf), SVAL(vwv+6, 0), state->received)
-           || (state->buf < bytes)) {
+           || (state->received && (state->buf < bytes))) {
                DEBUG(5, ("server returned invalid read&x data offset\n"));
                tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
                return;
@@ -239,7 +245,7 @@ struct cli_pull_subreq {
  */
 
 struct cli_pull_state {
-       struct async_req *req;
+       struct tevent_req *req;
 
        struct event_context *ev;
        struct cli_state *cli;
@@ -278,13 +284,13 @@ struct cli_pull_state {
        SMB_OFF_T pushed;
 };
 
-static char *cli_pull_print(TALLOC_CTX *mem_ctx, struct async_req *req)
+static char *cli_pull_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
 {
-       struct cli_pull_state *state = talloc_get_type_abort(
-               req->private_data, struct cli_pull_state);
+       struct cli_pull_state *state = tevent_req_data(
+               req, struct cli_pull_state);
        char *result;
 
-       result = async_req_print(mem_ctx, req);
+       result = tevent_req_print(mem_ctx, req);
        if (result == NULL) {
                return NULL;
        }
@@ -300,25 +306,25 @@ static void cli_pull_read_done(struct tevent_req *read_req);
  * Prepare an async pull request
  */
 
-struct async_req *cli_pull_send(TALLOC_CTX *mem_ctx,
-                               struct event_context *ev,
-                               struct cli_state *cli,
-                               uint16_t fnum, off_t start_offset,
-                               SMB_OFF_T size, size_t window_size,
-                               NTSTATUS (*sink)(char *buf, size_t n,
-                                                void *priv),
-                               void *priv)
+struct tevent_req *cli_pull_send(TALLOC_CTX *mem_ctx,
+                                struct event_context *ev,
+                                struct cli_state *cli,
+                                uint16_t fnum, off_t start_offset,
+                                SMB_OFF_T size, size_t window_size,
+                                NTSTATUS (*sink)(char *buf, size_t n,
+                                                 void *priv),
+                                void *priv)
 {
-       struct async_req *result;
+       struct tevent_req *req;
        struct cli_pull_state *state;
        int i;
 
-       if (!async_req_setup(mem_ctx, &result, &state,
-                            struct cli_pull_state)) {
+       req = tevent_req_create(mem_ctx, &state, struct cli_pull_state);
+       if (req == NULL) {
                return NULL;
        }
-       result->print = cli_pull_print;
-       state->req = result;
+       tevent_req_set_print_fn(req, cli_pull_print);
+       state->req = req;
 
        state->cli = cli;
        state->ev = ev;
@@ -332,10 +338,8 @@ struct async_req *cli_pull_send(TALLOC_CTX *mem_ctx,
        state->top_req = 0;
 
        if (size == 0) {
-               if (!async_post_ntstatus(result, ev, NT_STATUS_OK)) {
-                       goto failed;
-               }
-               return result;
+               tevent_req_done(req);
+               return tevent_req_post(req, ev);
        }
 
        state->chunk_size = cli_read_max_bufsize(cli);
@@ -372,14 +376,13 @@ struct async_req *cli_pull_send(TALLOC_CTX *mem_ctx,
                if (subreq->req == NULL) {
                        goto failed;
                }
-               tevent_req_set_callback(subreq->req, cli_pull_read_done,
-                                       result);
+               tevent_req_set_callback(subreq->req, cli_pull_read_done, req);
                state->requested += request_thistime;
        }
-       return result;
+       return req;
 
 failed:
-       TALLOC_FREE(result);
+       TALLOC_FREE(req);
        return NULL;
 }
 
@@ -390,10 +393,10 @@ failed:
 
 static void cli_pull_read_done(struct tevent_req *subreq)
 {
-       struct async_req *req = tevent_req_callback_data(
-               subreq, struct async_req);
-       struct cli_pull_state *state = talloc_get_type_abort(
-               req->private_data, struct cli_pull_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_pull_state *state = tevent_req_data(
+               req, struct cli_pull_state);
        struct cli_pull_subreq *pull_subreq = NULL;
        NTSTATUS status;
        int i;
@@ -406,14 +409,14 @@ static void cli_pull_read_done(struct tevent_req *subreq)
        }
        if (i == state->num_reqs) {
                /* Huh -- received something we did not send?? */
-               async_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
+               tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
                return;
        }
 
        status = cli_read_andx_recv(subreq, &pull_subreq->received,
                                    &pull_subreq->buf);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_nterror(state->req, status);
+               tevent_req_nterror(state->req, status);
                return;
        }
 
@@ -447,7 +450,7 @@ static void cli_pull_read_done(struct tevent_req *subreq)
                status = state->sink((char *)top_subreq->buf,
                                     top_subreq->received, state->priv);
                if (!NT_STATUS_IS_OK(status)) {
-                       async_req_nterror(state->req, status);
+                       tevent_req_nterror(state->req, status);
                        return;
                }
                state->pushed += top_subreq->received;
@@ -475,7 +478,7 @@ static void cli_pull_read_done(struct tevent_req *subreq)
                                state->start_offset + state->requested,
                                request_thistime);
 
-                       if (async_req_nomem(new_req, state->req)) {
+                       if (tevent_req_nomem(new_req, state->req)) {
                                return;
                        }
                        tevent_req_set_callback(new_req, cli_pull_read_done,
@@ -488,16 +491,16 @@ static void cli_pull_read_done(struct tevent_req *subreq)
                state->top_req = (state->top_req+1) % state->num_reqs;
        }
 
-       async_req_done(req);
+       tevent_req_done(req);
 }
 
-NTSTATUS cli_pull_recv(struct async_req *req, SMB_OFF_T *received)
+NTSTATUS cli_pull_recv(struct tevent_req *req, SMB_OFF_T *received)
 {
-       struct cli_pull_state *state = talloc_get_type_abort(
-               req->private_data, struct cli_pull_state);
+       struct cli_pull_state *state = tevent_req_data(
+               req, struct cli_pull_state);
        NTSTATUS status;
 
-       if (async_req_is_nterror(req, &status)) {
+       if (tevent_req_is_nterror(req, &status)) {
                return status;
        }
        *received = state->pushed;
@@ -511,7 +514,7 @@ NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
 {
        TALLOC_CTX *frame = talloc_stackframe();
        struct event_context *ev;
-       struct async_req *req;
+       struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
        if (cli_has_async_calls(cli)) {
@@ -535,11 +538,9 @@ NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
                goto fail;
        }
 
-       while (req->state < ASYNC_REQ_DONE) {
-               if (event_loop_once(ev) == -1) {
-                       status = map_nt_error_from_unix(errno);
-                       goto fail;
-               }
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
        }
 
        status = cli_pull_recv(req, received);
@@ -559,7 +560,7 @@ static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
        return NT_STATUS_OK;
 }
 
-ssize_t cli_read(struct cli_state *cli, int fnum, char *buf,
+ssize_t cli_read(struct cli_state *cli, uint16_t fnum, char *buf,
                 off_t offset, size_t size)
 {
        NTSTATUS status;
@@ -579,7 +580,7 @@ ssize_t cli_read(struct cli_state *cli, int fnum, char *buf,
 ****************************************************************************/
 
 static bool cli_issue_write(struct cli_state *cli,
-                               int fnum,
+                               uint16_t fnum,
                                off_t offset,
                                uint16 mode,
                                const char *buf,
@@ -679,7 +680,7 @@ static bool cli_issue_write(struct cli_state *cli,
 ****************************************************************************/
 
 ssize_t cli_write(struct cli_state *cli,
-                int fnum, uint16 write_mode,
+                uint16_t fnum, uint16 write_mode,
                 const char *buf, off_t offset, size_t size)
 {
        ssize_t bwritten = 0;
@@ -740,7 +741,7 @@ ssize_t cli_write(struct cli_state *cli,
 ****************************************************************************/
 
 ssize_t cli_smbwrite(struct cli_state *cli,
-                    int fnum, char *buf, off_t offset, size_t size1)
+                    uint16_t fnum, char *buf, off_t offset, size_t size1)
 {
        char *p;
        ssize_t total = 0;
@@ -853,9 +854,9 @@ struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
        }
 
        state->pad = 0;
-       state->iov[0].iov_base = &state->pad;
+       state->iov[0].iov_base = (void *)&state->pad;
        state->iov[0].iov_len = 1;
-       state->iov[1].iov_base = CONST_DISCARD(uint8_t *, buf);
+       state->iov[1].iov_base = CONST_DISCARD(void *, buf);
        state->iov[1].iov_len = size;
 
        subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
@@ -875,13 +876,19 @@ struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
                                       off_t offset, size_t size)
 {
        struct tevent_req *req, *subreq;
+       NTSTATUS status;
 
        req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
                                    size, NULL, 0, &subreq);
-       if ((req == NULL) || !cli_smb_req_send(subreq)) {
-               TALLOC_FREE(req);
+       if (req == NULL) {
                return NULL;
        }
+
+       status = cli_smb_req_send(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return tevent_req_post(req, ev);
+       }
        return req;
 }
 
@@ -932,20 +939,19 @@ struct cli_writeall_state {
 
 static void cli_writeall_written(struct tevent_req *req);
 
-static struct async_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
-                                          struct event_context *ev,
-                                          struct cli_state *cli,
-                                          uint16_t fnum,
-                                          uint16_t mode,
-                                          const uint8_t *buf,
-                                          off_t offset, size_t size)
+static struct tevent_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
+                                           struct event_context *ev,
+                                           struct cli_state *cli,
+                                           uint16_t fnum,
+                                           uint16_t mode,
+                                           const uint8_t *buf,
+                                           off_t offset, size_t size)
 {
-       struct async_req *result;
-       struct tevent_req *subreq;
+       struct tevent_req *req, *subreq;
        struct cli_writeall_state *state;
 
-       if (!async_req_setup(mem_ctx, &result, &state,
-                            struct cli_writeall_state)) {
+       req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
+       if (req == NULL) {
                return NULL;
        }
        state->ev = ev;
@@ -960,44 +966,40 @@ static struct async_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
        subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
                                     state->mode, state->buf, state->offset,
                                     state->size);
-       if (subreq == NULL) {
-               goto fail;
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
        }
-       tevent_req_set_callback(subreq, cli_writeall_written, result);
-       return result;
-
- fail:
-       TALLOC_FREE(result);
-       return NULL;
+       tevent_req_set_callback(subreq, cli_writeall_written, req);
+       return req;
 }
 
 static void cli_writeall_written(struct tevent_req *subreq)
 {
-       struct async_req *req = tevent_req_callback_data(
-               subreq, struct async_req);
-       struct cli_writeall_state *state = talloc_get_type_abort(
-               req->private_data, struct cli_writeall_state);
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_writeall_state *state = tevent_req_data(
+               req, struct cli_writeall_state);
        NTSTATUS status;
        size_t written, to_write;
 
        status = cli_write_andx_recv(subreq, &written);
        TALLOC_FREE(subreq);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_nterror(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
        state->written += written;
 
        if (state->written > state->size) {
-               async_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
+               tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
                return;
        }
 
        to_write = state->size - state->written;
 
        if (to_write == 0) {
-               async_req_done(req);
+               tevent_req_done(req);
                return;
        }
 
@@ -1005,20 +1007,19 @@ static void cli_writeall_written(struct tevent_req *subreq)
                                     state->mode,
                                     state->buf + state->written,
                                     state->offset + state->written, to_write);
-       if (subreq == NULL) {
-               async_req_nterror(req, NT_STATUS_NO_MEMORY);
+       if (tevent_req_nomem(subreq, req)) {
                return;
        }
        tevent_req_set_callback(subreq, cli_writeall_written, req);
 }
 
-static NTSTATUS cli_writeall_recv(struct async_req *req)
+static NTSTATUS cli_writeall_recv(struct tevent_req *req)
 {
-       return async_req_simple_recv_ntstatus(req);
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 struct cli_push_write_state {
-       struct async_req *req;/* This is the main request! Not the subreq */
+       struct tevent_req *req;/* This is the main request! Not the subreq */
        uint32_t idx;
        off_t ofs;
        uint8_t *buf;
@@ -1049,14 +1050,14 @@ struct cli_push_state {
        struct cli_push_write_state **reqs;
 };
 
-static void cli_push_written(struct async_req *req);
+static void cli_push_written(struct tevent_req *req);
 
-static bool cli_push_write_setup(struct async_req *req,
+static bool cli_push_write_setup(struct tevent_req *req,
                                 struct cli_push_state *state,
                                 uint32_t idx)
 {
        struct cli_push_write_state *substate;
-       struct async_req *subreq;
+       struct tevent_req *subreq;
 
        substate = talloc(state->reqs, struct cli_push_write_state);
        if (!substate) {
@@ -1090,8 +1091,7 @@ static bool cli_push_write_setup(struct async_req *req,
                talloc_free(substate);
                return false;
        }
-       subreq->async.fn = cli_push_written;
-       subreq->async.priv = substate;
+       tevent_req_set_callback(subreq, cli_push_written, substate);
 
        state->reqs[idx] = substate;
        state->pending += 1;
@@ -1100,20 +1100,20 @@ static bool cli_push_write_setup(struct async_req *req,
        return true;
 }
 
-struct async_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
-                               struct cli_state *cli,
-                               uint16_t fnum, uint16_t mode,
-                               off_t start_offset, size_t window_size,
-                               size_t (*source)(uint8_t *buf, size_t n,
-                                                void *priv),
-                               void *priv)
+struct tevent_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
+                                struct cli_state *cli,
+                                uint16_t fnum, uint16_t mode,
+                                off_t start_offset, size_t window_size,
+                                size_t (*source)(uint8_t *buf, size_t n,
+                                                 void *priv),
+                                void *priv)
 {
-       struct async_req *req;
+       struct tevent_req *req;
        struct cli_push_state *state;
        uint32_t i;
 
-       if (!async_req_setup(mem_ctx, &req, &state,
-                            struct cli_push_state)) {
+       req = tevent_req_create(mem_ctx, &state, struct cli_push_state);
+       if (req == NULL) {
                return NULL;
        }
        state->cli = cli;
@@ -1156,26 +1156,24 @@ struct async_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
        }
 
        if (state->pending == 0) {
-               if (!async_post_ntstatus(req, ev, NT_STATUS_OK)) {
-                       goto failed;
-               }
-               return req;
+               tevent_req_done(req);
+               return tevent_req_post(req, ev);
        }
 
        return req;
 
  failed:
-       TALLOC_FREE(req);
-       return NULL;
+       tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
+       return tevent_req_post(req, ev);
 }
 
-static void cli_push_written(struct async_req *subreq)
+static void cli_push_written(struct tevent_req *subreq)
 {
-       struct cli_push_write_state *substate = talloc_get_type_abort(
-               subreq->async.priv, struct cli_push_write_state);
-       struct async_req *req = substate->req;
-       struct cli_push_state *state = talloc_get_type_abort(
-               req->private_data, struct cli_push_state);
+       struct cli_push_write_state *substate = tevent_req_callback_data(
+               subreq, struct cli_push_write_state);
+       struct tevent_req *req = substate->req;
+       struct cli_push_state *state = tevent_req_data(
+               req, struct cli_push_state);
        NTSTATUS status;
        uint32_t idx = substate->idx;
 
@@ -1186,26 +1184,26 @@ static void cli_push_written(struct async_req *subreq)
        TALLOC_FREE(subreq);
        TALLOC_FREE(substate);
        if (!NT_STATUS_IS_OK(status)) {
-               async_req_nterror(req, status);
+               tevent_req_nterror(req, status);
                return;
        }
 
        if (!state->eof) {
                if (!cli_push_write_setup(req, state, idx)) {
-                       async_req_nomem(NULL, req);
+                       tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
                        return;
                }
        }
 
        if (state->pending == 0) {
-               async_req_done(req);
+               tevent_req_done(req);
                return;
        }
 }
 
-NTSTATUS cli_push_recv(struct async_req *req)
+NTSTATUS cli_push_recv(struct tevent_req *req)
 {
-       return async_req_simple_recv_ntstatus(req);
+       return tevent_req_simple_recv_ntstatus(req);
 }
 
 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
@@ -1215,7 +1213,7 @@ NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
 {
        TALLOC_CTX *frame = talloc_stackframe();
        struct event_context *ev;
-       struct async_req *req;
+       struct tevent_req *req;
        NTSTATUS status = NT_STATUS_OK;
 
        if (cli_has_async_calls(cli)) {
@@ -1239,11 +1237,9 @@ NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
                goto fail;
        }
 
-       while (req->state < ASYNC_REQ_DONE) {
-               if (event_loop_once(ev) == -1) {
-                       status = map_nt_error_from_unix(errno);
-                       goto fail;
-               }
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
        }
 
        status = cli_push_recv(req);