Make cli_getatr() async.
authorJeremy Allison <jra@samba.org>
Wed, 6 May 2009 03:59:22 +0000 (20:59 -0700)
committerJeremy Allison <jra@samba.org>
Wed, 6 May 2009 03:59:22 +0000 (20:59 -0700)
Jeremy.

source3/client/clitar.c
source3/include/proto.h
source3/lib/time.c
source3/libsmb/clifile.c
source3/libsmb/libsmb_file.c
source3/torture/torture.c

index fd6c4ccef9a5edbdfbeef59874a1a2ffb2da888d..80f6c81880a18c5c5caad4334698ef5f3c40bbf8 100644 (file)
@@ -594,7 +594,9 @@ static void do_setrattr(char *name, uint16 attr, int set)
 {
        uint16 oldattr;
 
-       if (!cli_getatr(cli, name, &oldattr, NULL, NULL)) return;
+       if (!NT_STATUS_IS_OK(cli_getatr(cli, name, &oldattr, NULL, NULL))) {
+               return;
+       }
 
        if (set == ATTRSET) {
                attr |= oldattr;
index 30c11a50eeb4c5ff7430738ea2af3ce52ebac515..194d74db5e523b6e30d7448ce021a475c62eafe1 100644 (file)
@@ -1018,6 +1018,7 @@ struct timespec get_ctimespec(const SMB_STRUCT_STAT *pst);
 void set_ctimespec(SMB_STRUCT_STAT *pst, struct timespec ts);
 void dos_filetime_timespec(struct timespec *tsp);
 time_t make_unix_date2(const void *date_ptr, int zone_offset);
+time_t make_unix_date3(const void *date_ptr, int zone_offset);
 time_t srv_make_unix_date(const void *date_ptr);
 time_t srv_make_unix_date2(const void *date_ptr);
 time_t srv_make_unix_date3(const void *date_ptr);
@@ -2459,12 +2460,23 @@ NTSTATUS cli_getattrE(struct cli_state *cli,
                        time_t *change_time,
                        time_t *access_time,
                        time_t *write_time);
-bool cli_getatr(struct cli_state *cli, const char *fname,
-               uint16_t *attr, SMB_OFF_T *size, time_t *write_time);
 bool cli_setattrE(struct cli_state *cli, int fd,
                  time_t change_time,
                   time_t access_time,
                   time_t write_time);
+struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
+                               struct event_context *ev,
+                               struct cli_state *cli,
+                               const char *fname);
+NTSTATUS cli_getatr_recv(struct tevent_req *req,
+                               uint16_t *attr,
+                               SMB_OFF_T *size,
+                               time_t *write_time);
+NTSTATUS cli_getatr(struct cli_state *cli,
+                       const char *fname,
+                       uint16_t *attr,
+                       SMB_OFF_T *size,
+                       time_t *write_time);
 bool cli_setatr(struct cli_state *cli, const char *fname, uint16_t attr, time_t t);
 struct tevent_req *cli_chkpath_send(TALLOC_CTX *mem_ctx,
                                  struct event_context *ev,
index 3e45a2e6e425e0081d57b63c62683775561c99da..611debe36677747b495e93e4b2d250a47f53f76b 100644 (file)
@@ -621,7 +621,7 @@ time_t make_unix_date2(const void *date_ptr, int zone_offset)
  these generally arrive as localtimes, with corresponding DST.
 ******************************************************************/
 
-static time_t make_unix_date3(const void *date_ptr, int zone_offset)
+time_t make_unix_date3(const void *date_ptr, int zone_offset)
 {
        time_t t = (time_t)IVAL(date_ptr,0);
        if (!null_mtime(t)) {
index bc466d131594ba3d2fd4745cae7ddba4c425f2c9..357923abcbc3a5c5733e88143dc4901a15138a66 100644 (file)
@@ -2243,49 +2243,148 @@ NTSTATUS cli_getattrE(struct cli_state *cli,
  Do a SMBgetatr call
 ****************************************************************************/
 
-bool cli_getatr(struct cli_state *cli, const char *fname,
-               uint16_t *attr, SMB_OFF_T *size, time_t *write_time)
-{
-       char *p;
+static void cli_getatr_done(struct tevent_req *subreq);
 
-       memset(cli->outbuf,'\0',smb_size);
-       memset(cli->inbuf,'\0',smb_size);
+struct cli_getatr_state {
+       int zone_offset;
+       uint16_t attr;
+       SMB_OFF_T size;
+       time_t write_time;
+};
 
-       cli_set_message(cli->outbuf,0,0,True);
+struct tevent_req *cli_getatr_send(TALLOC_CTX *mem_ctx,
+                               struct event_context *ev,
+                               struct cli_state *cli,
+                               const char *fname)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct cli_getatr_state *state = NULL;
+       uint8_t additional_flags = 0;
+       uint8_t *bytes = NULL;
 
-       SCVAL(cli->outbuf,smb_com,SMBgetatr);
-       SSVAL(cli->outbuf,smb_tid,cli->cnum);
-       cli_setup_packet(cli);
+       req = tevent_req_create(mem_ctx, &state, struct cli_getatr_state);
+       if (req == NULL) {
+               return NULL;
+       }
 
-       p = smb_buf(cli->outbuf);
-       *p++ = 4;
-       p += clistr_push(cli, p, fname,
-                       cli->bufsize - PTR_DIFF(p,cli->outbuf), STR_TERMINATE);
+       state->zone_offset = cli->serverzone;
 
-       cli_setup_bcc(cli, p);
+       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,
+                                  strlen(fname)+1, NULL);
 
-       cli_send_smb(cli);
-       if (!cli_receive_smb(cli)) {
-               return False;
+       if (tevent_req_nomem(bytes, req)) {
+               return tevent_req_post(req, ev);
        }
 
-       if (cli_is_error(cli)) {
-               return False;
+       subreq = cli_smb_send(state, ev, cli, SMBgetatr, additional_flags,
+                             0, NULL, talloc_get_size(bytes), bytes);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
        }
+       tevent_req_set_callback(subreq, cli_getatr_done, req);
+       return req;
+}
 
-       if (size) {
-               *size = IVAL(cli->inbuf, smb_vwv3);
+static void cli_getatr_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct cli_getatr_state *state = tevent_req_data(
+               req, struct cli_getatr_state);
+       uint8_t wct;
+       uint16_t *vwv = NULL;
+       NTSTATUS status;
+
+       status = cli_smb_recv(subreq, 4, &wct, &vwv, NULL, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return;
        }
 
+       state->attr = SVAL(vwv+0,0);
+       state->size = (SMB_OFF_T)IVAL(vwv+3,0);
+       state->write_time = make_unix_date3(vwv+1, state->zone_offset);
+
+       TALLOC_FREE(subreq);
+       tevent_req_done(req);
+}
+
+NTSTATUS cli_getatr_recv(struct tevent_req *req,
+                       uint16_t *attr,
+                       SMB_OFF_T *size,
+                       time_t *write_time)
+{
+       struct cli_getatr_state *state = tevent_req_data(
+                               req, struct cli_getatr_state);
+       NTSTATUS status;
+
+       if (tevent_req_is_nterror(req, &status)) {
+               return status;
+       }
+       if (attr) {
+               *attr = state->attr;
+       }
+       if (size) {
+               *size = state->size;
+       }
        if (write_time) {
-               *write_time = cli_make_unix_date3(cli, cli->inbuf+smb_vwv1);
+               *write_time = state->write_time;
        }
+       return NT_STATUS_OK;
+}
 
-       if (attr) {
-               *attr = SVAL(cli->inbuf,smb_vwv0);
+NTSTATUS cli_getatr(struct cli_state *cli,
+                       const char *fname,
+                       uint16_t *attr,
+                       SMB_OFF_T *size,
+                       time_t *write_time)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct event_context *ev = NULL;
+       struct tevent_req *req = NULL;
+       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_getatr_send(frame, ev, cli, fname);
+       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_getatr_recv(req,
+                               attr,
+                               size,
+                               write_time);
+
+ fail:
+       TALLOC_FREE(frame);
+       if (!NT_STATUS_IS_OK(status)) {
+               cli_set_error(cli, status);
+       }
+       return status;
 }
 
 /****************************************************************************
index a56126587a88fb7ba73c932aeaabaae2a1f74791..63590d2fdf1688ab66d8ad0523f6b68611ec439d 100644 (file)
@@ -578,7 +578,7 @@ SMBC_getatr(SMBCCTX * context,
                 return False;
         }
         
-       if (cli_getatr(targetcli, targetpath, mode, size, &write_time)) {
+       if (NT_STATUS_IS_OK(cli_getatr(targetcli, targetpath, mode, size, &write_time))) {
                 
                 struct timespec w_time_ts;
                 
index 00e8266a22af8a359aaabc00ce6ea9c87eef51aa..bc7ac12a76f98ff9f3deaaf50953ad30553e015c 100644 (file)
@@ -2471,7 +2471,7 @@ static bool run_attrtest(int dummy)
        cli_open(cli, fname, 
                        O_RDWR | O_CREAT | O_TRUNC, DENY_NONE, &fnum);
        cli_close(cli, fnum);
-       if (!cli_getatr(cli, fname, NULL, NULL, &t)) {
+       if (!NT_STATUS_IS_OK(cli_getatr(cli, fname, NULL, NULL, &t))) {
                printf("getatr failed (%s)\n", cli_errstr(cli));
                correct = False;
        }
@@ -2490,7 +2490,7 @@ static bool run_attrtest(int dummy)
                correct = True;
        }
 
-       if (!cli_getatr(cli, fname, NULL, NULL, &t)) {
+       if (!NT_STATUS_IS_OK(cli_getatr(cli, fname, NULL, NULL, &t))) {
                printf("getatr failed (%s)\n", cli_errstr(cli));
                correct = True;
        }
@@ -3796,7 +3796,7 @@ static bool run_opentest(int dummy)
        }
 
        /* Ensure size == 20. */
-       if (!cli_getatr(cli1, fname, NULL, &fsize, NULL)) {
+       if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, NULL, &fsize, NULL))) {
                printf("(3) getatr failed (%s)\n", cli_errstr(cli1));
                return False;
        }
@@ -3819,7 +3819,7 @@ static bool run_opentest(int dummy)
        }
 
        /* Ensure size == 0. */
-       if (!cli_getatr(cli1, fname, NULL, &fsize, NULL)) {
+       if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, NULL, &fsize, NULL))) {
                printf("(3) getatr failed (%s)\n", cli_errstr(cli1));
                return False;
        }
@@ -4285,7 +4285,7 @@ static bool run_openattrtest(int dummy)
                                return False;
                        }
 
-                       if (!cli_getatr(cli1, fname, &attr, NULL, NULL)) {
+                       if (!NT_STATUS_IS_OK(cli_getatr(cli1, fname, &attr, NULL, NULL))) {
                                printf("getatr(2) failed (%s)\n", cli_errstr(cli1));
                                return False;
                        }