2003-08-13 05:53:07 +04:00
/*
Unix SMB / CIFS implementation .
client file read / write routines
Copyright ( C ) Andrew Tridgell 1994 - 1998
Copyright ( C ) James Myers 2003
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
2007-07-10 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2003-08-13 05:53:07 +04:00
( 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
2007-07-10 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2003-08-13 05:53:07 +04:00
*/
# include "includes.h"
2004-11-01 04:03:22 +03:00
# include "libcli/raw/libcliraw.h"
2008-04-02 06:53:27 +04:00
# include "libcli/raw/raw_proto.h"
2007-09-08 17:27:14 +04:00
# include "libcli/libcli.h"
2003-08-13 05:53:07 +04:00
/****************************************************************************
Read size bytes at offset offset using SMBreadX .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-12-04 16:56:25 +03:00
ssize_t smbcli_read ( struct smbcli_tree * tree , int fnum , void * _buf , off_t offset ,
2004-02-08 03:51:07 +03:00
size_t size )
2003-08-13 05:53:07 +04:00
{
2007-09-07 17:31:15 +04:00
uint8_t * buf = ( uint8_t * ) _buf ;
2003-08-13 05:53:07 +04:00
union smb_read parms ;
int readsize ;
ssize_t total = 0 ;
if ( size = = 0 ) {
return 0 ;
}
parms . readx . level = RAW_READ_READX ;
2006-03-13 01:48:25 +03:00
parms . readx . in . file . fnum = fnum ;
2003-08-13 05:53:07 +04:00
/*
* Set readsize to the maximum size we can handle in one readX ,
* rounded down to a multiple of 1024.
*/
2004-10-29 09:31:35 +04:00
readsize = ( tree - > session - > transport - > negotiate . max_xmit - ( MIN_SMB_SIZE + 32 ) ) ;
2003-10-10 09:40:32 +04:00
if ( readsize > 0xFFFF ) readsize = 0xFFFF ;
2003-08-13 05:53:07 +04:00
while ( total < size ) {
NTSTATUS status ;
readsize = MIN ( readsize , size - total ) ;
parms . readx . in . offset = offset ;
parms . readx . in . mincnt = readsize ;
parms . readx . in . maxcnt = readsize ;
parms . readx . in . remaining = size - total ;
2007-10-07 02:28:14 +04:00
parms . readx . in . read_for_execute = false ;
2003-08-13 05:53:07 +04:00
parms . readx . out . data = buf + total ;
2004-02-08 03:51:07 +03:00
status = smb_raw_read ( tree , & parms ) ;
2003-08-13 05:53:07 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
return - 1 ;
}
total + = parms . readx . out . nread ;
offset + = parms . readx . out . nread ;
/* If the server returned less than we asked for we're at EOF */
if ( parms . readx . out . nread < readsize )
break ;
}
return total ;
}
/****************************************************************************
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
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-08-04 17:23:35 +04:00
ssize_t smbcli_write ( struct smbcli_tree * tree ,
2004-08-30 09:32:00 +04:00
int fnum , uint16_t write_mode ,
2004-12-04 16:56:25 +03:00
const void * _buf , off_t offset , size_t size )
2003-08-13 05:53:07 +04:00
{
2007-09-07 17:31:15 +04:00
const uint8_t * buf = ( const uint8_t * ) _buf ;
2003-08-13 05:53:07 +04:00
union smb_write parms ;
2004-10-29 09:31:35 +04:00
int block = ( tree - > session - > transport - > negotiate . max_xmit - ( MIN_SMB_SIZE + 32 ) ) ;
2003-08-13 05:53:07 +04:00
ssize_t total = 0 ;
if ( size = = 0 ) {
return 0 ;
}
2003-10-10 09:40:32 +04:00
if ( block > 0xFFFF ) block = 0xFFFF ;
2003-08-13 05:53:07 +04:00
parms . writex . level = RAW_WRITE_WRITEX ;
2006-03-13 01:48:25 +03:00
parms . writex . in . file . fnum = fnum ;
2003-08-13 05:53:07 +04:00
parms . writex . in . wmode = write_mode ;
parms . writex . in . remaining = 0 ;
while ( total < size ) {
NTSTATUS status ;
block = MIN ( block , size - total ) ;
parms . writex . in . offset = offset ;
parms . writex . in . count = block ;
parms . writex . in . data = buf ;
2004-02-08 03:51:07 +03:00
status = smb_raw_write ( tree , & parms ) ;
2003-08-13 05:53:07 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
return - 1 ;
}
offset + = parms . writex . out . nwritten ;
total + = parms . writex . out . nwritten ;
buf + = parms . writex . out . nwritten ;
}
return total ;
}
/****************************************************************************
write to a file using a SMBwrite and not bypassing 0 byte writes
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-08-04 17:23:35 +04:00
ssize_t smbcli_smbwrite ( struct smbcli_tree * tree ,
2004-12-04 16:56:25 +03:00
int fnum , const void * _buf , off_t offset , size_t size1 )
2003-08-13 05:53:07 +04:00
{
2007-09-07 17:31:15 +04:00
const uint8_t * buf = ( const uint8_t * ) _buf ;
2003-08-13 05:53:07 +04:00
union smb_write parms ;
ssize_t total = 0 ;
parms . write . level = RAW_WRITE_WRITE ;
parms . write . in . remaining = 0 ;
do {
2004-02-08 03:51:07 +03:00
size_t size = MIN ( size1 , tree - > session - > transport - > negotiate . max_xmit - 48 ) ;
2003-10-10 09:40:32 +04:00
if ( size > 0xFFFF ) size = 0xFFFF ;
2003-08-13 05:53:07 +04:00
2006-03-13 01:48:25 +03:00
parms . write . in . file . fnum = fnum ;
2003-08-13 05:53:07 +04:00
parms . write . in . offset = offset ;
parms . write . in . count = size ;
parms . write . in . data = buf + total ;
2004-02-08 03:51:07 +03:00
if ( NT_STATUS_IS_ERR ( smb_raw_write ( tree , & parms ) ) )
2003-08-13 05:53:07 +04:00
return - 1 ;
size = parms . write . out . nwritten ;
if ( size = = 0 )
break ;
size1 - = size ;
total + = size ;
offset + = size ;
} while ( size1 ) ;
return total ;
}