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.
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, char *buf, off_t offset,
39 parms.readx.level = RAW_READ_READX;
40 parms.readx.in.fnum = fnum;
43 * Set readsize to the maximum size we can handle in one readX,
44 * rounded down to a multiple of 1024.
46 readsize = (tree->session->transport->negotiate.max_xmit - (MIN_SMB_SIZE+32));
47 if (readsize > 0xFFFF) readsize = 0xFFFF;
49 while (total < size) {
52 readsize = MIN(readsize, size-total);
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;
60 status = smb_raw_read(tree, &parms);
62 if (!NT_STATUS_IS_OK(status)) {
66 total += parms.readx.out.nread;
67 offset += parms.readx.out.nread;
69 /* If the server returned less than we asked for we're at EOF */
70 if (parms.readx.out.nread < readsize)
78 /****************************************************************************
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)
89 union smb_write parms;
90 int block = (tree->session->transport->negotiate.max_xmit - (MIN_SMB_SIZE+32));
97 if (block > 0xFFFF) block = 0xFFFF;
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;
105 while (total < size) {
108 block = MIN(block, size - total);
110 parms.writex.in.offset = offset;
111 parms.writex.in.count = block;
112 parms.writex.in.data = buf;
114 status = smb_raw_write(tree, &parms);
116 if (!NT_STATUS_IS_OK(status)) {
120 offset += parms.writex.out.nwritten;
121 total += parms.writex.out.nwritten;
122 buf += parms.writex.out.nwritten;
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)
134 union smb_write parms;
137 parms.write.level = RAW_WRITE_WRITE;
138 parms.write.in.remaining = 0;
141 size_t size = MIN(size1, tree->session->transport->negotiate.max_xmit - 48);
142 if (size > 0xFFFF) size = 0xFFFF;
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;
149 if (NT_STATUS_IS_ERR(smb_raw_write(tree, &parms)))
152 size = parms.write.out.nwritten;