Test against W2K that we're doing large read/writes correctly (we are).
[sfrench/samba-autobuild/.git] / source3 / libsmb / clireadwrite.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client file read/write routines
4    Copyright (C) Andrew Tridgell 1994-1998
5    
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.
10    
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.
15    
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.
19 */
20
21 #define NO_SYSLOG
22
23 #include "includes.h"
24
25 /****************************************************************************
26 Issue a single SMBread and don't wait for a reply.
27 ****************************************************************************/
28
29 static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset, 
30                            size_t size, int i)
31 {
32         memset(cli->outbuf,'\0',smb_size);
33         memset(cli->inbuf,'\0',smb_size);
34
35         set_message(cli->outbuf,10,0,True);
36                 
37         SCVAL(cli->outbuf,smb_com,SMBreadX);
38         SSVAL(cli->outbuf,smb_tid,cli->cnum);
39         cli_setup_packet(cli);
40
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);
47
48         return cli_send_smb(cli);
49 }
50
51 /****************************************************************************
52 Issue a single SMBreadraw and don't wait for a reply.
53 ****************************************************************************/
54
55 static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset, 
56                            size_t size, int i)
57 {
58         memset(cli->outbuf,'\0',smb_size);
59         memset(cli->inbuf,'\0',smb_size);
60
61         set_message(cli->outbuf,10,0,True);
62                 
63         SCVAL(cli->outbuf,smb_com,SMBreadbraw);
64         SSVAL(cli->outbuf,smb_tid,cli->cnum);
65         cli_setup_packet(cli);
66
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);
72
73         return cli_send_smb(cli);
74 }
75
76 /****************************************************************************
77   Read size bytes at offset offset using SMBreadX.
78 ****************************************************************************/
79
80 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
81 {
82         char *p;
83         int size2;
84         int readsize;
85         ssize_t total = 0;
86
87         if (size == 0) 
88                 return 0;
89
90         /*
91          * Set readsize to the maximum size we can handle in one readX,
92          * rounded down to a multiple of 1024.
93          */
94
95         readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
96
97         while (total < size) {
98                 readsize = MIN(readsize, size-total);
99
100                 /* Issue a read and receive a reply */
101
102                 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
103                         return -1;
104
105                 if (!cli_receive_smb(cli))
106                         return -1;
107
108                 /* Check for error.  Make sure to check for DOS and NT
109                    errors. */
110
111                 if (cli_is_error(cli)) {
112                         NTSTATUS status = NT_STATUS_OK;
113                         uint8 eclass = 0;
114                         uint32 ecode = 0;
115
116                         if (cli_is_nt_error(cli))
117                                 status = cli_nt_error(cli);
118                         else
119                                 cli_dos_error(cli, &eclass, &ecode);
120
121                         if ((eclass == ERRDOS && ecode == ERRmoredata) ||
122                             NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
123                                 return -1;
124                 }
125
126                 size2 = SVAL(cli->inbuf, smb_vwv5);
127
128                 if (size2 > readsize) {
129                         DEBUG(5,("server returned more than we wanted!\n"));
130                         return -1;
131                 } else if (size2 < 0) {
132                         DEBUG(5,("read return < 0!\n"));
133                         return -1;
134                 }
135
136                 /* Copy data into buffer */
137
138                 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
139                 memcpy(buf + total, p, size2);
140
141                 total += size2;
142                 offset += size2;
143
144                 /*
145                  * If the server returned less than we asked for we're at EOF.
146                  */
147
148                 if (size2 < readsize)
149                         break;
150         }
151
152         return total;
153 }
154
155 /****************************************************************************
156  Tester for the readraw call.
157 ****************************************************************************/
158
159 ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
160 {
161         char *p;
162         int size2;
163         size_t readsize;
164         ssize_t total = 0;
165
166         if (size == 0) 
167                 return 0;
168
169         /*
170          * Set readsize to the maximum size we can handle in one readraw.
171          */
172
173         readsize = 0xFFFF;
174
175         while (total < size) {
176                 readsize = MIN(readsize, size-total);
177
178                 /* Issue a read and receive a reply */
179
180                 if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
181                         return -1;
182
183                 if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
184                         return -1;
185
186                 size2 = smb_len(cli->inbuf);
187
188                 if (size2 > readsize) {
189                         DEBUG(5,("server returned more than we wanted!\n"));
190                         return -1;
191                 } else if (size2 < 0) {
192                         DEBUG(5,("read return < 0!\n"));
193                         return -1;
194                 }
195
196                 /* Copy data into buffer */
197
198                 if (size2) {
199                         p = cli->inbuf + 4;
200                         memcpy(buf + total, p, size2);
201                 }
202
203                 total += size2;
204                 offset += size2;
205
206                 /*
207                  * If the server returned less than we asked for we're at EOF.
208                  */
209
210                 if (size2 < readsize)
211                         break;
212         }
213
214         return total;
215 }
216
217 /****************************************************************************
218 issue a single SMBwrite and don't wait for a reply
219 ****************************************************************************/
220
221 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset, uint16 mode, char *buf,
222                             size_t size, int i)
223 {
224         char *p;
225
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)
230                         return False;
231                 cli->bufsize = size + 1024;
232         }
233
234         memset(cli->outbuf,'\0',smb_size);
235         memset(cli->inbuf,'\0',smb_size);
236
237         if (size > 0xFFFF)
238                 set_message(cli->outbuf,14,0,True);
239         else
240                 set_message(cli->outbuf,12,0,True);
241         
242         SCVAL(cli->outbuf,smb_com,SMBwriteX);
243         SSVAL(cli->outbuf,smb_tid,cli->cnum);
244         cli_setup_packet(cli);
245         
246         SCVAL(cli->outbuf,smb_vwv0,0xFF);
247         SSVAL(cli->outbuf,smb_vwv2,fnum);
248
249         SIVAL(cli->outbuf,smb_vwv3,offset);
250         SIVAL(cli->outbuf,smb_vwv5,(mode & 0x0008) ? 0xFFFFFFFF : 0);
251         SSVAL(cli->outbuf,smb_vwv7,mode);
252
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));
258         
259         p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
260         memcpy(p, buf, size);
261         cli_setup_bcc(cli, p+size);
262
263         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
264         
265         show_msg(cli->outbuf);
266         return cli_send_smb(cli);
267 }
268
269 /****************************************************************************
270   write to a file
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 ****************************************************************************/
276
277 ssize_t cli_write(struct cli_state *cli,
278                   int fnum, uint16 write_mode,
279                   char *buf, off_t offset, size_t size)
280 {
281         int bwritten = 0;
282         int issued = 0;
283         int received = 0;
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;
287
288         while (received < blocks) {
289
290                 while ((issued - received < mpx) && (issued < blocks)) {
291                         int bsent = issued * block;
292                         int size1 = MIN(block, size - bsent);
293
294                         if (!cli_issue_write(cli, fnum, offset + bsent,
295                                         write_mode,
296                                         buf + bsent,
297                                         size1, issued))
298                                 return -1;
299                         issued++;
300                 }
301
302                 if (!cli_receive_smb(cli))
303                         return bwritten;
304
305                 received++;
306
307                 if (cli_is_error(cli))
308                         break;
309
310                 bwritten += SVAL(cli->inbuf, smb_vwv2);
311                 bwritten += (((int)(SVAL(cli->inbuf, smb_vwv4)))>>16);
312         }
313
314         while (received < issued && cli_receive_smb(cli))
315                 received++;
316         
317         return bwritten;
318 }
319
320 /****************************************************************************
321   write to a file using a SMBwrite and not bypassing 0 byte writes
322 ****************************************************************************/
323
324 ssize_t cli_smbwrite(struct cli_state *cli,
325                      int fnum, char *buf, off_t offset, size_t size1)
326 {
327         char *p;
328         ssize_t total = 0;
329
330         do {
331                 size_t size = MIN(size1, cli->max_xmit - 48);
332                 
333                 memset(cli->outbuf,'\0',smb_size);
334                 memset(cli->inbuf,'\0',smb_size);
335
336                 set_message(cli->outbuf,5, 0,True);
337
338                 SCVAL(cli->outbuf,smb_com,SMBwrite);
339                 SSVAL(cli->outbuf,smb_tid,cli->cnum);
340                 cli_setup_packet(cli);
341                 
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);
346                 
347                 p = smb_buf(cli->outbuf);
348                 *p++ = 1;
349                 SSVAL(p, 0, size); p += 2;
350                 memcpy(p, buf, size); p += size;
351
352                 cli_setup_bcc(cli, p);
353                 
354                 if (!cli_send_smb(cli))
355                         return -1;
356
357                 if (!cli_receive_smb(cli))
358                         return -1;
359                 
360                 if (cli_is_error(cli))
361                         return -1;
362
363                 size = SVAL(cli->inbuf,smb_vwv0);
364                 if (size == 0)
365                         break;
366
367                 size1 -= size;
368                 total += size;
369         } while (size1);
370
371         return total;
372 }