2000-04-25 18:04:06 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2000-04-25 18:04:06 +04:00
client file read / write routines
Copyright ( C ) Andrew Tridgell 1994 - 1998
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 2 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "includes.h"
/****************************************************************************
2001-08-25 00:20:08 +04:00
Issue a single SMBread and don ' t wait for a reply .
2000-04-25 18:04:06 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-06-29 04:22:22 +04:00
static BOOL cli_issue_read ( struct cli_state * cli , int fnum , off_t offset ,
2000-04-25 18:04:06 +04:00
size_t size , int i )
{
2002-12-19 23:26:44 +03:00
BOOL bigoffset = False ;
2000-04-25 18:04:06 +04:00
memset ( cli - > outbuf , ' \0 ' , smb_size ) ;
memset ( cli - > inbuf , ' \0 ' , smb_size ) ;
2002-12-19 23:26:44 +03:00
if ( ( SMB_BIG_UINT ) offset > > 32 )
bigoffset = True ;
2007-04-20 02:40:32 +04:00
set_message ( NULL , cli - > outbuf , bigoffset ? 12 : 10 , 0 , True ) ;
2000-04-25 18:04:06 +04:00
2002-01-11 22:10:25 +03:00
SCVAL ( cli - > outbuf , smb_com , SMBreadX ) ;
2000-04-25 18:04:06 +04:00
SSVAL ( cli - > outbuf , smb_tid , cli - > cnum ) ;
cli_setup_packet ( cli ) ;
2002-01-11 22:10:25 +03:00
SCVAL ( cli - > outbuf , smb_vwv0 , 0xFF ) ;
2000-04-25 18:04:06 +04:00
SSVAL ( cli - > outbuf , smb_vwv2 , fnum ) ;
SIVAL ( cli - > outbuf , smb_vwv3 , offset ) ;
SSVAL ( cli - > outbuf , smb_vwv5 , size ) ;
SSVAL ( cli - > outbuf , smb_vwv6 , size ) ;
2007-05-16 04:07:38 +04:00
SSVAL ( cli - > outbuf , smb_vwv7 , ( size > > 16 ) ) ;
2000-04-25 18:04:06 +04:00
SSVAL ( cli - > outbuf , smb_mid , cli - > mid + i ) ;
2006-01-24 23:15:08 +03:00
if ( bigoffset ) {
SIVAL ( cli - > outbuf , smb_vwv10 , ( ( ( SMB_BIG_UINT ) offset ) > > 32 ) & 0xffffffff ) ;
}
2002-12-19 23:26:44 +03:00
2001-06-29 04:22:22 +04:00
return cli_send_smb ( cli ) ;
2000-04-25 18:04:06 +04:00
}
2001-06-22 05:09:40 +04:00
/****************************************************************************
2001-06-29 04:22:22 +04:00
Read size bytes at offset offset using SMBreadX .
2001-06-22 05:09:40 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-06-29 04:22:22 +04:00
ssize_t cli_read ( struct cli_state * cli , int fnum , char * buf , off_t offset , size_t size )
2001-06-22 05:09:40 +04:00
{
char * p ;
2007-05-16 04:07:38 +04:00
size_t size2 ;
size_t readsize ;
2001-06-29 04:22:22 +04:00
ssize_t total = 0 ;
2007-05-16 04:07:38 +04:00
/* We can only do direct reads if not signing. */
BOOL direct_reads = ! client_is_signing_on ( cli ) ;
2001-06-22 05:09:40 +04:00
if ( size = = 0 )
return 0 ;
2001-06-29 04:22:22 +04:00
/*
* Set readsize to the maximum size we can handle in one readX ,
* rounded down to a multiple of 1024.
*/
2001-06-22 05:09:40 +04:00
2007-05-16 04:07:38 +04:00
if ( client_is_signing_on ( cli ) = = False & &
cli_encryption_on ( cli ) = = False & &
( cli - > posix_capabilities & CIFS_UNIX_LARGE_READ_CAP ) ) {
readsize = CLI_SAMBA_MAX_POSIX_LARGE_READX_SIZE ;
} else if ( cli - > capabilities & CAP_LARGE_READX ) {
2006-04-22 06:33:11 +04:00
if ( cli - > is_samba ) {
readsize = CLI_SAMBA_MAX_LARGE_READX_SIZE ;
} else {
readsize = CLI_WINDOWS_MAX_LARGE_READX_SIZE ;
}
2004-12-14 03:25:11 +03:00
} else {
readsize = ( cli - > max_xmit - ( smb_size + 32 ) ) & ~ 1023 ;
}
2001-06-22 05:09:40 +04:00
2001-06-29 04:22:22 +04:00
while ( total < size ) {
2001-07-01 17:18:35 +04:00
readsize = MIN ( readsize , size - total ) ;
2001-06-22 05:09:40 +04:00
2001-06-29 04:22:22 +04:00
/* Issue a read and receive a reply */
2001-06-22 05:09:40 +04:00
2001-06-29 04:22:22 +04:00
if ( ! cli_issue_read ( cli , fnum , offset , readsize , 0 ) )
return - 1 ;
2001-06-22 04:42:53 +04:00
2007-05-16 04:07:38 +04:00
if ( direct_reads ) {
if ( ! cli_receive_smb_readX_header ( cli ) )
return - 1 ;
} else {
if ( ! cli_receive_smb ( cli ) )
return - 1 ;
}
2000-04-25 18:04:06 +04:00
2001-08-10 10:00:33 +04:00
/* Check for error. Make sure to check for DOS and NT
errors . */
2000-04-25 18:04:06 +04:00
2001-08-10 10:00:33 +04:00
if ( cli_is_error ( cli ) ) {
2002-11-27 22:11:46 +03:00
BOOL recoverable_error = False ;
2001-08-27 21:52:23 +04:00
NTSTATUS status = NT_STATUS_OK ;
2001-08-10 10:00:33 +04:00
uint8 eclass = 0 ;
2001-08-27 21:52:23 +04:00
uint32 ecode = 0 ;
2001-08-10 10:00:33 +04:00
if ( cli_is_nt_error ( cli ) )
status = cli_nt_error ( cli ) ;
else
2001-08-27 21:52:23 +04:00
cli_dos_error ( cli , & eclass , & ecode ) ;
2001-08-10 10:00:33 +04:00
2002-11-27 22:11:46 +03:00
/*
* ERRDOS ERRmoredata or STATUS_MORE_ENRTIES is a
* recoverable error , plus we have valid data in the
* packet so don ' t error out here .
*/
2001-08-27 21:52:23 +04:00
if ( ( eclass = = ERRDOS & & ecode = = ERRmoredata ) | |
NT_STATUS_V ( status ) = = NT_STATUS_V ( STATUS_MORE_ENTRIES ) )
2002-11-27 22:11:46 +03:00
recoverable_error = True ;
if ( ! recoverable_error )
2001-08-10 10:00:33 +04:00
return - 1 ;
2000-04-25 18:04:06 +04:00
}
size2 = SVAL ( cli - > inbuf , smb_vwv5 ) ;
2007-05-16 04:07:38 +04:00
size2 | = ( ( ( unsigned int ) ( SVAL ( cli - > inbuf , smb_vwv7 ) ) ) < < 16 ) ;
2000-04-25 18:04:06 +04:00
2001-06-29 04:22:22 +04:00
if ( size2 > readsize ) {
DEBUG ( 5 , ( " server returned more than we wanted! \n " ) ) ;
2000-04-25 18:04:06 +04:00
return - 1 ;
2001-06-29 04:22:22 +04:00
} else if ( size2 < 0 ) {
DEBUG ( 5 , ( " read return < 0! \n " ) ) ;
2000-04-25 18:04:06 +04:00
return - 1 ;
}
2001-06-29 04:22:22 +04:00
2007-05-16 04:07:38 +04:00
if ( ! direct_reads ) {
/* Copy data into buffer */
p = smb_base ( cli - > inbuf ) + SVAL ( cli - > inbuf , smb_vwv6 ) ;
memcpy ( buf + total , p , size2 ) ;
} else {
/* Ensure the remaining data matches the return size. */
ssize_t toread = smb_len_large ( cli - > inbuf ) - SVAL ( cli - > inbuf , smb_vwv6 ) ;
/* Ensure the size is correct. */
if ( toread ! = size2 ) {
DEBUG ( 5 , ( " direct read logic fail toread (%d) != size2 (%u) \n " ,
( int ) toread , ( unsigned int ) size2 ) ) ;
return - 1 ;
}
/* Read data directly into buffer */
toread = cli_receive_smb_data ( cli , buf + total , size2 ) ;
if ( toread ! = size2 ) {
DEBUG ( 5 , ( " direct read read failure toread (%d) != size2 (%u) \n " ,
( int ) toread , ( unsigned int ) size2 ) ) ;
return - 1 ;
}
}
2000-04-25 18:04:06 +04:00
2001-06-29 04:22:22 +04:00
total + = size2 ;
offset + = size2 ;
2000-04-25 18:04:06 +04:00
2001-06-29 04:22:22 +04:00
/*
* If the server returned less than we asked for we ' re at EOF .
*/
2000-04-25 18:04:06 +04:00
2001-06-29 04:22:22 +04:00
if ( size2 < readsize )
break ;
2000-04-25 18:04:06 +04:00
}
2001-06-29 04:22:22 +04:00
2000-04-25 18:04:06 +04:00
return total ;
}
2002-09-25 19:19:00 +04:00
#if 0 /* relies on client_receive_smb(), now a static in libsmb/clientgen.c */
2002-07-15 14:35:28 +04:00
/* This call is INCOMPATIBLE with SMB signing. If you remove the #if 0
you must fix ensure you don ' t attempt to sign the packets - data
* will * be currupted */
/****************************************************************************
Issue a single SMBreadraw and don ' t wait for a reply .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL cli_issue_readraw ( struct cli_state * cli , int fnum , off_t offset ,
size_t size , int i )
{
if ( ! cli - > sign_info . use_smb_signing ) {
DEBUG ( 0 , ( " Cannot use readraw and SMB Signing \n " ) ) ;
return False ;
}
memset ( cli - > outbuf , ' \0 ' , smb_size ) ;
memset ( cli - > inbuf , ' \0 ' , smb_size ) ;
2007-04-20 02:40:32 +04:00
set_message ( NULL , cli - > outbuf , 10 , 0 , True ) ;
2002-07-15 14:35:28 +04:00
SCVAL ( 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 ) ;
}
2001-08-25 00:20:08 +04:00
/****************************************************************************
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 ;
}
2002-07-15 14:35:28 +04:00
# endif
2000-04-25 18:04:06 +04:00
/****************************************************************************
issue a single SMBwrite and don ' t wait for a reply
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-06-29 04:22:22 +04:00
2003-01-03 11:28:12 +03:00
static BOOL cli_issue_write ( struct cli_state * cli , int fnum , off_t offset ,
uint16 mode , const char * buf ,
2000-04-25 18:04:06 +04:00
size_t size , int i )
{
char * p ;
2004-12-15 04:25:24 +03:00
BOOL large_writex = False ;
2000-04-25 18:04:06 +04:00
2002-03-20 04:43:06 +03:00
if ( size > cli - > bufsize ) {
2006-07-31 07:53:39 +04:00
cli - > outbuf = ( char * ) SMB_REALLOC ( cli - > outbuf , size + 1024 ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( ! cli - > outbuf ) {
return False ;
}
2006-07-31 07:53:39 +04:00
cli - > inbuf = ( char * ) SMB_REALLOC ( cli - > inbuf , size + 1024 ) ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( cli - > inbuf = = NULL ) {
SAFE_FREE ( cli - > outbuf ) ;
2002-03-20 04:43:06 +03:00
return False ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
}
2002-03-20 04:43:06 +03:00
cli - > bufsize = size + 1024 ;
}
2000-04-25 18:04:06 +04:00
memset ( cli - > outbuf , ' \0 ' , smb_size ) ;
memset ( cli - > inbuf , ' \0 ' , smb_size ) ;
2004-12-15 04:25:24 +03:00
if ( ( ( SMB_BIG_UINT ) offset > > 32 ) | | ( size > 0xFFFF ) ) {
large_writex = True ;
}
2002-12-30 09:12:13 +03:00
2004-12-15 04:25:24 +03:00
if ( large_writex )
2007-04-20 02:40:32 +04:00
set_message ( NULL , cli - > outbuf , 14 , 0 , True ) ;
2001-06-05 12:17:16 +04:00
else
2007-04-20 02:40:32 +04:00
set_message ( NULL , cli - > outbuf , 12 , 0 , True ) ;
2000-04-25 18:04:06 +04:00
2002-01-11 22:10:25 +03:00
SCVAL ( cli - > outbuf , smb_com , SMBwriteX ) ;
2000-04-25 18:04:06 +04:00
SSVAL ( cli - > outbuf , smb_tid , cli - > cnum ) ;
cli_setup_packet ( cli ) ;
2002-01-11 22:10:25 +03:00
SCVAL ( cli - > outbuf , smb_vwv0 , 0xFF ) ;
2000-04-25 18:04:06 +04:00
SSVAL ( cli - > outbuf , smb_vwv2 , fnum ) ;
SIVAL ( cli - > outbuf , smb_vwv3 , offset ) ;
2002-12-30 09:12:13 +03:00
SIVAL ( cli - > outbuf , smb_vwv5 , 0 ) ;
2000-04-25 18:04:06 +04:00
SSVAL ( cli - > outbuf , smb_vwv7 , mode ) ;
2003-01-15 21:57:41 +03:00
SSVAL ( cli - > outbuf , smb_vwv8 , ( mode & 0x0008 ) ? size : 0 ) ;
2002-12-30 09:12:13 +03:00
/*
2003-01-15 21:57:41 +03:00
* According to CIFS - TR - 1 p00 , this following field should only
* be set if CAP_LARGE_WRITEX is set . We should check this
* locally . However , this check might already have been
* done by our callers .
2002-12-30 09:12:13 +03:00
*/
2001-06-05 12:17:16 +04:00
SSVAL ( cli - > outbuf , smb_vwv9 , ( ( size > > 16 ) & 1 ) ) ;
2000-04-25 18:04:06 +04:00
SSVAL ( cli - > outbuf , smb_vwv10 , size ) ;
SSVAL ( cli - > outbuf , smb_vwv11 ,
smb_buf ( cli - > outbuf ) - smb_base ( cli - > outbuf ) ) ;
2002-12-30 09:12:13 +03:00
2006-01-24 23:15:08 +03:00
if ( large_writex ) {
SIVAL ( cli - > outbuf , smb_vwv12 , ( ( ( SMB_BIG_UINT ) offset ) > > 32 ) & 0xffffffff ) ;
}
2000-04-25 18:04:06 +04:00
p = smb_base ( cli - > outbuf ) + SVAL ( cli - > outbuf , smb_vwv11 ) ;
memcpy ( p , buf , size ) ;
2001-06-21 13:10:42 +04:00
cli_setup_bcc ( cli , p + size ) ;
2000-04-25 18:04:06 +04:00
SSVAL ( cli - > outbuf , smb_mid , cli - > mid + i ) ;
show_msg ( cli - > outbuf ) ;
2001-06-29 04:22:22 +04:00
return cli_send_smb ( cli ) ;
2000-04-25 18:04:06 +04:00
}
/****************************************************************************
write to a file
write_mode : 0x0001 disallow write cacheing
0x0002 return bytes remaining
0x0004 use raw named pipe protocol
0x0008 start of message mode named pipe protocol
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-06-29 04:22:22 +04:00
2005-09-30 21:13:37 +04:00
ssize_t cli_write ( struct cli_state * cli ,
2004-10-14 07:19:57 +04:00
int fnum , uint16 write_mode ,
const char * buf , off_t offset , size_t size )
2000-04-25 18:04:06 +04:00
{
2005-09-30 21:13:37 +04:00
ssize_t bwritten = 0 ;
unsigned int issued = 0 ;
unsigned int received = 0 ;
2004-09-17 04:49:41 +04:00
int mpx = 1 ;
2003-08-08 21:08:35 +04:00
int block = cli - > max_xmit - ( smb_size + 32 ) ;
2000-04-25 18:04:06 +04:00
int blocks = ( size + ( block - 1 ) ) / block ;
2004-09-17 04:53:17 +04:00
if ( cli - > max_mux > 1 ) {
2004-09-17 04:49:41 +04:00
mpx = cli - > max_mux - 1 ;
2004-09-17 04:53:17 +04:00
} else {
mpx = 1 ;
2004-09-17 04:49:41 +04:00
}
2000-04-25 18:04:06 +04:00
while ( received < blocks ) {
2001-06-29 04:22:22 +04:00
while ( ( issued - received < mpx ) & & ( issued < blocks ) ) {
2005-09-30 21:13:37 +04:00
ssize_t bsent = issued * block ;
ssize_t size1 = MIN ( block , size - bsent ) ;
2000-04-25 18:04:06 +04:00
2001-06-29 04:22:22 +04:00
if ( ! cli_issue_write ( cli , fnum , offset + bsent ,
2000-04-25 18:04:06 +04:00
write_mode ,
buf + bsent ,
2001-06-29 04:22:22 +04:00
size1 , issued ) )
return - 1 ;
2000-04-25 18:04:06 +04:00
issued + + ;
}
if ( ! cli_receive_smb ( cli ) )
return bwritten ;
received + + ;
2001-09-05 15:32:59 +04:00
if ( cli_is_error ( cli ) )
2000-04-25 18:04:06 +04:00
break ;
bwritten + = SVAL ( cli - > inbuf , smb_vwv2 ) ;
2004-10-14 07:19:57 +04:00
bwritten + = ( ( ( int ) ( SVAL ( cli - > inbuf , smb_vwv4 ) ) ) < < 16 ) ;
2000-04-25 18:04:06 +04:00
}
while ( received < issued & & cli_receive_smb ( cli ) )
received + + ;
return bwritten ;
}
/****************************************************************************
write to a file using a SMBwrite and not bypassing 0 byte writes
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-06-29 04:22:22 +04:00
2000-04-25 18:04:06 +04:00
ssize_t cli_smbwrite ( struct cli_state * cli ,
int fnum , char * buf , off_t offset , size_t size1 )
{
char * p ;
ssize_t total = 0 ;
do {
size_t size = MIN ( size1 , cli - > max_xmit - 48 ) ;
memset ( cli - > outbuf , ' \0 ' , smb_size ) ;
memset ( cli - > inbuf , ' \0 ' , smb_size ) ;
2007-04-20 02:40:32 +04:00
set_message ( NULL , cli - > outbuf , 5 , 0 , True ) ;
2000-04-25 18:04:06 +04:00
2002-01-11 22:10:25 +03:00
SCVAL ( cli - > outbuf , smb_com , SMBwrite ) ;
2000-04-25 18:04:06 +04:00
SSVAL ( cli - > outbuf , smb_tid , cli - > cnum ) ;
cli_setup_packet ( cli ) ;
SSVAL ( cli - > outbuf , smb_vwv0 , fnum ) ;
SSVAL ( cli - > outbuf , smb_vwv1 , size ) ;
SIVAL ( cli - > outbuf , smb_vwv2 , offset ) ;
SSVAL ( cli - > outbuf , smb_vwv4 , 0 ) ;
p = smb_buf ( cli - > outbuf ) ;
* p + + = 1 ;
2001-06-21 13:10:42 +04:00
SSVAL ( p , 0 , size ) ; p + = 2 ;
memcpy ( p , buf , size ) ; p + = size ;
cli_setup_bcc ( cli , p ) ;
2000-04-25 18:04:06 +04:00
2001-06-29 04:22:22 +04:00
if ( ! cli_send_smb ( cli ) )
return - 1 ;
if ( ! cli_receive_smb ( cli ) )
2000-04-25 18:04:06 +04:00
return - 1 ;
2001-09-05 15:32:59 +04:00
if ( cli_is_error ( cli ) )
2000-04-25 18:04:06 +04:00
return - 1 ;
size = SVAL ( cli - > inbuf , smb_vwv0 ) ;
2001-06-29 04:22:22 +04:00
if ( size = = 0 )
break ;
2000-04-25 18:04:06 +04:00
size1 - = size ;
total + = size ;
2002-03-20 04:47:31 +03:00
offset + = size ;
2000-04-25 18:04:06 +04:00
} while ( size1 ) ;
return total ;
}