⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buf_subs.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
		/*		 * only move what we have space for		 */		cnt = MIN(cnt, outcnt);		bcopy(out, bufpt, cnt);		bufpt += cnt;		out += cnt;		outcnt -= cnt;	}	return(0);}/* * rd_wrbuf() *	copy from the read buffer into a supplied buffer a specified number of *	bytes. If the read buffer is empty fill it and continue to copy. *	usually used to obtain a file header for processing by a format *	specific read routine. * Return *	number of bytes copied to the buffer, 0 indicates EOF on archive volume, *	-1 is a read error */#if __STDC__intrd_wrbuf(register char *in, register int cpcnt)#elseintrd_wrbuf(in, cpcnt)	register char *in;	register int cpcnt;#endif{	register int res;	register int cnt;	register int incnt = cpcnt;	/*	 * loop until we fill the buffer with the requested number of bytes	 */	while (incnt > 0) {		cnt = bufend - bufpt;		if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) {			/*			 * read error, return what we got (or the error if			 * no data was copied). The caller must know that an			 * error occured and has the best knowledge what to			 * do with it			 */			if ((res = cpcnt - incnt) > 0)				return(res);			return(cnt);		}		/*		 * calculate how much data to copy based on whats left and		 * state of buffer		 */		cnt = MIN(cnt, incnt);		bcopy(bufpt, in, cnt);		bufpt += cnt;		incnt -= cnt;		in += cnt;	}	return(cpcnt);}/* * wr_skip() *	skip foward during a write. In other words add padding to the file. *	we add zero filled padding as it makes flawed archives much easier to *	recover from. the caller tells us how many bytes of padding to add *	This routine was not designed to add HUGE amount of padding, just small *	amounts (a few 512 byte blocks at most) * Return: *	0 if ok, -1 if there was a buf_flush failure */#if __STDC__intwr_skip(off_t skcnt)#elseintwr_skip(skcnt)	off_t skcnt;#endif{	register int cnt;	/*	 * loop while there is more padding to add	 */	while (skcnt > 0L) {		cnt = bufend - bufpt;		if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0))			return(-1);		cnt = MIN(cnt, skcnt);		bzero(bufpt, cnt);		bufpt += cnt;		skcnt -= cnt;	}	return(0);}/* * wr_rdfile() *	fill write buffer with the contents of a file. We are passed an	open *	file descriptor to the file an the archive structure that describes the *	file we are storing. The variable "left" is modified to contain the *	number of bytes of the file we were NOT able to write to the archive. *	it is important that we always write EXACTLY the number of bytes that *	the format specific write routine told us to. The file can also get *	bigger, so reading to the end of file would create an improper archive, *	we just detect this case and warn the user. We never create a bad *	archive if we can avoid it. Of course trying to archive files that are *	active is asking for trouble. It we fail, we pass back how much we *	could NOT copy and let the caller deal with it. * Return: *	0 ok, -1 if archive write failure. a short read of the file returns a *	0, but "left" is set to be greater than zero. */#if __STDC__intwr_rdfile(ARCHD *arcn, int ifd, off_t *left)#elseintwr_rdfile(arcn, ifd, left)	ARCHD *arcn;	int ifd;	off_t *left;#endif{	register int cnt;	register int res = 0;	register off_t size = arcn->sb.st_size;	struct stat sb;	/*	 * while there are more bytes to write	 */	while (size > 0L) {		cnt = bufend - bufpt;		if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) {			*left = size;			return(-1);		}		cnt = MIN(cnt, size);		if ((res = read(ifd, bufpt, cnt)) <= 0)			break;		size -= res;		bufpt += res;	}	/*	 * better check the file did not change during this operation	 * or the file read failed.	 */	if (res < 0)		syswarn(1, errno, "Read fault on %s", arcn->org_name);	else if (size != 0L)		warn(1, "File changed size during read %s", arcn->org_name);	else if (fstat(ifd, &sb) < 0)		syswarn(1, errno, "Failed stat on %s", arcn->org_name);	else if (arcn->sb.st_mtime != sb.st_mtime)		warn(1, "File %s was modified during copy to archive",			arcn->org_name);	*left = size;	return(0);}/* * rd_wrfile() *	extract the contents of a file from the archive. If we are unable to *	extract the entire file (due to failure to write the file) we return *	the numbers of bytes we did NOT process. This way the caller knows how *	many bytes to skip past to find the next archive header. If the failure *	was due to an archive read, we will catch that when we try to skip. If *	the format supplies a file data crc value, we calculate the actual crc *	so that it can be compared to the value stored in the header * NOTE: *	We call a special function to write the file. This function attempts to *	restore file holes (blocks of zeros) into the file. When files are *	sparse this saves space, and is a LOT faster. For non sparse files *	the performance hit is small. As of this writing, no archive supports *	information on where the file holes are. * Return: *	0 ok, -1 if archive read failure. if we cannot write the entire file, *	we return a 0 but "left" is set to be the amount unwritten */#if __STDC__intrd_wrfile(ARCHD *arcn, int ofd, off_t *left)#elseintrd_wrfile(arcn, ofd, left)	ARCHD *arcn;	int ofd;	off_t *left;#endif{	register int cnt = 0;	register off_t size = arcn->sb.st_size;	register int res = 0;	register char *fnm = arcn->name;	int isem = 1;	int rem;	int sz = MINFBSZ; 	struct stat sb;	u_long crc = 0L;	/*	 * pass the blocksize of the file being written to the write routine,	 * if the size is zero, use the default MINFBSZ	 */        if (fstat(ofd, &sb) == 0) {		if (sb.st_blksize > 0)			sz = (int)sb.st_blksize;        } else                syswarn(0,errno,"Unable to obtain block size for file %s",fnm);	rem = sz;	*left = 0L;	/*	 * Copy the archive to the file the number of bytes specified. We have	 * to assume that we want to recover file holes as none of the archive	 * formats can record the location of file holes.	 */	while (size > 0L) {		cnt = bufend - bufpt;		/*		 * if we get a read error, we do not want to skip, as we may		 * miss a header, so we do not set left, but if we get a write		 * error, we do want to skip over the unprocessed data.		 */		if ((cnt <= 0) && ((cnt = buf_fill()) <= 0))			break;		cnt = MIN(cnt, size);		if ((res = file_write(ofd,bufpt,cnt,&rem,&isem,sz,fnm)) <= 0) {			*left = size;			break;		}		if (docrc) {			/*			 * update the actual crc value			 */			cnt = res;			while (--cnt >= 0)				crc += *bufpt++ & 0xff;		} else			bufpt += res;		size -= res;	}	/*	 * if the last block has a file hole (all zero), we must make sure this	 * gets updated in the file. We force the last block of zeros to be	 * written. just closing with the file offset moved foward may not put	 * a hole at the end of the file.	 */	if (isem && (arcn->sb.st_size > 0L))		file_flush(ofd, fnm, isem);	/*	 * if we failed from archive read, we do not want to skip	 */	if ((size > 0L) && (*left == 0L)) 		return(-1);	/*	 * some formats record a crc on file data. If so, then we compare the	 * calculated crc to the crc stored in the archive	 */	if (docrc && (size == 0L) && (arcn->crc != crc))		warn(1,"Actual crc does not match expected crc %s",arcn->name);	return(0);}/* * cp_file() *	copy the contents of one file to another. used during -rw phase of pax *	just as in rd_wrfile() we use a special write function to write the *	destination file so we can properly copy files with holes. */#if __STDC__voidcp_file(ARCHD *arcn, int fd1, int fd2)#elsevoidcp_file(arcn, fd1, fd2)	ARCHD *arcn;	int fd1;	int fd2;#endif{	register int cnt;	register off_t cpcnt = 0L;	register int res = 0;	register char *fnm = arcn->name;	register int no_hole = 0;	int isem = 1;	int rem;	int sz = MINFBSZ;	struct stat sb;	/*	 * check for holes in the source file. If none, we will use regular	 * write instead of file write.	 */	 if (((off_t)(arcn->sb.st_blocks * BLKMULT)) >= arcn->sb.st_size)		++no_hole;	/*	 * pass the blocksize of the file being written to the write routine,	 * if the size is zero, use the default MINFBSZ	 */        if (fstat(fd2, &sb) == 0) {		if (sb.st_blksize > 0)			sz = sb.st_blksize;        } else                syswarn(0,errno,"Unable to obtain block size for file %s",fnm);	rem = sz;	/*	 * read the source file and copy to destination file until EOF	 */	for(;;) {		if ((cnt = read(fd1, buf, blksz)) <= 0)			break;		if (no_hole)			res = write(fd2, buf, cnt);		else			res = file_write(fd2, buf, cnt, &rem, &isem, sz, fnm);		if (res != cnt)			break;		cpcnt += cnt;	}	/*	 * check to make sure the copy is valid.	 */	if (res < 0)		syswarn(1, errno, "Failed write during copy of %s to %s",			arcn->org_name, arcn->name);	else if (cpcnt != arcn->sb.st_size)		warn(1, "File %s changed size during copy to %s",			arcn->org_name, arcn->name);	else if (fstat(fd1, &sb) < 0)		syswarn(1, errno, "Failed stat of %s", arcn->org_name);	else if (arcn->sb.st_mtime != sb.st_mtime)		warn(1, "File %s was modified during copy to %s",			arcn->org_name, arcn->name);	/*	 * if the last block has a file hole (all zero), we must make sure this	 * gets updated in the file. We force the last block of zeros to be	 * written. just closing with the file offset moved foward may not put	 * a hole at the end of the file.	 */	if (!no_hole && isem && (arcn->sb.st_size > 0L))		file_flush(fd2, fnm, isem);	return;}/* * buf_fill() *	fill the read buffer with the next record (or what we can get) from *	the archive volume. * Return: *	Number of bytes of data in the read buffer, -1 for read error, and *	0 when finished (user specified termination in ar_next()). */#if __STDC__intbuf_fill(void)#elseintbuf_fill()#endif{	register int cnt;	static int fini = 0;	if (fini)		return(0);	for(;;) {		/*		 * try to fill the buffer. on error the next archive volume is		 * opened and we try again.		 */		if ((cnt = ar_read(buf, blksz)) > 0) {			bufpt = buf;			bufend = buf + cnt;			rdcnt += cnt;			return(cnt);		}		/*		 * errors require resync, EOF goes to next archive		 */		if (cnt < 0)			break;		if (ar_next() < 0) {			fini = 1;			return(0);		}		rdcnt = 0;	}	exit_val = 1;	return(-1);}/* * buf_flush() *	force the write buffer to the archive. We are passed the number of *	bytes in the buffer at the point of the flush. When we change archives *	the record size might change. (either larger or smaller). * Return: *	0 if all is ok, -1 when a write error occurs. */#if __STDC__intbuf_flush(register int bufcnt)#elseintbuf_flush(bufcnt)	register int bufcnt;#endif{	register int cnt;	register int push = 0;	register int totcnt = 0;	/*	 * if we have reached the user specified byte count for each archive	 * volume, prompt for the next volume. (The non-standrad -R flag).	 * NOTE: If the wrlimit is smaller than wrcnt, we will always write	 * at least one record. We always round limit UP to next blocksize.	 */	if ((wrlimit > 0) && (wrcnt > wrlimit)) {		warn(0, "User specified archive volume byte limit reached.");		if (ar_next() < 0) {			wrcnt = 0;			exit_val = 1;			return(-1);		}		wrcnt = 0;		/*		 * The new archive volume might have changed the size of the		 * write blocksize. if so we figure out if we need to write		 * (one or more times), or if there is now free space left in		 * the buffer (it is no longer full). bufcnt has the number of		 * bytes in the buffer, (the blocksize, at the point we were		 * CALLED). Push has the amount of "extra" data in the buffer		 * if the block size has shrunk from a volume change.		 */		bufend = buf + blksz;		if (blksz > bufcnt)			return(0);		if (blksz < bufcnt)			push = bufcnt - blksz;	}	/*	 * We have enough data to write at least one archive block	 */	for (;;) {		/*		 * write a block and check if it all went out ok		 */		cnt = ar_write(buf, blksz); 		if (cnt == blksz) {			/*			 * the write went ok			 */			wrcnt += cnt;			totcnt += cnt;			if (push > 0) {				/* we have extra data to push to the front.				 * check for more than 1 block of push, and if				 * so we loop back to write again				 */				bcopy(bufend, buf, push);				bufpt = buf + push;				if (push >= blksz) {					push -= blksz;					continue;				}			} else				bufpt = buf;			return(totcnt);		} else if (cnt > 0) {			/*			 * Oh drat we got a partial write!			 * if format doesnt care about alignment let it go,			 * we warned the user in ar_write().... but this means			 * the last record on this volume violates pax spec....			 */			totcnt += cnt;			wrcnt += cnt;			bufpt = buf + cnt;			cnt = bufcnt - cnt;			bcopy(bufpt, buf, cnt);			bufpt = buf + cnt;			if (!frmt->blkalgn || ((cnt % frmt->blkalgn) == 0))				return(totcnt);			break;		}		/*		 * All done, go to next archive		 */		wrcnt = 0;		if (ar_next() < 0)			break;		/*		 * The new archive volume might also have changed the block		 * size. if so, figure out if we have too much or too little		 * data for using the new block size		 */		bufend = buf + blksz;		if (blksz > bufcnt)			return(0);		if (blksz < bufcnt)			push = bufcnt - blksz;	}	/*	 * write failed, stop pax. we must not create a bad archive!	 */	exit_val = 1;	return(-1);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -