#include "includes.h"
#include "clitar.h"
#include "client/client_proto.h"
static int clipfind(char **aret, int ret, char *tok);
typedef struct file_info_struct file_info2;
struct file_info_struct {
SMB_OFF_T size;
uint16 mode;
uid_t uid;
gid_t gid;
struct timespec mtime_ts;
struct timespec atime_ts;
struct timespec ctime_ts;
char *name;
file_info2 *next, *prev;
};
typedef struct {
file_info2 *top;
int items;
} stack;
#define SEPARATORS " \t\n\r"
extern time_t newer_than;
extern struct cli_state *cli;
#define ATTRSET 1
#define ATTRRESET 0
static uint16 attribute = aDIR | aSYSTEM | aHIDDEN;
#ifndef CLIENT_TIMEOUT
#define CLIENT_TIMEOUT (30*1000)
#endif
static char *tarbuf, *buffer_p;
static int tp, ntarf, tbufsiz;
static double ttarf;
static BOOL tar_inc=False;
static BOOL tar_reset=False;
static BOOL tar_excl=True;
static BOOL tar_re_search=False;
static BOOL dry_run=False;
static BOOL tar_system=True;
static BOOL tar_hidden=True;
static BOOL tar_noisy=True;
static BOOL tar_real_noisy=False;
char tar_type='\0';
static char **cliplist=NULL;
static int clipn=0;
static BOOL must_free_cliplist = False;
extern file_info def_finfo;
extern BOOL lowercase;
extern uint16 cnum;
extern BOOL readbraw_supported;
extern int max_xmit;
extern pstring cur_dir;
extern int get_total_time_ms;
extern int get_total_size;
static int blocksize=20;
static int tarhandle;
static void writetarheader(int f, const char *aname, SMB_BIG_UINT size, time_t mtime,
const char *amode, unsigned char ftype);
static void do_atar(char *rname,char *lname,file_info *finfo1);
static void do_tar(file_info *finfo);
static void oct_it(SMB_BIG_UINT value, int ndgs, char *p);
static void fixtarname(char *tptr, const char *fp, size_t l);
static int dotarbuf(int f, char *b, int n);
static void dozerobuf(int f, int n);
static void dotareof(int f);
static void initarbuf(void);
static long readtarheader(union hblock *hb, file_info2 *finfo, char *prefix);
static long unoct(char *p, int ndgs);
static void do_tarput(void);
static void unfixtarname(char *tptr, char *fp, int l, BOOL first);
static char *string_create_s(int size)
{
char *tmp;
tmp = (char *)SMB_MALLOC(size+1);
if (tmp == NULL) {
DEBUG(0, ("Out of memory in string_create_s\n"));
}
return(tmp);
}
static void writetarheader(int f, const char *aname, SMB_BIG_UINT size, time_t mtime,
const char *amode, unsigned char ftype)
{
union hblock hb;
int i, chk, l;
char *jp;
DEBUG(5, ("WriteTarHdr, Type = %c, Size= %.0f, Name = %s\n", ftype, (double)size, aname));
memset(hb.dummy, 0, sizeof(hb.dummy));
l=strlen(aname);
if (l+2 >= NAMSIZ) {
char *b;
b = (char *)SMB_MALLOC(l+TBLOCK+100);
if (!b) {
DEBUG(0,("out of memory\n"));
exit(1);
}
writetarheader(f, "/./@LongLink", l+2, 0, " 0 \0", 'L');
memset(b, 0, l+TBLOCK+100);
fixtarname(b, aname, l+2);
i = strlen(b)+1;
DEBUG(5, ("File name in tar file: %s, size=%d, \n", b, (int)strlen(b)));
dotarbuf(f, b, TBLOCK*(((i-1)/TBLOCK)+1));
SAFE_FREE(b);
}
fixtarname(hb.dbuf.name, aname, (l+2 >= NAMSIZ) ? NAMSIZ : l + 2);
if (lowercase)
strlower_m(hb.dbuf.name);
hb.dbuf.name[NAMSIZ-1]='\0';
safe_strcpy(hb.dbuf.mode, amode, sizeof(hb.dbuf.mode)-1);
oct_it((SMB_BIG_UINT)0, 8, hb.dbuf.uid);
oct_it((SMB_BIG_UINT)0, 8, hb.dbuf.gid);
oct_it((SMB_BIG_UINT) size, 13, hb.dbuf.size);
#ifdef HAVE_LONGLONG
if (size > (SMB_BIG_UINT)077777777777LL) {
#else
if (size > (SMB_BIG_UINT)077777777777) {
#endif
memset(hb.dbuf.size, 0, 4);
hb.dbuf.size[0]=128;
for (i = 8, jp=(char*)&size; i; i--)
hb.dbuf.size[i+3] = *(jp++);
}
oct_it((SMB_BIG_UINT) mtime, 13, hb.dbuf.mtime);
memcpy(hb.dbuf.chksum, " ", sizeof(hb.dbuf.chksum));
memset(hb.dbuf.linkname, 0, NAMSIZ);
hb.dbuf.linkflag=ftype;
for (chk=0, i=sizeof(hb.dummy), jp=hb.dummy; --i>=0;)
chk+=(0xFF & *jp++);
oct_it((SMB_BIG_UINT) chk, 8, hb.dbuf.chksum);
hb.dbuf.chksum[6] = '\0';
(void) dotarbuf(f, hb.dummy, sizeof(hb.dummy));
}
static long readtarheader(union hblock *hb, file_info2 *finfo, char *prefix)
{
long chk, fchk;
int i;
char *jp;
for (chk=0, i=sizeof(hb->dummy), jp=hb->dummy; --i>=0;)
chk+=(0xFF & *jp++);
if (chk == 0)
return chk;
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));
DEBUG(5, ("checksum totals chk=%ld fchk=%ld chksum=%s\n",
chk, fchk, hb->dbuf.chksum));
if (fchk != chk) {
DEBUG(0, ("checksums don't match %ld %ld\n", fchk, chk));
dump_data(5, (char *)hb - TBLOCK, TBLOCK *3);
return -1;
}
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);
}
safe_strcpy(finfo->name, prefix, strlen(prefix) + strlen(hb -> dbuf.name) + 3);
unfixtarname(finfo->name + strlen(prefix), hb->dbuf.name,
strlen(hb->dbuf.name) + 1, True);
if ((hb->dbuf.linkflag != '0') && (hb -> dbuf.linkflag != '5')) {
if (hb->dbuf.linkflag == 0) {
DEBUG(6, ("Warning: NULL link flag (gnu tar archive ?) %s\n",
finfo->name));
} else {
if (hb -> dbuf.linkflag == 'L') {
} else {
DEBUG(0, ("this tar file appears to contain some kind \
of link other than a GNUtar Longlink - ignoring\n"));
return -2;
}
}
}
if ((unoct(hb->dbuf.mode, sizeof(hb->dbuf.mode)) & S_IFDIR) ||
(*(finfo->name+strlen(finfo->name)-1) == '\\')) {
finfo->mode=aDIR;
} else {
finfo->mode=0;
}
finfo->mtime_ts = finfo->ctime_ts =
convert_time_t_to_timespec((time_t)strtol(hb->dbuf.mtime, NULL, 8));
finfo->atime_ts = convert_time_t_to_timespec(time(NULL));
finfo->size = unoct(hb->dbuf.size, sizeof(hb->dbuf.size));
return True;
}
static int dotarbuf(int f, char *b, int n)
{
int fail=1, writ=n;
if (dry_run) {
return writ;
}
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);
}
static void dozerobuf(int f, int n)
{
if (dry_run)
return;
if (n+tp >= tbufsiz) {
memset(tarbuf+tp, 0, tbufsiz-tp);
write(f, tarbuf, tbufsiz);
memset(tarbuf, 0, (tp+=n-tbufsiz));
} else {
memset(tarbuf+tp, 0, n);
tp+=n;
}
}
static void initarbuf(void)
{
tbufsiz=blocksize*TBLOCK;
tarbuf=(char *)SMB_MALLOC(tbufsiz);
tp=0; ntarf=0; ttarf=0;
}
static void dotareof(int f)
{
SMB_STRUCT_STAT stbuf;
if (dry_run)
return;
(void) dozerobuf(f, TBLOCK);
(void) dozerobuf(f, TBLOCK);
if (sys_fstat(f, &stbuf) == -1) {
DEBUG(0, ("Couldn't stat file handle\n"));
return;
}
if (tp > 0)
write(f, tarbuf, S_ISREG(stbuf.st_mode) ? tp : tbufsiz);
}
static void fixtarname(char *tptr, const char *fp, size_t l)
{
*tptr++='.';
l--;
StrnCpy(tptr, fp, l-1);
string_replace(tptr, '\\', '/');
}
static void oct_it (SMB_BIG_UINT value, int ndgs, char *p)
{
--ndgs;
p[--ndgs] = ' ';
do {
p[--ndgs] = '0' + (char) (value & 7);
value >>= 3;
} while (ndgs > 0 && value != 0);
while (ndgs > 0)
p[--ndgs] = '0';
}
static long unoct(char *p, int ndgs)
{
long value=0;
while (--ndgs) {
if (isdigit((int)*p))
value = (value << 3) | (long) (*p - '0');
p++;
}
return value;
}
static int strslashcmp(char *s1, char *s2)
{
char *s1_0=s1;
while(*s1 && *s2 && (*s1 == *s2 || tolower_ascii(*s1) == tolower_ascii(*s2) ||
(*s1 == '\\' && *s2=='/') || (*s1 == '/' && *s2=='\\'))) {
s1++; s2++;
}
if (!*s1 && s1 != s1_0 && (*(s1-1) == '/' || *(s1-1) == '\\'))
return 0;
if (!*s2 && (*s1 == '/' || *s1 == '\\') && !*(s1+1))
return 0;
if ((*s2 == '/' || *s2 == '\\') && !*s1)
return 0;
return *s1-*s2;
}
static BOOL ensurepath(char *fname)
{
char *partpath, *ffname;
char *p=fname, *basehack;
DEBUG(5, ( "Ensurepath called with: %s\n", fname));
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));
SAFE_FREE(partpath);
SAFE_FREE(ffname);
return(False);
}
*partpath = 0;
safe_strcpy(ffname, fname, strlen(fname));
if ((basehack=strrchr_m(ffname, '\\')) == NULL)
return True;
else
*basehack='\0';
p=strtok(ffname, "\\");
while (p) {
safe_strcat(partpath, p, strlen(fname) + 1);
if (!cli_chkpath(cli, partpath)) {
if (!cli_mkdir(cli, partpath)) {
DEBUG(0, ("Error mkdirhiering\n"));
return False;
} else {
DEBUG(3, ("mkdirhiering %s\n", partpath));
}
}
safe_strcat(partpath, "\\", strlen(fname) + 1);
p = strtok(NULL,"/\\");
}
return True;
}
static int padit(char *buf, SMB_BIG_UINT bufsize, SMB_BIG_UINT padsize)
{
int berr= 0;
int bytestowrite;
DEBUG(5, ("Padding with %0.f zeros\n", (double)padsize));
memset(buf, 0, (size_t)bufsize);
while( !berr && padsize > 0 ) {
bytestowrite= (int)MIN(bufsize, padsize);
berr = dotarbuf(tarhandle, buf, bytestowrite) != bytestowrite;
padsize -= bytestowrite;
}
return berr;
}
static void do_setrattr(char *name, uint16 attr, int set)
{
uint16 oldattr;
if (!cli_getatr(cli, name, &oldattr, NULL, NULL)) return;
if (set == ATTRSET) {
attr |= oldattr;
} else {
attr = oldattr & ~attr;
}
if (!cli_setatr(cli, name, attr, 0)) {
DEBUG(1,("setatr failed: %s\n", cli_errstr(cli)));
}
}
static void do_atar(char *rname,char *lname,file_info *finfo1)
{
int fnum;
SMB_BIG_UINT nread=0;
char ftype;
file_info2 finfo;
BOOL shallitime=True;
char data[65520];
int read_size = 65520;
int datalen=0;
struct timeval tp_start;
GetTimeOfDay(&tp_start);
ftype = '0';
if (finfo1) {
finfo.size = finfo1 -> size;
finfo.mode = finfo1 -> mode;
finfo.uid = finfo1 -> uid;
finfo.gid = finfo1 -> gid;
finfo.mtime_ts = finfo1 -> mtime_ts;
finfo.atime_ts = finfo1 -> atime_ts;
finfo.ctime_ts = finfo1 -> ctime_ts;
finfo.name = finfo1 -> name;
} else {
finfo.size = def_finfo.size;
finfo.mode = def_finfo.mode;
finfo.uid = def_finfo.uid;
finfo.gid = def_finfo.gid;
finfo.mtime_ts = def_finfo.mtime_ts;
finfo.atime_ts = def_finfo.atime_ts;
finfo.ctime_ts = def_finfo.ctime_ts;
finfo.name = def_finfo.name;
}
if (dry_run) {
DEBUG(3,("skipping file %s of size %12.0f bytes\n", finfo.name,
(double)finfo.size));
shallitime=0;
ttarf+=finfo.size + TBLOCK - (finfo.size % TBLOCK);
ntarf++;
return;
}
fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE);
clean_name(rname);
if (fnum == -1) {
DEBUG(0,("%s opening remote file %s (%s)\n",
cli_errstr(cli),rname, cur_dir));
return;
}
finfo.name = string_create_s(strlen(rname));
if (finfo.name == NULL) {
DEBUG(0, ("Unable to allocate space for finfo.name in do_atar\n"));
return;
}
safe_strcpy(finfo.name,rname, strlen(rname));
if (!finfo1) {
time_t atime, mtime;
if (!cli_getattrE(cli, fnum, &finfo.mode, &finfo.size, NULL, &atime, &mtime)) {
DEBUG(0, ("getattrE: %s\n", cli_errstr(cli)));
return;
}
finfo.atime_ts = convert_time_t_to_timespec(atime);
finfo.mtime_ts = convert_time_t_to_timespec(mtime);
finfo.ctime_ts = finfo.mtime_ts;
}
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;
} 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;
} else {
BOOL wrote_tar_header = False;
DEBUG(3,("getting file %s of size %.0f bytes as a tar file %s",
finfo.name, (double)finfo.size, lname));
do {
DEBUG(3,("nread=%.0f\n",(double)nread));
datalen = cli_read(cli, fnum, data, nread, read_size);
if (datalen == -1) {
DEBUG(0,("Error reading file %s : %s\n", rname, cli_errstr(cli)));
break;
}
nread += datalen;
if (!wrote_tar_header) {
writetarheader(tarhandle, rname, finfo.size,
finfo.mtime_ts.tv_sec, "100644 \0", ftype);
wrote_tar_header = True;
}
if (nread > finfo.size) {
datalen -= nread - finfo.size;
DEBUG(0,("File size change - truncating %s to %.0f bytes\n",
finfo.name, (double)finfo.size));
}
if (dotarbuf(tarhandle,data,datalen) != datalen) {
DEBUG(0,("Error writing to tar file - %s\n", strerror(errno)));
break;
}
if ( (datalen == 0) && (finfo.size != 0) ) {
DEBUG(0,("Error reading file %s. Got 0 bytes\n", rname));
break;
}
datalen=0;
} while ( nread < finfo.size );
if (wrote_tar_header) {
if (nread < finfo.size) {
DEBUG(0, ("Didn't get entire file. size=%.0f, nread=%d\n",
(double)finfo.size, (int)nread));
if (padit(data, (SMB_BIG_UINT)sizeof(data), finfo.size - nread))
DEBUG(0,("Error writing tar file - %s\n", strerror(errno)));
}
if (finfo.size % TBLOCK)
dozerobuf(tarhandle, TBLOCK - (finfo.size % TBLOCK));
ttarf+=finfo.size + TBLOCK - (finfo.size % TBLOCK);
ntarf++;
} else {
DEBUG(4, ("skipping %s - initial read failed (file was locked ?)\n", finfo.name));
shallitime=0;
}
}
cli_close(cli, fnum);
if (shallitime) {
struct timeval tp_end;
int this_time;
if (tar_reset && !dry_run)
(void) do_setrattr(finfo.name, aARCH, ATTRRESET);
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;
if (tar_noisy) {
DEBUG(0, ("%12.0f (%7.1f kb/s) %s\n",
(double)finfo.size, finfo.size / MAX(0.001, (1.024*this_time)),
finfo.name));
}
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))));
}
}
static void do_tar(file_info *finfo)
{
pstring rname;
if (strequal(finfo->name,"..") || strequal(finfo->name,"."))
return;
if (!tar_excl && clipn) {
pstring exclaim;
DEBUG(5, ("Excl: strlen(cur_dir) = %d\n", (int)strlen(cur_dir)));
pstrcpy(exclaim, cur_dir);
*(exclaim+strlen(exclaim)-1)='\0';
pstrcat(exclaim, "\\");
pstrcat(exclaim, finfo->name);
DEBUG(5, ("...tar_re_search: %d\n", tar_re_search));
if ((!tar_re_search && clipfind(cliplist, clipn, exclaim)) ||
(tar_re_search && mask_match_list(exclaim, cliplist, clipn, True))) {
DEBUG(3,("Skipping file %s\n", exclaim));
return;
}
}
if (finfo->mode & aDIR) {
pstring saved_curdir;
pstring mtar_mask;
pstrcpy(saved_curdir, cur_dir);
DEBUG(5, ("Sizeof(cur_dir)=%d, strlen(cur_dir)=%d, \
strlen(finfo->name)=%d\nname=%s,cur_dir=%s\n",
(int)sizeof(cur_dir), (int)strlen(cur_dir),
(int)strlen(finfo->name), finfo->name, cur_dir));
pstrcat(cur_dir,finfo->name);
pstrcat(cur_dir,"\\");
DEBUG(5, ("Writing a dir, Name = %s\n", cur_dir));
writetarheader(tarhandle, cur_dir, 0, finfo->mtime_ts.tv_sec, "040755 \0", '5');
if (tar_noisy) {
DEBUG(0,(" directory %s\n", cur_dir));
}
ntarf++;
pstrcpy(mtar_mask,cur_dir);
pstrcat(mtar_mask,"*");
DEBUG(5, ("Doing list with mtar_mask: %s\n", mtar_mask));
do_list(mtar_mask, attribute, do_tar, False, True);
pstrcpy(cur_dir,saved_curdir);
} else {
pstrcpy(rname,cur_dir);
pstrcat(rname,finfo->name);
do_atar(rname,finfo->name,finfo);
}
}
static void unfixtarname(char *tptr, char *fp, int l, BOOL 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--;
}
}
safe_strcpy(tptr, fp, l);
string_replace(tptr, '/', '\\');
}
static int next_block(char *ltarbuf, char **bufferp, int bufsiz)
{
int bufread, total = 0;
DEBUG(5, ("Advancing to next block: %0lx\n", (unsigned long)*bufferp));
*bufferp += TBLOCK;
total = TBLOCK;
if (*bufferp >= (ltarbuf + bufsiz)) {
DEBUG(5, ("Reading more data into ltarbuf ...\n"));
bufread = read(tarhandle, ltarbuf, bufsiz);
total = bufread;
while (total < bufsiz) {
if (bufread < 0) {
return (total > 0 ? -2 : bufread);
}
if (bufread == 0) {
if (total <= 0) {
return -2;
}
break;
}
bufread = read(tarhandle, <arbuf[total], bufsiz - total);
total += bufread;
}
DEBUG(5, ("Total bytes read ... %i\n", total));
*bufferp = ltarbuf;
}
return(total);
}
static int skip_file(int skipsize)
{
int dsize = skipsize;
DEBUG(5, ("Skiping file. Size = %i\n", skipsize));
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);
}
dsize -= TBLOCK;
}
return(True);
}
static int get_file(file_info2 finfo)
{
int fnum = -1, pos = 0, dsize = 0, bpos = 0;
SMB_BIG_UINT rsize = 0;
DEBUG(5, ("get_file: file: %s, size %.0f\n", finfo.name, (double)finfo.size));
if (ensurepath(finfo.name) &&
(fnum=cli_open(cli, finfo.name, O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) == -1) {
DEBUG(0, ("abandoning restore\n"));
return(False);
}
rsize = finfo.size;
while (rsize > 0) {
dsize = MIN(tbufsiz - (buffer_p - tarbuf) - bpos, 65520);
dsize = MIN(dsize, rsize);
DEBUG(5, ("writing %i bytes, bpos = %i ...\n", dsize, bpos));
if (cli_write(cli, fnum, 0, buffer_p + bpos, pos, dsize) != dsize) {
DEBUG(0, ("Error writing remote file\n"));
return 0;
}
rsize -= dsize;
pos += dsize;
if ((bpos) && ((bpos + dsize) >= TBLOCK)) {
dsize -= (TBLOCK - bpos);
bpos = 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;
}
}
while (((rsize != 0) && (dsize >= TBLOCK)) ||
((rsize == 0) && (dsize > TBLOCK))) {
if (next_block(tarbuf, &buffer_p, tbufsiz) <=0) {
DEBUG(0, ("Empty file, short tar file, or read error: %s\n", strerror(errno)));
return False;
}
dsize -= TBLOCK;
}
bpos = dsize;
}
if (!cli_close(cli, fnum)) {
DEBUG(0, ("Error closing remote file\n"));
return(False);
}
DEBUG(5, ("Updating creation date on %s\n", finfo.name));
if (!cli_setatr(cli, finfo.name, finfo.mode, finfo.mtime_ts.tv_sec)) {
if (tar_real_noisy) {
DEBUG(0, ("Could not set time on file: %s\n", finfo.name));
}
}
ntarf++;
DEBUG(0, ("restore tar file %s of size %.0f bytes\n", finfo.name, (double)finfo.size));
return(True);
}
static int get_dir(file_info2 finfo)
{
DEBUG(0, ("restore directory %s\n", finfo.name));
if (!ensurepath(finfo.name)) {
DEBUG(0, ("Problems creating directory\n"));
return(False);
}
ntarf++;
return(True);
}
static char *get_longfilename(file_info2 finfo)
{
int namesize = finfo.size + strlen(cur_dir) + 2;
char *longname = (char *)SMB_MALLOC(namesize);
int offset = 0, left = finfo.size;
BOOL first = True;
DEBUG(5, ("Restoring a long file name: %s\n", finfo.name));
DEBUG(5, ("Len = %.0f\n", (double)finfo.size));
if (longname == NULL) {
DEBUG(0, ("could not allocate buffer of size %d for longname\n", namesize));
return(NULL);
}
if (strlen(cur_dir) > 0) {
strncpy(longname, cur_dir, namesize);
offset = strlen(cur_dir);
}
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)));
SAFE_FREE(longname);
return(NULL);
}
unfixtarname(longname + offset, buffer_p, MIN(TBLOCK, finfo.size), first--);
DEBUG(5, ("UnfixedName: %s, buffer: %s\n", longname, buffer_p));
offset += TBLOCK;
left -= TBLOCK;
}
return(longname);
}
static void do_tarput(void)
{
file_info2 finfo;
struct timeval tp_start;
char *longfilename = NULL, linkflag;
int skip = False;
ZERO_STRUCT(finfo);
GetTimeOfDay(&tp_start);
DEBUG(5, ("RJS do_tarput called ...\n"));
buffer_p = tarbuf + tbufsiz;
while (True) {
if (next_block(tarbuf, &buffer_p, tbufsiz) <= 0) {
DEBUG(0, ("Empty file, short tar file, or read error: %s\n", strerror(errno)));
SAFE_FREE(longfilename);
return;
}
DEBUG(5, ("Reading the next header ...\n"));
switch (readtarheader((union hblock *) buffer_p, &finfo, cur_dir)) {
case -2:
DEBUG(0, ("Skipping %s...\n", finfo.name));
if ((next_block(tarbuf, &buffer_p, tbufsiz) <= 0) && !skip_file(finfo.size)) {
DEBUG(0, ("Short file, bailing out...\n"));
return;
}
break;
case -1:
DEBUG(0, ("abandoning restore, -1 from read tar header\n"));
return;
case 0:
DEBUG(0, ("tar: restored %d files and directories\n", ntarf));
return;
default:
break;
}
if (longfilename != NULL) {
SAFE_FREE(finfo.name);
finfo.name = longfilename;
longfilename = NULL;
}
skip = clipn && ((!tar_re_search && clipfind(cliplist, clipn, finfo.name) ^ tar_excl) ||
(tar_re_search && mask_match_list(finfo.name, cliplist, clipn, True)));
DEBUG(5, ("Skip = %i, cliplist=%s, file=%s\n", skip, (cliplist?cliplist[0]:NULL), finfo.name));
if (skip) {
skip_file(finfo.size);
continue;
}
linkflag = ((union hblock *)buffer_p) -> dbuf.linkflag;
switch (linkflag) {
case '0':
if ((finfo.size != 0) && next_block(tarbuf, &buffer_p, tbufsiz) <=0) {
DEBUG(0, ("Short file, bailing out...\n"));
return;
}
if (!get_file(finfo)) {
DEBUG(0, ("Abandoning restore\n"));
return;
}
break;
case '5':
if (!get_dir(finfo)) {
DEBUG(0, ("Abandoning restore \n"));
return;
}
break;
case 'L':
SAFE_FREE(longfilename);
longfilename = get_longfilename(finfo);
if (!longfilename) {
DEBUG(0, ("abandoning restore\n"));
return;
}
DEBUG(5, ("Long file name: %s\n", longfilename));
break;
default:
skip_file(finfo.size);
break;
}
}
}
int cmd_block(void)
{
fstring buf;
int block;
if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
DEBUG(0, ("blocksize <n>\n"));
return 1;
}
block=atoi(buf);
if (block < 0 || block > 65535) {
DEBUG(0, ("blocksize out of range"));
return 1;
}
blocksize=block;
DEBUG(2,("blocksize is now %d\n", blocksize));
return 0;
}
int cmd_tarmode(void)
{
fstring buf;
while (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
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;
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;
else
DEBUG(0, ("tarmode: unrecognised option %s\n", buf));
}
DEBUG(0, ("tarmode is now %s, %s, %s, %s, %s\n",
tar_inc ? "incremental" : "full",
tar_system ? "system" : "nosystem",
tar_hidden ? "hidden" : "nohidden",
tar_reset ? "reset" : "noreset",
tar_noisy ? "verbose" : "quiet"));
return 0;
}
int cmd_setmode(void)
{
char *q;
fstring buf;
pstring fname;
uint16 attra[2];
int direct=1;
attra[0] = attra[1] = 0;
if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
DEBUG(0, ("setmode <filename> <[+|-]rsha>\n"));
return 1;
}
pstrcpy(fname, cur_dir);
pstrcat(fname, buf);
while (next_token_nr(NULL,buf,NULL,sizeof(buf))) {
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 1;
}
}
}
if (attra[ATTRSET]==0 && attra[ATTRRESET]==0) {
DEBUG(0, ("setmode <filename> <[+|-]rsha>\n"));
return 1;
}
DEBUG(2, ("\nperm set %d %d\n", attra[ATTRSET], attra[ATTRRESET]));
do_setrattr(fname, attra[ATTRSET], ATTRSET);
do_setrattr(fname, attra[ATTRRESET], ATTRRESET);
return 0;
}
int cmd_tar(void)
{
fstring buf;
char **argl = NULL;
int argcl = 0;
int ret;
if (!next_token_nr(NULL,buf,NULL,sizeof(buf))) {
DEBUG(0,("tar <c|x>[IXbgan] <filename>\n"));
return 1;
}
argl=toktocliplist(&argcl, NULL);
if (!tar_parseargs(argcl, argl, buf, 0))
return 1;
ret = process_tar();
SAFE_FREE(argl);
return ret;
}
int process_tar(void)
{
int rc = 0;
initarbuf();
switch(tar_type) {
case 'x':
#if 0
do_tarput2();
#else
do_tarput();
#endif
SAFE_FREE(tarbuf);
close(tarhandle);
break;
case 'r':
case 'c':
if (clipn && tar_excl) {
int i;
pstring tarmac;
for (i=0; i<clipn; i++) {
DEBUG(5,("arg %d = %s\n", i, cliplist[i]));
if (*(cliplist[i]+strlen(cliplist[i])-1)=='\\') {
*(cliplist[i]+strlen(cliplist[i])-1)='\0';
}
if (strrchr_m(cliplist[i], '\\')) {
pstring saved_dir;
pstrcpy(saved_dir, cur_dir);
if (*cliplist[i]=='\\') {
pstrcpy(tarmac, cliplist[i]);
} else {
pstrcpy(tarmac, cur_dir);
pstrcat(tarmac, cliplist[i]);
}
pstrcpy(cur_dir, tarmac);
*(strrchr_m(cur_dir, '\\')+1)='\0';
DEBUG(5, ("process_tar, do_list with tarmac: %s\n", tarmac));
do_list(tarmac,attribute,do_tar, False, True);
pstrcpy(cur_dir,saved_dir);
} else {
pstrcpy(tarmac, cur_dir);
pstrcat(tarmac, cliplist[i]);
DEBUG(5, ("process_tar, do_list with tarmac: %s\n", tarmac));
do_list(tarmac,attribute,do_tar, False, True);
}
}
} else {
pstring mask;
pstrcpy(mask,cur_dir);
DEBUG(5, ("process_tar, do_list with mask: %s\n", mask));
pstrcat(mask,"\\*");
do_list(mask,attribute,do_tar,False, True);
}
if (ntarf)
dotareof(tarhandle);
close(tarhandle);
SAFE_FREE(tarbuf);
DEBUG(0, ("tar: dumped %d files and directories\n", ntarf));
DEBUG(0, ("Total bytes written: %.0f\n", (double)ttarf));
break;
}
if (must_free_cliplist) {
int i;
for (i = 0; i < clipn; ++i) {
SAFE_FREE(cliplist[i]);
}
SAFE_FREE(cliplist);
cliplist = NULL;
clipn = 0;
must_free_cliplist = False;
}
return rc;
}
static int clipfind(char **aret, int ret, char *tok)
{
if (aret==NULL)
return 0;
while(strchr_m("/\\.", *tok))
tok++;
while(ret--) {
char *pkey=*aret++;
while(strchr_m("/\\.", *pkey))
pkey++;
if (!strslashcmp(pkey, tok))
return 1;
}
return 0;
}
static int read_inclusion_file(char *filename)
{
XFILE *inclusion = NULL;
char buf[PATH_MAX + 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[PATH_MAX] = '\0';
if ((inclusion = x_fopen(filename, O_RDONLY, 0)) == NULL) {
DEBUG(0,("Unable to open inclusion file %s\n", filename));
return 0;
}
while ((! error) && (x_fgets(buf, sizeof(buf)-1, inclusion))) {
if (inclusion_buffer == NULL) {
inclusion_buffer_size = 1024;
if ((inclusion_buffer = (char *)SMB_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) {
inclusion_buffer_size *= 2;
inclusion_buffer = (char *)SMB_REALLOC(inclusion_buffer,inclusion_buffer_size);
if (!inclusion_buffer) {
DEBUG(0,("failure enlarging inclusion buffer to %d bytes\n",
inclusion_buffer_size));
error = 1;
break;
}
}
safe_strcpy(inclusion_buffer + inclusion_buffer_sofar, buf, inclusion_buffer_size - inclusion_buffer_sofar);
inclusion_buffer_sofar += strlen(buf) + 1;
clipn++;
}
x_fclose(inclusion);
if (! error) {
cliplist = SMB_MALLOC_ARRAY(char *, clipn + 1);
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++) {
cliplist[i] = NULL;
if ((tmpstr = (char *)SMB_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;
if ((p = strchr_m(p, '\000')) == NULL) {
DEBUG(0,("INTERNAL ERROR: inclusion_buffer is of unexpected contents.\n"));
abort();
}
}
++p;
}
must_free_cliplist = True;
}
}
SAFE_FREE(inclusion_buffer);
if (error) {
if (cliplist) {
char **pp;
for (pp = cliplist; *pp; ++pp) {
SAFE_FREE(*pp);
}
SAFE_FREE(cliplist);
cliplist = NULL;
must_free_cliplist = False;
}
return 0;
}
return 1;
}
int tar_parseargs(int argc, char *argv[], const char *Optarg, int Optind)
{
int newOptind = Optind;
char tar_clipfl='\0';
tar_type='\0';
tar_excl=True;
dry_run=False;
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++;
newOptind++;
}
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 {
SMB_STRUCT_STAT stbuf;
if (sys_stat(argv[Optind], &stbuf) == 0) {
newer_than = stbuf.st_mtime;
DEBUG(1,("Getting files newer than %s",
time_to_asc(newer_than)));
newOptind++;
Optind++;
} else {
DEBUG(0,("Error setting newer-than time\n"));
return 0;
}
}
break;
case 'a':
tar_reset=True;
break;
case 'q':
tar_noisy=False;
break;
case 'I':
if (tar_clipfl) {
DEBUG(0,("Only one of I,X,F must be specified\n"));
return 0;
}
tar_clipfl='I';
break;
case 'X':
if (tar_clipfl) {
DEBUG(0,("Only one of I,X,F must be specified\n"));
return 0;
}
tar_clipfl='X';
break;
case 'F':
if (tar_clipfl) {
DEBUG(0,("Only one of I,X,F must be specified\n"));
return 0;
}
tar_clipfl='F';
break;
case 'r':
DEBUG(0, ("tar_re_search set\n"));
tar_re_search = True;
break;
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;
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;
}
tar_excl=tar_clipfl!='X';
if (tar_clipfl=='F') {
if (argc-Optind-1 != 1) {
DEBUG(0,("Option F must be followed by exactly one filename.\n"));
return 0;
}
newOptind++;
if (! read_inclusion_file(argv[Optind+1])) {
return 0;
}
} else if (Optind+1<argc && !tar_re_search) {
char *tmpstr;
char **tmplist;
int clipcount;
cliplist=argv+Optind+1;
clipn=argc-Optind-1;
clipcount = clipn;
if ((tmplist=SMB_MALLOC_ARRAY(char *,clipn)) == 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 *)SMB_MALLOC(strlen(cliplist[clipcount])+1)) == NULL) {
DEBUG(0, ("Could not allocate space for a cliplist item, # %i\n", clipcount));
SAFE_FREE(tmplist);
return 0;
}
unfixtarname(tmpstr, cliplist[clipcount], strlen(cliplist[clipcount]) + 1, True);
tmplist[clipcount] = tmpstr;
DEBUG(5, ("Processed an item, %s\n", tmpstr));
DEBUG(5, ("Cliplist is: %s\n", cliplist[0]));
}
cliplist = tmplist;
must_free_cliplist = True;
newOptind += clipn;
}
if (Optind+1<argc && tar_re_search && tar_clipfl != 'F') {
clipn=argc-Optind-1;
cliplist=argv+Optind+1;
newOptind += clipn;
}
if (Optind>=argc || !strcmp(argv[Optind], "-")) {
tarhandle=(tar_type=='c');
if (tarhandle == 1) {
dbf = x_stderr;
}
if (!argv[Optind]) {
DEBUG(0,("Must specify tar filename\n"));
return 0;
}
if (!strcmp(argv[Optind], "-")) {
newOptind++;
}
} else {
if (tar_type=='c' && dry_run) {
tarhandle=-1;
} else if ((tar_type=='x' && (tarhandle = sys_open(argv[Optind], O_RDONLY, 0)) == -1)
|| (tar_type=='c' && (tarhandle=sys_creat(argv[Optind], 0644)) < 0)) {
DEBUG(0,("Error opening local file %s - %s\n", argv[Optind], strerror(errno)));
return(0);
}
newOptind++;
}
return newOptind;
}