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 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 static char sccsid[] = "@(#)cpio.c 8.1 (Berkeley) 5/31/93";
42 static const char rcsid[] =
43 "$FreeBSD: src/bin/pax/cpio.c,v 1.12.2.1 2001/08/01 05:03:11 obrien Exp $";
46 #include <sys/types.h>
57 static int rd_nm __P((register ARCHD *, int));
58 static int rd_ln_nm __P((register ARCHD *));
59 static int com_rd __P((register ARCHD *));
62 * Routines which support the different cpio versions
65 static int swp_head; /* binary cpio header byte swap */
68 * Routines common to all versions of cpio
73 * Fire up the hard link detection code
75 * 0 if ok -1 otherwise (the return values of lnk_start())
91 * Called to determine if a header block is a valid trailer. We are
92 * passed the block, the in_sync flag (which tells us we are in resync
93 * mode; looking for a valid header), and cnt (which starts at zero)
94 * which is used to count the number of empty blocks we have seen so far.
96 * 0 if a valid trailer, -1 if not a valid trailer,
101 cpio_trail(register ARCHD *arcn)
105 register ARCHD *arcn;
109 * look for trailer id in file we are about to process
111 if ((strcmp(arcn->name, TRAILER) == 0) && (arcn->sb.st_size == 0))
118 * operations common to all cpio read functions.
125 com_rd(register ARCHD *arcn)
129 register ARCHD *arcn;
134 arcn->org_name = arcn->name;
135 switch(arcn->sb.st_mode & C_IFMT) {
137 arcn->type = PAX_FIF;
140 arcn->type = PAX_DIR;
143 arcn->type = PAX_BLK;
146 arcn->type = PAX_CHR;
149 arcn->type = PAX_SLK;
152 arcn->type = PAX_SCK;
158 * we have file data, set up skip (pad is set in the format
161 arcn->sb.st_mode = (arcn->sb.st_mode & 0xfff) | C_ISREG;
162 arcn->type = PAX_REG;
163 arcn->skip = arcn->sb.st_size;
166 if (chk_lnk(arcn) < 0)
173 * write the special file with the name trailer in the proper format
175 * result of the write of the trailer from the cpio specific write func
189 * create a trailer request and call the proper format write function
191 memset(&last, 0, sizeof(last));
192 last.nlen = sizeof(TRAILER) - 1;
194 last.sb.st_nlink = 1;
195 (void)strcpy(last.name, TRAILER);
196 return((*frmt->wr)(&last));
201 * read in the file name which follows the cpio header
203 * 0 if ok, -1 otherwise
208 rd_nm(register ARCHD *arcn, int nsz)
212 register ARCHD *arcn;
217 * do not even try bogus values
219 if ((nsz == 0) || (nsz > sizeof(arcn->name))) {
220 paxwarn(1, "Cpio file name length %d is out of range", nsz);
225 * read the name and make sure it is not empty and is \0 terminated
227 if ((rd_wrbuf(arcn->name,nsz) != nsz) || (arcn->name[nsz-1] != '\0') ||
228 (arcn->name[0] == '\0')) {
229 paxwarn(1, "Cpio file name in header is corrupted");
237 * read in the link name for a file with links. The link name is stored
238 * like file data (and is NOT \0 terminated!)
240 * 0 if ok, -1 otherwise
245 rd_ln_nm(register ARCHD *arcn)
249 register ARCHD *arcn;
253 * check the length specified for bogus values
255 if ((arcn->sb.st_size == 0) ||
256 (arcn->sb.st_size >= sizeof(arcn->ln_name))) {
258 paxwarn(1, "Cpio link name length is invalid: %lu",
261 paxwarn(1, "Cpio link name length is invalid: %qu",
268 * read in the link name and \0 terminate it
270 if (rd_wrbuf(arcn->ln_name, (int)arcn->sb.st_size) !=
271 (int)arcn->sb.st_size) {
272 paxwarn(1, "Cpio link name read error");
275 arcn->ln_nlen = arcn->sb.st_size;
276 arcn->ln_name[arcn->ln_nlen] = '\0';
279 * watch out for those empty link names
281 if (arcn->ln_name[0] == '\0') {
282 paxwarn(1, "Cpio link name is corrupt");
289 * Routines common to the extended byte oriented cpio format
294 * determine if a block given to us is a valid extended byte oriented
297 * 0 if a valid header, -1 otherwise
302 cpio_id(char *blk, int size)
310 if ((size < sizeof(HD_CPIO)) ||
311 (strncmp(blk, AMAGIC, sizeof(AMAGIC) - 1) != 0))
318 * determine if a buffer is a byte oriented extended cpio archive entry.
319 * convert and store the values in the ARCHD parameter.
321 * 0 if a valid header, -1 otherwise.
326 cpio_rd(register ARCHD *arcn, register char *buf)
330 register ARCHD *arcn;
335 register HD_CPIO *hd;
338 * check that this is a valid header, if not return -1
340 if (cpio_id(buf, sizeof(HD_CPIO)) < 0)
345 * byte oriented cpio (posix) does not have padding! extract the octal
346 * ascii fields from the header
349 arcn->sb.st_dev = (dev_t)asc_ul(hd->c_dev, sizeof(hd->c_dev), OCT);
350 arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), OCT);
351 arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), OCT);
352 arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), OCT);
353 arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), OCT);
354 arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink),
356 arcn->sb.st_rdev = (dev_t)asc_ul(hd->c_rdev, sizeof(hd->c_rdev), OCT);
357 arcn->sb.st_mtime = (time_t)asc_ul(hd->c_mtime, sizeof(hd->c_mtime),
359 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
361 arcn->sb.st_size = (off_t)asc_ul(hd->c_filesize,sizeof(hd->c_filesize),
364 arcn->sb.st_size = (off_t)asc_uqd(hd->c_filesize,sizeof(hd->c_filesize),
369 * check name size and if valid, read in the name of this entry (name
370 * follows header in the archive)
372 if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),OCT)) < 2)
374 arcn->nlen = nsz - 1;
375 if (rd_nm(arcn, nsz) < 0)
378 if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
380 * no link name to read for this file
383 arcn->ln_name[0] = '\0';
384 return(com_rd(arcn));
388 * check link name size and read in the link name. Link names are
389 * stored like file data.
391 if (rd_ln_nm(arcn) < 0)
395 * we have a valid header (with a link)
397 return(com_rd(arcn));
402 * no cleanup needed here, just return size of the trailer (for append)
404 * size of trailer header in this format
415 return((off_t)(sizeof(HD_CPIO) + sizeof(TRAILER)));
420 * start up the device mapping table
422 * 0 if ok, -1 otherwise (what dev_start() returns)
438 * copy the data in the ARCHD to buffer in extended byte oriented cpio
441 * 0 if file has data to be written after the header, 1 if file has NO
442 * data to write after the header, -1 if archive write failed
447 cpio_wr(register ARCHD *arcn)
451 register ARCHD *arcn;
454 register HD_CPIO *hd;
456 char hdblk[sizeof(HD_CPIO)];
459 * check and repair truncated device and inode fields in the header
461 if (map_dev(arcn, (u_long)CPIO_MASK, (u_long)CPIO_MASK) < 0)
465 nsz = arcn->nlen + 1;
466 hd = (HD_CPIO *)hdblk;
467 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
468 arcn->sb.st_rdev = 0;
475 * set data size for file data
478 if (ul_asc((u_long)arcn->sb.st_size, hd->c_filesize,
479 sizeof(hd->c_filesize), OCT)) {
481 if (uqd_asc((u_quad_t)arcn->sb.st_size, hd->c_filesize,
482 sizeof(hd->c_filesize), OCT)) {
484 paxwarn(1,"File is too large for cpio format %s",
491 * set data size to hold link name
493 if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,
494 sizeof(hd->c_filesize), OCT))
499 * all other file types have no file data
501 if (ul_asc((u_long)0, hd->c_filesize, sizeof(hd->c_filesize),
508 * copy the values to the header using octal ascii
510 if (ul_asc((u_long)MAGIC, hd->c_magic, sizeof(hd->c_magic), OCT) ||
511 ul_asc((u_long)arcn->sb.st_dev, hd->c_dev, sizeof(hd->c_dev),
513 ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),
515 ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
517 ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),
519 ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),
521 ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
523 ul_asc((u_long)arcn->sb.st_rdev, hd->c_rdev, sizeof(hd->c_rdev),
525 ul_asc((u_long)arcn->sb.st_mtime,hd->c_mtime,sizeof(hd->c_mtime),
527 ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), OCT))
531 * write the file name to the archive
533 if ((wr_rdbuf(hdblk, (int)sizeof(HD_CPIO)) < 0) ||
534 (wr_rdbuf(arcn->name, nsz) < 0)) {
535 paxwarn(1, "Unable to write cpio header for %s", arcn->org_name);
540 * if this file has data, we are done. The caller will write the file
541 * data, if we are link tell caller we are done, go to next file
543 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
544 (arcn->type == PAX_HRG))
546 if (arcn->type != PAX_SLK)
550 * write the link name to the archive, tell the caller to go to the
551 * next file as we are done.
553 if (wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) {
554 paxwarn(1,"Unable to write cpio link name for %s",arcn->org_name);
561 * header field is out of range
563 paxwarn(1, "Cpio header field is too small to store file %s",
569 * Routines common to the system VR4 version of cpio (with/without file CRC)
574 * determine if a block given to us is a valid system VR4 cpio header
575 * WITHOUT crc. WATCH it the magic cookies are in OCTAL, the header
578 * 0 if a valid header, -1 otherwise
583 vcpio_id(char *blk, int size)
591 if ((size < sizeof(HD_VCPIO)) ||
592 (strncmp(blk, AVMAGIC, sizeof(AVMAGIC) - 1) != 0))
599 * determine if a block given to us is a valid system VR4 cpio header
600 * WITH crc. WATCH it the magic cookies are in OCTAL the header uses HEX
602 * 0 if a valid header, -1 otherwise
607 crc_id(char *blk, int size)
615 if ((size < sizeof(HD_VCPIO)) ||
616 (strncmp(blk, AVCMAGIC, sizeof(AVCMAGIC) - 1) != 0))
623 w set file data CRC calculations. Fire up the hard link detection code
625 * 0 if ok -1 otherwise (the return values of lnk_start())
642 * determine if a buffer is a system VR4 archive entry. (with/without CRC)
643 * convert and store the values in the ARCHD parameter.
645 * 0 if a valid header, -1 otherwise.
650 vcpio_rd(register ARCHD *arcn, register char *buf)
654 register ARCHD *arcn;
658 register HD_VCPIO *hd;
664 * during the id phase it was determined if we were using CRC, use the
668 if (crc_id(buf, sizeof(HD_VCPIO)) < 0)
671 if (vcpio_id(buf, sizeof(HD_VCPIO)) < 0)
675 hd = (HD_VCPIO *)buf;
679 * extract the hex ascii fields from the header
681 arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), HEX);
682 arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), HEX);
683 arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), HEX);
684 arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), HEX);
685 arcn->sb.st_mtime = (time_t)asc_ul(hd->c_mtime,sizeof(hd->c_mtime),HEX);
686 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
688 arcn->sb.st_size = (off_t)asc_ul(hd->c_filesize,
689 sizeof(hd->c_filesize), HEX);
691 arcn->sb.st_size = (off_t)asc_uqd(hd->c_filesize,
692 sizeof(hd->c_filesize), HEX);
694 arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink),
696 devmajor = (dev_t)asc_ul(hd->c_maj, sizeof(hd->c_maj), HEX);
697 devminor = (dev_t)asc_ul(hd->c_min, sizeof(hd->c_min), HEX);
698 arcn->sb.st_dev = TODEV(devmajor, devminor);
699 devmajor = (dev_t)asc_ul(hd->c_rmaj, sizeof(hd->c_maj), HEX);
700 devminor = (dev_t)asc_ul(hd->c_rmin, sizeof(hd->c_min), HEX);
701 arcn->sb.st_rdev = TODEV(devmajor, devminor);
702 arcn->crc = asc_ul(hd->c_chksum, sizeof(hd->c_chksum), HEX);
705 * check the length of the file name, if ok read it in, return -1 if
708 if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),HEX)) < 2)
710 arcn->nlen = nsz - 1;
711 if (rd_nm(arcn, nsz) < 0)
715 * skip padding. header + filename is aligned to 4 byte boundries
717 if (rd_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)
721 * if not a link (or a file with no data), calculate pad size (for
722 * padding which follows the file data), clear the link name and return
724 if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
726 * we have a valid header (not a link)
729 arcn->ln_name[0] = '\0';
730 arcn->pad = VCPIO_PAD(arcn->sb.st_size);
731 return(com_rd(arcn));
735 * read in the link name and skip over the padding
737 if ((rd_ln_nm(arcn) < 0) ||
738 (rd_skip((off_t)(VCPIO_PAD(arcn->sb.st_size))) < 0))
742 * we have a valid header (with a link)
744 return(com_rd(arcn));
749 * no cleanup needed here, just return size of the trailer (for append)
751 * size of trailer header in this format
762 return((off_t)(sizeof(HD_VCPIO) + sizeof(TRAILER) +
763 (VCPIO_PAD(sizeof(HD_VCPIO) + sizeof(TRAILER)))));
768 * start up the device mapping table, enable crc file calculation
770 * 0 if ok, -1 otherwise (what dev_start() returns)
787 * copy the data in the ARCHD to buffer in system VR4 cpio
788 * (with/without crc) format.
790 * 0 if file has data to be written after the header, 1 if file has
791 * NO data to write after the header, -1 if archive write failed
796 vcpio_wr(register ARCHD *arcn)
800 register ARCHD *arcn;
803 register HD_VCPIO *hd;
805 char hdblk[sizeof(HD_VCPIO)];
808 * check and repair truncated device and inode fields in the cpio
811 if (map_dev(arcn, (u_long)VCPIO_MASK, (u_long)VCPIO_MASK) < 0)
813 nsz = arcn->nlen + 1;
814 hd = (HD_VCPIO *)hdblk;
815 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
816 arcn->sb.st_rdev = 0;
819 * add the proper magic value depending whether we were asked for
820 * file data crc's, and the crc if needed.
823 if (ul_asc((u_long)VCMAGIC, hd->c_magic, sizeof(hd->c_magic),
825 ul_asc((u_long)arcn->crc,hd->c_chksum,sizeof(hd->c_chksum),
829 if (ul_asc((u_long)VMAGIC, hd->c_magic, sizeof(hd->c_magic),
831 ul_asc((u_long)0L, hd->c_chksum, sizeof(hd->c_chksum),HEX))
840 * caller will copy file data to the archive. tell him how
843 arcn->pad = VCPIO_PAD(arcn->sb.st_size);
845 if (ul_asc((u_long)arcn->sb.st_size, hd->c_filesize,
846 sizeof(hd->c_filesize), HEX)) {
848 if (uqd_asc((u_quad_t)arcn->sb.st_size, hd->c_filesize,
849 sizeof(hd->c_filesize), HEX)) {
851 paxwarn(1,"File is too large for sv4cpio format %s",
858 * no file data for the caller to process, the file data has
859 * the size of the link
862 if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,
863 sizeof(hd->c_filesize), HEX))
868 * no file data for the caller to process
871 if (ul_asc((u_long)0L, hd->c_filesize, sizeof(hd->c_filesize),
878 * set the other fields in the header
880 if (ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),
882 ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
884 ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),
886 ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),
888 ul_asc((u_long)arcn->sb.st_mtime, hd->c_mtime, sizeof(hd->c_mtime),
890 ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
892 ul_asc((u_long)MAJOR(arcn->sb.st_dev),hd->c_maj, sizeof(hd->c_maj),
894 ul_asc((u_long)MINOR(arcn->sb.st_dev),hd->c_min, sizeof(hd->c_min),
896 ul_asc((u_long)MAJOR(arcn->sb.st_rdev),hd->c_rmaj,sizeof(hd->c_maj),
898 ul_asc((u_long)MINOR(arcn->sb.st_rdev),hd->c_rmin,sizeof(hd->c_min),
900 ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), HEX))
904 * write the header, the file name and padding as required.
906 if ((wr_rdbuf(hdblk, (int)sizeof(HD_VCPIO)) < 0) ||
907 (wr_rdbuf(arcn->name, (int)nsz) < 0) ||
908 (wr_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)) {
909 paxwarn(1,"Could not write sv4cpio header for %s",arcn->org_name);
914 * if we have file data, tell the caller we are done, copy the file
916 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
917 (arcn->type == PAX_HRG))
921 * if we are not a link, tell the caller we are done, go to next file
923 if (arcn->type != PAX_SLK)
927 * write the link name, tell the caller we are done.
929 if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
930 (wr_skip((off_t)(VCPIO_PAD(arcn->ln_nlen))) < 0)) {
931 paxwarn(1,"Could not write sv4cpio link name for %s",
939 * header field is out of range
941 paxwarn(1,"Sv4cpio header field is too small for file %s",arcn->org_name);
946 * Routines common to the old binary header cpio
951 * determine if a block given to us is a old binary cpio header
952 * (with/without header byte swapping)
954 * 0 if a valid header, -1 otherwise
959 bcpio_id(char *blk, int size)
967 if (size < sizeof(HD_BCPIO))
971 * check both normal and byte swapped magic cookies
973 if (((u_short)SHRT_EXT(blk)) == MAGIC)
975 if (((u_short)RSHRT_EXT(blk)) == MAGIC) {
985 * determine if a buffer is a old binary archive entry. (it may have byte
986 * swapped header) convert and store the values in the ARCHD parameter.
987 * This is a very old header format and should not really be used.
989 * 0 if a valid header, -1 otherwise.
994 bcpio_rd(register ARCHD *arcn, register char *buf)
998 register ARCHD *arcn;
1002 register HD_BCPIO *hd;
1008 if (bcpio_id(buf, sizeof(HD_BCPIO)) < 0)
1012 hd = (HD_BCPIO *)buf;
1015 * header has swapped bytes on 16 bit boundaries
1017 arcn->sb.st_dev = (dev_t)(RSHRT_EXT(hd->h_dev));
1018 arcn->sb.st_ino = (ino_t)(RSHRT_EXT(hd->h_ino));
1019 arcn->sb.st_mode = (mode_t)(RSHRT_EXT(hd->h_mode));
1020 arcn->sb.st_uid = (uid_t)(RSHRT_EXT(hd->h_uid));
1021 arcn->sb.st_gid = (gid_t)(RSHRT_EXT(hd->h_gid));
1022 arcn->sb.st_nlink = (nlink_t)(RSHRT_EXT(hd->h_nlink));
1023 arcn->sb.st_rdev = (dev_t)(RSHRT_EXT(hd->h_rdev));
1024 arcn->sb.st_mtime = (time_t)(RSHRT_EXT(hd->h_mtime_1));
1025 arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) |
1026 ((time_t)(RSHRT_EXT(hd->h_mtime_2)));
1027 arcn->sb.st_size = (off_t)(RSHRT_EXT(hd->h_filesize_1));
1028 arcn->sb.st_size = (arcn->sb.st_size << 16) |
1029 ((off_t)(RSHRT_EXT(hd->h_filesize_2)));
1030 nsz = (int)(RSHRT_EXT(hd->h_namesize));
1032 arcn->sb.st_dev = (dev_t)(SHRT_EXT(hd->h_dev));
1033 arcn->sb.st_ino = (ino_t)(SHRT_EXT(hd->h_ino));
1034 arcn->sb.st_mode = (mode_t)(SHRT_EXT(hd->h_mode));
1035 arcn->sb.st_uid = (uid_t)(SHRT_EXT(hd->h_uid));
1036 arcn->sb.st_gid = (gid_t)(SHRT_EXT(hd->h_gid));
1037 arcn->sb.st_nlink = (nlink_t)(SHRT_EXT(hd->h_nlink));
1038 arcn->sb.st_rdev = (dev_t)(SHRT_EXT(hd->h_rdev));
1039 arcn->sb.st_mtime = (time_t)(SHRT_EXT(hd->h_mtime_1));
1040 arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) |
1041 ((time_t)(SHRT_EXT(hd->h_mtime_2)));
1042 arcn->sb.st_size = (off_t)(SHRT_EXT(hd->h_filesize_1));
1043 arcn->sb.st_size = (arcn->sb.st_size << 16) |
1044 ((off_t)(SHRT_EXT(hd->h_filesize_2)));
1045 nsz = (int)(SHRT_EXT(hd->h_namesize));
1047 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
1050 * check the file name size, if bogus give up. otherwise read the file
1055 arcn->nlen = nsz - 1;
1056 if (rd_nm(arcn, nsz) < 0)
1060 * header + file name are aligned to 2 byte boundries, skip if needed
1062 if (rd_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)
1066 * if not a link (or a file with no data), calculate pad size (for
1067 * padding which follows the file data), clear the link name and return
1069 if (((arcn->sb.st_mode & C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)){
1071 * we have a valid header (not a link)
1074 arcn->ln_name[0] = '\0';
1075 arcn->pad = BCPIO_PAD(arcn->sb.st_size);
1076 return(com_rd(arcn));
1079 if ((rd_ln_nm(arcn) < 0) ||
1080 (rd_skip((off_t)(BCPIO_PAD(arcn->sb.st_size))) < 0))
1084 * we have a valid header (with a link)
1086 return(com_rd(arcn));
1091 * no cleanup needed here, just return size of the trailer (for append)
1093 * size of trailer header in this format
1104 return((off_t)(sizeof(HD_BCPIO) + sizeof(TRAILER) +
1105 (BCPIO_PAD(sizeof(HD_BCPIO) + sizeof(TRAILER)))));
1110 * copy the data in the ARCHD to buffer in old binary cpio format
1111 * There is a real chance of field overflow with this critter. So we
1112 * always check the conversion is ok. nobody in his their right mind
1113 * should write an achive in this format...
1115 * 0 if file has data to be written after the header, 1 if file has NO
1116 * data to write after the header, -1 if archive write failed
1121 bcpio_wr(register ARCHD *arcn)
1125 register ARCHD *arcn;
1128 register HD_BCPIO *hd;
1130 char hdblk[sizeof(HD_BCPIO)];
1136 * check and repair truncated device and inode fields in the cpio
1139 if (map_dev(arcn, (u_long)BCPIO_MASK, (u_long)BCPIO_MASK) < 0)
1142 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
1143 arcn->sb.st_rdev = 0;
1144 hd = (HD_BCPIO *)hdblk;
1146 switch(arcn->type) {
1151 * caller will copy file data to the archive. tell him how
1154 arcn->pad = BCPIO_PAD(arcn->sb.st_size);
1155 hd->h_filesize_1[0] = CHR_WR_0(arcn->sb.st_size);
1156 hd->h_filesize_1[1] = CHR_WR_1(arcn->sb.st_size);
1157 hd->h_filesize_2[0] = CHR_WR_2(arcn->sb.st_size);
1158 hd->h_filesize_2[1] = CHR_WR_3(arcn->sb.st_size);
1159 t_offt = (off_t)(SHRT_EXT(hd->h_filesize_1));
1160 t_offt = (t_offt<<16) | ((off_t)(SHRT_EXT(hd->h_filesize_2)));
1161 if (arcn->sb.st_size != t_offt) {
1162 paxwarn(1,"File is too large for bcpio format %s",
1169 * no file data for the caller to process, the file data has
1170 * the size of the link
1173 hd->h_filesize_1[0] = CHR_WR_0(arcn->ln_nlen);
1174 hd->h_filesize_1[1] = CHR_WR_1(arcn->ln_nlen);
1175 hd->h_filesize_2[0] = CHR_WR_2(arcn->ln_nlen);
1176 hd->h_filesize_2[1] = CHR_WR_3(arcn->ln_nlen);
1177 t_int = (int)(SHRT_EXT(hd->h_filesize_1));
1178 t_int = (t_int << 16) | ((int)(SHRT_EXT(hd->h_filesize_2)));
1179 if (arcn->ln_nlen != t_int)
1184 * no file data for the caller to process
1187 hd->h_filesize_1[0] = (char)0;
1188 hd->h_filesize_1[1] = (char)0;
1189 hd->h_filesize_2[0] = (char)0;
1190 hd->h_filesize_2[1] = (char)0;
1195 * build up the rest of the fields
1197 hd->h_magic[0] = CHR_WR_2(MAGIC);
1198 hd->h_magic[1] = CHR_WR_3(MAGIC);
1199 hd->h_dev[0] = CHR_WR_2(arcn->sb.st_dev);
1200 hd->h_dev[1] = CHR_WR_3(arcn->sb.st_dev);
1201 if (arcn->sb.st_dev != (dev_t)(SHRT_EXT(hd->h_dev)))
1203 hd->h_ino[0] = CHR_WR_2(arcn->sb.st_ino);
1204 hd->h_ino[1] = CHR_WR_3(arcn->sb.st_ino);
1205 if (arcn->sb.st_ino != (ino_t)(SHRT_EXT(hd->h_ino)))
1207 hd->h_mode[0] = CHR_WR_2(arcn->sb.st_mode);
1208 hd->h_mode[1] = CHR_WR_3(arcn->sb.st_mode);
1209 if (arcn->sb.st_mode != (mode_t)(SHRT_EXT(hd->h_mode)))
1211 hd->h_uid[0] = CHR_WR_2(arcn->sb.st_uid);
1212 hd->h_uid[1] = CHR_WR_3(arcn->sb.st_uid);
1213 if (arcn->sb.st_uid != (uid_t)(SHRT_EXT(hd->h_uid)))
1215 hd->h_gid[0] = CHR_WR_2(arcn->sb.st_gid);
1216 hd->h_gid[1] = CHR_WR_3(arcn->sb.st_gid);
1217 if (arcn->sb.st_gid != (gid_t)(SHRT_EXT(hd->h_gid)))
1219 hd->h_nlink[0] = CHR_WR_2(arcn->sb.st_nlink);
1220 hd->h_nlink[1] = CHR_WR_3(arcn->sb.st_nlink);
1221 if (arcn->sb.st_nlink != (nlink_t)(SHRT_EXT(hd->h_nlink)))
1223 hd->h_rdev[0] = CHR_WR_2(arcn->sb.st_rdev);
1224 hd->h_rdev[1] = CHR_WR_3(arcn->sb.st_rdev);
1225 if (arcn->sb.st_rdev != (dev_t)(SHRT_EXT(hd->h_rdev)))
1227 hd->h_mtime_1[0] = CHR_WR_0(arcn->sb.st_mtime);
1228 hd->h_mtime_1[1] = CHR_WR_1(arcn->sb.st_mtime);
1229 hd->h_mtime_2[0] = CHR_WR_2(arcn->sb.st_mtime);
1230 hd->h_mtime_2[1] = CHR_WR_3(arcn->sb.st_mtime);
1231 t_timet = (time_t)(SHRT_EXT(hd->h_mtime_1));
1232 t_timet = (t_timet << 16) | ((time_t)(SHRT_EXT(hd->h_mtime_2)));
1233 if (arcn->sb.st_mtime != t_timet)
1235 nsz = arcn->nlen + 1;
1236 hd->h_namesize[0] = CHR_WR_2(nsz);
1237 hd->h_namesize[1] = CHR_WR_3(nsz);
1238 if (nsz != (int)(SHRT_EXT(hd->h_namesize)))
1242 * write the header, the file name and padding as required.
1244 if ((wr_rdbuf(hdblk, (int)sizeof(HD_BCPIO)) < 0) ||
1245 (wr_rdbuf(arcn->name, nsz) < 0) ||
1246 (wr_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)) {
1247 paxwarn(1, "Could not write bcpio header for %s", arcn->org_name);
1252 * if we have file data, tell the caller we are done
1254 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
1255 (arcn->type == PAX_HRG))
1259 * if we are not a link, tell the caller we are done, go to next file
1261 if (arcn->type != PAX_SLK)
1265 * write the link name, tell the caller we are done.
1267 if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
1268 (wr_skip((off_t)(BCPIO_PAD(arcn->ln_nlen))) < 0)) {
1269 paxwarn(1,"Could not write bcpio link name for %s",arcn->org_name);
1276 * header field is out of range
1278 paxwarn(1,"Bcpio header field is too small for file %s", arcn->org_name);