#include "includes.h"
/****************************************************************************
-issue a single SMBread and don't wait for a reply
+Issue a single SMBread and don't wait for a reply.
****************************************************************************/
-static void cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
+
+static BOOL cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
size_t size, int i)
{
memset(cli->outbuf,'\0',smb_size);
SSVAL(cli->outbuf,smb_vwv6,size);
SSVAL(cli->outbuf,smb_mid,cli->mid + i);
- cli_send_smb(cli);
+ return cli_send_smb(cli);
}
/****************************************************************************
- read from a file
+Issue a single SMBreadraw and don't wait for a reply.
****************************************************************************/
-size_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
+
+static BOOL cli_issue_readraw(struct cli_state *cli, int fnum, off_t offset,
+ size_t size, int i)
+{
+ memset(cli->outbuf,'\0',smb_size);
+ memset(cli->inbuf,'\0',smb_size);
+
+ set_message(cli->outbuf,10,0,True);
+
+ CVAL(cli->outbuf,smb_com) = SMBreadbraw;
+ SSVAL(cli->outbuf,smb_tid,cli->cnum);
+ cli_setup_packet(cli);
+
+ SSVAL(cli->outbuf,smb_vwv0,fnum);
+ SIVAL(cli->outbuf,smb_vwv1,offset);
+ SSVAL(cli->outbuf,smb_vwv2,size);
+ SSVAL(cli->outbuf,smb_vwv3,size);
+ SSVAL(cli->outbuf,smb_mid,cli->mid + i);
+
+ return cli_send_smb(cli);
+}
+
+/****************************************************************************
+ Read size bytes at offset offset using SMBreadX.
+****************************************************************************/
+
+ssize_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
{
char *p;
- int total = -1;
-/*
- * There is a problem in this code when mpx is more than one.
- * for some reason files can get corrupted when being read.
- * Until we understand this fully I am serializing reads (one
- * read/one reply) for now. JRA.
- */
-#if 0
- int mpx = MAX(cli->max_mux-1, 1);
-#else
- int mpx = 1;
-#endif
- int block;
- int mid;
- int blocks;
- /* issued is the number of readX requests we have sent so far */
- int issued=0;
- /* received is the number of readX replies we have received */
- int received=0;
-
- /* maybe its a very silly request? */
- if (size == 0) return 0;
-
- /* set block to the maximum size we can handle in one readX,
- rounded down to a multiple of 1024 */
- block = (cli->max_xmit - (smb_size+32)) & ~1023;
-
- /* work out how many readX calls we will need in total */
- blocks = (size + (block-1)) / block;
+ int size2;
+ int readsize;
+ ssize_t total = 0;
- while (received < blocks) {
- int size2;
+ if (size == 0)
+ return 0;
- while (issued - received < mpx && issued < blocks) {
- int size1 = MIN(block, size-issued*block);
- cli_issue_read(cli, fnum, offset+issued*block, size1, issued);
- issued++;
- }
+ /*
+ * Set readsize to the maximum size we can handle in one readX,
+ * rounded down to a multiple of 1024.
+ */
- if (!cli_receive_smb(cli)) {
- return total;
- }
+ readsize = (cli->max_xmit - (smb_size+32)) & ~1023;
- received++;
- mid = SVAL(cli->inbuf, smb_mid) - cli->mid;
- size2 = SVAL(cli->inbuf, smb_vwv5);
+ while (total < size) {
+ readsize = MIN(readsize, size-total);
- if (CVAL(cli->inbuf,smb_rcls) != 0) {
- blocks = MIN(blocks, mid-1);
- continue;
- }
+ /* Issue a read and receive a reply */
- if (size2 <= 0) {
- blocks = MIN(blocks, mid-1);
- /* this distinguishes EOF from an error */
- total = MAX(total, 0);
- continue;
- }
+ if (!cli_issue_read(cli, fnum, offset, readsize, 0))
+ return -1;
- if (size2 > block) {
- DEBUG(0,("server returned more than we wanted!\n"));
+ if (!cli_receive_smb(cli))
return -1;
+
+ /* Check for error. Make sure to check for DOS and NT
+ errors. */
+
+ if (cli_is_error(cli)) {
+ NTSTATUS status = NT_STATUS_OK;
+ uint8 eclass = 0;
+ uint32 ecode = 0;
+
+ if (cli_is_nt_error(cli))
+ status = cli_nt_error(cli);
+ else
+ cli_dos_error(cli, &eclass, &ecode);
+
+ if ((eclass == ERRDOS && ecode == ERRmoredata) ||
+ NT_STATUS_V(status) == NT_STATUS_V(STATUS_MORE_ENTRIES))
+ return -1;
}
- if (mid >= issued) {
- DEBUG(0,("invalid mid from server!\n"));
+
+ size2 = SVAL(cli->inbuf, smb_vwv5);
+
+ if (size2 > readsize) {
+ DEBUG(5,("server returned more than we wanted!\n"));
+ return -1;
+ } else if (size2 < 0) {
+ DEBUG(5,("read return < 0!\n"));
return -1;
}
+
+ /* Copy data into buffer */
+
p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
+ memcpy(buf + total, p, size2);
- memcpy(buf+mid*block, p, size2);
+ total += size2;
+ offset += size2;
- total = MAX(total, mid*block + size2);
- }
+ /*
+ * If the server returned less than we asked for we're at EOF.
+ */
- while (received < issued) {
- cli_receive_smb(cli);
- received++;
+ if (size2 < readsize)
+ break;
}
-
+
return total;
}
+/****************************************************************************
+ Tester for the readraw call.
+****************************************************************************/
+
+ssize_t cli_readraw(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
+{
+ char *p;
+ int size2;
+ size_t readsize;
+ ssize_t total = 0;
+
+ if (size == 0)
+ return 0;
+
+ /*
+ * Set readsize to the maximum size we can handle in one readraw.
+ */
+
+ readsize = 0xFFFF;
+
+ while (total < size) {
+ readsize = MIN(readsize, size-total);
+
+ /* Issue a read and receive a reply */
+
+ if (!cli_issue_readraw(cli, fnum, offset, readsize, 0))
+ return -1;
+
+ if (!client_receive_smb(cli->fd, cli->inbuf, cli->timeout))
+ return -1;
+
+ size2 = smb_len(cli->inbuf);
+
+ if (size2 > readsize) {
+ DEBUG(5,("server returned more than we wanted!\n"));
+ return -1;
+ } else if (size2 < 0) {
+ DEBUG(5,("read return < 0!\n"));
+ return -1;
+ }
+
+ /* Copy data into buffer */
+
+ if (size2) {
+ p = cli->inbuf + 4;
+ memcpy(buf + total, p, size2);
+ }
+
+ total += size2;
+ offset += size2;
+
+ /*
+ * If the server returned less than we asked for we're at EOF.
+ */
+
+ if (size2 < readsize)
+ break;
+ }
+
+ return total;
+}
/****************************************************************************
issue a single SMBwrite and don't wait for a reply
****************************************************************************/
-static void cli_issue_write(struct cli_state *cli, int fnum, off_t offset, uint16 mode, char *buf,
+
+static BOOL cli_issue_write(struct cli_state *cli, int fnum, off_t offset, uint16 mode, char *buf,
size_t size, int i)
{
char *p;
SSVAL(cli->outbuf,smb_mid,cli->mid + i);
show_msg(cli->outbuf);
- cli_send_smb(cli);
+ return cli_send_smb(cli);
}
/****************************************************************************
0x0004 use raw named pipe protocol
0x0008 start of message mode named pipe protocol
****************************************************************************/
+
ssize_t cli_write(struct cli_state *cli,
int fnum, uint16 write_mode,
char *buf, off_t offset, size_t size)
while (received < blocks) {
- while ((issued - received < mpx) && (issued < blocks))
- {
+ while ((issued - received < mpx) && (issued < blocks)) {
int bsent = issued * block;
int size1 = MIN(block, size - bsent);
- cli_issue_write(cli, fnum, offset + bsent,
+ if (!cli_issue_write(cli, fnum, offset + bsent,
write_mode,
buf + bsent,
- size1, issued);
+ size1, issued))
+ return -1;
issued++;
}
if (!cli_receive_smb(cli))
- {
return bwritten;
- }
received++;
- if (CVAL(cli->inbuf,smb_rcls) != 0)
- {
+ if (cli_is_error(cli))
break;
- }
bwritten += SVAL(cli->inbuf, smb_vwv2);
}
while (received < issued && cli_receive_smb(cli))
- {
received++;
- }
return bwritten;
}
-
/****************************************************************************
write to a file using a SMBwrite and not bypassing 0 byte writes
****************************************************************************/
+
ssize_t cli_smbwrite(struct cli_state *cli,
int fnum, char *buf, off_t offset, size_t size1)
{
cli_setup_bcc(cli, p);
- cli_send_smb(cli);
- if (!cli_receive_smb(cli)) {
+ if (!cli_send_smb(cli))
+ return -1;
+
+ if (!cli_receive_smb(cli))
return -1;
- }
- if (CVAL(cli->inbuf,smb_rcls) != 0) {
+ if (cli_is_error(cli))
return -1;
- }
size = SVAL(cli->inbuf,smb_vwv0);
- if (size == 0) break;
+ if (size == 0)
+ break;
size1 -= size;
total += size;
return total;
}
-