[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / libsmb / clireadwrite.c
blobdd7ddcebb55fb59bb41c84667e7769cd8a36f964
1 /*
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.
21 #include "includes.h"
23 /****************************************************************************
24 Issue a single SMBread and don't wait for a reply.
25 ****************************************************************************/
27 static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
28 size_t size, int i)
30 BOOL bigoffset = False;
32 memset(cli->outbuf,'\0',smb_size);
33 memset(cli->inbuf,'\0',smb_size);
35 if ((SMB_BIG_UINT)offset >> 32)
36 bigoffset = True;
38 set_message(cli->outbuf,bigoffset ? 12 : 10,0,True);
40 SCVAL(cli->outbuf,smb_com,SMBreadX);
41 SSVAL(cli->outbuf,smb_tid,cli->cnum);
42 cli_setup_packet(cli);
44 SCVAL(cli->outbuf,smb_vwv0,0xFF);
45 SSVAL(cli->outbuf,smb_vwv2,fnum);
46 SIVAL(cli->outbuf,smb_vwv3,offset);
47 SSVAL(cli->outbuf,smb_vwv5,size);
48 SSVAL(cli->outbuf,smb_vwv6,size);
49 SSVAL(cli->outbuf,smb_vwv7,((size >> 16) & 1));
50 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
52 if (bigoffset) {
53 SIVAL(cli->outbuf,smb_vwv10,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
56 return cli_send_smb(cli);
59 /****************************************************************************
60 Read size bytes at offset offset using SMBreadX.
61 ****************************************************************************/
63 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
65 char *p;
66 int size2;
67 int readsize;
68 ssize_t total = 0;
70 if (size == 0)
71 return 0;
74 * Set readsize to the maximum size we can handle in one readX,
75 * rounded down to a multiple of 1024.
78 if (cli->capabilities & CAP_LARGE_READX) {
79 if (cli->is_samba) {
80 readsize = CLI_SAMBA_MAX_LARGE_READX_SIZE;
81 } else {
82 readsize = CLI_WINDOWS_MAX_LARGE_READX_SIZE;
84 } else {
85 readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
88 while (total < size) {
89 readsize = MIN(readsize, size-total);
91 /* Issue a read and receive a reply */
93 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
94 return -1;
96 if (!cli_receive_smb(cli))
97 return -1;
99 /* Check for error. Make sure to check for DOS and NT
100 errors. */
102 if (cli_is_error(cli)) {
103 BOOL recoverable_error = False;
104 NTSTATUS status = NT_STATUS_OK;
105 uint8 eclass = 0;
106 uint32 ecode = 0;
108 if (cli_is_nt_error(cli))
109 status = cli_nt_error(cli);
110 else
111 cli_dos_error(cli, &eclass, &ecode);
114 * ERRDOS ERRmoredata or STATUS_MORE_ENRTIES is a
115 * recoverable error, plus we have valid data in the
116 * packet so don't error out here.
119 if ((eclass == ERRDOS && ecode == ERRmoredata) ||
120 NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
121 recoverable_error = True;
123 if (!recoverable_error)
124 return -1;
127 size2 = SVAL(cli->inbuf, smb_vwv5);
128 size2 |= (((unsigned int)(SVAL(cli->inbuf, smb_vwv7) & 1)) << 16);
130 if (size2 > readsize) {
131 DEBUG(5,("server returned more than we wanted!\n"));
132 return -1;
133 } else if (size2 < 0) {
134 DEBUG(5,("read return < 0!\n"));
135 return -1;
138 /* Copy data into buffer */
140 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
141 memcpy(buf + total, p, size2);
143 total += size2;
144 offset += size2;
147 * If the server returned less than we asked for we're at EOF.
150 if (size2 < readsize)
151 break;
154 return total;
157 #if 0 /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
159 /* This call is INCOMPATIBLE with SMB signing. If you remove the #if 0
160 you must fix ensure you don't attempt to sign the packets - data
161 *will* be currupted */
163 /****************************************************************************
164 Issue a single SMBreadraw and don't wait for a reply.
165 ****************************************************************************/
167 static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset,
168 size_t size, int i)
171 if (!cli->sign_info.use_smb_signing) {
172 DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
173 return False;
176 memset(cli->outbuf,'\0',smb_size);
177 memset(cli->inbuf,'\0',smb_size);
179 set_message(cli->outbuf,10,0,True);
181 SCVAL(cli->outbuf,smb_com,SMBreadbraw);
182 SSVAL(cli->outbuf,smb_tid,cli->cnum);
183 cli_setup_packet(cli);
185 SSVAL(cli->outbuf,smb_vwv0,fnum);
186 SIVAL(cli->outbuf,smb_vwv1,offset);
187 SSVAL(cli->outbuf,smb_vwv2,size);
188 SSVAL(cli->outbuf,smb_vwv3,size);
189 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
191 return cli_send_smb(cli);
194 /****************************************************************************
195 Tester for the readraw call.
196 ****************************************************************************/
198 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
200 char *p;
201 int size2;
202 size_t readsize;
203 ssize_t total = 0;
205 if (size == 0)
206 return 0;
209 * Set readsize to the maximum size we can handle in one readraw.
212 readsize = 0xFFFF;
214 while (total < size) {
215 readsize = MIN(readsize, size-total);
217 /* Issue a read and receive a reply */
219 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
220 return -1;
222 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
223 return -1;
225 size2 = smb_len(cli->inbuf);
227 if (size2 > readsize) {
228 DEBUG(5,("server returned more than we wanted!\n"));
229 return -1;
230 } else if (size2 < 0) {
231 DEBUG(5,("read return < 0!\n"));
232 return -1;
235 /* Copy data into buffer */
237 if (size2) {
238 p = cli->inbuf + 4;
239 memcpy(buf + total, p, size2);
242 total += size2;
243 offset += size2;
246 * If the server returned less than we asked for we're at EOF.
249 if (size2 < readsize)
250 break;
253 return total;
255 #endif
256 /****************************************************************************
257 issue a single SMBwrite and don't wait for a reply
258 ****************************************************************************/
260 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset,
261 uint16 mode, const char *buf,
262 size_t size, int i)
264 char *p;
265 BOOL large_writex = False;
267 if (size > cli->bufsize) {
268 cli->outbuf = (char *)SMB_REALLOC(cli->outbuf, size + 1024);
269 if (!cli->outbuf) {
270 return False;
272 cli->inbuf = (char *)SMB_REALLOC(cli->inbuf, size + 1024);
273 if (cli->inbuf == NULL) {
274 SAFE_FREE(cli->outbuf);
275 return False;
277 cli->bufsize = size + 1024;
280 memset(cli->outbuf,'\0',smb_size);
281 memset(cli->inbuf,'\0',smb_size);
283 if (((SMB_BIG_UINT)offset >> 32) || (size > 0xFFFF)) {
284 large_writex = True;
287 if (large_writex)
288 set_message(cli->outbuf,14,0,True);
289 else
290 set_message(cli->outbuf,12,0,True);
292 SCVAL(cli->outbuf,smb_com,SMBwriteX);
293 SSVAL(cli->outbuf,smb_tid,cli->cnum);
294 cli_setup_packet(cli);
296 SCVAL(cli->outbuf,smb_vwv0,0xFF);
297 SSVAL(cli->outbuf,smb_vwv2,fnum);
299 SIVAL(cli->outbuf,smb_vwv3,offset);
300 SIVAL(cli->outbuf,smb_vwv5,0);
301 SSVAL(cli->outbuf,smb_vwv7,mode);
303 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
305 * According to CIFS-TR-1p00, this following field should only
306 * be set if CAP_LARGE_WRITEX is set. We should check this
307 * locally. However, this check might already have been
308 * done by our callers.
310 SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
311 SSVAL(cli->outbuf,smb_vwv10,size);
312 SSVAL(cli->outbuf,smb_vwv11,
313 smb_buf(cli->outbuf) - smb_base(cli->outbuf));
315 if (large_writex) {
316 SIVAL(cli->outbuf,smb_vwv12,(((SMB_BIG_UINT)offset)>>32) & 0xffffffff);
319 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
320 memcpy(p, buf, size);
321 cli_setup_bcc(cli, p+size);
323 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
325 show_msg(cli->outbuf);
326 return cli_send_smb(cli);
329 /****************************************************************************
330 write to a file
331 write_mode: 0x0001 disallow write cacheing
332 0x0002 return bytes remaining
333 0x0004 use raw named pipe protocol
334 0x0008 start of message mode named pipe protocol
335 ****************************************************************************/
337 ssize_t cli_write(struct cli_state *cli,
338 int fnum, uint16 write_mode,
339 const char *buf, off_t offset, size_t size)
341 ssize_t bwritten = 0;
342 unsigned int issued = 0;
343 unsigned int received = 0;
344 int mpx = 1;
345 int block = cli->max_xmit - (smb_size+32);
346 int blocks = (size + (block-1)) / block;
348 if(cli->max_mux > 1) {
349 mpx = cli->max_mux-1;
350 } else {
351 mpx = 1;
354 while (received < blocks) {
356 while ((issued - received < mpx) && (issued < blocks)) {
357 ssize_t bsent = issued * block;
358 ssize_t size1 = MIN(block, size - bsent);
360 if (!cli_issue_write(cli, fnum, offset + bsent,
361 write_mode,
362 buf + bsent,
363 size1, issued))
364 return -1;
365 issued++;
368 if (!cli_receive_smb(cli))
369 return bwritten;
371 received++;
373 if (cli_is_error(cli))
374 break;
376 bwritten += SVAL(cli->inbuf, smb_vwv2);
377 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
380 while (received < issued && cli_receive_smb(cli))
381 received++;
383 return bwritten;
386 /****************************************************************************
387 write to a file using a SMBwrite and not bypassing 0 byte writes
388 ****************************************************************************/
390 ssize_t cli_smbwrite(struct cli_state *cli,
391 int fnum, char *buf, off_t offset, size_t size1)
393 char *p;
394 ssize_t total = 0;
396 do {
397 size_t size = MIN(size1, cli->max_xmit - 48);
399 memset(cli->outbuf,'\0',smb_size);
400 memset(cli->inbuf,'\0',smb_size);
402 set_message(cli->outbuf,5, 0,True);
404 SCVAL(cli->outbuf,smb_com,SMBwrite);
405 SSVAL(cli->outbuf,smb_tid,cli->cnum);
406 cli_setup_packet(cli);
408 SSVAL(cli->outbuf,smb_vwv0,fnum);
409 SSVAL(cli->outbuf,smb_vwv1,size);
410 SIVAL(cli->outbuf,smb_vwv2,offset);
411 SSVAL(cli->outbuf,smb_vwv4,0);
413 p = smb_buf(cli->outbuf);
414 *p++ = 1;
415 SSVAL(p, 0, size); p += 2;
416 memcpy(p, buf + total, size); p += size;
418 cli_setup_bcc(cli, p);
420 if (!cli_send_smb(cli))
421 return -1;
423 if (!cli_receive_smb(cli))
424 return -1;
426 if (cli_is_error(cli))
427 return -1;
429 size = SVAL(cli->inbuf,smb_vwv0);
430 if (size == 0)
431 break;
433 size1 -= size;
434 total += size;
435 offset += size;
437 } while (size1);
439 return total;