2007-12-07 22:28:16 +03:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2013-07-03 18:47:05 +04:00
Tar backup command extension
Copyright ( C ) Aurélien Aptel 2013
2007-12-07 22:28:16 +03:00
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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
1996-05-04 11:50:46 +04:00
( at your option ) any later version .
2007-12-07 22:28:16 +03:00
1996-05-04 11:50:46 +04:00
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 .
2007-12-07 22:28:16 +03:00
1996-05-04 11:50:46 +04:00
You should have received a copy of the GNU General Public License
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
1996-05-04 11:50:46 +04:00
*/
# include "includes.h"
2011-02-26 01:20:06 +03:00
# include "system/filesys.h"
2004-10-07 08:01:18 +04:00
# include "client/client_proto.h"
2013-07-08 20:09:47 +04:00
# include "client/clitar_proto.h"
2011-05-06 13:47:43 +04:00
# include "libsmb/libsmb.h"
2013-07-05 11:51:43 +04:00
# include <archive.h>
2013-07-09 20:01:47 +04:00
# include <archive_entry.h>
2013-07-05 11:51:43 +04:00
# define LEN(x) (sizeof(x) / sizeof((x)[0]))
2013-07-11 20:12:38 +04:00
# define DBG(a, b) (DEBUG(a, ("tar:%-3d ", __LINE__)), DEBUG(a, b))
2013-07-09 20:01:47 +04:00
/**
* Maximum value for the blocksize field
*/
# define TAR_MAX_BLOCK_SIZE 0xffff
/**
2013-07-10 01:07:06 +04:00
* Default tar block size in bytes . Hasn ' t changed since the first
* commit in 1996. . .
*
2013-07-09 20:01:47 +04:00
* A more adequate size will be used for better performance unless
2013-07-10 01:07:06 +04:00
* we ' re dealing with a tape device with a fixed read / write block
* size .
2013-07-09 20:01:47 +04:00
*
* The actual choice is made by libarchive .
*/
# define TAR_DEFAULT_BLOCK_SIZE 20
1998-05-08 17:51:17 +04:00
2013-07-11 02:57:40 +04:00
# define TAR_CLI_READ_SIZE 0xff00
2013-07-16 18:32:29 +04:00
# define TAR_DO_LIST_ATTR (FILE_ATTRIBUTE_DIRECTORY \
| FILE_ATTRIBUTE_SYSTEM \
| FILE_ATTRIBUTE_HIDDEN )
2013-07-11 02:57:40 +04:00
2013-07-11 17:56:03 +04:00
2013-07-05 20:14:50 +04:00
enum tar_operation {
TAR_NO_OPERATION ,
TAR_CREATE , /* c flag */
TAR_EXTRACT , /* x flag */
} ;
enum tar_selection {
TAR_NO_SELECTION ,
2013-07-16 18:22:13 +04:00
TAR_INCLUDE , /* I and F flag, default */
2013-07-05 20:14:50 +04:00
TAR_EXCLUDE , /* X flag */
2013-07-03 20:18:25 +04:00
} ;
2013-07-05 11:51:43 +04:00
enum {
ATTR_UNSET ,
ATTR_SET ,
} ;
struct tar {
2013-07-09 20:01:47 +04:00
/* in state that needs/can be processed? */
2013-07-09 17:10:44 +04:00
bool to_process ;
2013-07-03 20:18:25 +04:00
/* flags */
2013-07-05 20:14:50 +04:00
struct tar_mode {
enum tar_operation operation ; /* create, extract */
enum tar_selection selection ; /* inc, inc from file, exclude */
int blocksize ; /* size in bytes of a block in the tar file */
2013-07-03 20:18:25 +04:00
bool hidden ; /* backup hidden file? */
bool system ; /* backup system file? */
bool incremental ; /* backup _only_ archived file? */
bool reset ; /* unset archive bit? */
bool dry ; /* don't write tar file? */
2013-07-05 20:14:50 +04:00
bool regex ; /* XXX: never actually using regex... */
2013-07-05 11:51:43 +04:00
bool verbose ;
2013-07-03 20:18:25 +04:00
} mode ;
2013-07-11 20:17:25 +04:00
/* nb of bytes received */
uint64_t total_size ;
2013-07-03 20:18:25 +04:00
/* path to tar archive name */
char * tar_path ;
2013-07-08 20:09:47 +04:00
/* list of path to include or exclude */
char * * path_list ;
2013-07-10 01:17:46 +04:00
int path_list_size ;
1998-09-18 16:47:46 +04:00
2013-07-05 11:51:43 +04:00
/* archive handle */
struct archive * archive ;
} ;
2013-07-03 20:18:25 +04:00
2013-07-08 20:09:47 +04:00
struct tar tar_ctx = {
2013-07-05 20:14:50 +04:00
. mode . selection = TAR_INCLUDE ,
2013-07-09 20:01:47 +04:00
. mode . blocksize = TAR_DEFAULT_BLOCK_SIZE ,
2013-07-09 15:09:56 +04:00
. mode . hidden = true ,
. mode . system = true ,
. mode . incremental = false ,
. mode . dry = false ,
2013-07-05 11:51:43 +04:00
} ;
2013-07-03 20:18:25 +04:00
2013-07-09 16:27:55 +04:00
static char * fix_unix_path ( char * path , bool removeprefix )
{
char * from = path , * to = path ;
if ( ! path | | ! * path )
return path ;
/* remove prefix:
* . / path = > path
* / path = > path
*/
if ( removeprefix ) {
/* /path */
if ( path [ 0 ] = = ' / ' | | path [ 0 ] = = ' \\ ' ) {
from + = 1 ;
}
/* ./path */
if ( path [ 1 ] & & path [ 0 ] = = ' . ' & & ( path [ 1 ] = = ' / ' | | path [ 1 ] = = ' \\ ' ) ) {
from + = 2 ;
}
}
2013-07-10 01:44:16 +04:00
/* replace / with \ */
2013-07-09 16:27:55 +04:00
while ( * from ) {
2013-07-10 01:44:16 +04:00
if ( * from = = ' / ' ) {
* to = ' \\ ' ;
2013-07-09 16:27:55 +04:00
} else {
* to = * from ;
}
from + + ; to + + ;
}
* to = 0 ;
return path ;
}
2013-07-09 13:41:06 +04:00
# define XSET(v) [v] = #v
2013-07-11 17:56:03 +04:00
# define XTABLE(v, t) DBG(2, ("DUMP:%-20.20s = %s\n", #v, t[v]))
# define XBOOL(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v ? 1 : 0))
# define XSTR(v) DBG(2, ("DUMP:%-20.20s = %s\n", #v, v ? v : "NULL"))
# define XINT(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v))
2013-07-11 20:17:25 +04:00
# define XUINT64(v) DBG(2, ("DUMP:%-20.20s = %" PRIu64 "\n", #v, v))
2013-07-09 13:41:06 +04:00
static void tar_dump ( struct tar * t )
{
int i ;
const char * op [ ] = {
XSET ( TAR_NO_OPERATION ) ,
XSET ( TAR_CREATE ) ,
XSET ( TAR_EXTRACT ) ,
} ;
const char * sel [ ] = {
XSET ( TAR_NO_SELECTION ) ,
XSET ( TAR_INCLUDE ) ,
XSET ( TAR_EXCLUDE ) ,
} ;
2013-07-09 17:10:44 +04:00
XBOOL ( t - > to_process ) ;
2013-07-09 13:41:06 +04:00
XTABLE ( t - > mode . operation , op ) ;
XTABLE ( t - > mode . selection , sel ) ;
XINT ( t - > mode . blocksize ) ;
XBOOL ( t - > mode . hidden ) ;
XBOOL ( t - > mode . system ) ;
XBOOL ( t - > mode . incremental ) ;
XBOOL ( t - > mode . reset ) ;
XBOOL ( t - > mode . dry ) ;
XBOOL ( t - > mode . verbose ) ;
2013-07-11 20:17:25 +04:00
XUINT64 ( t - > total_size ) ;
2013-07-09 13:41:06 +04:00
XSTR ( t - > tar_path ) ;
2013-07-10 01:17:46 +04:00
XINT ( t - > path_list_size ) ;
2013-07-09 13:41:06 +04:00
2013-07-16 18:32:29 +04:00
for ( i = 0 ; t - > path_list & & t - > path_list [ i ] ; i + + ) {
2013-07-11 17:56:03 +04:00
DBG ( 2 , ( " DUMP: t->path_list[%2d] = %s \n " , i , t - > path_list [ i ] ) ) ;
2013-07-09 13:41:06 +04:00
}
2013-07-11 17:56:03 +04:00
DBG ( 2 , ( " DUMP:t->path_list @ %p (%d elem) \n " , t - > path_list , i ) ) ;
2013-07-09 13:41:06 +04:00
}
# undef XSET
# undef XTABLE
# undef XBOOL
# undef XSTR
# undef XINT
2013-07-10 01:17:46 +04:00
static void tar_add_selection_path ( struct tar * t , const char * path )
{
TALLOC_CTX * ctx = talloc_tos ( ) ;
2013-07-16 18:32:29 +04:00
if ( ! t - > path_list ) {
2013-07-10 01:17:46 +04:00
t - > path_list = str_list_make_empty ( ctx ) ;
t - > path_list_size = 0 ;
}
t - > path_list = str_list_add ( ( const char * * ) t - > path_list , path ) ;
t - > path_list_size + + ;
fix_unix_path ( t - > path_list [ t - > path_list_size - 1 ] , true ) ;
}
2013-07-05 20:14:50 +04:00
static int tar_set_blocksize ( struct tar * t , int size )
{
if ( size < = 0 | | size > TAR_MAX_BLOCK_SIZE ) {
return 0 ;
}
t - > mode . blocksize = size ;
return 1 ;
}
2013-07-09 13:41:06 +04:00
static bool tar_set_newer_than ( struct tar * t , const char * filename )
2013-07-05 20:14:50 +04:00
{
extern time_t newer_than ;
SMB_STRUCT_STAT stbuf ;
if ( sys_stat ( filename , & stbuf , false ) ! = 0 ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Error setting newer-than time \n " ) ) ;
2013-07-05 20:14:50 +04:00
return 0 ;
}
newer_than = convert_timespec_to_time_t ( stbuf . st_ex_mtime ) ;
2013-07-11 17:56:03 +04:00
DBG ( 1 , ( " Getting files newer than %s \n " , time_to_asc ( newer_than ) ) ) ;
2013-07-05 20:14:50 +04:00
return 1 ;
}
1996-05-04 11:50:46 +04:00
2013-07-08 20:09:47 +04:00
static bool tar_read_inclusion_file ( struct tar * t , const char * filename )
{
char * line ;
TALLOC_CTX * ctx = talloc_tos ( ) ;
int fd = open ( filename , O_RDONLY ) ;
if ( fd < 0 ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Can't open inclusion file '%s': %s \n " , filename , strerror ( errno ) ) ) ;
2013-07-08 20:09:47 +04:00
return 0 ;
}
while ( ( line = afdgets ( fd , ctx , 0 ) ) ) {
2013-07-10 01:17:46 +04:00
tar_add_selection_path ( t , line ) ;
2013-07-08 20:09:47 +04:00
}
close ( fd ) ;
return 1 ;
}
2013-07-11 17:56:03 +04:00
/* skip leading slashes or dots */
static const char * skip_useless_char_in_path ( const char * p )
{
while ( p ) {
if ( * p = = ' / ' | | * p = = ' \\ ' ) {
p + + ;
}
else if ( p [ 0 ] = = ' . ' & & ( p [ 1 ] = = ' / ' | | p [ 1 ] = = ' \\ ' ) ) {
p + = 2 ;
}
else
return p ;
}
return p ;
}
/**
* return true if the path @ sub is a subpath of @ full .
*
* case - insensitive , true if @ sub = @ full
*/
static bool is_subpath ( const char * sub , const char * full )
{
const char * full_copy = full ;
2013-07-16 18:32:29 +04:00
while ( * full & & * sub & &
( * full = = * sub | | tolower_m ( * full ) = = tolower_m ( * sub ) | |
( * full = = ' \\ ' & & * sub = = ' / ' ) | | ( * full = = ' / ' & & * sub = = ' \\ ' ) ) ) {
2013-07-11 17:56:03 +04:00
full + + ; sub + + ;
}
/* if full has a trailing slash, it compared equal, so full is an "initial"
string of sub .
*/
if ( ! * full & & full ! = full_copy & & ( * ( full - 1 ) = = ' / ' | | * ( full - 1 ) = = ' \\ ' ) )
return true ;
/* ignore trailing slash on full */
if ( ! * sub & & ( * full = = ' / ' | | * full = = ' \\ ' ) & & ! * ( full + 1 ) )
return true ;
/* check for full is an "initial" string of sub */
if ( ( * sub = = ' / ' | | * sub = = ' \\ ' ) & & ! * full )
return true ;
return * full = = * sub ;
}
2013-07-16 17:49:27 +04:00
static bool tar_path_in_list ( struct tar * t , const char * path , bool reverse )
2013-07-11 17:56:03 +04:00
{
int i ;
const char * p = path ;
const char * pattern ;
2013-07-16 17:49:27 +04:00
bool res ;
2013-07-11 17:56:03 +04:00
if ( ! p | | ! p [ 0 ] )
return false ;
p = skip_useless_char_in_path ( p ) ;
for ( i = 0 ; i < t - > path_list_size ; i + + ) {
pattern = skip_useless_char_in_path ( t - > path_list [ i ] ) ;
2013-07-16 17:49:27 +04:00
res = is_subpath ( p , pattern ) ;
if ( reverse ) {
res = res | | is_subpath ( pattern , p ) ;
}
if ( res ) {
2013-07-11 17:56:03 +04:00
return true ;
}
}
return false ;
}
2013-07-15 20:58:36 +04:00
static bool tar_extract_skip_path ( struct tar * t ,
struct archive_entry * entry )
{
const bool skip = true ;
const char * fullpath = archive_entry_pathname ( entry ) ;
bool in ;
2013-07-16 17:49:27 +04:00
in = t - > path_list_size > 0 ? tar_path_in_list ( t , fullpath , false ) : true ;
2013-07-15 20:58:36 +04:00
if ( t - > mode . selection = = TAR_EXCLUDE ) {
in = ! in ;
}
return in ? ! skip : skip ;
}
static bool tar_create_skip_path ( struct tar * t ,
const char * fullpath ,
const struct file_info * finfo )
2013-07-11 17:56:03 +04:00
{
/* syntaxic sugar */
const bool skip = true ;
2013-07-16 17:49:27 +04:00
const mode_t mode = finfo - > mode ;
const bool isdir = mode & FILE_ATTRIBUTE_DIRECTORY ;
2013-07-16 18:16:29 +04:00
const bool exclude = t - > mode . selection = = TAR_EXCLUDE ;
bool in = true ;
2013-07-11 17:56:03 +04:00
2013-07-16 17:49:27 +04:00
if ( ! isdir ) {
2013-07-11 17:56:03 +04:00
2013-07-16 17:49:27 +04:00
/* 1. if we dont want X and we have X, skip */
if ( ! t - > mode . system & & ( mode & FILE_ATTRIBUTE_SYSTEM ) ) {
return skip ;
}
2013-07-11 17:56:03 +04:00
2013-07-16 17:49:27 +04:00
if ( ! t - > mode . hidden & & ( mode & FILE_ATTRIBUTE_HIDDEN ) ) {
return skip ;
}
2013-07-11 17:56:03 +04:00
2013-07-16 17:49:27 +04:00
/* 2. if we only want archive and it's not, skip */
2013-07-11 17:56:03 +04:00
2013-07-16 17:49:27 +04:00
if ( t - > mode . incremental & & ! ( mode & FILE_ATTRIBUTE_ARCHIVE ) ) {
return skip ;
}
2013-07-11 17:56:03 +04:00
}
/* 3. is it in the selection list? */
2013-07-16 18:16:29 +04:00
if ( t - > path_list_size > 0 ) {
in = tar_path_in_list ( t , fullpath , isdir & & ! exclude ) ;
}
2013-07-11 17:56:03 +04:00
/* inverse result if in exclude mode */
2013-07-16 18:16:29 +04:00
if ( exclude ) {
2013-07-11 17:56:03 +04:00
in = ! in ;
}
return in ? ! skip : skip ;
}
2013-07-09 17:10:44 +04:00
bool tar_to_process ( struct tar * t )
{
return t - > to_process ;
}
2013-07-05 13:33:55 +04:00
/**
* cmd_block - interactive command to change tar blocksize
*
* Read a size from the client command line and update the current
* blocksize .
*/
2001-10-09 23:12:18 +04:00
int cmd_block ( void )
1996-05-04 11:50:46 +04:00
{
2013-07-03 20:18:25 +04:00
/* XXX: from client.c */
const extern char * cmd_ptr ;
char * buf ;
2013-07-05 11:51:43 +04:00
TALLOC_CTX * ctx = talloc_tos ( ) ;
2013-07-03 20:18:25 +04:00
2013-07-05 11:51:43 +04:00
if ( ! next_token_talloc ( ctx , & cmd_ptr , & buf , NULL ) ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " blocksize <n> \n " ) ) ;
2013-07-05 11:51:43 +04:00
return 1 ;
}
2013-07-03 20:18:25 +04:00
2013-07-16 18:32:29 +04:00
if ( ! tar_set_blocksize ( & tar_ctx , atoi ( buf ) ) ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " invalid blocksize \n " ) ) ;
2013-07-05 11:51:43 +04:00
}
2013-07-03 20:18:25 +04:00
2013-07-11 17:56:03 +04:00
DBG ( 2 , ( " blocksize is now %d \n " , tar_ctx . mode . blocksize ) ) ;
2013-07-03 20:18:25 +04:00
2013-07-05 11:51:43 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
2013-07-05 13:33:55 +04:00
/**
* cmd_tarmode - interactive command to change tar behaviour
*
* Read one or more modes from the client command line and update the
* current tar mode .
*/
2001-10-09 23:12:18 +04:00
int cmd_tarmode ( void )
1996-05-04 11:50:46 +04:00
{
2013-07-05 11:51:43 +04:00
const extern char * cmd_ptr ;
char * buf ;
int i ;
TALLOC_CTX * ctx = talloc_tos ( ) ;
struct {
const char * cmd ;
bool * p ;
bool value ;
} table [ ] = {
2013-07-09 15:09:56 +04:00
{ " full " , & tar_ctx . mode . incremental , false } ,
{ " inc " , & tar_ctx . mode . incremental , true } ,
{ " reset " , & tar_ctx . mode . reset , true } ,
{ " noreset " , & tar_ctx . mode . reset , false } ,
{ " system " , & tar_ctx . mode . system , true } ,
{ " nosystem " , & tar_ctx . mode . system , false } ,
{ " hidden " , & tar_ctx . mode . hidden , true } ,
{ " nohidden " , & tar_ctx . mode . hidden , false } ,
{ " verbose " , & tar_ctx . mode . verbose , true } ,
{ " noquiet " , & tar_ctx . mode . verbose , true } ,
{ " quiet " , & tar_ctx . mode . verbose , false } ,
{ " noverbose " , & tar_ctx . mode . verbose , false } ,
2013-07-05 11:51:43 +04:00
} ;
while ( next_token_talloc ( ctx , & cmd_ptr , & buf , NULL ) ) {
for ( i = 0 ; i < LEN ( table ) ; i + + ) {
if ( strequal ( table [ i ] . cmd , buf ) ) {
* table [ i ] . p = table [ i ] . value ;
break ;
}
}
if ( i = = LEN ( table ) )
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " tarmode: unrecognised option %s \n " , buf ) ) ;
2013-07-05 11:51:43 +04:00
TALLOC_FREE ( buf ) ;
}
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " tarmode is now %s, %s, %s, %s, %s \n " ,
2013-07-05 11:51:43 +04:00
tar_ctx . mode . incremental ? " incremental " : " full " ,
tar_ctx . mode . system ? " system " : " nosystem " ,
tar_ctx . mode . hidden ? " hidden " : " nohidden " ,
tar_ctx . mode . reset ? " reset " : " noreset " ,
tar_ctx . mode . verbose ? " verbose " : " quiet " ) ) ;
2013-07-03 20:18:25 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
2013-07-05 13:33:55 +04:00
/**
* set_remote_attr - set DOS attributes of a remote file
* @ filename : path to the file name
* @ new_attr : attribute bit mask to use
* @ mode : one of ATTR_SET or ATTR_UNSET
*
* Update the file attributes with the one provided .
*/
2013-07-16 16:47:29 +04:00
static void set_remote_attr ( const char * filename , uint16 new_attr , int mode )
2013-07-05 11:51:43 +04:00
{
extern struct cli_state * cli ;
uint16 old_attr ;
NTSTATUS status ;
if ( ! NT_STATUS_IS_OK ( cli_getatr ( cli , filename , & old_attr , NULL , NULL ) ) ) {
/* XXX: debug message */
return ;
}
if ( mode = = ATTR_SET ) {
new_attr | = old_attr ;
} else {
new_attr = old_attr & ~ new_attr ;
}
status = cli_setatr ( cli , filename , new_attr , 0 ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2013-07-11 17:56:03 +04:00
DBG ( 1 , ( " setatr failed: %s \n " , nt_errstr ( status ) ) ) ;
2013-07-05 11:51:43 +04:00
}
}
2013-07-05 13:33:55 +04:00
/**
* cmd_setmode - interactive command to set DOS attributes
*
* Read a filename and mode from the client command line and update
* the file DOS attributes .
*/
2001-10-09 23:12:18 +04:00
int cmd_setmode ( void )
1996-05-04 11:50:46 +04:00
{
2013-07-05 11:51:43 +04:00
const extern char * cmd_ptr ;
char * buf ;
char * fname = NULL ;
uint16 attr [ 2 ] = { 0 } ;
int mode = ATTR_SET ;
TALLOC_CTX * ctx = talloc_tos ( ) ;
if ( ! next_token_talloc ( ctx , & cmd_ptr , & buf , NULL ) ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " setmode <filename> <[+|-]rsha> \n " ) ) ;
2013-07-05 11:51:43 +04:00
return 1 ;
}
fname = talloc_asprintf ( ctx ,
" %s%s " ,
client_get_cur_dir ( ) ,
buf ) ;
if ( ! fname ) {
return 1 ;
}
while ( next_token_talloc ( ctx , & cmd_ptr , & buf , NULL ) ) {
const char * s = buf ;
2013-07-16 18:32:29 +04:00
while ( * s ) {
2013-07-05 11:51:43 +04:00
switch ( * s + + ) {
case ' + ' :
mode = ATTR_SET ;
break ;
case ' - ' :
mode = ATTR_UNSET ;
break ;
case ' r ' :
attr [ mode ] | = FILE_ATTRIBUTE_READONLY ;
break ;
case ' h ' :
attr [ mode ] | = FILE_ATTRIBUTE_HIDDEN ;
break ;
case ' s ' :
attr [ mode ] | = FILE_ATTRIBUTE_SYSTEM ;
break ;
case ' a ' :
attr [ mode ] | = FILE_ATTRIBUTE_ARCHIVE ;
break ;
default :
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " setmode <filename> <perm=[+|-]rsha> \n " ) ) ;
2013-07-05 11:51:43 +04:00
return 1 ;
}
}
}
if ( attr [ ATTR_SET ] = = 0 & & attr [ ATTR_UNSET ] = = 0 ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " setmode <filename> <[+|-]rsha> \n " ) ) ;
2013-07-05 11:51:43 +04:00
return 1 ;
}
2013-07-11 17:56:03 +04:00
DBG ( 2 , ( " \n perm set %d %d \n " , attr [ ATTR_SET ] , attr [ ATTR_UNSET ] ) ) ;
2013-07-05 11:51:43 +04:00
set_remote_attr ( fname , attr [ ATTR_SET ] , ATTR_SET ) ;
set_remote_attr ( fname , attr [ ATTR_UNSET ] , ATTR_UNSET ) ;
2013-07-03 20:18:25 +04:00
return 0 ;
1996-05-04 11:50:46 +04:00
}
2013-07-10 15:16:08 +04:00
static int make_remote_path ( const char * full_path )
{
extern struct cli_state * cli ;
TALLOC_CTX * ctx = talloc_tos ( ) ;
char * path ;
char * subpath ;
char * state ;
char * last_backslash ;
char * p ;
int len ;
NTSTATUS status ;
2013-07-10 16:12:25 +04:00
int err = 0 ;
2013-07-10 15:16:08 +04:00
subpath = talloc_strdup ( ctx , full_path ) ;
path = talloc_strdup ( ctx , full_path ) ;
len = talloc_get_size ( path ) - 1 ;
last_backslash = strrchr_m ( path , ' \\ ' ) ;
if ( ! last_backslash ) {
2013-07-10 16:12:25 +04:00
goto out ;
2013-07-10 15:16:08 +04:00
}
* last_backslash = 0 ;
subpath [ 0 ] = 0 ;
p = strtok_r ( path , " \\ " , & state ) ;
while ( p ) {
strlcat ( subpath , p , len ) ;
status = cli_chkpath ( cli , subpath ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
status = cli_mkdir ( cli , subpath ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2013-07-16 18:32:29 +04:00
DBG ( 0 , ( " Can't mkdir %s: %s \n " , subpath , nt_errstr ( status ) ) ) ;
2013-07-10 16:12:25 +04:00
err = 1 ;
goto out ;
2013-07-10 15:16:08 +04:00
}
2013-07-11 17:56:03 +04:00
DBG ( 3 , ( " mkdir %s \n " , subpath ) ) ;
2013-07-10 15:16:08 +04:00
}
strlcat ( subpath , " \\ " , len ) ;
p = strtok_r ( NULL , " / \\ " , & state ) ;
}
2013-07-10 16:12:25 +04:00
out :
return err ;
2013-07-10 15:16:08 +04:00
}
static int tar_send_file ( struct tar * t , struct archive_entry * entry )
{
extern struct cli_state * cli ;
TALLOC_CTX * ctx = talloc_tos ( ) ;
char * dos_path ;
char * full_path ;
NTSTATUS status ;
uint16_t remote_fd = ( uint16_t ) - 1 ;
int err = 0 ;
2013-07-10 16:12:25 +04:00
int flags = O_RDWR | O_CREAT | O_TRUNC ;
mode_t mode = archive_entry_filetype ( entry ) ;
2013-07-10 15:16:08 +04:00
dos_path = talloc_strdup ( ctx , archive_entry_pathname ( entry ) ) ;
fix_unix_path ( dos_path , true ) ;
full_path = talloc_strdup ( ctx , client_get_cur_dir ( ) ) ;
full_path = talloc_strdup_append ( full_path , dos_path ) ;
2013-07-10 16:12:25 +04:00
if ( mode ! = AE_IFREG & & mode ! = AE_IFDIR ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Skipping non-dir & non-regular file %s \n " , full_path ) ) ;
2013-07-10 16:12:25 +04:00
goto out ;
}
2013-07-10 15:16:08 +04:00
2013-07-10 16:12:25 +04:00
if ( make_remote_path ( full_path ) ) {
err = 1 ;
goto out ;
}
if ( mode = = AE_IFDIR ) {
goto out ;
}
status = cli_open ( cli , full_path , flags , DENY_NONE , & remote_fd ) ;
2013-07-11 02:57:40 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Error opening remote file %s: %s \n " ,
2013-07-10 16:12:25 +04:00
full_path , nt_errstr ( status ) ) ) ;
2013-07-11 02:57:40 +04:00
err = 1 ;
2013-07-10 16:12:25 +04:00
goto out ;
2013-07-11 02:57:40 +04:00
}
2013-07-10 15:16:08 +04:00
for ( ; ; ) {
2013-07-10 16:12:25 +04:00
const void * buf ;
size_t len ;
off_t off ;
int r ;
2013-07-10 15:16:08 +04:00
r = archive_read_data_block ( t - > archive , & buf , & len , & off ) ;
if ( r = = ARCHIVE_EOF ) {
break ;
}
if ( r = = ARCHIVE_WARN ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Warning: %s \n " , archive_error_string ( t - > archive ) ) ) ;
2013-07-10 15:16:08 +04:00
}
if ( r = = ARCHIVE_FATAL ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Fatal: %s \n " , archive_error_string ( t - > archive ) ) ) ;
2013-07-10 15:16:08 +04:00
err = 1 ;
2013-07-10 16:12:25 +04:00
goto close_out ;
2013-07-10 15:16:08 +04:00
}
status = cli_writeall ( cli , remote_fd , 0 , buf , off , len , NULL ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Error writing remote file %s: %s \n " ,
2013-07-10 15:16:08 +04:00
full_path , nt_errstr ( status ) ) ) ;
2013-07-10 16:12:25 +04:00
err = 1 ;
goto close_out ;
2013-07-10 15:16:08 +04:00
}
}
2013-07-10 16:12:25 +04:00
close_out :
2013-07-10 15:16:08 +04:00
status = cli_close ( cli , remote_fd ) ;
2013-07-11 02:57:40 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Error losing remote file %s: %s \n " ,
2013-07-10 15:16:08 +04:00
full_path , nt_errstr ( status ) ) ) ;
err = 1 ;
2013-07-11 02:57:40 +04:00
}
out :
return err ;
}
static int tar_get_file ( struct tar * t , const char * full_dos_path ,
struct file_info * finfo )
{
extern struct cli_state * cli ;
TALLOC_CTX * ctx = talloc_tos ( ) ;
NTSTATUS status ;
struct archive_entry * entry ;
char * full_unix_path ;
char buf [ TAR_CLI_READ_SIZE ] ;
size_t len ;
uint64_t off = 0 ;
uint16_t remote_fd = ( uint16_t ) - 1 ;
int err = 0 , r ;
2013-07-11 20:17:25 +04:00
const bool isdir = finfo - > mode & FILE_ATTRIBUTE_DIRECTORY ;
2013-07-11 02:57:40 +04:00
2013-07-11 17:56:03 +04:00
DBG ( 5 , ( " +++ %s \n " , full_dos_path ) ) ;
2013-07-11 20:17:25 +04:00
t - > total_size + = finfo - > size ;
if ( t - > mode . dry ) {
goto out ;
}
2013-07-11 02:57:40 +04:00
2013-07-16 16:47:29 +04:00
if ( t - > mode . reset ) {
set_remote_attr ( full_dos_path , FILE_ATTRIBUTE_ARCHIVE , ATTR_UNSET ) ;
}
2013-07-11 02:57:40 +04:00
full_unix_path = talloc_asprintf ( ctx , " .%s " , full_dos_path ) ;
string_replace ( full_unix_path , ' \\ ' , ' / ' ) ;
entry = archive_entry_new ( ) ;
archive_entry_copy_pathname ( entry , full_unix_path ) ;
2013-07-11 20:17:25 +04:00
archive_entry_set_filetype ( entry , isdir ? AE_IFDIR : AE_IFREG ) ;
archive_entry_set_atime ( entry ,
finfo - > atime_ts . tv_sec ,
finfo - > atime_ts . tv_nsec ) ;
archive_entry_set_mtime ( entry ,
finfo - > mtime_ts . tv_sec ,
finfo - > mtime_ts . tv_nsec ) ;
archive_entry_set_ctime ( entry ,
finfo - > ctime_ts . tv_sec ,
finfo - > ctime_ts . tv_nsec ) ;
archive_entry_set_perm ( entry , isdir ? 0755 : 0644 ) ;
/*
* check if we can safely cast unsigned file size to libarchive
* signed size . Very unlikely problem ( > 9 exabyte file )
*/
if ( finfo - > size > INT64_MAX ) {
DBG ( 0 , ( " Remote file %s too big \n " , full_dos_path ) ) ;
goto out_entry ;
}
archive_entry_set_size ( entry , ( int64_t ) finfo - > size ) ;
2013-07-11 02:57:40 +04:00
r = archive_write_header ( t - > archive , entry ) ;
if ( r ! = ARCHIVE_OK ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Fatal: %s \n " , archive_error_string ( t - > archive ) ) ) ;
2013-07-11 02:57:40 +04:00
err = 1 ;
goto out_entry ;
}
if ( isdir ) {
2013-07-11 20:17:25 +04:00
DBG ( 5 , ( " get_file skip dir %s \n " , full_dos_path ) ) ;
2013-07-11 02:57:40 +04:00
goto out_entry ;
}
status = cli_open ( cli , full_dos_path , O_RDONLY , DENY_NONE , & remote_fd ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " %s opening remote file %s \n " ,
2013-07-11 02:57:40 +04:00
nt_errstr ( status ) , full_dos_path ) ) ;
goto out_entry ;
}
do {
status = cli_read ( cli , remote_fd , buf , off , sizeof ( buf ) , & len ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Error reading file %s : %s \n " ,
2013-07-11 02:57:40 +04:00
full_dos_path , nt_errstr ( status ) ) ) ;
err = 1 ;
goto out_close ;
}
off + = len ;
r = archive_write_data ( t - > archive , buf , len ) ;
2013-07-11 20:17:25 +04:00
if ( r < 0 ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Fatal: %s \n " , archive_error_string ( t - > archive ) ) ) ;
2013-07-11 02:57:40 +04:00
err = 1 ;
2013-07-11 20:17:25 +04:00
goto out_close ;
2013-07-11 02:57:40 +04:00
}
} while ( off < finfo - > size ) ;
out_close :
cli_close ( cli , remote_fd ) ;
out_entry :
archive_entry_free ( entry ) ;
2013-07-11 20:17:25 +04:00
out :
2013-07-11 02:57:40 +04:00
return err ;
}
static NTSTATUS get_file_callback ( struct cli_state * cli ,
struct file_info * finfo ,
const char * dir )
{
TALLOC_CTX * ctx = talloc_tos ( ) ;
NTSTATUS err = NT_STATUS_OK ;
char * remote_name ;
2013-07-16 18:32:29 +04:00
const char * initial_dir = client_get_cur_dir ( ) ;
2013-07-11 02:57:40 +04:00
2013-07-16 18:32:29 +04:00
remote_name = talloc_asprintf ( ctx , " %s%s " , initial_dir , finfo - > name ) ;
2013-07-11 02:57:40 +04:00
if ( strequal ( finfo - > name , " .. " ) | | strequal ( finfo - > name , " . " ) ) {
2013-07-11 17:56:03 +04:00
goto out ;
}
2013-07-15 20:58:36 +04:00
if ( tar_create_skip_path ( & tar_ctx , remote_name , finfo ) ) {
2013-07-11 17:56:03 +04:00
DBG ( 5 , ( " --- %s \n " , remote_name ) ) ;
2013-07-11 02:57:40 +04:00
goto out ;
}
if ( finfo - > mode & FILE_ATTRIBUTE_DIRECTORY ) {
char * old_dir ;
char * new_dir ;
char * mask ;
2013-07-16 18:32:29 +04:00
old_dir = talloc_strdup ( ctx , initial_dir ) ;
new_dir = talloc_asprintf ( ctx , " %s%s \\ " , initial_dir , finfo - > name ) ;
2013-07-11 02:57:40 +04:00
mask = talloc_asprintf ( ctx , " %s* " , new_dir ) ;
if ( tar_get_file ( & tar_ctx , remote_name , finfo ) ) {
err = NT_STATUS_UNSUCCESSFUL ;
goto out ;
}
client_set_cur_dir ( new_dir ) ;
2013-07-16 18:32:29 +04:00
do_list ( mask , TAR_DO_LIST_ATTR , get_file_callback , false , true ) ;
2013-07-11 02:57:40 +04:00
client_set_cur_dir ( old_dir ) ;
}
else {
if ( tar_get_file ( & tar_ctx , remote_name , finfo ) ) {
err = NT_STATUS_UNSUCCESSFUL ;
goto out ;
}
}
out :
return err ;
}
static int tar_create ( struct tar * t )
{
TALLOC_CTX * ctx = talloc_tos ( ) ;
char * mask = talloc_asprintf ( ctx , " %s \\ * " , client_get_cur_dir ( ) ) ;
int r ;
int err = 0 ;
NTSTATUS status ;
t - > archive = archive_write_new ( ) ;
2013-07-11 20:17:25 +04:00
if ( ! t - > mode . dry ) {
r = archive_write_set_format_pax_restricted ( t - > archive ) ;
if ( r ! = ARCHIVE_OK ) {
2013-07-16 18:32:29 +04:00
DBG ( 0 , ( " Can't open %s: %s \n " , t - > tar_path ,
archive_error_string ( t - > archive ) ) ) ;
2013-07-11 20:17:25 +04:00
}
2013-07-11 02:57:40 +04:00
2013-07-11 20:17:25 +04:00
if ( strequal ( t - > tar_path , " - " ) ) {
r = archive_write_open_fd ( t - > archive , STDOUT_FILENO ) ;
} else {
r = archive_write_open_filename ( t - > archive , t - > tar_path ) ;
}
2013-07-11 02:57:40 +04:00
2013-07-11 20:17:25 +04:00
if ( r ! = ARCHIVE_OK ) {
2013-07-16 18:32:29 +04:00
DBG ( 0 , ( " Can't open %s: %s \n " , t - > tar_path ,
archive_error_string ( t - > archive ) ) ) ;
2013-07-11 20:17:25 +04:00
err = 1 ;
goto out_close ;
}
2013-07-11 02:57:40 +04:00
}
2013-07-11 17:56:03 +04:00
DBG ( 5 , ( " tar_process do_list with mask: %s \n " , mask ) ) ;
2013-07-16 18:32:29 +04:00
status = do_list ( mask , TAR_DO_LIST_ATTR , get_file_callback , false , true ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " do_list fail %s \n " , nt_errstr ( status ) ) ) ;
2013-07-11 02:57:40 +04:00
err = 1 ;
2013-07-11 20:17:25 +04:00
goto out_close ;
}
out_close :
DBG ( 0 , ( " Total bytes received: % " PRIu64 " \n " , t - > total_size ) ) ;
r = archive_write_close ( t - > archive ) ;
if ( r ! = ARCHIVE_OK ) {
DBG ( 0 , ( " Fatal: %s \n " , archive_error_string ( t - > archive ) ) ) ;
err = 1 ;
2013-07-11 02:57:40 +04:00
goto out ;
}
2013-07-10 15:16:08 +04:00
2013-07-10 16:12:25 +04:00
out :
2013-07-11 02:57:40 +04:00
archive_write_free ( t - > archive ) ;
2013-07-10 15:16:08 +04:00
return err ;
}
2007-12-19 23:59:28 +03:00
2013-07-16 21:15:48 +04:00
/**
* Return upper limit for the number of token in @ str .
*
* The result is not exact , the actual number of token might be less
* than what is returned .
*/
static int max_token ( const char * str )
{
const char * s = str ;
int nb = 0 ;
if ( ! str ) {
return 0 ;
}
while ( * s ) {
if ( isspace ( * s ) ) {
nb + + ;
}
s + + ;
}
nb + + ;
return nb ;
}
2013-07-08 20:09:47 +04:00
/**
* cmd_tar - interactive command to start a tar backup / restoration
*
* Check presence of argument , parse them and handle the request .
*/
2001-10-09 23:12:18 +04:00
int cmd_tar ( void )
1996-05-04 11:50:46 +04:00
{
2013-07-16 21:15:48 +04:00
TALLOC_CTX * ctx = talloc_tos ( ) ;
const extern char * cmd_ptr ;
const char * flag ;
const char * * val ;
char * buf ;
int maxtok = max_token ( cmd_ptr ) ;
int i = 0 ;
int err = 0 ;
if ( ! next_token_talloc ( ctx , & cmd_ptr , & buf , NULL ) ) {
DBG ( 0 , ( " tar <c|x>[IXFbganN] [options] <tar file> [path list] \n " ) ) ;
return 1 ;
}
flag = buf ;
val = talloc_array ( ctx , const char * , maxtok ) ;
while ( next_token_talloc ( ctx , & cmd_ptr , & buf , NULL ) ) {
val [ i + + ] = buf ;
}
if ( ! tar_parse_args ( & tar_ctx , flag , val , i ) ) {
DBG ( 0 , ( " parse_args failed \n " ) ) ;
err = 1 ;
goto out ;
}
if ( tar_process ( & tar_ctx ) ) {
DBG ( 0 , ( " tar_process failed \n " ) ) ;
err = 1 ;
goto out ;
}
out :
return err ;
1996-05-04 11:50:46 +04:00
}
2013-07-09 20:01:47 +04:00
static int tar_extract ( struct tar * t )
{
int err = 0 ;
int r ;
struct archive_entry * entry ;
t - > archive = archive_read_new ( ) ;
archive_read_support_format_all ( t - > archive ) ;
archive_read_support_filter_all ( t - > archive ) ;
if ( strequal ( t - > tar_path , " - " ) ) {
r = archive_read_open_fd ( t - > archive , STDIN_FILENO , t - > mode . blocksize ) ;
} else {
r = archive_read_open_filename ( t - > archive , t - > tar_path ,
t - > mode . blocksize ) ;
}
if ( r ! = ARCHIVE_OK ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Can't open %s : %s \n " , t - > tar_path ,
2013-07-09 20:01:47 +04:00
archive_error_string ( t - > archive ) ) ) ;
2013-07-10 15:16:08 +04:00
err = 1 ;
goto out ;
2013-07-09 20:01:47 +04:00
}
for ( ; ; ) {
r = archive_read_next_header ( t - > archive , & entry ) ;
if ( r = = ARCHIVE_EOF ) {
break ;
}
if ( r = = ARCHIVE_WARN ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Warning: %s \n " , archive_error_string ( t - > archive ) ) ) ;
2013-07-09 20:01:47 +04:00
}
if ( r = = ARCHIVE_FATAL ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Fatal: %s \n " , archive_error_string ( t - > archive ) ) ) ;
2013-07-09 20:01:47 +04:00
err = 1 ;
goto out ;
}
2013-07-15 20:58:36 +04:00
if ( tar_extract_skip_path ( t , entry ) ) {
DBG ( 5 , ( " --- %s \n " , archive_entry_pathname ( entry ) ) ) ;
continue ;
}
DBG ( 5 , ( " +++ %s \n " , archive_entry_pathname ( entry ) ) ) ;
2013-07-10 15:16:08 +04:00
if ( tar_send_file ( t , entry ) ) {
err = 1 ;
goto out ;
}
2013-07-09 20:01:47 +04:00
}
out :
r = archive_read_free ( t - > archive ) ;
if ( r ! = ARCHIVE_OK ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Can't close %s : %s \n " , t - > tar_path ,
2013-07-09 20:01:47 +04:00
archive_error_string ( t - > archive ) ) ) ;
err = 1 ;
}
return err ;
}
2013-07-09 17:24:40 +04:00
int tar_process ( struct tar * t )
1996-05-04 11:50:46 +04:00
{
2013-07-09 17:24:40 +04:00
int rc = 0 ;
switch ( t - > mode . operation ) {
case TAR_EXTRACT :
2013-07-09 20:01:47 +04:00
rc = tar_extract ( t ) ;
2013-07-09 17:24:40 +04:00
break ;
case TAR_CREATE :
2013-07-11 02:57:40 +04:00
rc = tar_create ( t ) ;
2013-07-09 17:24:40 +04:00
break ;
default :
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Invalid tar state \n " ) ) ;
2013-07-09 17:24:40 +04:00
rc = 1 ;
}
2013-07-11 02:57:40 +04:00
2013-07-11 17:56:03 +04:00
DBG ( 5 , ( " tar_process done, err = %d \n " , rc ) ) ;
2013-07-09 17:24:40 +04:00
return rc ;
1998-06-17 05:52:57 +04:00
}
2013-07-08 20:09:47 +04:00
/**
* tar_parse_args - parse and set tar command line arguments
* @ flag : string pointing to tar options
* @ val : number of tar arguments
* @ valsize : table of arguments after the flags ( number of element in val )
*
* tar arguments work in a weird way . For each flag f that takes a
* value v , the user is supposed to type :
*
* on the CLI :
* - Tf1f2f3 v1 v2 v3 TARFILE PATHS . . .
*
* in the interactive session :
* tar f1f2f3 v1 v2 v3 TARFILE PATHS . . .
*
* opt has only flags ( eg . " f1f2f3 " ) and val has the arguments
* ( values ) following them ( eg . [ " v1 " , " v2 " , " v3 " , " TARFILE " , " PATH1 " ,
* " PATH2 " ] ) .
*
* There are only 2 flags that take an arg : b and N . The other flags
* just change the semantic of PATH or TARFILE .
*
* PATH can be a list of included / excluded paths , the path to a file
* containing a list of included / excluded paths to use ( F flag ) . If no
* PATH is provided , the whole share is used ( / ) .
*/
2013-07-16 18:32:29 +04:00
int tar_parse_args ( struct tar * t , const char * flag ,
const char * * val , int valsize )
1996-05-04 11:50:46 +04:00
{
2013-07-08 20:09:47 +04:00
TALLOC_CTX * ctx = talloc_tos ( ) ;
2013-07-16 18:22:13 +04:00
bool list = false ;
2013-07-08 20:09:47 +04:00
/* index of next value to use */
int ival = 0 ;
2013-07-05 20:14:50 +04:00
/*
* Reset back some options - could be from interactive version
2013-07-09 13:41:06 +04:00
* all other modes are left as they are
*/
2013-07-08 20:09:47 +04:00
t - > mode . operation = TAR_NO_OPERATION ;
t - > mode . selection = TAR_NO_SELECTION ;
2013-07-09 15:09:56 +04:00
t - > mode . dry = false ;
2013-07-09 17:10:44 +04:00
t - > to_process = false ;
2013-07-11 20:17:25 +04:00
t - > total_size = 0 ;
2013-07-05 20:14:50 +04:00
2013-07-08 20:09:47 +04:00
while ( * flag ) {
switch ( * flag + + ) {
2013-07-05 20:14:50 +04:00
/* operation */
case ' c ' :
2013-07-08 20:09:47 +04:00
if ( t - > mode . operation ! = TAR_NO_OPERATION ) {
2013-07-05 20:14:50 +04:00
printf ( " Tar must be followed by only one of c or x. \n " ) ;
return 0 ;
}
2013-07-08 20:09:47 +04:00
t - > mode . operation = TAR_CREATE ;
2013-07-05 20:14:50 +04:00
break ;
case ' x ' :
2013-07-08 20:09:47 +04:00
if ( t - > mode . operation ! = TAR_NO_OPERATION ) {
2013-07-05 20:14:50 +04:00
printf ( " Tar must be followed by only one of c or x. \n " ) ;
return 0 ;
}
2013-07-09 13:41:06 +04:00
t - > mode . operation = TAR_EXTRACT ;
2013-07-05 20:14:50 +04:00
break ;
/* selection */
case ' I ' :
2013-07-08 20:09:47 +04:00
if ( t - > mode . selection ! = TAR_NO_SELECTION ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Only one of I,X,F must be specified \n " ) ) ;
2013-07-05 20:14:50 +04:00
return 0 ;
}
2013-07-08 20:09:47 +04:00
t - > mode . selection = TAR_INCLUDE ;
2013-07-05 20:14:50 +04:00
break ;
case ' X ' :
2013-07-08 20:09:47 +04:00
if ( t - > mode . selection ! = TAR_NO_SELECTION ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Only one of I,X,F must be specified \n " ) ) ;
2013-07-05 20:14:50 +04:00
return 0 ;
}
2013-07-08 20:09:47 +04:00
t - > mode . selection = TAR_EXCLUDE ;
2013-07-05 20:14:50 +04:00
break ;
case ' F ' :
2013-07-08 20:09:47 +04:00
if ( t - > mode . selection ! = TAR_NO_SELECTION ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Only one of I,X,F must be specified \n " ) ) ;
2013-07-05 20:14:50 +04:00
return 0 ;
}
2013-07-16 18:22:13 +04:00
t - > mode . selection = TAR_INCLUDE ;
list = true ;
2013-07-05 20:14:50 +04:00
break ;
/* blocksize */
case ' b ' :
2013-07-08 20:09:47 +04:00
if ( ival > = valsize ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Option b must be followed by a blocksize \n " ) ) ;
2013-07-05 20:14:50 +04:00
return 0 ;
}
2013-07-08 20:09:47 +04:00
if ( ! tar_set_blocksize ( t , atoi ( val [ ival ] ) ) ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Option b must be followed by a valid blocksize \n " ) ) ;
2013-07-05 20:14:50 +04:00
return 0 ;
}
2013-07-08 20:09:47 +04:00
ival + + ;
2013-07-05 20:14:50 +04:00
break ;
/* incremental mode */
case ' g ' :
2013-07-09 15:09:56 +04:00
t - > mode . incremental = true ;
2013-07-05 20:14:50 +04:00
break ;
/* newer than */
case ' N ' :
2013-07-08 20:09:47 +04:00
if ( ival > = valsize ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Option N must be followed by valid file name \n " ) ) ;
2013-07-05 20:14:50 +04:00
return 0 ;
}
2013-07-08 20:09:47 +04:00
if ( ! tar_set_newer_than ( t , val [ ival ] ) ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Error setting newer-than time \n " ) ) ;
2013-07-05 20:14:50 +04:00
return 0 ;
}
2013-07-08 20:09:47 +04:00
ival + + ;
2013-07-05 20:14:50 +04:00
break ;
/* reset mode */
case ' a ' :
2013-07-09 15:09:56 +04:00
t - > mode . reset = true ;
2013-07-05 20:14:50 +04:00
break ;
/* verbose */
case ' q ' :
2013-07-09 15:09:56 +04:00
t - > mode . verbose = true ;
2013-07-05 20:14:50 +04:00
break ;
/* regex match */
case ' r ' :
2013-07-09 15:09:56 +04:00
t - > mode . regex = true ;
2013-07-05 20:14:50 +04:00
break ;
/* dry run mode */
case ' n ' :
2013-07-08 20:09:47 +04:00
if ( t - > mode . operation ! = TAR_CREATE ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " n is only meaningful when creating a tar-file \n " ) ) ;
2013-07-05 20:14:50 +04:00
return 0 ;
}
2013-07-09 15:09:56 +04:00
t - > mode . dry = true ;
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " dry_run set \n " ) ) ;
2013-07-05 20:14:50 +04:00
break ;
default :
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Unknown tar option \n " ) ) ;
2013-07-05 20:14:50 +04:00
return 0 ;
}
}
2013-07-09 13:41:06 +04:00
/* no selection given? default selection is include */
if ( t - > mode . selection = = TAR_NO_SELECTION ) {
t - > mode . selection = TAR_INCLUDE ;
2013-07-05 20:14:50 +04:00
}
2013-07-08 20:09:47 +04:00
if ( valsize - ival < 1 ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " No tar file given. \n " ) ) ;
2013-07-08 20:09:47 +04:00
return 0 ;
}
/* handle TARFILE */
t - > tar_path = talloc_strdup ( ctx , val [ ival ] ) ;
ival + + ;
2013-07-11 02:57:40 +04:00
/*
* Make sure that dbf points to stderr if we are using stdout for
* tar output
*/
if ( t - > mode . operation = = TAR_CREATE & & strequal ( t - > tar_path , " - " ) ) {
setup_logging ( " smbclient " , DEBUG_STDERR ) ;
}
2013-07-08 20:09:47 +04:00
/* handle PATHs... */
/* flag F -> read file list */
2013-07-16 18:22:13 +04:00
if ( list ) {
2013-07-08 20:09:47 +04:00
if ( valsize - ival ! = 1 ) {
2013-07-11 17:56:03 +04:00
DBG ( 0 , ( " Option F must be followed by exactly one filename. \n " ) ) ;
2013-07-05 20:14:50 +04:00
return 0 ;
}
2013-07-08 20:09:47 +04:00
2013-07-09 13:41:06 +04:00
if ( ! tar_read_inclusion_file ( t , val [ ival ] ) ) {
2013-07-08 20:09:47 +04:00
return 0 ;
}
ival + + ;
2013-07-05 20:14:50 +04:00
}
2013-07-08 20:09:47 +04:00
/* otherwise store all the PATHs on the command line */
else {
int i ;
for ( i = ival ; i < valsize ; i + + ) {
2013-07-10 01:17:46 +04:00
tar_add_selection_path ( t , val [ i ] ) ;
2013-07-08 20:09:47 +04:00
}
}
2013-07-05 20:14:50 +04:00
2013-07-09 17:10:44 +04:00
t - > to_process = true ;
2013-07-09 13:41:06 +04:00
tar_dump ( t ) ;
2013-07-08 20:09:47 +04:00
return 1 ;
1996-05-04 11:50:46 +04:00
}