More async calls in libsmb/clifile.c
authorJeremy Allison <jra@samba.org>
Wed, 29 Apr 2009 17:48:16 +0000 (10:48 -0700)
committerJeremy Allison <jra@samba.org>
Wed, 29 Apr 2009 17:48:16 +0000 (10:48 -0700)
Jeremy.

source3/client/client.c
source3/include/proto.h
source3/libsmb/clifile.c

index 4735e8cc7ad5d6f4740434dcdeaf4f9489bdea9b..0271b456c3b8dc00f1fa4ee7f6d8ec0b2f045f10 100644 (file)
@@ -3370,7 +3370,7 @@ static int cmd_hardlink(void)
                return 1;
        }
 
-       if (!cli_nt_hardlink(targetcli, targetname, dest)) {
+       if (!NT_STATUS_IS_OK(cli_nt_hardlink(targetcli, targetname, dest))) {
                d_printf("%s doing an NT hard link of files\n",cli_errstr(targetcli));
                return 1;
        }
index 2eb838fd2a080900572f0da2e11776a0d6756250..962215b79fa5ad4dfffd2cb6dc5292ae3ac59837 100644 (file)
@@ -2339,10 +2339,25 @@ struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
                                 const char *fname_dst);
 NTSTATUS cli_rename_recv(struct tevent_req *req);
 NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst);
-bool cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst);
-bool cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst);
+struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
+                                struct event_context *ev,
+                                struct cli_state *cli,
+                                const char *fname_src,
+                                const char *fname_dst);
+NTSTATUS cli_ntrename_recv(struct tevent_req *req);
+NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst);
+
+struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
+                                struct event_context *ev,
+                                struct cli_state *cli,
+                                const char *fname_src,
+                                const char *fname_dst);
+NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req);
+NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst);
+
 bool cli_unlink_full(struct cli_state *cli, const char *fname, uint16_t attrs);
 bool cli_unlink(struct cli_state *cli, const char *fname);
+
 struct tevent_req *cli_mkdir_send(TALLOC_CTX *mem_ctx,
                                  struct event_context *ev,
                                  struct cli_state *cli,
index 97bc4d14140fd4cf1f680835debaf07b740854d7..e055a88000ba058d10edd7b58a888b57bc480db2 100644 (file)
@@ -552,84 +552,204 @@ NTSTATUS cli_rename(struct cli_state *cli, const char *fname_src, const char *fn
  NT Rename a file.
 ****************************************************************************/
 
-bool cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
+static void cli_ntrename_done(struct tevent_req *subreq);
+
+struct cli_ntrename_state {
+       uint16_t vwv[4];
+};
+
+static struct tevent_req *cli_ntrename_send_internal(TALLOC_CTX *mem_ctx,
+                               struct event_context *ev,
+                               struct cli_state *cli,
+                               const char *fname_src,
+                               const char *fname_dst,
+                               uint16_t rename_flag)
 {
-       char *p;
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct cli_ntrename_state *state = NULL;
+       uint8_t additional_flags = 0;
+       uint8_t *bytes = NULL;
 
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
+       req = tevent_req_create(mem_ctx, &state, struct cli_ntrename_state);
+       if (req == NULL) {
+               return NULL;
+       }
 
-       cli_set_message(cli->outbuf, 4, 0, true);
+       SSVAL(state->vwv+0, 0 ,aSYSTEM | aHIDDEN | aDIR);
+       SSVAL(state->vwv+1, 0, rename_flag);
 
-       SCVAL(cli->outbuf,smb_com,SMBntrename);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
+       bytes = talloc_array(state, uint8_t, 1);
+       if (tevent_req_nomem(bytes, req)) {
+               return tevent_req_post(req, ev);
+       }
+       bytes[0] = 4;
+       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_src,
+                                  strlen(fname_src)+1, NULL);
+       if (tevent_req_nomem(bytes, req)) {
+               return tevent_req_post(req, ev);
+       }
 
-       SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
-       SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_RENAME);
+       bytes = TALLOC_REALLOC_ARRAY(state, bytes, uint8_t,
+                       talloc_get_size(bytes)+1);
+       if (tevent_req_nomem(bytes, req)) {
+               return tevent_req_post(req, ev);
+       }
 
-       p = smb_buf(cli->outbuf);
-       *p++ = 4;
-       p += clistr_push(cli, p, fname_src,
-                       cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
-       *p++ = 4;
-       p += clistr_push(cli, p, fname_dst,
-                       cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
+       bytes[talloc_get_size(bytes)-1] = 4;
+       bytes = smb_bytes_push_str(bytes, cli_ucs2(cli), fname_dst,
+                                  strlen(fname_dst)+1, NULL);
+       if (tevent_req_nomem(bytes, req)) {
+               return tevent_req_post(req, ev);
+       }
 
-       cli_setup_bcc(cli, p);
+       subreq = cli_smb_send(state, ev, cli, SMBntrename, additional_flags,
+                             4, state->vwv, talloc_get_size(bytes), bytes);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
+       }
+       tevent_req_set_callback(subreq, cli_ntrename_done, req);
+       return req;
+}
 
-       cli_send_smb(cli);
-       if (!cli_receive_smb(cli)) {
-               return false;
+struct tevent_req *cli_ntrename_send(TALLOC_CTX *mem_ctx,
+                               struct event_context *ev,
+                               struct cli_state *cli,
+                               const char *fname_src,
+                               const char *fname_dst)
+{
+       return cli_ntrename_send_internal(mem_ctx,
+                                       ev,
+                                       cli,
+                                       fname_src,
+                                       fname_dst,
+                                       RENAME_FLAG_RENAME);
+}
+
+static void cli_ntrename_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+                               subreq, struct tevent_req);
+       NTSTATUS status;
+
+       status = cli_smb_recv(subreq, 0, NULL, NULL, NULL, NULL);
+       TALLOC_FREE(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return;
        }
+       tevent_req_done(req);
+}
 
