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"
25 #include "../libcli/smb/smbXcli_base.h"
27 /****************************************************************************
28 Calculate the recommended read buffer size
29 ****************************************************************************/
30 static size_t cli_read_max_bufsize(struct cli_state
*cli
)
35 uint32_t useable_space
= 0;
37 data_offset
= HDR_VWV
;
38 data_offset
+= wct
* sizeof(uint16_t);
39 data_offset
+= sizeof(uint16_t); /* byte count */
40 data_offset
+= 1; /* pad */
42 min_space
= cli_state_available_size(cli
, data_offset
);
44 if (cli
->server_posix_capabilities
& CIFS_UNIX_LARGE_READ_CAP
) {
45 useable_space
= 0xFFFFFF - data_offset
;
47 if (smb1cli_conn_signing_is_active(cli
->conn
)) {
51 if (smb1cli_conn_encryption_on(cli
->conn
)) {
56 } else if (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_READX
) {
58 * Note: CAP_LARGE_READX also works with signing
60 useable_space
= 0x1FFFF - data_offset
;
62 useable_space
= MIN(useable_space
, UINT16_MAX
);
70 /****************************************************************************
71 Calculate the recommended write buffer size
72 ****************************************************************************/
73 static size_t cli_write_max_bufsize(struct cli_state
*cli
,
79 uint32_t useable_space
= 0;
81 data_offset
= HDR_VWV
;
82 data_offset
+= wct
* sizeof(uint16_t);
83 data_offset
+= sizeof(uint16_t); /* byte count */
84 data_offset
+= 1; /* pad */
86 min_space
= cli_state_available_size(cli
, data_offset
);
88 if (cli
->server_posix_capabilities
& CIFS_UNIX_LARGE_WRITE_CAP
) {
89 useable_space
= 0xFFFFFF - data_offset
;
90 } else if (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_WRITEX
) {
91 useable_space
= 0x1FFFF - data_offset
;
96 if (write_mode
!= 0) {
100 if (smb1cli_conn_signing_is_active(cli
->conn
)) {
104 if (smb1cli_conn_encryption_on(cli
->conn
)) {
108 if (strequal(cli
->dev
, "LPT1:")) {
112 return useable_space
;
115 struct cli_read_andx_state
{
123 static void cli_read_andx_done(struct tevent_req
*subreq
);
125 struct tevent_req
*cli_read_andx_create(TALLOC_CTX
*mem_ctx
,
126 struct tevent_context
*ev
,
127 struct cli_state
*cli
, uint16_t fnum
,
128 off_t offset
, size_t size
,
129 struct tevent_req
**psmbreq
)
131 struct tevent_req
*req
, *subreq
;
132 struct cli_read_andx_state
*state
;
135 req
= tevent_req_create(mem_ctx
, &state
, struct cli_read_andx_state
);
141 SCVAL(state
->vwv
+ 0, 0, 0xFF);
142 SCVAL(state
->vwv
+ 0, 1, 0);
143 SSVAL(state
->vwv
+ 1, 0, 0);
144 SSVAL(state
->vwv
+ 2, 0, fnum
);
145 SIVAL(state
->vwv
+ 3, 0, offset
);
146 SSVAL(state
->vwv
+ 5, 0, size
);
147 SSVAL(state
->vwv
+ 6, 0, size
);
148 SSVAL(state
->vwv
+ 7, 0, (size
>> 16));
149 SSVAL(state
->vwv
+ 8, 0, 0);
150 SSVAL(state
->vwv
+ 9, 0, 0);
152 if (smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
) {
153 SIVAL(state
->vwv
+ 10, 0,
154 (((uint64_t)offset
)>>32) & 0xffffffff);
157 if ((((uint64_t)offset
) & 0xffffffff00000000LL
) != 0) {
158 DEBUG(10, ("cli_read_andx_send got large offset where "
159 "the server does not support it\n"));
160 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
161 return tevent_req_post(req
, ev
);
165 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBreadX
, 0, wct
,
166 state
->vwv
, 0, NULL
);
167 if (subreq
== NULL
) {
171 tevent_req_set_callback(subreq
, cli_read_andx_done
, req
);
176 struct tevent_req
*cli_read_andx_send(TALLOC_CTX
*mem_ctx
,
177 struct tevent_context
*ev
,
178 struct cli_state
*cli
, uint16_t fnum
,
179 off_t offset
, size_t size
)
181 struct tevent_req
*req
, *subreq
;
184 req
= cli_read_andx_create(mem_ctx
, ev
, cli
, fnum
, offset
, size
,
190 status
= smb1cli_req_chain_submit(&subreq
, 1);
191 if (tevent_req_nterror(req
, status
)) {
192 return tevent_req_post(req
, ev
);
197 static void cli_read_andx_done(struct tevent_req
*subreq
)
199 struct tevent_req
*req
= tevent_req_callback_data(
200 subreq
, struct tevent_req
);
201 struct cli_read_andx_state
*state
= tevent_req_data(
202 req
, struct cli_read_andx_state
);
209 state
->status
= cli_smb_recv(subreq
, state
, &inbuf
, 12, &wct
, &vwv
,
212 if (NT_STATUS_IS_ERR(state
->status
)) {
213 tevent_req_nterror(req
, state
->status
);
217 /* size is the number of bytes the server returned.
219 state
->received
= SVAL(vwv
+ 5, 0);
220 state
->received
|= (((unsigned int)SVAL(vwv
+ 7, 0)) << 16);
222 if (state
->received
> state
->size
) {
223 DEBUG(5,("server returned more than we wanted!\n"));
224 tevent_req_nterror(req
, NT_STATUS_UNEXPECTED_IO_ERROR
);
229 * bcc field must be valid for small reads, for large reads the 16-bit
230 * bcc field can't be correct.
233 if ((state
->received
< 0xffff) && (state
->received
> num_bytes
)) {
234 DEBUG(5, ("server announced more bytes than sent\n"));
235 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
239 state
->buf
= discard_const_p(uint8_t, smb_base(inbuf
)) + SVAL(vwv
+6, 0);
241 if (trans_oob(smb_len_tcp(inbuf
), SVAL(vwv
+6, 0), state
->received
)
242 || ((state
->received
!= 0) && (state
->buf
< bytes
))) {
243 DEBUG(5, ("server returned invalid read&x data offset\n"));
244 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
247 tevent_req_done(req
);
251 * Pull the data out of a finished async read_and_x request. rcvbuf is
252 * talloced from the request, so better make sure that you copy it away before
253 * you talloc_free(req). "rcvbuf" is NOT a talloc_ctx of its own, so do not
257 NTSTATUS
cli_read_andx_recv(struct tevent_req
*req
, ssize_t
*received
,
260 struct cli_read_andx_state
*state
= tevent_req_data(
261 req
, struct cli_read_andx_state
);
264 if (tevent_req_is_nterror(req
, &status
)) {
267 *received
= state
->received
;
268 *rcvbuf
= state
->buf
;
272 struct cli_readall_state
{
273 struct tevent_context
*ev
;
274 struct cli_state
*cli
;
282 static void cli_readall_done(struct tevent_req
*subreq
);
284 static struct tevent_req
*cli_readall_send(TALLOC_CTX
*mem_ctx
,
285 struct tevent_context
*ev
,
286 struct cli_state
*cli
,
288 off_t offset
, size_t size
)
290 struct tevent_req
*req
, *subreq
;
291 struct cli_readall_state
*state
;
293 req
= tevent_req_create(mem_ctx
, &state
, struct cli_readall_state
);
300 state
->start_offset
= offset
;
305 subreq
= cli_read_andx_send(state
, ev
, cli
, fnum
, offset
, size
);
306 if (tevent_req_nomem(subreq
, req
)) {
307 return tevent_req_post(req
, ev
);
309 tevent_req_set_callback(subreq
, cli_readall_done
, req
);
313 static void cli_readall_done(struct tevent_req
*subreq
)
315 struct tevent_req
*req
= tevent_req_callback_data(
316 subreq
, struct tevent_req
);
317 struct cli_readall_state
*state
= tevent_req_data(
318 req
, struct cli_readall_state
);
323 status
= cli_read_andx_recv(subreq
, &received
, &buf
);
324 if (tevent_req_nterror(req
, status
)) {
330 tevent_req_done(req
);
334 if ((state
->received
== 0) && (received
== state
->size
)) {
335 /* Ideal case: Got it all in one run */
337 state
->received
+= received
;
338 tevent_req_done(req
);
343 * We got a short read, issue a read for the
344 * rest. Unfortunately we have to allocate the buffer
345 * ourselves now, as our caller expects to receive a single
346 * buffer. cli_read_andx does it from the buffer received from
347 * the net, but with a short read we have to put it together
348 * from several reads.
351 if (state
->buf
== NULL
) {
352 state
->buf
= talloc_array(state
, uint8_t, state
->size
);
353 if (tevent_req_nomem(state
->buf
, req
)) {
357 memcpy(state
->buf
+ state
->received
, buf
, received
);
358 state
->received
+= received
;
362 if (state
->received
>= state
->size
) {
363 tevent_req_done(req
);
367 subreq
= cli_read_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
368 state
->start_offset
+ state
->received
,
369 state
->size
- state
->received
);
370 if (tevent_req_nomem(subreq
, req
)) {
373 tevent_req_set_callback(subreq
, cli_readall_done
, req
);
376 static NTSTATUS
cli_readall_recv(struct tevent_req
*req
, ssize_t
*received
,
379 struct cli_readall_state
*state
= tevent_req_data(
380 req
, struct cli_readall_state
);
383 if (tevent_req_is_nterror(req
, &status
)) {
386 *received
= state
->received
;
387 *rcvbuf
= state
->buf
;
391 struct cli_pull_subreq
{
392 struct tevent_req
*req
;
398 * Parallel read support.
400 * cli_pull sends as many read&x requests as the server would allow via
401 * max_mux at a time. When replies flow back in, the data is written into
402 * the callback function "sink" in the right order.
405 struct cli_pull_state
{
406 struct tevent_req
*req
;
408 struct tevent_context
*ev
;
409 struct cli_state
*cli
;
414 NTSTATUS (*sink
)(char *buf
, size_t n
, void *priv
);
420 * Outstanding requests
424 struct cli_pull_subreq
*reqs
;
427 * For how many bytes did we send requests already?
432 * Next request index to push into "sink". This walks around the "req"
433 * array, taking care that the requests are pushed to "sink" in the
434 * right order. If necessary (i.e. replies don't come in in the right
435 * order), replies are held back in "reqs".
440 * How many bytes did we push into "sink"?
446 static char *cli_pull_print(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
)
448 struct cli_pull_state
*state
= tevent_req_data(
449 req
, struct cli_pull_state
);
452 result
= tevent_req_default_print(req
, mem_ctx
);
453 if (result
== NULL
) {
457 return talloc_asprintf_append_buffer(
458 result
, "num_reqs=%d, top_req=%d",
459 state
->num_reqs
, state
->top_req
);
462 static void cli_pull_read_done(struct tevent_req
*read_req
);
465 * Prepare an async pull request
468 struct tevent_req
*cli_pull_send(TALLOC_CTX
*mem_ctx
,
469 struct tevent_context
*ev
,
470 struct cli_state
*cli
,
471 uint16_t fnum
, off_t start_offset
,
472 off_t size
, size_t window_size
,
473 NTSTATUS (*sink
)(char *buf
, size_t n
,
477 struct tevent_req
*req
;
478 struct cli_pull_state
*state
;
480 size_t page_size
= 1024;
482 req
= tevent_req_create(mem_ctx
, &state
, struct cli_pull_state
);
486 tevent_req_set_print_fn(req
, cli_pull_print
);
492 state
->start_offset
= start_offset
;
501 tevent_req_done(req
);
502 return tevent_req_post(req
, ev
);
505 state
->chunk_size
= cli_read_max_bufsize(cli
);
506 if (state
->chunk_size
> page_size
) {
507 state
->chunk_size
&= ~(page_size
- 1);
510 state
->max_reqs
= smbXcli_conn_max_requests(cli
->conn
);
512 state
->num_reqs
= MAX(window_size
/state
->chunk_size
, 1);
513 state
->num_reqs
= MIN(state
->num_reqs
, state
->max_reqs
);
515 state
->reqs
= talloc_zero_array(state
, struct cli_pull_subreq
,
517 if (state
->reqs
== NULL
) {
521 state
->requested
= 0;
523 for (i
=0; i
<state
->num_reqs
; i
++) {
524 struct cli_pull_subreq
*subreq
= &state
->reqs
[i
];
526 size_t request_thistime
;
528 if (state
->requested
>= size
) {
533 size_left
= size
- state
->requested
;
534 request_thistime
= MIN(size_left
, state
->chunk_size
);
536 subreq
->req
= cli_readall_send(
537 state
->reqs
, ev
, cli
, fnum
,
538 state
->start_offset
+ state
->requested
,
541 if (subreq
->req
== NULL
) {
544 tevent_req_set_callback(subreq
->req
, cli_pull_read_done
, req
);
545 state
->requested
+= request_thistime
;
555 * Handle incoming read replies, push the data into sink and send out new
556 * requests if necessary.
559 static void cli_pull_read_done(struct tevent_req
*subreq
)
561 struct tevent_req
*req
= tevent_req_callback_data(
562 subreq
, struct tevent_req
);
563 struct cli_pull_state
*state
= tevent_req_data(
564 req
, struct cli_pull_state
);
565 struct cli_pull_subreq
*pull_subreq
= NULL
;
569 for (i
= 0; i
< state
->num_reqs
; i
++) {
570 pull_subreq
= &state
->reqs
[i
];
571 if (subreq
== pull_subreq
->req
) {
575 if (i
== state
->num_reqs
) {
576 /* Huh -- received something we did not send?? */
577 tevent_req_nterror(req
, NT_STATUS_INTERNAL_ERROR
);
581 status
= cli_readall_recv(subreq
, &pull_subreq
->received
,
583 if (!NT_STATUS_IS_OK(status
)) {
584 tevent_req_nterror(state
->req
, status
);
589 * This loop is the one to take care of out-of-order replies. All
590 * pending requests are in state->reqs, state->reqs[top_req] is the
591 * one that is to be pushed next. If however a request later than
592 * top_req is replied to, then we can't push yet. If top_req is
593 * replied to at a later point then, we need to push all the finished
597 while (state
->reqs
[state
->top_req
].req
!= NULL
) {
598 struct cli_pull_subreq
*top_subreq
;
600 DEBUG(11, ("cli_pull_read_done: top_req = %d\n",
603 top_subreq
= &state
->reqs
[state
->top_req
];
605 if (tevent_req_is_in_progress(top_subreq
->req
)) {
606 DEBUG(11, ("cli_pull_read_done: top request not yet "
611 DEBUG(10, ("cli_pull_read_done: Pushing %d bytes, %d already "
612 "pushed\n", (int)top_subreq
->received
,
613 (int)state
->pushed
));
615 status
= state
->sink((char *)top_subreq
->buf
,
616 top_subreq
->received
, state
->priv
);
617 if (tevent_req_nterror(state
->req
, status
)) {
620 state
->pushed
+= top_subreq
->received
;
622 TALLOC_FREE(state
->reqs
[state
->top_req
].req
);
624 if (state
->requested
< state
->size
) {
625 struct tevent_req
*new_req
;
627 size_t request_thistime
;
629 size_left
= state
->size
- state
->requested
;
630 request_thistime
= MIN(size_left
, state
->chunk_size
);
632 DEBUG(10, ("cli_pull_read_done: Requesting %d bytes "
633 "at %d, position %d\n",
634 (int)request_thistime
,
635 (int)(state
->start_offset
639 new_req
= cli_readall_send(
640 state
->reqs
, state
->ev
, state
->cli
,
642 state
->start_offset
+ state
->requested
,
645 if (tevent_req_nomem(new_req
, state
->req
)) {
648 tevent_req_set_callback(new_req
, cli_pull_read_done
,
651 state
->reqs
[state
->top_req
].req
= new_req
;
652 state
->requested
+= request_thistime
;
655 state
->top_req
= (state
->top_req
+1) % state
->num_reqs
;
658 tevent_req_done(req
);
661 NTSTATUS
cli_pull_recv(struct tevent_req
*req
, off_t
*received
)
663 struct cli_pull_state
*state
= tevent_req_data(
664 req
, struct cli_pull_state
);
667 if (tevent_req_is_nterror(req
, &status
)) {
670 *received
= state
->pushed
;
674 NTSTATUS
cli_pull(struct cli_state
*cli
, uint16_t fnum
,
675 off_t start_offset
, off_t size
, size_t window_size
,
676 NTSTATUS (*sink
)(char *buf
, size_t n
, void *priv
),
677 void *priv
, off_t
*received
)
679 TALLOC_CTX
*frame
= talloc_stackframe();
680 struct tevent_context
*ev
;
681 struct tevent_req
*req
;
682 NTSTATUS status
= NT_STATUS_OK
;
684 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
686 * Can't use sync call while an async call is in flight
688 status
= NT_STATUS_INVALID_PARAMETER
;
692 ev
= samba_tevent_context_init(frame
);
694 status
= NT_STATUS_NO_MEMORY
;
698 req
= cli_pull_send(frame
, ev
, cli
, fnum
, start_offset
, size
,
699 window_size
, sink
, priv
);
701 status
= NT_STATUS_NO_MEMORY
;
705 if (!tevent_req_poll(req
, ev
)) {
706 status
= map_nt_error_from_unix(errno
);
710 status
= cli_pull_recv(req
, received
);
716 static NTSTATUS
cli_read_sink(char *buf
, size_t n
, void *priv
)
718 char **pbuf
= (char **)priv
;
719 memcpy(*pbuf
, buf
, n
);
724 NTSTATUS
cli_read(struct cli_state
*cli
, uint16_t fnum
,
725 char *buf
, off_t offset
, size_t size
,
731 status
= cli_pull(cli
, fnum
, offset
, size
, size
,
732 cli_read_sink
, &buf
, &ret
);
733 if (!NT_STATUS_IS_OK(status
)) {
744 /****************************************************************************
745 write to a file using a SMBwrite and not bypassing 0 byte writes
746 ****************************************************************************/
748 NTSTATUS
cli_smbwrite(struct cli_state
*cli
, uint16_t fnum
, char *buf
,
749 off_t offset
, size_t size1
, size_t *ptotal
)
758 bytes
= talloc_array(talloc_tos(), uint8_t, 3);
760 return NT_STATUS_NO_MEMORY
;
765 uint32_t usable_space
= cli_state_available_size(cli
, 48);
766 size_t size
= MIN(size1
, usable_space
);
767 struct tevent_req
*req
;
772 SSVAL(vwv
+0, 0, fnum
);
773 SSVAL(vwv
+1, 0, size
);
774 SIVAL(vwv
+2, 0, offset
);
777 bytes
= talloc_realloc(talloc_tos(), bytes
, uint8_t,
780 return NT_STATUS_NO_MEMORY
;
782 SSVAL(bytes
, 1, size
);
783 memcpy(bytes
+ 3, buf
+ total
, size
);
785 status
= cli_smb(talloc_tos(), cli
, SMBwrite
, 0, 5, vwv
,
786 size
+3, bytes
, &req
, 1, NULL
, &ret_vwv
,
788 if (!NT_STATUS_IS_OK(status
)) {
793 size
= SVAL(ret_vwv
+0, 0);
806 if (ptotal
!= NULL
) {
813 * Send a write&x request
816 struct cli_write_andx_state
{
824 static void cli_write_andx_done(struct tevent_req
*subreq
);
826 struct tevent_req
*cli_write_andx_create(TALLOC_CTX
*mem_ctx
,
827 struct tevent_context
*ev
,
828 struct cli_state
*cli
, uint16_t fnum
,
829 uint16_t mode
, const uint8_t *buf
,
830 off_t offset
, size_t size
,
831 struct tevent_req
**reqs_before
,
833 struct tevent_req
**psmbreq
)
835 struct tevent_req
*req
, *subreq
;
836 struct cli_write_andx_state
*state
;
837 bool bigoffset
= ((smb1cli_conn_capabilities(cli
->conn
) & CAP_LARGE_FILES
) != 0);
838 uint8_t wct
= bigoffset
? 14 : 12;
839 size_t max_write
= cli_write_max_bufsize(cli
, mode
, wct
);
842 req
= tevent_req_create(mem_ctx
, &state
, struct cli_write_andx_state
);
847 state
->size
= MIN(size
, max_write
);
851 SCVAL(vwv
+0, 0, 0xFF);
854 SSVAL(vwv
+2, 0, fnum
);
855 SIVAL(vwv
+3, 0, offset
);
857 SSVAL(vwv
+7, 0, mode
);
859 SSVAL(vwv
+9, 0, (state
->size
>>16));
860 SSVAL(vwv
+10, 0, state
->size
);
863 smb1cli_req_wct_ofs(reqs_before
, num_reqs_before
)
864 + 1 /* the wct field */
866 + 2 /* num_bytes field */
870 SIVAL(vwv
+12, 0, (((uint64_t)offset
)>>32) & 0xffffffff);
874 state
->iov
[0].iov_base
= (void *)&state
->pad
;
875 state
->iov
[0].iov_len
= 1;
876 state
->iov
[1].iov_base
= discard_const_p(void, buf
);
877 state
->iov
[1].iov_len
= state
->size
;
879 subreq
= cli_smb_req_create(state
, ev
, cli
, SMBwriteX
, 0, wct
, vwv
,
881 if (tevent_req_nomem(subreq
, req
)) {
882 return tevent_req_post(req
, ev
);
884 tevent_req_set_callback(subreq
, cli_write_andx_done
, req
);
889 struct tevent_req
*cli_write_andx_send(TALLOC_CTX
*mem_ctx
,
890 struct tevent_context
*ev
,
891 struct cli_state
*cli
, uint16_t fnum
,
892 uint16_t mode
, const uint8_t *buf
,
893 off_t offset
, size_t size
)
895 struct tevent_req
*req
, *subreq
;
898 req
= cli_write_andx_create(mem_ctx
, ev
, cli
, fnum
, mode
, buf
, offset
,
899 size
, NULL
, 0, &subreq
);
904 status
= smb1cli_req_chain_submit(&subreq
, 1);
905 if (tevent_req_nterror(req
, status
)) {
906 return tevent_req_post(req
, ev
);
911 static void cli_write_andx_done(struct tevent_req
*subreq
)
913 struct tevent_req
*req
= tevent_req_callback_data(
914 subreq
, struct tevent_req
);
915 struct cli_write_andx_state
*state
= tevent_req_data(
916 req
, struct cli_write_andx_state
);
921 status
= cli_smb_recv(subreq
, state
, NULL
, 6, &wct
, &vwv
,
924 if (NT_STATUS_IS_ERR(status
)) {
925 tevent_req_nterror(req
, status
);
928 state
->written
= SVAL(vwv
+2, 0);
929 if (state
->size
> UINT16_MAX
) {
931 * It is important that we only set the
932 * high bits only if we asked for a large write.
934 * OS/2 print shares get this wrong and may send
939 state
->written
|= SVAL(vwv
+4, 0)<<16;
941 tevent_req_done(req
);
944 NTSTATUS
cli_write_andx_recv(struct tevent_req
*req
, size_t *pwritten
)
946 struct cli_write_andx_state
*state
= tevent_req_data(
947 req
, struct cli_write_andx_state
);
950 if (tevent_req_is_nterror(req
, &status
)) {
954 *pwritten
= state
->written
;
959 struct cli_writeall_state
{
960 struct tevent_context
*ev
;
961 struct cli_state
*cli
;
970 static void cli_writeall_written(struct tevent_req
*req
);
972 static struct tevent_req
*cli_writeall_send(TALLOC_CTX
*mem_ctx
,
973 struct tevent_context
*ev
,
974 struct cli_state
*cli
,
978 off_t offset
, size_t size
)
980 struct tevent_req
*req
, *subreq
;
981 struct cli_writeall_state
*state
;
983 req
= tevent_req_create(mem_ctx
, &state
, struct cli_writeall_state
);
992 state
->offset
= offset
;
996 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
997 state
->mode
, state
->buf
, state
->offset
,
999 if (tevent_req_nomem(subreq
, req
)) {
1000 return tevent_req_post(req
, ev
);
1002 tevent_req_set_callback(subreq
, cli_writeall_written
, req
);
1006 static void cli_writeall_written(struct tevent_req
*subreq
)
1008 struct tevent_req
*req
= tevent_req_callback_data(
1009 subreq
, struct tevent_req
);
1010 struct cli_writeall_state
*state
= tevent_req_data(
1011 req
, struct cli_writeall_state
);
1013 size_t written
, to_write
;
1015 status
= cli_write_andx_recv(subreq
, &written
);
1016 TALLOC_FREE(subreq
);
1017 if (tevent_req_nterror(req
, status
)) {
1021 state
->written
+= written
;
1023 if (state
->written
> state
->size
) {
1024 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
1028 to_write
= state
->size
- state
->written
;
1030 if (to_write
== 0) {
1031 tevent_req_done(req
);
1035 subreq
= cli_write_andx_send(state
, state
->ev
, state
->cli
, state
->fnum
,
1037 state
->buf
+ state
->written
,
1038 state
->offset
+ state
->written
, to_write
);
1039 if (tevent_req_nomem(subreq
, req
)) {
1042 tevent_req_set_callback(subreq
, cli_writeall_written
, req
);
1045 static NTSTATUS
cli_writeall_recv(struct tevent_req
*req
,
1048 struct cli_writeall_state
*state
= tevent_req_data(
1049 req
, struct cli_writeall_state
);
1052 if (tevent_req_is_nterror(req
, &status
)) {
1055 if (pwritten
!= NULL
) {
1056 *pwritten
= state
->written
;
1058 return NT_STATUS_OK
;
1061 NTSTATUS
cli_writeall(struct cli_state
*cli
, uint16_t fnum
, uint16_t mode
,
1062 const uint8_t *buf
, off_t offset
, size_t size
,
1065 TALLOC_CTX
*frame
= talloc_stackframe();
1066 struct tevent_context
*ev
;
1067 struct tevent_req
*req
;
1068 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1070 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1072 * Can't use sync call while an async call is in flight
1074 status
= NT_STATUS_INVALID_PARAMETER
;
1077 ev
= samba_tevent_context_init(frame
);
1081 req
= cli_writeall_send(frame
, ev
, cli
, fnum
, mode
, buf
, offset
, size
);
1085 if (!tevent_req_poll(req
, ev
)) {
1086 status
= map_nt_error_from_unix(errno
);
1089 status
= cli_writeall_recv(req
, pwritten
);
1095 struct cli_push_write_state
{
1096 struct tevent_req
*req
;/* This is the main request! Not the subreq */
1103 struct cli_push_state
{
1104 struct tevent_context
*ev
;
1105 struct cli_state
*cli
;
1111 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
);
1120 * Outstanding requests
1125 struct cli_push_write_state
**reqs
;
1128 static void cli_push_written(struct tevent_req
*req
);
1130 static bool cli_push_write_setup(struct tevent_req
*req
,
1131 struct cli_push_state
*state
,
1134 struct cli_push_write_state
*substate
;
1135 struct tevent_req
*subreq
;
1137 substate
= talloc(state
->reqs
, struct cli_push_write_state
);
1141 substate
->req
= req
;
1142 substate
->idx
= idx
;
1143 substate
->ofs
= state
->next_offset
;
1144 substate
->buf
= talloc_array(substate
, uint8_t, state
->chunk_size
);
1145 if (!substate
->buf
) {
1146 talloc_free(substate
);
1149 substate
->size
= state
->source(substate
->buf
,
1152 if (substate
->size
== 0) {
1154 /* nothing to send */
1155 talloc_free(substate
);
1159 subreq
= cli_writeall_send(substate
,
1160 state
->ev
, state
->cli
,
1161 state
->fnum
, state
->mode
,
1166 talloc_free(substate
);
1169 tevent_req_set_callback(subreq
, cli_push_written
, substate
);
1171 state
->reqs
[idx
] = substate
;
1172 state
->pending
+= 1;
1173 state
->next_offset
+= substate
->size
;
1178 struct tevent_req
*cli_push_send(TALLOC_CTX
*mem_ctx
, struct tevent_context
*ev
,
1179 struct cli_state
*cli
,
1180 uint16_t fnum
, uint16_t mode
,
1181 off_t start_offset
, size_t window_size
,
1182 size_t (*source
)(uint8_t *buf
, size_t n
,
1186 struct tevent_req
*req
;
1187 struct cli_push_state
*state
;
1189 size_t page_size
= 1024;
1191 req
= tevent_req_create(mem_ctx
, &state
, struct cli_push_state
);
1198 state
->start_offset
= start_offset
;
1200 state
->source
= source
;
1204 state
->next_offset
= start_offset
;
1206 state
->chunk_size
= cli_write_max_bufsize(cli
, mode
, 14);
1207 if (state
->chunk_size
> page_size
) {
1208 state
->chunk_size
&= ~(page_size
- 1);
1211 state
->max_reqs
= smbXcli_conn_max_requests(cli
->conn
);
1213 if (window_size
== 0) {
1214 window_size
= state
->max_reqs
* state
->chunk_size
;
1216 state
->num_reqs
= window_size
/state
->chunk_size
;
1217 if ((window_size
% state
->chunk_size
) > 0) {
1218 state
->num_reqs
+= 1;
1220 state
->num_reqs
= MIN(state
->num_reqs
, state
->max_reqs
);
1221 state
->num_reqs
= MAX(state
->num_reqs
, 1);
1223 state
->reqs
= talloc_zero_array(state
, struct cli_push_write_state
*,
1225 if (state
->reqs
== NULL
) {
1229 for (i
=0; i
<state
->num_reqs
; i
++) {
1230 if (!cli_push_write_setup(req
, state
, i
)) {
1239 if (state
->pending
== 0) {
1240 tevent_req_done(req
);
1241 return tevent_req_post(req
, ev
);
1247 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1248 return tevent_req_post(req
, ev
);
1251 static void cli_push_written(struct tevent_req
*subreq
)
1253 struct cli_push_write_state
*substate
= tevent_req_callback_data(
1254 subreq
, struct cli_push_write_state
);
1255 struct tevent_req
*req
= substate
->req
;
1256 struct cli_push_state
*state
= tevent_req_data(
1257 req
, struct cli_push_state
);
1259 uint32_t idx
= substate
->idx
;
1261 state
->reqs
[idx
] = NULL
;
1262 state
->pending
-= 1;
1264 status
= cli_writeall_recv(subreq
, NULL
);
1265 TALLOC_FREE(subreq
);
1266 TALLOC_FREE(substate
);
1267 if (tevent_req_nterror(req
, status
)) {
1272 if (!cli_push_write_setup(req
, state
, idx
)) {
1273 tevent_req_nterror(req
, NT_STATUS_NO_MEMORY
);
1278 if (state
->pending
== 0) {
1279 tevent_req_done(req
);
1284 NTSTATUS
cli_push_recv(struct tevent_req
*req
)
1286 return tevent_req_simple_recv_ntstatus(req
);
1289 NTSTATUS
cli_push(struct cli_state
*cli
, uint16_t fnum
, uint16_t mode
,
1290 off_t start_offset
, size_t window_size
,
1291 size_t (*source
)(uint8_t *buf
, size_t n
, void *priv
),
1294 TALLOC_CTX
*frame
= talloc_stackframe();
1295 struct tevent_context
*ev
;
1296 struct tevent_req
*req
;
1297 NTSTATUS status
= NT_STATUS_OK
;
1299 if (smbXcli_conn_has_async_calls(cli
->conn
)) {
1301 * Can't use sync call while an async call is in flight
1303 status
= NT_STATUS_INVALID_PARAMETER
;
1307 ev
= samba_tevent_context_init(frame
);
1309 status
= NT_STATUS_NO_MEMORY
;
1313 req
= cli_push_send(frame
, ev
, cli
, fnum
, mode
, start_offset
,
1314 window_size
, source
, priv
);
1316 status
= NT_STATUS_NO_MEMORY
;
1320 if (!tevent_req_poll(req
, ev
)) {
1321 status
= map_nt_error_from_unix(errno
);
1325 status
= cli_push_recv(req
);