The branch, v3-2-test has been updated
       via  719527f55e88f0c5fdceda5c807475aba299c79f (commit)
       via  844a163458c7585e4306a21ffdae5d08e03d6e4d (commit)
       via  f5356825698a02df2d400b51dd95d1f857c83e81 (commit)
       via  ae254cb61f4b9331755848c47ebc34e90dd80390 (commit)
      from  d16c295642c3df49be02440427ded0cd9b4179f5 (commit)

http://gitweb.samba.org/?samba.git;a=shortlog;h=v3-2-test


- Log -----------------------------------------------------------------
commit 719527f55e88f0c5fdceda5c807475aba299c79f
Author: Volker Lendecke <[EMAIL PROTECTED]>
Date:   Thu Feb 28 15:26:01 2008 +0100

    Convert cli_read to use cli_pull

commit 844a163458c7585e4306a21ffdae5d08e03d6e4d
Author: Volker Lendecke <[EMAIL PROTECTED]>
Date:   Thu Feb 28 15:21:33 2008 +0100

    Add async cli_pull support
    
    This is the big (and potentially controversial) one. It took a phone call to
    explain to metze what is going on inside cli_pull_read_done, but I would 
really
    like everybody to understand this function. It is a very good and reasonably
    complex example of async programming. If we want more asynchronism in s3, 
this
    is what we will have to deal with :-)
    
    Make use of it in the smbclient "get" command.
    
    Volker

commit f5356825698a02df2d400b51dd95d1f857c83e81
Author: Volker Lendecke <[EMAIL PROTECTED]>
Date:   Thu Feb 28 14:41:25 2008 +0100

    Add infrastructure to support async SMB requests

commit ae254cb61f4b9331755848c47ebc34e90dd80390
Author: Volker Lendecke <[EMAIL PROTECTED]>
Date:   Thu Feb 28 13:55:53 2008 +0100

    Add basic infrastructure for general async requests

-----------------------------------------------------------------------

Summary of changes:
 source/Makefile.in           |    4 +-
 source/client/client.c       |   42 ++---
 source/include/async_req.h   |   89 +++++++
 source/include/async_smb.h   |   62 +++++
 source/include/client.h      |   30 +++
 source/include/includes.h    |    2 +
 source/lib/async_req.c       |   68 ++++++
 source/libsmb/async_smb.c    |  483 +++++++++++++++++++++++++++++++++++++++
 source/libsmb/clireadwrite.c |  519 ++++++++++++++++++++++++++++++++----------
 9 files changed, 1149 insertions(+), 150 deletions(-)
 create mode 100644 source/include/async_req.h
 create mode 100644 source/include/async_smb.h
 create mode 100644 source/lib/async_req.c
 create mode 100644 source/libsmb/async_smb.c


Changeset truncated at 500 lines:

diff --git a/source/Makefile.in b/source/Makefile.in
index 245889b..e0068ba 100644
--- a/source/Makefile.in
+++ b/source/Makefile.in
@@ -323,7 +323,7 @@ LIB_WITHOUT_PROTO_OBJ = $(LIBSAMBAUTIL_OBJ) \
        lib/messages.o librpc/gen_ndr/ndr_messaging.o lib/messages_local.o \
        lib/messages_ctdbd.o lib/packet.o lib/ctdbd_conn.o lib/talloc_stack.o \
        lib/interfaces.o lib/rbtree.o lib/memcache.o \
-       lib/util_transfer_file.o
+       lib/util_transfer_file.o lib/async_req.o
 
 LIB_WITH_PROTO_OBJ = $(VERSION_OBJ) lib/charcnv.o lib/debug.o lib/fault.o \
          lib/interface.o lib/md4.o \
@@ -408,7 +408,7 @@ LIBSMB_OBJ = libsmb/clientgen.o libsmb/cliconnect.o 
libsmb/clifile.o \
             libsmb/clistr.o libsmb/cliquota.o libsmb/clifsinfo.o 
libsmb/clidfs.o \
              libsmb/smberr.o libsmb/credentials.o libsmb/pwd_cache.o \
             libsmb/clioplock.o $(ERRORMAP_OBJ) libsmb/clirap2.o \
-            libsmb/smb_seal.o $(DOSERR_OBJ) \
+            libsmb/smb_seal.o $(DOSERR_OBJ) libsmb/async_smb.o \
             $(RPC_PARSE_OBJ1) $(LIBSAMBA_OBJ) $(LIBNMB_OBJ)
 
 RPC_CLIENT_OBJ1 = rpc_client/cli_netlogon.o rpc_client/cli_srvsvc.o
