r3220: merging current 3.0 code to release branch
[Samba.git] / source / libsmb / clireadwrite.c
blobd1a23d36c8b0cc568f5afdba1312401492494a2b
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 #define NO_SYSLOG
23 #include "includes.h"
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,
30 size_t size, int i)
32 BOOL bigoffset = False;
34 memset(cli->outbuf,'\0',smb_size);
35 memset(cli->inbuf,'\0',smb_size);
37 if ((SMB_BIG_UINT)offset >> 32)
38 bigoffset = True;
40 set_message(cli->outbuf,bigoffset ? 12 : 10,0,True);
42 SCVAL(cli->outbuf,smb_com,SMBreadX);
43 SSVAL(cli->outbuf,smb_tid,cli->cnum);
44 cli_setup_packet(cli);
46 SCVAL(cli->outbuf,smb_vwv0,0xFF);
47 SSVAL(cli->outbuf,smb_vwv2,fnum);
48 SIVAL(cli->outbuf,smb_vwv3,offset);
49 SSVAL(cli->outbuf,smb_vwv5,size);
50 SSVAL(cli->outbuf,smb_vwv6,size);
51 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
53 if (bigoffset)
54 SIVAL(cli->outbuf,smb_vwv10,(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 readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
80 while (total < size) {
81 readsize = MIN(readsize, size-total);
83 /* Issue a read and receive a reply */
85 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
86 return -1;
88 if (!cli_receive_smb(cli))
89 return -1;
91 /* Check for error. Make sure to check for DOS and NT
92 errors. */
94 if (cli_is_error(cli)) {
95 BOOL recoverable_error = False;
96 NTSTATUS status = NT_STATUS_OK;
97 uint8 eclass = 0;
98 uint32 ecode = 0;
100 if (cli_is_nt_error(cli))
101 status = cli_nt_error(cli);
102 else
103 cli_dos_error(cli, &eclass, &ecode);
106 * ERRDOS ERRmoredata or STATUS_MORE_ENRTIES is a
107 * recoverable error, plus we have valid data in the
108 * packet so don't error out here.
111 if ((eclass == ERRDOS && ecode == ERRmoredata) ||
112 NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
113 recoverable_error = True;
115 if (!recoverable_error)
116 return -1;
119 size2 = SVAL(cli->inbuf, smb_vwv5);
121 if (size2 > readsize) {
122 DEBUG(5,("server returned more than we wanted!\n"));
123 return -1;
124 } else if (size2 < 0) {
125 DEBUG(5,("read return < 0!\n"));
126 return -1;
129 /* Copy data into buffer */
131 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
132 memcpy(buf + total, p, size2);
134 total += size2;
135 offset += size2;
138 * If the server returned less than we asked for we're at EOF.
141 if (size2 < readsize)
142 break;
145 return total;
148 #if 0 /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
150 /* This call is INCOMPATIBLE with SMB signing. If you remove the #if 0
151 you must fix ensure you don't attempt to sign the packets - data
152 *will* be currupted */
154 /****************************************************************************
155 Issue a single SMBreadraw and don't wait for a reply.
156 ****************************************************************************/
158 static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset,
159 size_t size, int i)
162 if (!cli->sign_info.use_smb_signing) {
163 DEBUG(0, ("Cannot use readraw and SMB Signing\n"));
164 return False;
167 memset(cli->outbuf,'\0',smb_size);
168 memset(cli->inbuf,'\0',smb_size);
170 set_message(cli->outbuf,10,0,True);
172 SCVAL(cli->outbuf,smb_com,SMBreadbraw);
173 SSVAL(cli->outbuf,smb_tid,cli->cnum);
174 cli_setup_packet(cli);
176 SSVAL(cli->outbuf,smb_vwv0,fnum);
177 SIVAL(cli->outbuf,smb_vwv1,offset);
178 SSVAL(cli->outbuf,smb_vwv2,size);
179 SSVAL(cli->outbuf,smb_vwv3,size);
180 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
182 return cli_send_smb(cli);
185 /****************************************************************************
186 Tester for the readraw call.
187 ****************************************************************************/
189 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
191 char *p;
192 int size2;
193 size_t readsize;
194 ssize_t total = 0;
196 if (size == 0)
197 return 0;
200 * Set readsize to the maximum size we can handle in one readraw.
203 readsize = 0xFFFF;
205 while (total < size) {
206 readsize = MIN(readsize, size-total);
208 /* Issue a read and receive a reply */
210 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
211 return -1;
213 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
214 return -1;
216 size2 = smb_len(cli->inbuf);
218 if (size2 > readsize) {
219 DEBUG(5,("server returned more than we wanted!\n"));
220 return -1;
221 } else if (size2 < 0) {
222 DEBUG(5,("read return < 0!\n"));
223 return -1;
226 /* Copy data into buffer */
228 if (size2) {
229 p = cli->inbuf + 4;
230 memcpy(buf + total, p, size2);
233 total += size2;
234 offset += size2;
237 * If the server returned less than we asked for we're at EOF.
240 if (size2 < readsize)
241 break;
244 return total;
246 #endif
247 /****************************************************************************
248 issue a single SMBwrite and don't wait for a reply
249 ****************************************************************************/
251 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset,
252 uint16 mode, const char *buf,
253 size_t size, int i)
255 char *p;
256 BOOL bigoffset = False;
258 if (size > cli->bufsize) {
259 cli->outbuf = realloc(cli->outbuf, size + 1024);
260 cli->inbuf = realloc(cli->inbuf, size + 1024);
261 if (cli->outbuf == NULL || cli->inbuf == NULL)
262 return False;
263 cli->bufsize = size + 1024;
266 memset(cli->outbuf,'\0',smb_size);
267 memset(cli->inbuf,'\0',smb_size);
269 if ((SMB_BIG_UINT)offset >> 32)
270 bigoffset = True;
272 if (bigoffset)
273 set_message(cli->outbuf,14,0,True);
274 else
275 set_message(cli->outbuf,12,0,True);
277 SCVAL(cli->outbuf,smb_com,SMBwriteX);
278 SSVAL(cli->outbuf,smb_tid,cli->cnum);
279 cli_setup_packet(cli);
281 SCVAL(cli->outbuf,smb_vwv0,0xFF);
282 SSVAL(cli->outbuf,smb_vwv2,fnum);
284 SIVAL(cli->outbuf,smb_vwv3,offset);
285 SIVAL(cli->outbuf,smb_vwv5,0);
286 SSVAL(cli->outbuf,smb_vwv7,mode);
288 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
290 * According to CIFS-TR-1p00, this following field should only
291 * be set if CAP_LARGE_WRITEX is set. We should check this
292 * locally. However, this check might already have been
293 * done by our callers.
295 SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
296 SSVAL(cli->outbuf,smb_vwv10,size);
297 SSVAL(cli->outbuf,smb_vwv11,
298 smb_buf(cli->outbuf) - smb_base(cli->outbuf));
300 if (bigoffset)
301 SIVAL(cli->outbuf,smb_vwv12,(offset>>32) & 0xffffffff);
303 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
304 memcpy(p, buf, size);
305 cli_setup_bcc(cli, p+size);
307 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
309 show_msg(cli->outbuf);
310 return cli_send_smb(cli);
313 /****************************************************************************
314 write to a file
315 write_mode: 0x0001 disallow write cacheing
316 0x0002 return bytes remaining
317 0x0004 use raw named pipe protocol
318 0x0008 start of message mode named pipe protocol
319 ****************************************************************************/
321 size_t cli_write(struct cli_state *cli,
322 int fnum, uint16 write_mode,
323 const char *buf, off_t offset, size_t size)
325 int bwritten = 0;
326 int issued = 0;
327 int received = 0;
328 int mpx = 1;
329 int block = cli->max_xmit - (smb_size+32);
330 int blocks = (size + (block-1)) / block;
332 if(cli->max_mux > 1) {
333 mpx = cli->max_mux-1;
334 } else {
335 mpx = 1;
338 while (received < blocks) {
340 while ((issued - received < mpx) && (issued < blocks)) {
341 int bsent = issued * block;
342 int size1 = MIN(block, size - bsent);
344 if (!cli_issue_write(cli, fnum, offset + bsent,
345 write_mode,
346 buf + bsent,
347 size1, issued))
348 return -1;
349 issued++;
352 if (!cli_receive_smb(cli))
353 return bwritten;
355 received++;
357 if (cli_is_error(cli))
358 break;
360 bwritten += SVAL(cli->inbuf, smb_vwv2);
361 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))<<16);
364 while (received < issued && cli_receive_smb(cli))
365 received++;
367 return bwritten;
370 /****************************************************************************
371 write to a file using a SMBwrite and not bypassing 0 byte writes
372 ****************************************************************************/
374 ssize_t cli_smbwrite(struct cli_state *cli,
375 int fnum, char *buf, off_t offset, size_t size1)
377 char *p;
378 ssize_t total = 0;
380 do {
381 size_t size = MIN(size1, cli->max_xmit - 48);
383 memset(cli->outbuf,'\0',smb_size);
384 memset(cli->inbuf,'\0',smb_size);
386 set_message(cli->outbuf,5, 0,True);
388 SCVAL(cli->outbuf,smb_com,SMBwrite);
389 SSVAL(cli->outbuf,smb_tid,cli->cnum);
390 cli_setup_packet(cli);
392 SSVAL(cli->outbuf,smb_vwv0,fnum);
393 SSVAL(cli->outbuf,smb_vwv1,size);
394 SIVAL(cli->outbuf,smb_vwv2,offset);
395 SSVAL(cli->outbuf,smb_vwv4,0);
397 p = smb_buf(cli->outbuf);
398 *p++ = 1;
399 SSVAL(p, 0, size); p += 2;
400 memcpy(p, buf, size); p += size;
402 cli_setup_bcc(cli, p);
404 if (!cli_send_smb(cli))
405 return -1;
407 if (!cli_receive_smb(cli))
408 return -1;
410 if (cli_is_error(cli))
411 return -1;
413 size = SVAL(cli->inbuf,smb_vwv0);
414 if (size == 0)
415 break;
417 size1 -= size;
418 total += size;
419 offset += size;
421 } while (size1);
423 return total;