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

📄 cpio.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 3 页
字号:
		/*		 * no file data for the caller to process, the file data has		 * the size of the link		 */		arcn->pad = 0L;		if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,		    sizeof(hd->c_filesize), HEX))			goto out;		break;	default:		/*		 * no file data for the caller to process		 */		arcn->pad = 0L;		if (ul_asc((u_long)0L, hd->c_filesize, sizeof(hd->c_filesize),		    HEX))			goto out;		break;	}	/*	 * set the other fields in the header	 */	if (ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),		HEX) ||	    ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),		HEX) ||	    ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),		HEX) ||	    ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),    		HEX) ||	    ul_asc((u_long)arcn->sb.st_mtime, hd->c_mtime, sizeof(hd->c_mtime),    		HEX) ||	    ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),    		HEX) ||	    ul_asc((u_long)MAJOR(arcn->sb.st_dev),hd->c_maj, sizeof(hd->c_maj),		HEX) ||	    ul_asc((u_long)MINOR(arcn->sb.st_dev),hd->c_min, sizeof(hd->c_min),		HEX) ||	    ul_asc((u_long)MAJOR(arcn->sb.st_rdev),hd->c_rmaj,sizeof(hd->c_maj),		HEX) ||	    ul_asc((u_long)MINOR(arcn->sb.st_rdev),hd->c_rmin,sizeof(hd->c_min),		HEX) ||	    ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), HEX))		goto out;	/*	 * write the header, the file name and padding as required.	 */	if ((wr_rdbuf(hdblk, (int)sizeof(HD_VCPIO)) < 0) ||	    (wr_rdbuf(arcn->name, (int)nsz) < 0)  ||	    (wr_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)) {		warn(1,"Could not write sv4cpio header for %s",arcn->org_name);		return(-1);	}	/*	 * if we have file data, tell the caller we are done, copy the file	 */	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||	    (arcn->type == PAX_HRG))		return(0);	/*	 * if we are not a link, tell the caller we are done, go to next file	 */	if (arcn->type != PAX_SLK)		return(1);	/*	 * write the link name, tell the caller we are done.	 */	if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||	    (wr_skip((off_t)(VCPIO_PAD(arcn->ln_nlen))) < 0)) {		warn(1,"Could not write sv4cpio link name for %s",		    arcn->org_name);		return(-1);	}	return(1);    out:	/*	 * header field is out of range	 */	warn(1,"Sv4cpio header field is too small for file %s",arcn->org_name);	return(1);}/* * Routines common to the old binary header cpio *//* * bcpio_id() *      determine if a block given to us is a old binary cpio header *	(with/without header byte swapping) * Return: *      0 if a valid header, -1 otherwise */#if __STDC__intbcpio_id(char *blk, int size)#elseintbcpio_id(blk, size)	char *blk;	int size;#endif{	if (size < sizeof(HD_BCPIO))		return(-1);	/*	 * check both normal and byte swapped magic cookies	 */	if (((u_short)SHRT_EXT(blk)) == MAGIC)		return(0);	if (((u_short)RSHRT_EXT(blk)) == MAGIC) {		if (!swp_head)			++swp_head;		return(0);	}	return(-1);}/* * bcpio_rd() *	determine if a buffer is a old binary archive entry. (it may have byte *	swapped header) convert and store the values in the ARCHD parameter. *	This is a very old header format and should not really be used. * Return: *	0 if a valid header, -1 otherwise. */#if __STDC__intbcpio_rd(register ARCHD *arcn, register char *buf)#elseintbcpio_rd(arcn, buf)	register ARCHD *arcn;	register char *buf;#endif{	register HD_BCPIO *hd;	register int nsz;	/*	 * check the header	 */	if (bcpio_id(buf, sizeof(HD_BCPIO)) < 0)		return(-1);	arcn->pad = 0L;	hd = (HD_BCPIO *)buf;	if (swp_head) {		/*		 * header has swapped bytes on 16 bit boundries		 */		arcn->sb.st_dev = (dev_t)(RSHRT_EXT(hd->h_dev));		arcn->sb.st_ino = (ino_t)(RSHRT_EXT(hd->h_ino));		arcn->sb.st_mode = (mode_t)(RSHRT_EXT(hd->h_mode));		arcn->sb.st_uid = (uid_t)(RSHRT_EXT(hd->h_uid));		arcn->sb.st_gid = (gid_t)(RSHRT_EXT(hd->h_gid));		arcn->sb.st_nlink = (nlink_t)(RSHRT_EXT(hd->h_nlink));		arcn->sb.st_rdev = (dev_t)(RSHRT_EXT(hd->h_rdev));		arcn->sb.st_mtime = (time_t)(RSHRT_EXT(hd->h_mtime_1));		arcn->sb.st_mtime =  (arcn->sb.st_mtime << 16) |			((time_t)(RSHRT_EXT(hd->h_mtime_2)));		arcn->sb.st_size = (off_t)(RSHRT_EXT(hd->h_filesize_1));		arcn->sb.st_size = (arcn->sb.st_size << 16) |			((off_t)(RSHRT_EXT(hd->h_filesize_2)));		nsz = (int)(RSHRT_EXT(hd->h_namesize));	} else {		arcn->sb.st_dev = (dev_t)(SHRT_EXT(hd->h_dev));		arcn->sb.st_ino = (ino_t)(SHRT_EXT(hd->h_ino));		arcn->sb.st_mode = (mode_t)(SHRT_EXT(hd->h_mode));		arcn->sb.st_uid = (uid_t)(SHRT_EXT(hd->h_uid));		arcn->sb.st_gid = (gid_t)(SHRT_EXT(hd->h_gid));		arcn->sb.st_nlink = (nlink_t)(SHRT_EXT(hd->h_nlink));		arcn->sb.st_rdev = (dev_t)(SHRT_EXT(hd->h_rdev));		arcn->sb.st_mtime = (time_t)(SHRT_EXT(hd->h_mtime_1));		arcn->sb.st_mtime =  (arcn->sb.st_mtime << 16) |			((time_t)(SHRT_EXT(hd->h_mtime_2)));		arcn->sb.st_size = (off_t)(SHRT_EXT(hd->h_filesize_1));		arcn->sb.st_size = (arcn->sb.st_size << 16) |			((off_t)(SHRT_EXT(hd->h_filesize_2)));		nsz = (int)(SHRT_EXT(hd->h_namesize));	}	arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;	/*	 * check the file name size, if bogus give up. otherwise read the file	 * name	 */	if (nsz < 2)		return(-1);	arcn->nlen = nsz - 1;	if (rd_nm(arcn, nsz) < 0)		return(-1);	/*	 * header + file name are aligned to 2 byte boundries, skip if needed	 */	if (rd_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)		return(-1);	/*	 * if not a link (or a file with no data), calculate pad size (for	 * padding which follows the file data), clear the link name and return	 */	if (((arcn->sb.st_mode & C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)){		/*		 * we have a valid header (not a link)		 */		arcn->ln_nlen = 0;		arcn->ln_name[0] = '\0';		arcn->pad = BCPIO_PAD(arcn->sb.st_size);		return(com_rd(arcn));	}	if ((rd_ln_nm(arcn) < 0) ||	    (rd_skip((off_t)(BCPIO_PAD(arcn->sb.st_size))) < 0))		return(-1);	/*	 * we have a valid header (with a link)	 */	return(com_rd(arcn));}/* * bcpio_endrd() *      no cleanup needed here, just return size of the trailer (for append) * Return: *      size of trailer header in this format */#if __STDC__off_tbcpio_endrd(void)#elseoff_tbcpio_endrd()#endif{	return((off_t)(sizeof(HD_BCPIO) + sizeof(TRAILER) +		(BCPIO_PAD(sizeof(HD_BCPIO) + sizeof(TRAILER)))));}/* * bcpio_wr() *	copy the data in the ARCHD to buffer in old binary cpio format *	There is a real chance of field overflow with this critter. So we *	always check the conversion is ok. nobody in his their right mind *	should write an achive in this format... * Return *      0 if file has data to be written after the header, 1 if file has NO *	data to write after the header, -1 if archive write failed */#if __STDC__intbcpio_wr(register ARCHD *arcn)#elseintbcpio_wr(arcn)	register ARCHD *arcn;#endif{	register HD_BCPIO *hd;	register int nsz;	char hdblk[sizeof(HD_BCPIO)];	off_t t_offt;	int t_int;	time_t t_timet;	/*	 * check and repair truncated device and inode fields in the cpio	 * header	 */	if (map_dev(arcn, (u_long)BCPIO_MASK, (u_long)BCPIO_MASK) < 0)		return(-1);	if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))		arcn->sb.st_rdev = 0;	hd = (HD_BCPIO *)hdblk;	switch(arcn->type) {	case PAX_CTG:	case PAX_REG:	case PAX_HRG:		/*		 * caller will copy file data to the archive. tell him how		 * much to pad.		 */		arcn->pad = BCPIO_PAD(arcn->sb.st_size);		hd->h_filesize_1[0] = CHR_WR_0(arcn->sb.st_size);		hd->h_filesize_1[1] = CHR_WR_1(arcn->sb.st_size);		hd->h_filesize_2[0] = CHR_WR_2(arcn->sb.st_size);		hd->h_filesize_2[1] = CHR_WR_3(arcn->sb.st_size);		t_offt = (off_t)(SHRT_EXT(hd->h_filesize_1));		t_offt = (t_offt<<16) | ((off_t)(SHRT_EXT(hd->h_filesize_2)));		if (arcn->sb.st_size != t_offt) {			warn(1,"File is too large for bcpio format %s",			    arcn->org_name);			return(1);		}		break;	case PAX_SLK:		/*		 * no file data for the caller to process, the file data has		 * the size of the link		 */		arcn->pad = 0L;		hd->h_filesize_1[0] = CHR_WR_0(arcn->ln_nlen);		hd->h_filesize_1[1] = CHR_WR_1(arcn->ln_nlen);		hd->h_filesize_2[0] = CHR_WR_2(arcn->ln_nlen);		hd->h_filesize_2[1] = CHR_WR_3(arcn->ln_nlen);		t_int = (int)(SHRT_EXT(hd->h_filesize_1));		t_int = (t_int << 16) | ((int)(SHRT_EXT(hd->h_filesize_2)));		if (arcn->ln_nlen != t_int)			goto out;		break;	default:		/*		 * no file data for the caller to process		 */		arcn->pad = 0L;		hd->h_filesize_1[0] = (char)0;		hd->h_filesize_1[1] = (char)0;		hd->h_filesize_2[0] = (char)0;		hd->h_filesize_2[1] = (char)0;		break;	}	/*	 * build up the rest of the fields	 */	hd->h_magic[0] = CHR_WR_2(MAGIC);	hd->h_magic[1] = CHR_WR_3(MAGIC);	hd->h_dev[0] = CHR_WR_2(arcn->sb.st_dev);	hd->h_dev[1] = CHR_WR_3(arcn->sb.st_dev);	if (arcn->sb.st_dev != (dev_t)(SHRT_EXT(hd->h_dev)))		goto out;	hd->h_ino[0] = CHR_WR_2(arcn->sb.st_ino);	hd->h_ino[1] = CHR_WR_3(arcn->sb.st_ino);	if (arcn->sb.st_ino != (ino_t)(SHRT_EXT(hd->h_ino)))		goto out;	hd->h_mode[0] = CHR_WR_2(arcn->sb.st_mode);	hd->h_mode[1] = CHR_WR_3(arcn->sb.st_mode);	if (arcn->sb.st_mode != (mode_t)(SHRT_EXT(hd->h_mode)))		goto out;	hd->h_uid[0] = CHR_WR_2(arcn->sb.st_uid);	hd->h_uid[1] = CHR_WR_3(arcn->sb.st_uid);	if (arcn->sb.st_uid != (uid_t)(SHRT_EXT(hd->h_uid)))		goto out;	hd->h_gid[0] = CHR_WR_2(arcn->sb.st_gid);	hd->h_gid[1] = CHR_WR_3(arcn->sb.st_gid);	if (arcn->sb.st_gid != (gid_t)(SHRT_EXT(hd->h_gid)))		goto out;	hd->h_nlink[0] = CHR_WR_2(arcn->sb.st_nlink);	hd->h_nlink[1] = CHR_WR_3(arcn->sb.st_nlink);	if (arcn->sb.st_nlink != (nlink_t)(SHRT_EXT(hd->h_nlink)))		goto out;	hd->h_rdev[0] = CHR_WR_2(arcn->sb.st_rdev);	hd->h_rdev[1] = CHR_WR_3(arcn->sb.st_rdev);	if (arcn->sb.st_rdev != (dev_t)(SHRT_EXT(hd->h_rdev)))		goto out;	hd->h_mtime_1[0] = CHR_WR_0(arcn->sb.st_mtime);	hd->h_mtime_1[1] = CHR_WR_1(arcn->sb.st_mtime);	hd->h_mtime_2[0] = CHR_WR_2(arcn->sb.st_mtime);	hd->h_mtime_2[1] = CHR_WR_3(arcn->sb.st_mtime);	t_timet = (time_t)(SHRT_EXT(hd->h_mtime_1));	t_timet =  (t_timet << 16) | ((time_t)(SHRT_EXT(hd->h_mtime_2)));	if (arcn->sb.st_mtime != t_timet)		goto out;	nsz = arcn->nlen + 1;	hd->h_namesize[0] = CHR_WR_2(nsz);	hd->h_namesize[1] = CHR_WR_3(nsz);	if (nsz != (int)(SHRT_EXT(hd->h_namesize)))		goto out;	/*	 * write the header, the file name and padding as required.	 */	if ((wr_rdbuf(hdblk, (int)sizeof(HD_BCPIO)) < 0) ||	    (wr_rdbuf(arcn->name, nsz) < 0) ||	    (wr_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)) {		warn(1, "Could not write bcpio header for %s", arcn->org_name);		return(-1);	}	/*	 * if we have file data, tell the caller we are done	 */	if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||	    (arcn->type == PAX_HRG))		return(0);	/*	 * if we are not a link, tell the caller we are done, go to next file	 */	if (arcn->type != PAX_SLK)		return(1);	/*	 * write the link name, tell the caller we are done.	 */	if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||	    (wr_skip((off_t)(BCPIO_PAD(arcn->ln_nlen))) < 0)) {		warn(1,"Could not write bcpio link name for %s",arcn->org_name);		return(-1);	}	return(1);    out:	/*	 * header field is out of range	 */	warn(1,"Bcpio header field is too small for file %s", arcn->org_name);	return(1);}

⌨️ 快捷键说明

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