A rewrite of the error handling in the libsmb client code. I've separated
[nivanova/samba-autobuild/.git] / source3 / libsmb / clireadwrite.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    client file read/write routines
5    Copyright (C) Andrew Tridgell 1994-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #define NO_SYSLOG
23
24 #include "includes.h"
25
26 /****************************************************************************
27 issue a single SMBread and don't wait for a reply
28 ****************************************************************************/
29
30 static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset, 
31                            size_t size, int i)
32 {
33         memset(cli->outbuf,'\0',smb_size);
34         memset(cli->inbuf,'\0',smb_size);
35
36         set_message(cli->outbuf,10,0,True);
37                 
38         CVAL(cli->outbuf,smb_com) = SMBreadX;
39         SSVAL(cli->outbuf,smb_tid,cli->cnum);
40         cli_setup_packet(cli);
41
42         CVAL(cli->outbuf,smb_vwv0) = 0xFF;
43         SSVAL(cli->outbuf,smb_vwv2,fnum);
44         SIVAL(cli->outbuf,smb_vwv3,offset);
45         SSVAL(cli->outbuf,smb_vwv5,size);
46         SSVAL(cli->outbuf,smb_vwv6,size);
47         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
48
49         return cli_send_smb(cli);
50 }
51
52 /****************************************************************************
53   Read size bytes at offset offset using SMBreadX.
54 ****************************************************************************/
55
56 ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
57 {
58         char *p;
59         int size2;
60         int readsize;
61         ssize_t total = 0;
62
63         if (size == 0) 
64                 return 0;
65
66         /*
67          * Set readsize to the maximum size we can handle in one readX,
68          * rounded down to a multiple of 1024.
69          */
70
71         readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
72
73         while (total < size) {
74                 readsize = MIN(readsize, size-total);
75
76                 /* Issue a read and receive a reply */
77
78                 if (!cli_issue_read(cli, fnum, offset, readsize, 0))
79                         return -1;
80
81                 if (!cli_receive_smb(cli))
82                         return -1;
83
84                 /* Check for error.  Make sure to check for DOS and NT
85                    errors. */
86
87                 if (cli_is_error(cli)) {
88                         uint32 status = 0;
89                         uint8 eclass = 0;
90
91                         if (cli_is_nt_error(cli))
92                                 status = cli_nt_error(cli);
93                         else
94                                 cli_dos_error(cli, &eclass, &status);
95
96                         if ((eclass == ERRDOS && status == ERRmoredata) ||
97                             status == STATUS_MORE_ENTRIES)
98                                 return -1;
99                 }
100
101                 size2 = SVAL(cli->inbuf, smb_vwv5);
102
103                 if (size2 > readsize) {
104                         DEBUG(5,("server returned more than we wanted!\n"));
105                         return -1;
106                 } else if (size2 < 0) {
107                         DEBUG(5,("read return < 0!\n"));
108                         return -1;
109                 }
110
111                 /* Copy data into buffer */
112
113                 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
114                 memcpy(buf + total, p, size2);
115
116                 total += size2;
117                 offset += size2;
118
119                 /*
120                  * If the server returned less than we asked for we're at EOF.
121                  */
122
123                 if (size2 < readsize)
124                         break;
125         }
126
127         return total;
128 }
129
130 /****************************************************************************
131 issue a single SMBwrite and don't wait for a reply
132 ****************************************************************************/
133
134 static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset, uint16 mode, char *buf,
135                             size_t size, int i)
136 {
137         char *p;
138
139         memset(cli->outbuf,'\0',smb_size);
140         memset(cli->inbuf,'\0',smb_size);
141
142         if (size > 0xFFFF)
143                 set_message(cli->outbuf,14,0,True);
144         else
145                 set_message(cli->outbuf,12,0,True);
146         
147         CVAL(cli->outbuf,smb_com) = SMBwriteX;
148         SSVAL(cli->outbuf,smb_tid,cli->cnum);
149         cli_setup_packet(cli);
150         
151         CVAL(cli->outbuf,smb_vwv0) = 0xFF;
152         SSVAL(cli->outbuf,smb_vwv2,fnum);
153
154         SIVAL(cli->outbuf,smb_vwv3,offset);
155         SIVAL(cli->outbuf,smb_vwv5,(mode & 0x0008) ? 0xFFFFFFFF : 0);
156         SSVAL(cli->outbuf,smb_vwv7,mode);
157
158         SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
159         SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
160         SSVAL(cli->outbuf,smb_vwv10,size);
161         SSVAL(cli->outbuf,smb_vwv11,
162               smb_buf(cli->outbuf) - smb_base(cli->outbuf));
163         
164         p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
165         memcpy(p, buf, size);
166         cli_setup_bcc(cli, p+size);
167
168         SSVAL(cli->outbuf,smb_mid,cli->mid + i);
169         
170         show_msg(cli->outbuf);
171         return cli_send_smb(cli);
172 }
173
174 /****************************************************************************
175   write to a file
176   write_mode: 0x0001 disallow write cacheing
177               0x0002 return bytes remaining
178               0x0004 use raw named pipe protocol
179               0x0008 start of message mode named pipe protocol
180 ****************************************************************************/
181
182 ssize_t cli_write(struct cli_state *cli,
183                   int fnum, uint16 write_mode,
184                   char *buf, off_t offset, size_t size)
185 {
186         int bwritten = 0;
187         int issued = 0;
188         int received = 0;
189         int mpx = MAX(cli->max_mux-1, 1);
190         int block = (cli->max_xmit - (smb_size+32)) & ~1023;
191         int blocks = (size + (block-1)) / block;
192
193         while (received < blocks) {
194
195                 while ((issued - received < mpx) && (issued < blocks)) {
196                         int bsent = issued * block;
197                         int size1 = MIN(block, size - bsent);
198
199                         if (!cli_issue_write(cli, fnum, offset + bsent,
200                                         write_mode,
201                                         buf + bsent,
202                                         size1, issued))
203                                 return -1;
204                         issued++;
205                 }
206
207                 if (!cli_receive_smb(cli))
208                         return bwritten;
209
210                 received++;
211
212                 if (CVAL(cli->inbuf,smb_rcls) != 0)
213                         break;
214
215                 bwritten += SVAL(cli->inbuf, smb_vwv2);
216         }
217
218         while (received < issued && cli_receive_smb(cli))
219                 received++;
220         
221         return bwritten;
222 }
223
224 /****************************************************************************
225   write to a file using a SMBwrite and not bypassing 0 byte writes
226 ****************************************************************************/
227
228 ssize_t cli_smbwrite(struct cli_state *cli,
229                      int fnum, char *buf, off_t offset, size_t size1)
230 {
231         char *p;
232         ssize_t total = 0;
233
234         do {
235                 size_t size = MIN(size1, cli->max_xmit - 48);
236                 
237                 memset(cli->outbuf,'\0',smb_size);
238                 memset(cli->inbuf,'\0',smb_size);
239
240                 set_message(cli->outbuf,5, 0,True);
241
242                 CVAL(cli->outbuf,smb_com) = SMBwrite;
243                 SSVAL(cli->outbuf,smb_tid,cli->cnum);
244                 cli_setup_packet(cli);
245                 
246                 SSVAL(cli->outbuf,smb_vwv0,fnum);
247                 SSVAL(cli->outbuf,smb_vwv1,size);
248                 SIVAL(cli->outbuf,smb_vwv2,offset);
249                 SSVAL(cli->outbuf,smb_vwv4,0);
250                 
251                 p = smb_buf(cli->outbuf);
252                 *p++ = 1;
253                 SSVAL(p, 0, size); p += 2;
254                 memcpy(p, buf, size); p += size;
255
256                 cli_setup_bcc(cli, p);
257                 
258                 if (!cli_send_smb(cli))
259                         return -1;
260
261                 if (!cli_receive_smb(cli))
262                         return -1;
263                 
264                 if (CVAL(cli->inbuf,smb_rcls) != 0)
265                         return -1;
266
267                 size = SVAL(cli->inbuf,smb_vwv0);
268                 if (size == 0)
269                         break;
270
271                 size1 -= size;
272                 total += size;
273         } while (size1);
274
275         return total;
276 }