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