r9413: Bring Samba4 back up to date with lorikeet-heimdal.
[Samba/aatanasov.git] / source / libcli / clireadwrite.c
blob4248ac42860cc8ed11653dacb5ce0ef67c25b468
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
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.
22 #include "includes.h"
23 #include "libcli/raw/libcliraw.h"
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)
31 uint8_t *buf = _buf;
32 union smb_read parms;
33 int readsize;
34 ssize_t total = 0;
36 if (size == 0) {
37 return 0;
40 parms.readx.level = RAW_READ_READX;
41 parms.readx.in.fnum = fnum;
44 * Set readsize to the maximum size we can handle in one readX,
45 * rounded down to a multiple of 1024.
47 readsize = (tree->session->transport->negotiate.max_xmit - (MIN_SMB_SIZE+32));
48 if (readsize > 0xFFFF) readsize = 0xFFFF;
50 while (total < size) {
51 NTSTATUS status;
53 readsize = MIN(readsize, size-total);
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;
61 status = smb_raw_read(tree, &parms);
63 if (!NT_STATUS_IS_OK(status)) {
64 return -1;
67 total += parms.readx.out.nread;
68 offset += parms.readx.out.nread;
70 /* If the server returned less than we asked for we're at EOF */
71 if (parms.readx.out.nread < readsize)
72 break;
75 return total;
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)
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;
95 if (size == 0) {
96 return 0;
99 if (block > 0xFFFF) block = 0xFFFF;
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;
107 while (total < size) {
108 NTSTATUS status;
110 block = MIN(block, size - total);
112 parms.writex.in.offset = offset;
113 parms.writex.in.count = block;
114 parms.writex.in.data = buf;
116 status = smb_raw_write(tree, &parms);
118 if (!NT_STATUS_IS_OK(status)) {
119 return -1;
122 offset += parms.writex.out.nwritten;
123 total += parms.writex.out.nwritten;
124 buf += parms.writex.out.nwritten;
127 return total;
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)
136 const uint8_t *buf = _buf;
137 union smb_write parms;
138 ssize_t total = 0;
140 parms.write.level = RAW_WRITE_WRITE;
141 parms.write.in.remaining = 0;
143 do {
144 size_t size = MIN(size1, tree->session->transport->negotiate.max_xmit - 48);
145 if (size > 0xFFFF) size = 0xFFFF;
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;
152 if (NT_STATUS_IS_ERR(smb_raw_write(tree, &parms)))
153 return -1;
155 size = parms.write.out.nwritten;
156 if (size == 0)
157 break;
159 size1 -= size;
160 total += size;
161 offset += size;
162 } while (size1);
164 return total;