r3352: make smbcli_read() and smbcli_write() work with very small negotiated SMB...
[jelmer/samba4-debian.git] / source / libcli / clireadwrite.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client file read/write routines
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) James Myers 2003
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 #include "includes.h"
23
24 /****************************************************************************
25   Read size bytes at offset offset using SMBreadX.
26 ****************************************************************************/
27 ssize_t smbcli_read(struct smbcli_tree *tree, int fnum, char *buf, off_t offset, 
28                  size_t size)
29 {
30         union smb_read parms;
31         int readsize;
32         ssize_t total = 0;
33         
34         if (size == 0) {
35                 return 0;
36         }
37
38         parms.readx.level = RAW_READ_READX;
39         parms.readx.in.fnum = fnum;
40
41         /*
42          * Set readsize to the maximum size we can handle in one readX,
43          * rounded down to a multiple of 1024.
44          */
45         readsize = (tree->session->transport->negotiate.max_xmit - (MIN_SMB_SIZE+32));
46         if (readsize > 0xFFFF) readsize = 0xFFFF;
47
48         while (total < size) {
49                 NTSTATUS status;
50
51                 readsize = MIN(readsize, size-total);
52
53                 parms.readx.in.offset    = offset;
54                 parms.readx.in.mincnt    = readsize;
55                 parms.readx.in.maxcnt    = readsize;
56                 parms.readx.in.remaining = size - total;
57                 parms.readx.out.data     = buf + total;
58                 
59                 status = smb_raw_read(tree, &parms);
60                 
61                 if (!NT_STATUS_IS_OK(status)) {
62                         return -1;
63                 }
64
65                 total += parms.readx.out.nread;
66                 offset += parms.readx.out.nread;
67
68                 /* If the server returned less than we asked for we're at EOF */
69                 if (parms.readx.out.nread < readsize)
70                         break;
71         }
72
73         return total;
74 }
75
76
77 /****************************************************************************
78   write to a file
79   write_mode: 0x0001 disallow write cacheing
80               0x0002 return bytes remaining
81               0x0004 use raw named pipe protocol
82               0x0008 start of message mode named pipe protocol
83 ****************************************************************************/
84 ssize_t smbcli_write(struct smbcli_tree *tree,
85                      int fnum, uint16_t write_mode,
86                      const uint8_t *buf, off_t offset, size_t size)
87 {
88         union smb_write parms;
89         int block = (tree->session->transport->negotiate.max_xmit - (MIN_SMB_SIZE+32));
90         ssize_t total = 0;
91
92         if (size == 0) {
93                 return 0;
94         }
95
96         if (block > 0xFFFF) block = 0xFFFF;
97
98
99         parms.writex.level = RAW_WRITE_WRITEX;
100         parms.writex.in.fnum = fnum;
101         parms.writex.in.wmode = write_mode;
102         parms.writex.in.remaining = 0;
103         
104         while (total < size) {
105                 NTSTATUS status;
106
107                 block = MIN(block, size - total);
108
109                 parms.writex.in.offset = offset;
110                 parms.writex.in.count = block;
111                 parms.writex.in.data = buf;
112
113                 status = smb_raw_write(tree, &parms);
114
115                 if (!NT_STATUS_IS_OK(status)) {
116                         return -1;
117                 }
118
119                 offset += parms.writex.out.nwritten;
120                 total += parms.writex.out.nwritten;
121                 buf += parms.writex.out.nwritten;
122         }
123
124         return total;
125 }
126
127 /****************************************************************************
128   write to a file using a SMBwrite and not bypassing 0 byte writes
129 ****************************************************************************/
130 ssize_t smbcli_smbwrite(struct smbcli_tree *tree,
131                      int fnum, char *buf, off_t offset, size_t size1)
132 {
133         union smb_write parms;
134         ssize_t total = 0;
135
136         parms.write.level = RAW_WRITE_WRITE;
137         parms.write.in.remaining = 0;
138         
139         do {
140                 size_t size = MIN(size1, tree->session->transport->negotiate.max_xmit - 48);
141                 if (size > 0xFFFF) size = 0xFFFF;
142                 
143                 parms.write.in.fnum = fnum;
144                 parms.write.in.offset = offset;
145                 parms.write.in.count = size;
146                 parms.write.in.data = buf + total;
147
148                 if (NT_STATUS_IS_ERR(smb_raw_write(tree, &parms)))
149                         return -1;
150
151                 size = parms.write.out.nwritten;
152                 if (size == 0)
153                         break;
154
155                 size1 -= size;
156                 total += size;
157                 offset += size;
158         } while (size1);
159
160         return total;
161 }