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.
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,
38 parms.readx.level = RAW_READ_READX;
39 parms.readx.in.fnum = fnum;
42 * Set readsize to the maximum size we can handle in one readX,
43 * rounded down to a multiple of 1024.
45 readsize = (tree->session->transport->negotiate.max_xmit - (MIN_SMB_SIZE+32));
46 if (readsize > 0xFFFF) readsize = 0xFFFF;
48 while (total < size) {
51 readsize = MIN(readsize, size-total);
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;
59 status = smb_raw_read(tree, &parms);
61 if (!NT_STATUS_IS_OK(status)) {
65 total += parms.readx.out.nread;
66 offset += parms.readx.out.nread;
68 /* If the server returned less than we asked for we're at EOF */
69 if (parms.readx.out.nread < readsize)
77 /****************************************************************************
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)
88 union smb_write parms;
89 int block = (tree->session->transport->negotiate.max_xmit - (MIN_SMB_SIZE+32));
96 if (block > 0xFFFF) block = 0xFFFF;
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;
104 while (total < size) {
107 block = MIN(block, size - total);
109 parms.writex.in.offset = offset;
110 parms.writex.in.count = block;
111 parms.writex.in.data = buf;
113 status = smb_raw_write(tree, &parms);
115 if (!NT_STATUS_IS_OK(status)) {
119 offset += parms.writex.out.nwritten;
120 total += parms.writex.out.nwritten;
121 buf += parms.writex.out.nwritten;
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)
133 union smb_write parms;
136 parms.write.level = RAW_WRITE_WRITE;
137 parms.write.in.remaining = 0;
140 size_t size = MIN(size1, tree->session->transport->negotiate.max_xmit - 48);
141 if (size > 0xFFFF) size = 0xFFFF;
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;
148 if (NT_STATUS_IS_ERR(smb_raw_write(tree, &parms)))
151 size = parms.write.out.nwritten;