-       if (cli_is_error(cli)) {
-               return false;
+NTSTATUS cli_ntrename_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
+
+NTSTATUS cli_ntrename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_OK;
+
+       if (cli_has_async_calls(cli)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
        }
 
-       return true;
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       req = cli_ntrename_send(frame, ev, cli, fname_src, fname_dst);
+       if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
+
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
+       }
+
+       status = cli_ntrename_recv(req);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
 }
 
 /****************************************************************************
  NT hardlink a file.
 ****************************************************************************/
 
-bool cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
+struct tevent_req *cli_nt_hardlink_send(TALLOC_CTX *mem_ctx,
+                               struct event_context *ev,
+                               struct cli_state *cli,
+                               const char *fname_src,
+                               const char *fname_dst)
 {
-       char *p;
-
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
-
-       cli_set_message(cli->outbuf, 4, 0, true);
+       return cli_ntrename_send_internal(mem_ctx,
+                                       ev,
+                                       cli,
+                                       fname_src,
+                                       fname_dst,
+                                       RENAME_FLAG_HARD_LINK);
+}
 
-       SCVAL(cli->outbuf,smb_com,SMBntrename);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
+NTSTATUS cli_nt_hardlink_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
 
-       SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN | aDIR);
-       SSVAL(cli->outbuf,smb_vwv1, RENAME_FLAG_HARD_LINK);
+NTSTATUS cli_nt_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev;
+       struct tevent_req *req;
+       NTSTATUS status = NT_STATUS_OK;
 
-       p = smb_buf(cli->outbuf);
-       *p++ = 4;
-       p += clistr_push(cli, p, fname_src,
-                       cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
-       *p++ = 4;
-       p += clistr_push(cli, p, fname_dst,
-                       cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
+       if (cli_has_async_calls(cli)) {
+               /*
+                * Can't use sync call while an async call is in flight
+                */
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto fail;
+       }
 
-       cli_setup_bcc(cli, p);
+       ev = event_context_init(frame);
+       if (ev == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
 
-       cli_send_smb(cli);
-       if (!cli_receive_smb(cli)) {
-               return false;
+       req = cli_nt_hardlink_send(frame, ev, cli, fname_src, fname_dst);
+       if (req == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
        }
 
-       if (cli_is_error(cli)) {
-               return false;
+       if (!tevent_req_poll(req, ev)) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
        }
 
-       return true;
+       status = cli_nt_hardlink_recv(req);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
 }
 
 /****************************************************************************