From: Jeremy Allison Date: Tue, 28 Apr 2009 23:43:16 +0000 (-0700) Subject: Convert cli_rename to async. X-Git-Url: http://git.samba.org/?a=commitdiff_plain;h=bd6447dcf26ec217f335784f05df304d45288850;p=metze%2Fsamba%2Fwip.git Convert cli_rename to async. Jeremy. --- diff --git a/source3/client/client.c b/source3/client/client.c index d657bb98d28c..4735e8cc7ad5 100644 --- a/source3/client/client.c +++ b/source3/client/client.c @@ -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, diff --git a/source3/include/proto.h b/source3/include/proto.h index 00a37ddba9d0..46600e2db2d9 100644 --- a/source3/include/proto.h +++ b/source3/include/proto.h @@ -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); diff --git a/source3/libsmb/clifile.c b/source3/libsmb/clifile.c index d38b5c5014c4..340b9dd9aa80 100644 --- a/source3/libsmb/clifile.c +++ b/source3/libsmb/clifile.c @@ -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; } /**************************************************************************** diff --git a/source3/libsmb/libsmb_dir.c b/source3/libsmb/libsmb_dir.c index 508810538f8a..bc5e35f79b73 100644 --- a/source3/libsmb/libsmb_dir.c +++ b/source3/libsmb/libsmb_dir.c @@ -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); diff --git a/source3/torture/nbio.c b/source3/torture/nbio.c index 998adac4e0a4..7503e59357a6 100644 --- a/source3/torture/nbio.c +++ b/source3/torture/nbio.c @@ -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); diff --git a/source3/torture/torture.c b/source3/torture/torture.c index ed041f791250..238a2f3c8304 100644 --- a/source3/torture/torture.c +++ b/source3/torture/torture.c @@ -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;