faubackup/src faubackup-gather.c,1.7,1.8 faubackup-scatter.c,1.9,1.10

Martin Waitz <[email protected]> Mon, 29 Mar 2004 00:32:53 +0000
Newsgroups gmane.comp.sysutils.backup.faubackup.cvs
Message-ID <[email protected]>
Update of /cvsroot/faubackup/faubackup/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15658/src

Modified Files:
	faubackup-gather.c faubackup-scatter.c 
Log Message:
use stdio instead of direct syscalls

Index: faubackup-scatter.c
===================================================================
RCS file: /cvsroot/faubackup/faubackup/src/faubackup-scatter.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** faubackup-scatter.c	18 Mar 2004 01:13:58 -0000	1.9
--- faubackup-scatter.c	29 Mar 2004 00:32:51 -0000	1.10
***************
*** 51,68 ****
  
  static int
! recv_buf(void *buf, long long len, int mayfail)
  {
  	unsigned char *b;
! 	long long ret;
  
  	b = buf;
  	while( len>0 ) {
! 		ret = read(0, b, len);
! 		if( ret == 0 ) break;
! 		if( ret < 0 ) {
! 			fprintf(stderr, "%s: read: %s\n",
  					progname, strerror(errno));
  			break;
  		}
  		b += ret;
  		len -= ret;
--- 51,68 ----
  
  static int
! recv_buf(void *buf, size_t len, int mayfail)
  {
  	unsigned char *b;
! 	size_t ret;
  
  	b = buf;
  	while( len>0 ) {
! 		ret = fread(b, 1, len, stdin);
! 		if( ferror(stdin) ) {
! 			fprintf(stderr, "%s: fread: %s\n",
  					progname, strerror(errno));
  			break;
  		}
+ 		if( feof(stdin) ) break;
  		b += ret;
  		len -= ret;
***************
*** 70,74 ****
  
  	if( len!=0 && ! mayfail ) {
! 		fprintf(stderr, "%s: protocol error (%lld bytes missing)\n",
  				progname, len);
  		exit(1);
--- 70,74 ----
  
  	if( len!=0 && ! mayfail ) {
! 		fprintf(stderr, "%s: protocol error (%d bytes missing)\n",
  				progname, len);
  		exit(1);
***************
*** 327,331 ****
  
  static char *
! actdevinopath(dev_t dev, ino_t ino)
  {
  	static char path[MAXLEN];
--- 327,331 ----
  
  static char *
! devinopath(dev_t dev, ino_t ino)
  {
  	static char path[MAXLEN];
***************
*** 481,499 ****
  scatter_reg(char *path, struct stat *st)
  {
! 	char *actfile;
! 	char *oldfile;
  	struct stat oldst;
  	int equal;
! 	long long pos;
! 	int tfd;
! 	int ofd;
! 	long long ret;
  
  
  	assert( S_ISREG(st->st_mode) );
  
! 	oldfile = olddevinopath( st->st_dev, st->st_ino );
! 	actfile = actdevinopath( st->st_dev, st->st_ino );
! 	make_path( actfile );
  
  	equal = 1;
--- 481,498 ----
  scatter_reg(char *path, struct stat *st)
  {
! 	char *targetname;
! 	char *oldname;
! 	FILE *target, *old;
  	struct stat oldst;
  	int equal;
! 	off_t pos;
! 	int ret;
  
  
  	assert( S_ISREG(st->st_mode) );
  
! 	oldname = olddevinopath( st->st_dev, st->st_ino );
! 	targetname = devinopath( st->st_dev, st->st_ino );
! 	make_path( targetname );
  
  	equal = 1;
***************
*** 503,517 ****
  	 */
  
! 	ret = lstat( oldfile, &oldst );
  	if( ret < 0 && errno == ENOENT ) {
  		equal = 0;
  	} else if( ret < 0 ) {
  		fprintf( stderr, "%s: lstat %s: %s\n",
! 				progname, oldfile, strerror(errno) );
  		error++;
  		equal = 0;
  	}
  
! 	/* !! do _not_ check atime; will be changed by backup itself !! */
  	if( st->st_mode != oldst.st_mode
  	 || st->st_size != oldst.st_size
--- 502,516 ----
  	 */
  
! 	ret = lstat( oldname, &oldst );
  	if( ret < 0 && errno == ENOENT ) {
  		equal = 0;
  	} else if( ret < 0 ) {
  		fprintf( stderr, "%s: lstat %s: %s\n",
! 				progname, oldname, strerror(errno) );
  		error++;
  		equal = 0;
  	}
  
! 	/* do _not_ check atime/ctime; will be changed by backup itself */
  	if( st->st_mode != oldst.st_mode
  	 || st->st_size != oldst.st_size
***************
*** 526,534 ****
  	/* ================================================= */
  
! 	tfd = open( actfile, O_WRONLY | O_CREAT | O_EXCL, st->st_mode & 0700 );
! 	if( tfd < 0 ) {
  		if( errno!=EEXIST ) {
! 			fprintf( stderr, "%s: open(%s, O_WRONLY): %s\n",
! 					progname, actfile, strerror(errno) );
  			if( errno==ENOSPC ) exit(1);
  			error++;
--- 525,533 ----
  	/* ================================================= */
  
! 	target = fopen( targetname, "w");
! 	if( target==NULL ) {
  		if( errno!=EEXIST ) {
! 			fprintf( stderr, "%s: fopen(%s): %s\n",
! 					progname, targetname, strerror(errno) );
  			if( errno==ENOSPC ) exit(1);
  			error++;
***************
*** 538,544 ****
  		 * to be able to handle the following files
  		 */
! 		tfd = open( "/dev/null", O_WRONLY );
! 		if( tfd < 0 ) {
! 			fprintf( stderr, "%s: open(\"/dev/null\", O_WRONLY): %s\n",
  					progname, strerror(errno) );
  			exit( 1 );
--- 537,543 ----
  		 * to be able to handle the following files
  		 */
! 		target = fopen( "/dev/null", "w" );
! 		if( target==NULL ) {
! 			fprintf( stderr, "%s: fopen(\"/dev/null\"): %s\n",
  					progname, strerror(errno) );
  			exit( 1 );
***************
*** 546,551 ****
  	}
  	if( equal ) {
! 		ofd = open( oldfile, O_RDONLY );
! 		if( ofd < 0 && errno != ENOENT ) {
  			/*
  			 * problem with Solaris:
--- 545,550 ----
  	}
  	if( equal ) {
! 		old = fopen( oldname, "r" );
! 		if( old==NULL && errno!=ENOENT ) {
  			/*
  			 * problem with Solaris:
***************
*** 553,644 ****
  			 * with "-rw--l---" files even with "no_root_squash"
  			 */
! 			fprintf( stderr, "%s: WARNING: open(%s, O_RDONLY): %s\n",
! 					progname, oldfile, strerror(errno) );
  			error++;
  		}
! 		if( ofd < 0 ) {
  			equal = 0;
  		}
  	} else {
! 		ofd=-1;
  	}
  
  	for( pos = 0; pos < st->st_size; ) {
! 		static char zero[4096] = { 0, 0, /* ... */ };
  		char buf[4096];
  		char obuf[4096];
! 		long long len;
! 		long long n_read, n_written;
! 
! 		n_read=0;
! 		n_written=0;
  
! 		if( st->st_size - pos < (long long)sizeof(buf) ) {
  			len = st->st_size - pos;
  		} else {
! 			len = (long long)sizeof(buf);
  		}
  		len = recv_buf( buf, len, 0 );
  
  		/* compare files */
! 		if( equal && ofd>=0 ) while( (n_read < len) && equal ) {
! 			ret = read( ofd, obuf, len-n_read );
! 			if( ret < 0 ) {
! 				equal = 0;
! 			} else if( memcmp(buf, obuf, len) != 0 ) {
  				equal = 0;
  			}
- 			n_read += ret;
  		}
! 		if( len==(long long)sizeof(zero) && pos + len != st->st_size
  				&& memcmp(buf, zero, len) == 0 ) {
  			/* found an empty page, create hole in target file */
! 			ret = lseek( tfd, len, SEEK_CUR );
  			if( ret < 0 ) {
! 				fprintf( stderr, "%s: lseek(%d, %lld, SEEK_CUR) on %s : %lld: %s\n",
! 						progname, tfd, len, actfile, ret, strerror(errno) );
  				error++;
  			}
! 			/*
! 			 * do not assume 'ret' contains meaningful
! 			 * position data (32-bit problem)
! 			 */
! 			/* assert(ret == pos + len); */
! 
! 		} else while( n_written<len ) {
! 			/* write this page into a temp. file */
! 			ret = write( tfd, buf, len );
! 			if( ret < 0 ) {
! 				/* try again? */
! 				if( errno==EAGAIN || errno==EINTR ) continue;
! 
! 				fprintf( stderr, "%s: write %s: %s\n",
! 						progname, actfile, strerror(errno) );
  				/* fatal error on 'no space left on device' */
  				if( errno==ENOSPC ) exit( 1 );
  				error++;
- 				ret = 0;
  			}
- 			n_written += ret;
  		}
  		pos += len;
  	}
  
! 	if( ofd>=0 ) {
! 		ret = close( ofd );
  		if( ret < 0 ) {
! 			fprintf( stderr, "%s: close %s: %s\n",
! 					progname, oldfile, strerror(errno) );
  			error++;
  		}
  	}
! 	ret = close(tfd);
  	if( ret < 0 ) {
! 		fprintf( stderr, "%s: close %s: %s\n",
! 				progname, actfile, strerror(errno) );
  		error++;
  	}
  
! 	set_perms( actfile, st, SET_ALL );
  
  	/*
--- 552,628 ----
  			 * with "-rw--l---" files even with "no_root_squash"
  			 */
! 			fprintf( stderr, "%s: WARNING: fopen(%s): %s\n",
! 					progname, oldname, strerror(errno) );
  			error++;
  		}
! 		if( old==NULL ) {
  			equal = 0;
  		}
  	} else {
! 		old=NULL;
  	}
  
  	for( pos = 0; pos < st->st_size; ) {
! 		const static char zero[4096] = { 0, 0, /* ... */ };
  		char buf[4096];
  		char obuf[4096];
! 		size_t len;
  
! 		if( st->st_size - pos < (off_t)sizeof(buf) ) {
  			len = st->st_size - pos;
  		} else {
! 			len = sizeof(buf);
  		}
  		len = recv_buf( buf, len, 0 );
  
  		/* compare files */
! 		if( equal ) {
! 			size_t n_read = fread( obuf, 1, len, old );
! 			if( n_read!=len || ferror(old) ||
! 						memcmp(buf, obuf, len)!=0 ) {
  				equal = 0;
  			}
  		}
! 		if( len==sizeof(zero) && pos + len != st->st_size
  				&& memcmp(buf, zero, len) == 0 ) {
+ 
  			/* found an empty page, create hole in target file */
! 			ret = fseek( target, len, SEEK_CUR );
  			if( ret < 0 ) {
! 				fprintf( stderr, "%s: fseek(%s, %ld, SEEK_CUR): %s\n",
! 						progname, targetname,
! 						len, strerror(errno) );
  				error++;
  			}
! 		} else {
! 			fwrite( buf, 1, len, target );
! 			if( ferror(target) ) {
! 				fprintf( stderr, "%s: fwrite %s: %s\n",
! 						progname, targetname,
! 						strerror(errno) );
  				/* fatal error on 'no space left on device' */
  				if( errno==ENOSPC ) exit( 1 );
  				error++;
  			}
  		}
  		pos += len;
  	}
  
! 	if( old ) {
! 		ret = fclose( old );
  		if( ret < 0 ) {
! 			fprintf( stderr, "%s: fclose %s: %s\n",
! 					progname, oldname, strerror(errno) );
  			error++;
  		}
  	}
! 	ret = fclose(target);
  	if( ret < 0 ) {
! 		fprintf( stderr, "%s: fclose %s: %s\n",
! 				progname, targetname, strerror(errno) );
  		error++;
  	}
  
! 	set_perms( targetname, st, SET_ALL );
  
  	/*
***************
*** 653,666 ****
  
  	if( equal ) {
! 		ret = unlink(actfile);
  		if( ret < 0 ) {
  			fprintf( stderr, "%s: unlink %s: %s\n",
! 					progname, actfile, strerror(errno) );
  			error++;
  		} else {
! 			ret = link( oldfile, actfile );
  			if( ret < 0 ) {
  				fprintf( stderr, "%s: link %s, %s: %s\n",
! 						progname, oldfile, actfile, strerror(errno) );
  				error++;
  			}
--- 637,650 ----
  
  	if( equal ) {
! 		ret = unlink(targetname);
  		if( ret < 0 ) {
  			fprintf( stderr, "%s: unlink %s: %s\n",
! 					progname, targetname, strerror(errno) );
  			error++;
  		} else {
! 			ret = link( oldname, targetname );
  			if( ret < 0 ) {
  				fprintf( stderr, "%s: link %s, %s: %s\n",
! 						progname, oldname, targetname, strerror(errno) );
  				error++;
  			}
***************
*** 671,678 ****
  	 * link file into directory
  	 */
! 	ret = link( actfile, path );
  	if( ret < 0 ) {
  		fprintf( stderr, "%s: link %s, %s: %s\n",
! 				progname, actfile, path, strerror(errno) );
  		error++;
  	}
--- 655,662 ----
  	 * link file into directory
  	 */
! 	ret = link( targetname, path );
  	if( ret < 0 ) {
  		fprintf( stderr, "%s: link %s, %s: %s\n",
! 				progname, targetname, path, strerror(errno) );
  		error++;
  	}
***************
*** 782,796 ****
  	}
  
- 	/* ======================= */
  	/* get date of last backup */
- 	/* ======================= */
- 
  	dir_getlast();
  
! 	/* =========== */
! 	/* unset umask */
! 	/* =========== */
! 
! 	ret = umask(0000);
  	if( ret < 0 ) {
  		fprintf( stderr, "%s: umask(0): %s\n",
--- 766,774 ----
  	}
  
  	/* get date of last backup */
  	dir_getlast();
  
! 	/* make new files not readable at creation time */
! 	ret = umask(0577);
  	if( ret < 0 ) {
  		fprintf( stderr, "%s: umask(0): %s\n",

Index: faubackup-gather.c
===================================================================
RCS file: /cvsroot/faubackup/faubackup/src/faubackup-gather.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** faubackup-gather.c	18 Mar 2004 01:13:58 -0000	1.7
--- faubackup-gather.c	29 Mar 2004 00:32:51 -0000	1.8
***************
*** 49,53 ****
  
  static void
! send_buf(void *_buf, long long len)
  {
  	unsigned char *buf = _buf;
--- 49,53 ----
  
  static void
! send_buf(void *_buf, size_t len)
  {
  	unsigned char *buf = _buf;
***************
*** 55,62 ****
  
  	while( len > 0 ) {
! 		ret = write( 1, buf, len );
! 		if( ret < 0 ) {
! 			fprintf( stderr, "%s: write(1, ...): %s\n",
! 					progname, strerror(errno));
  			exit(1);
  		}
--- 55,62 ----
  
  	while( len > 0 ) {
! 		ret = fwrite( buf, 1, len, stdout );
! 		if( ferror(stdout) ) {
! 			fprintf( stderr, "%s: fwrite %d bytes: %s\n",
! 					progname, len, strerror(errno));
  			exit(1);
  		}
***************
*** 142,155 ****
  gather_reg(char *path, struct stat *st)
  {
! 	int fd;
  	int ret;
! 	long long pos;
  	struct utimbuf times;
  
  	assert( S_ISREG(st->st_mode) );
  
! 	fd = open( path, O_RDONLY );
! 	if( fd < 0) {
! 		fprintf( stderr, "%s: open(%s, O_RDONLY): %s\n",
  				progname, path, strerror(errno));
  		error++;
--- 142,155 ----
  gather_reg(char *path, struct stat *st)
  {
! 	FILE *file;
  	int ret;
! 	off_t pos;
  	struct utimbuf times;
  
  	assert( S_ISREG(st->st_mode) );
  
! 	file = fopen( path, "r" );
! 	if( !file ) {
! 		fprintf( stderr, "%s: fopen(%s): %s\n",
  				progname, path, strerror(errno));
  		error++;
***************
*** 161,175 ****
  
  	for( pos = 0; pos < st->st_size; ) {
! 		char buf[1024*1024];
! 		long long count;
! 		long long len;
  
! 		count = st->st_size - pos;
! 		if( sizeof(buf) < count ) {
! 			count = (long long)sizeof(buf);
  		}
! 		len = read(fd, buf, count);
! 		if( len < 0) {
! 			fprintf(stderr, "%s: read %s: %s\n",
  					progname, path, strerror(errno));
  			error++;
--- 161,177 ----
  
  	for( pos = 0; pos < st->st_size; ) {
! 		char buf[128*1024];
! 		size_t count;
! 		size_t len;
  
! 		if( st->st_size - pos < (off_t) sizeof(buf) ) {
! 			count = st->st_size - pos;
! 		} else {
! 			count = sizeof(buf);
  		}
! 
! 		len = fread(buf, 1, count, file);
! 		if( ferror(file) < 0) {
! 			fprintf(stderr, "%s: fread %s: %s\n",
  					progname, path, strerror(errno));
  			error++;
***************
*** 185,189 ****
  	}
  
! 	ret = close(fd);
  	if( ret < 0) {
  		fprintf(stderr, "%s: close %s: %s\n",
--- 187,191 ----
  	}
  
! 	ret = fclose(file);
  	if( ret < 0) {
  		fprintf(stderr, "%s: close %s: %s\n",



-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click