1996-05-04 11:50:46 +04:00
/*
Unix SMB / Netbios implementation .
Version 1.9 .
Tar Extensions
1998-01-22 16:27:43 +03:00
Copyright ( C ) Ricky Poulten 1995 - 1998
1998-06-17 05:52:57 +04:00
Copyright ( C ) Richard Sharpe 1998
1996-05-04 11:50:46 +04:00
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 .
*/
1998-04-11 11:52:13 +04:00
/* The following changes developed by Richard Sharpe for Canon Information
1998-06-17 05:52:57 +04:00
Systems Research Australia ( CISRA )
1998-04-11 11:52:13 +04:00
1. Restore can now restore files with long file names
2. Save now saves directory information so that we can restore
directory creation times
3. tar now accepts both UNIX path names and DOS path names . I prefer
those lovely / ' s to those UGLY \ ' s : - )
1998-05-08 17:51:17 +04:00
4. the files to exclude can be specified as a regular expression by adding
an r flag to the other tar flags . Eg :
1998-04-11 11:52:13 +04:00
1998-05-08 17:51:17 +04:00
- TcrX file . tar " *.(obj|exe) "
will skip all . obj and . exe files
1998-04-11 11:52:13 +04:00
*/
1996-05-04 11:50:46 +04:00
# include "includes.h"
# include "clitar.h"
1998-05-08 17:51:17 +04:00
1998-09-05 09:07:05 +04:00
static int clipfind ( char * * aret , int ret , char * tok ) ;
1998-05-08 17:51:17 +04:00
typedef struct file_info_struct file_info2 ;
struct file_info_struct
{
1998-11-09 23:33:37 +03:00
size_t size ;
uint16 mode ;
1998-05-08 17:51:17 +04:00
int uid ;
int gid ;
/* These times are normally kept in GMT */
time_t mtime ;
time_t atime ;
time_t ctime ;
char * name ; /* This is dynamically allocate */
file_info2 * next , * prev ; /* Used in the stack ... */
} ;
typedef struct
{
file_info2 * top ;
int items ;
} stack ;
stack dir_stack = { NULL , 0 } ; /* Want an empty stack */
1996-05-04 11:50:46 +04:00
# define SEPARATORS " \t\n\r"
extern int DEBUGLEVEL ;
1998-11-09 06:45:49 +03:00
extern struct cli_state * cli ;
1996-05-04 11:50:46 +04:00
/* These defines are for the do_setrattr routine, to indicate
* setting and reseting of file attributes in the function call */
# define ATTRSET 1
# define ATTRRESET 0
1998-11-09 23:33:37 +03:00
static uint16 attribute = aDIR | aSYSTEM | aHIDDEN ;
1996-05-04 11:50:46 +04:00
# ifndef CLIENT_TIMEOUT
# define CLIENT_TIMEOUT (30*1000)
# endif
1998-09-18 16:47:46 +04:00
static char * tarbuf , * buffer_p ;
1999-03-25 23:21:01 +03:00
static int tp , ntarf , tbufsiz ;
1999-03-30 14:41:07 +04:00
static double ttarf ;
1996-05-04 11:50:46 +04:00
/* Incremental mode */
BOOL tar_inc = False ;
/* Reset archive bit */
BOOL tar_reset = False ;
/* Include / exclude mode (true=include, false=exclude) */
BOOL tar_excl = True ;
1998-05-08 17:51:17 +04:00
/* use regular expressions for search on file names */
BOOL tar_re_search = False ;
# ifdef HAVE_REGEX_H
regex_t * preg ;
# endif
1998-09-21 12:45:11 +04:00
/* Do not dump anything, just calculate sizes */
BOOL dry_run = False ;
1998-04-02 05:01:24 +04:00
/* Dump files with System attribute */
1998-06-17 05:52:57 +04:00
BOOL tar_system = True ;
1998-04-02 05:01:24 +04:00
/* Dump files with Hidden attribute */
BOOL tar_hidden = True ;
/* Be noisy - make a catalogue */
BOOL tar_noisy = True ;
1998-09-26 04:41:20 +04:00
BOOL tar_real_noisy = False ; /* Don't want to be really noisy by default */
1998-04-02 05:01:24 +04:00
1996-05-04 11:50:46 +04:00
char tar_type = ' \0 ' ;
static char * * cliplist = NULL ;
static int clipn = 0 ;
1998-06-17 05:52:57 +04:00
static BOOL must_free_cliplist = False ;
1996-05-04 11:50:46 +04:00
extern file_info def_finfo ;
extern BOOL lowercase ;
1998-09-30 02:52:38 +04:00
extern uint16 cnum ;
1996-05-04 11:50:46 +04:00
extern BOOL readbraw_supported ;
extern int max_xmit ;
extern pstring cur_dir ;
extern int get_total_time_ms ;
extern int get_total_size ;
int blocksize = 20 ;
int tarhandle ;
1998-04-11 11:52:13 +04:00
static void writetarheader ( int f , char * aname , int size , time_t mtime ,
char * amode , unsigned char ftype ) ;
1998-05-08 17:51:17 +04:00
static void do_atar ( char * rname , char * lname , file_info * finfo1 ) ;
static void do_tar ( file_info * finfo ) ;
static void oct_it ( long value , int ndgs , char * p ) ;
1998-04-13 23:24:06 +04:00
static void fixtarname ( char * tptr , char * fp , int l ) ;
static int dotarbuf ( int f , char * b , int n ) ;
1998-05-08 17:51:17 +04:00
static void dozerobuf ( int f , int n ) ;
static void dotareof ( int f ) ;
static void initarbuf ( void ) ;
/* restore functions */
static long readtarheader ( union hblock * hb , file_info2 * finfo , char * prefix ) ;
1998-04-13 23:24:06 +04:00
static long unoct ( char * p , int ndgs ) ;
1998-05-08 17:51:17 +04:00
static void do_tarput ( void ) ;
1998-06-17 05:52:57 +04:00
static void unfixtarname ( char * tptr , char * fp , int l , BOOL first ) ;
1996-05-04 11:50:46 +04:00
/*
* tar specific utitlities
*/
1998-06-16 22:25:36 +04:00
/*******************************************************************
Create a string of size size + 1 ( for the null )
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-06-17 05:52:57 +04:00
static char * string_create_s ( int size )
1998-05-08 17:51:17 +04:00
{
char * tmp ;
tmp = ( char * ) malloc ( size + 1 ) ;
if ( tmp = = NULL ) {
DEBUG ( 0 , ( " Out of memory in string_create_s \n " ) ) ;
}
return ( tmp ) ;
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
Write a tar header to buffer
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void writetarheader ( int f , char * aname , int size , time_t mtime ,
1998-04-11 11:52:13 +04:00
char * amode , unsigned char ftype )
1996-05-04 11:50:46 +04:00
{
union hblock hb ;
int i , chk , l ;
char * jp ;
1998-05-08 17:51:17 +04:00
DEBUG ( 5 , ( " WriteTarHdr, Type = %c, Size= %i, Name = %s \n " , ftype , size , aname ) ) ;
1998-04-11 11:52:13 +04:00
1996-05-04 11:50:46 +04:00
memset ( hb . dummy , 0 , sizeof ( hb . dummy ) ) ;
l = strlen ( aname ) ;
2000-11-23 02:06:29 +03:00
if ( l > = NAMSIZ - 1 ) {
1997-09-18 10:52:49 +04:00
/* write a GNU tar style long header */
char * b ;
b = ( char * ) malloc ( l + TBLOCK + 100 ) ;
if ( ! b ) {
DEBUG ( 0 , ( " out of memory \n " ) ) ;
exit ( 1 ) ;
}
2000-11-23 02:06:29 +03:00
writetarheader ( f , " /./@LongLink " , l + 2 , 0 , " 0 \0 " , ' L ' ) ;
1997-09-18 10:52:49 +04:00
memset ( b , 0 , l + TBLOCK + 100 ) ;
1998-06-17 05:52:57 +04:00
fixtarname ( b , aname , l ) ;
1997-09-18 10:52:49 +04:00
i = strlen ( b ) + 1 ;
1999-12-13 16:27:58 +03:00
DEBUG ( 5 , ( " File name in tar file: %s, size=%d, \n " , b , ( int ) strlen ( b ) ) ) ;
1998-02-13 15:40:23 +03:00
dotarbuf ( f , b , TBLOCK * ( ( ( i - 1 ) / TBLOCK ) + 1 ) ) ;
1997-09-18 10:52:49 +04:00
free ( b ) ;
}
1996-05-04 11:50:46 +04:00
/* use l + 1 to do the null too */
fixtarname ( hb . dbuf . name , aname , ( l > = NAMSIZ ) ? NAMSIZ : l + 1 ) ;
if ( lowercase )
strlower ( hb . dbuf . name ) ;
/* write out a "standard" tar format header */
hb . dbuf . name [ NAMSIZ - 1 ] = ' \0 ' ;
1998-06-17 05:52:57 +04:00
safe_strcpy ( hb . dbuf . mode , amode , strlen ( amode ) ) ;
1996-05-04 11:50:46 +04:00
oct_it ( 0L , 8 , hb . dbuf . uid ) ;
oct_it ( 0L , 8 , hb . dbuf . gid ) ;
oct_it ( ( long ) size , 13 , hb . dbuf . size ) ;
oct_it ( ( long ) mtime , 13 , hb . dbuf . mtime ) ;
memcpy ( hb . dbuf . chksum , " " , sizeof ( hb . dbuf . chksum ) ) ;
memset ( hb . dbuf . linkname , 0 , NAMSIZ ) ;
1998-04-11 11:52:13 +04:00
hb . dbuf . linkflag = ftype ;
1996-05-04 11:50:46 +04:00
for ( chk = 0 , i = sizeof ( hb . dummy ) , jp = hb . dummy ; - - i > = 0 ; ) chk + = ( 0xFF & * jp + + ) ;
oct_it ( ( long ) chk , 8 , hb . dbuf . chksum ) ;
hb . dbuf . chksum [ 6 ] = ' \0 ' ;
( void ) dotarbuf ( f , hb . dummy , sizeof ( hb . dummy ) ) ;
}
/****************************************************************************
Read a tar header into a hblock structure , and validate
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-05-08 17:51:17 +04:00
static long readtarheader ( union hblock * hb , file_info2 * finfo , char * prefix )
1996-05-04 11:50:46 +04:00
{
long chk , fchk ;
int i ;
char * jp ;
/*
* read in a " standard " tar format header - we ' re not that interested
* in that many fields , though
*/
/* check the checksum */
for ( chk = 0 , i = sizeof ( hb - > dummy ) , jp = hb - > dummy ; - - i > = 0 ; ) chk + = ( 0xFF & * jp + + ) ;
if ( chk = = 0 )
return chk ;
/* compensate for blanks in chksum header */
for ( i = sizeof ( hb - > dbuf . chksum ) , jp = hb - > dbuf . chksum ; - - i > = 0 ; )
chk - = ( 0xFF & * jp + + ) ;
chk + = ' ' * sizeof ( hb - > dbuf . chksum ) ;
fchk = unoct ( hb - > dbuf . chksum , sizeof ( hb - > dbuf . chksum ) ) ;
1998-08-14 21:38:29 +04:00
DEBUG ( 5 , ( " checksum totals chk=%ld fchk=%ld chksum=%s \n " ,
1996-05-04 11:50:46 +04:00
chk , fchk , hb - > dbuf . chksum ) ) ;
if ( fchk ! = chk )
{
1998-08-14 21:38:29 +04:00
DEBUG ( 0 , ( " checksums don't match %ld %ld \n " , fchk , chk ) ) ;
1998-09-24 17:43:36 +04:00
dump_data ( 5 , ( char * ) hb - TBLOCK , TBLOCK * 3 ) ;
1996-05-04 11:50:46 +04:00
return - 1 ;
}
1998-05-08 17:51:17 +04:00
if ( ( finfo - > name = string_create_s ( strlen ( prefix ) + strlen ( hb - > dbuf . name ) + 3 ) ) = = NULL ) {
DEBUG ( 0 , ( " Out of space creating file_info2 for %s \n " , hb - > dbuf . name ) ) ;
return ( - 1 ) ;
}
1998-06-17 05:52:57 +04:00
safe_strcpy ( finfo - > name , prefix , strlen ( prefix ) + strlen ( hb - > dbuf . name ) + 3 ) ;
1996-05-04 11:50:46 +04:00
/* use l + 1 to do the null too; do prefix - prefcnt to zap leading slash */
unfixtarname ( finfo - > name + strlen ( prefix ) , hb - > dbuf . name ,
1998-06-17 05:52:57 +04:00
strlen ( hb - > dbuf . name ) + 1 , True ) ;
1996-05-04 11:50:46 +04:00
1998-09-23 18:37:01 +04:00
/* can't handle some links at present */
1998-04-11 11:52:13 +04:00
if ( ( hb - > dbuf . linkflag ! = ' 0 ' ) & & ( hb - > dbuf . linkflag ! = ' 5 ' ) ) {
1996-05-04 11:50:46 +04:00
if ( hb - > dbuf . linkflag = = 0 ) {
DEBUG ( 6 , ( " Warning: NULL link flag (gnu tar archive ?) %s \n " ,
finfo - > name ) ) ;
} else {
1998-04-11 11:52:13 +04:00
if ( hb - > dbuf . linkflag = = ' L ' ) { /* We have a longlink */
/* Do nothing here at the moment. do_tarput will handle this
as long as the longlink gets back to it , as it has to advance
the buffer pointer , etc */
} else {
DEBUG ( 0 , ( " this tar file appears to contain some kind of link other than a GNUtar Longlink - ignoring \n " ) ) ;
return - 2 ;
}
1996-05-04 11:50:46 +04:00
}
}
if ( ( unoct ( hb - > dbuf . mode , sizeof ( hb - > dbuf . mode ) ) & S_IFDIR )
| | ( * ( finfo - > name + strlen ( finfo - > name ) - 1 ) = = ' \\ ' ) )
{
finfo - > mode = aDIR ;
}
else
finfo - > mode = 0 ; /* we don't care about mode at the moment, we'll
* just make it a regular file */
/*
* Bug fix by richard @ sj . co . uk
*
* REC : restore times correctly ( as does tar )
* We only get the modification time of the file ; set the creation time
* from the mod . time , and the access time to current time
*/
finfo - > mtime = finfo - > ctime = strtol ( hb - > dbuf . mtime , NULL , 8 ) ;
finfo - > atime = time ( NULL ) ;
finfo - > size = unoct ( hb - > dbuf . size , sizeof ( hb - > dbuf . size ) ) ;
return True ;
}
/****************************************************************************
Write out the tar buffer to tape or wherever
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int dotarbuf ( int f , char * b , int n )
{
int fail = 1 , writ = n ;
1998-09-21 12:45:11 +04:00
if ( dry_run ) {
return writ ;
}
1996-05-04 11:50:46 +04:00
/* This routine and the next one should be the only ones that do write()s */
if ( tp + n > = tbufsiz )
{
int diff ;
diff = tbufsiz - tp ;
memcpy ( tarbuf + tp , b , diff ) ;
fail = fail & & ( 1 + write ( f , tarbuf , tbufsiz ) ) ;
n - = diff ;
b + = diff ;
tp = 0 ;
while ( n > = tbufsiz )
{
fail = fail & & ( 1 + write ( f , b , tbufsiz ) ) ;
n - = tbufsiz ;
b + = tbufsiz ;
}
}
if ( n > 0 ) {
memcpy ( tarbuf + tp , b , n ) ;
tp + = n ;
}
return ( fail ? writ : 0 ) ;
}
/****************************************************************************
1998-04-11 11:52:13 +04:00
Write zeros to buffer / tape
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void dozerobuf ( int f , int n )
{
/* short routine just to write out n zeros to buffer -
* used to round files to nearest block
* and to do tar EOFs */
1998-09-21 12:45:11 +04:00
if ( dry_run )
return ;
1996-05-04 11:50:46 +04:00
if ( n + tp > = tbufsiz )
{
memset ( tarbuf + tp , 0 , tbufsiz - tp ) ;
1998-04-11 11:52:13 +04:00
1996-05-04 11:50:46 +04:00
write ( f , tarbuf , tbufsiz ) ;
memset ( tarbuf , 0 , ( tp + = n - tbufsiz ) ) ;
}
else
{
memset ( tarbuf + tp , 0 , n ) ;
tp + = n ;
}
}
/****************************************************************************
Malloc tape buffer
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-09-29 01:43:48 +04:00
static void initarbuf ( void )
1996-05-04 11:50:46 +04:00
{
/* initialize tar buffer */
tbufsiz = blocksize * TBLOCK ;
1998-04-11 11:52:13 +04:00
tarbuf = malloc ( tbufsiz ) ; /* FIXME: We might not get the buffer */
1996-05-04 11:50:46 +04:00
1997-07-28 22:59:57 +04:00
/* reset tar buffer pointer and tar file counter and total dumped */
tp = 0 ; ntarf = 0 ; ttarf = 0 ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
Write two zero blocks at end of file
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void dotareof ( int f )
{
1998-09-02 00:11:54 +04:00
SMB_STRUCT_STAT stbuf ;
1996-05-04 11:50:46 +04:00
/* Two zero blocks at end of file, write out full buffer */
1998-09-21 12:45:11 +04:00
if ( dry_run )
return ;
1996-05-04 11:50:46 +04:00
( void ) dozerobuf ( f , TBLOCK ) ;
( void ) dozerobuf ( f , TBLOCK ) ;
1998-09-03 22:40:31 +04:00
if ( sys_fstat ( f , & stbuf ) = = - 1 )
1996-05-04 11:50:46 +04:00
{
DEBUG ( 0 , ( " Couldn't stat file handle \n " ) ) ;
return ;
}
/* Could be a pipe, in which case S_ISREG should fail,
* and we should write out at full size */
if ( tp > 0 ) write ( f , tarbuf , S_ISREG ( stbuf . st_mode ) ? tp : tbufsiz ) ;
}
/****************************************************************************
( Un ) mangle DOS pathname , make nonabsolute
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void fixtarname ( char * tptr , char * fp , int l )
{
2001-07-04 11:15:53 +04:00
/* add a '.' to start of file name, convert from ugly dos \'s in path
* to lovely unix / ' s : - } */
* tptr + + = ' . ' ;
safe_strcpy ( tptr , fp , l ) ;
string_replace ( tptr , ' \\ ' , ' / ' ) ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
Convert from decimal to octal string
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-04-13 23:24:06 +04:00
static void oct_it ( long value , int ndgs , char * p )
1996-05-04 11:50:46 +04:00
{
/* Converts long to octal string, pads with leading zeros */
/* skip final null, but do final space */
- - ndgs ;
p [ - - ndgs ] = ' ' ;
/* Loop does at least one digit */
do {
p [ - - ndgs ] = ' 0 ' + ( char ) ( value & 7 ) ;
value > > = 3 ;
}
while ( ndgs > 0 & & value ! = 0 ) ;
/* Do leading zeros */
while ( ndgs > 0 )
p [ - - ndgs ] = ' 0 ' ;
}
/****************************************************************************
Convert from octal string to long
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static long unoct ( char * p , int ndgs )
{
long value = 0 ;
/* Converts octal string to long, ignoring any non-digit */
while ( - - ndgs )
{
1998-08-15 05:19:26 +04:00
if ( isdigit ( ( int ) * p ) )
1996-05-04 11:50:46 +04:00
value = ( value < < 3 ) | ( long ) ( * p - ' 0 ' ) ;
p + + ;
}
return value ;
}
/****************************************************************************
1997-07-25 00:04:16 +04:00
Compare two strings in a slash insensitive way , allowing s1 to match s2
if s1 is an " initial " string ( up to directory marker ) . Thus , if s2 is
a file in any subdirectory of s1 , declare a match .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1997-08-28 00:20:58 +04:00
static int strslashcmp ( char * s1 , char * s2 )
1996-05-04 11:50:46 +04:00
{
1997-07-25 00:04:16 +04:00
char * s1_0 = s1 ;
1996-05-04 11:50:46 +04:00
while ( * s1 & & * s2 & &
( * s1 = = * s2
| | tolower ( * s1 ) = = tolower ( * s2 )
| | ( * s1 = = ' \\ ' & & * s2 = = ' / ' )
| | ( * s1 = = ' / ' & & * s2 = = ' \\ ' ) ) ) {
s1 + + ; s2 + + ;
}
1997-07-25 00:04:16 +04:00
/* if s1 has a trailing slash, it compared equal, so s1 is an "initial"
string of s2 .
*/
if ( ! * s1 & & s1 ! = s1_0 & & ( * ( s1 - 1 ) = = ' / ' | | * ( s1 - 1 ) = = ' \\ ' ) ) return 0 ;
/* ignore trailing slash on s1 */
if ( ! * s2 & & ( * s1 = = ' / ' | | * s1 = = ' \\ ' ) & & ! * ( s1 + 1 ) ) return 0 ;
/* check for s1 is an "initial" string of s2 */
if ( * s2 = = ' / ' | | * s2 = = ' \\ ' ) return 0 ;
1996-05-04 11:50:46 +04:00
return * s1 - * s2 ;
}
/****************************************************************************
Ensure a remote path exists ( make if necessary )
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-11-09 06:45:49 +03:00
static BOOL ensurepath ( char * fname )
1996-05-04 11:50:46 +04:00
{
/* *must* be called with buffer ready malloc'ed */
/* ensures path exists */
1998-05-08 17:51:17 +04:00
char * partpath , * ffname ;
1996-05-04 11:50:46 +04:00
char * p = fname , * basehack ;
1998-04-11 11:52:13 +04:00
DEBUG ( 5 , ( " Ensurepath called with: %s \n " , fname ) ) ;
1998-05-08 17:51:17 +04:00
partpath = string_create_s ( strlen ( fname ) ) ;
ffname = string_create_s ( strlen ( fname ) ) ;
if ( ( partpath = = NULL ) | | ( ffname = = NULL ) ) {
DEBUG ( 0 , ( " Out of memory in ensurepath: %s \n " , fname ) ) ;
return ( False ) ;
}
1996-05-04 11:50:46 +04:00
* partpath = 0 ;
/* fname copied to ffname so can strtok */
1998-06-17 05:52:57 +04:00
safe_strcpy ( ffname , fname , strlen ( fname ) ) ;
1996-05-04 11:50:46 +04:00
/* do a `basename' on ffname, so don't try and make file name directory */
2001-07-04 11:36:09 +04:00
if ( ( basehack = strrchr_m ( ffname , ' \\ ' ) ) = = NULL )
1996-05-04 11:50:46 +04:00
return True ;
else
* basehack = ' \0 ' ;
p = strtok ( ffname , " \\ " ) ;
while ( p )
{
1998-06-17 05:52:57 +04:00
safe_strcat ( partpath , p , strlen ( fname ) + 1 ) ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( ! cli_chkpath ( cli , partpath ) ) {
if ( ! cli_mkdir ( cli , partpath ) )
1996-05-04 11:50:46 +04:00
{
DEBUG ( 0 , ( " Error mkdirhiering \n " ) ) ;
return False ;
}
else
DEBUG ( 3 , ( " mkdirhiering %s \n " , partpath ) ) ;
}
1998-06-17 05:52:57 +04:00
safe_strcat ( partpath , " \\ " , strlen ( fname ) + 1 ) ;
1996-05-04 11:50:46 +04:00
p = strtok ( NULL , " / \\ " ) ;
}
return True ;
}
1998-09-05 09:07:05 +04:00
static int padit ( char * buf , int bufsize , int padsize )
1997-01-09 21:02:17 +03:00
{
1998-11-09 06:45:49 +03:00
int berr = 0 ;
int bytestowrite ;
1997-01-09 21:02:17 +03:00
1998-11-09 06:45:49 +03:00
DEBUG ( 5 , ( " Padding with %d zeros \n " , padsize ) ) ;
memset ( buf , 0 , bufsize ) ;
while ( ! berr & & padsize > 0 ) {
bytestowrite = MIN ( bufsize , padsize ) ;
berr = dotarbuf ( tarhandle , buf , bytestowrite ) ! = bytestowrite ;
padsize - = bytestowrite ;
}
1997-01-09 21:02:17 +03:00
1998-11-09 06:45:49 +03:00
return berr ;
1997-01-09 21:02:17 +03:00
}
1998-11-09 06:45:49 +03:00
1998-11-09 23:33:37 +03:00
static void do_setrattr ( char * name , uint16 attr , int set )
1998-11-09 06:45:49 +03:00
{
1998-11-09 23:33:37 +03:00
uint16 oldattr ;
1998-11-09 06:45:49 +03:00
1998-11-14 07:08:19 +03:00
if ( ! cli_getatr ( cli , name , & oldattr , NULL , NULL ) ) return ;
1998-11-09 06:45:49 +03:00
if ( set = = ATTRSET ) {
attr | = oldattr ;
} else {
attr = oldattr & ~ attr ;
}
1998-11-14 07:08:19 +03:00
if ( ! cli_setatr ( cli , name , attr , 0 ) ) {
1998-11-09 06:45:49 +03:00
DEBUG ( 1 , ( " setatr failed: %s \n " , cli_errstr ( cli ) ) ) ;
}
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
append one remote file to the tar file
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void do_atar ( char * rname , char * lname , file_info * finfo1 )
{
int fnum ;
uint32 nread = 0 ;
1998-11-09 23:33:37 +03:00
char ftype ;
1998-06-17 05:52:57 +04:00
file_info2 finfo ;
1996-05-04 11:50:46 +04:00
BOOL close_done = False ;
BOOL shallitime = True ;
1998-11-09 06:45:49 +03:00
char data [ 65520 ] ;
int read_size = 65520 ;
1996-05-04 11:50:46 +04:00
int datalen = 0 ;
struct timeval tp_start ;
GetTimeOfDay ( & tp_start ) ;
1998-04-11 11:52:13 +04:00
ftype = ' 0 ' ; /* An ordinary file ... */
1998-06-17 05:52:57 +04:00
if ( finfo1 ) {
finfo . size = finfo1 - > size ;
finfo . mode = finfo1 - > mode ;
finfo . uid = finfo1 - > uid ;
finfo . gid = finfo1 - > gid ;
finfo . mtime = finfo1 - > mtime ;
finfo . atime = finfo1 - > atime ;
finfo . ctime = finfo1 - > ctime ;
}
else {
finfo . size = def_finfo . size ;
finfo . mode = def_finfo . mode ;
finfo . uid = def_finfo . uid ;
finfo . gid = def_finfo . gid ;
finfo . mtime = def_finfo . mtime ;
finfo . atime = def_finfo . atime ;
finfo . ctime = def_finfo . ctime ;
}
1998-09-21 12:45:11 +04:00
if ( dry_run )
{
DEBUG ( 3 , ( " skipping file %s of size %d bytes \n " ,
finfo . name ,
1999-12-13 16:27:58 +03:00
( int ) finfo . size ) ) ;
1998-09-21 12:45:11 +04:00
shallitime = 0 ;
ttarf + = finfo . size + TBLOCK - ( finfo . size % TBLOCK ) ;
ntarf + + ;
return ;
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
fnum = cli_open ( cli , rname , O_RDONLY , DENY_NONE ) ;
1996-05-04 11:50:46 +04:00
dos_clean_name ( rname ) ;
1998-11-09 06:45:49 +03:00
if ( fnum = = - 1 ) {
DEBUG ( 0 , ( " %s opening remote file %s (%s) \n " ,
cli_errstr ( cli ) , rname , cur_dir ) ) ;
1996-05-04 11:50:46 +04:00
return ;
1998-11-09 06:45:49 +03:00
}
1996-05-04 11:50:46 +04:00
1998-06-17 05:52:57 +04:00
finfo . name = string_create_s ( strlen ( rname ) ) ;
if ( finfo . name = = NULL ) {
1998-11-09 06:45:49 +03:00
DEBUG ( 0 , ( " Unable to allocate space for finfo.name in do_atar \n " ) ) ;
return ;
1998-06-17 05:52:57 +04:00
}
safe_strcpy ( finfo . name , rname , strlen ( rname ) ) ;
1998-11-09 06:45:49 +03:00
if ( ! finfo1 ) {
if ( ! cli_getattrE ( cli , fnum , & finfo . mode , & finfo . size , NULL , & finfo . atime , & finfo . mtime ) ) {
DEBUG ( 0 , ( " getattrE: %s \n " , cli_errstr ( cli ) ) ) ;
return ;
}
finfo . ctime = finfo . mtime ;
}
1996-05-04 11:50:46 +04:00
DEBUG ( 3 , ( " file %s attrib 0x%X \n " , finfo . name , finfo . mode ) ) ;
if ( tar_inc & & ! ( finfo . mode & aARCH ) )
{
DEBUG ( 4 , ( " skipping %s - archive bit not set \n " , finfo . name ) ) ;
shallitime = 0 ;
}
1998-04-02 05:01:24 +04:00
else if ( ! tar_system & & ( finfo . mode & aSYSTEM ) )
{
DEBUG ( 4 , ( " skipping %s - system bit is set \n " , finfo . name ) ) ;
shallitime = 0 ;
}
else if ( ! tar_hidden & & ( finfo . mode & aHIDDEN ) )
{
DEBUG ( 4 , ( " skipping %s - hidden bit is set \n " , finfo . name ) ) ;
shallitime = 0 ;
}
1996-05-04 11:50:46 +04:00
else
{
1998-05-08 17:51:17 +04:00
DEBUG ( 3 , ( " getting file %s of size %d bytes as a tar file %s " ,
1996-05-04 11:50:46 +04:00
finfo . name ,
1999-12-13 16:27:58 +03:00
( int ) finfo . size ,
1996-05-04 11:50:46 +04:00
lname ) ) ;
/* write a tar header, don't bother with mode - just set to 100644 */
1998-04-11 11:52:13 +04:00
writetarheader ( tarhandle , rname , finfo . size , finfo . mtime , " 100644 \0 " , ftype ) ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
while ( nread < finfo . size & & ! close_done ) {
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
DEBUG ( 3 , ( " nread=%d \n " , nread ) ) ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
datalen = cli_read ( cli , fnum , data , nread , read_size ) ;
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( datalen = = - 1 ) {
DEBUG ( 0 , ( " Error reading file %s : %s \n " , rname , cli_errstr ( cli ) ) ) ;
break ;
}
1996-05-04 11:50:46 +04:00
2001-03-27 02:34:25 +04:00
nread + = datalen ;
/* if file size has increased since we made file size query, truncate
read so tar header for this file will be correct .
*/
if ( nread > finfo . size ) {
datalen - = nread - finfo . size ;
DEBUG ( 0 , ( " File size change - truncating %s to %d bytes \n " , finfo . name , ( int ) finfo . size ) ) ;
}
1998-11-09 06:45:49 +03:00
/* add received bits of file to buffer - dotarbuf will
* write out in 512 byte intervals */
if ( dotarbuf ( tarhandle , data , datalen ) ! = datalen ) {
DEBUG ( 0 , ( " Error writing to tar file - %s \n " , strerror ( errno ) ) ) ;
break ;
}
1996-05-04 11:50:46 +04:00
1998-11-09 06:45:49 +03:00
if ( datalen = = 0 ) {
DEBUG ( 0 , ( " Error reading file %s. Got 0 bytes \n " , rname ) ) ;
break ;
1996-05-04 11:50:46 +04:00
}
1998-11-09 06:45:49 +03:00
datalen = 0 ;
}
1997-01-09 21:02:17 +03:00
1998-11-09 06:45:49 +03:00
/* pad tar file with zero's if we couldn't get entire file */
if ( nread < finfo . size ) {
1999-12-13 16:27:58 +03:00
DEBUG ( 0 , ( " Didn't get entire file. size=%d, nread=%d \n " , ( int ) finfo . size , ( int ) nread ) ) ;
1998-11-09 06:45:49 +03:00
if ( padit ( data , sizeof ( data ) , finfo . size - nread ) )
DEBUG ( 0 , ( " Error writing tar file - %s \n " , strerror ( errno ) ) ) ;
}
1997-01-09 21:02:17 +03:00
1996-05-04 11:50:46 +04:00
/* round tar file to nearest block */
if ( finfo . size % TBLOCK )
dozerobuf ( tarhandle , TBLOCK - ( finfo . size % TBLOCK ) ) ;
1997-07-28 22:59:57 +04:00
ttarf + = finfo . size + TBLOCK - ( finfo . size % TBLOCK ) ;
1996-05-04 11:50:46 +04:00
ntarf + + ;
}
1998-11-09 06:45:49 +03:00
cli_close ( cli , fnum ) ;
1996-05-04 11:50:46 +04:00
if ( shallitime )
{
struct timeval tp_end ;
int this_time ;
/* if shallitime is true then we didn't skip */
1998-09-21 12:45:11 +04:00
if ( tar_reset & & ! dry_run )
( void ) do_setrattr ( finfo . name , aARCH , ATTRRESET ) ;
1996-05-04 11:50:46 +04:00
GetTimeOfDay ( & tp_end ) ;
this_time =
( tp_end . tv_sec - tp_start . tv_sec ) * 1000 +
( tp_end . tv_usec - tp_start . tv_usec ) / 1000 ;
get_total_time_ms + = this_time ;
get_total_size + = finfo . size ;
1998-04-02 05:01:24 +04:00
if ( tar_noisy )
{
1998-08-03 14:35:00 +04:00
DEBUG ( 0 , ( " %10d (%7.1f kb/s) %s \n " ,
1999-12-13 16:27:58 +03:00
( int ) finfo . size , finfo . size / MAX ( 0.001 , ( 1.024 * this_time ) ) ,
1998-08-03 14:35:00 +04:00
finfo . name ) ) ;
1998-04-02 05:01:24 +04:00
}
1998-08-03 14:35:00 +04:00
/* Thanks to Carel-Jan Engel (ease@mail.wirehub.nl) for this one */
DEBUG ( 3 , ( " (%g kb/s) (average %g kb/s) \n " ,
finfo . size / MAX ( 0.001 , ( 1.024 * this_time ) ) ,
get_total_size / MAX ( 0.001 , ( 1.024 * get_total_time_ms ) ) ) ) ;
1996-05-04 11:50:46 +04:00
}
}
/****************************************************************************
Append single file to tar file ( or not )
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void do_tar ( file_info * finfo )
{
pstring rname ;
1998-11-09 06:45:49 +03:00
if ( strequal ( finfo - > name , " .. " ) | | strequal ( finfo - > name , " . " ) )
1996-05-04 11:50:46 +04:00
return ;
/* Is it on the exclude list ? */
if ( ! tar_excl & & clipn ) {
pstring exclaim ;
1999-12-13 16:27:58 +03:00
DEBUG ( 5 , ( " Excl: strlen(cur_dir) = %d \n " , ( int ) strlen ( cur_dir ) ) ) ;
1998-06-17 05:52:57 +04:00
safe_strcpy ( exclaim , cur_dir , sizeof ( pstring ) ) ;
1996-05-04 11:50:46 +04:00
* ( exclaim + strlen ( exclaim ) - 1 ) = ' \0 ' ;
1998-06-17 05:52:57 +04:00
safe_strcat ( exclaim , " \\ " , sizeof ( pstring ) ) ;
safe_strcat ( exclaim , finfo - > name , sizeof ( exclaim ) ) ;
1996-05-04 11:50:46 +04:00
1998-05-08 17:51:17 +04:00
DEBUG ( 5 , ( " ...tar_re_search: %d \n " , tar_re_search ) ) ;
if ( ( ! tar_re_search & & clipfind ( cliplist , clipn , exclaim ) ) | |
# ifdef HAVE_REGEX_H
( tar_re_search & & ! regexec ( preg , exclaim , 0 , NULL , 0 ) ) ) {
# else
2000-04-30 15:04:28 +04:00
( tar_re_search & & mask_match ( exclaim , cliplist [ 0 ] , True ) ) ) {
1998-05-08 17:51:17 +04:00
# endif
1996-05-04 11:50:46 +04:00
DEBUG ( 3 , ( " Skipping file %s \n " , exclaim ) ) ;
return ;
}
}
if ( finfo - > mode & aDIR )
{
pstring saved_curdir ;
pstring mtar_mask ;
1998-06-17 05:52:57 +04:00
safe_strcpy ( saved_curdir , cur_dir , sizeof ( saved_curdir ) ) ;
1999-12-13 16:27:58 +03:00
DEBUG ( 5 , ( " Sizeof(cur_dir)=%d, strlen(cur_dir)=%d, strlen(finfo->name)=%d \n name=%s,cur_dir=%s \n " , ( int ) sizeof ( cur_dir ) , ( int ) strlen ( cur_dir ) , ( int ) strlen ( finfo - > name ) , finfo - > name , cur_dir ) ) ;
1996-05-04 11:50:46 +04:00
1998-06-17 05:52:57 +04:00
safe_strcat ( cur_dir , finfo - > name , sizeof ( cur_dir ) ) ;
safe_strcat ( cur_dir , " \\ " , sizeof ( cur_dir ) ) ;
1996-05-04 11:50:46 +04:00
1998-04-11 11:52:13 +04:00
DEBUG ( 5 , ( " Writing a dir, Name = %s \n " , cur_dir ) ) ;
1996-05-04 11:50:46 +04:00
/* write a tar directory, don't bother with mode - just set it to
* 40755 */
1998-04-11 11:52:13 +04:00
writetarheader ( tarhandle , cur_dir , 0 , finfo - > mtime , " 040755 \0 " , ' 5 ' ) ;
1998-06-17 05:52:57 +04:00
if ( tar_noisy ) {
1998-11-09 06:45:49 +03:00
DEBUG ( 0 , ( " directory %s \n " , cur_dir ) ) ;
1998-06-17 05:52:57 +04:00
}
1998-04-11 11:52:13 +04:00
ntarf + + ; /* Make sure we have a file on there */
1998-06-17 05:52:57 +04:00
safe_strcpy ( mtar_mask , cur_dir , sizeof ( pstring ) ) ;
1998-11-14 06:53:24 +03:00
safe_strcat ( mtar_mask , " * " , sizeof ( pstring ) ) ;
1998-12-19 05:08:35 +03:00
DEBUG ( 5 , ( " Doing list with mtar_mask: %s \n " , mtar_mask ) ) ;
1998-11-09 06:45:49 +03:00
do_list ( mtar_mask , attribute , do_tar , False , True ) ;
1998-06-17 05:52:57 +04:00
safe_strcpy ( cur_dir , saved_curdir , sizeof ( pstring ) ) ;
1996-05-04 11:50:46 +04:00
}
else
{
1998-06-17 05:52:57 +04:00
safe_strcpy ( rname , cur_dir , sizeof ( pstring ) ) ;
safe_strcat ( rname , finfo - > name , sizeof ( pstring ) ) ;
1996-05-04 11:50:46 +04:00
do_atar ( rname , finfo - > name , finfo ) ;
}
}
/****************************************************************************
Convert from UNIX to DOS file names
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-06-17 05:52:57 +04:00
static void unfixtarname ( char * tptr , char * fp , int l , BOOL first )
1996-05-04 11:50:46 +04:00
{
2001-07-04 11:15:53 +04:00
/* remove '.' from start of file name, convert from unix /'s to
* dos \ ' s in path . Kill any absolute path names . But only if first !
*/
DEBUG ( 5 , ( " firstb=%lX, secondb=%lX, len=%i \n " , ( long ) tptr , ( long ) fp , l ) ) ;
if ( first ) {
if ( * fp = = ' . ' ) {
fp + + ;
l - - ;
}
if ( * fp = = ' \\ ' | | * fp = = ' / ' ) {
fp + + ;
l - - ;
}
}
1996-05-04 11:50:46 +04:00
2001-07-04 11:15:53 +04:00
safe_strcpy ( tptr , fp , l ) ;
string_replace ( tptr , ' / ' , ' \\ ' ) ;
1996-05-04 11:50:46 +04:00
}
1998-05-08 20:59:30 +04:00
1998-05-08 17:51:17 +04:00
/****************************************************************************
Move to the next block in the buffer , which may mean read in another set of
1998-09-18 16:47:46 +04:00
blocks . FIXME , we should allow more than one block to be skipped .
1998-05-08 17:51:17 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-09-18 16:47:46 +04:00
static int next_block ( char * ltarbuf , char * * bufferp , int bufsiz )
1998-05-06 06:35:56 +04:00
{
1998-05-08 17:51:17 +04:00
int bufread , total = 0 ;
1998-10-05 16:34:43 +04:00
DEBUG ( 5 , ( " Advancing to next block: %0lx \n " , ( unsigned long ) * bufferp ) ) ;
1998-09-18 16:47:46 +04:00
* bufferp + = TBLOCK ;
total = TBLOCK ;
if ( * bufferp > = ( ltarbuf + bufsiz ) ) {
DEBUG ( 5 , ( " Reading more data into ltarbuf ... \n " ) ) ;
1999-12-13 16:27:58 +03:00
/*
* Bugfix from Bob Boehmer < boehmer @ worldnet . att . net >
* Fixes bug where read can return short if coming from
* a pipe .
*/
1998-09-18 16:47:46 +04:00
1999-12-13 16:27:58 +03:00
bufread = read ( tarhandle , ltarbuf , bufsiz ) ;
total = bufread ;
1998-05-08 17:51:17 +04:00
1999-12-13 16:27:58 +03:00
while ( total < bufsiz ) {
if ( bufread < 0 ) { /* An error, return false */
return ( total > 0 ? - 2 : bufread ) ;
1998-05-08 17:51:17 +04:00
}
1999-12-13 16:27:58 +03:00
if ( bufread = = 0 ) {
if ( total < = 0 ) {
return - 2 ;
}
break ;
}
bufread = read ( tarhandle , & ltarbuf [ total ] , bufsiz - total ) ;
total + = bufread ;
1998-05-08 17:51:17 +04:00
}
1998-09-18 16:47:46 +04:00
DEBUG ( 5 , ( " Total bytes read ... %i \n " , total ) ) ;
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
* bufferp = ltarbuf ;
1998-05-08 17:51:17 +04:00
}
1998-09-18 16:47:46 +04:00
return ( total ) ;
1998-05-08 17:51:17 +04:00
}
1998-09-18 16:47:46 +04:00
/* Skip a file, even if it includes a long file name? */
static int skip_file ( int skipsize )
1998-05-08 17:51:17 +04:00
{
1998-09-18 16:47:46 +04:00
int dsize = skipsize ;
DEBUG ( 5 , ( " Skiping file. Size = %i \n " , skipsize ) ) ;
/* FIXME, we should skip more than one block at a time */
while ( dsize > 0 ) {
if ( next_block ( tarbuf , & buffer_p , tbufsiz ) < = 0 ) {
DEBUG ( 0 , ( " Empty file, short tar file, or read error: %s \n " , strerror ( errno ) ) ) ;
return ( False ) ;
}
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
dsize - = TBLOCK ;
}
return ( True ) ;
1998-05-08 17:51:17 +04:00
}
1999-12-13 16:27:58 +03:00
/*************************************************************
Get a file from the tar file and store it .
When this is called , tarbuf already contains the first
file block . This is a bit broken & needs fixing .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-11-09 06:45:49 +03:00
static int get_file ( file_info2 finfo )
1998-05-08 17:51:17 +04:00
{
1998-11-14 00:41:01 +03:00
int fnum = - 1 , pos = 0 , dsize = 0 , rsize = 0 , bpos = 0 ;
1998-09-18 16:47:46 +04:00
1999-12-13 16:27:58 +03:00
DEBUG ( 5 , ( " get_file: file: %s, size %i \n " , finfo . name , ( int ) finfo . size ) ) ;
1998-09-18 16:47:46 +04:00
1998-11-09 06:45:49 +03:00
if ( ensurepath ( finfo . name ) & &
2000-01-08 13:15:53 +03:00
( fnum = cli_open ( cli , finfo . name , O_RDWR | O_CREAT | O_TRUNC , DENY_NONE ) ) = = - 1 ) {
1998-09-18 16:47:46 +04:00
DEBUG ( 0 , ( " abandoning restore \n " ) ) ;
return ( False ) ;
1999-12-13 16:27:58 +03:00
}
1998-09-18 16:47:46 +04:00
/* read the blocks from the tar file and write to the remote file */
1999-12-13 16:27:58 +03:00
rsize = finfo . size ; /* This is how much to write */
1998-09-18 16:47:46 +04:00
while ( rsize > 0 ) {
1998-09-23 18:37:01 +04:00
/* We can only write up to the end of the buffer */
1998-09-18 16:47:46 +04:00
1998-11-09 06:45:49 +03:00
dsize = MIN ( tbufsiz - ( buffer_p - tarbuf ) - bpos , 65520 ) ; /* Calculate the size to write */
1998-09-23 18:37:01 +04:00
dsize = MIN ( dsize , rsize ) ; /* Should be only what is left */
1998-11-09 06:45:49 +03:00
DEBUG ( 5 , ( " writing %i bytes, bpos = %i ... \n " , dsize , bpos ) ) ;
1998-09-18 16:47:46 +04:00
1998-11-09 06:45:49 +03:00
if ( cli_write ( cli , fnum , 0 , buffer_p + bpos , pos , dsize ) ! = dsize ) {
DEBUG ( 0 , ( " Error writing remote file \n " ) ) ;
return 0 ;
1998-09-18 16:47:46 +04:00
}
rsize - = dsize ;
1998-09-23 18:37:01 +04:00
pos + = dsize ;
1998-09-18 16:47:46 +04:00
/* Now figure out how much to move in the buffer */
/* FIXME, we should skip more than one block at a time */
1998-09-23 18:37:01 +04:00
/* First, skip any initial part of the part written that is left over */
/* from the end of the first TBLOCK */
1998-09-24 17:43:36 +04:00
if ( ( bpos ) & & ( ( bpos + dsize ) > = TBLOCK ) ) {
1998-09-23 18:37:01 +04:00
dsize - = ( TBLOCK - bpos ) ; /* Get rid of the end of the first block */
bpos = 0 ;
if ( next_block ( tarbuf , & buffer_p , tbufsiz ) < = 0 ) { /* and skip the block */
DEBUG ( 0 , ( " Empty file, short tar file, or read error: %s \n " , strerror ( errno ) ) ) ;
return False ;
}
}
1999-12-13 16:27:58 +03:00
/*
* Bugfix from Bob Boehmer < boehmer @ worldnet . att . net > .
* If the file being extracted is an exact multiple of
* TBLOCK bytes then we don ' t want to extract the next
* block from the tarfile here , as it will be done in
* the caller of get_file ( ) .
*/
1998-09-18 16:47:46 +04:00
1999-12-13 16:27:58 +03:00
while ( ( ( rsize ! = 0 ) & & ( dsize > = TBLOCK ) ) | |
( ( rsize = = 0 ) & & ( dsize > TBLOCK ) ) ) {
1998-09-18 16:47:46 +04:00
1999-12-13 16:27:58 +03:00
if ( next_block ( tarbuf , & buffer_p , tbufsiz ) < = 0 ) {
1998-09-18 16:47:46 +04:00
DEBUG ( 0 , ( " Empty file, short tar file, or read error: %s \n " , strerror ( errno ) ) ) ;
return False ;
}
dsize - = TBLOCK ;
}
1998-09-23 18:37:01 +04:00
bpos = dsize ;
1998-09-18 16:47:46 +04:00
}
/* Now close the file ... */
1998-11-09 06:45:49 +03:00
if ( ! cli_close ( cli , fnum ) ) {
DEBUG ( 0 , ( " Error closing remote file \n " ) ) ;
return ( False ) ;
1998-09-18 16:47:46 +04:00
}
/* Now we update the creation date ... */
DEBUG ( 5 , ( " Updating creation date on %s \n " , finfo . name ) ) ;
1998-11-09 06:45:49 +03:00
if ( ! cli_setatr ( cli , finfo . name , finfo . mode , finfo . mtime ) ) {
if ( tar_real_noisy ) {
DEBUG ( 0 , ( " Could not set time on file: %s \n " , finfo . name ) ) ;
/*return(False); */ /* Ignore, as Win95 does not allow changes */
}
1998-09-18 16:47:46 +04:00
}
ntarf + + ;
1998-09-23 18:37:01 +04:00
1999-12-13 16:27:58 +03:00
DEBUG ( 0 , ( " restore tar file %s of size %d bytes \n " , finfo . name , ( int ) finfo . size ) ) ;
1998-09-23 18:37:01 +04:00
1998-09-18 16:47:46 +04:00
return ( True ) ;
1998-05-08 17:51:17 +04:00
}
1998-09-18 16:47:46 +04:00
/* Create a directory. We just ensure that the path exists and return as there
is no file associated with a directory
*/
1998-11-09 06:45:49 +03:00
static int get_dir ( file_info2 finfo )
1998-05-08 17:51:17 +04:00
{
1999-12-13 16:27:58 +03:00
DEBUG ( 0 , ( " restore directory %s \n " , finfo . name ) ) ;
1998-05-08 17:51:17 +04:00
1998-11-09 06:45:49 +03:00
if ( ! ensurepath ( finfo . name ) ) {
1998-09-18 16:47:46 +04:00
DEBUG ( 0 , ( " Problems creating directory \n " ) ) ;
return ( False ) ;
}
1999-12-13 16:27:58 +03:00
ntarf + + ;
1998-09-18 16:47:46 +04:00
return ( True ) ;
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
}
/* Get a file with a long file name ... first file has file name, next file
has the data . We only want the long file name , as the loop in do_tarput
will deal with the rest .
*/
1998-05-08 17:51:17 +04:00
static char * get_longfilename ( file_info2 finfo )
{
1998-09-18 16:47:46 +04:00
int namesize = finfo . size + strlen ( cur_dir ) + 2 ;
char * longname = malloc ( namesize ) ;
int offset = 0 , left = finfo . size ;
1998-09-19 08:25:10 +04:00
BOOL first = True ;
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
DEBUG ( 5 , ( " Restoring a long file name: %s \n " , finfo . name ) ) ;
1999-12-13 16:27:58 +03:00
DEBUG ( 5 , ( " Len = %d \n " , ( int ) finfo . size ) ) ;
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
if ( longname = = NULL ) {
DEBUG ( 0 , ( " could not allocate buffer of size %d for longname \n " ,
1999-12-13 16:27:58 +03:00
( int ) ( finfo . size + strlen ( cur_dir ) + 2 ) ) ) ;
1998-09-18 16:47:46 +04:00
return ( NULL ) ;
}
/* First, add cur_dir to the long file name */
if ( strlen ( cur_dir ) > 0 ) {
strncpy ( longname , cur_dir , namesize ) ;
offset = strlen ( cur_dir ) ;
}
/* Loop through the blocks picking up the name */
while ( left > 0 ) {
if ( next_block ( tarbuf , & buffer_p , tbufsiz ) < = 0 ) {
DEBUG ( 0 , ( " Empty file, short tar file, or read error: %s \n " , strerror ( errno ) ) ) ;
return ( NULL ) ;
}
1998-09-19 08:25:10 +04:00
unfixtarname ( longname + offset , buffer_p , MIN ( TBLOCK , finfo . size ) , first - - ) ;
1998-09-18 16:47:46 +04:00
DEBUG ( 5 , ( " UnfixedName: %s, buffer: %s \n " , longname , buffer_p ) ) ;
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
offset + = TBLOCK ;
left - = TBLOCK ;
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
}
return ( longname ) ;
}
static void do_tarput ( void )
1998-05-08 17:51:17 +04:00
{
1998-09-23 04:57:34 +04:00
file_info2 finfo ;
1998-05-08 17:51:17 +04:00
struct timeval tp_start ;
1998-11-09 06:45:49 +03:00
char * longfilename = NULL , linkflag ;
1998-05-08 17:51:17 +04:00
int skip = False ;
GetTimeOfDay ( & tp_start ) ;
1998-09-18 16:47:46 +04:00
DEBUG ( 5 , ( " RJS do_tarput called ... \n " ) ) ;
buffer_p = tarbuf + tbufsiz ; /* init this to force first read */
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
/* Now read through those files ... */
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
while ( True ) {
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
/* Get us to the next block, or the first block first time around */
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
if ( next_block ( tarbuf , & buffer_p , tbufsiz ) < = 0 ) {
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
DEBUG ( 0 , ( " Empty file, short tar file, or read error: %s \n " , strerror ( errno ) ) ) ;
return ;
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
}
DEBUG ( 5 , ( " Reading the next header ... \n " ) ) ;
1998-09-23 18:37:01 +04:00
1998-09-18 16:47:46 +04:00
switch ( readtarheader ( ( union hblock * ) buffer_p , & finfo , cur_dir ) ) {
1998-05-08 17:51:17 +04:00
case - 2 : /* Hmm, not good, but not fatal */
DEBUG ( 0 , ( " Skipping %s... \n " , finfo . name ) ) ;
1998-09-18 16:47:46 +04:00
if ( ( next_block ( tarbuf , & buffer_p , tbufsiz ) < = 0 ) & &
1998-05-08 17:51:17 +04:00
! skip_file ( finfo . size ) ) {
DEBUG ( 0 , ( " Short file, bailing out... \n " ) ) ;
1998-09-23 18:37:01 +04:00
return ;
1998-05-08 17:51:17 +04:00
}
break ;
case - 1 :
DEBUG ( 0 , ( " abandoning restore, -1 from read tar header \n " ) ) ;
return ;
case 0 : /* chksum is zero - looks like an EOF */
1999-12-13 16:27:58 +03:00
DEBUG ( 0 , ( " tar: restored %d files and directories \n " , ntarf ) ) ;
1998-05-08 17:51:17 +04:00
return ; /* Hmmm, bad here ... */
1998-09-23 18:37:01 +04:00
default :
/* No action */
1998-05-08 17:51:17 +04:00
break ;
}
/* Now, do we have a long file name? */
if ( longfilename ! = NULL ) {
1998-09-18 16:47:46 +04:00
free ( finfo . name ) ; /* Free the space already allocated */
finfo . name = longfilename ;
longfilename = NULL ;
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
}
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
/* Well, now we have a header, process the file ... */
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
/* Should we skip the file? We have the long name as well here */
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
skip = clipn & &
( ( ! tar_re_search & & clipfind ( cliplist , clipn , finfo . name ) ^ tar_excl )
# ifdef HAVE_REGEX_H
| | ( tar_re_search & & ! regexec ( preg , finfo . name , 0 , NULL , 0 ) ) ) ;
# else
2000-04-30 15:04:28 +04:00
| | ( tar_re_search & & mask_match ( finfo . name , cliplist [ 0 ] , True ) ) ) ;
1998-09-18 16:47:46 +04:00
# endif
1998-05-08 17:51:17 +04:00
1998-09-23 18:37:01 +04:00
DEBUG ( 5 , ( " Skip = %i, cliplist=%s, file=%s \n " , skip , ( cliplist ? cliplist [ 0 ] : NULL ) , finfo . name ) ) ;
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
if ( skip ) {
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
skip_file ( finfo . size ) ;
continue ;
1998-05-08 17:51:17 +04:00
1998-09-18 16:47:46 +04:00
}
1998-05-08 17:51:17 +04:00
/* We only get this far if we should process the file */
1998-09-23 18:37:01 +04:00
linkflag = ( ( union hblock * ) buffer_p ) - > dbuf . linkflag ;
1998-05-08 17:51:17 +04:00
1998-09-23 18:37:01 +04:00
switch ( linkflag ) {
1998-05-08 17:51:17 +04:00
case ' 0 ' : /* Should use symbolic names--FIXME */
1998-09-23 18:37:01 +04:00
1999-12-13 16:27:58 +03:00
/*
* Skip to the next block first , so we can get the file , FIXME , should
* be in get_file . . .
* The ' finfo . size ! = 0 ' fix is from Bob Boehmer < boehmer @ worldnet . att . net >
* Fixes bug where file size in tarfile is zero .
*/
1998-09-23 18:37:01 +04:00
1999-12-13 16:27:58 +03:00
if ( ( finfo . size ! = 0 ) & & next_block ( tarbuf , & buffer_p , tbufsiz ) < = 0 ) {
1998-09-23 18:37:01 +04:00
DEBUG ( 0 , ( " Short file, bailing out... \n " ) ) ;
return ;
}
1998-11-09 06:45:49 +03:00
if ( ! get_file ( finfo ) ) {
1998-09-18 16:47:46 +04:00
DEBUG ( 0 , ( " Abandoning restore \n " ) ) ;
return ;
}
1998-05-08 17:51:17 +04:00
break ;
case ' 5 ' :
1998-11-09 06:45:49 +03:00
if ( ! get_dir ( finfo ) ) {
1998-09-18 16:47:46 +04:00
DEBUG ( 0 , ( " Abandoning restore \n " ) ) ;
return ;
}
1998-05-08 17:51:17 +04:00
break ;
case ' L ' :
longfilename = get_longfilename ( finfo ) ;
1998-09-18 16:47:46 +04:00
if ( ! longfilename ) {
DEBUG ( 0 , ( " abandoning restore \n " ) ) ;
return ;
}
DEBUG ( 5 , ( " Long file name: %s \n " , longfilename ) ) ;
1998-05-08 17:51:17 +04:00
break ;
default :
skip_file ( finfo . size ) ; /* Don't handle these yet */
break ;
}
}
}
1998-09-18 16:47:46 +04:00
1996-05-04 11:50:46 +04:00
/*
* samba interactive commands
*/
/****************************************************************************
Blocksize command
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-11-09 06:45:49 +03:00
void cmd_block ( void )
1996-05-04 11:50:46 +04:00
{
fstring buf ;
int block ;
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) )
1996-05-04 11:50:46 +04:00
{
DEBUG ( 0 , ( " blocksize <n> \n " ) ) ;
return ;
}
block = atoi ( buf ) ;
if ( block < 0 | | block > 65535 )
{
DEBUG ( 0 , ( " blocksize out of range " ) ) ;
return ;
}
blocksize = block ;
1998-04-02 05:01:24 +04:00
DEBUG ( 2 , ( " blocksize is now %d \n " , blocksize ) ) ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
command to set incremental / reset mode
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-11-09 06:45:49 +03:00
void cmd_tarmode ( void )
1996-05-04 11:50:46 +04:00
{
fstring buf ;
2001-06-21 13:10:42 +04:00
while ( next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) ) {
1996-05-04 11:50:46 +04:00
if ( strequal ( buf , " full " ) )
tar_inc = False ;
else if ( strequal ( buf , " inc " ) )
tar_inc = True ;
else if ( strequal ( buf , " reset " ) )
tar_reset = True ;
else if ( strequal ( buf , " noreset " ) )
tar_reset = False ;
1998-04-02 05:01:24 +04:00
else if ( strequal ( buf , " system " ) )
tar_system = True ;
else if ( strequal ( buf , " nosystem " ) )
tar_system = False ;
else if ( strequal ( buf , " hidden " ) )
tar_hidden = True ;
else if ( strequal ( buf , " nohidden " ) )
tar_hidden = False ;
else if ( strequal ( buf , " verbose " ) | | strequal ( buf , " noquiet " ) )
tar_noisy = True ;
else if ( strequal ( buf , " quiet " ) | | strequal ( buf , " noverbose " ) )
tar_noisy = False ;
1996-05-04 11:50:46 +04:00
else DEBUG ( 0 , ( " tarmode: unrecognised option %s \n " , buf ) ) ;
}
1998-04-02 05:01:24 +04:00
DEBUG ( 0 , ( " tarmode is now %s, %s, %s, %s, %s \n " ,
1996-05-04 11:50:46 +04:00
tar_inc ? " incremental " : " full " ,
1998-04-02 05:01:24 +04:00
tar_system ? " system " : " nosystem " ,
tar_hidden ? " hidden " : " nohidden " ,
tar_reset ? " reset " : " noreset " ,
tar_noisy ? " verbose " : " quiet " ) ) ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
Feeble attrib command
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-11-09 06:45:49 +03:00
void cmd_setmode ( void )
1996-05-04 11:50:46 +04:00
{
char * q ;
fstring buf ;
pstring fname ;
1998-11-09 23:33:37 +03:00
uint16 attra [ 2 ] ;
1996-05-04 11:50:46 +04:00
int direct = 1 ;
attra [ 0 ] = attra [ 1 ] = 0 ;
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) )
1996-05-04 11:50:46 +04:00
{
1998-11-14 07:08:19 +03:00
DEBUG ( 0 , ( " setmode <filename> <[+|-]rsha> \n " ) ) ;
1996-05-04 11:50:46 +04:00
return ;
}
1998-06-17 05:52:57 +04:00
safe_strcpy ( fname , cur_dir , sizeof ( pstring ) ) ;
safe_strcat ( fname , buf , sizeof ( pstring ) ) ;
1996-05-04 11:50:46 +04:00
2001-06-21 13:10:42 +04:00
while ( next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) ) {
1996-05-04 11:50:46 +04:00
q = buf ;
while ( * q )
switch ( * q + + ) {
case ' + ' : direct = 1 ;
break ;
case ' - ' : direct = 0 ;
break ;
case ' r ' : attra [ direct ] | = aRONLY ;
break ;
case ' h ' : attra [ direct ] | = aHIDDEN ;
break ;
case ' s ' : attra [ direct ] | = aSYSTEM ;
break ;
case ' a ' : attra [ direct ] | = aARCH ;
break ;
default : DEBUG ( 0 , ( " setmode <filename> <perm=[+|-]rsha> \n " ) ) ;
return ;
}
}
if ( attra [ ATTRSET ] = = 0 & & attra [ ATTRRESET ] = = 0 )
{
1998-11-14 07:08:19 +03:00
DEBUG ( 0 , ( " setmode <filename> <[+|-]rsha> \n " ) ) ;
1996-05-04 11:50:46 +04:00
return ;
}
1998-04-02 05:01:24 +04:00
DEBUG ( 2 , ( " \n perm set %d %d \n " , attra [ ATTRSET ] , attra [ ATTRRESET ] ) ) ;
1998-11-14 07:08:19 +03:00
do_setrattr ( fname , attra [ ATTRSET ] , ATTRSET ) ;
do_setrattr ( fname , attra [ ATTRRESET ] , ATTRRESET ) ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
Principal command for creating / extracting
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-11-09 06:45:49 +03:00
void cmd_tar ( void )
1996-05-04 11:50:46 +04:00
{
fstring buf ;
char * * argl ;
int argcl ;
2001-06-21 13:10:42 +04:00
if ( ! next_token_nr ( NULL , buf , NULL , sizeof ( buf ) ) )
1996-05-04 11:50:46 +04:00
{
1998-09-21 12:45:11 +04:00
DEBUG ( 0 , ( " tar <c|x>[IXbgan] <filename> \n " ) ) ;
1996-05-04 11:50:46 +04:00
return ;
}
argl = toktocliplist ( & argcl , NULL ) ;
if ( ! tar_parseargs ( argcl , argl , buf , 0 ) )
return ;
1998-11-09 06:45:49 +03:00
process_tar ( ) ;
1996-05-04 11:50:46 +04:00
free ( argl ) ;
}
/****************************************************************************
Command line ( option ) version
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-11-09 06:45:49 +03:00
int process_tar ( void )
1996-05-04 11:50:46 +04:00
{
initarbuf ( ) ;
switch ( tar_type ) {
case ' x ' :
1998-05-08 17:51:17 +04:00
1998-05-11 10:38:36 +04:00
#if 0
1998-05-08 17:51:17 +04:00
do_tarput2 ( ) ;
# else
1996-05-04 11:50:46 +04:00
do_tarput ( ) ;
1998-05-08 17:51:17 +04:00
# endif
1996-05-04 11:50:46 +04:00
free ( tarbuf ) ;
close ( tarhandle ) ;
break ;
case ' r ' :
case ' c ' :
if ( clipn & & tar_excl ) {
int i ;
pstring tarmac ;
for ( i = 0 ; i < clipn ; i + + ) {
1998-05-08 17:51:17 +04:00
DEBUG ( 5 , ( " arg %d = %s \n " , i , cliplist [ i ] ) ) ;
1996-05-04 11:50:46 +04:00
if ( * ( cliplist [ i ] + strlen ( cliplist [ i ] ) - 1 ) = = ' \\ ' ) {
* ( cliplist [ i ] + strlen ( cliplist [ i ] ) - 1 ) = ' \0 ' ;
}
2001-07-04 11:36:09 +04:00
if ( strrchr_m ( cliplist [ i ] , ' \\ ' ) ) {
1996-05-04 11:50:46 +04:00
pstring saved_dir ;
1998-06-17 05:52:57 +04:00
safe_strcpy ( saved_dir , cur_dir , sizeof ( pstring ) ) ;
1996-05-04 11:50:46 +04:00
if ( * cliplist [ i ] = = ' \\ ' ) {
1998-06-17 05:52:57 +04:00
safe_strcpy ( tarmac , cliplist [ i ] , sizeof ( pstring ) ) ;
1996-05-04 11:50:46 +04:00
} else {
1998-06-17 05:52:57 +04:00
safe_strcpy ( tarmac , cur_dir , sizeof ( pstring ) ) ;
safe_strcat ( tarmac , cliplist [ i ] , sizeof ( pstring ) ) ;
1996-05-04 11:50:46 +04:00
}
1998-06-17 05:52:57 +04:00
safe_strcpy ( cur_dir , tarmac , sizeof ( pstring ) ) ;
2001-07-04 11:36:09 +04:00
* ( strrchr_m ( cur_dir , ' \\ ' ) + 1 ) = ' \0 ' ;
1996-05-04 11:50:46 +04:00
1998-12-19 05:08:35 +03:00
DEBUG ( 5 , ( " process_tar, do_list with tarmac: %s \n " , tarmac ) ) ;
1998-11-09 06:45:49 +03:00
do_list ( tarmac , attribute , do_tar , False , True ) ;
1998-06-17 05:52:57 +04:00
safe_strcpy ( cur_dir , saved_dir , sizeof ( pstring ) ) ;
1996-05-04 11:50:46 +04:00
} else {
1998-06-17 05:52:57 +04:00
safe_strcpy ( tarmac , cur_dir , sizeof ( pstring ) ) ;
safe_strcat ( tarmac , cliplist [ i ] , sizeof ( pstring ) ) ;
1998-12-19 05:08:35 +03:00
DEBUG ( 5 , ( " process_tar, do_list with tarmac: %s \n " , tarmac ) ) ;
1998-11-09 06:45:49 +03:00
do_list ( tarmac , attribute , do_tar , False , True ) ;
1996-05-04 11:50:46 +04:00
}
}
} else {
pstring mask ;
1998-06-17 05:52:57 +04:00
safe_strcpy ( mask , cur_dir , sizeof ( pstring ) ) ;
1999-02-03 19:30:54 +03:00
DEBUG ( 5 , ( " process_tar, do_list with mask: %s \n " , mask ) ) ;
1998-12-18 01:44:56 +03:00
safe_strcat ( mask , " \\ * " , sizeof ( pstring ) ) ;
1998-11-09 06:45:49 +03:00
do_list ( mask , attribute , do_tar , False , True ) ;
1996-05-04 11:50:46 +04:00
}
if ( ntarf ) dotareof ( tarhandle ) ;
close ( tarhandle ) ;
free ( tarbuf ) ;
1999-12-13 16:27:58 +03:00
DEBUG ( 0 , ( " tar: dumped %d files and directories \n " , ntarf ) ) ;
1999-03-30 14:25:20 +04:00
DEBUG ( 0 , ( " Total bytes written: %.0f \n " , ( double ) ttarf ) ) ;
1996-05-04 11:50:46 +04:00
break ;
}
1998-06-17 05:52:57 +04:00
if ( must_free_cliplist ) {
int i ;
for ( i = 0 ; i < clipn ; + + i ) {
free ( cliplist [ i ] ) ;
}
free ( cliplist ) ;
cliplist = NULL ;
clipn = 0 ;
must_free_cliplist = False ;
}
1996-05-04 11:50:46 +04:00
return ( 0 ) ;
}
/****************************************************************************
Find a token ( filename ) in a clip list
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1998-09-05 09:07:05 +04:00
static int clipfind ( char * * aret , int ret , char * tok )
1996-05-04 11:50:46 +04:00
{
if ( aret = = NULL ) return 0 ;
/* ignore leading slashes or dots in token */
2001-07-04 11:36:09 +04:00
while ( strchr_m ( " / \\ . " , * tok ) ) tok + + ;
1996-05-04 11:50:46 +04:00
while ( ret - - ) {
char * pkey = * aret + + ;
/* ignore leading slashes or dots in list */
2001-07-04 11:36:09 +04:00
while ( strchr_m ( " / \\ . " , * pkey ) ) pkey + + ;
1996-05-04 11:50:46 +04:00
if ( ! strslashcmp ( pkey , tok ) ) return 1 ;
}
return 0 ;
}
1998-06-17 05:52:57 +04:00
/****************************************************************************
Read list of files to include from the file and initialize cliplist
accordingly .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static int read_inclusion_file ( char * filename )
{
2001-09-10 15:08:57 +04:00
XFILE * inclusion = NULL ;
1998-06-17 05:52:57 +04:00
char buf [ MAXPATHLEN + 1 ] ;
char * inclusion_buffer = NULL ;
int inclusion_buffer_size = 0 ;
int inclusion_buffer_sofar = 0 ;
char * p ;
char * tmpstr ;
int i ;
int error = 0 ;
clipn = 0 ;
buf [ MAXPATHLEN ] = ' \0 ' ; /* guarantee null-termination */
2001-09-10 15:08:57 +04:00
if ( ( inclusion = x_fopen ( filename , O_RDONLY , 0 ) ) = = NULL ) {
1998-06-17 05:52:57 +04:00
/* XXX It would be better to include a reason for failure, but without
* autoconf , it ' s hard to use strerror , sys_errlist , etc .
*/
DEBUG ( 0 , ( " Unable to open inclusion file %s \n " , filename ) ) ;
return 0 ;
}
2001-09-10 15:08:57 +04:00
while ( ( ! error ) & & ( x_fgets ( buf , sizeof ( buf ) - 1 , inclusion ) ) ) {
1998-06-17 05:52:57 +04:00
if ( inclusion_buffer = = NULL ) {
inclusion_buffer_size = 1024 ;
if ( ( inclusion_buffer = malloc ( inclusion_buffer_size ) ) = = NULL ) {
DEBUG ( 0 , ( " failure allocating buffer to read inclusion file \n " ) ) ;
error = 1 ;
break ;
}
}
if ( buf [ strlen ( buf ) - 1 ] = = ' \n ' ) {
buf [ strlen ( buf ) - 1 ] = ' \0 ' ;
}
if ( ( strlen ( buf ) + 1 + inclusion_buffer_sofar ) > = inclusion_buffer_size ) {
2001-08-12 21:30:01 +04:00
char * ib ;
1998-06-17 05:52:57 +04:00
inclusion_buffer_size * = 2 ;
2001-08-12 21:30:01 +04:00
ib = Realloc ( inclusion_buffer , inclusion_buffer_size ) ;
if ( ! ib ) {
1998-06-17 05:52:57 +04:00
DEBUG ( 0 , ( " failure enlarging inclusion buffer to %d bytes \n " ,
inclusion_buffer_size ) ) ;
error = 1 ;
break ;
}
2001-08-12 21:30:01 +04:00
else inclusion_buffer = ib ;
1998-06-17 05:52:57 +04:00
}
safe_strcpy ( inclusion_buffer + inclusion_buffer_sofar , buf , inclusion_buffer_size - inclusion_buffer_sofar ) ;
inclusion_buffer_sofar + = strlen ( buf ) + 1 ;
clipn + + ;
}
2001-09-10 15:08:57 +04:00
x_fclose ( inclusion ) ;
1998-06-17 05:52:57 +04:00
if ( ! error ) {
/* Allocate an array of clipn + 1 char*'s for cliplist */
cliplist = malloc ( ( clipn + 1 ) * sizeof ( char * ) ) ;
if ( cliplist = = NULL ) {
DEBUG ( 0 , ( " failure allocating memory for cliplist \n " ) ) ;
error = 1 ;
} else {
cliplist [ clipn ] = NULL ;
p = inclusion_buffer ;
for ( i = 0 ; ( ! error ) & & ( i < clipn ) ; i + + ) {
/* set current item to NULL so array will be null-terminated even if
* malloc fails below . */
cliplist [ i ] = NULL ;
if ( ( tmpstr = ( char * ) malloc ( strlen ( p ) + 1 ) ) = = NULL ) {
DEBUG ( 0 , ( " Could not allocate space for a cliplist item, # %i \n " , i ) ) ;
error = 1 ;
} else {
unfixtarname ( tmpstr , p , strlen ( p ) + 1 , True ) ;
cliplist [ i ] = tmpstr ;
2001-07-04 11:36:09 +04:00
if ( ( p = strchr_m ( p , ' \000 ' ) ) = = NULL ) {
1998-06-17 05:52:57 +04:00
DEBUG ( 0 , ( " INTERNAL ERROR: inclusion_buffer is of unexpected contents. \n " ) ) ;
abort ( ) ;
}
}
+ + p ;
}
must_free_cliplist = True ;
}
}
if ( inclusion_buffer ) {
free ( inclusion_buffer ) ;
}
if ( error ) {
if ( cliplist ) {
1998-06-20 14:06:26 +04:00
char * * pp ;
1998-06-17 05:52:57 +04:00
/* We know cliplist is always null-terminated */
1998-06-20 14:06:26 +04:00
for ( pp = cliplist ; * pp ; + + pp ) {
free ( * pp ) ;
1998-06-17 05:52:57 +04:00
}
free ( cliplist ) ;
cliplist = NULL ;
must_free_cliplist = False ;
}
return 0 ;
}
/* cliplist and its elements are freed at the end of process_tar. */
return 1 ;
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
Parse tar arguments . Sets tar_type , tar_excl , etc .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int tar_parseargs ( int argc , char * argv [ ] , char * Optarg , int Optind )
{
char tar_clipfl = ' \0 ' ;
/* Reset back to defaults - could be from interactive version
* reset mode and archive mode left as they are though
*/
tar_type = ' \0 ' ;
tar_excl = True ;
1998-09-21 12:45:11 +04:00
dry_run = False ;
1996-05-04 11:50:46 +04:00
while ( * Optarg )
switch ( * Optarg + + ) {
case ' c ' :
tar_type = ' c ' ;
break ;
case ' x ' :
if ( tar_type = = ' c ' ) {
printf ( " Tar must be followed by only one of c or x. \n " ) ;
return 0 ;
}
tar_type = ' x ' ;
break ;
case ' b ' :
if ( Optind > = argc | | ! ( blocksize = atoi ( argv [ Optind ] ) ) ) {
DEBUG ( 0 , ( " Option b must be followed by valid blocksize \n " ) ) ;
return 0 ;
} else {
Optind + + ;
}
break ;
case ' g ' :
tar_inc = True ;
break ;
case ' N ' :
if ( Optind > = argc ) {
DEBUG ( 0 , ( " Option N must be followed by valid file name \n " ) ) ;
return 0 ;
} else {
1998-09-02 00:11:54 +04:00
SMB_STRUCT_STAT stbuf ;
1996-05-04 11:50:46 +04:00
extern time_t newer_than ;
2001-07-04 11:15:53 +04:00
if ( sys_stat ( argv [ Optind ] , & stbuf ) = = 0 ) {
1996-05-04 11:50:46 +04:00
newer_than = stbuf . st_mtime ;
DEBUG ( 1 , ( " Getting files newer than %s " ,
1996-05-31 19:13:29 +04:00
asctime ( LocalTime ( & newer_than ) ) ) ) ;
1996-05-04 11:50:46 +04:00
Optind + + ;
} else {
DEBUG ( 0 , ( " Error setting newer-than time \n " ) ) ;
return 0 ;
}
}
break ;
case ' a ' :
tar_reset = True ;
break ;
1998-10-04 11:09:38 +04:00
case ' q ' :
tar_noisy = False ;
break ;
1996-05-04 11:50:46 +04:00
case ' I ' :
if ( tar_clipfl ) {
1998-06-17 05:52:57 +04:00
DEBUG ( 0 , ( " Only one of I,X,F must be specified \n " ) ) ;
1996-05-04 11:50:46 +04:00
return 0 ;
}
tar_clipfl = ' I ' ;
break ;
case ' X ' :
if ( tar_clipfl ) {
1998-06-17 05:52:57 +04:00
DEBUG ( 0 , ( " Only one of I,X,F must be specified \n " ) ) ;
1996-05-04 11:50:46 +04:00
return 0 ;
}
tar_clipfl = ' X ' ;
break ;
1998-06-17 05:52:57 +04:00
case ' F ' :
if ( tar_clipfl ) {
DEBUG ( 0 , ( " Only one of I,X,F must be specified \n " ) ) ;
return 0 ;
}
tar_clipfl = ' F ' ;
break ;
1998-05-08 17:51:17 +04:00
case ' r ' :
DEBUG ( 0 , ( " tar_re_search set \n " ) ) ;
tar_re_search = True ;
break ;
1998-09-21 12:45:11 +04:00
case ' n ' :
if ( tar_type = = ' c ' ) {
DEBUG ( 0 , ( " dry_run set \n " ) ) ;
dry_run = True ;
} else {
DEBUG ( 0 , ( " n is only meaningful when creating a tar-file \n " ) ) ;
return 0 ;
}
break ;
1996-05-04 11:50:46 +04:00
default :
DEBUG ( 0 , ( " Unknown tar option \n " ) ) ;
return 0 ;
}
if ( ! tar_type ) {
printf ( " Option T must be followed by one of c or x. \n " ) ;
return 0 ;
}
1998-06-17 05:52:57 +04:00
/* tar_excl is true if cliplist lists files to be included.
* Both ' I ' and ' F ' mean include . */
1997-01-09 21:02:17 +03:00
tar_excl = tar_clipfl ! = ' X ' ;
1998-05-08 17:51:17 +04:00
1998-06-17 05:52:57 +04:00
if ( tar_clipfl = = ' F ' ) {
if ( argc - Optind - 1 ! = 1 ) {
DEBUG ( 0 , ( " Option F must be followed by exactly one filename. \n " ) ) ;
return 0 ;
}
if ( ! read_inclusion_file ( argv [ Optind + 1 ] ) ) {
return 0 ;
}
} else if ( Optind + 1 < argc & & ! tar_re_search ) { /* For backwards compatibility */
1998-04-11 11:52:13 +04:00
char * tmpstr ;
char * * tmplist ;
int clipcount ;
1997-01-09 21:02:17 +03:00
cliplist = argv + Optind + 1 ;
clipn = argc - Optind - 1 ;
1998-04-11 11:52:13 +04:00
clipcount = clipn ;
if ( ( tmplist = malloc ( clipn * sizeof ( char * ) ) ) = = NULL ) {
DEBUG ( 0 , ( " Could not allocate space to process cliplist, count = %i \n " ,
clipn )
) ;
return 0 ;
}
for ( clipcount = 0 ; clipcount < clipn ; clipcount + + ) {
DEBUG ( 5 , ( " Processing an item, %s \n " , cliplist [ clipcount ] ) ) ;
if ( ( tmpstr = ( char * ) malloc ( strlen ( cliplist [ clipcount ] ) + 1 ) ) = = NULL ) {
DEBUG ( 0 , ( " Could not allocate space for a cliplist item, # %i \n " ,
clipcount )
) ;
return 0 ;
}
1998-06-17 05:52:57 +04:00
unfixtarname ( tmpstr , cliplist [ clipcount ] , strlen ( cliplist [ clipcount ] ) + 1 , True ) ;
1998-04-11 11:52:13 +04:00
tmplist [ clipcount ] = tmpstr ;
DEBUG ( 5 , ( " Processed an item, %s \n " , tmpstr ) ) ;
DEBUG ( 5 , ( " Cliplist is: %s \n " , cliplist [ 0 ] ) ) ;
}
cliplist = tmplist ;
1998-06-17 05:52:57 +04:00
must_free_cliplist = True ;
1997-01-09 21:02:17 +03:00
}
1998-05-08 17:51:17 +04:00
if ( Optind + 1 < argc & & tar_re_search ) { /* Doing regular expression seaches */
# ifdef HAVE_REGEX_H
int errcode ;
if ( ( preg = ( regex_t * ) malloc ( 65536 ) ) = = NULL ) {
DEBUG ( 0 , ( " Could not allocate buffer for regular expression search \n " ) ) ;
return ;
}
if ( errcode = regcomp ( preg , argv [ Optind + 1 ] , REG_EXTENDED ) ) {
char errstr [ 1024 ] ;
size_t errlen ;
errlen = regerror ( errcode , preg , errstr , sizeof ( errstr ) - 1 ) ;
DEBUG ( 0 , ( " Could not compile pattern buffer for re search: %s \n %s \n " , argv [ Optind + 1 ] , errstr ) ) ;
return ;
}
# endif
clipn = argc - Optind - 1 ;
cliplist = argv + Optind + 1 ;
}
1996-05-04 11:50:46 +04:00
if ( Optind > = argc | | ! strcmp ( argv [ Optind ] , " - " ) ) {
/* Sets tar handle to either 0 or 1, as appropriate */
tarhandle = ( tar_type = = ' c ' ) ;
1999-01-17 04:03:52 +03:00
/*
* Make sure that dbf points to stderr if we are using stdout for
* tar output
*/
if ( tarhandle = = 1 )
2001-09-10 15:08:57 +04:00
dbf = x_stderr ;
1996-05-04 11:50:46 +04:00
} else {
1998-09-21 12:45:11 +04:00
if ( tar_type = = ' c ' & & ( dry_run | | strcmp ( argv [ Optind ] , " /dev/null " ) = = 0 ) )
{
if ( ! dry_run ) {
DEBUG ( 0 , ( " Output is /dev/null, assuming dry_run " ) ) ;
dry_run = True ;
}
tarhandle = - 1 ;
} else
1998-11-17 23:50:07 +03:00
if ( ( tar_type = = ' x ' & & ( tarhandle = sys_open ( argv [ Optind ] , O_RDONLY , 0 ) ) = = - 1 )
| | ( tar_type = = ' c ' & & ( tarhandle = sys_creat ( argv [ Optind ] , 0644 ) ) < 0 ) )
1996-05-04 11:50:46 +04:00
{
1997-09-26 13:06:51 +04:00
DEBUG ( 0 , ( " Error opening local file %s - %s \n " ,
argv [ Optind ] , strerror ( errno ) ) ) ;
1996-05-04 11:50:46 +04:00
return ( 0 ) ;
}
}
return 1 ;
}