Convert cli_rename to async.
authorJeremy Allison <jra@samba.org>
Tue, 28 Apr 2009 23:43:16 +0000 (16:43 -0700)
committerJeremy Allison <jra@samba.org>
Tue, 28 Apr 2009 23:43:16 +0000 (16:43 -0700)
Jeremy.

source3/client/client.c
source3/include/proto.h
source3/libsmb/clifile.c
source3/libsmb/libsmb_dir.c
source3/torture/nbio.c
source3/torture/torture.c

index d657bb98d28c367c525c19233ef6b6af46bb8b6b..4735e8cc7ad5d6f4740434dcdeaf4f9489bdea9b 100644 (file)
@@ -3300,7 +3300,7 @@ static int cmd_rename(void)
                return 1;
        }
 
-       if (!cli_rename(targetcli, targetsrc, targetdest)) {
+       if (!NT_STATUS_IS_OK(cli_rename(targetcli, targetsrc, targetdest))) {
                d_printf("%s renaming files %s -> %s \n",
                        cli_errstr(targetcli),
                        targetsrc,
index 00a37ddba9d090a0cf7851d2a0c7d638318e4f77..46600e2db2d98fd0a60c4fbf1cca0a37b3d9203e 100644 (file)
@@ -2332,7 +2332,13 @@ bool cli_unix_symlink(struct cli_state *cli, const char *oldname, const char *ne
 bool cli_unix_hardlink(struct cli_state *cli, const char *oldname, const char *newname);
 bool cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode);
 bool cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid);
-bool cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst);
+struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
+                                struct event_context *ev,
+                                struct cli_state *cli,
+                                const char *fname_src,
+                                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);
 bool cli_unlink_full(struct cli_state *cli, const char *fname, uint16_t attrs);
index d38b5c5014c4979465f06dd5234913e689ad413b..340b9dd9aa80a531e55bb3b04c52a9830ebf2a6c 100644 (file)
@@ -429,41 +429,124 @@ bool cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t g
  Rename a file.
 ****************************************************************************/
 
-bool cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
+static void cli_rename_done(struct tevent_req *subreq);
+
+struct cli_rename_state {
+       uint16_t vwv[1];
+        int dummy;
+};
+
+struct tevent_req *cli_rename_send(TALLOC_CTX *mem_ctx,
+                               struct event_context *ev,
+                               struct cli_state *cli,
+                               const char *fname_src,
+                               const char *fname_dst)
 {
-       char *p;
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct cli_rename_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_rename_state);
+       if (req == NULL) {
+               return NULL;
+       }
 
-       cli_set_message(cli->outbuf,1, 0, true);
+       SSVAL(state->vwv+0, 0, aSYSTEM | aHIDDEN | aDIR);
 
-       SCVAL(cli->outbuf,smb_com,SMBmv);
-       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);
+       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, SMBmv, additional_flags,
+                             1, 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_rename_done, req);
+       return req;
+}
 
-       cli_send_smb(cli);
-       if (!cli_receive_smb(cli)) {
-               return false;
+static void cli_rename_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_rename_recv(struct tevent_req *req)
+{
+       return tevent_req_simple_recv_ntstatus(req);
+}
+
+NTSTATUS cli_rename(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_rename_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_rename_recv(req);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
 }
 
 /****************************************************************************
index 508810538f8a67d2021682c2fa17921ccde8864a..bc5e35f79b73c3493f6d57cb16f9000dd8df54d1 100644 (file)
@@ -1962,12 +1962,12 @@ SMBC_rename_ctx(SMBCCTX *ocontext,
                return -1;
        }
 
-       if (!cli_rename(targetcli1, targetpath1, targetpath2)) {
+       if (!NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
                int eno = SMBC_errno(ocontext, targetcli1);
 
                if (eno != EEXIST ||
                    !cli_unlink(targetcli1, targetpath2) ||
-                   !cli_rename(targetcli1, targetpath1, targetpath2)) {
+                   !NT_STATUS_IS_OK(cli_rename(targetcli1, targetpath1, targetpath2))) {
 
                        errno = eno;
                        TALLOC_FREE(frame);
index 998adac4e0a4b27e23fbad01d88aa2d2822340be..7503e59357a619152b69cd6184be8ebf0d324d68 100644 (file)
@@ -229,7 +229,7 @@ void nb_rmdir(const char *fname)
 
 void nb_rename(const char *oldname, const char *newname)
 {
-       if (!cli_rename(c, oldname, newname)) {
+       if (!NT_STATUS_IS_OK(cli_rename(c, oldname, newname))) {
                printf("ERROR: rename %s %s failed (%s)\n", 
                       oldname, newname, cli_errstr(c));
                exit(1);
index ed041f791250a04305ba538ddd3bf4ee4b171b0d..238a2f3c8304032293f109f9ff9d7b6642620f84 100644 (file)
@@ -3564,7 +3564,7 @@ static bool run_rename(int dummy)
                return False;
        }
 
-       if (!cli_rename(cli1, fname, fname1)) {
+       if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
                printf("First rename failed (SHARE_READ) (this is correct) - %s\n", cli_errstr(cli1));
        } else {
                printf("First rename succeeded (SHARE_READ) - this should have failed !\n");
@@ -3590,7 +3590,7 @@ static bool run_rename(int dummy)
                return False;
        }
 
-       if (!cli_rename(cli1, fname, fname1)) {
+       if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
                printf("Second rename failed (SHARE_DELETE | SHARE_READ) - this should have succeeded - %s\n", cli_errstr(cli1));
                correct = False;
        } else {
@@ -3637,7 +3637,7 @@ static bool run_rename(int dummy)
   }
 #endif
 
-       if (!cli_rename(cli1, fname, fname1)) {
+       if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
                printf("Third rename failed (SHARE_NONE) - this should have succeeded - %s\n", cli_errstr(cli1));
                correct = False;
        } else {
@@ -3662,7 +3662,7 @@ static bool run_rename(int dummy)
                return False;
        }
 
-       if (!cli_rename(cli1, fname, fname1)) {
+       if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
                printf("Fourth rename failed (SHARE_READ | SHARE_WRITE) (this is correct) - %s\n", cli_errstr(cli1));
        } else {
                printf("Fourth rename succeeded (SHARE_READ | SHARE_WRITE) - this should have failed !\n");
@@ -3687,7 +3687,7 @@ static bool run_rename(int dummy)
                return False;
        }
 
-       if (!cli_rename(cli1, fname, fname1)) {
+       if (!NT_STATUS_IS_OK(cli_rename(cli1, fname, fname1))) {
                printf("Fifth rename failed (SHARE_READ | SHARE_WRITE | SHARE_DELETE) - this should have succeeded - %s ! \n",
                        cli_errstr(cli1));
                correct = False;