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