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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /****************************************************************************
26 Issue a single SMBread and don't wait for a reply.
27 ****************************************************************************/
29 static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
32 memset(cli->outbuf,'\0',smb_size);
33 memset(cli->inbuf,'\0',smb_size);
35 set_message(cli->outbuf,10,0,True);
37 SCVAL(cli->outbuf,smb_com,SMBreadX);
38 SSVAL(cli->outbuf,smb_tid,cli->cnum);
39 cli_setup_packet(cli);
41 SCVAL(cli->outbuf,smb_vwv0,0xFF);
42 SSVAL(cli->outbuf,smb_vwv2,fnum);
43 SIVAL(cli->outbuf,smb_vwv3,offset);
44 SSVAL(cli->outbuf,smb_vwv5,size);
45 SSVAL(cli->outbuf,smb_vwv6,size);
46 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
48 return cli_send_smb(cli);
51 /****************************************************************************
52 Issue a single SMBreadraw and don't wait for a reply.
53 ****************************************************************************/
55 static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset,
58 memset(cli->outbuf,'\0',smb_size);
59 memset(cli->inbuf,'\0',smb_size);
61 set_message(cli->outbuf,10,0,True);
63 SCVAL(cli->outbuf,smb_com,SMBreadbraw);
64 SSVAL(cli->outbuf,smb_tid,cli->cnum);
65 cli_setup_packet(cli);
67 SSVAL(cli->outbuf,smb_vwv0,fnum);
68 SIVAL(cli->outbuf,smb_vwv1,offset);
69 SSVAL(cli->outbuf,smb_vwv2,size);
70 SSVAL(cli->outbuf,smb_vwv3,size);
71 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
73 return cli_send_smb(cli);
76 /****************************************************************************
77 Read size bytes at offset offset using SMBreadX.
78 ****************************************************************************/
80 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
91 * Set readsize to the maximum size we can handle in one readX,
92 * rounded down to a multiple of 1024.
95 readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
97 while (total < size) {
98 readsize = MIN(readsize, size-total);
100 /* Issue a read and receive a reply */
102 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
105 if (!cli_receive_smb(cli))
108 /* Check for error. Make sure to check for DOS and NT
111 if (cli_is_error(cli)) {
112 NTSTATUS status = NT_STATUS_OK;
116 if (cli_is_nt_error(cli))
117 status = cli_nt_error(cli);
119 cli_dos_error(cli, &eclass, &ecode);
121 if ((eclass == ERRDOS && ecode == ERRmoredata) ||
122 NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
126 size2 = SVAL(cli->inbuf, smb_vwv5);
128 if (size2 > readsize) {
129 DEBUG(5,("server returned more than we wanted!\n"));
131 } else if (size2 < 0) {
132 DEBUG(5,("read return < 0!\n"));
136 /* Copy data into buffer */
138 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
139 memcpy(buf + total, p, size2);
145 * If the server returned less than we asked for we're at EOF.
148 if (size2 < readsize)
155 /****************************************************************************
156 Tester for the readraw call.
157 ****************************************************************************/
159 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
170 * Set readsize to the maximum size we can handle in one readraw.
175 while (total < size) {
176 readsize = MIN(readsize, size-total);
178 /* Issue a read and receive a reply */
180 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
183 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
186 size2 = smb_len(cli->inbuf);
188 if (size2 > readsize) {
189 DEBUG(5,("server returned more than we wanted!\n"));
191 } else if (size2 < 0) {
192 DEBUG(5,("read return < 0!\n"));
196 /* Copy data into buffer */
200 memcpy(buf + total, p, size2);
207 * If the server returned less than we asked for we're at EOF.
210 if (size2 < readsize)
217 /****************************************************************************
218 issue a single SMBwrite and don't wait for a reply
219 ****************************************************************************/
221 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset, uint16 mode, char *buf,
226 if (size > cli->bufsize) {
227 cli->outbuf = realloc(cli->outbuf, size + 1024);
228 cli->inbuf = realloc(cli->inbuf, size + 1024);
229 if (cli->outbuf == NULL || cli->inbuf == NULL)
231 cli->bufsize = size + 1024;
234 memset(cli->outbuf,'\0',smb_size);
235 memset(cli->inbuf,'\0',smb_size);
238 set_message(cli->outbuf,14,0,True);
240 set_message(cli->outbuf,12,0,True);
242 SCVAL(cli->outbuf,smb_com,SMBwriteX);
243 SSVAL(cli->outbuf,smb_tid,cli->cnum);
244 cli_setup_packet(cli);
246 SCVAL(cli->outbuf,smb_vwv0,0xFF);
247 SSVAL(cli->outbuf,smb_vwv2,fnum);
249 SIVAL(cli->outbuf,smb_vwv3,offset);
250 SIVAL(cli->outbuf,smb_vwv5,(mode & 0x0008) ? 0xFFFFFFFF : 0);
251 SSVAL(cli->outbuf,smb_vwv7,mode);
253 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
254 SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
255 SSVAL(cli->outbuf,smb_vwv10,size);
256 SSVAL(cli->outbuf,smb_vwv11,
257 smb_buf(cli->outbuf) - smb_base(cli->outbuf));
259 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
260 memcpy(p, buf, size);
261 cli_setup_bcc(cli, p+size);
263 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
265 show_msg(cli->outbuf);
266 return cli_send_smb(cli);
269 /****************************************************************************
271 write_mode: 0x0001 disallow write cacheing
272 0x0002 return bytes remaining
273 0x0004 use raw named pipe protocol
274 0x0008 start of message mode named pipe protocol
275 ****************************************************************************/
277 ssize_t cli_write(struct cli_state *cli,
278 int fnum, uint16 write_mode,
279 char *buf, off_t offset, size_t size)
284 int mpx = MAX(cli->max_mux-1, 1);
285 int block = (cli->max_xmit - (smb_size+32)) & ~1023;
286 int blocks = (size + (block-1)) / block;
288 while (received < blocks) {
290 while ((issued - received < mpx) && (issued < blocks)) {
291 int bsent = issued * block;
292 int size1 = MIN(block, size - bsent);
294 if (!cli_issue_write(cli, fnum, offset + bsent,
302 if (!cli_receive_smb(cli))
307 if (cli_is_error(cli))
310 bwritten += SVAL(cli->inbuf, smb_vwv2);
311 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))>>16);
314 while (received < issued && cli_receive_smb(cli))
320 /****************************************************************************
321 write to a file using a SMBwrite and not bypassing 0 byte writes
322 ****************************************************************************/
324 ssize_t cli_smbwrite(struct cli_state *cli,
325 int fnum, char *buf, off_t offset, size_t size1)
331 size_t size = MIN(size1, cli->max_xmit - 48);
333 memset(cli->outbuf,'\0',smb_size);
334 memset(cli->inbuf,'\0',smb_size);
336 set_message(cli->outbuf,5, 0,True);
338 SCVAL(cli->outbuf,smb_com,SMBwrite);
339 SSVAL(cli->outbuf,smb_tid,cli->cnum);
340 cli_setup_packet(cli);
342 SSVAL(cli->outbuf,smb_vwv0,fnum);
343 SSVAL(cli->outbuf,smb_vwv1,size);
344 SIVAL(cli->outbuf,smb_vwv2,offset);
345 SSVAL(cli->outbuf,smb_vwv4,0);
347 p = smb_buf(cli->outbuf);
349 SSVAL(p, 0, size); p += 2;
350 memcpy(p, buf, size); p += size;
352 cli_setup_bcc(cli, p);
354 if (!cli_send_smb(cli))
357 if (!cli_receive_smb(cli))
360 if (cli_is_error(cli))
363 size = SVAL(cli->inbuf,smb_vwv0);