diff --git a/source/client/client.c b/source/client/client.c
index f7ed33a..1410fc2 100644
--- a/source/client/client.c
+++ b/source/client/client.c
@@ -964,12 +964,20 @@ static int cmd_echo(void)
  Get a file from rname to lname
 ****************************************************************************/
 
+static NTSTATUS writefile_sink(char *buf, size_t n, void *priv)
+{
+       int *pfd = (int *)priv;
+       if (writefile(*pfd, buf, n) == -1) {
+               return map_nt_error_from_unix(errno);
+       }
+       return NT_STATUS_OK;
+}
+
 static int do_get(const char *rname, const char *lname_in, bool reget)
 {
        TALLOC_CTX *ctx = talloc_tos();
        int handle = 0, fnum;
        bool newhandle = false;
-       char *data = NULL;
        struct timeval tp_start;
        int read_size = io_bufsize;
        uint16 attr;
@@ -980,6 +988,7 @@ static int do_get(const char *rname, const char *lname_in, 
bool reget)
        struct cli_state *targetcli = NULL;
        char *targetname = NULL;
        char *lname = NULL;
+       NTSTATUS status;
 
        lname = talloc_strdup(ctx, lname_in);
        if (!lname) {
@@ -1038,36 +1047,15 @@ static int do_get(const char *rname, const char 
*lname_in, bool reget)
        DEBUG(1,("getting file %s of size %.0f as %s ",
                 rname, (double)size, lname));
 
-       if(!(data = (char *)SMB_MALLOC(read_size))) {
-               d_printf("malloc fail for size %d\n", read_size);
+       status = cli_pull(targetcli, fnum, start, size, 1024*1024,
+                         writefile_sink, (void *)&handle, &nread);
+       if (!NT_STATUS_IS_OK(status)) {
+               d_fprintf(stderr, "parallel_read returned %s\n",
+                         nt_errstr(status));
                cli_close(targetcli, fnum);
                return 1;
        }
 
-       while (1) {
-               int n = cli_read(targetcli, fnum, data, nread + start, 
read_size);
-
-               if (n <= 0)
-                       break;
-
-               if (writefile(handle,data, n) != n) {
-                       d_printf("Error writing local file\n");
-                       rc = 1;
-                       break;
-               }
-
-               nread += n;
-       }
-
-       if (nread + start < size) {
-               DEBUG (0, ("Short read when getting file %s. Only got %ld 
bytes.\n",
-                           rname, (long)nread));
-
-               rc = 1;
-       }
-
-       SAFE_FREE(data);
-
        if (!cli_close(targetcli, fnum)) {
                d_printf("Error %s closing remote file\n",cli_errstr(cli));
                rc = 1;
diff --git a/source/include/async_req.h b/source/include/async_req.h
new file mode 100644
index 0000000..6df5360
--- /dev/null
+++ b/source/include/async_req.h
@@ -0,0 +1,89 @@
+/*
+   Unix SMB/CIFS implementation.
+   Infrastructure for async requests
+   Copyright (C) Volker Lendecke 2008
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __ASYNC_REQ_H__
+#define __ASYNC_REQ_H__
+
+#include "includes.h"
+
+/*
+ * An async request moves between the following 4 states.
+ */
+enum async_req_state {
+       ASYNC_REQ_INIT,         /* we are creating the request */
+       ASYNC_REQ_IN_PROGRESS,  /* we are waiting the request to complete */
+       ASYNC_REQ_DONE,         /* the request is finished */
+       ASYNC_REQ_ERROR };      /* an error has occured */
+
+struct async_req {
+       /* the external state - will be queried by the caller */
+       enum async_req_state state;
+
+       /* a private pointer for use by the async function implementation */
+       void *private_data;
+
+       /* print yourself, for debugging purposes */
+       char *(*print)(TALLOC_CTX *mem_ctx, struct async_req *);
+
+       /* status code when finished */
+       NTSTATUS status;
+
+       /* the event context we are using */
+       struct event_context *event_ctx;
+
+       /* information on what to do on completion */
+       struct {
+               void (*fn)(struct async_req *);
+               void *priv;
+       } async;
+};
+
+/*
+ * Print an async_req structure for debugging purposes
+ */
+char *async_req_print(TALLOC_CTX *mem_ctx, struct async_req *req);
+
+/*
+ * Create an async request
+ */
+struct async_req *async_req_new(TALLOC_CTX *mem_ctx, struct event_context *ev);
+
+/*
+ * An async request has successfully finished, invoke the callback
+ */
+void async_req_done(struct async_req *req);
+
+/*
+ * An async request has seen an error, invoke the callback
+ */
+void async_req_error(struct async_req *req, NTSTATUS status);
+
+/*
+ * Convenience helper to easily check alloc failure within a callback.
+ *
+ * Call pattern would be
+ * p = talloc(mem_ctx, bla);
+ * if (async_req_nomem(p, req)) {
+ *     return;
+ * }
+ *
+ */
+bool async_req_nomem(const void *p, struct async_req *req);
+
+#endif
diff --git a/source/include/async_smb.h b/source/include/async_smb.h
new file mode 100644
index 0000000..19408be
--- /dev/null
+++ b/source/include/async_smb.h
@@ -0,0 +1,62 @@
+/*
+   Unix SMB/CIFS implementation.
+   Infrastructure for async SMB client requests
+   Copyright (C) Volker Lendecke 2008
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+
+/*
+ * Create a fresh async smb request
+ */
+
+struct async_req *cli_request_new(TALLOC_CTX *mem_ctx,
+                                 struct event_context *ev,
+                                 struct cli_state *cli,
+                                 uint8_t num_words, size_t num_bytes,
+                                 struct cli_request **preq);
+
+/*
+ * Convenience function to get the SMB part out of an async_req
+ */
+
+struct cli_request *cli_request_get(struct async_req *req);
+
+/*
+ * Fetch an error out of a NBT packet
+ */
+
+NTSTATUS cli_pull_error(char *buf);
+
+/*
+ * Compatibility helper for the sync APIs: Fake NTSTATUS in cli->inbuf
+ */
+
+void cli_set_error(struct cli_state *cli, NTSTATUS status);
+
+/*
+ * Create a temporary event context for use in the sync helper functions
+ */
+
+struct cli_tmp_event *cli_tmp_event_ctx(TALLOC_CTX *mem_ctx,
+                                       struct cli_state *cli);
+
+/*
+ * Attach an event context permanently to a cli_struct
+ */
+
+NTSTATUS cli_add_event_ctx(struct cli_state *cli,
+                          struct event_context *event_ctx);
diff --git a/source/include/client.h b/source/include/client.h
index d29a2e7..52dc513 100644
--- a/source/include/client.h
+++ b/source/include/client.h
@@ -192,6 +192,36 @@ struct cli_state {
 
        bool force_dos_errors;
        bool case_sensitive; /* False by default. */
+
+       struct event_context *event_ctx;
+       struct fd_event *fd_event;
+       char *evt_inbuf;
+
+       struct cli_request *outstanding_requests;
+};
+
+struct cli_request {
+       struct cli_request *prev, *next;
+       struct async_req *async;
+
+       struct cli_state *cli;
+
+       struct smb_trans_enc_state *enc_state;
+
+       uint16_t mid;
+
+       char *outbuf;
+       size_t sent;
+       char *inbuf;
+
+       union {
+               struct {
+                       off_t ofs;
+                       size_t size;
+                       ssize_t received;
+                       uint8_t *rcvbuf;
+               } read;
+       } data;
 };
 
 typedef struct file_info {
diff --git a/source/include/includes.h b/source/include/includes.h
index a2c3a0c..269baa5 100644
--- a/source/include/includes.h
+++ b/source/include/includes.h
@@ -723,6 +723,8 @@ typedef char fstring[FSTRING_LEN];
 #include "ctdbd_conn.h"
 #include "talloc_stack.h"
 #include "memcache.h"
+#include "async_req.h"
+#include "async_smb.h"
 
 /* used in net.c */
 struct functable {
diff --git a/source/lib/async_req.c b/source/lib/async_req.c
new file mode 100644
index 0000000..01154ca
--- /dev/null
+++ b/source/lib/async_req.c
@@ -0,0 +1,68 @@
+/*
+   Unix SMB/CIFS implementation.
+   Infrastructure for async requests
+   Copyright (C) Volker Lendecke 2008
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+
+char *async_req_print(TALLOC_CTX *mem_ctx, struct async_req *req)
+{
+       return talloc_asprintf(mem_ctx, "async_req: state=%d, status=%s, "
+                              "priv=%s", req->state, nt_errstr(req->status),
+                              talloc_get_name(req->private_data));
+}
+
+struct async_req *async_req_new(TALLOC_CTX *mem_ctx, struct event_context *ev)
+{
+       struct async_req *result;
+
+       result = TALLOC_ZERO_P(mem_ctx, struct async_req);
+       if (result == NULL) {
+               return NULL;
+       }
+       result->state = ASYNC_REQ_IN_PROGRESS;
+       result->event_ctx = ev;
+       result->print = async_req_print;
+       return result;
+}
+
+void async_req_done(struct async_req *req)
+{
+       req->status = NT_STATUS_OK;
+       req->state = ASYNC_REQ_DONE;
+       if (req->async.fn != NULL) {
+               req->async.fn(req);
+       }
+}
+
+void async_req_error(struct async_req *req, NTSTATUS status)
+{
+       req->status = status;
+       req->state = ASYNC_REQ_ERROR;
+       if (req->async.fn != NULL) {
+               req->async.fn(req);
+       }
+}
+
+bool async_req_nomem(const void *p, struct async_req *req)
+{
+       if (p != NULL) {
+               return false;
+       }
+       async_req_error(req, NT_STATUS_NO_MEMORY);
+       return true;
+}
diff --git a/source/libsmb/async_smb.c b/source/libsmb/async_smb.c
new file mode 100644
index 0000000..21bcd5b
--- /dev/null
+++ b/source/libsmb/async_smb.c
@@ -0,0 +1,483 @@
+/*
+   Unix SMB/CIFS implementation.
+   Infrastructure for async SMB client requests
+   Copyright (C) Volker Lendecke 2008
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+
+/*
+ * Fetch an error out of a NBT packet
+ */
+
+NTSTATUS cli_pull_error(char *buf)
+{
+       uint32_t flags2 = SVAL(buf, smb_flg2);
+
+       if (flags2 & FLAGS2_32_BIT_ERROR_CODES) {
+               return NT_STATUS(IVAL(buf, smb_rcls));
+       }
+
+       return NT_STATUS_DOS(CVAL(buf, smb_rcls), SVAL(buf,smb_err));
+}
+
+/*
+ * Compatibility helper for the sync APIs: Fake NTSTATUS in cli->inbuf
+ */
+
+void cli_set_error(struct cli_state *cli, NTSTATUS status)
+{
+       uint32_t flags2 = SVAL(cli->inbuf, smb_flg2);
+
+       if (NT_STATUS_IS_DOS(status)) {
+               SSVAL(cli->inbuf, smb_flg2,
+                     flags2 & ~FLAGS2_32_BIT_ERROR_CODES);
+               SCVAL(cli->inbuf, smb_rcls, NT_STATUS_DOS_CLASS(status));
+               SSVAL(cli->inbuf, smb_err, NT_STATUS_DOS_CODE(status));
+               return;
+       }
+
+       SSVAL(cli->inbuf, smb_flg2, flags2 | FLAGS2_32_BIT_ERROR_CODES);
+       SIVAL(cli->inbuf, smb_rcls, NT_STATUS_V(status));
+       return;
+}
+
+/*
+ * Allocate a new mid
+ */
+
+static uint16_t cli_new_mid(struct cli_state *cli)
+{
+       uint16_t result;
+       struct cli_request *req;
+
+       while (true) {
+               result = cli->mid++;
+               if (result == 0) {
+                       continue;
+               }
+
+               for (req = cli->outstanding_requests; req; req = req->next) {
+                       if (result == req->mid) {
+                               break;
+                       }
+               }
+
+               if (req == NULL) {
+                       return result;
+               }
+       }
+}
+
+static char *cli_request_print(TALLOC_CTX *mem_ctx, struct async_req *req)
+{
+       char *result = async_req_print(mem_ctx, req);
+       struct cli_request *cli_req = cli_request_get(req);
+
+       if (result == NULL) {
+               return NULL;
+       }
+
+       return talloc_asprintf_append_buffer(
+               result, "mid=%d\n", cli_req->mid);
+}
+
+static int cli_request_destructor(struct cli_request *req)
+{
+       if (req->enc_state != NULL) {
+               common_free_enc_buffer(req->enc_state, req->outbuf);
+       }
+       DLIST_REMOVE(req->cli->outstanding_requests, req);
+       return 0;
+}


-- 
Samba Shared Repository

Reply via email to