2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)cpio.c 8.1 (Berkeley) 5/31/93";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/types.h>
54 static int rd_nm(ARCHD *, int);
55 static int rd_ln_nm(ARCHD *);
56 static int com_rd(ARCHD *);
59 * Routines which support the different cpio versions
62 static int swp_head; /* binary cpio header byte swap */
65 * Routines common to all versions of cpio
70 * Fire up the hard link detection code
72 * 0 if ok -1 otherwise (the return values of lnk_start())
83 * Called to determine if a header block is a valid trailer. We are
84 * passed the block, the in_sync flag (which tells us we are in resync
85 * mode; looking for a valid header), and cnt (which starts at zero)
86 * which is used to count the number of empty blocks we have seen so far.
88 * 0 if a valid trailer, -1 if not a valid trailer,
92 cpio_trail(ARCHD *arcn)
95 * look for trailer id in file we are about to process
97 if ((strcmp(arcn->name, TRAILER) == 0) && (arcn->sb.st_size == 0))
104 * operations common to all cpio read functions.
114 arcn->org_name = arcn->name;
115 switch(arcn->sb.st_mode & C_IFMT) {
117 arcn->type = PAX_FIF;
120 arcn->type = PAX_DIR;
123 arcn->type = PAX_BLK;
126 arcn->type = PAX_CHR;
129 arcn->type = PAX_SLK;
132 arcn->type = PAX_SCK;
138 * we have file data, set up skip (pad is set in the format
141 arcn->sb.st_mode = (arcn->sb.st_mode & 0xfff) | C_ISREG;
142 arcn->type = PAX_REG;
143 arcn->skip = arcn->sb.st_size;
146 if (chk_lnk(arcn) < 0)
153 * write the special file with the name trailer in the proper format
155 * result of the write of the trailer from the cpio specific write func
164 * create a trailer request and call the proper format write function
166 memset(&last, 0, sizeof(last));
167 last.nlen = sizeof(TRAILER) - 1;
169 last.sb.st_nlink = 1;
170 (void)strcpy(last.name, TRAILER);
171 return((*frmt->wr)(&last));
176 * read in the file name which follows the cpio header
178 * 0 if ok, -1 otherwise
182 rd_nm(ARCHD *arcn, int nsz)
185 * do not even try bogus values
187 if ((nsz == 0) || (nsz > (int)sizeof(arcn->name))) {
188 paxwarn(1, "Cpio file name length %d is out of range", nsz);
193 * read the name and make sure it is not empty and is \0 terminated
195 if ((rd_wrbuf(arcn->name,nsz) != nsz) || (arcn->name[nsz-1] != '\0') ||
196 (arcn->name[0] == '\0')) {
197 paxwarn(1, "Cpio file name in header is corrupted");
205 * read in the link name for a file with links. The link name is stored
206 * like file data (and is NOT \0 terminated!)
208 * 0 if ok, -1 otherwise
212 rd_ln_nm(ARCHD *arcn)
215 * check the length specified for bogus values
217 if ((arcn->sb.st_size == 0) ||
218 ((size_t)arcn->sb.st_size >= sizeof(arcn->ln_name))) {
220 paxwarn(1, "Cpio link name length is invalid: %lu",
223 paxwarn(1, "Cpio link name length is invalid: %ju",
224 (uintmax_t)arcn->sb.st_size);
230 * read in the link name and \0 terminate it
232 if (rd_wrbuf(arcn->ln_name, (int)arcn->sb.st_size) !=
233 (int)arcn->sb.st_size) {
234 paxwarn(1, "Cpio link name read error");
237 arcn->ln_nlen = arcn->sb.st_size;
238 arcn->ln_name[arcn->ln_nlen] = '\0';
241 * watch out for those empty link names
243 if (arcn->ln_name[0] == '\0') {
244 paxwarn(1, "Cpio link name is corrupt");
251 * Routines common to the extended byte oriented cpio format
256 * determine if a block given to us is a valid extended byte oriented
259 * 0 if a valid header, -1 otherwise
263 cpio_id(char *blk, int size)
265 if ((size < (int)sizeof(HD_CPIO)) ||
266 (strncmp(blk, AMAGIC, sizeof(AMAGIC) - 1) != 0))
273 * determine if a buffer is a byte oriented extended cpio archive entry.
274 * convert and store the values in the ARCHD parameter.
276 * 0 if a valid header, -1 otherwise.
280 cpio_rd(ARCHD *arcn, char *buf)
286 * check that this is a valid header, if not return -1
288 if (cpio_id(buf, sizeof(HD_CPIO)) < 0)
293 * byte oriented cpio (posix) does not have padding! extract the octal
294 * ascii fields from the header
297 arcn->sb.st_dev = (dev_t)asc_ul(hd->c_dev, sizeof(hd->c_dev), OCT);
298 arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), OCT);
299 arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), OCT);
300 arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), OCT);
301 arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), OCT);
302 arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink),
304 arcn->sb.st_rdev = (dev_t)asc_ul(hd->c_rdev, sizeof(hd->c_rdev), OCT);
306 arcn->sb.st_mtime = (time_t)asc_ul(hd->c_mtime, sizeof(hd->c_mtime),
309 arcn->sb.st_mtime = (time_t)asc_uqd(hd->c_mtime, sizeof(hd->c_mtime),
312 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
314 arcn->sb.st_size = (off_t)asc_ul(hd->c_filesize,sizeof(hd->c_filesize),
317 arcn->sb.st_size = (off_t)asc_uqd(hd->c_filesize,sizeof(hd->c_filesize),
322 * check name size and if valid, read in the name of this entry (name
323 * follows header in the archive)
325 if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),OCT)) < 2)
327 arcn->nlen = nsz - 1;
328 if (rd_nm(arcn, nsz) < 0)
331 if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
333 * no link name to read for this file
336 arcn->ln_name[0] = '\0';
337 return(com_rd(arcn));
341 * check link name size and read in the link name. Link names are
342 * stored like file data.
344 if (rd_ln_nm(arcn) < 0)
348 * we have a valid header (with a link)
350 return(com_rd(arcn));
355 * no cleanup needed here, just return size of the trailer (for append)
357 * size of trailer header in this format
363 return((off_t)(sizeof(HD_CPIO) + sizeof(TRAILER)));
368 * start up the device mapping table
370 * 0 if ok, -1 otherwise (what dev_start() returns)
381 * copy the data in the ARCHD to buffer in extended byte oriented cpio
384 * 0 if file has data to be written after the header, 1 if file has NO
385 * data to write after the header, -1 if archive write failed
396 * check and repair truncated device and inode fields in the header
398 if (map_dev(arcn, (u_long)CPIO_MASK, (u_long)CPIO_MASK) < 0)
402 nsz = arcn->nlen + 1;
404 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
405 arcn->sb.st_rdev = 0;
412 * set data size for file data
415 if (ul_asc((u_long)arcn->sb.st_size, hd->c_filesize,
416 sizeof(hd->c_filesize), OCT)) {
418 if (uqd_asc((u_quad_t)arcn->sb.st_size, hd->c_filesize,
419 sizeof(hd->c_filesize), OCT)) {
421 paxwarn(1,"File is too large for cpio format %s",
428 * set data size to hold link name
430 if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,
431 sizeof(hd->c_filesize), OCT))
436 * all other file types have no file data
438 if (ul_asc((u_long)0, hd->c_filesize, sizeof(hd->c_filesize),
445 * copy the values to the header using octal ascii
447 if (ul_asc((u_long)MAGIC, hd->c_magic, sizeof(hd->c_magic), OCT) ||
448 ul_asc((u_long)arcn->sb.st_dev, hd->c_dev, sizeof(hd->c_dev),
450 ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),
452 ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
454 ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),
456 ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),
458 ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
460 ul_asc((u_long)arcn->sb.st_rdev, hd->c_rdev, sizeof(hd->c_rdev),
462 ul_asc((u_long)arcn->sb.st_mtime,hd->c_mtime,sizeof(hd->c_mtime),
464 ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), OCT))
468 * write the file name to the archive
470 if ((wr_rdbuf((char *)&hdblk, (int)sizeof(HD_CPIO)) < 0) ||
471 (wr_rdbuf(arcn->name, nsz) < 0)) {
472 paxwarn(1, "Unable to write cpio header for %s", arcn->org_name);
477 * if this file has data, we are done. The caller will write the file
478 * data, if we are link tell caller we are done, go to next file
480 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
481 (arcn->type == PAX_HRG))
483 if (arcn->type != PAX_SLK)
487 * write the link name to the archive, tell the caller to go to the
488 * next file as we are done.
490 if (wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) {
491 paxwarn(1,"Unable to write cpio link name for %s",arcn->org_name);
498 * header field is out of range
500 paxwarn(1, "Cpio header field is too small to store file %s",
506 * Routines common to the system VR4 version of cpio (with/without file CRC)
511 * determine if a block given to us is a valid system VR4 cpio header
512 * WITHOUT crc. WATCH it the magic cookies are in OCTAL, the header
515 * 0 if a valid header, -1 otherwise
519 vcpio_id(char *blk, int size)
521 if ((size < (int)sizeof(HD_VCPIO)) ||
522 (strncmp(blk, AVMAGIC, sizeof(AVMAGIC) - 1) != 0))
529 * determine if a block given to us is a valid system VR4 cpio header
530 * WITH crc. WATCH it the magic cookies are in OCTAL the header uses HEX
532 * 0 if a valid header, -1 otherwise
536 crc_id(char *blk, int size)
538 if ((size < (int)sizeof(HD_VCPIO)) ||
539 (strncmp(blk, AVCMAGIC, (int)sizeof(AVCMAGIC) - 1) != 0))
546 w set file data CRC calculations. Fire up the hard link detection code
548 * 0 if ok -1 otherwise (the return values of lnk_start())
560 * determine if a buffer is a system VR4 archive entry. (with/without CRC)
561 * convert and store the values in the ARCHD parameter.
563 * 0 if a valid header, -1 otherwise.
567 vcpio_rd(ARCHD *arcn, char *buf)
575 * during the id phase it was determined if we were using CRC, use the
579 if (crc_id(buf, sizeof(HD_VCPIO)) < 0)
582 if (vcpio_id(buf, sizeof(HD_VCPIO)) < 0)
586 hd = (HD_VCPIO *)buf;
590 * extract the hex ascii fields from the header
592 arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), HEX);
593 arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), HEX);
594 arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), HEX);
595 arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), HEX);
597 arcn->sb.st_mtime = (time_t)asc_ul(hd->c_mtime,sizeof(hd->c_mtime),HEX);
599 arcn->sb.st_mtime = (time_t)asc_uqd(hd->c_mtime,sizeof(hd->c_mtime),HEX);
601 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
603 arcn->sb.st_size = (off_t)asc_ul(hd->c_filesize,
604 sizeof(hd->c_filesize), HEX);
606 arcn->sb.st_size = (off_t)asc_uqd(hd->c_filesize,
607 sizeof(hd->c_filesize), HEX);
609 arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink),
611 devmajor = (dev_t)asc_ul(hd->c_maj, sizeof(hd->c_maj), HEX);
612 devminor = (dev_t)asc_ul(hd->c_min, sizeof(hd->c_min), HEX);
613 arcn->sb.st_dev = TODEV(devmajor, devminor);
614 devmajor = (dev_t)asc_ul(hd->c_rmaj, sizeof(hd->c_maj), HEX);
615 devminor = (dev_t)asc_ul(hd->c_rmin, sizeof(hd->c_min), HEX);
616 arcn->sb.st_rdev = TODEV(devmajor, devminor);
617 arcn->crc = asc_ul(hd->c_chksum, sizeof(hd->c_chksum), HEX);
620 * check the length of the file name, if ok read it in, return -1 if
623 if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),HEX)) < 2)
625 arcn->nlen = nsz - 1;
626 if (rd_nm(arcn, nsz) < 0)
630 * skip padding. header + filename is aligned to 4 byte boundries
632 if (rd_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)
636 * if not a link (or a file with no data), calculate pad size (for
637 * padding which follows the file data), clear the link name and return
639 if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
641 * we have a valid header (not a link)
644 arcn->ln_name[0] = '\0';
645 arcn->pad = VCPIO_PAD(arcn->sb.st_size);
646 return(com_rd(arcn));
650 * read in the link name and skip over the padding
652 if ((rd_ln_nm(arcn) < 0) ||
653 (rd_skip((off_t)(VCPIO_PAD(arcn->sb.st_size))) < 0))
657 * we have a valid header (with a link)
659 return(com_rd(arcn));
664 * no cleanup needed here, just return size of the trailer (for append)
666 * size of trailer header in this format
672 return((off_t)(sizeof(HD_VCPIO) + sizeof(TRAILER) +
673 (VCPIO_PAD(sizeof(HD_VCPIO) + sizeof(TRAILER)))));
678 * start up the device mapping table, enable crc file calculation
680 * 0 if ok, -1 otherwise (what dev_start() returns)
692 * copy the data in the ARCHD to buffer in system VR4 cpio
693 * (with/without crc) format.
695 * 0 if file has data to be written after the header, 1 if file has
696 * NO data to write after the header, -1 if archive write failed
700 vcpio_wr(ARCHD *arcn)
707 * check and repair truncated device and inode fields in the cpio
710 if (map_dev(arcn, (u_long)VCPIO_MASK, (u_long)VCPIO_MASK) < 0)
712 nsz = arcn->nlen + 1;
714 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
715 arcn->sb.st_rdev = 0;
718 * add the proper magic value depending whether we were asked for
719 * file data crc's, and the crc if needed.
722 if (ul_asc((u_long)VCMAGIC, hd->c_magic, sizeof(hd->c_magic),
724 ul_asc((u_long)arcn->crc,hd->c_chksum,sizeof(hd->c_chksum),
728 if (ul_asc((u_long)VMAGIC, hd->c_magic, sizeof(hd->c_magic),
730 ul_asc((u_long)0L, hd->c_chksum, sizeof(hd->c_chksum),HEX))
739 * caller will copy file data to the archive. tell him how
742 arcn->pad = VCPIO_PAD(arcn->sb.st_size);
744 if (ul_asc((u_long)arcn->sb.st_size, hd->c_filesize,
745 sizeof(hd->c_filesize), HEX)) {
747 if (uqd_asc((u_quad_t)arcn->sb.st_size, hd->c_filesize,
748 sizeof(hd->c_filesize), HEX)) {
750 paxwarn(1,"File is too large for sv4cpio format %s",
757 * no file data for the caller to process, the file data has
758 * the size of the link
761 if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,
762 sizeof(hd->c_filesize), HEX))
767 * no file data for the caller to process
770 if (ul_asc((u_long)0L, hd->c_filesize, sizeof(hd->c_filesize),
777 * set the other fields in the header
779 if (ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),
781 ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
783 ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),
785 ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),
787 ul_asc((u_long)arcn->sb.st_mtime, hd->c_mtime, sizeof(hd->c_mtime),
789 ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
791 ul_asc((u_long)MAJOR(arcn->sb.st_dev),hd->c_maj, sizeof(hd->c_maj),
793 ul_asc((u_long)MINOR(arcn->sb.st_dev),hd->c_min, sizeof(hd->c_min),
795 ul_asc((u_long)MAJOR(arcn->sb.st_rdev),hd->c_rmaj,sizeof(hd->c_maj),
797 ul_asc((u_long)MINOR(arcn->sb.st_rdev),hd->c_rmin,sizeof(hd->c_min),
799 ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), HEX))
803 * write the header, the file name and padding as required.
805 if ((wr_rdbuf((char *)&hdblk, (int)sizeof(HD_VCPIO)) < 0) ||
806 (wr_rdbuf(arcn->name, (int)nsz) < 0) ||
807 (wr_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)) {
808 paxwarn(1,"Could not write sv4cpio header for %s",arcn->org_name);
813 * if we have file data, tell the caller we are done, copy the file
815 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
816 (arcn->type == PAX_HRG))
820 * if we are not a link, tell the caller we are done, go to next file
822 if (arcn->type != PAX_SLK)
826 * write the link name, tell the caller we are done.
828 if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
829 (wr_skip((off_t)(VCPIO_PAD(arcn->ln_nlen))) < 0)) {
830 paxwarn(1,"Could not write sv4cpio link name for %s",
838 * header field is out of range
840 paxwarn(1,"Sv4cpio header field is too small for file %s",arcn->org_name);
845 * Routines common to the old binary header cpio
850 * determine if a block given to us is an old binary cpio header
851 * (with/without header byte swapping)
853 * 0 if a valid header, -1 otherwise
857 bcpio_id(char *blk, int size)
859 if (size < (int)sizeof(HD_BCPIO))
863 * check both normal and byte swapped magic cookies
865 if (((u_short)SHRT_EXT(blk)) == MAGIC)
867 if (((u_short)RSHRT_EXT(blk)) == MAGIC) {
877 * determine if a buffer is an old binary archive entry. (It may have byte
878 * swapped header) convert and store the values in the ARCHD parameter.
879 * This is a very old header format and should not really be used.
881 * 0 if a valid header, -1 otherwise.
885 bcpio_rd(ARCHD *arcn, char *buf)
893 if (bcpio_id(buf, sizeof(HD_BCPIO)) < 0)
897 hd = (HD_BCPIO *)buf;
900 * header has swapped bytes on 16 bit boundaries
902 arcn->sb.st_dev = (dev_t)(RSHRT_EXT(hd->h_dev));
903 arcn->sb.st_ino = (ino_t)(RSHRT_EXT(hd->h_ino));
904 arcn->sb.st_mode = (mode_t)(RSHRT_EXT(hd->h_mode));
905 arcn->sb.st_uid = (uid_t)(RSHRT_EXT(hd->h_uid));
906 arcn->sb.st_gid = (gid_t)(RSHRT_EXT(hd->h_gid));
907 arcn->sb.st_nlink = (nlink_t)(RSHRT_EXT(hd->h_nlink));
908 arcn->sb.st_rdev = (dev_t)(RSHRT_EXT(hd->h_rdev));
909 arcn->sb.st_mtime = (time_t)(RSHRT_EXT(hd->h_mtime_1));
910 arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) |
911 ((time_t)(RSHRT_EXT(hd->h_mtime_2)));
912 arcn->sb.st_size = (off_t)(RSHRT_EXT(hd->h_filesize_1));
913 arcn->sb.st_size = (arcn->sb.st_size << 16) |
914 ((off_t)(RSHRT_EXT(hd->h_filesize_2)));
915 nsz = (int)(RSHRT_EXT(hd->h_namesize));
917 arcn->sb.st_dev = (dev_t)(SHRT_EXT(hd->h_dev));
918 arcn->sb.st_ino = (ino_t)(SHRT_EXT(hd->h_ino));
919 arcn->sb.st_mode = (mode_t)(SHRT_EXT(hd->h_mode));
920 arcn->sb.st_uid = (uid_t)(SHRT_EXT(hd->h_uid));
921 arcn->sb.st_gid = (gid_t)(SHRT_EXT(hd->h_gid));
922 arcn->sb.st_nlink = (nlink_t)(SHRT_EXT(hd->h_nlink));
923 arcn->sb.st_rdev = (dev_t)(SHRT_EXT(hd->h_rdev));
924 arcn->sb.st_mtime = (time_t)(SHRT_EXT(hd->h_mtime_1));
925 arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) |
926 ((time_t)(SHRT_EXT(hd->h_mtime_2)));
927 arcn->sb.st_size = (off_t)(SHRT_EXT(hd->h_filesize_1));
928 arcn->sb.st_size = (arcn->sb.st_size << 16) |
929 ((off_t)(SHRT_EXT(hd->h_filesize_2)));
930 nsz = (int)(SHRT_EXT(hd->h_namesize));
932 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
935 * check the file name size, if bogus give up. otherwise read the file
940 arcn->nlen = nsz - 1;
941 if (rd_nm(arcn, nsz) < 0)
945 * header + file name are aligned to 2 byte boundries, skip if needed
947 if (rd_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)
951 * if not a link (or a file with no data), calculate pad size (for
952 * padding which follows the file data), clear the link name and return
954 if (((arcn->sb.st_mode & C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)){
956 * we have a valid header (not a link)
959 arcn->ln_name[0] = '\0';
960 arcn->pad = BCPIO_PAD(arcn->sb.st_size);
961 return(com_rd(arcn));
964 if ((rd_ln_nm(arcn) < 0) ||
965 (rd_skip((off_t)(BCPIO_PAD(arcn->sb.st_size))) < 0))
969 * we have a valid header (with a link)
971 return(com_rd(arcn));
976 * no cleanup needed here, just return size of the trailer (for append)
978 * size of trailer header in this format
984 return((off_t)(sizeof(HD_BCPIO) + sizeof(TRAILER) +
985 (BCPIO_PAD(sizeof(HD_BCPIO) + sizeof(TRAILER)))));
990 * copy the data in the ARCHD to buffer in old binary cpio format
991 * There is a real chance of field overflow with this critter. So we
992 * always check the conversion is ok. nobody in his their right mind
993 * should write an achive in this format...
995 * 0 if file has data to be written after the header, 1 if file has NO
996 * data to write after the header, -1 if archive write failed
1000 bcpio_wr(ARCHD *arcn)
1010 * check and repair truncated device and inode fields in the cpio
1013 if (map_dev(arcn, (u_long)BCPIO_MASK, (u_long)BCPIO_MASK) < 0)
1016 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
1017 arcn->sb.st_rdev = 0;
1020 switch(arcn->type) {
1025 * caller will copy file data to the archive. tell him how
1028 arcn->pad = BCPIO_PAD(arcn->sb.st_size);
1029 hd->h_filesize_1[0] = CHR_WR_0(arcn->sb.st_size);
1030 hd->h_filesize_1[1] = CHR_WR_1(arcn->sb.st_size);
1031 hd->h_filesize_2[0] = CHR_WR_2(arcn->sb.st_size);
1032 hd->h_filesize_2[1] = CHR_WR_3(arcn->sb.st_size);
1033 t_offt = (off_t)(SHRT_EXT(hd->h_filesize_1));
1034 t_offt = (t_offt<<16) | ((off_t)(SHRT_EXT(hd->h_filesize_2)));
1035 if (arcn->sb.st_size != t_offt) {
1036 paxwarn(1,"File is too large for bcpio format %s",
1043 * no file data for the caller to process, the file data has
1044 * the size of the link
1047 hd->h_filesize_1[0] = CHR_WR_0(arcn->ln_nlen);
1048 hd->h_filesize_1[1] = CHR_WR_1(arcn->ln_nlen);
1049 hd->h_filesize_2[0] = CHR_WR_2(arcn->ln_nlen);
1050 hd->h_filesize_2[1] = CHR_WR_3(arcn->ln_nlen);
1051 t_int = (int)(SHRT_EXT(hd->h_filesize_1));
1052 t_int = (t_int << 16) | ((int)(SHRT_EXT(hd->h_filesize_2)));
1053 if (arcn->ln_nlen != t_int)
1058 * no file data for the caller to process
1061 hd->h_filesize_1[0] = (char)0;
1062 hd->h_filesize_1[1] = (char)0;
1063 hd->h_filesize_2[0] = (char)0;
1064 hd->h_filesize_2[1] = (char)0;
1069 * build up the rest of the fields
1071 hd->h_magic[0] = CHR_WR_2(MAGIC);
1072 hd->h_magic[1] = CHR_WR_3(MAGIC);
1073 hd->h_dev[0] = CHR_WR_2(arcn->sb.st_dev);
1074 hd->h_dev[1] = CHR_WR_3(arcn->sb.st_dev);
1075 if (arcn->sb.st_dev != (dev_t)(SHRT_EXT(hd->h_dev)))
1077 hd->h_ino[0] = CHR_WR_2(arcn->sb.st_ino);
1078 hd->h_ino[1] = CHR_WR_3(arcn->sb.st_ino);
1079 if (arcn->sb.st_ino != (ino_t)(SHRT_EXT(hd->h_ino)))
1081 hd->h_mode[0] = CHR_WR_2(arcn->sb.st_mode);
1082 hd->h_mode[1] = CHR_WR_3(arcn->sb.st_mode);
1083 if (arcn->sb.st_mode != (mode_t)(SHRT_EXT(hd->h_mode)))
1085 hd->h_uid[0] = CHR_WR_2(arcn->sb.st_uid);
1086 hd->h_uid[1] = CHR_WR_3(arcn->sb.st_uid);
1087 if (arcn->sb.st_uid != (uid_t)(SHRT_EXT(hd->h_uid)))
1089 hd->h_gid[0] = CHR_WR_2(arcn->sb.st_gid);
1090 hd->h_gid[1] = CHR_WR_3(arcn->sb.st_gid);
1091 if (arcn->sb.st_gid != (gid_t)(SHRT_EXT(hd->h_gid)))
1093 hd->h_nlink[0] = CHR_WR_2(arcn->sb.st_nlink);
1094 hd->h_nlink[1] = CHR_WR_3(arcn->sb.st_nlink);
1095 if (arcn->sb.st_nlink != (nlink_t)(SHRT_EXT(hd->h_nlink)))
1097 hd->h_rdev[0] = CHR_WR_2(arcn->sb.st_rdev);
1098 hd->h_rdev[1] = CHR_WR_3(arcn->sb.st_rdev);
1099 if (arcn->sb.st_rdev != (dev_t)(SHRT_EXT(hd->h_rdev)))
1101 hd->h_mtime_1[0] = CHR_WR_0(arcn->sb.st_mtime);
1102 hd->h_mtime_1[1] = CHR_WR_1(arcn->sb.st_mtime);
1103 hd->h_mtime_2[0] = CHR_WR_2(arcn->sb.st_mtime);
1104 hd->h_mtime_2[1] = CHR_WR_3(arcn->sb.st_mtime);
1105 t_timet = (time_t)(SHRT_EXT(hd->h_mtime_1));
1106 t_timet = (t_timet << 16) | ((time_t)(SHRT_EXT(hd->h_mtime_2)));
1107 if (arcn->sb.st_mtime != t_timet)
1109 nsz = arcn->nlen + 1;
1110 hd->h_namesize[0] = CHR_WR_2(nsz);
1111 hd->h_namesize[1] = CHR_WR_3(nsz);
1112 if (nsz != (int)(SHRT_EXT(hd->h_namesize)))
1116 * write the header, the file name and padding as required.
1118 if ((wr_rdbuf((char *)&hdblk, (int)sizeof(HD_BCPIO)) < 0) ||
1119 (wr_rdbuf(arcn->name, nsz) < 0) ||
1120 (wr_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)) {
1121 paxwarn(1, "Could not write bcpio header for %s", arcn->org_name);
1126 * if we have file data, tell the caller we are done
1128 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
1129 (arcn->type == PAX_HRG))
1133 * if we are not a link, tell the caller we are done, go to next file
1135 if (arcn->type != PAX_SLK)
1139 * write the link name, tell the caller we are done.
1141 if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
1142 (wr_skip((off_t)(BCPIO_PAD(arcn->ln_nlen))) < 0)) {
1143 paxwarn(1,"Could not write bcpio link name for %s",arcn->org_name);
1150 * header field is out of range
1152 paxwarn(1,"Bcpio header field is too small for file %s", arcn->org_name);