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 "libsmb/libsmb.h"
22 #include "../lib/util/tevent_ntstatus.h"
23 #include "async_smb.h"
26 /****************************************************************************
27 Calculate the recommended read buffer size
28 ****************************************************************************/
29 static size_t cli_read_max_bufsize(struct cli_state
*cli
)
31 size_t data_offset
= smb_size
- 4;
36 if (!client_is_signing_on(cli
) && !cli_encryption_on(cli
)
37 && (cli
->server_posix_capabilities
& CIFS_UNIX_LARGE_READ_CAP
)) {
38 return CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE
;
40 if (cli
->capabilities
& CAP_LARGE_READX
) {
42 ? CLI_SAMBA_MAX_LARGE_READX_SIZE
43 : CLI_WINDOWS_MAX_LARGE_READX_SIZE
;
46 data_offset
+= wct
* sizeof(uint16_t);
47 data_offset
+= 1; /* pad */
49 useable_space
= cli
->max_xmit
- data_offset
;
54 /****************************************************************************
55 Calculate the recommended write buffer size
56 ****************************************************************************/
57 static size_t cli_write_max_bufsize(struct cli_state
*cli
,
61 if (write_mode
== 0 &&
62 !client_is_signing_on(cli
) &&
63 !cli_encryption_on(cli
) &&
64 (cli
->server_posix_capabilities
& CIFS_UNIX_LARGE_WRITE_CAP
) &&
65 (cli
->capabilities
& CAP_LARGE_FILES
)) {
66 /* Only do massive writes if we can do them direct
67 * with no signing or encrypting - not on a pipe. */
68 return CLI_SAMBA_MAX_POSIX_LARGE_WRITEX_SIZE
;
72 return CLI_SAMBA_MAX_LARGE_WRITEX_SIZE
;
75 if (((cli
->capabilities
& CAP_LARGE_WRITEX
) == 0)
76 || client_is_signing_on(cli
)
77 || strequal(cli
->dev
, "LPT1:")) {
78 size_t data_offset
= smb_size
- 4;
81 data_offset
+= wct
* sizeof(uint16_t);
82 data_offset
+= 1; /* pad */
84 useable_space
= cli
->max_xmit
- data_offset
;
89 return CLI_WINDOWS_MAX_LARGE_WRITEX_SIZE
;
92 struct cli_read_andx_state
{
100 static void cli_read_andx_done(struct tevent_req
*subreq
);
102 struct tevent_req
*cli_read_andx_create(TALLOC_CTX
*mem_ctx
,
103 struct event_context
*ev
,
104 struct cli_state
*cli
, uint16_t fnum
,
105 off_t offset
, size_t size
,
106 struct tevent_req
**psmbreq
)
108 struct tevent_req
*req
, *subreq
;
109 struct cli_read_andx_state
*state
;
112 if (size
> cli_read_max_bufsize(cli
)) {
113 DEBUG(0, ("cli_read_andx_send got size=%d, can only handle "
114 "size=%d\n", (int)size
,
115 (int)cli_read_max_bufsize(cli
)));
119 req
= tevent_req_create(mem_ctx
, &state
, struct cli_read_andx_state
);
125 SCVAL(state
->vwv
+ 0, 0, 0xFF);
126 SCVAL(state
->vwv
+ 0, 1, 0);
127 SSVAL(state
->vwv
+ 1, 0, 0);
128 SSVAL(state
->vwv
+ 2, 0, fnum
);
129 SIVAL(state
->vwv
+ 3, 0, offset
);
130 SSVAL(state
->vwv
+ 5, 0, size
);
131 SSVAL(state
->vwv
+ 6, 0, size
);
132 SSVAL(state
->vwv
+ 7, 0, (size
>> 16));
133 SSVAL(state
->vwv
+ 8, 0, 0);
134 SSVAL(state
->vwv
+ 9, 0, 0);
136 if (cli
->capabilities
& CAP_LARGE_FILES
) {
137 SIVAL(state
->vwv
+ 10, 0,
138 (((uint64_t)offset
)>>32) & 0xffffffff);
141 if ((((uint64_t)offset
) & 0xffffffff00000000LL
) != 0) {
142 DEBUG(10, ("cli_read_andx_send got large offset where "
143 "the server does not support it\n"));
144 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
145 return tevent_req_post(req
, ev
);
149 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBreadX
, 0, wct
,
150 state
->vwv
, 0, NULL
);
151 if (subreq
== NULL
) {
155 tevent_req_set_callback(subreq
, cli_read_andx_done
, req
);
160 struct tevent_req
*cli_read_andx_send(TALLOC_CTX
*mem_ctx
,
161 struct event_context
*ev
,
162 struct cli_state
*cli
, uint16_t fnum
,
163 off_t offset
, size_t size
)
165 struct tevent_req
*req
, *subreq
;
168 req
= cli_read_andx_create(mem_ctx
, ev
, cli
, fnum
, offset
, size
,
174 status
= cli_smb_req_send(subreq
);
175 if (tevent_req_nterror(req
, status
)) {
176 return tevent_req_post(req
, ev
);
181 static void cli_read_andx_done(struct tevent_req
*subreq
)
183 struct tevent_req
*req
= tevent_req_callback_data(
184 subreq
, struct tevent_req
);
185 struct cli_read_andx_state
*state
= tevent_req_data(
186 req
, struct cli_read_andx_state
);
193 state
->status
= cli_smb_recv(subreq
, state
, &inbuf
, 12, &wct
, &vwv
,
196 if (NT_STATUS_IS_ERR(state
->status
)) {
197 tevent_req_nterror(req
, state
->status
);
201 /* size is the number of bytes the server returned.
203 state
->received
= SVAL(vwv
+ 5, 0);
204 state
->received
|= (((unsigned int)SVAL(vwv
+ 7, 0)) << 16);
206 if (state
->received
> state
->size
) {
207 DEBUG(5,("server returned more than we wanted!\n"));
208 tevent_req_nterror(req
, NT_STATUS_UNEXPECTED_IO_ERROR
);
213 * bcc field must be valid for small reads, for large reads the 16-bit
214 * bcc field can't be correct.
217 if ((state
->received
< 0xffff) && (state
->received
> num_bytes
)) {
218 DEBUG(5, ("server announced more bytes than sent\n"));
219 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
223 state
->buf
= (uint8_t *)smb_base(inbuf
) + SVAL(vwv
+6, 0);
225 if (trans_oob(smb_len_large(inbuf
), SVAL(vwv
+6, 0), state
->received
)
226 || ((state
->received
!= 0) && (state
->buf
< bytes
))) {
227 DEBUG(5, ("server returned invalid read&x data offset\n"));
228 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
231 tevent_req_done(req
);
235 * Pull the data out of a finished async read_and_x request. rcvbuf is
236 * talloced from the request, so better make sure that you copy it away before
237 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
241 NTSTATUS
cli_read_andx_recv(struct tevent_req
*req
, ssize_t
*received
,
244 struct cli_read_andx_state
*state
= tevent_req_data(
245 req
, struct cli_read_andx_state
);
248 if (tevent_req_is_nterror(req
, &status
)) {
251 *received
= state
->received
;
252 *rcvbuf
= state
->buf
;
256 struct cli_readall_state
{
257 struct tevent_context
*ev
;
258 struct cli_state
*cli
;
266 static void cli_readall_done(struct tevent_req
*subreq
);
268 static struct tevent_req
*cli_readall_send(TALLOC_CTX
*mem_ctx
,
269 struct event_context
*ev
,
270 struct cli_state
*cli
,
272 off_t offset
, size_t size
)
274 struct tevent_req
*req
, *subreq
;
275 struct cli_readall_state
*state
;
277 req
= tevent_req_create(mem_ctx
, &state
, struct cli_readall_state
);
284 state
->start_offset
= offset
;
289 subreq
= cli_read_andx_send(state
, ev
, cli
, fnum
, offset
, size
);
290 if (tevent_req_nomem(subreq
, req
)) {
291 return tevent_req_post(req
, ev
);
293 tevent_req_set_callback(subreq
, cli_readall_done
, req
);
297 static void cli_readall_done(struct tevent_req
*subreq
)
299 struct tevent_req
*req
= tevent_req_callback_data(
300 subreq
, struct tevent_req
);
301 struct cli_readall_state
*state
= tevent_req_data(
302 req
, struct cli_readall_state
);
307 status
= cli_read_andx_recv(subreq
, &received
, &buf
);
308 if (tevent_req_nterror(req
, status
)) {
314 tevent_req_done(req
);
318 if ((state
->received
== 0) && (received
== state
->size
)) {
319 /* Ideal case: Got it all in one run */
321 state
->received
+= received
;
322 tevent_req_done(req
);
327 * We got a short read, issue a read for the
328 * rest. Unfortunately we have to allocate the buffer
329 * ourselves now, as our caller expects to receive a single
330 * buffer. cli_read_andx does it from the buffer received from
331 * the net, but with a short read we have to put it together
332 * from several reads.
335 if (state
->buf
== NULL
) {
336 state
->buf
= talloc_array(state
, uint8_t, state
->size
);
337 if (tevent_req_nomem(state
->buf
, req
)) {
341 memcpy(state
->buf
+ state
->received
, buf
, received
);
342 state
->received
+= received
;
346 if (state
->received
>= state
->size
) {
347 tevent_req_done(req
);
351 subreq
= cli_read_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
352 state
->start_offset
+ state
->received
,
353 state
->size
- state
->received
);
354 if (tevent_req_nomem(subreq
, req
)) {
357 tevent_req_set_callback(subreq
, cli_readall_done
, req
);
360 static NTSTATUS
cli_readall_recv(struct tevent_req
*req
, ssize_t
*received
,
363 struct cli_readall_state
*state
= tevent_req_data(
364 req
, struct cli_readall_state
);
367 if (tevent_req_is_nterror(req
, &status
)) {
370 *received
= state
->received
;
371 *rcvbuf
= state
->buf
;
375 struct cli_pull_subreq
{
376 struct tevent_req
*req
;
382 * Parallel read support.
384 * cli_pull sends as many read&x requests as the server would allow via
385 * max_mux at a time. When replies flow back in, the data is written into
386 * the callback function "sink" in the right order.
389 struct cli_pull_state
{
390 struct tevent_req
*req
;
392 struct event_context
*ev
;
393 struct cli_state
*cli
;
398 NTSTATUS (*sink
)(char *buf
, size_t n
, void *priv
);
404 * Outstanding requests
407 struct cli_pull_subreq
*reqs
;
410 * For how many bytes did we send requests already?
415 * Next request index to push into "sink". This walks around the "req"
416 * array, taking care that the requests are pushed to "sink" in the
417 * right order. If necessary (i.e. replies don't come in in the right
418 * order), replies are held back in "reqs".
423 * How many bytes did we push into "sink"?
429 static char *cli_pull_print(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
431 struct cli_pull_state
*state
= tevent_req_data(
432 req
, struct cli_pull_state
);
435 result
= tevent_req_default_print(req
, mem_ctx
);
436 if (result
== NULL
) {
440 return talloc_asprintf_append_buffer(
441 result
, "num_reqs=%d, top_req=%d",
442 state
->num_reqs
, state
->top_req
);
445 static void cli_pull_read_done(struct tevent_req
*read_req
);
448 * Prepare an async pull request
451 struct tevent_req
*cli_pull_send(TALLOC_CTX
*mem_ctx
,
452 struct event_context
*ev
,
453 struct cli_state
*cli
,
454 uint16_t fnum
, off_t start_offset
,
455 SMB_OFF_T size
, size_t window_size
,
456 NTSTATUS (*sink
)(char *buf
, size_t n
,
460 struct tevent_req
*req
;
461 struct cli_pull_state
*state
;
464 req
= tevent_req_create(mem_ctx
, &state
, struct cli_pull_state
);
468 tevent_req_set_print_fn(req
, cli_pull_print
);
474 state
->start_offset
= start_offset
;
483 tevent_req_done(req
);
484 return tevent_req_post(req
, ev
);
487 state
->chunk_size
= cli_read_max_bufsize(cli
);
489 state
->num_reqs
= MAX(window_size
/state
->chunk_size
, 1);
490 state
->num_reqs
= MIN(state
->num_reqs
, cli
->max_mux
);
492 state
->reqs
= TALLOC_ZERO_ARRAY(state
, struct cli_pull_subreq
,
494 if (state
->reqs
== NULL
) {
498 state
->requested
= 0;
500 for (i
=0; i
<state
->num_reqs
; i
++) {
501 struct cli_pull_subreq
*subreq
= &state
->reqs
[i
];
503 size_t request_thistime
;
505 if (state
->requested
>= size
) {
510 size_left
= size
- state
->requested
;
511 request_thistime
= MIN(size_left
, state
->chunk_size
);
513 subreq
->req
= cli_readall_send(
514 state
->reqs
, ev
, cli
, fnum
,
515 state
->start_offset
+ state
->requested
,
518 if (subreq
->req
== NULL
) {
521 tevent_req_set_callback(subreq
->req
, cli_pull_read_done
, req
);
522 state
->requested
+= request_thistime
;
532 * Handle incoming read replies, push the data into sink and send out new
533 * requests if necessary.
536 static void cli_pull_read_done(struct tevent_req
*subreq
)
538 struct tevent_req
*req
= tevent_req_callback_data(
539 subreq
, struct tevent_req
);
540 struct cli_pull_state
*state
= tevent_req_data(
541 req
, struct cli_pull_state
);
542 struct cli_pull_subreq
*pull_subreq
= NULL
;
546 for (i
= 0; i
< state
->num_reqs
; i
++) {
547 pull_subreq
= &state
->reqs
[i
];
548 if (subreq
== pull_subreq
->req
) {
552 if (i
== state
->num_reqs
) {
553 /* Huh -- received something we did not send?? */
554 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
558 status
= cli_readall_recv(subreq
, &pull_subreq
->received
,
560 if (!NT_STATUS_IS_OK(status
)) {
561 tevent_req_nterror(state
->req
, status
);
566 * This loop is the one to take care of out-of-order replies. All
567 * pending requests are in state->reqs, state->reqs[top_req] is the
568 * one that is to be pushed next. If however a request later than
569 * top_req is replied to, then we can't push yet. If top_req is
570 * replied to at a later point then, we need to push all the finished
574 while (state
->reqs
[state
->top_req
].req
!= NULL
) {
575 struct cli_pull_subreq
*top_subreq
;
577 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
580 top_subreq
= &state
->reqs
[state
->top_req
];
582 if (tevent_req_is_in_progress(top_subreq
->req
)) {
583 DEBUG(11, ("cli_pull_read_done: top request not yet "
588 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
589 "pushed\n", (int)top_subreq
->received
,
590 (int)state
->pushed
));
592 status
= state
->sink((char *)top_subreq
->buf
,
593 top_subreq
->received
, state
->priv
);
594 if (tevent_req_nterror(state
->req
, status
)) {
597 state
->pushed
+= top_subreq
->received
;
599 TALLOC_FREE(state
->reqs
[state
->top_req
].req
);
601 if (state
->requested
< state
->size
) {
602 struct tevent_req
*new_req
;
604 size_t request_thistime
;
606 size_left
= state
->size
- state
->requested
;
607 request_thistime
= MIN(size_left
, state
->chunk_size
);
609 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
610 "at %d, position %d\n",
611 (int)request_thistime
,
612 (int)(state
->start_offset
616 new_req
= cli_readall_send(
617 state
->reqs
, state
->ev
, state
->cli
,
619 state
->start_offset
+ state
->requested
,
622 if (tevent_req_nomem(new_req
, state
->req
)) {
625 tevent_req_set_callback(new_req
, cli_pull_read_done
,
628 state
->reqs
[state
->top_req
].req
= new_req
;
629 state
->requested
+= request_thistime
;
632 state
->top_req
= (state
->top_req
+1) % state
->num_reqs
;
635 tevent_req_done(req
);
638 NTSTATUS
cli_pull_recv(struct tevent_req
*req
, SMB_OFF_T
*received
)
640 struct cli_pull_state
*state
= tevent_req_data(
641 req
, struct cli_pull_state
);
644 if (tevent_req_is_nterror(req
, &status
)) {
647 *received
= state
->pushed
;
651 NTSTATUS
cli_pull(struct cli_state
*cli
, uint16_t fnum
,
652 off_t start_offset
, SMB_OFF_T size
, size_t window_size
,
653 NTSTATUS (*sink
)(char *buf
, size_t n
, void *priv
),
654 void *priv
, SMB_OFF_T
*received
)
656 TALLOC_CTX
*frame
= talloc_stackframe();
657 struct event_context
*ev
;
658 struct tevent_req
*req
;
659 NTSTATUS status
= NT_STATUS_OK
;
661 if (cli_has_async_calls(cli
)) {
663 * Can't use sync call while an async call is in flight
665 status
= NT_STATUS_INVALID_PARAMETER
;
669 ev
= event_context_init(frame
);
671 status
= NT_STATUS_NO_MEMORY
;
675 req
= cli_pull_send(frame
, ev
, cli
, fnum
, start_offset
, size
,
676 window_size
, sink
, priv
);
678 status
= NT_STATUS_NO_MEMORY
;
682 if (!tevent_req_poll(req
, ev
)) {
683 status
= map_nt_error_from_unix(errno
);
687 status
= cli_pull_recv(req
, received
);
693 static NTSTATUS
cli_read_sink(char *buf
, size_t n
, void *priv
)
695 char **pbuf
= (char **)priv
;
696 memcpy(*pbuf
, buf
, n
);
701 ssize_t
cli_read(struct cli_state
*cli
, uint16_t fnum
, char *buf
,
702 off_t offset
, size_t size
)
707 status
= cli_pull(cli
, fnum
, offset
, size
, size
,
708 cli_read_sink
, &buf
, &ret
);
709 if (!NT_STATUS_IS_OK(status
)) {
715 /****************************************************************************
716 write to a file using a SMBwrite and not bypassing 0 byte writes
717 ****************************************************************************/
719 NTSTATUS
cli_smbwrite(struct cli_state
*cli
, uint16_t fnum
, char *buf
,
720 off_t offset
, size_t size1
, size_t *ptotal
)
729 bytes
= TALLOC_ARRAY(talloc_tos(), uint8_t, 3);
731 return NT_STATUS_NO_MEMORY
;
736 size_t size
= MIN(size1
, cli
->max_xmit
- 48);
737 struct tevent_req
*req
;
742 SSVAL(vwv
+0, 0, fnum
);
743 SSVAL(vwv
+1, 0, size
);
744 SIVAL(vwv
+2, 0, offset
);
747 bytes
= TALLOC_REALLOC_ARRAY(talloc_tos(), bytes
, uint8_t,
750 return NT_STATUS_NO_MEMORY
;
752 SSVAL(bytes
, 1, size
);
753 memcpy(bytes
+ 3, buf
+ total
, size
);
755 status
= cli_smb(talloc_tos(), cli
, SMBwrite
, 0, 5, vwv
,
756 size
+3, bytes
, &req
, 1, NULL
, &ret_vwv
,
758 if (!NT_STATUS_IS_OK(status
)) {
763 size
= SVAL(ret_vwv
+0, 0);
776 if (ptotal
!= NULL
) {
783 * Send a write&x request
786 struct cli_write_andx_state
{
794 static void cli_write_andx_done(struct tevent_req
*subreq
);
796 struct tevent_req
*cli_write_andx_create(TALLOC_CTX
*mem_ctx
,
797 struct event_context
*ev
,
798 struct cli_state
*cli
, uint16_t fnum
,
799 uint16_t mode
, const uint8_t *buf
,
800 off_t offset
, size_t size
,
801 struct tevent_req
**reqs_before
,
803 struct tevent_req
**psmbreq
)
805 struct tevent_req
*req
, *subreq
;
806 struct cli_write_andx_state
*state
;
807 bool bigoffset
= ((cli
->capabilities
& CAP_LARGE_FILES
) != 0);
808 uint8_t wct
= bigoffset
? 14 : 12;
809 size_t max_write
= cli_write_max_bufsize(cli
, mode
, wct
);
812 req
= tevent_req_create(mem_ctx
, &state
, struct cli_write_andx_state
);
817 state
->size
= MIN(size
, max_write
);
821 SCVAL(vwv
+0, 0, 0xFF);
824 SSVAL(vwv
+2, 0, fnum
);
825 SIVAL(vwv
+3, 0, offset
);
827 SSVAL(vwv
+7, 0, mode
);
829 SSVAL(vwv
+9, 0, (state
->size
>>16));
830 SSVAL(vwv
+10, 0, state
->size
);
833 cli_smb_wct_ofs(reqs_before
, num_reqs_before
)
834 + 1 /* the wct field */
836 + 2 /* num_bytes field */
840 SIVAL(vwv
+12, 0, (((uint64_t)offset
)>>32) & 0xffffffff);
844 state
->iov
[0].iov_base
= (void *)&state
->pad
;
845 state
->iov
[0].iov_len
= 1;
846 state
->iov
[1].iov_base
= CONST_DISCARD(void *, buf
);
847 state
->iov
[1].iov_len
= state
->size
;
849 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBwriteX
, 0, wct
, vwv
,
851 if (tevent_req_nomem(subreq
, req
)) {
852 return tevent_req_post(req
, ev
);
854 tevent_req_set_callback(subreq
, cli_write_andx_done
, req
);
859 struct tevent_req
*cli_write_andx_send(TALLOC_CTX
*mem_ctx
,
860 struct event_context
*ev
,
861 struct cli_state
*cli
, uint16_t fnum
,
862 uint16_t mode
, const uint8_t *buf
,
863 off_t offset
, size_t size
)
865 struct tevent_req
*req
, *subreq
;
868 req
= cli_write_andx_create(mem_ctx
, ev
, cli
, fnum
, mode
, buf
, offset
,
869 size
, NULL
, 0, &subreq
);
874 status
= cli_smb_req_send(subreq
);
875 if (tevent_req_nterror(req
, status
)) {
876 return tevent_req_post(req
, ev
);
881 static void cli_write_andx_done(struct tevent_req
*subreq
)
883 struct tevent_req
*req
= tevent_req_callback_data(
884 subreq
, struct tevent_req
);
885 struct cli_write_andx_state
*state
= tevent_req_data(
886 req
, struct cli_write_andx_state
);
892 status
= cli_smb_recv(subreq
, state
, &inbuf
, 6, &wct
, &vwv
,
895 if (NT_STATUS_IS_ERR(status
)) {
896 tevent_req_nterror(req
, status
);
899 state
->written
= SVAL(vwv
+2, 0);
900 if (state
->size
> UINT16_MAX
) {
902 * It is important that we only set the
903 * high bits only if we asked for a large write.
905 * OS/2 print shares get this wrong and may send
910 state
->written
|= SVAL(vwv
+4, 0)<<16;
912 tevent_req_done(req
);
915 NTSTATUS
cli_write_andx_recv(struct tevent_req
*req
, size_t *pwritten
)
917 struct cli_write_andx_state
*state
= tevent_req_data(
918 req
, struct cli_write_andx_state
);
921 if (tevent_req_is_nterror(req
, &status
)) {
924 *pwritten
= state
->written
;
928 struct cli_writeall_state
{
929 struct event_context
*ev
;
930 struct cli_state
*cli
;
939 static void cli_writeall_written(struct tevent_req
*req
);
941 static struct tevent_req
*cli_writeall_send(TALLOC_CTX
*mem_ctx
,
942 struct event_context
*ev
,
943 struct cli_state
*cli
,
947 off_t offset
, size_t size
)
949 struct tevent_req
*req
, *subreq
;
950 struct cli_writeall_state
*state
;
952 req
= tevent_req_create(mem_ctx
, &state
, struct cli_writeall_state
);
961 state
->offset
= offset
;
965 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
966 state
->mode
, state
->buf
, state
->offset
,
968 if (tevent_req_nomem(subreq
, req
)) {
969 return tevent_req_post(req
, ev
);
971 tevent_req_set_callback(subreq
, cli_writeall_written
, req
);
975 static void cli_writeall_written(struct tevent_req
*subreq
)
977 struct tevent_req
*req
= tevent_req_callback_data(
978 subreq
, struct tevent_req
);
979 struct cli_writeall_state
*state
= tevent_req_data(
980 req
, struct cli_writeall_state
);
982 size_t written
, to_write
;
984 status
= cli_write_andx_recv(subreq
, &written
);
986 if (tevent_req_nterror(req
, status
)) {
990 state
->written
+= written
;
992 if (state
->written
> state
->size
) {
993 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
997 to_write
= state
->size
- state
->written
;
1000 tevent_req_done(req
);
1004 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
1006 state
->buf
+ state
->written
,
1007 state
->offset
+ state
->written
, to_write
);
1008 if (tevent_req_nomem(subreq
, req
)) {
1011 tevent_req_set_callback(subreq
, cli_writeall_written
, req
);
1014 static NTSTATUS
cli_writeall_recv(struct tevent_req
*req
,
1017 struct cli_writeall_state
*state
= tevent_req_data(
1018 req
, struct cli_writeall_state
);
1021 if (tevent_req_is_nterror(req
, &status
)) {
1024 if (pwritten
!= NULL
) {
1025 *pwritten
= state
->written
;
1027 return NT_STATUS_OK
;
1030 NTSTATUS
cli_writeall(struct cli_state
*cli
, uint16_t fnum
, uint16_t mode
,
1031 const uint8_t *buf
, off_t offset
, size_t size
,
1034 TALLOC_CTX
*frame
= talloc_stackframe();
1035 struct event_context
*ev
;
1036 struct tevent_req
*req
;
1037 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1039 if (cli_has_async_calls(cli
)) {
1041 * Can't use sync call while an async call is in flight
1043 status
= NT_STATUS_INVALID_PARAMETER
;
1046 ev
= event_context_init(frame
);
1050 req
= cli_writeall_send(frame
, ev
, cli
, fnum
, mode
, buf
, offset
, size
);
1054 if (!tevent_req_poll(req
, ev
)) {
1055 status
= map_nt_error_from_unix(errno
);
1058 status
= cli_writeall_recv(req
, pwritten
);
1064 struct cli_push_write_state
{
1065 struct tevent_req
*req
;/* This is the main request! Not the subreq */
1072 struct cli_push_state
{
1073 struct event_context
*ev
;
1074 struct cli_state
*cli
;
1080 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
);
1089 * Outstanding requests
1093 struct cli_push_write_state
**reqs
;
1096 static void cli_push_written(struct tevent_req
*req
);
1098 static bool cli_push_write_setup(struct tevent_req
*req
,
1099 struct cli_push_state
*state
,
1102 struct cli_push_write_state
*substate
;
1103 struct tevent_req
*subreq
;
1105 substate
= talloc(state
->reqs
, struct cli_push_write_state
);
1109 substate
->req
= req
;
1110 substate
->idx
= idx
;
1111 substate
->ofs
= state
->next_offset
;
1112 substate
->buf
= talloc_array(substate
, uint8_t, state
->chunk_size
);
1113 if (!substate
->buf
) {
1114 talloc_free(substate
);
1117 substate
->size
= state
->source(substate
->buf
,
1120 if (substate
->size
== 0) {
1122 /* nothing to send */
1123 talloc_free(substate
);
1127 subreq
= cli_writeall_send(substate
,
1128 state
->ev
, state
->cli
,
1129 state
->fnum
, state
->mode
,
1134 talloc_free(substate
);
1137 tevent_req_set_callback(subreq
, cli_push_written
, substate
);
1139 state
->reqs
[idx
] = substate
;
1140 state
->pending
+= 1;
1141 state
->next_offset
+= substate
->size
;
1146 struct tevent_req
*cli_push_send(TALLOC_CTX
*mem_ctx
, struct event_context
*ev
,
1147 struct cli_state
*cli
,
1148 uint16_t fnum
, uint16_t mode
,
1149 off_t start_offset
, size_t window_size
,
1150 size_t (*source
)(uint8_t *buf
, size_t n
,
1154 struct tevent_req
*req
;
1155 struct cli_push_state
*state
;
1158 req
= tevent_req_create(mem_ctx
, &state
, struct cli_push_state
);
1165 state
->start_offset
= start_offset
;
1167 state
->source
= source
;
1171 state
->next_offset
= start_offset
;
1173 state
->chunk_size
= cli_write_max_bufsize(cli
, mode
, 14);
1175 if (window_size
== 0) {
1176 window_size
= cli
->max_mux
* state
->chunk_size
;
1178 state
->num_reqs
= window_size
/state
->chunk_size
;
1179 if ((window_size
% state
->chunk_size
) > 0) {
1180 state
->num_reqs
+= 1;
1182 state
->num_reqs
= MIN(state
->num_reqs
, cli
->max_mux
);
1183 state
->num_reqs
= MAX(state
->num_reqs
, 1);
1185 state
->reqs
= TALLOC_ZERO_ARRAY(state
, struct cli_push_write_state
*,
1187 if (state
->reqs
== NULL
) {
1191 for (i
=0; i
<state
->num_reqs
; i
++) {
1192 if (!cli_push_write_setup(req
, state
, i
)) {
1201 if (state
->pending
== 0) {
1202 tevent_req_done(req
);
1203 return tevent_req_post(req
, ev
);
1209 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1210 return tevent_req_post(req
, ev
);
1213 static void cli_push_written(struct tevent_req
*subreq
)
1215 struct cli_push_write_state
*substate
= tevent_req_callback_data(
1216 subreq
, struct cli_push_write_state
);
1217 struct tevent_req
*req
= substate
->req
;
1218 struct cli_push_state
*state
= tevent_req_data(
1219 req
, struct cli_push_state
);
1221 uint32_t idx
= substate
->idx
;
1223 state
->reqs
[idx
] = NULL
;
1224 state
->pending
-= 1;
1226 status
= cli_writeall_recv(subreq
, NULL
);
1227 TALLOC_FREE(subreq
);
1228 TALLOC_FREE(substate
);
1229 if (tevent_req_nterror(req
, status
)) {
1234 if (!cli_push_write_setup(req
, state
, idx
)) {
1235 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1240 if (state
->pending
== 0) {
1241 tevent_req_done(req
);
1246 NTSTATUS
cli_push_recv(struct tevent_req
*req
)
1248 return tevent_req_simple_recv_ntstatus(req
);
1251 NTSTATUS
cli_push(struct cli_state
*cli
, uint16_t fnum
, uint16_t mode
,
1252 off_t start_offset
, size_t window_size
,
1253 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
),
1256 TALLOC_CTX
*frame
= talloc_stackframe();
1257 struct event_context
*ev
;
1258 struct tevent_req
*req
;
1259 NTSTATUS status
= NT_STATUS_OK
;
1261 if (cli_has_async_calls(cli
)) {
1263 * Can't use sync call while an async call is in flight
1265 status
= NT_STATUS_INVALID_PARAMETER
;
1269 ev
= event_context_init(frame
);
1271 status
= NT_STATUS_NO_MEMORY
;
1275 req
= cli_push_send(frame
, ev
, cli
, fnum
, mode
, start_offset
,
1276 window_size
, source
, priv
);
1278 status
= NT_STATUS_NO_MEMORY
;
1282 if (!tevent_req_poll(req
, ev
)) {
1283 status
= map_nt_error_from_unix(errno
);
1287 status
= cli_push_recv(req
);