2 Unix SMB/Netbios implementation.
4 client file read/write routines
5 Copyright (C) Andrew Tridgell 1994-1998
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.
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.
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.
26 /****************************************************************************
27 issue a single SMBread and don't wait for a reply
28 ****************************************************************************/
29 static void 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 CVAL(cli->outbuf,smb_com) = SMBreadX;
38 SSVAL(cli->outbuf,smb_tid,cli->cnum);
39 cli_setup_packet(cli);
41 CVAL(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);
51 /****************************************************************************
53 ****************************************************************************/
54 size_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
59 * There is a problem in this code when mpx is more than one.
60 * for some reason files can get corrupted when being read.
61 * Until we understand this fully I am serializing reads (one
62 * read/one reply) for now. JRA.
65 int mpx = MAX(cli->max_mux-1, 1);
72 /* issued is the number of readX requests we have sent so far */
74 /* received is the number of readX replies we have received */
77 /* maybe its a very silly request? */
78 if (size == 0) return 0;
80 /* set block to the maximum size we can handle in one readX,
81 rounded down to a multiple of 1024 */
82 block = (cli->max_xmit - (smb_size+32)) & ~1023;
84 /* work out how many readX calls we will need in total */
85 blocks = (size + (block-1)) / block;
87 while (received < blocks) {
90 while (issued - received < mpx && issued < blocks) {
91 int size1 = MIN(block, size-issued*block);
92 cli_issue_read(cli, fnum, offset+issued*block, size1, issued);
96 if (!cli_receive_smb(cli)) {
101 mid = SVAL(cli->inbuf, smb_mid) - cli->mid;
102 size2 = SVAL(cli->inbuf, smb_vwv5);
104 if (CVAL(cli->inbuf,smb_rcls) != 0) {
105 blocks = MIN(blocks, mid-1);
110 blocks = MIN(blocks, mid-1);
111 /* this distinguishes EOF from an error */
112 total = MAX(total, 0);
117 DEBUG(0,("server returned more than we wanted!\n"));
121 DEBUG(0,("invalid mid from server!\n"));
124 p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
126 memcpy(buf+mid*block, p, size2);
128 total = MAX(total, mid*block + size2);
131 while (received < issued) {
132 cli_receive_smb(cli);
140 /****************************************************************************
141 issue a single SMBwrite and don't wait for a reply
142 ****************************************************************************/
143 static void cli_issue_write(struct cli_state *cli, int fnum, off_t offset, uint16 mode, char *buf,
148 memset(cli->outbuf,'\0',smb_size);
149 memset(cli->inbuf,'\0',smb_size);
152 set_message(cli->outbuf,14,0,True);
154 set_message(cli->outbuf,12,0,True);
156 CVAL(cli->outbuf,smb_com) = SMBwriteX;
157 SSVAL(cli->outbuf,smb_tid,cli->cnum);
158 cli_setup_packet(cli);
160 CVAL(cli->outbuf,smb_vwv0) = 0xFF;
161 SSVAL(cli->outbuf,smb_vwv2,fnum);
163 SIVAL(cli->outbuf,smb_vwv3,offset);
164 SIVAL(cli->outbuf,smb_vwv5,(mode & 0x0008) ? 0xFFFFFFFF : 0);
165 SSVAL(cli->outbuf,smb_vwv7,mode);
167 SSVAL(cli->outbuf,smb_vwv8,(mode & 0x0008) ? size : 0);
168 SSVAL(cli->outbuf,smb_vwv9,((size>>16)&1));
169 SSVAL(cli->outbuf,smb_vwv10,size);
170 SSVAL(cli->outbuf,smb_vwv11,
171 smb_buf(cli->outbuf) - smb_base(cli->outbuf));
173 p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
174 memcpy(p, buf, size);
175 cli_setup_bcc(cli, p+size);
177 SSVAL(cli->outbuf,smb_mid,cli->mid + i);
179 show_msg(cli->outbuf);
183 /****************************************************************************
185 write_mode: 0x0001 disallow write cacheing
186 0x0002 return bytes remaining
187 0x0004 use raw named pipe protocol
188 0x0008 start of message mode named pipe protocol
189 ****************************************************************************/
190 ssize_t cli_write(struct cli_state *cli,
191 int fnum, uint16 write_mode,
192 char *buf, off_t offset, size_t size)
197 int mpx = MAX(cli->max_mux-1, 1);
198 int block = (cli->max_xmit - (smb_size+32)) & ~1023;
199 int blocks = (size + (block-1)) / block;
201 while (received < blocks) {
203 while ((issued - received < mpx) && (issued < blocks))
205 int bsent = issued * block;
206 int size1 = MIN(block, size - bsent);
208 cli_issue_write(cli, fnum, offset + bsent,
215 if (!cli_receive_smb(cli))
222 if (CVAL(cli->inbuf,smb_rcls) != 0)
227 bwritten += SVAL(cli->inbuf, smb_vwv2);
230 while (received < issued && cli_receive_smb(cli))
239 /****************************************************************************
240 write to a file using a SMBwrite and not bypassing 0 byte writes
241 ****************************************************************************/
242 ssize_t cli_smbwrite(struct cli_state *cli,
243 int fnum, char *buf, off_t offset, size_t size1)
249 size_t size = MIN(size1, cli->max_xmit - 48);
251 memset(cli->outbuf,'\0',smb_size);
252 memset(cli->inbuf,'\0',smb_size);
254 set_message(cli->outbuf,5, 0,True);
256 CVAL(cli->outbuf,smb_com) = SMBwrite;
257 SSVAL(cli->outbuf,smb_tid,cli->cnum);
258 cli_setup_packet(cli);
260 SSVAL(cli->outbuf,smb_vwv0,fnum);
261 SSVAL(cli->outbuf,smb_vwv1,size);
262 SIVAL(cli->outbuf,smb_vwv2,offset);
263 SSVAL(cli->outbuf,smb_vwv4,0);
265 p = smb_buf(cli->outbuf);
267 SSVAL(p, 0, size); p += 2;
268 memcpy(p, buf, size); p += size;
270 cli_setup_bcc(cli, p);
273 if (!cli_receive_smb(cli)) {
277 if (CVAL(cli->inbuf,smb_rcls) != 0) {
281 size = SVAL(cli->inbuf,smb_vwv0);
282 if (size == 0) break;