2 Unix SMB/CIFS implementation.
3 client file read/write routines
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "../lib/util/tevent_ntstatus.h"
22 #include "async_smb.h"
25 /****************************************************************************
26 Calculate the recommended read buffer size
27 ****************************************************************************/
28 static size_t cli_read_max_bufsize(struct cli_state *cli)
30 if (!client_is_signing_on(cli) && !cli_encryption_on(cli)
31 && (cli->server_posix_capabilities & CIFS_UNIX_LARGE_READ_CAP)) {
32 return CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE;
34 if (cli->capabilities & CAP_LARGE_READX) {
36 ? CLI_SAMBA_MAX_LARGE_READX_SIZE
37 : CLI_WINDOWS_MAX_LARGE_READX_SIZE;
39 return (cli->max_xmit - (smb_size+32)) & ~1023;
42 /****************************************************************************
43 Calculate the recommended write buffer size
44 ****************************************************************************/
45 static size_t cli_write_max_bufsize(struct cli_state *cli, uint16_t write_mode)
47 if (write_mode == 0 &&
48 !client_is_signing_on(cli) &&
49 !cli_encryption_on(cli) &&
50 (cli->server_posix_capabilities & CIFS_UNIX_LARGE_WRITE_CAP) &&
51 (cli->capabilities & CAP_LARGE_FILES)) {
52 /* Only do massive writes if we can do them direct
53 * with no signing or encrypting - not on a pipe. */
54 return CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE;
58 return CLI_SAMBA_MAX_LARGE_WRITEX_SIZE;
61 if (((cli->capabilities & CAP_LARGE_WRITEX) == 0)
62 || client_is_signing_on(cli)
63 || strequal(cli->dev, "LPT1:")) {
66 * Printer devices are restricted to max_xmit writesize in
67 * Vista and XPSP3 as are signing connections.
70 return (cli->max_xmit - (smb_size+32)) & ~1023;
73 return CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE;
76 struct cli_read_andx_state {
84 static void cli_read_andx_done(struct tevent_req *subreq);
86 struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
87 struct event_context *ev,
88 struct cli_state *cli, uint16_t fnum,
89 off_t offset, size_t size,
90 struct tevent_req **psmbreq)
92 struct tevent_req *req, *subreq;
93 struct cli_read_andx_state *state;
96 if (size > cli_read_max_bufsize(cli)) {
97 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
98 "size=%d\n", (int)size,
99 (int)cli_read_max_bufsize(cli)));
103 req = tevent_req_create(mem_ctx, &state, struct cli_read_andx_state);
109 SCVAL(state->vwv + 0, 0, 0xFF);
110 SCVAL(state->vwv + 0, 1, 0);
111 SSVAL(state->vwv + 1, 0, 0);
112 SSVAL(state->vwv + 2, 0, fnum);
113 SIVAL(state->vwv + 3, 0, offset);
114 SSVAL(state->vwv + 5, 0, size);
115 SSVAL(state->vwv + 6, 0, size);
116 SSVAL(state->vwv + 7, 0, (size >> 16));
117 SSVAL(state->vwv + 8, 0, 0);
118 SSVAL(state->vwv + 9, 0, 0);
120 if ((uint64_t)offset >> 32) {
121 SIVAL(state->vwv + 10, 0,
122 (((uint64_t)offset)>>32) & 0xffffffff);
126 subreq = cli_smb_req_create(state, ev, cli, SMBreadX, 0, wct,
127 state->vwv, 0, NULL);
128 if (subreq == NULL) {
132 tevent_req_set_callback(subreq, cli_read_andx_done, req);
137 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
138 struct event_context *ev,
139 struct cli_state *cli, uint16_t fnum,
140 off_t offset, size_t size)
142 struct tevent_req *req, *subreq;
145 req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
151 status = cli_smb_req_send(subreq);
152 if (tevent_req_nterror(req, status)) {
153 return tevent_req_post(req, ev);
158 static void cli_read_andx_done(struct tevent_req *subreq)
160 struct tevent_req *req = tevent_req_callback_data(
161 subreq, struct tevent_req);
162 struct cli_read_andx_state *state = tevent_req_data(
163 req, struct cli_read_andx_state);
170 state->status = cli_smb_recv(subreq, state, &inbuf, 12, &wct, &vwv,
173 if (NT_STATUS_IS_ERR(state->status)) {
174 tevent_req_nterror(req, state->status);
178 /* size is the number of bytes the server returned.
180 state->received = SVAL(vwv + 5, 0);
181 state->received |= (((unsigned int)SVAL(vwv + 7, 0)) << 16);
183 if (state->received > state->size) {
184 DEBUG(5,("server returned more than we wanted!\n"));
185 tevent_req_nterror(req, NT_STATUS_UNEXPECTED_IO_ERROR);
190 * bcc field must be valid for small reads, for large reads the 16-bit
191 * bcc field can't be correct.
194 if ((state->received < 0xffff) && (state->received > num_bytes)) {
195 DEBUG(5, ("server announced more bytes than sent\n"));
196 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
200 state->buf = discard_const_p(uint8_t, smb_base(inbuf)) + SVAL(vwv+6, 0);
202 if (trans_oob(smb_len(inbuf), SVAL(vwv+6, 0), state->received)
203 || ((state->received != 0) && (state->buf < bytes))) {
204 DEBUG(5, ("server returned invalid read&x data offset\n"));
205 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
208 tevent_req_done(req);
212 * Pull the data out of a finished async read_and_x request. rcvbuf is
213 * talloced from the request, so better make sure that you copy it away before
214 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
218 NTSTATUS cli_read_andx_recv(struct tevent_req *req, ssize_t *received,
221 struct cli_read_andx_state *state = tevent_req_data(
222 req, struct cli_read_andx_state);
225 if (tevent_req_is_nterror(req, &status)) {
228 *received = state->received;
229 *rcvbuf = state->buf;
233 struct cli_readall_state {
234 struct tevent_context *ev;
235 struct cli_state *cli;
243 static void cli_readall_done(struct tevent_req *subreq);
245 static struct tevent_req *cli_readall_send(TALLOC_CTX *mem_ctx,
246 struct event_context *ev,
247 struct cli_state *cli,
249 off_t offset, size_t size)
251 struct tevent_req *req, *subreq;
252 struct cli_readall_state *state;
254 req = tevent_req_create(mem_ctx, &state, struct cli_readall_state);
261 state->start_offset = offset;
266 subreq = cli_read_andx_send(state, ev, cli, fnum, offset, size);
267 if (tevent_req_nomem(subreq, req)) {
268 return tevent_req_post(req, ev);
270 tevent_req_set_callback(subreq, cli_readall_done, req);
274 static void cli_readall_done(struct tevent_req *subreq)
276 struct tevent_req *req = tevent_req_callback_data(
277 subreq, struct tevent_req);
278 struct cli_readall_state *state = tevent_req_data(
279 req, struct cli_readall_state);
284 status = cli_read_andx_recv(subreq, &received, &buf);
285 if (tevent_req_nterror(req, status)) {
291 tevent_req_done(req);
295 if ((state->received == 0) && (received == state->size)) {
296 /* Ideal case: Got it all in one run */
298 state->received += received;
299 tevent_req_done(req);
304 * We got a short read, issue a read for the
305 * rest. Unfortunately we have to allocate the buffer
306 * ourselves now, as our caller expects to receive a single
307 * buffer. cli_read_andx does it from the buffer received from
308 * the net, but with a short read we have to put it together
309 * from several reads.
312 if (state->buf == NULL) {
313 state->buf = talloc_array(state, uint8_t, state->size);
314 if (tevent_req_nomem(state->buf, req)) {
318 memcpy(state->buf + state->received, buf, received);
319 state->received += received;
323 if (state->received >= state->size) {
324 tevent_req_done(req);
328 subreq = cli_read_andx_send(state, state->ev, state->cli, state->fnum,
329 state->start_offset + state->received,
330 state->size - state->received);
331 if (tevent_req_nomem(subreq, req)) {
334 tevent_req_set_callback(subreq, cli_readall_done, req);
337 static NTSTATUS cli_readall_recv(struct tevent_req *req, ssize_t *received,
340 struct cli_readall_state *state = tevent_req_data(
341 req, struct cli_readall_state);
344 if (tevent_req_is_nterror(req, &status)) {
347 *received = state->received;
348 *rcvbuf = state->buf;
352 struct cli_pull_subreq {
353 struct tevent_req *req;
359 * Parallel read support.
361 * cli_pull sends as many read&x requests as the server would allow via
362 * max_mux at a time. When replies flow back in, the data is written into
363 * the callback function "sink" in the right order.
366 struct cli_pull_state {
367 struct tevent_req *req;
369 struct event_context *ev;
370 struct cli_state *cli;
375 NTSTATUS (*sink)(char *buf, size_t n, void *priv);
381 * Outstanding requests
384 struct cli_pull_subreq *reqs;
387 * For how many bytes did we send requests already?
392 * Next request index to push into "sink". This walks around the "req"
393 * array, taking care that the requests are pushed to "sink" in the
394 * right order. If necessary (i.e. replies don't come in in the right
395 * order), replies are held back in "reqs".
400 * How many bytes did we push into "sink"?
406 static char *cli_pull_print(struct tevent_req *req, TALLOC_CTX *mem_ctx)
408 struct cli_pull_state *state = tevent_req_data(
409 req, struct cli_pull_state);
412 result = tevent_req_default_print(req, mem_ctx);
413 if (result == NULL) {
417 return talloc_asprintf_append_buffer(
418 result, "num_reqs=%d, top_req=%d",
419 state->num_reqs, state->top_req);
422 static void cli_pull_read_done(struct tevent_req *read_req);
425 * Prepare an async pull request
428 struct tevent_req *cli_pull_send(TALLOC_CTX *mem_ctx,
429 struct event_context *ev,
430 struct cli_state *cli,
431 uint16_t fnum, off_t start_offset,
432 SMB_OFF_T size, size_t window_size,
433 NTSTATUS (*sink)(char *buf, size_t n,
437 struct tevent_req *req;
438 struct cli_pull_state *state;
441 req = tevent_req_create(mem_ctx, &state, struct cli_pull_state);
445 tevent_req_set_print_fn(req, cli_pull_print);
451 state->start_offset = start_offset;
460 tevent_req_done(req);
461 return tevent_req_post(req, ev);
464 state->chunk_size = cli_read_max_bufsize(cli);
466 state->num_reqs = MAX(window_size/state->chunk_size, 1);
467 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
469 state->reqs = TALLOC_ZERO_ARRAY(state, struct cli_pull_subreq,
471 if (state->reqs == NULL) {
475 state->requested = 0;
477 for (i=0; i<state->num_reqs; i++) {
478 struct cli_pull_subreq *subreq = &state->reqs[i];
480 size_t request_thistime;
482 if (state->requested >= size) {
487 size_left = size - state->requested;
488 request_thistime = MIN(size_left, state->chunk_size);
490 subreq->req = cli_readall_send(
491 state->reqs, ev, cli, fnum,
492 state->start_offset + state->requested,
495 if (subreq->req == NULL) {
498 tevent_req_set_callback(subreq->req, cli_pull_read_done, req);
499 state->requested += request_thistime;
509 * Handle incoming read replies, push the data into sink and send out new
510 * requests if necessary.
513 static void cli_pull_read_done(struct tevent_req *subreq)
515 struct tevent_req *req = tevent_req_callback_data(
516 subreq, struct tevent_req);
517 struct cli_pull_state *state = tevent_req_data(
518 req, struct cli_pull_state);
519 struct cli_pull_subreq *pull_subreq = NULL;
523 for (i = 0; i < state->num_reqs; i++) {
524 pull_subreq = &state->reqs[i];
525 if (subreq == pull_subreq->req) {
529 if (i == state->num_reqs) {
530 /* Huh -- received something we did not send?? */
531 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
535 status = cli_readall_recv(subreq, &pull_subreq->received,
537 if (!NT_STATUS_IS_OK(status)) {
538 tevent_req_nterror(state->req, status);
543 * This loop is the one to take care of out-of-order replies. All
544 * pending requests are in state->reqs, state->reqs[top_req] is the
545 * one that is to be pushed next. If however a request later than
546 * top_req is replied to, then we can't push yet. If top_req is
547 * replied to at a later point then, we need to push all the finished
551 while (state->reqs[state->top_req].req != NULL) {
552 struct cli_pull_subreq *top_subreq;
554 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
557 top_subreq = &state->reqs[state->top_req];
559 if (tevent_req_is_in_progress(top_subreq->req)) {
560 DEBUG(11, ("cli_pull_read_done: top request not yet "
565 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
566 "pushed\n", (int)top_subreq->received,
567 (int)state->pushed));
569 status = state->sink((char *)top_subreq->buf,
570 top_subreq->received, state->priv);
571 if (tevent_req_nterror(state->req, status)) {
574 state->pushed += top_subreq->received;
576 TALLOC_FREE(state->reqs[state->top_req].req);
578 if (state->requested < state->size) {
579 struct tevent_req *new_req;
581 size_t request_thistime;
583 size_left = state->size - state->requested;
584 request_thistime = MIN(size_left, state->chunk_size);
586 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
587 "at %d, position %d\n",
588 (int)request_thistime,
589 (int)(state->start_offset
593 new_req = cli_readall_send(
594 state->reqs, state->ev, state->cli,
596 state->start_offset + state->requested,
599 if (tevent_req_nomem(new_req, state->req)) {
602 tevent_req_set_callback(new_req, cli_pull_read_done,
605 state->reqs[state->top_req].req = new_req;
606 state->requested += request_thistime;
609 state->top_req = (state->top_req+1) % state->num_reqs;
612 tevent_req_done(req);
615 NTSTATUS cli_pull_recv(struct tevent_req *req, SMB_OFF_T *received)
617 struct cli_pull_state *state = tevent_req_data(
618 req, struct cli_pull_state);
621 if (tevent_req_is_nterror(req, &status)) {
624 *received = state->pushed;
628 NTSTATUS cli_pull(struct cli_state *cli, uint16_t fnum,
629 off_t start_offset, SMB_OFF_T size, size_t window_size,
630 NTSTATUS (*sink)(char *buf, size_t n, void *priv),
631 void *priv, SMB_OFF_T *received)
633 TALLOC_CTX *frame = talloc_stackframe();
634 struct event_context *ev;
635 struct tevent_req *req;
636 NTSTATUS status = NT_STATUS_OK;
638 if (cli_has_async_calls(cli)) {
640 * Can't use sync call while an async call is in flight
642 status = NT_STATUS_INVALID_PARAMETER;
646 ev = event_context_init(frame);
648 status = NT_STATUS_NO_MEMORY;
652 req = cli_pull_send(frame, ev, cli, fnum, start_offset, size,
653 window_size, sink, priv);
655 status = NT_STATUS_NO_MEMORY;
659 if (!tevent_req_poll(req, ev)) {
660 status = map_nt_error_from_unix(errno);
664 status = cli_pull_recv(req, received);
667 if (!NT_STATUS_IS_OK(status)) {
668 cli_set_error(cli, status);
673 static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
675 char **pbuf = (char **)priv;
676 memcpy(*pbuf, buf, n);
681 ssize_t cli_read(struct cli_state *cli, uint16_t fnum, char *buf,
682 off_t offset, size_t size)
687 status = cli_pull(cli, fnum, offset, size, size,
688 cli_read_sink, &buf, &ret);
689 if (!NT_STATUS_IS_OK(status)) {
690 cli_set_error(cli, status);
696 /****************************************************************************
697 write to a file using a SMBwrite and not bypassing 0 byte writes
698 ****************************************************************************/
700 NTSTATUS cli_smbwrite(struct cli_state *cli, uint16_t fnum, char *buf,
701 off_t offset, size_t size1, size_t *ptotal)
710 bytes = TALLOC_ARRAY(talloc_tos(), uint8_t, 3);
712 return NT_STATUS_NO_MEMORY;
717 size_t size = MIN(size1, cli->max_xmit - 48);
718 struct tevent_req *req;
723 SSVAL(vwv+0, 0, fnum);
724 SSVAL(vwv+1, 0, size);
725 SIVAL(vwv+2, 0, offset);
728 bytes = TALLOC_REALLOC_ARRAY(talloc_tos(), bytes, uint8_t,
731 return NT_STATUS_NO_MEMORY;
733 SSVAL(bytes, 1, size);
734 memcpy(bytes + 3, buf + total, size);
736 status = cli_smb(talloc_tos(), cli, SMBwrite, 0, 5, vwv,
737 size+3, bytes, &req, 1, NULL, &ret_vwv,
739 if (!NT_STATUS_IS_OK(status)) {
744 size = SVAL(ret_vwv+0, 0);
757 if (ptotal != NULL) {
764 * Send a write&x request
767 struct cli_write_andx_state {
775 static void cli_write_andx_done(struct tevent_req *subreq);
777 struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
778 struct event_context *ev,
779 struct cli_state *cli, uint16_t fnum,
780 uint16_t mode, const uint8_t *buf,
781 off_t offset, size_t size,
782 struct tevent_req **reqs_before,
784 struct tevent_req **psmbreq)
786 struct tevent_req *req, *subreq;
787 struct cli_write_andx_state *state;
788 bool bigoffset = ((cli->capabilities & CAP_LARGE_FILES) != 0);
789 uint8_t wct = bigoffset ? 14 : 12;
790 size_t max_write = cli_write_max_bufsize(cli, mode);
793 req = tevent_req_create(mem_ctx, &state, struct cli_write_andx_state);
798 size = MIN(size, max_write);
802 SCVAL(vwv+0, 0, 0xFF);
805 SSVAL(vwv+2, 0, fnum);
806 SIVAL(vwv+3, 0, offset);
808 SSVAL(vwv+7, 0, mode);
810 SSVAL(vwv+9, 0, (size>>16));
811 SSVAL(vwv+10, 0, size);
814 cli_smb_wct_ofs(reqs_before, num_reqs_before)
815 + 1 /* the wct field */
817 + 2 /* num_bytes field */
821 SIVAL(vwv+12, 0, (((uint64_t)offset)>>32) & 0xffffffff);
825 state->iov[0].iov_base = (void *)&state->pad;
826 state->iov[0].iov_len = 1;
827 state->iov[1].iov_base = discard_const_p(void, buf);
828 state->iov[1].iov_len = size;
830 subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
832 if (tevent_req_nomem(subreq, req)) {
833 return tevent_req_post(req, ev);
835 tevent_req_set_callback(subreq, cli_write_andx_done, req);
840 struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
841 struct event_context *ev,
842 struct cli_state *cli, uint16_t fnum,
843 uint16_t mode, const uint8_t *buf,
844 off_t offset, size_t size)
846 struct tevent_req *req, *subreq;
849 req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
850 size, NULL, 0, &subreq);
855 status = cli_smb_req_send(subreq);
856 if (tevent_req_nterror(req, status)) {
857 return tevent_req_post(req, ev);
862 static void cli_write_andx_done(struct tevent_req *subreq)
864 struct tevent_req *req = tevent_req_callback_data(
865 subreq, struct tevent_req);
866 struct cli_write_andx_state *state = tevent_req_data(
867 req, struct cli_write_andx_state);
873 status = cli_smb_recv(subreq, state, &inbuf, 6, &wct, &vwv,
876 if (NT_STATUS_IS_ERR(status)) {
877 tevent_req_nterror(req, status);
880 state->written = SVAL(vwv+2, 0);
881 state->written |= SVAL(vwv+4, 0)<<16;
882 tevent_req_done(req);
885 NTSTATUS cli_write_andx_recv(struct tevent_req *req, size_t *pwritten)
887 struct cli_write_andx_state *state = tevent_req_data(
888 req, struct cli_write_andx_state);
891 if (tevent_req_is_nterror(req, &status)) {
894 *pwritten = state->written;
898 struct cli_writeall_state {
899 struct event_context *ev;
900 struct cli_state *cli;
909 static void cli_writeall_written(struct tevent_req *req);
911 static struct tevent_req *cli_writeall_send(TALLOC_CTX *mem_ctx,
912 struct event_context *ev,
913 struct cli_state *cli,
917 off_t offset, size_t size)
919 struct tevent_req *req, *subreq;
920 struct cli_writeall_state *state;
922 req = tevent_req_create(mem_ctx, &state, struct cli_writeall_state);
931 state->offset = offset;
935 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
936 state->mode, state->buf, state->offset,
938 if (tevent_req_nomem(subreq, req)) {
939 return tevent_req_post(req, ev);
941 tevent_req_set_callback(subreq, cli_writeall_written, req);
945 static void cli_writeall_written(struct tevent_req *subreq)
947 struct tevent_req *req = tevent_req_callback_data(
948 subreq, struct tevent_req);
949 struct cli_writeall_state *state = tevent_req_data(
950 req, struct cli_writeall_state);
952 size_t written, to_write;
954 status = cli_write_andx_recv(subreq, &written);
956 if (tevent_req_nterror(req, status)) {
960 state->written += written;
962 if (state->written > state->size) {
963 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
967 to_write = state->size - state->written;
970 tevent_req_done(req);
974 subreq = cli_write_andx_send(state, state->ev, state->cli, state->fnum,
976 state->buf + state->written,
977 state->offset + state->written, to_write);
978 if (tevent_req_nomem(subreq, req)) {
981 tevent_req_set_callback(subreq, cli_writeall_written, req);
984 static NTSTATUS cli_writeall_recv(struct tevent_req *req,
987 struct cli_writeall_state *state = tevent_req_data(
988 req, struct cli_writeall_state);
991 if (tevent_req_is_nterror(req, &status)) {
994 if (pwritten != NULL) {
995 *pwritten = state->written;
1000 NTSTATUS cli_writeall(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1001 const uint8_t *buf, off_t offset, size_t size,
1004 TALLOC_CTX *frame = talloc_stackframe();
1005 struct event_context *ev;
1006 struct tevent_req *req;
1007 NTSTATUS status = NT_STATUS_NO_MEMORY;
1009 if (cli_has_async_calls(cli)) {
1011 * Can't use sync call while an async call is in flight
1013 status = NT_STATUS_INVALID_PARAMETER;
1016 ev = event_context_init(frame);
1020 req = cli_writeall_send(frame, ev, cli, fnum, mode, buf, offset, size);
1024 if (!tevent_req_poll(req, ev)) {
1025 status = map_nt_error_from_unix(errno);
1028 status = cli_writeall_recv(req, pwritten);
1031 if (!NT_STATUS_IS_OK(status)) {
1032 cli_set_error(cli, status);
1037 struct cli_push_write_state {
1038 struct tevent_req *req;/* This is the main request! Not the subreq */
1045 struct cli_push_state {
1046 struct event_context *ev;
1047 struct cli_state *cli;
1053 size_t (*source)(uint8_t *buf, size_t n, void *priv);
1062 * Outstanding requests
1066 struct cli_push_write_state **reqs;
1069 static void cli_push_written(struct tevent_req *req);
1071 static bool cli_push_write_setup(struct tevent_req *req,
1072 struct cli_push_state *state,
1075 struct cli_push_write_state *substate;
1076 struct tevent_req *subreq;
1078 substate = talloc(state->reqs, struct cli_push_write_state);
1082 substate->req = req;
1083 substate->idx = idx;
1084 substate->ofs = state->next_offset;
1085 substate->buf = talloc_array(substate, uint8_t, state->chunk_size);
1086 if (!substate->buf) {
1087 talloc_free(substate);
1090 substate->size = state->source(substate->buf,
1093 if (substate->size == 0) {
1095 /* nothing to send */
1096 talloc_free(substate);
1100 subreq = cli_writeall_send(substate,
1101 state->ev, state->cli,
1102 state->fnum, state->mode,
1107 talloc_free(substate);
1110 tevent_req_set_callback(subreq, cli_push_written, substate);
1112 state->reqs[idx] = substate;
1113 state->pending += 1;
1114 state->next_offset += substate->size;
1119 struct tevent_req *cli_push_send(TALLOC_CTX *mem_ctx, struct event_context *ev,
1120 struct cli_state *cli,
1121 uint16_t fnum, uint16_t mode,
1122 off_t start_offset, size_t window_size,
1123 size_t (*source)(uint8_t *buf, size_t n,
1127 struct tevent_req *req;
1128 struct cli_push_state *state;
1131 req = tevent_req_create(mem_ctx, &state, struct cli_push_state);
1138 state->start_offset = start_offset;
1140 state->source = source;
1144 state->next_offset = start_offset;
1146 state->chunk_size = cli_write_max_bufsize(cli, mode);
1148 if (window_size == 0) {
1149 window_size = cli->max_mux * state->chunk_size;
1151 state->num_reqs = window_size/state->chunk_size;
1152 if ((window_size % state->chunk_size) > 0) {
1153 state->num_reqs += 1;
1155 state->num_reqs = MIN(state->num_reqs, cli->max_mux);
1156 state->num_reqs = MAX(state->num_reqs, 1);
1158 state->reqs = TALLOC_ZERO_ARRAY(state, struct cli_push_write_state *,
1160 if (state->reqs == NULL) {
1164 for (i=0; i<state->num_reqs; i++) {
1165 if (!cli_push_write_setup(req, state, i)) {
1174 if (state->pending == 0) {
1175 tevent_req_done(req);
1176 return tevent_req_post(req, ev);
1182 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1183 return tevent_req_post(req, ev);
1186 static void cli_push_written(struct tevent_req *subreq)
1188 struct cli_push_write_state *substate = tevent_req_callback_data(
1189 subreq, struct cli_push_write_state);
1190 struct tevent_req *req = substate->req;
1191 struct cli_push_state *state = tevent_req_data(
1192 req, struct cli_push_state);
1194 uint32_t idx = substate->idx;
1196 state->reqs[idx] = NULL;
1197 state->pending -= 1;
1199 status = cli_writeall_recv(subreq, NULL);
1200 TALLOC_FREE(subreq);
1201 TALLOC_FREE(substate);
1202 if (tevent_req_nterror(req, status)) {
1207 if (!cli_push_write_setup(req, state, idx)) {
1208 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
1213 if (state->pending == 0) {
1214 tevent_req_done(req);
1219 NTSTATUS cli_push_recv(struct tevent_req *req)
1221 return tevent_req_simple_recv_ntstatus(req);
1224 NTSTATUS cli_push(struct cli_state *cli, uint16_t fnum, uint16_t mode,
1225 off_t start_offset, size_t window_size,
1226 size_t (*source)(uint8_t *buf, size_t n, void *priv),
1229 TALLOC_CTX *frame = talloc_stackframe();
1230 struct event_context *ev;
1231 struct tevent_req *req;
1232 NTSTATUS status = NT_STATUS_OK;
1234 if (cli_has_async_calls(cli)) {
1236 * Can't use sync call while an async call is in flight
1238 status = NT_STATUS_INVALID_PARAMETER;
1242 ev = event_context_init(frame);
1244 status = NT_STATUS_NO_MEMORY;
1248 req = cli_push_send(frame, ev, cli, fnum, mode, start_offset,
1249 window_size, source, priv);
1251 status = NT_STATUS_NO_MEMORY;
1255 if (!tevent_req_poll(req, ev)) {
1256 status = map_nt_error_from_unix(errno);
1260 status = cli_push_recv(req);
1263 if (!NT_STATUS_IS_OK(status)) {
1264 cli_set_error(cli, status);